Skip to content

Commit 4344c3a

Browse files
authoredApr 26, 2020
Limit number of requests in pagination (#1181)
* 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
1 parent d087215 commit 4344c3a

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed
 

‎readme.md

+9
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,15 @@ Default: `Infinity`
760760

761761
The maximum amount of items that should be emitted.
762762

763+
###### pagination.requestLimit
764+
765+
Type: `number`\
766+
Default: `10000`
767+
768+
The maximum amount of request that should be triggered. [Retries on failure](#retry) are not counted towards this limit.
769+
770+
For example, it can be helpful during development to avoid an infinite number of requests.
771+
763772
##### localAddress
764773

765774
Type: `string`

‎source/as-promise/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export interface PaginationOptions<T> {
7777
paginate?: (response: Response, allItems: T[], currentItems: T[]) => Options | false;
7878
shouldContinue?: (item: T, allItems: T[], currentItems: T[]) => boolean;
7979
countLimit?: number;
80+
requestLimit?: number;
8081
};
8182
}
8283

‎source/create.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ const create = (defaults: InstanceDefaults): Got => {
206206

207207
const all: T[] = [];
208208

209-
while (true) {
209+
let numberOfRequests = 0;
210+
while (numberOfRequests < pagination.requestLimit) {
210211
// TODO: Throw when result is not an instance of Response
211212
// eslint-disable-next-line no-await-in-loop
212213
const result = (await got('', normalizedOptions)) as Response;
@@ -241,6 +242,8 @@ const create = (defaults: InstanceDefaults): Got => {
241242
if (optionsToMerge !== undefined) {
242243
normalizedOptions = normalizeArguments(undefined, optionsToMerge, normalizedOptions);
243244
}
245+
246+
numberOfRequests++;
244247
}
245248
}) as GotPaginate;
246249

‎source/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ const defaults: InstanceDefaults = {
108108
},
109109
filter: () => true,
110110
shouldContinue: () => true,
111-
countLimit: Infinity
111+
countLimit: Infinity,
112+
requestLimit: 10000
112113
}
113114
},
114115
handlers: [defaultHandler],

‎test/pagination.ts

+18
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,21 @@ test.failing('allowGetBody sends json payload with .paginate()', withBodyParsing
328328

329329
t.deepEqual(result.value, [1, 2, 3]);
330330
});
331+
332+
test('`requestLimit` works', withServer, async (t, server, got) => {
333+
attachHandler(server, 2);
334+
335+
const options = {
336+
pagination: {
337+
requestLimit: 1
338+
}
339+
};
340+
341+
const results: number[] = [];
342+
343+
for await (const item of got.paginate<number>(options)) {
344+
results.push(item);
345+
}
346+
347+
t.deepEqual(results, [1]);
348+
});

0 commit comments

Comments
 (0)
Please sign in to comment.