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

Fix default pagination handling for empty Link header #1768

Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions source/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,11 +674,12 @@ const defaultInternals: Options['_internals'] = {
return JSON.parse(response.body as string);
},
paginate: ({response}) => {
if (typeof response.headers.link !== 'string') {
const rawLinkHeader = response.headers.link;
if (typeof rawLinkHeader !== 'string' || rawLinkHeader.trim() === '') {
return false;
}

const parsed = parseLinkHeader(response.headers.link);
const parsed = parseLinkHeader(rawLinkHeader);
const next = parsed.find(entry => entry.parameters.rel === 'next' || entry.parameters.rel === '"next"');

if (next) {
Expand Down
12 changes: 12 additions & 0 deletions test/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ test('the link header has no next value', withServer, async (t, server, got) =>
t.deepEqual(received, items);
});

test('the link header is empty', withServer, async (t, server, got) => {
const items = [1];

server.get('/', (_request, response) => {
response.setHeader('link', '');
response.end(JSON.stringify(items));
});

const received = await got.paginate.all<number>('');
t.deepEqual(received, items);
});

test('retrieves all elements', withServer, async (t, server, got) => {
attachHandler(server, 2);

Expand Down