A lightweight, extensible HTTP client built around the native Fetch API, with custom error handling and advanced JSON serialization support. Designed as a simplified alternative to Axios.
- ✅ Custom
FetchClientError
with Axios-style properties - 🔁 Automatic serialization of complex types (
Date
,BigInt
,Map
,Set
) - 🔄 Circular reference detection
- 💥 Centralized error handling
- 🧪 Fully typed with TypeScript
npm install @nicotordev/fetch-client
# or
yarn install @nicotordev/fetch-client
This module assumes you are using it within a TypeScript project.
import FetchClient from "./FetchClient";
const api = new FetchClient("https://api.example.com");
const data = await api.get("/users");
const newUser = await api.post("/users", {
name: "Nico",
createdAt: new Date(),
});
await api.put("/users/123", { name: "Updated Name" });
await api.patch("/users/123", { name: "Partial Update" });
await api.delete("/users/123");
All failed responses (non-2xx status codes) throw a FetchClientError
, which mimics the structure of an Axios error:
try {
await api.get("/fail");
} catch (err) {
if (err instanceof FetchClientError) {
console.error("Error status:", err.response?.status);
console.error("Response data:", err.data);
console.error("Request config:", err.config);
}
}
Supports:
-
Date
→ ISO string -
BigInt
→ string -
Map
/Set
→ serializable object format - Circular references → safely replaced with
"[Circular Reference]"
MIT
Created by Nicolas Torres (nicotordev@gmail.com).