Files
knowai/client/common.ts
tobegold574 382e3aff21 feat(reset): 以构造器模式重构
- 加了大文件传输自定义分片协议

BREAKING CHANGES: 0.1.0(latest)
2025-11-30 20:27:53 +08:00

41 lines
1.2 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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;
}
}
}