base.ts
3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// 基类执行细节已下沉到 helpers,保留接口编排与可覆盖点
import type { RequestConfig, ResponseData } from "../../types";
import { processResponseSafely, transformToResponseData as defaultTransform, checkHttpStatus as defaultCheckHttpStatus } from "../handlers/response-helpers";
import { handleRequestError } from "../handlers/error-handler";
import { sendRequestGeneric, handleRequestGeneric, createPlatformRequestOptions } from "../handlers/request-helpers";
import type { Adapter, IResponseConfig } from "../types";
import { dispatchEvent } from "../../monitoring/events";
/**
* 基础适配器类(更“薄”)
* - 只负责方法编排与对外接口
* - 具体管线逻辑委托给 helpers,子类可按需重写挂钩方法
*/
export abstract class BaseAdapter {
/**
* 由子类提供平台请求器(如 uni.request / wx.request),要求返回对象包含 abort 方法
*/
protected abstract getPlatformRequester(): (options: any) => { abort: () => void };
/**
* 执行实际的请求操作;默认通过平台请求器实现,子类可重写以适配 Web fetch 等
*/
protected executeRequest(
_config: RequestConfig,
options: any
): Promise<any> {
const requester = this.getPlatformRequester();
return new Promise((resolve, reject) => {
requester({
...options,
success: (res: any) => resolve(res),
fail: (err: any) => reject(err),
});
});
}
// // 兼容:暴露基础请求选项创建方法,保持子类可调用
// protected createBaseRequestOptions(config: RequestConfig) {
// return createBaseOptions(config);
// }
protected createRequestOptions(
config: RequestConfig,
fullURL: string
): any {
return createPlatformRequestOptions(config, fullURL);
}
/** 将原始响应转换为标准 ResponseData;可覆盖 */
protected transformResponse(
rawResponse: any,
config: RequestConfig
): ResponseData<any> | Promise<ResponseData<any>> {
return defaultTransform(rawResponse, config);
}
/** 检查 HTTP 状态码并在错误时抛出异常;可覆盖 */
protected checkHttpStatus(data: IResponseConfig): void {
return defaultCheckHttpStatus(data);
}
/** 处理响应数据;可覆盖 */
protected handleResponse(data: IResponseConfig): ResponseData<any> {
return processResponseSafely(data);
}
/** 处理请求错误;可覆盖 */
protected handleError(
error: any,
config: RequestConfig,
signal?: AbortSignal
): never {
dispatchEvent("error", error, config);
return handleRequestError(error, config, signal);
}
/** 创建适配器函数 */
public createAdapter(): Adapter {
return (config: RequestConfig): Promise<ResponseData<any>> => {
return this.handleRequest(config);
};
}
/** 处理请求的通用逻辑 - 委托给 helpers */
protected handleRequest(config: RequestConfig): Promise<ResponseData<any>> {
return handleRequestGeneric(config, {
createRequestOptions: (c, url) => this.createRequestOptions(c, url),
sendRequest: (c, options, url) => this.sendRequest(c, options, url),
});
}
/** 发送请求的通用逻辑 - 委托给 helpers */
protected sendRequest(
config: RequestConfig,
options: any,
fullURL: string
): Promise<ResponseData<any>> {
return sendRequestGeneric(config, options, fullURL, {
executeRequest: (c, o) => this.executeRequest(c, o),
transformResponse: (raw, c) => this.transformResponse(raw, c),
checkHttpStatus: (resp, c) => this.checkHttpStatus({responseData:resp,config:c}),
handleResponse: (resp, c) => this.handleResponse({responseData:resp,config:c}),
handleError: (err, c, sig) => this.handleError(err, c, sig),
});
}
}