clear-router 2.7.5 → 2.7.7
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/dist/ClearRequest.cjs +23 -0
- package/dist/ClearRequest.d.cts +28 -0
- package/dist/ClearRequest.d.mts +28 -0
- package/dist/ClearRequest.mjs +22 -0
- package/dist/Contracts.d.cts +12 -0
- package/dist/Contracts.d.mts +12 -0
- package/dist/Controller.cjs +12 -0
- package/dist/Controller.d.cts +14 -0
- package/dist/Controller.d.mts +14 -0
- package/dist/Controller.mjs +11 -0
- package/dist/{Request-DKXwa_W0.d.mts → Request-BhTJDR_5.d.mts} +5 -5
- package/dist/Route.cjs +58 -0
- package/dist/Route.d.cts +35 -0
- package/dist/Route.d.mts +35 -0
- package/dist/Route.mjs +57 -0
- package/dist/core/Request.cjs +35 -0
- package/dist/core/Request.d.cts +25 -0
- package/dist/core/Request.d.mts +25 -0
- package/dist/core/Request.mjs +35 -0
- package/dist/core/Response.cjs +59 -0
- package/dist/core/Response.d.cts +24 -0
- package/dist/core/Response.d.mts +24 -0
- package/dist/core/Response.mjs +58 -0
- package/dist/{bindings-CLsZjOEy.cjs → core/bindings.cjs} +10 -160
- package/dist/{bindings-CNL7bpz5.d.mts → core/bindings.d.cts} +1 -1
- package/dist/{bindings-CxvtC8XS.d.cts → core/bindings.d.mts} +1 -1
- package/dist/{bindings-XLDXFpHZ.mjs → core/bindings.mjs} +3 -110
- package/dist/core/index.cjs +4 -20
- package/dist/core/index.d.cts +4 -1
- package/dist/core/index.d.mts +4 -1
- package/dist/core/index.mjs +4 -14
- package/dist/core/plugins.cjs +14 -0
- package/dist/core/plugins.d.cts +109 -0
- package/dist/core/plugins.d.mts +109 -0
- package/dist/core/plugins.mjs +13 -0
- package/dist/{responses-Bvnk0uvc.cjs → core/responses.cjs} +5 -20
- package/dist/{responses-BvETUeDL.mjs → core/responses.mjs} +2 -2
- package/dist/{router-C6W-k6sS.cjs → core/router.cjs} +11 -69
- package/dist/core/router.d.cts +274 -0
- package/dist/core/router.d.mts +274 -0
- package/dist/{router-Dc9w86Wn.mjs → core/router.mjs} +5 -58
- package/dist/decorators/index.cjs +1 -1
- package/dist/decorators/index.d.cts +1 -1
- package/dist/decorators/index.d.mts +1 -1
- package/dist/decorators/index.mjs +1 -1
- package/dist/decorators/setup.cjs +2 -2
- package/dist/decorators/setup.d.cts +1 -1
- package/dist/decorators/setup.d.mts +1 -1
- package/dist/decorators/setup.mjs +2 -2
- package/dist/express/index.cjs +2 -265
- package/dist/express/index.d.cts +1 -127
- package/dist/express/index.d.mts +1 -127
- package/dist/express/index.mjs +1 -264
- package/dist/express/router.cjs +265 -0
- package/dist/express/router.d.cts +131 -0
- package/dist/express/router.d.mts +131 -0
- package/dist/express/router.mjs +265 -0
- package/dist/fastify/index.cjs +2 -254
- package/dist/fastify/index.d.cts +1 -125
- package/dist/fastify/index.d.mts +1 -125
- package/dist/fastify/index.mjs +1 -253
- package/dist/fastify/router.cjs +254 -0
- package/dist/fastify/router.d.cts +129 -0
- package/dist/fastify/router.d.mts +129 -0
- package/dist/fastify/router.mjs +254 -0
- package/dist/h3/index.cjs +2 -260
- package/dist/h3/index.d.cts +1 -128
- package/dist/h3/index.d.mts +1 -128
- package/dist/h3/index.mjs +1 -259
- package/dist/h3/router.cjs +260 -0
- package/dist/h3/router.d.cts +132 -0
- package/dist/h3/router.d.mts +132 -0
- package/dist/h3/router.mjs +260 -0
- package/dist/hono/index.cjs +2 -251
- package/dist/hono/index.d.cts +1 -130
- package/dist/hono/index.d.mts +1 -130
- package/dist/hono/index.mjs +1 -250
- package/dist/hono/router.cjs +251 -0
- package/dist/hono/router.d.cts +134 -0
- package/dist/hono/router.d.mts +134 -0
- package/dist/hono/router.mjs +251 -0
- package/dist/index.cjs +16 -1097
- package/dist/index.d.cts +9 -563
- package/dist/index.d.mts +9 -563
- package/dist/index.mjs +8 -1089
- package/dist/koa/index.cjs +2 -261
- package/dist/koa/index.d.cts +1 -131
- package/dist/koa/index.d.mts +1 -131
- package/dist/koa/index.mjs +1 -260
- package/dist/koa/router.cjs +261 -0
- package/dist/koa/router.d.cts +135 -0
- package/dist/koa/router.d.mts +135 -0
- package/dist/koa/router.mjs +261 -0
- package/dist/types/basic.d.cts +43 -0
- package/dist/types/express.d.cts +44 -0
- package/dist/types/express.d.mts +4 -2
- package/dist/types/fastify.d.cts +23 -0
- package/dist/types/fastify.d.mts +4 -2
- package/dist/types/h3.d.cts +41 -0
- package/dist/types/h3.d.mts +5 -6
- package/dist/types/hono.d.cts +21 -0
- package/dist/types/hono.d.mts +5 -5
- package/dist/types/koa.d.cts +25 -0
- package/dist/types/koa.d.mts +5 -5
- package/package.json +1 -1
- package/dist/router-CYBshGMl.d.mts +0 -652
- package/dist/router-tpnSi_Y7.d.cts +0 -652
package/dist/hono/index.d.cts
CHANGED
|
@@ -1,131 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
//#region src/hono/router.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* @class clear-router Hono Router
|
|
6
|
-
* @description Laravel-style routing system for Hono using shared clear-router core
|
|
7
|
-
* @author 3m1n3nc3
|
|
8
|
-
* @repository https://github.com/arkstack-tmp/clear-router
|
|
9
|
-
*/
|
|
10
|
-
declare class Router extends CoreRouter {
|
|
11
|
-
protected static routerStateNamespace: string;
|
|
12
|
-
private static readonly bodyCache;
|
|
13
|
-
private static toResponse;
|
|
14
|
-
private static getParams;
|
|
15
|
-
private static readBodyCached;
|
|
16
|
-
/**
|
|
17
|
-
* Adds a new route to the router.
|
|
18
|
-
*
|
|
19
|
-
* @param methods
|
|
20
|
-
* @param path
|
|
21
|
-
* @param handler
|
|
22
|
-
* @param middlewares
|
|
23
|
-
*/
|
|
24
|
-
static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
25
|
-
/**
|
|
26
|
-
* Define a resourceful API controller with standard CRUD routes
|
|
27
|
-
*
|
|
28
|
-
* @param basePath
|
|
29
|
-
* @param controller
|
|
30
|
-
* @param options
|
|
31
|
-
*/
|
|
32
|
-
static apiResource(basePath: string, controller: any, options?: {
|
|
33
|
-
only?: ControllerAction[];
|
|
34
|
-
except?: ControllerAction[];
|
|
35
|
-
middlewares?: ApiResourceMiddleware<Middleware>;
|
|
36
|
-
}): void;
|
|
37
|
-
/**
|
|
38
|
-
* Define a GET route
|
|
39
|
-
*
|
|
40
|
-
* @param path
|
|
41
|
-
* @param handler
|
|
42
|
-
* @param middlewares
|
|
43
|
-
*/
|
|
44
|
-
static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
45
|
-
/**
|
|
46
|
-
* Define a POST route
|
|
47
|
-
*
|
|
48
|
-
* @param path
|
|
49
|
-
* @param handler
|
|
50
|
-
* @param middlewares
|
|
51
|
-
*/
|
|
52
|
-
static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
53
|
-
/**
|
|
54
|
-
* Define a PUT route
|
|
55
|
-
*
|
|
56
|
-
* @param path
|
|
57
|
-
* @param handler
|
|
58
|
-
* @param middlewares
|
|
59
|
-
*/
|
|
60
|
-
static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
61
|
-
/**
|
|
62
|
-
* Define a DELETE route
|
|
63
|
-
*
|
|
64
|
-
* @param path
|
|
65
|
-
* @param handler
|
|
66
|
-
* @param middlewares
|
|
67
|
-
*/
|
|
68
|
-
static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
69
|
-
/**
|
|
70
|
-
* Define a PATCH route
|
|
71
|
-
*
|
|
72
|
-
* @param path
|
|
73
|
-
* @param handler
|
|
74
|
-
* @param middlewares
|
|
75
|
-
*/
|
|
76
|
-
static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
77
|
-
/**
|
|
78
|
-
* Define an OPTIONS route
|
|
79
|
-
*
|
|
80
|
-
* @param path
|
|
81
|
-
* @param handler
|
|
82
|
-
* @param middlewares
|
|
83
|
-
*/
|
|
84
|
-
static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
85
|
-
/**
|
|
86
|
-
* Define a HEAD route
|
|
87
|
-
*
|
|
88
|
-
* @param path
|
|
89
|
-
* @param handler
|
|
90
|
-
* @param middlewares
|
|
91
|
-
*/
|
|
92
|
-
static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
93
|
-
/**
|
|
94
|
-
* Defines a group of routes with a common prefix.
|
|
95
|
-
*
|
|
96
|
-
* @param prefix
|
|
97
|
-
* @param callback
|
|
98
|
-
* @param middlewares
|
|
99
|
-
*/
|
|
100
|
-
static group(prefix: string, callback: () => void | Promise<void>, middlewares?: Middleware[]): Promise<void>;
|
|
101
|
-
/**
|
|
102
|
-
* Apply middlewares to a group of routes defined within the callback
|
|
103
|
-
*
|
|
104
|
-
* @param middlewares - Middleware or array of middlewares to apply
|
|
105
|
-
* @param callback - Function that defines the routes to which the middlewares will be applied
|
|
106
|
-
*/
|
|
107
|
-
static middleware(middlewares: Middleware[], callback: () => void): void;
|
|
108
|
-
/**
|
|
109
|
-
* Get all defined routes, optionally organized by path or method
|
|
110
|
-
*/
|
|
111
|
-
static allRoutes(): Array<Route<HttpContext, Middleware, Handler>>;
|
|
112
|
-
/**
|
|
113
|
-
* @param type - 'path' to get routes organized by path
|
|
114
|
-
*/
|
|
115
|
-
static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware, Handler>>;
|
|
116
|
-
/**
|
|
117
|
-
* @param type - 'method' to get routes organized by method
|
|
118
|
-
*/
|
|
119
|
-
static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
|
|
120
|
-
static allRoutes(type: 'name'): Record<string, Route<HttpContext, Middleware, Handler>>;
|
|
121
|
-
static route(name: string): Route<HttpContext, Middleware, Handler> | undefined;
|
|
122
|
-
/**
|
|
123
|
-
* Apply the defined routes to a Hono application instance
|
|
124
|
-
*
|
|
125
|
-
* @param app The Hono application instance
|
|
126
|
-
* @returns The Hono application instance with the applied routes
|
|
127
|
-
*/
|
|
128
|
-
static apply(app: HonoApp): HonoApp;
|
|
129
|
-
}
|
|
130
|
-
//#endregion
|
|
1
|
+
import { Router } from "./router.cjs";
|
|
131
2
|
export { Router };
|
package/dist/hono/index.d.mts
CHANGED
|
@@ -1,131 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
//#region src/hono/router.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* @class clear-router Hono Router
|
|
6
|
-
* @description Laravel-style routing system for Hono using shared clear-router core
|
|
7
|
-
* @author 3m1n3nc3
|
|
8
|
-
* @repository https://github.com/arkstack-tmp/clear-router
|
|
9
|
-
*/
|
|
10
|
-
declare class Router extends CoreRouter {
|
|
11
|
-
protected static routerStateNamespace: string;
|
|
12
|
-
private static readonly bodyCache;
|
|
13
|
-
private static toResponse;
|
|
14
|
-
private static getParams;
|
|
15
|
-
private static readBodyCached;
|
|
16
|
-
/**
|
|
17
|
-
* Adds a new route to the router.
|
|
18
|
-
*
|
|
19
|
-
* @param methods
|
|
20
|
-
* @param path
|
|
21
|
-
* @param handler
|
|
22
|
-
* @param middlewares
|
|
23
|
-
*/
|
|
24
|
-
static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
25
|
-
/**
|
|
26
|
-
* Define a resourceful API controller with standard CRUD routes
|
|
27
|
-
*
|
|
28
|
-
* @param basePath
|
|
29
|
-
* @param controller
|
|
30
|
-
* @param options
|
|
31
|
-
*/
|
|
32
|
-
static apiResource(basePath: string, controller: any, options?: {
|
|
33
|
-
only?: ControllerAction[];
|
|
34
|
-
except?: ControllerAction[];
|
|
35
|
-
middlewares?: ApiResourceMiddleware<Middleware>;
|
|
36
|
-
}): void;
|
|
37
|
-
/**
|
|
38
|
-
* Define a GET route
|
|
39
|
-
*
|
|
40
|
-
* @param path
|
|
41
|
-
* @param handler
|
|
42
|
-
* @param middlewares
|
|
43
|
-
*/
|
|
44
|
-
static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
45
|
-
/**
|
|
46
|
-
* Define a POST route
|
|
47
|
-
*
|
|
48
|
-
* @param path
|
|
49
|
-
* @param handler
|
|
50
|
-
* @param middlewares
|
|
51
|
-
*/
|
|
52
|
-
static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
53
|
-
/**
|
|
54
|
-
* Define a PUT route
|
|
55
|
-
*
|
|
56
|
-
* @param path
|
|
57
|
-
* @param handler
|
|
58
|
-
* @param middlewares
|
|
59
|
-
*/
|
|
60
|
-
static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
61
|
-
/**
|
|
62
|
-
* Define a DELETE route
|
|
63
|
-
*
|
|
64
|
-
* @param path
|
|
65
|
-
* @param handler
|
|
66
|
-
* @param middlewares
|
|
67
|
-
*/
|
|
68
|
-
static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
69
|
-
/**
|
|
70
|
-
* Define a PATCH route
|
|
71
|
-
*
|
|
72
|
-
* @param path
|
|
73
|
-
* @param handler
|
|
74
|
-
* @param middlewares
|
|
75
|
-
*/
|
|
76
|
-
static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
77
|
-
/**
|
|
78
|
-
* Define an OPTIONS route
|
|
79
|
-
*
|
|
80
|
-
* @param path
|
|
81
|
-
* @param handler
|
|
82
|
-
* @param middlewares
|
|
83
|
-
*/
|
|
84
|
-
static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
85
|
-
/**
|
|
86
|
-
* Define a HEAD route
|
|
87
|
-
*
|
|
88
|
-
* @param path
|
|
89
|
-
* @param handler
|
|
90
|
-
* @param middlewares
|
|
91
|
-
*/
|
|
92
|
-
static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
93
|
-
/**
|
|
94
|
-
* Defines a group of routes with a common prefix.
|
|
95
|
-
*
|
|
96
|
-
* @param prefix
|
|
97
|
-
* @param callback
|
|
98
|
-
* @param middlewares
|
|
99
|
-
*/
|
|
100
|
-
static group(prefix: string, callback: () => void | Promise<void>, middlewares?: Middleware[]): Promise<void>;
|
|
101
|
-
/**
|
|
102
|
-
* Apply middlewares to a group of routes defined within the callback
|
|
103
|
-
*
|
|
104
|
-
* @param middlewares - Middleware or array of middlewares to apply
|
|
105
|
-
* @param callback - Function that defines the routes to which the middlewares will be applied
|
|
106
|
-
*/
|
|
107
|
-
static middleware(middlewares: Middleware[], callback: () => void): void;
|
|
108
|
-
/**
|
|
109
|
-
* Get all defined routes, optionally organized by path or method
|
|
110
|
-
*/
|
|
111
|
-
static allRoutes(): Array<Route<HttpContext, Middleware, Handler>>;
|
|
112
|
-
/**
|
|
113
|
-
* @param type - 'path' to get routes organized by path
|
|
114
|
-
*/
|
|
115
|
-
static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware, Handler>>;
|
|
116
|
-
/**
|
|
117
|
-
* @param type - 'method' to get routes organized by method
|
|
118
|
-
*/
|
|
119
|
-
static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
|
|
120
|
-
static allRoutes(type: 'name'): Record<string, Route<HttpContext, Middleware, Handler>>;
|
|
121
|
-
static route(name: string): Route<HttpContext, Middleware, Handler> | undefined;
|
|
122
|
-
/**
|
|
123
|
-
* Apply the defined routes to a Hono application instance
|
|
124
|
-
*
|
|
125
|
-
* @param app The Hono application instance
|
|
126
|
-
* @returns The Hono application instance with the applied routes
|
|
127
|
-
*/
|
|
128
|
-
static apply(app: HonoApp): HonoApp;
|
|
129
|
-
}
|
|
130
|
-
//#endregion
|
|
1
|
+
import { Router } from "./router.mjs";
|
|
131
2
|
export { Router };
|
package/dist/hono/index.mjs
CHANGED
|
@@ -1,252 +1,3 @@
|
|
|
1
|
-
import "
|
|
2
|
-
import { t as CoreRouter } from "../router-Dc9w86Wn.mjs";
|
|
3
|
-
import { n as resolveResponseMeta } from "../responses-BvETUeDL.mjs";
|
|
1
|
+
import Router from "./router.mjs";
|
|
4
2
|
|
|
5
|
-
//#region src/hono/router.ts
|
|
6
|
-
/**
|
|
7
|
-
* @class clear-router Hono Router
|
|
8
|
-
* @description Laravel-style routing system for Hono using shared clear-router core
|
|
9
|
-
* @author 3m1n3nc3
|
|
10
|
-
* @repository https://github.com/arkstack-tmp/clear-router
|
|
11
|
-
*/
|
|
12
|
-
var Router = class Router extends CoreRouter {
|
|
13
|
-
static routerStateNamespace = "clear-router:hono";
|
|
14
|
-
static bodyCache = /* @__PURE__ */ new WeakMap();
|
|
15
|
-
static toResponse(ctx, value, method, path) {
|
|
16
|
-
const meta = resolveResponseMeta(value, {
|
|
17
|
-
headers: ctx.req.header(),
|
|
18
|
-
method,
|
|
19
|
-
path
|
|
20
|
-
});
|
|
21
|
-
if (!meta) return void 0;
|
|
22
|
-
if (meta.isNativeResponse) return meta.body;
|
|
23
|
-
const headers = meta.headers ? {} : meta.contentType ? { "Content-Type": meta.contentType } : void 0;
|
|
24
|
-
meta.headers?.forEach((headerValue, key) => {
|
|
25
|
-
if (headers) headers[key] = headerValue;
|
|
26
|
-
});
|
|
27
|
-
if (meta.isEmpty) return ctx.body(null, meta.status, headers);
|
|
28
|
-
if (meta.contentType?.startsWith("application/json")) return ctx.body(JSON.stringify(meta.body), meta.status, headers);
|
|
29
|
-
return ctx.body(meta.body, meta.status, headers);
|
|
30
|
-
}
|
|
31
|
-
static getParams(ctx) {
|
|
32
|
-
try {
|
|
33
|
-
const raw = ctx.req.param?.();
|
|
34
|
-
return raw && typeof raw === "object" ? raw : {};
|
|
35
|
-
} catch {
|
|
36
|
-
return {};
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
static async readBodyCached(ctx) {
|
|
40
|
-
if (this.bodyCache.has(ctx)) {
|
|
41
|
-
const cached = this.bodyCache.get(ctx) || {};
|
|
42
|
-
ctx.req.getBody = () => cached;
|
|
43
|
-
return cached;
|
|
44
|
-
}
|
|
45
|
-
let body = {};
|
|
46
|
-
const contentType = (ctx.req.header("content-type") || "").toLowerCase();
|
|
47
|
-
if (contentType.includes("application/json")) body = await ctx.req.json().catch(() => ({}));
|
|
48
|
-
else if (contentType.includes("multipart/form-data") || contentType.includes("application/x-www-form-urlencoded")) body = await ctx.req.parseBody().then((v) => v).catch(() => ({}));
|
|
49
|
-
ctx.req.getBody = () => body;
|
|
50
|
-
this.bodyCache.set(ctx, body);
|
|
51
|
-
return body;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Adds a new route to the router.
|
|
55
|
-
*
|
|
56
|
-
* @param methods
|
|
57
|
-
* @param path
|
|
58
|
-
* @param handler
|
|
59
|
-
* @param middlewares
|
|
60
|
-
*/
|
|
61
|
-
static add(methods, path, handler, middlewares) {
|
|
62
|
-
return super.add(methods, path, handler, middlewares);
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Define a resourceful API controller with standard CRUD routes
|
|
66
|
-
*
|
|
67
|
-
* @param basePath
|
|
68
|
-
* @param controller
|
|
69
|
-
* @param options
|
|
70
|
-
*/
|
|
71
|
-
static apiResource(basePath, controller, options) {
|
|
72
|
-
super.apiResource(basePath, controller, options);
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Define a GET route
|
|
76
|
-
*
|
|
77
|
-
* @param path
|
|
78
|
-
* @param handler
|
|
79
|
-
* @param middlewares
|
|
80
|
-
*/
|
|
81
|
-
static get(path, handler, middlewares) {
|
|
82
|
-
return super.get(path, handler, middlewares);
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Define a POST route
|
|
86
|
-
*
|
|
87
|
-
* @param path
|
|
88
|
-
* @param handler
|
|
89
|
-
* @param middlewares
|
|
90
|
-
*/
|
|
91
|
-
static post(path, handler, middlewares) {
|
|
92
|
-
return super.post(path, handler, middlewares);
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Define a PUT route
|
|
96
|
-
*
|
|
97
|
-
* @param path
|
|
98
|
-
* @param handler
|
|
99
|
-
* @param middlewares
|
|
100
|
-
*/
|
|
101
|
-
static put(path, handler, middlewares) {
|
|
102
|
-
return super.put(path, handler, middlewares);
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Define a DELETE route
|
|
106
|
-
*
|
|
107
|
-
* @param path
|
|
108
|
-
* @param handler
|
|
109
|
-
* @param middlewares
|
|
110
|
-
*/
|
|
111
|
-
static delete(path, handler, middlewares) {
|
|
112
|
-
return super.delete(path, handler, middlewares);
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Define a PATCH route
|
|
116
|
-
*
|
|
117
|
-
* @param path
|
|
118
|
-
* @param handler
|
|
119
|
-
* @param middlewares
|
|
120
|
-
*/
|
|
121
|
-
static patch(path, handler, middlewares) {
|
|
122
|
-
return super.patch(path, handler, middlewares);
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Define an OPTIONS route
|
|
126
|
-
*
|
|
127
|
-
* @param path
|
|
128
|
-
* @param handler
|
|
129
|
-
* @param middlewares
|
|
130
|
-
*/
|
|
131
|
-
static options(path, handler, middlewares) {
|
|
132
|
-
return super.options(path, handler, middlewares);
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Define a HEAD route
|
|
136
|
-
*
|
|
137
|
-
* @param path
|
|
138
|
-
* @param handler
|
|
139
|
-
* @param middlewares
|
|
140
|
-
*/
|
|
141
|
-
static head(path, handler, middlewares) {
|
|
142
|
-
return super.head(path, handler, middlewares);
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
145
|
-
* Defines a group of routes with a common prefix.
|
|
146
|
-
*
|
|
147
|
-
* @param prefix
|
|
148
|
-
* @param callback
|
|
149
|
-
* @param middlewares
|
|
150
|
-
*/
|
|
151
|
-
static async group(prefix, callback, middlewares) {
|
|
152
|
-
await super.group(prefix, callback, middlewares);
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Apply middlewares to a group of routes defined within the callback
|
|
156
|
-
*
|
|
157
|
-
* @param middlewares - Middleware or array of middlewares to apply
|
|
158
|
-
* @param callback - Function that defines the routes to which the middlewares will be applied
|
|
159
|
-
*/
|
|
160
|
-
static middleware(middlewares, callback) {
|
|
161
|
-
super.middleware(middlewares, callback);
|
|
162
|
-
}
|
|
163
|
-
static allRoutes(type) {
|
|
164
|
-
return super.allRoutes(type);
|
|
165
|
-
}
|
|
166
|
-
static route(name) {
|
|
167
|
-
return super.route(name);
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Apply the defined routes to a Hono application instance
|
|
171
|
-
*
|
|
172
|
-
* @param app The Hono application instance
|
|
173
|
-
* @returns The Hono application instance with the applied routes
|
|
174
|
-
*/
|
|
175
|
-
static apply(app) {
|
|
176
|
-
for (const route of Array.from(this.routes)) {
|
|
177
|
-
let handlerFunction = null;
|
|
178
|
-
let instance = null;
|
|
179
|
-
let bindingTarget;
|
|
180
|
-
let bindingMethod;
|
|
181
|
-
let bindingHandler;
|
|
182
|
-
let bindingMetadata;
|
|
183
|
-
try {
|
|
184
|
-
const resolved = this.resolveHandler(route);
|
|
185
|
-
handlerFunction = resolved.handlerFunction;
|
|
186
|
-
instance = resolved.instance;
|
|
187
|
-
bindingTarget = resolved.bindingTarget;
|
|
188
|
-
bindingMethod = resolved.bindingMethod;
|
|
189
|
-
bindingHandler = resolved.bindingHandler;
|
|
190
|
-
bindingMetadata = resolved.bindingMetadata;
|
|
191
|
-
} catch (error) {
|
|
192
|
-
console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
|
|
193
|
-
throw error;
|
|
194
|
-
}
|
|
195
|
-
if (!handlerFunction) continue;
|
|
196
|
-
for (const method of route.methods) {
|
|
197
|
-
const allowedMethods = [
|
|
198
|
-
"get",
|
|
199
|
-
"post",
|
|
200
|
-
"put",
|
|
201
|
-
"delete",
|
|
202
|
-
"patch",
|
|
203
|
-
"options",
|
|
204
|
-
"head"
|
|
205
|
-
];
|
|
206
|
-
if (method === "options" && route.methods.length > 1) continue;
|
|
207
|
-
if (!allowedMethods.includes(method)) throw new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
|
|
208
|
-
for (const registrationPath of route.registrationPaths) app[method](registrationPath, ...route.middlewares || [], async (context) => {
|
|
209
|
-
const ctx = context;
|
|
210
|
-
const reqBody = await Router.readBodyCached(ctx);
|
|
211
|
-
const override = Router.resolveMethodOverride(ctx.req.method, ctx.req.header(), reqBody);
|
|
212
|
-
if (method === "post" && override && override !== "post") return;
|
|
213
|
-
const inst = instance ?? route;
|
|
214
|
-
Router.bindRequestToInstance(ctx, inst, route, {
|
|
215
|
-
body: reqBody,
|
|
216
|
-
query: ctx.req.query(),
|
|
217
|
-
params: Router.getParams(ctx),
|
|
218
|
-
method
|
|
219
|
-
});
|
|
220
|
-
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
221
|
-
const resolved = await Promise.resolve(result);
|
|
222
|
-
const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
|
|
223
|
-
return Router.toResponse(ctx, outgoing, method, route.path);
|
|
224
|
-
});
|
|
225
|
-
if ([
|
|
226
|
-
"put",
|
|
227
|
-
"patch",
|
|
228
|
-
"delete"
|
|
229
|
-
].includes(method)) for (const registrationPath of route.registrationPaths) app.post(registrationPath, ...route.middlewares || [], async (context) => {
|
|
230
|
-
const ctx = context;
|
|
231
|
-
const reqBody = await Router.readBodyCached(ctx);
|
|
232
|
-
if (Router.resolveMethodOverride(ctx.req.method, ctx.req.header(), reqBody) !== method) return;
|
|
233
|
-
const inst = instance ?? route;
|
|
234
|
-
Router.bindRequestToInstance(ctx, inst, route, {
|
|
235
|
-
body: reqBody,
|
|
236
|
-
query: ctx.req.query(),
|
|
237
|
-
params: Router.getParams(ctx),
|
|
238
|
-
method
|
|
239
|
-
});
|
|
240
|
-
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
241
|
-
const resolved = await Promise.resolve(result);
|
|
242
|
-
const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
|
|
243
|
-
return Router.toResponse(ctx, outgoing, method, route.path);
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
return app;
|
|
248
|
-
}
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
//#endregion
|
|
252
3
|
export { Router };
|