initial commit
This commit is contained in:
155
src/utils/request.ts
Normal file
155
src/utils/request.ts
Normal file
@@ -0,0 +1,155 @@
|
||||
import type {
|
||||
AxiosError,
|
||||
AxiosInstance,
|
||||
AxiosRequestConfig,
|
||||
AxiosResponse,
|
||||
CreateAxiosDefaults,
|
||||
InternalAxiosRequestConfig,
|
||||
} from 'axios';
|
||||
|
||||
import axios from 'axios';
|
||||
|
||||
import type { Result } from '@/axios';
|
||||
|
||||
import { getAppEnvConfig } from './env';
|
||||
|
||||
export type Response<T> = [err: AxiosError | null, data: T | null, resp: Result<T> | null];
|
||||
|
||||
export interface RequestOptions extends CreateAxiosDefaults {
|
||||
requestInterceptor?: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>;
|
||||
responseInterceptor?: (resp: AxiosResponse) => AxiosResponse | Promise<AxiosResponse>;
|
||||
responseErrorInterceptor?: (error: any) => any;
|
||||
}
|
||||
|
||||
export class Request {
|
||||
private instance: AxiosInstance;
|
||||
|
||||
private abortController: AbortController;
|
||||
private lastAbortController: AbortController | null;
|
||||
private uniq: boolean;
|
||||
|
||||
constructor(config?: RequestOptions) {
|
||||
this.instance = axios.create(config);
|
||||
this.abortController = new AbortController();
|
||||
this.lastAbortController = null;
|
||||
this.uniq = false;
|
||||
|
||||
this.instance.interceptors.request.use((config) => {
|
||||
// 取消上一次请求
|
||||
if (this.uniq && this.lastAbortController?.signal) {
|
||||
this.lastAbortController.abort();
|
||||
}
|
||||
this.lastAbortController = this.abortController;
|
||||
this.abortController = new AbortController();
|
||||
return config;
|
||||
|
||||
// 业务登录所需headers
|
||||
// const { lampAuthorization, lampClientId, lampClientSecret } = getAppEnvConfig();
|
||||
// const newAuthorization = window.btoa(`${lampClientId}:${lampClientSecret}`);
|
||||
// const authorization = lampAuthorization.trim() !== '' ? lampAuthorization : newAuthorization;
|
||||
// config.headers.set('accept-language', 'zh-CN,zh;q=0.9');
|
||||
// config.headers.set('accept', 'application/json, text/plain, */*');
|
||||
// config.headers.set('Applicationid', '')
|
||||
// config.headers.set('Tenantid', '1');
|
||||
// config.headers.set('Authorization', authorization);
|
||||
// config.headers.set('token', this.extraInfo?.token ?? '')
|
||||
// return config;
|
||||
});
|
||||
|
||||
const requestInterceptor = config?.requestInterceptor ?? Request.defaultRequestInterceptor;
|
||||
const responseInterceptor = config?.responseInterceptor ?? Request.defaultResponseInterceptor;
|
||||
const responseErrorInterceptor = config?.responseErrorInterceptor ?? Request.defaultResponseErrorInterceptor;
|
||||
|
||||
this.instance.interceptors.request.use(requestInterceptor);
|
||||
|
||||
this.instance.interceptors.response.use(responseInterceptor, responseErrorInterceptor);
|
||||
}
|
||||
|
||||
private static defaultRequestInterceptor(config: InternalAxiosRequestConfig) {
|
||||
return config;
|
||||
}
|
||||
|
||||
private static defaultResponseInterceptor(response: AxiosResponse) {
|
||||
return response;
|
||||
}
|
||||
|
||||
private static defaultResponseErrorInterceptor(error: any) {
|
||||
const err = error as AxiosError;
|
||||
if (err.status === 401) {
|
||||
//
|
||||
}
|
||||
if (err.status === 404) {
|
||||
//
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
get<T>(url: string, option?: AxiosRequestConfig & { uniq?: boolean }): Promise<Response<T>> {
|
||||
const { uniq, ...reqConfig } = option ?? {};
|
||||
this.uniq = !!uniq;
|
||||
return new Promise((resolve) => {
|
||||
this.instance
|
||||
.get<Result<T>>(url, {
|
||||
...reqConfig,
|
||||
signal: this.abortController.signal,
|
||||
})
|
||||
.then((res) => {
|
||||
resolve([null, res.data.data, res.data]);
|
||||
})
|
||||
.catch((err) => {
|
||||
resolve([err as AxiosError, null, null]);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
post<T>(url: string, data: AxiosRequestConfig['data'], option?: Partial<Omit<AxiosRequestConfig, 'data'>> & { retRaw?: boolean; uniq?: boolean; upload?: boolean }): Promise<Response<T>> {
|
||||
const { retRaw, uniq, upload, ...reqConfig } = option ?? {};
|
||||
this.uniq = !!uniq;
|
||||
return new Promise((resolve) => {
|
||||
this.instance
|
||||
.post(url, data, { ...reqConfig, headers: { 'content-type': upload ? 'multipart/form-data' : 'application/json' }, signal: this.abortController.signal })
|
||||
.then((res) => {
|
||||
if (retRaw) {
|
||||
const data = res as T;
|
||||
resolve([null, data, null]);
|
||||
} else {
|
||||
const resp = res as AxiosResponse<Result<T>>;
|
||||
resolve([null, resp.data.data, resp.data]);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
resolve([err as AxiosError, null, null]);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
put<T>(url: string, data: AxiosRequestConfig['data'], option?: Partial<Omit<AxiosRequestConfig, 'data'>> & { uniq?: boolean }): Promise<Response<T>> {
|
||||
const { uniq, ...reqConfig } = option ?? {};
|
||||
this.uniq = !!uniq;
|
||||
return new Promise((resolve) => {
|
||||
this.instance
|
||||
.put<Result<T>>(url, data, { ...reqConfig, signal: this.abortController.signal })
|
||||
.then((res) => {
|
||||
resolve([null, res.data.data, res.data]);
|
||||
})
|
||||
.catch((err) => {
|
||||
resolve([err as AxiosError, null, null]);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
delete<T>(url: string, idList: string[], option?: Partial<Omit<AxiosRequestConfig, 'data'>> & { uniq?: boolean }): Promise<Response<T>> {
|
||||
const { uniq, ...reqConfig } = option ?? {};
|
||||
this.uniq = !!uniq;
|
||||
return new Promise((resolve) => {
|
||||
this.instance
|
||||
.delete<Result<T>>(url, { ...reqConfig, data: { ids: idList }, signal: this.abortController.signal })
|
||||
.then((res) => {
|
||||
resolve([null, res.data.data, res.data]);
|
||||
})
|
||||
.catch((err) => {
|
||||
resolve([err as AxiosError, null, null]);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user