pomdtr-jsoninvoice.web.val.run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/** @jsxImportSource npm:hono/jsx */
import { honoMiddleware } from "https://esm.town/v/pomdtr/codeOnValTown";
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo";
import * as basicTheme from "https://esm.town/v/pomdtr/invoice_basic_theme";
import {
extractParams,
Invoice,
Language,
schema,
} from "https://esm.town/v/pomdtr/invoice_schema";
import { Editor } from "https://esm.town/v/pomdtr/jsoninvoice_editor";
import { HomePage } from "https://esm.town/v/pomdtr/jsoninvoice_homepage";
import { Hono } from "npm:hono";
import { Base64 } from "npm:js-base64";
const app = new Hono();
const val = extractValInfo(import.meta.url);
app.use(
"/",
honoMiddleware({
val: {
handle: val.author,
name: val.name,
},
})
);
app.get("/", (c) => {
return c.html(<HomePage />);
});
app.get("/editor", (c) => {
return c.html(<Editor />);
});
app.get("/invoice/:code", async (c) => {
try {
const invoice: Invoice = JSON.parse(Base64.decode(c.req.param("code")));
const currencyCode = c.req.query("currency");
const currency = invoice.currencies.find(
(currency) => currency.code == currencyCode
);
if (!currency) {
return new Response(`${currencyCode} not available in currencies array`, {
status: 400,
});
}
const language = c.req.query("language") as Language;
const params = extractParams(invoice, {
currency,
language,
});
const rendered = await basicTheme.render(params);
return c.html(rendered);
} catch (e) {
return new Response(e.stack, {
status: 500,
});
}
});
app.get("/schema.json", () => {
return new Response(JSON.stringify(schema, null, 2), {
headers: {
"Content-Type": "application/json",
},
});
});
app.get("/TODO", (c) => {
return c.text("TODO");
});
export default app.fetch;
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v165
May 6, 2024