审查生成的 Hono 订单路由

来自 Hono
Hono 4.13.5 / Node 24 / TypeScript 6 高级 8分钟 找出 4处问题

在这段生成路由处理生产订单前,对它进行审查。

认证主体、验证订单、以持久且幂等的方式创建订单,并且只在成功提交后发送通知。

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)
})

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误