Comparing version 0.22.0 to 0.23.0
@@ -305,3 +305,3 @@ /// <reference lib="dom"/> | ||
/** | ||
Throw a `HTTPError` for error responses (non-2xx status codes). | ||
Throw an `HTTPError` when, after following redirects, the response has a non-2xx status code. To also throw for redirects instead of following them, set the [`redirect`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters) option to `'manual'`. | ||
@@ -336,2 +336,26 @@ Setting this to `false` may be useful if you are checking for resource availability and are expecting error responses. | ||
onDownloadProgress?: (progress: DownloadProgress, chunk: Uint8Array) => void; | ||
/** | ||
User-defined `fetch` function. | ||
Has to be fully compatible with the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) standard. | ||
Use-cases: | ||
1. Use custom `fetch` implementations like [`isomorphic-unfetch`](https://www.npmjs.com/package/isomorphic-unfetch). | ||
2. Use the `fetch` wrapper function provided by some frameworks that use server-side rendering (SSR). | ||
@default fetch | ||
@example | ||
``` | ||
import ky from 'ky'; | ||
import fetch from 'isomorphic-unfetch'; | ||
(async () => { | ||
const parsed = await ky('https://example.com', { | ||
fetch | ||
}).json(); | ||
})(); | ||
``` | ||
*/ | ||
fetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>; | ||
} | ||
@@ -338,0 +362,0 @@ |
13
index.js
@@ -169,3 +169,3 @@ /*! MIT License © Sindre Sorhus */ | ||
// `Promise.race()` workaround (#91) | ||
const timeout = (request, ms, abortController) => | ||
const timeout = (request, abortController, options) => | ||
new Promise((resolve, reject) => { | ||
@@ -178,6 +178,6 @@ const timeoutID = setTimeout(() => { | ||
reject(new TimeoutError(request)); | ||
}, ms); | ||
}, options.timeout); | ||
/* eslint-disable promise/prefer-await-to-then */ | ||
globals.fetch(request) | ||
options.fetch(request) | ||
.then(resolve) | ||
@@ -244,3 +244,4 @@ .catch(reject) | ||
throwHttpErrors: options.throwHttpErrors !== false, | ||
timeout: typeof options.timeout === 'undefined' ? 10000 : options.timeout | ||
timeout: typeof options.timeout === 'undefined' ? 10000 : options.timeout, | ||
fetch: options.fetch || globals.fetch | ||
}; | ||
@@ -455,6 +456,6 @@ | ||
if (this._options.timeout === false) { | ||
return globals.fetch(this.request.clone()); | ||
return this._options.fetch(this.request.clone()); | ||
} | ||
return timeout(this.request.clone(), this._options.timeout, this.abortController); | ||
return timeout(this.request.clone(), this.abortController, this._options); | ||
} | ||
@@ -461,0 +462,0 @@ |
{ | ||
"name": "ky", | ||
"version": "0.22.0", | ||
"version": "0.23.0", | ||
"description": "Tiny and elegant HTTP client based on the browser Fetch API", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -25,3 +25,3 @@ <div align="center"> | ||
- Method shortcuts (`ky.post()`) | ||
- Treats non-2xx status codes as errors | ||
- Treats non-2xx status codes as errors (after redirects) | ||
- Retries failed requests | ||
@@ -321,3 +321,3 @@ - JSON option | ||
Throw a `HTTPError` for error responses (non-2xx status codes). | ||
Throw an `HTTPError` when, after following redirects, the response has a non-2xx status code. To also throw for redirects instead of following them, set the [`redirect`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters) option to `'manual'`. | ||
@@ -373,2 +373,25 @@ Setting this to `false` may be useful if you are checking for resource availability and are expecting error responses. | ||
##### fetch | ||
Type: `Function`\ | ||
Default: `fetch` | ||
User-defined `fetch` function. | ||
Has to be fully compatible with the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) standard. | ||
Use-cases: | ||
1. Use custom `fetch` implementations like [`isomorphic-unfetch`](https://www.npmjs.com/package/isomorphic-unfetch). | ||
2. Use the `fetch` wrapper function provided by some frameworks that use server-side rendering (SSR). | ||
```js | ||
import ky from 'ky'; | ||
import fetch from 'isomorphic-unfetch'; | ||
(async () => { | ||
const parsed = await ky('https://example.com', { | ||
fetch | ||
}).json(); | ||
})(); | ||
``` | ||
### ky.extend(defaultOptions) | ||
@@ -375,0 +398,0 @@ |
13
umd.js
@@ -175,3 +175,3 @@ (function (global, factory) { | ||
// `Promise.race()` workaround (#91) | ||
const timeout = (request, ms, abortController) => | ||
const timeout = (request, abortController, options) => | ||
new Promise((resolve, reject) => { | ||
@@ -184,6 +184,6 @@ const timeoutID = setTimeout(() => { | ||
reject(new TimeoutError(request)); | ||
}, ms); | ||
}, options.timeout); | ||
/* eslint-disable promise/prefer-await-to-then */ | ||
globals.fetch(request) | ||
options.fetch(request) | ||
.then(resolve) | ||
@@ -250,3 +250,4 @@ .catch(reject) | ||
throwHttpErrors: options.throwHttpErrors !== false, | ||
timeout: typeof options.timeout === 'undefined' ? 10000 : options.timeout | ||
timeout: typeof options.timeout === 'undefined' ? 10000 : options.timeout, | ||
fetch: options.fetch || globals.fetch | ||
}; | ||
@@ -461,6 +462,6 @@ | ||
if (this._options.timeout === false) { | ||
return globals.fetch(this.request.clone()); | ||
return this._options.fetch(this.request.clone()); | ||
} | ||
return timeout(this.request.clone(), this._options.timeout, this.abortController); | ||
return timeout(this.request.clone(), this.abortController, this._options); | ||
} | ||
@@ -467,0 +468,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
66035
1286
635