Skip to content

Commit

Permalink
fix: typings for paginate
Browse files Browse the repository at this point in the history
  • Loading branch information
jaulz authored and Julian Hundeloh committed Mar 3, 2020
1 parent 62f3f38 commit bc51259
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 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,11 @@ 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 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: string | GotPaginateOptions<T>, options?: GotPaginateOptions<T>): AsyncIterableIterator<T>;
all<T>(url: string | GotPaginateOptions<T>, options?: GotPaginateOptions<T>): Promise<T[]>;
}

export interface Got extends Record<HTTPAlias, GotRequestMethod>, GotRequestMethod {
Expand Down Expand Up @@ -198,8 +200,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: string | GotPaginateOptions<T>, options?: GotPaginateOptions<T>) {
let normalizedOptions = normalizeArguments(url as URLOrOptions, options as Options, defaults);

const pagination = normalizedOptions._pagination!;

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

got.paginate.all = async <T>(url: URLOrOptions, options?: Options) => {
got.paginate.all = async <T>(url: string | GotPaginateOptions<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

0 comments on commit bc51259

Please sign in to comment.