Skip to content

Commit

Permalink
Fix default pagination handling for empty Link header (#1768)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
joshua-leyshon-canva and sindresorhus authored Jul 1, 2021
1 parent 7524d90 commit 1e1e506
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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

0 comments on commit 1e1e506

Please sign in to comment.