Skip to content

Commit

Permalink
fix(zod-openapi): support a base path (#609)
Browse files Browse the repository at this point in the history
* fix(zod-openapi): support a base path

* add changeset
  • Loading branch information
yusukebe authored Jul 1, 2024
1 parent 51813f6 commit b06bde6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-shoes-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---

fix: support a base path
30 changes: 24 additions & 6 deletions packages/zod-openapi/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
import type {
ResponseConfig,
RouteConfig as RouteConfigBase,
ZodContentObject,
ZodMediaTypeObject,
Expand Down Expand Up @@ -37,7 +36,7 @@ import type {
SuccessStatusCode,
} from 'hono/utils/http-status'
import { mergePath } from 'hono/utils/url'
import type { AnyZodObject, ZodError, ZodSchema } from 'zod'
import type { ZodError, ZodSchema } from 'zod'
import { ZodType, z } from 'zod'

type MaybePromise<T> = Promise<T> | T
Expand Down Expand Up @@ -405,16 +404,22 @@ export class OpenAPIHono<
return this
}

getOpenAPIDocument = (config: OpenAPIObjectConfig) => {
getOpenAPIDocument = (
config: OpenAPIObjectConfig
): ReturnType<typeof generator.generateDocument> => {
const generator = new OpenApiGeneratorV3(this.openAPIRegistry.definitions)
const document = generator.generateDocument(config)
return document
// @ts-expect-error the _basePath is a private property
return this._basePath ? addBasePathToDocument(document, this._basePath) : document
}

getOpenAPI31Document = (config: OpenAPIObjectConfig) => {
getOpenAPI31Document = (
config: OpenAPIObjectConfig
): ReturnType<typeof generator.generateDocument> => {
const generator = new OpenApiGeneratorV31(this.openAPIRegistry.definitions)
const document = generator.generateDocument(config)
return document
// @ts-expect-error the _basePath is a private property
return this._basePath ? addBasePathToDocument(document, this._basePath) : document
}

doc = <P extends string>(
Expand Down Expand Up @@ -533,3 +538,16 @@ export const createRoute = <P extends string, R extends Omit<RouteConfig, 'path'

extendZodWithOpenApi(z)
export { z }

function addBasePathToDocument(document: Record<string, any>, basePath: string) {
const updatedPaths: Record<string, any> = {}

Object.keys(document.paths).forEach((path) => {
updatedPaths[mergePath(basePath, path)] = document.paths[path]
})

return {
...document,
paths: updatedPaths,
}
}
7 changes: 7 additions & 0 deletions packages/zod-openapi/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,13 @@ describe('basePath()', () => {

expect(client.api.message.$url().pathname).toBe('/api/message')
})

it('Should add the base path to paths', async () => {
const res = await app.request('/api/doc')
expect(res.status).toBe(200)
const data = (await res.json()) as any
expect(Object.keys(data.paths)[0]).toBe('/api/message')
})
})

describe('With hc', () => {
Expand Down

0 comments on commit b06bde6

Please sign in to comment.