UNPKG

4.73 kBTypeScriptView Raw
1import { type stop } from '../core/constants.js';
2import type { Input, Options } from './options.js';
3import type { ResponsePromise } from './ResponsePromise.js';
4export type 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 <T>(url: Input, options?: Options): ResponsePromise<T>;
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: <T>(url: Input, options?: Options) => ResponsePromise<T>;
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: <T>(url: Input, options?: Options) => ResponsePromise<T>;
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: <T>(url: Input, options?: Options) => ResponsePromise<T>;
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: <T>(url: Input, options?: Options) => ResponsePromise<T>;
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: <T>(url: Input, options?: Options) => ResponsePromise<T>;
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 You can also refer to parent defaults by providing a function to `.extend()`.
76
77 @example
78 ```
79 import ky from 'ky';
80
81 const api = ky.create({prefixUrl: 'https://example.com/api'});
82
83 const usersApi = api.extend((options) => ({prefixUrl: `${options.prefixUrl}/users`}));
84
85 const response = await usersApi.get('123');
86 //=> 'https://example.com/api/users/123'
87
88 const response = await api.get('version');
89 //=> 'https://example.com/api/version'
90 ```
91
92 @returns A new Ky instance.
93 */
94 extend: (defaultOptions: Options | ((parentOptions: Options) => Options)) => KyInstance;
95 /**
96 A `Symbol` that can be returned by a `beforeRetry` hook to stop the retry. This will also short circuit the remaining `beforeRetry` hooks.
97
98 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.
99
100 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.
101
102 @example
103 ```
104 import ky from 'ky';
105
106 const options = {
107 hooks: {
108 beforeRetry: [
109 async ({request, options, error, retryCount}) => {
110 const shouldStopRetry = await ky('https://example.com/api');
111 if (shouldStopRetry) {
112 return ky.stop;
113 }
114 }
115 ]
116 }
117 };
118
119 // Note that response will be `undefined` in case `ky.stop` is returned.
120 const response = await ky.post('https://example.com', options);
121
122 // Using `.text()` or other body methods is not supported.
123 const text = await ky('https://example.com', options).text();
124 ```
125 */
126 readonly stop: typeof stop;
127};