mirror of
https://github.com/woodchen-ink/clash-and-dashboard.git
synced 2025-07-18 14:01:56 +08:00
Refactor: request lib
This commit is contained in:
parent
44a1fdf449
commit
68adf9e553
@ -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<Config>('configs')
|
||||
}
|
||||
|
||||
updateConfig (config: Partial<Config>) {
|
||||
return this.instance.put<void>('configs', config)
|
||||
}
|
||||
|
||||
getRules () {
|
||||
return this.instance.get<Rules>('rules')
|
||||
}
|
||||
|
||||
updateRules () {
|
||||
return this.instance.put<void>('rules')
|
||||
}
|
||||
|
||||
getProxies () {
|
||||
return this.instance.get<Proxies>('proxies')
|
||||
}
|
||||
|
||||
getProxy (name: string) {
|
||||
return this.instance.get<Proxy>('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<void>('proxies/:name', { params: { name }, data: { name: select } })
|
||||
}
|
||||
export async function getConfig () {
|
||||
const req = await getInstance()
|
||||
return req.get<Config>('configs')
|
||||
}
|
||||
|
||||
export async function Instance () {
|
||||
export async function updateConfig (config: Partial<Config>) {
|
||||
const req = await getInstance()
|
||||
return req.put<void>('configs', config)
|
||||
}
|
||||
|
||||
export async function getRules () {
|
||||
const req = await getInstance()
|
||||
return req.get<Rules>('rules')
|
||||
}
|
||||
|
||||
export async function updateRules () {
|
||||
const req = await getInstance()
|
||||
return req.put<void>('rules')
|
||||
}
|
||||
|
||||
export async function getProxies () {
|
||||
const req = await getInstance()
|
||||
return req.get<Proxies>('proxies')
|
||||
}
|
||||
|
||||
export async function getProxy (name: string) {
|
||||
const req = await getInstance()
|
||||
return req.get<Proxy>('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<void>('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 }
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user