diff --git a/.changeset/shiny-actors-scream.md b/.changeset/shiny-actors-scream.md new file mode 100644 index 00000000..5c894291 --- /dev/null +++ b/.changeset/shiny-actors-scream.md @@ -0,0 +1,5 @@ +--- +'@hono/zod-openapi': patch +--- + +fix(zod-openapi): make multiple routes types correct for `hc` diff --git a/packages/zod-openapi/package.json b/packages/zod-openapi/package.json index 34709a69..e69aad1f 100644 --- a/packages/zod-openapi/package.json +++ b/packages/zod-openapi/package.json @@ -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" } -} +} \ No newline at end of file diff --git a/packages/zod-openapi/src/index.ts b/packages/zod-openapi/src/index.ts index f153fe93..063ab46f 100644 --- a/packages/zod-openapi/src/index.ts +++ b/packages/zod-openapi/src/index.ts @@ -200,9 +200,9 @@ export class OpenAPIHono< P extends string = ConvertPathType >( route: R, - handler: RouteHandler, - hook?: RouteHook - ): OpenAPIHono>, BasePath> => { + handler: Handler>>, + hook?: Hook> + ): OpenAPIHono>, BasePath> => { this.openAPIRegistry.registerPath(route) const validators: MiddlewareHandler[] = [] diff --git a/packages/zod-openapi/test/index.test.ts b/packages/zod-openapi/test/index.test.ts index 4777f508..b9ec374b 100644 --- a/packages/zod-openapi/test/index.test.ts +++ b/packages/zod-openapi/test/index.test.ts @@ -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('http://localhost/') + + it('Should return correct URL without type errors', () => { + expect(client.posts.$url().pathname).toBe('/posts') + expect(client.books.$url().pathname).toBe('/books') + }) + }) +}) diff --git a/yarn.lock b/yarn.lock index 8f5d4619..fc56b38a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"