web.ts
1.46 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
import type { RequestConfig, ResponseData } from '../../types';
import type { Adapter } from '../types';
import { BaseAdapter } from './base';
import { transformFetchResponse,createWebRequestOptions } from '../handlers/web-helpers';
/**
* Web环境适配器实现
*/
class WebAdapter extends BaseAdapter {
/**
* 创建请求选项 -> 委托到 helpers
*/
protected createRequestOptions(config: RequestConfig, fullURL: string): RequestInit & { timeout?: number; fullURL?: string } {
return createWebRequestOptions(config, fullURL);
}
/**
* 执行请求
*/
protected executeRequest(_config: RequestConfig, options: RequestInit & { fullURL: string }): Promise<Response> {
const { fullURL, ...fetchOptions } = options as any;
return fetch(fullURL, fetchOptions);
}
/**
* 转换响应 -> 委托到 helpers 的 transformFetchResponse
*/
protected transformResponse(response: Response, config: RequestConfig): Promise<ResponseData<any>> {
return transformFetchResponse(response, config);
}
protected getPlatformRequester(): (options: any) => { abort: () => void } {
// Web 适配器不使用平台请求器(直接使用 fetch 并重写了 executeRequest),此实现仅为满足类型要求
return (_opts: any) => ({ abort: () => {} });
}
}
// 创建适配器实例
const webAdapterInstance = new WebAdapter();
// 导出适配器函数
const webAdapter: Adapter = webAdapterInstance.createAdapter();
export default webAdapter;