clear-router 2.9.1 → 2.9.2
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/core/CoreRouter.cjs +46 -35
- package/dist/core/CoreRouter.d.cts +7 -4
- package/dist/core/CoreRouter.d.mts +7 -4
- package/dist/core/CoreRouter.mjs +46 -35
- package/dist/core/bindings.cjs +197 -38
- package/dist/core/bindings.d.cts +82 -11
- package/dist/core/bindings.d.mts +82 -11
- package/dist/core/bindings.mjs +191 -38
- package/dist/core/index.cjs +13 -0
- package/dist/core/index.d.cts +2 -1
- package/dist/core/index.d.mts +2 -1
- package/dist/core/index.mjs +2 -1
- package/dist/core/plugins.d.cts +4 -4
- package/dist/core/plugins.d.mts +4 -4
- package/dist/decorators/index.cjs +3 -1
- package/dist/decorators/index.d.cts +2 -2
- package/dist/decorators/index.d.mts +2 -2
- package/dist/decorators/index.mjs +2 -2
- package/dist/decorators/setup.cjs +3 -1
- package/dist/decorators/setup.d.cts +2 -2
- package/dist/decorators/setup.d.mts +2 -2
- package/dist/decorators/setup.mjs +2 -2
- package/dist/index.cjs +13 -0
- package/dist/index.d.cts +2 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +2 -1
- package/dist/types/basic.d.cts +5 -0
- package/dist/types/basic.d.mts +5 -0
- package/package.json +1 -1
package/dist/core/CoreRouter.cjs
CHANGED
|
@@ -3,8 +3,8 @@ const require_RouteGroup = require('../RouteGroup.cjs');
|
|
|
3
3
|
const require_RouteRegistrar = require('../RouteRegistrar.cjs');
|
|
4
4
|
const require_Request = require('./Request.cjs');
|
|
5
5
|
const require_Response = require('./Response.cjs');
|
|
6
|
-
const require_middleware = require('../decorators/middleware.cjs');
|
|
7
6
|
const require_bindings = require('./bindings.cjs');
|
|
7
|
+
const require_middleware = require('../decorators/middleware.cjs');
|
|
8
8
|
const require_ResourceRoutes = require('../ResourceRoutes.cjs');
|
|
9
9
|
let node_async_hooks = require("node:async_hooks");
|
|
10
10
|
let node_module = require("node:module");
|
|
@@ -21,10 +21,6 @@ var CoreRouter = class {
|
|
|
21
21
|
static stateStoreKey = Symbol.for("clear-router:router-state");
|
|
22
22
|
static stateBoundKey = Symbol.for("clear-router:router-state-bound");
|
|
23
23
|
static defaultConfigKey = Symbol.for("clear-router:default-config");
|
|
24
|
-
static pluginStoreKey = Symbol.for("clear-router:plugins");
|
|
25
|
-
static pluginPendingKey = Symbol.for("clear-router:plugin-promises");
|
|
26
|
-
static pluginHttpCtxResolversKey = Symbol.for("clear-router:plugin-http-ctx");
|
|
27
|
-
static pluginArgumentResolversKey = Symbol.for("clear-router:plugin-argument-resolvers");
|
|
28
24
|
static requestProvider;
|
|
29
25
|
static responseProvider;
|
|
30
26
|
static domainMatcherCache = /* @__PURE__ */ new Map();
|
|
@@ -39,7 +35,8 @@ var CoreRouter = class {
|
|
|
39
35
|
},
|
|
40
36
|
container: {
|
|
41
37
|
enabled: false,
|
|
42
|
-
autoDiscover: false
|
|
38
|
+
autoDiscover: false,
|
|
39
|
+
strict: false
|
|
43
40
|
}
|
|
44
41
|
};
|
|
45
42
|
static groupContext = new node_async_hooks.AsyncLocalStorage();
|
|
@@ -129,7 +126,8 @@ var CoreRouter = class {
|
|
|
129
126
|
},
|
|
130
127
|
container: {
|
|
131
128
|
enabled: false,
|
|
132
|
-
autoDiscover: false
|
|
129
|
+
autoDiscover: false,
|
|
130
|
+
strict: false
|
|
133
131
|
}
|
|
134
132
|
};
|
|
135
133
|
}
|
|
@@ -163,24 +161,20 @@ var CoreRouter = class {
|
|
|
163
161
|
return g[this.stateStoreKey];
|
|
164
162
|
}
|
|
165
163
|
static getPluginStore() {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
return g[this.pluginStoreKey];
|
|
164
|
+
this.bindStateAccessors();
|
|
165
|
+
return this.pluginStore;
|
|
169
166
|
}
|
|
170
167
|
static getPluginPendingStore() {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
return g[this.pluginPendingKey];
|
|
168
|
+
this.bindStateAccessors();
|
|
169
|
+
return this.pluginPending;
|
|
174
170
|
}
|
|
175
171
|
static getPluginArgumentResolvers() {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
return g[this.pluginArgumentResolversKey];
|
|
172
|
+
this.bindStateAccessors();
|
|
173
|
+
return this.pluginArgumentResolvers;
|
|
179
174
|
}
|
|
180
175
|
static getPluginHttpCtxResolvers() {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
return g[this.pluginHttpCtxResolversKey];
|
|
176
|
+
this.bindStateAccessors();
|
|
177
|
+
return this.pluginHttpCtxResolvers;
|
|
184
178
|
}
|
|
185
179
|
static createDefaultState() {
|
|
186
180
|
return {
|
|
@@ -192,7 +186,12 @@ var CoreRouter = class {
|
|
|
192
186
|
routesByName: /* @__PURE__ */ new Map(),
|
|
193
187
|
prefix: "",
|
|
194
188
|
groupMiddlewares: [],
|
|
195
|
-
globalMiddlewares: []
|
|
189
|
+
globalMiddlewares: [],
|
|
190
|
+
container: require_bindings.Container.create(),
|
|
191
|
+
pluginStore: /* @__PURE__ */ new Set(),
|
|
192
|
+
pluginPending: /* @__PURE__ */ new Set(),
|
|
193
|
+
pluginArgumentResolvers: /* @__PURE__ */ new Set(),
|
|
194
|
+
pluginHttpCtxResolvers: /* @__PURE__ */ new Set()
|
|
196
195
|
};
|
|
197
196
|
}
|
|
198
197
|
static bindStateAccessors() {
|
|
@@ -209,7 +208,12 @@ var CoreRouter = class {
|
|
|
209
208
|
"routesByName",
|
|
210
209
|
"prefix",
|
|
211
210
|
"groupMiddlewares",
|
|
212
|
-
"globalMiddlewares"
|
|
211
|
+
"globalMiddlewares",
|
|
212
|
+
"container",
|
|
213
|
+
"pluginStore",
|
|
214
|
+
"pluginPending",
|
|
215
|
+
"pluginArgumentResolvers",
|
|
216
|
+
"pluginHttpCtxResolvers"
|
|
213
217
|
]) Object.defineProperty(this, key, {
|
|
214
218
|
get() {
|
|
215
219
|
const ns = this.resolveStateNamespace();
|
|
@@ -284,13 +288,14 @@ var CoreRouter = class {
|
|
|
284
288
|
* @returns
|
|
285
289
|
*/
|
|
286
290
|
static async use(plugin, options) {
|
|
291
|
+
this.ensureState();
|
|
287
292
|
const name = typeof plugin === "function" ? plugin.name : plugin.name;
|
|
288
293
|
const store = this.getPluginStore();
|
|
289
294
|
if (name && store.has(name)) return;
|
|
290
295
|
if (name) store.add(name);
|
|
291
296
|
const setup = async () => {
|
|
292
297
|
const ctx = {
|
|
293
|
-
container:
|
|
298
|
+
container: this.container,
|
|
294
299
|
bind: this.createPluginBind(),
|
|
295
300
|
resolveArguments: (resolver) => {
|
|
296
301
|
this.getPluginArgumentResolvers().add(resolver);
|
|
@@ -298,7 +303,7 @@ var CoreRouter = class {
|
|
|
298
303
|
useHttpContext: (resolver) => {
|
|
299
304
|
this.getPluginHttpCtxResolvers().add(resolver);
|
|
300
305
|
},
|
|
301
|
-
bindings:
|
|
306
|
+
bindings: this.container.bindings(),
|
|
302
307
|
configure: this.configure.bind(this),
|
|
303
308
|
configureDefaults: this.configureDefaults.bind(this),
|
|
304
309
|
get request() {
|
|
@@ -342,17 +347,25 @@ var CoreRouter = class {
|
|
|
342
347
|
ctx,
|
|
343
348
|
request,
|
|
344
349
|
response,
|
|
345
|
-
getBindings: () => require_bindings.Container.bindings()
|
|
350
|
+
getBindings: () => require_bindings.Container.current().bindings()
|
|
346
351
|
};
|
|
347
352
|
}
|
|
348
353
|
static createPluginBind() {
|
|
349
|
-
const bind = (token, value) => {
|
|
354
|
+
const bind = (token, value, options) => {
|
|
355
|
+
if (value && typeof value === "object" && "useFactory" in value) {
|
|
356
|
+
const factory = value.useFactory;
|
|
357
|
+
this.container.bind(token, {
|
|
358
|
+
...value,
|
|
359
|
+
useFactory: (ctx) => factory(this.createPluginRequestContext(ctx))
|
|
360
|
+
});
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
350
363
|
if (typeof value === "function" && !require_bindings.isClass(value)) {
|
|
351
364
|
const factory = value;
|
|
352
|
-
|
|
365
|
+
this.container.bind(token, (ctx) => factory(this.createPluginRequestContext(ctx)), options);
|
|
353
366
|
return;
|
|
354
367
|
}
|
|
355
|
-
|
|
368
|
+
this.container.bind(token, value, options);
|
|
356
369
|
};
|
|
357
370
|
return bind;
|
|
358
371
|
}
|
|
@@ -1062,7 +1075,9 @@ var CoreRouter = class {
|
|
|
1062
1075
|
};
|
|
1063
1076
|
}
|
|
1064
1077
|
static async callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata) {
|
|
1065
|
-
|
|
1078
|
+
this.ensureState();
|
|
1079
|
+
const requestContainer = this.container.createRequestScope(ctx);
|
|
1080
|
+
return require_bindings.Container.run(requestContainer, () => this.pluginRequestContext.run(this.createPluginRequestContext(ctx), async () => {
|
|
1066
1081
|
await this.pluginsReady();
|
|
1067
1082
|
await this.resolvePluginHttpCtx(ctx);
|
|
1068
1083
|
if (!this.config.container?.enabled) return handlerFunction(ctx, ctx.clearRequest);
|
|
@@ -1091,12 +1106,12 @@ var CoreRouter = class {
|
|
|
1091
1106
|
if (!metadata || !tokens.length) return handlerFunction(ctx, ctx.clearRequest);
|
|
1092
1107
|
const args = [];
|
|
1093
1108
|
for (const token of tokens) {
|
|
1094
|
-
const resolved = await
|
|
1109
|
+
const resolved = this.config.container?.strict ? await requestContainer.resolveOrFail(token, ctx, Boolean(this.config.container?.autoDiscover)) : await requestContainer.resolve(token, ctx, Boolean(this.config.container?.autoDiscover));
|
|
1095
1110
|
if (typeof resolved === "undefined") return handlerFunction(ctx, ctx.clearRequest);
|
|
1096
1111
|
args.push(resolved);
|
|
1097
1112
|
}
|
|
1098
1113
|
return handlerFunction(...args);
|
|
1099
|
-
});
|
|
1114
|
+
}));
|
|
1100
1115
|
}
|
|
1101
1116
|
static bindRequestToInstance(ctx, instance, route, payload) {
|
|
1102
1117
|
const clearRequest = ctx.clearRequest instanceof require_Request.Request ? ctx.clearRequest : this.initializeInstance(require_Request.Request, {
|
|
@@ -1117,11 +1132,7 @@ var CoreRouter = class {
|
|
|
1117
1132
|
clearRequest.query = payload.query;
|
|
1118
1133
|
clearRequest.params = payload.params;
|
|
1119
1134
|
ctx.clearRequest = clearRequest;
|
|
1120
|
-
|
|
1121
|
-
if (!(ctx.clearResponse instanceof require_Response.Response)) {
|
|
1122
|
-
ctx.clearResponse = this.initializeInstance(require_Response.Response, ctx.response ?? ctx.reply ?? ctx.res);
|
|
1123
|
-
require_bindings.Container.bind(require_Response.Response, ctx.clearResponse);
|
|
1124
|
-
}
|
|
1135
|
+
if (!(ctx.clearResponse instanceof require_Response.Response)) ctx.clearResponse = this.initializeInstance(require_Response.Response, ctx.response ?? ctx.reply ?? ctx.res);
|
|
1125
1136
|
if (!instance) return;
|
|
1126
1137
|
instance.ctx = ctx;
|
|
1127
1138
|
instance.body = payload.body;
|
|
@@ -2,6 +2,7 @@ import { Response } from "./Response.cjs";
|
|
|
2
2
|
import { Route } from "../Route.cjs";
|
|
3
3
|
import { ApiResourceMiddleware, HttpMethod, ResourceAction, RouteGroupContext, RouteGroupSource, RouterConfig } from "../types/basic.cjs";
|
|
4
4
|
import { Request } from "./Request.cjs";
|
|
5
|
+
import { Container } from "./bindings.cjs";
|
|
5
6
|
import { ClearRouterPluginArgumentsContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, PluginArgumentsResolver, PluginBind } from "./plugins.cjs";
|
|
6
7
|
import { Controller } from "../Controller.cjs";
|
|
7
8
|
import { ResourceRoutes } from "../ResourceRoutes.cjs";
|
|
@@ -21,16 +22,13 @@ declare abstract class CoreRouter {
|
|
|
21
22
|
private static readonly stateStoreKey;
|
|
22
23
|
private static readonly stateBoundKey;
|
|
23
24
|
private static readonly defaultConfigKey;
|
|
24
|
-
private static readonly pluginStoreKey;
|
|
25
|
-
private static readonly pluginPendingKey;
|
|
26
|
-
private static readonly pluginHttpCtxResolversKey;
|
|
27
|
-
private static readonly pluginArgumentResolversKey;
|
|
28
25
|
private static requestProvider?;
|
|
29
26
|
private static responseProvider?;
|
|
30
27
|
private static readonly domainMatcherCache;
|
|
31
28
|
private static readonly constraintRegexCache;
|
|
32
29
|
static routePatterns: Map<string, string | RegExp>;
|
|
33
30
|
static config: RouterConfig;
|
|
31
|
+
static container: Container;
|
|
34
32
|
protected static groupContext: AsyncLocalStorage<RouteGroupContext>;
|
|
35
33
|
protected static pluginRequestContext: AsyncLocalStorage<ClearRouterPluginRequestContext<any>>;
|
|
36
34
|
static routes: Set<Route<any, any, any>>;
|
|
@@ -75,6 +73,11 @@ declare abstract class CoreRouter {
|
|
|
75
73
|
prefix: string;
|
|
76
74
|
groupMiddlewares: any[];
|
|
77
75
|
globalMiddlewares: any[];
|
|
76
|
+
container: Container;
|
|
77
|
+
pluginStore: Set<string>;
|
|
78
|
+
pluginPending: Set<Promise<void>>;
|
|
79
|
+
pluginArgumentResolvers: Set<PluginArgumentsResolver>;
|
|
80
|
+
pluginHttpCtxResolvers: Set<PluginArgumentsResolver>;
|
|
78
81
|
};
|
|
79
82
|
protected static bindStateAccessors(): void;
|
|
80
83
|
protected static createDefaultOptionsHandler(): any;
|
|
@@ -2,6 +2,7 @@ import { Response } from "./Response.mjs";
|
|
|
2
2
|
import { Route } from "../Route.mjs";
|
|
3
3
|
import { ApiResourceMiddleware, HttpMethod, ResourceAction, RouteGroupContext, RouteGroupSource, RouterConfig } from "../types/basic.mjs";
|
|
4
4
|
import { Request } from "./Request.mjs";
|
|
5
|
+
import { Container } from "./bindings.mjs";
|
|
5
6
|
import { ClearRouterPluginArgumentsContext, ClearRouterPluginInput, ClearRouterPluginRequestContext, PluginArgumentsResolver, PluginBind } from "./plugins.mjs";
|
|
6
7
|
import { Controller } from "../Controller.mjs";
|
|
7
8
|
import { ResourceRoutes } from "../ResourceRoutes.mjs";
|
|
@@ -21,16 +22,13 @@ declare abstract class CoreRouter {
|
|
|
21
22
|
private static readonly stateStoreKey;
|
|
22
23
|
private static readonly stateBoundKey;
|
|
23
24
|
private static readonly defaultConfigKey;
|
|
24
|
-
private static readonly pluginStoreKey;
|
|
25
|
-
private static readonly pluginPendingKey;
|
|
26
|
-
private static readonly pluginHttpCtxResolversKey;
|
|
27
|
-
private static readonly pluginArgumentResolversKey;
|
|
28
25
|
private static requestProvider?;
|
|
29
26
|
private static responseProvider?;
|
|
30
27
|
private static readonly domainMatcherCache;
|
|
31
28
|
private static readonly constraintRegexCache;
|
|
32
29
|
static routePatterns: Map<string, string | RegExp>;
|
|
33
30
|
static config: RouterConfig;
|
|
31
|
+
static container: Container;
|
|
34
32
|
protected static groupContext: AsyncLocalStorage<RouteGroupContext>;
|
|
35
33
|
protected static pluginRequestContext: AsyncLocalStorage<ClearRouterPluginRequestContext<any>>;
|
|
36
34
|
static routes: Set<Route<any, any, any>>;
|
|
@@ -75,6 +73,11 @@ declare abstract class CoreRouter {
|
|
|
75
73
|
prefix: string;
|
|
76
74
|
groupMiddlewares: any[];
|
|
77
75
|
globalMiddlewares: any[];
|
|
76
|
+
container: Container;
|
|
77
|
+
pluginStore: Set<string>;
|
|
78
|
+
pluginPending: Set<Promise<void>>;
|
|
79
|
+
pluginArgumentResolvers: Set<PluginArgumentsResolver>;
|
|
80
|
+
pluginHttpCtxResolvers: Set<PluginArgumentsResolver>;
|
|
78
81
|
};
|
|
79
82
|
protected static bindStateAccessors(): void;
|
|
80
83
|
protected static createDefaultOptionsHandler(): any;
|
package/dist/core/CoreRouter.mjs
CHANGED
|
@@ -3,8 +3,8 @@ import { RouteGroup } from "../RouteGroup.mjs";
|
|
|
3
3
|
import { RouteRegistrar } from "../RouteRegistrar.mjs";
|
|
4
4
|
import { Request } from "./Request.mjs";
|
|
5
5
|
import { Response } from "./Response.mjs";
|
|
6
|
-
import { getControllerMiddlewares } from "../decorators/middleware.mjs";
|
|
7
6
|
import { Container, getBindingMetadataFromTargets, getDesignParamTypes, getStandardMetadata, isClass } from "./bindings.mjs";
|
|
7
|
+
import { getControllerMiddlewares } from "../decorators/middleware.mjs";
|
|
8
8
|
import { ResourceRoutes } from "../ResourceRoutes.mjs";
|
|
9
9
|
import { createRequire } from "node:module";
|
|
10
10
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
@@ -21,10 +21,6 @@ var CoreRouter = class {
|
|
|
21
21
|
static stateStoreKey = Symbol.for("clear-router:router-state");
|
|
22
22
|
static stateBoundKey = Symbol.for("clear-router:router-state-bound");
|
|
23
23
|
static defaultConfigKey = Symbol.for("clear-router:default-config");
|
|
24
|
-
static pluginStoreKey = Symbol.for("clear-router:plugins");
|
|
25
|
-
static pluginPendingKey = Symbol.for("clear-router:plugin-promises");
|
|
26
|
-
static pluginHttpCtxResolversKey = Symbol.for("clear-router:plugin-http-ctx");
|
|
27
|
-
static pluginArgumentResolversKey = Symbol.for("clear-router:plugin-argument-resolvers");
|
|
28
24
|
static requestProvider;
|
|
29
25
|
static responseProvider;
|
|
30
26
|
static domainMatcherCache = /* @__PURE__ */ new Map();
|
|
@@ -39,7 +35,8 @@ var CoreRouter = class {
|
|
|
39
35
|
},
|
|
40
36
|
container: {
|
|
41
37
|
enabled: false,
|
|
42
|
-
autoDiscover: false
|
|
38
|
+
autoDiscover: false,
|
|
39
|
+
strict: false
|
|
43
40
|
}
|
|
44
41
|
};
|
|
45
42
|
static groupContext = new AsyncLocalStorage();
|
|
@@ -129,7 +126,8 @@ var CoreRouter = class {
|
|
|
129
126
|
},
|
|
130
127
|
container: {
|
|
131
128
|
enabled: false,
|
|
132
|
-
autoDiscover: false
|
|
129
|
+
autoDiscover: false,
|
|
130
|
+
strict: false
|
|
133
131
|
}
|
|
134
132
|
};
|
|
135
133
|
}
|
|
@@ -163,24 +161,20 @@ var CoreRouter = class {
|
|
|
163
161
|
return g[this.stateStoreKey];
|
|
164
162
|
}
|
|
165
163
|
static getPluginStore() {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
return g[this.pluginStoreKey];
|
|
164
|
+
this.bindStateAccessors();
|
|
165
|
+
return this.pluginStore;
|
|
169
166
|
}
|
|
170
167
|
static getPluginPendingStore() {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
return g[this.pluginPendingKey];
|
|
168
|
+
this.bindStateAccessors();
|
|
169
|
+
return this.pluginPending;
|
|
174
170
|
}
|
|
175
171
|
static getPluginArgumentResolvers() {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
return g[this.pluginArgumentResolversKey];
|
|
172
|
+
this.bindStateAccessors();
|
|
173
|
+
return this.pluginArgumentResolvers;
|
|
179
174
|
}
|
|
180
175
|
static getPluginHttpCtxResolvers() {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
return g[this.pluginHttpCtxResolversKey];
|
|
176
|
+
this.bindStateAccessors();
|
|
177
|
+
return this.pluginHttpCtxResolvers;
|
|
184
178
|
}
|
|
185
179
|
static createDefaultState() {
|
|
186
180
|
return {
|
|
@@ -192,7 +186,12 @@ var CoreRouter = class {
|
|
|
192
186
|
routesByName: /* @__PURE__ */ new Map(),
|
|
193
187
|
prefix: "",
|
|
194
188
|
groupMiddlewares: [],
|
|
195
|
-
globalMiddlewares: []
|
|
189
|
+
globalMiddlewares: [],
|
|
190
|
+
container: Container.create(),
|
|
191
|
+
pluginStore: /* @__PURE__ */ new Set(),
|
|
192
|
+
pluginPending: /* @__PURE__ */ new Set(),
|
|
193
|
+
pluginArgumentResolvers: /* @__PURE__ */ new Set(),
|
|
194
|
+
pluginHttpCtxResolvers: /* @__PURE__ */ new Set()
|
|
196
195
|
};
|
|
197
196
|
}
|
|
198
197
|
static bindStateAccessors() {
|
|
@@ -209,7 +208,12 @@ var CoreRouter = class {
|
|
|
209
208
|
"routesByName",
|
|
210
209
|
"prefix",
|
|
211
210
|
"groupMiddlewares",
|
|
212
|
-
"globalMiddlewares"
|
|
211
|
+
"globalMiddlewares",
|
|
212
|
+
"container",
|
|
213
|
+
"pluginStore",
|
|
214
|
+
"pluginPending",
|
|
215
|
+
"pluginArgumentResolvers",
|
|
216
|
+
"pluginHttpCtxResolvers"
|
|
213
217
|
]) Object.defineProperty(this, key, {
|
|
214
218
|
get() {
|
|
215
219
|
const ns = this.resolveStateNamespace();
|
|
@@ -284,13 +288,14 @@ var CoreRouter = class {
|
|
|
284
288
|
* @returns
|
|
285
289
|
*/
|
|
286
290
|
static async use(plugin, options) {
|
|
291
|
+
this.ensureState();
|
|
287
292
|
const name = typeof plugin === "function" ? plugin.name : plugin.name;
|
|
288
293
|
const store = this.getPluginStore();
|
|
289
294
|
if (name && store.has(name)) return;
|
|
290
295
|
if (name) store.add(name);
|
|
291
296
|
const setup = async () => {
|
|
292
297
|
const ctx = {
|
|
293
|
-
container:
|
|
298
|
+
container: this.container,
|
|
294
299
|
bind: this.createPluginBind(),
|
|
295
300
|
resolveArguments: (resolver) => {
|
|
296
301
|
this.getPluginArgumentResolvers().add(resolver);
|
|
@@ -298,7 +303,7 @@ var CoreRouter = class {
|
|
|
298
303
|
useHttpContext: (resolver) => {
|
|
299
304
|
this.getPluginHttpCtxResolvers().add(resolver);
|
|
300
305
|
},
|
|
301
|
-
bindings:
|
|
306
|
+
bindings: this.container.bindings(),
|
|
302
307
|
configure: this.configure.bind(this),
|
|
303
308
|
configureDefaults: this.configureDefaults.bind(this),
|
|
304
309
|
get request() {
|
|
@@ -342,17 +347,25 @@ var CoreRouter = class {
|
|
|
342
347
|
ctx,
|
|
343
348
|
request,
|
|
344
349
|
response,
|
|
345
|
-
getBindings: () => Container.bindings()
|
|
350
|
+
getBindings: () => Container.current().bindings()
|
|
346
351
|
};
|
|
347
352
|
}
|
|
348
353
|
static createPluginBind() {
|
|
349
|
-
const bind = (token, value) => {
|
|
354
|
+
const bind = (token, value, options) => {
|
|
355
|
+
if (value && typeof value === "object" && "useFactory" in value) {
|
|
356
|
+
const factory = value.useFactory;
|
|
357
|
+
this.container.bind(token, {
|
|
358
|
+
...value,
|
|
359
|
+
useFactory: (ctx) => factory(this.createPluginRequestContext(ctx))
|
|
360
|
+
});
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
350
363
|
if (typeof value === "function" && !isClass(value)) {
|
|
351
364
|
const factory = value;
|
|
352
|
-
|
|
365
|
+
this.container.bind(token, (ctx) => factory(this.createPluginRequestContext(ctx)), options);
|
|
353
366
|
return;
|
|
354
367
|
}
|
|
355
|
-
|
|
368
|
+
this.container.bind(token, value, options);
|
|
356
369
|
};
|
|
357
370
|
return bind;
|
|
358
371
|
}
|
|
@@ -1062,7 +1075,9 @@ var CoreRouter = class {
|
|
|
1062
1075
|
};
|
|
1063
1076
|
}
|
|
1064
1077
|
static async callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata) {
|
|
1065
|
-
|
|
1078
|
+
this.ensureState();
|
|
1079
|
+
const requestContainer = this.container.createRequestScope(ctx);
|
|
1080
|
+
return Container.run(requestContainer, () => this.pluginRequestContext.run(this.createPluginRequestContext(ctx), async () => {
|
|
1066
1081
|
await this.pluginsReady();
|
|
1067
1082
|
await this.resolvePluginHttpCtx(ctx);
|
|
1068
1083
|
if (!this.config.container?.enabled) return handlerFunction(ctx, ctx.clearRequest);
|
|
@@ -1091,12 +1106,12 @@ var CoreRouter = class {
|
|
|
1091
1106
|
if (!metadata || !tokens.length) return handlerFunction(ctx, ctx.clearRequest);
|
|
1092
1107
|
const args = [];
|
|
1093
1108
|
for (const token of tokens) {
|
|
1094
|
-
const resolved = await
|
|
1109
|
+
const resolved = this.config.container?.strict ? await requestContainer.resolveOrFail(token, ctx, Boolean(this.config.container?.autoDiscover)) : await requestContainer.resolve(token, ctx, Boolean(this.config.container?.autoDiscover));
|
|
1095
1110
|
if (typeof resolved === "undefined") return handlerFunction(ctx, ctx.clearRequest);
|
|
1096
1111
|
args.push(resolved);
|
|
1097
1112
|
}
|
|
1098
1113
|
return handlerFunction(...args);
|
|
1099
|
-
});
|
|
1114
|
+
}));
|
|
1100
1115
|
}
|
|
1101
1116
|
static bindRequestToInstance(ctx, instance, route, payload) {
|
|
1102
1117
|
const clearRequest = ctx.clearRequest instanceof Request ? ctx.clearRequest : this.initializeInstance(Request, {
|
|
@@ -1117,11 +1132,7 @@ var CoreRouter = class {
|
|
|
1117
1132
|
clearRequest.query = payload.query;
|
|
1118
1133
|
clearRequest.params = payload.params;
|
|
1119
1134
|
ctx.clearRequest = clearRequest;
|
|
1120
|
-
|
|
1121
|
-
if (!(ctx.clearResponse instanceof Response)) {
|
|
1122
|
-
ctx.clearResponse = this.initializeInstance(Response, ctx.response ?? ctx.reply ?? ctx.res);
|
|
1123
|
-
Container.bind(Response, ctx.clearResponse);
|
|
1124
|
-
}
|
|
1135
|
+
if (!(ctx.clearResponse instanceof Response)) ctx.clearResponse = this.initializeInstance(Response, ctx.response ?? ctx.reply ?? ctx.res);
|
|
1125
1136
|
if (!instance) return;
|
|
1126
1137
|
instance.ctx = ctx;
|
|
1127
1138
|
instance.body = payload.body;
|