Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zod-openapi): extract range definitions to StatusCode #535

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/plenty-tables-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-openapi': minor
---

extract range definitions to StatusCode
32 changes: 27 additions & 5 deletions packages/zod-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ import type {
TypedResponse,
} from 'hono'
import type { MergePath, MergeSchemaPath } from 'hono/types'
import type { StatusCode } from 'hono/utils/http-status'
import type {
ClientErrorStatusCode,
InfoStatusCode,
RedirectStatusCode,
ServerErrorStatusCode,
StatusCode,
SuccessStatusCode,
} from 'hono/utils/http-status'
import type { Prettify, RemoveBlankRecord } from 'hono/utils/types'
import { mergePath } from 'hono/utils/url'
import type { AnyZodObject, ZodSchema, ZodError } from 'zod'
Expand Down Expand Up @@ -143,13 +150,28 @@ type ExtractContent<T> = T extends {
: never
: never

type StatusCodeRangeDefinitions = {
'1XX': InfoStatusCode
'2XX': SuccessStatusCode
'3XX': RedirectStatusCode
'4XX': ClientErrorStatusCode
'5XX': ServerErrorStatusCode
}
type RouteConfigStatusCode = keyof StatusCodeRangeDefinitions | StatusCode
type ExtractStatusCode<T extends RouteConfigStatusCode> = T extends keyof StatusCodeRangeDefinitions
? StatusCodeRangeDefinitions[T]
: T
export type RouteConfigToTypedResponse<R extends RouteConfig> = {
[Status in keyof R['responses'] & StatusCode]: IsJson<
[Status in keyof R['responses'] & RouteConfigStatusCode]: IsJson<
keyof R['responses'][Status]['content']
> extends never
? TypedResponse<{}, Status, string>
: TypedResponse<ExtractContent<R['responses'][Status]['content']>, Status, 'json'>
}[keyof R['responses'] & StatusCode]
? TypedResponse<{}, ExtractStatusCode<Status>, string>
: TypedResponse<
ExtractContent<R['responses'][Status]['content']>,
ExtractStatusCode<Status>,
'json'
>
}[keyof R['responses'] & RouteConfigStatusCode]

export type Hook<T, E extends Env, P extends string, R> = (
result:
Expand Down
28 changes: 26 additions & 2 deletions packages/zod-openapi/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { hc } from 'hono/client'
import { describe, it, expect, expectTypeOf } from 'vitest'
import { OpenAPIHono, createRoute, z, RouteConfigToTypedResponse } from '../src/index'
import { Expect, Equal } from 'hono/utils/types'
import { ServerErrorStatusCode } from 'hono/utils/http-status'

describe('Constructor', () => {
it('Should not require init object', () => {
Expand Down Expand Up @@ -200,11 +201,19 @@ describe('Basic - params', () => {
responses: {
'200': {
description: 'Get the user',
content: { 'application/json': { schema: { $ref: '#/components/schemas/User' } } },
content: {
'application/json': {
schema: { $ref: '#/components/schemas/User' },
},
},
},
'400': {
description: 'Error!',
content: { 'application/json': { schema: { $ref: '#/components/schemas/Error' } } },
content: {
'application/json': {
schema: { $ref: '#/components/schemas/Error' },
},
},
},
},
},
Expand Down Expand Up @@ -1465,6 +1474,14 @@ describe('RouteConfigToTypedResponse', () => {
},
description: 'Error!',
},
'5XX': {
content: {
'application/json': {
schema: ErrorSchema,
},
},
description: 'Server Error!',
},
},
}

Expand All @@ -1486,6 +1503,13 @@ describe('RouteConfigToTypedResponse', () => {
400,
'json'
>
| TypedResponse<
{
ok: boolean
},
ServerErrorStatusCode,
'json'
>
type verify = Expect<Equal<Expected, Actual>>
})
})