fix(zod-openapi): multiple routes types correct for `hc` (#176)

* fix(zod-openapi): multiple routes types correct for `hc`

* changeset
pull/178/head
Yusuke Wada 2023-09-25 12:57:51 +09:00 committed by GitHub
parent fd4aa8754e
commit fb63ef413c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 9 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---
fix(zod-openapi): make multiple routes types correct for `hc`

View File

@ -31,7 +31,8 @@
"zod": "3.*"
},
"devDependencies": {
"hono": "^3.6.3",
"@hono/zod-validator": "^0.1.8",
"hono": "^3.7.2",
"zod": "^3.22.1"
},
"dependencies": {
@ -41,4 +42,4 @@
"engines": {
"node": ">=16.0.0"
}
}
}

View File

@ -200,9 +200,9 @@ export class OpenAPIHono<
P extends string = ConvertPathType<R['path']>
>(
route: R,
handler: RouteHandler<R, E, I, P>,
hook?: RouteHook<R, E, I, P>
): OpenAPIHono<E, ToSchema<R['method'], P, I['in'], OutputType<R>>, BasePath> => {
handler: Handler<E, P, I, HandlerResponse<OutputType<R>>>,
hook?: Hook<I, E, P, OutputType<R>>
): OpenAPIHono<E, S & ToSchema<R['method'], P, I['in'], OutputType<R>>, BasePath> => {
this.openAPIRegistry.registerPath(route)
const validators: MiddlewareHandler[] = []

View File

@ -1,6 +1,7 @@
/* eslint-disable node/no-extraneous-import */
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { Hono, Env, ToSchema } from 'hono'
import { hc } from 'hono/client'
import { describe, it, expect, expectTypeOf } from 'vitest'
import { OpenAPIHono, createRoute, z } from '../src'
@ -714,3 +715,46 @@ describe('Multi params', () => {
})
})
})
describe('With hc', () => {
describe('Multiple routes', () => {
const app = new OpenAPIHono()
const createPostRoute = createRoute({
method: 'post',
path: '/posts',
operationId: 'createPost',
responses: {
200: {
description: 'A post',
},
},
})
const createBookRoute = createRoute({
method: 'post',
path: '/books',
operationId: 'createBook',
responses: {
200: {
description: 'A book',
},
},
})
const routes = app
.openapi(createPostRoute, (c) => {
return c.jsonT(0)
})
.openapi(createBookRoute, (c) => {
return c.jsonT(0)
})
const client = hc<typeof routes>('http://localhost/')
it('Should return correct URL without type errors', () => {
expect(client.posts.$url().pathname).toBe('/posts')
expect(client.books.$url().pathname).toBe('/books')
})
})
})

View File

@ -5864,10 +5864,10 @@ hono@^3.5.8:
resolved "https://registry.yarnpkg.com/hono/-/hono-3.5.8.tgz#9bbc412f5a54183cf2a81a36a9b9ea56da10f785"
integrity sha512-ZipTmGfHm43q5QOEBGog2wyejyNUcicjPt0BLDQ8yz9xij/y9RYXRpR1YPxMpQqeyNM7isvpsIAe9Ems51Wq0Q==
hono@^3.6.3:
version "3.6.3"
resolved "https://registry.yarnpkg.com/hono/-/hono-3.6.3.tgz#0dab94a9e49dadc0f99bf8b8ffc70b223f53ab9f"
integrity sha512-8WszeHGzUm45qJy2JcCXkEFXMsAysciGGQs+fbpdUYPO2bRMbjJznZE3LX8tCXBqR4f/3e6225B3YOX6pQZWvA==
hono@^3.7.2:
version "3.7.2"
resolved "https://registry.yarnpkg.com/hono/-/hono-3.7.2.tgz#c3839d7ffbb5120850b2b926363d065020f4d18c"
integrity sha512-5SWYrAQJlfjHggcDTnmKZd5zlUEXmoUiBjnmL6C1W8MX39/bUw6ZIvfEJZgpo7d7Z/vCJ5FRfkjIQPRH5yV/dQ==
hosted-git-info@^2.1.4:
version "2.8.9"