Skip to content

Commit eff4745

Browse files
committed
add return type
1 parent 83e2eb0 commit eff4745

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/routeHandlerBuilder.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class RouteHandlerBuilder<
3131
// eslint-disable-next-line @typescript-eslint/ban-types
3232
TContext = {},
3333
TMetadata extends z.Schema = z.Schema,
34+
TResponse extends z.Schema = z.Schema,
3435
> {
3536
readonly config: {
3637
paramsSchema: TParams;
@@ -49,6 +50,7 @@ export class RouteHandlerBuilder<
4950
querySchema: undefined as unknown as TQuery,
5051
bodySchema: undefined as unknown as TBody,
5152
metadataSchema: undefined as unknown as TMetadata,
53+
responseSchema: undefined as unknown as TResponse,
5254
},
5355
middlewares = [],
5456
handleServerError,
@@ -60,11 +62,13 @@ export class RouteHandlerBuilder<
6062
querySchema: TQuery;
6163
bodySchema: TBody;
6264
metadataSchema?: TMetadata;
65+
responseSchema?: TResponse;
6366
};
6467
middlewares?: Array<MiddlewareFunction<TContext, Record<string, unknown>, z.infer<TMetadata>>>;
6568
handleServerError?: HandlerServerErrorFn;
6669
contextType: TContext;
6770
metadataValue?: z.infer<TMetadata>;
71+
responseSchema?: TResponse;
6872
}) {
6973
this.config = config;
7074
this.middlewares = middlewares;
@@ -109,6 +113,18 @@ export class RouteHandlerBuilder<
109113
});
110114
}
111115

116+
/**
117+
* Define the schema for the response
118+
* @param schema - The schema for the response
119+
* @returns A new instance of the RouteHandlerBuilder
120+
*/
121+
response<T extends z.Schema>(schema: T) {
122+
return new RouteHandlerBuilder<TParams, TQuery, TBody, TContext, TMetadata, T>({
123+
...this,
124+
config: { ...this.config, responseSchema: schema },
125+
});
126+
}
127+
112128
/**
113129
* Define the schema for the metadata
114130
* @param schema - The schema for the metadata

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,5 @@ export type HandlerServerErrorFn = (error: Error) => Response;
9292
* Utility type to extract the return type of a route handler
9393
*/
9494
export type RouteResponse<T> = T extends OriginalRouteHandler<infer R> ? Awaited<R> : never;
95+
96+
export type ZodRouteResponse<T> = Response & { json: () => Promise<T> };

0 commit comments

Comments
 (0)