|
| 1 | +--- |
| 2 | +title: Bun Handler - idempot-js |
| 3 | +description: Add IETF-compliant idempotency to Bun.serve applications. Wraps native Request/Response handlers with Redis, PostgreSQL, MySQL, SQLite, or Bun SQL storage backends. |
| 4 | +--- |
| 5 | + |
| 6 | +# Bun |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +```bash |
| 11 | +bun add @idempot/bun-handler @idempot/bun-sql-store |
| 12 | +``` |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +```javascript |
| 17 | +import { idempotency } from "@idempot/bun-handler"; |
| 18 | +import { BunSqlIdempotencyStore } from "@idempot/bun-sql-store"; |
| 19 | + |
| 20 | +const store = new BunSqlIdempotencyStore("sqlite://:memory:"); |
| 21 | +const withIdempotency = idempotency({ store }); |
| 22 | + |
| 23 | +Bun.serve({ |
| 24 | + routes: { |
| 25 | + "/orders": withIdempotency(async (req) => { |
| 26 | + const body = await req.json(); |
| 27 | + const orderId = crypto.randomUUID(); |
| 28 | + return Response.json({ id: orderId, ...body }, { status: 201 }); |
| 29 | + }) |
| 30 | + } |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +Unlike Express/Fastify/Hono middleware, `idempotency()` returns a **handler wrapper** rather than a middleware function. Call it once per route to wrap the handler directly. |
| 35 | + |
| 36 | +## API |
| 37 | + |
| 38 | +### `idempotency(options)` |
| 39 | + |
| 40 | +Creates a handler wrapper for idempotency enforcement. |
| 41 | + |
| 42 | +**Options:** |
| 43 | + |
| 44 | +- `store` (required): Storage backend implementing `IdempotencyStore` |
| 45 | +- `required`: Whether idempotency key is required (default: `true`) |
| 46 | +- `ttlMs`: Time-to-live for idempotency records in milliseconds |
| 47 | +- `excludeFields`: Fields to exclude from fingerprint calculation |
| 48 | +- `resilience`: Circuit breaker and retry options |
| 49 | + |
| 50 | +**Returns:** A function `(handler) => handler` that wraps a `Request => Response` handler. The returned wrapper function has a `circuit` property for circuit-breaker monitoring. |
| 51 | + |
| 52 | +## Multiple Routes |
| 53 | + |
| 54 | +Call `idempotency()` once and reuse the wrapper across routes: |
| 55 | + |
| 56 | +```javascript |
| 57 | +import { idempotency } from "@idempot/bun-handler"; |
| 58 | +import { BunSqlIdempotencyStore } from "@idempot/bun-sql-store"; |
| 59 | + |
| 60 | +const store = new BunSqlIdempotencyStore("sqlite://:memory:"); |
| 61 | +const withIdempotency = idempotency({ store }); |
| 62 | + |
| 63 | +Bun.serve({ |
| 64 | + routes: { |
| 65 | + "/orders": withIdempotency(async (req) => { |
| 66 | + const body = await req.json(); |
| 67 | + return Response.json( |
| 68 | + { id: crypto.randomUUID(), ...body }, |
| 69 | + { status: 201 } |
| 70 | + ); |
| 71 | + }), |
| 72 | + |
| 73 | + "/payments": withIdempotency(async (req) => { |
| 74 | + const body = await req.json(); |
| 75 | + return Response.json( |
| 76 | + { id: crypto.randomUUID(), ...body }, |
| 77 | + { status: 201 } |
| 78 | + ); |
| 79 | + }) |
| 80 | + } |
| 81 | +}); |
| 82 | +``` |
| 83 | + |
| 84 | +## Circuit Breaker |
| 85 | + |
| 86 | +The wrapper exposes the underlying circuit breaker for monitoring: |
| 87 | + |
| 88 | +```javascript |
| 89 | +const withIdempotency = idempotency({ store }); |
| 90 | + |
| 91 | +console.log(withIdempotency.circuit.stats); |
| 92 | +``` |
0 commit comments