Skip to content

Commit

Permalink
Fix merging pagination options
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Feb 8, 2020
1 parent f9a719c commit 1f363b9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
12 changes: 6 additions & 6 deletions source/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ const create = (defaults: Defaults): Got => {
}

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

const pagination = normalizedOptions._pagination!;

Expand Down Expand Up @@ -240,16 +240,16 @@ const create = (defaults: Defaults): Got => {
}

if (optionsToMerge !== undefined) {
normalizedOptions = normalizeArguments(normalizedOptions, optionsToMerge) as NormalizedOptions & PaginationOptions<T>;
normalizedOptions = normalizeArguments(normalizedOptions, optionsToMerge);
}
}
};

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

for await (const item of got.paginate<T>(url, options)) {
results.push(item);
for await (const item of got.paginate<unknown>(url, options)) {
results.push(item as T);
}

return results;
Expand Down
17 changes: 5 additions & 12 deletions source/normalize-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,29 +200,22 @@ export const preNormalizeArguments = (options: Options, defaults?: NormalizedOpt

// `options._pagination`
if (is.object(options._pagination)) {
if (defaults && !(Reflect.has(options, '_pagination') && is.undefined(options._pagination))) {
options._pagination = {
...defaults.pagination,
...options._pagination
};
}

const pagination = options._pagination!;
const {_pagination: pagination} = options;

if (!is.function_(pagination.transform)) {
throw new Error('`options._pagination.transform` must be implemented');
throw new TypeError('`options._pagination.transform` must be implemented');
}

if (!is.function_(pagination.shouldContinue)) {
throw new Error('`options._pagination.shouldContinue` must be implemented');
throw new TypeError('`options._pagination.shouldContinue` must be implemented');
}

if (!is.function_(pagination.filter)) {
throw new Error('`options._pagination.filter` must be implemented');
throw new TypeError('`options._pagination.filter` must be implemented');
}

if (!is.function_(pagination.paginate)) {
throw new Error('`options._pagination.paginate` must be implemented');
throw new TypeError('`options._pagination.paginate` must be implemented');
}
}

Expand Down
14 changes: 13 additions & 1 deletion test/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ test('points to defaults when extending Got without custom `_pagination`', withS
t.deepEqual(result, [1, 2]);
});

test('pagination options can be extended', withServer, async (t, server, got) => {
attachHandler(server, 2);

const result = await got.extend({
_pagination: {
shouldContinue: () => false
}
}).paginate.all('');

t.deepEqual(result, []);
});

test('filters elements', withServer, async (t, server, got) => {
attachHandler(server, 3);

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

test('throws if no `pagination` option', async t => {
const iterator = got.extend({
_pagination: undefined
_pagination: false as any
}).paginate('', {
prefixUrl: 'https://example.com'
});
Expand Down

0 comments on commit 1f363b9

Please sign in to comment.