@nicotordev/fetch-client
v3.0.0
Published
Fetch client for Node.js & Browser
Readme
FetchClient
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.
🚀 Features
- ✅ Custom
FetchClientErrorwith Axios-style properties - 🔁 Automatic serialization of complex types (
Date,BigInt,Map,Set) - 🔄 Circular reference detection
- 💥 Centralized error handling
- 🧪 Fully typed with TypeScript
📦 Installation
npm install @nicotordev/fetch-client
# or
yarn install @nicotordev/fetch-clientThis module assumes you are using it within a TypeScript project.
🛠 Usage
Initialization
import FetchClient from "./FetchClient";
const api = new FetchClient("https://api.example.com");Example Requests
GET
const data = await api.get("/users");POST
const newUser = await api.post("/users", {
name: "Nico",
createdAt: new Date(),
});PUT / PATCH
await api.put("/users/123", { name: "Updated Name" });
await api.patch("/users/123", { name: "Partial Update" });DELETE
await api.delete("/users/123");❗ Error Handling
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);
}
}🔧 Advanced Serialization
Supports:
Date→ ISO stringBigInt→ stringMap/Set→ serializable object format- Circular references → safely replaced with
"[Circular Reference]"
📄 License
MIT
✍️ Author
Created by Nicolas Torres ([email protected]).
