latch-store - v0.2.4
    Preparing search index...

    latch-store - v0.2.4

    latch-store

    latch-store is a Node/Bun middleware library that makes any mutating endpoint idempotent with one line of code. It provides:

    • Two framework-specific entry points: idempotent.express() and idempotent.hono().
    • A pluggable store interface with two built-in adapters: memoryStore() (dev/test) and redisStore() (production).
    • Correctness guarantees under concurrent duplicate requests via atomic store operations and polling.
    • Body-hash mismatch detection to reject key reuse with different payloads.
    • A callback-based event system for observability.
    • Graceful degradation when the store is unavailable.
    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