Skip to content

Commit 1f363b9

Browse files
committed
Fix merging pagination options
1 parent f9a719c commit 1f363b9

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

source/create.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ const create = (defaults: Defaults): Got => {
198198
}
199199

200200
// @ts-ignore The missing property is added below
201-
got.paginate = async function * <T>(url: URLOrOptions & PaginationOptions<T>, options?: Options & PaginationOptions<T>) {
202-
let normalizedOptions = normalizeArguments(url, options, defaults) as NormalizedOptions & PaginationOptions<T>;
201+
got.paginate = async function * <T>(url: URLOrOptions, options?: Options) {
202+
let normalizedOptions = normalizeArguments(url, options, defaults);
203203

204204
const pagination = normalizedOptions._pagination!;
205205

@@ -240,16 +240,16 @@ const create = (defaults: Defaults): Got => {
240240
}
241241

242242
if (optionsToMerge !== undefined) {
243-
normalizedOptions = normalizeArguments(normalizedOptions, optionsToMerge) as NormalizedOptions & PaginationOptions<T>;
243+
normalizedOptions = normalizeArguments(normalizedOptions, optionsToMerge);
244244
}
245245
}
246246
};
247247

248-
got.paginate.all = async <T>(url: URLOrOptions & PaginationOptions<T>, options?: Options & PaginationOptions<T>) => {
248+
got.paginate.all = async <T>(url: URLOrOptions, options?: Options) => {
249249
const results: T[] = [];
250250

251-
for await (const item of got.paginate<T>(url, options)) {
252-
results.push(item);
251+
for await (const item of got.paginate<unknown>(url, options)) {
252+
results.push(item as T);
253253
}
254254

255255
return results;

source/normalize-arguments.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -200,29 +200,22 @@ export const preNormalizeArguments = (options: Options, defaults?: NormalizedOpt
200200

201201
// `options._pagination`
202202
if (is.object(options._pagination)) {
203-
if (defaults && !(Reflect.has(options, '_pagination') && is.undefined(options._pagination))) {
204-
options._pagination = {
205-
...defaults.pagination,
206-
...options._pagination
207-
};
208-
}
209-
210-
const pagination = options._pagination!;
203+
const {_pagination: pagination} = options;
211204

212205
if (!is.function_(pagination.transform)) {
213-
throw new Error('`options._pagination.transform` must be implemented');
206+
throw new TypeError('`options._pagination.transform` must be implemented');
214207
}
215208

216209
if (!is.function_(pagination.shouldContinue)) {
217-
throw new Error('`options._pagination.shouldContinue` must be implemented');
210+
throw new TypeError('`options._pagination.shouldContinue` must be implemented');
218211
}
219212

220213
if (!is.function_(pagination.filter)) {
221-
throw new Error('`options._pagination.filter` must be implemented');
214+
throw new TypeError('`options._pagination.filter` must be implemented');
222215
}
223216

224217
if (!is.function_(pagination.paginate)) {
225-
throw new Error('`options._pagination.paginate` must be implemented');
218+
throw new TypeError('`options._pagination.paginate` must be implemented');
226219
}
227220
}
228221

test/pagination.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ test('points to defaults when extending Got without custom `_pagination`', withS
5555
t.deepEqual(result, [1, 2]);
5656
});
5757

58+
test('pagination options can be extended', withServer, async (t, server, got) => {
59+
attachHandler(server, 2);
60+
61+
const result = await got.extend({
62+
_pagination: {
63+
shouldContinue: () => false
64+
}
65+
}).paginate.all('');
66+
67+
t.deepEqual(result, []);
68+
});
69+
5870
test('filters elements', withServer, async (t, server, got) => {
5971
attachHandler(server, 3);
6072

@@ -159,7 +171,7 @@ test('`countLimit` works', withServer, async (t, server, got) => {
159171

160172
test('throws if no `pagination` option', async t => {
161173
const iterator = got.extend({
162-
_pagination: undefined
174+
_pagination: false as any
163175
}).paginate('', {
164176
prefixUrl: 'https://example.com'
165177
});

0 commit comments

Comments
 (0)