tezx 2.0.11 → 3.0.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 (234) hide show
  1. package/README.md +122 -89
  2. package/bun/getConnInfo.d.ts +21 -0
  3. package/bun/getConnInfo.js +9 -0
  4. package/bun/index.d.ts +10 -4
  5. package/bun/index.js +8 -4
  6. package/bun/ws.d.ts +48 -0
  7. package/bun/ws.js +58 -0
  8. package/cjs/bun/getConnInfo.js +12 -0
  9. package/cjs/bun/index.js +35 -7
  10. package/cjs/bun/ws.js +63 -0
  11. package/cjs/core/config.js +2 -12
  12. package/cjs/core/context.js +131 -379
  13. package/cjs/core/error.js +49 -0
  14. package/cjs/core/request.js +79 -131
  15. package/cjs/core/router.js +54 -387
  16. package/cjs/core/server.js +83 -202
  17. package/cjs/deno/env.js +4 -4
  18. package/cjs/deno/getConnInfo.js +18 -0
  19. package/cjs/deno/index.js +11 -18
  20. package/cjs/deno/serveStatic.js +53 -0
  21. package/cjs/deno/ws.js +39 -0
  22. package/cjs/helper/index.js +46 -10
  23. package/cjs/index.js +5 -7
  24. package/cjs/jwt/node.js +94 -0
  25. package/cjs/jwt/web.js +178 -0
  26. package/cjs/middleware/basic-auth.js +42 -0
  27. package/cjs/middleware/bearer-auth.js +34 -0
  28. package/cjs/middleware/cache-control.js +44 -0
  29. package/cjs/middleware/cors.js +11 -21
  30. package/cjs/middleware/detect-bot.js +57 -0
  31. package/cjs/middleware/i18n.js +73 -60
  32. package/cjs/middleware/index.js +8 -46
  33. package/cjs/middleware/logger.js +9 -4
  34. package/cjs/middleware/pagination.js +3 -2
  35. package/cjs/middleware/powered-by.js +3 -2
  36. package/cjs/middleware/rate-limiter.js +38 -0
  37. package/cjs/middleware/request-id.js +4 -5
  38. package/cjs/middleware/sanitize-headers.js +22 -0
  39. package/cjs/middleware/secure-headers copy.js +143 -0
  40. package/cjs/middleware/secure-headers.js +157 -0
  41. package/cjs/middleware/{xssProtection.js → xss-protection.js} +5 -8
  42. package/cjs/node/env.js +7 -7
  43. package/cjs/node/getConnInfo.js +16 -0
  44. package/cjs/node/index.js +17 -18
  45. package/cjs/node/mount-node.js +59 -0
  46. package/cjs/node/serveStatic.js +56 -0
  47. package/cjs/node/toWebRequest.js +25 -0
  48. package/cjs/node/ws.js +82 -0
  49. package/cjs/registry/RadixRouter.js +148 -0
  50. package/cjs/registry/index.js +17 -0
  51. package/cjs/types/headers.js +2 -0
  52. package/cjs/types/index.js +13 -0
  53. package/cjs/utils/buffer.js +17 -0
  54. package/cjs/utils/colors.js +2 -0
  55. package/cjs/utils/cookie.js +59 -0
  56. package/cjs/utils/file.js +136 -0
  57. package/cjs/utils/formData.js +60 -10
  58. package/cjs/utils/generateID.js +37 -0
  59. package/cjs/utils/low-level.js +115 -0
  60. package/cjs/utils/{staticFile.js → mimeTypes.js} +0 -87
  61. package/cjs/utils/rateLimit.js +41 -0
  62. package/cjs/utils/response.js +65 -0
  63. package/cjs/{core/environment.js → utils/runtime.js} +2 -1
  64. package/cjs/utils/url.js +65 -30
  65. package/core/config.d.ts +2 -7
  66. package/core/config.js +2 -12
  67. package/core/context.d.ts +209 -164
  68. package/core/context.js +131 -346
  69. package/core/error.d.ts +96 -0
  70. package/core/error.js +44 -0
  71. package/core/request.d.ts +67 -107
  72. package/core/request.js +78 -130
  73. package/core/router.d.ts +138 -133
  74. package/core/router.js +53 -352
  75. package/core/server.d.ts +99 -38
  76. package/core/server.js +83 -202
  77. package/deno/env.js +3 -3
  78. package/deno/getConnInfo.d.ts +21 -0
  79. package/deno/getConnInfo.js +15 -0
  80. package/deno/index.d.ts +9 -4
  81. package/deno/index.js +7 -4
  82. package/deno/serveStatic.d.ts +28 -0
  83. package/deno/serveStatic.js +49 -0
  84. package/deno/ws.d.ts +42 -0
  85. package/deno/ws.js +36 -0
  86. package/helper/index.d.ts +29 -15
  87. package/helper/index.js +27 -7
  88. package/index.d.ts +10 -8
  89. package/index.js +4 -5
  90. package/jwt/node.d.ts +39 -0
  91. package/jwt/node.js +87 -0
  92. package/jwt/web.d.ts +14 -0
  93. package/jwt/web.js +174 -0
  94. package/middleware/basic-auth.d.ts +56 -0
  95. package/middleware/basic-auth.js +38 -0
  96. package/middleware/bearer-auth.d.ts +53 -0
  97. package/middleware/bearer-auth.js +30 -0
  98. package/middleware/cache-control.d.ts +30 -0
  99. package/middleware/cache-control.js +40 -0
  100. package/middleware/cors.d.ts +30 -3
  101. package/middleware/cors.js +12 -22
  102. package/middleware/detect-bot.d.ts +113 -0
  103. package/middleware/detect-bot.js +53 -0
  104. package/middleware/i18n.d.ts +166 -73
  105. package/middleware/i18n.js +73 -60
  106. package/middleware/index.d.ts +8 -32
  107. package/middleware/index.js +8 -44
  108. package/middleware/logger.d.ts +5 -2
  109. package/middleware/logger.js +9 -4
  110. package/middleware/pagination.d.ts +9 -6
  111. package/middleware/pagination.js +3 -2
  112. package/middleware/powered-by.d.ts +2 -1
  113. package/middleware/powered-by.js +3 -2
  114. package/middleware/{rateLimiter.d.ts → rate-limiter.d.ts} +15 -9
  115. package/middleware/rate-limiter.js +34 -0
  116. package/middleware/request-id.d.ts +2 -1
  117. package/middleware/request-id.js +5 -6
  118. package/middleware/{sanitizeHeader.d.ts → sanitize-headers.d.ts} +5 -19
  119. package/middleware/sanitize-headers.js +18 -0
  120. package/middleware/secure-headers copy.d.ts +15 -0
  121. package/middleware/secure-headers copy.js +136 -0
  122. package/middleware/secure-headers.d.ts +132 -0
  123. package/middleware/secure-headers.js +153 -0
  124. package/middleware/{xssProtection.d.ts → xss-protection.d.ts} +2 -1
  125. package/middleware/xss-protection.js +19 -0
  126. package/node/env.js +4 -4
  127. package/node/getConnInfo.d.ts +21 -0
  128. package/node/getConnInfo.js +13 -0
  129. package/node/index.d.ts +13 -4
  130. package/node/index.js +11 -4
  131. package/node/mount-node.d.ts +11 -0
  132. package/node/mount-node.js +56 -0
  133. package/node/serveStatic.d.ts +36 -0
  134. package/node/serveStatic.js +52 -0
  135. package/node/toWebRequest.js +22 -0
  136. package/node/ws.d.ts +56 -0
  137. package/node/ws.js +46 -0
  138. package/package.json +39 -30
  139. package/registry/RadixRouter.d.ts +40 -0
  140. package/registry/RadixRouter.js +144 -0
  141. package/registry/index.d.ts +2 -0
  142. package/registry/index.js +1 -0
  143. package/types/headers.d.ts +2 -0
  144. package/types/headers.js +1 -0
  145. package/types/index.d.ts +318 -18
  146. package/types/index.js +12 -1
  147. package/utils/buffer.d.ts +1 -0
  148. package/utils/buffer.js +14 -0
  149. package/utils/colors.d.ts +24 -0
  150. package/utils/colors.js +2 -0
  151. package/utils/cookie.d.ts +55 -0
  152. package/utils/cookie.js +53 -0
  153. package/utils/file.d.ts +38 -0
  154. package/utils/file.js +96 -0
  155. package/utils/formData.d.ts +41 -1
  156. package/utils/formData.js +58 -9
  157. package/utils/generateID.d.ts +42 -0
  158. package/utils/generateID.js +32 -0
  159. package/utils/httpStatusMap.d.ts +14 -0
  160. package/utils/low-level.d.ts +58 -0
  161. package/utils/low-level.js +108 -0
  162. package/utils/mimeTypes.d.ts +4 -0
  163. package/utils/{staticFile.js → mimeTypes.js} +0 -53
  164. package/utils/rateLimit.d.ts +18 -0
  165. package/utils/rateLimit.js +37 -0
  166. package/utils/response.d.ts +18 -0
  167. package/utils/response.js +58 -0
  168. package/{core/environment.d.ts → utils/runtime.d.ts} +1 -0
  169. package/{core/environment.js → utils/runtime.js} +1 -0
  170. package/utils/url.d.ts +42 -14
  171. package/utils/url.js +61 -27
  172. package/bun/adapter.d.ts +0 -127
  173. package/bun/adapter.js +0 -97
  174. package/cjs/bun/adapter.js +0 -100
  175. package/cjs/core/MiddlewareConfigure.js +0 -68
  176. package/cjs/core/common.js +0 -15
  177. package/cjs/deno/adpater.js +0 -67
  178. package/cjs/helper/common.js +0 -17
  179. package/cjs/middleware/basicAuth.js +0 -71
  180. package/cjs/middleware/cacheControl.js +0 -90
  181. package/cjs/middleware/detectBot.js +0 -104
  182. package/cjs/middleware/detectLocale.js +0 -43
  183. package/cjs/middleware/lazyLoadModules.js +0 -73
  184. package/cjs/middleware/rateLimiter.js +0 -24
  185. package/cjs/middleware/requestTimeout.js +0 -42
  186. package/cjs/middleware/sanitizeHeader.js +0 -51
  187. package/cjs/middleware/secureHeaders.js +0 -42
  188. package/cjs/node/adapter.js +0 -138
  189. package/cjs/utils/regexRouter.js +0 -58
  190. package/cjs/utils/state.js +0 -34
  191. package/cjs/utils/toWebRequest.js +0 -35
  192. package/cjs/ws/deno.js +0 -20
  193. package/cjs/ws/index.js +0 -53
  194. package/cjs/ws/node.js +0 -65
  195. package/core/MiddlewareConfigure.d.ts +0 -15
  196. package/core/MiddlewareConfigure.js +0 -63
  197. package/core/common.d.ts +0 -21
  198. package/core/common.js +0 -11
  199. package/deno/adpater.d.ts +0 -38
  200. package/deno/adpater.js +0 -64
  201. package/helper/common.d.ts +0 -5
  202. package/helper/common.js +0 -14
  203. package/middleware/basicAuth.d.ts +0 -81
  204. package/middleware/basicAuth.js +0 -67
  205. package/middleware/cacheControl.d.ts +0 -48
  206. package/middleware/cacheControl.js +0 -53
  207. package/middleware/detectBot.d.ts +0 -121
  208. package/middleware/detectBot.js +0 -98
  209. package/middleware/detectLocale.d.ts +0 -55
  210. package/middleware/detectLocale.js +0 -39
  211. package/middleware/lazyLoadModules.d.ts +0 -72
  212. package/middleware/lazyLoadModules.js +0 -69
  213. package/middleware/rateLimiter.js +0 -20
  214. package/middleware/requestTimeout.d.ts +0 -25
  215. package/middleware/requestTimeout.js +0 -38
  216. package/middleware/sanitizeHeader.js +0 -47
  217. package/middleware/secureHeaders.d.ts +0 -78
  218. package/middleware/secureHeaders.js +0 -38
  219. package/middleware/xssProtection.js +0 -22
  220. package/node/adapter.d.ts +0 -46
  221. package/node/adapter.js +0 -102
  222. package/utils/regexRouter.d.ts +0 -66
  223. package/utils/regexRouter.js +0 -53
  224. package/utils/state.d.ts +0 -50
  225. package/utils/state.js +0 -30
  226. package/utils/staticFile.d.ts +0 -10
  227. package/utils/toWebRequest.js +0 -32
  228. package/ws/deno.d.ts +0 -6
  229. package/ws/deno.js +0 -16
  230. package/ws/index.d.ts +0 -180
  231. package/ws/index.js +0 -50
  232. package/ws/node.d.ts +0 -7
  233. package/ws/node.js +0 -28
  234. /package/{utils → node}/toWebRequest.d.ts +0 -0
package/core/router.d.ts CHANGED
@@ -1,58 +1,77 @@
1
- import { Callback, DuplicateMiddlewares, HTTPMethod, Middleware, PathType, StaticServeOption, UniqueMiddlewares } from "../types/index.js";
2
- import { Context } from "./context.js";
3
- import MiddlewareConfigure from "./MiddlewareConfigure.js";
1
+ import { Callback, HandlerType, HTTPMethod, Middleware, RouteRegistry, ServeStatic } from "../types/index.js";
2
+ /**
3
+ * Router configuration options.
4
+ */
4
5
  export type RouterConfig = {
5
6
  /**
6
- * `env` allows you to define environment variables for the router.
7
- * It is a record of key-value pairs where the key is the variable name
8
- * and the value can be either a string or a number.
7
+ * `env` allows you to define environment variables for the router instance.
8
+ * Example: `{ NODE_ENV: "production", API_VERSION: 2 }`
9
9
  */
10
10
  env?: Record<string, string | number>;
11
11
  /**
12
- * `basePath` sets the base path for the router. This is useful for grouping
13
- * routes under a specific path prefix.
12
+ * `basePath` sets a base path prefix for the router. Useful to group routes
13
+ * under a common prefix (for example when mounting the router on a sub-path).
14
+ *
15
+ * - Example: `basePath: "/api/v1"` will prefix all registered routes with `/api/v1`.
16
+ * - Should not end with a trailing slash — the router will normalize it.
14
17
  */
15
18
  basePath?: string;
16
19
  };
17
- export declare class TrieRouter {
18
- children: Map<string, TrieRouter>;
19
- handlers: Map<HTTPMethod, {
20
- callback: Callback<any>;
21
- middlewares: UniqueMiddlewares | DuplicateMiddlewares;
22
- }>;
23
- pathname: string;
24
- paramName: any;
25
- isParam: boolean;
26
- constructor(pathname?: string);
27
- }
28
- export type RouterHandler<T extends Record<string, any>> = {
29
- callback: Callback<T>;
30
- paramNames: string[];
31
- regex: RegExp;
32
- middlewares: UniqueMiddlewares | DuplicateMiddlewares;
33
- };
34
- export declare class Router<T extends Record<string, any> = {}> extends MiddlewareConfigure<T> {
20
+ /**
21
+ * Router class responsible for managing HTTP routes, middlewares,
22
+ * and serving static files.
23
+ *
24
+ * @template T - Context state type, extended by route handlers.
25
+ */
26
+ export declare class Router<T extends Record<string, any> = {}> {
35
27
  #private;
36
- protected routers: Map<string, Map<HTTPMethod, RouterHandler<T>>>;
28
+ /** Environment variables accessible within this router */
37
29
  protected env: Record<string, string | number>;
38
- protected triRouter: TrieRouter;
30
+ /** Internal route registry to hold all routes */
31
+ protected router?: RouteRegistry;
32
+ /** Array tracking registered routes and their handlers */
33
+ protected route: {
34
+ method: string;
35
+ pattern: string;
36
+ handlers: HandlerType;
37
+ }[];
38
+ /** Static file routes mapping */
39
+ protected staticFile: Record<string, Callback<any>>;
40
+ /** Base path prefix for all routes */
41
+ protected basePath: string;
42
+ /**
43
+ * Creates a new Router instance.
44
+ *
45
+ * @param config - Router configuration options
46
+ * @param config.basePath - Base path prefix for the router
47
+ * @param config.env - Environment variables for router
48
+ * @param config.routeRegistry - Custom route registry instance
49
+ */
39
50
  constructor({ basePath, env }?: RouterConfig);
40
51
  /**
41
- * Serves static files from a specified directory.
52
+ * Registers static file routes to the application for serving files like HTML, CSS, JS, images, etc.
42
53
  *
43
- * This method provides two overloads:
44
- * 1. `static(route: string, folder: string, option?: StaticServeOption): this;`
45
- * - Serves static files from `folder` at the specified `route`.
46
- * 2. `static(folder: string, option?: StaticServeOption): this;`
47
- * - Serves static files from `folder` at the root (`/`).
54
+ * This method maps routes to files defined in the `ServeStatic` object,
55
+ * and sets appropriate HTTP headers like `Cache-Control` and custom headers.
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * import { serveStatic } from "tezx/bun"; // or "tezx/node"
60
+ *
61
+ * app.static(
62
+ * serveStatic("public", {
63
+ * cacheControl: "max-age=86400",
64
+ * headers: {
65
+ * "X-Powered-By": "TezX"
66
+ * }
67
+ * })
68
+ * );
69
+ * ```
48
70
  *
49
- * @param {string} route - The base route to serve static files from (optional in overload).
50
- * @param {string} folder - The folder containing the static files.
51
- * @param {StaticServeOption} [option] - Optional settings for static file serving.
52
- * @returns {this} Returns the current instance to allow method chaining.
71
+ * @param serveStatic - An object containing static file definitions and optional configuration.
72
+ * @returns The current instance for chaining.
53
73
  */
54
- static(route: string, folder: string, option?: StaticServeOption): this;
55
- static(folder: string, Option?: StaticServeOption): this;
74
+ static(serveStatic: ServeStatic): this;
56
75
  /**
57
76
  * Registers a GET route with optional middleware(s)
58
77
  * @param path - URL path pattern (supports route parameters)
@@ -62,158 +81,144 @@ export declare class Router<T extends Record<string, any> = {}> extends Middlewa
62
81
  * @example
63
82
  * // Simple GET route
64
83
  * app.get('/users', (ctx) => { ... });
65
- *
66
- * // With middleware
67
- * app.get('/secure', authMiddleware, (ctx) => { ... });
68
- * app.get(/^\/item\/(\d+)\/(edit|view)?\/?$/, (ctx) => { ... });
69
- *
70
84
  * // With multiple middlewares
71
85
  * app.get('/admin', [authMiddleware, adminMiddleware], (ctx) => { ... });
72
86
  */
73
- get<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, callback: Callback<T & U, Path>): this;
74
- get<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
75
- get<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
76
- /**
77
- * Registers a Server-Sent Events (SSE) route handler for the given path.
78
- *
79
- * This method sets up an HTTP GET route that sends real-time updates to the client
80
- * over a persistent HTTP connection using the SSE protocol.
81
- *
82
- * ### Example:
83
- * ```ts
84
- * app.sse("/events", async (ctx) => {
85
- * const stream = new ReadableStream({
86
- * start(controller) {
87
- * controller.enqueue(new TextEncoder().encode("data: Hello\n\n"));
88
- * ctx.rawRequest?.signal?.addEventListener("abort", () => {
89
- * clearInterval(interval);
90
- * controller.close()
91
- * });
92
- * },
93
- * });
94
- *
95
- * return ctx.send(stream, {
96
- * headers: {
97
- * "Content-Type": "text/event-stream",
98
- * "Cache-Control": "no-cache",
99
- * "Connection": "keep-alive",
100
- * },
101
- * });
102
- * });
103
- * ```
104
- *
105
- * @param {PathType} path - The route path for SSE (e.g. `/events`).
106
- * @param {(ctx: Context) => any} handler - A handler function that returns a streamed response.
107
- */
108
- sse<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, handler: (ctx: Context<T & U, Path>) => any): void;
87
+ get<U extends Record<string, any> = {}, Path extends string = any>(path: Path, callback: Callback<T & U, Path>): this;
88
+ get<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
89
+ get<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
109
90
  /**
110
91
  * Registers a POST route with optional middleware(s)
111
92
  * @param path - URL path pattern
112
93
  * @param args - Handler callback or middleware(s) + handler
113
94
  */
114
- post<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, callback: Callback<T & U, Path>): this;
115
- post<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
116
- post<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
95
+ post<U extends Record<string, any> = {}, Path extends string = any>(path: Path, callback: Callback<T & U, Path>): this;
96
+ post<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
97
+ post<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
117
98
  /**
118
99
  * Registers a PUT route with optional middleware(s)
119
100
  * @param path - URL path pattern
120
101
  * @param args - Handler callback or middleware(s) + handler
121
102
  */
122
- put<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, callback: Callback<T & U, Path>): this;
123
- put<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
124
- put<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
103
+ put<U extends Record<string, any> = {}, Path extends string = any>(path: Path, callback: Callback<T & U, Path>): this;
104
+ put<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
105
+ put<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
125
106
  /**
126
107
  * Registers a PATCH route with optional middleware(s)
127
108
  * @param path - URL path pattern
128
109
  * @param args - Handler callback or middleware(s) + handler
129
110
  */
130
- patch<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, callback: Callback<T & U, Path>): this;
131
- patch<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
132
- patch<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
111
+ patch<U extends Record<string, any> = {}, Path extends string = any>(path: Path, callback: Callback<T & U, Path>): this;
112
+ patch<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
113
+ patch<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
133
114
  /**
134
115
  * Registers a DELETE route with optional middleware(s)
135
116
  * @param path - URL path pattern
136
117
  * @param args - Handler callback or middleware(s) + handler
137
118
  */
138
- delete<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, callback: Callback<T & U, Path>): this;
139
- delete<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
140
- delete<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
119
+ delete<U extends Record<string, any> = {}, Path extends string = any>(path: Path, callback: Callback<T & U, Path>): this;
120
+ delete<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
121
+ delete<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
141
122
  /**
142
123
  * Registers an OPTIONS route (primarily for CORS preflight requests)
143
124
  * @param path - URL path pattern
144
125
  * @param args - Handler callback or middleware(s) + handler
145
126
  */
146
- options<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, callback: Callback<T & U, Path>): this;
147
- options<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
148
- options<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
127
+ options<U extends Record<string, any> = {}, Path extends string = any>(path: Path, callback: Callback<T & U, Path>): this;
128
+ options<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
129
+ options<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
149
130
  /**
150
131
  * Registers a HEAD route (returns headers only)
151
132
  * @param path - URL path pattern
152
133
  * @param args - Handler callback or middleware(s) + handler
153
134
  */
154
- head<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, callback: Callback<T & U, Path>): this;
155
- head<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
156
- head<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
157
135
  /**
158
- * Registers a route that responds to all HTTP methods
136
+ * Register a route that responds to all HTTP methods.
137
+ *
159
138
  * @param path - URL path pattern
160
139
  * @param args - Handler callback or middleware(s) + handler
140
+ * @returns The Router instance for chaining
161
141
  */
162
- all<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, callback: Callback<T & U, Path>): this;
163
- all<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
164
- all<U extends Record<string, any> = {}, Path extends PathType = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
142
+ all<U extends Record<string, any> = {}, Path extends string = any>(path: Path, callback: Callback<T & U, Path>): this;
143
+ all<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
144
+ all<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
165
145
  /**
166
- * Generic method registration for custom HTTP methods
167
- * @param method - HTTP method name (e.g., 'PURGE')
168
- * @param path - URL path pattern
169
- * @param args - Handler callback or middleware(s) + handler
146
+ * Registers one or more HTTP method handlers for a given path.
170
147
  *
171
- * @example
172
- * // Register custom method
173
- * server.addRoute('PURGE', '/cache', purgeHandler);
148
+ * Supports three overloads:
149
+ *
150
+ * 1. A single callback:
151
+ * app.when("GET", "/path", callback)
152
+ *
153
+ * 2. A middleware and a callback:
154
+ * app.when("GET", "/path", middleware, callback)
155
+ *
156
+ * 3. An array of middlewares and a callback:
157
+ * app.when("GET", "/path", [middleware1, middleware2], callback)
158
+ *
159
+ * @template U - Additional context to be merged into the main context
160
+ * @template Path - The string literal type of the route path
161
+ *
162
+ * @param {HTTPMethod | HTTPMethod[]} methods - One or more HTTP methods like 'GET', 'POST', etc.
163
+ * @param {Path} path - The route path (e.g., '/login')
164
+ * @param {...(Middleware<T & U, Path> | Middleware<T & U, Path>[] | Callback<T & U, Path>)} args - Middleware(s) and final callback
165
+ *
166
+ * @returns {this} Returns the app instance for chaining
174
167
  */
175
- addRoute<U extends Record<string, any> = {}, Path extends PathType = any>(method: HTTPMethod, path: Path, callback: Callback<T & U, Path>): this;
176
- addRoute<U extends Record<string, any> = {}, Path extends PathType = any>(method: HTTPMethod, path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
177
- addRoute<U extends Record<string, any> = {}, Path extends PathType = any>(method: HTTPMethod, path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
168
+ when<U extends Record<string, any> = {}, Path extends string = any>(methods: HTTPMethod | HTTPMethod[], path: Path, callback: Callback<T & U, Path>): this;
169
+ when<U extends Record<string, any> = {}, Path extends string = any>(methods: HTTPMethod | HTTPMethod[], path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path>): this;
170
+ when<U extends Record<string, any> = {}, Path extends string = any>(methods: HTTPMethod | HTTPMethod[], path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path>): this;
178
171
  /**
179
- * Mount a sub-router at specific path prefix
180
- * @param path - Base path for the sub-router
172
+ * Mount another Router instance at a given base path.
173
+ *
174
+ * Useful for modular route organization.
175
+ *
176
+ * @param path - Base path prefix to mount the sub-router
181
177
  * @param router - Router instance to mount
182
- * @returns Current instance for chaining
178
+ * @returns The current Router instance for chaining
183
179
  *
184
180
  * @example
185
181
  * const apiRouter = new Router();
186
- * apiRouter.get('/users', () => { ... });
187
- * server.addRouter('/api', apiRouter);
182
+ * apiRouter.get('/users', userHandler);
183
+ * router.addRouter('/api', apiRouter);
188
184
  */
189
185
  addRouter<U extends Record<string, any> = {}, Path extends string = any>(path: Path, router: Router<T | U | any>): void;
190
186
  /**
191
- * Create route group with shared path prefix
187
+ * Create a route group with shared path prefix.
188
+ *
189
+ * Provides a scoped Router instance to define routes under a common prefix.
190
+ *
192
191
  * @param prefix - Path prefix for the group
193
- * @param callback - Function that receives group-specific router
194
- * @returns Current router instance for chaining
192
+ * @param callback - Function receiving the scoped Router
193
+ * @returns The current Router instance for chaining
195
194
  *
196
195
  * @example
197
- * app.group('/v1', (group) => {
196
+ * router.group('/v1', (group) => {
198
197
  * group.get('/users', v1UserHandler);
199
198
  * });
200
199
  */
201
200
  group<U extends Record<string, any> = {}, Prefix extends string = any>(prefix: Prefix, callback: (group: Router<T & U>) => void): this;
202
201
  /**
203
- * Register middleware with flexible signature
204
- * @overload
205
- * @param path - Optional path to scope middleware
206
- * @param middlewares - Middleware(s) to register
207
- * @param [callback] - Optional sub-router or handler
202
+ * Register middleware(s) optionally scoped by path.
203
+ *
204
+ * Supports multiple overloads for flexibility:
205
+ * - `use(middleware)`
206
+ * - `use([middleware1, middleware2])`
207
+ * - `use(path, middleware)`
208
+ * - `use(path, [middleware1, middleware2])`
209
+ * - `use(path, middleware, callbackOrRouter)`
210
+ *
211
+ * @param args - Path (optional), middleware(s), and optional handler or sub-router
212
+ * @returns The current Router instance for chaining
208
213
  */
209
- use<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path> | Router<T & U | any>): this;
210
- use<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path> | Router<T & U | any>): this;
214
+ use<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path> | Router<(T & U) | any>): this;
215
+ use<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path> | Router<(T & U) | any>): this;
211
216
  use<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middlewares: Middleware<T & U, Path>[]): this;
212
217
  use<U extends Record<string, any> = {}, Path extends string = any>(path: Path, middlewares: Middleware<T & U, Path>): this;
213
- use<U extends Record<string, any> = {}, Path extends string = any>(path: Path, callback: Callback<T & U, Path> | Router<T & U | any>): this;
214
- use<U extends Record<string, any> = {}, Path extends string = any>(middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path> | Router<T & U | any>): this;
215
- use<U extends Record<string, any> = {}, Path extends string = any>(middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path> | Router<T & U | any>): this;
218
+ use<U extends Record<string, any> = {}, Path extends string = any>(path: Path, callback: Callback<T & U, Path> | Router<(T & U) | any>): this;
219
+ use<U extends Record<string, any> = {}, Path extends string = any>(middlewares: Middleware<T & U, Path>[], callback: Callback<T & U, Path> | Router<(T & U) | any>): this;
220
+ use<U extends Record<string, any> = {}, Path extends string = any>(middleware: Middleware<T & U, Path>, callback: Callback<T & U, Path> | Router<(T & U) | any>): this;
216
221
  use<U extends Record<string, any> = {}, Path extends string = any>(middlewares: Middleware<T & U, Path>[]): this;
217
222
  use<U extends Record<string, any> = {}, Path extends string = any>(middleware: Middleware<T & U, Path>): this;
218
- use<U extends Record<string, any> = {}, Path extends string = any>(callback: Callback<T & U, Path> | Router<T & U | any>): this;
223
+ use<U extends Record<string, any> = {}, Path extends string = any>(callback: Callback<T & U, Path> | Router<(T & U) | any>): this;
219
224
  }