@tsed/common 6.82.0 → 6.85.0

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, ProviderType, ProviderScope, Injectable, Inject, InjectorService, Constant, Scope, DIContext, Provider, GlobalProviders, setLoggerLevel, registerProvider, Configuration, createContainer, Container, getConfiguration, DITest } from '@tsed/di';
4
+ import { DIConfiguration, Injectable, ProviderScope, Inject, InjectorService, Constant, Scope, DIContext, Provider, ProviderType, GlobalProviders, 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';
@@ -17,8 +17,7 @@ import { importProviders } from '@tsed/components-scan';
17
17
  export * from '@tsed/components-scan';
18
18
  export * from '@tsed/platform-log-middleware';
19
19
  import { __decorate, __metadata, __param } from 'tslib';
20
- import { Env, AnyToPromise, AnyToPromiseStatus, nameOf, Store, descriptorOf, prototypeOf, deepMerge, isFunction, DecoratorTypes, isStream, uniq, isBoolean, isNumber, isString, getValue, toMap, StoreMerge, Metadata, useDecorators, useMethodDecorators, StoreSet, isClass } from '@tsed/core';
21
- import { PerfLogger } from '@tsed/perf';
20
+ import { AnyToPromise, AnyToPromiseStatus, nameOf, Store, descriptorOf, prototypeOf, deepMerge, isFunction, DecoratorTypes, isStream, uniq, isBoolean, isNumber, isString, getValue, Env, toMap, StoreMerge, Metadata, useDecorators, useMethodDecorators, StoreSet, isClass } from '@tsed/core';
22
21
  import { Exception, NotAcceptable } from '@tsed/exceptions';
23
22
  import { JsonParameterStore, JsonMethodStore, JsonEntityStore, JsonEntityComponent, getOperationsRoutes, OperationMethods, JsonOperationRoute, JsonPropertyStore, getProperties, concatPath, Returns, Consumes, Produces, JsonEntityFn } from '@tsed/schema';
24
23
  export { All, Delete, Get, Head, Location, Options, Patch, Post, Put, Redirect } from '@tsed/schema';
@@ -82,27 +81,7 @@ const rootDir = process.cwd();
82
81
  let PlatformConfiguration = PlatformConfiguration_1 = class PlatformConfiguration extends DIConfiguration {
83
82
  constructor() {
84
83
  super({
85
- rootDir,
86
- env: process.env.NODE_ENV || Env.DEV,
87
- httpPort: 8080,
88
- httpsPort: false,
89
- scopes: {
90
- [ProviderType.CONTROLLER]: ProviderScope.SINGLETON
91
- },
92
- logger: {
93
- debug: false,
94
- level: "info",
95
- logRequest: true,
96
- jsonIndentation: process.env.NODE_ENV === Env.PROD ? 0 : 2
97
- },
98
- errors: {
99
- headerName: "errors"
100
- },
101
- mount: {
102
- "/rest": "${rootDir}/controllers/**/*.ts"
103
- },
104
- exclude: ["**/*.spec.ts", "**/*.spec.js"],
105
- componentsScan: ["${rootDir}/mvc/**/*.ts", "${rootDir}/services/**/*.ts", "${rootDir}/middlewares/**/*.ts"]
84
+ rootDir
106
85
  });
107
86
  }
108
87
 
@@ -2500,45 +2479,81 @@ function getStaticsOptions(statics) {
2500
2479
  }, []);
2501
2480
  }
2502
2481
 
2503
- const {
2504
- bind,
2505
- start,
2506
- end,
2507
- log
2508
- } = PerfLogger.get("bootstrap");
2482
+ function getConfiguration(configuration = {}, module = null) {
2483
+ const store = module ? Store.from(module).get("configuration") || {} : {};
2484
+ const env = configuration.env || store.mount || process.env.NODE_ENV || Env.DEV;
2485
+
2486
+ const config = _extends({
2487
+ exclude: ["**/*.spec.ts", "**/*.spec.js"],
2488
+ componentsScan: ["${rootDir}/mvc/**/*.ts", "${rootDir}/services/**/*.ts", "${rootDir}/middlewares/**/*.ts"],
2489
+ env,
2490
+ httpPort: 8080,
2491
+ httpsPort: false
2492
+ }, store, configuration, {
2493
+ scopes: _extends({
2494
+ [ProviderType.CONTROLLER]: ProviderScope.SINGLETON
2495
+ }, store.scopes, configuration.scopes),
2496
+ mount: store.mount || configuration.mount ? deepMerge(store.mount || {}, configuration.mount || {}) : {
2497
+ "/rest": "${rootDir}/controllers/**/*.ts"
2498
+ },
2499
+ logger: _extends({
2500
+ debug: false,
2501
+ level: env === Env.TEST ? "off" : "info",
2502
+ logRequest: true
2503
+ }, store.logger, configuration.logger, {
2504
+ jsonIndentation: process.env.NODE_ENV === Env.PROD ? 0 : 2
2505
+ })
2506
+ });
2507
+
2508
+ if (!configuration.disableComponentsScan && store.componentsScan && configuration.componentsScan) {
2509
+ config.componentsScan = [...store.componentsScan, ...configuration.componentsScan];
2510
+ }
2511
+
2512
+ return config;
2513
+ }
2514
+
2515
+ const DEFAULT_PROVIDERS = [{
2516
+ provide: PlatformHandler
2517
+ }, {
2518
+ provide: PlatformResponse
2519
+ }, {
2520
+ provide: PlatformRequest
2521
+ }, {
2522
+ provide: PlatformRouter
2523
+ }, {
2524
+ provide: PlatformApplication
2525
+ }, {
2526
+ provide: Platform
2527
+ }];
2509
2528
  /**
2510
2529
  * @platform
2511
2530
  */
2512
2531
 
2513
2532
  var _injector = /*#__PURE__*/_classPrivateFieldLooseKey("injector");
2514
2533
 
2515
- var _providers = /*#__PURE__*/_classPrivateFieldLooseKey("providers");
2516
-
2517
2534
  var _rootModule = /*#__PURE__*/_classPrivateFieldLooseKey("rootModule");
2518
2535
 
2519
2536
  class PlatformBuilder {
2520
2537
  constructor({
2521
2538
  name,
2522
- providers
2539
+ providers,
2540
+ settings,
2541
+ module
2523
2542
  }) {
2524
2543
  Object.defineProperty(this, _injector, {
2525
2544
  writable: true,
2526
2545
  value: void 0
2527
2546
  });
2528
- Object.defineProperty(this, _providers, {
2529
- writable: true,
2530
- value: void 0
2531
- });
2532
2547
  Object.defineProperty(this, _rootModule, {
2533
2548
  writable: true,
2534
2549
  value: void 0
2535
2550
  });
2536
2551
  this.name = "";
2537
2552
  this.startedAt = new Date();
2553
+ this.current = new Date();
2538
2554
  this.name = name;
2539
- _classPrivateFieldLooseBase(this, _providers)[_providers] = toMap(providers, "provide");
2540
- this.locals = new Container();
2541
- this.useProvider(PlatformHandler, _classPrivateFieldLooseBase(this, _providers)[_providers].get(PlatformHandler)).useProvider(PlatformResponse, _classPrivateFieldLooseBase(this, _providers)[_providers].get(PlatformResponse)).useProvider(PlatformRequest, _classPrivateFieldLooseBase(this, _providers)[_providers].get(PlatformRequest)).useProvider(PlatformRouter, _classPrivateFieldLooseBase(this, _providers)[_providers].get(PlatformRouter)).useProvider(PlatformApplication, _classPrivateFieldLooseBase(this, _providers)[_providers].get(PlatformApplication)).useProvider(Platform, _classPrivateFieldLooseBase(this, _providers)[_providers].get(Platform));
2555
+ _classPrivateFieldLooseBase(this, _rootModule)[_rootModule] = module;
2556
+ this.createInjector(module, providers, settings);
2542
2557
  }
2543
2558
 
2544
2559
  get injector() {
@@ -2593,15 +2608,25 @@ class PlatformBuilder {
2593
2608
  return (_this$settings$logger = this.settings.logger) == null ? void 0 : _this$settings$logger.disableBootstrapLog;
2594
2609
  }
2595
2610
 
2596
- static build(platformBuildClass) {
2611
+ static build(platformBuildClass, module, settings = {}) {
2597
2612
  return new platformBuildClass({
2598
2613
  name: nameOf(platformBuildClass).replace("Platform", "").toLowerCase(),
2614
+ module,
2615
+ settings,
2599
2616
  providers: platformBuildClass.providers
2600
2617
  });
2601
2618
  }
2602
2619
 
2620
+ callback(req, res) {
2621
+ if (req && res) {
2622
+ return this.app.callback()(req, res);
2623
+ }
2624
+
2625
+ return this.app.callback();
2626
+ }
2627
+
2603
2628
  log(...data) {
2604
- return this.disableBootstrapLog && this.logger.info(...data);
2629
+ return !this.disableBootstrapLog && this.logger.info(...data, this.diff());
2605
2630
  }
2606
2631
  /**
2607
2632
  * Add classes to the components list
@@ -2647,6 +2672,7 @@ class PlatformBuilder {
2647
2672
  this.useRouter();
2648
2673
  await this.loadRoutes();
2649
2674
  await this.logRoutes();
2675
+ return this;
2650
2676
  }
2651
2677
 
2652
2678
  async loadInjector() {
@@ -2660,7 +2686,7 @@ class PlatformBuilder {
2660
2686
  scope: ProviderScope.SINGLETON
2661
2687
  });
2662
2688
  await injector.load(container);
2663
- this.log("Settings and injector loaded");
2689
+ this.log("Settings and injector loaded...");
2664
2690
  await this.callHook("$afterInit");
2665
2691
  }
2666
2692
 
@@ -2669,7 +2695,6 @@ class PlatformBuilder {
2669
2695
  await this.listenServers();
2670
2696
  await this.callHook("$afterListen");
2671
2697
  await this.ready();
2672
- end();
2673
2698
  }
2674
2699
 
2675
2700
  async stop() {
@@ -2690,16 +2715,15 @@ class PlatformBuilder {
2690
2715
  const {
2691
2716
  injector
2692
2717
  } = this;
2693
- log(hook);
2694
2718
 
2695
2719
  if (!this.disableBootstrapLog) {
2696
- injector.logger.info(`\x1B[1mCall hook ${hook}\x1B[22m`);
2720
+ injector.logger.debug(`\x1B[1mCall hook ${hook}\x1B[22m`);
2697
2721
  } // Load middlewares for the given hook
2698
2722
 
2699
2723
 
2700
2724
  this.loadMiddlewaresFor(hook); // call hooks added by providers
2701
2725
 
2702
- await injector.emit(hook);
2726
+ await injector.emit(hook, ...args);
2703
2727
  }
2704
2728
 
2705
2729
  async loadStatics(hook) {
@@ -2715,10 +2739,20 @@ class PlatformBuilder {
2715
2739
  }
2716
2740
 
2717
2741
  useProvider(token, settings) {
2718
- this.locals.addProvider(token, settings);
2742
+ this.injector.addProvider(token, settings);
2719
2743
  return this;
2720
2744
  }
2721
2745
 
2746
+ async bootstrap() {
2747
+ return this.runLifecycle();
2748
+ }
2749
+
2750
+ diff() {
2751
+ const ms = colors.yellow(`+${new Date().getTime() - this.current.getTime()}ms`);
2752
+ this.current = new Date();
2753
+ return ms;
2754
+ }
2755
+
2722
2756
  async importProviders() {
2723
2757
  this.injector.logger.debug("Scan components");
2724
2758
  const providers = await importProviders(this.injector.settings, ["imports", "mount", "componentsScan"]);
@@ -2730,6 +2764,7 @@ class PlatformBuilder {
2730
2764
  token
2731
2765
  }));
2732
2766
  this.settings.set("routes", routes);
2767
+ this.log("Providers loaded...");
2733
2768
  }
2734
2769
  /**
2735
2770
  * Load middlewares from configuration for the given hook
@@ -2754,25 +2789,6 @@ class PlatformBuilder {
2754
2789
  return this;
2755
2790
  }
2756
2791
 
2757
- async bootstrap(module, settings = {}) {
2758
- var _settings$logger;
2759
-
2760
- // istanbul ignore next
2761
- if ((_settings$logger = settings.logger) != null && _settings$logger.perf) {
2762
- start();
2763
- bind(this);
2764
- settings.logger = _extends({}, settings.logger, {
2765
- level: "off"
2766
- });
2767
- }
2768
-
2769
- this.createInjector(module, _extends({}, settings, {
2770
- PLATFORM_NAME: this.name
2771
- }));
2772
- await this.runLifecycle();
2773
- return this;
2774
- }
2775
-
2776
2792
  async listenServers() {
2777
2793
  await Promise.all([listenHttpServer(this.injector), listenHttpsServer(this.injector)]);
2778
2794
  }
@@ -2784,9 +2800,9 @@ class PlatformBuilder {
2784
2800
  logger,
2785
2801
  platform
2786
2802
  } = this;
2803
+ this.log("Routes mounted...");
2787
2804
 
2788
2805
  if (!((_this$settings$logger2 = this.settings.logger) != null && _this$settings$logger2.disableRoutesSummary) && !this.disableBootstrapLog) {
2789
- logger.info("Routes mounted :");
2790
2806
  logger.info(printRoutes(await this.injector.alterAsync("$logRoutes", platform.getRoutes())));
2791
2807
  }
2792
2808
  }
@@ -2816,17 +2832,18 @@ class PlatformBuilder {
2816
2832
  await this.callHook("$afterRoutesInit");
2817
2833
  }
2818
2834
 
2819
- createInjector(module, settings) {
2820
- _classPrivateFieldLooseBase(this, _rootModule)[_rootModule] = module;
2821
- const configuration = getConfiguration(module, settings);
2822
- _classPrivateFieldLooseBase(this, _injector)[_injector] = createInjector(configuration); // configure locals providers
2823
-
2824
- this.locals.forEach(provider => {
2825
- this.injector.addProvider(provider.token, provider);
2835
+ createInjector(module, providers, settings) {
2836
+ const configuration = getConfiguration(settings, module);
2837
+ configuration.PLATFORM_NAME = this.name;
2838
+ _classPrivateFieldLooseBase(this, _injector)[_injector] = createInjector(configuration);
2839
+ providers = [...DEFAULT_PROVIDERS, ...providers];
2840
+ toMap(providers, "provide").forEach((provider, token) => {
2841
+ this.injector.addProvider(token, provider);
2826
2842
  });
2827
2843
  createPlatformApplication(this.injector);
2828
2844
  createHttpsServer(this.injector);
2829
2845
  createHttpServer(this.injector);
2846
+ this.log("Injector created...");
2830
2847
  }
2831
2848
 
2832
2849
  }
@@ -3202,7 +3219,7 @@ function mapReturnedResponse(_ref) {
3202
3219
 
3203
3220
  class PlatformTest extends DITest {
3204
3221
  static async create(options = {}) {
3205
- DITest.injector = PlatformTest.createInjector(options);
3222
+ DITest.injector = PlatformTest.createInjector(getConfiguration(options, null));
3206
3223
  const container = createContainer();
3207
3224
  await DITest.injector.load(container);
3208
3225
  }
@@ -3234,7 +3251,8 @@ class PlatformTest extends DITest {
3234
3251
  } // @ts-ignore
3235
3252
 
3236
3253
 
3237
- instance = await PlatformBuilder.build(platform).bootstrap(mod, DITest.configure(settings));
3254
+ settings = DITest.configure(settings);
3255
+ instance = await PlatformBuilder.build(platform, mod, settings).bootstrap();
3238
3256
 
3239
3257
  if (!settings.listen) {
3240
3258
  await instance.callHook("$beforeListen");