clear-router 2.1.12 → 2.3.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 (43) hide show
  1. package/README.md +4 -4
  2. package/dist/core/index.cjs +4 -0
  3. package/dist/core/index.d.cts +2 -0
  4. package/dist/core/index.d.mts +2 -0
  5. package/dist/core/index.mjs +3 -0
  6. package/dist/express/index.cjs +86 -247
  7. package/dist/express/index.d.cts +68 -97
  8. package/dist/express/index.d.mts +68 -97
  9. package/dist/express/index.mjs +86 -247
  10. package/dist/fastify/index.cjs +213 -0
  11. package/dist/fastify/index.d.cts +137 -0
  12. package/dist/fastify/index.d.mts +137 -0
  13. package/dist/fastify/index.mjs +212 -0
  14. package/dist/h3/index.cjs +88 -242
  15. package/dist/h3/index.d.cts +72 -93
  16. package/dist/h3/index.d.mts +72 -93
  17. package/dist/h3/index.mjs +88 -242
  18. package/dist/hono/index.cjs +227 -0
  19. package/dist/hono/index.d.cts +141 -0
  20. package/dist/hono/index.d.mts +141 -0
  21. package/dist/hono/index.mjs +226 -0
  22. package/dist/index.cjs +357 -0
  23. package/dist/index.d.cts +195 -40
  24. package/dist/index.d.mts +195 -40
  25. package/dist/index.mjs +358 -1
  26. package/dist/router-Ba2MVNn-.cjs +412 -0
  27. package/dist/router-Bug2IE_u.mjs +407 -0
  28. package/dist/router-DLmimm_U.d.cts +320 -0
  29. package/dist/router-cWYmcfTX.d.mts +320 -0
  30. package/dist/types/ClearRequest.d.mts +1 -1
  31. package/dist/types/Route.d.mts +5 -5
  32. package/dist/types/basic.d.mts +1 -4
  33. package/dist/types/express.d.mts +1 -1
  34. package/dist/types/fastify.d.mts +19 -0
  35. package/dist/types/fastify.mjs +1 -0
  36. package/dist/types/h3.d.mts +1 -1
  37. package/dist/types/hono.d.mts +18 -0
  38. package/dist/types/hono.mjs +1 -0
  39. package/package.json +26 -2
  40. package/dist/Route-BbPXcDGX.mjs +0 -50
  41. package/dist/Route-DhC4kNPX.cjs +0 -62
  42. package/dist/basic-C_1O6RVq.d.cts +0 -138
  43. package/dist/basic-cLeny2Zk.d.mts +0 -138
@@ -0,0 +1,227 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_router = require('../router-Ba2MVNn-.cjs');
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/toneflix/clear-router
10
+ */
11
+ var Router = class Router extends require_router.CoreRouter {
12
+ static bodyCache = /* @__PURE__ */ new WeakMap();
13
+ static toResponse(ctx, value) {
14
+ if (value instanceof Response) return value;
15
+ if (typeof value === "undefined") return void 0;
16
+ if (value === null) return ctx.body(null);
17
+ if (typeof value === "string") return ctx.text(value);
18
+ if (typeof value === "object") return ctx.json(value);
19
+ return ctx.text(String(value));
20
+ }
21
+ static getParams(ctx) {
22
+ try {
23
+ const raw = ctx.req.param?.();
24
+ return raw && typeof raw === "object" ? raw : {};
25
+ } catch {
26
+ return {};
27
+ }
28
+ }
29
+ static async readBodyCached(ctx) {
30
+ if (this.bodyCache.has(ctx)) {
31
+ const cached = this.bodyCache.get(ctx) || {};
32
+ ctx.req.getBody = () => cached;
33
+ return cached;
34
+ }
35
+ let body = {};
36
+ const contentType = (ctx.req.header("content-type") || "").toLowerCase();
37
+ if (contentType.includes("application/json")) body = await ctx.req.json().catch(() => ({}));
38
+ else if (contentType.includes("multipart/form-data") || contentType.includes("application/x-www-form-urlencoded")) body = await ctx.req.parseBody().then((v) => v).catch(() => ({}));
39
+ ctx.req.getBody = () => body;
40
+ this.bodyCache.set(ctx, body);
41
+ return body;
42
+ }
43
+ /**
44
+ * Resolve method override from query, headers, or body
45
+ *
46
+ * @param methods
47
+ * @param path
48
+ * @param handler
49
+ * @param middlewares
50
+ */
51
+ static add(methods, path, handler, middlewares) {
52
+ super.add(methods, path, handler, middlewares);
53
+ }
54
+ /**
55
+ * Define a resourceful API controller with standard CRUD routes
56
+ *
57
+ * @param basePath
58
+ * @param controller
59
+ * @param options
60
+ */
61
+ static apiResource(basePath, controller, options) {
62
+ super.apiResource(basePath, controller, options);
63
+ }
64
+ /**
65
+ * Define a GET route
66
+ *
67
+ * @param path
68
+ * @param handler
69
+ * @param middlewares
70
+ */
71
+ static get(path, handler, middlewares) {
72
+ super.get(path, handler, middlewares);
73
+ }
74
+ /**
75
+ * Define a POST route
76
+ *
77
+ * @param path
78
+ * @param handler
79
+ * @param middlewares
80
+ */
81
+ static post(path, handler, middlewares) {
82
+ super.post(path, handler, middlewares);
83
+ }
84
+ /**
85
+ * Define a PUT route
86
+ *
87
+ * @param path
88
+ * @param handler
89
+ * @param middlewares
90
+ */
91
+ static put(path, handler, middlewares) {
92
+ super.put(path, handler, middlewares);
93
+ }
94
+ /**
95
+ * Define a DELETE route
96
+ *
97
+ * @param path
98
+ * @param handler
99
+ * @param middlewares
100
+ */
101
+ static delete(path, handler, middlewares) {
102
+ super.delete(path, handler, middlewares);
103
+ }
104
+ /**
105
+ * Define a PATCH route
106
+ *
107
+ * @param path
108
+ * @param handler
109
+ * @param middlewares
110
+ */
111
+ static patch(path, handler, middlewares) {
112
+ super.patch(path, handler, middlewares);
113
+ }
114
+ /**
115
+ * Define an OPTIONS route
116
+ *
117
+ * @param path
118
+ * @param handler
119
+ * @param middlewares
120
+ */
121
+ static options(path, handler, middlewares) {
122
+ super.options(path, handler, middlewares);
123
+ }
124
+ /**
125
+ * Define a HEAD route
126
+ *
127
+ * @param path
128
+ * @param handler
129
+ * @param middlewares
130
+ */
131
+ static head(path, handler, middlewares) {
132
+ super.head(path, handler, middlewares);
133
+ }
134
+ /**
135
+ * Define a group of routes with a common prefix and optional middlewares
136
+ *
137
+ * @param prefix
138
+ * @param callback
139
+ * @param middlewares
140
+ */
141
+ static async group(prefix, callback, middlewares) {
142
+ await super.group(prefix, callback, middlewares);
143
+ }
144
+ /**
145
+ * Apply middlewares to a group of routes defined within the callback
146
+ *
147
+ * @param middlewares - Middleware or array of middlewares to apply
148
+ * @param callback - Function that defines the routes to which the middlewares will be applied
149
+ */
150
+ static middleware(middlewares, callback) {
151
+ super.middleware(middlewares, callback);
152
+ }
153
+ static allRoutes(type) {
154
+ return super.allRoutes(type);
155
+ }
156
+ /**
157
+ * Apply the defined routes to a Hono application instance
158
+ *
159
+ * @param app The Hono application instance
160
+ * @returns The Hono application instance with the applied routes
161
+ */
162
+ static apply(app) {
163
+ for (const route of this.routes) {
164
+ let handlerFunction = null;
165
+ let instance = null;
166
+ try {
167
+ const resolved = this.resolveHandler(route);
168
+ handlerFunction = resolved.handlerFunction;
169
+ instance = resolved.instance;
170
+ } catch (error) {
171
+ console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
172
+ throw error;
173
+ }
174
+ if (!handlerFunction) continue;
175
+ for (const method of route.methods) {
176
+ const allowedMethods = [
177
+ "get",
178
+ "post",
179
+ "put",
180
+ "delete",
181
+ "patch",
182
+ "options",
183
+ "head"
184
+ ];
185
+ if (method === "options" && route.methods.length > 1) continue;
186
+ if (!allowedMethods.includes(method)) throw new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
187
+ app[method](route.path, ...route.middlewares || [], async (context) => {
188
+ const ctx = context;
189
+ const reqBody = await Router.readBodyCached(ctx);
190
+ const override = Router.resolveMethodOverride(ctx.req.method, ctx.req.header(), reqBody);
191
+ if (method === "post" && override && override !== "post") return;
192
+ const inst = instance ?? route;
193
+ Router.bindRequestToInstance(ctx, inst, route, {
194
+ body: reqBody,
195
+ query: ctx.req.query(),
196
+ params: Router.getParams(ctx)
197
+ });
198
+ const result = handlerFunction(ctx, inst.clearRequest);
199
+ const resolved = await Promise.resolve(result);
200
+ return Router.toResponse(ctx, resolved);
201
+ });
202
+ if ([
203
+ "put",
204
+ "patch",
205
+ "delete"
206
+ ].includes(method)) app.post(route.path, ...route.middlewares || [], async (context) => {
207
+ const ctx = context;
208
+ const reqBody = await Router.readBodyCached(ctx);
209
+ if (Router.resolveMethodOverride(ctx.req.method, ctx.req.header(), reqBody) !== method) return;
210
+ const inst = instance ?? route;
211
+ Router.bindRequestToInstance(ctx, inst, route, {
212
+ body: reqBody,
213
+ query: ctx.req.query(),
214
+ params: Router.getParams(ctx)
215
+ });
216
+ const result = handlerFunction(ctx, inst.clearRequest);
217
+ const resolved = await Promise.resolve(result);
218
+ return Router.toResponse(ctx, resolved);
219
+ });
220
+ }
221
+ }
222
+ return app;
223
+ }
224
+ };
225
+
226
+ //#endregion
227
+ exports.Router = Router;
@@ -0,0 +1,141 @@
1
+ import { d as ApiResourceMiddleware, f as ControllerAction, m as HttpMethod, n as ClearRequest, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-DLmimm_U.cjs";
2
+ import { Context, HonoRequest, MiddlewareHandler } from "hono";
3
+
4
+ //#region types/hono.d.ts
5
+ type RequestWithGetBody = HonoRequest & {
6
+ getBody: () => Record<string, any>;
7
+ };
8
+ type HttpContext = Context & {
9
+ req: RequestWithGetBody;
10
+ };
11
+ type RouteHandler = (ctx: HttpContext, req: ClearRequest) => any | Promise<any>;
12
+ type Handler = RouteHandler | ControllerHandler;
13
+ type Middleware = MiddlewareHandler;
14
+ type HonoApp = { [K in 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head']: (path: string, ...handlers: Middleware[]) => any };
15
+ //#endregion
16
+ //#region src/hono/router.d.ts
17
+ /**
18
+ * @class clear-router Hono Router
19
+ * @description Laravel-style routing system for Hono using shared clear-router core
20
+ * @author 3m1n3nc3
21
+ * @repository https://github.com/toneflix/clear-router
22
+ */
23
+ declare class Router extends CoreRouter {
24
+ private static readonly bodyCache;
25
+ private static toResponse;
26
+ private static getParams;
27
+ private static readBodyCached;
28
+ /**
29
+ * Resolve method override from query, headers, or body
30
+ *
31
+ * @param methods
32
+ * @param path
33
+ * @param handler
34
+ * @param middlewares
35
+ */
36
+ static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
37
+ /**
38
+ * Define a resourceful API controller with standard CRUD routes
39
+ *
40
+ * @param basePath
41
+ * @param controller
42
+ * @param options
43
+ */
44
+ static apiResource(basePath: string, controller: any, options?: {
45
+ only?: ControllerAction[];
46
+ except?: ControllerAction[];
47
+ middlewares?: ApiResourceMiddleware<Middleware>;
48
+ }): void;
49
+ /**
50
+ * Define a GET route
51
+ *
52
+ * @param path
53
+ * @param handler
54
+ * @param middlewares
55
+ */
56
+ static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
57
+ /**
58
+ * Define a POST route
59
+ *
60
+ * @param path
61
+ * @param handler
62
+ * @param middlewares
63
+ */
64
+ static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
65
+ /**
66
+ * Define a PUT route
67
+ *
68
+ * @param path
69
+ * @param handler
70
+ * @param middlewares
71
+ */
72
+ static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
73
+ /**
74
+ * Define a DELETE route
75
+ *
76
+ * @param path
77
+ * @param handler
78
+ * @param middlewares
79
+ */
80
+ static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
81
+ /**
82
+ * Define a PATCH route
83
+ *
84
+ * @param path
85
+ * @param handler
86
+ * @param middlewares
87
+ */
88
+ static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
89
+ /**
90
+ * Define an OPTIONS route
91
+ *
92
+ * @param path
93
+ * @param handler
94
+ * @param middlewares
95
+ */
96
+ static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
97
+ /**
98
+ * Define a HEAD route
99
+ *
100
+ * @param path
101
+ * @param handler
102
+ * @param middlewares
103
+ */
104
+ static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
105
+ /**
106
+ * Define a group of routes with a common prefix and optional middlewares
107
+ *
108
+ * @param prefix
109
+ * @param callback
110
+ * @param middlewares
111
+ */
112
+ static group(prefix: string, callback: () => void | Promise<void>, middlewares?: Middleware[]): Promise<void>;
113
+ /**
114
+ * Apply middlewares to a group of routes defined within the callback
115
+ *
116
+ * @param middlewares - Middleware or array of middlewares to apply
117
+ * @param callback - Function that defines the routes to which the middlewares will be applied
118
+ */
119
+ static middleware(middlewares: Middleware[], callback: () => void): void;
120
+ /**
121
+ * Get all defined routes, optionally organized by path or method
122
+ */
123
+ static allRoutes(): Array<Route<HttpContext, Middleware, Handler>>;
124
+ /**
125
+ * @param type - 'path' to get routes organized by path
126
+ */
127
+ static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware, Handler>>;
128
+ /**
129
+ * @param type - 'method' to get routes organized by method
130
+ */
131
+ static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
132
+ /**
133
+ * Apply the defined routes to a Hono application instance
134
+ *
135
+ * @param app The Hono application instance
136
+ * @returns The Hono application instance with the applied routes
137
+ */
138
+ static apply(app: HonoApp): HonoApp;
139
+ }
140
+ //#endregion
141
+ export { Router };
@@ -0,0 +1,141 @@
1
+ import { d as ApiResourceMiddleware, f as ControllerAction, m as HttpMethod, n as ClearRequest, p as ControllerHandler, r as Route, t as CoreRouter } from "../router-cWYmcfTX.mjs";
2
+ import { Context, HonoRequest, MiddlewareHandler } from "hono";
3
+
4
+ //#region types/hono.d.ts
5
+ type RequestWithGetBody = HonoRequest & {
6
+ getBody: () => Record<string, any>;
7
+ };
8
+ type HttpContext = Context & {
9
+ req: RequestWithGetBody;
10
+ };
11
+ type RouteHandler = (ctx: HttpContext, req: ClearRequest) => any | Promise<any>;
12
+ type Handler = RouteHandler | ControllerHandler;
13
+ type Middleware = MiddlewareHandler;
14
+ type HonoApp = { [K in 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head']: (path: string, ...handlers: Middleware[]) => any };
15
+ //#endregion
16
+ //#region src/hono/router.d.ts
17
+ /**
18
+ * @class clear-router Hono Router
19
+ * @description Laravel-style routing system for Hono using shared clear-router core
20
+ * @author 3m1n3nc3
21
+ * @repository https://github.com/toneflix/clear-router
22
+ */
23
+ declare class Router extends CoreRouter {
24
+ private static readonly bodyCache;
25
+ private static toResponse;
26
+ private static getParams;
27
+ private static readBodyCached;
28
+ /**
29
+ * Resolve method override from query, headers, or body
30
+ *
31
+ * @param methods
32
+ * @param path
33
+ * @param handler
34
+ * @param middlewares
35
+ */
36
+ static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
37
+ /**
38
+ * Define a resourceful API controller with standard CRUD routes
39
+ *
40
+ * @param basePath
41
+ * @param controller
42
+ * @param options
43
+ */
44
+ static apiResource(basePath: string, controller: any, options?: {
45
+ only?: ControllerAction[];
46
+ except?: ControllerAction[];
47
+ middlewares?: ApiResourceMiddleware<Middleware>;
48
+ }): void;
49
+ /**
50
+ * Define a GET route
51
+ *
52
+ * @param path
53
+ * @param handler
54
+ * @param middlewares
55
+ */
56
+ static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
57
+ /**
58
+ * Define a POST route
59
+ *
60
+ * @param path
61
+ * @param handler
62
+ * @param middlewares
63
+ */
64
+ static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
65
+ /**
66
+ * Define a PUT route
67
+ *
68
+ * @param path
69
+ * @param handler
70
+ * @param middlewares
71
+ */
72
+ static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
73
+ /**
74
+ * Define a DELETE route
75
+ *
76
+ * @param path
77
+ * @param handler
78
+ * @param middlewares
79
+ */
80
+ static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
81
+ /**
82
+ * Define a PATCH route
83
+ *
84
+ * @param path
85
+ * @param handler
86
+ * @param middlewares
87
+ */
88
+ static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
89
+ /**
90
+ * Define an OPTIONS route
91
+ *
92
+ * @param path
93
+ * @param handler
94
+ * @param middlewares
95
+ */
96
+ static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
97
+ /**
98
+ * Define a HEAD route
99
+ *
100
+ * @param path
101
+ * @param handler
102
+ * @param middlewares
103
+ */
104
+ static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
105
+ /**
106
+ * Define a group of routes with a common prefix and optional middlewares
107
+ *
108
+ * @param prefix
109
+ * @param callback
110
+ * @param middlewares
111
+ */
112
+ static group(prefix: string, callback: () => void | Promise<void>, middlewares?: Middleware[]): Promise<void>;
113
+ /**
114
+ * Apply middlewares to a group of routes defined within the callback
115
+ *
116
+ * @param middlewares - Middleware or array of middlewares to apply
117
+ * @param callback - Function that defines the routes to which the middlewares will be applied
118
+ */
119
+ static middleware(middlewares: Middleware[], callback: () => void): void;
120
+ /**
121
+ * Get all defined routes, optionally organized by path or method
122
+ */
123
+ static allRoutes(): Array<Route<HttpContext, Middleware, Handler>>;
124
+ /**
125
+ * @param type - 'path' to get routes organized by path
126
+ */
127
+ static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware, Handler>>;
128
+ /**
129
+ * @param type - 'method' to get routes organized by method
130
+ */
131
+ static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
132
+ /**
133
+ * Apply the defined routes to a Hono application instance
134
+ *
135
+ * @param app The Hono application instance
136
+ * @returns The Hono application instance with the applied routes
137
+ */
138
+ static apply(app: HonoApp): HonoApp;
139
+ }
140
+ //#endregion
141
+ export { Router };