|
|
@ -4,7 +4,7 @@
|
|
|
|
// Distributed under terms of the MIT license.
|
|
|
|
// Distributed under terms of the MIT license.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
import axios, { AxiosError, AxiosInstance, type AxiosResponse } from 'axios';
|
|
|
|
import axios, { AxiosError, type AxiosInstance, type AxiosResponse } from 'axios';
|
|
|
|
|
|
|
|
|
|
|
|
// Be careful when using SSR for cross-request state pollution
|
|
|
|
// Be careful when using SSR for cross-request state pollution
|
|
|
|
// due to creating a Singleton instance here;
|
|
|
|
// due to creating a Singleton instance here;
|
|
|
@ -16,6 +16,7 @@ import axios, { AxiosError, AxiosInstance, type AxiosResponse } from 'axios';
|
|
|
|
export const token = {
|
|
|
|
export const token = {
|
|
|
|
value: '',
|
|
|
|
value: '',
|
|
|
|
update: () => {
|
|
|
|
update: () => {
|
|
|
|
|
|
|
|
console.warn('token updater not set')
|
|
|
|
return new Promise<string>((resolve) => { resolve(token.value) })
|
|
|
|
return new Promise<string>((resolve) => { resolve(token.value) })
|
|
|
|
},
|
|
|
|
},
|
|
|
|
set_updator: (fn: () => Promise<string>) => {
|
|
|
|
set_updator: (fn: () => Promise<string>) => {
|
|
|
@ -49,10 +50,9 @@ export const token = {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 请求拦截
|
|
|
|
// 请求拦截
|
|
|
|
const beforeRequest = (config: any) => {
|
|
|
|
const beforeRequest = (config: any) => {
|
|
|
|
config.retryTimes = 3
|
|
|
|
config.retryTimes = config.retryTimes || 3
|
|
|
|
// NOTE 添加自定义头部
|
|
|
|
// NOTE 添加自定义头部
|
|
|
|
token.value && (config.headers.Authorization = `Bearer ${token.value}`)
|
|
|
|
token.value && (config.headers.Authorization = `Bearer ${token.value}`)
|
|
|
|
// config.headers['auth_token'] = ''
|
|
|
|
|
|
|
|
return config
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -83,17 +83,18 @@ const responseFailed = (client: AxiosInstance) => {
|
|
|
|
return Promise.reject(new Error('请检查网络连接'))
|
|
|
|
return Promise.reject(new Error('请检查网络连接'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let needRetry = true
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
|
|
let needRetry = config?.needRetry !== false
|
|
|
|
if (response?.status == 404) {
|
|
|
|
if (response?.status == 404) {
|
|
|
|
needRetry = false
|
|
|
|
needRetry = false
|
|
|
|
} else if (response?.status == 401) {
|
|
|
|
} else if (response?.status == 401 && needRetry) {
|
|
|
|
needRetry = false
|
|
|
|
needRetry = false
|
|
|
|
// AuthNotFound = New(40100, "auth not found")
|
|
|
|
// AuthNotFound = New(40100, "auth not found")
|
|
|
|
// AuthExpired = New(40102, "auth expired")
|
|
|
|
// AuthExpired = New(40102, "auth expired")
|
|
|
|
if (data.code === 40102 || data.code === 40100) {
|
|
|
|
if (data.code === 40102 || data.code === 40100) {
|
|
|
|
token.value = ''
|
|
|
|
token.value = ''
|
|
|
|
return token.update().then(() => {
|
|
|
|
return token.update().then(() => {
|
|
|
|
return requestRetry(client)(1000, response!)
|
|
|
|
return requestRetry(client)(200, response!)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (response?.status == 500) {
|
|
|
|
} else if (response?.status == 500) {
|
|
|
@ -143,6 +144,7 @@ interface data {
|
|
|
|
query?: any
|
|
|
|
query?: any
|
|
|
|
form?: any
|
|
|
|
form?: any
|
|
|
|
header?: any
|
|
|
|
header?: any
|
|
|
|
|
|
|
|
config?: Object
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -159,6 +161,9 @@ function transData(d: data) {
|
|
|
|
if (d.header) {
|
|
|
|
if (d.header) {
|
|
|
|
opts.headers = Object.assign(opts.headers, d.header)
|
|
|
|
opts.headers = Object.assign(opts.headers, d.header)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d.config) {
|
|
|
|
|
|
|
|
opts = Object.assign(opts, d.config)
|
|
|
|
|
|
|
|
}
|
|
|
|
return opts
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|