-
-
Notifications
You must be signed in to change notification settings - Fork 366
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
Add baseUrl option, rename prefixUrl, and allow leading slashes in input #606
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,23 +97,23 @@ export type KyOptions = { | |
Useful when used with [`ky.extend()`](#kyextenddefaultoptions) to create niche-specific Ky-instances. | ||
|
||
Notes: | ||
- After `prefixUrl` and `input` are joined, the result is resolved against the [base URL](https://developer.mozilla.org/en-US/docs/Web/API/Node/baseURI) of the page (if any). | ||
- Leading slashes in `input` are disallowed when using this option to enforce consistency and avoid confusion about how the `input` URL is handled, given that `input` will not follow the normal URL resolution rules when `prefixUrl` is being used, which changes the meaning of a leading slash. | ||
- After `startPath` and `input` are joined, the result is resolved against the [base URL](https://developer.mozilla.org/en-US/docs/Web/API/Node/baseURI) of the page (if any). | ||
- Leading slashes in `input` are disallowed when using this option to enforce consistency and avoid confusion about how the `input` URL is handled, given that `input` will not follow the normal URL resolution rules when `startPath` is being used, which changes the meaning of a leading slash. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
From what I can tell from the changed code, this is no longer the case? I personally prefer it that way though and think that we should keep that behaviour There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed. As mentioned in the TODOs section, I haven't gotten around to updating the docs yet, other than a simple find & replace. But thank you for the reminder. 🙂 |
||
|
||
@example | ||
``` | ||
import ky from 'ky'; | ||
|
||
// On https://example.com | ||
|
||
const response = await ky('unicorn', {prefixUrl: '/api'}); | ||
const response = await ky('unicorn', {startPath: '/api'}); | ||
//=> 'https://example.com/api/unicorn' | ||
|
||
const response = await ky('unicorn', {prefixUrl: 'https://cats.com'}); | ||
const response = await ky('unicorn', {startPath: 'https://cats.com'}); | ||
//=> 'https://cats.com/unicorn' | ||
``` | ||
*/ | ||
prefixUrl?: URL | string; | ||
startPath?: URL | string; | ||
|
||
/** | ||
An object representing `limit`, `methods`, `statusCodes` and `maxRetryAfter` fields for maximum retry count, allowed methods, allowed status codes and maximum [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) time. | ||
|
@@ -265,12 +265,12 @@ export interface Options extends KyOptions, Omit<RequestInit, 'headers'> { // es | |
|
||
export type InternalOptions = Required< | ||
Omit<Options, 'hooks' | 'retry'>, | ||
'fetch' | 'prefixUrl' | 'timeout' | ||
'fetch' | 'startPath' | 'timeout' | ||
> & { | ||
headers: Required<Headers>; | ||
hooks: Required<Hooks>; | ||
retry: Required<RetryOptions>; | ||
prefixUrl: string; | ||
startPath: string; | ||
}; | ||
|
||
/** | ||
|
@@ -283,7 +283,7 @@ export interface NormalizedOptions extends RequestInit { // eslint-disable-line | |
|
||
// Extended from custom `KyOptions`, but ensured to be set (not optional). | ||
retry: RetryOptions; | ||
prefixUrl: string; | ||
startPath: string; | ||
onDownloadProgress: Options['onDownloadProgress']; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import test from 'ava'; | ||
import ky from '../source/index.js'; | ||
import {createHttpTestServer} from './helpers/create-http-test-server.js'; | ||
|
||
test('baseUrl option', async t => { | ||
const server = await createHttpTestServer(); | ||
server.get('/', (_request, response) => { | ||
response.end('/'); | ||
}); | ||
server.get('/foo', (_request, response) => { | ||
response.end('/foo'); | ||
}); | ||
server.get('/bar', (_request, response) => { | ||
response.end('/bar'); | ||
}); | ||
server.get('/foo/bar', (_request, response) => { | ||
response.end('/foo/bar'); | ||
}); | ||
|
||
t.is( | ||
// @ts-expect-error {baseUrl: boolean} isn't officially supported | ||
await ky(`${server.url}/foo/bar`, {baseUrl: false}).text(), | ||
'/foo/bar', | ||
); | ||
t.is(await ky(`${server.url}/foo/bar`, {baseUrl: ''}).text(), '/foo/bar'); | ||
t.is(await ky(new URL(`${server.url}/foo/bar`), {baseUrl: ''}).text(), '/foo/bar'); | ||
t.is(await ky('foo/bar', {baseUrl: server.url}).text(), '/foo/bar'); | ||
t.is(await ky('foo/bar', {baseUrl: new URL(server.url)}).text(), '/foo/bar'); | ||
t.is(await ky('/bar', {baseUrl: `${server.url}/foo/`}).text(), '/bar'); | ||
t.is(await ky('/bar', {baseUrl: `${server.url}/foo`}).text(), '/bar'); | ||
t.is(await ky('bar', {baseUrl: `${server.url}/foo/`}).text(), '/foo/bar'); | ||
t.is(await ky('bar', {baseUrl: `${server.url}/foo`}).text(), '/bar'); | ||
t.is(await ky('bar', {baseUrl: new URL(`${server.url}/foo`)}).text(), '/bar'); | ||
t.is(await ky('', {baseUrl: server.url}).text(), '/'); | ||
t.is(await ky('', {baseUrl: `${server.url}/`}).text(), '/'); | ||
t.is(await ky('', {baseUrl: new URL(server.url)}).text(), '/'); | ||
|
||
await server.close(); | ||
}); |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import test from 'ava'; | ||
import ky from '../source/index.js'; | ||
import {createHttpTestServer} from './helpers/create-http-test-server.js'; | ||
|
||
test('startPath option', async t => { | ||
const server = await createHttpTestServer(); | ||
server.get('/', (_request, response) => { | ||
response.end('/'); | ||
}); | ||
server.get('/foo', (_request, response) => { | ||
response.end('/foo'); | ||
}); | ||
server.get('/bar', (_request, response) => { | ||
response.end('/bar'); | ||
}); | ||
server.get('/foo/bar', (_request, response) => { | ||
response.end('/foo/bar'); | ||
}); | ||
|
||
t.is( | ||
// @ts-expect-error {startPath: boolean} isn't officially supported | ||
await ky(`${server.url}/foo/bar`, {startPath: false}).text(), | ||
'/foo/bar', | ||
); | ||
t.is(await ky(`${server.url}/foo/bar`, {startPath: ''}).text(), '/foo/bar'); | ||
t.is(await ky(new URL(`${server.url}/foo/bar`), {startPath: ''}).text(), '/foo/bar'); | ||
t.is(await ky('foo/bar', {startPath: server.url}).text(), '/foo/bar'); | ||
t.is(await ky('foo/bar', {startPath: new URL(server.url)}).text(), '/foo/bar'); | ||
t.is(await ky('/bar', {startPath: `${server.url}/foo/`}).text(), '/foo/bar'); | ||
t.is(await ky('/bar', {startPath: `${server.url}/foo`}).text(), '/foo/bar'); | ||
t.is(await ky('bar', {startPath: `${server.url}/foo/`}).text(), '/foo/bar'); | ||
t.is(await ky('bar', {startPath: `${server.url}/foo`}).text(), '/foo/bar'); | ||
t.is(await ky('bar', {startPath: new URL(`${server.url}/foo`)}).text(), '/foo/bar'); | ||
t.is(await ky('', {startPath: server.url}).text(), '/'); | ||
t.is(await ky('', {startPath: `${server.url}/`}).text(), '/'); | ||
t.is(await ky('', {startPath: new URL(server.url)}).text(), '/'); | ||
|
||
await server.close(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if
_input
starts with a protocol (or is a valid URL?) then we should ignorestartPath
in order to avoid an URL likehttp://foo.com/http://bar.com
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That kind of "double URL" construct is useful for reverse proxies. It should be supported.
Also, you would be surprised how hard URL parsing is. Especially for a library like Ky that supports non-browsers.