|
|
@ -25,6 +25,11 @@ export class Plugin implements IPlugin { |
|
|
|
destory() { } |
|
|
|
} |
|
|
|
|
|
|
|
export const enum EFor { |
|
|
|
Continue, |
|
|
|
Break, |
|
|
|
} |
|
|
|
|
|
|
|
export abstract class PluginsManager { |
|
|
|
static plugins: IPlugin[] = [] |
|
|
|
static use(plugin: IPlugin | IPlugin[]) { |
|
|
@ -34,28 +39,44 @@ export abstract class PluginsManager { |
|
|
|
} |
|
|
|
|
|
|
|
plugins: IPlugin[] = [] |
|
|
|
exculdPlugins: string[] = [] |
|
|
|
|
|
|
|
use(plugin: IPlugin) { |
|
|
|
if (Array.isArray(plugin)) { |
|
|
|
this.plugins = this.plugins.concat(plugin) |
|
|
|
} else this.plugins.push(plugin) |
|
|
|
} |
|
|
|
callPluginByName<T>(name: string, key: keyof T, ...argus: any[]) { |
|
|
|
async runPlugins(cb: (p: IPlugin) => Promise<EFor | undefined>) { |
|
|
|
const array = [...PluginsManager.plugins, ...this.plugins] |
|
|
|
for (let i = 0; i < array.length; i++) { |
|
|
|
let p = array[i] |
|
|
|
let result = await cb(p) |
|
|
|
if (result === EFor.Continue) { |
|
|
|
continue |
|
|
|
} else if (result === EFor.Break) { |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
async _callPluginByName<T>(name: string, key: keyof T, ...argus: any[]) { |
|
|
|
const array = [...PluginsManager.plugins, ...this.plugins] |
|
|
|
for (let i = 0; i < array.length; i++) { |
|
|
|
let p = array[i] |
|
|
|
if (this.exculdPlugins.includes(p.name)) continue |
|
|
|
if (!p.name) continue |
|
|
|
if (name === p.name) { |
|
|
|
const fn = (p as T)[key] |
|
|
|
typeof fn === "function" && fn.apply(p, argus) |
|
|
|
typeof fn === "function" && await fn.apply(p, argus) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
callPlugin(key: keyof IPlugin, ...argus: any[]) { |
|
|
|
async _callPlugin(key: keyof IPlugin, ...argus: any[]) { |
|
|
|
const array = [...PluginsManager.plugins, ...this.plugins] |
|
|
|
for (let i = 0; i < array.length; i++) { |
|
|
|
let p = array[i] |
|
|
|
if (this.exculdPlugins.includes(p.name)) continue |
|
|
|
const fn = p[key] |
|
|
|
typeof fn === "function" && fn.apply(p, argus) |
|
|
|
typeof fn === "function" && await fn.apply(p, argus) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -66,24 +87,45 @@ export abstract class httpBase extends PluginsManager { |
|
|
|
requestInterceptorId: null | number = null |
|
|
|
responseInterceptorId: null | number = null |
|
|
|
|
|
|
|
async callPluginByName<T>(name: string, key: keyof T, ...argus: any[]) { |
|
|
|
await this.runPlugins(async (p) => { |
|
|
|
if (this.exculdPlugins.includes(p.name)) return EFor.Continue |
|
|
|
if (!p.name) return EFor.Continue |
|
|
|
if (name === p.name) { |
|
|
|
const fn = (p as T)[key] |
|
|
|
typeof fn === "function" && await fn.apply(p, argus) |
|
|
|
} |
|
|
|
}) |
|
|
|
// await this._callPluginByName<T>(name, key, ...argus)
|
|
|
|
} |
|
|
|
|
|
|
|
async callPlugin(key: keyof IPlugin, ...argus: any[]) { |
|
|
|
await this.runPlugins(async (p) => { |
|
|
|
if (this.exculdPlugins.includes(p.name)) return EFor.Continue |
|
|
|
const fn = p[key] |
|
|
|
typeof fn === "function" && await fn.apply(p, argus) |
|
|
|
}) |
|
|
|
// await this._callPlugin(key, ...argus)
|
|
|
|
} |
|
|
|
|
|
|
|
create<T>(config?: CreateAxiosDefaults<T>) { |
|
|
|
this.instance = axios.create(deepAssign<CreateAxiosDefaults<T>>(axios.defaults, config ?? {})) |
|
|
|
this.requestInterceptorId = this.instance.interceptors.request.use(config => { |
|
|
|
this.requestInterceptorId = this.instance.interceptors.request.use(async config => { |
|
|
|
const argu = { config } |
|
|
|
this.callPlugin("beforeRequestConfig", argu) |
|
|
|
await this.callPlugin("beforeRequestConfig", argu) |
|
|
|
return argu.config |
|
|
|
}, error => { |
|
|
|
}, async error => { |
|
|
|
const argu = { error } |
|
|
|
this.callPlugin("beforeRequestError", argu) |
|
|
|
await this.callPlugin("beforeRequestError", argu) |
|
|
|
return Promise.reject(argu.error) |
|
|
|
}) |
|
|
|
this.responseInterceptorId = this.instance.interceptors.response.use(response => { |
|
|
|
this.responseInterceptorId = this.instance.interceptors.response.use(async response => { |
|
|
|
const argu = { response } |
|
|
|
this.callPlugin("beforeResponse", argu) |
|
|
|
await this.callPlugin("beforeResponse", argu) |
|
|
|
return Promise.resolve(argu.response) |
|
|
|
}, (error) => { |
|
|
|
}, async (error) => { |
|
|
|
const argu = { error } |
|
|
|
this.callPlugin("beforeResponseError", argu) |
|
|
|
await this.callPlugin("beforeResponseError", argu) |
|
|
|
return Promise.reject(argu.error) |
|
|
|
}) |
|
|
|
return this.instance |
|
|
|