diff --git a/source/create.ts b/source/create.ts index ae4b46eb7..43acfa04a 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,12 @@ export interface GotRequestMethod { (url: string | Merge, options?: Merge): ProxyStream; } +export type GotPaginateOptions = Except> & PaginationOptions; +export type URLOrGotPaginateOptions = string | GotPaginateOptions; + export interface GotPaginate { - (url: URLOrOptions & PaginationOptions, options?: Options & PaginationOptions): AsyncIterableIterator; - all(url: URLOrOptions & PaginationOptions, options?: Options & PaginationOptions): Promise; + (url: URLOrGotPaginateOptions, options?: GotPaginateOptions): AsyncIterableIterator; + all(url: URLOrGotPaginateOptions, options?: GotPaginateOptions): Promise; } export interface Got extends Record, GotRequestMethod { @@ -198,8 +201,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: URLOrGotPaginateOptions, options?: GotPaginateOptions) { + let normalizedOptions = normalizeArguments(url as URLOrOptions, options as Options, defaults); const pagination = normalizedOptions._pagination!; @@ -247,11 +250,11 @@ const create = (defaults: Defaults): Got => { } }; - got.paginate.all = async (url: URLOrOptions, options?: Options) => { + got.paginate.all = async (url: URLOrGotPaginateOptions, 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; diff --git a/test/pagination.ts b/test/pagination.ts index 3446e0fa2..71fc7c4e1 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]); }); @@ -60,7 +60,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]); }); @@ -72,7 +72,7 @@ test('pagination options can be extended', withServer, async (t, server, got) => _pagination: { shouldContinue: () => false } - }).paginate.all(''); + }).paginate.all(''); t.deepEqual(result, []); }); @@ -80,7 +80,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, allItems, currentItems) => { t.true(Array.isArray(allItems)); @@ -97,7 +97,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] } @@ -109,7 +109,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] } @@ -121,7 +121,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') { @@ -198,9 +198,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); } @@ -216,9 +216,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); }