-
Notifications
You must be signed in to change notification settings - Fork 102
/
frameworks.shared.ts
48 lines (43 loc) · 1.23 KB
/
frameworks.shared.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
// deno-lint-ignore-file no-explicit-any
export const SECRET_HEADER = "X-Telegram-Bot-Api-Secret-Token";
/** express web framework */
const express = (req: any, res: any) => ({
update: Promise.resolve(req.body),
header: req.header(SECRET_HEADER),
end: () => res.end(),
respond: (json: string) => {
res.set("Content-Type", "application/json");
res.send(json);
},
unauthorized: () => {
res.send(401, "secret token is wrong");
},
});
/** koa web framework */
const koa = (ctx: any) => ({
update: Promise.resolve(ctx.request.body),
header: ctx.get(SECRET_HEADER),
end: () => {
ctx.body = "";
},
respond: (json: string) => {
ctx.set("Content-Type", "application/json");
ctx.response.body = json;
},
unauthorized: () => {
ctx.status = 401;
},
});
/** fastify web framework */
const fastify = (req: any, reply: any) => ({
update: Promise.resolve(req.body),
header: req.headers[SECRET_HEADER.toLowerCase()],
end: () => reply.status(200).send(),
respond: (json: string) => reply.send(json),
unauthorized: () => reply.code(401).send("secret token is wrong"),
});
export const adapters = {
express,
koa,
fastify,
};