clear-router 2.7.6 → 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 +4 -4
  101. package/dist/types/hono.d.cts +21 -0
  102. package/dist/types/hono.d.mts +4 -3
  103. package/dist/types/koa.d.cts +25 -0
  104. package/dist/types/koa.d.mts +4 -3
  105. package/package.json +1 -1
  106. package/dist/router-BEgAxp5A.d.cts +0 -649
  107. package/dist/router-DKKYx23P.d.mts +0 -649
package/dist/h3/index.mjs CHANGED
@@ -1,261 +1,3 @@
1
- import "../bindings-XLDXFpHZ.mjs";
2
- import { t as CoreRouter } from "../router-Dc9w86Wn.mjs";
3
- import { n as resolveResponseMeta } from "../responses-BvETUeDL.mjs";
4
- import { HTTPResponse, getQuery, getRouterParams, readBody } from "h3";
1
+ import Router from "./router.mjs";
5
2
 
6
- //#region src/h3/router.ts
7
- /**
8
- * @class clear-router H3 Router
9
- * @description Laravel-style routing system for Express.js and H3 with support for CommonJS, ESM, and TypeScript
10
- * @author 3m1n3nc3
11
- * @repository https://github.com/arkstack-tmp/clear-router
12
- */
13
- var Router = class Router extends CoreRouter {
14
- static routerStateNamespace = "clear-router:h3";
15
- static bodyCache = /* @__PURE__ */ new WeakMap();
16
- static toResponse(ctx, value, method, path) {
17
- const meta = resolveResponseMeta(value, {
18
- headers: ctx.req.headers,
19
- method,
20
- path,
21
- status: typeof ctx.res.status === "number" && ctx.res.status !== 200 ? ctx.res.status : void 0
22
- });
23
- if (!meta) return void 0;
24
- if (meta.isNativeResponse) return meta.body;
25
- const headers = meta.headers ? {} : meta.contentType ? { "Content-Type": meta.contentType } : void 0;
26
- meta.headers?.forEach((headerValue, key) => {
27
- if (headers) headers[key] = headerValue;
28
- });
29
- return new HTTPResponse(meta.contentType?.startsWith("application/json") ? JSON.stringify(meta.body) : meta.isEmpty ? null : meta.body, {
30
- status: meta.status,
31
- headers
32
- });
33
- }
34
- static async readBodyCached(ctx) {
35
- if (this.bodyCache.has(ctx)) {
36
- const cached = this.bodyCache.get(ctx);
37
- ctx.req.getBody = () => cached;
38
- return cached;
39
- }
40
- let body = {};
41
- if (ctx.req.headers.get("content-type")?.includes("multipart/form-data")) (await ctx.req.formData()).forEach((value, key) => {
42
- body[key] = value;
43
- });
44
- else body = await readBody(ctx) ?? {};
45
- ctx.req.getBody = () => body;
46
- this.bodyCache.set(ctx, body);
47
- return body;
48
- }
49
- /**
50
- * Adds a new route to the router.
51
- *
52
- * @param methods
53
- * @param path
54
- * @param handler
55
- * @param middlewares
56
- */
57
- static add(methods, path, handler, middlewares) {
58
- return super.add(methods, path, handler, middlewares);
59
- }
60
- /**
61
- * Define a resourceful API controller with standard CRUD routes
62
- *
63
- * @param basePath
64
- * @param controller
65
- * @param options
66
- */
67
- static apiResource(basePath, controller, options) {
68
- super.apiResource(basePath, controller, options);
69
- }
70
- /**
71
- * Adds a new GET route to the router with the specified path, handler, and optional middlewares.
72
- *
73
- * @param path
74
- * @param handler
75
- * @param middlewares
76
- */
77
- static get(path, handler, middlewares) {
78
- return super.get(path, handler, middlewares);
79
- }
80
- /**
81
- * Adds a new POST route to the router with the specified path, handler, and optional middlewares.
82
- *
83
- * @param path
84
- * @param handler
85
- * @param middlewares
86
- */
87
- static post(path, handler, middlewares) {
88
- return super.post(path, handler, middlewares);
89
- }
90
- /**
91
- * Adds a new PUT route to the router with the specified path, handler, and optional middlewares.
92
- *
93
- * @param path
94
- * @param handler
95
- * @param middlewares
96
- */
97
- static put(path, handler, middlewares) {
98
- return super.put(path, handler, middlewares);
99
- }
100
- /**
101
- * Adds a new DELETE route to the router with the specified path, handler, and optional middlewares.
102
- *
103
- * @param path
104
- * @param handler
105
- * @param middlewares
106
- */
107
- static delete(path, handler, middlewares) {
108
- return super.delete(path, handler, middlewares);
109
- }
110
- /**
111
- * Adds a new PATCH route to the router with the specified path, handler, and optional middlewares.
112
- *
113
- * @param path
114
- * @param handler
115
- * @param middlewares
116
- */
117
- static patch(path, handler, middlewares) {
118
- return super.patch(path, handler, middlewares);
119
- }
120
- /**
121
- * Adds a new OPTIONS route to the router with the specified path, handler, and optional middlewares.
122
- *
123
- * @param path
124
- * @param handler
125
- * @param middlewares
126
- */
127
- static options(path, handler, middlewares) {
128
- return super.options(path, handler, middlewares);
129
- }
130
- /**
131
- * Adds a new HEAD route to the router.
132
- *
133
- * @param path
134
- * @param handler
135
- * @param middlewares
136
- */
137
- static head(path, handler, middlewares) {
138
- return super.head(path, handler, middlewares);
139
- }
140
- /**
141
- * Defines a group of routes with a common prefix.
142
- *
143
- * @param prefix
144
- * @param callback
145
- * @param middlewares
146
- */
147
- static async group(prefix, callback, middlewares) {
148
- await super.group(prefix, callback, middlewares);
149
- }
150
- /**
151
- * Adds global middlewares to the router, which will be applied to all routes.
152
- *
153
- * @param middlewares
154
- * @param callback
155
- */
156
- static middleware(middlewares, callback) {
157
- super.middleware(middlewares, callback);
158
- }
159
- static allRoutes(type) {
160
- return super.allRoutes(type);
161
- }
162
- static route(name) {
163
- return super.route(name);
164
- }
165
- /**
166
- * Applies the registered routes to the given H3 application instance, setting up the
167
- * necessary handlers and middlewares for each route.
168
- *
169
- * @param app
170
- * @returns
171
- */
172
- static apply(app) {
173
- for (const route of Array.from(this.routes)) {
174
- let handlerFunction = null;
175
- let instance = null;
176
- let bindingTarget;
177
- let bindingMethod;
178
- let bindingHandler;
179
- let bindingMetadata;
180
- try {
181
- const resolved = this.resolveHandler(route);
182
- handlerFunction = resolved.handlerFunction;
183
- instance = resolved.instance;
184
- bindingTarget = resolved.bindingTarget;
185
- bindingMethod = resolved.bindingMethod;
186
- bindingHandler = resolved.bindingHandler;
187
- bindingMetadata = resolved.bindingMetadata;
188
- } catch (error) {
189
- console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
190
- throw error;
191
- }
192
- if (!handlerFunction) continue;
193
- for (const method of route.methods) {
194
- const allowedMethods = [
195
- "get",
196
- "post",
197
- "put",
198
- "delete",
199
- "patch",
200
- "options",
201
- "head"
202
- ];
203
- if (method === "options" && route.methods.length > 1) continue;
204
- if (!allowedMethods.includes(method)) {
205
- const error = /* @__PURE__ */ new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
206
- console.error("[ROUTES]", error.message);
207
- throw error;
208
- }
209
- for (const registrationPath of route.registrationPaths) app[method](registrationPath, async (event) => {
210
- try {
211
- const ctx = event;
212
- const reqBody = await Router.readBodyCached(ctx);
213
- const override = Router.resolveMethodOverride(ctx.req.method, ctx.req.headers, reqBody);
214
- if (method === "post" && override && override !== "post") return;
215
- const inst = instance ?? route;
216
- Router.bindRequestToInstance(ctx, inst, route, {
217
- body: reqBody,
218
- query: getQuery(ctx),
219
- params: getRouterParams(ctx, { decode: true }),
220
- method
221
- });
222
- const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
223
- const resolved = await Promise.resolve(result);
224
- const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
225
- return Router.toResponse(ctx, outgoing, method, route.path);
226
- } catch (error) {
227
- return error;
228
- }
229
- }, { middleware: route.middlewares });
230
- if ([
231
- "put",
232
- "patch",
233
- "delete"
234
- ].includes(method)) for (const registrationPath of route.registrationPaths) app.post(registrationPath, async (event) => {
235
- try {
236
- const ctx = event;
237
- const reqBody = await Router.readBodyCached(ctx);
238
- if (Router.resolveMethodOverride(ctx.req.method, ctx.req.headers, reqBody) !== method) return Symbol.for("h3.notFound");
239
- const inst = instance ?? route;
240
- Router.bindRequestToInstance(ctx, inst, route, {
241
- body: reqBody,
242
- query: getQuery(ctx),
243
- params: getRouterParams(ctx, { decode: true }),
244
- method
245
- });
246
- const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
247
- const resolved = await Promise.resolve(result);
248
- const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
249
- return Router.toResponse(ctx, outgoing, method, route.path);
250
- } catch (error) {
251
- return error;
252
- }
253
- }, { middleware: route.middlewares });
254
- }
255
- }
256
- return app;
257
- }
258
- };
259
-
260
- //#endregion
261
3
  export { Router };
@@ -0,0 +1,260 @@
1
+ const require_router = require('../core/router.cjs');
2
+ const require_responses = require('../core/responses.cjs');
3
+ let h3 = require("h3");
4
+
5
+ //#region src/h3/router.ts
6
+ /**
7
+ * @class clear-router H3 Router
8
+ * @description Laravel-style routing system for Express.js and H3 with support for CommonJS, ESM, and TypeScript
9
+ * @author 3m1n3nc3
10
+ * @repository https://github.com/arkstack-tmp/clear-router
11
+ */
12
+ var Router = class Router extends require_router.CoreRouter {
13
+ static routerStateNamespace = "clear-router:h3";
14
+ static bodyCache = /* @__PURE__ */ new WeakMap();
15
+ static toResponse(ctx, value, method, path) {
16
+ const meta = require_responses.resolveResponseMeta(value, {
17
+ headers: ctx.req.headers,
18
+ method,
19
+ path,
20
+ status: typeof ctx.res.status === "number" && ctx.res.status !== 200 ? ctx.res.status : void 0
21
+ });
22
+ if (!meta) return void 0;
23
+ if (meta.isNativeResponse) return meta.body;
24
+ const headers = meta.headers ? {} : meta.contentType ? { "Content-Type": meta.contentType } : void 0;
25
+ meta.headers?.forEach((headerValue, key) => {
26
+ if (headers) headers[key] = headerValue;
27
+ });
28
+ return new h3.HTTPResponse(meta.contentType?.startsWith("application/json") ? JSON.stringify(meta.body) : meta.isEmpty ? null : meta.body, {
29
+ status: meta.status,
30
+ headers
31
+ });
32
+ }
33
+ static async readBodyCached(ctx) {
34
+ if (this.bodyCache.has(ctx)) {
35
+ const cached = this.bodyCache.get(ctx);
36
+ ctx.req.getBody = () => cached;
37
+ return cached;
38
+ }
39
+ let body = {};
40
+ if (ctx.req.headers.get("content-type")?.includes("multipart/form-data")) (await ctx.req.formData()).forEach((value, key) => {
41
+ body[key] = value;
42
+ });
43
+ else body = await (0, h3.readBody)(ctx) ?? {};
44
+ ctx.req.getBody = () => body;
45
+ this.bodyCache.set(ctx, body);
46
+ return body;
47
+ }
48
+ /**
49
+ * Adds a new route to the router.
50
+ *
51
+ * @param methods
52
+ * @param path
53
+ * @param handler
54
+ * @param middlewares
55
+ */
56
+ static add(methods, path, handler, middlewares) {
57
+ return super.add(methods, path, handler, middlewares);
58
+ }
59
+ /**
60
+ * Define a resourceful API controller with standard CRUD routes
61
+ *
62
+ * @param basePath
63
+ * @param controller
64
+ * @param options
65
+ */
66
+ static apiResource(basePath, controller, options) {
67
+ super.apiResource(basePath, controller, options);
68
+ }
69
+ /**
70
+ * Adds a new GET route to the router with the specified path, handler, and optional middlewares.
71
+ *
72
+ * @param path
73
+ * @param handler
74
+ * @param middlewares
75
+ */
76
+ static get(path, handler, middlewares) {
77
+ return super.get(path, handler, middlewares);
78
+ }
79
+ /**
80
+ * Adds a new POST route to the router with the specified path, handler, and optional middlewares.
81
+ *
82
+ * @param path
83
+ * @param handler
84
+ * @param middlewares
85
+ */
86
+ static post(path, handler, middlewares) {
87
+ return super.post(path, handler, middlewares);
88
+ }
89
+ /**
90
+ * Adds a new PUT route to the router with the specified path, handler, and optional middlewares.
91
+ *
92
+ * @param path
93
+ * @param handler
94
+ * @param middlewares
95
+ */
96
+ static put(path, handler, middlewares) {
97
+ return super.put(path, handler, middlewares);
98
+ }
99
+ /**
100
+ * Adds a new DELETE route to the router with the specified path, handler, and optional middlewares.
101
+ *
102
+ * @param path
103
+ * @param handler
104
+ * @param middlewares
105
+ */
106
+ static delete(path, handler, middlewares) {
107
+ return super.delete(path, handler, middlewares);
108
+ }
109
+ /**
110
+ * Adds a new PATCH route to the router with the specified path, handler, and optional middlewares.
111
+ *
112
+ * @param path
113
+ * @param handler
114
+ * @param middlewares
115
+ */
116
+ static patch(path, handler, middlewares) {
117
+ return super.patch(path, handler, middlewares);
118
+ }
119
+ /**
120
+ * Adds a new OPTIONS route to the router with the specified path, handler, and optional middlewares.
121
+ *
122
+ * @param path
123
+ * @param handler
124
+ * @param middlewares
125
+ */
126
+ static options(path, handler, middlewares) {
127
+ return super.options(path, handler, middlewares);
128
+ }
129
+ /**
130
+ * Adds a new HEAD route to the router.
131
+ *
132
+ * @param path
133
+ * @param handler
134
+ * @param middlewares
135
+ */
136
+ static head(path, handler, middlewares) {
137
+ return super.head(path, handler, middlewares);
138
+ }
139
+ /**
140
+ * Defines a group of routes with a common prefix.
141
+ *
142
+ * @param prefix
143
+ * @param callback
144
+ * @param middlewares
145
+ */
146
+ static async group(prefix, callback, middlewares) {
147
+ await super.group(prefix, callback, middlewares);
148
+ }
149
+ /**
150
+ * Adds global middlewares to the router, which will be applied to all routes.
151
+ *
152
+ * @param middlewares
153
+ * @param callback
154
+ */
155
+ static middleware(middlewares, callback) {
156
+ super.middleware(middlewares, callback);
157
+ }
158
+ static allRoutes(type) {
159
+ return super.allRoutes(type);
160
+ }
161
+ static route(name) {
162
+ return super.route(name);
163
+ }
164
+ /**
165
+ * Applies the registered routes to the given H3 application instance, setting up the
166
+ * necessary handlers and middlewares for each route.
167
+ *
168
+ * @param app
169
+ * @returns
170
+ */
171
+ static apply(app) {
172
+ for (const route of Array.from(this.routes)) {
173
+ let handlerFunction = null;
174
+ let instance = null;
175
+ let bindingTarget;
176
+ let bindingMethod;
177
+ let bindingHandler;
178
+ let bindingMetadata;
179
+ try {
180
+ const resolved = this.resolveHandler(route);
181
+ handlerFunction = resolved.handlerFunction;
182
+ instance = resolved.instance;
183
+ bindingTarget = resolved.bindingTarget;
184
+ bindingMethod = resolved.bindingMethod;
185
+ bindingHandler = resolved.bindingHandler;
186
+ bindingMetadata = resolved.bindingMetadata;
187
+ } catch (error) {
188
+ console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
189
+ throw error;
190
+ }
191
+ if (!handlerFunction) continue;
192
+ for (const method of route.methods) {
193
+ const allowedMethods = [
194
+ "get",
195
+ "post",
196
+ "put",
197
+ "delete",
198
+ "patch",
199
+ "options",
200
+ "head"
201
+ ];
202
+ if (method === "options" && route.methods.length > 1) continue;
203
+ if (!allowedMethods.includes(method)) {
204
+ const error = /* @__PURE__ */ new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
205
+ console.error("[ROUTES]", error.message);
206
+ throw error;
207
+ }
208
+ for (const registrationPath of route.registrationPaths) app[method](registrationPath, async (event) => {
209
+ try {
210
+ const ctx = event;
211
+ const reqBody = await Router.readBodyCached(ctx);
212
+ const override = Router.resolveMethodOverride(ctx.req.method, ctx.req.headers, reqBody);
213
+ if (method === "post" && override && override !== "post") return;
214
+ const inst = instance ?? route;
215
+ Router.bindRequestToInstance(ctx, inst, route, {
216
+ body: reqBody,
217
+ query: (0, h3.getQuery)(ctx),
218
+ params: (0, h3.getRouterParams)(ctx, { decode: true }),
219
+ method
220
+ });
221
+ const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
222
+ const resolved = await Promise.resolve(result);
223
+ const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
224
+ return Router.toResponse(ctx, outgoing, method, route.path);
225
+ } catch (error) {
226
+ return error;
227
+ }
228
+ }, { middleware: route.middlewares });
229
+ if ([
230
+ "put",
231
+ "patch",
232
+ "delete"
233
+ ].includes(method)) for (const registrationPath of route.registrationPaths) app.post(registrationPath, async (event) => {
234
+ try {
235
+ const ctx = event;
236
+ const reqBody = await Router.readBodyCached(ctx);
237
+ if (Router.resolveMethodOverride(ctx.req.method, ctx.req.headers, reqBody) !== method) return Symbol.for("h3.notFound");
238
+ const inst = instance ?? route;
239
+ Router.bindRequestToInstance(ctx, inst, route, {
240
+ body: reqBody,
241
+ query: (0, h3.getQuery)(ctx),
242
+ params: (0, h3.getRouterParams)(ctx, { decode: true }),
243
+ method
244
+ });
245
+ const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
246
+ const resolved = await Promise.resolve(result);
247
+ const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
248
+ return Router.toResponse(ctx, outgoing, method, route.path);
249
+ } catch (error) {
250
+ return error;
251
+ }
252
+ }, { middleware: route.middlewares });
253
+ }
254
+ }
255
+ return app;
256
+ }
257
+ };
258
+
259
+ //#endregion
260
+ exports.default = Router;
@@ -0,0 +1,132 @@
1
+ import { ApiResourceMiddleware, ControllerAction, HttpMethod } from "../types/basic.cjs";
2
+ import { H3App, Handler, HttpContext, Middleware } from "../types/h3.cjs";
3
+ import { Route } from "../Route.cjs";
4
+ import { CoreRouter } from "../core/router.cjs";
5
+ import { H3 } from "h3";
6
+
7
+ //#region src/h3/router.d.ts
8
+ /**
9
+ * @class clear-router H3 Router
10
+ * @description Laravel-style routing system for Express.js and H3 with support for CommonJS, ESM, and TypeScript
11
+ * @author 3m1n3nc3
12
+ * @repository https://github.com/arkstack-tmp/clear-router
13
+ */
14
+ declare class Router extends CoreRouter {
15
+ protected static routerStateNamespace: string;
16
+ private static readonly bodyCache;
17
+ private static toResponse;
18
+ private static readBodyCached;
19
+ /**
20
+ * Adds a new route to the router.
21
+ *
22
+ * @param methods
23
+ * @param path
24
+ * @param handler
25
+ * @param middlewares
26
+ */
27
+ static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
28
+ /**
29
+ * Define a resourceful API controller with standard CRUD routes
30
+ *
31
+ * @param basePath
32
+ * @param controller
33
+ * @param options
34
+ */
35
+ static apiResource(basePath: string, controller: any, options?: {
36
+ only?: ControllerAction[];
37
+ except?: ControllerAction[];
38
+ middlewares?: ApiResourceMiddleware<Middleware>;
39
+ }): void;
40
+ /**
41
+ * Adds a new GET route to the router with the specified path, handler, and optional middlewares.
42
+ *
43
+ * @param path
44
+ * @param handler
45
+ * @param middlewares
46
+ */
47
+ static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
48
+ /**
49
+ * Adds a new POST route to the router with the specified path, handler, and optional middlewares.
50
+ *
51
+ * @param path
52
+ * @param handler
53
+ * @param middlewares
54
+ */
55
+ static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
56
+ /**
57
+ * Adds a new PUT route to the router with the specified path, handler, and optional middlewares.
58
+ *
59
+ * @param path
60
+ * @param handler
61
+ * @param middlewares
62
+ */
63
+ static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
64
+ /**
65
+ * Adds a new DELETE route to the router with the specified path, handler, and optional middlewares.
66
+ *
67
+ * @param path
68
+ * @param handler
69
+ * @param middlewares
70
+ */
71
+ static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
72
+ /**
73
+ * Adds a new PATCH route to the router with the specified path, handler, and optional middlewares.
74
+ *
75
+ * @param path
76
+ * @param handler
77
+ * @param middlewares
78
+ */
79
+ static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
80
+ /**
81
+ * Adds a new OPTIONS route to the router with the specified path, handler, and optional middlewares.
82
+ *
83
+ * @param path
84
+ * @param handler
85
+ * @param middlewares
86
+ */
87
+ static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
88
+ /**
89
+ * Adds a new HEAD route to the router.
90
+ *
91
+ * @param path
92
+ * @param handler
93
+ * @param middlewares
94
+ */
95
+ static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
96
+ /**
97
+ * Defines a group of routes with a common prefix.
98
+ *
99
+ * @param prefix
100
+ * @param callback
101
+ * @param middlewares
102
+ */
103
+ static group(prefix: string, callback: () => void | Promise<void>, middlewares?: Middleware[]): Promise<void>;
104
+ /**
105
+ * Adds global middlewares to the router, which will be applied to all routes.
106
+ *
107
+ * @param middlewares
108
+ * @param callback
109
+ */
110
+ static middleware(middlewares: Middleware[], callback: () => void): void;
111
+ /**
112
+ * Retrieves all registered routes in the router, optionally organized by path or method
113
+ * for easier access and management.
114
+ *
115
+ * @param type
116
+ */
117
+ static allRoutes(): Array<Route<HttpContext, Middleware, Handler>>;
118
+ static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware, Handler>>;
119
+ static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
120
+ static allRoutes(type: 'name'): Record<string, Route<HttpContext, Middleware, Handler>>;
121
+ static route(name: string): Route<HttpContext, Middleware, Handler> | undefined;
122
+ /**
123
+ * Applies the registered routes to the given H3 application instance, setting up the
124
+ * necessary handlers and middlewares for each route.
125
+ *
126
+ * @param app
127
+ * @returns
128
+ */
129
+ static apply(app: H3): H3App;
130
+ }
131
+ //#endregion
132
+ export { Router };