From eda69ff924a621e499d31cbc590993a32ddb48d3 Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Wed, 4 Aug 2021 22:58:45 +0200 Subject: [PATCH] Require `url` to be an instance of `URL` when paginating Fixes #1818 --- documentation/4-pagination.md | 4 ++++ source/core/options.ts | 2 +- source/create.ts | 2 ++ test/pagination.ts | 16 +++++++++++++++- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/documentation/4-pagination.md b/documentation/4-pagination.md index 32459bf32..f2a153703 100644 --- a/documentation/4-pagination.md +++ b/documentation/4-pagination.md @@ -129,6 +129,10 @@ It should return an object representing Got options pointing to the next page. I The options are merged automatically with the previous request.\ Therefore the options returned by `pagination.paginate(…)` must reflect changes only. +**Note:** +> - The `url` option (if set) accepts **only** a [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) instance.\ +> This prevents `prefixUrl` ambiguity. In order to use a relative URL string, merge it via `new URL(relativeUrl, response.url)`. + #### `filter` **Type: `Function`**\ diff --git a/source/core/options.ts b/source/core/options.ts index 3f24f30d7..a10ec22e5 100644 --- a/source/core/options.ts +++ b/source/core/options.ts @@ -692,7 +692,7 @@ const defaultInternals: Options['_internals'] = { if (next) { return { - url: new URL(next.reference, response.requestUrl), + url: new URL(next.reference, response.url), }; } diff --git a/source/create.ts b/source/create.ts index fadb50bfb..398d947ff 100644 --- a/source/create.ts +++ b/source/create.ts @@ -201,6 +201,8 @@ const create = (defaults: InstanceDefaults): Got => { } else { normalizedOptions.merge(optionsToMerge); + assert.any([is.urlInstance, is.undefined], optionsToMerge.url); + if (optionsToMerge.url !== undefined) { normalizedOptions.prefixUrl = ''; normalizedOptions.url = optionsToMerge.url; diff --git a/test/pagination.ts b/test/pagination.ts index 9fd4e6fdd..a627100a7 100644 --- a/test/pagination.ts +++ b/test/pagination.ts @@ -575,7 +575,7 @@ test('next url in json response', withServer, async (t, server, got) => { } return { - url: next, + url: new URL(next), prefixUrl: '', searchParams: undefined, }; @@ -728,6 +728,20 @@ test('retrieves all elements - relative url', withServer, async (t, server, got) t.deepEqual(result, [1, 2]); }); +test('throws if url is not an instance of URL', withServer, async (t, server, got) => { + attachHandler(server, 2); + + await t.throwsAsync(got.paginate.all('', { + pagination: { + paginate: () => ({ + url: 'not an instance of URL', + }), + }, + }), { + instanceOf: TypeError, + }); +}); + test('throws when transform does not return an array', withServer, async (t, server) => { server.get('/', (_request, response) => { response.end(JSON.stringify(''));