Skip to content

Commit 0b50a32

Browse files
authored
feat: add node-fetch and cross-fetch (#272)
* Add node-fetch and cross-fetch * Change to a single fetch file
1 parent 1035393 commit 0b50a32

4 files changed

Lines changed: 74 additions & 60 deletions

File tree

docs/modules/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ESLint plugin.
1313
## List of modules
1414

1515
- [`@jsdevtools/ezspawn`](./ez-spawn.md)
16-
- [`axios`](./axios.md)
16+
- [`axios`](./fetch.md)
1717
- [`bluebird`](./bluebird-q.md)
1818
- [`body-parser`](./body-parser.md)
1919
- [`buf-compare`](./buf-compare.md)
@@ -23,6 +23,7 @@ ESLint plugin.
2323
- [`chalk`](./chalk.md)
2424
- [`core-util-is`](./core-util-is.md)
2525
- [`cpx`](./cpx.md)
26+
- [`cross-fetch`](.fetch.md)
2627
- [`crypto-js`](./crypto-js.md)
2728
- [`deep-equal`](./deep-equal.md)
2829
- [`depcheck`](./depcheck.md)
@@ -60,6 +61,7 @@ ESLint plugin.
6061
- [`mkdirp`](./mkdirp.md)
6162
- [`moment.js`](./moment.md)
6263
- [`npm-run-all`](./npm-run-all.md)
64+
- [`node-fetch`](.fetch.md)
6365
- [`object-hash`](./object-hash.md)
6466
- [`ora`](./ora.md)
6567
- [`path-exists`](./path-exists.md)

docs/modules/axios.md

Lines changed: 0 additions & 58 deletions
This file was deleted.

docs/modules/fetch.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
description: Shared alternatives and examples for fetch based HTTP clients used across related modules
3+
---
4+
5+
# Fetch-based HTTP clients (shared)
6+
7+
This page contains the common, recommended alternatives and examples for fetch based HTTP clients used by `axios`, `node-fetch`, and `cross-fetch` replacement docs.
8+
9+
## Native `fetch` API
10+
11+
The native [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) API is available in Node.js (since v18.x) and all modern browsers. For many use cases it replaces `axios`/`node-fetch`/`cross-fetch` without adding dependencies.
12+
13+
Example:
14+
15+
```ts
16+
// GET
17+
const res = await fetch('https://api.example.com/data');
18+
const data = await res.json();
19+
20+
// POST
21+
await fetch('https://api.example.com/data', {
22+
method: 'POST',
23+
headers: {'Content-Type': 'application/json'},
24+
body: JSON.stringify({key: 'value'})
25+
});
26+
```
27+
28+
## `ky`
29+
30+
[`ky`](https://github.com/sindresorhus/ky) is a lightweight HTTP client built on top of the Fetch API. It adds convenience features like timeouts, hooks (interceptors), and a simpler API surface.
31+
32+
Example:
33+
34+
```ts
35+
import ky from 'ky';
36+
37+
const api = ky.create({
38+
prefixUrl: 'https://api.example.com',
39+
timeout: 5000 // ms
40+
});
41+
42+
const data = await api.get('users').json();
43+
```
44+
45+
## `ofetch`
46+
47+
[`ofetch`](https://github.com/unjs/ofetch) is a small fetch wrapper with automatic JSON parsing, request/response interceptors, and retry support.
48+
49+
Example:
50+
51+
```ts
52+
import {ofetch} from 'ofetch';
53+
54+
const api = ofetch.create({baseURL: 'https://api.example.com'});
55+
56+
const data = await api('/user', {query: {id: 123}});
57+
const created = await api('/items', {method: 'POST', body: {name: 'A'}});
58+
```

manifests/preferred.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{
1010
"type": "documented",
1111
"moduleName": "axios",
12-
"docPath": "axios",
12+
"docPath": "fetch",
1313
"category": "preferred"
1414
},
1515
{
@@ -72,6 +72,12 @@
7272
"docPath": "cpx",
7373
"category": "preferred"
7474
},
75+
{
76+
"type": "documented",
77+
"moduleName": "cross-fetch",
78+
"docPath": "fetch",
79+
"category": "preferred"
80+
},
7581
{
7682
"type": "documented",
7783
"moduleName": "crypto-js",
@@ -2232,6 +2238,12 @@
22322238
"docPath": "moment",
22332239
"category": "preferred"
22342240
},
2241+
{
2242+
"type": "documented",
2243+
"moduleName": "node-fetch",
2244+
"docPath": "fetch",
2245+
"category": "preferred"
2246+
},
22352247
{
22362248
"type": "documented",
22372249
"moduleName": "npm-run-all",

0 commit comments

Comments
 (0)