@tsed/common 6.75.12 → 6.76.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, setLoggerLevel, GlobalProviders, registerProvider, Configuration, createContainer, Container, getConfiguration, DITest } from '@tsed/di';
4
+ import { DIConfiguration, ProviderType, ProviderScope, Injectable, Inject, InjectorService, Constant, Scope, DIContext, Provider, GlobalProviders, setLoggerLevel, registerProvider, Configuration, createContainer, Container, getConfiguration, DITest } from '@tsed/di';
5
5
  export * from '@tsed/di';
6
6
  import { ParamTypes, ParamMetadata, PlatformParams, Context, UseParam, mapParamsOptions } from '@tsed/platform-params';
7
7
  export * from '@tsed/platform-params';
@@ -17,10 +17,10 @@ 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, classOf, prototypeOf, deepMerge, isFunction, DecoratorTypes, isStream, uniq, isBoolean, isNumber, isString, getValue, toMap, StoreMerge, Metadata, useDecorators, useMethodDecorators, isClass } from '@tsed/core';
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
21
  import { PerfLogger } from '@tsed/perf';
22
22
  import { Exception, NotAcceptable } from '@tsed/exceptions';
23
- import { JsonEntityStore, getOperationsStores, JsonEntityComponent, OperationMethods, getProperties, Returns, Consumes, JsonEntityFn, Produces } from '@tsed/schema';
23
+ import { JsonEntityStore, JsonEntityComponent, getOperationsRoutes, OperationMethods, JsonOperationRoute, getProperties, concatPath, Returns, Consumes, Produces, JsonEntityFn } from '@tsed/schema';
24
24
  export { All, Delete, Get, Head, Location, Options, Patch, Post, Put, Redirect } from '@tsed/schema';
25
25
  import { promisify } from 'util';
26
26
  import Http, { ServerResponse, IncomingMessage } from 'http';
@@ -492,7 +492,6 @@ class HandlerMetadata {
492
492
 
493
493
  }
494
494
 
495
- var EndpointMetadata_1;
496
495
  /**
497
496
  * EndpointMetadata contains metadata about a controller and his method.
498
497
  * Each annotation (@Get, @Body...) attached to a method are stored in a endpoint.
@@ -510,7 +509,7 @@ var EndpointMetadata_1;
510
509
  *
511
510
  */
512
511
 
513
- let EndpointMetadata = EndpointMetadata_1 = class EndpointMetadata extends JsonEntityStore {
512
+ let EndpointMetadata = class EndpointMetadata extends JsonEntityStore {
514
513
  constructor(options) {
515
514
  super(_extends({
516
515
  store: Store.fromMethod(options.target, options.propertyKey),
@@ -564,21 +563,6 @@ let EndpointMetadata = EndpointMetadata_1 = class EndpointMetadata extends JsonE
564
563
  set acceptMimes(mimes) {
565
564
  this.store.set("acceptMimes", mimes);
566
565
  }
567
- /**
568
- * Get all endpoints from a given class and his parents.
569
- * @param {Type<any>} target
570
- * @returns {EndpointMetadata[]}
571
- */
572
-
573
-
574
- static getEndpoints(target) {
575
- const operations = getOperationsStores(target);
576
- return Array.from(operations.values()).map(endpoint => {
577
- endpoint = endpoint.clone();
578
- endpoint.token = classOf(target);
579
- return endpoint;
580
- });
581
- }
582
566
  /**
583
567
  * Get an endpoint.
584
568
  * @param target
@@ -640,26 +624,8 @@ let EndpointMetadata = EndpointMetadata_1 = class EndpointMetadata extends JsonE
640
624
  return this;
641
625
  }
642
626
 
643
- clone() {
644
- const endpoint = new EndpointMetadata_1(_extends({}, this, {
645
- target: this.target,
646
- propertyKey: this.propertyKey,
647
- descriptor: this.descriptor,
648
- store: this.store,
649
- children: this.children
650
- }));
651
- endpoint.collectionType = this.collectionType;
652
- endpoint._type = this._type;
653
- endpoint._operation = this.operation;
654
- endpoint._schema = this._schema;
655
- endpoint.middlewares = [...this.middlewares];
656
- endpoint.afterMiddlewares = [...this.afterMiddlewares];
657
- endpoint.beforeMiddlewares = [...this.beforeMiddlewares];
658
- return endpoint;
659
- }
660
-
661
627
  };
662
- EndpointMetadata = EndpointMetadata_1 = __decorate([JsonEntityComponent(DecoratorTypes.METHOD), __metadata("design:paramtypes", [Object])], EndpointMetadata);
628
+ EndpointMetadata = __decorate([JsonEntityComponent(DecoratorTypes.METHOD), __metadata("design:paramtypes", [Object])], EndpointMetadata);
663
629
 
664
630
  function isMetadata(input) {
665
631
  return input instanceof HandlerMetadata;
@@ -677,7 +643,7 @@ function createHandlerMetadata(injector, obj, routeOptions = {}) {
677
643
  let options;
678
644
 
679
645
  if (obj instanceof EndpointMetadata) {
680
- const provider = injector.getProvider(obj.token);
646
+ const provider = injector.getProvider(routeOptions.token);
681
647
  options = {
682
648
  token: provider.token,
683
649
  target: provider.useClass,
@@ -1029,6 +995,7 @@ PlatformHandler = __decorate([Injectable({
1029
995
  scope: ProviderScope.SINGLETON
1030
996
  }), __metadata("design:paramtypes", [InjectorService, PlatformParams])], PlatformHandler);
1031
997
 
998
+ const _excluded$3 = ["method", "path", "handlers"];
1032
999
  var PlatformRouter_1;
1033
1000
  /**
1034
1001
  * @ignore
@@ -1080,15 +1047,15 @@ let PlatformRouter = PlatformRouter_1 = class PlatformRouter {
1080
1047
  const {
1081
1048
  method,
1082
1049
  path,
1083
- handlers,
1084
- isFinal
1085
- } = options; // @ts-ignore
1050
+ handlers
1051
+ } = options,
1052
+ opts = _objectWithoutPropertiesLoose(options, _excluded$3); // @ts-ignore
1053
+
1086
1054
 
1087
- this.getRouter()[method](path, ...this.mapHandlers(handlers, {
1055
+ this.getRouter()[method](path, ...this.mapHandlers(handlers, _extends({
1088
1056
  method,
1089
- path,
1090
- isFinal
1091
- }));
1057
+ path
1058
+ }, opts)));
1092
1059
  return this;
1093
1060
  }
1094
1061
 
@@ -1377,7 +1344,6 @@ class PlatformControllerBuilder {
1377
1344
  this.provider = provider;
1378
1345
  }
1379
1346
  /**
1380
- *
1381
1347
  * @returns {any}
1382
1348
  */
1383
1349
 
@@ -1397,48 +1363,32 @@ class PlatformControllerBuilder {
1397
1363
  }
1398
1364
 
1399
1365
  buildEndpoints() {
1400
- const {
1401
- endpoints
1402
- } = this.provider;
1403
- const operationPaths = new Map();
1404
-
1405
- const getKey = (method, path) => `${method}-${path}`;
1406
-
1407
- const updateFinalRouteState = key => {
1408
- if (operationPaths.has(key)) {
1409
- operationPaths.get(key).isFinal = false;
1410
- }
1411
- };
1412
-
1413
- const setFinalRoute = (key, operationPath) => {
1414
- operationPaths.set(key, operationPath);
1415
- operationPath.isFinal = true;
1416
- };
1417
-
1418
- endpoints.forEach(({
1419
- operation
1420
- }) => {
1421
- operation == null ? void 0 : operation.operationPaths.forEach(operationPath => {
1422
- if (operationPath.method !== OperationMethods.CUSTOM) {
1423
- const key = getKey(operationPath.method, operationPath.path);
1424
- updateFinalRouteState(key);
1425
- updateFinalRouteState(getKey(OperationMethods.ALL, operationPath.path));
1426
- setFinalRoute(key, operationPath);
1427
- }
1428
- });
1429
- });
1430
- endpoints.forEach(endpoint => {
1431
- this.buildEndpoint(endpoint);
1366
+ getOperationsRoutes(this.provider.token).forEach(operationRoute => {
1367
+ this.buildEndpoint(operationRoute);
1432
1368
  });
1433
1369
  return this;
1434
1370
  }
1435
1371
 
1436
- buildEndpoint(endpoint) {
1372
+ buildEndpoint(operationRoute) {
1373
+ let handlers = this.getMiddlewares(operationRoute);
1374
+ const router = this.provider.getRouter();
1375
+ router.addRoute({
1376
+ handlers,
1377
+ token: operationRoute.token,
1378
+ method: formatMethod(operationRoute.method),
1379
+ path: operationRoute.path,
1380
+ isFinal: operationRoute.isFinal
1381
+ });
1382
+ }
1383
+
1384
+ getMiddlewares(operationRoute) {
1385
+ const {
1386
+ endpoint
1387
+ } = operationRoute;
1437
1388
  const {
1438
1389
  beforeMiddlewares,
1439
1390
  middlewares: mldwrs,
1440
- afterMiddlewares,
1441
- operation
1391
+ afterMiddlewares
1442
1392
  } = endpoint;
1443
1393
  const {
1444
1394
  middlewares: {
@@ -1446,35 +1396,15 @@ class PlatformControllerBuilder {
1446
1396
  useAfter
1447
1397
  }
1448
1398
  } = this.provider;
1449
- const router = this.provider.getRouter(); // Endpoint lifecycle
1450
-
1451
- let handlers = [];
1452
- const hasFiles = [...endpoint.children.values()].find(item => item.paramType === ParamTypes.FILES);
1453
- handlers = handlers.concat(useCtxHandler(bindEndpointMiddleware(endpoint))).concat(PlatformAcceptMimesMiddleware).concat(hasFiles && PlatformMulterMiddleware).concat(beforeMiddlewares) // Endpoint before-middlewares
1399
+ const hasFiles = [...operationRoute.endpoint.children.values()].find(item => item.paramType === ParamTypes.FILES);
1400
+ return [].concat(useCtxHandler(bindEndpointMiddleware(endpoint))).concat(PlatformAcceptMimesMiddleware).concat(hasFiles && PlatformMulterMiddleware).concat(beforeMiddlewares) // Endpoint before-middlewares
1454
1401
  .concat(use) // Controller use-middlewares
1455
1402
  // .concat(endpoint.cache && PlatformCacheMiddleware)
1456
1403
  .concat(mldwrs) // Endpoint middlewares
1457
1404
  .concat(endpoint) // Endpoint metadata
1458
1405
  .concat(afterMiddlewares) // Endpoint after-middlewares
1459
1406
  .concat(useAfter) // Controller after middlewares (equivalent to afterEach)
1460
- .filter(item => !!item); // Add handlers to the router
1461
-
1462
- operation == null ? void 0 : operation.operationPaths.forEach(({
1463
- path,
1464
- method,
1465
- isFinal
1466
- }) => {
1467
- router.addRoute({
1468
- method: formatMethod(method),
1469
- path,
1470
- handlers,
1471
- isFinal
1472
- });
1473
- });
1474
-
1475
- if (!(operation != null && operation.operationPaths.size)) {
1476
- router.use(...handlers);
1477
- }
1407
+ .filter(item => !!item);
1478
1408
  }
1479
1409
 
1480
1410
  buildChildrenCtrls(injector) {
@@ -2079,18 +2009,14 @@ class PlatformContext extends DIContext {
2079
2009
 
2080
2010
  }
2081
2011
 
2082
- class PlatformRouteDetails {
2083
- constructor({
2084
- provider,
2085
- endpoint,
2086
- method,
2087
- url
2088
- }) {
2089
- this.provider = provider;
2090
- this.endpoint = endpoint;
2091
- this.method = method;
2092
- this.url = url;
2093
- this.rawBody = !!endpoint.params.find(param => param.paramType === ParamTypes.RAW_BODY);
2012
+ class PlatformRouteDetails extends JsonOperationRoute {
2013
+ constructor(options = {}) {
2014
+ super(options);
2015
+ this.rawBody = !!this.endpoint.params.find(param => param.paramType === ParamTypes.RAW_BODY);
2016
+ }
2017
+
2018
+ get url() {
2019
+ return this.fullPath;
2094
2020
  }
2095
2021
 
2096
2022
  get name() {
@@ -2129,37 +2055,6 @@ class ControllerProvider extends Provider {
2129
2055
  constructor(provide, options = {}) {
2130
2056
  super(provide, options);
2131
2057
  this.type = ProviderType.CONTROLLER;
2132
- this.entity = JsonEntityStore.from(provide);
2133
- }
2134
-
2135
- get path() {
2136
- return this.entity.path;
2137
- }
2138
-
2139
- set path(path) {
2140
- this.entity.path = path;
2141
- }
2142
- /**
2143
- *
2144
- * @returns {EndpointMetadata[]}
2145
- */
2146
-
2147
-
2148
- get endpoints() {
2149
- return EndpointMetadata.getEndpoints(this.provide);
2150
- }
2151
-
2152
- get children() {
2153
- return this.store.get("childrenControllers", []);
2154
- }
2155
- /**
2156
- *
2157
- * @returns {ControllerProvider}
2158
- */
2159
-
2160
-
2161
- get parent() {
2162
- return this.store.get("parentController");
2163
2058
  }
2164
2059
  /**
2165
2060
  *
@@ -2207,24 +2102,6 @@ class ControllerProvider extends Provider {
2207
2102
  });
2208
2103
  this.store.set("middlewares", mdlwrs);
2209
2104
  }
2210
- /**
2211
- *
2212
- * @returns {boolean}
2213
- */
2214
-
2215
-
2216
- hasChildren() {
2217
- return !!this.children.length;
2218
- }
2219
- /**
2220
- *
2221
- * @returns {boolean}
2222
- */
2223
-
2224
-
2225
- hasParent() {
2226
- return !!this.store.get("parentController");
2227
- }
2228
2105
 
2229
2106
  getRouter() {
2230
2107
  return this.router;
@@ -2253,9 +2130,12 @@ let PropertyMetadata = class PropertyMetadata extends JsonEntityStore {
2253
2130
  };
2254
2131
  PropertyMetadata = __decorate([JsonEntityComponent(DecoratorTypes.PROP)], PropertyMetadata);
2255
2132
 
2256
- function getControllerPath(basePath, provider) {
2257
- return (basePath === provider.path ? provider.path : (basePath || "") + provider.path).replace(/\/\//gi, "/");
2258
- }
2133
+ GlobalProviders.createRegistry(ProviderType.CONTROLLER, ControllerProvider, {
2134
+ onInvoke(provider, locals) {
2135
+ locals.set(PlatformRouter, provider.getRouter());
2136
+ }
2137
+
2138
+ });
2259
2139
 
2260
2140
  /**
2261
2141
  * `Platform` is used to provide all routes collected by annotation `@Controller`.
@@ -2280,42 +2160,44 @@ let Platform = class Platform {
2280
2160
  return this._routes;
2281
2161
  }
2282
2162
 
2283
- $onInit() {
2284
- this.buildControllers();
2285
- }
2286
-
2287
2163
  addRoutes(routes) {
2288
2164
  routes.forEach(routeSettings => {
2289
2165
  this.addRoute(routeSettings.route, routeSettings.token);
2290
2166
  });
2291
2167
  }
2292
2168
 
2293
- addRoute(endpoint, token) {
2169
+ addRoute(basePath, token) {
2294
2170
  const {
2295
2171
  injector
2296
2172
  } = this;
2173
+ const provider = injector.getProvider(token);
2297
2174
 
2298
- if (injector.hasProvider(token)) {
2299
- const provider = injector.getProvider(token);
2300
-
2301
- if (provider.type === ProviderType.CONTROLLER) {
2302
- const route = getControllerPath(endpoint, provider);
2303
-
2304
- if (!provider.hasParent()) {
2305
- const routes = this.buildRoutes(route, provider);
2175
+ if (!provider || provider.hasParent()) {
2176
+ return;
2177
+ }
2306
2178
 
2307
- this._routes.push(...routes);
2179
+ const ctrlPath = concatPath(basePath, JsonEntityStore.from(provider.token).path);
2308
2180
 
2309
- this._controllers.push({
2310
- route,
2311
- provider
2312
- });
2181
+ this._controllers.push({
2182
+ route: ctrlPath,
2183
+ provider
2184
+ });
2313
2185
 
2314
- this.app.use(route, ...[].concat(provider.getRouter().callback()));
2315
- }
2186
+ this.app.use(ctrlPath, ...[].concat(provider.getRouter().callback()));
2187
+ this._routes = getOperationsRoutes(provider.token, {
2188
+ withChildren: true,
2189
+ basePath
2190
+ }).reduce((routes, operationRoute) => {
2191
+ if (injector.hasProvider(token)) {
2192
+ const provider = injector.getProvider(operationRoute.token);
2193
+ const route = new PlatformRouteDetails(_extends({}, operationRoute, {
2194
+ provider
2195
+ }));
2196
+ return routes.concat(route);
2316
2197
  }
2317
- }
2318
2198
 
2199
+ return routes;
2200
+ }, this._routes);
2319
2201
  return this;
2320
2202
  }
2321
2203
  /**
@@ -2331,6 +2213,10 @@ let Platform = class Platform {
2331
2213
  getMountedControllers() {
2332
2214
  return this._controllers;
2333
2215
  }
2216
+
2217
+ $onInit() {
2218
+ this.buildControllers();
2219
+ }
2334
2220
  /**
2335
2221
  * Create routers from the collected controllers.
2336
2222
  * @private
@@ -2361,38 +2247,6 @@ let Platform = class Platform {
2361
2247
  }
2362
2248
  });
2363
2249
  }
2364
- /**
2365
- *
2366
- * @param ctrl
2367
- * @param endpointUrl
2368
- */
2369
-
2370
-
2371
- buildRoutes(endpointUrl, ctrl) {
2372
- const {
2373
- injector
2374
- } = this;
2375
- let routes = [];
2376
- routes = ctrl.children.map(ctrl => injector.getProvider(ctrl)).reduce((routes, provider) => {
2377
- return routes.concat(this.buildRoutes(`${endpointUrl}${provider.path}`, provider));
2378
- }, routes);
2379
- ctrl.endpoints.forEach(endpoint => {
2380
- endpoint.operationPaths.forEach(({
2381
- path,
2382
- method
2383
- }) => {
2384
- if (method) {
2385
- routes.push(new PlatformRouteDetails({
2386
- provider: ctrl,
2387
- endpoint,
2388
- method,
2389
- url: `${endpointUrl}${path || ""}`.replace(/\/\//gi, "/")
2390
- }));
2391
- }
2392
- });
2393
- });
2394
- return routes;
2395
- }
2396
2250
 
2397
2251
  };
2398
2252
  Platform = __decorate([Injectable({
@@ -3156,16 +3010,6 @@ function MultipartFile(name, maxCount) {
3156
3010
  };
3157
3011
  }
3158
3012
 
3159
- /**
3160
- *
3161
- * @param fn
3162
- * @decorator
3163
- */
3164
-
3165
- function EndpointFn(fn) {
3166
- return JsonEntityFn(fn);
3167
- }
3168
-
3169
3013
  /**
3170
3014
  * Set a mime list which are acceptable and checks if the specified content types are acceptable, based on the request’s Accept HTTP header field.
3171
3015
  *
@@ -3186,9 +3030,18 @@ function EndpointFn(fn) {
3186
3030
  */
3187
3031
 
3188
3032
  function AcceptMime(...mimes) {
3189
- return useDecorators(Produces(...mimes), EndpointFn(endpoint => {
3190
- endpoint.acceptMimes = mimes;
3191
- }));
3033
+ return useDecorators(Produces(...mimes), StoreSet("acceptMimes", mimes));
3034
+ }
3035
+
3036
+ /**
3037
+ *
3038
+ * @param fn
3039
+ * @decorator
3040
+ * @deprecated Deprecated since 2021-10-05. Use JsonEntityFn instead.
3041
+ */
3042
+
3043
+ function EndpointFn(fn) {
3044
+ return JsonEntityFn(fn);
3192
3045
  }
3193
3046
 
3194
3047
  /**
@@ -3561,13 +3414,6 @@ class PlatformTest extends DITest {
3561
3414
 
3562
3415
  }
3563
3416
 
3564
- GlobalProviders.createRegistry(ProviderType.CONTROLLER, ControllerProvider, {
3565
- onInvoke(provider, locals) {
3566
- locals.set(PlatformRouter, provider.getRouter());
3567
- }
3568
-
3569
- });
3570
-
3571
3417
  /**
3572
3418
  * @ignore
3573
3419
  * @deprecated Will be removed in v7