-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
67 lines (58 loc) · 1.72 KB
/
main.ts
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
#!/usr/bin/env -S deno run -A --unstable-hmr
import {render} from 'https://deno.land/x/resvg_wasm/mod.ts'
import {qrcode} from 'jsr:@libs/qrcode'
import {zValidator} from 'npm:@hono/zod-validator'
import {Hono} from 'npm:hono'
import {cors} from 'npm:hono/cors'
import {z} from 'npm:zod'
const app = new Hono()
app.use(cors())
app.get('/', (c) => c.redirect('https://github.com/maks11060/qr'))
app.get(
'/svg',
zValidator(
'query',
z.object({
text: z.string().max(7089),
s: z.coerce.number().nonnegative().min(29).max(5_000).optional(),
b: z.coerce.number().nonnegative().min(1).max(100).default(4),
ecl: z.enum(['LOW', 'MEDIUM', 'QUARTILE', 'HIGH']).default('LOW'),
})
),
(c) => {
const {text, s, b, ecl} = c.req.valid('query')
const svg = qrcode(text, {output: 'svg', border: b, ecl}).replace(
'<svg',
`<svg width="${s}px" height="${s}px"`
)
return new Response(svg, {
headers: {'content-type': 'image/svg+xml'},
})
}
)
app.get(
'/png',
zValidator(
'query',
z.object({
text: z.string().max(7089),
s: z.coerce.number().nonnegative().min(29).max(5_000).default(300),
b: z.coerce.number().nonnegative().min(1).max(100).default(4),
ecl: z.enum(['LOW', 'MEDIUM', 'QUARTILE', 'HIGH']).default('LOW'),
// light: z.string().optional(),
// dark: z.string().optional(),
})
),
async (c) => {
const {text, s, b, ecl} = c.req.valid('query')
const svq = qrcode(text, {output: 'svg', border: b, ecl}).replace(
'<svg',
`<svg width="${s}px" height="${s}px"`
)
const png = await render(svq)
return new Response(png, {
headers: {'content-type': 'image/png'},
})
}
)
Deno.serve(app.fetch)