got 10.5.7 → 11.0.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/{calculate-retry-delay.d.ts → as-promise/calculate-retry-delay.d.ts} +0 -0
- package/dist/source/{calculate-retry-delay.js → as-promise/calculate-retry-delay.js} +6 -7
- package/dist/source/as-promise/core.d.ts +13 -0
- package/dist/source/as-promise/core.js +124 -0
- package/dist/source/as-promise/create-rejection.d.ts +2 -0
- package/dist/source/as-promise/create-rejection.js +30 -0
- package/dist/source/as-promise/index.d.ts +5 -0
- package/dist/source/as-promise/index.js +203 -0
- package/dist/source/as-promise/types.d.ts +77 -0
- package/dist/source/as-promise/types.js +27 -0
- package/dist/source/core/index.d.ts +272 -0
- package/dist/source/core/index.js +1091 -0
- package/dist/source/{utils → core/utils}/get-body-size.d.ts +0 -0
- package/dist/source/{utils → core/utils}/get-body-size.js +0 -0
- package/dist/source/core/utils/is-form-data.d.ts +8 -0
- package/dist/source/{utils → core/utils}/is-form-data.js +0 -0
- package/dist/source/core/utils/options-to-url.d.ts +15 -0
- package/dist/source/{utils → core/utils}/options-to-url.js +5 -34
- package/dist/source/core/utils/proxy-events.d.ts +3 -0
- package/dist/source/core/utils/proxy-events.js +17 -0
- package/dist/source/{utils → core/utils}/timed-out.d.ts +0 -0
- package/dist/source/{utils → core/utils}/timed-out.js +2 -5
- package/dist/source/{utils → core/utils}/unhandle.d.ts +1 -1
- package/dist/source/{utils → core/utils}/unhandle.js +0 -0
- package/dist/source/{utils → core/utils}/url-to-options.d.ts +0 -0
- package/dist/source/{utils → core/utils}/url-to-options.js +0 -0
- package/dist/source/core/utils/weakable-map.d.ts +8 -0
- package/dist/source/core/utils/weakable-map.js +29 -0
- package/dist/source/create.d.ts +3 -79
- package/dist/source/create.js +83 -39
- package/dist/source/index.d.ts +3 -6
- package/dist/source/index.js +13 -19
- package/dist/source/types.d.ts +84 -198
- package/dist/source/types.js +0 -1
- package/package.json +41 -37
- package/readme.md +211 -97
- package/dist/source/as-promise.d.ts +0 -3
- package/dist/source/as-promise.js +0 -152
- package/dist/source/as-stream.d.ts +0 -7
- package/dist/source/as-stream.js +0 -121
- package/dist/source/errors.d.ts +0 -41
- package/dist/source/errors.js +0 -103
- package/dist/source/get-response.d.ts +0 -6
- package/dist/source/get-response.js +0 -25
- package/dist/source/known-hook-events.d.ts +0 -88
- package/dist/source/known-hook-events.js +0 -11
- package/dist/source/normalize-arguments.d.ts +0 -19
- package/dist/source/normalize-arguments.js +0 -436
- package/dist/source/progress.d.ts +0 -4
- package/dist/source/progress.js +0 -40
- package/dist/source/request-as-event-emitter.d.ts +0 -12
- package/dist/source/request-as-event-emitter.js +0 -284
- package/dist/source/utils/dynamic-require.d.ts +0 -3
- package/dist/source/utils/dynamic-require.js +0 -4
- package/dist/source/utils/is-form-data.d.ts +0 -3
- package/dist/source/utils/merge.d.ts +0 -6
- package/dist/source/utils/merge.js +0 -35
- package/dist/source/utils/options-to-url.d.ts +0 -19
- package/dist/source/utils/supports-brotli.d.ts +0 -2
- package/dist/source/utils/supports-brotli.js +0 -4
package/readme.md
CHANGED
|
@@ -28,7 +28,8 @@ For browser usage, we recommend [Ky](https://github.com/sindresorhus/ky) by the
|
|
|
28
28
|
|
|
29
29
|
- [Promise API](#api)
|
|
30
30
|
- [Stream API](#streams)
|
|
31
|
-
- [Pagination API
|
|
31
|
+
- [Pagination API](#pagination)
|
|
32
|
+
- [HTTP2 support](#http2)
|
|
32
33
|
- [Request cancelation](#aborting-the-request)
|
|
33
34
|
- [RFC compliant caching](#cache-adapters)
|
|
34
35
|
- [Follows redirects](#followredirect)
|
|
@@ -44,8 +45,8 @@ For browser usage, we recommend [Ky](https://github.com/sindresorhus/ky) by the
|
|
|
44
45
|
- [Types](#types)
|
|
45
46
|
- [Composable](documentation/advanced-creation.md#merging-instances)
|
|
46
47
|
- [Plugins](documentation/lets-make-a-plugin.md)
|
|
47
|
-
- [Used by
|
|
48
|
-
- Actively maintained
|
|
48
|
+
- [Used by 4K+ packages and 1.8M+ repos](https://github.com/sindresorhus/got/network/dependents)
|
|
49
|
+
- [Actively maintained](https://github.com/sindresorhus/got/graphs/contributors)
|
|
49
50
|
|
|
50
51
|
## Install
|
|
51
52
|
|
|
@@ -94,17 +95,17 @@ const pipeline = promisify(stream.pipeline);
|
|
|
94
95
|
})();
|
|
95
96
|
```
|
|
96
97
|
|
|
97
|
-
**Tip:**
|
|
98
|
+
**Tip:** `from.pipe(to)` doesn't forward errors. Instead, use [`stream.pipeline(from, ..., to, callback)`](https://nodejs.org/api/stream.html#stream_stream_pipeline_streams_callback).
|
|
98
99
|
|
|
99
100
|
### API
|
|
100
101
|
|
|
101
|
-
It's a `GET` request by default, but can be changed by using different methods or via `options.method
|
|
102
|
+
It's a `GET` request by default, but can be changed by using different methods or via [`options.method`](#method).
|
|
102
103
|
|
|
103
104
|
**By default, Got will retry on failure. To disable this option, set [`options.retry`](#retry) to `0`.**
|
|
104
105
|
|
|
105
106
|
#### got(url?, options?)
|
|
106
107
|
|
|
107
|
-
Returns a Promise
|
|
108
|
+
Returns a Promise giving a [Response object](#response) or a [Got Stream](#streams-1) if `options.isStream` is set to true.
|
|
108
109
|
|
|
109
110
|
##### url
|
|
110
111
|
|
|
@@ -116,8 +117,6 @@ Properties from `options` will override properties in the parsed `url`.
|
|
|
116
117
|
|
|
117
118
|
If no protocol is specified, it will throw a `TypeError`.
|
|
118
119
|
|
|
119
|
-
**Note:** this can also be an option.
|
|
120
|
-
|
|
121
120
|
##### options
|
|
122
121
|
|
|
123
122
|
Type: `object`
|
|
@@ -126,17 +125,25 @@ Any of the [`https.request`](https://nodejs.org/api/https.html#https_https_reque
|
|
|
126
125
|
|
|
127
126
|
**Note:** Legacy URL support is disabled. `options.path` is supported only for backwards compatibility. Use `options.pathname` and `options.searchParams` instead. `options.auth` has been replaced with `options.username` & `options.password`.
|
|
128
127
|
|
|
128
|
+
###### method
|
|
129
|
+
|
|
130
|
+
Type: `string`\
|
|
131
|
+
Default: `GET`
|
|
132
|
+
|
|
133
|
+
The HTTP method used to make the request.
|
|
134
|
+
|
|
129
135
|
###### prefixUrl
|
|
130
136
|
|
|
131
137
|
Type: `string | URL`
|
|
132
138
|
|
|
133
|
-
When specified, `prefixUrl` will be prepended to `url`. The prefix can be any valid URL, either relative or absolute
|
|
139
|
+
When specified, `prefixUrl` will be prepended to `url`. The prefix can be any valid URL, either relative or absolute.\
|
|
140
|
+
A trailing slash `/` is optional - one will be added automatically.
|
|
134
141
|
|
|
135
142
|
**Note:** `prefixUrl` will be ignored if the `url` argument is a URL instance.
|
|
136
143
|
|
|
137
144
|
**Note:** Leading slashes in `input` are disallowed when using this option to enforce consistency and avoid confusion. 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`. The latter is used by browsers.
|
|
138
145
|
|
|
139
|
-
**Tip:** Useful when used with [`got.extend()`](#custom-endpoints) to create niche-specific Got
|
|
146
|
+
**Tip:** Useful when used with [`got.extend()`](#custom-endpoints) to create niche-specific Got instances.
|
|
140
147
|
|
|
141
148
|
**Tip:** You can change `prefixUrl` using hooks as long as the URL still includes the `prefixUrl`. If the URL doesn't include it anymore, it will throw.
|
|
142
149
|
|
|
@@ -188,7 +195,9 @@ Type: `string | Buffer | stream.Readable` or [`form-data` instance](https://gith
|
|
|
188
195
|
|
|
189
196
|
**Note #2:** If you provide this option, `got.stream()` will be read-only.
|
|
190
197
|
|
|
191
|
-
**Note #3:** If you provide a payload with the `GET` or `HEAD` method, it will throw a `TypeError`.
|
|
198
|
+
**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`.
|
|
199
|
+
|
|
200
|
+
**Note #4:** This option is not enumerable and will not be merged with the instance defaults.
|
|
192
201
|
|
|
193
202
|
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`.
|
|
194
203
|
|
|
@@ -196,7 +205,8 @@ The `content-length` header will be automatically set if `body` is a `string` /
|
|
|
196
205
|
|
|
197
206
|
Type: `object | Array | number | string | boolean | null` *(JSON-serializable values)*
|
|
198
207
|
|
|
199
|
-
**Note:** If you provide this option, `got.stream()` will be read-only
|
|
208
|
+
**Note #1:** If you provide this option, `got.stream()` will be read-only.\
|
|
209
|
+
**Note #2:** This option is not enumerable and will not be merged with the instance defaults.
|
|
200
210
|
|
|
201
211
|
JSON body. If the `Content-Type` header is not set, it will be set to `application/json`.
|
|
202
212
|
|
|
@@ -217,7 +227,7 @@ const instance = got.extend({
|
|
|
217
227
|
hooks: {
|
|
218
228
|
beforeRequest: [
|
|
219
229
|
options => {
|
|
220
|
-
if (!options.context
|
|
230
|
+
if (!options.context || !options.context.token) {
|
|
221
231
|
throw new Error('Token required');
|
|
222
232
|
}
|
|
223
233
|
|
|
@@ -248,21 +258,35 @@ Default: `'text'`
|
|
|
248
258
|
|
|
249
259
|
The parsing method. Can be `'text'`, `'json'` or `'buffer'`.
|
|
250
260
|
|
|
251
|
-
The promise also has `.text()`, `.json()` and `.buffer()` methods which
|
|
261
|
+
The promise also has `.text()`, `.json()` and `.buffer()` methods which return another Got promise for the parsed body.\
|
|
262
|
+
It's like setting the options to `{responseType: 'json', resolveBodyOnly: true}` but without affecting the main Got promise.
|
|
252
263
|
|
|
253
264
|
Example:
|
|
254
265
|
|
|
266
|
+
```js
|
|
267
|
+
(async () => {
|
|
268
|
+
const responsePromise = got(url);
|
|
269
|
+
const bufferPromise = responsePromise.buffer();
|
|
270
|
+
const jsonPromise = responsePromise.json();
|
|
271
|
+
|
|
272
|
+
const [response, buffer, json] = Promise.all([responsePromise, bufferPromise, jsonPromise]);
|
|
273
|
+
// `response` is an instance of Got Response
|
|
274
|
+
// `buffer` is an instance of Buffer
|
|
275
|
+
// `json` is an object
|
|
276
|
+
})();
|
|
277
|
+
```
|
|
278
|
+
|
|
255
279
|
```js
|
|
256
280
|
// This
|
|
257
281
|
const body = await got(url).json();
|
|
258
282
|
|
|
259
|
-
// is the same as this
|
|
283
|
+
// is semantically the same as this
|
|
260
284
|
const body = await got(url, {responseType: 'json', resolveBodyOnly: true});
|
|
261
285
|
```
|
|
262
286
|
|
|
263
287
|
###### resolveBodyOnly
|
|
264
288
|
|
|
265
|
-
Type: `
|
|
289
|
+
Type: `boolean`\
|
|
266
290
|
Default: `false`
|
|
267
291
|
|
|
268
292
|
When set to `true` the promise will return the [Response body](#body-1) instead of the [Response](#response) object.
|
|
@@ -303,15 +327,18 @@ Default: `'utf8'`
|
|
|
303
327
|
|
|
304
328
|
To get a [`Buffer`](https://nodejs.org/api/buffer.html), you need to set [`responseType`](#responseType) to `buffer` instead.
|
|
305
329
|
|
|
330
|
+
**Note:** This doesn't affect streams! Instead, you need to do `got.stream(...).setEncoding(encoding)`.
|
|
331
|
+
|
|
306
332
|
###### form
|
|
307
333
|
|
|
308
|
-
Type: `object
|
|
334
|
+
Type: `object`
|
|
309
335
|
|
|
310
|
-
**Note:** If you provide this option, `got.stream()` will be read-only
|
|
336
|
+
**Note #1:** If you provide this option, `got.stream()` will be read-only.\
|
|
337
|
+
**Note #2:** This option is not enumerable and will not be merged with the instance defaults.
|
|
311
338
|
|
|
312
|
-
The form body is converted to query string using [`(new URLSearchParams(object)).toString()`](https://nodejs.org/api/url.html#url_constructor_new_urlsearchparams_obj).
|
|
339
|
+
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).
|
|
313
340
|
|
|
314
|
-
If
|
|
341
|
+
If the `Content-Type` header is not present, it will be set to `application/x-www-form-urlencoded`.
|
|
315
342
|
|
|
316
343
|
###### searchParams
|
|
317
344
|
|
|
@@ -332,20 +359,6 @@ console.log(searchParams.toString());
|
|
|
332
359
|
//=> 'key=a&key=b'
|
|
333
360
|
```
|
|
334
361
|
|
|
335
|
-
And if you need a different array format, you could use the [`query-string`](https://github.com/sindresorhus/query-string) package:
|
|
336
|
-
|
|
337
|
-
```js
|
|
338
|
-
const got = require('got');
|
|
339
|
-
const queryString = require('query-string');
|
|
340
|
-
|
|
341
|
-
const searchParams = queryString.stringify({key: ['a', 'b']}, {arrayFormat: 'bracket'});
|
|
342
|
-
|
|
343
|
-
got('https://example.com', {searchParams});
|
|
344
|
-
|
|
345
|
-
console.log(searchParams);
|
|
346
|
-
//=> 'key[]=a&key[]=b'
|
|
347
|
-
```
|
|
348
|
-
|
|
349
362
|
###### timeout
|
|
350
363
|
|
|
351
364
|
Type: `number | object`
|
|
@@ -410,6 +423,15 @@ Default: `true`
|
|
|
410
423
|
|
|
411
424
|
By default, redirects will use [method rewriting](https://tools.ietf.org/html/rfc7231#section-6.4). 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).
|
|
412
425
|
|
|
426
|
+
###### allowGetBody
|
|
427
|
+
|
|
428
|
+
Type: `boolean`\
|
|
429
|
+
Default: `false`
|
|
430
|
+
|
|
431
|
+
**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)**.
|
|
432
|
+
|
|
433
|
+
Set this to `true` to allow sending body for the `GET` method. 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. This option is only meant to interact with non-compliant servers when you have no other choice.
|
|
434
|
+
|
|
413
435
|
###### maxRedirects
|
|
414
436
|
|
|
415
437
|
Type: `number`\
|
|
@@ -438,25 +460,38 @@ Default: `false`
|
|
|
438
460
|
###### dnsCache
|
|
439
461
|
|
|
440
462
|
Type: `object`\
|
|
441
|
-
Default: `
|
|
463
|
+
Default: `new CacheableLookup()`
|
|
442
464
|
|
|
443
|
-
|
|
465
|
+
An instance of [`CacheableLookup`](https://github.com/szmarczak/cacheable-lookup) used for making DNS lookups.
|
|
444
466
|
|
|
445
467
|
###### request
|
|
446
468
|
|
|
447
469
|
Type: `Function`\
|
|
448
470
|
Default: `http.request | https.request` *(Depending on the protocol)*
|
|
449
471
|
|
|
450
|
-
Custom request function. The main purpose of this is to [support HTTP2 using a wrapper](
|
|
472
|
+
Custom request function. The main purpose of this is to [support HTTP2 using a wrapper](https://github.com/szmarczak/http2-wrapper).
|
|
451
473
|
|
|
452
|
-
######
|
|
474
|
+
###### http2
|
|
453
475
|
|
|
454
476
|
Type: `boolean`\
|
|
455
477
|
Default: `false`
|
|
456
478
|
|
|
457
|
-
|
|
479
|
+
If set to `true`, Got will additionally accept HTTP2 requests.\
|
|
480
|
+
It will choose either HTTP/1.1 or HTTP/2 depending on the ALPN protocol.
|
|
458
481
|
|
|
459
|
-
|
|
482
|
+
**Note:** Overriding `options.request` will disable HTTP2 support.
|
|
483
|
+
|
|
484
|
+
**Note:** This option will default to `true` in the next upcoming major release.
|
|
485
|
+
|
|
486
|
+
```js
|
|
487
|
+
const got = require('got');
|
|
488
|
+
|
|
489
|
+
(async () => {
|
|
490
|
+
const {headers} = await got('https://nghttp2.org/httpbin/anything', {http2: true});
|
|
491
|
+
console.log(headers.via);
|
|
492
|
+
//=> '2 nghttpx'
|
|
493
|
+
})();
|
|
494
|
+
```
|
|
460
495
|
|
|
461
496
|
###### throwHttpErrors
|
|
462
497
|
|
|
@@ -469,9 +504,11 @@ If this is disabled, requests that encounter an error status code will be resolv
|
|
|
469
504
|
|
|
470
505
|
###### agent
|
|
471
506
|
|
|
472
|
-
|
|
507
|
+
Type: `object`
|
|
508
|
+
|
|
509
|
+
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. This is necessary because a request to one protocol might redirect to another. In such a scenario, Got will switch over to the right protocol agent for you.
|
|
473
510
|
|
|
474
|
-
If
|
|
511
|
+
If a key is not present, it will default to a global agent.
|
|
475
512
|
|
|
476
513
|
```js
|
|
477
514
|
const got = require('got');
|
|
@@ -501,14 +538,16 @@ Called with plain [request options](#options), right before their normalization.
|
|
|
501
538
|
|
|
502
539
|
See the [Request migration guide](documentation/migration-guides.md#breaking-changes) for an example.
|
|
503
540
|
|
|
504
|
-
**Note:** This hook must be synchronous
|
|
541
|
+
**Note #1:** This hook must be synchronous!\
|
|
542
|
+
**Note #2:** Errors in this hook will be converted into an instances of [`RequestError`](#got.requesterror).\
|
|
543
|
+
**Note #3:** The options object may not have a `url` property. To modify it, use a `beforeRequest` hook instead.
|
|
505
544
|
|
|
506
545
|
###### hooks.beforeRequest
|
|
507
546
|
|
|
508
547
|
Type: `Function[]`\
|
|
509
548
|
Default: `[]`
|
|
510
549
|
|
|
511
|
-
Called with [normalized](source/
|
|
550
|
+
Called with [normalized](source/core/index.ts) [request options](#options). Got will make no further changes to the request before it is sent. This is especially useful in conjunction with [`got.extend()`](#instances) when you want to create an API client that, for example, uses HMAC-signing.
|
|
512
551
|
|
|
513
552
|
See the [AWS section](#aws) for an example.
|
|
514
553
|
|
|
@@ -517,7 +556,7 @@ See the [AWS section](#aws) for an example.
|
|
|
517
556
|
Type: `Function[]`\
|
|
518
557
|
Default: `[]`
|
|
519
558
|
|
|
520
|
-
Called with [normalized](source/
|
|
559
|
+
Called with [normalized](source/core/index.ts) [request options](#options) and the redirect [response](#response). Got will make no further changes to the request. This is especially useful when you want to avoid dead sites. Example:
|
|
521
560
|
|
|
522
561
|
```js
|
|
523
562
|
const got = require('got');
|
|
@@ -551,7 +590,7 @@ got.post('https://example.com', {
|
|
|
551
590
|
hooks: {
|
|
552
591
|
beforeRetry: [
|
|
553
592
|
(options, error, retryCount) => {
|
|
554
|
-
if (error.statusCode === 413) { // Payload too large
|
|
593
|
+
if (error.response.statusCode === 413) { // Payload too large
|
|
555
594
|
options.body = getNewBody();
|
|
556
595
|
}
|
|
557
596
|
}
|
|
@@ -627,7 +666,7 @@ got('https://api.github.com/some-endpoint', {
|
|
|
627
666
|
const {response} = error;
|
|
628
667
|
if (response && response.body) {
|
|
629
668
|
error.name = 'GitHubError';
|
|
630
|
-
error.message = `${response.body.message} (${
|
|
669
|
+
error.message = `${response.body.message} (${response.statusCode})`;
|
|
631
670
|
}
|
|
632
671
|
|
|
633
672
|
return error;
|
|
@@ -637,49 +676,96 @@ got('https://api.github.com/some-endpoint', {
|
|
|
637
676
|
});
|
|
638
677
|
```
|
|
639
678
|
|
|
640
|
-
#####
|
|
679
|
+
##### pagination
|
|
641
680
|
|
|
642
681
|
Type: `object`
|
|
643
682
|
|
|
644
|
-
**Note:**
|
|
683
|
+
**Note:** We're [looking for feedback](https://github.com/sindresorhus/got/issues/1052), any ideas on how to improve the API are welcome.
|
|
645
684
|
|
|
646
|
-
######
|
|
685
|
+
###### pagination.transform
|
|
647
686
|
|
|
648
687
|
Type: `Function`\
|
|
649
688
|
Default: `response => JSON.parse(response.body)`
|
|
650
689
|
|
|
651
690
|
A function that transform [`Response`](#response) into an array of items. This is where you should do the parsing.
|
|
652
691
|
|
|
653
|
-
######
|
|
692
|
+
###### pagination.paginate
|
|
654
693
|
|
|
655
694
|
Type: `Function`\
|
|
656
695
|
Default: [`Link` header logic](source/index.ts)
|
|
657
696
|
|
|
658
|
-
|
|
697
|
+
The function takes three arguments:
|
|
698
|
+
- `response` - The current response object.
|
|
699
|
+
- `allItems` - An array of the emitted items.
|
|
700
|
+
- `currentItems` - Items from the current response.
|
|
701
|
+
|
|
702
|
+
It should return an object representing Got options pointing to the next page. If there are no more pages, `false` should be returned.
|
|
659
703
|
|
|
660
|
-
|
|
704
|
+
For example, if you want to stop when the response contains less items than expected, you can use something like this:
|
|
705
|
+
|
|
706
|
+
```js
|
|
707
|
+
const got = require('got');
|
|
708
|
+
|
|
709
|
+
(async () => {
|
|
710
|
+
const limit = 10;
|
|
711
|
+
|
|
712
|
+
const items = got.paginate('https://example.com/items', {
|
|
713
|
+
searchParams: {
|
|
714
|
+
limit,
|
|
715
|
+
offset: 0
|
|
716
|
+
},
|
|
717
|
+
pagination: {
|
|
718
|
+
paginate: (response, allItems, currentItems) => {
|
|
719
|
+
const previousSearchParams = response.request.options.searchParams;
|
|
720
|
+
const {offset: previousOffset} = previousSearchParams;
|
|
721
|
+
|
|
722
|
+
if (currentItems.length < limit) {
|
|
723
|
+
return false;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
return {
|
|
727
|
+
searchParams: {
|
|
728
|
+
...previousSearchParams,
|
|
729
|
+
offset: previousOffset + limit,
|
|
730
|
+
}
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
});
|
|
735
|
+
|
|
736
|
+
console.log('Items from all pages:', items);
|
|
737
|
+
})();
|
|
738
|
+
```
|
|
739
|
+
|
|
740
|
+
###### pagination.filter
|
|
661
741
|
|
|
662
742
|
Type: `Function`\
|
|
663
|
-
Default: `(item, allItems) => true`
|
|
743
|
+
Default: `(item, allItems, currentItems) => true`
|
|
664
744
|
|
|
665
745
|
Checks whether the item should be emitted or not.
|
|
666
746
|
|
|
667
|
-
######
|
|
747
|
+
###### pagination.shouldContinue
|
|
668
748
|
|
|
669
749
|
Type: `Function`\
|
|
670
|
-
Default: `(item, allItems) => true`
|
|
750
|
+
Default: `(item, allItems, currentItems) => true`
|
|
671
751
|
|
|
672
752
|
Checks whether the pagination should continue.
|
|
673
753
|
|
|
674
|
-
For example, if you need to stop **before** emitting an entry with some flag, you should use `(item, allItems) => !item.flag`. If you want to stop **after** emitting the entry, you should use `(item, allItems) => allItems.some(entry => entry.flag)` instead.
|
|
754
|
+
For example, if you need to stop **before** emitting an entry with some flag, you should use `(item, allItems, currentItems) => !item.flag`. If you want to stop **after** emitting the entry, you should use `(item, allItems, currentItems) => allItems.some(entry => entry.flag)` instead.
|
|
675
755
|
|
|
676
|
-
######
|
|
756
|
+
###### pagination.countLimit
|
|
677
757
|
|
|
678
758
|
Type: `number`\
|
|
679
759
|
Default: `Infinity`
|
|
680
760
|
|
|
681
761
|
The maximum amount of items that should be emitted.
|
|
682
762
|
|
|
763
|
+
##### localAddress
|
|
764
|
+
|
|
765
|
+
Type: `string`
|
|
766
|
+
|
|
767
|
+
The IP address used to send the request from.
|
|
768
|
+
|
|
683
769
|
#### Response
|
|
684
770
|
|
|
685
771
|
The response object will typically be a [Node.js HTTP response stream](https://nodejs.org/api/http.html#http_class_http_incomingmessage), however, if returned from the cache it will be a [response-like object](https://github.com/lukechilds/responselike) which behaves in the same way.
|
|
@@ -698,6 +784,12 @@ Type: `string | object | Buffer` *(Depending on `options.responseType`)*
|
|
|
698
784
|
|
|
699
785
|
The result of the request.
|
|
700
786
|
|
|
787
|
+
##### rawBody
|
|
788
|
+
|
|
789
|
+
Type: `Buffer`
|
|
790
|
+
|
|
791
|
+
The raw result of the request.
|
|
792
|
+
|
|
701
793
|
##### url
|
|
702
794
|
|
|
703
795
|
Type: `string`
|
|
@@ -798,7 +890,9 @@ The `response` event to get the response object of the final request.
|
|
|
798
890
|
The `redirect` event to get the response object of a redirect. The second argument is options for the next request to the redirect location.
|
|
799
891
|
|
|
800
892
|
##### .on('uploadProgress', progress)
|
|
893
|
+
##### .uploadProgress
|
|
801
894
|
##### .on('downloadProgress', progress)
|
|
895
|
+
##### .downloadProgress
|
|
802
896
|
|
|
803
897
|
Progress events for uploading (sending a request) and downloading (receiving a response). The `progress` argument is an object like:
|
|
804
898
|
|
|
@@ -826,9 +920,33 @@ If the `content-length` header is missing, `total` will be `undefined`.
|
|
|
826
920
|
})();
|
|
827
921
|
```
|
|
828
922
|
|
|
829
|
-
##### .
|
|
923
|
+
##### .ip
|
|
924
|
+
|
|
925
|
+
Type: `string`
|
|
926
|
+
|
|
927
|
+
The remote IP address.
|
|
928
|
+
|
|
929
|
+
##### .aborted
|
|
930
|
+
|
|
931
|
+
Type: `boolean`
|
|
932
|
+
|
|
933
|
+
Indicates whether the request has been aborted or not.
|
|
934
|
+
|
|
935
|
+
##### .timings
|
|
936
|
+
|
|
937
|
+
The same as `response.timings`.
|
|
938
|
+
|
|
939
|
+
##### .isFromCache
|
|
940
|
+
|
|
941
|
+
The same as `response.isFromCache`.
|
|
942
|
+
|
|
943
|
+
##### .socket
|
|
830
944
|
|
|
831
|
-
The
|
|
945
|
+
The same as `response.socket`.
|
|
946
|
+
|
|
947
|
+
##### .on('error', error)
|
|
948
|
+
|
|
949
|
+
The emitted `error` is an instance of [`RequestError`](#got.requesterror).
|
|
832
950
|
|
|
833
951
|
#### Pagination
|
|
834
952
|
|
|
@@ -841,7 +959,7 @@ Returns an async iterator:
|
|
|
841
959
|
const countLimit = 10;
|
|
842
960
|
|
|
843
961
|
const pagination = got.paginate('https://api.github.com/repos/sindresorhus/got/commits', {
|
|
844
|
-
|
|
962
|
+
pagination: {countLimit}
|
|
845
963
|
});
|
|
846
964
|
|
|
847
965
|
console.log(`Printing latest ${countLimit} Got commits (newest to oldest):`);
|
|
@@ -852,7 +970,7 @@ Returns an async iterator:
|
|
|
852
970
|
})();
|
|
853
971
|
```
|
|
854
972
|
|
|
855
|
-
See [`options.
|
|
973
|
+
See [`options.pagination`](#pagination) for more pagination options.
|
|
856
974
|
|
|
857
975
|
#### got.get(url, options?)
|
|
858
976
|
#### got.post(url, options?)
|
|
@@ -861,7 +979,7 @@ See [`options._pagination`](#_pagination) for more pagination options.
|
|
|
861
979
|
#### got.head(url, options?)
|
|
862
980
|
#### got.delete(url, options?)
|
|
863
981
|
|
|
864
|
-
Sets `options.method` to the method name and makes a request.
|
|
982
|
+
Sets [`options.method`](#method) to the method name and makes a request.
|
|
865
983
|
|
|
866
984
|
### Instances
|
|
867
985
|
|
|
@@ -931,7 +1049,7 @@ const mergedHandlers = got.extend({
|
|
|
931
1049
|
|
|
932
1050
|
```js
|
|
933
1051
|
const handler = (options, next) => {
|
|
934
|
-
if (options.
|
|
1052
|
+
if (options.isStream) {
|
|
935
1053
|
// It's a Stream
|
|
936
1054
|
return next(options);
|
|
937
1055
|
}
|
|
@@ -1003,6 +1121,14 @@ Options are deeply merged to a new object. The value of each key is determined a
|
|
|
1003
1121
|
- If the parent property is a plain `object` too, both values are merged recursively into a new `object`.
|
|
1004
1122
|
- Otherwise, only the new value is deeply cloned.
|
|
1005
1123
|
- If the new property is an `Array`, it overwrites the old one with a deep clone of the new property.
|
|
1124
|
+
- Properties that are not enumerable, such as `context`, `body`, `json`, and `form`, will not be merged.
|
|
1125
|
+
```js
|
|
1126
|
+
const a = {json: {cat: 'meow'}};
|
|
1127
|
+
const b = {json: {cow: 'moo'}};
|
|
1128
|
+
|
|
1129
|
+
got.mergeOptions(a, b);
|
|
1130
|
+
//=> {json: {cow: 'moo'}}
|
|
1131
|
+
```
|
|
1006
1132
|
- Otherwise, the new value is assigned to the key.
|
|
1007
1133
|
|
|
1008
1134
|
#### got.defaults
|
|
@@ -1091,7 +1217,8 @@ const addAccessToken = (accessToken: string): BeforeRequestHook => options => {
|
|
|
1091
1217
|
|
|
1092
1218
|
## Errors
|
|
1093
1219
|
|
|
1094
|
-
Each error contains an `options` property which are the options Got used to create a request - just to make debugging easier
|
|
1220
|
+
Each error contains an `options` property which are the options Got used to create a request - just to make debugging easier.\
|
|
1221
|
+
Additionaly, the errors may have `request` (Got Stream) and `response` (Got Response) properties depending on which phase of the request failed.
|
|
1095
1222
|
|
|
1096
1223
|
#### got.CacheError
|
|
1097
1224
|
|
|
@@ -1182,10 +1309,19 @@ const got = require('got');
|
|
|
1182
1309
|
})();
|
|
1183
1310
|
```
|
|
1184
1311
|
|
|
1312
|
+
To abort the Got Stream request, just call `stream.destroy()`.
|
|
1313
|
+
|
|
1314
|
+
```js
|
|
1315
|
+
const got = require('got');
|
|
1316
|
+
|
|
1317
|
+
const stream = got.stream(url);
|
|
1318
|
+
stream.destroy();
|
|
1319
|
+
```
|
|
1320
|
+
|
|
1185
1321
|
<a name="cache-adapters"></a>
|
|
1186
1322
|
## Cache
|
|
1187
1323
|
|
|
1188
|
-
Got implements [RFC 7234](
|
|
1324
|
+
Got implements [RFC 7234](https://httpwg.org/specs/rfc7234.html) compliant HTTP caching which works out of the box in-memory and is easily pluggable with a wide range of storage adapters. Fresh cache entries are served directly from the cache, and stale cache entries are revalidated with `If-None-Match`/`If-Modified-Since` headers. You can read more about the underlying cache behavior in the [`cacheable-request` documentation](https://github.com/lukechilds/cacheable-request). For DNS cache, Got uses [`cacheable-lookup`](https://github.com/szmarczak/cacheable-lookup).
|
|
1189
1325
|
|
|
1190
1326
|
You can use the JavaScript `Map` type as an in-memory cache:
|
|
1191
1327
|
|
|
@@ -1496,42 +1632,26 @@ const custom = got.extend({
|
|
|
1496
1632
|
})();
|
|
1497
1633
|
```
|
|
1498
1634
|
|
|
1499
|
-
### Experimental HTTP2 support
|
|
1500
|
-
|
|
1501
|
-
Got provides an experimental support for HTTP2 using the [`http2-wrapper`](https://github.com/szmarczak/http2-wrapper) package:
|
|
1502
|
-
|
|
1503
|
-
```js
|
|
1504
|
-
const got = require('got');
|
|
1505
|
-
const {request} = require('http2-wrapper');
|
|
1506
|
-
|
|
1507
|
-
const h2got = got.extend({request});
|
|
1508
|
-
|
|
1509
|
-
(async () => {
|
|
1510
|
-
const {body} = await h2got('https://nghttp2.org/httpbin/headers');
|
|
1511
|
-
console.log(body);
|
|
1512
|
-
})();
|
|
1513
|
-
```
|
|
1514
|
-
|
|
1515
1635
|
## FAQ
|
|
1516
1636
|
|
|
1517
1637
|
### Why yet another HTTP client?
|
|
1518
1638
|
|
|
1519
1639
|
Got was created because the popular [`request`](https://github.com/request/request) package is bloated: [](https://packagephobia.now.sh/result?p=request)\
|
|
1520
|
-
Furthermore, Got is fully written in TypeScript.
|
|
1640
|
+
Furthermore, Got is fully written in TypeScript and actively maintained.
|
|
1521
1641
|
|
|
1522
|
-
### Electron support has been
|
|
1642
|
+
### Electron support has been removed
|
|
1523
1643
|
|
|
1524
|
-
|
|
1644
|
+
The Electron `net` module is not consistent with the Node.js `http` module. See [#899](https://github.com/sindresorhus/got/issues/899) for more info.
|
|
1525
1645
|
|
|
1526
1646
|
## Comparison
|
|
1527
1647
|
|
|
1528
1648
|
| | `got` | [`request`][r0] | [`node-fetch`][n0] | [`ky`][k0] | [`axios`][a0] | [`superagent`][s0] |
|
|
1529
1649
|
|-----------------------|:------------------:|:------------------:|:--------------------:|:------------------------:|:------------------:|:----------------------:|
|
|
1530
|
-
| HTTP/2 support | :
|
|
1650
|
+
| HTTP/2 support | :sparkle: | :x: | :x: | :x: | :x: | :heavy_check_mark:\*\* |
|
|
1531
1651
|
| Browser support | :x: | :x: | :heavy_check_mark:\* | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
|
1532
1652
|
| Promise API | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
|
1533
1653
|
| Stream API | :heavy_check_mark: | :heavy_check_mark: | Node.js only | :x: | :x: | :heavy_check_mark: |
|
|
1534
|
-
| Pagination API | :
|
|
1654
|
+
| Pagination API | :heavy_check_mark: | :x: | :x: | :x: | :x: | :x: |
|
|
1535
1655
|
| Request cancelation | :heavy_check_mark: | :x: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
|
|
1536
1656
|
| RFC compliant caching | :heavy_check_mark: | :x: | :x: | :x: | :x: | :x: |
|
|
1537
1657
|
| Cookies (out-of-box) | :heavy_check_mark: | :heavy_check_mark: | :x: | :x: | :x: | :x: |
|
|
@@ -1714,14 +1834,8 @@ Some of the Got features may not work properly. See [#899](https://github.com/si
|
|
|
1714
1834
|
- [Brandon Smith](https://github.com/brandon93s)
|
|
1715
1835
|
- [Luke Childs](https://github.com/lukechilds)
|
|
1716
1836
|
|
|
1717
|
-
|
|
1837
|
+
## For enterprise
|
|
1718
1838
|
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
</b>
|
|
1723
|
-
<br>
|
|
1724
|
-
<sub>
|
|
1725
|
-
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
|
1726
|
-
</sub>
|
|
1727
|
-
</div>
|
|
1839
|
+
Available as part of the Tidelift Subscription.
|
|
1840
|
+
|
|
1841
|
+
The maintainers of `got` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-got?utm_source=npm-got&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
|