Making an operation safe to call more than once — payment capture, order creation, webhook handling — under retries or duplicate requests.
Overview
The 'don't double-charge, don't send twice' primitive. Run any operation at-most-once per idempotency key and replay the stored result on retries — concurrency-safe (in-flight de-dupe + atomic create), with request fingerprinting and a pluggable store. Framework-agnostic (every other lib is Hono/AWS-locked).
Install
$ npm install @lacspace/idempotency
Quick example
@lacspace/idempotencyts
await idempotent(key, () => charge(order))
Returns { value, replayed }
Tips
Register the in-flight promise synchronously (before any await) so concurrent same-key calls share one execution — the package does this for you.
Return the same stored result for a repeated key within the TTL window.
Zero-dependency and isomorphic — safe to import on the server, in the browser, on the edge and in React Native. It tree-shakes, so you only ship what you import.
Watch out for
The default store is in-memory (per process). For multi-instance deployments, back it with a shared store (e.g. Redis).