Skip to content

Commit

Permalink
fix(zod-openapi): update RouteHandler type to support MaybePromise (#529
Browse files Browse the repository at this point in the history
)

* fix(zod-openapi): update RouteHandler type to support MaybePromise

* add .changeset/cuddly-pets-drum.md
  • Loading branch information
akineko authored May 18, 2024
1 parent cd0a2ce commit 0a43d2c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-pets-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---

fix(zod-openapi): update RouteHandler type to support MaybePromise
4 changes: 2 additions & 2 deletions packages/zod-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ export type RouteHandler<
}
}
}
? RouteConfigToTypedResponse<R>
: RouteConfigToTypedResponse<R> | Response | Promise<Response>
? MaybePromise<RouteConfigToTypedResponse<R>>
: MaybePromise<RouteConfigToTypedResponse<R>> | MaybePromise<Response>
>

export type RouteHook<
Expand Down
41 changes: 31 additions & 10 deletions packages/zod-openapi/test/handler.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { RouteHandler } from '../src'
import { OpenAPIHono, createRoute, z } from '../src'

test('supports async handler', () => {
const hono = new OpenAPIHono()

describe('supports async handler', () => {
const route = createRoute({
method: 'get',
path: '/users',
Expand All @@ -20,15 +19,37 @@ test('supports async handler', () => {
},
})

hono.openapi(route, (c) => {
return c.json({
id: '123',
test('argument of openapi method', () => {
const hono = new OpenAPIHono()

hono.openapi(route, (c) => {
return c.json({
id: '123',
})
})
})

hono.openapi(route, async (c) => {
return c.json({
id: '123',
hono.openapi(route, async (c) => {
return c.json({
id: '123',
})
})
})

test('RouteHandler type', () => {
const handler: RouteHandler<typeof route> = (c) => {
return c.json({
id: '123',
})
}

const asyncHandler: RouteHandler<typeof route> = async (c) => {
return c.json({
id: '123',
})
}

const hono = new OpenAPIHono()
hono.openapi(route, handler)
hono.openapi(route, asyncHandler)
})
})

0 comments on commit 0a43d2c

Please sign in to comment.