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
Changes from 5 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