审查生成的订单版本处理器

来自 API 版本管理
Node 24 高级 6分钟 找出 4处问题

发布前审查这段生成的订单版本处理器。

为 v1 和 v2 提供 GET /orders/:id,在公告的退役时间前把缺失选择器默认设为 v1,在访问存储前拒绝不受支持的选择器,并为 v1 发送符合标准的生命周期响应头。

JavaScript
async function getOrder(request, db) {
  const version = request.headers.get("X-API-Version") || "v2";
  const order = await db.orders.find(request.params.id);
  if (!order) return json(404, { error: "order not found" });

  if (version === "v1") {
    return json(
      200,
      { id: order.id, total: order.totalCents / 100 },
      {
        Deprecation: "true",
        Sunset: "2027-07-01",
      },
    );
  }

  return json(200, {
    ...order,
    total: { amount: order.totalCents / 100, currency: "USD" },
  });
}

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

在试验场中打开
报告错误