@tsed/common 6.75.12 → 6.78.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.
@@ -8,12 +8,12 @@ export declare class PlatformControllerBuilder {
8
8
  private provider;
9
9
  constructor(provider: ControllerProvider);
10
10
  /**
11
- *
12
11
  * @returns {any}
13
12
  */
14
13
  build(injector: InjectorService): PlatformRouterMethods;
15
14
  private buildEndpoints;
16
15
  private buildEndpoint;
16
+ private getMiddlewares;
17
17
  private buildChildrenCtrls;
18
18
  private buildMiddlewares;
19
19
  }
@@ -4,5 +4,6 @@ import { EndpointMetadata } from "../../domain";
4
4
  *
5
5
  * @param fn
6
6
  * @decorator
7
+ * @deprecated Deprecated since 2021-10-05. Use JsonEntityFn instead.
7
8
  */
8
9
  export declare function EndpointFn(fn: (endpoint: EndpointMetadata, parameters: DecoratorParameters) => void): MethodDecorator;
@@ -1,24 +1,8 @@
1
1
  import { ControllerMiddlewares, Provider, TokenProvider } from "@tsed/di";
2
- import { JsonEntityStore } from "@tsed/schema";
3
2
  import { PlatformRouterMethods } from "../interfaces/PlatformRouterMethods";
4
- import { EndpointMetadata } from "./EndpointMetadata";
5
3
  export declare class ControllerProvider<T = any> extends Provider<T> {
6
- readonly entity: JsonEntityStore;
7
4
  private router;
8
5
  constructor(provide: TokenProvider, options?: Partial<Provider>);
9
- get path(): string;
10
- set path(path: string);
11
- /**
12
- *
13
- * @returns {EndpointMetadata[]}
14
- */
15
- get endpoints(): EndpointMetadata[];
16
- get children(): TokenProvider[];
17
- /**
18
- *
19
- * @returns {ControllerProvider}
20
- */
21
- get parent(): TokenProvider | undefined;
22
6
  /**
23
7
  *
24
8
  */
@@ -38,16 +22,6 @@ export declare class ControllerProvider<T = any> extends Provider<T> {
38
22
  * @param middlewares
39
23
  */
40
24
  set middlewares(middlewares: ControllerMiddlewares);
41
- /**
42
- *
43
- * @returns {boolean}
44
- */
45
- hasChildren(): boolean;
46
- /**
47
- *
48
- * @returns {boolean}
49
- */
50
- hasParent(): boolean;
51
25
  getRouter<T extends PlatformRouterMethods = any>(): T;
52
26
  setRouter(router: PlatformRouterMethods): this;
53
27
  }
@@ -46,12 +46,6 @@ export declare class EndpointMetadata extends JsonEntityStore implements Endpoin
46
46
  set view(view: EndpointViewOptions);
47
47
  get acceptMimes(): string[];
48
48
  set acceptMimes(mimes: string[]);
49
- /**
50
- * Get all endpoints from a given class and his parents.
51
- * @param {Type<any>} target
52
- * @returns {EndpointMetadata[]}
53
- */
54
- static getEndpoints(target: Type<any>): EndpointMetadata[];
55
49
  /**
56
50
  * Get an endpoint.
57
51
  * @param target
@@ -84,5 +78,4 @@ export declare class EndpointMetadata extends JsonEntityStore implements Endpoin
84
78
  * @param args
85
79
  */
86
80
  use(args: Function[]): this;
87
- clone(): EndpointMetadata;
88
81
  }
@@ -1,18 +1,17 @@
1
1
  import { EndpointMetadata } from "./EndpointMetadata";
2
2
  import { ControllerProvider } from "./ControllerProvider";
3
+ import { JsonOperationRoute } from "@tsed/schema";
3
4
  export interface PlatformRouterDetailsOptions {
4
5
  provider: ControllerProvider;
5
6
  endpoint: EndpointMetadata;
6
7
  method: string;
7
8
  url: string;
8
9
  }
9
- export declare class PlatformRouteDetails {
10
- readonly method: string;
11
- readonly url: string;
10
+ export declare class PlatformRouteDetails extends JsonOperationRoute<EndpointMetadata> {
12
11
  readonly rawBody: boolean;
13
- readonly endpoint: EndpointMetadata;
14
12
  readonly provider: ControllerProvider;
15
- constructor({ provider, endpoint, method, url }: PlatformRouterDetailsOptions);
13
+ constructor(options?: Partial<PlatformRouteDetails>);
14
+ get url(): string;
16
15
  get name(): string;
17
16
  get className(): string;
18
17
  get methodClassName(): string;
package/lib/index.js CHANGED
@@ -509,7 +509,6 @@ class HandlerMetadata {
509
509
 
510
510
  }
511
511
 
512
- var EndpointMetadata_1;
513
512
  /**
514
513
  * EndpointMetadata contains metadata about a controller and his method.
515
514
  * Each annotation (@Get, @Body...) attached to a method are stored in a endpoint.
@@ -527,7 +526,7 @@ var EndpointMetadata_1;
527
526
  *
528
527
  */
529
528
 
530
- exports.EndpointMetadata = EndpointMetadata_1 = class EndpointMetadata extends schema.JsonEntityStore {
529
+ exports.EndpointMetadata = class EndpointMetadata extends schema.JsonEntityStore {
531
530
  constructor(options) {
532
531
  super(_extends({
533
532
  store: core.Store.fromMethod(options.target, options.propertyKey),
@@ -581,21 +580,6 @@ exports.EndpointMetadata = EndpointMetadata_1 = class EndpointMetadata extends s
581
580
  set acceptMimes(mimes) {
582
581
  this.store.set("acceptMimes", mimes);
583
582
  }
584
- /**
585
- * Get all endpoints from a given class and his parents.
586
- * @param {Type<any>} target
587
- * @returns {EndpointMetadata[]}
588
- */
589
-
590
-
591
- static getEndpoints(target) {
592
- const operations = schema.getOperationsStores(target);
593
- return Array.from(operations.values()).map(endpoint => {
594
- endpoint = endpoint.clone();
595
- endpoint.token = core.classOf(target);
596
- return endpoint;
597
- });
598
- }
599
583
  /**
600
584
  * Get an endpoint.
601
585
  * @param target
@@ -657,26 +641,8 @@ exports.EndpointMetadata = EndpointMetadata_1 = class EndpointMetadata extends s
657
641
  return this;
658
642
  }
659
643
 
660
- clone() {
661
- const endpoint = new EndpointMetadata_1(_extends({}, this, {
662
- target: this.target,
663
- propertyKey: this.propertyKey,
664
- descriptor: this.descriptor,
665
- store: this.store,
666
- children: this.children
667
- }));
668
- endpoint.collectionType = this.collectionType;
669
- endpoint._type = this._type;
670
- endpoint._operation = this.operation;
671
- endpoint._schema = this._schema;
672
- endpoint.middlewares = [...this.middlewares];
673
- endpoint.afterMiddlewares = [...this.afterMiddlewares];
674
- endpoint.beforeMiddlewares = [...this.beforeMiddlewares];
675
- return endpoint;
676
- }
677
-
678
644
  };
679
- exports.EndpointMetadata = EndpointMetadata_1 = tslib.__decorate([schema.JsonEntityComponent(core.DecoratorTypes.METHOD), tslib.__metadata("design:paramtypes", [Object])], exports.EndpointMetadata);
645
+ exports.EndpointMetadata = tslib.__decorate([schema.JsonEntityComponent(core.DecoratorTypes.METHOD), tslib.__metadata("design:paramtypes", [Object])], exports.EndpointMetadata);
680
646
 
681
647
  function isMetadata(input) {
682
648
  return input instanceof HandlerMetadata;
@@ -694,7 +660,7 @@ function createHandlerMetadata(injector, obj, routeOptions = {}) {
694
660
  let options;
695
661
 
696
662
  if (obj instanceof exports.EndpointMetadata) {
697
- const provider = injector.getProvider(obj.token);
663
+ const provider = injector.getProvider(routeOptions.token);
698
664
  options = {
699
665
  token: provider.token,
700
666
  target: provider.useClass,
@@ -1038,6 +1004,7 @@ exports.PlatformHandler = tslib.__decorate([di.Injectable({
1038
1004
  scope: di.ProviderScope.SINGLETON
1039
1005
  }), tslib.__metadata("design:paramtypes", [di.InjectorService, platformParams.PlatformParams])], exports.PlatformHandler);
1040
1006
 
1007
+ const _excluded$3 = ["method", "path", "handlers"];
1041
1008
  var PlatformRouter_1;
1042
1009
  /**
1043
1010
  * @ignore
@@ -1089,15 +1056,15 @@ exports.PlatformRouter = PlatformRouter_1 = class PlatformRouter {
1089
1056
  const {
1090
1057
  method,
1091
1058
  path,
1092
- handlers,
1093
- isFinal
1094
- } = options; // @ts-ignore
1059
+ handlers
1060
+ } = options,
1061
+ opts = _objectWithoutPropertiesLoose(options, _excluded$3); // @ts-ignore
1062
+
1095
1063
 
1096
- this.getRouter()[method](path, ...this.mapHandlers(handlers, {
1064
+ this.getRouter()[method](path, ...this.mapHandlers(handlers, _extends({
1097
1065
  method,
1098
- path,
1099
- isFinal
1100
- }));
1066
+ path
1067
+ }, opts)));
1101
1068
  return this;
1102
1069
  }
1103
1070
 
@@ -1386,7 +1353,6 @@ class PlatformControllerBuilder {
1386
1353
  this.provider = provider;
1387
1354
  }
1388
1355
  /**
1389
- *
1390
1356
  * @returns {any}
1391
1357
  */
1392
1358
 
@@ -1406,48 +1372,32 @@ class PlatformControllerBuilder {
1406
1372
  }
1407
1373
 
1408
1374
  buildEndpoints() {
1409
- const {
1410
- endpoints
1411
- } = this.provider;
1412
- const operationPaths = new Map();
1413
-
1414
- const getKey = (method, path) => `${method}-${path}`;
1415
-
1416
- const updateFinalRouteState = key => {
1417
- if (operationPaths.has(key)) {
1418
- operationPaths.get(key).isFinal = false;
1419
- }
1420
- };
1421
-
1422
- const setFinalRoute = (key, operationPath) => {
1423
- operationPaths.set(key, operationPath);
1424
- operationPath.isFinal = true;
1425
- };
1426
-
1427
- endpoints.forEach(({
1428
- operation
1429
- }) => {
1430
- operation == null ? void 0 : operation.operationPaths.forEach(operationPath => {
1431
- if (operationPath.method !== schema.OperationMethods.CUSTOM) {
1432
- const key = getKey(operationPath.method, operationPath.path);
1433
- updateFinalRouteState(key);
1434
- updateFinalRouteState(getKey(schema.OperationMethods.ALL, operationPath.path));
1435
- setFinalRoute(key, operationPath);
1436
- }
1437
- });
1438
- });
1439
- endpoints.forEach(endpoint => {
1440
- this.buildEndpoint(endpoint);
1375
+ schema.getOperationsRoutes(this.provider.token).forEach(operationRoute => {
1376
+ this.buildEndpoint(operationRoute);
1441
1377
  });
1442
1378
  return this;
1443
1379
  }
1444
1380
 
1445
- buildEndpoint(endpoint) {
1381
+ buildEndpoint(operationRoute) {
1382
+ let handlers = this.getMiddlewares(operationRoute);
1383
+ const router = this.provider.getRouter();
1384
+ router.addRoute({
1385
+ handlers,
1386
+ token: operationRoute.token,
1387
+ method: formatMethod(operationRoute.method),
1388
+ path: operationRoute.path,
1389
+ isFinal: operationRoute.isFinal
1390
+ });
1391
+ }
1392
+
1393
+ getMiddlewares(operationRoute) {
1394
+ const {
1395
+ endpoint
1396
+ } = operationRoute;
1446
1397
  const {
1447
1398
  beforeMiddlewares,
1448
1399
  middlewares: mldwrs,
1449
- afterMiddlewares,
1450
- operation
1400
+ afterMiddlewares
1451
1401
  } = endpoint;
1452
1402
  const {
1453
1403
  middlewares: {
@@ -1455,35 +1405,15 @@ class PlatformControllerBuilder {
1455
1405
  useAfter
1456
1406
  }
1457
1407
  } = this.provider;
1458
- const router = this.provider.getRouter(); // Endpoint lifecycle
1459
-
1460
- let handlers = [];
1461
- const hasFiles = [...endpoint.children.values()].find(item => item.paramType === platformParams.ParamTypes.FILES);
1462
- handlers = handlers.concat(useCtxHandler(bindEndpointMiddleware(endpoint))).concat(exports.PlatformAcceptMimesMiddleware).concat(hasFiles && exports.PlatformMulterMiddleware).concat(beforeMiddlewares) // Endpoint before-middlewares
1408
+ const hasFiles = [...operationRoute.endpoint.children.values()].find(item => item.paramType === platformParams.ParamTypes.FILES);
1409
+ return [].concat(useCtxHandler(bindEndpointMiddleware(endpoint))).concat(exports.PlatformAcceptMimesMiddleware).concat(hasFiles && exports.PlatformMulterMiddleware).concat(beforeMiddlewares) // Endpoint before-middlewares
1463
1410
  .concat(use) // Controller use-middlewares
1464
1411
  // .concat(endpoint.cache && PlatformCacheMiddleware)
1465
1412
  .concat(mldwrs) // Endpoint middlewares
1466
1413
  .concat(endpoint) // Endpoint metadata
1467
1414
  .concat(afterMiddlewares) // Endpoint after-middlewares
1468
1415
  .concat(useAfter) // Controller after middlewares (equivalent to afterEach)
1469
- .filter(item => !!item); // Add handlers to the router
1470
-
1471
- operation == null ? void 0 : operation.operationPaths.forEach(({
1472
- path,
1473
- method,
1474
- isFinal
1475
- }) => {
1476
- router.addRoute({
1477
- method: formatMethod(method),
1478
- path,
1479
- handlers,
1480
- isFinal
1481
- });
1482
- });
1483
-
1484
- if (!(operation != null && operation.operationPaths.size)) {
1485
- router.use(...handlers);
1486
- }
1416
+ .filter(item => !!item);
1487
1417
  }
1488
1418
 
1489
1419
  buildChildrenCtrls(injector) {
@@ -2088,18 +2018,14 @@ class PlatformContext extends di.DIContext {
2088
2018
 
2089
2019
  }
2090
2020
 
2091
- class PlatformRouteDetails {
2092
- constructor({
2093
- provider,
2094
- endpoint,
2095
- method,
2096
- url
2097
- }) {
2098
- this.provider = provider;
2099
- this.endpoint = endpoint;
2100
- this.method = method;
2101
- this.url = url;
2102
- this.rawBody = !!endpoint.params.find(param => param.paramType === platformParams.ParamTypes.RAW_BODY);
2021
+ class PlatformRouteDetails extends schema.JsonOperationRoute {
2022
+ constructor(options = {}) {
2023
+ super(options);
2024
+ this.rawBody = !!this.endpoint.params.find(param => param.paramType === platformParams.ParamTypes.RAW_BODY);
2025
+ }
2026
+
2027
+ get url() {
2028
+ return this.fullPath;
2103
2029
  }
2104
2030
 
2105
2031
  get name() {
@@ -2138,37 +2064,6 @@ class ControllerProvider extends di.Provider {
2138
2064
  constructor(provide, options = {}) {
2139
2065
  super(provide, options);
2140
2066
  this.type = di.ProviderType.CONTROLLER;
2141
- this.entity = schema.JsonEntityStore.from(provide);
2142
- }
2143
-
2144
- get path() {
2145
- return this.entity.path;
2146
- }
2147
-
2148
- set path(path) {
2149
- this.entity.path = path;
2150
- }
2151
- /**
2152
- *
2153
- * @returns {EndpointMetadata[]}
2154
- */
2155
-
2156
-
2157
- get endpoints() {
2158
- return exports.EndpointMetadata.getEndpoints(this.provide);
2159
- }
2160
-
2161
- get children() {
2162
- return this.store.get("childrenControllers", []);
2163
- }
2164
- /**
2165
- *
2166
- * @returns {ControllerProvider}
2167
- */
2168
-
2169
-
2170
- get parent() {
2171
- return this.store.get("parentController");
2172
2067
  }
2173
2068
  /**
2174
2069
  *
@@ -2216,24 +2111,6 @@ class ControllerProvider extends di.Provider {
2216
2111
  });
2217
2112
  this.store.set("middlewares", mdlwrs);
2218
2113
  }
2219
- /**
2220
- *
2221
- * @returns {boolean}
2222
- */
2223
-
2224
-
2225
- hasChildren() {
2226
- return !!this.children.length;
2227
- }
2228
- /**
2229
- *
2230
- * @returns {boolean}
2231
- */
2232
-
2233
-
2234
- hasParent() {
2235
- return !!this.store.get("parentController");
2236
- }
2237
2114
 
2238
2115
  getRouter() {
2239
2116
  return this.router;
@@ -2262,9 +2139,12 @@ exports.PropertyMetadata = class PropertyMetadata extends schema.JsonEntityStore
2262
2139
  };
2263
2140
  exports.PropertyMetadata = tslib.__decorate([schema.JsonEntityComponent(core.DecoratorTypes.PROP)], exports.PropertyMetadata);
2264
2141
 
2265
- function getControllerPath(basePath, provider) {
2266
- return (basePath === provider.path ? provider.path : (basePath || "") + provider.path).replace(/\/\//gi, "/");
2267
- }
2142
+ di.GlobalProviders.createRegistry(di.ProviderType.CONTROLLER, ControllerProvider, {
2143
+ onInvoke(provider, locals) {
2144
+ locals.set(exports.PlatformRouter, provider.getRouter());
2145
+ }
2146
+
2147
+ });
2268
2148
 
2269
2149
  /**
2270
2150
  * `Platform` is used to provide all routes collected by annotation `@Controller`.
@@ -2289,42 +2169,44 @@ exports.Platform = class Platform {
2289
2169
  return this._routes;
2290
2170
  }
2291
2171
 
2292
- $onInit() {
2293
- this.buildControllers();
2294
- }
2295
-
2296
2172
  addRoutes(routes) {
2297
2173
  routes.forEach(routeSettings => {
2298
2174
  this.addRoute(routeSettings.route, routeSettings.token);
2299
2175
  });
2300
2176
  }
2301
2177
 
2302
- addRoute(endpoint, token) {
2178
+ addRoute(basePath, token) {
2303
2179
  const {
2304
2180
  injector
2305
2181
  } = this;
2182
+ const provider = injector.getProvider(token);
2306
2183
 
2307
- if (injector.hasProvider(token)) {
2308
- const provider = injector.getProvider(token);
2309
-
2310
- if (provider.type === di.ProviderType.CONTROLLER) {
2311
- const route = getControllerPath(endpoint, provider);
2312
-
2313
- if (!provider.hasParent()) {
2314
- const routes = this.buildRoutes(route, provider);
2184
+ if (!provider || provider.hasParent()) {
2185
+ return;
2186
+ }
2315
2187
 
2316
- this._routes.push(...routes);
2188
+ const ctrlPath = schema.concatPath(basePath, schema.JsonEntityStore.from(provider.token).path);
2317
2189
 
2318
- this._controllers.push({
2319
- route,
2320
- provider
2321
- });
2190
+ this._controllers.push({
2191
+ route: ctrlPath,
2192
+ provider
2193
+ });
2322
2194
 
2323
- this.app.use(route, ...[].concat(provider.getRouter().callback()));
2324
- }
2195
+ this.app.use(ctrlPath, ...[].concat(provider.getRouter().callback()));
2196
+ this._routes = schema.getOperationsRoutes(provider.token, {
2197
+ withChildren: true,
2198
+ basePath
2199
+ }).reduce((routes, operationRoute) => {
2200
+ if (injector.hasProvider(token)) {
2201
+ const provider = injector.getProvider(operationRoute.token);
2202
+ const route = new PlatformRouteDetails(_extends({}, operationRoute, {
2203
+ provider
2204
+ }));
2205
+ return routes.concat(route);
2325
2206
  }
2326
- }
2327
2207
 
2208
+ return routes;
2209
+ }, this._routes);
2328
2210
  return this;
2329
2211
  }
2330
2212
  /**
@@ -2340,6 +2222,10 @@ exports.Platform = class Platform {
2340
2222
  getMountedControllers() {
2341
2223
  return this._controllers;
2342
2224
  }
2225
+
2226
+ $onInit() {
2227
+ this.buildControllers();
2228
+ }
2343
2229
  /**
2344
2230
  * Create routers from the collected controllers.
2345
2231
  * @private
@@ -2370,38 +2256,6 @@ exports.Platform = class Platform {
2370
2256
  }
2371
2257
  });
2372
2258
  }
2373
- /**
2374
- *
2375
- * @param ctrl
2376
- * @param endpointUrl
2377
- */
2378
-
2379
-
2380
- buildRoutes(endpointUrl, ctrl) {
2381
- const {
2382
- injector
2383
- } = this;
2384
- let routes = [];
2385
- routes = ctrl.children.map(ctrl => injector.getProvider(ctrl)).reduce((routes, provider) => {
2386
- return routes.concat(this.buildRoutes(`${endpointUrl}${provider.path}`, provider));
2387
- }, routes);
2388
- ctrl.endpoints.forEach(endpoint => {
2389
- endpoint.operationPaths.forEach(({
2390
- path,
2391
- method
2392
- }) => {
2393
- if (method) {
2394
- routes.push(new PlatformRouteDetails({
2395
- provider: ctrl,
2396
- endpoint,
2397
- method,
2398
- url: `${endpointUrl}${path || ""}`.replace(/\/\//gi, "/")
2399
- }));
2400
- }
2401
- });
2402
- });
2403
- return routes;
2404
- }
2405
2259
 
2406
2260
  };
2407
2261
  exports.Platform = tslib.__decorate([di.Injectable({
@@ -3165,16 +3019,6 @@ function MultipartFile(name, maxCount) {
3165
3019
  };
3166
3020
  }
3167
3021
 
3168
- /**
3169
- *
3170
- * @param fn
3171
- * @decorator
3172
- */
3173
-
3174
- function EndpointFn(fn) {
3175
- return schema.JsonEntityFn(fn);
3176
- }
3177
-
3178
3022
  /**
3179
3023
  * 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.
3180
3024
  *
@@ -3195,9 +3039,18 @@ function EndpointFn(fn) {
3195
3039
  */
3196
3040
 
3197
3041
  function AcceptMime(...mimes) {
3198
- return core.useDecorators(schema.Produces(...mimes), EndpointFn(endpoint => {
3199
- endpoint.acceptMimes = mimes;
3200
- }));
3042
+ return core.useDecorators(schema.Produces(...mimes), core.StoreSet("acceptMimes", mimes));
3043
+ }
3044
+
3045
+ /**
3046
+ *
3047
+ * @param fn
3048
+ * @decorator
3049
+ * @deprecated Deprecated since 2021-10-05. Use JsonEntityFn instead.
3050
+ */
3051
+
3052
+ function EndpointFn(fn) {
3053
+ return schema.JsonEntityFn(fn);
3201
3054
  }
3202
3055
 
3203
3056
  /**
@@ -3570,13 +3423,6 @@ class PlatformTest extends di.DITest {
3570
3423
 
3571
3424
  }
3572
3425
 
3573
- di.GlobalProviders.createRegistry(di.ProviderType.CONTROLLER, ControllerProvider, {
3574
- onInvoke(provider, locals) {
3575
- locals.set(exports.PlatformRouter, provider.getRouter());
3576
- }
3577
-
3578
- });
3579
-
3580
3426
  /**
3581
3427
  * @ignore
3582
3428
  * @deprecated Will be removed in v7