next-api-middleware
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -11,3 +11,3 @@ import type { NextApiHandler, NextApiRequest, NextApiResponse } from "next"; | ||
*/ | ||
export declare type Middleware = (req: NextApiRequest, res: NextApiResponse, next: () => void | Promise<void>) => void | Promise<void>; | ||
export declare type Middleware = (req: NextApiRequest, res: NextApiResponse, next: () => Promise<void>) => Promise<void>; | ||
/** | ||
@@ -31,3 +31,3 @@ * @example | ||
[name: string]: Middleware | Middleware[]; | ||
}>(namedMiddlewares: T) => (...chosenMiddleware: (keyof T)[]) => (apiRouteHandler: NextApiHandler) => (req: NextApiRequest, res: NextApiResponse) => Promise<any>; | ||
}>(namedMiddlewares: T) => (...chosenMiddleware: (Middleware | keyof T)[]) => (apiRouteHandler: NextApiHandler) => (req: NextApiRequest, res: NextApiResponse) => Promise<any>; | ||
export declare function makeMiddlewareExecutor(middleware: Middleware[]): (apiRouteHandler: NextApiHandler) => (req: NextApiRequest, res: NextApiResponse) => Promise<any>; |
@@ -40,11 +40,16 @@ /** | ||
const middleware = chosenMiddleware.reduce((middleware, chosen) => { | ||
// Validate that chosen exists. | ||
if (!(chosen in namedMiddlewares)) { | ||
throw new Error(`Middleware "${chosen}" not available`); | ||
if (isMiddleware(chosen)) { | ||
return [...middleware, chosen]; | ||
} | ||
// Get middleware from named collection. | ||
const fn = namedMiddlewares[chosen]; | ||
// Add function(s) to array. | ||
// NOTE: The isArray check allows for middleware groups | ||
return [...middleware, ...(Array.isArray(fn) ? fn : [fn])]; | ||
else { | ||
// Validate that chosen exists. | ||
if (!(chosen in namedMiddlewares)) { | ||
throw new Error(`Middleware "${chosen}" not available`); | ||
} | ||
// Get middleware from named collection. | ||
const fn = namedMiddlewares[chosen]; | ||
// Add function(s) to array. | ||
// NOTE: The isArray check allows for middleware groups | ||
return [...middleware, ...(Array.isArray(fn) ? fn : [fn])]; | ||
} | ||
}, []); | ||
@@ -64,3 +69,3 @@ return makeMiddlewareExecutor(middleware); | ||
// Execute the current middleware, passing along remaining middlewares. | ||
return current(req, res, () => | ||
return current(req, res, async () => | ||
// If last, execute the API route handler itself. | ||
@@ -67,0 +72,0 @@ last ? apiRouteHandler(req, res) : run(remaining)); |
{ | ||
"name": "next-api-middleware", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Middleware solution for Next.js API routes", | ||
@@ -16,3 +16,4 @@ "main": "index.js", | ||
"build": "tsc", | ||
"test": "jest" | ||
"test": "jest", | ||
"prepublish": "rm -rf dist && npm run build" | ||
}, | ||
@@ -19,0 +20,0 @@ "devDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8496
110