Skip to content

Commit

Permalink
Make got.paginate() an alias for got.paginate.each()
Browse files Browse the repository at this point in the history
Fixes #1195
  • Loading branch information
szmarczak committed May 12, 2020
1 parent 6f84051 commit 5480b31
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
13 changes: 12 additions & 1 deletion source/create.ts
Expand Up @@ -110,6 +110,7 @@ const create = (defaults: InstanceDefaults): Got => {
return result;
}));

// Got interface
const got: Got = ((url: string | URL, options?: Options): GotReturn => {
let iteration = 0;
const iterateHandlers = (newOptions: NormalizedOptions): GotReturn => {
Expand Down Expand Up @@ -190,7 +191,8 @@ const create = (defaults: InstanceDefaults): Got => {
});
};

got.paginate = (async function * <T, R>(url: string | URL, options?: OptionsWithPagination<T, R>) {
// Pagination
const paginateEach = (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,6 +249,10 @@ const create = (defaults: InstanceDefaults): Got => {

numberOfRequests++;
}
});

got.paginate = (<T, R>(url: string | URL, options?: OptionsWithPagination<T, R>) => {
return paginateEach(url, options);
}) as GotPaginate;

got.paginate.all = (async <T, R>(url: string | URL, options?: OptionsWithPagination<T, R>) => {
Expand All @@ -259,8 +265,13 @@ const create = (defaults: InstanceDefaults): Got => {
return results;
}) as GotPaginate['all'];

// For those who like very descriptive names
got.paginate.each = paginateEach as GotPaginate['each'];

// Stream API
got.stream = ((url: string | URL, options?: StreamOptions) => got(url, {...options, isStream: true})) as GotStream;

// Shortcuts
for (const method of aliases) {
got[method] = ((url: string | URL, options?: Options): GotReturn => got(url, {...options, method})) as GotRequestFunction;

Expand Down
11 changes: 5 additions & 6 deletions source/types.ts
Expand Up @@ -52,13 +52,12 @@ export type OptionsWithPagination<T = unknown, R = unknown> = Merge<Options, Pag

export interface GotPaginate {
<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, R = unknown>(options?: OptionsWithPagination<T, R>): AsyncIterableIterator<T>;
// A bug.
// eslint-disable-next-line @typescript-eslint/adjacent-overload-signatures

each<T, R = unknown>(url: string | URL, options?: OptionsWithPagination<T, R>): AsyncIterableIterator<T>;
each<T, R = unknown>(options?: OptionsWithPagination<T, R>): AsyncIterableIterator<T>;

all<T, R = unknown>(url: string | URL, options?: OptionsWithPagination<T, R>): Promise<T[]>;
all<T, R = unknown>(options?: OptionsWithPagination<T, R>): Promise<T[]>;
}

Expand Down
12 changes: 12 additions & 0 deletions test/pagination.ts
Expand Up @@ -190,6 +190,18 @@ test('iterator works', withServer, async (t, server, got) => {
t.deepEqual(results, [1, 2, 3, 4, 5]);
});

test('iterator works #2', withServer, async (t, server, got) => {
attachHandler(server, 5);

const results: number[] = [];

for await (const item of got.paginate.each<number>('')) {
results.push(item);
}

t.deepEqual(results, [1, 2, 3, 4, 5]);
});

test('`shouldContinue` works', withServer, async (t, server, got) => {
attachHandler(server, 2);

Expand Down

1 comment on commit 5480b31

@szmarczak
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, I forgot to add docs...

Please sign in to comment.