clear-router 2.7.6 → 2.8.0

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