41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
// common.ts —— 各协议共用的统一请求配置格式
|
||
|
||
export interface BaseRequestConfig {
|
||
url: string;
|
||
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
||
headers?: Record<string, string>;
|
||
query?: Record<string, string | number>; // 暂时也就分页和搜索
|
||
body?: any;
|
||
|
||
timeout?: number;
|
||
withCredentials?: boolean;
|
||
// upload的时候处理表单要用,避免axios处理
|
||
transformRequest?: ((data: any) => any) | ((data: any) => any)[];
|
||
}
|
||
|
||
// 设计Mock或者实际后端的时候也直接对应这个接口
|
||
export interface BaseResponseConfig<T = unknown> {
|
||
status: number; // 状态码(HTTP状态码)
|
||
msg?: string; // 报错信息(可以没有),和error不是一层语义
|
||
data: T;
|
||
}
|
||
|
||
/*
|
||
* 基础错误类(client层)
|
||
*/
|
||
export class BaseError extends Error {
|
||
code: string; // 错误码(字符串)
|
||
response?: BaseResponseConfig;
|
||
|
||
constructor({ message, code, response }: { message: string; code: string; response?: BaseResponseConfig }) {
|
||
super(message);
|
||
this.name = 'BaseError'; // 补充缺失的 name 属性,会在toString()打印,ES5规范,还是遵守一下吧
|
||
this.code = code;
|
||
if (response) {
|
||
this.response = response;
|
||
}
|
||
}
|
||
}
|
||
|
||
|