From a1b259cb747c449f6f6daf94b5ed24490fccf010 Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Fri, 26 Feb 2021 13:24:33 +0100 Subject: [PATCH] object --- readme.md | 4 ++-- source/as-promise/types.ts | 8 +++++++- source/create.ts | 28 ++++++++++++++++------------ source/index.ts | 2 +- test/hooks.ts | 2 +- test/pagination.ts | 25 +++++++++++++------------ 6 files changed, 40 insertions(+), 29 deletions(-) diff --git a/readme.md b/readme.md index 642c2912e..fb7043805 100644 --- a/readme.md +++ b/readme.md @@ -934,7 +934,7 @@ A function that transform [`Response`](#response) into an array of items. This i Type: `Function`\ Default: [`Link` header logic](source/index.ts) -The function takes three arguments: +The function takes an object with the following properties: - `response` - The current response object. - `allItems` - An array of the emitted items. - `currentItems` - Items from the current response. @@ -955,7 +955,7 @@ const got = require('got'); offset: 0 }, pagination: { - paginate: (response, allItems, currentItems) => { + paginate: ({response, allItems, currentItems}) => { const previousSearchParams = response.request.options.searchParams; const previousOffset = previousSearchParams.get('offset'); diff --git a/source/as-promise/types.ts b/source/as-promise/types.ts index aa52695bd..83564a0d5 100644 --- a/source/as-promise/types.ts +++ b/source/as-promise/types.ts @@ -11,6 +11,12 @@ All parsing methods supported by Got. */ export type ResponseType = 'json' | 'buffer' | 'text'; +export interface PaginateData { + response: Response; + allItems: T[]; + currentItems: T[]; +} + export interface PaginationOptions { /** All options accepted by `got.paginate()`. @@ -76,7 +82,7 @@ export interface PaginationOptions { })(); ``` */ - paginate?: (response: Response, allItems: T[], currentItems: T[]) => Options | false; + paginate?: (paginate: PaginateData) => Options | false; /** Checks whether the pagination should continue. diff --git a/source/create.ts b/source/create.ts index 77fbdaa73..9079d7228 100644 --- a/source/create.ts +++ b/source/create.ts @@ -225,7 +225,7 @@ const create = (defaults: InstanceDefaults): Got => { throw new TypeError('`options.pagination` must be implemented'); } - const all: T[] = []; + const allItems: T[] = []; let {countLimit} = pagination; let numberOfRequests = 0; @@ -236,27 +236,27 @@ const create = (defaults: InstanceDefaults): Got => { } // @ts-expect-error FIXME! - // TODO: Throw when result is not an instance of Response + // TODO: Throw when response is not an instance of Response // eslint-disable-next-line no-await-in-loop - const result = (await got(undefined, undefined, normalizedOptions)) as Response; + const response = (await got(undefined, undefined, normalizedOptions)) as Response; // eslint-disable-next-line no-await-in-loop - const parsed = await pagination.transform(result); - const current: T[] = []; + const parsed = await pagination.transform(response); + const currentItems: T[] = []; for (const item of parsed) { - if (pagination.filter(item, all, current)) { - if (!pagination.shouldContinue(item, all, current)) { + if (pagination.filter(item, allItems, currentItems)) { + if (!pagination.shouldContinue(item, allItems, currentItems)) { return; } yield item as T; if (pagination.stackAllItems) { - all.push(item as T); + allItems.push(item as T); } - current.push(item as T); + currentItems.push(item as T); if (--countLimit <= 0) { return; @@ -264,14 +264,18 @@ const create = (defaults: InstanceDefaults): Got => { } } - const optionsToMerge = pagination.paginate(result, all, current); + const optionsToMerge = pagination.paginate({ + response, + allItems, + currentItems + }); if (optionsToMerge === false) { return; } - if (optionsToMerge === result.request.options) { - normalizedOptions = result.request.options; + if (optionsToMerge === response.request.options) { + normalizedOptions = response.request.options; } else if (optionsToMerge !== undefined) { normalizedOptions = normalizeArguments(undefined, optionsToMerge, normalizedOptions); } diff --git a/source/index.ts b/source/index.ts index 42dce9311..3d33587aa 100644 --- a/source/index.ts +++ b/source/index.ts @@ -77,7 +77,7 @@ const defaults: InstanceDefaults = { return JSON.parse(response.body as string); }, - paginate: response => { + paginate: ({response}) => { if (!Reflect.has(response.headers, 'link')) { return false; } diff --git a/test/hooks.ts b/test/hooks.ts index 55c8d0b79..6a53eb02d 100644 --- a/test/hooks.ts +++ b/test/hooks.ts @@ -1208,7 +1208,7 @@ test('no duplicate hook calls when returning original request options', withServ }; // Test only two requests, one after another - const paginate = (response: Response) => requestNumber++ === 0 ? response.request.options : false; + const paginate = ({response}: {response: Response}) => requestNumber++ === 0 ? response.request.options : false; const instance = got.extend({ hooks, diff --git a/test/pagination.ts b/test/pagination.ts index 754e99888..96ec22bbb 100644 --- a/test/pagination.ts +++ b/test/pagination.ts @@ -126,7 +126,7 @@ test('custom paginate function', withServer, async (t, server, got) => { const result = await got.paginate.all({ pagination: { - paginate: response => { + paginate: ({response}) => { const url = new URL(response.url); if (url.search === '?page=3') { @@ -148,13 +148,14 @@ test('custom paginate function using allItems', withServer, async (t, server, go const result = await got.paginate.all({ pagination: { - paginate: (_response, allItems: number[]) => { + paginate: ({allItems}) => { if (allItems.length === 2) { return false; } return {path: '/?page=3'}; - } + }, + stackAllItems: true } }); @@ -166,7 +167,7 @@ test('custom paginate function using currentItems', withServer, async (t, server const result = await got.paginate.all({ pagination: { - paginate: (_response, _allItems: number[], currentItems: number[]) => { + paginate: ({currentItems}) => { if (currentItems[0] === 3) { return false; } @@ -357,7 +358,7 @@ test('`hooks` are not duplicated', withServer, async (t, server, got) => { const result = await got.paginate.all({ pagination: { - paginate: response => { + paginate: ({response}) => { if ((response.body as string) === '[3]') { return false; // Stop after page 3 } @@ -495,11 +496,11 @@ test('`stackAllItems` set to true', withServer, async (t, server, got) => { return true; }, - paginate: (response, allItems, currentItems) => { + paginate: ({response, allItems, currentItems}) => { itemCount += 1; t.is(allItems.length, itemCount); - return got.defaults.options.pagination!.paginate(response, allItems, currentItems); + return got.defaults.options.pagination!.paginate({response, allItems, currentItems}); } } }); @@ -523,10 +524,10 @@ test('`stackAllItems` set to false', withServer, async (t, server, got) => { return true; }, - paginate: (response, allItems, currentItems) => { + paginate: ({response, allItems, currentItems}) => { t.is(allItems.length, 0); - return got.defaults.options.pagination!.paginate(response, allItems, currentItems); + return got.defaults.options.pagination!.paginate({response, allItems, currentItems}); } } }); @@ -559,7 +560,7 @@ test('next url in json response', withServer, async (t, server, got) => { transform: (response: Response) => { return [response.body.currentUrl]; }, - paginate: (response: Response) => { + paginate: ({response}) => { const {next} = response.body; if (!next) { @@ -608,7 +609,7 @@ test('pagination using searchParams', withServer, async (t, server, got) => { transform: (response: Response) => { return [response.body.currentUrl]; }, - paginate: (response: Response) => { + paginate: ({response}) => { const {next} = response.body; const previousPage = Number(response.request.options.searchParams!.get('page')); @@ -664,7 +665,7 @@ test('pagination using extended searchParams', withServer, async (t, server, got transform: (response: Response) => { return [response.body.currentUrl]; }, - paginate: (response: Response) => { + paginate: ({response}) => { const {next} = response.body; const previousPage = Number(response.request.options.searchParams!.get('page'));