From c127f5bc3a3956c8a0ef7942d3a90edaf693cff6 Mon Sep 17 00:00:00 2001 From: Tom Spencer Date: Sat, 2 May 2020 10:28:33 +0100 Subject: [PATCH] Allow response body to be typed for pagination API (#1212) --- source/as-promise/types.ts | 12 ++++++------ source/create.ts | 6 +++--- source/types.ts | 10 +++++----- test/pagination.ts | 16 ++++++++-------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/source/as-promise/types.ts b/source/as-promise/types.ts index 6eb44850b..0ccc54250 100644 --- a/source/as-promise/types.ts +++ b/source/as-promise/types.ts @@ -70,18 +70,18 @@ export interface Hooks extends RequestHooks { afterResponse?: AfterResponseHook[]; } -export interface PaginationOptions { +export interface PaginationOptions { pagination?: { - transform?: (response: Response) => Promise | T[]; + transform?: (response: Response) => Promise | T[]; filter?: (item: T, allItems: T[], currentItems: T[]) => boolean; - paginate?: (response: Response, allItems: T[], currentItems: T[]) => Options | false; + paginate?: (response: Response, allItems: T[], currentItems: T[]) => Options | false; shouldContinue?: (item: T, allItems: T[], currentItems: T[]) => boolean; countLimit?: number; requestLimit?: number; }; } -export interface Options extends RequestOptions, PaginationOptions { +export interface Options extends RequestOptions, PaginationOptions { hooks?: Hooks; responseType?: ResponseType; resolveBodyOnly?: boolean; @@ -97,7 +97,7 @@ export interface NormalizedOptions extends RequestNormalizedOptions { retry: RequiredRetryOptions; isStream: boolean; encoding?: BufferEncoding; - pagination?: Required['pagination']>; + pagination?: Required['pagination']>; } export interface Defaults extends RequestDefaults { @@ -106,7 +106,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 0464219cb..ebeb7fd33 100644 --- a/source/create.ts +++ b/source/create.ts @@ -194,7 +194,7 @@ const create = (defaults: InstanceDefaults): Got => { }); }; - got.paginate = (async function * (url: string | URL, options?: OptionsWithPagination) { + got.paginate = (async function * (url: string | URL, options?: OptionsWithPagination) { let normalizedOptions = normalizeArguments(url, options, defaults.options); normalizedOptions.resolveBodyOnly = false; @@ -247,10 +247,10 @@ const create = (defaults: InstanceDefaults): Got => { } }) as GotPaginate; - got.paginate.all = (async (url: string | URL, options?: OptionsWithPagination) => { + got.paginate.all = (async (url: string | URL, options?: OptionsWithPagination) => { const results: T[] = []; - for await (const item of got.paginate(url, options)) { + for await (const item of got.paginate(url, options)) { results.push(item); } diff --git a/source/types.ts b/source/types.ts index 437d91ec7..22a7891c5 100644 --- a/source/types.ts +++ b/source/types.ts @@ -48,18 +48,18 @@ export type StrictOptions = Except; type ResponseBodyOnly = {resolveBodyOnly: true}; -export type OptionsWithPagination = Merge>; +export type OptionsWithPagination = Merge>; export interface GotPaginate { - (url: string | URL, options?: OptionsWithPagination): AsyncIterableIterator; - all(url: string | URL, options?: OptionsWithPagination): Promise; + (url: string | URL, options?: OptionsWithPagination): AsyncIterableIterator; + all(url: string | URL, options?: OptionsWithPagination): Promise; // A bug. // eslint-disable-next-line @typescript-eslint/adjacent-overload-signatures - (options?: OptionsWithPagination): AsyncIterableIterator; + (options?: OptionsWithPagination): AsyncIterableIterator; // A bug. // eslint-disable-next-line @typescript-eslint/adjacent-overload-signatures - all(options?: OptionsWithPagination): Promise; + all(options?: OptionsWithPagination): Promise; } export interface GotRequestFunction { diff --git a/test/pagination.ts b/test/pagination.ts index dec80f850..0e378a472 100644 --- a/test/pagination.ts +++ b/test/pagination.ts @@ -1,6 +1,6 @@ import {URL} from 'url'; import test from 'ava'; -import got, {Response} from '../source'; +import got from '../source'; import withServer, {withBodyParsingServer} from './helpers/with-server'; import {ExtendedTestServer} from './helpers/types'; @@ -98,9 +98,9 @@ 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] + transform: response => [response.body.length] } }); @@ -110,9 +110,9 @@ 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] + transform: async response => [response.body.length] } }); @@ -124,7 +124,7 @@ test('custom paginate function', withServer, async (t, server, got) => { const result = await got.paginate.all({ pagination: { - paginate: (response: Response) => { + paginate: response => { const url = new URL(response.url); if (url.search === '?page=3') { @@ -146,7 +146,7 @@ test('custom paginate function using allItems', withServer, async (t, server, go const result = await got.paginate.all({ pagination: { - paginate: (_response: Response, allItems: number[]) => { + paginate: (_response, allItems: number[]) => { if (allItems.length === 2) { return false; } @@ -164,7 +164,7 @@ test('custom paginate function using currentItems', withServer, async (t, server const result = await got.paginate.all({ pagination: { - paginate: (_response: Response, _allItems: number[], currentItems: number[]) => { + paginate: (_response, _allItems: number[], currentItems: number[]) => { if (currentItems[0] === 3) { return false; }