Skip to content

Commit

Permalink
Limit number of requests in pagination (#1181)
Browse files Browse the repository at this point in the history
* feat: limit number of requests in pagination

* style: fix lint

* fix: increase default to 10000

* fix: use lower than comparator

* docs: add note about retries

* docs: fix typo

* docs: update readme
  • Loading branch information
jaulz committed Apr 26, 2020
1 parent d087215 commit 4344c3a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
9 changes: 9 additions & 0 deletions readme.md
Expand Up @@ -760,6 +760,15 @@ Default: `Infinity`

The maximum amount of items that should be emitted.

###### pagination.requestLimit

Type: `number`\
Default: `10000`

The maximum amount of request that should be triggered. [Retries on failure](#retry) are not counted towards this limit.

For example, it can be helpful during development to avoid an infinite number of requests.

##### localAddress

Type: `string`
Expand Down
1 change: 1 addition & 0 deletions source/as-promise/types.ts
Expand Up @@ -77,6 +77,7 @@ export interface PaginationOptions<T> {
paginate?: (response: Response, allItems: T[], currentItems: T[]) => Options | false;
shouldContinue?: (item: T, allItems: T[], currentItems: T[]) => boolean;
countLimit?: number;
requestLimit?: number;
};
}

Expand Down
5 changes: 4 additions & 1 deletion source/create.ts
Expand Up @@ -206,7 +206,8 @@ const create = (defaults: InstanceDefaults): Got => {

const all: T[] = [];

while (true) {
let numberOfRequests = 0;
while (numberOfRequests < pagination.requestLimit) {
// TODO: Throw when result is not an instance of Response
// eslint-disable-next-line no-await-in-loop
const result = (await got('', normalizedOptions)) as Response;
Expand Down Expand Up @@ -241,6 +242,8 @@ const create = (defaults: InstanceDefaults): Got => {
if (optionsToMerge !== undefined) {
normalizedOptions = normalizeArguments(undefined, optionsToMerge, normalizedOptions);
}

numberOfRequests++;
}
}) as GotPaginate;

Expand Down
3 changes: 2 additions & 1 deletion source/index.ts
Expand Up @@ -108,7 +108,8 @@ const defaults: InstanceDefaults = {
},
filter: () => true,
shouldContinue: () => true,
countLimit: Infinity
countLimit: Infinity,
requestLimit: 10000
}
},
handlers: [defaultHandler],
Expand Down
18 changes: 18 additions & 0 deletions test/pagination.ts
Expand Up @@ -328,3 +328,21 @@ test.failing('allowGetBody sends json payload with .paginate()', withBodyParsing

t.deepEqual(result.value, [1, 2, 3]);
});

test('`requestLimit` works', withServer, async (t, server, got) => {
attachHandler(server, 2);

const options = {
pagination: {
requestLimit: 1
}
};

const results: number[] = [];

for await (const item of got.paginate<number>(options)) {
results.push(item);
}

t.deepEqual(results, [1]);
});

0 comments on commit 4344c3a

Please sign in to comment.