clear-router 2.1.8 → 2.1.9
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/README.md +6 -0
- package/dist/express/index.cjs +16 -11
- package/dist/express/index.d.cts +1 -0
- package/dist/express/index.d.mts +1 -0
- package/dist/express/index.mjs +16 -11
- package/dist/h3/index.cjs +16 -11
- package/dist/h3/index.d.cts +1 -0
- package/dist/h3/index.d.mts +1 -0
- package/dist/h3/index.mjs +16 -11
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Clear Router
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/clear-router)
|
|
4
|
+
[](https://www.npmjs.com/package/clear-router)
|
|
5
|
+
[](https://github.com/toneflix/clear-router/blob/main/LICENSE)
|
|
6
|
+
[](https://github.com/arkstack-hq/clear-router/actions/workflows/npm-publish.yml)
|
|
7
|
+
[](https://github.com/arkstack-hq/clear-router/actions/workflows/ci.yml)
|
|
8
|
+
|
|
3
9
|
Laravel-style routing system for H3 and Express.js, with clean route definitions, middleware support, controller bindings and full TypeScript support.
|
|
4
10
|
|
|
5
11
|
## Installation
|
package/dist/express/index.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
const require_Route = require('../Route-DhC4kNPX.cjs');
|
|
3
|
+
let node_async_hooks = require("node:async_hooks");
|
|
3
4
|
|
|
4
5
|
//#region src/express/router.ts
|
|
5
6
|
/**
|
|
@@ -10,6 +11,7 @@ const require_Route = require('../Route-DhC4kNPX.cjs');
|
|
|
10
11
|
* @repository https://github.com/toneflix/clear-router
|
|
11
12
|
*/
|
|
12
13
|
var Router = class Router {
|
|
14
|
+
static groupContext = new node_async_hooks.AsyncLocalStorage();
|
|
13
15
|
/**
|
|
14
16
|
* All registered routes
|
|
15
17
|
*/
|
|
@@ -50,12 +52,15 @@ var Router = class Router {
|
|
|
50
52
|
* @param middlewares - Array of middleware functions
|
|
51
53
|
*/
|
|
52
54
|
static add(methods, path, handler, middlewares) {
|
|
55
|
+
const context = this.groupContext.getStore();
|
|
56
|
+
const activePrefix = context?.prefix ?? this.prefix;
|
|
57
|
+
const activeGroupMiddlewares = context?.groupMiddlewares ?? this.groupMiddlewares;
|
|
53
58
|
methods = Array.isArray(methods) ? methods : [methods];
|
|
54
59
|
middlewares = middlewares ? Array.isArray(middlewares) ? middlewares : [middlewares] : void 0;
|
|
55
|
-
const fullPath = this.normalizePath(`${
|
|
60
|
+
const fullPath = this.normalizePath(`${activePrefix}/${path}`);
|
|
56
61
|
const route = new require_Route.Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, [
|
|
57
62
|
...this.globalMiddlewares,
|
|
58
|
-
...
|
|
63
|
+
...activeGroupMiddlewares,
|
|
59
64
|
...middlewares || []
|
|
60
65
|
]);
|
|
61
66
|
if (!methods.includes("options") && !this.routesByPathMethod[`OPTIONS ${fullPath}`]) this.options(path, ({ res }) => {
|
|
@@ -181,17 +186,17 @@ var Router = class Router {
|
|
|
181
186
|
* @param middlewares - Middleware functions applied to all routes in group
|
|
182
187
|
*/
|
|
183
188
|
static async group(prefix, callback, middlewares) {
|
|
184
|
-
const
|
|
185
|
-
const
|
|
189
|
+
const context = this.groupContext.getStore();
|
|
190
|
+
const previousPrefix = context?.prefix ?? this.prefix;
|
|
191
|
+
const previousMiddlewares = context?.groupMiddlewares ?? this.groupMiddlewares;
|
|
186
192
|
const fullPrefix = [previousPrefix, prefix].filter(Boolean).join("/");
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
193
|
+
const nextContext = {
|
|
194
|
+
prefix: this.normalizePath(fullPrefix),
|
|
195
|
+
groupMiddlewares: [...previousMiddlewares, ...middlewares || []]
|
|
196
|
+
};
|
|
197
|
+
await this.groupContext.run(nextContext, async () => {
|
|
190
198
|
await Promise.resolve(callback());
|
|
191
|
-
}
|
|
192
|
-
this.prefix = previousPrefix;
|
|
193
|
-
this.groupMiddlewares = previousMiddlewares;
|
|
194
|
-
}
|
|
199
|
+
});
|
|
195
200
|
}
|
|
196
201
|
/**
|
|
197
202
|
* Apply global middlewares for the duration of the callback
|
package/dist/express/index.d.cts
CHANGED
package/dist/express/index.d.mts
CHANGED
package/dist/express/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { n as ClearRequest, t as Route } from "../Route-BbPXcDGX.mjs";
|
|
2
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
3
|
|
|
3
4
|
//#region src/express/router.ts
|
|
4
5
|
/**
|
|
@@ -9,6 +10,7 @@ import { n as ClearRequest, t as Route } from "../Route-BbPXcDGX.mjs";
|
|
|
9
10
|
* @repository https://github.com/toneflix/clear-router
|
|
10
11
|
*/
|
|
11
12
|
var Router = class Router {
|
|
13
|
+
static groupContext = new AsyncLocalStorage();
|
|
12
14
|
/**
|
|
13
15
|
* All registered routes
|
|
14
16
|
*/
|
|
@@ -49,12 +51,15 @@ var Router = class Router {
|
|
|
49
51
|
* @param middlewares - Array of middleware functions
|
|
50
52
|
*/
|
|
51
53
|
static add(methods, path, handler, middlewares) {
|
|
54
|
+
const context = this.groupContext.getStore();
|
|
55
|
+
const activePrefix = context?.prefix ?? this.prefix;
|
|
56
|
+
const activeGroupMiddlewares = context?.groupMiddlewares ?? this.groupMiddlewares;
|
|
52
57
|
methods = Array.isArray(methods) ? methods : [methods];
|
|
53
58
|
middlewares = middlewares ? Array.isArray(middlewares) ? middlewares : [middlewares] : void 0;
|
|
54
|
-
const fullPath = this.normalizePath(`${
|
|
59
|
+
const fullPath = this.normalizePath(`${activePrefix}/${path}`);
|
|
55
60
|
const route = new Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, [
|
|
56
61
|
...this.globalMiddlewares,
|
|
57
|
-
...
|
|
62
|
+
...activeGroupMiddlewares,
|
|
58
63
|
...middlewares || []
|
|
59
64
|
]);
|
|
60
65
|
if (!methods.includes("options") && !this.routesByPathMethod[`OPTIONS ${fullPath}`]) this.options(path, ({ res }) => {
|
|
@@ -180,17 +185,17 @@ var Router = class Router {
|
|
|
180
185
|
* @param middlewares - Middleware functions applied to all routes in group
|
|
181
186
|
*/
|
|
182
187
|
static async group(prefix, callback, middlewares) {
|
|
183
|
-
const
|
|
184
|
-
const
|
|
188
|
+
const context = this.groupContext.getStore();
|
|
189
|
+
const previousPrefix = context?.prefix ?? this.prefix;
|
|
190
|
+
const previousMiddlewares = context?.groupMiddlewares ?? this.groupMiddlewares;
|
|
185
191
|
const fullPrefix = [previousPrefix, prefix].filter(Boolean).join("/");
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
192
|
+
const nextContext = {
|
|
193
|
+
prefix: this.normalizePath(fullPrefix),
|
|
194
|
+
groupMiddlewares: [...previousMiddlewares, ...middlewares || []]
|
|
195
|
+
};
|
|
196
|
+
await this.groupContext.run(nextContext, async () => {
|
|
189
197
|
await Promise.resolve(callback());
|
|
190
|
-
}
|
|
191
|
-
this.prefix = previousPrefix;
|
|
192
|
-
this.groupMiddlewares = previousMiddlewares;
|
|
193
|
-
}
|
|
198
|
+
});
|
|
194
199
|
}
|
|
195
200
|
/**
|
|
196
201
|
* Apply global middlewares for the duration of the callback
|
package/dist/h3/index.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
const require_Route = require('../Route-DhC4kNPX.cjs');
|
|
3
|
+
let node_async_hooks = require("node:async_hooks");
|
|
3
4
|
let h3 = require("h3");
|
|
4
5
|
|
|
5
6
|
//#region src/h3/router.ts
|
|
@@ -10,6 +11,7 @@ let h3 = require("h3");
|
|
|
10
11
|
* @repository https://github.com/toneflix/clear-router
|
|
11
12
|
*/
|
|
12
13
|
var Router = class Router {
|
|
14
|
+
static groupContext = new node_async_hooks.AsyncLocalStorage();
|
|
13
15
|
/**
|
|
14
16
|
* All registered routes
|
|
15
17
|
*/
|
|
@@ -50,12 +52,15 @@ var Router = class Router {
|
|
|
50
52
|
* @param middlewares - Array of middleware functions
|
|
51
53
|
*/
|
|
52
54
|
static add(methods, path, handler, middlewares) {
|
|
55
|
+
const context = this.groupContext.getStore();
|
|
56
|
+
const activePrefix = context?.prefix ?? this.prefix;
|
|
57
|
+
const activeGroupMiddlewares = context?.groupMiddlewares ?? this.groupMiddlewares;
|
|
53
58
|
methods = Array.isArray(methods) ? methods : [methods];
|
|
54
59
|
middlewares = middlewares ? Array.isArray(middlewares) ? middlewares : [middlewares] : void 0;
|
|
55
|
-
const fullPath = this.normalizePath(`${
|
|
60
|
+
const fullPath = this.normalizePath(`${activePrefix}/${path}`);
|
|
56
61
|
const route = new require_Route.Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, [
|
|
57
62
|
...this.globalMiddlewares,
|
|
58
|
-
...
|
|
63
|
+
...activeGroupMiddlewares,
|
|
59
64
|
...middlewares || []
|
|
60
65
|
]);
|
|
61
66
|
if (!methods.includes("options") && !this.routesByPathMethod[`OPTIONS ${fullPath}`]) this.options(path, ({ res }) => {
|
|
@@ -181,17 +186,17 @@ var Router = class Router {
|
|
|
181
186
|
* @param middlewares - Middleware functions applied to all routes in group
|
|
182
187
|
*/
|
|
183
188
|
static async group(prefix, callback, middlewares) {
|
|
184
|
-
const
|
|
185
|
-
const
|
|
189
|
+
const context = this.groupContext.getStore();
|
|
190
|
+
const previousPrefix = context?.prefix ?? this.prefix;
|
|
191
|
+
const previousMiddlewares = context?.groupMiddlewares ?? this.groupMiddlewares;
|
|
186
192
|
const fullPrefix = [previousPrefix, prefix].filter(Boolean).join("/");
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
193
|
+
const nextContext = {
|
|
194
|
+
prefix: this.normalizePath(fullPrefix),
|
|
195
|
+
groupMiddlewares: [...previousMiddlewares, ...middlewares || []]
|
|
196
|
+
};
|
|
197
|
+
await this.groupContext.run(nextContext, async () => {
|
|
190
198
|
await Promise.resolve(callback());
|
|
191
|
-
}
|
|
192
|
-
this.prefix = previousPrefix;
|
|
193
|
-
this.groupMiddlewares = previousMiddlewares;
|
|
194
|
-
}
|
|
199
|
+
});
|
|
195
200
|
}
|
|
196
201
|
/**
|
|
197
202
|
* Apply global middlewares for the duration of the callback
|
package/dist/h3/index.d.cts
CHANGED
package/dist/h3/index.d.mts
CHANGED
package/dist/h3/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { n as ClearRequest, t as Route } from "../Route-BbPXcDGX.mjs";
|
|
2
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
3
|
import { getQuery, getRouterParams, readBody } from "h3";
|
|
3
4
|
|
|
4
5
|
//#region src/h3/router.ts
|
|
@@ -9,6 +10,7 @@ import { getQuery, getRouterParams, readBody } from "h3";
|
|
|
9
10
|
* @repository https://github.com/toneflix/clear-router
|
|
10
11
|
*/
|
|
11
12
|
var Router = class Router {
|
|
13
|
+
static groupContext = new AsyncLocalStorage();
|
|
12
14
|
/**
|
|
13
15
|
* All registered routes
|
|
14
16
|
*/
|
|
@@ -49,12 +51,15 @@ var Router = class Router {
|
|
|
49
51
|
* @param middlewares - Array of middleware functions
|
|
50
52
|
*/
|
|
51
53
|
static add(methods, path, handler, middlewares) {
|
|
54
|
+
const context = this.groupContext.getStore();
|
|
55
|
+
const activePrefix = context?.prefix ?? this.prefix;
|
|
56
|
+
const activeGroupMiddlewares = context?.groupMiddlewares ?? this.groupMiddlewares;
|
|
52
57
|
methods = Array.isArray(methods) ? methods : [methods];
|
|
53
58
|
middlewares = middlewares ? Array.isArray(middlewares) ? middlewares : [middlewares] : void 0;
|
|
54
|
-
const fullPath = this.normalizePath(`${
|
|
59
|
+
const fullPath = this.normalizePath(`${activePrefix}/${path}`);
|
|
55
60
|
const route = new Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, [
|
|
56
61
|
...this.globalMiddlewares,
|
|
57
|
-
...
|
|
62
|
+
...activeGroupMiddlewares,
|
|
58
63
|
...middlewares || []
|
|
59
64
|
]);
|
|
60
65
|
if (!methods.includes("options") && !this.routesByPathMethod[`OPTIONS ${fullPath}`]) this.options(path, ({ res }) => {
|
|
@@ -180,17 +185,17 @@ var Router = class Router {
|
|
|
180
185
|
* @param middlewares - Middleware functions applied to all routes in group
|
|
181
186
|
*/
|
|
182
187
|
static async group(prefix, callback, middlewares) {
|
|
183
|
-
const
|
|
184
|
-
const
|
|
188
|
+
const context = this.groupContext.getStore();
|
|
189
|
+
const previousPrefix = context?.prefix ?? this.prefix;
|
|
190
|
+
const previousMiddlewares = context?.groupMiddlewares ?? this.groupMiddlewares;
|
|
185
191
|
const fullPrefix = [previousPrefix, prefix].filter(Boolean).join("/");
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
192
|
+
const nextContext = {
|
|
193
|
+
prefix: this.normalizePath(fullPrefix),
|
|
194
|
+
groupMiddlewares: [...previousMiddlewares, ...middlewares || []]
|
|
195
|
+
};
|
|
196
|
+
await this.groupContext.run(nextContext, async () => {
|
|
189
197
|
await Promise.resolve(callback());
|
|
190
|
-
}
|
|
191
|
-
this.prefix = previousPrefix;
|
|
192
|
-
this.groupMiddlewares = previousMiddlewares;
|
|
193
|
-
}
|
|
198
|
+
});
|
|
194
199
|
}
|
|
195
200
|
/**
|
|
196
201
|
* Apply global middlewares for the duration of the callback
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "clear-router",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.9",
|
|
4
4
|
"description": "Laravel-style routing system for Express.js and H3, with CommonJS, ESM, and TypeScript support",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"h3",
|
|
@@ -90,6 +90,7 @@
|
|
|
90
90
|
"lint": "eslint",
|
|
91
91
|
"test:esm": "vitest tests/esm.test.ts",
|
|
92
92
|
"test:ts": "vitest tests/typescript.test.ts",
|
|
93
|
+
"test:coverage": "vitest run --coverage",
|
|
93
94
|
"example": "tsx example/express/index.ts",
|
|
94
95
|
"example:esm": "tsx example/express/esm.ts",
|
|
95
96
|
"example:ts": "tsx example/express/typescript.ts",
|