@@ -52,8 +52,8 @@ You can optionally return a response object from the middleware to short-circuit
5252You can also extend the request object with additional properties that can be accessed in the router handlers in a type-safe manner.
5353
5454``` typescript
55- import { TsRestRequest , TsRestResponse } from ' @ts-rest/serverless' ;
56- import { fetchRequestHandler } from ' @ts-rest/serverless/fetch' ;
55+ import { TsRestResponse } from ' @ts-rest/serverless' ;
56+ import { fetchRequestHandler , tsr } from ' @ts-rest/serverless/fetch' ;
5757import { contract } from ' ./contract' ;
5858
5959export default async (request : Request ) => {
@@ -62,7 +62,9 @@ export default async (request: Request) => {
6262 contract ,
6363 router: {
6464 getPost : async ({ params : { id } }, { request }) => {
65- const post = prisma .post .findUniqueOrThrow ({ where: { id , ownerId: request .userId } });
65+ const post = prisma .post .findUniqueOrThrow ({
66+ where: { id , ownerId: request .userId },
67+ });
6668
6769 return {
6870 status: 200 ,
@@ -72,19 +74,22 @@ export default async (request: Request) => {
7274 },
7375 options: {
7476 requestMiddleware: [
75- ( request : TsRestRequest & { userId: string }) => {
77+ tsr . middleware < { userId: string }>(( request ) => {
7678 if (request .headers .get (' Authorization' )) {
7779 const userId = authenticate (request .headers .get (' Authorization' ));
7880 if (! userId ) {
79- return TsRestResponse .fromJson ({ message: ' Unauthorized' }, { status: 401 });
81+ return TsRestResponse .fromJson (
82+ { message: ' Unauthorized' },
83+ { status: 401 },
84+ );
8085 }
8186 request .userId = userId ;
8287 }
83- },
88+ }) ,
8489 ],
8590 },
8691 });
87- }
92+ };
8893```
8994
9095### Global Response Handlers
@@ -93,8 +98,7 @@ You can set global response handlers by using the `responseHandlers` option. Thi
9398This can be useful for logging, adding headers, etc.
9499
95100``` typescript
96- import { TsRestRequest } from ' @ts-rest/serverless' ;
97- import { fetchRequestHandler } from ' @ts-rest/serverless/fetch' ;
101+ import { fetchRequestHandler , tsr } from ' @ts-rest/serverless/fetch' ;
98102import { contract } from ' ./contract' ;
99103import { router } from ' ./router' ;
100104
@@ -105,9 +109,9 @@ export default async (request: Request) => {
105109 router ,
106110 options: {
107111 requestMiddleware: [
108- ( request : TsRestRequest & { time: Date }) => {
112+ tsr . middleware < { time: Date }>(( request ) => {
109113 request .time = new Date ();
110- },
114+ }) ,
111115 ],
112116 responseHandlers: [
113117 (response , request ) => {
@@ -133,7 +137,7 @@ export default async (request: Request) => {
133137 router: {
134138 getPost: {
135139 middleware: [authenticationMiddleware ],
136- handler : async ({ params : { id } }) => {
140+ handler : async ({ params : { id } }, { request } ) => {
137141 const post = prisma .post .findUniqueOrThrow ({ where: { id , ownerId: request .userId } });
138142
139143 return {
@@ -146,3 +150,54 @@ export default async (request: Request) => {
146150 });
147151}
148152```
153+
154+ If you would like to have different request contexts defined in your global middleware and route-specific middleware,
155+ you can use the ` tsr.routeWithMiddleware() ` helper function.
156+ Please note, that you will need to manually pass the global request middleware context type,
157+ so it would be a good idea to define it outside your router definition and use that everywhere.
158+
159+ ``` typescript
160+ import { fetchRequestHandler , tsr } from ' @ts-rest/serverless/fetch' ;
161+ import { contract } from ' ./contract' ;
162+
163+ type GlobalRequestContext = {
164+ time: Date ;
165+ };
166+
167+ export default async (request : Request ) => {
168+ return fetchRequestHandler ({
169+ request ,
170+ contract ,
171+ router: {
172+ getPost: tsr .routeWithMiddleware (contract .getPost )<
173+ GlobalRequestContext , // <--- this is the global context
174+ { userId : string } // <--- this is the route-level context
175+ >({
176+ middleware : [
177+ (request ) => {
178+ // do authentication
179+ request .userId = ' 123' ;
180+ },
181+ ],
182+ handler : async ({ params : { id } }, { request }) => {
183+ const post = prisma .post .findUniqueOrThrow ({
184+ where: { id , ownerId: request .userId },
185+ });
186+
187+ return {
188+ status: 200 ,
189+ body: post ,
190+ };
191+ },
192+ }),
193+ },
194+ options: {
195+ requestMiddleware: [
196+ tsr .middleware <GlobalRequestContext >((request ) => {
197+ request .time = new Date ();
198+ }),
199+ ],
200+ },
201+ });
202+ };
203+ ```
0 commit comments