Review a generated Hono order route

from Hono
Hono 4.13.5 / Node 24 / TypeScript 6 advanced 8 min 4 issues to find

Review this generated route before it handles production orders.

Authenticate the actor, validate one order, create it durably and idempotently, and enqueue a notification only after a successful commit.

TypeScript
import { Hono } from 'hono'

type Order = { id: string; tenantId: string; key: string | undefined; sku: string; quantity: number }
type Bindings = { NOTIFICATIONS: { send(message: Order): Promise<void> } }
const orders: Order[] = []

const app = new Hono<{ Bindings: Bindings }>()

app.post('/orders', async (c) => {
  const body = await c.req.json() as { sku: string; quantity: number }
  const tenantId = c.req.header('x-tenant-id')!
  const key = c.req.header('idempotency-key')

  const existing = orders.find((order) => order.key === key)
  if (existing) return c.json(existing, 201)

  const order = {
    id: crypto.randomUUID(), tenantId, key,
    sku: body.sku, quantity: body.quantity,
  }

  orders.push(order)
  c.env.NOTIFICATIONS.send(order)
  return c.json(order, 201)
})

generated code is illustrative, not from any one model

Open in playground
Report an error