got 11.4.0 → 11.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/source/as-promise/index.d.ts +1 -3
- package/dist/source/as-promise/index.js +56 -117
- package/dist/source/as-promise/normalize-arguments.d.ts +3 -0
- package/dist/source/as-promise/normalize-arguments.js +78 -0
- package/dist/source/as-promise/parse-body.d.ts +3 -0
- package/dist/source/as-promise/parse-body.js +25 -0
- package/dist/source/as-promise/types.d.ts +235 -58
- package/dist/source/as-promise/types.js +29 -16
- package/dist/source/{as-promise → core}/calculate-retry-delay.d.ts +2 -1
- package/dist/source/core/calculate-retry-delay.js +29 -0
- package/dist/source/core/index.d.ts +851 -28
- package/dist/source/core/index.js +291 -55
- package/dist/source/core/utils/dns-ip-version.js +1 -0
- package/dist/source/core/utils/get-body-size.d.ts +2 -4
- package/dist/source/core/utils/is-form-data.d.ts +2 -2
- package/dist/source/core/utils/is-response-ok.d.ts +2 -0
- package/dist/source/core/utils/is-response-ok.js +8 -0
- package/dist/source/core/utils/options-to-url.d.ts +0 -1
- package/dist/source/core/utils/timed-out.js +1 -0
- package/dist/source/core/utils/url-to-options.d.ts +0 -1
- package/dist/source/core/utils/weakable-map.d.ts +1 -1
- package/dist/source/create.js +42 -13
- package/dist/source/index.js +16 -6
- package/dist/source/types.d.ts +245 -8
- package/dist/source/utils/deep-freeze.d.ts +1 -1
- package/dist/source/utils/deprecation-warning.js +1 -1
- package/package.json +30 -29
- package/readme.md +171 -35
- package/dist/source/as-promise/calculate-retry-delay.js +0 -38
- package/dist/source/as-promise/core.d.ts +0 -13
- package/dist/source/as-promise/core.js +0 -124
|
@@ -13,6 +13,7 @@ import ResponseLike = require('responselike');
|
|
|
13
13
|
import { Delays, TimeoutError as TimedOutTimeoutError } from './utils/timed-out';
|
|
14
14
|
import { URLOptions } from './utils/options-to-url';
|
|
15
15
|
import { DnsLookupIpVersion } from './utils/dns-ip-version';
|
|
16
|
+
import { PromiseOnly } from '../as-promise/types';
|
|
16
17
|
declare type HttpRequestFunction = typeof httpRequest;
|
|
17
18
|
declare type Error = NodeJS.ErrnoException;
|
|
18
19
|
declare const kRequest: unique symbol;
|
|
@@ -31,6 +32,7 @@ declare const kTriggerRead: unique symbol;
|
|
|
31
32
|
declare const kBody: unique symbol;
|
|
32
33
|
declare const kJobs: unique symbol;
|
|
33
34
|
declare const kOriginalResponse: unique symbol;
|
|
35
|
+
declare const kRetryTimeout: unique symbol;
|
|
34
36
|
export declare const kIsNormalizedAlready: unique symbol;
|
|
35
37
|
export interface Agents {
|
|
36
38
|
http?: http.Agent;
|
|
@@ -39,32 +41,145 @@ export interface Agents {
|
|
|
39
41
|
}
|
|
40
42
|
export declare const withoutBody: ReadonlySet<string>;
|
|
41
43
|
export interface ToughCookieJar {
|
|
42
|
-
getCookieString(currentUrl: string, options: {
|
|
44
|
+
getCookieString: ((currentUrl: string, options: {
|
|
43
45
|
[key: string]: unknown;
|
|
44
|
-
}, cb: (err: Error | null, cookies: string) => void): void;
|
|
45
|
-
|
|
46
|
-
setCookie(cookieOrString: unknown, currentUrl: string, options: {
|
|
46
|
+
}, cb: (err: Error | null, cookies: string) => void) => void) & ((url: string, callback: (error: Error | null, cookieHeader: string) => void) => void);
|
|
47
|
+
setCookie: ((cookieOrString: unknown, currentUrl: string, options: {
|
|
47
48
|
[key: string]: unknown;
|
|
48
|
-
}, cb: (err: Error | null, cookie: unknown) => void): void;
|
|
49
|
-
setCookie(rawCookie: string, url: string, callback: (error: Error | null, result: unknown) => void): void;
|
|
49
|
+
}, cb: (err: Error | null, cookie: unknown) => void) => void) & ((rawCookie: string, url: string, callback: (error: Error | null, result: unknown) => void) => void);
|
|
50
50
|
}
|
|
51
51
|
export interface PromiseCookieJar {
|
|
52
|
-
getCookieString(url: string)
|
|
53
|
-
setCookie(rawCookie: string, url: string)
|
|
52
|
+
getCookieString: (url: string) => Promise<string>;
|
|
53
|
+
setCookie: (rawCookie: string, url: string) => Promise<unknown>;
|
|
54
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
All available HTTP request methods provided by Got.
|
|
57
|
+
*/
|
|
55
58
|
export declare type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'HEAD' | 'DELETE' | 'OPTIONS' | 'TRACE' | 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'options' | 'trace';
|
|
56
59
|
declare type Promisable<T> = T | Promise<T>;
|
|
57
|
-
export declare type InitHook = (options: Options) =>
|
|
60
|
+
export declare type InitHook = (options: Options) => void;
|
|
58
61
|
export declare type BeforeRequestHook = (options: NormalizedOptions) => Promisable<void | Response | ResponseLike>;
|
|
59
62
|
export declare type BeforeRedirectHook = (options: NormalizedOptions, response: Response) => Promisable<void>;
|
|
60
63
|
export declare type BeforeErrorHook = (error: RequestError) => Promisable<RequestError>;
|
|
61
|
-
export
|
|
64
|
+
export declare type BeforeRetryHook = (options: NormalizedOptions, error?: RequestError, retryCount?: number) => void | Promise<void>;
|
|
65
|
+
interface PlainHooks {
|
|
66
|
+
/**
|
|
67
|
+
Called with plain request options, right before their normalization.
|
|
68
|
+
This is especially useful in conjunction with `got.extend()` when the input needs custom handling.
|
|
69
|
+
|
|
70
|
+
__Note #1__: This hook must be synchronous!
|
|
71
|
+
|
|
72
|
+
__Note #2__: Errors in this hook will be converted into an instances of `RequestError`.
|
|
73
|
+
|
|
74
|
+
__Note #3__: The options object may not have a `url` property.
|
|
75
|
+
To modify it, use a `beforeRequest` hook instead.
|
|
76
|
+
|
|
77
|
+
@default []
|
|
78
|
+
*/
|
|
62
79
|
init?: InitHook[];
|
|
80
|
+
/**
|
|
81
|
+
Called with normalized request options.
|
|
82
|
+
Got will make no further changes to the request before it is sent.
|
|
83
|
+
This is especially useful in conjunction with `got.extend()` when you want to create an API client that, for example, uses HMAC-signing.
|
|
84
|
+
|
|
85
|
+
@default []
|
|
86
|
+
*/
|
|
63
87
|
beforeRequest?: BeforeRequestHook[];
|
|
88
|
+
/**
|
|
89
|
+
Called with normalized request options and the redirect response.
|
|
90
|
+
Got will make no further changes to the request.
|
|
91
|
+
This is especially useful when you want to avoid dead sites.
|
|
92
|
+
|
|
93
|
+
@default []
|
|
94
|
+
|
|
95
|
+
@example
|
|
96
|
+
```
|
|
97
|
+
const got = require('got');
|
|
98
|
+
|
|
99
|
+
got('https://example.com', {
|
|
100
|
+
hooks: {
|
|
101
|
+
beforeRedirect: [
|
|
102
|
+
(options, response) => {
|
|
103
|
+
if (options.hostname === 'deadSite') {
|
|
104
|
+
options.hostname = 'fallbackSite';
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
]
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
*/
|
|
64
112
|
beforeRedirect?: BeforeRedirectHook[];
|
|
113
|
+
/**
|
|
114
|
+
Called with an `Error` instance.
|
|
115
|
+
The error is passed to the hook right before it's thrown.
|
|
116
|
+
This is especially useful when you want to have more detailed errors.
|
|
117
|
+
|
|
118
|
+
__Note__: Errors thrown while normalizing input options are thrown directly and not part of this hook.
|
|
119
|
+
|
|
120
|
+
@default []
|
|
121
|
+
|
|
122
|
+
@example
|
|
123
|
+
```
|
|
124
|
+
const got = require('got');
|
|
125
|
+
|
|
126
|
+
got('https://api.github.com/some-endpoint', {
|
|
127
|
+
hooks: {
|
|
128
|
+
beforeError: [
|
|
129
|
+
error => {
|
|
130
|
+
const {response} = error;
|
|
131
|
+
if (response && response.body) {
|
|
132
|
+
error.name = 'GitHubError';
|
|
133
|
+
error.message = `${response.body.message} (${response.statusCode})`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return error;
|
|
137
|
+
}
|
|
138
|
+
]
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
```
|
|
142
|
+
*/
|
|
65
143
|
beforeError?: BeforeErrorHook[];
|
|
144
|
+
/**
|
|
145
|
+
Called with normalized request options, the error and the retry count.
|
|
146
|
+
Got will make no further changes to the request.
|
|
147
|
+
This is especially useful when some extra work is required before the next try.
|
|
148
|
+
|
|
149
|
+
__Note__: When using streams, this hook is ignored.
|
|
150
|
+
__Note__: When retrying in a `afterResponse` hook, all remaining `beforeRetry` hooks will be called without the `error` and `retryCount` arguments.
|
|
151
|
+
|
|
152
|
+
@default []
|
|
153
|
+
|
|
154
|
+
@example
|
|
155
|
+
```
|
|
156
|
+
const got = require('got');
|
|
157
|
+
|
|
158
|
+
got.post('https://example.com', {
|
|
159
|
+
hooks: {
|
|
160
|
+
beforeRetry: [
|
|
161
|
+
(options, error, retryCount) => {
|
|
162
|
+
if (error.response.statusCode === 413) { // Payload too large
|
|
163
|
+
options.body = getNewBody();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
]
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
```
|
|
170
|
+
*/
|
|
171
|
+
beforeRetry?: BeforeRetryHook[];
|
|
66
172
|
}
|
|
67
|
-
|
|
173
|
+
/**
|
|
174
|
+
All available hook of Got.
|
|
175
|
+
*/
|
|
176
|
+
export interface Hooks extends PromiseOnly.Hooks, PlainHooks {
|
|
177
|
+
}
|
|
178
|
+
declare type PlainHookEvent = 'init' | 'beforeRequest' | 'beforeRedirect' | 'beforeError' | 'beforeRetry';
|
|
179
|
+
/**
|
|
180
|
+
All hook events acceptable by Got.
|
|
181
|
+
*/
|
|
182
|
+
export declare type HookEvent = PromiseOnly.HookEvent | PlainHookEvent;
|
|
68
183
|
export declare const knownHookEvents: HookEvent[];
|
|
69
184
|
declare type AcceptableResponse = IncomingMessageWithTimings | ResponseLike;
|
|
70
185
|
declare type AcceptableRequestResult = AcceptableResponse | ClientRequest | Promise<AcceptableResponse | ClientRequest> | undefined;
|
|
@@ -73,58 +188,558 @@ export declare type Headers = Record<string, string | string[] | undefined>;
|
|
|
73
188
|
declare type CheckServerIdentityFunction = (hostname: string, certificate: DetailedPeerCertificate) => Error | void;
|
|
74
189
|
export declare type ParseJsonFunction = (text: string) => unknown;
|
|
75
190
|
export declare type StringifyJsonFunction = (object: unknown) => string;
|
|
76
|
-
export interface
|
|
191
|
+
export interface RetryObject {
|
|
192
|
+
attemptCount: number;
|
|
193
|
+
retryOptions: RequiredRetryOptions;
|
|
194
|
+
error: TimeoutError | RequestError;
|
|
195
|
+
computedValue: number;
|
|
196
|
+
retryAfter?: number;
|
|
197
|
+
}
|
|
198
|
+
export declare type RetryFunction = (retryObject: RetryObject) => number | Promise<number>;
|
|
199
|
+
/**
|
|
200
|
+
An object representing `limit`, `calculateDelay`, `methods`, `statusCodes`, `maxRetryAfter` and `errorCodes` fields for maximum retry count, retry handler, allowed methods, allowed status codes, maximum [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) time and allowed error codes.
|
|
201
|
+
|
|
202
|
+
Delays between retries counts with function `1000 * Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 1).
|
|
203
|
+
|
|
204
|
+
The `calculateDelay` property is a `function` that receives an object with `attemptCount`, `retryOptions`, `error` and `computedValue` properties for current retry count, the retry options, error and default computed value.
|
|
205
|
+
The function must return a delay in milliseconds (or a Promise resolving with it) (`0` return value cancels retry).
|
|
206
|
+
|
|
207
|
+
By default, it retries *only* on the specified methods, status codes, and on these network errors:
|
|
208
|
+
- `ETIMEDOUT`: One of the [timeout](#timeout) limits were reached.
|
|
209
|
+
- `ECONNRESET`: Connection was forcibly closed by a peer.
|
|
210
|
+
- `EADDRINUSE`: Could not bind to any free port.
|
|
211
|
+
- `ECONNREFUSED`: Connection was refused by the server.
|
|
212
|
+
- `EPIPE`: The remote side of the stream being written has been closed.
|
|
213
|
+
- `ENOTFOUND`: Couldn't resolve the hostname to an IP address.
|
|
214
|
+
- `ENETUNREACH`: No internet connection.
|
|
215
|
+
- `EAI_AGAIN`: DNS lookup timed out.
|
|
216
|
+
|
|
217
|
+
__Note__: If `maxRetryAfter` is set to `undefined`, it will use `options.timeout`.
|
|
218
|
+
__Note__: If [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) header is greater than `maxRetryAfter`, it will cancel the request.
|
|
219
|
+
*/
|
|
220
|
+
export interface RequiredRetryOptions {
|
|
221
|
+
limit: number;
|
|
222
|
+
methods: Method[];
|
|
223
|
+
statusCodes: number[];
|
|
224
|
+
errorCodes: string[];
|
|
225
|
+
calculateDelay: RetryFunction;
|
|
226
|
+
maxRetryAfter?: number;
|
|
227
|
+
}
|
|
228
|
+
export interface CacheOptions {
|
|
229
|
+
shared?: boolean;
|
|
230
|
+
cacheHeuristic?: number;
|
|
231
|
+
immutableMinTimeToLive?: number;
|
|
232
|
+
ignoreCargoCult?: boolean;
|
|
233
|
+
}
|
|
234
|
+
interface PlainOptions extends URLOptions {
|
|
235
|
+
/**
|
|
236
|
+
Custom request function.
|
|
237
|
+
The main purpose of this is to [support HTTP2 using a wrapper](https://github.com/szmarczak/http2-wrapper).
|
|
238
|
+
|
|
239
|
+
@default http.request | https.request
|
|
240
|
+
*/
|
|
77
241
|
request?: RequestFunction;
|
|
242
|
+
/**
|
|
243
|
+
An object representing `http`, `https` and `http2` keys for [`http.Agent`](https://nodejs.org/api/http.html#http_class_http_agent), [`https.Agent`](https://nodejs.org/api/https.html#https_class_https_agent) and [`http2wrapper.Agent`](https://github.com/szmarczak/http2-wrapper#new-http2agentoptions) instance.
|
|
244
|
+
This is necessary because a request to one protocol might redirect to another.
|
|
245
|
+
In such a scenario, Got will switch over to the right protocol agent for you.
|
|
246
|
+
|
|
247
|
+
If a key is not present, it will default to a global agent.
|
|
248
|
+
|
|
249
|
+
@example
|
|
250
|
+
```
|
|
251
|
+
const got = require('got');
|
|
252
|
+
const HttpAgent = require('agentkeepalive');
|
|
253
|
+
const {HttpsAgent} = HttpAgent;
|
|
254
|
+
|
|
255
|
+
got('https://sindresorhus.com', {
|
|
256
|
+
agent: {
|
|
257
|
+
http: new HttpAgent(),
|
|
258
|
+
https: new HttpsAgent()
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
```
|
|
262
|
+
*/
|
|
78
263
|
agent?: Agents | false;
|
|
264
|
+
/**
|
|
265
|
+
Decompress the response automatically.
|
|
266
|
+
This will set the `accept-encoding` header to `gzip, deflate, br` on Node.js 11.7.0+ or `gzip, deflate` for older Node.js versions, unless you set it yourself.
|
|
267
|
+
|
|
268
|
+
Brotli (`br`) support requires Node.js 11.7.0 or later.
|
|
269
|
+
|
|
270
|
+
If this is disabled, a compressed response is returned as a `Buffer`.
|
|
271
|
+
This may be useful if you want to handle decompression yourself or stream the raw compressed data.
|
|
272
|
+
|
|
273
|
+
@default true
|
|
274
|
+
*/
|
|
79
275
|
decompress?: boolean;
|
|
276
|
+
/**
|
|
277
|
+
Milliseconds to wait for the server to end the response before aborting the request with `got.TimeoutError` error (a.k.a. `request` property).
|
|
278
|
+
By default, there's no timeout.
|
|
279
|
+
|
|
280
|
+
This also accepts an `object` with the following fields to constrain the duration of each phase of the request lifecycle:
|
|
281
|
+
|
|
282
|
+
- `lookup` starts when a socket is assigned and ends when the hostname has been resolved.
|
|
283
|
+
Does not apply when using a Unix domain socket.
|
|
284
|
+
- `connect` starts when `lookup` completes (or when the socket is assigned if lookup does not apply to the request) and ends when the socket is connected.
|
|
285
|
+
- `secureConnect` starts when `connect` completes and ends when the handshaking process completes (HTTPS only).
|
|
286
|
+
- `socket` starts when the socket is connected. See [request.setTimeout](https://nodejs.org/api/http.html#http_request_settimeout_timeout_callback).
|
|
287
|
+
- `response` starts when the request has been written to the socket and ends when the response headers are received.
|
|
288
|
+
- `send` starts when the socket is connected and ends with the request has been written to the socket.
|
|
289
|
+
- `request` starts when the request is initiated and ends when the response's end event fires.
|
|
290
|
+
*/
|
|
80
291
|
timeout?: Delays | number;
|
|
292
|
+
/**
|
|
293
|
+
When specified, `prefixUrl` will be prepended to `url`.
|
|
294
|
+
The prefix can be any valid URL, either relative or absolute.
|
|
295
|
+
A trailing slash `/` is optional - one will be added automatically.
|
|
296
|
+
|
|
297
|
+
__Note__: `prefixUrl` will be ignored if the `url` argument is a URL instance.
|
|
298
|
+
|
|
299
|
+
__Note__: Leading slashes in `input` are disallowed when using this option to enforce consistency and avoid confusion.
|
|
300
|
+
For example, when the prefix URL is `https://example.com/foo` and the input is `/bar`, there's ambiguity whether the resulting URL would become `https://example.com/foo/bar` or `https://example.com/bar`.
|
|
301
|
+
The latter is used by browsers.
|
|
302
|
+
|
|
303
|
+
__Tip__: Useful when used with `got.extend()` to create niche-specific Got instances.
|
|
304
|
+
|
|
305
|
+
__Tip__: You can change `prefixUrl` using hooks as long as the URL still includes the `prefixUrl`.
|
|
306
|
+
If the URL doesn't include it anymore, it will throw.
|
|
307
|
+
|
|
308
|
+
@example
|
|
309
|
+
```
|
|
310
|
+
const got = require('got');
|
|
311
|
+
|
|
312
|
+
(async () => {
|
|
313
|
+
await got('unicorn', {prefixUrl: 'https://cats.com'});
|
|
314
|
+
//=> 'https://cats.com/unicorn'
|
|
315
|
+
|
|
316
|
+
const instance = got.extend({
|
|
317
|
+
prefixUrl: 'https://google.com'
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
await instance('unicorn', {
|
|
321
|
+
hooks: {
|
|
322
|
+
beforeRequest: [
|
|
323
|
+
options => {
|
|
324
|
+
options.prefixUrl = 'https://cats.com';
|
|
325
|
+
}
|
|
326
|
+
]
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
//=> 'https://cats.com/unicorn'
|
|
330
|
+
})();
|
|
331
|
+
```
|
|
332
|
+
*/
|
|
81
333
|
prefixUrl?: string | URL;
|
|
334
|
+
/**
|
|
335
|
+
__Note #1__: The `body` option cannot be used with the `json` or `form` option.
|
|
336
|
+
|
|
337
|
+
__Note #2__: If you provide this option, `got.stream()` will be read-only.
|
|
338
|
+
|
|
339
|
+
__Note #3__: If you provide a payload with the `GET` or `HEAD` method, it will throw a `TypeError` unless the method is `GET` and the `allowGetBody` option is set to `true`.
|
|
340
|
+
|
|
341
|
+
__Note #4__: This option is not enumerable and will not be merged with the instance defaults.
|
|
342
|
+
|
|
343
|
+
The `content-length` header will be automatically set if `body` is a `string` / `Buffer` / `fs.createReadStream` instance / [`form-data` instance](https://github.com/form-data/form-data), and `content-length` and `transfer-encoding` are not manually set in `options.headers`.
|
|
344
|
+
*/
|
|
82
345
|
body?: string | Buffer | Readable;
|
|
346
|
+
/**
|
|
347
|
+
The form body is converted to a query string using [`(new URLSearchParams(object)).toString()`](https://nodejs.org/api/url.html#url_constructor_new_urlsearchparams_obj).
|
|
348
|
+
|
|
349
|
+
If the `Content-Type` header is not present, it will be set to `application/x-www-form-urlencoded`.
|
|
350
|
+
|
|
351
|
+
__Note #1__: If you provide this option, `got.stream()` will be read-only.
|
|
352
|
+
|
|
353
|
+
__Note #2__: This option is not enumerable and will not be merged with the instance defaults.
|
|
354
|
+
*/
|
|
83
355
|
form?: {
|
|
84
356
|
[key: string]: any;
|
|
85
357
|
};
|
|
358
|
+
/**
|
|
359
|
+
JSON body. If the `Content-Type` header is not set, it will be set to `application/json`.
|
|
360
|
+
|
|
361
|
+
__Note #1__: If you provide this option, `got.stream()` will be read-only.
|
|
362
|
+
|
|
363
|
+
__Note #2__: This option is not enumerable and will not be merged with the instance defaults.
|
|
364
|
+
*/
|
|
86
365
|
json?: {
|
|
87
366
|
[key: string]: any;
|
|
88
367
|
};
|
|
368
|
+
/**
|
|
369
|
+
The URL to request, as a string, a [`https.request` options object](https://nodejs.org/api/https.html#https_https_request_options_callback), or a [WHATWG `URL`](https://nodejs.org/api/url.html#url_class_url).
|
|
370
|
+
|
|
371
|
+
Properties from `options` will override properties in the parsed `url`.
|
|
372
|
+
|
|
373
|
+
If no protocol is specified, it will throw a `TypeError`.
|
|
374
|
+
|
|
375
|
+
__Note__: The query string is **not** parsed as search params.
|
|
376
|
+
|
|
377
|
+
@example
|
|
378
|
+
```
|
|
379
|
+
got('https://example.com/?query=a b'); //=> https://example.com/?query=a%20b
|
|
380
|
+
got('https://example.com/', {searchParams: {query: 'a b'}}); //=> https://example.com/?query=a+b
|
|
381
|
+
|
|
382
|
+
// The query string is overridden by `searchParams`
|
|
383
|
+
got('https://example.com/?query=a b', {searchParams: {query: 'a b'}}); //=> https://example.com/?query=a+b
|
|
384
|
+
```
|
|
385
|
+
*/
|
|
89
386
|
url?: string | URL;
|
|
387
|
+
/**
|
|
388
|
+
Cookie support. You don't have to care about parsing or how to store them.
|
|
389
|
+
|
|
390
|
+
__Note__: If you provide this option, `options.headers.cookie` will be overridden.
|
|
391
|
+
*/
|
|
90
392
|
cookieJar?: PromiseCookieJar | ToughCookieJar;
|
|
393
|
+
/**
|
|
394
|
+
Ignore invalid cookies instead of throwing an error.
|
|
395
|
+
Only useful when the `cookieJar` option has been set. Not recommended.
|
|
396
|
+
|
|
397
|
+
@default false
|
|
398
|
+
*/
|
|
91
399
|
ignoreInvalidCookies?: boolean;
|
|
400
|
+
/**
|
|
401
|
+
Query string that will be added to the request URL.
|
|
402
|
+
This will override the query string in `url`.
|
|
403
|
+
|
|
404
|
+
If you need to pass in an array, you can do it using a `URLSearchParams` instance.
|
|
405
|
+
|
|
406
|
+
@example
|
|
407
|
+
```
|
|
408
|
+
const got = require('got');
|
|
409
|
+
|
|
410
|
+
const searchParams = new URLSearchParams([['key', 'a'], ['key', 'b']]);
|
|
411
|
+
|
|
412
|
+
got('https://example.com', {searchParams});
|
|
413
|
+
|
|
414
|
+
console.log(searchParams.toString());
|
|
415
|
+
//=> 'key=a&key=b'
|
|
416
|
+
```
|
|
417
|
+
*/
|
|
92
418
|
searchParams?: string | {
|
|
93
419
|
[key: string]: string | number | boolean | null | undefined;
|
|
94
420
|
} | URLSearchParams;
|
|
421
|
+
/**
|
|
422
|
+
An instance of [`CacheableLookup`](https://github.com/szmarczak/cacheable-lookup) used for making DNS lookups.
|
|
423
|
+
Useful when making lots of requests to different *public* hostnames.
|
|
424
|
+
|
|
425
|
+
`CacheableLookup` uses `dns.resolver4(..)` and `dns.resolver6(...)` under the hood and fall backs to `dns.lookup(...)` when the first two fail, which may lead to additional delay.
|
|
426
|
+
|
|
427
|
+
__Note__: This should stay disabled when making requests to internal hostnames such as `localhost`, `database.local` etc.
|
|
428
|
+
|
|
429
|
+
@default false
|
|
430
|
+
*/
|
|
95
431
|
dnsCache?: CacheableLookup | boolean;
|
|
96
|
-
|
|
432
|
+
/**
|
|
433
|
+
User data. In contrast to other options, `context` is not enumerable.
|
|
434
|
+
|
|
435
|
+
__Note__: The object is never merged, it's just passed through.
|
|
436
|
+
Got will not modify the object in any way.
|
|
437
|
+
|
|
438
|
+
@example
|
|
439
|
+
```
|
|
440
|
+
const got = require('got');
|
|
441
|
+
|
|
442
|
+
const instance = got.extend({
|
|
443
|
+
hooks: {
|
|
444
|
+
beforeRequest: [
|
|
445
|
+
options => {
|
|
446
|
+
if (!options.context || !options.context.token) {
|
|
447
|
+
throw new Error('Token required');
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
options.headers.token = options.context.token;
|
|
451
|
+
}
|
|
452
|
+
]
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
(async () => {
|
|
457
|
+
const context = {
|
|
458
|
+
token: 'secret'
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
const response = await instance('https://httpbin.org/headers', {context});
|
|
462
|
+
|
|
463
|
+
// Let's see the headers
|
|
464
|
+
console.log(response.body);
|
|
465
|
+
})();
|
|
466
|
+
```
|
|
467
|
+
*/
|
|
468
|
+
context?: Record<string, unknown>;
|
|
469
|
+
/**
|
|
470
|
+
Hooks allow modifications during the request lifecycle.
|
|
471
|
+
Hook functions may be async and are run serially.
|
|
472
|
+
*/
|
|
97
473
|
hooks?: Hooks;
|
|
474
|
+
/**
|
|
475
|
+
Defines if redirect responses should be followed automatically.
|
|
476
|
+
|
|
477
|
+
Note that if a `303` is sent by the server in response to any request type (`POST`, `DELETE`, etc.), Got will automatically request the resource pointed to in the location header via `GET`.
|
|
478
|
+
This is in accordance with [the spec](https://tools.ietf.org/html/rfc7231#section-6.4.4).
|
|
479
|
+
|
|
480
|
+
@default true
|
|
481
|
+
*/
|
|
98
482
|
followRedirect?: boolean;
|
|
483
|
+
/**
|
|
484
|
+
If exceeded, the request will be aborted and a `MaxRedirectsError` will be thrown.
|
|
485
|
+
|
|
486
|
+
@default 10
|
|
487
|
+
*/
|
|
99
488
|
maxRedirects?: number;
|
|
489
|
+
/**
|
|
490
|
+
A cache adapter instance for storing cached response data.
|
|
491
|
+
|
|
492
|
+
@default false
|
|
493
|
+
*/
|
|
100
494
|
cache?: string | CacheableRequest.StorageAdapter | false;
|
|
495
|
+
/**
|
|
496
|
+
Determines if a `got.HTTPError` is thrown for unsuccessful responses.
|
|
497
|
+
|
|
498
|
+
If this is disabled, requests that encounter an error status code will be resolved with the `response` instead of throwing.
|
|
499
|
+
This may be useful if you are checking for resource availability and are expecting error responses.
|
|
500
|
+
|
|
501
|
+
@default true
|
|
502
|
+
*/
|
|
101
503
|
throwHttpErrors?: boolean;
|
|
102
504
|
username?: string;
|
|
103
505
|
password?: string;
|
|
506
|
+
/**
|
|
507
|
+
If set to `true`, Got will additionally accept HTTP2 requests.
|
|
508
|
+
|
|
509
|
+
It will choose either HTTP/1.1 or HTTP/2 depending on the ALPN protocol.
|
|
510
|
+
|
|
511
|
+
__Note__: Overriding `options.request` will disable HTTP2 support.
|
|
512
|
+
|
|
513
|
+
__Note__: This option will default to `true` in the next upcoming major release.
|
|
514
|
+
|
|
515
|
+
@default false
|
|
516
|
+
|
|
517
|
+
@example
|
|
518
|
+
```
|
|
519
|
+
const got = require('got');
|
|
520
|
+
|
|
521
|
+
(async () => {
|
|
522
|
+
const {headers} = await got('https://nghttp2.org/httpbin/anything', {http2: true});
|
|
523
|
+
console.log(headers.via);
|
|
524
|
+
//=> '2 nghttpx'
|
|
525
|
+
})();
|
|
526
|
+
```
|
|
527
|
+
*/
|
|
104
528
|
http2?: boolean;
|
|
529
|
+
/**
|
|
530
|
+
Set this to `true` to allow sending body for the `GET` method.
|
|
531
|
+
However, the [HTTP/2 specification](https://tools.ietf.org/html/rfc7540#section-8.1.3) says that `An HTTP GET request includes request header fields and no payload body`, therefore when using the HTTP/2 protocol this option will have no effect.
|
|
532
|
+
This option is only meant to interact with non-compliant servers when you have no other choice.
|
|
533
|
+
|
|
534
|
+
__Note__: The [RFC 7321](https://tools.ietf.org/html/rfc7231#section-4.3.1) doesn't specify any particular behavior for the GET method having a payload, therefore __it's considered an [anti-pattern](https://en.wikipedia.org/wiki/Anti-pattern)__.
|
|
535
|
+
|
|
536
|
+
@default false
|
|
537
|
+
*/
|
|
105
538
|
allowGetBody?: boolean;
|
|
106
539
|
lookup?: CacheableLookup['lookup'];
|
|
540
|
+
/**
|
|
541
|
+
Request headers.
|
|
542
|
+
|
|
543
|
+
Existing headers will be overwritten. Headers set to `undefined` will be omitted.
|
|
544
|
+
|
|
545
|
+
@default {}
|
|
546
|
+
*/
|
|
107
547
|
headers?: Headers;
|
|
548
|
+
/**
|
|
549
|
+
By default, redirects will use [method rewriting](https://tools.ietf.org/html/rfc7231#section-6.4).
|
|
550
|
+
For example, when sending a POST request and receiving a `302`, it will resend the body to the new location using the same HTTP method (`POST` in this case).
|
|
551
|
+
|
|
552
|
+
@default true
|
|
553
|
+
*/
|
|
108
554
|
methodRewriting?: boolean;
|
|
555
|
+
/**
|
|
556
|
+
Indicates which DNS record family to use.
|
|
557
|
+
|
|
558
|
+
Values:
|
|
559
|
+
- `auto`: IPv4 (if present) or IPv6
|
|
560
|
+
- `ipv4`: Only IPv4
|
|
561
|
+
- `ipv6`: Only IPv6
|
|
562
|
+
|
|
563
|
+
__Note__: If you are using the undocumented option `family`, `dnsLookupIpVersion` will override it.
|
|
564
|
+
|
|
565
|
+
@default 'auto'
|
|
566
|
+
*/
|
|
109
567
|
dnsLookupIpVersion?: DnsLookupIpVersion;
|
|
568
|
+
/**
|
|
569
|
+
A function used to parse JSON responses.
|
|
570
|
+
|
|
571
|
+
@example
|
|
572
|
+
```
|
|
573
|
+
const got = require('got');
|
|
574
|
+
const Bourne = require('@hapi/bourne');
|
|
575
|
+
|
|
576
|
+
(async () => {
|
|
577
|
+
const parsed = await got('https://example.com', {
|
|
578
|
+
parseJson: text => Bourne.parse(text)
|
|
579
|
+
}).json();
|
|
580
|
+
|
|
581
|
+
console.log(parsed);
|
|
582
|
+
})();
|
|
583
|
+
```
|
|
584
|
+
*/
|
|
110
585
|
parseJson?: ParseJsonFunction;
|
|
586
|
+
/**
|
|
587
|
+
A function used to stringify the body of JSON requests.
|
|
588
|
+
|
|
589
|
+
@example
|
|
590
|
+
```
|
|
591
|
+
const got = require('got');
|
|
592
|
+
|
|
593
|
+
(async () => {
|
|
594
|
+
await got.post('https://example.com', {
|
|
595
|
+
stringifyJson: object => JSON.stringify(object, (key, value) => {
|
|
596
|
+
if (key.startsWith('_')) {
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
return value;
|
|
601
|
+
}),
|
|
602
|
+
json: {
|
|
603
|
+
some: 'payload',
|
|
604
|
+
_ignoreMe: 1234
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
})();
|
|
608
|
+
```
|
|
609
|
+
|
|
610
|
+
@example
|
|
611
|
+
```
|
|
612
|
+
const got = require('got');
|
|
613
|
+
|
|
614
|
+
(async () => {
|
|
615
|
+
await got.post('https://example.com', {
|
|
616
|
+
stringifyJson: object => JSON.stringify(object, (key, value) => {
|
|
617
|
+
if (typeof value === 'number') {
|
|
618
|
+
return value.toString();
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
return value;
|
|
622
|
+
}),
|
|
623
|
+
json: {
|
|
624
|
+
some: 'payload',
|
|
625
|
+
number: 1
|
|
626
|
+
}
|
|
627
|
+
});
|
|
628
|
+
})();
|
|
629
|
+
```
|
|
630
|
+
*/
|
|
111
631
|
stringifyJson?: StringifyJsonFunction;
|
|
632
|
+
/**
|
|
633
|
+
An object representing `limit`, `calculateDelay`, `methods`, `statusCodes`, `maxRetryAfter` and `errorCodes` fields for maximum retry count, retry handler, allowed methods, allowed status codes, maximum [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) time and allowed error codes.
|
|
634
|
+
|
|
635
|
+
Delays between retries counts with function `1000 * Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 1).
|
|
636
|
+
|
|
637
|
+
The `calculateDelay` property is a `function` that receives an object with `attemptCount`, `retryOptions`, `error` and `computedValue` properties for current retry count, the retry options, error and default computed value.
|
|
638
|
+
The function must return a delay in milliseconds (or a Promise resolving with it) (`0` return value cancels retry).
|
|
639
|
+
|
|
640
|
+
By default, it retries *only* on the specified methods, status codes, and on these network errors:
|
|
641
|
+
|
|
642
|
+
- `ETIMEDOUT`: One of the [timeout](#timeout) limits were reached.
|
|
643
|
+
- `ECONNRESET`: Connection was forcibly closed by a peer.
|
|
644
|
+
- `EADDRINUSE`: Could not bind to any free port.
|
|
645
|
+
- `ECONNREFUSED`: Connection was refused by the server.
|
|
646
|
+
- `EPIPE`: The remote side of the stream being written has been closed.
|
|
647
|
+
- `ENOTFOUND`: Couldn't resolve the hostname to an IP address.
|
|
648
|
+
- `ENETUNREACH`: No internet connection.
|
|
649
|
+
- `EAI_AGAIN`: DNS lookup timed out.
|
|
650
|
+
|
|
651
|
+
__Note__: If `maxRetryAfter` is set to `undefined`, it will use `options.timeout`.
|
|
652
|
+
__Note__: If [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) header is greater than `maxRetryAfter`, it will cancel the request.
|
|
653
|
+
*/
|
|
654
|
+
retry?: Partial<RequiredRetryOptions> | number;
|
|
655
|
+
/**
|
|
656
|
+
The IP address used to send the request from.
|
|
657
|
+
*/
|
|
112
658
|
localAddress?: string;
|
|
113
659
|
socketPath?: string;
|
|
660
|
+
/**
|
|
661
|
+
The HTTP method used to make the request.
|
|
662
|
+
|
|
663
|
+
@default 'GET'
|
|
664
|
+
*/
|
|
114
665
|
method?: Method;
|
|
115
666
|
createConnection?: (options: http.RequestOptions, oncreate: (error: Error, socket: Socket) => void) => Socket;
|
|
667
|
+
cacheOptions?: CacheOptions;
|
|
668
|
+
/**
|
|
669
|
+
If set to `false`, all invalid SSL certificates will be ignored and no error will be thrown.
|
|
670
|
+
|
|
671
|
+
If set to `true`, it will throw an error whenever an invalid SSL certificate is detected.
|
|
672
|
+
|
|
673
|
+
We strongly recommend to have this set to `true` for security reasons.
|
|
674
|
+
|
|
675
|
+
@default true
|
|
676
|
+
|
|
677
|
+
@example
|
|
678
|
+
```
|
|
679
|
+
const got = require('got');
|
|
680
|
+
|
|
681
|
+
(async () => {
|
|
682
|
+
// Correct:
|
|
683
|
+
await got('https://example.com', {rejectUnauthorized: true});
|
|
684
|
+
|
|
685
|
+
// You can disable it when developing an HTTPS app:
|
|
686
|
+
await got('https://localhost', {rejectUnauthorized: false});
|
|
687
|
+
|
|
688
|
+
// Never do this:
|
|
689
|
+
await got('https://example.com', {rejectUnauthorized: false});
|
|
690
|
+
})();
|
|
691
|
+
```
|
|
692
|
+
*/
|
|
116
693
|
rejectUnauthorized?: boolean;
|
|
694
|
+
/**
|
|
695
|
+
Options for the advanced HTTPS API.
|
|
696
|
+
*/
|
|
117
697
|
https?: HTTPSOptions;
|
|
118
698
|
}
|
|
699
|
+
export interface Options extends PromiseOnly.Options, PlainOptions {
|
|
700
|
+
}
|
|
119
701
|
export interface HTTPSOptions {
|
|
120
702
|
rejectUnauthorized?: https.RequestOptions['rejectUnauthorized'];
|
|
121
703
|
checkServerIdentity?: CheckServerIdentityFunction;
|
|
704
|
+
/**
|
|
705
|
+
Override the default Certificate Authorities ([from Mozilla](https://ccadb-public.secure.force.com/mozilla/IncludedCACertificateReport)).
|
|
706
|
+
|
|
707
|
+
@example
|
|
708
|
+
```
|
|
709
|
+
// Single Certificate Authority
|
|
710
|
+
got('https://example.com', {
|
|
711
|
+
https: {
|
|
712
|
+
certificateAuthority: fs.readFileSync('./my_ca.pem')
|
|
713
|
+
}
|
|
714
|
+
});
|
|
715
|
+
```
|
|
716
|
+
*/
|
|
122
717
|
certificateAuthority?: SecureContextOptions['ca'];
|
|
718
|
+
/**
|
|
719
|
+
Private keys in [PEM](https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail) format.
|
|
720
|
+
|
|
721
|
+
[PEM](https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail) allows the option of private keys being encrypted.
|
|
722
|
+
Encrypted keys will be decrypted with `options.https.passphrase`.
|
|
723
|
+
|
|
724
|
+
Multiple keys with different passphrases can be provided as an array of `{pem: <string | Buffer>, passphrase: <string>}`
|
|
725
|
+
*/
|
|
123
726
|
key?: SecureContextOptions['key'];
|
|
727
|
+
/**
|
|
728
|
+
[Certificate chains](https://en.wikipedia.org/wiki/X.509#Certificate_chains_and_cross-certification) in [PEM](https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail) format.
|
|
729
|
+
|
|
730
|
+
One cert chain should be provided per private key (`options.https.key`).
|
|
731
|
+
|
|
732
|
+
When providing multiple cert chains, they do not have to be in the same order as their private keys in `options.https.key`.
|
|
733
|
+
|
|
734
|
+
If the intermediate certificates are not provided, the peer will not be able to validate the certificate, and the handshake will fail.
|
|
735
|
+
*/
|
|
124
736
|
certificate?: SecureContextOptions['cert'];
|
|
737
|
+
/**
|
|
738
|
+
The passphrase to decrypt the `options.https.key` (if different keys have different passphrases refer to `options.https.key` documentation).
|
|
739
|
+
*/
|
|
125
740
|
passphrase?: SecureContextOptions['passphrase'];
|
|
126
741
|
}
|
|
127
|
-
|
|
742
|
+
interface NormalizedPlainOptions extends PlainOptions {
|
|
128
743
|
method: Method;
|
|
129
744
|
url: URL;
|
|
130
745
|
timeout: Delays;
|
|
@@ -134,7 +749,7 @@ export interface NormalizedOptions extends Options {
|
|
|
134
749
|
searchParams?: URLSearchParams;
|
|
135
750
|
cookieJar?: PromiseCookieJar;
|
|
136
751
|
headers: Headers;
|
|
137
|
-
context:
|
|
752
|
+
context: Record<string, unknown>;
|
|
138
753
|
hooks: Required<Hooks>;
|
|
139
754
|
followRedirect: boolean;
|
|
140
755
|
maxRedirects: number;
|
|
@@ -150,16 +765,20 @@ export interface NormalizedOptions extends Options {
|
|
|
150
765
|
password: string;
|
|
151
766
|
parseJson: ParseJsonFunction;
|
|
152
767
|
stringifyJson: StringifyJsonFunction;
|
|
768
|
+
retry: RequiredRetryOptions;
|
|
769
|
+
cacheOptions: CacheOptions;
|
|
153
770
|
[kRequest]: HttpRequestFunction;
|
|
154
771
|
[kIsNormalizedAlready]?: boolean;
|
|
155
772
|
}
|
|
156
|
-
export interface
|
|
773
|
+
export interface NormalizedOptions extends PromiseOnly.NormalizedOptions, NormalizedPlainOptions {
|
|
774
|
+
}
|
|
775
|
+
interface PlainDefaults {
|
|
157
776
|
timeout: Delays;
|
|
158
777
|
prefixUrl: string;
|
|
159
778
|
method: Method;
|
|
160
779
|
ignoreInvalidCookies: boolean;
|
|
161
780
|
decompress: boolean;
|
|
162
|
-
context:
|
|
781
|
+
context: Record<string, unknown>;
|
|
163
782
|
cookieJar?: PromiseCookieJar | ToughCookieJar;
|
|
164
783
|
dnsCache?: CacheableLookup;
|
|
165
784
|
headers: Headers;
|
|
@@ -174,12 +793,16 @@ export interface Defaults {
|
|
|
174
793
|
methodRewriting: boolean;
|
|
175
794
|
parseJson: ParseJsonFunction;
|
|
176
795
|
stringifyJson: StringifyJsonFunction;
|
|
796
|
+
retry: RequiredRetryOptions;
|
|
177
797
|
agent?: Agents | false;
|
|
178
798
|
request?: RequestFunction;
|
|
179
799
|
searchParams?: URLSearchParams;
|
|
180
800
|
lookup?: CacheableLookup['lookup'];
|
|
181
801
|
localAddress?: string;
|
|
182
802
|
createConnection?: Options['createConnection'];
|
|
803
|
+
cacheOptions: CacheOptions;
|
|
804
|
+
}
|
|
805
|
+
export interface Defaults extends PromiseOnly.Defaults, PlainDefaults {
|
|
183
806
|
}
|
|
184
807
|
export interface Progress {
|
|
185
808
|
percent: number;
|
|
@@ -187,27 +810,156 @@ export interface Progress {
|
|
|
187
810
|
total?: number;
|
|
188
811
|
}
|
|
189
812
|
export interface PlainResponse extends IncomingMessageWithTimings {
|
|
813
|
+
/**
|
|
814
|
+
The original request URL.
|
|
815
|
+
*/
|
|
190
816
|
requestUrl: string;
|
|
817
|
+
/**
|
|
818
|
+
The redirect URLs.
|
|
819
|
+
*/
|
|
191
820
|
redirectUrls: string[];
|
|
821
|
+
/**
|
|
822
|
+
- `options` - The Got options that were set on this request.
|
|
823
|
+
|
|
824
|
+
__Note__: This is not a [http.ClientRequest](https://nodejs.org/api/http.html#http_class_http_clientrequest).
|
|
825
|
+
*/
|
|
192
826
|
request: Request;
|
|
827
|
+
/**
|
|
828
|
+
The remote IP address.
|
|
829
|
+
|
|
830
|
+
This is hopefully a temporary limitation, see [lukechilds/cacheable-request#86](https://github.com/lukechilds/cacheable-request/issues/86).
|
|
831
|
+
|
|
832
|
+
__Note__: Not available when the response is cached.
|
|
833
|
+
*/
|
|
193
834
|
ip?: string;
|
|
835
|
+
/**
|
|
836
|
+
Whether the response was retrieved from the cache.
|
|
837
|
+
*/
|
|
194
838
|
isFromCache: boolean;
|
|
839
|
+
/**
|
|
840
|
+
The status code of the response.
|
|
841
|
+
*/
|
|
195
842
|
statusCode: number;
|
|
843
|
+
/**
|
|
844
|
+
The request URL or the final URL after redirects.
|
|
845
|
+
*/
|
|
196
846
|
url: string;
|
|
847
|
+
/**
|
|
848
|
+
The object contains the following properties:
|
|
849
|
+
|
|
850
|
+
- `start` - Time when the request started.
|
|
851
|
+
- `socket` - Time when a socket was assigned to the request.
|
|
852
|
+
- `lookup` - Time when the DNS lookup finished.
|
|
853
|
+
- `connect` - Time when the socket successfully connected.
|
|
854
|
+
- `secureConnect` - Time when the socket securely connected.
|
|
855
|
+
- `upload` - Time when the request finished uploading.
|
|
856
|
+
- `response` - Time when the request fired `response` event.
|
|
857
|
+
- `end` - Time when the response fired `end` event.
|
|
858
|
+
- `error` - Time when the request fired `error` event.
|
|
859
|
+
- `abort` - Time when the request fired `abort` event.
|
|
860
|
+
- `phases`
|
|
861
|
+
- `wait` - `timings.socket - timings.start`
|
|
862
|
+
- `dns` - `timings.lookup - timings.socket`
|
|
863
|
+
- `tcp` - `timings.connect - timings.lookup`
|
|
864
|
+
- `tls` - `timings.secureConnect - timings.connect`
|
|
865
|
+
- `request` - `timings.upload - (timings.secureConnect || timings.connect)`
|
|
866
|
+
- `firstByte` - `timings.response - timings.upload`
|
|
867
|
+
- `download` - `timings.end - timings.response`
|
|
868
|
+
- `total` - `(timings.end || timings.error || timings.abort) - timings.start`
|
|
869
|
+
|
|
870
|
+
If something has not been measured yet, it will be `undefined`.
|
|
871
|
+
|
|
872
|
+
__Note__: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
|
|
873
|
+
*/
|
|
197
874
|
timings: Timings;
|
|
875
|
+
/**
|
|
876
|
+
The number of times the request was retried.
|
|
877
|
+
*/
|
|
878
|
+
retryCount: number;
|
|
879
|
+
/**
|
|
880
|
+
The raw result of the request.
|
|
881
|
+
*/
|
|
882
|
+
rawBody?: Buffer;
|
|
883
|
+
/**
|
|
884
|
+
The result of the request.
|
|
885
|
+
*/
|
|
886
|
+
body?: unknown;
|
|
198
887
|
}
|
|
199
888
|
export interface Response<T = unknown> extends PlainResponse {
|
|
889
|
+
/**
|
|
890
|
+
The result of the request.
|
|
891
|
+
*/
|
|
200
892
|
body: T;
|
|
893
|
+
/**
|
|
894
|
+
The raw result of the request.
|
|
895
|
+
*/
|
|
201
896
|
rawBody: Buffer;
|
|
202
|
-
retryCount: number;
|
|
203
897
|
}
|
|
204
898
|
export interface RequestEvents<T> {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
899
|
+
/**
|
|
900
|
+
`request` event to get the request object of the request.
|
|
901
|
+
|
|
902
|
+
__Tip__: You can use `request` event to abort requests.
|
|
903
|
+
|
|
904
|
+
@example
|
|
905
|
+
```
|
|
906
|
+
got.stream('https://github.com')
|
|
907
|
+
.on('request', request => setTimeout(() => request.destroy(), 50));
|
|
908
|
+
```
|
|
909
|
+
*/
|
|
910
|
+
on: ((name: 'request', listener: (request: http.ClientRequest) => void) => T)
|
|
911
|
+
/**
|
|
912
|
+
The `response` event to get the response object of the final request.
|
|
913
|
+
*/
|
|
914
|
+
& (<R extends Response>(name: 'response', listener: (response: R) => void) => T)
|
|
915
|
+
/**
|
|
916
|
+
The `redirect` event to get the response object of a redirect. The second argument is options for the next request to the redirect location.
|
|
917
|
+
*/
|
|
918
|
+
& (<R extends Response, N extends NormalizedOptions>(name: 'redirect', listener: (response: R, nextOptions: N) => void) => T)
|
|
919
|
+
/**
|
|
920
|
+
Progress events for uploading (sending a request) and downloading (receiving a response).
|
|
921
|
+
The `progress` argument is an object like:
|
|
922
|
+
|
|
923
|
+
```js
|
|
924
|
+
{
|
|
925
|
+
percent: 0.1,
|
|
926
|
+
transferred: 1024,
|
|
927
|
+
total: 10240
|
|
928
|
+
}
|
|
929
|
+
```
|
|
930
|
+
|
|
931
|
+
If the `content-length` header is missing, `total` will be `undefined`.
|
|
932
|
+
|
|
933
|
+
@example
|
|
934
|
+
```js
|
|
935
|
+
(async () => {
|
|
936
|
+
const response = await got('https://sindresorhus.com')
|
|
937
|
+
.on('downloadProgress', progress => {
|
|
938
|
+
// Report download progress
|
|
939
|
+
})
|
|
940
|
+
.on('uploadProgress', progress => {
|
|
941
|
+
// Report upload progress
|
|
942
|
+
});
|
|
943
|
+
|
|
944
|
+
console.log(response);
|
|
945
|
+
})();
|
|
946
|
+
```
|
|
947
|
+
*/
|
|
948
|
+
& ((name: 'uploadProgress' | 'downloadProgress', listener: (progress: Progress) => void) => T)
|
|
949
|
+
/**
|
|
950
|
+
To enable retrying on a Got stream, it is required to have a `retry` handler attached.
|
|
951
|
+
|
|
952
|
+
When this event is emitted, you should reset the stream you were writing to and prepare the body again.
|
|
953
|
+
|
|
954
|
+
See `got.options.retry` for more information.
|
|
955
|
+
*/
|
|
956
|
+
& ((name: 'retry', listener: (retryCount: number, error: RequestError) => void) => T);
|
|
209
957
|
}
|
|
210
|
-
export declare const setNonEnumerableProperties: (sources:
|
|
958
|
+
export declare const setNonEnumerableProperties: (sources: Array<Options | Defaults | undefined>, to: Options) => void;
|
|
959
|
+
/**
|
|
960
|
+
An error to be thrown when a request fails.
|
|
961
|
+
Contains a `code` property with error class code, like `ECONNREFUSED`.
|
|
962
|
+
*/
|
|
211
963
|
export declare class RequestError extends Error {
|
|
212
964
|
code?: string;
|
|
213
965
|
stack: string;
|
|
@@ -219,38 +971,63 @@ export declare class RequestError extends Error {
|
|
|
219
971
|
code?: string;
|
|
220
972
|
}>, self: Request | NormalizedOptions);
|
|
221
973
|
}
|
|
974
|
+
/**
|
|
975
|
+
An error to be thrown when the server redirects you more than ten times.
|
|
976
|
+
Includes a `response` property.
|
|
977
|
+
*/
|
|
222
978
|
export declare class MaxRedirectsError extends RequestError {
|
|
223
979
|
readonly response: Response;
|
|
224
980
|
readonly request: Request;
|
|
225
981
|
readonly timings: Timings;
|
|
226
982
|
constructor(request: Request);
|
|
227
983
|
}
|
|
984
|
+
/**
|
|
985
|
+
An error to be thrown when the server response code is not 2xx nor 3xx if `options.followRedirect` is `true`, but always except for 304.
|
|
986
|
+
Includes a `response` property.
|
|
987
|
+
*/
|
|
228
988
|
export declare class HTTPError extends RequestError {
|
|
229
989
|
readonly response: Response;
|
|
230
990
|
readonly request: Request;
|
|
231
991
|
readonly timings: Timings;
|
|
232
992
|
constructor(response: Response);
|
|
233
993
|
}
|
|
994
|
+
/**
|
|
995
|
+
An error to be thrown when a cache method fails.
|
|
996
|
+
For example, if the database goes down or there's a filesystem error.
|
|
997
|
+
*/
|
|
234
998
|
export declare class CacheError extends RequestError {
|
|
235
999
|
readonly request: Request;
|
|
236
1000
|
constructor(error: Error, request: Request);
|
|
237
1001
|
}
|
|
1002
|
+
/**
|
|
1003
|
+
An error to be thrown when the request body is a stream and an error occurs while reading from that stream.
|
|
1004
|
+
*/
|
|
238
1005
|
export declare class UploadError extends RequestError {
|
|
239
1006
|
readonly request: Request;
|
|
240
1007
|
constructor(error: Error, request: Request);
|
|
241
1008
|
}
|
|
1009
|
+
/**
|
|
1010
|
+
An error to be thrown when the request is aborted due to a timeout.
|
|
1011
|
+
Includes an `event` and `timings` property.
|
|
1012
|
+
*/
|
|
242
1013
|
export declare class TimeoutError extends RequestError {
|
|
243
1014
|
readonly request: Request;
|
|
244
1015
|
readonly timings: Timings;
|
|
245
1016
|
readonly event: string;
|
|
246
1017
|
constructor(error: TimedOutTimeoutError, timings: Timings, request: Request);
|
|
247
1018
|
}
|
|
1019
|
+
/**
|
|
1020
|
+
An error to be thrown when reading from response stream fails.
|
|
1021
|
+
*/
|
|
248
1022
|
export declare class ReadError extends RequestError {
|
|
249
1023
|
readonly request: Request;
|
|
250
1024
|
readonly response: Response;
|
|
251
1025
|
readonly timings: Timings;
|
|
252
1026
|
constructor(error: Error, request: Request);
|
|
253
1027
|
}
|
|
1028
|
+
/**
|
|
1029
|
+
An error to be thrown when given an unsupported protocol.
|
|
1030
|
+
*/
|
|
254
1031
|
export declare class UnsupportedProtocolError extends RequestError {
|
|
255
1032
|
constructor(options: NormalizedOptions);
|
|
256
1033
|
}
|
|
@@ -264,6 +1041,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
|
|
|
264
1041
|
[kTriggerRead]: boolean;
|
|
265
1042
|
[kBody]: Options['body'];
|
|
266
1043
|
[kJobs]: Array<() => void>;
|
|
1044
|
+
[kRetryTimeout]?: NodeJS.Timeout;
|
|
267
1045
|
[kBodySize]?: number;
|
|
268
1046
|
[kServerResponsesPiped]: Set<ServerResponse>;
|
|
269
1047
|
[kIsFromCache]?: boolean;
|
|
@@ -279,29 +1057,74 @@ export default class Request extends Duplex implements RequestEvents<Request> {
|
|
|
279
1057
|
requestUrl: string;
|
|
280
1058
|
requestInitialized: boolean;
|
|
281
1059
|
redirects: string[];
|
|
282
|
-
|
|
1060
|
+
retryCount: number;
|
|
1061
|
+
constructor(url: string | URL | undefined, options?: Options, defaults?: Defaults);
|
|
283
1062
|
static normalizeArguments(url?: string | URL, options?: Options, defaults?: Defaults): NormalizedOptions;
|
|
284
1063
|
_lockWrite(): void;
|
|
285
1064
|
_unlockWrite(): void;
|
|
286
1065
|
_finalizeBody(): Promise<void>;
|
|
1066
|
+
_onResponseBase(response: IncomingMessageWithTimings): Promise<void>;
|
|
287
1067
|
_onResponse(response: IncomingMessageWithTimings): Promise<void>;
|
|
288
1068
|
_onRequest(request: ClientRequest): void;
|
|
289
1069
|
_createCacheableRequest(url: URL, options: RequestOptions): Promise<ClientRequest | ResponseLike>;
|
|
290
1070
|
_makeRequest(): Promise<void>;
|
|
291
|
-
|
|
1071
|
+
_error(error: RequestError): Promise<void>;
|
|
1072
|
+
_beforeError(error: Error): void;
|
|
292
1073
|
_read(): void;
|
|
293
|
-
_write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
|
|
294
|
-
_writeRequest(chunk: any, encoding:
|
|
1074
|
+
_write(chunk: any, encoding: string | undefined, callback: (error?: Error | null) => void): void;
|
|
1075
|
+
_writeRequest(chunk: any, encoding: BufferEncoding | undefined, callback: (error?: Error | null) => void): void;
|
|
295
1076
|
_final(callback: (error?: Error | null) => void): void;
|
|
296
1077
|
_destroy(error: Error | null, callback: (error: Error | null) => void): void;
|
|
1078
|
+
get _isAboutToError(): boolean;
|
|
1079
|
+
/**
|
|
1080
|
+
The remote IP address.
|
|
1081
|
+
*/
|
|
297
1082
|
get ip(): string | undefined;
|
|
1083
|
+
/**
|
|
1084
|
+
Indicates whether the request has been aborted or not.
|
|
1085
|
+
*/
|
|
298
1086
|
get aborted(): boolean;
|
|
299
1087
|
get socket(): Socket | undefined;
|
|
1088
|
+
/**
|
|
1089
|
+
Progress event for downloading (receiving a response).
|
|
1090
|
+
*/
|
|
300
1091
|
get downloadProgress(): Progress;
|
|
1092
|
+
/**
|
|
1093
|
+
Progress event for uploading (sending a request).
|
|
1094
|
+
*/
|
|
301
1095
|
get uploadProgress(): Progress;
|
|
1096
|
+
/**
|
|
1097
|
+
The object contains the following properties:
|
|
1098
|
+
|
|
1099
|
+
- `start` - Time when the request started.
|
|
1100
|
+
- `socket` - Time when a socket was assigned to the request.
|
|
1101
|
+
- `lookup` - Time when the DNS lookup finished.
|
|
1102
|
+
- `connect` - Time when the socket successfully connected.
|
|
1103
|
+
- `secureConnect` - Time when the socket securely connected.
|
|
1104
|
+
- `upload` - Time when the request finished uploading.
|
|
1105
|
+
- `response` - Time when the request fired `response` event.
|
|
1106
|
+
- `end` - Time when the response fired `end` event.
|
|
1107
|
+
- `error` - Time when the request fired `error` event.
|
|
1108
|
+
- `abort` - Time when the request fired `abort` event.
|
|
1109
|
+
- `phases`
|
|
1110
|
+
- `wait` - `timings.socket - timings.start`
|
|
1111
|
+
- `dns` - `timings.lookup - timings.socket`
|
|
1112
|
+
- `tcp` - `timings.connect - timings.lookup`
|
|
1113
|
+
- `tls` - `timings.secureConnect - timings.connect`
|
|
1114
|
+
- `request` - `timings.upload - (timings.secureConnect || timings.connect)`
|
|
1115
|
+
- `firstByte` - `timings.response - timings.upload`
|
|
1116
|
+
- `download` - `timings.end - timings.response`
|
|
1117
|
+
- `total` - `(timings.end || timings.error || timings.abort) - timings.start`
|
|
1118
|
+
|
|
1119
|
+
If something has not been measured yet, it will be `undefined`.
|
|
1120
|
+
|
|
1121
|
+
__Note__: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
|
|
1122
|
+
*/
|
|
302
1123
|
get timings(): Timings | undefined;
|
|
1124
|
+
/**
|
|
1125
|
+
Whether the response was retrieved from the cache.
|
|
1126
|
+
*/
|
|
303
1127
|
get isFromCache(): boolean | undefined;
|
|
304
|
-
get _response(): Response | undefined;
|
|
305
1128
|
pipe<T extends NodeJS.WritableStream>(destination: T, options?: {
|
|
306
1129
|
end?: boolean;
|
|
307
1130
|
}): T;
|