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.2.0 to 0.2.1

4

dist/main.d.ts

@@ -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": {

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