diff --git a/src/lib/request.ts b/src/lib/request.ts index f5a5454..2ff279d 100644 --- a/src/lib/request.ts +++ b/src/lib/request.ts @@ -3,7 +3,7 @@ import { Partial, getLocalStorageItem } from '@lib/helper' import { isClashX } from '@lib/jsBridge' import { rootStores } from '@lib/createStore' -let instance: Request +let instance: AxiosInstance export interface Config { port: number @@ -30,70 +30,84 @@ export interface Proxy { all?: string[] } -export class Request { - protected instance: AxiosInstance - - constructor (host: string, secret?: string) { - this.instance = axios.create({ - baseURL: host, - headers: secret ? { Authorization: `Bearer ${secret}` } : {} - }) - } - - getConfig () { - return this.instance.get('configs') - } - - updateConfig (config: Partial) { - return this.instance.put('configs', config) - } - - getRules () { - return this.instance.get('rules') - } - - updateRules () { - return this.instance.put('rules') - } - - getProxies () { - return this.instance.get('proxies') - } - - getProxy (name: string) { - return this.instance.get('proxies/:name', { params: { name } }) - } - - getProxyDelay (name: string) { - return this.instance.get<{ delay: number }>('proxies/:name/delay', { params: { name } }) - } - - changeProxySelected (name: string, select: string) { - return this.instance.get('proxies/:name', { params: { name }, data: { name: select } }) - } +export async function getConfig () { + const req = await getInstance() + return req.get('configs') } -export async function Instance () { +export async function updateConfig (config: Partial) { + const req = await getInstance() + return req.put('configs', config) +} + +export async function getRules () { + const req = await getInstance() + return req.get('rules') +} + +export async function updateRules () { + const req = await getInstance() + return req.put('rules') +} + +export async function getProxies () { + const req = await getInstance() + return req.get('proxies') +} + +export async function getProxy (name: string) { + const req = await getInstance() + return req.get('proxies/:name', { params: { name } }) +} + +export async function getProxyDelay (name: string) { + const req = await getInstance() + return req.get<{ delay: number }>('proxies/:name/delay', { params: { name } }) +} + +export async function changeProxySelected (name: string, select: string) { + const req = await getInstance() + return req.get('proxies/:name', { params: { name }, data: { name: select } }) +} + +export async function getInstance () { if (instance) { return instance } + const { + hostname, + port, + secret + } = await getExternalControllerConfig() + + instance = axios.create({ + baseURL: `http://${hostname}:${port}`, + headers: { Authorization: `Bearer ${secret}` } + }) + + return instance +} + +export async function getExternalControllerConfig () { if (isClashX()) { await rootStores.config.fetchAndParseConfig() const general = rootStores.config.config.general - instance = new Request( - `http://${general.externalControllerAddr}:${general.externalControllerPort}`, - general.secret - ) - return instance + + return { + hostname: general.externalControllerAddr, + port: general.externalControllerPort, + secret: general.secret + } } const hostname = getLocalStorageItem('externalControllerAddr', '') const port = getLocalStorageItem('externalControllerPort', '') const secret = getLocalStorageItem('secret', '') + if (!hostname || !port) { throw new Error('can\'t get hostname or port') } - instance = new Request(`http://${hostname}:${port}`, secret) - return instance + + return { hostname, port, secret } }