clear-router 2.8.2 → 2.8.4
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/dist/ClearRequest.d.cts +1 -1
- package/dist/ClearRequest.d.mts +1 -1
- package/dist/Controller.d.cts +1 -1
- package/dist/Controller.d.mts +1 -1
- package/dist/ResourceRoutes.cjs +12 -0
- package/dist/ResourceRoutes.d.cts +13 -19
- package/dist/ResourceRoutes.d.mts +13 -19
- package/dist/ResourceRoutes.mjs +12 -0
- package/dist/{Request-Ci0UQ-Vl.d.mts → Route-tq-rge1g.d.mts} +46 -46
- package/dist/Route.d.cts +1 -1
- package/dist/Route.d.mts +1 -1
- package/dist/RouteGroup.cjs +150 -0
- package/dist/RouteGroup.d.cts +73 -0
- package/dist/RouteGroup.d.mts +73 -0
- package/dist/RouteGroup.mjs +150 -0
- package/dist/core/{router.cjs → CoreRouter.cjs} +13 -41
- package/dist/core/{router.d.cts → CoreRouter.d.cts} +6 -13
- package/dist/core/{router.d.mts → CoreRouter.d.mts} +6 -13
- package/dist/core/{router.mjs → CoreRouter.mjs} +13 -41
- package/dist/core/Request.d.cts +2 -2
- package/dist/core/Request.d.mts +2 -2
- package/dist/core/helpers.d.mts +1 -0
- package/dist/core/index.cjs +3 -2
- package/dist/core/index.d.cts +3 -2
- package/dist/core/index.d.mts +3 -2
- package/dist/core/index.mjs +3 -2
- package/dist/core/plugins.d.cts +1 -1
- package/dist/core/plugins.d.mts +1 -1
- package/dist/decorators/setup.cjs +2 -2
- package/dist/decorators/setup.mjs +1 -1
- package/dist/express/router.cjs +4 -4
- package/dist/express/router.d.cts +4 -3
- package/dist/express/router.d.mts +4 -3
- package/dist/express/router.mjs +3 -3
- package/dist/fastify/router.cjs +4 -4
- package/dist/fastify/router.d.cts +4 -3
- package/dist/fastify/router.d.mts +4 -3
- package/dist/fastify/router.mjs +3 -3
- package/dist/h3/router.cjs +4 -4
- package/dist/h3/router.d.cts +4 -3
- package/dist/h3/router.d.mts +4 -3
- package/dist/h3/router.mjs +3 -3
- package/dist/hono/router.cjs +4 -4
- package/dist/hono/router.d.cts +4 -3
- package/dist/hono/router.d.mts +4 -3
- package/dist/hono/router.mjs +3 -3
- package/dist/index.cjs +5 -3
- package/dist/index.d.cts +4 -3
- package/dist/index.d.mts +4 -3
- package/dist/index.mjs +4 -3
- package/dist/koa/router.cjs +4 -4
- package/dist/koa/router.d.cts +4 -3
- package/dist/koa/router.d.mts +4 -3
- package/dist/koa/router.mjs +3 -3
- package/dist/types/basic.d.cts +40 -3
- package/dist/types/basic.d.mts +40 -3
- package/dist/types/express.d.cts +1 -1
- package/dist/types/express.d.mts +1 -1
- package/dist/types/fastify.d.cts +1 -1
- package/dist/types/fastify.d.mts +1 -1
- package/dist/types/h3.d.cts +1 -1
- package/dist/types/h3.d.mts +1 -1
- package/dist/types/hono.d.cts +1 -1
- package/dist/types/hono.d.mts +1 -1
- package/dist/types/index.d.mts +1 -1
- package/dist/types/koa.d.cts +1 -1
- package/dist/types/koa.d.mts +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { importFile } from "./core/helpers.mjs";
|
|
2
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
3
|
+
import { readdir, stat } from "node:fs/promises";
|
|
4
|
+
|
|
5
|
+
//#region src/RouteGroup.ts
|
|
6
|
+
/**
|
|
7
|
+
* @class clear-router RouteGroup
|
|
8
|
+
* @description A route group describes a collection of routes on clear-router
|
|
9
|
+
* @author 3m1n3nc3
|
|
10
|
+
* @repository https://github.com/arkstack-tmp/clear-router
|
|
11
|
+
*/
|
|
12
|
+
var RouteGroup = class {
|
|
13
|
+
checks = [];
|
|
14
|
+
conditions = [];
|
|
15
|
+
registration;
|
|
16
|
+
routes = /* @__PURE__ */ new Set();
|
|
17
|
+
unfilteredSources = /* @__PURE__ */ new Set();
|
|
18
|
+
constructor(options) {
|
|
19
|
+
this.options = options;
|
|
20
|
+
this.registration = this.register();
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Returning a falsy value will stop route group registration
|
|
24
|
+
*
|
|
25
|
+
* @param condition
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
when(condition) {
|
|
29
|
+
this.conditions.push(condition);
|
|
30
|
+
const unfilteredSources = Array.from(this.unfilteredSources);
|
|
31
|
+
if (unfilteredSources.length) this.checks.push(this.registration.then(async () => {
|
|
32
|
+
for (const source of unfilteredSources) if (!await condition(source)) {
|
|
33
|
+
this.rollback();
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
}));
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Register one or more middleware that will be executed before every route in the group.
|
|
41
|
+
*
|
|
42
|
+
* @param middlewares
|
|
43
|
+
* @returns
|
|
44
|
+
*/
|
|
45
|
+
middleware(middlewares) {
|
|
46
|
+
this.checks.push(this.registration.then(() => {
|
|
47
|
+
for (const route of this.routes) route.middleware(middlewares);
|
|
48
|
+
}));
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Attaches callbacks for the resolution and/or rejection of the RouteGroup.
|
|
53
|
+
*
|
|
54
|
+
* @param onfulfilled
|
|
55
|
+
* @param onrejected
|
|
56
|
+
* @returns
|
|
57
|
+
*/
|
|
58
|
+
then(onfulfilled, onrejected) {
|
|
59
|
+
return Promise.all([this.registration, ...this.checks]).then(() => void 0).then(onfulfilled, onrejected);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Register the routes
|
|
63
|
+
*/
|
|
64
|
+
async register() {
|
|
65
|
+
const current = this.options.context.getStore();
|
|
66
|
+
const previousPrefix = current?.prefix ?? this.options.defaultPrefix;
|
|
67
|
+
const previousMiddlewares = current?.groupMiddlewares ?? this.options.defaultMiddlewares;
|
|
68
|
+
const fullPrefix = [previousPrefix, this.options.prefix].filter(Boolean).join("/");
|
|
69
|
+
const nextContext = {
|
|
70
|
+
prefix: this.options.normalizePath(fullPrefix),
|
|
71
|
+
groupMiddlewares: [...previousMiddlewares, ...this.options.middlewares || []],
|
|
72
|
+
routeCollectors: [...current?.routeCollectors ?? [], this.routes]
|
|
73
|
+
};
|
|
74
|
+
await this.options.context.run(nextContext, async () => {
|
|
75
|
+
for (const entry of Array.isArray(this.options.source) ? this.options.source : [this.options.source]) {
|
|
76
|
+
if (typeof entry === "function") {
|
|
77
|
+
if (!this.conditions.length) this.unfilteredSources.add(entry);
|
|
78
|
+
else if (!await this.accepts(entry)) continue;
|
|
79
|
+
await Promise.resolve(entry());
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const resolved = await this.resolveFiles(entry);
|
|
83
|
+
if (!resolved.directory && !await this.accepts(entry)) continue;
|
|
84
|
+
for (const file of resolved.files) {
|
|
85
|
+
if (resolved.directory && !await this.accepts(file)) continue;
|
|
86
|
+
await importFile(file);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Rollback the route registration
|
|
93
|
+
*/
|
|
94
|
+
rollback() {
|
|
95
|
+
for (const route of this.routes) this.options.removeRoute(route);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Check whether a callback or path should be registered.
|
|
99
|
+
*
|
|
100
|
+
* @param source
|
|
101
|
+
* @returns
|
|
102
|
+
*/
|
|
103
|
+
async accepts(source) {
|
|
104
|
+
for (const condition of this.conditions) if (!await condition(source)) return false;
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Resolve files from the group path
|
|
109
|
+
*
|
|
110
|
+
* @param source
|
|
111
|
+
* @returns
|
|
112
|
+
*/
|
|
113
|
+
async resolveFiles(source) {
|
|
114
|
+
const resolved = isAbsolute(source) ? source : resolve(process.cwd(), source);
|
|
115
|
+
let sourceStat;
|
|
116
|
+
try {
|
|
117
|
+
sourceStat = await stat(resolved);
|
|
118
|
+
} catch {
|
|
119
|
+
throw new Error(`Route group source not found: ${source}`);
|
|
120
|
+
}
|
|
121
|
+
if (sourceStat.isFile()) return {
|
|
122
|
+
directory: false,
|
|
123
|
+
files: [resolved]
|
|
124
|
+
};
|
|
125
|
+
if (!sourceStat.isDirectory()) throw new Error(`Route group source must be a file or directory: ${source}`);
|
|
126
|
+
return {
|
|
127
|
+
directory: true,
|
|
128
|
+
files: await this.readDirectory(resolved)
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Read all the files in the configured directory
|
|
133
|
+
*
|
|
134
|
+
* @param directory
|
|
135
|
+
* @returns
|
|
136
|
+
*/
|
|
137
|
+
async readDirectory(directory) {
|
|
138
|
+
const entries = await readdir(directory, { withFileTypes: true });
|
|
139
|
+
const files = [];
|
|
140
|
+
for (const entry of entries.sort((left, right) => left.name.localeCompare(right.name))) {
|
|
141
|
+
const path = join(directory, entry.name);
|
|
142
|
+
if (entry.isDirectory()) files.push(...await this.readDirectory(path));
|
|
143
|
+
else if (entry.isFile() && /\.(?:[cm]?ts)$/.test(entry.name) && !entry.name.endsWith(".d.ts")) files.push(path);
|
|
144
|
+
}
|
|
145
|
+
return files;
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
//#endregion
|
|
150
|
+
export { RouteGroup };
|
|
@@ -3,13 +3,11 @@ const require_Response = require('./Response.cjs');
|
|
|
3
3
|
const require_bindings = require('./bindings.cjs');
|
|
4
4
|
const require_ResourceRoutes = require('../ResourceRoutes.cjs');
|
|
5
5
|
const require_Route = require('../Route.cjs');
|
|
6
|
-
const
|
|
7
|
-
let node_path = require("node:path");
|
|
8
|
-
let node_fs_promises = require("node:fs/promises");
|
|
6
|
+
const require_RouteGroup = require('../RouteGroup.cjs');
|
|
9
7
|
let node_async_hooks = require("node:async_hooks");
|
|
10
8
|
let node_module = require("node:module");
|
|
11
9
|
|
|
12
|
-
//#region src/core/
|
|
10
|
+
//#region src/core/CoreRouter.ts
|
|
13
11
|
/**
|
|
14
12
|
* @class clear-router CoreRouter
|
|
15
13
|
* @description Core routing logic for clear-router, shared between all supported adapters (Express.js, H3, etc.)
|
|
@@ -533,6 +531,7 @@ var CoreRouter = class {
|
|
|
533
531
|
});
|
|
534
532
|
if (!methods.includes("options") && !this.routesByPathMethod.get(`OPTIONS ${fullPath}`)) this.options(path, this.createDefaultOptionsHandler());
|
|
535
533
|
this.routes.add(route);
|
|
534
|
+
for (const collector of context?.routeCollectors ?? []) collector.add(route);
|
|
536
535
|
for (const method of methods.map((m) => m.toUpperCase())) {
|
|
537
536
|
this.routesByPathMethod.set(`${method} ${fullPath}`, route);
|
|
538
537
|
if (!this.routesByMethod.has(method)) this.routesByMethod.set(method, []);
|
|
@@ -643,46 +642,19 @@ var CoreRouter = class {
|
|
|
643
642
|
* @param callback
|
|
644
643
|
* @param middlewares
|
|
645
644
|
*/
|
|
646
|
-
static
|
|
645
|
+
static group(prefix, source, middlewares) {
|
|
647
646
|
this.ensureState();
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
if (typeof source === "function") {
|
|
658
|
-
await Promise.resolve(source());
|
|
659
|
-
return;
|
|
660
|
-
}
|
|
661
|
-
for (const file of await this.resolveGroupFiles(source)) await require_helpers.importFile(file);
|
|
647
|
+
return new require_RouteGroup.RouteGroup({
|
|
648
|
+
prefix,
|
|
649
|
+
source,
|
|
650
|
+
middlewares,
|
|
651
|
+
context: this.groupContext,
|
|
652
|
+
defaultPrefix: this.prefix,
|
|
653
|
+
defaultMiddlewares: this.groupMiddlewares,
|
|
654
|
+
normalizePath: (path) => this.normalizePath(path),
|
|
655
|
+
removeRoute: (route) => this.removeRoute(route)
|
|
662
656
|
});
|
|
663
657
|
}
|
|
664
|
-
static async resolveGroupFiles(source) {
|
|
665
|
-
const resolved = (0, node_path.isAbsolute)(source) ? source : (0, node_path.resolve)(process.cwd(), source);
|
|
666
|
-
let sourceStat;
|
|
667
|
-
try {
|
|
668
|
-
sourceStat = await (0, node_fs_promises.stat)(resolved);
|
|
669
|
-
} catch {
|
|
670
|
-
throw new Error(`Route group source not found: ${source}`);
|
|
671
|
-
}
|
|
672
|
-
if (sourceStat.isFile()) return [resolved];
|
|
673
|
-
if (!sourceStat.isDirectory()) throw new Error(`Route group source must be a file or directory: ${source}`);
|
|
674
|
-
return this.readGroupDirectory(resolved);
|
|
675
|
-
}
|
|
676
|
-
static async readGroupDirectory(directory) {
|
|
677
|
-
const entries = await (0, node_fs_promises.readdir)(directory, { withFileTypes: true });
|
|
678
|
-
const files = [];
|
|
679
|
-
for (const entry of entries.sort((left, right) => left.name.localeCompare(right.name))) {
|
|
680
|
-
const path = (0, node_path.join)(directory, entry.name);
|
|
681
|
-
if (entry.isDirectory()) files.push(...await this.readGroupDirectory(path));
|
|
682
|
-
else if (entry.isFile() && /\.(?:[cm]?ts)$/.test(entry.name) && !entry.name.endsWith(".d.ts")) files.push(path);
|
|
683
|
-
}
|
|
684
|
-
return files;
|
|
685
|
-
}
|
|
686
658
|
/**
|
|
687
659
|
* Adds global middlewares to the router, which will be applied to all routes.
|
|
688
660
|
*
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { ApiResourceMiddleware, HttpMethod, ResourceAction, RouteGroupSource, RouterConfig } from "../types/basic.cjs";
|
|
2
1
|
import { Response } from "./Response.cjs";
|
|
3
2
|
import { Route } from "../Route.cjs";
|
|
3
|
+
import { ApiResourceMiddleware, HttpMethod, ResourceAction, RouteGroupContext, RouteGroupSource, RouterConfig } from "../types/basic.cjs";
|
|
4
4
|
import { Request } from "./Request.cjs";
|
|
5
5
|
import { ClearRouterPluginArgumentsContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, PluginArgumentsResolver, PluginBind } from "./plugins.cjs";
|
|
6
6
|
import { Controller } from "../Controller.cjs";
|
|
7
7
|
import { ResourceRoutes } from "../ResourceRoutes.cjs";
|
|
8
|
+
import { RouteGroup } from "../RouteGroup.cjs";
|
|
8
9
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
9
10
|
|
|
10
|
-
//#region src/core/
|
|
11
|
+
//#region src/core/CoreRouter.d.ts
|
|
11
12
|
/**
|
|
12
13
|
* @class clear-router CoreRouter
|
|
13
14
|
* @description Core routing logic for clear-router, shared between all supported adapters (Express.js, H3, etc.)
|
|
@@ -26,10 +27,7 @@ declare abstract class CoreRouter {
|
|
|
26
27
|
private static requestProvider?;
|
|
27
28
|
private static responseProvider?;
|
|
28
29
|
static config: RouterConfig;
|
|
29
|
-
protected static groupContext: AsyncLocalStorage<
|
|
30
|
-
prefix: string;
|
|
31
|
-
groupMiddlewares: any[];
|
|
32
|
-
}>;
|
|
30
|
+
protected static groupContext: AsyncLocalStorage<RouteGroupContext>;
|
|
33
31
|
protected static pluginRequestContext: AsyncLocalStorage<ClearRouterPluginRequestContext<any>>;
|
|
34
32
|
static routes: Set<Route<any, any, any>>;
|
|
35
33
|
static routesByPathMethod: Map<string, Route<any, any, any>>;
|
|
@@ -65,10 +63,7 @@ declare abstract class CoreRouter {
|
|
|
65
63
|
protected static getPluginHttpCtxResolvers(): Set<PluginArgumentsResolver>;
|
|
66
64
|
protected static createDefaultState(): {
|
|
67
65
|
config: RouterConfig;
|
|
68
|
-
groupContext: AsyncLocalStorage<
|
|
69
|
-
prefix: string;
|
|
70
|
-
groupMiddlewares: any[];
|
|
71
|
-
}>;
|
|
66
|
+
groupContext: AsyncLocalStorage<RouteGroupContext>;
|
|
72
67
|
routes: Set<never>;
|
|
73
68
|
routesByPathMethod: Map<any, any>;
|
|
74
69
|
routesByMethod: Map<any, any>;
|
|
@@ -219,9 +214,7 @@ declare abstract class CoreRouter {
|
|
|
219
214
|
* @param callback
|
|
220
215
|
* @param middlewares
|
|
221
216
|
*/
|
|
222
|
-
static group(prefix: string, source: RouteGroupSource, middlewares?: any[]):
|
|
223
|
-
protected static resolveGroupFiles(source: string): Promise<string[]>;
|
|
224
|
-
protected static readGroupDirectory(directory: string): Promise<string[]>;
|
|
217
|
+
static group(prefix: string, source: RouteGroupSource, middlewares?: any[]): RouteGroup<any, any, any>;
|
|
225
218
|
/**
|
|
226
219
|
* Adds global middlewares to the router, which will be applied to all routes.
|
|
227
220
|
*
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { ApiResourceMiddleware, HttpMethod, ResourceAction, RouteGroupSource, RouterConfig } from "../types/basic.mjs";
|
|
2
1
|
import { Response } from "./Response.mjs";
|
|
3
2
|
import { Route } from "../Route.mjs";
|
|
3
|
+
import { ApiResourceMiddleware, HttpMethod, ResourceAction, RouteGroupContext, RouteGroupSource, RouterConfig } from "../types/basic.mjs";
|
|
4
4
|
import { Request } from "./Request.mjs";
|
|
5
5
|
import { ClearRouterPluginArgumentsContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, PluginArgumentsResolver, PluginBind } from "./plugins.mjs";
|
|
6
6
|
import { Controller } from "../Controller.mjs";
|
|
7
7
|
import { ResourceRoutes } from "../ResourceRoutes.mjs";
|
|
8
|
+
import { RouteGroup } from "../RouteGroup.mjs";
|
|
8
9
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
9
10
|
|
|
10
|
-
//#region src/core/
|
|
11
|
+
//#region src/core/CoreRouter.d.ts
|
|
11
12
|
/**
|
|
12
13
|
* @class clear-router CoreRouter
|
|
13
14
|
* @description Core routing logic for clear-router, shared between all supported adapters (Express.js, H3, etc.)
|
|
@@ -26,10 +27,7 @@ declare abstract class CoreRouter {
|
|
|
26
27
|
private static requestProvider?;
|
|
27
28
|
private static responseProvider?;
|
|
28
29
|
static config: RouterConfig;
|
|
29
|
-
protected static groupContext: AsyncLocalStorage<
|
|
30
|
-
prefix: string;
|
|
31
|
-
groupMiddlewares: any[];
|
|
32
|
-
}>;
|
|
30
|
+
protected static groupContext: AsyncLocalStorage<RouteGroupContext>;
|
|
33
31
|
protected static pluginRequestContext: AsyncLocalStorage<ClearRouterPluginRequestContext<any>>;
|
|
34
32
|
static routes: Set<Route<any, any, any>>;
|
|
35
33
|
static routesByPathMethod: Map<string, Route<any, any, any>>;
|
|
@@ -65,10 +63,7 @@ declare abstract class CoreRouter {
|
|
|
65
63
|
protected static getPluginHttpCtxResolvers(): Set<PluginArgumentsResolver>;
|
|
66
64
|
protected static createDefaultState(): {
|
|
67
65
|
config: RouterConfig;
|
|
68
|
-
groupContext: AsyncLocalStorage<
|
|
69
|
-
prefix: string;
|
|
70
|
-
groupMiddlewares: any[];
|
|
71
|
-
}>;
|
|
66
|
+
groupContext: AsyncLocalStorage<RouteGroupContext>;
|
|
72
67
|
routes: Set<never>;
|
|
73
68
|
routesByPathMethod: Map<any, any>;
|
|
74
69
|
routesByMethod: Map<any, any>;
|
|
@@ -219,9 +214,7 @@ declare abstract class CoreRouter {
|
|
|
219
214
|
* @param callback
|
|
220
215
|
* @param middlewares
|
|
221
216
|
*/
|
|
222
|
-
static group(prefix: string, source: RouteGroupSource, middlewares?: any[]):
|
|
223
|
-
protected static resolveGroupFiles(source: string): Promise<string[]>;
|
|
224
|
-
protected static readGroupDirectory(directory: string): Promise<string[]>;
|
|
217
|
+
static group(prefix: string, source: RouteGroupSource, middlewares?: any[]): RouteGroup<any, any, any>;
|
|
225
218
|
/**
|
|
226
219
|
* Adds global middlewares to the router, which will be applied to all routes.
|
|
227
220
|
*
|
|
@@ -3,13 +3,11 @@ import { Response } from "./Response.mjs";
|
|
|
3
3
|
import { Container, getBindingMetadataFromTargets, getDesignParamTypes, getStandardMetadata, isClass } from "./bindings.mjs";
|
|
4
4
|
import { ResourceRoutes } from "../ResourceRoutes.mjs";
|
|
5
5
|
import { Route } from "../Route.mjs";
|
|
6
|
-
import {
|
|
6
|
+
import { RouteGroup } from "../RouteGroup.mjs";
|
|
7
7
|
import { createRequire } from "node:module";
|
|
8
|
-
import { isAbsolute, join, resolve } from "node:path";
|
|
9
|
-
import { readdir, stat } from "node:fs/promises";
|
|
10
8
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
11
9
|
|
|
12
|
-
//#region src/core/
|
|
10
|
+
//#region src/core/CoreRouter.ts
|
|
13
11
|
/**
|
|
14
12
|
* @class clear-router CoreRouter
|
|
15
13
|
* @description Core routing logic for clear-router, shared between all supported adapters (Express.js, H3, etc.)
|
|
@@ -533,6 +531,7 @@ var CoreRouter = class {
|
|
|
533
531
|
});
|
|
534
532
|
if (!methods.includes("options") && !this.routesByPathMethod.get(`OPTIONS ${fullPath}`)) this.options(path, this.createDefaultOptionsHandler());
|
|
535
533
|
this.routes.add(route);
|
|
534
|
+
for (const collector of context?.routeCollectors ?? []) collector.add(route);
|
|
536
535
|
for (const method of methods.map((m) => m.toUpperCase())) {
|
|
537
536
|
this.routesByPathMethod.set(`${method} ${fullPath}`, route);
|
|
538
537
|
if (!this.routesByMethod.has(method)) this.routesByMethod.set(method, []);
|
|
@@ -643,46 +642,19 @@ var CoreRouter = class {
|
|
|
643
642
|
* @param callback
|
|
644
643
|
* @param middlewares
|
|
645
644
|
*/
|
|
646
|
-
static
|
|
645
|
+
static group(prefix, source, middlewares) {
|
|
647
646
|
this.ensureState();
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
if (typeof source === "function") {
|
|
658
|
-
await Promise.resolve(source());
|
|
659
|
-
return;
|
|
660
|
-
}
|
|
661
|
-
for (const file of await this.resolveGroupFiles(source)) await importFile(file);
|
|
647
|
+
return new RouteGroup({
|
|
648
|
+
prefix,
|
|
649
|
+
source,
|
|
650
|
+
middlewares,
|
|
651
|
+
context: this.groupContext,
|
|
652
|
+
defaultPrefix: this.prefix,
|
|
653
|
+
defaultMiddlewares: this.groupMiddlewares,
|
|
654
|
+
normalizePath: (path) => this.normalizePath(path),
|
|
655
|
+
removeRoute: (route) => this.removeRoute(route)
|
|
662
656
|
});
|
|
663
657
|
}
|
|
664
|
-
static async resolveGroupFiles(source) {
|
|
665
|
-
const resolved = isAbsolute(source) ? source : resolve(process.cwd(), source);
|
|
666
|
-
let sourceStat;
|
|
667
|
-
try {
|
|
668
|
-
sourceStat = await stat(resolved);
|
|
669
|
-
} catch {
|
|
670
|
-
throw new Error(`Route group source not found: ${source}`);
|
|
671
|
-
}
|
|
672
|
-
if (sourceStat.isFile()) return [resolved];
|
|
673
|
-
if (!sourceStat.isDirectory()) throw new Error(`Route group source must be a file or directory: ${source}`);
|
|
674
|
-
return this.readGroupDirectory(resolved);
|
|
675
|
-
}
|
|
676
|
-
static async readGroupDirectory(directory) {
|
|
677
|
-
const entries = await readdir(directory, { withFileTypes: true });
|
|
678
|
-
const files = [];
|
|
679
|
-
for (const entry of entries.sort((left, right) => left.name.localeCompare(right.name))) {
|
|
680
|
-
const path = join(directory, entry.name);
|
|
681
|
-
if (entry.isDirectory()) files.push(...await this.readGroupDirectory(path));
|
|
682
|
-
else if (entry.isFile() && /\.(?:[cm]?ts)$/.test(entry.name) && !entry.name.endsWith(".d.ts")) files.push(path);
|
|
683
|
-
}
|
|
684
|
-
return files;
|
|
685
|
-
}
|
|
686
658
|
/**
|
|
687
659
|
* Adds global middlewares to the router, which will be applied to all routes.
|
|
688
660
|
*
|
package/dist/core/Request.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { HttpMethod, RequestData } from "../types/basic.cjs";
|
|
2
|
-
import { Route } from "../Route.cjs";
|
|
3
1
|
import { ClearRequest } from "../ClearRequest.cjs";
|
|
2
|
+
import { Route } from "../Route.cjs";
|
|
3
|
+
import { HttpMethod, RequestData } from "../types/basic.cjs";
|
|
4
4
|
|
|
5
5
|
//#region src/core/Request.d.ts
|
|
6
6
|
declare class Request<X = any, M = any> extends ClearRequest<X, M> {
|
package/dist/core/Request.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { HttpMethod, RequestData } from "../types/basic.mjs";
|
|
2
|
-
import { Route } from "../Route.mjs";
|
|
3
1
|
import { ClearRequest } from "../ClearRequest.mjs";
|
|
2
|
+
import { Route } from "../Route.mjs";
|
|
3
|
+
import { HttpMethod, RequestData } from "../types/basic.mjs";
|
|
4
4
|
|
|
5
5
|
//#region src/core/Request.d.ts
|
|
6
6
|
declare class Request<X = any, M = any> extends ClearRequest<X, M> {
|
package/dist/core/helpers.d.mts
CHANGED
package/dist/core/index.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
const require_helpers = require('./helpers.cjs');
|
|
2
|
+
const require_RouteGroup = require('../RouteGroup.cjs');
|
|
1
3
|
const require_Request = require('./Request.cjs');
|
|
2
4
|
const require_Response = require('./Response.cjs');
|
|
3
5
|
const require_plugins = require('./plugins.cjs');
|
|
4
|
-
const
|
|
5
|
-
const require_router = require('./router.cjs');
|
|
6
|
+
const require_CoreRouter = require('./CoreRouter.cjs');
|
package/dist/core/index.d.cts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Response } from "./Response.cjs";
|
|
2
2
|
import { Request } from "./Request.cjs";
|
|
3
3
|
import { ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, definePlugin } from "./plugins.cjs";
|
|
4
|
-
import {
|
|
4
|
+
import { RouteGroup } from "../RouteGroup.cjs";
|
|
5
|
+
import { CoreRouter } from "./CoreRouter.cjs";
|
|
5
6
|
import { importFile, wrap } from "./helpers.cjs";
|
|
6
|
-
export { ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, CoreRouter, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, Request, Response, definePlugin, importFile, wrap };
|
|
7
|
+
export { ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, CoreRouter, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, Request, Response, RouteGroup, definePlugin, importFile, wrap };
|
package/dist/core/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Response } from "./Response.mjs";
|
|
2
2
|
import { Request } from "./Request.mjs";
|
|
3
3
|
import { ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, definePlugin } from "./plugins.mjs";
|
|
4
|
-
import {
|
|
4
|
+
import { RouteGroup } from "../RouteGroup.mjs";
|
|
5
|
+
import { CoreRouter } from "./CoreRouter.mjs";
|
|
5
6
|
import { importFile, wrap } from "./helpers.mjs";
|
|
6
|
-
export { ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, CoreRouter, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, Request, Response, definePlugin, importFile, wrap };
|
|
7
|
+
export { ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, CoreRouter, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, Request, Response, RouteGroup, definePlugin, importFile, wrap };
|
package/dist/core/index.mjs
CHANGED
|
@@ -2,6 +2,7 @@ import { Request } from "./Request.mjs";
|
|
|
2
2
|
import { Response } from "./Response.mjs";
|
|
3
3
|
import { definePlugin } from "./plugins.mjs";
|
|
4
4
|
import { importFile, wrap } from "./helpers.mjs";
|
|
5
|
-
import {
|
|
5
|
+
import { RouteGroup } from "../RouteGroup.mjs";
|
|
6
|
+
import { CoreRouter } from "./CoreRouter.mjs";
|
|
6
7
|
|
|
7
|
-
export { CoreRouter, Request, Response, definePlugin, importFile, wrap };
|
|
8
|
+
export { CoreRouter, Request, Response, RouteGroup, definePlugin, importFile, wrap };
|
package/dist/core/plugins.d.cts
CHANGED
package/dist/core/plugins.d.mts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
2
|
const require_bindings = require('../core/bindings.cjs');
|
|
3
|
-
const
|
|
3
|
+
const require_CoreRouter = require('../core/CoreRouter.cjs');
|
|
4
4
|
require('./index.cjs');
|
|
5
5
|
require("reflect-metadata");
|
|
6
6
|
|
|
7
7
|
//#region src/decorators/setup.ts
|
|
8
|
-
|
|
8
|
+
require_CoreRouter.CoreRouter.configureDefaults({ container: {
|
|
9
9
|
enabled: true,
|
|
10
10
|
autoDiscover: true
|
|
11
11
|
} });
|
package/dist/express/router.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_CoreRouter = require('../core/CoreRouter.cjs');
|
|
2
2
|
const require_responses = require('../core/responses.cjs');
|
|
3
3
|
|
|
4
4
|
//#region src/express/router.ts
|
|
@@ -9,7 +9,7 @@ const require_responses = require('../core/responses.cjs');
|
|
|
9
9
|
* @author 3m1n3nc3
|
|
10
10
|
* @repository https://github.com/arkstack-tmp/clear-router
|
|
11
11
|
*/
|
|
12
|
-
var Router = class Router extends
|
|
12
|
+
var Router = class Router extends require_CoreRouter.CoreRouter {
|
|
13
13
|
static routerStateNamespace = "clear-router:express";
|
|
14
14
|
static ensureRequestBodyAccessor(req) {
|
|
15
15
|
if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
|
|
@@ -141,8 +141,8 @@ var Router = class Router extends require_router.CoreRouter {
|
|
|
141
141
|
* @param callback
|
|
142
142
|
* @param middlewares
|
|
143
143
|
*/
|
|
144
|
-
static
|
|
145
|
-
|
|
144
|
+
static group(prefix, source, middlewares) {
|
|
145
|
+
return super.group(prefix, source, middlewares);
|
|
146
146
|
}
|
|
147
147
|
/**
|
|
148
148
|
* Adds global middlewares to the router, which will be applied to all routes.
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { ApiResourceMiddleware, HttpMethod, ResourceAction, RouteGroupSource } from "../types/basic.cjs";
|
|
2
1
|
import { Handler, HttpContext, Middleware } from "../types/express.cjs";
|
|
3
2
|
import { Route } from "../Route.cjs";
|
|
3
|
+
import { ApiResourceMiddleware, HttpMethod, ResourceAction, RouteGroupSource } from "../types/basic.cjs";
|
|
4
4
|
import { ResourceRoutes } from "../ResourceRoutes.cjs";
|
|
5
|
-
import {
|
|
5
|
+
import { RouteGroup } from "../RouteGroup.cjs";
|
|
6
|
+
import { CoreRouter } from "../core/CoreRouter.cjs";
|
|
6
7
|
import { Router } from "express";
|
|
7
8
|
|
|
8
9
|
//#region src/express/router.d.ts
|
|
@@ -101,7 +102,7 @@ declare class Router$1 extends CoreRouter {
|
|
|
101
102
|
* @param callback
|
|
102
103
|
* @param middlewares
|
|
103
104
|
*/
|
|
104
|
-
static group(prefix: string, source: RouteGroupSource, middlewares?: Middleware[]):
|
|
105
|
+
static group(prefix: string, source: RouteGroupSource, middlewares?: Middleware[]): RouteGroup<HttpContext, Middleware, Handler>;
|
|
105
106
|
/**
|
|
106
107
|
* Adds global middlewares to the router, which will be applied to all routes.
|
|
107
108
|
*
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { ApiResourceMiddleware, HttpMethod, ResourceAction, RouteGroupSource } from "../types/basic.mjs";
|
|
2
1
|
import { Handler, HttpContext, Middleware } from "../types/express.mjs";
|
|
3
2
|
import { Route } from "../Route.mjs";
|
|
3
|
+
import { ApiResourceMiddleware, HttpMethod, ResourceAction, RouteGroupSource } from "../types/basic.mjs";
|
|
4
4
|
import { ResourceRoutes } from "../ResourceRoutes.mjs";
|
|
5
|
-
import {
|
|
5
|
+
import { RouteGroup } from "../RouteGroup.mjs";
|
|
6
|
+
import { CoreRouter } from "../core/CoreRouter.mjs";
|
|
6
7
|
import { Router } from "express";
|
|
7
8
|
|
|
8
9
|
//#region src/express/router.d.ts
|
|
@@ -101,7 +102,7 @@ declare class Router$1 extends CoreRouter {
|
|
|
101
102
|
* @param callback
|
|
102
103
|
* @param middlewares
|
|
103
104
|
*/
|
|
104
|
-
static group(prefix: string, source: RouteGroupSource, middlewares?: Middleware[]):
|
|
105
|
+
static group(prefix: string, source: RouteGroupSource, middlewares?: Middleware[]): RouteGroup<HttpContext, Middleware, Handler>;
|
|
105
106
|
/**
|
|
106
107
|
* Adds global middlewares to the router, which will be applied to all routes.
|
|
107
108
|
*
|
package/dist/express/router.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CoreRouter } from "../core/
|
|
1
|
+
import { CoreRouter } from "../core/CoreRouter.mjs";
|
|
2
2
|
import { isFetchResponse, resolveResponseMeta, responseWasSent } from "../core/responses.mjs";
|
|
3
3
|
|
|
4
4
|
//#region src/express/router.ts
|
|
@@ -141,8 +141,8 @@ var Router = class Router extends CoreRouter {
|
|
|
141
141
|
* @param callback
|
|
142
142
|
* @param middlewares
|
|
143
143
|
*/
|
|
144
|
-
static
|
|
145
|
-
|
|
144
|
+
static group(prefix, source, middlewares) {
|
|
145
|
+
return super.group(prefix, source, middlewares);
|
|
146
146
|
}
|
|
147
147
|
/**
|
|
148
148
|
* Adds global middlewares to the router, which will be applied to all routes.
|
package/dist/fastify/router.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const require_CoreRouter = require('../core/CoreRouter.cjs');
|
|
2
2
|
const require_responses = require('../core/responses.cjs');
|
|
3
3
|
|
|
4
4
|
//#region src/fastify/router.ts
|
|
@@ -8,7 +8,7 @@ const require_responses = require('../core/responses.cjs');
|
|
|
8
8
|
* @author 3m1n3nc3
|
|
9
9
|
* @repository https://github.com/arkstack-tmp/clear-router
|
|
10
10
|
*/
|
|
11
|
-
var Router = class Router extends
|
|
11
|
+
var Router = class Router extends require_CoreRouter.CoreRouter {
|
|
12
12
|
static routerStateNamespace = "clear-router:fastify";
|
|
13
13
|
static ensureRequestBodyAccessor(req) {
|
|
14
14
|
if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
|
|
@@ -134,8 +134,8 @@ var Router = class Router extends require_router.CoreRouter {
|
|
|
134
134
|
* @param callback
|
|
135
135
|
* @param middlewares
|
|
136
136
|
*/
|
|
137
|
-
static
|
|
138
|
-
|
|
137
|
+
static group(prefix, source, middlewares) {
|
|
138
|
+
return super.group(prefix, source, middlewares);
|
|
139
139
|
}
|
|
140
140
|
/**
|
|
141
141
|
* Apply middlewares to a group of routes defined within the callback
|