Skip to content

Bug(optimize): Request body API removal affects outside of the handler #64

@yusukebe

Description

@yusukebe

This may be not a real bug, but I had a problem. When I use @hono/mcp in my Hono app, optimize it with the hono optimize command, and deploy the optimized code. But it does not work well now. I investigated and found that the @hono/mcp uses request body APIs in the functions outside of the handler. So, if I added the option --no-request-body-api-removal, it works well.

This is a minimal code to reproduce my problem:

import { type Context, Hono } from 'hono'

const app = new Hono()

const service = async (c: Context) => {
  if (c.req.method !== 'POST') {
    return c.json({ message: 'Only POST requests are allowed' }, 405)
 }
  const receivedData = await c.req.json()
  // do something with receivedData
  return receivedData
}

app.all('/', async (c) => {
  const result = await service(c)
  return c.json({ status: 'success', result })
})

export default app

The repo: https://github.com/yusukebe/hono-cli-request-body-api-removal-bug

There will likely be some similar cases. If the user encounters this problem, it's hard to identify the cause and solution. So, making --no-request-body-api-removal as default will be a good idea, I think.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions