-
-
Notifications
You must be signed in to change notification settings - Fork 469
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
Context method to send request to self #1677
Comments
Hi, @nicksrandall I think we can do it with this code: app.get('/api', (c) => {
return c.json({ foo: 'bar' })
})
app.get('/', async (c) => {
const res = await app.request('/api')
return c.json(await res.json())
}) |
Thanks for responding @yusukebe As I understand it, that code does not keep track of the execution context or environment variables so it doesn't work for my use-case. |
@yusukebe to be more specific, the code you suggest does work for very simple use-cases but it doesn't not work if the API route is using ENV variables or execution context (in a cloudflare worker). That is why I think it makes sense for a new method to live on the "context" because we want to execute the handler with the current context. While I think If we made that change, your example would look something like this: app.get('/api', (c) => {
return c.json({ foo: 'bar' })
})
app.get('/', async (c) => {
const res = await c.request('/api')
return c.json(await res.json())
}) Again thanks for your help with this! |
I'm not run it yet, but I think this would work with app.get('/', (c) => {
app.request('/api', c.req.raw, c.env)
return c.json(0)
}) |
@yusukebe Your suggestion does pass provides the env variables but it still doesn't pass the execution context. |
Hi. You can also pass the execution context like the following: app.request('/api', c.req.raw, c.env, c.executionCtx) |
That works, however, this error is thrown in the console:
|
That warning is annoying, but you can ignore it. |
Sometimes in a view route, I want to call an another route handler to get data. It would be awesome if Hono has this built in.
Runtimes like Cloudflare workers do not allow workers to "fetch" themselves over the network so this would be a nice work around that would allow hono to skip the network entirely and just call itself.
Proposed API
I'd be happy to send a PR if maintainers are open to this change.
Originally posted by @nicksrandall in #1675
The text was updated successfully, but these errors were encountered: