Fetch
The net/fetch.js module enhances the native Fetch API with common patterns.
fetchJSON
Typed JSON fetch with timeout, retry, and deduplication:
js
import { fetchJSON } from 'weblisk/net/fetch.js'; const user = await fetchJSON('/api/user/123');
Options
js
const data = await fetchJSON('/api/search', { method: 'POST', body: { query: 'weblisk' }, timeout: 5000, // 5s timeout (default: 10000) retries: 3, // Retry on failure (default: 0) dedupe: true, // Deduplicate concurrent identical requests headers: { Authorization: 'Bearer token' }, });
Retries use exponential backoff (1s, 2s, 4s, ...) and only retry on network errors or 5xx status codes.
fetchStream
Stream NDJSON (newline-delimited JSON) as an async generator:
js
import { fetchStream } from 'weblisk/net/fetch.js'; for await (const event of fetchStream('/api/events')) { console.log('Event:', event); }
Works with any API that returns newline-separated JSON objects.
Offline Queue
Queue mutations when offline, flush when connectivity returns:
js
import { fetchOffline, flushOfflineQueue } from 'weblisk/net/fetch.js'; // This works even when offline const result = await fetchOffline('/api/todos', { method: 'POST', body: JSON.stringify({ text: 'Buy milk' }), }); if (result.queued) { console.log('Saved offline, will sync when online'); } // Manually flush the queue window.addEventListener('online', async () => { const count = await flushOfflineQueue(); console.log(`Flushed ${count} queued requests`); });