HonoH
Honoβ€’7mo ago
codewansh

@hono/zod-openapi Middleware Issue

Hi there,
I need some help in implementing middleware while using hono and @hono/zod-openapi.
What I want to implement is running middleware after zod validation of query params but before the actual route handler.

My code is as follows:

This is the route code
import { createRouter } from "@/lib/create-app";
import { authMiddleware } from "@/middlewares/auth";

import * as handlers from "./state.handlers";
import * as routes from "./state.routes";

const router = createRouter();
router.openapi(routes.list, handlers.list);

export default router;


This is route initialization code
import { OpenAPIHono } from "@hono/zod-openapi";
import { notFound, onError, serveEmojiFavicon } from "stoker/middlewares";
import { defaultHook } from "stoker/openapi";
import type { AppBindings, AppOpenAPI } from "./types";

export function createRouter() {
  return new OpenAPIHono<AppBindings>({
    strict: false,
    defaultHook,
  });
}

export default function createApp() {
  const app = createRouter();
  app.use(serveEmojiFavicon("πŸ“"));
  app.use("/api/*", cors());

  app.notFound(notFound);
  app.onError(onError);
  return app;
}
Was this page helpful?