Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add bun web framework adapter #583

Merged
merged 11 commits into from
Jun 2, 2024
24 changes: 24 additions & 0 deletions src/convenience/frameworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ export type AzureAdapter = (request: {
};
}) => ReqResHandler;

export type BunAdapter = (request: {
headers: Headers;
json: () => Promise<Update>;
}) => ReqResHandler<Response>;

export type CloudflareAdapter = (event: {
request: Request;
respondWith: (response: Promise<Response>) => void;
Expand Down Expand Up @@ -289,6 +294,24 @@ const azure: AzureAdapter = (request, context) => ({
},
});

/** Bun.serve */
const bun: BunAdapter = (request) => {
let resolveResponse: (response: Response) => void;
return {
update: request.json(),
header: request.headers.get(SECRET_HEADER) || undefined,
end: () => {
resolveResponse(ok());
},
respond: (json) => {
resolveResponse(okJson(json));
},
unauthorized: () => {
resolveResponse(unauthorized());
},
};
};

/** Native CloudFlare workers (service worker) */
const cloudflare: CloudflareAdapter = (event) => {
let resolveResponse: (response: Response) => void;
Expand Down Expand Up @@ -521,6 +544,7 @@ export const adapters = {
"aws-lambda": awsLambda,
"aws-lambda-async": awsLambdaAsync,
azure,
bun,
cloudflare,
"cloudflare-mod": cloudflareModule,
express,
Expand Down
18 changes: 16 additions & 2 deletions test/convenience/webhook.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference types="npm:@types/node" />

import type { Hono } from "https://deno.land/x/hono/mod.ts";
import type {
APIGatewayProxyEventV2,
Expand Down Expand Up @@ -28,6 +26,22 @@ describe("webhook", () => {
));
});

it("Bun.serve should be compatible with grammY adapter", () => {
type BunServe = (
options: {
fetch: (request: Request) => Response | Promise<Response>;
},
) => object;

const handler = webhookCallback(bot, "bun");
const serve = (() => {}) as unknown as BunServe;
serve({
fetch: (request) => {
return handler(request);
},
});
});

it("Cloudflare Workers should be compatible with grammY adapter", async () => {
const req = {
json: () => ({}),
Expand Down
Loading