Write middlewares and handlers once, target Hono, Express, Cloudflare, Hattip, Webroute, Fastify, h3, Elysia Vercel , (and more on the way!).
Learn more on the Documentation
A middleware that returns an early response if some header is missing.
// src/middlewares/demo.middleware.ts
import type { Get, UniversalMiddleware } from "@universal-middleware/core";
interface Config {
header: string;
}
// This middleware will return an early response if given header is missing
const guardMiddleware = ((config) => (request, ctx) => {
if (!request.headers.has(config.header)) {
return new Response("Header not present", {
status: 401,
});
}
// else we do nothing
// Using `satisfies` to not lose return type
}) satisfies Get<[Config], UniversalMiddleware>;
// export default is mandatory
export default guardMiddleware;
After bundling and publishing, this would be used like this:
// hono-entry.ts
import { Hono } from "hono";
// hattip users would use "some-lib/middlewares/demo-middleware-hattip"
// express users would use "some-lib/middlewares/demo-middleware-express"
// etc.
import demoMiddleware from "some-lib/middlewares/demo-middleware-hono";
const app = new Hono();
app.use(demoMiddleware({ header: 'X-Universal-Demo' }));
app.get("/", () => new Response('ok')));
export default app;