Skip to content
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

Middleware context extension doesn't work with app.on #3795

Closed
bdashore3 opened this issue Jan 1, 2025 · 1 comment · Fixed by #3802
Closed

Middleware context extension doesn't work with app.on #3795

bdashore3 opened this issue Jan 1, 2025 · 1 comment · Fixed by #3802
Labels

Comments

@bdashore3
Copy link

What version of Hono are you using?

4.6.14

What runtime/platform is your app running on? (with version if possible)

Deno (npm package)

What steps can reproduce the bug?

Following the Documentation, I'm adding a variable into the context via middleware. When I use a normal app.get or app.post function, there are no errors. However, when I use app.on for multiple routes in one function, Typescript errors stating the types c and c are incompatible.

I've attached a minimal reproducible example below.

import { Hono } from "hono";
import { createMiddleware } from "hono/factory";

const app = new Hono();

// Extra vars for context
interface CtxOptions {
    Variables: {
        testString: string;
    };
}

const messageMiddleware = createMiddleware<CtxOptions>(async (c, next) => {
    console.log("Middleware!");
    c.set("testString", "hello!");
    await next();
});

// No Errors
app.get(
    "/route",
    messageMiddleware,
    (c) => {
        return c.body("Hello!")
    }
)

// Errors on messageMiddleware
app.on(
    "GET",
    ["/route", "/altRoute"],
    messageMiddleware,
    (c) => {
        return c.body("Hello!")
    }
)

What is the expected behavior?

The middleware validates with app.on in the same way as using app.get, app.post, etc.

What do you see instead?

Type error stating that the contexts are incompatible

Check file:///Users/kingbri/Documents/projects/deno-test/main.ts
error: TS2769 [ERROR]: No overload matches this call.
  The last overload gave the following error.
    Argument of type 'MiddlewareHandler<CtxOptions, string, {}>' is not assignable to parameter of type 'H<BlankEnv, any, {}, HandlerResponse<any>>'.
      Type 'MiddlewareHandler<CtxOptions, string, {}>' is not assignable to type 'MiddlewareHandler<BlankEnv, any, {}>'.
        Types of parameters 'c' and 'c' are incompatible.
          Type 'Context<BlankEnv, any, {}>' is not assignable to type 'Context<CtxOptions, string, {}>'.
            Types of property 'get' are incompatible.
              Type 'Get<BlankEnv>' is not assignable to type 'Get<CtxOptions>'.
                Property 'Variables' is missing in type 'BlankEnv' but required in type 'CtxOptions'.
    messageMiddleware,
    ~~~~~~~~~~~~~~~~~
    at file:///Users/kingbri/Documents/projects/deno-test/main.ts:30:5

    'Variables' is declared here.
        Variables: {
        ~~~~~~~~~
        at file:///Users/kingbri/Documents/projects/deno-test/main.ts:8:5TS2771 [ERROR]:     The last overload is declared here.
        at file:///Users/kingbri/Library/Caches/deno/npm/registry.npmjs.org/hono/4.6.14/dist/types/types.d.ts:1211:5

Additional information

No response

@yusukebe
Copy link
Member

yusukebe commented Jan 5, 2025

Hi @bdashore3

Thank you for raising the issue. This was fixed in the new version 4.6.16. Please try it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants