{"library":"nextjs-middleware-wrappers","title":"Next.js Middleware Wrappers","description":"nextjs-middleware-wrappers is a utility package designed to streamline the composition of middleware functions in Next.js API routes and endpoints. It provides a `wrappers` function that allows developers to chain multiple higher-order middleware functions in a more readable and type-safe manner, contrasting with deeply nested function calls. The package, currently at version 1.3.0, is actively maintained with a structured release process informed by Angular JS commit message conventions, implying regular updates for fixes, features, and potential breaking changes. Its primary differentiator lies in its ability to preserve the input and output types across multiple middleware layers, significantly reducing the common type-mismatch issues encountered when manually composing such functions. This utility aims to improve code clarity and maintainability for applications heavily relying on request-based middleware patterns within the Next.js ecosystem, ensuring that complex middleware logic remains manageable and robust.","language":"javascript","status":"active","last_verified":"Thu Apr 23","install":{"commands":["npm install nextjs-middleware-wrappers"],"cli":null},"imports":["import { wrappers } from 'nextjs-middleware-wrappers';","import { wrappers } from 'nextjs-middleware-wrappers';","import type { NextApiRequest, NextApiResponse } from 'next';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { NextApiRequest, NextApiResponse } from 'next';\nimport { wrappers } from 'nextjs-middleware-wrappers';\n\n// Example middleware: Adds a timestamp to the request object\nconst withTimestamp = (next) => async (req: NextApiRequest, res: NextApiResponse) => {\n  // For demonstration, cast req to any to add custom properties\n  (req as any).timestamp = Date.now();\n  console.log('Middleware: Timestamp added:', (req as any).timestamp);\n  return next(req, res);\n};\n\n// Example middleware: Logs the request method and path with a configurable prefix\nconst withRequestLogger = (logPrefix: string) =>\n  (next) =>\n  async (req: NextApiRequest, res: NextApiResponse) => {\n    console.log(`${logPrefix} GOT REQUEST ${req.method} ${req.url}`);\n    return next(req, res);\n  };\n\n// The actual Next.js API route handler function\nconst myApiHandler = async (req: NextApiRequest, res: NextApiResponse) => {\n  console.log('Handler received request at timestamp:', (req as any).timestamp);\n  res.status(200).json({\n    message: 'Hello from API!',\n    method: req.method,\n    url: req.url,\n    timestamp: (req as any).timestamp,\n  });\n};\n\n// Compose the handler with middleware using wrappers\n// Middleware are applied in the order they are listed.\nexport default wrappers(\n  withTimestamp,\n  withRequestLogger(\"[API_LOG]\"), // Middleware with parameters\n  myApiHandler\n);\n","lang":"typescript","description":"This quickstart demonstrates how to compose a Next.js API route handler using `nextjs-middleware-wrappers`. It chains two example middleware functions—one adding a timestamp to the request and another logging request details with a configurable prefix—before executing the main API handler, showcasing type-safe and cleaner middleware composition.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}