diff --git a/source/create.ts b/source/create.ts index e59a39ee6..db69a3093 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!; @@ -245,11 +248,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 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); }