WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Export the type of the handler of app.openapi() for better code organization #1492

@lucafaggianelli

Description

@lucafaggianelli

Which middleware is the feature for?

@hono/zod-openapi

What is the feature you are proposing?

I would like to define routes and handlers in separate files for better organization and register them in a central file where I declare the app object.

Let's say I have the file routes/shorturls.ts:

export const createShortUrlRoute = createRoute({})

// What's the type of createShortUrlHandler ?
export const createShortUrlHandler = async (c) => {
  const data = c.req.valid('json');
}

and the main file index.ts where I declare the app and I register the routes:

import { OpenAPIHono } from "@hono/zod-openapi";

export const app = new OpenAPIHono();

// I would like to register the routes here
app.openapi(createShortUrlRoute, createShortUrlHandler)

The problem is the type of createShortUrlHandler, if I use a normal Handler type, I loose the request and response validation types, I tried also to reuse the types declared in the sourcecode of @hono/zod-openapi but basically I need to copy 100s of lines of code.

Proposed solution

Can you expose the type of the handler so I can do something like this:

import { OpenAPIHandler } from "@hono/zod-openapi";

export const createShortUrlRoute = createRoute({})

export const createShortUrlHandler: OpenAPIHandler<typeof createShortUrlRoute> = async (c) => {
  const data = c.req.valid('json');
}

Actual solution

Today I have my OpenAPIHono app defined in a dedicated app.ts file and I have multiple route files that import app and register the routes and finally all route files are imported into index.ts as import './routes/shorturls.ts'

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions