Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix got.paginate(…) typings (#1099)
  • Loading branch information
jaulz committed Mar 24, 2020
1 parent 62b3388 commit 0b798ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
19 changes: 11 additions & 8 deletions 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';
Expand Down Expand Up @@ -61,9 +61,12 @@ export interface GotRequestMethod {
<T>(url: string | Merge<Options, {isStream: true}>, options?: Merge<Options, {isStream: true}>): ProxyStream<T>;
}

export type GotPaginateOptions<T> = Except<Options, keyof PaginationOptions<unknown>> & PaginationOptions<T>;
export type URLOrGotPaginateOptions<T> = string | GotPaginateOptions<T>;

export interface GotPaginate {
<T>(url: URLOrOptions & PaginationOptions<T>, options?: Options & PaginationOptions<T>): AsyncIterableIterator<T>;
all<T>(url: URLOrOptions & PaginationOptions<T>, options?: Options & PaginationOptions<T>): Promise<T[]>;
<T>(url: URLOrGotPaginateOptions<T>, options?: GotPaginateOptions<T>): AsyncIterableIterator<T>;
all<T>(url: URLOrGotPaginateOptions<T>, options?: GotPaginateOptions<T>): Promise<T[]>;
}

export interface Got extends Record<HTTPAlias, GotRequestMethod>, GotRequestMethod {
Expand Down Expand Up @@ -198,8 +201,8 @@ const create = (defaults: Defaults): Got => {
}

// @ts-ignore The missing property is added below
got.paginate = async function * <T>(url: URLOrOptions, options?: Options) {
let normalizedOptions = normalizeArguments(url, options, defaults);
got.paginate = async function * <T>(url: URLOrGotPaginateOptions<T>, options?: GotPaginateOptions<T>) {
let normalizedOptions = normalizeArguments(url as URLOrOptions, options as Options, defaults);

const pagination = normalizedOptions._pagination!;

Expand Down Expand Up @@ -247,11 +250,11 @@ const create = (defaults: Defaults): Got => {
}
};

got.paginate.all = async <T>(url: URLOrOptions, options?: Options) => {
got.paginate.all = async <T>(url: URLOrGotPaginateOptions<T>, options?: GotPaginateOptions<T>) => {
const results: T[] = [];

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

return results;
Expand Down
22 changes: 11 additions & 11 deletions test/pagination.ts
Expand Up @@ -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<number>('');

t.deepEqual(result, [1, 2]);
});
Expand All @@ -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<number>('');

t.deepEqual(result, [1, 2]);
});
Expand All @@ -72,15 +72,15 @@ test('pagination options can be extended', withServer, async (t, server, got) =>
_pagination: {
shouldContinue: () => false
}
}).paginate.all('');
}).paginate.all<number>('');

t.deepEqual(result, []);
});

test('filters elements', withServer, async (t, server, got) => {
attachHandler(server, 3);

const result = await got.paginate.all({
const result = await got.paginate.all<number>({
_pagination: {
filter: (element, allItems, currentItems) => {
t.true(Array.isArray(allItems));
Expand All @@ -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<number>('?page=100', {
_pagination: {
transform: (response: Response) => [(response as Response<string>).body.length]
}
Expand All @@ -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<number>('?page=100', {
_pagination: {
transform: async (response: Response) => [(response as Response<string>).body.length]
}
Expand All @@ -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<number>({
_pagination: {
paginate: response => {
if (response.request.options.path === '/?page=3') {
Expand Down Expand Up @@ -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<number>(options)) {
results.push(item);
}

Expand All @@ -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<number>(options)) {
results.push(item);
}

Expand Down

0 comments on commit 0b798ea

Please sign in to comment.