Skip to content

Commit a1b259c

Browse files
committed
object
1 parent 51d88a0 commit a1b259c

File tree

6 files changed

+40
-29
lines changed

6 files changed

+40
-29
lines changed

readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ A function that transform [`Response`](#response) into an array of items. This i
934934
Type: `Function`\
935935
Default: [`Link` header logic](source/index.ts)
936936

937-
The function takes three arguments:
937+
The function takes an object with the following properties:
938938
- `response` - The current response object.
939939
- `allItems` - An array of the emitted items.
940940
- `currentItems` - Items from the current response.
@@ -955,7 +955,7 @@ const got = require('got');
955955
offset: 0
956956
},
957957
pagination: {
958-
paginate: (response, allItems, currentItems) => {
958+
paginate: ({response, allItems, currentItems}) => {
959959
const previousSearchParams = response.request.options.searchParams;
960960
const previousOffset = previousSearchParams.get('offset');
961961

source/as-promise/types.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ All parsing methods supported by Got.
1111
*/
1212
export type ResponseType = 'json' | 'buffer' | 'text';
1313

14+
export interface PaginateData<R, T> {
15+
response: Response<R>;
16+
allItems: T[];
17+
currentItems: T[];
18+
}
19+
1420
export interface PaginationOptions<T, R> {
1521
/**
1622
All options accepted by `got.paginate()`.
@@ -76,7 +82,7 @@ export interface PaginationOptions<T, R> {
7682
})();
7783
```
7884
*/
79-
paginate?: (response: Response<R>, allItems: T[], currentItems: T[]) => Options | false;
85+
paginate?: (paginate: PaginateData<R, T>) => Options | false;
8086

8187
/**
8288
Checks whether the pagination should continue.

source/create.ts

+16-12
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ const create = (defaults: InstanceDefaults): Got => {
225225
throw new TypeError('`options.pagination` must be implemented');
226226
}
227227

228-
const all: T[] = [];
228+
const allItems: T[] = [];
229229
let {countLimit} = pagination;
230230

231231
let numberOfRequests = 0;
@@ -236,42 +236,46 @@ const create = (defaults: InstanceDefaults): Got => {
236236
}
237237

238238
// @ts-expect-error FIXME!
239-
// TODO: Throw when result is not an instance of Response
239+
// TODO: Throw when response is not an instance of Response
240240
// eslint-disable-next-line no-await-in-loop
241-
const result = (await got(undefined, undefined, normalizedOptions)) as Response;
241+
const response = (await got(undefined, undefined, normalizedOptions)) as Response;
242242

243243
// eslint-disable-next-line no-await-in-loop
244-
const parsed = await pagination.transform(result);
245-
const current: T[] = [];
244+
const parsed = await pagination.transform(response);
245+
const currentItems: T[] = [];
246246

247247
for (const item of parsed) {
248-
if (pagination.filter(item, all, current)) {
249-
if (!pagination.shouldContinue(item, all, current)) {
248+
if (pagination.filter(item, allItems, currentItems)) {
249+
if (!pagination.shouldContinue(item, allItems, currentItems)) {
250250
return;
251251
}
252252

253253
yield item as T;
254254

255255
if (pagination.stackAllItems) {
256-
all.push(item as T);
256+
allItems.push(item as T);
257257
}
258258

259-
current.push(item as T);
259+
currentItems.push(item as T);
260260

261261
if (--countLimit <= 0) {
262262
return;
263263
}
264264
}
265265
}
266266

267-
const optionsToMerge = pagination.paginate(result, all, current);
267+
const optionsToMerge = pagination.paginate({
268+
response,
269+
allItems,
270+
currentItems
271+
});
268272

269273
if (optionsToMerge === false) {
270274
return;
271275
}
272276

273-
if (optionsToMerge === result.request.options) {
274-
normalizedOptions = result.request.options;
277+
if (optionsToMerge === response.request.options) {
278+
normalizedOptions = response.request.options;
275279
} else if (optionsToMerge !== undefined) {
276280
normalizedOptions = normalizeArguments(undefined, optionsToMerge, normalizedOptions);
277281
}

source/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const defaults: InstanceDefaults = {
7777

7878
return JSON.parse(response.body as string);
7979
},
80-
paginate: response => {
80+
paginate: ({response}) => {
8181
if (!Reflect.has(response.headers, 'link')) {
8282
return false;
8383
}

test/hooks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@ test('no duplicate hook calls when returning original request options', withServ
12081208
};
12091209

12101210
// Test only two requests, one after another
1211-
const paginate = (response: Response) => requestNumber++ === 0 ? response.request.options : false;
1211+
const paginate = ({response}: {response: Response}) => requestNumber++ === 0 ? response.request.options : false;
12121212

12131213
const instance = got.extend({
12141214
hooks,

test/pagination.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ test('custom paginate function', withServer, async (t, server, got) => {
126126

127127
const result = await got.paginate.all<number>({
128128
pagination: {
129-
paginate: response => {
129+
paginate: ({response}) => {
130130
const url = new URL(response.url);
131131

132132
if (url.search === '?page=3') {
@@ -148,13 +148,14 @@ test('custom paginate function using allItems', withServer, async (t, server, go
148148

149149
const result = await got.paginate.all<number>({
150150
pagination: {
151-
paginate: (_response, allItems: number[]) => {
151+
paginate: ({allItems}) => {
152152
if (allItems.length === 2) {
153153
return false;
154154
}
155155

156156
return {path: '/?page=3'};
157-
}
157+
},
158+
stackAllItems: true
158159
}
159160
});
160161

@@ -166,7 +167,7 @@ test('custom paginate function using currentItems', withServer, async (t, server
166167

167168
const result = await got.paginate.all<number>({
168169
pagination: {
169-
paginate: (_response, _allItems: number[], currentItems: number[]) => {
170+
paginate: ({currentItems}) => {
170171
if (currentItems[0] === 3) {
171172
return false;
172173
}
@@ -357,7 +358,7 @@ test('`hooks` are not duplicated', withServer, async (t, server, got) => {
357358

358359
const result = await got.paginate.all<number>({
359360
pagination: {
360-
paginate: response => {
361+
paginate: ({response}) => {
361362
if ((response.body as string) === '[3]') {
362363
return false; // Stop after page 3
363364
}
@@ -495,11 +496,11 @@ test('`stackAllItems` set to true', withServer, async (t, server, got) => {
495496

496497
return true;
497498
},
498-
paginate: (response, allItems, currentItems) => {
499+
paginate: ({response, allItems, currentItems}) => {
499500
itemCount += 1;
500501
t.is(allItems.length, itemCount);
501502

502-
return got.defaults.options.pagination!.paginate(response, allItems, currentItems);
503+
return got.defaults.options.pagination!.paginate({response, allItems, currentItems});
503504
}
504505
}
505506
});
@@ -523,10 +524,10 @@ test('`stackAllItems` set to false', withServer, async (t, server, got) => {
523524

524525
return true;
525526
},
526-
paginate: (response, allItems, currentItems) => {
527+
paginate: ({response, allItems, currentItems}) => {
527528
t.is(allItems.length, 0);
528529

529-
return got.defaults.options.pagination!.paginate(response, allItems, currentItems);
530+
return got.defaults.options.pagination!.paginate({response, allItems, currentItems});
530531
}
531532
}
532533
});
@@ -559,7 +560,7 @@ test('next url in json response', withServer, async (t, server, got) => {
559560
transform: (response: Response<Page>) => {
560561
return [response.body.currentUrl];
561562
},
562-
paginate: (response: Response<Page>) => {
563+
paginate: ({response}) => {
563564
const {next} = response.body;
564565

565566
if (!next) {
@@ -608,7 +609,7 @@ test('pagination using searchParams', withServer, async (t, server, got) => {
608609
transform: (response: Response<Page>) => {
609610
return [response.body.currentUrl];
610611
},
611-
paginate: (response: Response<Page>) => {
612+
paginate: ({response}) => {
612613
const {next} = response.body;
613614
const previousPage = Number(response.request.options.searchParams!.get('page'));
614615

@@ -664,7 +665,7 @@ test('pagination using extended searchParams', withServer, async (t, server, got
664665
transform: (response: Response<Page>) => {
665666
return [response.body.currentUrl];
666667
},
667-
paginate: (response: Response<Page>) => {
668+
paginate: ({response}) => {
668669
const {next} = response.body;
669670
const previousPage = Number(response.request.options.searchParams!.get('page'));
670671

0 commit comments

Comments
 (0)