-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.d.ts
139 lines (121 loc) · 3.31 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
export type BodyCallback = (
error: Error,
body: any
) => void;
export type HttpResponseHandler = (
callback: BodyCallback,
decodeResponseBody: boolean
) => XhrCallback;
export type XhrCallback = (
error: Error,
response: XhrResponse,
body: any
) => void;
export interface XhrResponse {
body: Object | string;
statusCode: number;
method: string;
headers: XhrHeaders;
url: string;
rawRequest: XMLHttpRequest;
}
export interface XhrHeaders {
[key: string]: string;
}
export interface XhrBaseConfig {
useXDR?: boolean;
sync?: boolean;
method?: 'DELETE' | 'GET' | 'HEAD' | 'OPTIONS' | 'POST' | 'PUT' | 'PATCH';
timeout?: number;
headers?: XhrHeaders;
body?: string | any;
json?: boolean;
username?: string;
password?: string;
withCredentials?: boolean;
responseType?: '' | 'arraybuffer' | 'blob' | 'document' | 'json' | 'text';
requestType?: string;
metadata?: Record<string, unknown>;
retry?: Retry;
beforeSend?: (xhrObject: XMLHttpRequest) => void;
xhr?: XMLHttpRequest;
}
export interface XhrUriConfig extends XhrBaseConfig {
uri: string;
}
export interface XhrUrlConfig extends XhrBaseConfig {
url: string;
}
export interface XhrInstance {
(options: XhrUriConfig | XhrUrlConfig, callback: XhrCallback): any;
(url: string, callback: XhrCallback): any;
(url: string, options: XhrBaseConfig, callback: XhrCallback): any;
}
export interface Retry {
getCurrentDelay(): number;
getCurrentMinPossibleDelay(): number;
getCurrentMaxPossibleDelay(): number;
getCurrentFuzzedDelay(): number;
shouldRetry(): boolean;
moveToNextAttempt(): void;
}
export interface NetworkRequest {
headers: Record<string, string>;
uri: string;
metadata: Record<string, unknown>;
body?: unknown;
retry?: Retry;
timeout?: number;
}
export interface NetworkResponse {
headers: Record<string, string>;
responseUrl: string;
body?: unknown;
responseType?: XMLHttpRequestResponseType;
}
export type Interceptor<T> = (payload: T) => T;
export interface InterceptorsStorage<T> {
enable(): void;
disable(): void;
getIsEnabled(): boolean;
reset(): void;
addInterceptor(type: string, interceptor: Interceptor<T>): boolean;
removeInterceptor(type: string, interceptor: Interceptor<T>): boolean;
clearInterceptorsByType(type: string): boolean;
clear(): boolean;
getForType(type: string): Set<Interceptor<T>>;
execute(type: string, payload: T): T;
}
export interface RetryOptions {
maxAttempts?: number;
delayFactor?: number;
fuzzFactor?: number;
initialDelay?: number;
}
export interface RetryManager {
enable(): void;
disable(): void;
reset(): void;
getMaxAttempts(): number;
setMaxAttempts(maxAttempts: number): void;
getDelayFactor(): number;
setDelayFactor(delayFactor: number): void;
getFuzzFactor(): number;
setFuzzFactor(fuzzFactor: number): void;
getInitialDelay(): number;
setInitialDelay(initialDelay: number): void;
createRetry(options?: RetryOptions): Retry;
}
export interface XhrStatic extends XhrInstance {
del: XhrInstance;
get: XhrInstance;
head: XhrInstance;
patch: XhrInstance;
post: XhrInstance;
put: XhrInstance;
requestInterceptorsStorage: InterceptorsStorage<NetworkRequest>;
responseInterceptorsStorage: InterceptorsStorage<NetworkResponse>;
retryManager: RetryManager;
}
declare const Xhr: XhrStatic;
export default Xhr;