UNPKG

4.16 kBTypeScriptView Raw
1import { stop } from '../core/constants.js';
2import type { Input, Options } from './options.js';
3import type { ResponsePromise } from './response.js';
4export interface KyInstance {
5 /**
6 Fetch the given `url`.
7
8 @param url - `Request` object, `URL` object, or URL string.
9 @returns A promise with `Body` method added.
10
11 @example
12 ```
13 import ky from 'ky';
14
15 const json = await ky('https://example.com', {json: {foo: true}}).json();
16
17 console.log(json);
18 //=> `{data: '🦄'}`
19 ```
20 */
21 (url: Input, options?: Options): ResponsePromise;
22 /**
23 Fetch the given `url` using the option `{method: 'get'}`.
24
25 @param url - `Request` object, `URL` object, or URL string.
26 @returns A promise with `Body` methods added.
27 */
28 get: (url: Input, options?: Options) => ResponsePromise;
29 /**
30 Fetch the given `url` using the option `{method: 'post'}`.
31
32 @param url - `Request` object, `URL` object, or URL string.
33 @returns A promise with `Body` methods added.
34 */
35 post: (url: Input, options?: Options) => ResponsePromise;
36 /**
37 Fetch the given `url` using the option `{method: 'put'}`.
38
39 @param url - `Request` object, `URL` object, or URL string.
40 @returns A promise with `Body` methods added.
41 */
42 put: (url: Input, options?: Options) => ResponsePromise;
43 /**
44 Fetch the given `url` using the option `{method: 'delete'}`.
45
46 @param url - `Request` object, `URL` object, or URL string.
47 @returns A promise with `Body` methods added.
48 */
49 delete: (url: Input, options?: Options) => ResponsePromise;
50 /**
51 Fetch the given `url` using the option `{method: 'patch'}`.
52
53 @param url - `Request` object, `URL` object, or URL string.
54 @returns A promise with `Body` methods added.
55 */
56 patch: (url: Input, options?: Options) => ResponsePromise;
57 /**
58 Fetch the given `url` using the option `{method: 'head'}`.
59
60 @param url - `Request` object, `URL` object, or URL string.
61 @returns A promise with `Body` methods added.
62 */
63 head: (url: Input, options?: Options) => ResponsePromise;
64 /**
65 Create a new Ky instance with complete new defaults.
66
67 @returns A new Ky instance.
68 */
69 create: (defaultOptions: Options) => KyInstance;
70 /**
71 Create a new Ky instance with some defaults overridden with your own.
72
73 In contrast to `ky.create()`, `ky.extend()` inherits defaults from its parent.
74
75 @returns A new Ky instance.
76 */
77 extend: (defaultOptions: Options) => KyInstance;
78 /**
79 A `Symbol` that can be returned by a `beforeRetry` hook to stop the retry. This will also short circuit the remaining `beforeRetry` hooks.
80
81 Note: Returning this symbol makes Ky abort and return with an `undefined` response. Be sure to check for a response before accessing any properties on it or use [optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining). It is also incompatible with body methods, such as `.json()` or `.text()`, because there is no response to parse. In general, we recommend throwing an error instead of returning this symbol, as that will cause Ky to abort and then throw, which avoids these limitations.
82
83 A valid use-case for `ky.stop` is to prevent retries when making requests for side effects, where the returned data is not important. For example, logging client activity to the server.
84
85 @example
86 ```
87 import ky from 'ky';
88
89 const options = {
90 hooks: {
91 beforeRetry: [
92 async ({request, options, error, retryCount}) => {
93 const shouldStopRetry = await ky('https://example.com/api');
94 if (shouldStopRetry) {
95 return ky.stop;
96 }
97 }
98 ]
99 }
100 };
101
102 // Note that response will be `undefined` in case `ky.stop` is returned.
103 const response = await ky.post('https://example.com', options);
104
105 // Using `.text()` or other body methods is not supported.
106 const text = await ky('https://example.com', options).text();
107 ```
108 */
109 readonly stop: typeof stop;
110}