-
Notifications
You must be signed in to change notification settings - Fork 333
/
index.d.ts
93 lines (77 loc) · 2.94 KB
/
index.d.ts
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
import { ResponseError } from "./utils";
export type ResponseType = 'json' | 'text' | 'blob' | 'arrayBuffer' | 'formData';
/**
* 增加的参数
* @param {string} requestType post类型, 用来简化写content-Type, 默认json
* @param {*} data post数据
* @param {object} params query参数
* @param {string} responseType 服务端返回的数据类型, 用来解析数据, 默认json
* @param {boolean} useCache 是否使用缓存,只有get时有效, 默认关闭, 启用后如果命中缓存, response中有useCache=true. 另: 内存缓存, 刷新就没.
* @param {number} ttl 缓存生命周期, 默认60秒, 单位毫秒
* @param {number} timeout 超时时长, 默认未设, 单位毫秒
* @param {boolean} getResponse 是否获取response源
* @param {function} errorHandler 错误处理
* @param {string} prefix 前缀
* @param {string} suffix 后缀
* @param {string} charset 字符集, 默认utf8
*/
export interface RequestOptionsInit extends RequestInit {
charset?: 'utf8' | 'gbk';
requestType?: 'json' | 'form';
data?: any;
params?: object;
responseType?: ResponseType;
useCache?: boolean;
ttl?: number;
timeout?: number;
errorHandler?: (error: ResponseError) => void;
prefix?: string;
suffix?: string;
}
export interface RequestOptionsWithoutResponse extends RequestOptionsInit {
getResponse: false;
}
export interface RequestOptionsWithResponse extends RequestOptionsInit {
getResponse: true;
}
export type RequestResponse<T = any> = {
data: T;
response: Response;
};
export type RequestInterceptor = (url: string, options: RequestOptionsInit) => {
url?: string,
options?: RequestOptionsInit,
};
export type ResponseInterceptor = (response: Response, options: RequestOptionsInit) => Response;
export interface RequestMethod<R = false> {
<T = any>(url: string, options: RequestOptionsWithResponse): Promise<RequestResponse<T>>;
<T = any>(url: string, options: RequestOptionsWithoutResponse): Promise<T>;
<T = any>(url: string, options?: RequestOptionsInit): R extends true ? Promise<RequestResponse<T>> : Promise<T>;
get: RequestMethod<R>;
post: RequestMethod<R>;
delete: RequestMethod<R>;
put: RequestMethod<R>;
rpc: RequestMethod<R>;
interceptors: {
request: {
use: (handler: RequestInterceptor) => void;
};
response: {
use: (handler: ResponseInterceptor) => void;
}
}
}
export interface ExtendOnlyOptions {
maxCache?: number;
}
export type ExtendOptionsInit = RequestOptionsInit & ExtendOnlyOptions;
export type ExtendOptionsWithoutResponse = RequestOptionsWithoutResponse & ExtendOnlyOptions;
export type ExtendOptionsWithResponse = RequestOptionsWithResponse & ExtendOnlyOptions;
export interface Extend {
(options: ExtendOptionsWithoutResponse): RequestMethod<false>;
(options: ExtendOptionsWithResponse): RequestMethod<true>;
(options: ExtendOptionsInit): RequestMethod;
}
export declare var extend: Extend;
declare var request: RequestMethod;
export default request;