mirror of
https://github.com/XiaoDaiGua-Ray/ray-template.git
synced 2025-05-22 04:39:32 +08:00
axios 拦截器一些细节修改
This commit is contained in:
parent
5e1cf2f1a3
commit
a359839d75
@ -25,9 +25,11 @@ export const appendRequestHeaders = (
|
||||
instance: AxiosRequestConfig<unknown>,
|
||||
options: RequestHeaderOptions[],
|
||||
) => {
|
||||
if (instance) {
|
||||
const requestHeaders = instance.headers as RawAxiosRequestHeaders
|
||||
|
||||
options.forEach((curr) => {
|
||||
requestHeaders[curr.key] = curr.value
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
*/
|
||||
|
||||
import RequestCanceler from '@/axios/helper/canceler'
|
||||
import { getAppEnvironment } from '@use-utils/hook'
|
||||
|
||||
import type {
|
||||
RequestInterceptorConfig,
|
||||
@ -53,16 +54,25 @@ export const useAxiosInterceptor = () => {
|
||||
axiosFetchInstance['responseInstance'] = instance
|
||||
}
|
||||
|
||||
/** 获取请求实例或者响应实例 */
|
||||
const getAxiosFetchInstance = (key: ImplementKey) => {
|
||||
return axiosFetchInstance[key]
|
||||
}
|
||||
|
||||
/** 请求前, 执行队列所有方法 */
|
||||
const beforeAxiosFetch = (key: ImplementKey) => {
|
||||
const funcArr = implement[getImplementKey(key)]
|
||||
const instance = getAxiosFetchInstance(key)
|
||||
const { MODE } = getAppEnvironment()
|
||||
|
||||
if (instance) {
|
||||
funcArr?.forEach((curr) => {
|
||||
if (typeof curr === 'function') {
|
||||
curr()
|
||||
curr(instance, MODE)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/** 设置拦截器队列 */
|
||||
const setImplementQueue = (func: AnyFunc[], key: ImplementKey) => {
|
||||
@ -76,11 +86,6 @@ export const useAxiosInterceptor = () => {
|
||||
return implement[getImplementKey(key)]
|
||||
}
|
||||
|
||||
/** 获取请求实例或者响应实例 */
|
||||
const getAxiosFetchInstance = (key: ImplementKey) => {
|
||||
return axiosFetchInstance[key]
|
||||
}
|
||||
|
||||
return {
|
||||
createRequestAxiosInstance,
|
||||
createResponseAxiosInstance,
|
||||
|
@ -9,22 +9,25 @@
|
||||
* @remark 今天也是元气满满撸代码的一天
|
||||
*/
|
||||
|
||||
/** 请求拦截器入口 */
|
||||
/**
|
||||
*
|
||||
* 请求拦截器入口
|
||||
* 被注册方法执行时其实例能够保证获取到, 所以不需要做额外空判断
|
||||
* 在内部执行方法中, 已经做了边界处理
|
||||
*/
|
||||
|
||||
import { useAxiosInterceptor, axiosCanceler } from '@/axios/helper/interceptor'
|
||||
import { appendRequestHeaders } from '@/axios/helper/axiosCopilot'
|
||||
|
||||
import type { RequestInterceptorConfig } from '@/axios/type'
|
||||
|
||||
const { getAxiosFetchInstance, setImplementQueue } = useAxiosInterceptor()
|
||||
import type { RequestInterceptorConfig, ImplementFunction } from '@/axios/type'
|
||||
const { setImplementQueue } = useAxiosInterceptor()
|
||||
|
||||
/** 注入请求头信息 */
|
||||
const injectRequestHeaders = () => {
|
||||
const request = getAxiosFetchInstance(
|
||||
'requestInstance',
|
||||
) as RequestInterceptorConfig
|
||||
|
||||
appendRequestHeaders(request, [
|
||||
const injectRequestHeaders: ImplementFunction<RequestInterceptorConfig> = (
|
||||
ins,
|
||||
mode,
|
||||
) => {
|
||||
appendRequestHeaders(ins, [
|
||||
{
|
||||
key: 'X-TOKEN',
|
||||
value: 'token',
|
||||
@ -33,16 +36,19 @@ const injectRequestHeaders = () => {
|
||||
}
|
||||
|
||||
/** 注入重复请求拦截器 */
|
||||
const injectCanceler = () => {
|
||||
const request = getAxiosFetchInstance(
|
||||
'requestInstance',
|
||||
) as RequestInterceptorConfig
|
||||
|
||||
axiosCanceler.removePendingRequest(request) // 检查是否存在重复请求, 若存在则取消已发的请求
|
||||
axiosCanceler.addPendingRequest(request) // 把当前的请求信息添加到 pendingRequest 表中
|
||||
const injectCanceler: ImplementFunction<RequestInterceptorConfig> = (
|
||||
ins,
|
||||
mode,
|
||||
) => {
|
||||
axiosCanceler.removePendingRequest(ins) // 检查是否存在重复请求, 若存在则取消已发的请求
|
||||
axiosCanceler.addPendingRequest(ins) // 把当前的请求信息添加到 pendingRequest 表中
|
||||
}
|
||||
|
||||
/** 注册请求拦截器 */
|
||||
/**
|
||||
*
|
||||
* 注册请求拦截器
|
||||
* 请注意执行顺序, setImplementQueue 方法按照注册顺序执行
|
||||
*/
|
||||
export const setupRequestInterceptor = () => {
|
||||
setImplementQueue([injectRequestHeaders, injectCanceler], 'requestInstance')
|
||||
}
|
||||
|
@ -9,24 +9,32 @@
|
||||
* @remark 今天也是元气满满撸代码的一天
|
||||
*/
|
||||
|
||||
/** 响应拦截器入口 */
|
||||
/**
|
||||
*
|
||||
* 响应拦截器入口
|
||||
* 被注册方法执行时其实例能够保证获取到, 所以不需要做额外空判断
|
||||
* 在内部执行方法中, 已经做了边界处理
|
||||
*/
|
||||
|
||||
import { useAxiosInterceptor, axiosCanceler } from '@/axios/helper/interceptor'
|
||||
|
||||
import type { ResponseInterceptorConfig } from '@/axios/type'
|
||||
import type { ResponseInterceptorConfig, ImplementFunction } from '@/axios/type'
|
||||
|
||||
const { getAxiosFetchInstance, setImplementQueue } = useAxiosInterceptor()
|
||||
const { setImplementQueue } = useAxiosInterceptor()
|
||||
|
||||
/** 响应成功后移除缓存请求 url */
|
||||
const injectResponseCanceler = () => {
|
||||
const response = getAxiosFetchInstance(
|
||||
'responseInstance',
|
||||
) as ResponseInterceptorConfig
|
||||
|
||||
axiosCanceler.removePendingRequest(response.config)
|
||||
const injectResponseCanceler: ImplementFunction<ResponseInterceptorConfig> = (
|
||||
ins,
|
||||
mode,
|
||||
) => {
|
||||
axiosCanceler.removePendingRequest(ins.config)
|
||||
}
|
||||
|
||||
/** 注册响应拦截器 */
|
||||
/**
|
||||
*
|
||||
* 注册响应拦截器
|
||||
* 请注意执行顺序, setImplementQueue 方法按照注册顺序执行
|
||||
*/
|
||||
export const setupResponseInterceptor = () => {
|
||||
setImplementQueue([injectResponseCanceler], 'responseInstance')
|
||||
}
|
||||
|
@ -89,3 +89,7 @@ export interface ImplementQueue {
|
||||
implementRequestInterceptorArray: AnyFunc[]
|
||||
implementResponseInterceptorArray: AnyFunc[]
|
||||
}
|
||||
|
||||
export type ImplementFunction<
|
||||
T = RequestInterceptorConfig | ResponseInterceptorConfig,
|
||||
> = <K extends T>(ins: K, mode: string) => void
|
||||
|
@ -9,10 +9,19 @@
|
||||
* @remark 今天也是元气满满撸代码的一天
|
||||
*/
|
||||
|
||||
import { getDetermineEnv } from '@use-utils/hook'
|
||||
/**
|
||||
*
|
||||
* onlyoffice
|
||||
*
|
||||
* 该功能暂未实现, 后续应该会补上
|
||||
* 由于该方法需要后端进行相关配合, 因为目前还在考虑是否接上私有 onlyoffice 服务器, 所以该功能暂未实现
|
||||
* 望多多理解, 理解万岁
|
||||
*/
|
||||
|
||||
import { getAppEnvironment } from '@use-utils/hook'
|
||||
import request from '@/axios/instance'
|
||||
|
||||
export const getOfficeDocumentApi = async (uuid: string) => {
|
||||
const { VITE_APP_OFFICE_PROXY_URL } = getDetermineEnv()
|
||||
const { VITE_APP_OFFICE_PROXY_URL } = getAppEnvironment()
|
||||
const { get } = request
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
*
|
||||
* @returns 获取当前项目环境
|
||||
*/
|
||||
export const getDetermineEnv = () => {
|
||||
export const getAppEnvironment = () => {
|
||||
const env = import.meta.env
|
||||
|
||||
return env
|
||||
|
Loading…
x
Reference in New Issue
Block a user