got 11.5.1 → 11.6.2
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/create-rejection.d.ts +0 -0
- package/dist/source/as-promise/create-rejection.js +0 -0
- package/dist/source/as-promise/index.d.ts +1 -3
- package/dist/source/as-promise/index.js +44 -125
- 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 +206 -24
- package/dist/source/as-promise/types.js +18 -7
- 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 +822 -5
- package/dist/source/core/index.js +234 -46
- package/dist/source/core/utils/dns-ip-version.d.ts +0 -0
- package/dist/source/core/utils/dns-ip-version.js +0 -0
- package/dist/source/core/utils/get-body-size.d.ts +0 -0
- package/dist/source/core/utils/get-body-size.js +0 -0
- package/dist/source/core/utils/get-buffer.d.ts +0 -0
- package/dist/source/core/utils/get-buffer.js +0 -0
- package/dist/source/core/utils/is-form-data.d.ts +0 -0
- package/dist/source/core/utils/is-form-data.js +0 -0
- 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 -0
- package/dist/source/core/utils/options-to-url.js +0 -0
- package/dist/source/core/utils/proxy-events.d.ts +0 -0
- package/dist/source/core/utils/proxy-events.js +0 -0
- package/dist/source/core/utils/timed-out.d.ts +0 -0
- package/dist/source/core/utils/timed-out.js +0 -0
- package/dist/source/core/utils/unhandle.d.ts +0 -0
- package/dist/source/core/utils/unhandle.js +0 -0
- package/dist/source/core/utils/url-to-options.d.ts +0 -0
- package/dist/source/core/utils/url-to-options.js +0 -0
- package/dist/source/core/utils/weakable-map.d.ts +0 -0
- package/dist/source/core/utils/weakable-map.js +0 -0
- package/dist/source/create.d.ts +0 -0
- package/dist/source/create.js +22 -14
- package/dist/source/index.d.ts +0 -0
- package/dist/source/index.js +3 -2
- package/dist/source/types.d.ts +240 -1
- package/dist/source/types.js +0 -0
- package/dist/source/utils/deep-freeze.d.ts +0 -0
- package/dist/source/utils/deep-freeze.js +0 -0
- package/dist/source/utils/deprecation-warning.d.ts +0 -0
- package/dist/source/utils/deprecation-warning.js +0 -0
- package/license +0 -0
- package/package.json +20 -16
- package/readme.md +180 -40
- 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 -127
|
@@ -32,6 +32,7 @@ declare const kTriggerRead: unique symbol;
|
|
|
32
32
|
declare const kBody: unique symbol;
|
|
33
33
|
declare const kJobs: unique symbol;
|
|
34
34
|
declare const kOriginalResponse: unique symbol;
|
|
35
|
+
declare const kRetryTimeout: unique symbol;
|
|
35
36
|
export declare const kIsNormalizedAlready: unique symbol;
|
|
36
37
|
export interface Agents {
|
|
37
38
|
http?: http.Agent;
|
|
@@ -51,21 +52,133 @@ export interface PromiseCookieJar {
|
|
|
51
52
|
getCookieString: (url: string) => Promise<string>;
|
|
52
53
|
setCookie: (rawCookie: string, url: string) => Promise<unknown>;
|
|
53
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
All available HTTP request methods provided by Got.
|
|
57
|
+
*/
|
|
54
58
|
export declare type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'HEAD' | 'DELETE' | 'OPTIONS' | 'TRACE' | 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'options' | 'trace';
|
|
55
59
|
declare type Promisable<T> = T | Promise<T>;
|
|
56
60
|
export declare type InitHook = (options: Options) => void;
|
|
57
61
|
export declare type BeforeRequestHook = (options: NormalizedOptions) => Promisable<void | Response | ResponseLike>;
|
|
58
62
|
export declare type BeforeRedirectHook = (options: NormalizedOptions, response: Response) => Promisable<void>;
|
|
59
63
|
export declare type BeforeErrorHook = (error: RequestError) => Promisable<RequestError>;
|
|
64
|
+
export declare type BeforeRetryHook = (options: NormalizedOptions, error?: RequestError, retryCount?: number) => void | Promise<void>;
|
|
60
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
|
+
*/
|
|
61
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
|
+
*/
|
|
62
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
|
+
*/
|
|
63
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
|
+
*/
|
|
64
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[];
|
|
65
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
All available hook of Got.
|
|
175
|
+
*/
|
|
66
176
|
export interface Hooks extends PromiseOnly.Hooks, PlainHooks {
|
|
67
177
|
}
|
|
68
|
-
declare type PlainHookEvent = 'init' | 'beforeRequest' | 'beforeRedirect' | 'beforeError';
|
|
178
|
+
declare type PlainHookEvent = 'init' | 'beforeRequest' | 'beforeRedirect' | 'beforeError' | 'beforeRetry';
|
|
179
|
+
/**
|
|
180
|
+
All hook events acceptable by Got.
|
|
181
|
+
*/
|
|
69
182
|
export declare type HookEvent = PromiseOnly.HookEvent | PlainHookEvent;
|
|
70
183
|
export declare const knownHookEvents: HookEvent[];
|
|
71
184
|
declare type AcceptableResponse = IncomingMessageWithTimings | ResponseLike;
|
|
@@ -75,47 +188,512 @@ export declare type Headers = Record<string, string | string[] | undefined>;
|
|
|
75
188
|
declare type CheckServerIdentityFunction = (hostname: string, certificate: DetailedPeerCertificate) => Error | void;
|
|
76
189
|
export declare type ParseJsonFunction = (text: string) => unknown;
|
|
77
190
|
export declare type StringifyJsonFunction = (object: unknown) => string;
|
|
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
|
+
}
|
|
78
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
|
+
*/
|
|
79
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
|
+
*/
|
|
80
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
|
+
*/
|
|
81
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
|
+
*/
|
|
82
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
|
+
*/
|
|
83
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
|
+
*/
|
|
84
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
|
+
*/
|
|
85
355
|
form?: {
|
|
86
356
|
[key: string]: any;
|
|
87
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
|
+
*/
|
|
88
365
|
json?: {
|
|
89
366
|
[key: string]: any;
|
|
90
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
|
+
*/
|
|
91
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
|
+
*/
|
|
92
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
|
+
*/
|
|
93
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
|
+
*/
|
|
94
418
|
searchParams?: string | {
|
|
95
419
|
[key: string]: string | number | boolean | null | undefined;
|
|
96
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
|
+
*/
|
|
97
431
|
dnsCache?: CacheableLookup | boolean;
|
|
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
|
+
*/
|
|
98
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
|
+
*/
|
|
99
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
|
+
*/
|
|
100
482
|
followRedirect?: boolean;
|
|
483
|
+
/**
|
|
484
|
+
If exceeded, the request will be aborted and a `MaxRedirectsError` will be thrown.
|
|
485
|
+
|
|
486
|
+
@default 10
|
|
487
|
+
*/
|
|
101
488
|
maxRedirects?: number;
|
|
489
|
+
/**
|
|
490
|
+
A cache adapter instance for storing cached response data.
|
|
491
|
+
|
|
492
|
+
@default false
|
|
493
|
+
*/
|
|
102
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
|
+
*/
|
|
103
503
|
throwHttpErrors?: boolean;
|
|
104
504
|
username?: string;
|
|
105
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
|
+
*/
|
|
106
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
|
+
*/
|
|
107
538
|
allowGetBody?: boolean;
|
|
108
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
|
+
*/
|
|
109
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
|
+
*/
|
|
110
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
|
+
*/
|
|
111
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
|
+
*/
|
|
112
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
|
+
*/
|
|
113
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
|
+
*/
|
|
114
658
|
localAddress?: string;
|
|
115
659
|
socketPath?: string;
|
|
660
|
+
/**
|
|
661
|
+
The HTTP method used to make the request.
|
|
662
|
+
|
|
663
|
+
@default 'GET'
|
|
664
|
+
*/
|
|
116
665
|
method?: Method;
|
|
117
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
|
+
*/
|
|
118
693
|
rejectUnauthorized?: boolean;
|
|
694
|
+
/**
|
|
695
|
+
Options for the advanced HTTPS API.
|
|
696
|
+
*/
|
|
119
697
|
https?: HTTPSOptions;
|
|
120
698
|
}
|
|
121
699
|
export interface Options extends PromiseOnly.Options, PlainOptions {
|
|
@@ -123,9 +701,42 @@ export interface Options extends PromiseOnly.Options, PlainOptions {
|
|
|
123
701
|
export interface HTTPSOptions {
|
|
124
702
|
rejectUnauthorized?: https.RequestOptions['rejectUnauthorized'];
|
|
125
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
|
+
*/
|
|
126
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
|
+
*/
|
|
127
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
|
+
*/
|
|
128
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
|
+
*/
|
|
129
740
|
passphrase?: SecureContextOptions['passphrase'];
|
|
130
741
|
}
|
|
131
742
|
interface NormalizedPlainOptions extends PlainOptions {
|
|
@@ -154,6 +765,8 @@ interface NormalizedPlainOptions extends PlainOptions {
|
|
|
154
765
|
password: string;
|
|
155
766
|
parseJson: ParseJsonFunction;
|
|
156
767
|
stringifyJson: StringifyJsonFunction;
|
|
768
|
+
retry: RequiredRetryOptions;
|
|
769
|
+
cacheOptions: CacheOptions;
|
|
157
770
|
[kRequest]: HttpRequestFunction;
|
|
158
771
|
[kIsNormalizedAlready]?: boolean;
|
|
159
772
|
}
|
|
@@ -180,12 +793,14 @@ interface PlainDefaults {
|
|
|
180
793
|
methodRewriting: boolean;
|
|
181
794
|
parseJson: ParseJsonFunction;
|
|
182
795
|
stringifyJson: StringifyJsonFunction;
|
|
796
|
+
retry: RequiredRetryOptions;
|
|
183
797
|
agent?: Agents | false;
|
|
184
798
|
request?: RequestFunction;
|
|
185
799
|
searchParams?: URLSearchParams;
|
|
186
800
|
lookup?: CacheableLookup['lookup'];
|
|
187
801
|
localAddress?: string;
|
|
188
802
|
createConnection?: Options['createConnection'];
|
|
803
|
+
cacheOptions: CacheOptions;
|
|
189
804
|
}
|
|
190
805
|
export interface Defaults extends PromiseOnly.Defaults, PlainDefaults {
|
|
191
806
|
}
|
|
@@ -195,24 +810,156 @@ export interface Progress {
|
|
|
195
810
|
total?: number;
|
|
196
811
|
}
|
|
197
812
|
export interface PlainResponse extends IncomingMessageWithTimings {
|
|
813
|
+
/**
|
|
814
|
+
The original request URL.
|
|
815
|
+
*/
|
|
198
816
|
requestUrl: string;
|
|
817
|
+
/**
|
|
818
|
+
The redirect URLs.
|
|
819
|
+
*/
|
|
199
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
|
+
*/
|
|
200
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
|
+
*/
|
|
201
834
|
ip?: string;
|
|
835
|
+
/**
|
|
836
|
+
Whether the response was retrieved from the cache.
|
|
837
|
+
*/
|
|
202
838
|
isFromCache: boolean;
|
|
839
|
+
/**
|
|
840
|
+
The status code of the response.
|
|
841
|
+
*/
|
|
203
842
|
statusCode: number;
|
|
843
|
+
/**
|
|
844
|
+
The request URL or the final URL after redirects.
|
|
845
|
+
*/
|
|
204
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
|
+
*/
|
|
205
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;
|
|
206
887
|
}
|
|
207
888
|
export interface Response<T = unknown> extends PlainResponse {
|
|
889
|
+
/**
|
|
890
|
+
The result of the request.
|
|
891
|
+
*/
|
|
208
892
|
body: T;
|
|
893
|
+
/**
|
|
894
|
+
The raw result of the request.
|
|
895
|
+
*/
|
|
209
896
|
rawBody: Buffer;
|
|
210
|
-
retryCount: number;
|
|
211
897
|
}
|
|
212
898
|
export interface RequestEvents<T> {
|
|
213
|
-
|
|
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);
|
|
214
957
|
}
|
|
215
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
|
+
*/
|
|
216
963
|
export declare class RequestError extends Error {
|
|
217
964
|
code?: string;
|
|
218
965
|
stack: string;
|
|
@@ -224,38 +971,63 @@ export declare class RequestError extends Error {
|
|
|
224
971
|
code?: string;
|
|
225
972
|
}>, self: Request | NormalizedOptions);
|
|
226
973
|
}
|
|
974
|
+
/**
|
|
975
|
+
An error to be thrown when the server redirects you more than ten times.
|
|
976
|
+
Includes a `response` property.
|
|
977
|
+
*/
|
|
227
978
|
export declare class MaxRedirectsError extends RequestError {
|
|
228
979
|
readonly response: Response;
|
|
229
980
|
readonly request: Request;
|
|
230
981
|
readonly timings: Timings;
|
|
231
982
|
constructor(request: Request);
|
|
232
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
|
+
*/
|
|
233
988
|
export declare class HTTPError extends RequestError {
|
|
234
989
|
readonly response: Response;
|
|
235
990
|
readonly request: Request;
|
|
236
991
|
readonly timings: Timings;
|
|
237
992
|
constructor(response: Response);
|
|
238
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
|
+
*/
|
|
239
998
|
export declare class CacheError extends RequestError {
|
|
240
999
|
readonly request: Request;
|
|
241
1000
|
constructor(error: Error, request: Request);
|
|
242
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
|
+
*/
|
|
243
1005
|
export declare class UploadError extends RequestError {
|
|
244
1006
|
readonly request: Request;
|
|
245
1007
|
constructor(error: Error, request: Request);
|
|
246
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
|
+
*/
|
|
247
1013
|
export declare class TimeoutError extends RequestError {
|
|
248
1014
|
readonly request: Request;
|
|
249
1015
|
readonly timings: Timings;
|
|
250
1016
|
readonly event: string;
|
|
251
1017
|
constructor(error: TimedOutTimeoutError, timings: Timings, request: Request);
|
|
252
1018
|
}
|
|
1019
|
+
/**
|
|
1020
|
+
An error to be thrown when reading from response stream fails.
|
|
1021
|
+
*/
|
|
253
1022
|
export declare class ReadError extends RequestError {
|
|
254
1023
|
readonly request: Request;
|
|
255
1024
|
readonly response: Response;
|
|
256
1025
|
readonly timings: Timings;
|
|
257
1026
|
constructor(error: Error, request: Request);
|
|
258
1027
|
}
|
|
1028
|
+
/**
|
|
1029
|
+
An error to be thrown when given an unsupported protocol.
|
|
1030
|
+
*/
|
|
259
1031
|
export declare class UnsupportedProtocolError extends RequestError {
|
|
260
1032
|
constructor(options: NormalizedOptions);
|
|
261
1033
|
}
|
|
@@ -269,6 +1041,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
|
|
|
269
1041
|
[kTriggerRead]: boolean;
|
|
270
1042
|
[kBody]: Options['body'];
|
|
271
1043
|
[kJobs]: Array<() => void>;
|
|
1044
|
+
[kRetryTimeout]?: NodeJS.Timeout;
|
|
272
1045
|
[kBodySize]?: number;
|
|
273
1046
|
[kServerResponsesPiped]: Set<ServerResponse>;
|
|
274
1047
|
[kIsFromCache]?: boolean;
|
|
@@ -284,7 +1057,8 @@ export default class Request extends Duplex implements RequestEvents<Request> {
|
|
|
284
1057
|
requestUrl: string;
|
|
285
1058
|
requestInitialized: boolean;
|
|
286
1059
|
redirects: string[];
|
|
287
|
-
|
|
1060
|
+
retryCount: number;
|
|
1061
|
+
constructor(url: string | URL | undefined, options?: Options, defaults?: Defaults);
|
|
288
1062
|
static normalizeArguments(url?: string | URL, options?: Options, defaults?: Defaults): NormalizedOptions;
|
|
289
1063
|
_lockWrite(): void;
|
|
290
1064
|
_unlockWrite(): void;
|
|
@@ -294,20 +1068,63 @@ export default class Request extends Duplex implements RequestEvents<Request> {
|
|
|
294
1068
|
_onRequest(request: ClientRequest): void;
|
|
295
1069
|
_createCacheableRequest(url: URL, options: RequestOptions): Promise<ClientRequest | ResponseLike>;
|
|
296
1070
|
_makeRequest(): Promise<void>;
|
|
1071
|
+
_error(error: RequestError): Promise<void>;
|
|
297
1072
|
_beforeError(error: Error): void;
|
|
298
1073
|
_read(): void;
|
|
299
1074
|
_write(chunk: any, encoding: string | undefined, callback: (error?: Error | null) => void): void;
|
|
300
1075
|
_writeRequest(chunk: any, encoding: BufferEncoding | undefined, callback: (error?: Error | null) => void): void;
|
|
301
1076
|
_final(callback: (error?: Error | null) => void): void;
|
|
302
1077
|
_destroy(error: Error | null, callback: (error: Error | null) => void): void;
|
|
1078
|
+
get _isAboutToError(): boolean;
|
|
1079
|
+
/**
|
|
1080
|
+
The remote IP address.
|
|
1081
|
+
*/
|
|
303
1082
|
get ip(): string | undefined;
|
|
1083
|
+
/**
|
|
1084
|
+
Indicates whether the request has been aborted or not.
|
|
1085
|
+
*/
|
|
304
1086
|
get aborted(): boolean;
|
|
305
1087
|
get socket(): Socket | undefined;
|
|
1088
|
+
/**
|
|
1089
|
+
Progress event for downloading (receiving a response).
|
|
1090
|
+
*/
|
|
306
1091
|
get downloadProgress(): Progress;
|
|
1092
|
+
/**
|
|
1093
|
+
Progress event for uploading (sending a request).
|
|
1094
|
+
*/
|
|
307
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
|
+
*/
|
|
308
1123
|
get timings(): Timings | undefined;
|
|
1124
|
+
/**
|
|
1125
|
+
Whether the response was retrieved from the cache.
|
|
1126
|
+
*/
|
|
309
1127
|
get isFromCache(): boolean | undefined;
|
|
310
|
-
get _response(): Response | undefined;
|
|
311
1128
|
pipe<T extends NodeJS.WritableStream>(destination: T, options?: {
|
|
312
1129
|
end?: boolean;
|
|
313
1130
|
}): T;
|