clear-router 2.5.4 → 2.5.5
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 +4 -0
- package/dist/bindings-BL1d0dFY.d.cts +20 -0
- package/dist/bindings-DIanvIVd.mjs +211 -0
- package/dist/bindings-DqvZ9-YW.d.mts +20 -0
- package/dist/bindings-DvV2DXWi.cjs +253 -0
- package/dist/core/index.cjs +5 -2
- package/dist/core/index.d.cts +2 -2
- package/dist/core/index.d.mts +2 -2
- package/dist/core/index.mjs +3 -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 +271 -27
- package/dist/index.d.cts +70 -18
- package/dist/index.d.mts +70 -18
- package/dist/index.mjs +270 -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-CS_2XQ7I.d.cts → router-78fDk87B.d.cts} +68 -12
- package/dist/{router-BReOXz-F.mjs → router-BAWdklDQ.mjs} +130 -48
- package/dist/{router-_w2VzMQF.d.mts → router-BdfAh1me.d.mts} +67 -11
- package/dist/{router-CHg0pZUO.cjs → router-CCUF5FE7.cjs} +130 -48
- 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/hono/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import "../bindings-DIanvIVd.mjs";
|
|
2
|
+
import { t as CoreRouter } from "../router-BAWdklDQ.mjs";
|
|
3
|
+
import { n as resolveResponseMeta } from "../responses-_II3dOJ5.mjs";
|
|
3
4
|
|
|
4
5
|
//#region src/hono/router.ts
|
|
5
6
|
/**
|
|
@@ -19,9 +20,13 @@ var Router = class Router extends CoreRouter {
|
|
|
19
20
|
});
|
|
20
21
|
if (!meta) return void 0;
|
|
21
22
|
if (meta.isNativeResponse) return meta.body;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
const headers = meta.headers ? {} : meta.contentType ? { "Content-Type": meta.contentType } : void 0;
|
|
24
|
+
meta.headers?.forEach((headerValue, key) => {
|
|
25
|
+
if (headers) headers[key] = headerValue;
|
|
26
|
+
});
|
|
27
|
+
if (meta.isEmpty) return ctx.body(null, meta.status, headers);
|
|
28
|
+
if (meta.contentType?.startsWith("application/json")) return ctx.body(JSON.stringify(meta.body), meta.status, headers);
|
|
29
|
+
return ctx.body(meta.body, meta.status, headers);
|
|
25
30
|
}
|
|
26
31
|
static getParams(ctx) {
|
|
27
32
|
try {
|
|
@@ -168,10 +173,18 @@ var Router = class Router extends CoreRouter {
|
|
|
168
173
|
for (const route of this.routes) {
|
|
169
174
|
let handlerFunction = null;
|
|
170
175
|
let instance = null;
|
|
176
|
+
let bindingTarget;
|
|
177
|
+
let bindingMethod;
|
|
178
|
+
let bindingHandler;
|
|
179
|
+
let bindingMetadata;
|
|
171
180
|
try {
|
|
172
181
|
const resolved = this.resolveHandler(route);
|
|
173
182
|
handlerFunction = resolved.handlerFunction;
|
|
174
183
|
instance = resolved.instance;
|
|
184
|
+
bindingTarget = resolved.bindingTarget;
|
|
185
|
+
bindingMethod = resolved.bindingMethod;
|
|
186
|
+
bindingHandler = resolved.bindingHandler;
|
|
187
|
+
bindingMetadata = resolved.bindingMetadata;
|
|
175
188
|
} catch (error) {
|
|
176
189
|
console.error(`[ROUTES] Error setting up route ${route.path}:`, error.message);
|
|
177
190
|
throw error;
|
|
@@ -198,11 +211,13 @@ var Router = class Router extends CoreRouter {
|
|
|
198
211
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
199
212
|
body: reqBody,
|
|
200
213
|
query: ctx.req.query(),
|
|
201
|
-
params: Router.getParams(ctx)
|
|
214
|
+
params: Router.getParams(ctx),
|
|
215
|
+
method
|
|
202
216
|
});
|
|
203
|
-
const result = handlerFunction
|
|
217
|
+
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
204
218
|
const resolved = await Promise.resolve(result);
|
|
205
|
-
|
|
219
|
+
const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
|
|
220
|
+
return Router.toResponse(ctx, outgoing, method, route.path);
|
|
206
221
|
});
|
|
207
222
|
if ([
|
|
208
223
|
"put",
|
|
@@ -216,11 +231,13 @@ var Router = class Router extends CoreRouter {
|
|
|
216
231
|
Router.bindRequestToInstance(ctx, inst, route, {
|
|
217
232
|
body: reqBody,
|
|
218
233
|
query: ctx.req.query(),
|
|
219
|
-
params: Router.getParams(ctx)
|
|
234
|
+
params: Router.getParams(ctx),
|
|
235
|
+
method
|
|
220
236
|
});
|
|
221
|
-
const result = handlerFunction
|
|
237
|
+
const result = await Router.callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata);
|
|
222
238
|
const resolved = await Promise.resolve(result);
|
|
223
|
-
|
|
239
|
+
const outgoing = typeof resolved === "undefined" && ctx.clearResponse?.sent ? ctx.clearResponse : resolved;
|
|
240
|
+
return Router.toResponse(ctx, outgoing, method, route.path);
|
|
224
241
|
});
|
|
225
242
|
}
|
|
226
243
|
}
|
package/dist/index.cjs
CHANGED
|
@@ -59,6 +59,146 @@ var Route = class {
|
|
|
59
59
|
}
|
|
60
60
|
};
|
|
61
61
|
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/core/Request.ts
|
|
64
|
+
var Request = class extends ClearRequest {
|
|
65
|
+
original;
|
|
66
|
+
method = "GET";
|
|
67
|
+
path = "/";
|
|
68
|
+
url = "/";
|
|
69
|
+
headers = {};
|
|
70
|
+
constructor(init) {
|
|
71
|
+
super(init);
|
|
72
|
+
Object.assign(this, init);
|
|
73
|
+
}
|
|
74
|
+
getBody() {
|
|
75
|
+
return this.body ?? {};
|
|
76
|
+
}
|
|
77
|
+
header(name) {
|
|
78
|
+
if (typeof this.headers.get === "function") return this.headers.get(name) || "";
|
|
79
|
+
const headers = this.headers;
|
|
80
|
+
const value = headers[name] ?? headers[name.toLowerCase()];
|
|
81
|
+
return Array.isArray(value) ? String(value[0] ?? "") : String(value ?? "");
|
|
82
|
+
}
|
|
83
|
+
param(name) {
|
|
84
|
+
return this.params?.[name];
|
|
85
|
+
}
|
|
86
|
+
input(name) {
|
|
87
|
+
return this.body?.[name] ?? this.query?.[name] ?? this.params?.[name];
|
|
88
|
+
}
|
|
89
|
+
is(method) {
|
|
90
|
+
return this.method.toLowerCase() === String(method).toLowerCase();
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
//#endregion
|
|
95
|
+
//#region src/core/Response.ts
|
|
96
|
+
var Response = class {
|
|
97
|
+
body;
|
|
98
|
+
headers = new Headers();
|
|
99
|
+
sent = false;
|
|
100
|
+
statusCode = 200;
|
|
101
|
+
constructor(init) {
|
|
102
|
+
Object.assign(this, init);
|
|
103
|
+
if (init?.headers && !(init.headers instanceof Headers)) this.headers = new Headers(init.headers);
|
|
104
|
+
}
|
|
105
|
+
status(code) {
|
|
106
|
+
this.statusCode = code;
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
code(code) {
|
|
110
|
+
return this.status(code);
|
|
111
|
+
}
|
|
112
|
+
setHeader(name, value) {
|
|
113
|
+
this.headers.set(name, value);
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
header(name, value) {
|
|
117
|
+
return this.setHeader(name, value);
|
|
118
|
+
}
|
|
119
|
+
set(name, value) {
|
|
120
|
+
return this.setHeader(name, value);
|
|
121
|
+
}
|
|
122
|
+
type(contentType) {
|
|
123
|
+
return this.setHeader("Content-Type", contentType);
|
|
124
|
+
}
|
|
125
|
+
send(body) {
|
|
126
|
+
this.body = body;
|
|
127
|
+
this.sent = true;
|
|
128
|
+
return this;
|
|
129
|
+
}
|
|
130
|
+
json(body) {
|
|
131
|
+
return this.type("application/json; charset=utf-8").send(body);
|
|
132
|
+
}
|
|
133
|
+
html(body) {
|
|
134
|
+
return this.type("text/html; charset=utf-8").send(body);
|
|
135
|
+
}
|
|
136
|
+
text(body) {
|
|
137
|
+
return this.type("text/plain; charset=utf-8").send(body);
|
|
138
|
+
}
|
|
139
|
+
noContent() {
|
|
140
|
+
return this.status(204).send(null);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
//#endregion
|
|
145
|
+
//#region src/core/bindings.ts
|
|
146
|
+
const metadataKey = Symbol.for("clear-router:binding-metadata");
|
|
147
|
+
const bindings = /* @__PURE__ */ new WeakMap();
|
|
148
|
+
var Container = class {
|
|
149
|
+
static registry = /* @__PURE__ */ new Map();
|
|
150
|
+
static bind(token, value) {
|
|
151
|
+
this.registry.set(token, value);
|
|
152
|
+
}
|
|
153
|
+
static unbind(token) {
|
|
154
|
+
this.registry.delete(token);
|
|
155
|
+
}
|
|
156
|
+
static clear() {
|
|
157
|
+
this.registry.clear();
|
|
158
|
+
}
|
|
159
|
+
static has(token) {
|
|
160
|
+
return this.registry.has(token);
|
|
161
|
+
}
|
|
162
|
+
static async resolve(token, ctx, autoDiscover = false) {
|
|
163
|
+
if (token === Request) return ctx.clearRequest;
|
|
164
|
+
if (token === Response) return ctx.clearResponse;
|
|
165
|
+
if (this.registry.has(token)) return this.resolveBinding(this.registry.get(token), ctx, autoDiscover);
|
|
166
|
+
if (autoDiscover && typeof token === "function") return new token();
|
|
167
|
+
}
|
|
168
|
+
static async resolveBinding(binding, ctx, autoDiscover) {
|
|
169
|
+
if (!binding) return void 0;
|
|
170
|
+
if (typeof binding !== "function") return binding;
|
|
171
|
+
if (isClass(binding)) return new binding();
|
|
172
|
+
const resolved = await binding(ctx);
|
|
173
|
+
if (typeof resolved === "function" && autoDiscover && isClass(resolved)) return new resolved();
|
|
174
|
+
return resolved;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
function getStandardMetadata(metadata, propertyKey) {
|
|
178
|
+
const store = metadata && metadata[metadataKey];
|
|
179
|
+
if (!store) return void 0;
|
|
180
|
+
return propertyKey ? store[propertyKey] : void 0;
|
|
181
|
+
}
|
|
182
|
+
function getBindingMetadataFromTargets(targets) {
|
|
183
|
+
for (const { target, propertyKey } of targets) {
|
|
184
|
+
if (!target) continue;
|
|
185
|
+
const metadata = getBindingMetadata(target, propertyKey);
|
|
186
|
+
if (metadata) return metadata;
|
|
187
|
+
const standardMetadata = getStandardMetadata(target[Symbol.metadata], propertyKey);
|
|
188
|
+
if (standardMetadata) return standardMetadata;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function getBindingMetadata(target, propertyKey) {
|
|
192
|
+
if (propertyKey) return bindings.get(target)?.get(propertyKey);
|
|
193
|
+
return bindings.get(target)?.get("__route_handler__");
|
|
194
|
+
}
|
|
195
|
+
function getDesignParamTypes(target, propertyKey) {
|
|
196
|
+
return Reflect.getMetadata?.("design:paramtypes", target, propertyKey) ?? [];
|
|
197
|
+
}
|
|
198
|
+
function isClass(value) {
|
|
199
|
+
return typeof value === "function" && /^class\s/.test(Function.prototype.toString.call(value));
|
|
200
|
+
}
|
|
201
|
+
|
|
62
202
|
//#endregion
|
|
63
203
|
//#region src/core/router.ts
|
|
64
204
|
/**
|
|
@@ -71,6 +211,40 @@ var CoreRouter = class {
|
|
|
71
211
|
static routerStateNamespace = "clear-router:core";
|
|
72
212
|
static stateStoreKey = Symbol.for("clear-router:router-state");
|
|
73
213
|
static stateBoundKey = Symbol.for("clear-router:router-state-bound");
|
|
214
|
+
static defaultConfigKey = Symbol.for("clear-router:default-config");
|
|
215
|
+
static createBaseConfig() {
|
|
216
|
+
return {
|
|
217
|
+
methodOverride: {
|
|
218
|
+
enabled: true,
|
|
219
|
+
bodyKeys: ["_method"],
|
|
220
|
+
headerKeys: ["x-http-method"]
|
|
221
|
+
},
|
|
222
|
+
container: {
|
|
223
|
+
enabled: false,
|
|
224
|
+
autoDiscover: false
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
static mergeConfig(target, source) {
|
|
229
|
+
if (!source) return target;
|
|
230
|
+
if (source.methodOverride) target.methodOverride = {
|
|
231
|
+
...target.methodOverride || {},
|
|
232
|
+
...source.methodOverride
|
|
233
|
+
};
|
|
234
|
+
if (source.container) target.container = {
|
|
235
|
+
...target.container || {},
|
|
236
|
+
...source.container
|
|
237
|
+
};
|
|
238
|
+
return target;
|
|
239
|
+
}
|
|
240
|
+
static getDefaultConfig() {
|
|
241
|
+
const g = globalThis;
|
|
242
|
+
if (!g[this.defaultConfigKey]) g[this.defaultConfigKey] = this.createBaseConfig();
|
|
243
|
+
return {
|
|
244
|
+
methodOverride: { ...g[this.defaultConfigKey].methodOverride },
|
|
245
|
+
container: { ...g[this.defaultConfigKey].container }
|
|
246
|
+
};
|
|
247
|
+
}
|
|
74
248
|
static resolveStateNamespace() {
|
|
75
249
|
return String(this.routerStateNamespace || this.name || "clear-router:core");
|
|
76
250
|
}
|
|
@@ -81,11 +255,7 @@ var CoreRouter = class {
|
|
|
81
255
|
}
|
|
82
256
|
static createDefaultState() {
|
|
83
257
|
return {
|
|
84
|
-
config:
|
|
85
|
-
enabled: true,
|
|
86
|
-
bodyKeys: ["_method"],
|
|
87
|
-
headerKeys: ["x-http-method"]
|
|
88
|
-
} },
|
|
258
|
+
config: this.getDefaultConfig(),
|
|
89
259
|
groupContext: new node_async_hooks.AsyncLocalStorage(),
|
|
90
260
|
routes: [],
|
|
91
261
|
routesByPathMethod: {},
|
|
@@ -162,11 +332,24 @@ var CoreRouter = class {
|
|
|
162
332
|
}
|
|
163
333
|
};
|
|
164
334
|
}
|
|
165
|
-
static config = {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
335
|
+
static config = {
|
|
336
|
+
methodOverride: {
|
|
337
|
+
enabled: true,
|
|
338
|
+
bodyKeys: ["_method"],
|
|
339
|
+
headerKeys: ["x-http-method"]
|
|
340
|
+
},
|
|
341
|
+
container: {
|
|
342
|
+
enabled: false,
|
|
343
|
+
autoDiscover: false
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
static configureDefaults(options) {
|
|
347
|
+
const g = globalThis;
|
|
348
|
+
const defaults = this.mergeConfig(g[this.defaultConfigKey] || this.createBaseConfig(), options);
|
|
349
|
+
g[this.defaultConfigKey] = defaults;
|
|
350
|
+
const store = this.getStateStore();
|
|
351
|
+
for (const state of Object.values(store)) state.config = this.mergeConfig(state.config || this.createBaseConfig(), options);
|
|
352
|
+
}
|
|
170
353
|
static groupContext = new node_async_hooks.AsyncLocalStorage();
|
|
171
354
|
static routes = [];
|
|
172
355
|
static routesByPathMethod = {};
|
|
@@ -208,11 +391,12 @@ var CoreRouter = class {
|
|
|
208
391
|
*/
|
|
209
392
|
static configure(options) {
|
|
210
393
|
this.ensureState();
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
394
|
+
this.config = this.mergeConfig(this.getDefaultConfig(), this.config);
|
|
395
|
+
const container = options?.container;
|
|
396
|
+
if (container) {
|
|
397
|
+
if (typeof container.enabled === "boolean") this.config.container.enabled = container.enabled;
|
|
398
|
+
if (typeof container.autoDiscover === "boolean") this.config.container.autoDiscover = container.autoDiscover;
|
|
399
|
+
}
|
|
216
400
|
const override = options?.methodOverride;
|
|
217
401
|
if (!override) return;
|
|
218
402
|
if (typeof override.enabled === "boolean") this.config.methodOverride.enabled = override.enabled;
|
|
@@ -449,36 +633,94 @@ var CoreRouter = class {
|
|
|
449
633
|
static resolveHandler(route) {
|
|
450
634
|
let handlerFunction;
|
|
451
635
|
let instance = null;
|
|
452
|
-
|
|
453
|
-
|
|
636
|
+
let bindingTarget;
|
|
637
|
+
let bindingMethod;
|
|
638
|
+
let bindingHandler;
|
|
639
|
+
let bindingMetadata;
|
|
640
|
+
if (typeof route.handler === "function") {
|
|
641
|
+
handlerFunction = route.handler.bind(route);
|
|
642
|
+
bindingTarget = route.handler;
|
|
643
|
+
bindingHandler = route.handler;
|
|
644
|
+
} else if (Array.isArray(route.handler) && route.handler.length === 2) {
|
|
454
645
|
const [ControllerType, method] = route.handler;
|
|
455
646
|
if (["function", "object"].includes(typeof ControllerType) && typeof ControllerType[method] === "function") {
|
|
456
647
|
instance = ControllerType;
|
|
457
648
|
handlerFunction = ControllerType[method].bind(ControllerType);
|
|
649
|
+
bindingTarget = ControllerType;
|
|
650
|
+
bindingMethod = method;
|
|
651
|
+
bindingHandler = ControllerType[method];
|
|
652
|
+
bindingMetadata = ControllerType[Symbol.metadata];
|
|
458
653
|
} else if (typeof ControllerType === "function") {
|
|
459
654
|
instance = new ControllerType();
|
|
460
|
-
if (typeof instance[method] === "function")
|
|
461
|
-
|
|
655
|
+
if (typeof instance[method] === "function") {
|
|
656
|
+
handlerFunction = instance[method].bind(instance);
|
|
657
|
+
bindingTarget = ControllerType.prototype;
|
|
658
|
+
bindingMethod = method;
|
|
659
|
+
bindingHandler = instance[method];
|
|
660
|
+
bindingMetadata = ControllerType[Symbol.metadata];
|
|
661
|
+
} else throw new Error(`Method "${method}" not found in controller instance "${ControllerType.name}"`);
|
|
462
662
|
} else throw new Error(`Invalid controller type for route: ${route.path}`);
|
|
463
663
|
} else throw new Error(`Invalid handler format for route: ${route.path}`);
|
|
464
664
|
return {
|
|
465
665
|
handlerFunction,
|
|
466
|
-
instance
|
|
666
|
+
instance,
|
|
667
|
+
bindingTarget,
|
|
668
|
+
bindingMethod,
|
|
669
|
+
bindingHandler,
|
|
670
|
+
bindingMetadata
|
|
467
671
|
};
|
|
468
672
|
}
|
|
673
|
+
static async callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata) {
|
|
674
|
+
if (!this.config.container?.enabled) return handlerFunction(ctx, ctx.clearRequest);
|
|
675
|
+
const metadata = getBindingMetadataFromTargets([
|
|
676
|
+
{
|
|
677
|
+
target: bindingTarget,
|
|
678
|
+
propertyKey: bindingMethod
|
|
679
|
+
},
|
|
680
|
+
{ target: bindingHandler },
|
|
681
|
+
{
|
|
682
|
+
target: bindingTarget,
|
|
683
|
+
propertyKey: "__class__"
|
|
684
|
+
}
|
|
685
|
+
]) ?? getStandardMetadata(bindingMetadata, bindingMethod) ?? getStandardMetadata(bindingMetadata, "__class__");
|
|
686
|
+
if (!metadata) return handlerFunction(ctx, ctx.clearRequest);
|
|
687
|
+
const designTokens = [...bindingTarget ? getDesignParamTypes(bindingTarget, bindingMethod) : [], ...bindingHandler ? getDesignParamTypes(bindingHandler) : []];
|
|
688
|
+
const tokens = metadata.tokens?.length ? metadata.tokens : designTokens;
|
|
689
|
+
if (!tokens.length) return handlerFunction(ctx, ctx.clearRequest);
|
|
690
|
+
const args = [];
|
|
691
|
+
for (const token of tokens) {
|
|
692
|
+
const resolved = await Container.resolve(token, ctx, Boolean(this.config.container?.autoDiscover));
|
|
693
|
+
if (typeof resolved === "undefined") return handlerFunction(ctx, ctx.clearRequest);
|
|
694
|
+
args.push(resolved);
|
|
695
|
+
}
|
|
696
|
+
return handlerFunction(...args);
|
|
697
|
+
}
|
|
469
698
|
static bindRequestToInstance(ctx, instance, route, payload) {
|
|
699
|
+
const clearRequest = ctx.clearRequest instanceof Request ? ctx.clearRequest : new Request({
|
|
700
|
+
ctx,
|
|
701
|
+
route,
|
|
702
|
+
body: payload.body,
|
|
703
|
+
query: payload.query,
|
|
704
|
+
params: payload.params,
|
|
705
|
+
method: String(payload.method || ctx.req?.method || ctx.method || "GET").toUpperCase(),
|
|
706
|
+
path: String(ctx.path || ctx.req?.path || ctx.req?.url || route.path),
|
|
707
|
+
url: String(ctx.url || ctx.req?.url || ctx.req?.originalUrl || route.path),
|
|
708
|
+
headers: ctx.req?.headers || ctx.headers || {},
|
|
709
|
+
original: ctx.req || ctx.request || ctx
|
|
710
|
+
});
|
|
711
|
+
clearRequest.ctx = ctx;
|
|
712
|
+
clearRequest.route = route;
|
|
713
|
+
clearRequest.body = payload.body;
|
|
714
|
+
clearRequest.query = payload.query;
|
|
715
|
+
clearRequest.params = payload.params;
|
|
716
|
+
ctx.clearRequest = clearRequest;
|
|
717
|
+
if (!(ctx.clearResponse instanceof Response)) ctx.clearResponse = new Response();
|
|
470
718
|
if (!instance) return;
|
|
471
719
|
instance.ctx = ctx;
|
|
472
720
|
instance.body = payload.body;
|
|
473
721
|
instance.query = payload.query;
|
|
474
722
|
instance.params = payload.params;
|
|
475
|
-
instance.clearRequest =
|
|
476
|
-
ctx,
|
|
477
|
-
route,
|
|
478
|
-
body: instance.body,
|
|
479
|
-
query: instance.query,
|
|
480
|
-
params: instance.params
|
|
481
|
-
});
|
|
723
|
+
instance.clearRequest = clearRequest;
|
|
482
724
|
}
|
|
483
725
|
};
|
|
484
726
|
|
|
@@ -486,4 +728,6 @@ var CoreRouter = class {
|
|
|
486
728
|
exports.ClearRequest = ClearRequest;
|
|
487
729
|
exports.Controller = Controller;
|
|
488
730
|
exports.CoreRouter = CoreRouter;
|
|
731
|
+
exports.Request = Request;
|
|
732
|
+
exports.Response = Response;
|
|
489
733
|
exports.Route = Route;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { NextFunction, Request, Response as Response$
|
|
2
|
-
import { Middleware } from "h3";
|
|
1
|
+
import { NextFunction, Request as Request$1, Response as Response$2 } from "express";
|
|
2
|
+
import { Middleware as Middleware$1 } from "h3";
|
|
3
3
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
4
|
|
|
5
5
|
//#region types/basic.d.ts
|
|
@@ -26,16 +26,37 @@ interface RouterConfig {
|
|
|
26
26
|
bodyKeys?: string[] | string; /** Keys in the request headers to check for method override */
|
|
27
27
|
headerKeys?: string[] | string;
|
|
28
28
|
};
|
|
29
|
+
/**
|
|
30
|
+
* Optional method binding / container resolution support. Disabled by default.
|
|
31
|
+
*/
|
|
32
|
+
container?: {
|
|
33
|
+
/** Whether decorated handler parameter binding is enabled */enabled?: boolean; /** Whether unknown constructor tokens should be instantiated automatically */
|
|
34
|
+
autoDiscover?: boolean;
|
|
35
|
+
};
|
|
29
36
|
}
|
|
30
37
|
//#endregion
|
|
31
|
-
//#region
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
38
|
+
//#region src/core/Response.d.ts
|
|
39
|
+
declare class Response$1 {
|
|
40
|
+
body: any;
|
|
41
|
+
headers: Headers;
|
|
42
|
+
sent: boolean;
|
|
43
|
+
statusCode: number;
|
|
44
|
+
constructor(init?: Partial<Response$1>);
|
|
45
|
+
status(code: number): this;
|
|
46
|
+
code(code: number): this;
|
|
47
|
+
setHeader(name: string, value: string): this;
|
|
48
|
+
header(name: string, value: string): this;
|
|
49
|
+
set(name: string, value: string): this;
|
|
50
|
+
type(contentType: string): this;
|
|
51
|
+
send(body?: any): this;
|
|
52
|
+
json(body: any): this;
|
|
53
|
+
html(body: string): this;
|
|
54
|
+
text(body: string): this;
|
|
55
|
+
noContent(): this;
|
|
56
|
+
}
|
|
36
57
|
//#endregion
|
|
37
58
|
//#region src/Route.d.ts
|
|
38
|
-
declare class Route<X = any, M = Middleware | Middleware
|
|
59
|
+
declare class Route<X = any, M = Middleware$1 | Middleware, H = any> {
|
|
39
60
|
ctx: X;
|
|
40
61
|
body: RequestData;
|
|
41
62
|
query: RequestData;
|
|
@@ -52,8 +73,34 @@ declare class Route<X = any, M = Middleware | Middleware$1, H = any> {
|
|
|
52
73
|
constructor(methods: HttpMethod[], path: string, handler: H, middlewares?: M[]);
|
|
53
74
|
}
|
|
54
75
|
//#endregion
|
|
76
|
+
//#region src/core/Request.d.ts
|
|
77
|
+
declare class Request<X = any, M = any> extends ClearRequest<X, M> {
|
|
78
|
+
original?: any;
|
|
79
|
+
method: string;
|
|
80
|
+
path: string;
|
|
81
|
+
url: string;
|
|
82
|
+
headers: Headers | Record<string, any>;
|
|
83
|
+
constructor(init?: Partial<Request<X, M>> & {
|
|
84
|
+
body?: RequestData;
|
|
85
|
+
query?: RequestData;
|
|
86
|
+
params?: RequestData;
|
|
87
|
+
route?: Route<X, M>;
|
|
88
|
+
});
|
|
89
|
+
getBody(): RequestData;
|
|
90
|
+
header(name: string): string;
|
|
91
|
+
param(name: string): any;
|
|
92
|
+
input(name: string): any;
|
|
93
|
+
is(method: HttpMethod | string): boolean;
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region types/express.d.ts
|
|
97
|
+
/**
|
|
98
|
+
* Middleware function type
|
|
99
|
+
*/
|
|
100
|
+
type Middleware = (req: Request$1, res: Response$2, next: NextFunction) => any | Promise<any>;
|
|
101
|
+
//#endregion
|
|
55
102
|
//#region src/ClearRequest.d.ts
|
|
56
|
-
declare class ClearRequest<X = any, M = Middleware | Middleware
|
|
103
|
+
declare class ClearRequest<X = any, M = Middleware$1 | Middleware> {
|
|
57
104
|
[key: string]: any;
|
|
58
105
|
/**
|
|
59
106
|
* @param body - Parsed request body
|
|
@@ -92,16 +139,14 @@ declare abstract class CoreRouter {
|
|
|
92
139
|
protected static routerStateNamespace: string;
|
|
93
140
|
private static readonly stateStoreKey;
|
|
94
141
|
private static readonly stateBoundKey;
|
|
142
|
+
private static readonly defaultConfigKey;
|
|
143
|
+
protected static createBaseConfig(): RouterConfig;
|
|
144
|
+
protected static mergeConfig(target: RouterConfig, source?: RouterConfig): RouterConfig;
|
|
145
|
+
protected static getDefaultConfig(): RouterConfig;
|
|
95
146
|
protected static resolveStateNamespace(this: any): string;
|
|
96
147
|
protected static getStateStore(): Record<string, any>;
|
|
97
148
|
protected static createDefaultState(): {
|
|
98
|
-
config:
|
|
99
|
-
methodOverride: {
|
|
100
|
-
enabled: boolean;
|
|
101
|
-
bodyKeys: string[];
|
|
102
|
-
headerKeys: string[];
|
|
103
|
-
};
|
|
104
|
-
};
|
|
149
|
+
config: RouterConfig;
|
|
105
150
|
groupContext: AsyncLocalStorage<{
|
|
106
151
|
prefix: string;
|
|
107
152
|
groupMiddlewares: any[];
|
|
@@ -116,6 +161,7 @@ declare abstract class CoreRouter {
|
|
|
116
161
|
protected static bindStateAccessors(this: any): void;
|
|
117
162
|
protected static createDefaultOptionsHandler(): any;
|
|
118
163
|
static config: RouterConfig;
|
|
164
|
+
static configureDefaults(this: any, options?: RouterConfig): void;
|
|
119
165
|
protected static groupContext: AsyncLocalStorage<{
|
|
120
166
|
prefix: string;
|
|
121
167
|
groupMiddlewares: any[];
|
|
@@ -265,14 +311,20 @@ declare abstract class CoreRouter {
|
|
|
265
311
|
*/
|
|
266
312
|
static allRoutes(this: any, type: 'method'): { [method in Uppercase<HttpMethod>]?: Array<Route<any, any, any>> };
|
|
267
313
|
protected static resolveHandler(route: Route<any, any, any>): {
|
|
268
|
-
handlerFunction: ((ctx: any, req:
|
|
314
|
+
handlerFunction: ((ctx: any, req: Request) => any | Promise<any>) | null;
|
|
269
315
|
instance: Controller<any> | null;
|
|
316
|
+
bindingTarget?: object;
|
|
317
|
+
bindingMethod?: PropertyKey;
|
|
318
|
+
bindingHandler?: object;
|
|
319
|
+
bindingMetadata?: object;
|
|
270
320
|
};
|
|
321
|
+
protected static callHandler(this: any, handlerFunction: (ctx: any, req: Request) => any | Promise<any>, ctx: any, bindingTarget?: object, bindingMethod?: PropertyKey, bindingHandler?: object, bindingMetadata?: object): Promise<any>;
|
|
271
322
|
protected static bindRequestToInstance(ctx: any, instance: Controller<any> | Route<any, any, any> | null, route: Route<any, any, any>, payload: {
|
|
272
323
|
body: Record<string, any>;
|
|
273
324
|
query: Record<string, any>;
|
|
274
325
|
params: Record<string, any>;
|
|
326
|
+
method?: HttpMethod | string;
|
|
275
327
|
}): void;
|
|
276
328
|
}
|
|
277
329
|
//#endregion
|
|
278
|
-
export { ClearRequest, Controller, CoreRouter, Route };
|
|
330
|
+
export { ClearRequest, Controller, CoreRouter, Request, Response$1 as Response, Route };
|