Comparing version 0.9.0 to 0.9.1
227
index.d.ts
@@ -0,1 +1,3 @@ | ||
/// <reference lib="dom"/> | ||
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>; | ||
@@ -9,23 +11,23 @@ | ||
export type BeforeRequestHook = (options: Options) => void; | ||
export type BeforeRequestHook = (options: Options) => void | Promise<void>; | ||
export type AfterResponseHook = (response: Response) => Response | void; | ||
export type AfterResponseHook = (response: Response) => Response | void | Promise<Response | void>; | ||
export interface Hooks { | ||
/** | ||
* Before the request is sent. | ||
* | ||
* 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. | ||
* | ||
* @default [] | ||
*/ | ||
Before the request is sent. | ||
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. | ||
@default [] | ||
*/ | ||
beforeRequest?: BeforeRequestHook[]; | ||
/** | ||
* After the response is received. | ||
* | ||
* This hook enables you to read and optionally modify the response. The return value of the hook function will be used by Ky as the response object if it's an instance of [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response). | ||
* | ||
* @default [] | ||
*/ | ||
After the response is received. | ||
This hook enables you to read and optionally modify the response. The return value of the hook function will be used by Ky as the response object if it's an instance of [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response). | ||
@default [] | ||
*/ | ||
afterResponse?: AfterResponseHook[]; | ||
@@ -35,13 +37,13 @@ } | ||
/** | ||
* Options are the same as fetch, with some exceptions. | ||
*/ | ||
Options are the same as fetch, with some exceptions. | ||
*/ | ||
export interface Options extends RequestInit { | ||
/** | ||
* Shortcut for sending JSON. Use this instead of the `body` option. | ||
*/ | ||
Shortcut for sending JSON. Use this instead of the `body` option. | ||
*/ | ||
json?: JSONStringifyable; | ||
/** | ||
* Search parameters to include in the request URL. | ||
* Setting this will override all existing search parameters in the input URL. | ||
Search parameters to include in the request URL. | ||
Setting this will override all existing search parameters in the input URL. | ||
*/ | ||
@@ -51,4 +53,4 @@ searchParams?: string | {[key: string]: string | number} | URLSearchParams; | ||
/** | ||
* Prepends the input with the specified prefix. | ||
* The prefix can be any valid URL, either relative or absolute. | ||
Prepends the input URL with the specified prefix. | ||
The prefix can be any valid URL, either relative or absolute. | ||
*/ | ||
@@ -58,25 +60,25 @@ prefixUrl?: URL | string; | ||
/** | ||
* Numer of times to retry failed requests. | ||
* | ||
* @default 2 | ||
*/ | ||
Numer of times to retry failed requests. | ||
@default 2 | ||
*/ | ||
retry?: number; | ||
/** | ||
* Timeout in milliseconds for getting a response. | ||
* | ||
* @default 10000 | ||
*/ | ||
Timeout in milliseconds for getting a response. | ||
@default 10000 | ||
*/ | ||
timeout?: number; | ||
/** | ||
* Hooks allow modifications during the request lifecycle. Hook functions may be async and are run serially. | ||
*/ | ||
Hooks allow modifications during the request lifecycle. Hook functions may be async and are run serially. | ||
*/ | ||
hooks?: Hooks; | ||
/** | ||
* Throw a `HTTPError` for error responses (non-2xx status codes). | ||
* | ||
* @default true | ||
*/ | ||
Throw a `HTTPError` for error responses (non-2xx status codes). | ||
@default true | ||
*/ | ||
throwHttpErrors?: boolean; | ||
@@ -94,4 +96,4 @@ } | ||
/** | ||
* Returns a `Response` object with `Body` methods added for convenience. | ||
*/ | ||
Returns a `Response` object with `Body` methods added for convenience. | ||
*/ | ||
export interface ResponsePromise extends Promise<Response> { | ||
@@ -107,16 +109,22 @@ arrayBuffer(): Promise<ArrayBuffer>; | ||
/** | ||
* Get the response body as JSON. | ||
* | ||
* @example | ||
* | ||
* const parsed = await ky(…).json(); | ||
* | ||
* @example | ||
* | ||
* interface Result { | ||
* value: number; | ||
* } | ||
Get the response body as JSON. | ||
* const result = await ky(…).json<Result>(); | ||
*/ | ||
@example | ||
``` | ||
import ky from 'ky'; | ||
const parsed = await ky(…).json(); | ||
``` | ||
@example | ||
``` | ||
import ky from 'ky'; | ||
interface Result { | ||
value: number; | ||
} | ||
const result = await ky(…).json<Result>(); | ||
``` | ||
*/ | ||
json<T = JSONValue>(): Promise<T>; | ||
@@ -128,5 +136,6 @@ | ||
/** | ||
* The error has a response property with the `Response` object. | ||
*/ | ||
The error has a response property with the `Response` object. | ||
*/ | ||
export class HTTPError extends Error { | ||
constructor(response: Response); | ||
response: Response; | ||
@@ -136,68 +145,82 @@ } | ||
/** | ||
* The error thrown when the request times out. | ||
*/ | ||
export class TimeoutError extends Error {} | ||
The error thrown when the request times out. | ||
*/ | ||
export class TimeoutError extends Error { | ||
constructor(); | ||
} | ||
export interface Ky { | ||
/** | ||
* Fetches the `input` URL. | ||
* | ||
* @param input - `Request` object, `URL` object, or URL string. | ||
* @returns Promise with `Body` method added. | ||
*/ | ||
(input: Request | URL | string, options?: OptionsWithoutBody | OptionsWithBody): ResponsePromise; | ||
Fetch the given `url`. | ||
@param url - `Request` object, `URL` object, or URL string. | ||
@returns A promise with `Body` method added. | ||
@example | ||
``` | ||
import ky from 'ky'; | ||
(async () => { | ||
const parsed = await ky('https://example.com', {json: {foo: true}}).json(); | ||
console.log(parsed); | ||
//=> `{data: '🦄'}` | ||
})(); | ||
``` | ||
*/ | ||
(url: Request | URL | string, options?: OptionsWithoutBody | OptionsWithBody): ResponsePromise; | ||
/** | ||
* Fetches the `input` URL with the option `{method: 'get'}`. | ||
* | ||
* @param input - `Request` object, `URL` object, or URL string. | ||
* @returns Promise with `Body` method added. | ||
*/ | ||
get(input: Request | URL | string, options?: Omit<Options, 'body'>): ResponsePromise; | ||
Fetch the given `url` using the option `{method: 'get'}`. | ||
@param url - `Request` object, `URL` object, or URL string. | ||
@returns A promise with `Body` methods added. | ||
*/ | ||
get(url: Request | URL | string, options?: Omit<Options, 'body'>): ResponsePromise; | ||
/** | ||
* Fetches the `input` URL with the option `{method: 'post'}`. | ||
* | ||
* @param input - `Request` object, `URL` object, or URL string. | ||
* @returns Promise with `Body` method added. | ||
*/ | ||
post(input: Request | URL | string, options?: Options): ResponsePromise; | ||
Fetch the given `url` using the option `{method: 'post'}`. | ||
@param url - `Request` object, `URL` object, or URL string. | ||
@returns A promise with `Body` methods added. | ||
*/ | ||
post(url: Request | URL | string, options?: Options): ResponsePromise; | ||
/** | ||
* Fetches the `input` URL with the option `{method: 'put'}`. | ||
* | ||
* @param input - `Request` object, `URL` object, or URL string. | ||
* @returns Promise with `Body` method added. | ||
*/ | ||
put(input: Request | URL | string, options?: Options): ResponsePromise; | ||
Fetch the given `url` using the option `{method: 'put'}`. | ||
@param url - `Request` object, `URL` object, or URL string. | ||
@returns A promise with `Body` methods added. | ||
*/ | ||
put(url: Request | URL | string, options?: Options): ResponsePromise; | ||
/** | ||
* Fetches the `input` URL with the option `{method: 'patch'}`. | ||
* | ||
* @param input - `Request` object, `URL` object, or URL string. | ||
* @returns Promise with `Body` method added. | ||
*/ | ||
patch(input: Request | URL | string, options?: Options): ResponsePromise; | ||
Fetch the given `url` using the option `{method: 'patch'}`. | ||
@param url - `Request` object, `URL` object, or URL string. | ||
@returns A promise with `Body` methods added. | ||
*/ | ||
patch(url: Request | URL | string, options?: Options): ResponsePromise; | ||
/** | ||
* Fetches the `input` URL with the option `{method: 'head'}`. | ||
* | ||
* @param input - `Request` object, `URL` object, or URL string. | ||
* @returns Promise with `Body` method added. | ||
*/ | ||
head(input: Request | URL | string, options?: Omit<Options, 'body'>): ResponsePromise; | ||
Fetch the given `url` using the option `{method: 'head'}`. | ||
@param url - `Request` object, `URL` object, or URL string. | ||
@returns A promise with `Body` methods added. | ||
*/ | ||
head(url: Request | URL | string, options?: Omit<Options, 'body'>): ResponsePromise; | ||
/** | ||
* Fetches the `input` URL with the option `{method: 'delete'}`. | ||
* | ||
* @param input - `Request` object, `URL` object, or URL string. | ||
* @returns Promise with `Body` method added. | ||
*/ | ||
delete(input: Request | URL | string, options?: Options): ResponsePromise; | ||
Fetch the given `url` using the option `{method: 'delete'}`. | ||
@param url - `Request` object, `URL` object, or URL string. | ||
@returns A promise with `Body` methods added. | ||
*/ | ||
delete(url: Request | URL | string, options?: Options): ResponsePromise; | ||
/** | ||
* Create a new Ky instance with some defaults overridden with your own. | ||
* | ||
* @returns New Ky instance | ||
*/ | ||
Create a new Ky instance with some defaults overridden with your own. | ||
@returns A new Ky instance. | ||
*/ | ||
extend(defaultOptions: Options): Ky; | ||
@@ -204,0 +227,0 @@ } |
22
index.js
@@ -116,15 +116,13 @@ /*! MIT License © Sindre Sorhus */ | ||
const timeout = (promise, ms, abortController) => Promise.race([ | ||
promise, | ||
(async () => { | ||
await delay(ms); | ||
if (abortController) { | ||
// Throw TimeoutError first | ||
setTimeout(() => abortController.abort(), 1); | ||
} | ||
// `Promise.race()` workaround (#91) | ||
const timeout = (promise, ms, abortController) => new Promise((resolve, reject) => { | ||
/* eslint-disable promise/prefer-await-to-then */ | ||
promise.then(resolve).catch(reject); | ||
delay(ms).then(() => { | ||
reject(new TimeoutError()); | ||
abortController.abort(); | ||
}); | ||
/* eslint-enable promise/prefer-await-to-then */ | ||
}); | ||
throw new TimeoutError(); | ||
})() | ||
]); | ||
const normalizeRequestMethod = input => requestMethods.includes(input) ? input.toUpperCase() : input; | ||
@@ -131,0 +129,0 @@ |
{ | ||
"name": "ky", | ||
"version": "0.9.0", | ||
"version": "0.9.1", | ||
"description": "Tiny and elegant HTTP client based on the browser Fetch API", | ||
@@ -18,3 +18,3 @@ "license": "MIT", | ||
"prepublishOnly": "npm run build", | ||
"test": "xo && npm run build && nyc ava && tsd-check" | ||
"test": "xo && npm run build && nyc ava && tsd" | ||
}, | ||
@@ -50,5 +50,4 @@ "files": [ | ||
"devDependencies": { | ||
"@types/jquery": "^3.3.29", | ||
"abort-controller": "^2.0.2", | ||
"ava": "^1.1.0", | ||
"abort-controller": "^3.0.0", | ||
"ava": "^1.4.1", | ||
"body": "^5.1.0", | ||
@@ -58,3 +57,3 @@ "codecov": "^3.1.0", | ||
"delay": "^4.1.0", | ||
"esm": "^3.0.84", | ||
"esm": "^3.2.22", | ||
"node-fetch": "^2.3.0", | ||
@@ -64,3 +63,3 @@ "nyc": "^13.1.0", | ||
"rollup": "^1.1.0", | ||
"tsd-check": "^0.3.0", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
@@ -70,7 +69,5 @@ }, | ||
"envs": [ | ||
"browser", | ||
"mocha" | ||
"browser" | ||
], | ||
"globals": [ | ||
"expect", | ||
"globalThis" | ||
@@ -77,0 +74,0 @@ ] |
@@ -17,3 +17,3 @@ <div align="center"> | ||
Ky targets [modern browsers](#browser-support) and [Deno](https://github.com/denoland/deno). For older browsers, you will need to transpile and use a [`fetch` polyfill](https://github.com/github/fetch). For Node.js, check out [Got](https://github.com/sindresorhus/got). | ||
Ky targets [modern browsers](#browser-support) and [Deno](https://github.com/denoland/deno). For older browsers, you will need to transpile and use a [`fetch` polyfill](https://github.com/github/fetch). For Node.js, check out [Got](https://github.com/sindresorhus/got). For isomorphic needs (like SSR), check out [`ky-universal`](https://github.com/sindresorhus/ky-universal). | ||
@@ -27,3 +27,3 @@ 1 KB *(minified & gzipped)*, one file, and no dependencies. | ||
- Method shortcuts (`ky.post()`) | ||
- Treats non-200 status codes as errors | ||
- Treats non-2xx status codes as errors | ||
- Retries failed requests | ||
@@ -122,3 +122,3 @@ - JSON option | ||
Returns a [`Response` object](https://developer.mozilla.org/en-US/docs/Web/API/Response) with [`Body` methods](https://developer.mozilla.org/en-US/docs/Web/API/Body#Methods) added for convenience. So you can, for example, call `ky.json()` directly on the `Response` without having to await it first. Unlike the `Body` methods of `window.Fetch`; these will throw an `HTTPError` if the response status is not in the range `200...299`. | ||
Returns a [`Response` object](https://developer.mozilla.org/en-US/docs/Web/API/Response) with [`Body` methods](https://developer.mozilla.org/en-US/docs/Web/API/Body#Methods) added for convenience. So you can, for example, call `ky.get(input).json()` directly without having to await the `Response` first. Unlike the `Body` methods of `window.Fetch`; these will throw an `HTTPError` if the response status is not in the range `200...299`. | ||
@@ -259,3 +259,3 @@ ### ky.get(input, [options]) | ||
(async () => { | ||
await api.get('/users/123'); | ||
await api.get('users/123'); | ||
//=> 'https://example.com/api/users/123' | ||
@@ -313,20 +313,14 @@ | ||
#### How is it different from [`got`](https://github.com/sindresorhus/got) | ||
#### How do I use this in Node.js? | ||
See my answer [here](https://twitter.com/sindresorhus/status/1037406558945042432). Got is maintained by the same people as Ky. | ||
Check out [`ky-universal`](https://github.com/sindresorhus/ky-universal#faq). | ||
#### How is it different from [`axios`](https://github.com/axios/axios)? | ||
#### How do I use this with a web app (React, Vue.js, etc.) that uses server-side rendering (SSR)? | ||
See my answer [here](https://twitter.com/sindresorhus/status/1037763588826398720). | ||
Check out [`ky-universal`](https://github.com/sindresorhus/ky-universal#faq). | ||
#### How is it different from [`r2`](https://github.com/mikeal/r2)? | ||
#### How do I test a browser library that uses this? | ||
See my answer in [#10](https://github.com/sindresorhus/ky/issues/10). | ||
Either use a test runner that can run in the browser, like Mocha, or use [AVA](http://ava.li) with `ky-universal`. [Read more.](https://github.com/sindresorhus/ky-universal#faq) | ||
#### 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. | ||
#### How do I use this without a bundler like Webpack? | ||
@@ -366,2 +360,21 @@ | ||
#### 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 | ||
@@ -374,2 +387,3 @@ | ||
- [ky-universal](https://github.com/sindresorhus/ky-universal) - Use Ky in both Node.js and browsers | ||
- [got](https://github.com/sindresorhus/got) - Simplified HTTP requests for Node.js | ||
@@ -376,0 +390,0 @@ |
24
umd.js
@@ -122,15 +122,13 @@ (function (global, factory) { | ||
const timeout = (promise, ms, abortController) => Promise.race([ | ||
promise, | ||
(async () => { | ||
await delay(ms); | ||
if (abortController) { | ||
// Throw TimeoutError first | ||
setTimeout(() => abortController.abort(), 1); | ||
} | ||
// `Promise.race()` workaround (#91) | ||
const timeout = (promise, ms, abortController) => new Promise((resolve, reject) => { | ||
/* eslint-disable promise/prefer-await-to-then */ | ||
promise.then(resolve).catch(reject); | ||
delay(ms).then(() => { | ||
reject(new TimeoutError()); | ||
abortController.abort(); | ||
}); | ||
/* eslint-enable promise/prefer-await-to-then */ | ||
}); | ||
throw new TimeoutError(); | ||
})() | ||
]); | ||
const normalizeRequestMethod = input => requestMethods.includes(input) ? input.toUpperCase() : input; | ||
@@ -322,5 +320,5 @@ | ||
exports.default = index; | ||
exports.HTTPError = HTTPError; | ||
exports.TimeoutError = TimeoutError; | ||
exports.default = index; | ||
@@ -327,0 +325,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
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
35760
13
395
683