From 61321e66969fedc4ed21247725ebc7129e01c92d Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Wed, 25 Mar 2020 11:21:50 +0100 Subject: [PATCH] Port #1099 --- source/types.ts | 10 ++++++---- test/pagination.ts | 38 +++++++++++++++++++------------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/source/types.ts b/source/types.ts index 7c559b061..31be811cf 100644 --- a/source/types.ts +++ b/source/types.ts @@ -22,7 +22,9 @@ import { } from './as-promise'; import Request from './core'; +// `type-fest` utilities type Except = Pick>; +type Merge = Except> & SecondType; export interface InstanceDefaults { options: DefaultOptions; @@ -46,15 +48,15 @@ export type StrictOptions = Except(url: string | URL, options?: Options & PaginationOptions): AsyncIterableIterator; - all(url: string | URL, options?: Options & PaginationOptions): Promise; + (url: string | URL, options?: Merge>): AsyncIterableIterator; + all(url: string | URL, options?: Merge>): Promise; // A bug. // eslint-disable-next-line @typescript-eslint/adjacent-overload-signatures - (options?: Options & PaginationOptions): AsyncIterableIterator; + (options?: Merge>): AsyncIterableIterator; // A bug. // eslint-disable-next-line @typescript-eslint/adjacent-overload-signatures - all(options?: Options & PaginationOptions): Promise; + all(options?: Merge>): Promise; } export interface GotRequestFunction { diff --git a/test/pagination.ts b/test/pagination.ts index c1e35a50c..1306965e3 100644 --- a/test/pagination.ts +++ b/test/pagination.ts @@ -43,7 +43,7 @@ test('the link header has no next value', withServer, async (t, server, got) => test('retrieves all elements', withServer, async (t, server, got) => { attachHandler(server, 2); - const result = await got.paginate.all(''); + const result = await got.paginate.all(''); t.deepEqual(result, [1, 2]); }); @@ -53,7 +53,7 @@ test('retrieves all elements with JSON responseType', withServer, async (t, serv const result = await got.extend({ responseType: 'json' - }).paginate.all(''); + }).paginate.all(''); t.deepEqual(result, [1, 2]); }); @@ -61,7 +61,7 @@ test('retrieves all elements with JSON responseType', withServer, async (t, serv 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(''); + const result = await got.extend().paginate.all(''); t.deepEqual(result, [1, 2]); }); @@ -73,7 +73,7 @@ test('pagination options can be extended', withServer, async (t, server, got) => pagination: { shouldContinue: () => false } - }).paginate.all(''); + }).paginate.all(''); t.deepEqual(result, []); }); @@ -81,9 +81,9 @@ test('pagination options can be extended', withServer, async (t, server, got) => test('filters elements', withServer, async (t, server, got) => { attachHandler(server, 3); - const result = await got.paginate.all({ + const result = await got.paginate.all({ pagination: { - filter: (element: unknown, allItems: unknown[], currentItems: unknown[]) => { + filter: (element: number, allItems: number[], currentItems: number[]) => { t.true(Array.isArray(allItems)); t.true(Array.isArray(currentItems)); @@ -98,7 +98,7 @@ test('filters elements', withServer, async (t, server, got) => { test('parses elements', withServer, async (t, server, got) => { attachHandler(server, 100); - const result = await got.paginate.all('?page=100', { + const result = await got.paginate.all('?page=100', { pagination: { transform: (response: Response) => [(response as Response).body.length] } @@ -110,7 +110,7 @@ test('parses elements', withServer, async (t, server, got) => { test('parses elements - async function', withServer, async (t, server, got) => { attachHandler(server, 100); - const result = await got.paginate.all('?page=100', { + const result = await got.paginate.all('?page=100', { pagination: { transform: async (response: Response) => [(response as Response).body.length] } @@ -122,7 +122,7 @@ test('parses elements - async function', withServer, async (t, server, got) => { test('custom paginate function', withServer, async (t, server, got) => { attachHandler(server, 3); - const result = await got.paginate.all({ + const result = await got.paginate.all({ pagination: { paginate: (response: Response) => { const url = new URL(response.url); @@ -144,9 +144,9 @@ test('custom paginate function', withServer, async (t, server, got) => { test('custom paginate function using allItems', withServer, async (t, server, got) => { attachHandler(server, 3); - const result = await got.paginate.all({ + const result = await got.paginate.all({ pagination: { - paginate: (_response: Response, allItems: unknown[]) => { + paginate: (_response: Response, allItems: number[]) => { if (allItems.length === 2) { return false; } @@ -162,9 +162,9 @@ test('custom paginate function using allItems', withServer, async (t, server, go test('custom paginate function using currentItems', withServer, async (t, server, got) => { attachHandler(server, 3); - const result = await got.paginate.all({ + const result = await got.paginate.all({ pagination: { - paginate: (_response: Response, _allItems: unknown[], currentItems: unknown[]) => { + paginate: (_response: Response, _allItems: number[], currentItems: number[]) => { if (currentItems[0] === 3) { return false; } @@ -180,9 +180,9 @@ test('custom paginate function using currentItems', withServer, async (t, server test('iterator works', withServer, async (t, server, got) => { attachHandler(server, 5); - const results = []; + const results: number[] = []; - for await (const item of got.paginate('')) { + for await (const item of got.paginate('')) { results.push(item); } @@ -203,9 +203,9 @@ test('`shouldContinue` works', withServer, async (t, server, got) => { } }; - const results = []; + const results: number[] = []; - for await (const item of got.paginate(options)) { + for await (const item of got.paginate(options)) { results.push(item); } @@ -221,9 +221,9 @@ test('`countLimit` works', withServer, async (t, server, got) => { } }; - const results = []; + const results: number[] = []; - for await (const item of got.paginate(options)) { + for await (const item of got.paginate(options)) { results.push(item); }