@tsed/schema 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.
@@ -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;
@@ -18,4 +18,4 @@ import { JsonEntityStore } from "../../domain";
18
18
  * @utils
19
19
  * @model
20
20
  */
21
- export declare function JsonEntityFn<T extends JsonEntityStore>(fn: (entity: T, parameters: DecoratorParameters) => void): (...args: any[]) => any;
21
+ export declare function JsonEntityFn<T extends JsonEntityStore>(fn: (entity: T, parameters: DecoratorParameters) => any): (...args: any[]) => any;
@@ -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
  */
@@ -77,8 +75,9 @@ export declare class JsonEntityStore extends Entity implements JsonEntityStoreOp
77
75
  *
78
76
  * @param args
79
77
  */
80
- static from<T extends JsonEntityStore = JsonEntityStore>(...args: any[]): NonNullable<T>;
78
+ static from<T extends JsonEntityStore = JsonEntityStore>(...args: any[]): 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
@@ -180,11 +180,22 @@ function execMapper(type, schema, options) {
180
180
  */
181
181
  const JsonEntitiesContainer = new Map();
182
182
 
183
+ /**
184
+ * @ignore
185
+ */
186
+
187
+ function isJsonEntityStore(model) {
188
+ return model.isStore;
189
+ }
183
190
  /**
184
191
  * @ignore
185
192
  */
186
193
 
187
194
  function getJsonEntityStore(...args) {
195
+ if (isJsonEntityStore(args[0])) {
196
+ return args[0];
197
+ }
198
+
188
199
  const store = core.Store.from(...args);
189
200
 
190
201
  if (!store.has("JsonEntityStore")) {
@@ -201,9 +212,6 @@ function getJsonEntityStore(...args) {
201
212
 
202
213
  return store.get("JsonEntityStore");
203
214
  }
204
- function isJsonEntityStore(model) {
205
- return model.isStore;
206
- }
207
215
 
208
216
  class JsonLazyRef {
209
217
  constructor(getType) {
@@ -2015,11 +2023,6 @@ class JsonEntityStore extends core.Entity {
2015
2023
  */
2016
2024
 
2017
2025
  this.children = new Map();
2018
- /**
2019
- * Path used to generate open spec.
2020
- */
2021
-
2022
- this.path = "/";
2023
2026
  this.store = options.store;
2024
2027
  /* istanbul ignore next */
2025
2028
 
@@ -2029,6 +2032,14 @@ class JsonEntityStore extends core.Entity {
2029
2032
 
2030
2033
  this.build();
2031
2034
  }
2035
+
2036
+ get path() {
2037
+ return this.store.get("path", "/");
2038
+ }
2039
+
2040
+ set path(path) {
2041
+ this.store.set("path", path);
2042
+ }
2032
2043
  /**
2033
2044
  * Return the JsonSchema
2034
2045
  */
@@ -2187,6 +2198,10 @@ class JsonEntityStore extends core.Entity {
2187
2198
  return this.from(target, propertyKey, core.descriptorOf(target, propertyKey));
2188
2199
  }
2189
2200
 
2201
+ get(key, defaultValue) {
2202
+ return this.store.get(key, defaultValue);
2203
+ }
2204
+
2190
2205
  getResponseOptions(status, contentType = "application/json") {
2191
2206
  var _this$operation, _this$operation$getRe;
2192
2207
 
@@ -2866,7 +2881,7 @@ function get$1(entity, options) {
2866
2881
  }
2867
2882
 
2868
2883
  function getJsonSchema(model, options = {}) {
2869
- const entity = isJsonEntityStore(model) ? model : getJsonEntityStore(model);
2884
+ const entity = getJsonEntityStore(model);
2870
2885
  options = _extends({
2871
2886
  endpoint: true,
2872
2887
  groups: []
@@ -2930,6 +2945,35 @@ function getProperties(target, options = {}) {
2930
2945
  return map;
2931
2946
  }
2932
2947
 
2948
+ class JsonOperationPathsMap extends Map {
2949
+ constructor() {
2950
+ super(...arguments);
2951
+
2952
+ this.getKey = (method, path) => `${method}-${path}`;
2953
+ }
2954
+
2955
+ setOperationPath(operationPath) {
2956
+ if (operationPath.method !== exports.OperationMethods.CUSTOM) {
2957
+ const key = this.getKey(operationPath.method, operationPath.path);
2958
+ this.updateFinalRouteState(key);
2959
+ this.updateFinalRouteState(this.getKey(exports.OperationMethods.ALL, operationPath.path));
2960
+ this.setFinalRoute(key, operationPath);
2961
+ }
2962
+ }
2963
+
2964
+ updateFinalRouteState(key) {
2965
+ if (this.has(key)) {
2966
+ this.get(key).isFinal = false;
2967
+ }
2968
+ }
2969
+
2970
+ setFinalRoute(key, operationPath) {
2971
+ this.set(key, operationPath);
2972
+ operationPath.isFinal = true;
2973
+ }
2974
+
2975
+ }
2976
+
2933
2977
  /**
2934
2978
  * @ignore
2935
2979
  */
@@ -2939,6 +2983,7 @@ function getOperationsStores(target) {
2939
2983
 
2940
2984
  if (!store.$operations) {
2941
2985
  const stores = getInheritedStores(store);
2986
+ const operationPaths = new JsonOperationPathsMap();
2942
2987
  store.$operations = new Map();
2943
2988
  stores.forEach(currentStore => {
2944
2989
  currentStore.children.forEach(propStore => {
@@ -2947,11 +2992,94 @@ function getOperationsStores(target) {
2947
2992
  }
2948
2993
  });
2949
2994
  });
2995
+ store.$operations.forEach(store => {
2996
+ var _store$operation;
2997
+
2998
+ (_store$operation = store.operation) == null ? void 0 : _store$operation.operationPaths.forEach(operationPath => {
2999
+ operationPaths.setOperationPath(operationPath);
3000
+ });
3001
+ });
3002
+ operationPaths.clear();
2950
3003
  }
2951
3004
 
2952
3005
  return store.$operations;
2953
3006
  }
2954
3007
 
3008
+ function concatPath(basePath, path) {
3009
+ return ((basePath || "") + (path || "")).replace(/\/\//gi, "/");
3010
+ }
3011
+
3012
+ class JsonOperationRoute {
3013
+ constructor(options) {
3014
+ Object.assign(this, options);
3015
+ }
3016
+
3017
+ get path() {
3018
+ var _this$operationPath;
3019
+
3020
+ return (_this$operationPath = this.operationPath) == null ? void 0 : _this$operationPath.path;
3021
+ }
3022
+
3023
+ get fullPath() {
3024
+ return concatPath(this.basePath, this.path);
3025
+ }
3026
+
3027
+ get method() {
3028
+ var _this$operationPath2;
3029
+
3030
+ return ((_this$operationPath2 = this.operationPath) == null ? void 0 : _this$operationPath2.method) || "USE";
3031
+ }
3032
+
3033
+ get isFinal() {
3034
+ var _this$operationPath3;
3035
+
3036
+ return !!((_this$operationPath3 = this.operationPath) != null && _this$operationPath3.isFinal);
3037
+ }
3038
+
3039
+ get propertyKey() {
3040
+ return this.endpoint.propertyKey;
3041
+ }
3042
+
3043
+ get store() {
3044
+ return this.endpoint.store;
3045
+ }
3046
+
3047
+ }
3048
+
3049
+ function getOperationsRoutes(token, options = {}) {
3050
+ const store = token.isStore ? token : getJsonEntityStore(token);
3051
+ const basePath = ((options.basePath || "") + (store.path || "")).replace(/\/\//gi, "/");
3052
+ let operationsRoutes = [];
3053
+
3054
+ if (options.withChildren) {
3055
+ const children = store.get("childrenControllers", []);
3056
+ operationsRoutes = children.reduce((operationsRoutes, token) => {
3057
+ return operationsRoutes.concat(getOperationsRoutes(token, _extends({}, options, {
3058
+ basePath
3059
+ })));
3060
+ }, operationsRoutes);
3061
+ }
3062
+
3063
+ return [...getOperationsStores(token).values()].reduce((routes, endpoint) => {
3064
+ const {
3065
+ operation
3066
+ } = endpoint;
3067
+
3068
+ if (operation) {
3069
+ operation.operationPaths.forEach(operationPath => {
3070
+ routes.push(new JsonOperationRoute({
3071
+ basePath,
3072
+ token,
3073
+ endpoint,
3074
+ operationPath
3075
+ }));
3076
+ });
3077
+ }
3078
+
3079
+ return routes;
3080
+ }, operationsRoutes);
3081
+ }
3082
+
2955
3083
  /**
2956
3084
  * Return a sanitized path
2957
3085
  * @param path
@@ -3592,6 +3720,54 @@ function getStatusModel(status) {
3592
3720
  return statusesModel.get(status);
3593
3721
  }
3594
3722
 
3723
+ /**
3724
+ * Decorator util to compose another decorator. See @@Description@@ decorator implementation for more details.
3725
+ *
3726
+ * ## Usage
3727
+ *
3728
+ * ```typescript
3729
+ * export function Description(description: any) {
3730
+ * return JsonEntityFn((entity: JsonEntityStore, args: DecoratorParameters) => {
3731
+ * entity.itemSchema.description(description)
3732
+ * });
3733
+ * }
3734
+ * ```
3735
+ *
3736
+ * @param fn
3737
+ * @decorator
3738
+ * @utils
3739
+ * @model
3740
+ */
3741
+
3742
+ function JsonEntityFn(fn) {
3743
+ return (...parameters) => {
3744
+ const result = fn(getJsonEntityStore(...parameters), parameters);
3745
+
3746
+ if (typeof result === "function") {
3747
+ result(...parameters);
3748
+ }
3749
+ };
3750
+ }
3751
+
3752
+ /**
3753
+ * Add Children class to compose routes
3754
+ *
3755
+ * @decorator
3756
+ * @swagger
3757
+ * @schema
3758
+ * @classDecorator
3759
+ * @param children
3760
+ */
3761
+
3762
+ function Children(...children) {
3763
+ return JsonEntityFn(store => {
3764
+ store.store.set("childrenControllers", children);
3765
+ children.forEach(childToken => {
3766
+ core.Store.from(childToken).set("parentController", store.token);
3767
+ });
3768
+ });
3769
+ }
3770
+
3595
3771
  /**
3596
3772
  * Set the type of the item collection. The possible value is String, Boolean, Number, Date, Object, Class, etc...
3597
3773
  *
@@ -3726,35 +3902,6 @@ function CollectionContains(type) {
3726
3902
  return CollectionOf(type).Contains();
3727
3903
  }
3728
3904
 
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
3905
  /**
3759
3906
  * The value `maxItems` MUST be a non-negative integer.
3760
3907
  *
@@ -7788,6 +7935,7 @@ exports.Allow = Allow;
7788
7935
  exports.Any = Any;
7789
7936
  exports.AnyOf = AnyOf;
7790
7937
  exports.ArrayOf = ArrayOf;
7938
+ exports.Children = Children;
7791
7939
  exports.CollectionContains = CollectionContains;
7792
7940
  exports.CollectionOf = CollectionOf;
7793
7941
  exports.Const = Const;
@@ -7830,6 +7978,7 @@ exports.JsonLazyRef = JsonLazyRef;
7830
7978
  exports.JsonMap = JsonMap;
7831
7979
  exports.JsonMedia = JsonMedia;
7832
7980
  exports.JsonOperation = JsonOperation;
7981
+ exports.JsonOperationRoute = JsonOperationRoute;
7833
7982
  exports.JsonParameter = JsonParameter;
7834
7983
  exports.JsonRequestBody = JsonRequestBody;
7835
7984
  exports.JsonResponse = JsonResponse;
@@ -7885,6 +8034,7 @@ exports.any = any;
7885
8034
  exports.anyOf = anyOf;
7886
8035
  exports.array = array;
7887
8036
  exports.boolean = boolean;
8037
+ exports.concatPath = concatPath;
7888
8038
  exports.date = date;
7889
8039
  exports.datetime = datetime;
7890
8040
  exports.defineStatusModel = defineStatusModel;
@@ -7894,6 +8044,7 @@ exports.from = from;
7894
8044
  exports.getGenericsMap = getGenericsMap;
7895
8045
  exports.getJsonSchema = getJsonSchema;
7896
8046
  exports.getJsonType = getJsonType;
8047
+ exports.getOperationsRoutes = getOperationsRoutes;
7897
8048
  exports.getOperationsStores = getOperationsStores;
7898
8049
  exports.getProperties = getProperties;
7899
8050
  exports.getPropertiesStores = getPropertiesStores;