-
Notifications
You must be signed in to change notification settings - Fork 0
helpers
Higher-level glue built on the core verbs. Currently this is a single helper, io.update, which turns a read-modify-write into a safe conditional PUT with automatic conflict retry.
import io from 'double-meh';
// bump a counter without clobbering a concurrent writer
const updated = await io.update('https://example.com/u/1', cur => ({
...cur,
n: cur.n + 1
}));An optimistic-concurrency read-modify-write. update:
-
GETs the current representation with its envelope (cache bypassed), capturing theETag. - Calls
fn(data)to produce the next value (fnmay be async). - If
fnreturnsundefined, nothing is written — the current data is returned as-is. - Otherwise
PUTs the result withIf-Match: <etag>, so the server rejects the write if the resource changed underneath you. - On success, invalidates the target's cache entry (when the cache is active) and returns the
PUTresult.
If the PUT comes back 412 Precondition Failed, update re-reads the resource, re-applies fn to the fresh data, and retries — up to 3 conflict retries before the BadStatus error is rethrown. Any non-412 error propagates immediately.
// read-modify-write: append a tag, retrying if someone else writes first
const result = await io.update('/api/doc/42', doc => ({
...doc,
tags: [...doc.tags, 'urgent']
}));Return undefined from fn to abort the write conditionally — a convenient no-op when the current state already satisfies you:
await io.update('/api/doc/42', doc =>
doc.archived ? undefined : {...doc, archived: true}
);options (third argument) is forwarded to both the GET and the PUT, so you can pass headers, query, signal, and other per-request settings. Note that cache: false (on the read) and ifMatch (on the write) are set for you.
target may be a URL string, a URL, or an options object with a url — the same shapes the core verbs accept.
Guides
Concepts
Services
Modules
- fetch transport
- keys & URLs
- helpers
- records
- sse
- storage backends
- compression encoders
- Service Worker integration
Cookbook