Skip to content

Commit

Permalink
feat(convenience): elysia webhook adapter (#752)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lutymane authored Jan 31, 2025
1 parent 827dd40 commit 8d297d3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/convenience/frameworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ export type CloudflareModuleAdapter = (
request: Request,
) => ReqResHandler<Response>;

export type ElysiaAdapter = (ctx: {
body: Update;
headers: Record<string, string | undefined>;
set: {
headers: Record<string, string>;
status: number;
};
}) => ReqResHandler<string>;

export type ExpressAdapter = (req: {
body: Update;
header: (header: string) => string | undefined;
Expand Down Expand Up @@ -539,6 +548,33 @@ const worktop: WorktopAdapter = (req, res) => ({
unauthorized: () => res.send(401, WRONG_TOKEN_ERROR),
});

const elysia: ElysiaAdapter = (ctx) => {
// @note upgrade target to use modern code?
// const { promise, resolve } = Promise.withResolvers<string>();

let resolve: (result: string) => void;
const handlerReturn = new Promise<string>((res) => resolve = res);

return {
// @note technically the type shouldn't be limited to Promise, because it's fine to await plain values as well
update: Promise.resolve(ctx.body as Update),
header: ctx.headers[SECRET_HEADER_LOWERCASE],
end() {
resolve("");
},
respond(json) {
// @note since json is passed as string here, we gotta define proper content-type
ctx.set.headers["content-type"] = "application/json";
resolve(json);
},
unauthorized() {
ctx.set.status = 401;
resolve("");
},
handlerReturn,
};
};

// Please open a pull request if you want to add another adapter
export const adapters = {
"aws-lambda": awsLambda,
Expand All @@ -547,6 +583,7 @@ export const adapters = {
bun,
cloudflare,
"cloudflare-mod": cloudflareModule,
elysia,
express,
fastify,
hono,
Expand Down
7 changes: 7 additions & 0 deletions test/convenience/webhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
import type { NHttp } from "jsr:@nhttp/nhttp";
import type { Application } from "jsr:@oak/oak";
import type { createServer } from "node:http";
import type { Elysia } from "npm:elysia";
import type { Express } from "npm:@types/express";
import type bodyParser from "npm:@types/koa-bodyparser";
import type Koa from "npm:@types/koa";
Expand Down Expand Up @@ -51,6 +52,12 @@ describe("webhook", () => {
const _res: Response = await handler(req);
});

it("Elysia should be compatible with grammY adapter", () => {
const app = { post: () => {} } as unknown as Elysia;

app.post("/", webhookCallback(bot, "elysia"));
});

it("Express should be compatible with grammY adapter", () => {
const app = { post: () => {} } as unknown as Express;
const handler = webhookCallback(bot, "express");
Expand Down

0 comments on commit 8d297d3

Please sign in to comment.