Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

next-api-middleware

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-api-middleware - npm Package Compare versions

Comparing version 0.5.1 to 0.5.2

6

dist/index.d.ts

@@ -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[];

48

dist/index.js
"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",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc