Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix got.paginate(...) typings #1099

Merged
merged 6 commits into from Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -245,11 +248,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,15 +42,15 @@ 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]);
});

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 @@ -62,15 +62,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 => element !== 2
}
Expand All @@ -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<number>('?page=100', {
_pagination: {
transform: (response: Response) => [(response as Response<string>).body.length]
}
Expand All @@ -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<number>('?page=100', {
_pagination: {
transform: async (response: Response) => [(response as Response<string>).body.length]
}
Expand All @@ -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<number>({
_pagination: {
paginate: response => {
if (response.request.options.path === '/?page=3') {
Expand Down Expand Up @@ -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<number>(options)) {
results.push(item);
}

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

Expand Down