@tsed/schema 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.
@@ -0,0 +1,11 @@
1
+ import { Type } from "@tsed/core";
2
+ /**
3
+ * Add Children class to compose routes
4
+ *
5
+ * @decorator
6
+ * @swagger
7
+ * @schema
8
+ * @classDecorator
9
+ * @param children
10
+ */
11
+ export declare function Children(...children: Type<any>[]): ClassDecorator;
@@ -1,3 +1,4 @@
1
+ export * from "./class/children";
1
2
  export * from "./collections/collectionContains";
2
3
  export * from "./collections/collectionOf";
3
4
  export * from "./collections/maxItems";
@@ -12,32 +12,30 @@ export declare class JsonEntityStore extends Entity implements JsonEntityStoreOp
12
12
  * List of children JsonEntityStore (properties or methods or params)
13
13
  */
14
14
  readonly children: Map<string | number, JsonEntityStore>;
15
- /**
16
- * Path used to generate open spec.
17
- */
18
- path: string;
19
- constructor(options: JsonEntityStoreOptions);
20
15
  /**
21
16
  * Ref to JsonSchema
22
17
  */
23
18
  protected _schema: JsonSchema;
24
19
  /**
25
- * Return the JsonSchema
20
+ * Ref to JsonOperation when the decorated object is a method.
26
21
  */
27
- get schema(): JsonSchema;
22
+ protected _operation: JsonOperation;
23
+ /**
24
+ * Ref to JsonParameter when the decorated object is a parameter.
25
+ */
26
+ protected _parameter: JsonParameter;
28
27
  [key: string]: any;
28
+ constructor(options: JsonEntityStoreOptions);
29
+ get path(): string;
30
+ set path(path: string);
29
31
  /**
30
- * Ref to JsonOperation when the decorated object is a method.
32
+ * Return the JsonSchema
31
33
  */
32
- protected _operation: JsonOperation;
34
+ get schema(): JsonSchema;
33
35
  /**
34
36
  * Return the JsonOperation
35
37
  */
36
38
  get operation(): JsonOperation | undefined;
37
- /**
38
- * Ref to JsonParameter when the decorated object is a parameter.
39
- */
40
- protected _parameter: JsonParameter;
41
39
  /**
42
40
  * Return the JsonParameter
43
41
  */
@@ -79,6 +77,7 @@ export declare class JsonEntityStore extends Entity implements JsonEntityStoreOp
79
77
  */
80
78
  static from<T extends JsonEntityStore = JsonEntityStore>(...args: any[]): NonNullable<T>;
81
79
  static fromMethod(target: any, propertyKey: string | symbol): JsonEntityStore;
80
+ get<T = any>(key: string, defaultValue?: any): T;
82
81
  getResponseOptions(status: number, contentType?: string): undefined | any;
83
82
  /**
84
83
  * Check precondition between value, required and allowedRequiredValues to know if the entity is required.
@@ -7,6 +7,7 @@ import { JsonSchema } from "./JsonSchema";
7
7
  export interface JsonMethodPath {
8
8
  path: string | RegExp;
9
9
  method: string;
10
+ isFinal?: boolean;
10
11
  [key: string]: any;
11
12
  }
12
13
  export interface JsonOperationOptions extends OS3Operation<JsonSchema, JsonParameter, JsonMap<JsonResponse>> {
@@ -0,0 +1,7 @@
1
+ import { JsonMethodPath } from "./JsonOperation";
2
+ export declare class JsonOperationPathsMap extends Map<string, JsonMethodPath> {
3
+ setOperationPath(operationPath: JsonMethodPath): void;
4
+ protected getKey: (method: string, path: any) => string;
5
+ protected updateFinalRouteState(key: string): void;
6
+ protected setFinalRoute(key: string, operationPath: JsonMethodPath): void;
7
+ }
@@ -0,0 +1,16 @@
1
+ import { Type } from "@tsed/core";
2
+ import { JsonMethodPath } from "./JsonOperation";
3
+ import { JsonEntityStore } from "./JsonEntityStore";
4
+ export declare class JsonOperationRoute<Entity extends JsonEntityStore = JsonEntityStore> {
5
+ readonly token: Type<any>;
6
+ readonly endpoint: Entity;
7
+ readonly operationPath?: JsonMethodPath;
8
+ readonly basePath?: string;
9
+ constructor(options: Partial<JsonOperationRoute>);
10
+ get path(): string | RegExp | undefined;
11
+ get fullPath(): string;
12
+ get method(): string;
13
+ get isFinal(): boolean;
14
+ get propertyKey(): string | symbol;
15
+ get store(): import("@tsed/core").Store;
16
+ }
@@ -7,5 +7,6 @@ export * from "./JsonSchema";
7
7
  export * from "./JsonFormatTypes";
8
8
  export * from "./SpecTypes";
9
9
  export * from "./JsonEntityStore";
10
+ export * from "./JsonOperationRoute";
10
11
  export * from "./JsonParameterTypes";
11
12
  export * from "./JsonLazyRef";
package/lib/index.d.ts CHANGED
@@ -4,12 +4,14 @@ export * from "./utils/getJsonSchema";
4
4
  export * from "./utils/getJsonType";
5
5
  export * from "./utils/getPropertiesStores";
6
6
  export * from "./utils/getOperationsStores";
7
+ export * from "./utils/getOperationsRoutes";
7
8
  export * from "./utils/getSpec";
8
9
  export * from "./utils/mergeSpec";
9
10
  export * from "./utils/generics";
10
11
  export * from "./utils/mapHeaders";
11
12
  export * from "./utils/isSuccessStatus";
12
13
  export * from "./utils/from";
14
+ export * from "./utils/concatPath";
13
15
  export * from "./utils/defineStatusModel";
14
16
  export * from "./domain";
15
17
  export * from "./hooks/alterIgnore";
package/lib/index.js CHANGED
@@ -2015,11 +2015,6 @@ class JsonEntityStore extends core.Entity {
2015
2015
  */
2016
2016
 
2017
2017
  this.children = new Map();
2018
- /**
2019
- * Path used to generate open spec.
2020
- */
2021
-
2022
- this.path = "/";
2023
2018
  this.store = options.store;
2024
2019
  /* istanbul ignore next */
2025
2020
 
@@ -2029,6 +2024,14 @@ class JsonEntityStore extends core.Entity {
2029
2024
 
2030
2025
  this.build();
2031
2026
  }
2027
+
2028
+ get path() {
2029
+ return this.store.get("path", "/");
2030
+ }
2031
+
2032
+ set path(path) {
2033
+ this.store.set("path", path);
2034
+ }
2032
2035
  /**
2033
2036
  * Return the JsonSchema
2034
2037
  */
@@ -2187,6 +2190,10 @@ class JsonEntityStore extends core.Entity {
2187
2190
  return this.from(target, propertyKey, core.descriptorOf(target, propertyKey));
2188
2191
  }
2189
2192
 
2193
+ get(key, defaultValue) {
2194
+ return this.store.get(key, defaultValue);
2195
+ }
2196
+
2190
2197
  getResponseOptions(status, contentType = "application/json") {
2191
2198
  var _this$operation, _this$operation$getRe;
2192
2199
 
@@ -2930,6 +2937,35 @@ function getProperties(target, options = {}) {
2930
2937
  return map;
2931
2938
  }
2932
2939
 
2940
+ class JsonOperationPathsMap extends Map {
2941
+ constructor() {
2942
+ super(...arguments);
2943
+
2944
+ this.getKey = (method, path) => `${method}-${path}`;
2945
+ }
2946
+
2947
+ setOperationPath(operationPath) {
2948
+ if (operationPath.method !== exports.OperationMethods.CUSTOM) {
2949
+ const key = this.getKey(operationPath.method, operationPath.path);
2950
+ this.updateFinalRouteState(key);
2951
+ this.updateFinalRouteState(this.getKey(exports.OperationMethods.ALL, operationPath.path));
2952
+ this.setFinalRoute(key, operationPath);
2953
+ }
2954
+ }
2955
+
2956
+ updateFinalRouteState(key) {
2957
+ if (this.has(key)) {
2958
+ this.get(key).isFinal = false;
2959
+ }
2960
+ }
2961
+
2962
+ setFinalRoute(key, operationPath) {
2963
+ this.set(key, operationPath);
2964
+ operationPath.isFinal = true;
2965
+ }
2966
+
2967
+ }
2968
+
2933
2969
  /**
2934
2970
  * @ignore
2935
2971
  */
@@ -2939,6 +2975,7 @@ function getOperationsStores(target) {
2939
2975
 
2940
2976
  if (!store.$operations) {
2941
2977
  const stores = getInheritedStores(store);
2978
+ const operationPaths = new JsonOperationPathsMap();
2942
2979
  store.$operations = new Map();
2943
2980
  stores.forEach(currentStore => {
2944
2981
  currentStore.children.forEach(propStore => {
@@ -2947,11 +2984,94 @@ function getOperationsStores(target) {
2947
2984
  }
2948
2985
  });
2949
2986
  });
2987
+ store.$operations.forEach(store => {
2988
+ var _store$operation;
2989
+
2990
+ (_store$operation = store.operation) == null ? void 0 : _store$operation.operationPaths.forEach(operationPath => {
2991
+ operationPaths.setOperationPath(operationPath);
2992
+ });
2993
+ });
2994
+ operationPaths.clear();
2950
2995
  }
2951
2996
 
2952
2997
  return store.$operations;
2953
2998
  }
2954
2999
 
3000
+ function concatPath(basePath, path) {
3001
+ return ((basePath || "") + (path || "")).replace(/\/\//gi, "/");
3002
+ }
3003
+
3004
+ class JsonOperationRoute {
3005
+ constructor(options) {
3006
+ Object.assign(this, options);
3007
+ }
3008
+
3009
+ get path() {
3010
+ var _this$operationPath;
3011
+
3012
+ return (_this$operationPath = this.operationPath) == null ? void 0 : _this$operationPath.path;
3013
+ }
3014
+
3015
+ get fullPath() {
3016
+ return concatPath(this.basePath, this.path);
3017
+ }
3018
+
3019
+ get method() {
3020
+ var _this$operationPath2;
3021
+
3022
+ return ((_this$operationPath2 = this.operationPath) == null ? void 0 : _this$operationPath2.method) || "USE";
3023
+ }
3024
+
3025
+ get isFinal() {
3026
+ var _this$operationPath3;
3027
+
3028
+ return !!((_this$operationPath3 = this.operationPath) != null && _this$operationPath3.isFinal);
3029
+ }
3030
+
3031
+ get propertyKey() {
3032
+ return this.endpoint.propertyKey;
3033
+ }
3034
+
3035
+ get store() {
3036
+ return this.endpoint.store;
3037
+ }
3038
+
3039
+ }
3040
+
3041
+ function getOperationsRoutes(token, options = {}) {
3042
+ const store = token.isStore ? token : getJsonEntityStore(token);
3043
+ const basePath = ((options.basePath || "") + (store.path || "")).replace(/\/\//gi, "/");
3044
+ let operationsRoutes = [];
3045
+
3046
+ if (options.withChildren) {
3047
+ const children = store.get("childrenControllers", []);
3048
+ operationsRoutes = children.reduce((operationsRoutes, token) => {
3049
+ return operationsRoutes.concat(getOperationsRoutes(token, _extends({}, options, {
3050
+ basePath
3051
+ })));
3052
+ }, operationsRoutes);
3053
+ }
3054
+
3055
+ return [...getOperationsStores(token).values()].reduce((routes, endpoint) => {
3056
+ const {
3057
+ operation
3058
+ } = endpoint;
3059
+
3060
+ if (operation) {
3061
+ operation.operationPaths.forEach(operationPath => {
3062
+ routes.push(new JsonOperationRoute({
3063
+ basePath,
3064
+ token,
3065
+ endpoint,
3066
+ operationPath
3067
+ }));
3068
+ });
3069
+ }
3070
+
3071
+ return routes;
3072
+ }, operationsRoutes);
3073
+ }
3074
+
2955
3075
  /**
2956
3076
  * Return a sanitized path
2957
3077
  * @param path
@@ -3592,6 +3712,54 @@ function getStatusModel(status) {
3592
3712
  return statusesModel.get(status);
3593
3713
  }
3594
3714
 
3715
+ /**
3716
+ * Decorator util to compose another decorator. See @@Description@@ decorator implementation for more details.
3717
+ *
3718
+ * ## Usage
3719
+ *
3720
+ * ```typescript
3721
+ * export function Description(description: any) {
3722
+ * return JsonEntityFn((entity: JsonEntityStore, args: DecoratorParameters) => {
3723
+ * entity.itemSchema.description(description)
3724
+ * });
3725
+ * }
3726
+ * ```
3727
+ *
3728
+ * @param fn
3729
+ * @decorator
3730
+ * @utils
3731
+ * @model
3732
+ */
3733
+
3734
+ function JsonEntityFn(fn) {
3735
+ return (...parameters) => {
3736
+ const result = fn(getJsonEntityStore(...parameters), parameters);
3737
+
3738
+ if (typeof result === "function") {
3739
+ result(...parameters);
3740
+ }
3741
+ };
3742
+ }
3743
+
3744
+ /**
3745
+ * Add Children class to compose routes
3746
+ *
3747
+ * @decorator
3748
+ * @swagger
3749
+ * @schema
3750
+ * @classDecorator
3751
+ * @param children
3752
+ */
3753
+
3754
+ function Children(...children) {
3755
+ return JsonEntityFn(store => {
3756
+ store.store.set("childrenControllers", children);
3757
+ children.forEach(childToken => {
3758
+ core.Store.from(childToken).set("parentController", store.token);
3759
+ });
3760
+ });
3761
+ }
3762
+
3595
3763
  /**
3596
3764
  * Set the type of the item collection. The possible value is String, Boolean, Number, Date, Object, Class, etc...
3597
3765
  *
@@ -3726,35 +3894,6 @@ function CollectionContains(type) {
3726
3894
  return CollectionOf(type).Contains();
3727
3895
  }
3728
3896
 
3729
- /**
3730
- * Decorator util to compose another decorator. See @@Description@@ decorator implementation for more details.
3731
- *
3732
- * ## Usage
3733
- *
3734
- * ```typescript
3735
- * export function Description(description: any) {
3736
- * return JsonEntityFn((entity: JsonEntityStore, args: DecoratorParameters) => {
3737
- * entity.itemSchema.description(description)
3738
- * });
3739
- * }
3740
- * ```
3741
- *
3742
- * @param fn
3743
- * @decorator
3744
- * @utils
3745
- * @model
3746
- */
3747
-
3748
- function JsonEntityFn(fn) {
3749
- return (...parameters) => {
3750
- const result = fn(getJsonEntityStore(...parameters), parameters);
3751
-
3752
- if (typeof result === "function") {
3753
- result(...parameters);
3754
- }
3755
- };
3756
- }
3757
-
3758
3897
  /**
3759
3898
  * The value `maxItems` MUST be a non-negative integer.
3760
3899
  *
@@ -7788,6 +7927,7 @@ exports.Allow = Allow;
7788
7927
  exports.Any = Any;
7789
7928
  exports.AnyOf = AnyOf;
7790
7929
  exports.ArrayOf = ArrayOf;
7930
+ exports.Children = Children;
7791
7931
  exports.CollectionContains = CollectionContains;
7792
7932
  exports.CollectionOf = CollectionOf;
7793
7933
  exports.Const = Const;
@@ -7830,6 +7970,7 @@ exports.JsonLazyRef = JsonLazyRef;
7830
7970
  exports.JsonMap = JsonMap;
7831
7971
  exports.JsonMedia = JsonMedia;
7832
7972
  exports.JsonOperation = JsonOperation;
7973
+ exports.JsonOperationRoute = JsonOperationRoute;
7833
7974
  exports.JsonParameter = JsonParameter;
7834
7975
  exports.JsonRequestBody = JsonRequestBody;
7835
7976
  exports.JsonResponse = JsonResponse;
@@ -7885,6 +8026,7 @@ exports.any = any;
7885
8026
  exports.anyOf = anyOf;
7886
8027
  exports.array = array;
7887
8028
  exports.boolean = boolean;
8029
+ exports.concatPath = concatPath;
7888
8030
  exports.date = date;
7889
8031
  exports.datetime = datetime;
7890
8032
  exports.defineStatusModel = defineStatusModel;
@@ -7894,6 +8036,7 @@ exports.from = from;
7894
8036
  exports.getGenericsMap = getGenericsMap;
7895
8037
  exports.getJsonSchema = getJsonSchema;
7896
8038
  exports.getJsonType = getJsonType;
8039
+ exports.getOperationsRoutes = getOperationsRoutes;
7897
8040
  exports.getOperationsStores = getOperationsStores;
7898
8041
  exports.getProperties = getProperties;
7899
8042
  exports.getPropertiesStores = getPropertiesStores;