Comparing version 1.6.0 to 1.7.0
import type { NormalizedOptions } from '../types/options.js'; | ||
import type { KyRequest } from '../types/request.js'; | ||
import type { KyResponse } from '../types/response.js'; | ||
export declare class HTTPError extends Error { | ||
response: KyResponse; | ||
export declare class HTTPError<T = unknown> extends Error { | ||
response: KyResponse<T>; | ||
request: KyRequest; | ||
@@ -7,0 +7,0 @@ options: NormalizedOptions; |
@@ -1,2 +0,1 @@ | ||
// eslint-lint-disable-next-line @typescript-eslint/naming-convention | ||
export class HTTPError extends Error { | ||
@@ -3,0 +2,0 @@ response; |
@@ -21,3 +21,3 @@ import { type stop } from '../core/constants.js'; | ||
*/ | ||
(url: Input, options?: Options): ResponsePromise; | ||
<T>(url: Input, options?: Options): ResponsePromise<T>; | ||
/** | ||
@@ -29,3 +29,3 @@ Fetch the given `url` using the option `{method: 'get'}`. | ||
*/ | ||
get: (url: Input, options?: Options) => ResponsePromise; | ||
get: <T>(url: Input, options?: Options) => ResponsePromise<T>; | ||
/** | ||
@@ -37,3 +37,3 @@ Fetch the given `url` using the option `{method: 'post'}`. | ||
*/ | ||
post: (url: Input, options?: Options) => ResponsePromise; | ||
post: <T>(url: Input, options?: Options) => ResponsePromise<T>; | ||
/** | ||
@@ -45,3 +45,3 @@ Fetch the given `url` using the option `{method: 'put'}`. | ||
*/ | ||
put: (url: Input, options?: Options) => ResponsePromise; | ||
put: <T>(url: Input, options?: Options) => ResponsePromise<T>; | ||
/** | ||
@@ -53,3 +53,3 @@ Fetch the given `url` using the option `{method: 'delete'}`. | ||
*/ | ||
delete: (url: Input, options?: Options) => ResponsePromise; | ||
delete: <T>(url: Input, options?: Options) => ResponsePromise<T>; | ||
/** | ||
@@ -61,3 +61,3 @@ Fetch the given `url` using the option `{method: 'patch'}`. | ||
*/ | ||
patch: (url: Input, options?: Options) => ResponsePromise; | ||
patch: <T>(url: Input, options?: Options) => ResponsePromise<T>; | ||
/** | ||
@@ -64,0 +64,0 @@ Fetch the given `url` using the option `{method: 'head'}`. |
@@ -27,5 +27,5 @@ type UndiciHeadersInit = string[][] | Record<string, string | readonly string[]> | Headers; | ||
}; | ||
export type KyRequest = { | ||
json: <T = unknown>() => Promise<T>; | ||
export type KyRequest<T = unknown> = { | ||
json: <J = T>() => Promise<J>; | ||
} & Request; | ||
export {}; |
@@ -1,3 +0,3 @@ | ||
export type KyResponse = { | ||
json: <T = unknown>() => Promise<T>; | ||
export type KyResponse<T = unknown> = { | ||
json: <J = T>() => Promise<J>; | ||
} & Response; |
@@ -5,3 +5,3 @@ /** | ||
import { type KyResponse } from './response.js'; | ||
export type ResponsePromise = { | ||
export type ResponsePromise<T = unknown> = { | ||
arrayBuffer: () => Promise<ArrayBuffer>; | ||
@@ -28,7 +28,9 @@ blob: () => Promise<Blob>; | ||
const result = await ky(…).json<Result>(); | ||
const result1 = await ky(…).json<Result>(); | ||
// or | ||
const result2 = await ky<Result>(…).json(); | ||
``` | ||
*/ | ||
json: <T = unknown>() => Promise<T>; | ||
json: <J = T>() => Promise<J>; | ||
text: () => Promise<string>; | ||
} & Promise<KyResponse>; | ||
} & Promise<KyResponse<T>>; |
{ | ||
"name": "ky", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "Tiny and elegant HTTP client based on the Fetch API", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -60,3 +60,3 @@ <div align="center"> | ||
- Hooks | ||
- TypeScript niceties (e.g. `.json()` resolves to `unknown`, not `any`; `.json<T>()` can be used too) | ||
- TypeScript niceties (e.g. `.json()` supports generics and defaults to `unknown`, not `any`) | ||
@@ -124,9 +124,29 @@ ## Install | ||
The `input` and `options` are the same as [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch), with some exceptions: | ||
The `input` and `options` are the same as [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch), with additional `options` available (see below). | ||
- The `credentials` option is `same-origin` by default, which is the default in the spec too, but not all browsers have caught up yet. | ||
- Adds some more options. See below. | ||
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/Fetch_API/Using_Fetch#body) added for convenience. So you can, for example, call `ky.get(input).json()` directly without having to await the `Response` first. When called like that, an appropriate `Accept` header will be set depending on the body method used. Unlike the `Body` methods of `window.Fetch`; these will throw an `HTTPError` if the response status is not in the range of `200...299`. Also, `.json()` will return an empty string if body is empty or the response status is `204` instead of throwing a parse error due to an empty body. | ||
```js | ||
import ky from 'ky'; | ||
const user = await ky('/api/user').json(); | ||
console.log(user); | ||
``` | ||
⌨️ **TypeScript:** Accepts an optional [type parameter](https://www.typescriptlang.org/docs/handbook/2/generics.html), which defaults to [`unknown`](https://www.typescriptlang.org/docs/handbook/2/functions.html#unknown), and is passed through to the return type of `.json()`. | ||
```ts | ||
import ky from 'ky'; | ||
// user1 is unknown | ||
const user1 = await ky('/api/users/1').json(); | ||
// user2 is a User | ||
const user2 = await ky<User>('/api/users/2').json(); | ||
// user3 is a User | ||
const user3 = await ky('/api/users/3').json<User>(); | ||
console.log([user1, user2, user3]); | ||
``` | ||
### ky.get(input, options?) | ||
@@ -141,2 +161,10 @@ ### ky.post(input, options?) | ||
⌨️ **TypeScript:** Accepts an optional type parameter for use with JSON responses (see [`ky()`](#kyinput-options)). | ||
#### input | ||
Type: `string` | `URL` | `Request` | ||
Same as [`fetch` input](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request#input). | ||
When using a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) instance as `input`, any URL altering options (such as `prefixUrl`) will be ignored. | ||
@@ -148,3 +176,3 @@ | ||
In addition to all the [`fetch` options](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options), it supports these options: | ||
Same as [`fetch` options](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options), plus the following additional options: | ||
@@ -604,2 +632,4 @@ ##### method | ||
⌨️ **TypeScript:** Accepts an optional [type parameter](https://www.typescriptlang.org/docs/handbook/2/generics.html), which defaults to [`unknown`](https://www.typescriptlang.org/docs/handbook/2/functions.html#unknown), and is passed through to the return type of `error.response.json()`. | ||
### TimeoutError | ||
@@ -606,0 +636,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
157538
1175
777