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
@@ -1,13 +1,13 @@
1
+ import { Middleware } from "./types/h3.cjs";
2
+ import { Route } from "./Route.cjs";
1
3
  import { RequestData } from "./types/basic.cjs";
2
- import { Middleware } from "./types/express.cjs";
3
- import { Middleware as Middleware$1 } from "./types/fastify.cjs";
4
- import { Middleware as Middleware$2 } from "./types/h3.cjs";
4
+ import { Middleware as Middleware$1 } from "./types/express.cjs";
5
+ import { Middleware as Middleware$2 } from "./types/fastify.cjs";
5
6
  import { Middleware as Middleware$3 } from "./types/hono.cjs";
6
7
  import { Middleware as Middleware$4 } from "./types/koa.cjs";
7
- import { Route } from "./Route.cjs";
8
8
 
9
9
  //#region src/ClearRequest.d.ts
10
- declare class ClearRequest<X = any, M = Middleware$2 | Middleware | Middleware$1 | Middleware$3 | Middleware$4> {
10
+ declare class ClearRequest<X = any, M = Middleware | Middleware$1 | Middleware$2 | Middleware$3 | Middleware$4> {
11
11
  [key: string]: any;
12
12
  /**
13
13
  * @param body - Parsed request body
@@ -1,10 +1,10 @@
1
- import { RequestData } from "./types/basic.mjs";
2
1
  import { Middleware } from "./types/express.mjs";
3
2
  import { Middleware as Middleware$1 } from "./types/fastify.mjs";
4
3
  import { Middleware as Middleware$2 } from "./types/h3.mjs";
5
4
  import { Middleware as Middleware$3 } from "./types/hono.mjs";
6
5
  import { Middleware as Middleware$4 } from "./types/koa.mjs";
7
6
  import { Route } from "./Route.mjs";
7
+ import { RequestData } from "./types/basic.mjs";
8
8
 
9
9
  //#region src/ClearRequest.d.ts
10
10
  declare class ClearRequest<X = any, M = Middleware$2 | Middleware | Middleware$1 | Middleware$3 | Middleware$4> {
@@ -1,5 +1,5 @@
1
- import { RequestData } from "./types/basic.mjs";
2
1
  import { ClearRequest } from "./ClearRequest.mjs";
2
+ import { RequestData } from "./types/basic.mjs";
3
3
 
4
4
  //#region src/Controller.d.ts
5
5
  declare abstract class Controller<X = any> {
@@ -1,9 +1,9 @@
1
- import { Middleware } from "./types/express.cjs";
2
- import { Middleware as Middleware$1 } from "./types/h3.cjs";
1
+ import { Middleware } from "./types/h3.cjs";
3
2
  import { Route } from "./Route.cjs";
3
+ import { Middleware as Middleware$1 } from "./types/express.cjs";
4
4
 
5
5
  //#region src/ResourceRouteSelection.d.ts
6
- declare class ResourceRouteSelection<X = any, M = Middleware$1 | Middleware, H = any> {
6
+ declare class ResourceRouteSelection<X = any, M = Middleware | Middleware$1, H = any> {
7
7
  readonly routes: Array<Route<X, M, H>>;
8
8
  constructor(routes: Array<Route<X, M, H>>);
9
9
  /**
@@ -64,6 +64,10 @@ var ResourceRoutes = class ResourceRoutes {
64
64
  }
65
65
  /**
66
66
  * Only register routes for the provided actions.
67
+ *
68
+ * @param action
69
+ * @param actions
70
+ * @returns
67
71
  */
68
72
  only(action, ...actions) {
69
73
  this.options.only = Array.from(new Set(require_helpers.wrap(action).concat(actions)));
@@ -71,6 +75,10 @@ var ResourceRoutes = class ResourceRoutes {
71
75
  }
72
76
  /**
73
77
  * Register all resource routes except the provided actions.
78
+ *
79
+ * @param action
80
+ * @param actions
81
+ * @returns
74
82
  */
75
83
  except(action, ...actions) {
76
84
  this.options.except = Array.from(new Set(require_helpers.wrap(action).concat(actions)));
@@ -78,6 +86,10 @@ var ResourceRoutes = class ResourceRoutes {
78
86
  }
79
87
  /**
80
88
  * Register one or more middleware that will be executed before the route.
89
+ *
90
+ * @param middlewares
91
+ * @param remember
92
+ * @returns
81
93
  */
82
94
  middleware(middlewares, remember = true) {
83
95
  if (remember) this.chainedMiddlewares.push(middlewares);
@@ -1,35 +1,17 @@
1
- import { ApiResourceMiddleware, HttpMethod, ResourceAction } from "./types/basic.cjs";
2
- import { Middleware } from "./types/express.cjs";
3
- import { Middleware as Middleware$1 } from "./types/h3.cjs";
1
+ import { Middleware } from "./types/h3.cjs";
4
2
  import { Route } from "./Route.cjs";
3
+ import { ApiResourceMiddleware, HttpMethod, ResourceAction, ResourceRouteDefinition, ResourceRouteRegistrar, ResourceRouteRemover, ResourceRoutesOptions } from "./types/basic.cjs";
4
+ import { Middleware as Middleware$1 } from "./types/express.cjs";
5
5
  import { ResourceRouteSelection } from "./ResourceRouteSelection.cjs";
6
6
 
7
7
  //#region src/ResourceRoutes.d.ts
8
- type ResourceRouteDefinition = {
9
- method: HttpMethod;
10
- path: string;
11
- };
12
- type ResourceRouteRegistrar<X, M, H> = (input: {
13
- action: ResourceAction;
14
- method: HttpMethod;
15
- path: string;
16
- handler: any;
17
- middlewares?: M[];
18
- name: string;
19
- }) => Route<X, M, H>;
20
- type ResourceRouteRemover<X, M, H> = (route: Route<X, M, H>) => void;
21
- type ResourceRoutesOptions<M = any> = {
22
- only?: ResourceAction[];
23
- except?: ResourceAction[];
24
- middlewares?: ApiResourceMiddleware<M>;
25
- };
26
8
  /**
27
9
  * @class clear-router ResourceRoutes
28
10
  * @description A ResourceRoutes creates a collection of resourceful routes in a single call
29
11
  * @author 3m1n3nc3
30
12
  * @repository https://github.com/arkstack-tmp/clear-router
31
13
  */
32
- declare class ResourceRoutes<X = any, M = Middleware$1 | Middleware, H = any> {
14
+ declare class ResourceRoutes<X = any, M = Middleware | Middleware$1, H = any> {
33
15
  readonly basePath: string;
34
16
  readonly controller: any;
35
17
  readonly paramName: string;
@@ -64,14 +46,26 @@ declare class ResourceRoutes<X = any, M = Middleware$1 | Middleware, H = any> {
64
46
  register(): this;
65
47
  /**
66
48
  * Only register routes for the provided actions.
49
+ *
50
+ * @param action
51
+ * @param actions
52
+ * @returns
67
53
  */
68
54
  only(action: ResourceAction | ResourceAction[], ...actions: ResourceAction[]): this;
69
55
  /**
70
56
  * Register all resource routes except the provided actions.
57
+ *
58
+ * @param action
59
+ * @param actions
60
+ * @returns
71
61
  */
72
62
  except(action: ResourceAction | ResourceAction[], ...actions: ResourceAction[]): this;
73
63
  /**
74
64
  * Register one or more middleware that will be executed before the route.
65
+ *
66
+ * @param middlewares
67
+ * @param remember
68
+ * @returns
75
69
  */
76
70
  middleware(middlewares: M[] | M, remember?: boolean): this;
77
71
  action(action: ResourceAction): Route<X, M, H> | undefined;
@@ -1,28 +1,10 @@
1
- import { ApiResourceMiddleware, HttpMethod, ResourceAction } from "./types/basic.mjs";
2
1
  import { Middleware } from "./types/express.mjs";
3
2
  import { Middleware as Middleware$1 } from "./types/h3.mjs";
4
3
  import { Route } from "./Route.mjs";
4
+ import { ApiResourceMiddleware, HttpMethod, ResourceAction, ResourceRouteDefinition, ResourceRouteRegistrar, ResourceRouteRemover, ResourceRoutesOptions } from "./types/basic.mjs";
5
5
  import { ResourceRouteSelection } from "./ResourceRouteSelection.mjs";
6
6
 
7
7
  //#region src/ResourceRoutes.d.ts
8
- type ResourceRouteDefinition = {
9
- method: HttpMethod;
10
- path: string;
11
- };
12
- type ResourceRouteRegistrar<X, M, H> = (input: {
13
- action: ResourceAction;
14
- method: HttpMethod;
15
- path: string;
16
- handler: any;
17
- middlewares?: M[];
18
- name: string;
19
- }) => Route<X, M, H>;
20
- type ResourceRouteRemover<X, M, H> = (route: Route<X, M, H>) => void;
21
- type ResourceRoutesOptions<M = any> = {
22
- only?: ResourceAction[];
23
- except?: ResourceAction[];
24
- middlewares?: ApiResourceMiddleware<M>;
25
- };
26
8
  /**
27
9
  * @class clear-router ResourceRoutes
28
10
  * @description A ResourceRoutes creates a collection of resourceful routes in a single call
@@ -64,14 +46,26 @@ declare class ResourceRoutes<X = any, M = Middleware$1 | Middleware, H = any> {
64
46
  register(): this;
65
47
  /**
66
48
  * Only register routes for the provided actions.
49
+ *
50
+ * @param action
51
+ * @param actions
52
+ * @returns
67
53
  */
68
54
  only(action: ResourceAction | ResourceAction[], ...actions: ResourceAction[]): this;
69
55
  /**
70
56
  * Register all resource routes except the provided actions.
57
+ *
58
+ * @param action
59
+ * @param actions
60
+ * @returns
71
61
  */
72
62
  except(action: ResourceAction | ResourceAction[], ...actions: ResourceAction[]): this;
73
63
  /**
74
64
  * Register one or more middleware that will be executed before the route.
65
+ *
66
+ * @param middlewares
67
+ * @param remember
68
+ * @returns
75
69
  */
76
70
  middleware(middlewares: M[] | M, remember?: boolean): this;
77
71
  action(action: ResourceAction): Route<X, M, H> | undefined;
@@ -64,6 +64,10 @@ var ResourceRoutes = class ResourceRoutes {
64
64
  }
65
65
  /**
66
66
  * Only register routes for the provided actions.
67
+ *
68
+ * @param action
69
+ * @param actions
70
+ * @returns
67
71
  */
68
72
  only(action, ...actions) {
69
73
  this.options.only = Array.from(new Set(wrap(action).concat(actions)));
@@ -71,6 +75,10 @@ var ResourceRoutes = class ResourceRoutes {
71
75
  }
72
76
  /**
73
77
  * Register all resource routes except the provided actions.
78
+ *
79
+ * @param action
80
+ * @param actions
81
+ * @returns
74
82
  */
75
83
  except(action, ...actions) {
76
84
  this.options.except = Array.from(new Set(wrap(action).concat(actions)));
@@ -78,6 +86,10 @@ var ResourceRoutes = class ResourceRoutes {
78
86
  }
79
87
  /**
80
88
  * Register one or more middleware that will be executed before the route.
89
+ *
90
+ * @param middlewares
91
+ * @param remember
92
+ * @returns
81
93
  */
82
94
  middleware(middlewares, remember = true) {
83
95
  if (remember) this.chainedMiddlewares.push(middlewares);
@@ -1,9 +1,9 @@
1
+ import { Middleware } from "./types/express.mjs";
2
+ import { Middleware as Middleware$1 } from "./types/fastify.mjs";
3
+ import { Middleware as Middleware$2 } from "./types/h3.mjs";
4
+ import { Middleware as Middleware$3 } from "./types/hono.mjs";
5
+ import { Middleware as Middleware$4 } from "./types/koa.mjs";
1
6
  import { HttpMethod, RequestData } from "./types/basic.mjs";
2
- import { Middleware } from "./types/fastify.mjs";
3
- import { Middleware as Middleware$1 } from "./types/h3.mjs";
4
- import { Middleware as Middleware$2 } from "./types/hono.mjs";
5
- import { Middleware as Middleware$3 } from "./types/koa.mjs";
6
- import { Middleware as Middleware$4 } from "./types/express.mjs";
7
7
 
8
8
  //#region src/Contracts.d.ts
9
9
  /**
@@ -16,6 +16,26 @@ interface RouteParameter {
16
16
  optional: boolean;
17
17
  }
18
18
  //#endregion
19
+ //#region src/core/Request.d.ts
20
+ declare class Request<X = any, M = any> extends ClearRequest<X, M> {
21
+ original?: any;
22
+ method: string;
23
+ path: string;
24
+ url: string;
25
+ headers: Headers | Record<string, any>;
26
+ constructor(init?: Partial<Request<X, M>> & {
27
+ body?: RequestData;
28
+ query?: RequestData;
29
+ params?: RequestData;
30
+ route?: Route<X, M>;
31
+ });
32
+ getBody(): RequestData;
33
+ header(name: string): string;
34
+ param(name: string): any;
35
+ input(name: string): any;
36
+ is(method: HttpMethod | string): boolean;
37
+ }
38
+ //#endregion
19
39
  //#region src/core/Response.d.ts
20
40
  declare class Response {
21
41
  [key: string]: any;
@@ -39,6 +59,25 @@ declare class Response {
39
59
  noContent(): this;
40
60
  }
41
61
  //#endregion
62
+ //#region src/ClearRequest.d.ts
63
+ declare class ClearRequest<X = any, M = Middleware$2 | Middleware | Middleware$1 | Middleware$3 | Middleware$4> {
64
+ [key: string]: any;
65
+ /**
66
+ * @param body - Parsed request body
67
+ */
68
+ body: RequestData;
69
+ /**
70
+ * @param query - Parsed query parameters
71
+ */
72
+ query: RequestData;
73
+ /**
74
+ * @param params - Parsed route parameters
75
+ */
76
+ params: RequestData;
77
+ route: Route<X, M>;
78
+ constructor(init?: Partial<ClearRequest>);
79
+ }
80
+ //#endregion
42
81
  //#region src/Route.d.ts
43
82
  /**
44
83
  * @class clear-router Route
@@ -46,7 +85,7 @@ declare class Response {
46
85
  * @author 3m1n3nc3
47
86
  * @repository https://github.com/arkstack-tmp/clear-router
48
87
  */
49
- declare class Route<X = any, M = Middleware$1 | Middleware$4, H = any> {
88
+ declare class Route<X = any, M = Middleware$2 | Middleware, H = any> {
50
89
  ctx: X;
51
90
  body: RequestData;
52
91
  query: RequestData;
@@ -94,43 +133,4 @@ declare class Route<X = any, M = Middleware$1 | Middleware$4, H = any> {
94
133
  toPath(params?: RequestData): string;
95
134
  }
96
135
  //#endregion
97
- //#region src/ClearRequest.d.ts
98
- declare class ClearRequest<X = any, M = Middleware$1 | Middleware$4 | Middleware | Middleware$2 | Middleware$3> {
99
- [key: string]: any;
100
- /**
101
- * @param body - Parsed request body
102
- */
103
- body: RequestData;
104
- /**
105
- * @param query - Parsed query parameters
106
- */
107
- query: RequestData;
108
- /**
109
- * @param params - Parsed route parameters
110
- */
111
- params: RequestData;
112
- route: Route<X, M>;
113
- constructor(init?: Partial<ClearRequest>);
114
- }
115
- //#endregion
116
- //#region src/core/Request.d.ts
117
- declare class Request<X = any, M = any> extends ClearRequest<X, M> {
118
- original?: any;
119
- method: string;
120
- path: string;
121
- url: string;
122
- headers: Headers | Record<string, any>;
123
- constructor(init?: Partial<Request<X, M>> & {
124
- body?: RequestData;
125
- query?: RequestData;
126
- params?: RequestData;
127
- route?: Route<X, M>;
128
- });
129
- getBody(): RequestData;
130
- header(name: string): string;
131
- param(name: string): any;
132
- input(name: string): any;
133
- is(method: HttpMethod | string): boolean;
134
- }
135
- //#endregion
136
- export { Response as n, ClearHttpContext as r, Request as t };
136
+ export { ClearHttpContext as i, Response as n, Request as r, Route as t };
package/dist/Route.d.cts CHANGED
@@ -1,7 +1,7 @@
1
- import { HttpMethod, RequestData } from "./types/basic.cjs";
2
1
  import { RouteParameter } from "./Contracts.cjs";
3
- import { Middleware } from "./types/express.cjs";
4
- import { Middleware as Middleware$1 } from "./types/h3.cjs";
2
+ import { Middleware } from "./types/h3.cjs";
3
+ import { HttpMethod, RequestData } from "./types/basic.cjs";
4
+ import { Middleware as Middleware$1 } from "./types/express.cjs";
5
5
  import { ClearRequest } from "./ClearRequest.cjs";
6
6
 
7
7
  //#region src/Route.d.ts
@@ -11,7 +11,7 @@ import { ClearRequest } from "./ClearRequest.cjs";
11
11
  * @author 3m1n3nc3
12
12
  * @repository https://github.com/arkstack-tmp/clear-router
13
13
  */
14
- declare class Route<X = any, M = Middleware$1 | Middleware, H = any> {
14
+ declare class Route<X = any, M = Middleware | Middleware$1, H = any> {
15
15
  ctx: X;
16
16
  body: RequestData;
17
17
  query: RequestData;
package/dist/Route.d.mts CHANGED
@@ -1,8 +1,8 @@
1
- import { HttpMethod, RequestData } from "./types/basic.mjs";
2
1
  import { RouteParameter } from "./Contracts.mjs";
3
2
  import { Middleware } from "./types/express.mjs";
4
3
  import { Middleware as Middleware$1 } from "./types/h3.mjs";
5
4
  import { ClearRequest } from "./ClearRequest.mjs";
5
+ import { HttpMethod, RequestData } from "./types/basic.mjs";
6
6
 
7
7
  //#region src/Route.d.ts
8
8
  /**
@@ -0,0 +1,120 @@
1
+ const require_helpers = require('./core/helpers.cjs');
2
+ let node_path = require("node:path");
3
+ let node_fs_promises = require("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 require_helpers.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 = (0, node_path.isAbsolute)(source) ? source : (0, node_path.resolve)(process.cwd(), source);
91
+ let sourceStat;
92
+ try {
93
+ sourceStat = await (0, node_fs_promises.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 (0, node_fs_promises.readdir)(directory, { withFileTypes: true });
109
+ const files = [];
110
+ for (const entry of entries.sort((left, right) => left.name.localeCompare(right.name))) {
111
+ const path = (0, node_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
+ exports.RouteGroup = RouteGroup;
@@ -0,0 +1,64 @@
1
+ import { Middleware } from "./types/h3.cjs";
2
+ import { RouteGroupCondition, RouteGroupOptions } from "./types/basic.cjs";
3
+ import { Middleware as Middleware$1 } from "./types/express.cjs";
4
+
5
+ //#region src/RouteGroup.d.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
+ declare class RouteGroup<X = any, M = Middleware | Middleware$1, H = any> implements PromiseLike<void> {
13
+ private readonly options;
14
+ private readonly checks;
15
+ private readonly registration;
16
+ private readonly routes;
17
+ constructor(options: RouteGroupOptions);
18
+ /**
19
+ * Returning a falsy value will stop route group registration
20
+ *
21
+ * @param condition
22
+ * @returns
23
+ */
24
+ when(condition: RouteGroupCondition): this;
25
+ /**
26
+ * Register one or more middleware that will be executed before every route in the group.
27
+ *
28
+ * @param middlewares
29
+ * @returns
30
+ */
31
+ middleware(middlewares: M[] | M): this;
32
+ /**
33
+ * Attaches callbacks for the resolution and/or rejection of the RouteGroup.
34
+ *
35
+ * @param onfulfilled
36
+ * @param onrejected
37
+ * @returns
38
+ */
39
+ then<TResult1 = void, TResult2 = never>(onfulfilled?: ((value: void) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): PromiseLike<TResult1 | TResult2>;
40
+ /**
41
+ * Register the routes
42
+ */
43
+ private register;
44
+ /**
45
+ * Rollback the route registrations
46
+ */
47
+ private rollback;
48
+ /**
49
+ * Resolve files from the group path
50
+ *
51
+ * @param source
52
+ * @returns
53
+ */
54
+ private resolveFiles;
55
+ /**
56
+ * Read all the files in the configured directory
57
+ *
58
+ * @param directory
59
+ * @returns
60
+ */
61
+ private readDirectory;
62
+ }
63
+ //#endregion
64
+ export { RouteGroup };
@@ -0,0 +1,64 @@
1
+ import { Middleware } from "./types/express.mjs";
2
+ import { Middleware as Middleware$1 } from "./types/h3.mjs";
3
+ import { RouteGroupCondition, RouteGroupOptions } from "./types/basic.mjs";
4
+
5
+ //#region src/RouteGroup.d.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
+ declare class RouteGroup<X = any, M = Middleware$1 | Middleware, H = any> implements PromiseLike<void> {
13
+ private readonly options;
14
+ private readonly checks;
15
+ private readonly registration;
16
+ private readonly routes;
17
+ constructor(options: RouteGroupOptions);
18
+ /**
19
+ * Returning a falsy value will stop route group registration
20
+ *
21
+ * @param condition
22
+ * @returns
23
+ */
24
+ when(condition: RouteGroupCondition): this;
25
+ /**
26
+ * Register one or more middleware that will be executed before every route in the group.
27
+ *
28
+ * @param middlewares
29
+ * @returns
30
+ */
31
+ middleware(middlewares: M[] | M): this;
32
+ /**
33
+ * Attaches callbacks for the resolution and/or rejection of the RouteGroup.
34
+ *
35
+ * @param onfulfilled
36
+ * @param onrejected
37
+ * @returns
38
+ */
39
+ then<TResult1 = void, TResult2 = never>(onfulfilled?: ((value: void) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): PromiseLike<TResult1 | TResult2>;
40
+ /**
41
+ * Register the routes
42
+ */
43
+ private register;
44
+ /**
45
+ * Rollback the route registrations
46
+ */
47
+ private rollback;
48
+ /**
49
+ * Resolve files from the group path
50
+ *
51
+ * @param source
52
+ * @returns
53
+ */
54
+ private resolveFiles;
55
+ /**
56
+ * Read all the files in the configured directory
57
+ *
58
+ * @param directory
59
+ * @returns
60
+ */
61
+ private readDirectory;
62
+ }
63
+ //#endregion
64
+ export { RouteGroup };