diff --git a/packages/zod-openapi/README.md b/packages/zod-openapi/README.md index dfe94139..902f3560 100644 --- a/packages/zod-openapi/README.md +++ b/packages/zod-openapi/README.md @@ -4,10 +4,6 @@ _Note: This is not standalone middleware but is hosted on the monorepo "[github.com/honojs/middleware](https://github.com/honojs/middleware)"._ -## Limitations - -- An instance of Zod OpenAPI Hono cannot be used as a "subApp" in conjunction with `rootApp.route('/api', subApp)`. - ## Usage ### Installation @@ -208,6 +204,47 @@ const appRoutes = app.openapi(route, (c) => { const client = hc('http://localhost:8787/') ``` +## Limitations + +An instance of Zod OpenAPI Hono cannot be used as a "subApp" in conjunction with `rootApp.route('/api', subApp)`. +Use `app.mount('/api', subApp.fetch)` instead. + +```ts +const api = OpenAPIHono() + +// ... + +// Set the `/api` as a base path in the document. +api.get('/doc', (c) => { + const url = new URL(c.req.url) + url.pathname = '/api' + url.search = '' + + return c.json( + // `api.getOpenAPIDocument()` will return a JSON object of the docs. + api.getOpenAPIDocument({ + openapi: '3.0.0', + info: { + version: '1.0.0', + title: 'My API', + }, + servers: [ + { + url: `${url.toString()}`, + }, + ], + }) + ) +}) + +const app = new Hono() + +// Mount the Open API app to `/api` in the main app. +app.mount('/api', api.fetch) + +export default app +``` + ## References - [Hono](https://hono.dev/)