Skip to content

Démarrage

Installation

bash
npm install @hamdymohamedak/openfetch

Paquet ESM uniquement. Import : "@hamdymohamedak/openfetch".

Instance par défaut

ts
import openFetch from "@hamdymohamedak/openfetch";

const res = await openFetch.get("https://api.example.com/v1/users");

createClient / create

Alias. Passez des defaults initiaux ; client.defaults reste modifiable.

ts
import { createClient } from "@hamdymohamedak/openfetch";

const api = createClient({
  baseURL: "https://api.example.com",
  headers: { Authorization: "Bearer <token>" },
  timeout: 10_000,
  unwrapResponse: true,
});

const users = await api.get("/v1/users");

Méthodes HTTP

Retour Promise<OpenFetchResponse<T>> sauf si unwrapResponsePromise<T>.

MéthodeNotes
request(urlOrConfig, config?)URL ou config complète
get, head, optionsURL + config optionnelle
post, put, patchURL, data optionnel — objets → JSON + content-type si absent
deleteURL + config

URL requise : sinon erreur openfetch: `url` is required.

Middleware

ts
import { createClient, createRetryMiddleware } from "@hamdymohamedak/openfetch";

const client = createClient({ middlewares: [createRetryMiddleware()] });
client.use(createRetryMiddleware({ maxAttempts: 5 }));

Voir Intercepteurs et middleware et Nouvelles tentatives et cache.

Suite

MIT · @hamdymohamedak/openfetch