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.
- package/README.md +122 -89
- package/bun/getConnInfo.d.ts +21 -0
- package/bun/getConnInfo.js +9 -0
- package/bun/index.d.ts +10 -4
- package/bun/index.js +8 -4
- package/bun/ws.d.ts +48 -0
- package/bun/ws.js +58 -0
- package/cjs/bun/getConnInfo.js +12 -0
- package/cjs/bun/index.js +35 -7
- package/cjs/bun/ws.js +63 -0
- package/cjs/core/config.js +2 -12
- package/cjs/core/context.js +131 -379
- package/cjs/core/error.js +49 -0
- package/cjs/core/request.js +79 -131
- package/cjs/core/router.js +54 -387
- package/cjs/core/server.js +83 -202
- package/cjs/deno/env.js +4 -4
- package/cjs/deno/getConnInfo.js +18 -0
- package/cjs/deno/index.js +11 -18
- package/cjs/deno/serveStatic.js +53 -0
- package/cjs/deno/ws.js +39 -0
- package/cjs/helper/index.js +46 -10
- package/cjs/index.js +5 -7
- package/cjs/jwt/node.js +94 -0
- package/cjs/jwt/web.js +178 -0
- package/cjs/middleware/basic-auth.js +42 -0
- package/cjs/middleware/bearer-auth.js +34 -0
- package/cjs/middleware/cache-control.js +44 -0
- package/cjs/middleware/cors.js +11 -21
- package/cjs/middleware/detect-bot.js +57 -0
- package/cjs/middleware/i18n.js +73 -60
- package/cjs/middleware/index.js +8 -46
- package/cjs/middleware/logger.js +9 -4
- package/cjs/middleware/pagination.js +3 -2
- package/cjs/middleware/powered-by.js +3 -2
- package/cjs/middleware/rate-limiter.js +38 -0
- package/cjs/middleware/request-id.js +4 -5
- package/cjs/middleware/sanitize-headers.js +22 -0
- package/cjs/middleware/secure-headers copy.js +143 -0
- package/cjs/middleware/secure-headers.js +157 -0
- package/cjs/middleware/{xssProtection.js → xss-protection.js} +5 -8
- package/cjs/node/env.js +7 -7
- package/cjs/node/getConnInfo.js +16 -0
- package/cjs/node/index.js +17 -18
- package/cjs/node/mount-node.js +59 -0
- package/cjs/node/serveStatic.js +56 -0
- package/cjs/node/toWebRequest.js +25 -0
- package/cjs/node/ws.js +82 -0
- package/cjs/registry/RadixRouter.js +148 -0
- package/cjs/registry/index.js +17 -0
- package/cjs/types/headers.js +2 -0
- package/cjs/types/index.js +13 -0
- package/cjs/utils/buffer.js +17 -0
- package/cjs/utils/colors.js +2 -0
- package/cjs/utils/cookie.js +59 -0
- package/cjs/utils/file.js +136 -0
- package/cjs/utils/formData.js +60 -10
- package/cjs/utils/generateID.js +37 -0
- package/cjs/utils/low-level.js +115 -0
- package/cjs/utils/{staticFile.js → mimeTypes.js} +0 -87
- package/cjs/utils/rateLimit.js +41 -0
- package/cjs/utils/response.js +65 -0
- package/cjs/{core/environment.js → utils/runtime.js} +2 -1
- package/cjs/utils/url.js +65 -30
- package/core/config.d.ts +2 -7
- package/core/config.js +2 -12
- package/core/context.d.ts +209 -164
- package/core/context.js +131 -346
- package/core/error.d.ts +96 -0
- package/core/error.js +44 -0
- package/core/request.d.ts +67 -107
- package/core/request.js +78 -130
- package/core/router.d.ts +138 -133
- package/core/router.js +53 -352
- package/core/server.d.ts +99 -38
- package/core/server.js +83 -202
- package/deno/env.js +3 -3
- package/deno/getConnInfo.d.ts +21 -0
- package/deno/getConnInfo.js +15 -0
- package/deno/index.d.ts +9 -4
- package/deno/index.js +7 -4
- package/deno/serveStatic.d.ts +28 -0
- package/deno/serveStatic.js +49 -0
- package/deno/ws.d.ts +42 -0
- package/deno/ws.js +36 -0
- package/helper/index.d.ts +29 -15
- package/helper/index.js +27 -7
- package/index.d.ts +10 -8
- package/index.js +4 -5
- package/jwt/node.d.ts +39 -0
- package/jwt/node.js +87 -0
- package/jwt/web.d.ts +14 -0
- package/jwt/web.js +174 -0
- package/middleware/basic-auth.d.ts +56 -0
- package/middleware/basic-auth.js +38 -0
- package/middleware/bearer-auth.d.ts +53 -0
- package/middleware/bearer-auth.js +30 -0
- package/middleware/cache-control.d.ts +30 -0
- package/middleware/cache-control.js +40 -0
- package/middleware/cors.d.ts +30 -3
- package/middleware/cors.js +12 -22
- package/middleware/detect-bot.d.ts +113 -0
- package/middleware/detect-bot.js +53 -0
- package/middleware/i18n.d.ts +166 -73
- package/middleware/i18n.js +73 -60
- package/middleware/index.d.ts +8 -32
- package/middleware/index.js +8 -44
- package/middleware/logger.d.ts +5 -2
- package/middleware/logger.js +9 -4
- package/middleware/pagination.d.ts +9 -6
- package/middleware/pagination.js +3 -2
- package/middleware/powered-by.d.ts +2 -1
- package/middleware/powered-by.js +3 -2
- package/middleware/{rateLimiter.d.ts → rate-limiter.d.ts} +15 -9
- package/middleware/rate-limiter.js +34 -0
- package/middleware/request-id.d.ts +2 -1
- package/middleware/request-id.js +5 -6
- package/middleware/{sanitizeHeader.d.ts → sanitize-headers.d.ts} +5 -19
- package/middleware/sanitize-headers.js +18 -0
- package/middleware/secure-headers copy.d.ts +15 -0
- package/middleware/secure-headers copy.js +136 -0
- package/middleware/secure-headers.d.ts +132 -0
- package/middleware/secure-headers.js +153 -0
- package/middleware/{xssProtection.d.ts → xss-protection.d.ts} +2 -1
- package/middleware/xss-protection.js +19 -0
- package/node/env.js +4 -4
- package/node/getConnInfo.d.ts +21 -0
- package/node/getConnInfo.js +13 -0
- package/node/index.d.ts +13 -4
- package/node/index.js +11 -4
- package/node/mount-node.d.ts +11 -0
- package/node/mount-node.js +56 -0
- package/node/serveStatic.d.ts +36 -0
- package/node/serveStatic.js +52 -0
- package/node/toWebRequest.js +22 -0
- package/node/ws.d.ts +56 -0
- package/node/ws.js +46 -0
- package/package.json +39 -30
- package/registry/RadixRouter.d.ts +40 -0
- package/registry/RadixRouter.js +144 -0
- package/registry/index.d.ts +2 -0
- package/registry/index.js +1 -0
- package/types/headers.d.ts +2 -0
- package/types/headers.js +1 -0
- package/types/index.d.ts +318 -18
- package/types/index.js +12 -1
- package/utils/buffer.d.ts +1 -0
- package/utils/buffer.js +14 -0
- package/utils/colors.d.ts +24 -0
- package/utils/colors.js +2 -0
- package/utils/cookie.d.ts +55 -0
- package/utils/cookie.js +53 -0
- package/utils/file.d.ts +38 -0
- package/utils/file.js +96 -0
- package/utils/formData.d.ts +41 -1
- package/utils/formData.js +58 -9
- package/utils/generateID.d.ts +42 -0
- package/utils/generateID.js +32 -0
- package/utils/httpStatusMap.d.ts +14 -0
- package/utils/low-level.d.ts +58 -0
- package/utils/low-level.js +108 -0
- package/utils/mimeTypes.d.ts +4 -0
- package/utils/{staticFile.js → mimeTypes.js} +0 -53
- package/utils/rateLimit.d.ts +18 -0
- package/utils/rateLimit.js +37 -0
- package/utils/response.d.ts +18 -0
- package/utils/response.js +58 -0
- package/{core/environment.d.ts → utils/runtime.d.ts} +1 -0
- package/{core/environment.js → utils/runtime.js} +1 -0
- package/utils/url.d.ts +42 -14
- package/utils/url.js +61 -27
- package/bun/adapter.d.ts +0 -127
- package/bun/adapter.js +0 -97
- package/cjs/bun/adapter.js +0 -100
- package/cjs/core/MiddlewareConfigure.js +0 -68
- package/cjs/core/common.js +0 -15
- package/cjs/deno/adpater.js +0 -67
- package/cjs/helper/common.js +0 -17
- package/cjs/middleware/basicAuth.js +0 -71
- package/cjs/middleware/cacheControl.js +0 -90
- package/cjs/middleware/detectBot.js +0 -104
- package/cjs/middleware/detectLocale.js +0 -43
- package/cjs/middleware/lazyLoadModules.js +0 -73
- package/cjs/middleware/rateLimiter.js +0 -24
- package/cjs/middleware/requestTimeout.js +0 -42
- package/cjs/middleware/sanitizeHeader.js +0 -51
- package/cjs/middleware/secureHeaders.js +0 -42
- package/cjs/node/adapter.js +0 -138
- package/cjs/utils/regexRouter.js +0 -58
- package/cjs/utils/state.js +0 -34
- package/cjs/utils/toWebRequest.js +0 -35
- package/cjs/ws/deno.js +0 -20
- package/cjs/ws/index.js +0 -53
- package/cjs/ws/node.js +0 -65
- package/core/MiddlewareConfigure.d.ts +0 -15
- package/core/MiddlewareConfigure.js +0 -63
- package/core/common.d.ts +0 -21
- package/core/common.js +0 -11
- package/deno/adpater.d.ts +0 -38
- package/deno/adpater.js +0 -64
- package/helper/common.d.ts +0 -5
- package/helper/common.js +0 -14
- package/middleware/basicAuth.d.ts +0 -81
- package/middleware/basicAuth.js +0 -67
- package/middleware/cacheControl.d.ts +0 -48
- package/middleware/cacheControl.js +0 -53
- package/middleware/detectBot.d.ts +0 -121
- package/middleware/detectBot.js +0 -98
- package/middleware/detectLocale.d.ts +0 -55
- package/middleware/detectLocale.js +0 -39
- package/middleware/lazyLoadModules.d.ts +0 -72
- package/middleware/lazyLoadModules.js +0 -69
- package/middleware/rateLimiter.js +0 -20
- package/middleware/requestTimeout.d.ts +0 -25
- package/middleware/requestTimeout.js +0 -38
- package/middleware/sanitizeHeader.js +0 -47
- package/middleware/secureHeaders.d.ts +0 -78
- package/middleware/secureHeaders.js +0 -38
- package/middleware/xssProtection.js +0 -22
- package/node/adapter.d.ts +0 -46
- package/node/adapter.js +0 -102
- package/utils/regexRouter.d.ts +0 -66
- package/utils/regexRouter.js +0 -53
- package/utils/state.d.ts +0 -50
- package/utils/state.js +0 -30
- package/utils/staticFile.d.ts +0 -10
- package/utils/toWebRequest.js +0 -32
- package/ws/deno.d.ts +0 -6
- package/ws/deno.js +0 -16
- package/ws/index.d.ts +0 -180
- package/ws/index.js +0 -50
- package/ws/node.d.ts +0 -7
- package/ws/node.js +0 -28
- /package/{utils → node}/toWebRequest.d.ts +0 -0
package/core/router.d.ts
CHANGED
|
@@ -1,58 +1,77 @@
|
|
|
1
|
-
import { Callback,
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
*
|
|
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
|
|
13
|
-
*
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
28
|
+
/** Environment variables accessible within this router */
|
|
37
29
|
protected env: Record<string, string | number>;
|
|
38
|
-
|
|
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
|
-
*
|
|
52
|
+
* Registers static file routes to the application for serving files like HTML, CSS, JS, images, etc.
|
|
42
53
|
*
|
|
43
|
-
* This method
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
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
|
|
50
|
-
* @
|
|
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(
|
|
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
|
|
74
|
-
get<U extends Record<string, any> = {}, Path extends
|
|
75
|
-
get<U extends Record<string, any> = {}, Path extends
|
|
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
|
|
115
|
-
post<U extends Record<string, any> = {}, Path extends
|
|
116
|
-
post<U extends Record<string, any> = {}, Path extends
|
|
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
|
|
123
|
-
put<U extends Record<string, any> = {}, Path extends
|
|
124
|
-
put<U extends Record<string, any> = {}, Path extends
|
|
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
|
|
131
|
-
patch<U extends Record<string, any> = {}, Path extends
|
|
132
|
-
patch<U extends Record<string, any> = {}, Path extends
|
|
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
|
|
139
|
-
delete<U extends Record<string, any> = {}, Path extends
|
|
140
|
-
delete<U extends Record<string, any> = {}, Path extends
|
|
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
|
|
147
|
-
options<U extends Record<string, any> = {}, Path extends
|
|
148
|
-
options<U extends Record<string, any> = {}, Path extends
|
|
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
|
-
*
|
|
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
|
|
163
|
-
all<U extends Record<string, any> = {}, Path extends
|
|
164
|
-
all<U extends Record<string, any> = {}, Path extends
|
|
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
|
-
*
|
|
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
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
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
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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
|
|
180
|
-
*
|
|
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
|
|
178
|
+
* @returns The current Router instance for chaining
|
|
183
179
|
*
|
|
184
180
|
* @example
|
|
185
181
|
* const apiRouter = new Router();
|
|
186
|
-
* apiRouter.get('/users',
|
|
187
|
-
*
|
|
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
|
|
194
|
-
* @returns
|
|
192
|
+
* @param callback - Function receiving the scoped Router
|
|
193
|
+
* @returns The current Router instance for chaining
|
|
195
194
|
*
|
|
196
195
|
* @example
|
|
197
|
-
*
|
|
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
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
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
|
}
|