peppa/bench/tasks/03-bugfix/repo/product/routes.mjs
2026-11-17 02:09:17 +05:00

35 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { esc, html, json, page } from "./lib/http.mjs";
import { catalog } from "./catalog.mjs";
import { CartError, quote } from "./cart.mjs";
const home = () => `
<h1>Пион</h1>
<p>Цветы с доставкой по Алматы. Доставка бесплатно от 5 000 ₸.</p>
<ul>${catalog.map((i) => `<li>${esc(i.title)} — ${i.price} ₸</li>`).join("")}</ul>`;
export const routes = [
{
method: "GET",
path: "/healthz",
handler: ({ res }) => {
res.writeHead(200, { "content-type": "text/plain; charset=utf-8" });
res.end("ok");
},
},
{ method: "GET", path: "/", handler: ({ res }) => html(res, page("Пион — цветы", home())) },
{
// Расчёт заказа: { items: [{ sku, qty }], promo? } → { subtotal, discount, delivery, total }.
method: "POST",
path: "/api/quote",
async handler({ res, body }) {
const b = await body();
try {
json(res, 200, quote(b.items, b.promo));
} catch (e) {
if (e instanceof CartError) return json(res, 400, { errors: { items: e.message } });
throw e;
}
},
},
];