Skip to content

Commit eda69ff

Browse files
committed
Require url to be an instance of URL when paginating
Fixes #1818
1 parent be3462d commit eda69ff

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

documentation/4-pagination.md

+4
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ It should return an object representing Got options pointing to the next page. I
129129
The options are merged automatically with the previous request.\
130130
Therefore the options returned by `pagination.paginate(…)` must reflect changes only.
131131

132+
**Note:**
133+
> - The `url` option (if set) accepts **only** a [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) instance.\
134+
> This prevents `prefixUrl` ambiguity. In order to use a relative URL string, merge it via `new URL(relativeUrl, response.url)`.
135+
132136
#### `filter`
133137

134138
**Type: `Function`**\

source/core/options.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ const defaultInternals: Options['_internals'] = {
692692

693693
if (next) {
694694
return {
695-
url: new URL(next.reference, response.requestUrl),
695+
url: new URL(next.reference, response.url),
696696
};
697697
}
698698

source/create.ts

+2
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ const create = (defaults: InstanceDefaults): Got => {
201201
} else {
202202
normalizedOptions.merge(optionsToMerge);
203203

204+
assert.any([is.urlInstance, is.undefined], optionsToMerge.url);
205+
204206
if (optionsToMerge.url !== undefined) {
205207
normalizedOptions.prefixUrl = '';
206208
normalizedOptions.url = optionsToMerge.url;

test/pagination.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ test('next url in json response', withServer, async (t, server, got) => {
575575
}
576576

577577
return {
578-
url: next,
578+
url: new URL(next),
579579
prefixUrl: '',
580580
searchParams: undefined,
581581
};
@@ -728,6 +728,20 @@ test('retrieves all elements - relative url', withServer, async (t, server, got)
728728
t.deepEqual(result, [1, 2]);
729729
});
730730

731+
test('throws if url is not an instance of URL', withServer, async (t, server, got) => {
732+
attachHandler(server, 2);
733+
734+
await t.throwsAsync(got.paginate.all<number>('', {
735+
pagination: {
736+
paginate: () => ({
737+
url: 'not an instance of URL',
738+
}),
739+
},
740+
}), {
741+
instanceOf: TypeError,
742+
});
743+
});
744+
731745
test('throws when transform does not return an array', withServer, async (t, server) => {
732746
server.get('/', (_request, response) => {
733747
response.end(JSON.stringify(''));

0 commit comments

Comments
 (0)