From 1f363b976cd23626462314c7cd5670cddd951c73 Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Sat, 8 Feb 2020 19:23:13 +0100 Subject: [PATCH] Fix merging pagination options --- source/create.ts | 12 ++++++------ source/normalize-arguments.ts | 17 +++++------------ test/pagination.ts | 14 +++++++++++++- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/source/create.ts b/source/create.ts index d49c55aaa..e59a39ee6 100644 --- a/source/create.ts +++ b/source/create.ts @@ -198,8 +198,8 @@ const create = (defaults: Defaults): Got => { } // @ts-ignore The missing property is added below - got.paginate = async function * (url: URLOrOptions & PaginationOptions, options?: Options & PaginationOptions) { - let normalizedOptions = normalizeArguments(url, options, defaults) as NormalizedOptions & PaginationOptions; + got.paginate = async function * (url: URLOrOptions, options?: Options) { + let normalizedOptions = normalizeArguments(url, options, defaults); const pagination = normalizedOptions._pagination!; @@ -240,16 +240,16 @@ const create = (defaults: Defaults): Got => { } if (optionsToMerge !== undefined) { - normalizedOptions = normalizeArguments(normalizedOptions, optionsToMerge) as NormalizedOptions & PaginationOptions; + normalizedOptions = normalizeArguments(normalizedOptions, optionsToMerge); } } }; - got.paginate.all = async (url: URLOrOptions & PaginationOptions, options?: Options & PaginationOptions) => { + got.paginate.all = async (url: URLOrOptions, options?: Options) => { const results: T[] = []; - for await (const item of got.paginate(url, options)) { - results.push(item); + for await (const item of got.paginate(url, options)) { + results.push(item as T); } return results; diff --git a/source/normalize-arguments.ts b/source/normalize-arguments.ts index fb7c3548e..39abc76e2 100644 --- a/source/normalize-arguments.ts +++ b/source/normalize-arguments.ts @@ -200,29 +200,22 @@ export const preNormalizeArguments = (options: Options, defaults?: NormalizedOpt // `options._pagination` if (is.object(options._pagination)) { - if (defaults && !(Reflect.has(options, '_pagination') && is.undefined(options._pagination))) { - options._pagination = { - ...defaults.pagination, - ...options._pagination - }; - } - - const pagination = options._pagination!; + const {_pagination: pagination} = options; if (!is.function_(pagination.transform)) { - throw new Error('`options._pagination.transform` must be implemented'); + throw new TypeError('`options._pagination.transform` must be implemented'); } if (!is.function_(pagination.shouldContinue)) { - throw new Error('`options._pagination.shouldContinue` must be implemented'); + throw new TypeError('`options._pagination.shouldContinue` must be implemented'); } if (!is.function_(pagination.filter)) { - throw new Error('`options._pagination.filter` must be implemented'); + throw new TypeError('`options._pagination.filter` must be implemented'); } if (!is.function_(pagination.paginate)) { - throw new Error('`options._pagination.paginate` must be implemented'); + throw new TypeError('`options._pagination.paginate` must be implemented'); } } diff --git a/test/pagination.ts b/test/pagination.ts index c55b0c767..031294dbe 100644 --- a/test/pagination.ts +++ b/test/pagination.ts @@ -55,6 +55,18 @@ test('points to defaults when extending Got without custom `_pagination`', withS t.deepEqual(result, [1, 2]); }); +test('pagination options can be extended', withServer, async (t, server, got) => { + attachHandler(server, 2); + + const result = await got.extend({ + _pagination: { + shouldContinue: () => false + } + }).paginate.all(''); + + t.deepEqual(result, []); +}); + test('filters elements', withServer, async (t, server, got) => { attachHandler(server, 3); @@ -159,7 +171,7 @@ test('`countLimit` works', withServer, async (t, server, got) => { test('throws if no `pagination` option', async t => { const iterator = got.extend({ - _pagination: undefined + _pagination: false as any }).paginate('', { prefixUrl: 'https://example.com' });