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

fix(output): don't allow empty outputs #24

Merged
merged 1 commit into from
Mar 31, 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
6 changes: 3 additions & 3 deletions src/lib/create-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type ResponseObject,
} from 'openapi3-ts/oas31'
import { type ZodObject, type ZodTypeAny, type z } from 'zod'
import { type OneOrMore, type ValueOf } from '../types'
import { type NonEmptyObj, type OneOrMore, type ValueOf } from '../types'

export interface ResponseDefinition {
body: ZodTypeAny
Expand Down Expand Up @@ -52,7 +52,7 @@ export type Endpoint<
handlers: EndpointParams<RequestBody, Params, Query, ResponseBodies>['handlers']
errorHandler?: EndpointParams<RequestBody, Params, Query, ResponseBodies>['errorHandler']
input?: EndpointParams<RequestBody, Params, Query, ResponseBodies>['input']
output: ResponseBodies
output: EndpointParams<RequestBody, Params, Query, ResponseBodies>['output']
}

/**
Expand All @@ -77,7 +77,7 @@ export type EndpointParams<
query?: ZodObject<any, 'strip', ZodTypeAny, Query, any>
headers?: HeadersObject
}
output: ResponseBodies
output: NonEmptyObj<ResponseBodies, 'Output cannot be empty'>
handlers: OneOrMore<Handler<RequestBody, Params, Query, ResponseBodies>>
errorHandler?: ErrorHandler<RequestBody, Params, Query, ResponseBodies>
}
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export type OneOrMore<T> = T | [T, ...T[]]
export type ValueOf<T> = T[keyof T]
export type NonEmptyObj<T extends Record<string, unknown>, Message extends string = 'Object cannot be empty'> =
T extends Record<string, never> ? Message : T
18 changes: 15 additions & 3 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ describe('create-app', () => {
throw new Error()
},
],
output: {},
output: {
200: {
body: z.any(),
},
},
}),
},
},
Expand Down Expand Up @@ -349,15 +353,23 @@ describe('create-app', () => {
throw new Error('Error from get')
},
],
output: {},
output: {
200: {
body: z.any(),
},
},
}),
post: createEndpoint({
handlers: [
async () => {
throw new Error('Error from post')
},
],
output: {},
output: {
200: {
body: z.any(),
},
},
}),
},
},
Expand Down
Loading