Comparing version 0.3.0 to 0.4.0
17
index.js
@@ -90,3 +90,2 @@ const isObject = value => value !== null && typeof value === 'object'; | ||
constructor(input, {timeout = 10000, hooks = {beforeRequest: []}, throwHttpErrors = true, json, ...otherOptions}) { | ||
this._input = input; | ||
this._retryCount = 0; | ||
@@ -97,6 +96,16 @@ | ||
credentials: 'same-origin', // TODO: This can be removed when the spec change is implemented in all browsers. Context: https://www.chromestatus.com/feature/4539473312350208 | ||
retry: 3, | ||
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; | ||
@@ -106,3 +115,3 @@ this._hooks = hooks; | ||
const headers = new window.Headers(this._options.headers || {}); | ||
const headers = new self.Headers(this._options.headers || {}); | ||
@@ -170,3 +179,3 @@ if (json) { | ||
return timeout(window.fetch(this._input, this._options), this._timeout); | ||
return timeout(self.fetch(this._input, this._options), this._timeout); | ||
} | ||
@@ -173,0 +182,0 @@ } |
{ | ||
"name": "ky", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Tiny and elegant HTTP client based on the browser Fetch API", | ||
@@ -65,5 +65,6 @@ "license": "MIT", | ||
"require": [ | ||
"esm" | ||
"esm", | ||
"./test/_require" | ||
] | ||
} | ||
} |
@@ -28,3 +28,5 @@ <div align="center"> | ||
- Timeout support | ||
- URL prefix option | ||
- Instances with custom defaults | ||
- Hooks | ||
@@ -62,3 +64,3 @@ | ||
const response = await fetch('https://sindresorhus.com', { | ||
const response = await fetch('https://some-api.com', { | ||
method: 'POST', | ||
@@ -113,2 +115,22 @@ body: JSON.stringify({foo: true}), | ||
#### 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 | ||
@@ -158,2 +180,16 @@ | ||
```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 | ||
@@ -160,0 +196,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17483
278
274