Skip to content

Commit 5480b31

Browse files
committedMay 12, 2020
Make got.paginate() an alias for got.paginate.each()
Fixes #1195
1 parent 6f84051 commit 5480b31

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed
 

‎source/create.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ const create = (defaults: InstanceDefaults): Got => {
110110
return result;
111111
}));
112112

113+
// Got interface
113114
const got: Got = ((url: string | URL, options?: Options): GotReturn => {
114115
let iteration = 0;
115116
const iterateHandlers = (newOptions: NormalizedOptions): GotReturn => {
@@ -190,7 +191,8 @@ const create = (defaults: InstanceDefaults): Got => {
190191
});
191192
};
192193

193-
got.paginate = (async function * <T, R>(url: string | URL, options?: OptionsWithPagination<T, R>) {
194+
// Pagination
195+
const paginateEach = (async function * <T, R>(url: string | URL, options?: OptionsWithPagination<T, R>) {
194196
let normalizedOptions = normalizeArguments(url, options, defaults.options);
195197
normalizedOptions.resolveBodyOnly = false;
196198

@@ -247,6 +249,10 @@ const create = (defaults: InstanceDefaults): Got => {
247249

248250
numberOfRequests++;
249251
}
252+
});
253+
254+
got.paginate = (<T, R>(url: string | URL, options?: OptionsWithPagination<T, R>) => {
255+
return paginateEach(url, options);
250256
}) as GotPaginate;
251257

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

268+
// For those who like very descriptive names
269+
got.paginate.each = paginateEach as GotPaginate['each'];
270+
271+
// Stream API
262272
got.stream = ((url: string | URL, options?: StreamOptions) => got(url, {...options, isStream: true})) as GotStream;
263273

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

‎source/types.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,12 @@ export type OptionsWithPagination<T = unknown, R = unknown> = Merge<Options, Pag
5252

5353
export interface GotPaginate {
5454
<T, R = unknown>(url: string | URL, options?: OptionsWithPagination<T, R>): AsyncIterableIterator<T>;
55-
all<T, R = unknown>(url: string | URL, options?: OptionsWithPagination<T, R>): Promise<T[]>;
56-
57-
// A bug.
58-
// eslint-disable-next-line @typescript-eslint/adjacent-overload-signatures
5955
<T, R = unknown>(options?: OptionsWithPagination<T, R>): AsyncIterableIterator<T>;
60-
// A bug.
61-
// eslint-disable-next-line @typescript-eslint/adjacent-overload-signatures
56+
57+
each<T, R = unknown>(url: string | URL, options?: OptionsWithPagination<T, R>): AsyncIterableIterator<T>;
58+
each<T, R = unknown>(options?: OptionsWithPagination<T, R>): AsyncIterableIterator<T>;
59+
60+
all<T, R = unknown>(url: string | URL, options?: OptionsWithPagination<T, R>): Promise<T[]>;
6261
all<T, R = unknown>(options?: OptionsWithPagination<T, R>): Promise<T[]>;
6362
}
6463

‎test/pagination.ts

+12
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,18 @@ test('iterator works', withServer, async (t, server, got) => {
190190
t.deepEqual(results, [1, 2, 3, 4, 5]);
191191
});
192192

193+
test('iterator works #2', withServer, async (t, server, got) => {
194+
attachHandler(server, 5);
195+
196+
const results: number[] = [];
197+
198+
for await (const item of got.paginate.each<number>('')) {
199+
results.push(item);
200+
}
201+
202+
t.deepEqual(results, [1, 2, 3, 4, 5]);
203+
});
204+
193205
test('`shouldContinue` works', withServer, async (t, server, got) => {
194206
attachHandler(server, 2);
195207

1 commit comments

Comments
 (1)

szmarczak commented on May 14, 2020

@szmarczak
CollaboratorAuthor

Whoops, I forgot to add docs...

Please sign in to comment.