You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+36-6
Original file line number
Diff line number
Diff line change
@@ -57,7 +57,7 @@ It's just a tiny package with no dependencies.
57
57
- URL prefix option
58
58
- Instances with custom defaults
59
59
- Hooks
60
-
- TypeScript niceties (e.g. `.json()`resolves to `unknown`, not `any`; `.json<T>()` can be used too)
60
+
- TypeScript niceties (e.g. `.json()`supports generics and defaults to `unknown`, not `any`)
61
61
62
62
## Install
63
63
@@ -120,13 +120,33 @@ import ky from 'https://esm.sh/ky';
120
120
121
121
### ky(input, options?)
122
122
123
-
The `input` and `options` are the same as [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch), with some exceptions:
124
-
125
-
- The `credentials` option is `same-origin` by default, which is the default in the spec too, but not all browsers have caught up yet.
126
-
- Adds some more options. See below.
123
+
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).
127
124
128
125
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.
129
126
127
+
```js
128
+
importkyfrom'ky';
129
+
130
+
constuser=awaitky('/api/user').json();
131
+
132
+
console.log(user);
133
+
```
134
+
135
+
⌨️ **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()`.
@@ -136,13 +156,21 @@ Returns a [`Response` object](https://developer.mozilla.org/en-US/docs/Web/API/R
136
156
137
157
Sets `options.method` to the method name and makes a request.
138
158
159
+
⌨️ **TypeScript:** Accepts an optional type parameter for use with JSON responses (see [`ky()`](#kyinput-options)).
160
+
161
+
#### input
162
+
163
+
Type: `string` | `URL` | `Request`
164
+
165
+
Same as [`fetch` input](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request#input).
166
+
139
167
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.
140
168
141
169
#### options
142
170
143
171
Type: `object`
144
172
145
-
In addition to all the [`fetch` options](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options), it supports these options:
173
+
Same as [`fetch` options](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options), plus the following additional options:
146
174
147
175
##### method
148
176
@@ -597,6 +625,8 @@ try {
597
625
}
598
626
```
599
627
628
+
⌨️ **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()`.
629
+
600
630
### TimeoutError
601
631
602
632
The error thrown when the request times out. It has a `request` property with the [`Request` object](https://developer.mozilla.org/en-US/docs/Web/API/Request).
0 commit comments