Skip to content

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

Closed
@bdashore3

Description

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

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions