Skip to content

Commit

Permalink
fix(output): don't allow empty outputs
Browse files Browse the repository at this point in the history
Outputs will always have at least one response, requests cannot respond to nothing so now outputs do
not allow empty responses

BREAKING CHANGE: Any empty outputs will now fail type check. Any endpoints MUST now have at least
one key in the output object
  • Loading branch information
khaosdoctor committed Mar 31, 2024
1 parent 076268c commit 25dc301
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
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

0 comments on commit 25dc301

Please sign in to comment.