Skip to content

Commit

Permalink
Add prefixUrl option (#11)
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
sholladay authored and sindresorhus committed Sep 29, 2018
1 parent 91de80f commit 66fb6c3
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ const timeout = (promise, ms) => Promise.race([

class Ky {
constructor(input, {timeout = 10000, hooks = {beforeRequest: []}, throwHttpErrors = true, json, ...otherOptions}) {
this._input = input;
this._retryCount = 0;

this._options = {
Expand All @@ -96,7 +95,17 @@ class Ky {
retry: 2,
...otherOptions
};
this._options.prefixUrl = String(this._options.prefixUrl || '');
this._input = String(input || '');

if (this._options.prefixUrl && this._input.startsWith('/')) {
throw new Error('`input` must not begin with a slash when using `prefixUrl`');
}
if (this._options.prefixUrl && !this._options.prefixUrl.endsWith('/')) {
this._options.prefixUrl += '/';
}

this._input = this._options.prefixUrl + this._input;
this._timeout = timeout;
this._hooks = hooks;
this._throwHttpErrors = throwHttpErrors;
Expand Down
35 changes: 35 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Ky targets [modern browsers](#browser-support). For older browsers, you will nee
- Retries failed requests
- JSON option
- Timeout support
- URL prefix option
- Instances with custom defaults
- Hooks

Expand Down Expand Up @@ -109,6 +110,26 @@ Shortcut for sending JSON. Use this instead of the `body` option. Accepts a plai

Sets `options.method` to the method name and makes a request.

#### prefixUrl

Type: `string` [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL)

When specified, `prefixUrl` will be prepended to `input`. The prefix can be any valid URL, either relative or absolute. A trailing slash `/` is optional, one will be added automatically, if needed, when joining `prefixUrl` and `input`. The `input` argument cannot start with a `/` when using this option.

Useful when used with [`ky.extend()`](#kyextenddefaultoptions) to create niche-specific Ky-instances.

```js
// On https://example.com

(async () => {
await ky('unicorn', {prefixUrl: '/api'});
//=> 'https://example.com/api/unicorn'

await ky('unicorn', {prefixUrl: 'https://cats.com'});
//=> 'https://cats.com/unicorn'
})();
```

#### retry

Type: `number`<br>
Expand Down Expand Up @@ -153,6 +174,20 @@ Setting this to `false` may be useful if you are checking for resource availabil

Create a new `ky` instance with some defaults overridden with your own.

```js
// On https://my-site.com

const api = ky.extend({prefixUrl: 'https://example.com/api'});

(async () => {
await api.get('/users/123');
//=> 'https://example.com/api/users/123'

await api.get('/status', {prefixUrl: ''});
//=> 'https://my-site.com/status'
})();
```

#### defaultOptions

Type: `Object`
Expand Down
2 changes: 2 additions & 0 deletions test/_require.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {URL} from 'url';
import fetch, {Headers} from 'node-fetch';

global.window = {
fetch,
Headers
};
global.URL = URL;
34 changes: 34 additions & 0 deletions test/prefix-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import test from 'ava';
import createTestServer from 'create-test-server';
import ky from '..';

test('prefixUrl option', async t => {
const server = await createTestServer();
server.get('/', (request, response) => {
if (request.query.page === '/https://cat.com/') {
response.end('meow');
return;
}
response.end('zebra');
});
server.get('/api/unicorn', (request, response) => {
response.end('rainbow');
});

t.is(await ky(`${server.url}/api/unicorn`, {prefixUrl: false}).text(), 'rainbow');
t.is(await ky(`${server.url}/api/unicorn`, {prefixUrl: ''}).text(), 'rainbow');
t.is(await ky('api/unicorn', {prefixUrl: server.url}).text(), 'rainbow');
t.is(await ky('unicorn', {prefixUrl: `${server.url}/api`}).text(), 'rainbow');
t.is(await ky('unicorn', {prefixUrl: `${server.url}/api/`}).text(), 'rainbow');
t.is(await ky('', {prefixUrl: server.url}).text(), 'zebra');
t.is(await ky('', {prefixUrl: `${server.url}/`}).text(), 'zebra');
t.is(await ky('https://cat.com/', {prefixUrl: new URL(`${server.url}/?page=`)}).text(), 'meow');
t.is(await ky(new URL('https://cat.com'), {prefixUrl: `${server.url}/?page=`}).text(), 'meow');
t.is(await ky(new URL('https://cat.com'), {prefixUrl: new URL(`${server.url}/?page=`)}).text(), 'meow');

t.throws(() => {
ky('/unicorn', {prefixUrl: `${server.url}/api`});
}, '`input` must not begin with a slash when using `prefixUrl`');

await server.close();
});

0 comments on commit 66fb6c3

Please sign in to comment.