@tsed/common 6.86.0 → 6.87.1

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.
@@ -1,7 +1,7 @@
1
1
  import { $log, colorize } from '@tsed/logger';
2
2
  export { $log, Logger } from '@tsed/logger';
3
3
  import '@tsed/logger-file';
4
- import { DIConfiguration, Injectable, ProviderScope, Inject, InjectorService, Constant, Scope, DIContext, Provider, ProviderType, GlobalProviders, setLoggerLevel, registerProvider, Configuration, createContainer, colors, DITest } from '@tsed/di';
4
+ import { DIConfiguration, Injectable, ProviderScope, Inject, InjectorService, Constant, Provider, ProviderType, GlobalProviders, Scope, DIContext, setLoggerLevel, registerProvider, Configuration, createContainer, colors, DITest } from '@tsed/di';
5
5
  export * from '@tsed/di';
6
6
  import { ParamTypes, PlatformParams, Context, UseParam, mapParamsOptions } from '@tsed/platform-params';
7
7
  export * from '@tsed/platform-params';
@@ -977,6 +977,7 @@ const PLATFORM_ROUTER_OPTIONS = Symbol.for("PlatformRouterOptions");
977
977
  let PlatformRouter = PlatformRouter_1 = class PlatformRouter {
978
978
  constructor(platformHandler) {
979
979
  this.platformHandler = platformHandler;
980
+ this.isBuilt = false;
980
981
  this.rawRouter = this.raw = PlatformRouter_1.createRawRouter();
981
982
  }
982
983
  /**
@@ -1283,72 +1284,77 @@ __decorate([__param(0, Context()), __metadata("design:type", Function), __metada
1283
1284
 
1284
1285
  AcceptMimesMiddleware = __decorate([Middleware()], AcceptMimesMiddleware);
1285
1286
 
1286
- /**
1287
- * Create Ts.ED context handler
1288
- * @param fn
1289
- * @ignore
1290
- */
1287
+ const ROUTER_OPTIONS = Symbol.for("ROUTER_OPTIONS");
1291
1288
 
1292
- function useCtxHandler(fn) {
1293
- fn.type = HandlerType.CTX_FN;
1294
- return fn;
1295
- }
1289
+ let AUTO_INC = 0;
1290
+ class ControllerProvider extends Provider {
1291
+ constructor(provide, options = {}) {
1292
+ super(provide, options);
1293
+ this.type = ProviderType.CONTROLLER;
1294
+ this.tokenRouter = `${this.name}_ROUTER_${AUTO_INC++}`;
1295
+ }
1296
+ /**
1297
+ *
1298
+ */
1296
1299
 
1297
- /**
1298
- * @ignore
1299
- */
1300
1300
 
1301
- function formatMethod(method) {
1302
- return (method === OperationMethods.CUSTOM ? "use" : method || "use").toLowerCase();
1303
- }
1304
- /**
1305
- * @ignore
1306
- */
1301
+ get routerOptions() {
1302
+ return this.store.get(ROUTER_OPTIONS) || {};
1303
+ }
1304
+ /**
1305
+ *
1306
+ * @param value
1307
+ */
1307
1308
 
1308
1309
 
1309
- class PlatformControllerBuilder {
1310
- constructor(provider) {
1311
- this.provider = provider;
1310
+ set routerOptions(value) {
1311
+ this.store.set(ROUTER_OPTIONS, value);
1312
1312
  }
1313
1313
  /**
1314
- * @returns {any}
1314
+ *
1315
+ * @returns {any[]}
1315
1316
  */
1316
1317
 
1317
1318
 
1318
- build(injector) {
1319
- const {
1320
- middlewares: {
1321
- useBefore
1322
- }
1323
- } = this.provider; // Controller lifecycle
1319
+ get middlewares() {
1320
+ return Object.assign({
1321
+ use: [],
1322
+ useAfter: [],
1323
+ useBefore: []
1324
+ }, this.store.get("middlewares", {}));
1325
+ }
1326
+ /**
1327
+ *
1328
+ * @param middlewares
1329
+ */
1324
1330
 
1325
- this.buildMiddlewares(useBefore) // Controller before-middleware
1326
- .buildEndpoints() // All endpoints and his middlewares
1327
- .buildChildrenCtrls(injector); // Children controllers
1328
1331
 
1329
- return this.provider.getRouter();
1330
- }
1332
+ set middlewares(middlewares) {
1333
+ const mdlwrs = this.middlewares;
1331
1334
 
1332
- buildEndpoints() {
1333
- getOperationsRoutes(this.provider.token).forEach(operationRoute => {
1334
- this.buildEndpoint(operationRoute);
1335
- });
1336
- return this;
1337
- }
1335
+ const concat = (key, a, b) => a[key] = a[key].concat(b[key]);
1338
1336
 
1339
- buildEndpoint(operationRoute) {
1340
- let handlers = this.getMiddlewares(operationRoute);
1341
- const router = this.provider.getRouter();
1342
- router.addRoute({
1343
- handlers,
1344
- token: operationRoute.token,
1345
- method: formatMethod(operationRoute.method),
1346
- path: operationRoute.path,
1347
- isFinal: operationRoute.isFinal
1337
+ Object.keys(middlewares).forEach(key => {
1338
+ concat(key, mdlwrs, middlewares);
1348
1339
  });
1340
+ this.store.set("middlewares", mdlwrs);
1349
1341
  }
1350
1342
 
1351
- getMiddlewares(operationRoute) {
1343
+ }
1344
+
1345
+ /**
1346
+ * Create Ts.ED context handler
1347
+ * @param fn
1348
+ * @ignore
1349
+ */
1350
+
1351
+ function useCtxHandler(fn) {
1352
+ fn.type = HandlerType.CTX_FN;
1353
+ return fn;
1354
+ }
1355
+
1356
+ let PlatformMiddlewaresChain = class PlatformMiddlewaresChain {
1357
+ get(provider, operationRoute) {
1352
1358
  const {
1353
1359
  endpoint
1354
1360
  } = operationRoute;
@@ -1362,11 +1368,10 @@ class PlatformControllerBuilder {
1362
1368
  use,
1363
1369
  useAfter
1364
1370
  }
1365
- } = this.provider;
1371
+ } = provider;
1366
1372
  const hasFiles = [...operationRoute.endpoint.children.values()].find(item => item.paramType === ParamTypes.FILES);
1367
1373
  return [].concat(useCtxHandler(bindEndpointMiddleware(endpoint))).concat(PlatformAcceptMimesMiddleware).concat(hasFiles && PlatformMulterMiddleware).concat(beforeMiddlewares) // Endpoint before-middlewares
1368
1374
  .concat(use) // Controller use-middlewares
1369
- // .concat(endpoint.cache && PlatformCacheMiddleware)
1370
1375
  .concat(mldwrs) // Endpoint middlewares
1371
1376
  .concat(endpoint) // Endpoint metadata
1372
1377
  .concat(afterMiddlewares) // Endpoint after-middlewares
@@ -1374,32 +1379,97 @@ class PlatformControllerBuilder {
1374
1379
  .filter(item => !!item);
1375
1380
  }
1376
1381
 
1377
- buildChildrenCtrls(injector) {
1378
- const {
1379
- children
1380
- } = this.provider;
1381
- const router = this.provider.getRouter();
1382
+ };
1383
+ PlatformMiddlewaresChain = __decorate([Injectable()], PlatformMiddlewaresChain);
1384
+
1385
+ GlobalProviders.createRegistry(ProviderType.CONTROLLER, ControllerProvider, {
1386
+ onInvoke(provider, locals, {
1387
+ injector
1388
+ }) {
1389
+ const router = createRouter(injector, provider);
1390
+ locals.set(PlatformRouter, router);
1391
+ }
1392
+
1393
+ });
1394
+ /**
1395
+ * @ignore
1396
+ */
1397
+
1398
+ function formatMethod(method) {
1399
+ return (method === OperationMethods.CUSTOM ? "use" : method || "use").toLowerCase();
1400
+ }
1401
+ /**
1402
+ * @ignore
1403
+ */
1404
+
1405
+
1406
+ function getRouter(injector, provider) {
1407
+ return injector.get(provider.tokenRouter);
1408
+ }
1409
+ /**
1410
+ * @ignore
1411
+ */
1412
+
1413
+ function createRouter(injector, provider) {
1414
+ const token = provider.tokenRouter;
1415
+
1416
+ if (injector.has(token)) {
1417
+ return getRouter(injector, provider);
1418
+ }
1419
+
1420
+ const router = PlatformRouter.create(injector, provider.routerOptions);
1421
+ return injector.add(token, {
1422
+ useValue: router
1423
+ }).invoke(token);
1424
+ }
1425
+ /**
1426
+ * @ignore
1427
+ * @param injector
1428
+ * @param provider
1429
+ */
1430
+
1431
+ function buildRouter(injector, provider) {
1432
+ const {
1433
+ middlewares: {
1434
+ useBefore
1435
+ },
1436
+ children
1437
+ } = provider; // Controller lifecycle
1438
+
1439
+ const router = createRouter(injector, provider);
1440
+
1441
+ if (!router.isBuilt) {
1442
+ router.isBuilt = true;
1443
+ const platformMiddlewaresChain = injector.get(PlatformMiddlewaresChain);
1444
+ useBefore // Controller before-middleware
1445
+ .filter(o => typeof o === "function").forEach(middleware => {
1446
+ router.use(middleware);
1447
+ }); // build all endpoints and his middlewares
1448
+
1449
+ getOperationsRoutes(provider.token).forEach(operationRoute => {
1450
+ const handlers = platformMiddlewaresChain == null ? void 0 : platformMiddlewaresChain.get(provider, operationRoute);
1451
+ router.addRoute({
1452
+ handlers,
1453
+ token: operationRoute.token,
1454
+ method: formatMethod(operationRoute.method),
1455
+ path: operationRoute.path,
1456
+ isFinal: operationRoute.isFinal
1457
+ });
1458
+ }); // build children controllers
1459
+
1382
1460
  children.forEach(child => {
1383
- const provider = injector.getProvider(child);
1461
+ const childProvider = injector.getProvider(child);
1384
1462
  /* istanbul ignore next */
1385
1463
 
1386
- if (!provider) {
1464
+ if (!childProvider) {
1387
1465
  throw new Error("Controller component not found in the ControllerRegistry");
1388
1466
  }
1389
1467
 
1390
- new PlatformControllerBuilder(provider).build(injector);
1391
- router.use(provider.path, provider.getRouter());
1468
+ router.use(childProvider.path, buildRouter(injector, childProvider));
1392
1469
  });
1393
1470
  }
1394
1471
 
1395
- buildMiddlewares(middlewares) {
1396
- const router = this.provider.getRouter();
1397
- middlewares.filter(o => typeof o === "function").forEach(middleware => {
1398
- router.use(middleware);
1399
- });
1400
- return this;
1401
- }
1402
-
1472
+ return router;
1403
1473
  }
1404
1474
 
1405
1475
  /**
@@ -1996,71 +2066,6 @@ class PlatformRouteDetails extends JsonOperationRoute {
1996
2066
 
1997
2067
  }
1998
2068
 
1999
- const ROUTER_OPTIONS = Symbol.for("ROUTER_OPTIONS");
2000
-
2001
- class ControllerProvider extends Provider {
2002
- constructor(provide, options = {}) {
2003
- super(provide, options);
2004
- this.type = ProviderType.CONTROLLER;
2005
- }
2006
- /**
2007
- *
2008
- */
2009
-
2010
-
2011
- get routerOptions() {
2012
- return this.store.get(ROUTER_OPTIONS) || {};
2013
- }
2014
- /**
2015
- *
2016
- * @param value
2017
- */
2018
-
2019
-
2020
- set routerOptions(value) {
2021
- this.store.set(ROUTER_OPTIONS, value);
2022
- }
2023
- /**
2024
- *
2025
- * @returns {any[]}
2026
- */
2027
-
2028
-
2029
- get middlewares() {
2030
- return Object.assign({
2031
- use: [],
2032
- useAfter: [],
2033
- useBefore: []
2034
- }, this.store.get("middlewares", {}));
2035
- }
2036
- /**
2037
- *
2038
- * @param middlewares
2039
- */
2040
-
2041
-
2042
- set middlewares(middlewares) {
2043
- const mdlwrs = this.middlewares;
2044
-
2045
- const concat = (key, a, b) => a[key] = a[key].concat(b[key]);
2046
-
2047
- Object.keys(middlewares).forEach(key => {
2048
- concat(key, mdlwrs, middlewares);
2049
- });
2050
- this.store.set("middlewares", mdlwrs);
2051
- }
2052
-
2053
- getRouter() {
2054
- return this.router;
2055
- }
2056
-
2057
- setRouter(router) {
2058
- this.router = router;
2059
- return this;
2060
- }
2061
-
2062
- }
2063
-
2064
2069
  /**
2065
2070
  * @deprecated Since 2020-11-11. Use getProperties from @tsed/schema
2066
2071
  */
@@ -2084,13 +2089,6 @@ let PropertyMetadata = class PropertyMetadata extends JsonPropertyStore {
2084
2089
  };
2085
2090
  PropertyMetadata = __decorate([JsonEntityComponent(DecoratorTypes.PROP)], PropertyMetadata);
2086
2091
 
2087
- GlobalProviders.createRegistry(ProviderType.CONTROLLER, ControllerProvider, {
2088
- onInvoke(provider, locals) {
2089
- locals.set(PlatformRouter, provider.getRouter());
2090
- }
2091
-
2092
- });
2093
-
2094
2092
  /**
2095
2093
  * `Platform` is used to provide all routes collected by annotation `@Controller`.
2096
2094
  *
@@ -2137,7 +2135,7 @@ let Platform = class Platform {
2137
2135
  provider
2138
2136
  });
2139
2137
 
2140
- this.app.use(ctrlPath, ...[].concat(provider.getRouter().callback()));
2138
+ this.app.use(ctrlPath, ...[].concat(getRouter(injector, provider).callback()));
2141
2139
  this._routes = getOperationsRoutes(provider.token, {
2142
2140
  withChildren: true,
2143
2141
  basePath
@@ -2182,7 +2180,7 @@ let Platform = class Platform {
2182
2180
  injector
2183
2181
  } = this;
2184
2182
  injector.getProviders(ProviderType.CONTROLLER).map(provider => {
2185
- provider.setRouter(PlatformRouter.create(injector, provider.routerOptions));
2183
+ createRouter(injector, provider);
2186
2184
  });
2187
2185
  }
2188
2186
  /**
@@ -2197,33 +2195,28 @@ let Platform = class Platform {
2197
2195
  } = this;
2198
2196
  injector.getProviders(ProviderType.CONTROLLER).map(provider => {
2199
2197
  if (!provider.hasParent()) {
2200
- return new PlatformControllerBuilder(provider).build(injector);
2198
+ return buildRouter(injector, provider);
2201
2199
  }
2202
2200
  });
2203
2201
  }
2204
2202
 
2205
2203
  };
2206
2204
  Platform = __decorate([Injectable({
2207
- scope: ProviderScope.SINGLETON
2205
+ scope: ProviderScope.SINGLETON,
2206
+ imports: [PlatformMiddlewaresChain]
2208
2207
  }), __metadata("design:paramtypes", [InjectorService, PlatformApplication])], Platform);
2209
2208
 
2210
2209
  $log.name = "TSED";
2211
2210
  function createInjector(settings = {}) {
2212
2211
  const injector = new InjectorService();
2213
- injector.settings = createSettingsService(injector);
2212
+ injector.addProvider(PlatformConfiguration);
2213
+ injector.settings = injector.invoke(PlatformConfiguration);
2214
2214
  injector.logger = $log;
2215
2215
  injector.settings.set(settings);
2216
2216
  setLoggerLevel(injector);
2217
2217
  return injector;
2218
2218
  }
2219
2219
 
2220
- function createSettingsService(injector) {
2221
- const provider = GlobalProviders.get(PlatformConfiguration).clone();
2222
- provider.instance = injector.invoke(provider.useClass);
2223
- injector.addProvider(PlatformConfiguration, provider);
2224
- return provider.instance;
2225
- }
2226
-
2227
2220
  function createPlatformApplication(injector) {
2228
2221
  injector.forkProvider(PlatformApplication);
2229
2222
  }
@@ -3413,5 +3406,5 @@ class PlatformTest extends DITest {
3413
3406
  */
3414
3407
  class PlatformModule {}
3415
3408
 
3416
- export { AcceptMime, AcceptMimesMiddleware, AnyToPromiseWithCtx, ControllerProvider, EndpointFn, EndpointInfo, EndpointMetadata, Err, GlobalAcceptMimesMiddleware, HandlerMetadata, HandlerType, HttpServer, HttpsServer, MulterFileSize, MulterOptions, MultipartFile, Next, PLATFORM_ROUTER_OPTIONS, Platform, PlatformAcceptMimesMiddleware, PlatformApplication, PlatformBuilder, PlatformConfiguration, PlatformContext, PlatformControllerBuilder, PlatformHandler, PlatformModule, PlatformMulterMiddleware, PlatformRequest, PlatformResponse, PlatformRouteDetails, PlatformRouter, PlatformTest, PropertyMetadata, ROUTER_OPTIONS, Req, Request, Res, Response, ResponseData, bindEndpointMiddleware, createContext, createHttpServer, createHttpsServer, createInjector, createPlatformApplication, listenHttpServer, listenHttpsServer, listenServer, loadInjector, mapReturnedResponse, printRoutes, useCtxHandler };
3409
+ export { AcceptMime, AcceptMimesMiddleware, AnyToPromiseWithCtx, ControllerProvider, EndpointFn, EndpointInfo, EndpointMetadata, Err, GlobalAcceptMimesMiddleware, HandlerMetadata, HandlerType, HttpServer, HttpsServer, MulterFileSize, MulterOptions, MultipartFile, Next, PLATFORM_ROUTER_OPTIONS, Platform, PlatformAcceptMimesMiddleware, PlatformApplication, PlatformBuilder, PlatformConfiguration, PlatformContext, PlatformHandler, PlatformMiddlewaresChain, PlatformModule, PlatformMulterMiddleware, PlatformRequest, PlatformResponse, PlatformRouteDetails, PlatformRouter, PlatformTest, PropertyMetadata, ROUTER_OPTIONS, Req, Request, Res, Response, ResponseData, bindEndpointMiddleware, buildRouter, createContext, createHttpServer, createHttpsServer, createInjector, createPlatformApplication, createRouter, getRouter, listenHttpServer, listenHttpsServer, listenServer, loadInjector, mapReturnedResponse, printRoutes, useCtxHandler };
3417
3410
  //# sourceMappingURL=index.modern.js.map