From bc51259d0e2d5447ad6d38904f31946786616a38 Mon Sep 17 00:00:00 2001 From: Julian Hundeloh Date: Tue, 3 Mar 2020 10:48:50 +0100 Subject: [PATCH 1/6] fix: typings for paginate --- source/create.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/source/create.ts b/source/create.ts index e59a39ee6..6af323667 100644 --- a/source/create.ts +++ b/source/create.ts @@ -1,4 +1,4 @@ -import {Merge} from 'type-fest'; +import {Merge, Except} from 'type-fest'; import is from '@sindresorhus/is'; import asPromise, {createRejection} from './as-promise'; import asStream, {ProxyStream} from './as-stream'; @@ -61,9 +61,11 @@ export interface GotRequestMethod { (url: string | Merge, options?: Merge): ProxyStream; } +export type GotPaginateOptions = Except> & PaginationOptions + export interface GotPaginate { - (url: URLOrOptions & PaginationOptions, options?: Options & PaginationOptions): AsyncIterableIterator; - all(url: URLOrOptions & PaginationOptions, options?: Options & PaginationOptions): Promise; + (url: string | GotPaginateOptions, options?: GotPaginateOptions): AsyncIterableIterator; + all(url: string | GotPaginateOptions, options?: GotPaginateOptions): Promise; } export interface Got extends Record, GotRequestMethod { @@ -198,8 +200,8 @@ const create = (defaults: Defaults): Got => { } // @ts-ignore The missing property is added below - got.paginate = async function * (url: URLOrOptions, options?: Options) { - let normalizedOptions = normalizeArguments(url, options, defaults); + got.paginate = async function * (url: string | GotPaginateOptions, options?: GotPaginateOptions) { + let normalizedOptions = normalizeArguments(url as URLOrOptions, options as Options, defaults); const pagination = normalizedOptions._pagination!; @@ -245,11 +247,11 @@ const create = (defaults: Defaults): Got => { } }; - got.paginate.all = async (url: URLOrOptions, options?: Options) => { + got.paginate.all = async (url: string | GotPaginateOptions, options?: GotPaginateOptions) => { const results: T[] = []; - for await (const item of got.paginate(url, options)) { - results.push(item as T); + for await (const item of got.paginate(url, options)) { + results.push(item); } return results; From 665b5e30d1c8875e4bedf3e0eb460b6a28a5fa3a Mon Sep 17 00:00:00 2001 From: Julian Hundeloh Date: Tue, 3 Mar 2020 13:00:32 +0000 Subject: [PATCH 2/6] style: fix lint --- source/create.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/create.ts b/source/create.ts index 6af323667..660c23cbd 100644 --- a/source/create.ts +++ b/source/create.ts @@ -61,7 +61,7 @@ export interface GotRequestMethod { (url: string | Merge, options?: Merge): ProxyStream; } -export type GotPaginateOptions = Except> & PaginationOptions +export type GotPaginateOptions = Except> & PaginationOptions; export interface GotPaginate { (url: string | GotPaginateOptions, options?: GotPaginateOptions): AsyncIterableIterator; From 7c8b888b1b906c55b14a9301642494ab2f7a7937 Mon Sep 17 00:00:00 2001 From: Julian Hundeloh Date: Fri, 20 Mar 2020 06:53:31 +0100 Subject: [PATCH 3/6] fix: use Omit instead of Except --- source/create.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/create.ts b/source/create.ts index 660c23cbd..e7c5d3ad7 100644 --- a/source/create.ts +++ b/source/create.ts @@ -1,4 +1,4 @@ -import {Merge, Except} from 'type-fest'; +import {Merge} from 'type-fest'; import is from '@sindresorhus/is'; import asPromise, {createRejection} from './as-promise'; import asStream, {ProxyStream} from './as-stream'; @@ -61,7 +61,7 @@ export interface GotRequestMethod { (url: string | Merge, options?: Merge): ProxyStream; } -export type GotPaginateOptions = Except> & PaginationOptions; +export type GotPaginateOptions = Omit> & PaginationOptions; export interface GotPaginate { (url: string | GotPaginateOptions, options?: GotPaginateOptions): AsyncIterableIterator; From afdb1fa3cedc5082e6bb2da35a7000aa88639fb1 Mon Sep 17 00:00:00 2001 From: Julian Hundeloh Date: Fri, 20 Mar 2020 07:07:28 +0100 Subject: [PATCH 4/6] fix: revert use of Omit --- source/create.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/create.ts b/source/create.ts index e7c5d3ad7..660c23cbd 100644 --- a/source/create.ts +++ b/source/create.ts @@ -1,4 +1,4 @@ -import {Merge} from 'type-fest'; +import {Merge, Except} from 'type-fest'; import is from '@sindresorhus/is'; import asPromise, {createRejection} from './as-promise'; import asStream, {ProxyStream} from './as-stream'; @@ -61,7 +61,7 @@ export interface GotRequestMethod { (url: string | Merge, options?: Merge): ProxyStream; } -export type GotPaginateOptions = Omit> & PaginationOptions; +export type GotPaginateOptions = Except> & PaginationOptions; export interface GotPaginate { (url: string | GotPaginateOptions, options?: GotPaginateOptions): AsyncIterableIterator; From db0724171b22c465559859f7d19444951ca8c39e Mon Sep 17 00:00:00 2001 From: Julian Hundeloh Date: Tue, 24 Mar 2020 06:47:55 +0000 Subject: [PATCH 5/6] fix: add URLOrGotPaginateOptions type --- source/create.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/source/create.ts b/source/create.ts index 660c23cbd..db69a3093 100644 --- a/source/create.ts +++ b/source/create.ts @@ -62,10 +62,11 @@ export interface GotRequestMethod { } export type GotPaginateOptions = Except> & PaginationOptions; +export type URLOrGotPaginateOptions = string | GotPaginateOptions; export interface GotPaginate { - (url: string | GotPaginateOptions, options?: GotPaginateOptions): AsyncIterableIterator; - all(url: string | GotPaginateOptions, options?: GotPaginateOptions): Promise; + (url: URLOrGotPaginateOptions, options?: GotPaginateOptions): AsyncIterableIterator; + all(url: URLOrGotPaginateOptions, options?: GotPaginateOptions): Promise; } export interface Got extends Record, GotRequestMethod { @@ -200,7 +201,7 @@ const create = (defaults: Defaults): Got => { } // @ts-ignore The missing property is added below - got.paginate = async function * (url: string | GotPaginateOptions, options?: GotPaginateOptions) { + got.paginate = async function * (url: URLOrGotPaginateOptions, options?: GotPaginateOptions) { let normalizedOptions = normalizeArguments(url as URLOrOptions, options as Options, defaults); const pagination = normalizedOptions._pagination!; @@ -247,7 +248,7 @@ const create = (defaults: Defaults): Got => { } }; - got.paginate.all = async (url: string | GotPaginateOptions, options?: GotPaginateOptions) => { + got.paginate.all = async (url: URLOrGotPaginateOptions, options?: GotPaginateOptions) => { const results: T[] = []; for await (const item of got.paginate(url, options)) { From c8169b9ec74d20fc527644846bdc4d16d54ad5ea Mon Sep 17 00:00:00 2001 From: Julian Hundeloh Date: Tue, 24 Mar 2020 15:20:43 +0000 Subject: [PATCH 6/6] test: add types to tests --- test/pagination.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/pagination.ts b/test/pagination.ts index 031294dbe..585d92e64 100644 --- a/test/pagination.ts +++ b/test/pagination.ts @@ -42,7 +42,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]); }); @@ -50,7 +50,7 @@ test('retrieves all elements', 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(''); + const result = await got.extend().paginate.all(''); t.deepEqual(result, [1, 2]); }); @@ -62,7 +62,7 @@ test('pagination options can be extended', withServer, async (t, server, got) => _pagination: { shouldContinue: () => false } - }).paginate.all(''); + }).paginate.all(''); t.deepEqual(result, []); }); @@ -70,7 +70,7 @@ 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 => element !== 2 } @@ -82,7 +82,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] } @@ -94,7 +94,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] } @@ -106,7 +106,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 => { if (response.request.options.path === '/?page=3') { @@ -142,9 +142,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); } @@ -160,9 +160,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); }