/
types.ts
274 lines (240 loc) 路 7.54 KB
/
types.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import http = require('http');
import https = require('https');
import Keyv = require('keyv');
import CacheableRequest = require('cacheable-request');
import PCancelable = require('p-cancelable');
import ResponseLike = require('responselike');
import {URL} from 'url';
import {Readable as ReadableStream} from 'stream';
import {Timings, IncomingMessageWithTimings} from '@szmarczak/http-timer';
import CacheableLookup from 'cacheable-lookup';
import {Except, Merge} from 'type-fest';
import {GotReturn} from './create';
import {GotError, HTTPError, MaxRedirectsError, ParseError, TimeoutError, RequestError} from './errors';
import {Hooks} from './known-hook-events';
import {URLOptions} from './utils/options-to-url';
export type GeneralError = Error | GotError | HTTPError | MaxRedirectsError | ParseError;
export type Method =
| 'GET'
| 'POST'
| 'PUT'
| 'PATCH'
| 'HEAD'
| 'DELETE'
| 'OPTIONS'
| 'TRACE'
| 'get'
| 'post'
| 'put'
| 'patch'
| 'head'
| 'delete'
| 'options'
| 'trace';
export type ResponseType = 'json' | 'buffer' | 'text';
export interface Response<BodyType = unknown> extends IncomingMessageWithTimings {
body: BodyType;
statusCode: number;
/**
The remote IP address.
Note: Not available when the response is cached. This is hopefully a temporary limitation, see [lukechilds/cacheable-request#86](https://github.com/lukechilds/cacheable-request/issues/86).
*/
ip: string;
fromCache?: boolean;
isFromCache?: boolean;
req?: http.ClientRequest;
requestUrl: string;
retryCount: number;
timings: Timings;
redirectUrls: string[];
request: {
options: NormalizedOptions;
};
url: string;
}
// TODO: The `ResponseLike` type should be properly fixed instead:
// https://github.com/sindresorhus/got/pull/827/files#r323633794
export interface ResponseObject extends Partial<ResponseLike> {
socket: {
remoteAddress: string;
};
}
export interface RetryObject {
attemptCount: number;
retryOptions: Required<RetryOptions>;
error: TimeoutError | RequestError;
computedValue: number;
}
export type RetryFunction = (retryObject: RetryObject) => number;
export type HandlerFunction = <T extends GotReturn>(options: NormalizedOptions, next: (options: NormalizedOptions) => T) => T | Promise<T>;
export interface DefaultRetryOptions {
limit: number;
methods: Method[];
statusCodes: number[];
errorCodes: string[];
calculateDelay: RetryFunction;
maxRetryAfter?: number;
}
export interface RetryOptions extends Partial<DefaultRetryOptions> {
retries?: number;
}
export type RequestFunction = typeof http.request;
export interface AgentByProtocol {
http?: http.Agent;
https?: https.Agent;
}
export interface Delays {
lookup?: number;
connect?: number;
secureConnect?: number;
socket?: number;
response?: number;
send?: number;
request?: number;
}
export type Headers = Record<string, string | string[] | undefined>;
interface ToughCookieJar {
getCookieString(currentUrl: string, options: {[key: string]: unknown}, cb: (err: Error | null, cookies: string) => void): void;
getCookieString(url: string, callback: (error: Error | null, cookieHeader: string) => void): void;
setCookie(cookieOrString: unknown, currentUrl: string, options: {[key: string]: unknown}, cb: (err: Error | null, cookie: unknown) => void): void;
setCookie(rawCookie: string, url: string, callback: (error: Error | null, result: unknown) => void): void;
}
interface PromiseCookieJar {
getCookieString(url: string): Promise<string>;
setCookie(rawCookie: string, url: string): Promise<unknown>;
}
export const requestSymbol = Symbol('request');
/* eslint-disable @typescript-eslint/indent */
export type DefaultOptions = Merge<
Required<
Except<
GotOptions,
// Override
'hooks' |
'retry' |
'timeout' |
'context' |
'_pagination' |
// Should not be present
'agent' |
'body' |
'cookieJar' |
'encoding' |
'form' |
'json' |
'lookup' |
'request' |
'url' |
typeof requestSymbol
>
>,
{
hooks: Required<Hooks>;
retry: DefaultRetryOptions;
timeout: Delays;
context: {[key: string]: any};
_pagination?: PaginationOptions<unknown>['_pagination'];
}
>;
/* eslint-enable @typescript-eslint/indent */
export interface PaginationOptions<T> {
_pagination?: {
transform?: (response: Response) => Promise<T[]> | T[];
filter?: (item: T, allItems: T[], currentItems: T[]) => boolean;
paginate?: (response: Response, allItems: T[], currentItems: T[]) => Options | false;
shouldContinue?: (item: T, allItems: T[], currentItems: T[]) => boolean;
countLimit?: number;
};
}
export interface GotOptions extends PaginationOptions<unknown> {
[requestSymbol]?: RequestFunction;
url?: URL | string;
body?: string | Buffer | ReadableStream;
hooks?: Hooks;
decompress?: boolean;
isStream?: boolean;
encoding?: BufferEncoding;
method?: Method;
retry?: RetryOptions | number;
throwHttpErrors?: boolean;
cookieJar?: ToughCookieJar | PromiseCookieJar;
ignoreInvalidCookies?: boolean;
request?: RequestFunction;
agent?: http.Agent | https.Agent | boolean | AgentByProtocol;
cache?: string | CacheableRequest.StorageAdapter | false;
headers?: Headers;
responseType?: ResponseType;
resolveBodyOnly?: boolean;
followRedirect?: boolean;
prefixUrl?: URL | string;
timeout?: number | Delays;
dnsCache?: CacheableLookup | Map<string, string> | Keyv | false;
useElectronNet?: boolean;
form?: {[key: string]: any};
json?: {[key: string]: any};
context?: {[key: string]: any};
maxRedirects?: number;
lookup?: CacheableLookup['lookup'];
allowGetBody?: boolean;
methodRewriting?: boolean;
}
export type Options = Merge<https.RequestOptions, Merge<GotOptions, URLOptions>>;
export interface NormalizedOptions extends Options {
// Normalized Got options
headers: Headers;
hooks: Required<Hooks>;
timeout: Delays;
dnsCache: CacheableLookup | false;
lookup?: CacheableLookup['lookup'];
retry: Required<RetryOptions>;
prefixUrl: string;
method: Method;
url: URL;
cacheableRequest?: (options: string | URL | http.RequestOptions, callback?: (response: http.ServerResponse | ResponseLike) => void) => CacheableRequest.Emitter;
cookieJar?: PromiseCookieJar;
maxRedirects: number;
pagination?: Required<PaginationOptions<unknown>['_pagination']>;
[requestSymbol]: RequestFunction;
// Other values
decompress: boolean;
isStream: boolean;
throwHttpErrors: boolean;
ignoreInvalidCookies: boolean;
cache: CacheableRequest.StorageAdapter | false;
responseType: ResponseType;
resolveBodyOnly: boolean;
followRedirect: boolean;
useElectronNet: boolean;
methodRewriting: boolean;
allowGetBody: boolean;
context: {[key: string]: any};
// UNIX socket support
path?: string;
}
export interface ExtendOptions extends Options {
handlers?: HandlerFunction[];
mutableDefaults?: boolean;
}
export interface Defaults {
options: DefaultOptions;
handlers: HandlerFunction[];
mutableDefaults: boolean;
_rawHandlers?: HandlerFunction[];
}
export type URLOrOptions = Options | string;
export interface Progress {
percent: number;
transferred: number;
total?: number;
}
export interface GotEvents<T> {
on(name: 'request', listener: (request: http.ClientRequest) => void): T;
on(name: 'response', listener: (response: Response) => void): T;
on(name: 'redirect', listener: (response: Response, nextOptions: NormalizedOptions) => void): T;
on(name: 'uploadProgress' | 'downloadProgress', listener: (progress: Progress) => void): T;
}
export interface CancelableRequest<T extends Response | Response['body']> extends PCancelable<T>, GotEvents<CancelableRequest<T>> {
json<ReturnType>(): CancelableRequest<ReturnType>;
buffer(): CancelableRequest<Buffer>;
text(): CancelableRequest<string>;
}