clear-router 2.5.4 → 2.5.6
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 +5 -0
- package/dist/bindings-CV1e5jho.d.mts +20 -0
- package/dist/bindings-DIanvIVd.mjs +211 -0
- package/dist/bindings-DvV2DXWi.cjs +253 -0
- package/dist/bindings-NV0CdqGl.d.cts +20 -0
- package/dist/core/index.cjs +12 -2
- package/dist/core/index.d.cts +2 -2
- package/dist/core/index.d.mts +2 -2
- package/dist/core/index.mjs +9 -2
- package/dist/decorators/index.cjs +5 -0
- package/dist/decorators/index.d.cts +2 -0
- package/dist/decorators/index.d.mts +2 -0
- package/dist/decorators/index.mjs +3 -0
- package/dist/decorators/setup.cjs +15 -0
- package/dist/decorators/setup.d.cts +2 -0
- package/dist/decorators/setup.d.mts +2 -0
- package/dist/decorators/setup.mjs +13 -0
- package/dist/express/index.cjs +26 -9
- package/dist/express/index.d.cts +1 -1
- package/dist/express/index.d.mts +1 -1
- package/dist/express/index.mjs +26 -9
- package/dist/fastify/index.cjs +24 -8
- package/dist/fastify/index.d.cts +4 -2
- package/dist/fastify/index.d.mts +4 -2
- package/dist/fastify/index.mjs +24 -8
- package/dist/h3/index.cjs +26 -9
- package/dist/h3/index.d.cts +1 -1
- package/dist/h3/index.d.mts +1 -1
- package/dist/h3/index.mjs +26 -9
- package/dist/hono/index.cjs +28 -11
- package/dist/hono/index.d.cts +4 -2
- package/dist/hono/index.d.mts +4 -2
- package/dist/hono/index.mjs +28 -11
- package/dist/index.cjs +308 -28
- package/dist/index.d.cts +112 -18
- package/dist/index.d.mts +112 -18
- package/dist/index.mjs +305 -28
- package/dist/koa/index.cjs +24 -8
- package/dist/koa/index.d.cts +4 -2
- package/dist/koa/index.d.mts +4 -2
- package/dist/koa/index.mjs +24 -8
- package/dist/{responses-B-UirWFf.cjs → responses-JzXstGU5.cjs} +18 -1
- package/dist/{responses-CJaD0ugv.mjs → responses-_II3dOJ5.mjs} +19 -1
- package/dist/{router-BReOXz-F.mjs → router-B3QjblRX.mjs} +159 -48
- package/dist/{router-CS_2XQ7I.d.cts → router-BYZmNzrZ.d.cts} +97 -12
- package/dist/{router-CHg0pZUO.cjs → router-CU4V1kX0.cjs} +159 -48
- package/dist/{router-_w2VzMQF.d.mts → router-DCMtQ_Xi.d.mts} +96 -11
- package/dist/types/basic.d.mts +7 -0
- package/dist/types/core/Request.d.mts +25 -0
- package/dist/types/core/Response.d.mts +21 -0
- package/dist/types/express.d.mts +5 -2
- package/dist/types/fastify.d.mts +5 -2
- package/dist/types/h3.d.mts +5 -2
- package/dist/types/hono.d.mts +5 -2
- package/dist/types/koa.d.mts +5 -2
- package/package.json +16 -2
package/dist/koa/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { _ as ApiResourceMiddleware, b as HttpMethod, c as Request, l as Route, t as CoreRouter, v as ControllerAction, x as Response, y as ControllerHandler } from "../router-DCMtQ_Xi.mjs";
|
|
2
2
|
import Koa from "koa";
|
|
3
3
|
import Router$1 from "@koa/router";
|
|
4
4
|
|
|
@@ -9,10 +9,12 @@ interface RequestWithGetBody extends Koa.Request {
|
|
|
9
9
|
}
|
|
10
10
|
interface HttpContext extends Koa.Context {
|
|
11
11
|
request: RequestWithGetBody;
|
|
12
|
+
clearRequest: Request;
|
|
13
|
+
clearResponse: Response;
|
|
12
14
|
params: Record<string, any>;
|
|
13
15
|
query: Record<string, any>;
|
|
14
16
|
}
|
|
15
|
-
type RouteHandler = (ctx: HttpContext, req:
|
|
17
|
+
type RouteHandler = (ctx: HttpContext, req: Request) => any | Promise<any>;
|
|
16
18
|
type Handler = RouteHandler | ControllerHandler;
|
|
17
19
|
type Middleware = Koa.Middleware<any, any>;
|
|
18
20
|
type KoaRouterApp = Router$1<any, any>;
|
package/dist/koa/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import "../bindings-DIanvIVd.mjs";
|
|
2
|
+
import { t as CoreRouter } from "../router-B3QjblRX.mjs";
|
|
3
|
+
import { n as resolveResponseMeta, t as isFetchResponse } from "../responses-_II3dOJ5.mjs";
|
|
3
4
|
|
|
4
5
|
//#region src/koa/router.ts
|
|
5
6
|
/**
|
|
@@ -43,6 +44,9 @@ var Router = class Router extends CoreRouter {
|
|
|
43
44
|
});
|
|
44
45
|
if (!meta) return void 0;
|
|
45
46
|
ctx.status = meta.status;
|
|
47
|
+
meta.headers?.forEach((headerValue, key) => {
|
|
48
|
+
ctx.set(key, headerValue);
|
|
49
|
+
});
|
|
46
50
|
if (isFetchResponse(meta.body)) {
|
|
47
51
|
meta.body.headers.forEach((headerValue, key) => {
|
|
48
52
|
ctx.set(key, headerValue);
|
|
@@ -179,10 +183,18 @@ var Router = class Router extends CoreRouter {
|
|
|
179
183
|
for (const route of this.routes) {
|
|
180
184
|
let handlerFunction = null;
|
|
181
185
|
let instance = null;
|
|
186
|
+
let bindingTarget;
|
|
187
|
+
let bindingMethod;
|
|
188
|
+
let bindingHandler;
|
|
189
|
+
let bindingMetadata;
|
|
182
190
|
try {
|
|
183
191
|
const resolved = this.resolveHandler(route);
|
|
184
192
|
handlerFunction = resolved.handlerFunction;
|
|
185
193
|
instance = resolved.instance;
|
|
194
|
+
bindingTarget = resolved.bindingTarget;
|
|
195
|
+
bindingMethod = resolved.bindingMethod;
|
|
196
|
+
bindingHandler = resolved.bindingHandler;
|
|
197
|
+
bindingMetadata = resolved.bindingMetadata;
|
|
186
198
|
} catch (error) {
|
|
187
199
|
console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
|
|
188
200
|
throw error;
|
|
@@ -209,11 +221,13 @@ var Router = class Router extends CoreRouter {
|
|
|
209
221
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
210
222
|
body: reqBody,
|
|
211
223
|
query: ctx.query,
|
|
212
|
-
params: ctx.params ?? {}
|
|
224
|
+
params: ctx.params ?? {},
|
|
225
|
+
method
|
|
213
226
|
});
|
|
214
|
-
const result = handlerFunction
|
|
227
|
+
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
215
228
|
const resolved = await Promise.resolve(result);
|
|
216
|
-
|
|
229
|
+
const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
|
|
230
|
+
return Router.sendReturnValue(ctx, outgoing, method, route.path);
|
|
217
231
|
});
|
|
218
232
|
if ([
|
|
219
233
|
"put",
|
|
@@ -227,11 +241,13 @@ var Router = class Router extends CoreRouter {
|
|
|
227
241
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
228
242
|
body: reqBody,
|
|
229
243
|
query: ctx.query,
|
|
230
|
-
params: ctx.params ?? {}
|
|
244
|
+
params: ctx.params ?? {},
|
|
245
|
+
method
|
|
231
246
|
});
|
|
232
|
-
const result = handlerFunction
|
|
247
|
+
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
233
248
|
const resolved = await Promise.resolve(result);
|
|
234
|
-
|
|
249
|
+
const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
|
|
250
|
+
return Router.sendReturnValue(ctx, outgoing, method, route.path);
|
|
235
251
|
});
|
|
236
252
|
}
|
|
237
253
|
}
|
|
@@ -1,12 +1,17 @@
|
|
|
1
|
+
const require_bindings = require('./bindings-DvV2DXWi.cjs');
|
|
1
2
|
|
|
2
3
|
//#region src/core/responses.ts
|
|
3
4
|
function isFetchResponse(value) {
|
|
4
|
-
return typeof Response !== "undefined" && value instanceof Response;
|
|
5
|
+
return typeof globalThis.Response !== "undefined" && value instanceof globalThis.Response;
|
|
6
|
+
}
|
|
7
|
+
function isCoreResponse(value) {
|
|
8
|
+
return value instanceof require_bindings.Response;
|
|
5
9
|
}
|
|
6
10
|
function isH3Response(value) {
|
|
7
11
|
return Boolean(value && typeof value === "object" && value.constructor?.name === "HTTPResponse");
|
|
8
12
|
}
|
|
9
13
|
function responseWasSent(target) {
|
|
14
|
+
if (isCoreResponse(target)) return false;
|
|
10
15
|
return Boolean(target?.headersSent || target?.sent || target?.raw?.headersSent);
|
|
11
16
|
}
|
|
12
17
|
function getHeader(headers, name) {
|
|
@@ -23,6 +28,17 @@ function isApiRequest(headers, path) {
|
|
|
23
28
|
}
|
|
24
29
|
function resolveResponseMeta(value, options = {}) {
|
|
25
30
|
if (typeof value === "undefined") return void 0;
|
|
31
|
+
if (isCoreResponse(value)) {
|
|
32
|
+
const contentType = value.headers.get("content-type") || void 0;
|
|
33
|
+
return {
|
|
34
|
+
body: value.body,
|
|
35
|
+
status: value.statusCode,
|
|
36
|
+
contentType,
|
|
37
|
+
headers: value.headers,
|
|
38
|
+
isEmpty: value.body === null || typeof value.body === "undefined",
|
|
39
|
+
isNativeResponse: false
|
|
40
|
+
};
|
|
41
|
+
}
|
|
26
42
|
if (isFetchResponse(value) || isH3Response(value)) return {
|
|
27
43
|
body: value,
|
|
28
44
|
status: getStatus(value, options.method, options.status),
|
|
@@ -59,6 +75,7 @@ function resolveResponseMeta(value, options = {}) {
|
|
|
59
75
|
};
|
|
60
76
|
}
|
|
61
77
|
function getStatus(value, method, explicitStatus) {
|
|
78
|
+
if (typeof value?.statusCode === "number" && value.statusCode >= 100 && value.statusCode <= 999) return value.statusCode;
|
|
62
79
|
if (typeof value?.status === "number" && value.status >= 100 && value.status <= 999) return value.status;
|
|
63
80
|
if (typeof explicitStatus === "number" && explicitStatus >= 100 && explicitStatus <= 999) return explicitStatus;
|
|
64
81
|
return String(method || "").toLowerCase() === "post" ? 201 : 200;
|
|
@@ -1,11 +1,17 @@
|
|
|
1
|
+
import { o as Response } from "./bindings-DIanvIVd.mjs";
|
|
2
|
+
|
|
1
3
|
//#region src/core/responses.ts
|
|
2
4
|
function isFetchResponse(value) {
|
|
3
|
-
return typeof Response !== "undefined" && value instanceof Response;
|
|
5
|
+
return typeof globalThis.Response !== "undefined" && value instanceof globalThis.Response;
|
|
6
|
+
}
|
|
7
|
+
function isCoreResponse(value) {
|
|
8
|
+
return value instanceof Response;
|
|
4
9
|
}
|
|
5
10
|
function isH3Response(value) {
|
|
6
11
|
return Boolean(value && typeof value === "object" && value.constructor?.name === "HTTPResponse");
|
|
7
12
|
}
|
|
8
13
|
function responseWasSent(target) {
|
|
14
|
+
if (isCoreResponse(target)) return false;
|
|
9
15
|
return Boolean(target?.headersSent || target?.sent || target?.raw?.headersSent);
|
|
10
16
|
}
|
|
11
17
|
function getHeader(headers, name) {
|
|
@@ -22,6 +28,17 @@ function isApiRequest(headers, path) {
|
|
|
22
28
|
}
|
|
23
29
|
function resolveResponseMeta(value, options = {}) {
|
|
24
30
|
if (typeof value === "undefined") return void 0;
|
|
31
|
+
if (isCoreResponse(value)) {
|
|
32
|
+
const contentType = value.headers.get("content-type") || void 0;
|
|
33
|
+
return {
|
|
34
|
+
body: value.body,
|
|
35
|
+
status: value.statusCode,
|
|
36
|
+
contentType,
|
|
37
|
+
headers: value.headers,
|
|
38
|
+
isEmpty: value.body === null || typeof value.body === "undefined",
|
|
39
|
+
isNativeResponse: false
|
|
40
|
+
};
|
|
41
|
+
}
|
|
25
42
|
if (isFetchResponse(value) || isH3Response(value)) return {
|
|
26
43
|
body: value,
|
|
27
44
|
status: getStatus(value, options.method, options.status),
|
|
@@ -58,6 +75,7 @@ function resolveResponseMeta(value, options = {}) {
|
|
|
58
75
|
};
|
|
59
76
|
}
|
|
60
77
|
function getStatus(value, method, explicitStatus) {
|
|
78
|
+
if (typeof value?.statusCode === "number" && value.statusCode >= 100 && value.statusCode <= 999) return value.statusCode;
|
|
61
79
|
if (typeof value?.status === "number" && value.status >= 100 && value.status <= 999) return value.status;
|
|
62
80
|
if (typeof explicitStatus === "number" && explicitStatus >= 100 && explicitStatus <= 999) return explicitStatus;
|
|
63
81
|
return String(method || "").toLowerCase() === "post" ? 201 : 200;
|
|
@@ -1,26 +1,6 @@
|
|
|
1
|
+
import { a as getStandardMetadata, i as getDesignParamTypes, n as Container, o as Response, r as getBindingMetadataFromTargets, s as Request } from "./bindings-DIanvIVd.mjs";
|
|
1
2
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
3
|
|
|
3
|
-
//#region src/ClearRequest.ts
|
|
4
|
-
var ClearRequest = class {
|
|
5
|
-
/**
|
|
6
|
-
* @param body - Parsed request body
|
|
7
|
-
*/
|
|
8
|
-
body;
|
|
9
|
-
/**
|
|
10
|
-
* @param query - Parsed query parameters
|
|
11
|
-
*/
|
|
12
|
-
query;
|
|
13
|
-
/**
|
|
14
|
-
* @param params - Parsed route parameters
|
|
15
|
-
*/
|
|
16
|
-
params;
|
|
17
|
-
route;
|
|
18
|
-
constructor(init) {
|
|
19
|
-
Object.assign(this, init);
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
//#endregion
|
|
24
4
|
//#region src/Route.ts
|
|
25
5
|
var Route = class {
|
|
26
6
|
ctx;
|
|
@@ -60,6 +40,41 @@ var CoreRouter = class {
|
|
|
60
40
|
static routerStateNamespace = "clear-router:core";
|
|
61
41
|
static stateStoreKey = Symbol.for("clear-router:router-state");
|
|
62
42
|
static stateBoundKey = Symbol.for("clear-router:router-state-bound");
|
|
43
|
+
static defaultConfigKey = Symbol.for("clear-router:default-config");
|
|
44
|
+
static pluginStoreKey = Symbol.for("clear-router:plugins");
|
|
45
|
+
static createBaseConfig() {
|
|
46
|
+
return {
|
|
47
|
+
methodOverride: {
|
|
48
|
+
enabled: true,
|
|
49
|
+
bodyKeys: ["_method"],
|
|
50
|
+
headerKeys: ["x-http-method"]
|
|
51
|
+
},
|
|
52
|
+
container: {
|
|
53
|
+
enabled: false,
|
|
54
|
+
autoDiscover: false
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
static mergeConfig(target, source) {
|
|
59
|
+
if (!source) return target;
|
|
60
|
+
if (source.methodOverride) target.methodOverride = {
|
|
61
|
+
...target.methodOverride || {},
|
|
62
|
+
...source.methodOverride
|
|
63
|
+
};
|
|
64
|
+
if (source.container) target.container = {
|
|
65
|
+
...target.container || {},
|
|
66
|
+
...source.container
|
|
67
|
+
};
|
|
68
|
+
return target;
|
|
69
|
+
}
|
|
70
|
+
static getDefaultConfig() {
|
|
71
|
+
const g = globalThis;
|
|
72
|
+
if (!g[this.defaultConfigKey]) g[this.defaultConfigKey] = this.createBaseConfig();
|
|
73
|
+
return {
|
|
74
|
+
methodOverride: { ...g[this.defaultConfigKey].methodOverride },
|
|
75
|
+
container: { ...g[this.defaultConfigKey].container }
|
|
76
|
+
};
|
|
77
|
+
}
|
|
63
78
|
static resolveStateNamespace() {
|
|
64
79
|
return String(this.routerStateNamespace || this.name || "clear-router:core");
|
|
65
80
|
}
|
|
@@ -68,13 +83,14 @@ var CoreRouter = class {
|
|
|
68
83
|
if (!g[this.stateStoreKey]) g[this.stateStoreKey] = Object.create(null);
|
|
69
84
|
return g[this.stateStoreKey];
|
|
70
85
|
}
|
|
86
|
+
static getPluginStore() {
|
|
87
|
+
const g = globalThis;
|
|
88
|
+
if (!g[this.pluginStoreKey]) g[this.pluginStoreKey] = /* @__PURE__ */ new Set();
|
|
89
|
+
return g[this.pluginStoreKey];
|
|
90
|
+
}
|
|
71
91
|
static createDefaultState() {
|
|
72
92
|
return {
|
|
73
|
-
config:
|
|
74
|
-
enabled: true,
|
|
75
|
-
bodyKeys: ["_method"],
|
|
76
|
-
headerKeys: ["x-http-method"]
|
|
77
|
-
} },
|
|
93
|
+
config: this.getDefaultConfig(),
|
|
78
94
|
groupContext: new AsyncLocalStorage(),
|
|
79
95
|
routes: [],
|
|
80
96
|
routesByPathMethod: {},
|
|
@@ -151,11 +167,47 @@ var CoreRouter = class {
|
|
|
151
167
|
}
|
|
152
168
|
};
|
|
153
169
|
}
|
|
154
|
-
static config = {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
170
|
+
static config = {
|
|
171
|
+
methodOverride: {
|
|
172
|
+
enabled: true,
|
|
173
|
+
bodyKeys: ["_method"],
|
|
174
|
+
headerKeys: ["x-http-method"]
|
|
175
|
+
},
|
|
176
|
+
container: {
|
|
177
|
+
enabled: false,
|
|
178
|
+
autoDiscover: false
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
static configureDefaults(options) {
|
|
182
|
+
const g = globalThis;
|
|
183
|
+
const defaults = this.mergeConfig(g[this.defaultConfigKey] || this.createBaseConfig(), options);
|
|
184
|
+
g[this.defaultConfigKey] = defaults;
|
|
185
|
+
const store = this.getStateStore();
|
|
186
|
+
for (const state of Object.values(store)) state.config = this.mergeConfig(state.config || this.createBaseConfig(), options);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Use a registered plugin
|
|
190
|
+
*
|
|
191
|
+
* @param this
|
|
192
|
+
* @param plugin
|
|
193
|
+
* @param options
|
|
194
|
+
* @returns
|
|
195
|
+
*/
|
|
196
|
+
static use(plugin, options) {
|
|
197
|
+
const name = typeof plugin === "function" ? plugin.name : plugin.name;
|
|
198
|
+
const store = this.getPluginStore();
|
|
199
|
+
if (name && store.has(name)) return;
|
|
200
|
+
const ctx = {
|
|
201
|
+
container: Container,
|
|
202
|
+
bind: Container.bind.bind(Container),
|
|
203
|
+
configure: this.configure.bind(this),
|
|
204
|
+
configureDefaults: this.configureDefaults.bind(this),
|
|
205
|
+
options
|
|
206
|
+
};
|
|
207
|
+
if (typeof plugin === "function") plugin(ctx);
|
|
208
|
+
else plugin.setup(ctx);
|
|
209
|
+
if (name) store.add(name);
|
|
210
|
+
}
|
|
159
211
|
static groupContext = new AsyncLocalStorage();
|
|
160
212
|
static routes = [];
|
|
161
213
|
static routesByPathMethod = {};
|
|
@@ -197,11 +249,12 @@ var CoreRouter = class {
|
|
|
197
249
|
*/
|
|
198
250
|
static configure(options) {
|
|
199
251
|
this.ensureState();
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
252
|
+
this.config = this.mergeConfig(this.getDefaultConfig(), this.config);
|
|
253
|
+
const container = options?.container;
|
|
254
|
+
if (container) {
|
|
255
|
+
if (typeof container.enabled === "boolean") this.config.container.enabled = container.enabled;
|
|
256
|
+
if (typeof container.autoDiscover === "boolean") this.config.container.autoDiscover = container.autoDiscover;
|
|
257
|
+
}
|
|
205
258
|
const override = options?.methodOverride;
|
|
206
259
|
if (!override) return;
|
|
207
260
|
if (typeof override.enabled === "boolean") this.config.methodOverride.enabled = override.enabled;
|
|
@@ -438,36 +491,94 @@ var CoreRouter = class {
|
|
|
438
491
|
static resolveHandler(route) {
|
|
439
492
|
let handlerFunction;
|
|
440
493
|
let instance = null;
|
|
441
|
-
|
|
442
|
-
|
|
494
|
+
let bindingTarget;
|
|
495
|
+
let bindingMethod;
|
|
496
|
+
let bindingHandler;
|
|
497
|
+
let bindingMetadata;
|
|
498
|
+
if (typeof route.handler === "function") {
|
|
499
|
+
handlerFunction = route.handler.bind(route);
|
|
500
|
+
bindingTarget = route.handler;
|
|
501
|
+
bindingHandler = route.handler;
|
|
502
|
+
} else if (Array.isArray(route.handler) && route.handler.length === 2) {
|
|
443
503
|
const [ControllerType, method] = route.handler;
|
|
444
504
|
if (["function", "object"].includes(typeof ControllerType) && typeof ControllerType[method] === "function") {
|
|
445
505
|
instance = ControllerType;
|
|
446
506
|
handlerFunction = ControllerType[method].bind(ControllerType);
|
|
507
|
+
bindingTarget = ControllerType;
|
|
508
|
+
bindingMethod = method;
|
|
509
|
+
bindingHandler = ControllerType[method];
|
|
510
|
+
bindingMetadata = ControllerType[Symbol.metadata];
|
|
447
511
|
} else if (typeof ControllerType === "function") {
|
|
448
512
|
instance = new ControllerType();
|
|
449
|
-
if (typeof instance[method] === "function")
|
|
450
|
-
|
|
513
|
+
if (typeof instance[method] === "function") {
|
|
514
|
+
handlerFunction = instance[method].bind(instance);
|
|
515
|
+
bindingTarget = ControllerType.prototype;
|
|
516
|
+
bindingMethod = method;
|
|
517
|
+
bindingHandler = instance[method];
|
|
518
|
+
bindingMetadata = ControllerType[Symbol.metadata];
|
|
519
|
+
} else throw new Error(`Method "${method}" not found in controller instance "${ControllerType.name}"`);
|
|
451
520
|
} else throw new Error(`Invalid controller type for route: ${route.path}`);
|
|
452
521
|
} else throw new Error(`Invalid handler format for route: ${route.path}`);
|
|
453
522
|
return {
|
|
454
523
|
handlerFunction,
|
|
455
|
-
instance
|
|
524
|
+
instance,
|
|
525
|
+
bindingTarget,
|
|
526
|
+
bindingMethod,
|
|
527
|
+
bindingHandler,
|
|
528
|
+
bindingMetadata
|
|
456
529
|
};
|
|
457
530
|
}
|
|
531
|
+
static async callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata) {
|
|
532
|
+
if (!this.config.container?.enabled) return handlerFunction(ctx, ctx.clearRequest);
|
|
533
|
+
const metadata = getBindingMetadataFromTargets([
|
|
534
|
+
{
|
|
535
|
+
target: bindingTarget,
|
|
536
|
+
propertyKey: bindingMethod
|
|
537
|
+
},
|
|
538
|
+
{ target: bindingHandler },
|
|
539
|
+
{
|
|
540
|
+
target: bindingTarget,
|
|
541
|
+
propertyKey: "__class__"
|
|
542
|
+
}
|
|
543
|
+
]) ?? getStandardMetadata(bindingMetadata, bindingMethod) ?? getStandardMetadata(bindingMetadata, "__class__");
|
|
544
|
+
if (!metadata) return handlerFunction(ctx, ctx.clearRequest);
|
|
545
|
+
const designTokens = [...bindingTarget ? getDesignParamTypes(bindingTarget, bindingMethod) : [], ...bindingHandler ? getDesignParamTypes(bindingHandler) : []];
|
|
546
|
+
const tokens = metadata.tokens?.length ? metadata.tokens : designTokens;
|
|
547
|
+
if (!tokens.length) return handlerFunction(ctx, ctx.clearRequest);
|
|
548
|
+
const args = [];
|
|
549
|
+
for (const token of tokens) {
|
|
550
|
+
const resolved = await Container.resolve(token, ctx, Boolean(this.config.container?.autoDiscover));
|
|
551
|
+
if (typeof resolved === "undefined") return handlerFunction(ctx, ctx.clearRequest);
|
|
552
|
+
args.push(resolved);
|
|
553
|
+
}
|
|
554
|
+
return handlerFunction(...args);
|
|
555
|
+
}
|
|
458
556
|
static bindRequestToInstance(ctx, instance, route, payload) {
|
|
557
|
+
const clearRequest = ctx.clearRequest instanceof Request ? ctx.clearRequest : new Request({
|
|
558
|
+
ctx,
|
|
559
|
+
route,
|
|
560
|
+
body: payload.body,
|
|
561
|
+
query: payload.query,
|
|
562
|
+
params: payload.params,
|
|
563
|
+
method: String(payload.method || ctx.req?.method || ctx.method || "GET").toUpperCase(),
|
|
564
|
+
path: String(ctx.path || ctx.req?.path || ctx.req?.url || route.path),
|
|
565
|
+
url: String(ctx.url || ctx.req?.url || ctx.req?.originalUrl || route.path),
|
|
566
|
+
headers: ctx.req?.headers || ctx.headers || {},
|
|
567
|
+
original: ctx.req || ctx.request || ctx
|
|
568
|
+
});
|
|
569
|
+
clearRequest.ctx = ctx;
|
|
570
|
+
clearRequest.route = route;
|
|
571
|
+
clearRequest.body = payload.body;
|
|
572
|
+
clearRequest.query = payload.query;
|
|
573
|
+
clearRequest.params = payload.params;
|
|
574
|
+
ctx.clearRequest = clearRequest;
|
|
575
|
+
if (!(ctx.clearResponse instanceof Response)) ctx.clearResponse = new Response();
|
|
459
576
|
if (!instance) return;
|
|
460
577
|
instance.ctx = ctx;
|
|
461
578
|
instance.body = payload.body;
|
|
462
579
|
instance.query = payload.query;
|
|
463
580
|
instance.params = payload.params;
|
|
464
|
-
instance.clearRequest =
|
|
465
|
-
ctx,
|
|
466
|
-
route,
|
|
467
|
-
body: instance.body,
|
|
468
|
-
query: instance.query,
|
|
469
|
-
params: instance.params
|
|
470
|
-
});
|
|
581
|
+
instance.clearRequest = clearRequest;
|
|
471
582
|
}
|
|
472
583
|
};
|
|
473
584
|
|
|
@@ -1,7 +1,28 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as BindValue, i as BindToken, o as Container } from "./bindings-NV0CdqGl.cjs";
|
|
2
2
|
import { NextFunction, Request, Response as Response$1 } from "express";
|
|
3
3
|
import { EventHandlerRequest, H3, H3Event, Middleware, TypedServerRequest } from "h3";
|
|
4
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
5
|
|
|
6
|
+
//#region src/core/Response.d.ts
|
|
7
|
+
declare class Response$2 {
|
|
8
|
+
body: any;
|
|
9
|
+
headers: Headers;
|
|
10
|
+
sent: boolean;
|
|
11
|
+
statusCode: number;
|
|
12
|
+
constructor(init?: Partial<Response$2>);
|
|
13
|
+
status(code: number): this;
|
|
14
|
+
code(code: number): this;
|
|
15
|
+
setHeader(name: string, value: string): this;
|
|
16
|
+
header(name: string, value: string): this;
|
|
17
|
+
set(name: string, value: string): this;
|
|
18
|
+
type(contentType: string): this;
|
|
19
|
+
send(body?: any): this;
|
|
20
|
+
json(body: any): this;
|
|
21
|
+
html(body: string): this;
|
|
22
|
+
text(body: string): this;
|
|
23
|
+
noContent(): this;
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
5
26
|
//#region types/basic.d.ts
|
|
6
27
|
/**
|
|
7
28
|
* Controller method reference
|
|
@@ -30,6 +51,13 @@ interface RouterConfig {
|
|
|
30
51
|
bodyKeys?: string[] | string; /** Keys in the request headers to check for method override */
|
|
31
52
|
headerKeys?: string[] | string;
|
|
32
53
|
};
|
|
54
|
+
/**
|
|
55
|
+
* Optional method binding / container resolution support. Disabled by default.
|
|
56
|
+
*/
|
|
57
|
+
container?: {
|
|
58
|
+
/** Whether decorated handler parameter binding is enabled */enabled?: boolean; /** Whether unknown constructor tokens should be instantiated automatically */
|
|
59
|
+
autoDiscover?: boolean;
|
|
60
|
+
};
|
|
33
61
|
}
|
|
34
62
|
//#endregion
|
|
35
63
|
//#region types/express.d.ts
|
|
@@ -43,6 +71,8 @@ interface HttpContext$1 {
|
|
|
43
71
|
req: RequestWithGetBody;
|
|
44
72
|
res: Response$1;
|
|
45
73
|
next: NextFunction;
|
|
74
|
+
clearRequest: Request$1;
|
|
75
|
+
clearResponse: Response$2;
|
|
46
76
|
}
|
|
47
77
|
/**
|
|
48
78
|
* Route handler function type
|
|
@@ -57,7 +87,7 @@ ctx: HttpContext$1,
|
|
|
57
87
|
* ClearRequest instance
|
|
58
88
|
*/
|
|
59
89
|
|
|
60
|
-
req:
|
|
90
|
+
req: Request$1) => any | Promise<any>;
|
|
61
91
|
/**
|
|
62
92
|
* Handler can be either a function or controller reference
|
|
63
93
|
*/
|
|
@@ -80,6 +110,8 @@ type RequestlessH3Event = Omit<H3Event, 'req'>;
|
|
|
80
110
|
*/
|
|
81
111
|
interface HttpContext extends RequestlessH3Event {
|
|
82
112
|
req: HttpRequest;
|
|
113
|
+
clearRequest: Request$1;
|
|
114
|
+
clearResponse: Response$2;
|
|
83
115
|
}
|
|
84
116
|
/**
|
|
85
117
|
* Route handler function type
|
|
@@ -94,7 +126,7 @@ ctx: HttpContext,
|
|
|
94
126
|
* ClearRequest instance
|
|
95
127
|
*/
|
|
96
128
|
|
|
97
|
-
req:
|
|
129
|
+
req: Request$1) => any | Promise<any>;
|
|
98
130
|
/**
|
|
99
131
|
* Handler can be either a function or controller reference
|
|
100
132
|
*/
|
|
@@ -137,6 +169,43 @@ declare class ClearRequest<X = any, M = Middleware | Middleware$1> {
|
|
|
137
169
|
constructor(init?: Partial<ClearRequest>);
|
|
138
170
|
}
|
|
139
171
|
//#endregion
|
|
172
|
+
//#region src/core/Request.d.ts
|
|
173
|
+
declare class Request$1<X = any, M = any> extends ClearRequest<X, M> {
|
|
174
|
+
original?: any;
|
|
175
|
+
method: string;
|
|
176
|
+
path: string;
|
|
177
|
+
url: string;
|
|
178
|
+
headers: Headers | Record<string, any>;
|
|
179
|
+
constructor(init?: Partial<Request$1<X, M>> & {
|
|
180
|
+
body?: RequestData;
|
|
181
|
+
query?: RequestData;
|
|
182
|
+
params?: RequestData;
|
|
183
|
+
route?: Route<X, M>;
|
|
184
|
+
});
|
|
185
|
+
getBody(): RequestData;
|
|
186
|
+
header(name: string): string;
|
|
187
|
+
param(name: string): any;
|
|
188
|
+
input(name: string): any;
|
|
189
|
+
is(method: HttpMethod | string): boolean;
|
|
190
|
+
}
|
|
191
|
+
//#endregion
|
|
192
|
+
//#region src/core/plugins.d.ts
|
|
193
|
+
type PluginSetupResult = void;
|
|
194
|
+
type PluginBind = <T>(token: BindToken<T>, value: BindValue<T>) => void;
|
|
195
|
+
interface ClearRouterPluginContext<Options = any> {
|
|
196
|
+
container: typeof Container;
|
|
197
|
+
bind: PluginBind;
|
|
198
|
+
configure: (options: RouterConfig) => void;
|
|
199
|
+
configureDefaults: (options: RouterConfig) => void;
|
|
200
|
+
options: Options;
|
|
201
|
+
}
|
|
202
|
+
interface ClearRouterPlugin<Options = any> {
|
|
203
|
+
name?: string;
|
|
204
|
+
setup: (ctx: ClearRouterPluginContext<Options>) => PluginSetupResult;
|
|
205
|
+
}
|
|
206
|
+
type ClearRouterPluginInput<Options = any> = ClearRouterPlugin<Options> | ((ctx: ClearRouterPluginContext<Options>) => PluginSetupResult);
|
|
207
|
+
declare function definePlugin<Options = any>(plugin: ClearRouterPlugin<Options>): ClearRouterPlugin<Options>;
|
|
208
|
+
//#endregion
|
|
140
209
|
//#region src/Controller.d.ts
|
|
141
210
|
declare abstract class Controller<X = any> {
|
|
142
211
|
[x: string]: any;
|
|
@@ -158,16 +227,16 @@ declare abstract class CoreRouter {
|
|
|
158
227
|
protected static routerStateNamespace: string;
|
|
159
228
|
private static readonly stateStoreKey;
|
|
160
229
|
private static readonly stateBoundKey;
|
|
230
|
+
private static readonly defaultConfigKey;
|
|
231
|
+
private static readonly pluginStoreKey;
|
|
232
|
+
protected static createBaseConfig(): RouterConfig;
|
|
233
|
+
protected static mergeConfig(target: RouterConfig, source?: RouterConfig): RouterConfig;
|
|
234
|
+
protected static getDefaultConfig(): RouterConfig;
|
|
161
235
|
protected static resolveStateNamespace(this: any): string;
|
|
162
236
|
protected static getStateStore(): Record<string, any>;
|
|
237
|
+
protected static getPluginStore(): Set<string>;
|
|
163
238
|
protected static createDefaultState(): {
|
|
164
|
-
config:
|
|
165
|
-
methodOverride: {
|
|
166
|
-
enabled: boolean;
|
|
167
|
-
bodyKeys: string[];
|
|
168
|
-
headerKeys: string[];
|
|
169
|
-
};
|
|
170
|
-
};
|
|
239
|
+
config: RouterConfig;
|
|
171
240
|
groupContext: AsyncLocalStorage<{
|
|
172
241
|
prefix: string;
|
|
173
242
|
groupMiddlewares: any[];
|
|
@@ -182,6 +251,16 @@ declare abstract class CoreRouter {
|
|
|
182
251
|
protected static bindStateAccessors(this: any): void;
|
|
183
252
|
protected static createDefaultOptionsHandler(): any;
|
|
184
253
|
static config: RouterConfig;
|
|
254
|
+
static configureDefaults(this: any, options?: RouterConfig): void;
|
|
255
|
+
/**
|
|
256
|
+
* Use a registered plugin
|
|
257
|
+
*
|
|
258
|
+
* @param this
|
|
259
|
+
* @param plugin
|
|
260
|
+
* @param options
|
|
261
|
+
* @returns
|
|
262
|
+
*/
|
|
263
|
+
static use<Options = any>(this: any, plugin: ClearRouterPluginInput<Options>, options?: Options): void;
|
|
185
264
|
protected static groupContext: AsyncLocalStorage<{
|
|
186
265
|
prefix: string;
|
|
187
266
|
groupMiddlewares: any[];
|
|
@@ -331,14 +410,20 @@ declare abstract class CoreRouter {
|
|
|
331
410
|
*/
|
|
332
411
|
static allRoutes(this: any, type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
|
|
333
412
|
protected static resolveHandler(route: Route<any, any, any>): {
|
|
334
|
-
handlerFunction: ((ctx: any, req:
|
|
413
|
+
handlerFunction: ((ctx: any, req: Request$1) => any | Promise<any>) | null;
|
|
335
414
|
instance: Controller<any> | null;
|
|
415
|
+
bindingTarget?: object;
|
|
416
|
+
bindingMethod?: PropertyKey;
|
|
417
|
+
bindingHandler?: object;
|
|
418
|
+
bindingMetadata?: object;
|
|
336
419
|
};
|
|
420
|
+
protected static callHandler(this: any, handlerFunction: (ctx: any, req: Request$1) => any | Promise<any>, ctx: any, bindingTarget?: object, bindingMethod?: PropertyKey, bindingHandler?: object, bindingMetadata?: object): Promise<any>;
|
|
337
421
|
protected static bindRequestToInstance(ctx: any, instance: Controller<any> | Route<any, any, any> | null, route: Route<any, any, any>, payload: {
|
|
338
422
|
body: Record<string, any>;
|
|
339
423
|
query: Record<string, any>;
|
|
340
424
|
params: Record<string, any>;
|
|
425
|
+
method?: HttpMethod | string;
|
|
341
426
|
}): void;
|
|
342
427
|
}
|
|
343
428
|
//#endregion
|
|
344
|
-
export {
|
|
429
|
+
export { ApiResourceMiddleware as _, PluginBind as a, HttpMethod as b, Request$1 as c, Handler as d, HttpContext as f, Middleware$1 as g, HttpContext$1 as h, ClearRouterPluginInput as i, Route as l, Handler$1 as m, ClearRouterPlugin as n, PluginSetupResult as o, Middleware as p, ClearRouterPluginContext as r, definePlugin as s, CoreRouter as t, H3App as u, ControllerAction as v, Response$2 as x, ControllerHandler as y };
|