feat: add backend-mock app

This commit is contained in:
vben
2024-06-30 14:09:44 +08:00
parent c58aa26dbf
commit ca1cad0cd3
71 changed files with 3420 additions and 735 deletions

View File

@@ -30,6 +30,7 @@ class RequestClient {
* @param options - Axios请求配置可选
*/
constructor(options: RequestClientOptions = {}) {
this.bindMethods();
// 合并默认配置和传入的配置
const defaultConfig: CreateAxiosDefaults = {
headers: {
@@ -63,6 +64,21 @@ class RequestClient {
this.setupInterceptors();
}
private bindMethods() {
const propertyNames = Object.getOwnPropertyNames(
Object.getPrototypeOf(this),
);
propertyNames.forEach((propertyName) => {
const propertyValue = (this as any)[propertyName];
if (
typeof propertyValue === 'function' &&
propertyName !== 'constructor'
) {
(this as any)[propertyName] = propertyValue.bind(this);
}
});
}
private errorHandler(error: any) {
return Promise.reject(error);
}
@@ -71,8 +87,8 @@ class RequestClient {
this.addRequestInterceptor((config: InternalAxiosRequestConfig) => {
const authorization = this.makeAuthorization?.(config);
if (authorization) {
config.headers[authorization.key || 'Authorization'] =
authorization.handler?.();
const { token } = authorization.handler?.() ?? {};
config.headers[authorization.key || 'Authorization'] = token;
}
return config;
}, this.errorHandler);

View File

@@ -7,7 +7,7 @@ type RequestContentType =
| 'multipart/form-data;charset=utf-8';
interface MakeAuthorization {
handler: () => null | string;
handler: () => { refreshToken: string; token: string } | null;
key?: string;
}