honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
18.51k stars 522 forks source link

WorkerEntrypoint ergonomic handler support #3182

Open GregBrimble opened 1 month ago

GregBrimble commented 1 month ago

What is the feature you are proposing?

Hey! Love that I can just do export default { fetch: app.fetch } for ESM Workers. What's the ergonomic equivalent for WorkerEntrypoints?

I currently define them like:

import { WorkerEntrypoint } from "cloudflare:workers";

export default class extends WorkerEntrypoint {
  fetch(request) {
    return app.fetch(request, this.env, this.ctx);
  }
}

Is there some magic we'd consider adding so I can just do this?

import { WorkerEntrypoint } from "cloudflare:workers";

export default class extends WorkerEntrypoint {
  fetch = app.entrypointFetch
}
yusukebe commented 1 month ago

Hi @GregBrimble

The following works well.

import { Hono } from 'hono'
import { WorkerEntrypoint } from 'cloudflare:workers'

const app = new Hono()

app.get('/', (c) => {
  return c.text('Hello Hono!')
})

export default class extends WorkerEntrypoint {
  fetch = app.fetch
}

Do you think about this? I think this is the smartest way.

GregBrimble commented 1 month ago

Unfortunately not. WorkerEntrypoint's fetch signature is just fetch(request: Request). It doesn't come with the env or ctx parameters that app.fetch expects. These are instead available on the WorkerEntrypoint instance, this.

yusukebe commented 1 month ago

@GregBrimble Ahh, understood. I’ll consider it.

lucaspieran commented 1 week ago

@yusukebe and how you call the method or endpoint in the other worker?