Skip to content

Commit 8d297d3

Browse files
authored
feat(convenience): elysia webhook adapter (#752)
1 parent 827dd40 commit 8d297d3

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/convenience/frameworks.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ export type CloudflareModuleAdapter = (
109109
request: Request,
110110
) => ReqResHandler<Response>;
111111

112+
export type ElysiaAdapter = (ctx: {
113+
body: Update;
114+
headers: Record<string, string | undefined>;
115+
set: {
116+
headers: Record<string, string>;
117+
status: number;
118+
};
119+
}) => ReqResHandler<string>;
120+
112121
export type ExpressAdapter = (req: {
113122
body: Update;
114123
header: (header: string) => string | undefined;
@@ -539,6 +548,33 @@ const worktop: WorktopAdapter = (req, res) => ({
539548
unauthorized: () => res.send(401, WRONG_TOKEN_ERROR),
540549
});
541550

551+
const elysia: ElysiaAdapter = (ctx) => {
552+
// @note upgrade target to use modern code?
553+
// const { promise, resolve } = Promise.withResolvers<string>();
554+
555+
let resolve: (result: string) => void;
556+
const handlerReturn = new Promise<string>((res) => resolve = res);
557+
558+
return {
559+
// @note technically the type shouldn't be limited to Promise, because it's fine to await plain values as well
560+
update: Promise.resolve(ctx.body as Update),
561+
header: ctx.headers[SECRET_HEADER_LOWERCASE],
562+
end() {
563+
resolve("");
564+
},
565+
respond(json) {
566+
// @note since json is passed as string here, we gotta define proper content-type
567+
ctx.set.headers["content-type"] = "application/json";
568+
resolve(json);
569+
},
570+
unauthorized() {
571+
ctx.set.status = 401;
572+
resolve("");
573+
},
574+
handlerReturn,
575+
};
576+
};
577+
542578
// Please open a pull request if you want to add another adapter
543579
export const adapters = {
544580
"aws-lambda": awsLambda,
@@ -547,6 +583,7 @@ export const adapters = {
547583
bun,
548584
cloudflare,
549585
"cloudflare-mod": cloudflareModule,
586+
elysia,
550587
express,
551588
fastify,
552589
hono,

test/convenience/webhook.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
import type { NHttp } from "jsr:@nhttp/nhttp";
77
import type { Application } from "jsr:@oak/oak";
88
import type { createServer } from "node:http";
9+
import type { Elysia } from "npm:elysia";
910
import type { Express } from "npm:@types/express";
1011
import type bodyParser from "npm:@types/koa-bodyparser";
1112
import type Koa from "npm:@types/koa";
@@ -51,6 +52,12 @@ describe("webhook", () => {
5152
const _res: Response = await handler(req);
5253
});
5354

55+
it("Elysia should be compatible with grammY adapter", () => {
56+
const app = { post: () => {} } as unknown as Elysia;
57+
58+
app.post("/", webhookCallback(bot, "elysia"));
59+
});
60+
5461
it("Express should be compatible with grammY adapter", () => {
5562
const app = { post: () => {} } as unknown as Express;
5663
const handler = webhookCallback(bot, "express");

0 commit comments

Comments
 (0)