|
@@ -19,6 +19,7 @@ export const axiosFactory = () => {
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
type AxiosConfig = typeof axiosConfig
|
|
|
type ExponseApi<K extends keyof AxiosConfig> =
|
|
|
{ set: (val: AxiosConfig[K]) => void }
|
|
@@ -30,22 +31,22 @@ export const axiosFactory = () => {
|
|
|
: { del: () => void })
|
|
|
|
|
|
const getExponseApi = <K extends keyof AxiosConfig>(key: K): ExponseApi<K> => {
|
|
|
- const axiosObj: any = axiosConfig[key]
|
|
|
+ let axiosObj = axiosConfig[key] as any[]
|
|
|
const apis: any = {
|
|
|
set (val: AxiosConfig[K]) {
|
|
|
- axiosConfig[key] = val
|
|
|
+ axiosObj = axiosConfig[key] = val as any
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (Array.isArray(axiosObj)) {
|
|
|
apis.add = (...val: any[]) => {
|
|
|
- axiosObj.push(...val as any)
|
|
|
+ axiosObj.push(...val)
|
|
|
}
|
|
|
apis.del = (...val: any[]) => {
|
|
|
if (val) {
|
|
|
- axiosConfig[key] = axiosObj.filter(item => !val?.includes(item)) as any
|
|
|
+ apis.set(axiosObj.filter((item: any) => !val?.includes(item)) as any)
|
|
|
} else {
|
|
|
- axiosConfig[key] = [] as any
|
|
|
+ axiosObj.length = 0
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
@@ -56,11 +57,16 @@ export const axiosFactory = () => {
|
|
|
return apis
|
|
|
}
|
|
|
|
|
|
- const {
|
|
|
- set: setToken,
|
|
|
- del: delToken,
|
|
|
- } = getExponseApi('token')
|
|
|
-
|
|
|
+ const getToken = () => axiosConfig.token
|
|
|
+ const setToken = (token: string) => {
|
|
|
+ localStorage.setItem('token', token)
|
|
|
+ axiosConfig.token = token
|
|
|
+ }
|
|
|
+ const delToken = () => {
|
|
|
+ localStorage.removeItem('token')
|
|
|
+ axiosConfig.token = null
|
|
|
+ }
|
|
|
+
|
|
|
const {
|
|
|
set: setUnsetTokenURLS,
|
|
|
add: addUnsetTokenURLS,
|
|
@@ -98,13 +104,21 @@ export const axiosFactory = () => {
|
|
|
const matchURL = (urls: string[], config: AxiosRequestConfig<any>) =>
|
|
|
config.url && urls.includes(config.url)
|
|
|
|
|
|
+ const callErrorHandler = (key: 'req' | 'res', ...args: any[]) => {
|
|
|
+ Promise.resolve()
|
|
|
+ .then(() => {
|
|
|
+ const api = `${key}ErrorHandler`
|
|
|
+ ;(axiosConfig as any)[api].forEach((handler: any) => handler(...args))
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
axiosRaw.interceptors.request.use(
|
|
|
config => {
|
|
|
if (!matchURL(axiosConfig.unTokenSet, config)) {
|
|
|
if (!axiosConfig.token) {
|
|
|
if (!matchURL(axiosConfig.unReqErrorSet, config)) {
|
|
|
const error = new Error('缺少token')
|
|
|
- axiosConfig.reqErrorHandler.forEach(handler => handler(error, config))
|
|
|
+ callErrorHandler('req', error, config)
|
|
|
throw error
|
|
|
}
|
|
|
} else {
|
|
@@ -125,10 +139,10 @@ export const axiosFactory = () => {
|
|
|
}
|
|
|
|
|
|
if (response.status !== 200) {
|
|
|
- axiosConfig.resErrorHandler.forEach(handler => handler(response))
|
|
|
+ callErrorHandler('res', response)
|
|
|
throw new Error(response.statusText)
|
|
|
} else if (response.data.code !== ResCode.SUCCESS) {
|
|
|
- axiosConfig.resErrorHandler.forEach(handler => handler(response, response.data))
|
|
|
+ callErrorHandler('res', response, response.data)
|
|
|
if (response.data.code === ResCode.TOKEN_INVALID) {
|
|
|
delToken()
|
|
|
}
|
|
@@ -139,7 +153,7 @@ export const axiosFactory = () => {
|
|
|
},
|
|
|
(err) => {
|
|
|
if (!matchURL(axiosConfig.unResErrorSet, err.config)) {
|
|
|
- axiosConfig.resErrorHandler.forEach(handler => handler(err.response))
|
|
|
+ callErrorHandler('res', err.response)
|
|
|
}
|
|
|
throw new Error(err.response.statusText)
|
|
|
}
|
|
@@ -170,6 +184,7 @@ export const axiosFactory = () => {
|
|
|
|
|
|
return {
|
|
|
axios,
|
|
|
+ getToken,
|
|
|
setToken,
|
|
|
delToken,
|
|
|
setUnsetTokenURLS,
|