clear-router 2.8.1 → 2.8.3

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.
Files changed (72) hide show
  1. package/dist/ClearRequest.d.cts +5 -5
  2. package/dist/ClearRequest.d.mts +1 -1
  3. package/dist/Controller.d.mts +1 -1
  4. package/dist/ResourceRouteSelection.d.cts +3 -3
  5. package/dist/ResourceRoutes.cjs +12 -0
  6. package/dist/ResourceRoutes.d.cts +16 -22
  7. package/dist/ResourceRoutes.d.mts +13 -19
  8. package/dist/ResourceRoutes.mjs +12 -0
  9. package/dist/{Request-Ci0UQ-Vl.d.mts → Route-tq-rge1g.d.mts} +46 -46
  10. package/dist/Route.d.cts +4 -4
  11. package/dist/Route.d.mts +1 -1
  12. package/dist/RouteGroup.cjs +120 -0
  13. package/dist/RouteGroup.d.cts +64 -0
  14. package/dist/RouteGroup.d.mts +64 -0
  15. package/dist/RouteGroup.mjs +120 -0
  16. package/dist/core/{router.cjs → CoreRouter.cjs} +13 -12
  17. package/dist/core/{router.d.cts → CoreRouter.d.cts} +8 -13
  18. package/dist/core/{router.d.mts → CoreRouter.d.mts} +6 -11
  19. package/dist/core/{router.mjs → CoreRouter.mjs} +13 -12
  20. package/dist/core/Request.d.cts +1 -1
  21. package/dist/core/Request.d.mts +2 -2
  22. package/dist/core/bindings.d.cts +1 -6
  23. package/dist/core/helpers.cjs +23 -0
  24. package/dist/core/helpers.d.cts +15 -1
  25. package/dist/core/helpers.d.mts +15 -1
  26. package/dist/core/helpers.mjs +24 -1
  27. package/dist/core/index.cjs +3 -9
  28. package/dist/core/index.d.cts +4 -3
  29. package/dist/core/index.d.mts +4 -3
  30. package/dist/core/index.mjs +4 -3
  31. package/dist/core/plugins.d.cts +2 -2
  32. package/dist/core/plugins.d.mts +1 -1
  33. package/dist/decorators/setup.cjs +2 -2
  34. package/dist/decorators/setup.mjs +1 -1
  35. package/dist/express/router.cjs +4 -4
  36. package/dist/express/router.d.cts +4 -3
  37. package/dist/express/router.d.mts +4 -3
  38. package/dist/express/router.mjs +3 -3
  39. package/dist/fastify/router.cjs +4 -4
  40. package/dist/fastify/router.d.cts +4 -3
  41. package/dist/fastify/router.d.mts +4 -3
  42. package/dist/fastify/router.mjs +3 -3
  43. package/dist/h3/router.cjs +4 -4
  44. package/dist/h3/router.d.cts +4 -3
  45. package/dist/h3/router.d.mts +4 -3
  46. package/dist/h3/router.mjs +3 -3
  47. package/dist/hono/router.cjs +4 -4
  48. package/dist/hono/router.d.cts +4 -3
  49. package/dist/hono/router.d.mts +4 -3
  50. package/dist/hono/router.mjs +3 -3
  51. package/dist/index.cjs +6 -3
  52. package/dist/index.d.cts +5 -4
  53. package/dist/index.d.mts +5 -4
  54. package/dist/index.mjs +4 -3
  55. package/dist/koa/router.cjs +4 -4
  56. package/dist/koa/router.d.cts +4 -3
  57. package/dist/koa/router.d.mts +4 -3
  58. package/dist/koa/router.mjs +3 -3
  59. package/dist/types/basic.d.cts +49 -5
  60. package/dist/types/basic.d.mts +49 -1
  61. package/dist/types/express.d.cts +2 -36
  62. package/dist/types/express.d.mts +1 -1
  63. package/dist/types/fastify.d.cts +3 -15
  64. package/dist/types/fastify.d.mts +1 -1
  65. package/dist/types/h3.d.cts +3 -38
  66. package/dist/types/h3.d.mts +1 -1
  67. package/dist/types/hono.d.cts +3 -17
  68. package/dist/types/hono.d.mts +1 -1
  69. package/dist/types/index.d.mts +1 -0
  70. package/dist/types/koa.d.cts +2 -20
  71. package/dist/types/koa.d.mts +1 -1
  72. package/package.json +5 -2
@@ -0,0 +1,120 @@
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
+ registration;
15
+ routes = /* @__PURE__ */ new Set();
16
+ constructor(options) {
17
+ this.options = options;
18
+ this.registration = this.register();
19
+ }
20
+ /**
21
+ * Returning a falsy value will stop route group registration
22
+ *
23
+ * @param condition
24
+ * @returns
25
+ */
26
+ when(condition) {
27
+ this.checks.push(this.registration.then(async () => {
28
+ if (!await condition(this.options.source)) this.rollback();
29
+ }));
30
+ return this;
31
+ }
32
+ /**
33
+ * Register one or more middleware that will be executed before every route in the group.
34
+ *
35
+ * @param middlewares
36
+ * @returns
37
+ */
38
+ middleware(middlewares) {
39
+ this.checks.push(this.registration.then(() => {
40
+ for (const route of this.routes) route.middleware(middlewares);
41
+ }));
42
+ return this;
43
+ }
44
+ /**
45
+ * Attaches callbacks for the resolution and/or rejection of the RouteGroup.
46
+ *
47
+ * @param onfulfilled
48
+ * @param onrejected
49
+ * @returns
50
+ */
51
+ then(onfulfilled, onrejected) {
52
+ return Promise.all([this.registration, ...this.checks]).then(() => void 0).then(onfulfilled, onrejected);
53
+ }
54
+ /**
55
+ * Register the routes
56
+ */
57
+ async register() {
58
+ const current = this.options.context.getStore();
59
+ const previousPrefix = current?.prefix ?? this.options.defaultPrefix;
60
+ const previousMiddlewares = current?.groupMiddlewares ?? this.options.defaultMiddlewares;
61
+ const fullPrefix = [previousPrefix, this.options.prefix].filter(Boolean).join("/");
62
+ const nextContext = {
63
+ prefix: this.options.normalizePath(fullPrefix),
64
+ groupMiddlewares: [...previousMiddlewares, ...this.options.middlewares || []],
65
+ routeCollectors: [...current?.routeCollectors ?? [], this.routes]
66
+ };
67
+ await this.options.context.run(nextContext, async () => {
68
+ for (const entry of Array.isArray(this.options.source) ? this.options.source : [this.options.source]) {
69
+ if (typeof entry === "function") {
70
+ await Promise.resolve(entry());
71
+ continue;
72
+ }
73
+ for (const file of await this.resolveFiles(entry)) await importFile(file);
74
+ }
75
+ });
76
+ }
77
+ /**
78
+ * Rollback the route registrations
79
+ */
80
+ rollback() {
81
+ for (const route of this.routes) this.options.removeRoute(route);
82
+ }
83
+ /**
84
+ * Resolve files from the group path
85
+ *
86
+ * @param source
87
+ * @returns
88
+ */
89
+ async resolveFiles(source) {
90
+ const resolved = isAbsolute(source) ? source : resolve(process.cwd(), source);
91
+ let sourceStat;
92
+ try {
93
+ sourceStat = await stat(resolved);
94
+ } catch {
95
+ throw new Error(`Route group source not found: ${source}`);
96
+ }
97
+ if (sourceStat.isFile()) return [resolved];
98
+ if (!sourceStat.isDirectory()) throw new Error(`Route group source must be a file or directory: ${source}`);
99
+ return this.readDirectory(resolved);
100
+ }
101
+ /**
102
+ * Read all the files in the configured directory
103
+ *
104
+ * @param directory
105
+ * @returns
106
+ */
107
+ async readDirectory(directory) {
108
+ const entries = await readdir(directory, { withFileTypes: true });
109
+ const files = [];
110
+ for (const entry of entries.sort((left, right) => left.name.localeCompare(right.name))) {
111
+ const path = join(directory, entry.name);
112
+ if (entry.isDirectory()) files.push(...await this.readDirectory(path));
113
+ else if (entry.isFile() && /\.(?:[cm]?ts)$/.test(entry.name) && !entry.name.endsWith(".d.ts")) files.push(path);
114
+ }
115
+ return files;
116
+ }
117
+ };
118
+
119
+ //#endregion
120
+ export { RouteGroup };
@@ -3,10 +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 require_RouteGroup = require('../RouteGroup.cjs');
6
7
  let node_async_hooks = require("node:async_hooks");
7
8
  let node_module = require("node:module");
8
9
 
9
- //#region src/core/router.ts
10
+ //#region src/core/CoreRouter.ts
10
11
  /**
11
12
  * @class clear-router CoreRouter
12
13
  * @description Core routing logic for clear-router, shared between all supported adapters (Express.js, H3, etc.)
@@ -530,6 +531,7 @@ var CoreRouter = class {
530
531
  });
531
532
  if (!methods.includes("options") && !this.routesByPathMethod.get(`OPTIONS ${fullPath}`)) this.options(path, this.createDefaultOptionsHandler());
532
533
  this.routes.add(route);
534
+ for (const collector of context?.routeCollectors ?? []) collector.add(route);
533
535
  for (const method of methods.map((m) => m.toUpperCase())) {
534
536
  this.routesByPathMethod.set(`${method} ${fullPath}`, route);
535
537
  if (!this.routesByMethod.has(method)) this.routesByMethod.set(method, []);
@@ -640,18 +642,17 @@ var CoreRouter = class {
640
642
  * @param callback
641
643
  * @param middlewares
642
644
  */
643
- static async group(prefix, callback, middlewares) {
645
+ static group(prefix, source, middlewares) {
644
646
  this.ensureState();
645
- const context = this.groupContext.getStore();
646
- const previousPrefix = context?.prefix ?? this.prefix;
647
- const previousMiddlewares = context?.groupMiddlewares ?? this.groupMiddlewares;
648
- const fullPrefix = [previousPrefix, prefix].filter(Boolean).join("/");
649
- const nextContext = {
650
- prefix: this.normalizePath(fullPrefix),
651
- groupMiddlewares: [...previousMiddlewares, ...middlewares || []]
652
- };
653
- await this.groupContext.run(nextContext, async () => {
654
- await Promise.resolve(callback());
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)
655
656
  });
656
657
  }
657
658
  /**
@@ -1,13 +1,14 @@
1
- import { ApiResourceMiddleware, HttpMethod, ResourceAction, RouterConfig } from "../types/basic.cjs";
1
+ import { Request } from "./Request.cjs";
2
2
  import { Response } from "./Response.cjs";
3
3
  import { Route } from "../Route.cjs";
4
- import { Request } from "./Request.cjs";
5
- import { ClearRouterPluginArgumentsContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, PluginArgumentsResolver, PluginBind } from "./plugins.cjs";
4
+ import { ApiResourceMiddleware, HttpMethod, ResourceAction, RouteGroupContext, RouteGroupSource, RouterConfig } from "../types/basic.cjs";
6
5
  import { Controller } from "../Controller.cjs";
6
+ import { RouteGroup } from "../RouteGroup.cjs";
7
+ import { ClearRouterPluginArgumentsContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, PluginArgumentsResolver, PluginBind } from "./plugins.cjs";
7
8
  import { ResourceRoutes } from "../ResourceRoutes.cjs";
8
9
  import { AsyncLocalStorage } from "node:async_hooks";
9
10
 
10
- //#region src/core/router.d.ts
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,7 +214,7 @@ declare abstract class CoreRouter {
219
214
  * @param callback
220
215
  * @param middlewares
221
216
  */
222
- static group(prefix: string, callback: () => void | Promise<void>, middlewares?: any[]): Promise<void>;
217
+ static group(prefix: string, source: RouteGroupSource, middlewares?: any[]): RouteGroup<any, any, any>;
223
218
  /**
224
219
  * Adds global middlewares to the router, which will be applied to all routes.
225
220
  *
@@ -1,13 +1,14 @@
1
- import { ApiResourceMiddleware, HttpMethod, ResourceAction, 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/router.d.ts
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,7 +214,7 @@ declare abstract class CoreRouter {
219
214
  * @param callback
220
215
  * @param middlewares
221
216
  */
222
- static group(prefix: string, callback: () => void | Promise<void>, middlewares?: any[]): Promise<void>;
217
+ static group(prefix: string, source: RouteGroupSource, middlewares?: any[]): RouteGroup<any, any, any>;
223
218
  /**
224
219
  * Adds global middlewares to the router, which will be applied to all routes.
225
220
  *
@@ -3,10 +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 { RouteGroup } from "../RouteGroup.mjs";
6
7
  import { createRequire } from "node:module";
7
8
  import { AsyncLocalStorage } from "node:async_hooks";
8
9
 
9
- //#region src/core/router.ts
10
+ //#region src/core/CoreRouter.ts
10
11
  /**
11
12
  * @class clear-router CoreRouter
12
13
  * @description Core routing logic for clear-router, shared between all supported adapters (Express.js, H3, etc.)
@@ -530,6 +531,7 @@ var CoreRouter = class {
530
531
  });
531
532
  if (!methods.includes("options") && !this.routesByPathMethod.get(`OPTIONS ${fullPath}`)) this.options(path, this.createDefaultOptionsHandler());
532
533
  this.routes.add(route);
534
+ for (const collector of context?.routeCollectors ?? []) collector.add(route);
533
535
  for (const method of methods.map((m) => m.toUpperCase())) {
534
536
  this.routesByPathMethod.set(`${method} ${fullPath}`, route);
535
537
  if (!this.routesByMethod.has(method)) this.routesByMethod.set(method, []);
@@ -640,18 +642,17 @@ var CoreRouter = class {
640
642
  * @param callback
641
643
  * @param middlewares
642
644
  */
643
- static async group(prefix, callback, middlewares) {
645
+ static group(prefix, source, middlewares) {
644
646
  this.ensureState();
645
- const context = this.groupContext.getStore();
646
- const previousPrefix = context?.prefix ?? this.prefix;
647
- const previousMiddlewares = context?.groupMiddlewares ?? this.groupMiddlewares;
648
- const fullPrefix = [previousPrefix, prefix].filter(Boolean).join("/");
649
- const nextContext = {
650
- prefix: this.normalizePath(fullPrefix),
651
- groupMiddlewares: [...previousMiddlewares, ...middlewares || []]
652
- };
653
- await this.groupContext.run(nextContext, async () => {
654
- await Promise.resolve(callback());
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)
655
656
  });
656
657
  }
657
658
  /**
@@ -1,5 +1,5 @@
1
- import { HttpMethod, RequestData } from "../types/basic.cjs";
2
1
  import { Route } from "../Route.cjs";
2
+ import { HttpMethod, RequestData } from "../types/basic.cjs";
3
3
  import { ClearRequest } from "../ClearRequest.cjs";
4
4
 
5
5
  //#region src/core/Request.d.ts
@@ -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> {
@@ -2,10 +2,6 @@
2
2
  type BindToken<T = any> = abstract new (...args: any[]) => T;
3
3
  type BindFactory<T = any> = (ctx: any) => T | Promise<T>;
4
4
  type BindValue<T = any> = T | BindFactory<T> | BindToken<T>;
5
- type BindDecorator = MethodDecorator & ClassDecorator & {
6
- <This, Value extends (this: This, ...args: any[]) => any>(value: Value, context: ClassMethodDecoratorContext<This, Value>): void | Value;
7
- <Value extends abstract new (...args: any[]) => any>(value: Value, context: ClassDecoratorContext<Value>): void | Value;
8
- };
9
5
  declare class Container {
10
6
  private static readonly registry;
11
7
  static bind<T>(token: BindToken<T>, value: BindValue<T>): void;
@@ -20,6 +16,5 @@ declare class Container {
20
16
  private static staticPropsMatch;
21
17
  private static resolveBinding;
22
18
  }
23
- declare function Bind(...tokens: BindToken[]): BindDecorator;
24
19
  //#endregion
25
- export { Bind, BindDecorator, BindFactory, BindToken, BindValue, Container };
20
+ export { BindToken, BindValue, Container };
@@ -1,9 +1,32 @@
1
+ let node_path = require("node:path");
2
+ let jiti = require("jiti");
3
+ let node_url = require("node:url");
1
4
 
2
5
  //#region src/core/helpers.ts
3
6
  const wrap = (value) => {
4
7
  if (value === null || value === void 0) return [];
5
8
  return Array.isArray(value) ? value : [value];
6
9
  };
10
+ /**
11
+ *
12
+ * Dynamically imports a file at the given path with full TypeScript support,
13
+ * including `tsconfig.json` path aliases.
14
+ *
15
+ * @param filePath - The path to the file to import.
16
+ * @returns The imported module typed as `T`.
17
+ *
18
+ * @example
19
+ * const config = await importFile<AppConfig>('./config/app.ts')
20
+ */
21
+ const importFile = async (filePath, userOptions, resolveOptions) => {
22
+ const resolvedPath = (0, node_path.resolve)(filePath);
23
+ return await (0, jiti.createJiti)((0, node_url.pathToFileURL)(resolvedPath).href, {
24
+ ...userOptions,
25
+ interopDefault: false,
26
+ tsconfigPaths: true
27
+ }).import(resolvedPath, resolveOptions);
28
+ };
7
29
 
8
30
  //#endregion
31
+ exports.importFile = importFile;
9
32
  exports.wrap = wrap;
@@ -1,4 +1,18 @@
1
+ import { FileImporter } from "../types/basic.cjs";
2
+
1
3
  //#region src/core/helpers.d.ts
2
4
  declare const wrap: <T>(value: T | T[] | null | undefined) => T[];
5
+ /**
6
+ *
7
+ * Dynamically imports a file at the given path with full TypeScript support,
8
+ * including `tsconfig.json` path aliases.
9
+ *
10
+ * @param filePath - The path to the file to import.
11
+ * @returns The imported module typed as `T`.
12
+ *
13
+ * @example
14
+ * const config = await importFile<AppConfig>('./config/app.ts')
15
+ */
16
+ declare const importFile: FileImporter;
3
17
  //#endregion
4
- export { wrap };
18
+ export { importFile, wrap };
@@ -1,4 +1,18 @@
1
+ import { FileImporter } from "../types/basic.mjs";
2
+
1
3
  //#region src/core/helpers.d.ts
2
4
  declare const wrap: <T>(value: T | T[] | null | undefined) => T[];
5
+ /**
6
+ *
7
+ * Dynamically imports a file at the given path with full TypeScript support,
8
+ * including `tsconfig.json` path aliases.
9
+ *
10
+ * @param filePath - The path to the file to import.
11
+ * @returns The imported module typed as `T`.
12
+ *
13
+ * @example
14
+ * const config = await importFile<AppConfig>('./config/app.ts')
15
+ */
16
+ declare const importFile: FileImporter;
3
17
  //#endregion
4
- export { wrap };
18
+ export { importFile, wrap };
@@ -1,8 +1,31 @@
1
+ import { resolve } from "node:path";
2
+ import { createJiti } from "jiti";
3
+ import { pathToFileURL } from "node:url";
4
+
1
5
  //#region src/core/helpers.ts
2
6
  const wrap = (value) => {
3
7
  if (value === null || value === void 0) return [];
4
8
  return Array.isArray(value) ? value : [value];
5
9
  };
10
+ /**
11
+ *
12
+ * Dynamically imports a file at the given path with full TypeScript support,
13
+ * including `tsconfig.json` path aliases.
14
+ *
15
+ * @param filePath - The path to the file to import.
16
+ * @returns The imported module typed as `T`.
17
+ *
18
+ * @example
19
+ * const config = await importFile<AppConfig>('./config/app.ts')
20
+ */
21
+ const importFile = async (filePath, userOptions, resolveOptions) => {
22
+ const resolvedPath = resolve(filePath);
23
+ return await createJiti(pathToFileURL(resolvedPath).href, {
24
+ ...userOptions,
25
+ interopDefault: false,
26
+ tsconfigPaths: true
27
+ }).import(resolvedPath, resolveOptions);
28
+ };
6
29
 
7
30
  //#endregion
8
- export { wrap };
31
+ export { importFile, wrap };
@@ -1,12 +1,6 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1
+ const require_helpers = require('./helpers.cjs');
2
+ const require_RouteGroup = require('../RouteGroup.cjs');
2
3
  const require_Request = require('./Request.cjs');
3
4
  const require_Response = require('./Response.cjs');
4
5
  const require_plugins = require('./plugins.cjs');
5
- const require_router = require('./router.cjs');
6
- const require_helpers = require('./helpers.cjs');
7
-
8
- exports.CoreRouter = require_router.CoreRouter;
9
- exports.Request = require_Request.Request;
10
- exports.Response = require_Response.Response;
11
- exports.definePlugin = require_plugins.definePlugin;
12
- exports.wrap = require_helpers.wrap;
6
+ const require_CoreRouter = require('./CoreRouter.cjs');
@@ -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 { CoreRouter } from "./router.cjs";
5
- import { wrap } from "./helpers.cjs";
6
- export { ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, CoreRouter, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, Request, Response, definePlugin, wrap };
4
+ import { RouteGroup } from "../RouteGroup.cjs";
5
+ import { CoreRouter } from "./CoreRouter.cjs";
6
+ import { importFile, wrap } from "./helpers.cjs";
7
+ export { ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, CoreRouter, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, Request, Response, RouteGroup, definePlugin, importFile, wrap };
@@ -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 { CoreRouter } from "./router.mjs";
5
- import { wrap } from "./helpers.mjs";
6
- export { ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, CoreRouter, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, Request, Response, definePlugin, wrap };
4
+ import { RouteGroup } from "../RouteGroup.mjs";
5
+ import { CoreRouter } from "./CoreRouter.mjs";
6
+ import { importFile, wrap } from "./helpers.mjs";
7
+ export { ClearRouterPlugin, ClearRouterPluginArgumentsContext, ClearRouterPluginContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, CoreRouter, PluginArgumentsResolver, PluginBind, PluginBindFactory, PluginBindValue, PluginSetupResult, Request, Response, RouteGroup, definePlugin, importFile, wrap };
@@ -1,7 +1,8 @@
1
1
  import { Request } from "./Request.mjs";
2
2
  import { Response } from "./Response.mjs";
3
3
  import { definePlugin } from "./plugins.mjs";
4
- import { CoreRouter } from "./router.mjs";
5
- import { wrap } from "./helpers.mjs";
4
+ import { importFile, wrap } from "./helpers.mjs";
5
+ import { RouteGroup } from "../RouteGroup.mjs";
6
+ import { CoreRouter } from "./CoreRouter.mjs";
6
7
 
7
- export { CoreRouter, Request, Response, definePlugin, wrap };
8
+ export { CoreRouter, Request, Response, RouteGroup, definePlugin, importFile, wrap };
@@ -1,6 +1,6 @@
1
- import { RouterConfig } from "../types/basic.cjs";
2
- import { Response } from "./Response.cjs";
3
1
  import { Request } from "./Request.cjs";
2
+ import { Response } from "./Response.cjs";
3
+ import { RouterConfig } from "../types/basic.cjs";
4
4
  import { BindToken, BindValue, Container } from "./bindings.cjs";
5
5
 
6
6
  //#region src/core/plugins.d.ts
@@ -1,5 +1,5 @@
1
- import { RouterConfig } from "../types/basic.mjs";
2
1
  import { Response } from "./Response.mjs";
2
+ import { RouterConfig } from "../types/basic.mjs";
3
3
  import { Request } from "./Request.mjs";
4
4
  import { BindToken, BindValue, Container } from "./bindings.mjs";
5
5
 
@@ -1,11 +1,11 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
2
  const require_bindings = require('../core/bindings.cjs');
3
- const require_router = require('../core/router.cjs');
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
- require_router.CoreRouter.configureDefaults({ container: {
8
+ require_CoreRouter.CoreRouter.configureDefaults({ container: {
9
9
  enabled: true,
10
10
  autoDiscover: true
11
11
  } });
@@ -1,5 +1,5 @@
1
1
  import { Bind, Container } from "../core/bindings.mjs";
2
- import { CoreRouter } from "../core/router.mjs";
2
+ import { CoreRouter } from "../core/CoreRouter.mjs";
3
3
  import "./index.mjs";
4
4
  import "reflect-metadata";
5
5
 
@@ -1,4 +1,4 @@
1
- const require_router = require('../core/router.cjs');
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 require_router.CoreRouter {
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 async group(prefix, callback, middlewares) {
145
- await super.group(prefix, callback, middlewares);
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 } 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 { CoreRouter } from "../core/router.cjs";
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, callback: () => void | Promise<void>, middlewares?: Middleware[]): Promise<void>;
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 } 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 { CoreRouter } from "../core/router.mjs";
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, callback: () => void | Promise<void>, middlewares?: Middleware[]): Promise<void>;
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,4 +1,4 @@
1
- import { CoreRouter } from "../core/router.mjs";
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 async group(prefix, callback, middlewares) {
145
- await super.group(prefix, callback, middlewares);
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.