Skip to content

Commit

Permalink
feat: add bun web framework adapter (#583)
Browse files Browse the repository at this point in the history
Co-authored-by: winstxnhdw <winstxnhdw@gmail.com>
Co-authored-by: KnorpelSenf <shtrog@gmail.com>
  • Loading branch information
3 people authored Jun 2, 2024
1 parent b919b52 commit 57ffb26
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
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

0 comments on commit 57ffb26

Please sign in to comment.