smart-fetch-pro
v1.0.1
Published
A smart fetch wrapper with retries, caching, error handling, loading states, and React support.
Maintainers
Readme
📦 smart-fetch-pro
Effortless API requests with retry logic, error handling, caching, and React support — built on native fetch.
🔥 Why Use smart-fetch-pro?
Making API requests in modern apps requires more than just calling fetch():
- ❌ Repeating try-catch blocks
- ❌ Manually handling retries
- ❌ Lack of built-in caching
- ❌ Messy code in React components
smart-fetch-pro solves all of this with:
✅ Retry support
✅ Error handling
✅ In-memory caching
✅ React hook: useSmartFetch
✅ Request/Response interceptors
✅ Lightweight & TypeScript-ready
📌 Installation
```bash`` npm install smart-fetch-pro
🧑💻 Usage
Basic Fetch with Retry
import { fetchSmart } from 'smart-fetch-pro';
const data = await fetchSmart('/api/user', { retries: 3, retryDelay: 1000, });
With Caching
const data = await fetchSmart('/api/products', { cacheTTL: 60000, // 1 min in-memory cache });
React Hook
import { useSmartFetch } from 'smart-fetch-pro';
const { data, error, isLoading } = useSmartFetch('/api/posts');
if (isLoading) return Loading...; if (error) return Error: {error.message};
return ;
🧠 Real-World Use Cases
1. 📊 Dashboard Analytics with Retry + Cache
const stats = await fetchSmart('/api/stats', { retries: 2, cacheTTL: 30000, });
✅ Reduces server load ✅ Improves stability on flaky APIs ✅ Prevents duplicate requests
2. 🛍️ E-commerce Product List in React
const { data, isLoading, error } = useSmartFetch('/api/products');
✅ Cleaner UI code ✅ Handles loading/error automatically ✅ Great DX (developer experience)
3. 🔐 Add Global Authorization Header (via Interceptor)
import { setRequestInterceptor } from 'smart-fetch-pro';
setRequestInterceptor((config) => {
config.headers['Authorization'] = Bearer ${getToken()};
return config;
});
✅ DRY authentication logic ✅ Supports all requests globally
⚙️ Features Overview
| Feature | Native Fetch | Axios | smart-fetch-pro | | --------------------- | ------------ | ----- | --------------- | | Retry Support | ❌ | ✅ | ✅ | | React Hook | ❌ | ❌ | ✅ | | In-Memory Caching | ❌ | ❌ | ✅ | | Request Interceptor | ❌ | ✅ | ✅ | | Response Interceptor | ❌ | ✅ | ✅ | | TypeScript Support | ⚠️ Partial | ✅ | ✅ | | Lightweight (no deps) | ✅ | ❌ | ✅ |
📈 Coming Soon
✅ Response schema validation
✅ Offline queue support
✅ Retry on network failure with exponential backoff
✅ Logging and devtools integration
🧰 API Reference
fetchSmart(url, options) Make smart fetch calls with retry, error handling, and caching.
url – API endpoint (string)
options – Optional fetch options with:
retries (number)
retryDelay (ms)
cacheTTL (ms)
headers, body, etc.
useSmartFetch(url, options) React hook for using fetchSmart with internal state.
setRequestInterceptor(fn) Add logic to modify request before it is sent.
setResponseInterceptor(fn) Add logic to process or transform response data.
💡 Tip for Devs
Think of smart-fetch-pro as the bridge between vanilla fetch, Axios, and SWR — giving you the best of all worlds, in one neat package.
