latch-store is a Node/Bun middleware library that makes any mutating endpoint idempotent with one line of code. It provides:
idempotent.express() and idempotent.hono().memoryStore() (dev/test) and redisStore() (production).npm install latch-store
# or
bun add latch-store
import { idempotent, memoryStore } from 'latch-store'
const store = memoryStore()
const app = express()
app.post('/charge', idempotent.express({ store }), async (req, res) => {
// This handler runs at most once per unique Idempotency-Key
res.json({ status: 'charged' })
})
import { idempotent, memoryStore } from 'latch-store'
const store = memoryStore()
const app = new Hono()
app.post('/charge', idempotent.hono({ store }), async (c) => {
return c.json({ status: 'charged' })
})
See the generated documentation for the full API reference.
| Adapter | Factory | Use Case |
|---|---|---|
| In-memory | memoryStore() |
Local dev, tests |
| Redis | redisStore() |
Production, multi-instance |
Implement the Store interface (4 methods):
import type { Store } from 'latch-store'
class MyStore implements Store {
async setIfAbsent(key, value, ttlMs) { /* ... */ }
async set(key, value, ttlMs) { /* ... */ }
async get(key) { /* ... */ }
async del(key) { /* ... */ }
}
bun install
bun test # run tests
bun run typecheck # type-check all source files
MIT