markusahlstrand / tsoa-hono

A library that integrates the hono router with tsoa
MIT License
2 stars 2 forks source link

How to access Cloudflare Worker Bindings? #8

Open hrueger opened 6 months ago

hrueger commented 6 months ago

Hi @markusahlstrand, I'm currently building an api and looking for a framework. tsoa + tsoa-hono looks awesome! I just cannot figure out how to access Bindings for deploying to Cloudflare Workers... Is there an example on how to do this? Specifically, I'd like to talk to a D1 database. Thanks in advance!

Best Hannes

hrueger commented 6 months ago

Never mind, I figured it out by looking at the code of https://github.com/authhero/auth. For anyone interested:

const server = { async fetch(request: Request, env: Env, ctx: ExecutionContext) { const adapter = new PrismaD1(env.D1_MAIN); return app.fetch( request, { ...env, prisma: new PrismaClient({ adapter }) }, ctx, ); }, };

export default server;


everything attached to the env here can be used by adding `@Request() request: RequestWithContext` as a parameter to your route handler. Now, you can use `request.ctx.env.prisma`.
hrueger commented 6 months ago

@markusahlstrand I had to patch tsoa-hono since wrangler complained about now knowing about Buffer here:

[ERROR] ReferenceError: Buffer is not defined

nodejs_compat was enabled. Maybe you want to include this? tsoa-hono-npm-1.2.1-04da50d363.patch

markusahlstrand commented 6 months ago

Hi! Yes, it's on my todo list to see if we somehow can remove that dependency..

hrueger commented 6 months ago

Great to know, thanks! It works fine with the patch though, so no worries.