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
package/dist/index.d.cts CHANGED
@@ -1,57 +1,41 @@
1
1
  import { NextFunction, Request, Response as Response$1 } from "express";
2
- import { H3Event, Middleware as Middleware$1 } from "h3";
2
+ import { Middleware } from "h3";
3
+ import { AsyncLocalStorage } from "node:async_hooks";
3
4
 
4
- //#region types/h3.d.ts
5
- type HttpRequest = H3Event['req'] & {
6
- getBody: () => Record<string, any>;
7
- };
8
- /**
9
- * HTTP context passed to route handlers
10
- */
11
- type HttpContext = Omit<H3Event, 'req'> & {
12
- req: HttpRequest;
13
- };
14
- /**
15
- * Route handler function type
16
- */
17
- type RouteHandler = (
18
- /**
19
- * H3 event context
20
- */
21
-
22
- ctx: HttpContext,
23
- /**
24
- * ClearRequest instance
25
- */
26
-
27
- req: ClearRequest) => any | Promise<any>;
28
- /**
29
- * Handler can be either a function or controller reference
30
- */
31
- type Handler = RouteHandler | ControllerHandler;
32
- //#endregion
33
5
  //#region types/basic.d.ts
34
- /**
35
- * Controller method reference
36
- */
37
- type ControllerHandler = [any, string];
38
6
  /**
39
7
  * HTTP methods supported by the router
40
8
  */
41
9
  type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
10
+ /**
11
+ * Common controller action names
12
+ */
13
+ type ControllerAction = 'index' | 'show' | 'create' | 'update' | 'destroy';
42
14
  /**
43
15
  * Generic Object type for request data
44
16
  */
45
17
  type RequestData = Record<string, any>;
18
+ type ApiResourceMiddleware<M = any> = M | M[] | { [K in ControllerAction]?: M | M[] };
19
+ interface RouterConfig {
20
+ /**
21
+ * Configuration for method override functionality, allowing clients to use a
22
+ * specific header or body parameter to override the HTTP method.
23
+ */
24
+ methodOverride?: {
25
+ /** Whether method override is enabled */enabled?: boolean; /** Keys in the request body to check for method override */
26
+ bodyKeys?: string[] | string; /** Keys in the request headers to check for method override */
27
+ headerKeys?: string[] | string;
28
+ };
29
+ }
46
30
  //#endregion
47
31
  //#region types/express.d.ts
48
32
  /**
49
33
  * Middleware function type
50
34
  */
51
- type Middleware = (req: Request, res: Response$1, next: NextFunction) => any | Promise<any>;
35
+ type Middleware$1 = (req: Request, res: Response$1, next: NextFunction) => any | Promise<any>;
52
36
  //#endregion
53
37
  //#region src/Route.d.ts
54
- declare class Route<X = any, M = Middleware$1 | Middleware> {
38
+ declare class Route<X = any, M = Middleware | Middleware$1, H = any> {
55
39
  ctx: X;
56
40
  body: RequestData;
57
41
  query: RequestData;
@@ -59,17 +43,17 @@ declare class Route<X = any, M = Middleware$1 | Middleware> {
59
43
  clearRequest: ClearRequest;
60
44
  methods: HttpMethod[];
61
45
  path: string;
62
- handler: Handler;
46
+ handler: H;
63
47
  middlewares: M[];
64
48
  controllerName?: string;
65
49
  actionName?: string;
66
50
  handlerType: 'function' | 'controller';
67
51
  middlewareCount: number;
68
- constructor(methods: HttpMethod[], path: string, handler: Handler, middlewares?: M[]);
52
+ constructor(methods: HttpMethod[], path: string, handler: H, middlewares?: M[]);
69
53
  }
70
54
  //#endregion
71
55
  //#region src/ClearRequest.d.ts
72
- declare class ClearRequest<X = any, M = Middleware$1 | Middleware> {
56
+ declare class ClearRequest<X = any, M = Middleware | Middleware$1> {
73
57
  [key: string]: any;
74
58
  /**
75
59
  * @param body - Parsed request body
@@ -97,4 +81,175 @@ declare abstract class Controller<X = any> {
97
81
  clearRequest: ClearRequest;
98
82
  }
99
83
  //#endregion
100
- export { ClearRequest, Controller, Route };
84
+ //#region src/core/router.d.ts
85
+ /**
86
+ * @class clear-router CoreRouter
87
+ * @description Core routing logic for clear-router, shared between all supported adapters (Express.js, H3, etc.)
88
+ * @author 3m1n3nc3
89
+ * @repository https://github.com/toneflix/clear-router
90
+ */
91
+ declare abstract class CoreRouter {
92
+ protected static createDefaultOptionsHandler(): any;
93
+ static config: RouterConfig;
94
+ protected static groupContext: AsyncLocalStorage<{
95
+ prefix: string;
96
+ groupMiddlewares: any[];
97
+ }>;
98
+ static routes: Array<Route<any, any, any>>;
99
+ static routesByPathMethod: Record<string, Route<any, any, any>>;
100
+ static routesByMethod: { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
101
+ static prefix: string;
102
+ static groupMiddlewares: any[];
103
+ static globalMiddlewares: any[];
104
+ protected static ensureState(this: any): void;
105
+ /**
106
+ * Normalizes a path by ensuring it starts with a single slash and does not have trailing
107
+ * slashes, while preserving dynamic segments and parameters.
108
+ *
109
+ * @param path The path to normalize.
110
+ * @returns The normalized path.
111
+ */
112
+ static normalizePath(path: string): string;
113
+ /**
114
+ * Configures the router with the given options, such as method override settings.
115
+ *
116
+ * @param this
117
+ * @param options
118
+ * @returns
119
+ */
120
+ static configure(this: any, options?: RouterConfig): void;
121
+ protected static resolveMethodOverride(this: any, method: string, headers: Headers | Record<string, any>, body: unknown): HttpMethod | null;
122
+ /**
123
+ * Adds a new route to the router with the specified methods, path, handler, and middlewares.
124
+ *
125
+ * @param this
126
+ * @param methods
127
+ * @param path
128
+ * @param handler
129
+ * @param middlewares
130
+ */
131
+ static add(this: any, methods: HttpMethod | HttpMethod[], path: string, handler: any, middlewares?: any[] | any): void;
132
+ /**
133
+ * Adds a new API resource route to the router for the specified base path and controller, with
134
+ * options to include/exclude specific actions and apply middlewares.
135
+ *
136
+ * @param this
137
+ * @param basePath
138
+ * @param controller
139
+ * @param options
140
+ */
141
+ static apiResource(this: any, basePath: string, controller: any, options?: {
142
+ only?: ControllerAction[];
143
+ except?: ControllerAction[];
144
+ middlewares?: ApiResourceMiddleware<any>;
145
+ }): void;
146
+ /**
147
+ * Adds a new GET route to the router with the specified path, handler, and optional middlewares.
148
+ *
149
+ * @param this The router instance.
150
+ * @param path The path for the GET route.
151
+ * @param handler The handler function for the GET route.
152
+ * @param middlewares Optional middlewares to apply to the GET route.
153
+ */
154
+ static get(this: any, path: string, handler: any, middlewares?: any[] | any): void;
155
+ /**
156
+ * Adds a new POST route to the router with the specified path, handler, and optional middlewares.
157
+ *
158
+ * @param this
159
+ * @param path
160
+ * @param handler
161
+ * @param middlewares
162
+ */
163
+ static post(this: any, path: string, handler: any, middlewares?: any[] | any): void;
164
+ /**
165
+ * Adds a new PUT route to the router with the specified path, handler, and optional middlewares.
166
+ *
167
+ * @param this
168
+ * @param path
169
+ * @param handler
170
+ * @param middlewares
171
+ */
172
+ static put(this: any, path: string, handler: any, middlewares?: any[] | any): void;
173
+ /**
174
+ * Adds a new DELETE route to the router with the specified path, handler, and optional middlewares.
175
+ *
176
+ * @param this
177
+ * @param path
178
+ * @param handler
179
+ * @param middlewares
180
+ */
181
+ static delete(this: any, path: string, handler: any, middlewares?: any[] | any): void;
182
+ /**
183
+ * Adds a new PATCH route to the router with the specified path, handler, and optional middlewares.
184
+ *
185
+ * @param this
186
+ * @param path
187
+ * @param handler
188
+ * @param middlewares
189
+ */
190
+ static patch(this: any, path: string, handler: any, middlewares?: any[] | any): void;
191
+ /**
192
+ * Adds a new OPTIONS route to the router with the specified path, handler, and optional middlewares.
193
+ *
194
+ * @param this
195
+ * @param path
196
+ * @param handler
197
+ * @param middlewares
198
+ */
199
+ static options(this: any, path: string, handler: any, middlewares?: any[] | any): void;
200
+ /**
201
+ * Adds a new HEAD route to the router with the specified path, handler, and optional middlewares.
202
+ *
203
+ * @param this
204
+ * @param path
205
+ * @param handler
206
+ * @param middlewares
207
+ */
208
+ static head(this: any, path: string, handler: any, middlewares?: any[] | any): void;
209
+ /**
210
+ * Defines a group of routes with a common prefix and optional middlewares, allowing for better
211
+ * organization and reuse of route configurations.
212
+ *
213
+ * @param this
214
+ * @param prefix
215
+ * @param callback
216
+ * @param middlewares
217
+ */
218
+ static group(this: any, prefix: string, callback: () => void | Promise<void>, middlewares?: any[]): Promise<void>;
219
+ /**
220
+ * Adds global middlewares to the router, which will be applied to all routes.
221
+ *
222
+ * @param this
223
+ * @param middlewares
224
+ * @param callback
225
+ */
226
+ static middleware(this: any, middlewares: any[], callback: () => void): void;
227
+ /**
228
+ * Retrieves all registered routes in the router, optionally organized by path or method
229
+ * for easier access and management.
230
+ *
231
+ * @param this
232
+ */
233
+ static allRoutes(this: any): Array<Route<any, any, any>>;
234
+ /**
235
+ * @param this
236
+ * @param type - 'path' to get routes organized by path
237
+ */
238
+ static allRoutes(this: any, type: 'path'): Record<string, Route<any, any, any>>;
239
+ /**
240
+ * @param this
241
+ * @param type - 'method' to get routes organized by method
242
+ */
243
+ static allRoutes(this: any, type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
244
+ protected static resolveHandler(route: Route<any, any, any>): {
245
+ handlerFunction: ((ctx: any, req: ClearRequest) => any | Promise<any>) | null;
246
+ instance: Controller<any> | null;
247
+ };
248
+ protected static bindRequestToInstance(ctx: any, instance: Controller<any> | Route<any, any, any> | null, route: Route<any, any, any>, payload: {
249
+ body: Record<string, any>;
250
+ query: Record<string, any>;
251
+ params: Record<string, any>;
252
+ }): void;
253
+ }
254
+ //#endregion
255
+ export { ClearRequest, Controller, CoreRouter, Route };
package/dist/index.d.mts CHANGED
@@ -1,57 +1,41 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
1
2
  import { NextFunction, Request, Response as Response$1 } from "express";
2
- import { H3Event, Middleware as Middleware$1 } from "h3";
3
+ import { Middleware } from "h3";
3
4
 
4
- //#region types/h3.d.ts
5
- type HttpRequest = H3Event['req'] & {
6
- getBody: () => Record<string, any>;
7
- };
8
- /**
9
- * HTTP context passed to route handlers
10
- */
11
- type HttpContext = Omit<H3Event, 'req'> & {
12
- req: HttpRequest;
13
- };
14
- /**
15
- * Route handler function type
16
- */
17
- type RouteHandler = (
18
- /**
19
- * H3 event context
20
- */
21
-
22
- ctx: HttpContext,
23
- /**
24
- * ClearRequest instance
25
- */
26
-
27
- req: ClearRequest) => any | Promise<any>;
28
- /**
29
- * Handler can be either a function or controller reference
30
- */
31
- type Handler = RouteHandler | ControllerHandler;
32
- //#endregion
33
5
  //#region types/basic.d.ts
34
- /**
35
- * Controller method reference
36
- */
37
- type ControllerHandler = [any, string];
38
6
  /**
39
7
  * HTTP methods supported by the router
40
8
  */
41
9
  type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
10
+ /**
11
+ * Common controller action names
12
+ */
13
+ type ControllerAction = 'index' | 'show' | 'create' | 'update' | 'destroy';
42
14
  /**
43
15
  * Generic Object type for request data
44
16
  */
45
17
  type RequestData = Record<string, any>;
18
+ type ApiResourceMiddleware<M = any> = M | M[] | { [K in ControllerAction]?: M | M[] };
19
+ interface RouterConfig {
20
+ /**
21
+ * Configuration for method override functionality, allowing clients to use a
22
+ * specific header or body parameter to override the HTTP method.
23
+ */
24
+ methodOverride?: {
25
+ /** Whether method override is enabled */enabled?: boolean; /** Keys in the request body to check for method override */
26
+ bodyKeys?: string[] | string; /** Keys in the request headers to check for method override */
27
+ headerKeys?: string[] | string;
28
+ };
29
+ }
46
30
  //#endregion
47
31
  //#region types/express.d.ts
48
32
  /**
49
33
  * Middleware function type
50
34
  */
51
- type Middleware = (req: Request, res: Response$1, next: NextFunction) => any | Promise<any>;
35
+ type Middleware$1 = (req: Request, res: Response$1, next: NextFunction) => any | Promise<any>;
52
36
  //#endregion
53
37
  //#region src/Route.d.ts
54
- declare class Route<X = any, M = Middleware$1 | Middleware> {
38
+ declare class Route<X = any, M = Middleware | Middleware$1, H = any> {
55
39
  ctx: X;
56
40
  body: RequestData;
57
41
  query: RequestData;
@@ -59,17 +43,17 @@ declare class Route<X = any, M = Middleware$1 | Middleware> {
59
43
  clearRequest: ClearRequest;
60
44
  methods: HttpMethod[];
61
45
  path: string;
62
- handler: Handler;
46
+ handler: H;
63
47
  middlewares: M[];
64
48
  controllerName?: string;
65
49
  actionName?: string;
66
50
  handlerType: 'function' | 'controller';
67
51
  middlewareCount: number;
68
- constructor(methods: HttpMethod[], path: string, handler: Handler, middlewares?: M[]);
52
+ constructor(methods: HttpMethod[], path: string, handler: H, middlewares?: M[]);
69
53
  }
70
54
  //#endregion
71
55
  //#region src/ClearRequest.d.ts
72
- declare class ClearRequest<X = any, M = Middleware$1 | Middleware> {
56
+ declare class ClearRequest<X = any, M = Middleware | Middleware$1> {
73
57
  [key: string]: any;
74
58
  /**
75
59
  * @param body - Parsed request body
@@ -97,4 +81,175 @@ declare abstract class Controller<X = any> {
97
81
  clearRequest: ClearRequest;
98
82
  }
99
83
  //#endregion
100
- export { ClearRequest, Controller, Route };
84
+ //#region src/core/router.d.ts
85
+ /**
86
+ * @class clear-router CoreRouter
87
+ * @description Core routing logic for clear-router, shared between all supported adapters (Express.js, H3, etc.)
88
+ * @author 3m1n3nc3
89
+ * @repository https://github.com/toneflix/clear-router
90
+ */
91
+ declare abstract class CoreRouter {
92
+ protected static createDefaultOptionsHandler(): any;
93
+ static config: RouterConfig;
94
+ protected static groupContext: AsyncLocalStorage<{
95
+ prefix: string;
96
+ groupMiddlewares: any[];
97
+ }>;
98
+ static routes: Array<Route<any, any, any>>;
99
+ static routesByPathMethod: Record<string, Route<any, any, any>>;
100
+ static routesByMethod: { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
101
+ static prefix: string;
102
+ static groupMiddlewares: any[];
103
+ static globalMiddlewares: any[];
104
+ protected static ensureState(this: any): void;
105
+ /**
106
+ * Normalizes a path by ensuring it starts with a single slash and does not have trailing
107
+ * slashes, while preserving dynamic segments and parameters.
108
+ *
109
+ * @param path The path to normalize.
110
+ * @returns The normalized path.
111
+ */
112
+ static normalizePath(path: string): string;
113
+ /**
114
+ * Configures the router with the given options, such as method override settings.
115
+ *
116
+ * @param this
117
+ * @param options
118
+ * @returns
119
+ */
120
+ static configure(this: any, options?: RouterConfig): void;
121
+ protected static resolveMethodOverride(this: any, method: string, headers: Headers | Record<string, any>, body: unknown): HttpMethod | null;
122
+ /**
123
+ * Adds a new route to the router with the specified methods, path, handler, and middlewares.
124
+ *
125
+ * @param this
126
+ * @param methods
127
+ * @param path
128
+ * @param handler
129
+ * @param middlewares
130
+ */
131
+ static add(this: any, methods: HttpMethod | HttpMethod[], path: string, handler: any, middlewares?: any[] | any): void;
132
+ /**
133
+ * Adds a new API resource route to the router for the specified base path and controller, with
134
+ * options to include/exclude specific actions and apply middlewares.
135
+ *
136
+ * @param this
137
+ * @param basePath
138
+ * @param controller
139
+ * @param options
140
+ */
141
+ static apiResource(this: any, basePath: string, controller: any, options?: {
142
+ only?: ControllerAction[];
143
+ except?: ControllerAction[];
144
+ middlewares?: ApiResourceMiddleware<any>;
145
+ }): void;
146
+ /**
147
+ * Adds a new GET route to the router with the specified path, handler, and optional middlewares.
148
+ *
149
+ * @param this The router instance.
150
+ * @param path The path for the GET route.
151
+ * @param handler The handler function for the GET route.
152
+ * @param middlewares Optional middlewares to apply to the GET route.
153
+ */
154
+ static get(this: any, path: string, handler: any, middlewares?: any[] | any): void;
155
+ /**
156
+ * Adds a new POST route to the router with the specified path, handler, and optional middlewares.
157
+ *
158
+ * @param this
159
+ * @param path
160
+ * @param handler
161
+ * @param middlewares
162
+ */
163
+ static post(this: any, path: string, handler: any, middlewares?: any[] | any): void;
164
+ /**
165
+ * Adds a new PUT route to the router with the specified path, handler, and optional middlewares.
166
+ *
167
+ * @param this
168
+ * @param path
169
+ * @param handler
170
+ * @param middlewares
171
+ */
172
+ static put(this: any, path: string, handler: any, middlewares?: any[] | any): void;
173
+ /**
174
+ * Adds a new DELETE route to the router with the specified path, handler, and optional middlewares.
175
+ *
176
+ * @param this
177
+ * @param path
178
+ * @param handler
179
+ * @param middlewares
180
+ */
181
+ static delete(this: any, path: string, handler: any, middlewares?: any[] | any): void;
182
+ /**
183
+ * Adds a new PATCH route to the router with the specified path, handler, and optional middlewares.
184
+ *
185
+ * @param this
186
+ * @param path
187
+ * @param handler
188
+ * @param middlewares
189
+ */
190
+ static patch(this: any, path: string, handler: any, middlewares?: any[] | any): void;
191
+ /**
192
+ * Adds a new OPTIONS route to the router with the specified path, handler, and optional middlewares.
193
+ *
194
+ * @param this
195
+ * @param path
196
+ * @param handler
197
+ * @param middlewares
198
+ */
199
+ static options(this: any, path: string, handler: any, middlewares?: any[] | any): void;
200
+ /**
201
+ * Adds a new HEAD route to the router with the specified path, handler, and optional middlewares.
202
+ *
203
+ * @param this
204
+ * @param path
205
+ * @param handler
206
+ * @param middlewares
207
+ */
208
+ static head(this: any, path: string, handler: any, middlewares?: any[] | any): void;
209
+ /**
210
+ * Defines a group of routes with a common prefix and optional middlewares, allowing for better
211
+ * organization and reuse of route configurations.
212
+ *
213
+ * @param this
214
+ * @param prefix
215
+ * @param callback
216
+ * @param middlewares
217
+ */
218
+ static group(this: any, prefix: string, callback: () => void | Promise<void>, middlewares?: any[]): Promise<void>;
219
+ /**
220
+ * Adds global middlewares to the router, which will be applied to all routes.
221
+ *
222
+ * @param this
223
+ * @param middlewares
224
+ * @param callback
225
+ */
226
+ static middleware(this: any, middlewares: any[], callback: () => void): void;
227
+ /**
228
+ * Retrieves all registered routes in the router, optionally organized by path or method
229
+ * for easier access and management.
230
+ *
231
+ * @param this
232
+ */
233
+ static allRoutes(this: any): Array<Route<any, any, any>>;
234
+ /**
235
+ * @param this
236
+ * @param type - 'path' to get routes organized by path
237
+ */
238
+ static allRoutes(this: any, type: 'path'): Record<string, Route<any, any, any>>;
239
+ /**
240
+ * @param this
241
+ * @param type - 'method' to get routes organized by method
242
+ */
243
+ static allRoutes(this: any, type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
244
+ protected static resolveHandler(route: Route<any, any, any>): {
245
+ handlerFunction: ((ctx: any, req: ClearRequest) => any | Promise<any>) | null;
246
+ instance: Controller<any> | null;
247
+ };
248
+ protected static bindRequestToInstance(ctx: any, instance: Controller<any> | Route<any, any, any> | null, route: Route<any, any, any>, payload: {
249
+ body: Record<string, any>;
250
+ query: Record<string, any>;
251
+ params: Record<string, any>;
252
+ }): void;
253
+ }
254
+ //#endregion
255
+ export { ClearRequest, Controller, CoreRouter, Route };