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,137 @@
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 { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
3
+
4
+ //#region types/fastify.d.ts
5
+ interface RequestWithGetBody extends FastifyRequest {
6
+ getBody: () => Record<string, any>;
7
+ }
8
+ interface HttpContext {
9
+ req: RequestWithGetBody;
10
+ reply: FastifyReply;
11
+ }
12
+ type RouteHandler = (ctx: HttpContext, req: ClearRequest) => any | Promise<any>;
13
+ type Handler = RouteHandler | ControllerHandler;
14
+ type NextFunction = (err?: Error) => void;
15
+ type Middleware = (req: RequestWithGetBody, reply: FastifyReply, next: NextFunction) => any | Promise<any>;
16
+ type FastifyApp = FastifyInstance;
17
+ //#endregion
18
+ //#region src/fastify/router.d.ts
19
+ /**
20
+ * @class clear-router Fastify Router
21
+ * @description Laravel-style routing system for Fastify using shared clear-router core
22
+ * @author 3m1n3nc3
23
+ * @repository https://github.com/toneflix/clear-router
24
+ */
25
+ declare class Router extends CoreRouter {
26
+ private static ensureRequestBodyAccessor;
27
+ /**
28
+ * Add a route to the router
29
+ *
30
+ * @param methods HTTP methods for the route
31
+ * @param path Route path
32
+ * @param handler Route handler function
33
+ * @param middlewares Optional middlewares for the route
34
+ */
35
+ static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
36
+ /**
37
+ * Define a resourceful API controller with standard CRUD routes
38
+ *
39
+ * @param basePath Base path for the resource
40
+ * @param controller Controller class or instance
41
+ * @param options Optional configuration for the resource
42
+ */
43
+ static apiResource(basePath: string, controller: any, options?: {
44
+ only?: ControllerAction[];
45
+ except?: ControllerAction[];
46
+ middlewares?: ApiResourceMiddleware<Middleware>;
47
+ }): void;
48
+ /**
49
+ * Define a GET route
50
+ *
51
+ * @param path
52
+ * @param handler
53
+ * @param middlewares
54
+ */
55
+ static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
56
+ /**
57
+ * Define a POST route
58
+ *
59
+ * @param path
60
+ * @param handler
61
+ * @param middlewares
62
+ */
63
+ static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
64
+ /**
65
+ * Define a PUT route
66
+ *
67
+ * @param path
68
+ * @param handler
69
+ * @param middlewares
70
+ */
71
+ static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
72
+ /**
73
+ * Define a DELETE route
74
+ *
75
+ * @param path
76
+ * @param handler
77
+ * @param middlewares
78
+ */
79
+ static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
80
+ /**
81
+ * Define a PATCH route
82
+ *
83
+ * @param path
84
+ * @param handler
85
+ * @param middlewares
86
+ */
87
+ static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
88
+ /**
89
+ * Define an OPTIONS route
90
+ *
91
+ * @param path
92
+ * @param handler
93
+ * @param middlewares
94
+ */
95
+ static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
96
+ /**
97
+ * Define a HEAD route
98
+ *
99
+ * @param path
100
+ * @param handler
101
+ * @param middlewares
102
+ */
103
+ static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
104
+ /**
105
+ * Define a group of routes with a common prefix and optional middlewares
106
+ *
107
+ * @param prefix
108
+ * @param callback
109
+ * @param middlewares
110
+ */
111
+ static group(prefix: string, callback: () => void | Promise<void>, middlewares?: Middleware[]): Promise<void>;
112
+ /**
113
+ * Apply middlewares to a group of routes defined within the callback
114
+ *
115
+ * @param middlewares
116
+ * @param callback
117
+ */
118
+ static middleware(middlewares: Middleware[], callback: () => void): void;
119
+ static allRoutes(): Array<Route<HttpContext, Middleware, Handler>>;
120
+ /**
121
+ * @param type - 'path' to get routes organized by path
122
+ */
123
+ static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware, Handler>>;
124
+ /**
125
+ * @param type - 'method' to get routes organized by method
126
+ */
127
+ static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
128
+ /**
129
+ * Apply the defined routes to a Fastify application instance
130
+ *
131
+ * @param app - The Fastify application instance
132
+ * @returns The Fastify application instance with the applied routes
133
+ */
134
+ static apply(app: FastifyApp): FastifyApp;
135
+ }
136
+ //#endregion
137
+ export { Router };
@@ -0,0 +1,137 @@
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 { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
3
+
4
+ //#region types/fastify.d.ts
5
+ interface RequestWithGetBody extends FastifyRequest {
6
+ getBody: () => Record<string, any>;
7
+ }
8
+ interface HttpContext {
9
+ req: RequestWithGetBody;
10
+ reply: FastifyReply;
11
+ }
12
+ type RouteHandler = (ctx: HttpContext, req: ClearRequest) => any | Promise<any>;
13
+ type Handler = RouteHandler | ControllerHandler;
14
+ type NextFunction = (err?: Error) => void;
15
+ type Middleware = (req: RequestWithGetBody, reply: FastifyReply, next: NextFunction) => any | Promise<any>;
16
+ type FastifyApp = FastifyInstance;
17
+ //#endregion
18
+ //#region src/fastify/router.d.ts
19
+ /**
20
+ * @class clear-router Fastify Router
21
+ * @description Laravel-style routing system for Fastify using shared clear-router core
22
+ * @author 3m1n3nc3
23
+ * @repository https://github.com/toneflix/clear-router
24
+ */
25
+ declare class Router extends CoreRouter {
26
+ private static ensureRequestBodyAccessor;
27
+ /**
28
+ * Add a route to the router
29
+ *
30
+ * @param methods HTTP methods for the route
31
+ * @param path Route path
32
+ * @param handler Route handler function
33
+ * @param middlewares Optional middlewares for the route
34
+ */
35
+ static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
36
+ /**
37
+ * Define a resourceful API controller with standard CRUD routes
38
+ *
39
+ * @param basePath Base path for the resource
40
+ * @param controller Controller class or instance
41
+ * @param options Optional configuration for the resource
42
+ */
43
+ static apiResource(basePath: string, controller: any, options?: {
44
+ only?: ControllerAction[];
45
+ except?: ControllerAction[];
46
+ middlewares?: ApiResourceMiddleware<Middleware>;
47
+ }): void;
48
+ /**
49
+ * Define a GET route
50
+ *
51
+ * @param path
52
+ * @param handler
53
+ * @param middlewares
54
+ */
55
+ static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
56
+ /**
57
+ * Define a POST route
58
+ *
59
+ * @param path
60
+ * @param handler
61
+ * @param middlewares
62
+ */
63
+ static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
64
+ /**
65
+ * Define a PUT route
66
+ *
67
+ * @param path
68
+ * @param handler
69
+ * @param middlewares
70
+ */
71
+ static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
72
+ /**
73
+ * Define a DELETE route
74
+ *
75
+ * @param path
76
+ * @param handler
77
+ * @param middlewares
78
+ */
79
+ static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
80
+ /**
81
+ * Define a PATCH route
82
+ *
83
+ * @param path
84
+ * @param handler
85
+ * @param middlewares
86
+ */
87
+ static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
88
+ /**
89
+ * Define an OPTIONS route
90
+ *
91
+ * @param path
92
+ * @param handler
93
+ * @param middlewares
94
+ */
95
+ static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
96
+ /**
97
+ * Define a HEAD route
98
+ *
99
+ * @param path
100
+ * @param handler
101
+ * @param middlewares
102
+ */
103
+ static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): void;
104
+ /**
105
+ * Define a group of routes with a common prefix and optional middlewares
106
+ *
107
+ * @param prefix
108
+ * @param callback
109
+ * @param middlewares
110
+ */
111
+ static group(prefix: string, callback: () => void | Promise<void>, middlewares?: Middleware[]): Promise<void>;
112
+ /**
113
+ * Apply middlewares to a group of routes defined within the callback
114
+ *
115
+ * @param middlewares
116
+ * @param callback
117
+ */
118
+ static middleware(middlewares: Middleware[], callback: () => void): void;
119
+ static allRoutes(): Array<Route<HttpContext, Middleware, Handler>>;
120
+ /**
121
+ * @param type - 'path' to get routes organized by path
122
+ */
123
+ static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware, Handler>>;
124
+ /**
125
+ * @param type - 'method' to get routes organized by method
126
+ */
127
+ static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
128
+ /**
129
+ * Apply the defined routes to a Fastify application instance
130
+ *
131
+ * @param app - The Fastify application instance
132
+ * @returns The Fastify application instance with the applied routes
133
+ */
134
+ static apply(app: FastifyApp): FastifyApp;
135
+ }
136
+ //#endregion
137
+ export { Router };
@@ -0,0 +1,212 @@
1
+ import { t as CoreRouter } from "../router-Bug2IE_u.mjs";
2
+
3
+ //#region src/fastify/router.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/toneflix/clear-router
9
+ */
10
+ var Router = class Router extends CoreRouter {
11
+ static ensureRequestBodyAccessor(req) {
12
+ if (typeof req.getBody !== "function") req.getBody = () => req.body ?? {};
13
+ }
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, path, handler, middlewares) {
23
+ super.add(methods, path, handler, middlewares);
24
+ }
25
+ /**
26
+ * Define a resourceful API controller with standard CRUD routes
27
+ *
28
+ * @param basePath Base path for the resource
29
+ * @param controller Controller class or instance
30
+ * @param options Optional configuration for the resource
31
+ */
32
+ static apiResource(basePath, controller, options) {
33
+ super.apiResource(basePath, controller, options);
34
+ }
35
+ /**
36
+ * Define a GET route
37
+ *
38
+ * @param path
39
+ * @param handler
40
+ * @param middlewares
41
+ */
42
+ static get(path, handler, middlewares) {
43
+ super.get(path, handler, middlewares);
44
+ }
45
+ /**
46
+ * Define a POST route
47
+ *
48
+ * @param path
49
+ * @param handler
50
+ * @param middlewares
51
+ */
52
+ static post(path, handler, middlewares) {
53
+ super.post(path, handler, middlewares);
54
+ }
55
+ /**
56
+ * Define a PUT route
57
+ *
58
+ * @param path
59
+ * @param handler
60
+ * @param middlewares
61
+ */
62
+ static put(path, handler, middlewares) {
63
+ super.put(path, handler, middlewares);
64
+ }
65
+ /**
66
+ * Define a DELETE route
67
+ *
68
+ * @param path
69
+ * @param handler
70
+ * @param middlewares
71
+ */
72
+ static delete(path, handler, middlewares) {
73
+ super.delete(path, handler, middlewares);
74
+ }
75
+ /**
76
+ * Define a PATCH route
77
+ *
78
+ * @param path
79
+ * @param handler
80
+ * @param middlewares
81
+ */
82
+ static patch(path, handler, middlewares) {
83
+ super.patch(path, handler, middlewares);
84
+ }
85
+ /**
86
+ * Define an OPTIONS route
87
+ *
88
+ * @param path
89
+ * @param handler
90
+ * @param middlewares
91
+ */
92
+ static options(path, handler, middlewares) {
93
+ super.options(path, handler, middlewares);
94
+ }
95
+ /**
96
+ * Define a HEAD route
97
+ *
98
+ * @param path
99
+ * @param handler
100
+ * @param middlewares
101
+ */
102
+ static head(path, handler, middlewares) {
103
+ super.head(path, handler, middlewares);
104
+ }
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 async group(prefix, callback, middlewares) {
113
+ await super.group(prefix, callback, middlewares);
114
+ }
115
+ /**
116
+ * Apply middlewares to a group of routes defined within the callback
117
+ *
118
+ * @param middlewares
119
+ * @param callback
120
+ */
121
+ static middleware(middlewares, callback) {
122
+ super.middleware(middlewares, callback);
123
+ }
124
+ static allRoutes(type) {
125
+ return super.allRoutes(type);
126
+ }
127
+ /**
128
+ * Apply the defined routes to a Fastify application instance
129
+ *
130
+ * @param app - The Fastify application instance
131
+ * @returns The Fastify application instance with the applied routes
132
+ */
133
+ static apply(app) {
134
+ for (const route of this.routes) {
135
+ let handlerFunction = null;
136
+ let instance = null;
137
+ try {
138
+ const resolved = this.resolveHandler(route);
139
+ handlerFunction = resolved.handlerFunction;
140
+ instance = resolved.instance;
141
+ } catch (error) {
142
+ console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
143
+ throw error;
144
+ }
145
+ if (!handlerFunction) continue;
146
+ for (const method of route.methods) {
147
+ const allowedMethods = [
148
+ "get",
149
+ "post",
150
+ "put",
151
+ "delete",
152
+ "patch",
153
+ "options",
154
+ "head"
155
+ ];
156
+ if (method === "options" && route.methods.length > 1) continue;
157
+ if (!allowedMethods.includes(method)) throw new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
158
+ app.route({
159
+ method: method.toUpperCase(),
160
+ url: route.path,
161
+ preHandler: route.middlewares,
162
+ handler: async (req, reply) => {
163
+ Router.ensureRequestBodyAccessor(req);
164
+ const override = Router.resolveMethodOverride(req.method, req.headers, req.body);
165
+ if (method === "post" && override && override !== "post") return reply.code(404).send();
166
+ const ctx = {
167
+ req,
168
+ reply
169
+ };
170
+ const inst = instance ?? route;
171
+ Router.bindRequestToInstance(ctx, inst, route, {
172
+ body: ctx.req.getBody(),
173
+ query: ctx.req.query ?? {},
174
+ params: ctx.req.params ?? {}
175
+ });
176
+ const result = handlerFunction(ctx, inst.clearRequest);
177
+ return await Promise.resolve(result);
178
+ }
179
+ });
180
+ if ([
181
+ "put",
182
+ "patch",
183
+ "delete"
184
+ ].includes(method)) app.route({
185
+ method: "POST",
186
+ url: route.path,
187
+ preHandler: route.middlewares,
188
+ handler: async (req, reply) => {
189
+ Router.ensureRequestBodyAccessor(req);
190
+ if (Router.resolveMethodOverride(req.method, req.headers, req.body) !== method) return reply.code(404).send();
191
+ const ctx = {
192
+ req,
193
+ reply
194
+ };
195
+ const inst = instance ?? route;
196
+ Router.bindRequestToInstance(ctx, inst, route, {
197
+ body: ctx.req.getBody(),
198
+ query: ctx.req.query ?? {},
199
+ params: ctx.req.params ?? {}
200
+ });
201
+ const result = handlerFunction(ctx, inst.clearRequest);
202
+ return await Promise.resolve(result);
203
+ }
204
+ });
205
+ }
206
+ }
207
+ return app;
208
+ }
209
+ };
210
+
211
+ //#endregion
212
+ export { Router };