Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-request): 请求拦截器兼容方式支持异步写法 #12045

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions examples/max/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-ignore
import type { RequestConfig } from '@@/plugin-request';
import type { RequestConfig, RequestInterceptorAxios } from '@umijs/max';
import { message } from 'antd';
import { notification } from 'antd/es';

Expand Down Expand Up @@ -62,12 +61,16 @@ interface ResponseStructure {
showType?: number;
}

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

export const request: RequestConfig = {
requestInterceptors: [
(config) => {
console.log('Interceptor:', config);
(async (config) => {
console.log('Async Interceptor before :', config);
await sleep(500);
console.log('Async Interceptor after :', config);
return config;
},
}) satisfies RequestInterceptorAxios,
(url, options) => {
console.log(url, options);
return { url, options };
Expand All @@ -78,6 +81,12 @@ export const request: RequestConfig = {
console.log('responseInterceptor', res);
return res;
},
async (res) => {
console.log('Async responseInterceptor before', res);
await sleep(500);
console.log('Async responseInterceptor after', res);
return res;
},
],
errorConfig: {
errorHandler: (error: any, opts: any) => {
Expand Down
33 changes: 21 additions & 12 deletions packages/plugins/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,13 @@ type RequestError = AxiosError | Error
interface IErrorHandler {
(error: RequestError, opts: IRequestOptions): void;
}
type IRequestInterceptorAxios = (config: RequestOptions) => RequestOptions;
type IRequestInterceptorUmiRequest = (url: string, config : RequestOptions) => { url: string, options: RequestOptions };
type WithPromise<T> = T | Promise<T>;
type IRequestInterceptorAxios = (config: IRequestOptions) => WithPromise<IRequestOptions>;
type IRequestInterceptorUmiRequest = (url: string, config : IRequestOptions) => WithPromise<{ url: string, options: IRequestOptions }>;
type IRequestInterceptor = IRequestInterceptorAxios | IRequestInterceptorUmiRequest;
type IErrorInterceptor = (error: Error) => Promise<Error>;
type IResponseInterceptor = <T = any>(response : AxiosResponse<T>) => AxiosResponse<T> ;
type IRequestInterceptorTuple = [IRequestInterceptor , IErrorInterceptor] | [ IRequestInterceptor ] | IRequestInterceptor
type IResponseInterceptor = <T = any>(response : AxiosResponse<T>) => WithPromise<AxiosResponse<T>> ;
type IRequestInterceptorTuple = [IRequestInterceptor , IErrorInterceptor] | [IRequestInterceptor] | IRequestInterceptor
type IResponseInterceptorTuple = [IResponseInterceptor, IErrorInterceptor] | [IResponseInterceptor] | IResponseInterceptor

export interface RequestConfig<T = any> extends AxiosRequestConfig {
Expand Down Expand Up @@ -168,19 +169,19 @@ const getRequestInstance = (): AxiosInstance => {

config?.requestInterceptors?.forEach((interceptor) => {
if(interceptor instanceof Array){
requestInstance.interceptors.request.use((config) => {
requestInstance.interceptors.request.use(async (config) => {
const { url } = config;
if(interceptor[0].length === 2){
const { url: newUrl, options } = interceptor[0](url, config);
const { url: newUrl, options } = await interceptor[0](url, config);
return { ...options, url: newUrl };
}
return interceptor[0](config);
}, interceptor[1]);
} else {
requestInstance.interceptors.request.use((config) => {
requestInstance.interceptors.request.use(async (config) => {
const { url } = config;
if(interceptor.length === 2){
const { url: newUrl, options } = interceptor(url, config);
const { url: newUrl, options } = await interceptor(url, config);
return { ...options, url: newUrl };
}
return interceptor(config);
Expand Down Expand Up @@ -211,19 +212,19 @@ const request: IRequest = (url: string, opts: any = { method: 'GET' }) => {
const { getResponse = false, requestInterceptors, responseInterceptors } = opts;
const requestInterceptorsToEject = requestInterceptors?.map((interceptor) => {
if(interceptor instanceof Array){
return requestInstance.interceptors.request.use((config) => {
return requestInstance.interceptors.request.use(async (config) => {
const { url } = config;
if(interceptor[0].length === 2){
const { url: newUrl, options } = interceptor[0](url, config);
const { url: newUrl, options } = await interceptor[0](url, config);
return { ...options, url: newUrl };
}
return interceptor[0](config);
}, interceptor[1]);
} else {
return requestInstance.interceptors.request.use((config) => {
return requestInstance.interceptors.request.use(async (config) => {
const { url } = config;
if(interceptor.length === 2){
const { url: newUrl, options } = interceptor(url, config);
const { url: newUrl, options } = await interceptor(url, config);
return { ...options, url: newUrl };
}
return interceptor(config);
Expand Down Expand Up @@ -280,6 +281,10 @@ export type {
AxiosResponse,
AxiosError,
RequestError,
IRequestInterceptorAxios as RequestInterceptorAxios,
IRequestInterceptorUmiRequest as RequestInterceptorUmiRequest,
IRequestInterceptor as RequestInterceptor,
IErrorInterceptor as ErrorInterceptor,
IResponseInterceptor as ResponseInterceptor,
IRequestOptions as RequestOptions,
IRequest as Request,
Expand Down Expand Up @@ -320,6 +325,10 @@ export type {
AxiosResponse,
AxiosError,
RequestError,
RequestInterceptorAxios,
RequestInterceptorUmiRequest,
RequestInterceptor,
ErrorInterceptor,
ResponseInterceptor,
RequestOptions,
Request } from './request';
Expand Down
Loading