clear-router 2.7.6 → 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 +4 -4
- package/dist/types/hono.d.cts +21 -0
- package/dist/types/hono.d.mts +4 -3
- package/dist/types/koa.d.cts +25 -0
- package/dist/types/koa.d.mts +4 -3
- package/package.json +1 -1
- package/dist/router-BEgAxp5A.d.cts +0 -649
- package/dist/router-DKKYx23P.d.mts +0 -649
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { ApiResourceMiddleware, ControllerAction, HttpMethod } from "../types/basic.mjs";
|
|
2
|
+
import { Handler, HttpContext, KoaRouterApp, Middleware } from "../types/koa.mjs";
|
|
3
|
+
import { Route } from "../Route.mjs";
|
|
4
|
+
import { CoreRouter } from "../core/router.mjs";
|
|
5
|
+
|
|
6
|
+
//#region src/koa/router.d.ts
|
|
7
|
+
/**
|
|
8
|
+
* @class clear-router Koa Router
|
|
9
|
+
* @description Laravel-style routing system for Koa using @koa/router and shared clear-router core
|
|
10
|
+
* @author 3m1n3nc3
|
|
11
|
+
* @repository https://github.com/arkstack-tmp/clear-router
|
|
12
|
+
*/
|
|
13
|
+
declare class Router extends CoreRouter {
|
|
14
|
+
protected static routerStateNamespace: string;
|
|
15
|
+
private static readonly bodyCache;
|
|
16
|
+
private static readBodyCached;
|
|
17
|
+
private static readBody;
|
|
18
|
+
private static sendReturnValue;
|
|
19
|
+
/**
|
|
20
|
+
* Adds a new route to the router.
|
|
21
|
+
*
|
|
22
|
+
* @param methods
|
|
23
|
+
* @param path
|
|
24
|
+
* @param handler
|
|
25
|
+
* @param middlewares
|
|
26
|
+
*/
|
|
27
|
+
static add(methods: HttpMethod | HttpMethod[], path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
28
|
+
/**
|
|
29
|
+
* Define a resourceful API controller with standard CRUD routes
|
|
30
|
+
*
|
|
31
|
+
* @param basePath
|
|
32
|
+
* @param controller
|
|
33
|
+
* @param options
|
|
34
|
+
*/
|
|
35
|
+
static apiResource(basePath: string, controller: any, options?: {
|
|
36
|
+
only?: ControllerAction[];
|
|
37
|
+
except?: ControllerAction[];
|
|
38
|
+
middlewares?: ApiResourceMiddleware<Middleware>;
|
|
39
|
+
}): void;
|
|
40
|
+
/**
|
|
41
|
+
* Define a GET route
|
|
42
|
+
*
|
|
43
|
+
* @param path
|
|
44
|
+
* @param handler
|
|
45
|
+
* @param middlewares
|
|
46
|
+
*/
|
|
47
|
+
static get(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
48
|
+
/**
|
|
49
|
+
* Define a POST route
|
|
50
|
+
*
|
|
51
|
+
* @param path
|
|
52
|
+
* @param handler
|
|
53
|
+
* @param middlewares
|
|
54
|
+
*/
|
|
55
|
+
static post(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
56
|
+
/**
|
|
57
|
+
* Define a PUT route
|
|
58
|
+
*
|
|
59
|
+
* @param path
|
|
60
|
+
* @param handler
|
|
61
|
+
* @param middlewares
|
|
62
|
+
*/
|
|
63
|
+
static put(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
64
|
+
/**
|
|
65
|
+
* Define a DELETE route
|
|
66
|
+
*
|
|
67
|
+
* @param path
|
|
68
|
+
* @param handler
|
|
69
|
+
* @param middlewares
|
|
70
|
+
*/
|
|
71
|
+
static delete(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
72
|
+
/**
|
|
73
|
+
* Define a PATCH route
|
|
74
|
+
*
|
|
75
|
+
* @param path
|
|
76
|
+
* @param handler
|
|
77
|
+
* @param middlewares
|
|
78
|
+
*/
|
|
79
|
+
static patch(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
80
|
+
/**
|
|
81
|
+
* Define an OPTIONS route
|
|
82
|
+
*
|
|
83
|
+
* @param path
|
|
84
|
+
* @param handler
|
|
85
|
+
* @param middlewares
|
|
86
|
+
*/
|
|
87
|
+
static options(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, Handler>;
|
|
88
|
+
/**
|
|
89
|
+
* Adds a new HEAD route to the router.
|
|
90
|
+
*
|
|
91
|
+
* @param this
|
|
92
|
+
* @param path
|
|
93
|
+
* @param handler
|
|
94
|
+
* @param middlewares
|
|
95
|
+
*/
|
|
96
|
+
static head(path: string, handler: Handler, middlewares?: Middleware[] | Middleware): Route<HttpContext, Middleware, 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[]): Promise<void>;
|
|
105
|
+
/**
|
|
106
|
+
* Apply middlewares to a group of routes defined within the callback
|
|
107
|
+
*
|
|
108
|
+
* @param middlewares - Middleware or array of middlewares to apply
|
|
109
|
+
* @param callback - Function that defines the routes to which the middlewares will be applied
|
|
110
|
+
*/
|
|
111
|
+
static middleware(middlewares: Middleware[], callback: () => void): void;
|
|
112
|
+
/**
|
|
113
|
+
* Get all defined routes, optionally organized by path or method
|
|
114
|
+
*/
|
|
115
|
+
static allRoutes(): Array<Route<HttpContext, Middleware, Handler>>;
|
|
116
|
+
/**
|
|
117
|
+
* @param type - 'path' to get routes organized by path
|
|
118
|
+
*/
|
|
119
|
+
static allRoutes(type: 'path'): Record<string, Route<HttpContext, Middleware, Handler>>;
|
|
120
|
+
/**
|
|
121
|
+
* @param type - 'method' to get routes organized by method
|
|
122
|
+
*/
|
|
123
|
+
static allRoutes(type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<HttpContext, Middleware, Handler>> };
|
|
124
|
+
static allRoutes(type: 'name'): Record<string, Route<HttpContext, Middleware, Handler>>;
|
|
125
|
+
static route(name: string): Route<HttpContext, Middleware, Handler> | undefined;
|
|
126
|
+
/**
|
|
127
|
+
* Apply the defined routes to a @koa/router instance
|
|
128
|
+
*
|
|
129
|
+
* @param router @koa/router instance
|
|
130
|
+
* @returns The @koa/router instance with the applied routes
|
|
131
|
+
*/
|
|
132
|
+
static apply(router: KoaRouterApp): KoaRouterApp;
|
|
133
|
+
}
|
|
134
|
+
//#endregion
|
|
135
|
+
export { Router };
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { CoreRouter } from "../core/router.mjs";
|
|
2
|
+
import { isFetchResponse, resolveResponseMeta } from "../core/responses.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/koa/router.ts
|
|
5
|
+
/**
|
|
6
|
+
* @class clear-router Koa Router
|
|
7
|
+
* @description Laravel-style routing system for Koa using @koa/router and shared clear-router core
|
|
8
|
+
* @author 3m1n3nc3
|
|
9
|
+
* @repository https://github.com/arkstack-tmp/clear-router
|
|
10
|
+
*/
|
|
11
|
+
var Router = class Router extends CoreRouter {
|
|
12
|
+
static routerStateNamespace = "clear-router:koa";
|
|
13
|
+
static bodyCache = /* @__PURE__ */ new WeakMap();
|
|
14
|
+
static async readBodyCached(ctx) {
|
|
15
|
+
if (this.bodyCache.has(ctx)) {
|
|
16
|
+
const cached = this.bodyCache.get(ctx) || {};
|
|
17
|
+
ctx.request.getBody = () => cached;
|
|
18
|
+
return cached;
|
|
19
|
+
}
|
|
20
|
+
let body = ctx.request.body && typeof ctx.request.body === "object" ? ctx.request.body : {};
|
|
21
|
+
if (!Object.keys(body).length && !["GET", "HEAD"].includes(ctx.method.toUpperCase())) body = await this.readBody(ctx);
|
|
22
|
+
ctx.request.getBody = () => body;
|
|
23
|
+
this.bodyCache.set(ctx, body);
|
|
24
|
+
return body;
|
|
25
|
+
}
|
|
26
|
+
static async readBody(ctx) {
|
|
27
|
+
const contentType = String(ctx.get("content-type") || "").toLowerCase();
|
|
28
|
+
const chunks = [];
|
|
29
|
+
for await (const chunk of ctx.req) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
30
|
+
const raw = Buffer.concat(chunks).toString("utf8");
|
|
31
|
+
if (!raw) return {};
|
|
32
|
+
if (contentType.includes("application/json")) return JSON.parse(raw);
|
|
33
|
+
if (contentType.includes("application/x-www-form-urlencoded")) return Object.fromEntries(new URLSearchParams(raw));
|
|
34
|
+
return {};
|
|
35
|
+
}
|
|
36
|
+
static async sendReturnValue(ctx, value, method, path) {
|
|
37
|
+
if (ctx.respond === false || ctx.headerSent) return value;
|
|
38
|
+
const meta = resolveResponseMeta(value, {
|
|
39
|
+
headers: ctx.headers,
|
|
40
|
+
method,
|
|
41
|
+
path,
|
|
42
|
+
status: ctx.status && ctx.status !== 404 ? ctx.status : void 0
|
|
43
|
+
});
|
|
44
|
+
if (!meta) return void 0;
|
|
45
|
+
ctx.status = meta.status;
|
|
46
|
+
meta.headers?.forEach((headerValue, key) => {
|
|
47
|
+
ctx.set(key, headerValue);
|
|
48
|
+
});
|
|
49
|
+
if (isFetchResponse(meta.body)) {
|
|
50
|
+
meta.body.headers.forEach((headerValue, key) => {
|
|
51
|
+
ctx.set(key, headerValue);
|
|
52
|
+
});
|
|
53
|
+
ctx.status = meta.body.status;
|
|
54
|
+
ctx.body = Buffer.from(await meta.body.arrayBuffer());
|
|
55
|
+
return ctx.body;
|
|
56
|
+
}
|
|
57
|
+
if (meta.contentType) ctx.type = meta.contentType;
|
|
58
|
+
ctx.body = meta.isEmpty ? null : meta.body;
|
|
59
|
+
return ctx.body;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Adds a new route to the router.
|
|
63
|
+
*
|
|
64
|
+
* @param methods
|
|
65
|
+
* @param path
|
|
66
|
+
* @param handler
|
|
67
|
+
* @param middlewares
|
|
68
|
+
*/
|
|
69
|
+
static add(methods, path, handler, middlewares) {
|
|
70
|
+
return super.add(methods, path, handler, middlewares);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Define a resourceful API controller with standard CRUD routes
|
|
74
|
+
*
|
|
75
|
+
* @param basePath
|
|
76
|
+
* @param controller
|
|
77
|
+
* @param options
|
|
78
|
+
*/
|
|
79
|
+
static apiResource(basePath, controller, options) {
|
|
80
|
+
super.apiResource(basePath, controller, options);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Define a GET route
|
|
84
|
+
*
|
|
85
|
+
* @param path
|
|
86
|
+
* @param handler
|
|
87
|
+
* @param middlewares
|
|
88
|
+
*/
|
|
89
|
+
static get(path, handler, middlewares) {
|
|
90
|
+
return super.get(path, handler, middlewares);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Define a POST route
|
|
94
|
+
*
|
|
95
|
+
* @param path
|
|
96
|
+
* @param handler
|
|
97
|
+
* @param middlewares
|
|
98
|
+
*/
|
|
99
|
+
static post(path, handler, middlewares) {
|
|
100
|
+
return super.post(path, handler, middlewares);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Define a PUT route
|
|
104
|
+
*
|
|
105
|
+
* @param path
|
|
106
|
+
* @param handler
|
|
107
|
+
* @param middlewares
|
|
108
|
+
*/
|
|
109
|
+
static put(path, handler, middlewares) {
|
|
110
|
+
return super.put(path, handler, middlewares);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Define a DELETE route
|
|
114
|
+
*
|
|
115
|
+
* @param path
|
|
116
|
+
* @param handler
|
|
117
|
+
* @param middlewares
|
|
118
|
+
*/
|
|
119
|
+
static delete(path, handler, middlewares) {
|
|
120
|
+
return super.delete(path, handler, middlewares);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Define a PATCH route
|
|
124
|
+
*
|
|
125
|
+
* @param path
|
|
126
|
+
* @param handler
|
|
127
|
+
* @param middlewares
|
|
128
|
+
*/
|
|
129
|
+
static patch(path, handler, middlewares) {
|
|
130
|
+
return super.patch(path, handler, middlewares);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Define an OPTIONS route
|
|
134
|
+
*
|
|
135
|
+
* @param path
|
|
136
|
+
* @param handler
|
|
137
|
+
* @param middlewares
|
|
138
|
+
*/
|
|
139
|
+
static options(path, handler, middlewares) {
|
|
140
|
+
return super.options(path, handler, middlewares);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Adds a new HEAD route to the router.
|
|
144
|
+
*
|
|
145
|
+
* @param this
|
|
146
|
+
* @param path
|
|
147
|
+
* @param handler
|
|
148
|
+
* @param middlewares
|
|
149
|
+
*/
|
|
150
|
+
static head(path, handler, middlewares) {
|
|
151
|
+
return super.head(path, handler, middlewares);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Defines a group of routes with a common prefix.
|
|
155
|
+
*
|
|
156
|
+
* @param prefix
|
|
157
|
+
* @param callback
|
|
158
|
+
* @param middlewares
|
|
159
|
+
*/
|
|
160
|
+
static async group(prefix, callback, middlewares) {
|
|
161
|
+
await super.group(prefix, callback, middlewares);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Apply middlewares to a group of routes defined within the callback
|
|
165
|
+
*
|
|
166
|
+
* @param middlewares - Middleware or array of middlewares to apply
|
|
167
|
+
* @param callback - Function that defines the routes to which the middlewares will be applied
|
|
168
|
+
*/
|
|
169
|
+
static middleware(middlewares, callback) {
|
|
170
|
+
super.middleware(middlewares, callback);
|
|
171
|
+
}
|
|
172
|
+
static allRoutes(type) {
|
|
173
|
+
return super.allRoutes(type);
|
|
174
|
+
}
|
|
175
|
+
static route(name) {
|
|
176
|
+
return super.route(name);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Apply the defined routes to a @koa/router instance
|
|
180
|
+
*
|
|
181
|
+
* @param router @koa/router instance
|
|
182
|
+
* @returns The @koa/router instance with the applied routes
|
|
183
|
+
*/
|
|
184
|
+
static apply(router) {
|
|
185
|
+
for (const route of Array.from(this.routes)) {
|
|
186
|
+
let handlerFunction = null;
|
|
187
|
+
let instance = null;
|
|
188
|
+
let bindingTarget;
|
|
189
|
+
let bindingMethod;
|
|
190
|
+
let bindingHandler;
|
|
191
|
+
let bindingMetadata;
|
|
192
|
+
try {
|
|
193
|
+
const resolved = this.resolveHandler(route);
|
|
194
|
+
handlerFunction = resolved.handlerFunction;
|
|
195
|
+
instance = resolved.instance;
|
|
196
|
+
bindingTarget = resolved.bindingTarget;
|
|
197
|
+
bindingMethod = resolved.bindingMethod;
|
|
198
|
+
bindingHandler = resolved.bindingHandler;
|
|
199
|
+
bindingMetadata = resolved.bindingMetadata;
|
|
200
|
+
} catch (error) {
|
|
201
|
+
console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
if (!handlerFunction) continue;
|
|
205
|
+
for (const method of route.methods) {
|
|
206
|
+
const allowedMethods = [
|
|
207
|
+
"get",
|
|
208
|
+
"post",
|
|
209
|
+
"put",
|
|
210
|
+
"delete",
|
|
211
|
+
"patch",
|
|
212
|
+
"options",
|
|
213
|
+
"head"
|
|
214
|
+
];
|
|
215
|
+
if (method === "options" && route.methods.length > 1) continue;
|
|
216
|
+
if (!allowedMethods.includes(method)) throw new Error(`Invalid HTTP method: ${method} for route: ${route.path}`);
|
|
217
|
+
for (const registrationPath of route.registrationPaths) router[method](registrationPath, ...route.middlewares || [], async (context, next) => {
|
|
218
|
+
const ctx = context;
|
|
219
|
+
const reqBody = await Router.readBodyCached(ctx);
|
|
220
|
+
const override = Router.resolveMethodOverride(ctx.method, ctx.headers, reqBody);
|
|
221
|
+
if (method === "post" && override && override !== "post") return next();
|
|
222
|
+
const inst = instance ?? route;
|
|
223
|
+
Router.bindRequestToInstance(ctx, inst, route, {
|
|
224
|
+
body: reqBody,
|
|
225
|
+
query: ctx.query,
|
|
226
|
+
params: ctx.params ?? {},
|
|
227
|
+
method
|
|
228
|
+
});
|
|
229
|
+
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
230
|
+
const resolved = await Promise.resolve(result);
|
|
231
|
+
const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
|
|
232
|
+
return Router.sendReturnValue(ctx, outgoing, method, route.path);
|
|
233
|
+
});
|
|
234
|
+
if ([
|
|
235
|
+
"put",
|
|
236
|
+
"patch",
|
|
237
|
+
"delete"
|
|
238
|
+
].includes(method)) for (const registrationPath of route.registrationPaths) router.post(registrationPath, ...route.middlewares || [], async (context, next) => {
|
|
239
|
+
const ctx = context;
|
|
240
|
+
const reqBody = await Router.readBodyCached(ctx);
|
|
241
|
+
if (Router.resolveMethodOverride(ctx.method, ctx.headers, reqBody) !== method) return next();
|
|
242
|
+
const inst = instance ?? route;
|
|
243
|
+
Router.bindRequestToInstance(ctx, inst, route, {
|
|
244
|
+
body: reqBody,
|
|
245
|
+
query: ctx.query,
|
|
246
|
+
params: ctx.params ?? {},
|
|
247
|
+
method
|
|
248
|
+
});
|
|
249
|
+
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
250
|
+
const resolved = await Promise.resolve(result);
|
|
251
|
+
const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
|
|
252
|
+
return Router.sendReturnValue(ctx, outgoing, method, route.path);
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return router;
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
//#endregion
|
|
261
|
+
export { Router as default };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
//#region src/types/basic.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Controller method reference
|
|
4
|
+
*/
|
|
5
|
+
type ControllerHandler = [any, string];
|
|
6
|
+
/**
|
|
7
|
+
* HTTP methods supported by the router
|
|
8
|
+
*/
|
|
9
|
+
type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
|
|
10
|
+
/**
|
|
11
|
+
* Common controller action names
|
|
12
|
+
*/
|
|
13
|
+
type ControllerAction = 'index' | 'show' | 'create' | 'update' | 'destroy';
|
|
14
|
+
/**
|
|
15
|
+
* Generic Object type for request data
|
|
16
|
+
*/
|
|
17
|
+
type RequestData = Record<string, any>;
|
|
18
|
+
type ApiResourceMiddleware<M = any> = M | M[] | { [K in ControllerAction]?: M | M[] };
|
|
19
|
+
interface RouterConfig {
|
|
20
|
+
/**
|
|
21
|
+
* When enabled, API param name will be infered from the route path.
|
|
22
|
+
* So instead of getting /api/users/:id, we will now get /api/users/:user
|
|
23
|
+
*/
|
|
24
|
+
inferParamName?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Configuration for method override functionality, allowing clients to use a
|
|
27
|
+
* specific header or body parameter to override the HTTP method.
|
|
28
|
+
*/
|
|
29
|
+
methodOverride?: {
|
|
30
|
+
/** Whether method override is enabled */enabled?: boolean; /** Keys in the request body to check for method override */
|
|
31
|
+
bodyKeys?: string[] | string; /** Keys in the request headers to check for method override */
|
|
32
|
+
headerKeys?: string[] | string;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Optional method binding / container resolution support. Disabled by default.
|
|
36
|
+
*/
|
|
37
|
+
container?: {
|
|
38
|
+
/** Whether decorated handler parameter binding is enabled */enabled?: boolean; /** Whether unknown constructor tokens should be instantiated automatically */
|
|
39
|
+
autoDiscover?: boolean;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
export { ApiResourceMiddleware, ControllerAction, ControllerHandler, HttpMethod, RequestData, RouterConfig };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ControllerHandler } from "./basic.cjs";
|
|
2
|
+
import { ClearHttpContext } from "../Contracts.cjs";
|
|
3
|
+
import { Response as Response$1 } from "../core/Response.cjs";
|
|
4
|
+
import { Request as Request$1 } from "../core/Request.cjs";
|
|
5
|
+
import { NextFunction, Request, Response } from "express";
|
|
6
|
+
|
|
7
|
+
//#region src/types/express.d.ts
|
|
8
|
+
interface RequestWithGetBody extends Request {
|
|
9
|
+
getBody: () => Record<string, any>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* HTTP context passed to route handlers
|
|
13
|
+
*/
|
|
14
|
+
interface HttpContext extends ClearHttpContext {
|
|
15
|
+
req: RequestWithGetBody;
|
|
16
|
+
res: Response;
|
|
17
|
+
next: NextFunction;
|
|
18
|
+
clearRequest: Request$1;
|
|
19
|
+
clearResponse: Response$1;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Route handler function type
|
|
23
|
+
*/
|
|
24
|
+
type RouteHandler = (
|
|
25
|
+
/**
|
|
26
|
+
* Express context object containing req, res, and next
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
ctx: HttpContext,
|
|
30
|
+
/**
|
|
31
|
+
* ClearRequest instance
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
req: Request$1) => any | Promise<any>;
|
|
35
|
+
/**
|
|
36
|
+
* Handler can be either a function or controller reference
|
|
37
|
+
*/
|
|
38
|
+
type Handler = RouteHandler | ControllerHandler;
|
|
39
|
+
/**
|
|
40
|
+
* Middleware function type
|
|
41
|
+
*/
|
|
42
|
+
type Middleware = (req: Request, res: Response, next: NextFunction) => any | Promise<any>;
|
|
43
|
+
//#endregion
|
|
44
|
+
export { Handler, HttpContext, Middleware };
|
package/dist/types/express.d.mts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { ControllerHandler } from "./basic.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { ClearHttpContext } from "../Contracts.mjs";
|
|
3
|
+
import { Response as Response$1 } from "../core/Response.mjs";
|
|
4
|
+
import { Request as Request$1 } from "../core/Request.mjs";
|
|
3
5
|
import { NextFunction, Request, Response } from "express";
|
|
4
6
|
|
|
5
7
|
//#region src/types/express.d.ts
|
|
@@ -39,4 +41,4 @@ type Handler = RouteHandler | ControllerHandler;
|
|
|
39
41
|
*/
|
|
40
42
|
type Middleware = (req: Request, res: Response, next: NextFunction) => any | Promise<any>;
|
|
41
43
|
//#endregion
|
|
42
|
-
export { Handler, HttpContext, Middleware
|
|
44
|
+
export { Handler, HttpContext, Middleware };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ControllerHandler } from "./basic.cjs";
|
|
2
|
+
import { ClearHttpContext } from "../Contracts.cjs";
|
|
3
|
+
import { Response } from "../core/Response.cjs";
|
|
4
|
+
import { Request } from "../core/Request.cjs";
|
|
5
|
+
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
|
6
|
+
|
|
7
|
+
//#region src/types/fastify.d.ts
|
|
8
|
+
interface RequestWithGetBody extends FastifyRequest {
|
|
9
|
+
getBody: () => Record<string, any>;
|
|
10
|
+
}
|
|
11
|
+
interface HttpContext extends ClearHttpContext {
|
|
12
|
+
req: RequestWithGetBody;
|
|
13
|
+
reply: FastifyReply;
|
|
14
|
+
clearRequest: Request;
|
|
15
|
+
clearResponse: Response;
|
|
16
|
+
}
|
|
17
|
+
type RouteHandler = (ctx: HttpContext, req: Request) => any | Promise<any>;
|
|
18
|
+
type Handler = RouteHandler | ControllerHandler;
|
|
19
|
+
type NextFunction = (err?: Error) => void;
|
|
20
|
+
type Middleware = (req: RequestWithGetBody, reply: FastifyReply, next: NextFunction) => any | Promise<any>;
|
|
21
|
+
type FastifyApp = FastifyInstance;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { FastifyApp, Handler, HttpContext, Middleware };
|
package/dist/types/fastify.d.mts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { ControllerHandler } from "./basic.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { ClearHttpContext } from "../Contracts.mjs";
|
|
3
|
+
import { Response } from "../core/Response.mjs";
|
|
4
|
+
import { Request } from "../core/Request.mjs";
|
|
3
5
|
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
|
4
6
|
|
|
5
7
|
//#region src/types/fastify.d.ts
|
|
@@ -18,4 +20,4 @@ type NextFunction = (err?: Error) => void;
|
|
|
18
20
|
type Middleware = (req: RequestWithGetBody, reply: FastifyReply, next: NextFunction) => any | Promise<any>;
|
|
19
21
|
type FastifyApp = FastifyInstance;
|
|
20
22
|
//#endregion
|
|
21
|
-
export { FastifyApp, Handler, HttpContext, Middleware
|
|
23
|
+
export { FastifyApp, Handler, HttpContext, Middleware };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ControllerHandler } from "./basic.cjs";
|
|
2
|
+
import { ClearHttpContext } from "../Contracts.cjs";
|
|
3
|
+
import { Response as Response$1 } from "../core/Response.cjs";
|
|
4
|
+
import { Request } from "../core/Request.cjs";
|
|
5
|
+
import { EventHandlerRequest, H3, H3Event, Middleware, TypedServerRequest } from "h3";
|
|
6
|
+
|
|
7
|
+
//#region src/types/h3.d.ts
|
|
8
|
+
type H3App = Omit<H3, 'fetch'> & {
|
|
9
|
+
fetch: (request: TypedServerRequest<EventHandlerRequest>) => Promise<Response>;
|
|
10
|
+
};
|
|
11
|
+
interface HttpRequest extends TypedServerRequest<EventHandlerRequest> {
|
|
12
|
+
getBody: () => Record<string, any>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* HTTP context passed to route handlers
|
|
16
|
+
*/
|
|
17
|
+
interface HttpContext extends Omit<H3Event, 'req'>, ClearHttpContext {
|
|
18
|
+
req: HttpRequest;
|
|
19
|
+
clearRequest: Request;
|
|
20
|
+
clearResponse: Response$1;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Route handler function type
|
|
24
|
+
*/
|
|
25
|
+
type RouteHandler = (
|
|
26
|
+
/**
|
|
27
|
+
* H3 event context
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
ctx: HttpContext,
|
|
31
|
+
/**
|
|
32
|
+
* ClearRequest instance
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
req: Request) => any | Promise<any>;
|
|
36
|
+
/**
|
|
37
|
+
* Handler can be either a function or controller reference
|
|
38
|
+
*/
|
|
39
|
+
type Handler = RouteHandler | ControllerHandler;
|
|
40
|
+
//#endregion
|
|
41
|
+
export { H3App, Handler, HttpContext, type Middleware };
|
package/dist/types/h3.d.mts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { ControllerHandler } from "./basic.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { ClearHttpContext } from "../Contracts.mjs";
|
|
3
|
+
import { Response as Response$1 } from "../core/Response.mjs";
|
|
4
|
+
import { Request } from "../core/Request.mjs";
|
|
3
5
|
import { EventHandlerRequest, H3, H3Event, Middleware, TypedServerRequest } from "h3";
|
|
4
6
|
|
|
5
7
|
//#region src/types/h3.d.ts
|
|
6
8
|
type H3App = Omit<H3, 'fetch'> & {
|
|
7
9
|
fetch: (request: TypedServerRequest<EventHandlerRequest>) => Promise<Response>;
|
|
8
10
|
};
|
|
9
|
-
type MaybePromise<T = unknown> = T | Promise<T>;
|
|
10
11
|
interface HttpRequest extends TypedServerRequest<EventHandlerRequest> {
|
|
11
12
|
getBody: () => Record<string, any>;
|
|
12
13
|
}
|
|
@@ -36,6 +37,5 @@ req: Request) => any | Promise<any>;
|
|
|
36
37
|
* Handler can be either a function or controller reference
|
|
37
38
|
*/
|
|
38
39
|
type Handler = RouteHandler | ControllerHandler;
|
|
39
|
-
type NextFunction = () => MaybePromise<unknown | undefined>;
|
|
40
40
|
//#endregion
|
|
41
|
-
export { H3App, Handler, HttpContext,
|
|
41
|
+
export { H3App, Handler, HttpContext, type Middleware };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ControllerHandler } from "./basic.cjs";
|
|
2
|
+
import { ClearHttpContext } from "../Contracts.cjs";
|
|
3
|
+
import { Response } from "../core/Response.cjs";
|
|
4
|
+
import { Request } from "../core/Request.cjs";
|
|
5
|
+
import { Context, HonoRequest, MiddlewareHandler } from "hono";
|
|
6
|
+
|
|
7
|
+
//#region src/types/hono.d.ts
|
|
8
|
+
type RequestWithGetBody = HonoRequest & {
|
|
9
|
+
getBody: () => Record<string, any>;
|
|
10
|
+
};
|
|
11
|
+
interface HttpContext extends Context, ClearHttpContext {
|
|
12
|
+
req: RequestWithGetBody;
|
|
13
|
+
clearRequest: Request;
|
|
14
|
+
clearResponse: Response;
|
|
15
|
+
}
|
|
16
|
+
type RouteHandler = (ctx: HttpContext, req: Request) => any | Promise<any>;
|
|
17
|
+
type Handler = RouteHandler | ControllerHandler;
|
|
18
|
+
type Middleware = MiddlewareHandler;
|
|
19
|
+
type HonoApp = { [K in 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head']: (path: string, ...handlers: Middleware[]) => any };
|
|
20
|
+
//#endregion
|
|
21
|
+
export { Handler, HonoApp, HttpContext, Middleware };
|
package/dist/types/hono.d.mts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { ControllerHandler } from "./basic.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { ClearHttpContext } from "../Contracts.mjs";
|
|
3
|
+
import { Response } from "../core/Response.mjs";
|
|
4
|
+
import { Request } from "../core/Request.mjs";
|
|
3
5
|
import { Context, HonoRequest, MiddlewareHandler } from "hono";
|
|
4
6
|
|
|
5
7
|
//#region src/types/hono.d.ts
|
|
@@ -13,8 +15,7 @@ interface HttpContext extends Context, ClearHttpContext {
|
|
|
13
15
|
}
|
|
14
16
|
type RouteHandler = (ctx: HttpContext, req: Request) => any | Promise<any>;
|
|
15
17
|
type Handler = RouteHandler | ControllerHandler;
|
|
16
|
-
type NextFunction = () => Promise<void>;
|
|
17
18
|
type Middleware = MiddlewareHandler;
|
|
18
19
|
type HonoApp = { [K in 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head']: (path: string, ...handlers: Middleware[]) => any };
|
|
19
20
|
//#endregion
|
|
20
|
-
export { Handler, HonoApp, HttpContext, Middleware
|
|
21
|
+
export { Handler, HonoApp, HttpContext, Middleware };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ControllerHandler } from "./basic.cjs";
|
|
2
|
+
import { ClearHttpContext } from "../Contracts.cjs";
|
|
3
|
+
import { Response } from "../core/Response.cjs";
|
|
4
|
+
import { Request } from "../core/Request.cjs";
|
|
5
|
+
import Koa from "koa";
|
|
6
|
+
import Router from "@koa/router";
|
|
7
|
+
|
|
8
|
+
//#region src/types/koa.d.ts
|
|
9
|
+
interface RequestWithGetBody extends Koa.Request {
|
|
10
|
+
getBody: () => Record<string, any>;
|
|
11
|
+
body?: any;
|
|
12
|
+
}
|
|
13
|
+
interface HttpContext extends Koa.Context, ClearHttpContext {
|
|
14
|
+
request: RequestWithGetBody;
|
|
15
|
+
clearRequest: Request;
|
|
16
|
+
clearResponse: Response;
|
|
17
|
+
params: Record<string, any>;
|
|
18
|
+
query: Record<string, any>;
|
|
19
|
+
}
|
|
20
|
+
type RouteHandler = (ctx: HttpContext, req: Request) => any | Promise<any>;
|
|
21
|
+
type Handler = RouteHandler | ControllerHandler;
|
|
22
|
+
type Middleware = Koa.Middleware<any, any>;
|
|
23
|
+
type KoaRouterApp = Router<any, any>;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { Handler, HttpContext, KoaRouterApp, Middleware };
|