Comparing version 0.2.0 to 0.3.0
51
index.js
@@ -1,3 +0,1 @@ | ||
'use strict'; | ||
const isObject = value => value !== null && typeof value === 'object'; | ||
@@ -82,13 +80,12 @@ | ||
const timeout = (promise, ms) => new Promise((resolve, reject) => { | ||
promise.then(resolve, reject); // eslint-disable-line promise/prefer-await-to-then | ||
const timeout = (promise, ms) => Promise.race([ | ||
promise, | ||
(async () => { | ||
await delay(ms); | ||
reject(new TimeoutError()); | ||
})(); | ||
}); | ||
throw new TimeoutError(); | ||
})() | ||
]); | ||
class Ky { | ||
constructor(input, options) { | ||
constructor(input, {timeout = 10000, hooks = {beforeRequest: []}, throwHttpErrors = true, json, ...otherOptions}) { | ||
this._input = input; | ||
@@ -101,15 +98,14 @@ this._retryCount = 0; | ||
retry: 3, | ||
timeout: 10000, | ||
...options | ||
...otherOptions | ||
}; | ||
this._timeout = this._options.timeout; | ||
delete this._options.timeout; | ||
this._timeout = timeout; | ||
this._hooks = hooks; | ||
this._throwHttpErrors = throwHttpErrors; | ||
const headers = new window.Headers(this._options.headers || {}); | ||
if (this._options.json) { | ||
if (json) { | ||
headers.set('content-type', 'application/json'); | ||
this._options.body = JSON.stringify(this._options.json); | ||
delete this._options.json; | ||
this._options.body = JSON.stringify(json); | ||
} | ||
@@ -158,3 +154,5 @@ | ||
throw error; | ||
if (this._throwHttpErrors) { | ||
throw error; | ||
} | ||
} | ||
@@ -166,3 +164,8 @@ }; | ||
_fetch() { | ||
async _fetch() { | ||
for (const hook of this._hooks.beforeRequest) { | ||
// eslint-disable-next-line no-await-in-loop | ||
await hook(this._options); | ||
} | ||
return timeout(window.fetch(this._input, this._options), this._timeout); | ||
@@ -179,8 +182,12 @@ } | ||
ky.extend = defaults => createInstance(defaults); | ||
return ky; | ||
}; | ||
module.exports = createInstance(); | ||
module.exports.extend = defaults => createInstance(defaults); | ||
module.exports.HTTPError = HTTPError; | ||
module.exports.TimeoutError = TimeoutError; | ||
export default createInstance(); | ||
export { | ||
HTTPError, | ||
TimeoutError | ||
}; |
{ | ||
"name": "ky", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Tiny and elegant HTTP client based on the browser Fetch API", | ||
@@ -16,6 +16,7 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "xo && nyc ava" | ||
"test": "xo && nyc ava && tsd-check" | ||
}, | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
@@ -47,6 +48,9 @@ "keywords": [ | ||
"body": "^5.1.0", | ||
"codecov": "^3.0.4", | ||
"create-test-server": "2.1.1", | ||
"delay": "^4.0.0", | ||
"esm": "^3.0.82", | ||
"node-fetch": "^2.2.0", | ||
"nyc": "^13.0.1", | ||
"tsd-check": "^0.2.1", | ||
"xo": "^0.23.0" | ||
@@ -58,3 +62,10 @@ }, | ||
] | ||
}, | ||
"ava": { | ||
"babel": false, | ||
"compileEnhancements": false, | ||
"require": [ | ||
"esm" | ||
] | ||
} | ||
} |
<div align="center"> | ||
<br> | ||
<div> | ||
<img width="600" src="media/logo.svg" alt="ky"> | ||
<img width="600" height="600" src="media/logo.svg" alt="ky"> | ||
</div> | ||
@@ -70,3 +70,3 @@ <br> | ||
if (!response.ok) { | ||
throw new HTTPError(`Fetch error:`, response.statusText); | ||
throw new HTTPError('Fetch error:', response.statusText); | ||
} | ||
@@ -129,2 +129,25 @@ | ||
#### hooks | ||
Type: `Object<string, Function[]>`<br> | ||
Default: `{beforeRequest: []}` | ||
Hooks allow modifications during the request lifecycle. Hook functions may be async and are run serially. | ||
##### hooks.beforeRequest | ||
Type: `Function[]`<br> | ||
Default: `[]` | ||
This hook enables you to modify the request right before it is sent. Ky will make no further changes to the request after this. The hook function receives the normalized options as the first argument. You could, for example, modify `options.headers` here. | ||
### throwHttpErrors | ||
Type: `boolean`<br> | ||
Default: `true` | ||
Throw a `HTTPError` for error responses (non-2xx status codes). | ||
Setting this to `false` may be useful if you are checking for resource availability and are expecting error responses. | ||
### ky.extend(defaultOptions) | ||
@@ -138,7 +161,7 @@ | ||
### ky.HTTPError | ||
### HTTPError | ||
Exposed for `instanceof` checks. The error has a `response` property with the [`Response` object](https://developer.mozilla.org/en-US/docs/Web/API/Response). | ||
### ky.TimeoutError | ||
### TimeoutError | ||
@@ -148,2 +171,53 @@ The error thrown when the request times out. | ||
## Tips | ||
### Cancelation | ||
Fetch (and hence Ky) has built-in support for request cancelation through the [`AbortController` API](https://developer.mozilla.org/en-US/docs/Web/API/AbortController). [Read more.](https://developers.google.com/web/updates/2017/09/abortable-fetch) | ||
Example: | ||
```js | ||
import ky from 'ky'; | ||
const controller = new AbortController(); | ||
const {signal} = controller; | ||
setTimeout(() => controller.abort(), 5000); | ||
(async () => { | ||
try { | ||
console.log(await ky(url, {signal}).text()); | ||
} catch (error) { | ||
if (error.name === 'AbortError') { | ||
console.log('Fetch aborted'); | ||
} else { | ||
console.error('Fetch error:', error); | ||
} | ||
} | ||
})(); | ||
``` | ||
## FAQ | ||
#### How is it different from [`got`](https://github.com/sindresorhus/got) | ||
See my answer [here](https://twitter.com/sindresorhus/status/1037406558945042432). Got is maintained by the same people as Ky. | ||
#### How is it different from [`axios`](https://github.com/axios/axios)? | ||
See my answer [here](https://twitter.com/sindresorhus/status/1037763588826398720). | ||
#### How is it different from [`r2`](https://github.com/mikeal/r2)? | ||
See my answer in [#10](https://github.com/sindresorhus/ky/issues/10). | ||
#### What does `ky` mean? | ||
It's just a random short npm package name I managed to get. It does, however, have a meaning in Japanese: | ||
> A form of text-able slang, KY is an abbreviation for 空気読めない (kuuki yomenai), which literally translates into “cannot read the air.” It's a phrase applied to someone who misses the implied meaning. | ||
## Browser support | ||
@@ -159,4 +233,10 @@ | ||
## Maintainers | ||
- [Sindre Sorhus](https://github.com/sindresorhus) | ||
- [Szymon Marczak](https://github.com/szmarczak) | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) | ||
MIT |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16031
5
270
238
0
10