35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
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;
|
||
}
|
||
},
|
||
},
|
||
];
|