From 1bfb67185b57eba271bbc3062b20add39017d4a4 Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Wed, 25 Mar 2020 10:30:39 +0100 Subject: [PATCH] Rename options._pagination to options.pagination --- source/as-promise/core.ts | 20 +++++++++---------- source/as-promise/types.ts | 6 +++--- source/create.ts | 4 ++-- source/index.ts | 4 ++-- test/pagination.ts | 40 +++++++++++++++++++------------------- 5 files changed, 37 insertions(+), 37 deletions(-) diff --git a/source/as-promise/core.ts b/source/as-promise/core.ts index 310204b7a..6692ce0e7 100644 --- a/source/as-promise/core.ts +++ b/source/as-promise/core.ts @@ -80,31 +80,31 @@ export default class PromisableRequest extends Request { ); } - // `options._pagination` - if (is.object(options._pagination)) { + // `options.pagination` + if (is.object(options.pagination)) { if (defaults) { - (options as Options)._pagination = { - ...defaults._pagination, - ...options._pagination + (options as Options).pagination = { + ...defaults.pagination, + ...options.pagination }; } - const {_pagination: pagination} = options; + const {pagination} = options; if (!is.function_(pagination.transform)) { - throw new Error('`options._pagination.transform` must be implemented'); + throw new Error('`options.pagination.transform` must be implemented'); } if (!is.function_(pagination.shouldContinue)) { - throw new Error('`options._pagination.shouldContinue` must be implemented'); + throw new Error('`options.pagination.shouldContinue` must be implemented'); } if (!is.function_(pagination.filter)) { - throw new TypeError('`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 Error('`options.pagination.paginate` must be implemented'); } } diff --git a/source/as-promise/types.ts b/source/as-promise/types.ts index 9fbaee314..f1bed81ff 100644 --- a/source/as-promise/types.ts +++ b/source/as-promise/types.ts @@ -69,7 +69,7 @@ export interface Hooks extends RequestHooks { } export interface PaginationOptions { - _pagination?: { + pagination?: { transform?: (response: Response) => Promise | T[]; filter?: (item: T, allItems: T[], currentItems: T[]) => boolean; paginate?: (response: Response, allItems: T[], currentItems: T[]) => Options | false; @@ -92,7 +92,7 @@ export interface NormalizedOptions extends RequestNormalizedOptions { resolveBodyOnly: boolean; retry: RequiredRetryOptions; isStream: boolean; - _pagination?: Required['_pagination']>; + pagination?: Required['pagination']>; } export interface Defaults extends RequestDefaults { @@ -101,7 +101,7 @@ export interface Defaults extends RequestDefaults { resolveBodyOnly: boolean; retry: RequiredRetryOptions; isStream: boolean; - _pagination?: Required['_pagination']>; + pagination?: Required['pagination']>; } export class ParseError extends RequestError { diff --git a/source/create.ts b/source/create.ts index 3249f502a..0fbd16a02 100644 --- a/source/create.ts +++ b/source/create.ts @@ -195,10 +195,10 @@ const create = (defaults: InstanceDefaults): Got => { got.paginate = (async function * (url: string | URL, options?: Options) { let normalizedOptions = normalizeArguments(url, options, defaults.options); - const pagination = normalizedOptions._pagination!; + const pagination = normalizedOptions.pagination!; if (!is.object(pagination)) { - throw new TypeError('`options._pagination` must be implemented'); + throw new TypeError('`options.pagination` must be implemented'); } const all: T[] = []; diff --git a/source/index.ts b/source/index.ts index aeef0d0b3..a05d358ff 100644 --- a/source/index.ts +++ b/source/index.ts @@ -66,11 +66,11 @@ const defaults: InstanceDefaults = { methodRewriting: true, ignoreInvalidCookies: false, context: {}, - // TODO: Set this to `false` when Got 12 gets released + // TODO: Set this to `true` when Got 12 gets released http2: false, allowGetBody: false, rejectUnauthorized: true, - _pagination: { + pagination: { transform: (response: Response) => { if (response.request.options.responseType === 'json') { return response.body; diff --git a/test/pagination.ts b/test/pagination.ts index b45e3b582..c1e35a50c 100644 --- a/test/pagination.ts +++ b/test/pagination.ts @@ -58,7 +58,7 @@ test('retrieves all elements with JSON responseType', withServer, async (t, serv t.deepEqual(result, [1, 2]); }); -test('points to defaults when extending Got without custom `_pagination`', withServer, async (t, server, got) => { +test('points to defaults when extending Got without custom `pagination`', withServer, async (t, server, got) => { attachHandler(server, 2); const result = await got.extend().paginate.all(''); @@ -70,7 +70,7 @@ test('pagination options can be extended', withServer, async (t, server, got) => attachHandler(server, 2); const result = await got.extend({ - _pagination: { + pagination: { shouldContinue: () => false } }).paginate.all(''); @@ -82,7 +82,7 @@ test('filters elements', withServer, async (t, server, got) => { attachHandler(server, 3); const result = await got.paginate.all({ - _pagination: { + pagination: { filter: (element: unknown, allItems: unknown[], currentItems: unknown[]) => { t.true(Array.isArray(allItems)); t.true(Array.isArray(currentItems)); @@ -99,7 +99,7 @@ test('parses elements', withServer, async (t, server, got) => { attachHandler(server, 100); const result = await got.paginate.all('?page=100', { - _pagination: { + pagination: { transform: (response: Response) => [(response as Response).body.length] } }); @@ -111,7 +111,7 @@ test('parses elements - async function', withServer, async (t, server, got) => { attachHandler(server, 100); const result = await got.paginate.all('?page=100', { - _pagination: { + pagination: { transform: async (response: Response) => [(response as Response).body.length] } }); @@ -123,7 +123,7 @@ test('custom paginate function', withServer, async (t, server, got) => { attachHandler(server, 3); const result = await got.paginate.all({ - _pagination: { + pagination: { paginate: (response: Response) => { const url = new URL(response.url); @@ -145,7 +145,7 @@ test('custom paginate function using allItems', withServer, async (t, server, go attachHandler(server, 3); const result = await got.paginate.all({ - _pagination: { + pagination: { paginate: (_response: Response, allItems: unknown[]) => { if (allItems.length === 2) { return false; @@ -163,7 +163,7 @@ test('custom paginate function using currentItems', withServer, async (t, server attachHandler(server, 3); const result = await got.paginate.all({ - _pagination: { + pagination: { paginate: (_response: Response, _allItems: unknown[], currentItems: unknown[]) => { if (currentItems[0] === 3) { return false; @@ -193,7 +193,7 @@ test('`shouldContinue` works', withServer, async (t, server, got) => { attachHandler(server, 2); const options = { - _pagination: { + pagination: { shouldContinue: (_item: unknown, allItems: unknown[], currentItems: unknown[]) => { t.true(Array.isArray(allItems)); t.true(Array.isArray(currentItems)); @@ -216,7 +216,7 @@ test('`countLimit` works', withServer, async (t, server, got) => { attachHandler(server, 2); const options = { - _pagination: { + pagination: { countLimit: 1 } }; @@ -232,30 +232,30 @@ test('`countLimit` works', withServer, async (t, server, got) => { test('throws if no `pagination` option', async t => { const iterator = got.extend({ - _pagination: false as any + pagination: false as any }).paginate('', { prefixUrl: 'https://example.com' }); await t.throwsAsync(iterator.next(), { - message: '`options._pagination` must be implemented' + message: '`options.pagination` must be implemented' }); }); test('throws if the `pagination` option does not have `transform` property', async t => { const iterator = got.paginate('', { - _pagination: {...resetPagination}, + pagination: {...resetPagination}, prefixUrl: 'https://example.com' }); await t.throwsAsync(iterator.next(), { - message: '`options._pagination.transform` must be implemented' + message: '`options.pagination.transform` must be implemented' }); }); test('throws if the `pagination` option does not have `shouldContinue` property', async t => { const iterator = got.paginate('', { - _pagination: { + pagination: { ...resetPagination, transform: thrower }, @@ -263,13 +263,13 @@ test('throws if the `pagination` option does not have `shouldContinue` property' }); await t.throwsAsync(iterator.next(), { - message: '`options._pagination.shouldContinue` must be implemented' + message: '`options.pagination.shouldContinue` must be implemented' }); }); test('throws if the `pagination` option does not have `filter` property', async t => { const iterator = got.paginate('', { - _pagination: { + pagination: { ...resetPagination, transform: thrower, shouldContinue: thrower, @@ -279,13 +279,13 @@ test('throws if the `pagination` option does not have `filter` property', async }); await t.throwsAsync(iterator.next(), { - message: '`options._pagination.filter` must be implemented' + message: '`options.pagination.filter` must be implemented' }); }); test('throws if the `pagination` option does not have `paginate` property', async t => { const iterator = got.paginate('', { - _pagination: { + pagination: { ...resetPagination, transform: thrower, shouldContinue: thrower, @@ -295,6 +295,6 @@ test('throws if the `pagination` option does not have `paginate` property', asyn }); await t.throwsAsync(iterator.next(), { - message: '`options._pagination.paginate` must be implemented' + message: '`options.pagination.paginate` must be implemented' }); });