Comparing version 0.5.2 to 0.6.0
@@ -1,2 +0,2 @@ | ||
export type BeforeRequestHook = (options: Object) => void; | ||
export type BeforeRequestHook = (options: Options) => void; | ||
@@ -13,3 +13,3 @@ export type AfterResponseHook = (response: Response) => Response | void; | ||
*/ | ||
beforeRequest: BeforeRequestHook[]; | ||
beforeRequest?: BeforeRequestHook[]; | ||
@@ -23,3 +23,3 @@ /** | ||
*/ | ||
afterResponse: AfterResponseHook[]; | ||
afterResponse?: AfterResponseHook[]; | ||
} | ||
@@ -26,0 +26,0 @@ |
63
index.js
@@ -1,18 +0,22 @@ | ||
// Polyfill for `globalThis` | ||
const _globalThis = (() => { | ||
if (typeof self !== 'undefined') { | ||
return self; | ||
} | ||
/*! MIT License © Sindre Sorhus */ | ||
const getGlobal = property => { | ||
/* istanbul ignore next */ | ||
if (typeof window !== 'undefined') { | ||
return window; | ||
if (typeof self !== 'undefined' && self && property in self) { | ||
return self[property]; | ||
} | ||
/* istanbul ignore next */ | ||
if (typeof global !== 'undefined') { | ||
return global; | ||
if (typeof window !== 'undefined' && window && property in window) { | ||
return window[property]; | ||
} | ||
})(); | ||
return global[property]; | ||
}; | ||
const document = getGlobal('document'); | ||
const Headers = getGlobal('Headers'); | ||
const Response = getGlobal('Response'); | ||
const fetch = getGlobal('fetch'); | ||
const isObject = value => value !== null && typeof value === 'object'; | ||
@@ -141,11 +145,15 @@ | ||
const url = new _globalThis.URL(this._options.prefixUrl + this._input, document.baseURI); | ||
if (typeof searchParams === 'string' || searchParams instanceof _globalThis.URLSearchParams) { | ||
url.search = searchParams; | ||
} else if (searchParams && Object.values(searchParams).every(param => typeof param === 'number' || typeof param === 'string')) { | ||
url.search = new _globalThis.URLSearchParams(searchParams).toString(); | ||
} else if (searchParams) { | ||
throw new Error('The `searchParams` option must be either a string, `URLSearchParams` instance or an object with string and number values'); | ||
this._input = this._options.prefixUrl + this._input; | ||
if (searchParams) { | ||
const url = new URL(this._input, document && document.baseURI); | ||
if (typeof searchParams === 'string' || (URLSearchParams && searchParams instanceof URLSearchParams)) { | ||
url.search = searchParams; | ||
} else if (Object.values(searchParams).every(param => typeof param === 'number' || typeof param === 'string')) { | ||
url.search = new URLSearchParams(searchParams).toString(); | ||
} else { | ||
throw new Error('The `searchParams` option must be either a string, `URLSearchParams` instance or an object with string and number values'); | ||
} | ||
this._input = url.toString(); | ||
} | ||
this._input = url.toString(); | ||
@@ -159,3 +167,3 @@ this._timeout = timeout; | ||
const headers = new _globalThis.Headers(this._options.headers || {}); | ||
const headers = new Headers(this._options.headers || {}); | ||
@@ -172,3 +180,4 @@ if (json) { | ||
for (const type of responseTypes) { | ||
this._response[type] = this._retry(async () => { | ||
const isRetriableMethod = retryMethods.has(this._options.method.toLowerCase()); | ||
const fn = async () => { | ||
if (this._retryCount > 0) { | ||
@@ -184,3 +193,3 @@ this._response = this._fetch(); | ||
if (modifiedResponse instanceof _globalThis.Response) { | ||
if (modifiedResponse instanceof Response) { | ||
response = modifiedResponse; | ||
@@ -190,3 +199,3 @@ } | ||
if (!response.ok) { | ||
if (!response.ok && (isRetriableMethod || this._throwHttpErrors)) { | ||
throw new HTTPError(response); | ||
@@ -196,3 +205,5 @@ } | ||
return response.clone()[type](); | ||
}); | ||
}; | ||
this._response[type] = isRetriableMethod ? this._retry(fn) : fn; | ||
} | ||
@@ -237,6 +248,2 @@ | ||
_retry(fn) { | ||
if (!retryMethods.has(this._options.method.toLowerCase())) { | ||
return fn; | ||
} | ||
const retry = async () => { | ||
@@ -267,3 +274,3 @@ try { | ||
return timeout(_globalThis.fetch(this._input, this._options), this._timeout); | ||
return timeout(fetch(this._input, this._options), this._timeout); | ||
} | ||
@@ -270,0 +277,0 @@ } |
{ | ||
"name": "ky", | ||
"version": "0.5.2", | ||
"version": "0.6.0", | ||
"description": "Tiny and elegant HTTP client based on the browser Fetch API", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
@@ -16,0 +16,0 @@ "scripts": { |
@@ -39,2 +39,14 @@ <div align="center"> | ||
###### Download | ||
- [Normal](https://cdn.jsdelivr.net/npm/ky/index.js) | ||
- ~~[Minified](https://cdn.jsdelivr.net/npm/ky/index.min.js)~~<br><sup>(Blocked by [jsdelivr/jsdelivr#18043](https://github.com/jsdelivr/jsdelivr/issues/18043))</sup> | ||
###### CDN | ||
- [jsdelivr](https://www.jsdelivr.com/package/npm/ky) | ||
- [unpkg](https://unpkg.com/ky) | ||
--- | ||
<a href="https://www.patreon.com/sindresorhus"> | ||
@@ -300,3 +312,21 @@ <img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160"> | ||
#### How do I use this without a bundler like Webpack? | ||
Upload the [`index.js`](index.js) file in this repo somewhere, for example, to your website server, or use a CDN version. Then import the file. | ||
```html | ||
<script type="module"> | ||
// Replace the version number with the latest version | ||
import ky from 'https://cdn.jsdelivr.net/npm/ky@0.5.2/index.js'; | ||
(async () => { | ||
const json = await ky('https://jsonplaceholder.typicode.com/todos/1').json(); | ||
console.log(json.title); | ||
//=> 'delectus aut autem | ||
})(); | ||
</script> | ||
``` | ||
## Browser support | ||
@@ -316,2 +346,3 @@ | ||
- [Szymon Marczak](https://github.com/szmarczak) | ||
- [Seth Holladay](https://github.com/sholladay) | ||
@@ -318,0 +349,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
Network access
Supply chain riskThis module accesses the network.
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
23680
375
350
1