chore: api client
This commit is contained in:
@@ -1,4 +1,28 @@
|
|||||||
|
import type { AxiosError } from 'axios';
|
||||||
|
|
||||||
import { Request } from '@/utils/request';
|
import { Request } from '@/utils/request';
|
||||||
|
|
||||||
export const ndmClient = new Request();
|
export const ndmClient = new Request({
|
||||||
export const userClient = 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';
|
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 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 {
|
export class Request {
|
||||||
private instance: AxiosInstance;
|
private instance: AxiosInstance;
|
||||||
|
|
||||||
@@ -15,7 +28,7 @@ export class Request {
|
|||||||
private lastAbortController: AbortController | null;
|
private lastAbortController: AbortController | null;
|
||||||
private uniq: boolean;
|
private uniq: boolean;
|
||||||
|
|
||||||
constructor(config?: CreateAxiosDefaults) {
|
constructor(config?: RequestOptions) {
|
||||||
this.instance = axios.create(config);
|
this.instance = axios.create(config);
|
||||||
this.abortController = new AbortController();
|
this.abortController = new AbortController();
|
||||||
this.lastAbortController = null;
|
this.lastAbortController = null;
|
||||||
@@ -28,34 +41,47 @@ export class Request {
|
|||||||
}
|
}
|
||||||
this.lastAbortController = this.abortController;
|
this.lastAbortController = this.abortController;
|
||||||
this.abortController = new 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;
|
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(
|
const requestInterceptor = config?.requestInterceptor ?? Request.defaultRequestInterceptor;
|
||||||
(response) => {
|
const responseInterceptor = config?.responseInterceptor ?? Request.defaultResponseInterceptor;
|
||||||
return response;
|
const responseErrorInterceptor = config?.responseErrorInterceptor ?? Request.defaultResponseErrorInterceptor;
|
||||||
},
|
|
||||||
(error) => {
|
this.instance.interceptors.request.use(requestInterceptor);
|
||||||
const err = error as AxiosError;
|
|
||||||
if (err.status === 401) {
|
this.instance.interceptors.response.use(responseInterceptor, responseErrorInterceptor);
|
||||||
//
|
}
|
||||||
}
|
|
||||||
if (err.status === 404) {
|
private static defaultRequestInterceptor(config: InternalAxiosRequestConfig) {
|
||||||
//
|
return config;
|
||||||
}
|
}
|
||||||
return Promise.reject(error);
|
|
||||||
},
|
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>> {
|
get<T>(url: string, option?: AxiosRequestConfig & { uniq?: boolean }): Promise<Response<T>> {
|
||||||
|
|||||||
Reference in New Issue
Block a user