sushi-fetch
v1.0.0
Published
π£ A tiny but powerful data-fetching & caching library for modern JavaScript & TypeScript apps
Maintainers
Readme
π£ sushi-fetch
The lightweight, reactive powerhouse for modern data fetching.
Explore Docs β’ Report Bug β’ Request Feature
π The v1.0.0 Evolution
Modern web apps need more than just fetch(). They need to survive poor networks, deduplicate aggressive UI renders, and keep data fresh automatically. Usually, you'd have to choose between "too barebones" (native fetch) or "too heavy" (GraphQL clients/TanStack).
sushi-fetch v1.0.0 is the "Sweet Spot". We bring Silicon Valley-grade data synchronization to a ridiculously small footprint.
β¨ The "Secret Sauce" (Features)
- π¦ [NEW] Smart Batching (Event Loop Deduplication): Prevents network spam. 10 identical requests in the same tick? Only 1 hits the server.
- πΎ [NEW] Offline Persistence: Automatically dumps cache to
localStorage(or custom adapters) and hydrates instantly on reload. - π‘ [NEW] Auto-Polling & Focus Revalidation: Keeps your UI feeling "alive". Data refetches automatically when the user switches tabs or on a set interval.
- π§ Smart Memory Cache: Built-in TTL, sliding expiration, and LRU eviction.
- π SWR (Stale-While-Revalidate): Instant UI updates with background synchronization.
- β‘ Reactive Pub/Sub: Real-time state sync across your entire UI without needing Redux or Pinia.
- π Native Streaming: Simple progress tracking for big uploads/downloads.
- π Smart Retries: Fixed or Exponential Backoff strategies out of the box.
- πͺΆ Zero Dependencies: Pure, tree-shakable, strongly-typed TypeScript.
π¦ Installation
npm install sushi-fetch # or pnpm, yarn, bunπ The Magic (Quick Starts)
1. The "Indestructible" Request (v1.0.0)
Combine our most powerful features in a single, elegant call.
import { sushi } from 'sushi-fetch';
const data = await sushi.get('/api/dashboard', {
batch: true, // Groups simultaneous calls into one network request
revalidateOnFocus: true, // Silently updates data when user returns to the tab
pollInterval: 10000 // Keeps data fresh every 10 seconds
});2. Offline Persistence (Zero-Config Hydration)
Keep your app working even when the signal drops. sushi-fetch handles the storage and hydration automatically.
import { SushiCache } from 'sushi-fetch';
// Setup persistent cache (Uses localStorage by default in browsers)
const persistentCache = new SushiCache({
persistKey: 'my-app-offline-db'
});
// Next time the user opens the app, data loads from disk in 0ms!3. The Power of SWR (Stale-While-Revalidate)
Show the old data immediately, fetch the new one in the background. Your UI feels 10x faster.
const { data } = await sushi.get('/api/stats', { revalidate: true });4. Real-time Progress (Streaming)
Tracking download/upload progress without messing with raw Streams.
await sushi.get('/api/large-file', {
onProgress: ({ percentage }) => console.log(`Downloading: ${percentage}%`),
});π Comparison: No Bloat, Just Speed
| Feature | Native Fetch | Axios | Sushi-Fetch | | :--- | :--- | :--- | :--- | | Size (Gzip)| ~0kB | ~5.5kB | ~3.5kB | | Caching | β | β | β (Built-in) | | Smart Batching | β | β | β (Auto) | | Offline Sync | β | β | β | | SWR | β | β | β | | Revalidation | β | β | β (On Focus / Interval) | | Streaming | β (Hard) | β | β (Simple) |
βοΈ Advanced: Global Instance & Interceptors
Create a pre-configured instance for your specific API, complete with middleware and auth interception.
import { createSushi } from 'sushi-fetch';
const api = createSushi({
baseUrl: '[https://api.myapp.com/v1](https://api.myapp.com/v1)',
interceptors: {
request: async (url, options) => {
options.token = getMySecretToken(); // Automatically attaches Bearer token
return options;
}
}
});
// Usage anywhere in your app:
const user = await api.get('/profile');π€ Support the Movement
This project is a labor of love for efficient, high-performance code. If sushi-fetch helped you build a faster app, please consider:
- Giving it a Star β (It helps others find the library!)
- Submitting an Issue if you find a bug.
- Sharing it on Twitter/X or LinkedIn.
π License
MIT Β© sushilibdev
