Skip to content

Commit fb42b94

Browse files
bestickleyGabrola
andauthored
feat(fastify): add contract route to fastify route config (#625)
Co-authored-by: Youssef Gaber <1728215+Gabrola@users.noreply.github.com>
1 parent b1fb60c commit fb42b94

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

.changeset/metal-pumpkins-grin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ts-rest/fastify': minor
3+
---
4+
5+
Add ts-rest route to fastify config object

libs/ts-rest/fastify/src/lib/ts-rest-fastify.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ import {
1515
} from '@ts-rest/core';
1616
import * as fastify from 'fastify';
1717
import { z } from 'zod';
18+
import {
19+
RawServerDefault,
20+
RawRequestDefaultExpression,
21+
FastifySchema,
22+
FastifyTypeProviderDefault,
23+
RouteGenericInterface,
24+
RawReplyDefaultExpression,
25+
} from 'fastify';
1826

1927
export class RequestValidationError extends Error {
2028
constructor(
@@ -29,8 +37,21 @@ export class RequestValidationError extends Error {
2937

3038
type AppRouteImplementation<T extends AppRoute> = (
3139
input: ServerInferRequest<T, fastify.FastifyRequest['headers']> & {
32-
request: fastify.FastifyRequest;
33-
reply: fastify.FastifyReply;
40+
request: fastify.FastifyRequest<
41+
RouteGenericInterface,
42+
RawServerDefault,
43+
RawRequestDefaultExpression,
44+
FastifySchema,
45+
FastifyTypeProviderDefault,
46+
{ tsRestRoute: T }
47+
>;
48+
reply: fastify.FastifyReply<
49+
RawServerDefault,
50+
RawRequestDefaultExpression,
51+
RawReplyDefaultExpression,
52+
RouteGenericInterface,
53+
{ tsRestRoute: T }
54+
>;
3455
appRoute: T;
3556
},
3657
) => Promise<ServerInferResponses<T>>;
@@ -209,6 +230,9 @@ const registerRoute = <TAppRoute extends AppRoute>(
209230
fastify.route({
210231
method: appRoute.method,
211232
url: appRoute.path,
233+
config: {
234+
tsRestRoute: appRoute,
235+
},
212236
handler: async (request, reply) => {
213237
const validationResults = validateRequest(
214238
request,

0 commit comments

Comments
 (0)