Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass allItems and currentItems to _pagination.paginate() #1100

Merged
merged 28 commits into from Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions readme.md
Expand Up @@ -687,18 +687,18 @@ A function that returns an object representing Got options pointing to the next
###### \_pagination.filter

Type: `Function`\
Default: `(item, allItems) => true`
Default: `(item, allItems, currentItems) => true`

Checks whether the item should be emitted or not.
Checks whether the item should be emitted or not. `currentItems` is an array of the emitted of items of the current response and `allItems` is an error of _all_ the previously emitted items.

###### \_pagination.shouldContinue

Type: `Function`\
Default: `(item, allItems) => true`
Default: `(item, allItems, currentItems) => true`

Checks whether the pagination should continue.
Checks whether the pagination should continue. `currentItems` is an array of the emitted of items of the current response and `allItems` is an error of _all_ the previously emitted items.
jaulz marked this conversation as resolved.
Show resolved Hide resolved

For example, if you need to stop **before** emitting an entry with some flag, you should use `(item, allItems) => !item.flag`. If you want to stop **after** emitting the entry, you should use `(item, allItems) => allItems.some(entry => entry.flag)` instead.
For example, if you need to stop **before** emitting an entry with some flag, you should use `(item, allItems, currentItems) => !item.flag`. If you want to stop **after** emitting the entry, you should use `(item, allItems) => allItems.some(entry => entry.flag)` instead.
jaulz marked this conversation as resolved.
Show resolved Hide resolved

###### \_pagination.countLimit

Expand Down
9 changes: 5 additions & 4 deletions source/create.ts
Expand Up @@ -216,24 +216,25 @@ const create = (defaults: Defaults): Got => {

// eslint-disable-next-line no-await-in-loop
const parsed = await pagination.transform!(result);

const current: T[] = [];
for (const item of parsed) {
if (pagination.filter!(item, all)) {
if (!pagination.shouldContinue!(item, all)) {
if (pagination.filter!(item, all, current)) {
if (!pagination.shouldContinue!(item, all, current)) {
return;
}

yield item;

all.push(item as T);
current.push(item as T);

if (all.length === pagination.countLimit) {
return;
}
}
}

const optionsToMerge = pagination.paginate!(result);
const optionsToMerge = pagination.paginate!(result, all, current);

if (optionsToMerge === false) {
return;
Expand Down
6 changes: 3 additions & 3 deletions source/types.ts
Expand Up @@ -163,9 +163,9 @@ export type DefaultOptions = Merge<
export interface PaginationOptions<T> {
_pagination?: {
transform?: (response: Response) => Promise<T[]> | T[];
filter?: (item: T, allItems: T[]) => boolean;
paginate?: (response: Response) => Options | false;
shouldContinue?: (item: T, allItems: T[]) => boolean;
filter?: (item: T, allItems: T[], currentItems: T[]) => boolean;
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
paginate?: (response: Response, allItems: T[], currentItems: T[]) => Options | false;
shouldContinue?: (item: T, allItems: T[], currentItems: T[]) => boolean;
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
countLimit?: number;
};
}
Expand Down
50 changes: 48 additions & 2 deletions test/pagination.ts
Expand Up @@ -72,7 +72,12 @@ test('filters elements', withServer, async (t, server, got) => {

const result = await got.paginate.all({
_pagination: {
filter: element => element !== 2
filter: (element, allItems, currentItems) => {
t.true(Array.isArray(allItems));
t.true(Array.isArray(currentItems));

return element !== 2
}
}
});

Expand Down Expand Up @@ -121,6 +126,42 @@ test('custom paginate function', withServer, async (t, server, got) => {
t.deepEqual(result, [1, 3]);
});

test('custom paginate function using allItems', withServer, async (t, server, got) => {
attachHandler(server, 3);

const result = await got.paginate.all({
_pagination: {
paginate: (_response, allItems) => {
if (allItems.length === 2) {
return false;
}

return {path: '/?page=3'};
}
}
});

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

test('custom paginate function using currentItems', withServer, async (t, server, got) => {
attachHandler(server, 3);

const result = await got.paginate.all({
_pagination: {
paginate: (_response, _allItems, currentItems) => {
if (currentItems[0] === 3) {
return false;
}

return {path: '/?page=3'};
}
}
});

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

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

Expand All @@ -138,7 +179,12 @@ test('`shouldContinue` works', withServer, async (t, server, got) => {

const options = {
_pagination: {
shouldContinue: () => false
shouldContinue: (_element: any, allItems: any, currentItems: any) => {
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
t.true(Array.isArray(allItems));
t.true(Array.isArray(currentItems));

return false
}
}
};

Expand Down