diff --git a/docs/modules/README.md b/docs/modules/README.md index f4114b0..00f9f37 100644 --- a/docs/modules/README.md +++ b/docs/modules/README.md @@ -13,7 +13,7 @@ ESLint plugin. ## List of modules - [`@jsdevtools/ezspawn`](./ez-spawn.md) -- [`axios`](./axios.md) +- [`axios`](./fetch.md) - [`bluebird`](./bluebird-q.md) - [`body-parser`](./body-parser.md) - [`buf-compare`](./buf-compare.md) @@ -23,6 +23,7 @@ ESLint plugin. - [`chalk`](./chalk.md) - [`core-util-is`](./core-util-is.md) - [`cpx`](./cpx.md) +- [`cross-fetch`](.fetch.md) - [`crypto-js`](./crypto-js.md) - [`deep-equal`](./deep-equal.md) - [`depcheck`](./depcheck.md) @@ -60,6 +61,7 @@ ESLint plugin. - [`mkdirp`](./mkdirp.md) - [`moment.js`](./moment.md) - [`npm-run-all`](./npm-run-all.md) +- [`node-fetch`](.fetch.md) - [`object-hash`](./object-hash.md) - [`ora`](./ora.md) - [`path-exists`](./path-exists.md) diff --git a/docs/modules/axios.md b/docs/modules/axios.md deleted file mode 100644 index 6b4499c..0000000 --- a/docs/modules/axios.md +++ /dev/null @@ -1,58 +0,0 @@ ---- -description: Modern alternatives to the axios package for making HTTP requests in browsers and Node.js ---- - -# Replacements for `axios` - -## Native `fetch` API - -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 most HTTP requests, it can replace `axios` without extra dependencies. - -Example: - -```ts -// GET -const res = await fetch('https://api.example.com/data') -const data = await res.json() - -// POST -await fetch('https://api.example.com/data', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ key: 'value' }) -}) -``` - -## `ky` - -[`ky`](https://github.com/sindresorhus/ky) is a lightweight HTTP client based on the Fetch API with timeout support, hooks (interceptors) and other helpers. - -Example: - -```ts -import ky from 'ky' - -const api = ky.create({ - prefixUrl: 'https://api.example.com', - timeout: 5000, // ms -}) - -const data = await api.get('users').json() -``` - -## `ofetch` - -[`ofetch`](https://github.com/unjs/ofetch) s a fetch wrapper with automatic JSON parsing, request/response interceptors, and retries. - -Example: - -```ts -import { ofetch } from 'ofetch' - -const api = ofetch.create({ - baseURL: 'https://api.example.com', -}) - -const data = await api('/user', { query: { id: 123 } }) -const created = await api('/items', { method: 'POST', body: { name: 'A' } }) -``` diff --git a/docs/modules/fetch.md b/docs/modules/fetch.md new file mode 100644 index 0000000..21d1957 --- /dev/null +++ b/docs/modules/fetch.md @@ -0,0 +1,58 @@ +--- +description: Shared alternatives and examples for fetch based HTTP clients used across related modules +--- + +# Fetch-based HTTP clients (shared) + +This page contains the common, recommended alternatives and examples for fetch based HTTP clients used by `axios`, `node-fetch`, and `cross-fetch` replacement docs. + +## Native `fetch` API + +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. + +Example: + +```ts +// GET +const res = await fetch('https://api.example.com/data'); +const data = await res.json(); + +// POST +await fetch('https://api.example.com/data', { + method: 'POST', + headers: {'Content-Type': 'application/json'}, + body: JSON.stringify({key: 'value'}) +}); +``` + +## `ky` + +[`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. + +Example: + +```ts +import ky from 'ky'; + +const api = ky.create({ + prefixUrl: 'https://api.example.com', + timeout: 5000 // ms +}); + +const data = await api.get('users').json(); +``` + +## `ofetch` + +[`ofetch`](https://github.com/unjs/ofetch) is a small fetch wrapper with automatic JSON parsing, request/response interceptors, and retry support. + +Example: + +```ts +import {ofetch} from 'ofetch'; + +const api = ofetch.create({baseURL: 'https://api.example.com'}); + +const data = await api('/user', {query: {id: 123}}); +const created = await api('/items', {method: 'POST', body: {name: 'A'}}); +``` diff --git a/manifests/preferred.json b/manifests/preferred.json index eaa7abc..613f1af 100644 --- a/manifests/preferred.json +++ b/manifests/preferred.json @@ -9,7 +9,7 @@ { "type": "documented", "moduleName": "axios", - "docPath": "axios", + "docPath": "fetch", "category": "preferred" }, { @@ -72,6 +72,12 @@ "docPath": "cpx", "category": "preferred" }, + { + "type": "documented", + "moduleName": "cross-fetch", + "docPath": "fetch", + "category": "preferred" + }, { "type": "documented", "moduleName": "crypto-js", @@ -2232,6 +2238,12 @@ "docPath": "moment", "category": "preferred" }, + { + "type": "documented", + "moduleName": "node-fetch", + "docPath": "fetch", + "category": "preferred" + }, { "type": "documented", "moduleName": "npm-run-all",