( options: DefineRouteOptions<TBody, TParams, TQuery, TSuccessData>, handler: RouteDefinition<TBody, TParams, TQuery>['handler'], )
| 231 | * app verb, e.g. `app.post(route.path, route.options, route.handler)`. |
| 232 | */ |
| 233 | export function defineRoute< |
| 234 | TBody extends z.ZodTypeAny | undefined, |
| 235 | TParams extends z.ZodTypeAny | undefined, |
| 236 | TQuery extends z.ZodTypeAny | undefined, |
| 237 | TSuccessData extends z.ZodTypeAny | undefined, |
| 238 | >( |
| 239 | options: DefineRouteOptions<TBody, TParams, TQuery, TSuccessData>, |
| 240 | handler: RouteDefinition<TBody, TParams, TQuery>['handler'], |
| 241 | ): RouteDefinition<TBody, TParams, TQuery> { |
| 242 | // -- runtime validators ---------------------------------------------------- |
| 243 | const preHandler: unknown[] = []; |
| 244 | |
| 245 | if (options.params) { |
| 246 | preHandler.push(validateParams(options.params)); |
| 247 | } |
| 248 | if (options.body) { |
| 249 | preHandler.push(validateBody(options.body)); |
| 250 | } |
| 251 | if (options.querystring) { |
| 252 | preHandler.push(validateQuery(options.querystring)); |
| 253 | } |
| 254 | |
| 255 | // -- swagger schema -------------------------------------------------------- |
| 256 | const schema: Record<string, unknown> = {}; |
| 257 | |
| 258 | if (options.body) { |
| 259 | schema['body'] = jsonSchema(options.body); |
| 260 | } |
| 261 | if (options.params) { |
| 262 | schema['params'] = jsonSchema(options.params); |
| 263 | } |
| 264 | if (options.querystring) { |
| 265 | schema['querystring'] = jsonSchema(options.querystring); |
| 266 | } |
| 267 | |
| 268 | const hasResponse = |
| 269 | options.success !== undefined || |
| 270 | (options.errors !== undefined && Object.keys(options.errors).length > 0) || |
| 271 | options.rawResponse !== undefined; |
| 272 | |
| 273 | if (hasResponse) { |
| 274 | const responses: Record<string, unknown> = {}; |
| 275 | |
| 276 | if (options.success || options.errors) { |
| 277 | responses['200'] = buildUnifiedResponseSchema( |
| 278 | options.success?.data ?? z.null(), |
| 279 | options.errors ?? {}, |
| 280 | ); |
| 281 | } |
| 282 | |
| 283 | if (options.rawResponse) { |
| 284 | for (const [code, rawSchema] of Object.entries(options.rawResponse)) { |
| 285 | responses[String(code)] = rawSchema; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | schema['response'] = responses; |
| 290 | } |
no test coverage detected