Skip to content

Commit 1e1e506

Browse files
Fix default pagination handling for empty Link header (#1768)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 7524d90 commit 1e1e506

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

source/core/options.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -674,11 +674,12 @@ const defaultInternals: Options['_internals'] = {
674674
return JSON.parse(response.body as string);
675675
},
676676
paginate: ({response}) => {
677-
if (typeof response.headers.link !== 'string') {
677+
const rawLinkHeader = response.headers.link;
678+
if (typeof rawLinkHeader !== 'string' || rawLinkHeader.trim() === '') {
678679
return false;
679680
}
680681

681-
const parsed = parseLinkHeader(response.headers.link);
682+
const parsed = parseLinkHeader(rawLinkHeader);
682683
const next = parsed.find(entry => entry.parameters.rel === 'next' || entry.parameters.rel === '"next"');
683684

684685
if (next) {

test/pagination.ts

+12
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ test('the link header has no next value', withServer, async (t, server, got) =>
4343
t.deepEqual(received, items);
4444
});
4545

46+
test('the link header is empty', withServer, async (t, server, got) => {
47+
const items = [1];
48+
49+
server.get('/', (_request, response) => {
50+
response.setHeader('link', '');
51+
response.end(JSON.stringify(items));
52+
});
53+
54+
const received = await got.paginate.all<number>('');
55+
t.deepEqual(received, items);
56+
});
57+
4658
test('retrieves all elements', withServer, async (t, server, got) => {
4759
attachHandler(server, 2);
4860

0 commit comments

Comments
 (0)