Skip to content

Commit

Permalink
Allow response body to be typed for pagination API (#1212)
Browse files Browse the repository at this point in the history
  • Loading branch information
fiznool committed May 2, 2020
1 parent 278c421 commit c127f5b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
12 changes: 6 additions & 6 deletions source/as-promise/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ export interface Hooks extends RequestHooks {
afterResponse?: AfterResponseHook[];
}

export interface PaginationOptions<T> {
export interface PaginationOptions<T, R> {
pagination?: {
transform?: (response: Response) => Promise<T[]> | T[];
transform?: (response: Response<R>) => Promise<T[]> | T[];
filter?: (item: T, allItems: T[], currentItems: T[]) => boolean;
paginate?: (response: Response, allItems: T[], currentItems: T[]) => Options | false;
paginate?: (response: Response<R>, allItems: T[], currentItems: T[]) => Options | false;
shouldContinue?: (item: T, allItems: T[], currentItems: T[]) => boolean;
countLimit?: number;
requestLimit?: number;
};
}

export interface Options extends RequestOptions, PaginationOptions<unknown> {
export interface Options extends RequestOptions, PaginationOptions<unknown, unknown> {
hooks?: Hooks;
responseType?: ResponseType;
resolveBodyOnly?: boolean;
Expand All @@ -97,7 +97,7 @@ export interface NormalizedOptions extends RequestNormalizedOptions {
retry: RequiredRetryOptions;
isStream: boolean;
encoding?: BufferEncoding;
pagination?: Required<PaginationOptions<unknown>['pagination']>;
pagination?: Required<PaginationOptions<unknown, unknown>['pagination']>;
}

export interface Defaults extends RequestDefaults {
Expand All @@ -106,7 +106,7 @@ export interface Defaults extends RequestDefaults {
resolveBodyOnly: boolean;
retry: RequiredRetryOptions;
isStream: boolean;
pagination?: Required<PaginationOptions<unknown>['pagination']>;
pagination?: Required<PaginationOptions<unknown, unknown>['pagination']>;
}

export class ParseError extends RequestError {
Expand Down
6 changes: 3 additions & 3 deletions source/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ const create = (defaults: InstanceDefaults): Got => {
});
};

got.paginate = (async function * <T>(url: string | URL, options?: OptionsWithPagination<T>) {
got.paginate = (async function * <T, R>(url: string | URL, options?: OptionsWithPagination<T, R>) {
let normalizedOptions = normalizeArguments(url, options, defaults.options);
normalizedOptions.resolveBodyOnly = false;

Expand Down Expand Up @@ -247,10 +247,10 @@ const create = (defaults: InstanceDefaults): Got => {
}
}) as GotPaginate;

got.paginate.all = (async <T>(url: string | URL, options?: OptionsWithPagination<T>) => {
got.paginate.all = (async <T, R>(url: string | URL, options?: OptionsWithPagination<T, R>) => {
const results: T[] = [];

for await (const item of got.paginate<T>(url, options)) {
for await (const item of got.paginate<T, R>(url, options)) {
results.push(item);
}

Expand Down
10 changes: 5 additions & 5 deletions source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ export type StrictOptions = Except<Options, 'isStream' | 'responseType' | 'resol
export type StreamOptions = Merge<Options, {isStream?: true}>;
type ResponseBodyOnly = {resolveBodyOnly: true};

export type OptionsWithPagination<T = unknown> = Merge<Options, PaginationOptions<T>>;
export type OptionsWithPagination<T = unknown, R = unknown> = Merge<Options, PaginationOptions<T, R>>;

export interface GotPaginate {
<T>(url: string | URL, options?: OptionsWithPagination<T>): AsyncIterableIterator<T>;
all<T>(url: string | URL, options?: OptionsWithPagination<T>): Promise<T[]>;
<T, R = unknown>(url: string | URL, options?: OptionsWithPagination<T, R>): AsyncIterableIterator<T>;
all<T, R = unknown>(url: string | URL, options?: OptionsWithPagination<T, R>): Promise<T[]>;

// A bug.
// eslint-disable-next-line @typescript-eslint/adjacent-overload-signatures
<T>(options?: OptionsWithPagination<T>): AsyncIterableIterator<T>;
<T, R = unknown>(options?: OptionsWithPagination<T, R>): AsyncIterableIterator<T>;
// A bug.
// eslint-disable-next-line @typescript-eslint/adjacent-overload-signatures
all<T>(options?: OptionsWithPagination<T>): Promise<T[]>;
all<T, R = unknown>(options?: OptionsWithPagination<T, R>): Promise<T[]>;
}

export interface GotRequestFunction {
Expand Down
16 changes: 8 additions & 8 deletions test/pagination.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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<number>('?page=100', {
const result = await got.paginate.all<number, string>('?page=100', {
pagination: {
transform: (response: Response) => [(response as Response<string>).body.length]
transform: response => [response.body.length]
}
});

Expand All @@ -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<number>('?page=100', {
const result = await got.paginate.all<number, string>('?page=100', {
pagination: {
transform: async (response: Response) => [(response as Response<string>).body.length]
transform: async response => [response.body.length]
}
});

Expand All @@ -124,7 +124,7 @@ test('custom paginate function', withServer, async (t, server, got) => {

const result = await got.paginate.all<number>({
pagination: {
paginate: (response: Response) => {
paginate: response => {
const url = new URL(response.url);

if (url.search === '?page=3') {
Expand All @@ -146,7 +146,7 @@ test('custom paginate function using allItems', withServer, async (t, server, go

const result = await got.paginate.all<number>({
pagination: {
paginate: (_response: Response, allItems: number[]) => {
paginate: (_response, allItems: number[]) => {
if (allItems.length === 2) {
return false;
}
Expand All @@ -164,7 +164,7 @@ test('custom paginate function using currentItems', withServer, async (t, server

const result = await got.paginate.all<number>({
pagination: {
paginate: (_response: Response, _allItems: number[], currentItems: number[]) => {
paginate: (_response, _allItems: number[], currentItems: number[]) => {
if (currentItems[0] === 3) {
return false;
}
Expand Down

0 comments on commit c127f5b

Please sign in to comment.