|
@@ -0,0 +1,184 @@
|
|
|
+import Axios from 'axios'
|
|
|
+import { ResCode } from 'constant'
|
|
|
+
|
|
|
+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 const axiosFactory = () => {
|
|
|
+ const axiosRaw = Axios.create()
|
|
|
+ const axiosConfig = {
|
|
|
+ token: localStorage.getItem('token'),
|
|
|
+ unTokenSet: [] as string[],
|
|
|
+ unReqErrorSet: [] as string[],
|
|
|
+ unResErrorSet: [] as string[],
|
|
|
+ resErrorHandler: [] as ResErrorHandler[],
|
|
|
+ reqErrorHandler: [] as ReqErrorHandler[],
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ type AxiosConfig = typeof axiosConfig
|
|
|
+ type ExponseApi<K extends keyof AxiosConfig> =
|
|
|
+ { set: (val: AxiosConfig[K]) => void }
|
|
|
+ & (AxiosConfig[K] extends Array<any>
|
|
|
+ ? {
|
|
|
+ add: (val: AxiosConfig[K]) => void,
|
|
|
+ del: (val?: AxiosConfig[K]) => void
|
|
|
+ }
|
|
|
+ : { del: () => void })
|
|
|
+
|
|
|
+ const getExponseApi = <K extends keyof AxiosConfig>(key: K): ExponseApi<K> => {
|
|
|
+ const axiosObj: any = axiosConfig[key]
|
|
|
+ const apis: any = {
|
|
|
+ set (val: AxiosConfig[K]) {
|
|
|
+ axiosConfig[key] = val
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Array.isArray(axiosObj)) {
|
|
|
+ apis.add = (val: AxiosConfig[K]) => {
|
|
|
+ axiosObj.push(...val as any)
|
|
|
+ }
|
|
|
+ apis.del = (val?: AxiosConfig[K]) => {
|
|
|
+ if (val) {
|
|
|
+ axiosConfig[key] = axiosObj.filter(item => !val?.includes(item)) as any
|
|
|
+ } else {
|
|
|
+ axiosConfig[key] = [] as any
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ apis.del = () => {
|
|
|
+ axiosConfig[key] = undefined as any
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return apis
|
|
|
+ }
|
|
|
+
|
|
|
+ const {
|
|
|
+ set: setToken,
|
|
|
+ del: delToken,
|
|
|
+ } = getExponseApi('token')
|
|
|
+
|
|
|
+ const {
|
|
|
+ set: setUnsetTokenURLS,
|
|
|
+ add: addUnsetTokenURLS,
|
|
|
+ del: delUnsetTokenURLS
|
|
|
+ } = getExponseApi('unTokenSet')
|
|
|
+
|
|
|
+ const {
|
|
|
+ set: setResErrorHandler,
|
|
|
+ add: addResErrorHandler,
|
|
|
+ del: delResErrorHandler
|
|
|
+ } = getExponseApi('resErrorHandler')
|
|
|
+
|
|
|
+ const {
|
|
|
+ set: setUnsetReqErrorURLS,
|
|
|
+ add: addUnsetReqErrorURLS,
|
|
|
+ del: delUnsetReqErrorURLS
|
|
|
+ } = getExponseApi('unReqErrorSet')
|
|
|
+
|
|
|
+ const {
|
|
|
+ set: setReqErrorHandler,
|
|
|
+ add: addReqErrorHandler,
|
|
|
+ del: delReqErrorHandler
|
|
|
+ } = getExponseApi('reqErrorHandler')
|
|
|
+
|
|
|
+ const {
|
|
|
+ set: setUnsetResErrorURLS,
|
|
|
+ add: addUnsetResErrorURLS,
|
|
|
+ del: delUnsetResErrorURLS
|
|
|
+ } = getExponseApi('unResErrorSet')
|
|
|
+
|
|
|
+ const setDefaultURI = (url: string) => {
|
|
|
+ axiosRaw.defaults.baseURL = url
|
|
|
+ }
|
|
|
+
|
|
|
+ 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
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return config
|
|
|
+ })
|
|
|
+
|
|
|
+ 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()
|
|
|
+ }
|
|
|
+ throw new Error(response.data.msg)
|
|
|
+ } else {
|
|
|
+ return response.data.data
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+ type AxiosProcess = {
|
|
|
+ getUri(config?: AxiosRequestConfig): string;
|
|
|
+ request<R = any, D = any>(config: AxiosRequestConfig<D>): Promise<R>;
|
|
|
+ get<R = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
|
|
|
+ delete<R = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
|
|
|
+ head<R = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
|
|
|
+ options<R = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;
|
|
|
+ post<R = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
|
|
|
+ put<R = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
|
|
|
+ patch<R = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
|
|
|
+ postForm<R = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
|
|
|
+ putForm<R = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
|
|
|
+ patchForm<R = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;
|
|
|
+ }
|
|
|
+
|
|
|
+ interface AxiosInstanceProcess extends AxiosProcess {
|
|
|
+ <R = any>(config: AxiosRequestConfig): Promise<R>;
|
|
|
+ <R = any>(url: string, config?: AxiosRequestConfig): Promise<R>;
|
|
|
+ }
|
|
|
+
|
|
|
+ const axios: AxiosInstanceProcess = axiosRaw as any
|
|
|
+
|
|
|
+ return {
|
|
|
+ axios,
|
|
|
+ setToken,
|
|
|
+ delToken,
|
|
|
+ setUnsetTokenURLS,
|
|
|
+ addUnsetTokenURLS,
|
|
|
+ delUnsetTokenURLS,
|
|
|
+ setResErrorHandler,
|
|
|
+ addResErrorHandler,
|
|
|
+ delResErrorHandler,
|
|
|
+ setUnsetReqErrorURLS,
|
|
|
+ addUnsetReqErrorURLS,
|
|
|
+ delUnsetReqErrorURLS,
|
|
|
+ setReqErrorHandler,
|
|
|
+ addReqErrorHandler,
|
|
|
+ delReqErrorHandler,
|
|
|
+ setUnsetResErrorURLS,
|
|
|
+ addUnsetResErrorURLS,
|
|
|
+ delUnsetResErrorURLS,
|
|
|
+ setDefaultURI
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export default axiosFactory
|