@@ -142,13 +142,15 @@ One powerful use case for metadata is defining required permissions for routes a
142142Here's how to implement permission-based authorization:
143143
144144``` ts
145+ import { type MiddlewareFunction } from ' next-zod-route' ;
146+
145147// Define a schema for permissions metadata
146148const permissionsMetadataSchema = z .object ({
147149 requiredPermissions: z .array (z .string ()).optional (),
148150});
149151
150152// Create a middleware that checks permissions
151- const permissionCheckMiddleware = async ({ next , metadata , request }) => {
153+ const permissionCheckMiddleware: MiddlewareFunction = async ({ next , metadata , request }) => {
152154 // Get user permissions from auth header, token, or session
153155 const userPermissions = getUserPermissions (request );
154156
@@ -219,10 +221,13 @@ This pattern allows you to:
219221You can add middleware to your route handler with the ` use ` method. Middleware functions can add data to the context that will be available in your handler.
220222
221223``` ts
222- const loggingMiddleware = async ({ next }) => {
224+ import { type MiddlewareFunction , createZodRoute } from ' next-zod-route' ;
225+
226+ const loggingMiddleware: MiddlewareFunction = async ({ next }) => {
223227 console .log (' Before handler' );
224228 const startTime = performance .now ();
225229
230+ // next() returns a Promise<Response>
226231 const response = await next ();
227232
228233 const endTime = performance .now () - startTime ;
@@ -231,7 +236,7 @@ const loggingMiddleware = async ({ next }) => {
231236 return response ;
232237};
233238
234- const authMiddleware = async ({ request , metadata , next }) => {
239+ const authMiddleware: MiddlewareFunction = async ({ request , metadata , next }) => {
235240 try {
236241 // Get the token from the request headers
237242 const token = request .headers .get (' authorization' )?.split (' ' )[1 ];
@@ -245,8 +250,9 @@ const authMiddleware = async ({ request, metadata, next }) => {
245250 const user = await validateToken (token );
246251
247252 // Add context & continue chain
253+ // next() accepts an optional object with a context property
248254 const response = await next ({
249- context: { user },
255+ context: { user }, // This context will be merged with existing context
250256 });
251257
252258 // You can modify the response after the handler
@@ -263,7 +269,7 @@ const authMiddleware = async ({ request, metadata, next }) => {
263269 }
264270};
265271
266- const permissionsMiddleware = async ({ metadata , next }) => {
272+ const permissionsMiddleware: MiddlewareFunction = async ({ metadata , next }) => {
267273 // Metadata are optional and type-safe
268274 const response = await next ({
269275 context: { permissions: metadata ?.permissions ?? [' read' ] },
@@ -311,7 +317,9 @@ The middleware can:
311317#### Pre/Post Handler Execution
312318
313319``` ts
314- const timingMiddleware = async ({ next }) => {
320+ import { type MiddlewareFunction } from ' next-zod-route' ;
321+
322+ const timingMiddleware: MiddlewareFunction = async ({ next }) => {
315323 console .log (' Starting request...' );
316324 const start = performance .now ();
317325
@@ -327,7 +335,9 @@ const timingMiddleware = async ({ next }) => {
327335#### Response Modification
328336
329337``` ts
330- const headerMiddleware = async ({ next }) => {
338+ import { type MiddlewareFunction } from ' next-zod-route' ;
339+
340+ const headerMiddleware: MiddlewareFunction = async ({ next }) => {
331341 const response = await next ();
332342
333343 return new Response (response .body , {
@@ -343,14 +353,16 @@ const headerMiddleware = async ({ next }) => {
343353#### Context Chaining
344354
345355``` ts
346- const middleware1 = async ({ next }) => {
356+ import { type MiddlewareFunction } from ' next-zod-route' ;
357+
358+ const middleware1: MiddlewareFunction = async ({ next }) => {
347359 const response = await next ({
348360 context: { value1: ' first' },
349361 });
350362 return response ;
351363};
352364
353- const middleware2 = async ({ context , next }) => {
365+ const middleware2: MiddlewareFunction = async ({ context , next }) => {
354366 // Access previous context
355367 console .log (context .value1 ); // 'first'
356368
@@ -364,7 +376,9 @@ const middleware2 = async ({ context, next }) => {
364376#### Early Returns
365377
366378``` ts
367- const authMiddleware = async ({ next }) => {
379+ import { type MiddlewareFunction } from ' next-zod-route' ;
380+
381+ const authMiddleware: MiddlewareFunction = async ({ next }) => {
368382 const isAuthed = false ;
369383
370384 if (! isAuthed ) {
@@ -400,7 +414,9 @@ const route = createZodRoute()
400414#### After (v0.2.0)
401415
402416``` typescript
403- const authMiddleware = async ({ next }) => {
417+ import { type MiddlewareFunction } from ' next-zod-route' ;
418+
419+ const authMiddleware: MiddlewareFunction = async ({ next }) => {
404420 // Execute code before handler
405421 console .log (' Checking auth...' );
406422
0 commit comments