Skip to content

Commit

Permalink
Avoid parsing JSON twice in _pagination.transform (#1102)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaulz committed Mar 3, 2020
1 parent 62f3f38 commit cf4fdad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
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

0 comments on commit cf4fdad

Please sign in to comment.