Investigat type composition of middlewares

Open
#3,484 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
30/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
deno, typescript

Research direction

Start by reading the linked Hono middleware example and Fresh issue #414, then locate Fresh's middleware and route handler type definitions. Determine whether middleware types can compose so later handlers see variables added by earlier middleware; done means the expected types are available across a middleware chain and covered by type tests. The issue has had no activity for 389 days, so check whether the proposal is still relevant.

Written by the indexing model from the issue text.

Description

feat

A user on discord mentioned that Hono has figured that out: https://discord.com/channels/684898665143206084/991511118524715139/1420356127610110044

It's not clearly documented but here's a minimal example that compiles. Could potentially be improved upon if it was more than a neat type hack.

import { Hono } from "jsr:@hono/[email protected]";
import { createMiddleware } from "jsr:@hono/[email protected]/factory";

const fooMiddleware = createMiddleware<{
    Variables: {
        foo: number;
    };
}>(async (c, next) => {
    c.set("foo", 12);
    await next();
});

const barMiddleware = createMiddleware<{
    Variables: {
        bar: string;
    };
}>(async (c, next) => {
    c.set("bar", "bar");

    await next();
});

const app = new Hono();

app.get("/", fooMiddleware, barMiddleware, (c) => {
    console.log(c.var.foo); 
    console.log(c.var.bar);
    return c.text("");
});

I discovered it by chance looking to do dependency injection via this issue
https://github.com/honojs/hono/issues/414

Dominant language
TypeScript
Stars
13.8k
Forks
757
Avg merge
3m
Merged PRs (30d)
2

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from freshframework/fresh

All issues in freshframework/fresh

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.