diff --git a/src/webhook.ts b/src/webhook.ts index d61a4c0..49b7ccb 100644 --- a/src/webhook.ts +++ b/src/webhook.ts @@ -12,6 +12,32 @@ export type FrameworkAdapter = (...args: any[]) => FrameworkHandler; export const frameworks = { elysia: ({ body }) => ({ body, response: () => responseOK }), + fastify: (request, reply) => ({ + body: request.body, + response: () => reply.send("OK"), + }), + hono: (c) => ({ body: c.req.json(), response: () => c.text("OK") }), + express: (req, res) => ({ body: req.body, response: () => res.send("OK") }), + koa: (ctx) => ({ + body: ctx.request.body, + response: () => { + ctx.body = "OK"; + }, + }), + http: (req, res) => ({ + body: new Promise((resolve) => { + let body = ""; + + req.on("data", (chunk: Buffer) => { + body += chunk.toString(); + }); + + req.on("end", () => resolve(JSON.parse(body))); + }), + response: () => res.writeHead(200).end("OK"), + }), + "std/http": (req) => ({ body: req.json(), response: () => responseOK }), + "Bun.serve": (req) => ({ body: req.json(), response: () => responseOK }), } satisfies Record; export function webhookHandler( @@ -20,7 +46,7 @@ export function webhookHandler( ) { const frameworkAdapter = frameworks[framework]; - return async (...args: any[]) => { + return (async (...args: any[]) => { const { body, response } = frameworkAdapter( // @ts-expect-error ...args, @@ -29,12 +55,11 @@ export function webhookHandler( await tKassa.emit(await body); if (response) return response(); - }; - // as unknown as ReturnType<(typeof frameworks)[Framework]> extends { - // response: () => any; - // } - // ? ( - // ...args: Parameters<(typeof frameworks)[Framework]> - // ) => ReturnType["response"]> - // : (...args: Parameters<(typeof frameworks)[Framework]>) => void; + }) as ReturnType<(typeof frameworks)[Framework]> extends { + response: () => any; + } + ? ( + ...args: Parameters<(typeof frameworks)[Framework]> + ) => ReturnType["response"]> + : (...args: Parameters<(typeof frameworks)[Framework]>) => void; }