UNPKG

1.24 kBTypeScriptView Raw
1/**
2Returns a `Response` object with `Body` methods 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.
3*/
4import { type KyResponse } from './response.js';
5export type ResponsePromise<T = unknown> = {
6 arrayBuffer: () => Promise<ArrayBuffer>;
7 blob: () => Promise<Blob>;
8 formData: () => Promise<FormData>;
9 /**
10 Get the response body as JSON.
11
12 @example
13 ```
14 import ky from 'ky';
15
16 const json = await ky(…).json();
17 ```
18
19 @example
20 ```
21 import ky from 'ky';
22
23 interface Result {
24 value: number;
25 }
26
27 const result1 = await ky(…).json<Result>();
28 // or
29 const result2 = await ky<Result>(…).json();
30 ```
31 */
32 json: <J = T>() => Promise<J>;
33 text: () => Promise<string>;
34} & Promise<KyResponse<T>>;