@heyhru/common-util-fetch
v0.3.0
Published
Shared HTTP fetch wrapper based on axios, usable on web/server/RN
Downloads
303
Readme
@heyhru/common-util-fetch
Shared HTTP fetch wrapper based on axios, usable on web, server, and React Native.
Installation
pnpm add @heyhru/common-util-fetchUsage
import { create } from "@heyhru/common-util-fetch";
const client = create({
baseURL: "https://api.example.com",
getToken: () => localStorage.getItem("token"), // optional: attach Bearer token
onUnauthorized: () => {
localStorage.removeItem("token");
window.location.href = "/login";
},
});
// GET
const user = await client.get<User>("/users/1");
// POST
const created = await client.post<User>("/users", { name: "Alice" });
// PUT
const updated = await client.put<User>("/users/1", { name: "Bob" });
// DELETE
await client.delete("/users/1");API
create(config)
| Option | Type | Default | Description |
| ----------------- | ------------------------------------------------- | -------- | ------------------------------------------------- |
| baseURL | string | required | Base URL for all requests |
| getToken | () => string \| null \| Promise<string \| null> | — | Called before each request to attach Bearer token |
| withCredentials | boolean | false | Send cookies with cross-origin requests |
| onUnauthorized | () => void | — | Called when a 401 response is received |
Returns a FetchClient with get, post, put, delete methods. All methods return response.data directly.
