chore: adjust code

This commit is contained in:
GyDi 2022-12-15 12:22:20 +08:00
parent 6e421e60c5
commit d1ba0ed2b2
No known key found for this signature in database
GPG Key ID: 9C3AD40F1F99880A

View File

@ -2,14 +2,15 @@ import axios, { AxiosInstance } from "axios";
import { getClashInfo } from "./cmds"; import { getClashInfo } from "./cmds";
let axiosIns: AxiosInstance = null!; let axiosIns: AxiosInstance = null!;
let server = "";
let secret = "";
/// initialize some information /// initialize some information
/// enable force update axiosIns /// enable force update axiosIns
export async function getAxios(force: boolean = false) { export const getAxios = async (force: boolean = false) => {
if (axiosIns && !force) return axiosIns; if (axiosIns && !force) return axiosIns;
let server = "";
let secret = "";
try { try {
const info = await getClashInfo(); const info = await getClashInfo();
@ -29,73 +30,66 @@ export async function getAxios(force: boolean = false) {
}); });
axiosIns.interceptors.response.use((r) => r.data); axiosIns.interceptors.response.use((r) => r.data);
return axiosIns; return axiosIns;
} };
/// get information
export async function getInformation() {
if (server) return { server, secret };
const info = await getClashInfo();
return info!;
}
/// Get Version /// Get Version
export async function getVersion() { export const getVersion = async () => {
const instance = await getAxios(); const instance = await getAxios();
return instance.get("/version") as Promise<{ return instance.get("/version") as Promise<{
premium: boolean; premium: boolean;
meta?: boolean; meta?: boolean;
version: string; version: string;
}>; }>;
} };
/// Get current base configs /// Get current base configs
export async function getClashConfig() { export const getClashConfig = async () => {
const instance = await getAxios(); const instance = await getAxios();
return instance.get("/configs") as Promise<IConfigData>; return instance.get("/configs") as Promise<IConfigData>;
} };
/// Update current configs /// Update current configs
export async function updateConfigs(config: Partial<IConfigData>) { export const updateConfigs = async (config: Partial<IConfigData>) => {
const instance = await getAxios(); const instance = await getAxios();
return instance.patch("/configs", config); return instance.patch("/configs", config);
} };
/// Get current rules /// Get current rules
export async function getRules() { export const getRules = async () => {
const instance = await getAxios(); const instance = await getAxios();
const response = await instance.get<any, any>("/rules"); const response = await instance.get<any, any>("/rules");
return response?.rules as IRuleItem[]; return response?.rules as IRuleItem[];
}
/// Get Proxy delay
export async function getProxyDelay(
name: string,
url?: string
): Promise<{ delay: number }> {
const params = {
timeout: 3000,
url: url || "http://www.gstatic.com/generate_204",
}; };
/// Get Proxy delay
export const getProxyDelay = async (name: string, url?: string) => {
const params = {
timeout: 5000,
url: url || "http://www.gstatic.com/generate_204",
};
const instance = await getAxios(); const instance = await getAxios();
return instance.get(`/proxies/${encodeURIComponent(name)}/delay`, { params }); const result = await instance.get(
} `/proxies/${encodeURIComponent(name)}/delay`,
{ params }
);
return result as any as { delay: number };
};
/// Update the Proxy Choose /// Update the Proxy Choose
export async function updateProxy(group: string, proxy: string) { export const updateProxy = async (group: string, proxy: string) => {
const instance = await getAxios(); const instance = await getAxios();
return instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy }); return instance.put(`/proxies/${encodeURIComponent(group)}`, { name: proxy });
} };
// get proxy // get proxy
async function getProxiesInner() { export const getProxiesInner = async () => {
const instance = await getAxios(); const instance = await getAxios();
const response = await instance.get<any, any>("/proxies"); const response = await instance.get<any, any>("/proxies");
return (response?.proxies || {}) as Record<string, IProxyItem>; return (response?.proxies || {}) as Record<string, IProxyItem>;
} };
/// Get the Proxy information /// Get the Proxy information
export async function getProxies() { export const getProxies = async () => {
const [proxyRecord, providerRecord] = await Promise.all([ const [proxyRecord, providerRecord] = await Promise.all([
getProxiesInner(), getProxiesInner(),
getProviders(), getProviders(),
@ -149,10 +143,10 @@ export async function getProxies() {
}; };
return { global: _global, direct, groups, records: proxyRecord, proxies }; return { global: _global, direct, groups, records: proxyRecord, proxies };
} };
// get proxy providers // get proxy providers
export async function getProviders() { export const getProviders = async () => {
const instance = await getAxios(); const instance = await getAxios();
const response = await instance.get<any, any>("/providers/proxies"); const response = await instance.get<any, any>("/providers/proxies");
@ -164,30 +158,30 @@ export async function getProviders() {
return type === "http" || type === "file"; return type === "http" || type === "file";
}) })
); );
} };
// proxy providers health check // proxy providers health check
export async function providerHealthCheck(name: string) { export const providerHealthCheck = async (name: string) => {
const instance = await getAxios(); const instance = await getAxios();
return instance.get( return instance.get(
`/providers/proxies/${encodeURIComponent(name)}/healthcheck` `/providers/proxies/${encodeURIComponent(name)}/healthcheck`
); );
} };
export async function getConnections() { export const getConnections = async () => {
const instance = await getAxios(); const instance = await getAxios();
const result = await instance.get("/connections"); const result = await instance.get("/connections");
return result as any as IConnections; return result as any as IConnections;
} };
// Close specific connection // Close specific connection
export async function deleteConnection(id: string) { export const deleteConnection = async (id: string) => {
const instance = await getAxios(); const instance = await getAxios();
await instance.delete<any, any>(`/connections/${encodeURIComponent(id)}`); await instance.delete<any, any>(`/connections/${encodeURIComponent(id)}`);
} };
// Close all connections // Close all connections
export async function closeAllConnections() { export const closeAllConnections = async () => {
const instance = await getAxios(); const instance = await getAxios();
await instance.delete<any, any>(`/connections`); await instance.delete<any, any>(`/connections`);
} };