diff options
| author | Adam Soltys <[email protected]> | 2026-02-28 14:14:30 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-02-28 14:14:30 -0800 |
| commit | a1632ae402d11fdd7ffc360de97c55e0b4fad677 (patch) | |
| tree | aad5a1ea73f95dcd055c1d0b4ebf2708c36b4e01 /routes/invoices.ts | |
| parent | 76c7e52172f3dc25ee109434157d4d982ad9d2bd (diff) | |
| download | coinos-server-a1632ae402d11fdd7ffc360de97c55e0b4fad677.tar.gz coinos-server-a1632ae402d11fdd7ffc360de97c55e0b4fad677.zip | |
migrate to hono
Diffstat (limited to 'routes/invoices.ts')
| -rw-r--r-- | routes/invoices.ts | 51 |
1 files changed, 26 insertions, 25 deletions
diff --git a/routes/invoices.ts b/routes/invoices.ts index 7f44a28d..6fc0b67a 100644 --- a/routes/invoices.ts +++ b/routes/invoices.ts @@ -6,11 +6,9 @@ import { bail, fail, fields, getAccount, getInvoice, getUser } from "$lib/utils" import rpc from "@coinos/rpc"; export default { - async get(req, res) { + async get(c) { try { - const { - params: { id }, - } = req; + const id = c.req.param("id"); if (id === "undefined") fail("invalid id"); const invoice = await getInvoice(id); @@ -21,36 +19,37 @@ export default { invoice.items ||= []; } - if (invoice) res.send(invoice); + if (invoice) return c.json(invoice); else fail("invoice not found"); } catch (e) { - bail(res, e.message); + return bail(c, e.message); } }, - async create(req, res) { - let { body, user } = req; + async create(c) { + const body = await c.req.json(); + let user = c.get("user"); const { invoice } = body; if (body.user) user = body.user; - if (req.user.username === user.username) invoice.own = true; + if (c.get("user")?.username === user.username) invoice.own = true; else invoice.own = false; try { const result = await generate({ invoice, user }); - res.send(result); + return c.json(result); } catch (e) { console.trace(); console.log(e); - err("problem generating invoice", req.user?.username, body.user?.username, e.message); - bail(res, e.message); + err("problem generating invoice", c.get("user")?.username, body.user?.username, e.message); + return bail(c, e.message); } }, - async update(req, res) { + async update(c) { try { - const { id } = req.params; - const { body } = req; + const id = c.req.param("id"); + const body = await c.req.json(); const { tip, webhook, secret, received } = body.invoice; if (tip < 0) fail("Invalid tip"); @@ -61,7 +60,7 @@ export default { if (typeof tip !== "undefined") invoice.tip = tip; if (webhook && secret) { - if (invoice.uid !== req.user?.id) fail("Unauthorized"); + if (invoice.uid !== c.get("user")?.id) fail("Unauthorized"); invoice.webhook = webhook; invoice.secret = secret; } @@ -70,30 +69,32 @@ export default { await s(`invoice:${id}`, invoice); - res.send(invoice); + return c.json(invoice); } catch (e) { - bail(res, e.message); + return bail(c, e.message); } }, - async list(req, res) { - const { id } = req.user; + async list(c) { + const user = c.get("user"); + const { id } = user; let invoices = await db.lRange(`${id}:invoices`, 0, -1); invoices = (await Promise.all(invoices.map((i) => getInvoice(i)))).filter(Boolean); - res.send(invoices); + return c.json(invoices); }, - async sign(req, res) { + async sign(c) { try { - const { address, message, type = "bitcoin" } = req.body; + const body = await c.req.json(); + const { address, message, type = "bitcoin" } = body; const node = rpc(config[type]); if (config[type].walletpass) await node.walletPassphrase(config[type].walletpass, 300); const signature = await node.signMessage({ address, message }); - res.send({ signature }); + return c.json({ signature }); } catch (e) { - bail(res, e.message); + return bail(c, e.message); } }, }; |
