Comparing version 10.6.0 to 10.7.0
/// <reference types="node" /> | ||
import { Merge } from 'type-fest'; | ||
import { Merge, Except } from 'type-fest'; | ||
import { ProxyStream } from './as-stream'; | ||
@@ -54,5 +54,7 @@ import * as errors from './errors'; | ||
} | ||
export declare type GotPaginateOptions<T> = Except<Options, keyof PaginationOptions<unknown>> & PaginationOptions<T>; | ||
export declare type URLOrGotPaginateOptions<T> = string | GotPaginateOptions<T>; | ||
export interface GotPaginate { | ||
<T>(url: URLOrOptions & PaginationOptions<T>, options?: Options & PaginationOptions<T>): AsyncIterableIterator<T>; | ||
all<T>(url: URLOrOptions & PaginationOptions<T>, options?: Options & PaginationOptions<T>): Promise<T[]>; | ||
<T>(url: URLOrGotPaginateOptions<T>, options?: GotPaginateOptions<T>): AsyncIterableIterator<T>; | ||
all<T>(url: URLOrGotPaginateOptions<T>, options?: GotPaginateOptions<T>): Promise<T[]>; | ||
} | ||
@@ -59,0 +61,0 @@ export interface Got extends Record<HTTPAlias, GotRequestMethod>, GotRequestMethod { |
@@ -114,5 +114,6 @@ "use strict"; | ||
const parsed = await pagination.transform(result); | ||
const current = []; | ||
for (const item of parsed) { | ||
if (pagination.filter(item, all)) { | ||
if (!pagination.shouldContinue(item, all)) { | ||
if (pagination.filter(item, all, current)) { | ||
if (!pagination.shouldContinue(item, all, current)) { | ||
return; | ||
@@ -122,2 +123,3 @@ } | ||
all.push(item); | ||
current.push(item); | ||
if (all.length === pagination.countLimit) { | ||
@@ -128,3 +130,3 @@ return; | ||
} | ||
const optionsToMerge = pagination.paginate(result); | ||
const optionsToMerge = pagination.paginate(result, all, current); | ||
if (optionsToMerge === false) { | ||
@@ -131,0 +133,0 @@ return; |
@@ -75,2 +75,5 @@ "use strict"; | ||
transform: (response) => { | ||
if (response.request.options.responseType === 'json') { | ||
return response.body; | ||
} | ||
return JSON.parse(response.body); | ||
@@ -77,0 +80,0 @@ }, |
@@ -107,5 +107,5 @@ /// <reference types="node" /> | ||
transform?: (response: Response) => Promise<T[]> | T[]; | ||
filter?: (item: T, allItems: T[]) => boolean; | ||
paginate?: (response: Response) => Options | false; | ||
shouldContinue?: (item: T, allItems: T[]) => boolean; | ||
filter?: (item: T, allItems: T[], currentItems: T[]) => boolean; | ||
paginate?: (response: Response, allItems: T[], currentItems: T[]) => Options | false; | ||
shouldContinue?: (item: T, allItems: T[], currentItems: T[]) => boolean; | ||
countLimit?: number; | ||
@@ -112,0 +112,0 @@ }; |
{ | ||
"name": "got", | ||
"version": "10.6.0", | ||
"version": "10.7.0", | ||
"description": "Human-friendly and powerful HTTP request library for Node.js", | ||
@@ -97,3 +97,2 @@ "license": "MIT", | ||
"browser": { | ||
"decompress-response": false, | ||
"electron": false | ||
@@ -100,0 +99,0 @@ }, |
@@ -193,2 +193,4 @@ <div align="center"> | ||
**Note #4:** This option is not enumerable and will not be merged with the instance defaults. | ||
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`. | ||
@@ -200,3 +202,4 @@ | ||
**Note:** If you provide this option, `got.stream()` will be read-only. | ||
**Note #1:** If you provide this option, `got.stream()` will be read-only. | ||
**Note #2:** This option is not enumerable and will not be merged with the instance defaults. | ||
@@ -222,3 +225,3 @@ JSON body. If the `Content-Type` header is not set, it will be set to `application/json`. | ||
options => { | ||
if (!options.context && !options.context.token) { | ||
if (!options.context || !options.context.token) { | ||
throw new Error('Token required'); | ||
@@ -254,3 +257,4 @@ } | ||
The promise also has `.text()`, `.json()` and `.buffer()` methods which sets this and the `resolveBodyOnly` option automatically. | ||
The promise also has `.text()`, `.json()` and `.buffer()` methods which return another Got promise for the parsed body.\ | ||
It's like setting the options to `{responseType: 'json', resolveBodyOnly: true}` but without affecting the main Got promise. | ||
@@ -260,6 +264,19 @@ Example: | ||
```js | ||
(async () => { | ||
const responsePromise = got(url); | ||
const bufferPromise = responsePromise.buffer(); | ||
const jsonPromise = responsePromise.json(); | ||
const [response, buffer, json] = Promise.all([responsePromise, bufferPromise, jsonPromise]); | ||
// `response` is an instance of Got Response | ||
// `buffer` is an instance of Buffer | ||
// `json` is an object | ||
})(); | ||
``` | ||
```js | ||
// This | ||
const body = await got(url).json(); | ||
// is the same as this | ||
// is semantically the same as this | ||
const body = await got(url, {responseType: 'json', resolveBodyOnly: true}); | ||
@@ -270,3 +287,3 @@ ``` | ||
Type: `string`\ | ||
Type: `boolean`\ | ||
Default: `false` | ||
@@ -316,3 +333,4 @@ | ||
**Note:** If you provide this option, `got.stream()` will be read-only. | ||
**Note #1:** If you provide this option, `got.stream()` will be read-only. | ||
**Note #2:** This option is not enumerable and will not be merged with the instance defaults. | ||
@@ -570,3 +588,3 @@ The form body is converted to query string using [`(new URLSearchParams(object)).toString()`](https://nodejs.org/api/url.html#url_constructor_new_urlsearchparams_obj). | ||
(options, error, retryCount) => { | ||
if (error.statusCode === 413) { // Payload too large | ||
if (error.response.statusCode === 413) { // Payload too large | ||
options.body = getNewBody(); | ||
@@ -647,3 +665,3 @@ } | ||
error.name = 'GitHubError'; | ||
error.message = `${response.body.message} (${error.statusCode})`; | ||
error.message = `${response.body.message} (${response.statusCode})`; | ||
} | ||
@@ -676,8 +694,49 @@ | ||
A function that returns an object representing Got options pointing to the next page. If there are no more pages, `false` should be returned. | ||
The function takes three arguments: | ||
- `response` - The current response object. | ||
- `allItems` - An array of the emitted items. | ||
- `currentItems` - Items from the current response. | ||
It should return an object representing Got options pointing to the next page. If there are no more pages, `false` should be returned. | ||
For example, if you want to stop when the response contains less items than expected, you can use something like this: | ||
```js | ||
const got = require('got'); | ||
(async () => { | ||
const limit = 10; | ||
const items = got.paginate('https://example.com/items', { | ||
searchParams: { | ||
limit, | ||
offset: 0 | ||
}, | ||
_pagination: { | ||
paginate: (response, allItems, currentItems) => { | ||
const previousSearchParams = response.request.options.searchParams; | ||
const {offset: previousOffset} = previousSearchParams; | ||
if (currentItems.length < limit) { | ||
return false; | ||
} | ||
return { | ||
searchParams: { | ||
...previousSearchParams, | ||
offset: previousOffset + limit, | ||
} | ||
}; | ||
} | ||
} | ||
}); | ||
console.log('Items from all pages:', items); | ||
})(); | ||
``` | ||
###### \_pagination.filter | ||
Type: `Function`\ | ||
Default: `(item, allItems) => true` | ||
Default: `(item, allItems, currentItems) => true` | ||
@@ -689,7 +748,7 @@ Checks whether the item should be emitted or not. | ||
Type: `Function`\ | ||
Default: `(item, allItems) => true` | ||
Default: `(item, allItems, currentItems) => true` | ||
Checks whether the pagination should continue. | ||
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. | ||
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. | ||
@@ -1026,2 +1085,10 @@ ###### \_pagination.countLimit | ||
- If the new property is an `Array`, it overwrites the old one with a deep clone of the new property. | ||
- Properties that are not enumerable, such as `context`, `body`, `json`, and `form`, will not be merged. | ||
```js | ||
const a = {json: {cat: 'meow'}}; | ||
const b = {json: {cow: 'moo'}}; | ||
got.mergeOptions(a, b); | ||
//=> {json: {cow: 'moo'}} | ||
``` | ||
- Otherwise, the new value is assigned to the key. | ||
@@ -1028,0 +1095,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
167281
2411
1804