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