clear-router 2.7.5 → 2.7.7

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 (107) hide show
  1. package/dist/ClearRequest.cjs +23 -0
  2. package/dist/ClearRequest.d.cts +28 -0
  3. package/dist/ClearRequest.d.mts +28 -0
  4. package/dist/ClearRequest.mjs +22 -0
  5. package/dist/Contracts.d.cts +12 -0
  6. package/dist/Contracts.d.mts +12 -0
  7. package/dist/Controller.cjs +12 -0
  8. package/dist/Controller.d.cts +14 -0
  9. package/dist/Controller.d.mts +14 -0
  10. package/dist/Controller.mjs +11 -0
  11. package/dist/{Request-DKXwa_W0.d.mts → Request-BhTJDR_5.d.mts} +5 -5
  12. package/dist/Route.cjs +58 -0
  13. package/dist/Route.d.cts +35 -0
  14. package/dist/Route.d.mts +35 -0
  15. package/dist/Route.mjs +57 -0
  16. package/dist/core/Request.cjs +35 -0
  17. package/dist/core/Request.d.cts +25 -0
  18. package/dist/core/Request.d.mts +25 -0
  19. package/dist/core/Request.mjs +35 -0
  20. package/dist/core/Response.cjs +59 -0
  21. package/dist/core/Response.d.cts +24 -0
  22. package/dist/core/Response.d.mts +24 -0
  23. package/dist/core/Response.mjs +58 -0
  24. package/dist/{bindings-CLsZjOEy.cjs → core/bindings.cjs} +10 -160
  25. package/dist/{bindings-CNL7bpz5.d.mts → core/bindings.d.cts} +1 -1
  26. package/dist/{bindings-CxvtC8XS.d.cts → core/bindings.d.mts} +1 -1
  27. package/dist/{bindings-XLDXFpHZ.mjs → core/bindings.mjs} +3 -110
  28. package/dist/core/index.cjs +4 -20
  29. package/dist/core/index.d.cts +4 -1
  30. package/dist/core/index.d.mts +4 -1
  31. package/dist/core/index.mjs +4 -14
  32. package/dist/core/plugins.cjs +14 -0
  33. package/dist/core/plugins.d.cts +109 -0
  34. package/dist/core/plugins.d.mts +109 -0
  35. package/dist/core/plugins.mjs +13 -0
  36. package/dist/{responses-Bvnk0uvc.cjs → core/responses.cjs} +5 -20
  37. package/dist/{responses-BvETUeDL.mjs → core/responses.mjs} +2 -2
  38. package/dist/{router-C6W-k6sS.cjs → core/router.cjs} +11 -69
  39. package/dist/core/router.d.cts +274 -0
  40. package/dist/core/router.d.mts +274 -0
  41. package/dist/{router-Dc9w86Wn.mjs → core/router.mjs} +5 -58
  42. package/dist/decorators/index.cjs +1 -1
  43. package/dist/decorators/index.d.cts +1 -1
  44. package/dist/decorators/index.d.mts +1 -1
  45. package/dist/decorators/index.mjs +1 -1
  46. package/dist/decorators/setup.cjs +2 -2
  47. package/dist/decorators/setup.d.cts +1 -1
  48. package/dist/decorators/setup.d.mts +1 -1
  49. package/dist/decorators/setup.mjs +2 -2
  50. package/dist/express/index.cjs +2 -265
  51. package/dist/express/index.d.cts +1 -127
  52. package/dist/express/index.d.mts +1 -127
  53. package/dist/express/index.mjs +1 -264
  54. package/dist/express/router.cjs +265 -0
  55. package/dist/express/router.d.cts +131 -0
  56. package/dist/express/router.d.mts +131 -0
  57. package/dist/express/router.mjs +265 -0
  58. package/dist/fastify/index.cjs +2 -254
  59. package/dist/fastify/index.d.cts +1 -125
  60. package/dist/fastify/index.d.mts +1 -125
  61. package/dist/fastify/index.mjs +1 -253
  62. package/dist/fastify/router.cjs +254 -0
  63. package/dist/fastify/router.d.cts +129 -0
  64. package/dist/fastify/router.d.mts +129 -0
  65. package/dist/fastify/router.mjs +254 -0
  66. package/dist/h3/index.cjs +2 -260
  67. package/dist/h3/index.d.cts +1 -128
  68. package/dist/h3/index.d.mts +1 -128
  69. package/dist/h3/index.mjs +1 -259
  70. package/dist/h3/router.cjs +260 -0
  71. package/dist/h3/router.d.cts +132 -0
  72. package/dist/h3/router.d.mts +132 -0
  73. package/dist/h3/router.mjs +260 -0
  74. package/dist/hono/index.cjs +2 -251
  75. package/dist/hono/index.d.cts +1 -130
  76. package/dist/hono/index.d.mts +1 -130
  77. package/dist/hono/index.mjs +1 -250
  78. package/dist/hono/router.cjs +251 -0
  79. package/dist/hono/router.d.cts +134 -0
  80. package/dist/hono/router.d.mts +134 -0
  81. package/dist/hono/router.mjs +251 -0
  82. package/dist/index.cjs +16 -1097
  83. package/dist/index.d.cts +9 -563
  84. package/dist/index.d.mts +9 -563
  85. package/dist/index.mjs +8 -1089
  86. package/dist/koa/index.cjs +2 -261
  87. package/dist/koa/index.d.cts +1 -131
  88. package/dist/koa/index.d.mts +1 -131
  89. package/dist/koa/index.mjs +1 -260
  90. package/dist/koa/router.cjs +261 -0
  91. package/dist/koa/router.d.cts +135 -0
  92. package/dist/koa/router.d.mts +135 -0
  93. package/dist/koa/router.mjs +261 -0
  94. package/dist/types/basic.d.cts +43 -0
  95. package/dist/types/express.d.cts +44 -0
  96. package/dist/types/express.d.mts +4 -2
  97. package/dist/types/fastify.d.cts +23 -0
  98. package/dist/types/fastify.d.mts +4 -2
  99. package/dist/types/h3.d.cts +41 -0
  100. package/dist/types/h3.d.mts +5 -6
  101. package/dist/types/hono.d.cts +21 -0
  102. package/dist/types/hono.d.mts +5 -5
  103. package/dist/types/koa.d.cts +25 -0
  104. package/dist/types/koa.d.mts +5 -5
  105. package/package.json +1 -1
  106. package/dist/router-CYBshGMl.d.mts +0 -652
  107. package/dist/router-tpnSi_Y7.d.cts +0 -652
@@ -1,126 +1,2 @@
1
- import { A as Middleware, D as FastifyApp, F as ApiResourceMiddleware, I as ControllerAction, L as HttpMethod, O as Handler, k as HttpContext, m as Route, t as CoreRouter } from "../router-CYBshGMl.mjs";
2
-
3
- //#region src/fastify/router.d.ts
4
- /**
5
- * @class clear-router Fastify Router
6
- * @description Laravel-style routing system for Fastify using shared clear-router core
7
- * @author 3m1n3nc3
8
- * @repository https://github.com/arkstack-tmp/clear-router
9
- */
10
- declare class Router extends CoreRouter {
11
- protected static routerStateNamespace: string;
12
- private static ensureRequestBodyAccessor;
13
- private static sendReturnValue;
14
- /**
15
- * Add a route to the router
16
- *
17
- * @param methods HTTP methods for the route
18
- * @param path Route path
19
- * @param handler Route handler function
20
- * @param middlewares Optional middlewares for the route
21
- */
22
- static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
23
- /**
24
- * Define a resourceful API controller with standard CRUD routes
25
- *
26
- * @param basePath Base path for the resource
27
- * @param controller Controller class or instance
28
- * @param options Optional configuration for the resource
29
- */
30
- static apiResource(basePath: string, controller: any, options?: {
31
- only?: ControllerAction[];
32
- except?: ControllerAction[];
33
- middlewares?: ApiResourceMiddleware<Middleware>;
34
- }): void;
35
- /**
36
- * Define a GET route
37
- *
38
- * @param path
39
- * @param handler
40
- * @param middlewares
41
- */
42
- static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
43
- /**
44
- * Define a POST route
45
- *
46
- * @param path
47
- * @param handler
48
- * @param middlewares
49
- */
50
- static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
51
- /**
52
- * Define a PUT route
53
- *
54
- * @param path
55
- * @param handler
56
- * @param middlewares
57
- */
58
- static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
59
- /**
60
- * Define a DELETE route
61
- *
62
- * @param path
63
- * @param handler
64
- * @param middlewares
65
- */
66
- static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
67
- /**
68
- * Define a PATCH route
69
- *
70
- * @param path
71
- * @param handler
72
- * @param middlewares
73
- */
74
- static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
75
- /**
76
- * Define an OPTIONS route
77
- *
78
- * @param path
79
- * @param handler
80
- * @param middlewares
81
- */
82
- static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
83
- /**
84
- * Define a HEAD route
85
- *
86
- * @param path
87
- * @param handler
88
- * @param middlewares
89
- */
90
- static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
91
- /**
92
- * Define a group of routes with a common prefix and optional middlewares
93
- *
94
- * @param prefix
95
- * @param callback
96
- * @param middlewares
97
- */
98
- static group(prefix: string, callback: () => void | Promise<void>, middlewares?: Middleware[]): Promise<void>;
99
- /**
100
- * Apply middlewares to a group of routes defined within the callback
101
- *
102
- * @param middlewares
103
- * @param callback
104
- */
105
- static middleware(middlewares: Middleware[], callback: () => void): void;
106
- static allRoutes(): Array<Route<HttpContext, Middleware, Handler>>;
107
- /**
108
- * @param type - 'path' to get routes organized by path
109
- */
110
- static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware, Handler>>;
111
- /**
112
- * @param type - 'method' to get routes organized by method
113
- */
114
- static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
115
- static allRoutes(type: 'name'): Record<string, Route<HttpContext, Middleware, Handler>>;
116
- static route(name: string): Route<HttpContext, Middleware, Handler> | undefined;
117
- /**
118
- * Apply the defined routes to a Fastify application instance
119
- *
120
- * @param app - The Fastify application instance
121
- * @returns The Fastify application instance with the applied routes
122
- */
123
- static apply(app: FastifyApp): FastifyApp;
124
- }
125
- //#endregion
1
+ import { Router } from "./router.mjs";
126
2
  export { Router };
@@ -1,255 +1,3 @@
1
- import "../bindings-XLDXFpHZ.mjs";
2
- import { t as CoreRouter } from "../router-Dc9w86Wn.mjs";
3
- import { n as resolveResponseMeta, r as responseWasSent, t as isFetchResponse } from "../responses-BvETUeDL.mjs";
1
+ import Router from "./router.mjs";
4
2
 
5
- //#region src/fastify/router.ts
6
- /**
7
- * @class clear-router Fastify Router
8
- * @description Laravel-style routing system for Fastify using shared clear-router core
9
- * @author 3m1n3nc3
10
- * @repository https://github.com/arkstack-tmp/clear-router
11
- */
12
- var Router = class Router extends CoreRouter {
13
- static routerStateNamespace = "clear-router:fastify";
14
- static ensureRequestBodyAccessor(req) {
15
- if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
16
- }
17
- static async sendReturnValue(req, reply, value, method, path) {
18
- if (responseWasSent(reply) || value === reply || responseWasSent(value)) return value;
19
- const meta = resolveResponseMeta(value, {
20
- headers: req.headers,
21
- method,
22
- path
23
- });
24
- if (!meta) return void 0;
25
- reply.code(meta.status);
26
- meta.headers?.forEach((headerValue, key) => {
27
- reply.header(key, headerValue);
28
- });
29
- if (isFetchResponse(meta.body)) {
30
- meta.body.headers.forEach((headerValue, key) => {
31
- reply.header(key, headerValue);
32
- });
33
- reply.code(meta.body.status);
34
- return reply.send(Buffer.from(await meta.body.arrayBuffer()));
35
- }
36
- if (meta.contentType) reply.type(meta.contentType);
37
- if (meta.isEmpty) return reply.send();
38
- return reply.send(meta.body);
39
- }
40
- /**
41
- * Add a route to the router
42
- *
43
- * @param methods HTTP methods for the route
44
- * @param path Route path
45
- * @param handler Route handler function
46
- * @param middlewares Optional middlewares for the route
47
- */
48
- static add(methods, path, handler, middlewares) {
49
- return super.add(methods, path, handler, middlewares);
50
- }
51
- /**
52
- * Define a resourceful API controller with standard CRUD routes
53
- *
54
- * @param basePath Base path for the resource
55
- * @param controller Controller class or instance
56
- * @param options Optional configuration for the resource
57
- */
58
- static apiResource(basePath, controller, options) {
59
- super.apiResource(basePath, controller, options);
60
- }
61
- /**
62
- * Define a GET route
63
- *
64
- * @param path
65
- * @param handler
66
- * @param middlewares
67
- */
68
- static get(path, handler, middlewares) {
69
- return super.get(path, handler, middlewares);
70
- }
71
- /**
72
- * Define a POST route
73
- *
74
- * @param path
75
- * @param handler
76
- * @param middlewares
77
- */
78
- static post(path, handler, middlewares) {
79
- return super.post(path, handler, middlewares);
80
- }
81
- /**
82
- * Define a PUT route
83
- *
84
- * @param path
85
- * @param handler
86
- * @param middlewares
87
- */
88
- static put(path, handler, middlewares) {
89
- return super.put(path, handler, middlewares);
90
- }
91
- /**
92
- * Define a DELETE route
93
- *
94
- * @param path
95
- * @param handler
96
- * @param middlewares
97
- */
98
- static delete(path, handler, middlewares) {
99
- return super.delete(path, handler, middlewares);
100
- }
101
- /**
102
- * Define a PATCH route
103
- *
104
- * @param path
105
- * @param handler
106
- * @param middlewares
107
- */
108
- static patch(path, handler, middlewares) {
109
- return super.patch(path, handler, middlewares);
110
- }
111
- /**
112
- * Define an OPTIONS route
113
- *
114
- * @param path
115
- * @param handler
116
- * @param middlewares
117
- */
118
- static options(path, handler, middlewares) {
119
- return super.options(path, handler, middlewares);
120
- }
121
- /**
122
- * Define a HEAD route
123
- *
124
- * @param path
125
- * @param handler
126
- * @param middlewares
127
- */
128
- static head(path, handler, middlewares) {
129
- return super.head(path, handler, middlewares);
130
- }
131
- /**
132
- * Define a group of routes with a common prefix and optional middlewares
133
- *
134
- * @param prefix
135
- * @param callback
136
- * @param middlewares
137
- */
138
- static async group(prefix, callback, middlewares) {
139
- await super.group(prefix, callback, middlewares);
140
- }
141
- /**
142
- * Apply middlewares to a group of routes defined within the callback
143
- *
144
- * @param middlewares
145
- * @param callback
146
- */
147
- static middleware(middlewares, callback) {
148
- super.middleware(middlewares, callback);
149
- }
150
- static allRoutes(type) {
151
- return super.allRoutes(type);
152
- }
153
- static route(name) {
154
- return super.route(name);
155
- }
156
- /**
157
- * Apply the defined routes to a Fastify application instance
158
- *
159
- * @param app - The Fastify application instance
160
- * @returns The Fastify application instance with the applied routes
161
- */
162
- static apply(app) {
163
- for (const route of Array.from(this.routes)) {
164
- let handlerFunction = null;
165
- let instance = null;
166
- let bindingTarget;
167
- let bindingMethod;
168
- let bindingHandler;
169
- let bindingMetadata;
170
- try {
171
- const resolved = this.resolveHandler(route);
172
- handlerFunction = resolved.handlerFunction;
173
- instance = resolved.instance;
174
- bindingTarget = resolved.bindingTarget;
175
- bindingMethod = resolved.bindingMethod;
176
- bindingHandler = resolved.bindingHandler;
177
- bindingMetadata = resolved.bindingMetadata;
178
- } catch (error) {
179
- console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
180
- throw error;
181
- }
182
- if (!handlerFunction) continue;
183
- for (const method of route.methods) {
184
- const allowedMethods = [
185
- "get",
186
- "post",
187
- "put",
188
- "delete",
189
- "patch",
190
- "options",
191
- "head"
192
- ];
193
- if (method === "options" && route.methods.length > 1) continue;
194
- if (!allowedMethods.includes(method)) throw new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
195
- for (const registrationPath of route.registrationPaths) app.route({
196
- method: method.toUpperCase(),
197
- url: registrationPath,
198
- preHandler: route.middlewares,
199
- handler: async (req, reply) => {
200
- Router.ensureRequestBodyAccessor(req);
201
- const override = Router.resolveMethodOverride(req.method, req.headers, req.body);
202
- if (method === "post" && override && override !== "post") return reply.code(404).send();
203
- const ctx = {
204
- req,
205
- reply
206
- };
207
- const inst = instance ?? route;
208
- Router.bindRequestToInstance(ctx, inst, route, {
209
- body: ctx.req.getBody(),
210
- query: ctx.req.query ?? {},
211
- params: ctx.req.params ?? {},
212
- method
213
- });
214
- const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
215
- const resolved = await Promise.resolve(result);
216
- const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
217
- return Router.sendReturnValue(req, reply, outgoing, method, route.path);
218
- }
219
- });
220
- if ([
221
- "put",
222
- "patch",
223
- "delete"
224
- ].includes(method)) for (const registrationPath of route.registrationPaths) app.route({
225
- method: "POST",
226
- url: registrationPath,
227
- preHandler: route.middlewares,
228
- handler: async (req, reply) => {
229
- Router.ensureRequestBodyAccessor(req);
230
- if (Router.resolveMethodOverride(req.method, req.headers, req.body) !== method) return reply.code(404).send();
231
- const ctx = {
232
- req,
233
- reply
234
- };
235
- const inst = instance ?? route;
236
- Router.bindRequestToInstance(ctx, inst, route, {
237
- body: ctx.req.getBody(),
238
- query: ctx.req.query ?? {},
239
- params: ctx.req.params ?? {},
240
- method
241
- });
242
- const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
243
- const resolved = await Promise.resolve(result);
244
- const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
245
- return Router.sendReturnValue(req, reply, outgoing, method, route.path);
246
- }
247
- });
248
- }
249
- }
250
- return app;
251
- }
252
- };
253
-
254
- //#endregion
255
3
  export { Router };
@@ -0,0 +1,254 @@
1
+ const require_router = require('../core/router.cjs');
2
+ const require_responses = require('../core/responses.cjs');
3
+
4
+ //#region src/fastify/router.ts
5
+ /**
6
+ * @class clear-router Fastify Router
7
+ * @description Laravel-style routing system for Fastify using shared clear-router core
8
+ * @author 3m1n3nc3
9
+ * @repository https://github.com/arkstack-tmp/clear-router
10
+ */
11
+ var Router = class Router extends require_router.CoreRouter {
12
+ static routerStateNamespace = "clear-router:fastify";
13
+ static ensureRequestBodyAccessor(req) {
14
+ if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
15
+ }
16
+ static async sendReturnValue(req, reply, value, method, path) {
17
+ if (require_responses.responseWasSent(reply) || value === reply || require_responses.responseWasSent(value)) return value;
18
+ const meta = require_responses.resolveResponseMeta(value, {
19
+ headers: req.headers,
20
+ method,
21
+ path
22
+ });
23
+ if (!meta) return void 0;
24
+ reply.code(meta.status);
25
+ meta.headers?.forEach((headerValue, key) => {
26
+ reply.header(key, headerValue);
27
+ });
28
+ if (require_responses.isFetchResponse(meta.body)) {
29
+ meta.body.headers.forEach((headerValue, key) => {
30
+ reply.header(key, headerValue);
31
+ });
32
+ reply.code(meta.body.status);
33
+ return reply.send(Buffer.from(await meta.body.arrayBuffer()));
34
+ }
35
+ if (meta.contentType) reply.type(meta.contentType);
36
+ if (meta.isEmpty) return reply.send();
37
+ return reply.send(meta.body);
38
+ }
39
+ /**
40
+ * Add a route to the router
41
+ *
42
+ * @param methods HTTP methods for the route
43
+ * @param path Route path
44
+ * @param handler Route handler function
45
+ * @param middlewares Optional middlewares for the route
46
+ */
47
+ static add(methods, path, handler, middlewares) {
48
+ return super.add(methods, path, handler, middlewares);
49
+ }
50
+ /**
51
+ * Define a resourceful API controller with standard CRUD routes
52
+ *
53
+ * @param basePath Base path for the resource
54
+ * @param controller Controller class or instance
55
+ * @param options Optional configuration for the resource
56
+ */
57
+ static apiResource(basePath, controller, options) {
58
+ super.apiResource(basePath, controller, options);
59
+ }
60
+ /**
61
+ * Define a GET route
62
+ *
63
+ * @param path
64
+ * @param handler
65
+ * @param middlewares
66
+ */
67
+ static get(path, handler, middlewares) {
68
+ return super.get(path, handler, middlewares);
69
+ }
70
+ /**
71
+ * Define a POST route
72
+ *
73
+ * @param path
74
+ * @param handler
75
+ * @param middlewares
76
+ */
77
+ static post(path, handler, middlewares) {
78
+ return super.post(path, handler, middlewares);
79
+ }
80
+ /**
81
+ * Define a PUT route
82
+ *
83
+ * @param path
84
+ * @param handler
85
+ * @param middlewares
86
+ */
87
+ static put(path, handler, middlewares) {
88
+ return super.put(path, handler, middlewares);
89
+ }
90
+ /**
91
+ * Define a DELETE route
92
+ *
93
+ * @param path
94
+ * @param handler
95
+ * @param middlewares
96
+ */
97
+ static delete(path, handler, middlewares) {
98
+ return super.delete(path, handler, middlewares);
99
+ }
100
+ /**
101
+ * Define a PATCH route
102
+ *
103
+ * @param path
104
+ * @param handler
105
+ * @param middlewares
106
+ */
107
+ static patch(path, handler, middlewares) {
108
+ return super.patch(path, handler, middlewares);
109
+ }
110
+ /**
111
+ * Define an OPTIONS route
112
+ *
113
+ * @param path
114
+ * @param handler
115
+ * @param middlewares
116
+ */
117
+ static options(path, handler, middlewares) {
118
+ return super.options(path, handler, middlewares);
119
+ }
120
+ /**
121
+ * Define a HEAD route
122
+ *
123
+ * @param path
124
+ * @param handler
125
+ * @param middlewares
126
+ */
127
+ static head(path, handler, middlewares) {
128
+ return super.head(path, handler, middlewares);
129
+ }
130
+ /**
131
+ * Define a group of routes with a common prefix and optional middlewares
132
+ *
133
+ * @param prefix
134
+ * @param callback
135
+ * @param middlewares
136
+ */
137
+ static async group(prefix, callback, middlewares) {
138
+ await super.group(prefix, callback, middlewares);
139
+ }
140
+ /**
141
+ * Apply middlewares to a group of routes defined within the callback
142
+ *
143
+ * @param middlewares
144
+ * @param callback
145
+ */
146
+ static middleware(middlewares, callback) {
147
+ super.middleware(middlewares, callback);
148
+ }
149
+ static allRoutes(type) {
150
+ return super.allRoutes(type);
151
+ }
152
+ static route(name) {
153
+ return super.route(name);
154
+ }
155
+ /**
156
+ * Apply the defined routes to a Fastify application instance
157
+ *
158
+ * @param app - The Fastify application instance
159
+ * @returns The Fastify application instance with the applied routes
160
+ */
161
+ static apply(app) {
162
+ for (const route of Array.from(this.routes)) {
163
+ let handlerFunction = null;
164
+ let instance = null;
165
+ let bindingTarget;
166
+ let bindingMethod;
167
+ let bindingHandler;
168
+ let bindingMetadata;
169
+ try {
170
+ const resolved = this.resolveHandler(route);
171
+ handlerFunction = resolved.handlerFunction;
172
+ instance = resolved.instance;
173
+ bindingTarget = resolved.bindingTarget;
174
+ bindingMethod = resolved.bindingMethod;
175
+ bindingHandler = resolved.bindingHandler;
176
+ bindingMetadata = resolved.bindingMetadata;
177
+ } catch (error) {
178
+ console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
179
+ throw error;
180
+ }
181
+ if (!handlerFunction) continue;
182
+ for (const method of route.methods) {
183
+ const allowedMethods = [
184
+ "get",
185
+ "post",
186
+ "put",
187
+ "delete",
188
+ "patch",
189
+ "options",
190
+ "head"
191
+ ];
192
+ if (method === "options" && route.methods.length > 1) continue;
193
+ if (!allowedMethods.includes(method)) throw new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
194
+ for (const registrationPath of route.registrationPaths) app.route({
195
+ method: method.toUpperCase(),
196
+ url: registrationPath,
197
+ preHandler: route.middlewares,
198
+ handler: async (req, reply) => {
199
+ Router.ensureRequestBodyAccessor(req);
200
+ const override = Router.resolveMethodOverride(req.method, req.headers, req.body);
201
+ if (method === "post" && override && override !== "post") return reply.code(404).send();
202
+ const ctx = {
203
+ req,
204
+ reply
205
+ };
206
+ const inst = instance ?? route;
207
+ Router.bindRequestToInstance(ctx, inst, route, {
208
+ body: ctx.req.getBody(),
209
+ query: ctx.req.query ?? {},
210
+ params: ctx.req.params ?? {},
211
+ method
212
+ });
213
+ const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
214
+ const resolved = await Promise.resolve(result);
215
+ const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
216
+ return Router.sendReturnValue(req, reply, outgoing, method, route.path);
217
+ }
218
+ });
219
+ if ([
220
+ "put",
221
+ "patch",
222
+ "delete"
223
+ ].includes(method)) for (const registrationPath of route.registrationPaths) app.route({
224
+ method: "POST",
225
+ url: registrationPath,
226
+ preHandler: route.middlewares,
227
+ handler: async (req, reply) => {
228
+ Router.ensureRequestBodyAccessor(req);
229
+ if (Router.resolveMethodOverride(req.method, req.headers, req.body) !== method) return reply.code(404).send();
230
+ const ctx = {
231
+ req,
232
+ reply
233
+ };
234
+ const inst = instance ?? route;
235
+ Router.bindRequestToInstance(ctx, inst, route, {
236
+ body: ctx.req.getBody(),
237
+ query: ctx.req.query ?? {},
238
+ params: ctx.req.params ?? {},
239
+ method
240
+ });
241
+ const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
242
+ const resolved = await Promise.resolve(result);
243
+ const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
244
+ return Router.sendReturnValue(req, reply, outgoing, method, route.path);
245
+ }
246
+ });
247
+ }
248
+ }
249
+ return app;
250
+ }
251
+ };
252
+
253
+ //#endregion
254
+ exports.default = Router;