Skip to content

Commit

Permalink
fix(zod-openapi): multiple routes types correct for hc (#176)
Browse files Browse the repository at this point in the history
* fix(zod-openapi): multiple routes types correct for `hc`

* changeset
  • Loading branch information
yusukebe authored Sep 25, 2023
1 parent fd4aa87 commit fb63ef4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-actors-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---

fix(zod-openapi): make multiple routes types correct for `hc`
5 changes: 3 additions & 2 deletions packages/zod-openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -41,4 +42,4 @@
"engines": {
"node": ">=16.0.0"
}
}
}
6 changes: 3 additions & 3 deletions packages/zod-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = []
Expand Down
44 changes: 44 additions & 0 deletions packages/zod-openapi/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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')
})
})
})
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit fb63ef4

Please sign in to comment.