owofetch-core
v1.0.0
Published
Smart Fetch Library by OwO System
Readme
🦉 owofetch-core
Smart, ultra-lightweight, and professional HTTP fetch wrapper that saves you dozens of lines of boilerplate code. Get auto-retry, TTL cache, request deduplication, and timeout guard out-of-the-box in a single elegant line of code! 🚀
⚡ How does OwO Fetch simplify your code? (The Ultimate Comparison)
Instead of writing dozens of lines of redundant code to handle network hiccups, timeouts, in-memory caching, and concurrent request deduplication in every part of your app...
❌ The Old Way (Native Fetch) - Over 40 Lines of Boilerplate!
If you want a safe fetch call with a Timeout guard, automatic Retry on network blips, in-memory Caching, and concurrent request Deduplication (coalescing):
const cache = new Map();
const pending = new Map();
async function fetchUser(userId) {
const url = `https://jsonplaceholder.typicode.com/posts/${userId}`;
if (cache.has(url)) return cache.get(url);
if (pending.has(url)) return pending.get(url);
const promise = (async () => {
let retries = 2;
while (retries >= 0) {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 8000);
try {
const res = await fetch(url, { signal: controller.signal });
clearTimeout(timer);
if (!res.ok) throw new Error("HTTP Error");
const data = await res.json();
cache.set(url, data);
return data;
} catch (err) {
clearTimeout(timer);
if (retries === 0) throw err;
retries--;
}
}
})();
pending.set(url, promise);
try {
return await promise;
} finally {
pending.delete(url);
}
}✔ The OwO Fetch Way - A Single Elegant Line of Code!
All the complex logic, error handling, abort controllers, retries, and map states are handled internally and compressed into one clean, robust line:
import owo from "owofetch-core";
// One line of code handles caching, deduplication, timeout, and retry automatically!
const user = await owo.get("https://jsonplaceholder.typicode.com/posts/1");🚀 Outstanding Out-of-the-Box Features
- 🔄 Automatic Retries (Auto-Retry): If the network blips momentarily, the library automatically retries behind the scenes, keeping your UX smooth and error-free.
- ⏱️ Timeout Guard: Slow or hanging connections are aborted automatically using a built-in
AbortControllerto prevent memory leaks and sluggish performance. - 💾 Smart TTL Memory Cache: Duplicate requests fetch data instantly from the local cache, saving bandwidth and optimizing client performance.
- 🔗 Request Deduplication (Promise Coalescing): If multiple components query the exact same endpoint simultaneously, only one network request is sent. The response promise is shared among all callers, boosting performance by up to 300%.
💻 Installation
Install the package via npm:
npm install owofetch-core💡 Usage & API Guide
1. Retrieve Data (GET)
By default, GET requests activate caching and request deduplication automatically:
import owo from "owofetch-core";
const posts = await owo.get("https://jsonplaceholder.typicode.com/posts");
console.log(posts);2. Send Data (POST)
Send request payloads effortlessly with automatic headers configuration (e.g., application/json is added by default):
import owo from "owofetch-core";
const newPost = await owo.post("https://jsonplaceholder.typicode.com/posts", {
title: "OwO Fetch Core Rocks!",
body: "Simplifying HTTP requests like a charm.",
userId: 1
});
console.log("Response:", newPost);3. Custom Configurations
Customize parameters per-request or update the default configurations seamlessly:
// Override defaults for a specific request
const customData = await owo.get("https://jsonplaceholder.typicode.com/posts/1", {
timeout: 3000, // Abort after 3 seconds instead of 8
retries: 5 // Retry 5 times instead of 2
});4. Debug & Trace Mode
Enable console debugging to see exactly what happens behind the scenes in real-time:
import owo from "owofetch-core";
// Enable logger
owo.enableDebug();
// Your console or terminal will now display detailed cache hits and request events!
const res1 = await owo.get("https://jsonplaceholder.typicode.com/posts/1");
const res2 = await owo.get("https://jsonplaceholder.typicode.com/posts/1"); // Triggers console: [OwO] CACHE HIT📄 License
MIT Open Source License — provided with love by OwO System 🦉.
