losi-tiny-http
v1.0.1
Published
Blazing-fast, zero-dependency HTTP client for Node & Browser
Maintainers
Readme
🚀 LosiTinyHttp
A lightweight, fetch-compatible HTTP client for browser and Node.js.
Supports interceptors, retries, timeouts, and cancellation — all with zero dependencies.
📦 Installation
npm install losi-tiny-http
# or
yarn add losi-tiny-http
# or
pnpm add losi-tiny-http⚡ Quick Start
import { client } from "losi-tiny-http";
async function run() {
const res = await client.get("https://jsonplaceholder.typicode.com/posts/1");
console.log(res.data);
}
run();✅ Works in both browser and Node.js — no extra setup required.
🧩 Create Your Own Instance
import { create } from "losi-tiny-http";
const api = create({
baseURL: "https://jsonplaceholder.typicode.com",
timeout: 5000,
headers: {
"Accept": "application/json",
},
retry: {
attempts: 3,
backoff: "exponential",
delay: 500,
},
});
const response = await api.get("/posts/1");
console.log(response.data);🌐 HTTP Methods
await api.get("/posts/1");
await api.post("/posts", { title: "TinyHttp", body: "Lightweight HTTP" });
await api.put("/posts/1", { title: "Updated" });
await api.patch("/posts/1", { body: "Partial update" });
await api.delete("/posts/1");
await api.head("/posts/1");
await api.options("/posts");🧱 Interceptors
Request Interceptor
api.interceptRequest(async (config) => {
config.headers.Authorization = "Bearer my-token";
console.log("➡️", config.method, config.url);
return config;
});Response Interceptor
api.interceptResponse(async (response) => {
console.log("✅", response.status);
return response;
});Error Interceptor
api.interceptError(async (error) => {
console.error("❌", error.message);
return Promise.reject(error);
});🔁 Retry Logic
const api = create({
retry: {
attempts: 3,
backoff: "exponential", // or "fixed"
delay: 300, // ms
},
});Automatically retries failed requests (network errors, timeouts, or 5xx).
⏱️ Timeout Support
const api = create({ timeout: 3000 });
try {
await api.get("https://httpstat.us/200?sleep=5000");
} catch (err) {
console.error("⏰ Timeout:", err.message);
}💾 File Uploads & Downloads
// Upload
const form = new FormData();
form.append("file", myFile);
await api.post("/upload", form, {
headers: { "Content-Type": "multipart/form-data" },
});
// Download
const res = await api.get("/file.pdf", { responseType: "blob" });
const blob = res.data;🛑 Request Cancellation
const controller = new AbortController();
const req = api.get("/posts", { signal: controller.signal });
setTimeout(() => controller.abort(), 1000);
try {
await req;
} catch (err) {
console.log("❌ Cancelled:", err.name);
}⚛️ React Example
import React, { useEffect, useState } from "react";
import { client } from "tiny-http";
export function Posts() {
const [posts, setPosts] = useState<any[]>([]);
useEffect(() => {
const controller = new AbortController();
client
.get("/posts", { signal: controller.signal })
.then((res) => setPosts(res.data))
.catch((err) => {
if (err.name !== "AbortError") console.error("Fetch failed:", err);
});
return () => controller.abort();
}, []);
return (
<ul>
{posts.slice(0, 5).map((p) => (
<li key={p.id}>{p.title}</li>
))}
</ul>
);
}⚙️ Configuration Options
| Option | Type | Description |
|--------|------|-------------|
| baseURL | string | Base API URL |
| timeout | number | Request timeout (ms) |
| headers | Record<string,string> | Default headers |
| retry.attempts | number | Retry count |
| retry.backoff | "fixed" or "exponential" | Retry delay type |
| retry.delay | number | Base retry delay (ms) |
| cache | RequestCache | "default", "no-store", "reload", etc. |
| responseType | "json", "text", "blob", "arraybuffer" | Expected response data type |
🧠 Features Summary
✅ Works in both Node.js and Browser
✅ Zero dependencies
✅ TypeScript-first design
✅ Built-in retry & timeout
✅ Interceptors (request, response, error)
✅ Abortable requests
✅ Supports JSON, Blob, and FormData
📄 License
MIT © 2025
Created with ❤️ by developers who love clean HTTP clients.
