deno.land / x / ky@v0.31.3 / source / types / ky.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import {stop} from '../core/constants.js';import type {Input, Options} from './options.js';import type {ResponsePromise} from './ResponsePromise.js';
export interface KyInstance { /** 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';
const json = await ky('https://example.com', {json: {foo: true}}).json();
console.log(json); //=> `{data: '🦄'}` ``` */ (url: Input, options?: Options): 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: Input, 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: Input, 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: Input, 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: Input, 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: Input, options?: Options) => 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: Input, options?: Options) => ResponsePromise;
/** Create a new Ky instance with complete new defaults.
@returns A new Ky instance. */ create: (defaultOptions: Options) => KyInstance;
/** Create a new Ky instance with some defaults overridden with your own.
In contrast to `ky.create()`, `ky.extend()` inherits defaults from its parent.
@returns A new Ky instance. */ extend: (defaultOptions: Options) => KyInstance;
/** A `Symbol` that can be returned by a `beforeRetry` hook to stop the retry. This will also short circuit the remaining `beforeRetry` hooks.
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.
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.
@example ``` import ky from 'ky';
const options = { hooks: { beforeRetry: [ async ({request, options, error, retryCount}) => { const shouldStopRetry = await ky('https://example.com/api'); if (shouldStopRetry) { return ky.stop; } } ] } };
// Note that response will be `undefined` in case `ky.stop` is returned. const response = await ky.post('https://example.com', options);
// Using `.text()` or other body methods is not supported. const text = await ky('https://example.com', options).text(); ``` */ readonly stop: typeof stop;}
ky

Version Info

Tagged at
a year ago