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 16 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
14 changes: 8 additions & 6 deletions readme.md
Expand Up @@ -680,25 +680,27 @@ A function that transform [`Response`](#response) into an array of items. This i
###### \_pagination.paginate

Type: `Function`\
Default: [`Link` header logic](source/index.ts)
Default: `(response, allItems, currentItems) => getLinkFromHeaders(response)` (find details [here](source/index.ts))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This change is unnecessary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I restored it but changed the sentence afterwards to make clear how the signature of the function should be. This should make it easier for users without diving into the code first.


A function that returns an object representing Got options pointing to the next page. If there are no more pages, `false` should be returned.

For example, if you want to stop when the response contains less items that expected, you should use `(response, allItems, currentItems) => currentItems.length < LIMIT ? false : { url: getNextLink(response, allItems, currentItems) }`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

the LIMIT should be a response.request.options.searchParams.entriesPerPage

Copy link
Collaborator

Choose a reason for hiding this comment

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

that expected -> than expected

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, it's updated.


###### \_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. `allItems` is an array of the all emitted items, while `currentItems` is an array of the current response 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. `allItems` is an array of the all emitted items, while `currentItems` is an array of the current response items.

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, currentItems) => allItems.some(entry => entry.flag)` instead.

###### \_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