|
@@ -5,7 +5,7 @@ import type { AxiosResponse, AxiosRequestConfig } from 'axios'
|
|
|
|
|
|
export type ResErrorHandler = <D, T extends ResData<D>>(response: AxiosResponse<T>, data?: T) => void
|
|
|
export type ReqErrorHandler = <T>(err: Error, response: AxiosRequestConfig<T>) => void
|
|
|
-export type ResData<T> = { code: ResCode, msg: string, data: T }
|
|
|
+export type ResData<T> = { code: ResCode, message: string, data: T }
|
|
|
|
|
|
export const axiosFactory = () => {
|
|
|
const axiosRaw = Axios.create()
|
|
@@ -24,8 +24,8 @@ export const axiosFactory = () => {
|
|
|
{ set: (val: AxiosConfig[K]) => void }
|
|
|
& (AxiosConfig[K] extends Array<any>
|
|
|
? {
|
|
|
- add: (val: AxiosConfig[K]) => void,
|
|
|
- del: (val?: AxiosConfig[K]) => void
|
|
|
+ add: (...val: AxiosConfig[K]) => void,
|
|
|
+ del: (...val: AxiosConfig[K]) => void
|
|
|
}
|
|
|
: { del: () => void })
|
|
|
|
|
@@ -38,10 +38,10 @@ export const axiosFactory = () => {
|
|
|
}
|
|
|
|
|
|
if (Array.isArray(axiosObj)) {
|
|
|
- apis.add = (val: AxiosConfig[K]) => {
|
|
|
+ apis.add = (...val: any[]) => {
|
|
|
axiosObj.push(...val as any)
|
|
|
}
|
|
|
- apis.del = (val?: AxiosConfig[K]) => {
|
|
|
+ apis.del = (...val: any[]) => {
|
|
|
if (val) {
|
|
|
axiosConfig[key] = axiosObj.filter(item => !val?.includes(item)) as any
|
|
|
} else {
|
|
@@ -98,42 +98,52 @@ export const axiosFactory = () => {
|
|
|
const matchURL = (urls: string[], config: AxiosRequestConfig<any>) =>
|
|
|
config.url && urls.includes(config.url)
|
|
|
|
|
|
- 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))
|
|
|
- throw error
|
|
|
- }
|
|
|
- } else {
|
|
|
- config.headers = {
|
|
|
- ...config.headers,
|
|
|
- token: axiosConfig.token
|
|
|
+ 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))
|
|
|
+ throw error
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ config.headers = {
|
|
|
+ ...config.headers,
|
|
|
+ token: axiosConfig.token
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ return config
|
|
|
}
|
|
|
- return config
|
|
|
- })
|
|
|
+ )
|
|
|
|
|
|
- axiosRaw.interceptors.response.use((response: AxiosResponse<ResData<any>>) => {
|
|
|
- if (matchURL(axiosConfig.unResErrorSet, response.config)) {
|
|
|
- return response
|
|
|
- }
|
|
|
+ axiosRaw.interceptors.response.use(
|
|
|
+ (response: AxiosResponse<ResData<any>>) => {
|
|
|
+ if (matchURL(axiosConfig.unResErrorSet, response.config)) {
|
|
|
+ return response
|
|
|
+ }
|
|
|
|
|
|
- if (response.status !== 200) {
|
|
|
- axiosConfig.resErrorHandler.forEach(handler => handler(response))
|
|
|
- throw new Error(response.statusText)
|
|
|
- } else if (response.data.code !== ResCode.SUCCESS) {
|
|
|
- axiosConfig.resErrorHandler.forEach(handler => handler(response, response.data))
|
|
|
- if (response.data.code === ResCode.TOKEN_INVALID) {
|
|
|
- delToken()
|
|
|
+ if (response.status !== 200) {
|
|
|
+ axiosConfig.resErrorHandler.forEach(handler => handler(response))
|
|
|
+ throw new Error(response.statusText)
|
|
|
+ } else if (response.data.code !== ResCode.SUCCESS) {
|
|
|
+ axiosConfig.resErrorHandler.forEach(handler => handler(response, response.data))
|
|
|
+ if (response.data.code === ResCode.TOKEN_INVALID) {
|
|
|
+ delToken()
|
|
|
+ }
|
|
|
+ throw new Error(response.data.message)
|
|
|
+ } else {
|
|
|
+ return response.data.data
|
|
|
}
|
|
|
- throw new Error(response.data.msg)
|
|
|
- } else {
|
|
|
- return response.data.data
|
|
|
+ },
|
|
|
+ (err) => {
|
|
|
+ if (!matchURL(axiosConfig.unResErrorSet, err.config)) {
|
|
|
+ axiosConfig.resErrorHandler.forEach(handler => handler(err.response))
|
|
|
+ }
|
|
|
+ throw new Error(err.response.statusText)
|
|
|
}
|
|
|
- })
|
|
|
+ )
|
|
|
|
|
|
|
|
|
type AxiosProcess = {
|