分页 API¶
源码: source/create.ts
How does the Link
header work?¶
The RFC5988 defines how the Link
header looks like.
When the response has been processed, Got looks for the reference of the next
relation.
This way Got knows the URL it should visit afterwards. The header can look like this:
By default, Got looks only at the next
relation. To use other relations, you need to customize the paginate
function below.
got.paginate(url, options?)
¶
got.paginate.each(url, options?)
¶
Returns an async iterator.
This is memory efficient, as the logic is executed immediately when new data comes in.
got.paginate.all(url, options?)
¶
Note:
- Querying a large dataset significantly increases memory usage.
Returns a Promise for an array of all results.
pagination
¶
类型: object
Default:
This option represents the pagination
object.
transform
¶
类型: Function
默认: response => JSON.parse(response.body)
A function that transforms Response
into an array of items.
This is where you should do the parsing.
paginate
¶
类型: Function
默认: Link
header logic
The function takes an object with the following properties:
response
- The current response object,currentItems
- Items from the current response,allItems
- An empty array, unlessstackAllItems
istrue
, otherwise it contains all emitted items.
It should return an object representing Got options pointing to the next page. If there is no next page, false
should be returned instead.
The options are merged automatically with the previous request.
Therefore the options returned by pagination.paginate(…)
must reflect changes only.
Note:
- The
url
option (if set) accepts only aURL
instance.
This preventsprefixUrl
ambiguity. In order to use a relative URL string, merge it vianew URL(relativeUrl, response.url)
.
filter
¶
类型: Function
默认: ({item, currentItems, allItems}) => true
Whether the item should be emitted or not.
shouldContinue
¶
类型: Function
默认: ({item, currentItems, allItems}) => true
Note:
- This function executes only when
filter
returnstrue
.
For example, if you need to stop before emitting an entry with some flag, you should use ({item}) => !item.flag
.
If you want to stop after emitting the entry, you should use ({item, allItems}) => allItems.some(item => item.flag)
instead.
countLimit
¶
类型: number
默认: Number.POSITIVE_INFINITY
The maximum amount of items that should be emitted.
backoff
¶
类型: number
默认: 0
Milliseconds to wait before the next request is triggered.
requestLimit
¶
类型: number
默认: 10000
The maximum amount of request that should be triggered.
Note:
- Retries on failure are not counted towards this limit.
stackAllItems
¶
类型: boolean
默认: false
Defines how allItems
is managed in pagination.paginate
, pagination.filter
and pagination.shouldContinue
.
By default, allItems
is always an empty array. Setting this to true
will significantly increase memory usage when working with a large dataset.
Example¶
In this example we will use searchParams
instead of Link
header.
Just to show how you can customize the paginate
function.
The reason filter
looks exactly the same like shouldContinue
is that the latter will tell Got to stop once we reach our timestamp.
The filter
function is needed as well, because in the same response we can get results with different timestamps.