next-api-middleware
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -17,2 +17,6 @@ import type { NextApiHandler, NextApiRequest, NextApiResponse } from "next"; | ||
*/ | ||
export declare function hasMiddlewareSignature(input: unknown): input is Middleware; | ||
export declare function isValidMiddleware(input: unknown, throwOnFailure?: boolean): input is Middleware; | ||
/** | ||
* @private | ||
*/ | ||
export declare function isValidMiddlewareArray(input: unknown[], throwOnFailure?: boolean): input is Middleware[]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.hasMiddlewareSignature = exports.makeMiddlewareExecutor = exports.label = exports.use = void 0; | ||
exports.isValidMiddlewareArray = exports.isValidMiddleware = exports.makeMiddlewareExecutor = exports.label = exports.use = void 0; | ||
function use(...middleware) { | ||
@@ -8,5 +8,3 @@ // Flatten middleware groups | ||
// Check signatures | ||
if (middlewareFns.some((fn) => !hasMiddlewareSignature(fn))) { | ||
throw new Error("Invalid middleware functions"); | ||
} | ||
isValidMiddlewareArray(middlewareFns, true); | ||
// Make executor | ||
@@ -18,7 +16,3 @@ return makeMiddlewareExecutor(middlewareFns); | ||
// Check signatures | ||
if (Object.values(middleware) | ||
.flat() | ||
.some((fn) => !hasMiddlewareSignature(fn))) { | ||
throw new Error("Invalid middleware function(s)"); | ||
} | ||
isValidMiddlewareArray(Object.values(middleware).flat(), true); | ||
// Receive chosen middleware (either names or literal middleware functions) | ||
@@ -29,8 +23,4 @@ return function curryMiddlewareChoices(...chosenMiddleware) { | ||
for (const choice of [...defaults, ...chosenMiddleware]) { | ||
if (hasMiddlewareSignature(choice)) { | ||
// Choice is a middleware function, add directly to array | ||
middlewareFns.push(choice); | ||
} | ||
else { | ||
// Choice is the name of a registered function, get from registered middleware | ||
// Choice is the name of a registered function, get from registered middleware | ||
if (typeof choice === "string") { | ||
const fn = middleware[choice]; | ||
@@ -42,3 +32,14 @@ if (!fn) { | ||
middlewareFns.push(...(Array.isArray(fn) ? fn : [fn])); | ||
continue; | ||
} | ||
if (Array.isArray(choice) && isValidMiddlewareArray(choice, true)) { | ||
// Choice is an array of middleware functions | ||
middlewareFns.push(...choice); | ||
continue; | ||
} | ||
if (isValidMiddleware(choice, true)) { | ||
// Choice is a middleware function, add directly to array | ||
middlewareFns.push(choice); | ||
continue; | ||
} | ||
} | ||
@@ -65,5 +66,16 @@ // Make executor | ||
*/ | ||
function hasMiddlewareSignature(input) { | ||
return typeof input === "function" && input.length === 3; | ||
function isValidMiddleware(input, throwOnFailure = false) { | ||
const valid = typeof input === "function" && input.length === 3; | ||
if (!valid && throwOnFailure) { | ||
throw new Error("Invalid middleware"); | ||
} | ||
return valid; | ||
} | ||
exports.hasMiddlewareSignature = hasMiddlewareSignature; | ||
exports.isValidMiddleware = isValidMiddleware; | ||
/** | ||
* @private | ||
*/ | ||
function isValidMiddlewareArray(input, throwOnFailure = false) { | ||
return input.every((item) => isValidMiddleware(item, throwOnFailure)); | ||
} | ||
exports.isValidMiddlewareArray = isValidMiddlewareArray; |
{ | ||
"name": "next-api-middleware", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "Middleware solution for Next.js API routes", | ||
@@ -5,0 +5,0 @@ "repository": "git@github.com:htunnicliff/next-api-middleware.git", |
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
12140
97