审查生成的订单端点

来自 RESTful API 设计
Node 24 高级 6分钟 找出 4处问题

发布前审查这段生成的订单创建端点。

为已授权账户创建订单,对精确的正数金额只扣款一次,只公开公共表示,并允许调用方安全重试结果未知的请求。

JavaScript
async function createOrder(request, db, payments) {
  const body = await request.json();
  const key = request.headers.get("Idempotency-Key");
  const accountId = body.accountId;
  const order = {
    id: crypto.randomUUID(),
    accountId,
    total: Number(body.total),
    status: "paid",
    internalNote: body.internalNote,
  };
  await db.orders.insert(order);
  await payments.charge(accountId, order.total);
  return json(
    201,
    order,
    { Location: `/orders/${order.id}`, "Idempotency-Key": key },
  );
}

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

在试验场中打开
报告错误