clear-router 2.9.0 → 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/README.md +15 -1
- package/dist/core/CoreRouter.cjs +47 -34
- package/dist/core/CoreRouter.d.cts +7 -4
- package/dist/core/CoreRouter.d.mts +7 -4
- package/dist/core/CoreRouter.mjs +47 -34
- 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 +16 -0
- package/dist/core/index.d.cts +3 -1
- package/dist/core/index.d.mts +3 -1
- package/dist/core/index.mjs +3 -1
- package/dist/core/plugins.d.cts +4 -4
- package/dist/core/plugins.d.mts +4 -4
- package/dist/decorators/index.cjs +6 -1
- package/dist/decorators/index.d.cts +3 -2
- package/dist/decorators/index.d.mts +3 -2
- package/dist/decorators/index.mjs +3 -2
- package/dist/decorators/middleware.cjs +100 -0
- package/dist/decorators/middleware.d.cts +51 -0
- package/dist/decorators/middleware.d.mts +51 -0
- package/dist/decorators/middleware.mjs +98 -0
- package/dist/decorators/setup.cjs +6 -1
- package/dist/decorators/setup.d.cts +3 -2
- package/dist/decorators/setup.d.mts +3 -2
- package/dist/decorators/setup.mjs +3 -2
- package/dist/index.cjs +16 -0
- package/dist/index.d.cts +3 -1
- package/dist/index.d.mts +3 -1
- package/dist/index.mjs +3 -1
- package/dist/types/basic.d.cts +5 -0
- package/dist/types/basic.d.mts +5 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -95,7 +95,21 @@ See [API.md](https://arkstack-hq.github.io/clear-router/api) for complete API do
|
|
|
95
95
|
## Middleware Execution Order
|
|
96
96
|
|
|
97
97
|
```txt
|
|
98
|
-
[ Global Middleware ] → [ Group Middleware ] → [ Route Middleware ]
|
|
98
|
+
[ Global Middleware ] → [ Group Middleware ] → [ @middleware (class) ] → [ @middleware (method) ] → [ Route Middleware ]
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Controllers and controller methods can declare middleware with the `@middleware`
|
|
102
|
+
decorator, accepting the same shapes as any other middleware (callbacks,
|
|
103
|
+
middleware classes, or instances exposing a `handle` method):
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import { middleware } from 'clear-router/decorators';
|
|
107
|
+
|
|
108
|
+
@middleware([auth])
|
|
109
|
+
class AccountController {
|
|
110
|
+
@middleware(GuestMiddleware)
|
|
111
|
+
create() {}
|
|
112
|
+
}
|
|
99
113
|
```
|
|
100
114
|
|
|
101
115
|
## Handler Execution
|
package/dist/core/CoreRouter.cjs
CHANGED
|
@@ -4,6 +4,7 @@ const require_RouteRegistrar = require('../RouteRegistrar.cjs');
|
|
|
4
4
|
const require_Request = require('./Request.cjs');
|
|
5
5
|
const require_Response = require('./Response.cjs');
|
|
6
6
|
const require_bindings = require('./bindings.cjs');
|
|
7
|
+
const require_middleware = require('../decorators/middleware.cjs');
|
|
7
8
|
const require_ResourceRoutes = require('../ResourceRoutes.cjs');
|
|
8
9
|
let node_async_hooks = require("node:async_hooks");
|
|
9
10
|
let node_module = require("node:module");
|
|
@@ -20,10 +21,6 @@ var CoreRouter = class {
|
|
|
20
21
|
static stateStoreKey = Symbol.for("clear-router:router-state");
|
|
21
22
|
static stateBoundKey = Symbol.for("clear-router:router-state-bound");
|
|
22
23
|
static defaultConfigKey = Symbol.for("clear-router:default-config");
|
|
23
|
-
static pluginStoreKey = Symbol.for("clear-router:plugins");
|
|
24
|
-
static pluginPendingKey = Symbol.for("clear-router:plugin-promises");
|
|
25
|
-
static pluginHttpCtxResolversKey = Symbol.for("clear-router:plugin-http-ctx");
|
|
26
|
-
static pluginArgumentResolversKey = Symbol.for("clear-router:plugin-argument-resolvers");
|
|
27
24
|
static requestProvider;
|
|
28
25
|
static responseProvider;
|
|
29
26
|
static domainMatcherCache = /* @__PURE__ */ new Map();
|
|
@@ -38,7 +35,8 @@ var CoreRouter = class {
|
|
|
38
35
|
},
|
|
39
36
|
container: {
|
|
40
37
|
enabled: false,
|
|
41
|
-
autoDiscover: false
|
|
38
|
+
autoDiscover: false,
|
|
39
|
+
strict: false
|
|
42
40
|
}
|
|
43
41
|
};
|
|
44
42
|
static groupContext = new node_async_hooks.AsyncLocalStorage();
|
|
@@ -128,7 +126,8 @@ var CoreRouter = class {
|
|
|
128
126
|
},
|
|
129
127
|
container: {
|
|
130
128
|
enabled: false,
|
|
131
|
-
autoDiscover: false
|
|
129
|
+
autoDiscover: false,
|
|
130
|
+
strict: false
|
|
132
131
|
}
|
|
133
132
|
};
|
|
134
133
|
}
|
|
@@ -162,24 +161,20 @@ var CoreRouter = class {
|
|
|
162
161
|
return g[this.stateStoreKey];
|
|
163
162
|
}
|
|
164
163
|
static getPluginStore() {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
return g[this.pluginStoreKey];
|
|
164
|
+
this.bindStateAccessors();
|
|
165
|
+
return this.pluginStore;
|
|
168
166
|
}
|
|
169
167
|
static getPluginPendingStore() {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
return g[this.pluginPendingKey];
|
|
168
|
+
this.bindStateAccessors();
|
|
169
|
+
return this.pluginPending;
|
|
173
170
|
}
|
|
174
171
|
static getPluginArgumentResolvers() {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
return g[this.pluginArgumentResolversKey];
|
|
172
|
+
this.bindStateAccessors();
|
|
173
|
+
return this.pluginArgumentResolvers;
|
|
178
174
|
}
|
|
179
175
|
static getPluginHttpCtxResolvers() {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
return g[this.pluginHttpCtxResolversKey];
|
|
176
|
+
this.bindStateAccessors();
|
|
177
|
+
return this.pluginHttpCtxResolvers;
|
|
183
178
|
}
|
|
184
179
|
static createDefaultState() {
|
|
185
180
|
return {
|
|
@@ -191,7 +186,12 @@ var CoreRouter = class {
|
|
|
191
186
|
routesByName: /* @__PURE__ */ new Map(),
|
|
192
187
|
prefix: "",
|
|
193
188
|
groupMiddlewares: [],
|
|
194
|
-
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()
|
|
195
195
|
};
|
|
196
196
|
}
|
|
197
197
|
static bindStateAccessors() {
|
|
@@ -208,7 +208,12 @@ var CoreRouter = class {
|
|
|
208
208
|
"routesByName",
|
|
209
209
|
"prefix",
|
|
210
210
|
"groupMiddlewares",
|
|
211
|
-
"globalMiddlewares"
|
|
211
|
+
"globalMiddlewares",
|
|
212
|
+
"container",
|
|
213
|
+
"pluginStore",
|
|
214
|
+
"pluginPending",
|
|
215
|
+
"pluginArgumentResolvers",
|
|
216
|
+
"pluginHttpCtxResolvers"
|
|
212
217
|
]) Object.defineProperty(this, key, {
|
|
213
218
|
get() {
|
|
214
219
|
const ns = this.resolveStateNamespace();
|
|
@@ -283,13 +288,14 @@ var CoreRouter = class {
|
|
|
283
288
|
* @returns
|
|
284
289
|
*/
|
|
285
290
|
static async use(plugin, options) {
|
|
291
|
+
this.ensureState();
|
|
286
292
|
const name = typeof plugin === "function" ? plugin.name : plugin.name;
|
|
287
293
|
const store = this.getPluginStore();
|
|
288
294
|
if (name && store.has(name)) return;
|
|
289
295
|
if (name) store.add(name);
|
|
290
296
|
const setup = async () => {
|
|
291
297
|
const ctx = {
|
|
292
|
-
container:
|
|
298
|
+
container: this.container,
|
|
293
299
|
bind: this.createPluginBind(),
|
|
294
300
|
resolveArguments: (resolver) => {
|
|
295
301
|
this.getPluginArgumentResolvers().add(resolver);
|
|
@@ -297,7 +303,7 @@ var CoreRouter = class {
|
|
|
297
303
|
useHttpContext: (resolver) => {
|
|
298
304
|
this.getPluginHttpCtxResolvers().add(resolver);
|
|
299
305
|
},
|
|
300
|
-
bindings:
|
|
306
|
+
bindings: this.container.bindings(),
|
|
301
307
|
configure: this.configure.bind(this),
|
|
302
308
|
configureDefaults: this.configureDefaults.bind(this),
|
|
303
309
|
get request() {
|
|
@@ -341,17 +347,25 @@ var CoreRouter = class {
|
|
|
341
347
|
ctx,
|
|
342
348
|
request,
|
|
343
349
|
response,
|
|
344
|
-
getBindings: () => require_bindings.Container.bindings()
|
|
350
|
+
getBindings: () => require_bindings.Container.current().bindings()
|
|
345
351
|
};
|
|
346
352
|
}
|
|
347
353
|
static createPluginBind() {
|
|
348
|
-
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
|
+
}
|
|
349
363
|
if (typeof value === "function" && !require_bindings.isClass(value)) {
|
|
350
364
|
const factory = value;
|
|
351
|
-
|
|
365
|
+
this.container.bind(token, (ctx) => factory(this.createPluginRequestContext(ctx)), options);
|
|
352
366
|
return;
|
|
353
367
|
}
|
|
354
|
-
|
|
368
|
+
this.container.bind(token, value, options);
|
|
355
369
|
};
|
|
356
370
|
return bind;
|
|
357
371
|
}
|
|
@@ -790,6 +804,7 @@ var CoreRouter = class {
|
|
|
790
804
|
const route = new require_Route.Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, this.resolveMiddlewares([
|
|
791
805
|
...this.globalMiddlewares,
|
|
792
806
|
...activeGroupMiddlewares,
|
|
807
|
+
...require_middleware.getControllerMiddlewares(handler),
|
|
793
808
|
...middlewares || []
|
|
794
809
|
]), {
|
|
795
810
|
registrationPaths,
|
|
@@ -1060,7 +1075,9 @@ var CoreRouter = class {
|
|
|
1060
1075
|
};
|
|
1061
1076
|
}
|
|
1062
1077
|
static async callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata) {
|
|
1063
|
-
|
|
1078
|
+
this.ensureState();
|
|
1079
|
+
const requestContainer = this.container.createRequestScope(ctx);
|
|
1080
|
+
return require_bindings.Container.run(requestContainer, () => this.pluginRequestContext.run(this.createPluginRequestContext(ctx), async () => {
|
|
1064
1081
|
await this.pluginsReady();
|
|
1065
1082
|
await this.resolvePluginHttpCtx(ctx);
|
|
1066
1083
|
if (!this.config.container?.enabled) return handlerFunction(ctx, ctx.clearRequest);
|
|
@@ -1089,12 +1106,12 @@ var CoreRouter = class {
|
|
|
1089
1106
|
if (!metadata || !tokens.length) return handlerFunction(ctx, ctx.clearRequest);
|
|
1090
1107
|
const args = [];
|
|
1091
1108
|
for (const token of tokens) {
|
|
1092
|
-
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));
|
|
1093
1110
|
if (typeof resolved === "undefined") return handlerFunction(ctx, ctx.clearRequest);
|
|
1094
1111
|
args.push(resolved);
|
|
1095
1112
|
}
|
|
1096
1113
|
return handlerFunction(...args);
|
|
1097
|
-
});
|
|
1114
|
+
}));
|
|
1098
1115
|
}
|
|
1099
1116
|
static bindRequestToInstance(ctx, instance, route, payload) {
|
|
1100
1117
|
const clearRequest = ctx.clearRequest instanceof require_Request.Request ? ctx.clearRequest : this.initializeInstance(require_Request.Request, {
|
|
@@ -1115,11 +1132,7 @@ var CoreRouter = class {
|
|
|
1115
1132
|
clearRequest.query = payload.query;
|
|
1116
1133
|
clearRequest.params = payload.params;
|
|
1117
1134
|
ctx.clearRequest = clearRequest;
|
|
1118
|
-
|
|
1119
|
-
if (!(ctx.clearResponse instanceof require_Response.Response)) {
|
|
1120
|
-
ctx.clearResponse = this.initializeInstance(require_Response.Response, ctx.response ?? ctx.reply ?? ctx.res);
|
|
1121
|
-
require_bindings.Container.bind(require_Response.Response, ctx.clearResponse);
|
|
1122
|
-
}
|
|
1135
|
+
if (!(ctx.clearResponse instanceof require_Response.Response)) ctx.clearResponse = this.initializeInstance(require_Response.Response, ctx.response ?? ctx.reply ?? ctx.res);
|
|
1123
1136
|
if (!instance) return;
|
|
1124
1137
|
instance.ctx = ctx;
|
|
1125
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
|
@@ -4,6 +4,7 @@ import { RouteRegistrar } from "../RouteRegistrar.mjs";
|
|
|
4
4
|
import { Request } from "./Request.mjs";
|
|
5
5
|
import { Response } from "./Response.mjs";
|
|
6
6
|
import { Container, getBindingMetadataFromTargets, getDesignParamTypes, getStandardMetadata, isClass } from "./bindings.mjs";
|
|
7
|
+
import { getControllerMiddlewares } from "../decorators/middleware.mjs";
|
|
7
8
|
import { ResourceRoutes } from "../ResourceRoutes.mjs";
|
|
8
9
|
import { createRequire } from "node:module";
|
|
9
10
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
@@ -20,10 +21,6 @@ var CoreRouter = class {
|
|
|
20
21
|
static stateStoreKey = Symbol.for("clear-router:router-state");
|
|
21
22
|
static stateBoundKey = Symbol.for("clear-router:router-state-bound");
|
|
22
23
|
static defaultConfigKey = Symbol.for("clear-router:default-config");
|
|
23
|
-
static pluginStoreKey = Symbol.for("clear-router:plugins");
|
|
24
|
-
static pluginPendingKey = Symbol.for("clear-router:plugin-promises");
|
|
25
|
-
static pluginHttpCtxResolversKey = Symbol.for("clear-router:plugin-http-ctx");
|
|
26
|
-
static pluginArgumentResolversKey = Symbol.for("clear-router:plugin-argument-resolvers");
|
|
27
24
|
static requestProvider;
|
|
28
25
|
static responseProvider;
|
|
29
26
|
static domainMatcherCache = /* @__PURE__ */ new Map();
|
|
@@ -38,7 +35,8 @@ var CoreRouter = class {
|
|
|
38
35
|
},
|
|
39
36
|
container: {
|
|
40
37
|
enabled: false,
|
|
41
|
-
autoDiscover: false
|
|
38
|
+
autoDiscover: false,
|
|
39
|
+
strict: false
|
|
42
40
|
}
|
|
43
41
|
};
|
|
44
42
|
static groupContext = new AsyncLocalStorage();
|
|
@@ -128,7 +126,8 @@ var CoreRouter = class {
|
|
|
128
126
|
},
|
|
129
127
|
container: {
|
|
130
128
|
enabled: false,
|
|
131
|
-
autoDiscover: false
|
|
129
|
+
autoDiscover: false,
|
|
130
|
+
strict: false
|
|
132
131
|
}
|
|
133
132
|
};
|
|
134
133
|
}
|
|
@@ -162,24 +161,20 @@ var CoreRouter = class {
|
|
|
162
161
|
return g[this.stateStoreKey];
|
|
163
162
|
}
|
|
164
163
|
static getPluginStore() {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
return g[this.pluginStoreKey];
|
|
164
|
+
this.bindStateAccessors();
|
|
165
|
+
return this.pluginStore;
|
|
168
166
|
}
|
|
169
167
|
static getPluginPendingStore() {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
return g[this.pluginPendingKey];
|
|
168
|
+
this.bindStateAccessors();
|
|
169
|
+
return this.pluginPending;
|
|
173
170
|
}
|
|
174
171
|
static getPluginArgumentResolvers() {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
return g[this.pluginArgumentResolversKey];
|
|
172
|
+
this.bindStateAccessors();
|
|
173
|
+
return this.pluginArgumentResolvers;
|
|
178
174
|
}
|
|
179
175
|
static getPluginHttpCtxResolvers() {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
return g[this.pluginHttpCtxResolversKey];
|
|
176
|
+
this.bindStateAccessors();
|
|
177
|
+
return this.pluginHttpCtxResolvers;
|
|
183
178
|
}
|
|
184
179
|
static createDefaultState() {
|
|
185
180
|
return {
|
|
@@ -191,7 +186,12 @@ var CoreRouter = class {
|
|
|
191
186
|
routesByName: /* @__PURE__ */ new Map(),
|
|
192
187
|
prefix: "",
|
|
193
188
|
groupMiddlewares: [],
|
|
194
|
-
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()
|
|
195
195
|
};
|
|
196
196
|
}
|
|
197
197
|
static bindStateAccessors() {
|
|
@@ -208,7 +208,12 @@ var CoreRouter = class {
|
|
|
208
208
|
"routesByName",
|
|
209
209
|
"prefix",
|
|
210
210
|
"groupMiddlewares",
|
|
211
|
-
"globalMiddlewares"
|
|
211
|
+
"globalMiddlewares",
|
|
212
|
+
"container",
|
|
213
|
+
"pluginStore",
|
|
214
|
+
"pluginPending",
|
|
215
|
+
"pluginArgumentResolvers",
|
|
216
|
+
"pluginHttpCtxResolvers"
|
|
212
217
|
]) Object.defineProperty(this, key, {
|
|
213
218
|
get() {
|
|
214
219
|
const ns = this.resolveStateNamespace();
|
|
@@ -283,13 +288,14 @@ var CoreRouter = class {
|
|
|
283
288
|
* @returns
|
|
284
289
|
*/
|
|
285
290
|
static async use(plugin, options) {
|
|
291
|
+
this.ensureState();
|
|
286
292
|
const name = typeof plugin === "function" ? plugin.name : plugin.name;
|
|
287
293
|
const store = this.getPluginStore();
|
|
288
294
|
if (name && store.has(name)) return;
|
|
289
295
|
if (name) store.add(name);
|
|
290
296
|
const setup = async () => {
|
|
291
297
|
const ctx = {
|
|
292
|
-
container:
|
|
298
|
+
container: this.container,
|
|
293
299
|
bind: this.createPluginBind(),
|
|
294
300
|
resolveArguments: (resolver) => {
|
|
295
301
|
this.getPluginArgumentResolvers().add(resolver);
|
|
@@ -297,7 +303,7 @@ var CoreRouter = class {
|
|
|
297
303
|
useHttpContext: (resolver) => {
|
|
298
304
|
this.getPluginHttpCtxResolvers().add(resolver);
|
|
299
305
|
},
|
|
300
|
-
bindings:
|
|
306
|
+
bindings: this.container.bindings(),
|
|
301
307
|
configure: this.configure.bind(this),
|
|
302
308
|
configureDefaults: this.configureDefaults.bind(this),
|
|
303
309
|
get request() {
|
|
@@ -341,17 +347,25 @@ var CoreRouter = class {
|
|
|
341
347
|
ctx,
|
|
342
348
|
request,
|
|
343
349
|
response,
|
|
344
|
-
getBindings: () => Container.bindings()
|
|
350
|
+
getBindings: () => Container.current().bindings()
|
|
345
351
|
};
|
|
346
352
|
}
|
|
347
353
|
static createPluginBind() {
|
|
348
|
-
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
|
+
}
|
|
349
363
|
if (typeof value === "function" && !isClass(value)) {
|
|
350
364
|
const factory = value;
|
|
351
|
-
|
|
365
|
+
this.container.bind(token, (ctx) => factory(this.createPluginRequestContext(ctx)), options);
|
|
352
366
|
return;
|
|
353
367
|
}
|
|
354
|
-
|
|
368
|
+
this.container.bind(token, value, options);
|
|
355
369
|
};
|
|
356
370
|
return bind;
|
|
357
371
|
}
|
|
@@ -790,6 +804,7 @@ var CoreRouter = class {
|
|
|
790
804
|
const route = new Route(methods.includes("options") ? methods : methods.concat("options"), fullPath, handler, this.resolveMiddlewares([
|
|
791
805
|
...this.globalMiddlewares,
|
|
792
806
|
...activeGroupMiddlewares,
|
|
807
|
+
...getControllerMiddlewares(handler),
|
|
793
808
|
...middlewares || []
|
|
794
809
|
]), {
|
|
795
810
|
registrationPaths,
|
|
@@ -1060,7 +1075,9 @@ var CoreRouter = class {
|
|
|
1060
1075
|
};
|
|
1061
1076
|
}
|
|
1062
1077
|
static async callHandler(handlerFunction, ctx, bindingTarget, bindingMethod, bindingHandler, bindingMetadata) {
|
|
1063
|
-
|
|
1078
|
+
this.ensureState();
|
|
1079
|
+
const requestContainer = this.container.createRequestScope(ctx);
|
|
1080
|
+
return Container.run(requestContainer, () => this.pluginRequestContext.run(this.createPluginRequestContext(ctx), async () => {
|
|
1064
1081
|
await this.pluginsReady();
|
|
1065
1082
|
await this.resolvePluginHttpCtx(ctx);
|
|
1066
1083
|
if (!this.config.container?.enabled) return handlerFunction(ctx, ctx.clearRequest);
|
|
@@ -1089,12 +1106,12 @@ var CoreRouter = class {
|
|
|
1089
1106
|
if (!metadata || !tokens.length) return handlerFunction(ctx, ctx.clearRequest);
|
|
1090
1107
|
const args = [];
|
|
1091
1108
|
for (const token of tokens) {
|
|
1092
|
-
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));
|
|
1093
1110
|
if (typeof resolved === "undefined") return handlerFunction(ctx, ctx.clearRequest);
|
|
1094
1111
|
args.push(resolved);
|
|
1095
1112
|
}
|
|
1096
1113
|
return handlerFunction(...args);
|
|
1097
|
-
});
|
|
1114
|
+
}));
|
|
1098
1115
|
}
|
|
1099
1116
|
static bindRequestToInstance(ctx, instance, route, payload) {
|
|
1100
1117
|
const clearRequest = ctx.clearRequest instanceof Request ? ctx.clearRequest : this.initializeInstance(Request, {
|
|
@@ -1115,11 +1132,7 @@ var CoreRouter = class {
|
|
|
1115
1132
|
clearRequest.query = payload.query;
|
|
1116
1133
|
clearRequest.params = payload.params;
|
|
1117
1134
|
ctx.clearRequest = clearRequest;
|
|
1118
|
-
|
|
1119
|
-
if (!(ctx.clearResponse instanceof Response)) {
|
|
1120
|
-
ctx.clearResponse = this.initializeInstance(Response, ctx.response ?? ctx.reply ?? ctx.res);
|
|
1121
|
-
Container.bind(Response, ctx.clearResponse);
|
|
1122
|
-
}
|
|
1135
|
+
if (!(ctx.clearResponse instanceof Response)) ctx.clearResponse = this.initializeInstance(Response, ctx.response ?? ctx.reply ?? ctx.res);
|
|
1123
1136
|
if (!instance) return;
|
|
1124
1137
|
instance.ctx = ctx;
|
|
1125
1138
|
instance.body = payload.body;
|