Skip to content

Commit

Permalink
fix: Link header parsing when order is first prev and then next link
Browse files Browse the repository at this point in the history
This used to rely on the link header having first the next and then the prev link.
  • Loading branch information
chdorner committed May 2, 2023
1 parent 99cc24b commit 853da99
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/paginator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ describe('Paginator', () => {
});
});

it('parses the next url regardless of order', async () => {
http.request.mockReturnValue({
headers: new Headers({
link: '<https://mastodon.social/api/v1/timelines/home?min_id=109382039876197520>; rel="prev", <https://mastodon.social/api/v1/timelines/home?max_id=109382006402042919>; rel="next"',
}),
});
const paginator = new Paginator(http, '/v1/api/timelines');
await paginator.next();
await paginator.next();
expect(http.request).toBeCalledWith({
requestInit: { method: 'GET' },
searchParams: { max_id: '109382006402042919' },
path: '/api/v1/timelines/home',
});
});

it('returns done when next link does not exist', async () => {
const paginator = new Paginator(http, '/v1/api/timelines');
await paginator.next();
Expand Down
2 changes: 1 addition & 1 deletion src/paginator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class Paginator<Entity, Params = never>
}

const path = link
.match(/<(.+?)>; rel="next"/)?.[1]
.match(/<([^>]+?)>; rel="next"/)?.[1]
.replace(/^https?:\/\/[^/]+/, '');

return path;
Expand Down

0 comments on commit 853da99

Please sign in to comment.