-
-
Notifications
You must be signed in to change notification settings - Fork 436
Add baseUrl option, rename prefix, and allow leading slashes in input #606
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
41ffd18
e5c8e32
e626944
913d104
351020a
9335149
ad5fa3e
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 |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| import {HTTPError} from '../errors/HTTPError.js'; | ||
| import {TimeoutError} from '../errors/TimeoutError.js'; | ||
| import type {Hooks} from '../types/hooks.js'; | ||
| import type {Input, InternalOptions, NormalizedOptions, Options, SearchParamsInit} from '../types/options.js'; | ||
| import type { | ||
| Input, InternalOptions, NormalizedOptions, Options, SearchParamsInit, | ||
| } from '../types/options.js'; | ||
| import {type ResponsePromise} from '../types/ResponsePromise.js'; | ||
| import {deepMerge, mergeHeaders} from '../utils/merge.js'; | ||
| import {normalizeRequestMethod, normalizeRetryOptions} from '../utils/normalize.js'; | ||
|
|
@@ -137,9 +139,9 @@ export class Ky { | |
| options.hooks, | ||
| ), | ||
| method: normalizeRequestMethod(options.method ?? (this._input as Request).method), | ||
| // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing | ||
| prefixUrl: String(options.prefixUrl || ''), | ||
| retry: normalizeRetryOptions(options.retry), | ||
| // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing | ||
| startPath: String(options.startPath || ''), | ||
| throwHttpErrors: options.throwHttpErrors !== false, | ||
| timeout: options.timeout ?? 10_000, | ||
| fetch: options.fetch ?? globalThis.fetch.bind(globalThis), | ||
|
|
@@ -149,16 +151,22 @@ export class Ky { | |
| throw new TypeError('`input` must be a string, URL, or Request'); | ||
| } | ||
|
|
||
| if (this._options.prefixUrl && typeof this._input === 'string') { | ||
| if (this._input.startsWith('/')) { | ||
| throw new Error('`input` must not begin with a slash when using `prefixUrl`'); | ||
| } | ||
| if (typeof this._input === 'string') { | ||
| if (this._options.startPath) { | ||
| if (!this._options.startPath.endsWith('/')) { | ||
| this._options.startPath += '/'; | ||
| } | ||
|
|
||
| if (!this._options.prefixUrl.endsWith('/')) { | ||
| this._options.prefixUrl += '/'; | ||
| if (this._input.startsWith('/')) { | ||
| this._input = this._input.slice(1); | ||
| } | ||
|
|
||
| this._input = this._options.startPath + this._input; | ||
|
||
| } | ||
|
|
||
| this._input = this._options.prefixUrl + this._input; | ||
| if (this._options.baseUrl) { | ||
| this._input = new URL(this._input, (new Request(options.baseUrl)).url); | ||
| } | ||
| } | ||
|
|
||
| if (supportsAbortController) { | ||
|
|
||
| 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. | ||
|
||
|
|
||
| @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.
We need even clearer docs about the difference between
baseUrlandprefixand when to use each. Maybe with some real-world example use-cases. Otherwise, it's going to be confusing for users and that ends up being a support burden for us.