diff --git a/readme.md b/readme.md index f6d03edb5..429f46b1a 100644 --- a/readme.md +++ b/readme.md @@ -724,7 +724,7 @@ The function takes three arguments: - `allItems` - An array of the emitted items. - `currentItems` - Items from the current response. -It should return an object representing Got options pointing to the next page. If there are no more pages, `false` should be returned. +It should return an object representing Got options pointing to the next page. The options are merged automatically with the previous request, therefore the options returned `pagination.paginate(...)` must reflect changes only. If there are no more pages, `false` should be returned. For example, if you want to stop when the response contains less items than expected, you can use something like this: diff --git a/test/pagination.ts b/test/pagination.ts index 4aad683d4..2f0a05de4 100644 --- a/test/pagination.ts +++ b/test/pagination.ts @@ -1,5 +1,6 @@ import {URL} from 'url'; import test from 'ava'; +import getStream = require('get-stream'); import got, {Response} from '../source'; import withServer, {withBodyParsingServer} from './helpers/with-server'; import {ExtendedTestServer} from './helpers/types'; @@ -333,7 +334,7 @@ test('allowGetBody sends json payload with .paginate()', withBodyParsingServer, t.deepEqual(results, [1, 2, 3]); }); -test('`hooks` are not duplicated', withBodyParsingServer, async (t, server, got) => { +test('`hooks` are not duplicated', withServer, async (t, server, got) => { let page = 1; server.get('/', (_request, response) => { response.end(JSON.stringify([page++])); @@ -370,11 +371,11 @@ test('`hooks` are not duplicated', withBodyParsingServer, async (t, server, got) t.deepEqual(result, [1, 2, 3]); }); -test.failing('allowGetBody sends correct json payload with .paginate()', withBodyParsingServer, async (t, server, got) => { +test('allowGetBody sends correct json payload with .paginate()', withServer, async (t, server, got) => { let page = 1; - server.get('/', (request, response) => { + server.get('/', async (request, response) => { try { - JSON.parse(request.body); + JSON.parse(await getStream(request)); } catch { response.statusCode = 422; } @@ -392,9 +393,11 @@ test.failing('allowGetBody sends correct json payload with .paginate()', withBod return false; // Stop after page 3 } - const {json, ...otherOptions}: any = response.request.options; + const {json} = response.request.options; - return {json: {...json, page}, ...otherOptions}; + return { + json: {...json, page} + }; } } });