@aghlimi/next-middleware-chain 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Aghlimi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,138 @@
1
+ # Next Middleware Chain
2
+
3
+ A lightweight middleware chain for Next.js with route-based middleware and a `next()` API inspired by NestJS/Express.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @aghlimi/next-middleware-chain
9
+ ```
10
+
11
+ or:
12
+
13
+ ```bash
14
+ npm install @aghlimi/next-middleware-chain
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ Register middleware in your Next.js `proxy.ts`:
20
+
21
+ ```ts
22
+ import { NextRequest, NextResponse } from "next/server";
23
+ import setMiddleware, {
24
+ executeMiddlewares,
25
+ } from "@aghlimi/next-middleware-chain";
26
+
27
+ setMiddleware("/api/*", async (request, next) => {
28
+ console.log(`Request to ${request.nextUrl.pathname}`);
29
+
30
+ const allowed = true;
31
+
32
+ if (!allowed) {
33
+ return NextResponse.json(
34
+ { message: "Forbidden" },
35
+ { status: 403 }
36
+ );
37
+ }
38
+
39
+ next();
40
+ });
41
+
42
+ setMiddleware("/api/*", async (request, next) => {
43
+ console.log("Second middleware");
44
+ next();
45
+ });
46
+
47
+ export async function proxy(request: NextRequest) {
48
+ return executeMiddlewares(request);
49
+ }
50
+ ```
51
+
52
+ Call `next()` to continue to the next matching middleware.
53
+
54
+ Return a `NextResponse` without calling `next()` to stop the middleware chain.
55
+
56
+ ## Route Matching
57
+
58
+ Paths passed to `setMiddleware()` are JavaScript regular expressions:
59
+
60
+ ```ts
61
+ setMiddleware("/api/*", middleware);
62
+ ```
63
+
64
+ The expression is evaluated using:
65
+
66
+ ```ts
67
+ new RegExp(pathExpression).test(request.nextUrl.pathname);
68
+ ```
69
+
70
+ For stricter matching, you can use any valid regex:
71
+
72
+ ```ts
73
+ setMiddleware("^/api(?:/|$)", middleware);
74
+ setMiddleware("^/api/admin(?:/|$)", adminMiddleware);
75
+ ```
76
+
77
+ Multiple middleware functions can match the same request and are executed in registration order.
78
+
79
+ ## Separate Middleware Files
80
+
81
+ You can define reusable middleware using `MiddlewareFunction`:
82
+
83
+ ```ts
84
+ import type {
85
+ MiddlewareFunction,
86
+ } from "@aghlimi/next-middleware-chain";
87
+
88
+ const logger: MiddlewareFunction = async (request, next) => {
89
+ console.log(`${request.method} ${request.nextUrl.pathname}`);
90
+ next();
91
+ };
92
+
93
+ export default logger;
94
+ ```
95
+
96
+ Then register it:
97
+
98
+ ```ts
99
+ import setMiddleware from "@aghlimi/next-middleware-chain";
100
+ import logger from "./middlewares/logger";
101
+
102
+ setMiddleware("/api/*", logger);
103
+ ```
104
+
105
+ ## API
106
+
107
+ ### `setMiddleware(pathExpression, middleware)`
108
+
109
+ Registers middleware for a path expression.
110
+
111
+ ```ts
112
+ setMiddleware("/api/*", middleware);
113
+ ```
114
+
115
+ ### `executeMiddlewares(request)`
116
+
117
+ Executes middleware matching the current request.
118
+
119
+ ```ts
120
+ export async function proxy(request: NextRequest) {
121
+ return executeMiddlewares(request);
122
+ }
123
+ ```
124
+
125
+ ### `MiddlewareFunction`
126
+
127
+ Type for reusable middleware:
128
+
129
+ ```ts
130
+ type MiddlewareFunction = (
131
+ request: NextRequest,
132
+ next: () => Promise<NextResponse | void>
133
+ ) => Promise<NextResponse | void>;
134
+ ```
135
+
136
+ ## License
137
+
138
+ MIT
@@ -0,0 +1,7 @@
1
+ import { NextRequest, NextResponse } from "next/server";
2
+ export type MiddlewareFunction = (request: NextRequest, next: () => Promise<NextResponse | void>) => Promise<NextResponse | void>;
3
+ declare function setMiddleware(pathExpression: string, middlewareFunction: MiddlewareFunction): void;
4
+ export declare function getMiddlewares(path: string): MiddlewareFunction[];
5
+ export declare function executeMiddlewares(request: NextRequest): Promise<NextResponse>;
6
+ export default setMiddleware;
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAExD,MAAM,MAAM,kBAAkB,GAAG,CAC/B,OAAO,EAAE,WAAW,EACpB,IAAI,EAAE,MAAM,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,KACrC,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,CAAC;AAIlC,iBAAS,aAAa,CACpB,cAAc,EAAE,MAAM,EACtB,kBAAkB,EAAE,kBAAkB,GACrC,IAAI,CAKN;AAED,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,kBAAkB,EAAE,CAIjE;AAED,wBAAsB,kBAAkB,CACtC,OAAO,EAAE,WAAW,GACnB,OAAO,CAAC,YAAY,CAAC,CAmBvB;AAED,eAAe,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,28 @@
1
+ import { NextResponse } from "next/server";
2
+ const config = new Map();
3
+ function setMiddleware(pathExpression, middlewareFunction) {
4
+ config.set(pathExpression, [
5
+ ...(config.get(pathExpression) || []),
6
+ middlewareFunction,
7
+ ]);
8
+ }
9
+ export function getMiddlewares(path) {
10
+ return Array.from(config.entries())
11
+ .filter(([key]) => new RegExp(key).test(path))
12
+ .flatMap(([, value]) => value);
13
+ }
14
+ export async function executeMiddlewares(request) {
15
+ const path = request.nextUrl.pathname;
16
+ const middlewares = getMiddlewares(path);
17
+ async function dispatch(index) {
18
+ const middleware = middlewares[index];
19
+ if (!middleware) {
20
+ return NextResponse.next();
21
+ }
22
+ const next = () => dispatch(index + 1);
23
+ return middleware(request, next);
24
+ }
25
+ const response = await dispatch(0);
26
+ return response instanceof NextResponse ? response : NextResponse.next();
27
+ }
28
+ export default setMiddleware;
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@aghlimi/next-middleware-chain",
3
+ "version": "1.0.0",
4
+ "description": "A tiny middleware-chain utility for Next.js request proxies.",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "keywords": [
18
+ "nextjs",
19
+ "middleware",
20
+ "typescript",
21
+ "proxy"
22
+ ],
23
+ "license": "MIT",
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/Aghlimi/nextjs-middleware.git"
27
+ },
28
+ "peerDependencies": {
29
+ "next": ">=16"
30
+ },
31
+ "devDependencies": {
32
+ "next": "^16.3.4",
33
+ "typescript": "^5.9.2"
34
+ },
35
+ "scripts": {
36
+ "build": "tsc -p tsconfig.json"
37
+ }
38
+ }