chore: api client
This commit is contained in:
@@ -1,4 +1,28 @@
|
||||
import type { AxiosError } from 'axios';
|
||||
|
||||
import { Request } from '@/utils/request';
|
||||
|
||||
export const ndmClient = new Request();
|
||||
export const userClient = new Request();
|
||||
export const ndmClient = new Request({
|
||||
requestInterceptor: (config) => {
|
||||
return config;
|
||||
},
|
||||
responseInterceptor: (response) => {
|
||||
return response;
|
||||
},
|
||||
responseErrorInterceptor: (error) => {
|
||||
return Promise.reject(error);
|
||||
},
|
||||
});
|
||||
|
||||
export const userClient = new Request({
|
||||
responseErrorInterceptor: (error) => {
|
||||
const err = error as AxiosError;
|
||||
if (err.response?.status === 401) {
|
||||
// TODO: 处理 401 错误,例如跳转到登录页
|
||||
}
|
||||
if (err.response?.status === 404) {
|
||||
// TODO: 处理 404 错误
|
||||
}
|
||||
return Promise.reject(error);
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
import type { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, CreateAxiosDefaults } from 'axios';
|
||||
import type {
|
||||
AxiosError,
|
||||
AxiosInstance,
|
||||
AxiosRequestConfig,
|
||||
AxiosResponse,
|
||||
CreateAxiosDefaults,
|
||||
InternalAxiosRequestConfig,
|
||||
} from 'axios';
|
||||
|
||||
import axios from 'axios';
|
||||
|
||||
@@ -8,6 +15,12 @@ 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;
|
||||
|
||||
@@ -15,7 +28,7 @@ export class Request {
|
||||
private lastAbortController: AbortController | null;
|
||||
private uniq: boolean;
|
||||
|
||||
constructor(config?: CreateAxiosDefaults) {
|
||||
constructor(config?: RequestOptions) {
|
||||
this.instance = axios.create(config);
|
||||
this.abortController = new AbortController();
|
||||
this.lastAbortController = null;
|
||||
@@ -28,34 +41,47 @@ export class Request {
|
||||
}
|
||||
this.lastAbortController = this.abortController;
|
||||
this.abortController = new AbortController();
|
||||
// 业务登录所需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;
|
||||
|
||||
// 业务登录所需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;
|
||||
});
|
||||
|
||||
this.instance.interceptors.response.use(
|
||||
(response) => {
|
||||
return response;
|
||||
},
|
||||
(error) => {
|
||||
const err = error as AxiosError;
|
||||
if (err.status === 401) {
|
||||
//
|
||||
}
|
||||
if (err.status === 404) {
|
||||
//
|
||||
}
|
||||
return Promise.reject(error);
|
||||
},
|
||||
);
|
||||
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>> {
|
||||
|
||||
Reference in New Issue
Block a user