Skip to content

Commit 2be558c

Browse files
committed
style: lint repo with prettier
1 parent 55efdce commit 2be558c

6 files changed

+66
-69
lines changed

.eslintrc

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
{
2-
"extends": [
3-
"eslint-config-unjs"
4-
],
2+
"extends": ["eslint-config-unjs"],
53
"rules": {
64
"no-undef": 0,
75
"unicorn/consistent-destructuring": 0,
8-
"unicorn/no-await-expression-member": 0,
6+
"unicorn/no-await-expression-member": 0
97
}
108
}

README.md

+56-48
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ Import:
2525

2626
```js
2727
// ESM / Typescript
28-
import { ofetch } from 'ofetch'
28+
import { ofetch } from "ofetch";
2929

3030
// CommonJS
31-
const { ofetch } = require('ofetch')
31+
const { ofetch } = require("ofetch");
3232
```
3333

3434
## ✔️ Works with Node.js
3535

3636
We use [conditional exports](https://nodejs.org/api/packages.html#packages_conditional_exports) to detect Node.js
37-
and automatically use [unjs/node-fetch-native](https://github.com/unjs/node-fetch-native). If `globalThis.fetch` is available, will be used instead. To leverage Node.js 17.5.0 experimental native fetch API use [`--experimental-fetch` flag](https://nodejs.org/dist/latest-v17.x/docs/api/cli.html#--experimental-fetch).
37+
and automatically use [unjs/node-fetch-native](https://github.com/unjs/node-fetch-native). If `globalThis.fetch` is available, will be used instead. To leverage Node.js 17.5.0 experimental native fetch API use [`--experimental-fetch` flag](https://nodejs.org/dist/latest-v17.x/docs/api/cli.html#--experimental-fetch).
3838

3939
### `keepAlive` support
4040

@@ -47,7 +47,7 @@ By setting the `FETCH_KEEP_ALIVE` environment variable to `true`, an http/https
4747
`ofetch` will smartly parse JSON and native values using [destr](https://github.com/unjs/destr), falling back to text if it fails to parse.
4848

4949
```js
50-
const { users } = await ofetch('/api/users')
50+
const { users } = await ofetch("/api/users");
5151
```
5252

5353
For binary content types, `ofetch` will instead return a `Blob` object.
@@ -56,21 +56,24 @@ You can optionally provide a different parser than destr, or specify `blob`, `ar
5656

5757
```js
5858
// Use JSON.parse
59-
await ofetch('/movie?lang=en', { parseResponse: JSON.parse })
59+
await ofetch("/movie?lang=en", { parseResponse: JSON.parse });
6060

6161
// Return text as is
62-
await ofetch('/movie?lang=en', { parseResponse: txt => txt })
62+
await ofetch("/movie?lang=en", { parseResponse: (txt) => txt });
6363

6464
// Get the blob version of the response
65-
await ofetch('/api/generate-image', { responseType: 'blob' })
65+
await ofetch("/api/generate-image", { responseType: "blob" });
6666
```
6767

6868
## ✔️ JSON Body
6969

7070
`ofetch` automatically stringifies request body (if an object is passed) and adds JSON `Content-Type` and `Accept` headers (for `put`, `patch` and `post` requests).
7171

7272
```js
73-
const { users } = await ofetch('/api/users', { method: 'POST', body: { some: 'json' } })
73+
const { users } = await ofetch("/api/users", {
74+
method: "POST",
75+
body: { some: "json" },
76+
});
7477
```
7578

7679
## ✔️ Handling Errors
@@ -80,21 +83,21 @@ const { users } = await ofetch('/api/users', { method: 'POST', body: { some: 'js
8083
Parsed error body is available with `error.data`. You may also use `FetchError` type.
8184

8285
```ts
83-
await ofetch('http://google.com/404')
86+
await ofetch("http://google.com/404");
8487
// FetchError: 404 Not Found (http://google.com/404)
8588
// at async main (/project/playground.ts:4:3)
8689
```
8790

8891
To catch error response:
8992

9093
```ts
91-
await ofetch('/url').catch(err => err.data)
94+
await ofetch("/url").catch((err) => err.data);
9295
```
9396

9497
To bypass status error catching you can set `ignoreResponseError` option:
9598

9699
```ts
97-
await ofetch('/url', { ignoreResponseError: true })
100+
await ofetch("/url", { ignoreResponseError: true });
98101
```
99102

100103
## ✔️ Auto Retry
@@ -119,18 +122,18 @@ Default for `retry` is `1` retry, except for `POST`, `PUT`, `PATCH` and `DELETE`
119122
Default for `retryDelay` is `0` ms.
120123

121124
```ts
122-
await ofetch('http://google.com/404', {
125+
await ofetch("http://google.com/404", {
123126
retry: 3,
124-
retryDelay: 500 // ms
125-
})
127+
retryDelay: 500, // ms
128+
});
126129
```
127130

128131
## ✔️ Type Friendly
129132

130133
Response can be type assisted:
131134

132135
```ts
133-
const article = await ofetch<Article>(`/api/article/${id}`)
136+
const article = await ofetch<Article>(`/api/article/${id}`);
134137
// Auto complete working with article.id
135138
```
136139

@@ -139,15 +142,15 @@ const article = await ofetch<Article>(`/api/article/${id}`)
139142
By using `baseURL` option, `ofetch` prepends it with respecting to trailing/leading slashes and query search params for baseURL using [ufo](https://github.com/unjs/ufo):
140143

141144
```js
142-
await ofetch('/config', { baseURL })
145+
await ofetch("/config", { baseURL });
143146
```
144147

145148
## ✔️ Adding Query Search Params
146149

147150
By using `query` option (or `params` as alias), `ofetch` adds query search params to URL by preserving query in request itself using [ufo](https://github.com/unjs/ufo):
148151

149152
```js
150-
await ofetch('/movie?lang=en', { query: { id: 123 } })
153+
await ofetch("/movie?lang=en", { query: { id: 123 } });
151154
```
152155

153156
## ✔️ Interceptors
@@ -161,56 +164,60 @@ You might want to use `ofetch.create` to set shared interceptors.
161164
`onRequest` is called as soon as `ofetch` is being called, allowing to modify options or just do simple logging.
162165

163166
```js
164-
await ofetch('/api', {
167+
await ofetch("/api", {
165168
async onRequest({ request, options }) {
166169
// Log request
167-
console.log('[fetch request]', request, options)
170+
console.log("[fetch request]", request, options);
168171

169172
// Add `?t=1640125211170` to query search params
170-
options.query = options.query || {}
171-
options.query.t = new Date()
172-
}
173-
})
173+
options.query = options.query || {};
174+
options.query.t = new Date();
175+
},
176+
});
174177
```
175178

176179
### `onRequestError({ request, options, error })`
177180

178181
`onRequestError` will be called when fetch request fails.
179182

180183
```js
181-
await ofetch('/api', {
184+
await ofetch("/api", {
182185
async onRequestError({ request, options, error }) {
183186
// Log error
184-
console.log('[fetch request error]', request, error)
185-
}
186-
})
187+
console.log("[fetch request error]", request, error);
188+
},
189+
});
187190
```
188191

189-
190192
### `onResponse({ request, options, response })`
191193

192194
`onResponse` will be called after `fetch` call and parsing body.
193195

194196
```js
195-
await ofetch('/api', {
197+
await ofetch("/api", {
196198
async onResponse({ request, response, options }) {
197199
// Log response
198-
console.log('[fetch response]', request, response.status, response.body)
199-
}
200-
})
200+
console.log("[fetch response]", request, response.status, response.body);
201+
},
202+
});
201203
```
202204

203205
### `onResponseError({ request, options, response })`
204206

205207
`onResponseError` is same as `onResponse` but will be called when fetch happens but `response.ok` is not `true`.
206208

207209
```js
208-
await ofetch('/api', {
210+
await ofetch("/api", {
209211
async onResponseError({ request, response, options }) {
210212
// Log error
211-
console.log('[fetch response error]', request, response.status, response.body)
212-
}
213-
})
213+
console.log(
214+
"[fetch response error]",
215+
request,
216+
response.status,
217+
response.body
218+
);
219+
},
220+
});
214221
```
215222

216223
## ✔️ Create fetch with default options
@@ -220,22 +227,22 @@ This utility is useful if you need to use common options across several fetch ca
220227
**Note:** Defaults will be cloned at one level and inherited. Be careful about nested options like `headers`.
221228

222229
```js
223-
const apiFetch = ofetch.create({ baseURL: '/api' })
230+
const apiFetch = ofetch.create({ baseURL: "/api" });
224231

225-
apiFetch('/test') // Same as ofetch('/test', { baseURL: '/api' })
232+
apiFetch("/test"); // Same as ofetch('/test', { baseURL: '/api' })
226233
```
227234

228235
## 💡 Adding headers
229236

230237
By using `headers` option, `ofetch` adds extra headers in addition to the request default headers:
231238

232239
```js
233-
await ofetch('/movies', {
240+
await ofetch("/movies", {
234241
headers: {
235-
Accept: 'application/json',
236-
'Cache-Control': 'no-cache'
237-
}
238-
})
242+
Accept: "application/json",
243+
"Cache-Control": "no-cache",
244+
},
245+
});
239246
```
240247

241248
## 💡 Adding HTTP(S) Agent
@@ -245,17 +252,17 @@ If you need use HTTP(S) Agent, can add `agent` option with `https-proxy-agent` (
245252
```js
246253
import { HttpsProxyAgent } from "https-proxy-agent";
247254

248-
await ofetch('/api', {
249-
agent: new HttpsProxyAgent('http://example.com')
250-
})
255+
await ofetch("/api", {
256+
agent: new HttpsProxyAgent("http://example.com"),
257+
});
251258
```
252259

253260
## 🍣 Access to Raw Response
254261

255262
If you need to access raw response (for headers, etc), can use `ofetch.raw`:
256263

257264
```js
258-
const response = await ofetch.raw('/sushi')
265+
const response = await ofetch.raw("/sushi");
259266

260267
// response._data
261268
// response.headers
@@ -267,7 +274,7 @@ const response = await ofetch.raw('/sushi')
267274
As a shortcut, you can use `ofetch.native` that provides native `fetch` API
268275

269276
```js
270-
const json = await ofetch.native('/sushi').then(r => r.json())
277+
const json = await ofetch.native("/sushi").then((r) => r.json());
271278
```
272279

273280
## 📦 Bundler Notes
@@ -300,6 +307,7 @@ If you need to support legacy users, you can optionally transpile the library in
300307
MIT. Made with 💖
301308

302309
<!-- Badges -->
310+
303311
[npm-version-src]: https://img.shields.io/npm/v/ofetch?style=flat&colorA=18181B&colorB=F0DB4F
304312
[npm-version-href]: https://npmjs.com/package/ofetch
305313
[npm-downloads-src]: https://img.shields.io/npm/dm/ofetch?style=flat&colorA=18181B&colorB=F0DB4F

build.config.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import { defineBuildConfig } from "unbuild";
33
export default defineBuildConfig({
44
declaration: true,
55
rollup: {
6-
emitCJS: true
6+
emitCJS: true,
77
},
8-
entries: [
9-
"src/index",
10-
"src/node"
11-
]
8+
entries: ["src/index", "src/node"],
129
});

renovate.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
{
2-
"extends": [
3-
"github>unjs/renovate-config"
4-
]
2+
"extends": ["github>unjs/renovate-config"]
53
}

tsconfig.json

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77
"outDir": "dist",
88
"strict": true,
99
"declaration": true,
10-
"types": [
11-
"node"
12-
]
10+
"types": ["node"]
1311
},
14-
"include": [
15-
"src"
16-
]
12+
"include": ["src"]
1713
}

vitest.config.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { defineConfig } from "vitest/config";
33
export default defineConfig({
44
test: {
55
coverage: {
6-
reporter: ["text", "clover", "json"]
7-
}
8-
}
6+
reporter: ["text", "clover", "json"],
7+
},
8+
},
99
});

0 commit comments

Comments
 (0)