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

Avoid parsing JSON twice in _pagination.transform #1102

Merged
merged 3 commits into from Mar 3, 2020
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
4 changes: 4 additions & 0 deletions source/index.ts
Expand Up @@ -69,6 +69,10 @@ const defaults: Defaults = {
context: {},
_pagination: {
transform: (response: Response) => {
if (response.request.options.responseType === 'json') {
return response.body;
}

return JSON.parse(response.body as string);
},
paginate: response => {
Expand Down
10 changes: 10 additions & 0 deletions test/pagination.ts
Expand Up @@ -47,6 +47,16 @@ test('retrieves all elements', withServer, async (t, server, got) => {
t.deepEqual(result, [1, 2]);
});

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

const result = await got.extend({
responseType: 'json'
}).paginate.all('');

t.deepEqual(result, [1, 2]);
});

test('points to defaults when extending Got without custom `_pagination`', withServer, async (t, server, got) => {
attachHandler(server, 2);

Expand Down