@tsed/schema 8.0.1 → 8.0.3

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.
Files changed (292) hide show
  1. package/package.json +11 -10
  2. package/readme.md +6 -6
  3. package/src/components/async-api/channelsMapper.ts +81 -0
  4. package/src/components/async-api/generate.ts +24 -0
  5. package/src/components/async-api/messageMapper.ts +62 -0
  6. package/src/components/async-api/payloadMapper.ts +61 -0
  7. package/src/components/async-api/responseMapper.ts +70 -0
  8. package/src/components/default/anyMapper.spec.ts +7 -0
  9. package/src/components/default/anyMapper.ts +33 -0
  10. package/src/components/default/classMapper.ts +55 -0
  11. package/src/components/default/discriminatorMappingMapper.ts +36 -0
  12. package/src/components/default/enumsMapper.spec.ts +64 -0
  13. package/src/components/default/enumsMapper.ts +17 -0
  14. package/src/components/default/genericsMapper.ts +70 -0
  15. package/src/components/default/inheritedClassMapper.ts +24 -0
  16. package/src/components/default/itemMapper.ts +8 -0
  17. package/src/components/default/lazyRefMapper.ts +21 -0
  18. package/src/components/default/mapMapper.ts +25 -0
  19. package/src/components/default/nullableMapper.ts +55 -0
  20. package/src/components/default/objectMapper.ts +36 -0
  21. package/src/components/default/ofMapper.ts +16 -0
  22. package/src/components/default/propertiesMapper.ts +25 -0
  23. package/src/components/default/requiredMapper.ts +69 -0
  24. package/src/components/default/schemaMapper.ts +151 -0
  25. package/src/components/index.ts +36 -0
  26. package/src/components/open-spec/discriminatorMappingMapper.ts +7 -0
  27. package/src/components/open-spec/enumsMapper.ts +20 -0
  28. package/src/components/open-spec/generate.ts +29 -0
  29. package/src/components/open-spec/nullableMapper.ts +51 -0
  30. package/src/components/open-spec/operationInFilesMapper.ts +32 -0
  31. package/src/components/open-spec/operationInParameterMapper.ts +56 -0
  32. package/src/components/open-spec/operationInParametersMapper.ts +9 -0
  33. package/src/components/open-spec/operationInQueryMapper.ts +74 -0
  34. package/src/components/open-spec/operationMapper.ts +65 -0
  35. package/src/components/open-spec/operationMediaMapper.ts +11 -0
  36. package/src/components/open-spec/operationRequestBodyMapper.ts +82 -0
  37. package/src/components/open-spec/operationResponseMapper.ts +26 -0
  38. package/src/components/open-spec/pathsMapper.ts +132 -0
  39. package/src/constants/OperationVerbs.ts +29 -0
  40. package/src/constants/httpStatusMessages.ts +21 -0
  41. package/src/constants/jsonSchemaProperties.ts +17 -0
  42. package/src/decorators/class/children.ts +22 -0
  43. package/src/decorators/class/discriminatorValue.ts +53 -0
  44. package/src/decorators/collections/collectionContains.ts +25 -0
  45. package/src/decorators/collections/collectionOf.spec.ts +327 -0
  46. package/src/decorators/collections/collectionOf.ts +182 -0
  47. package/src/decorators/collections/maxItems.spec.ts +40 -0
  48. package/src/decorators/collections/maxItems.ts +59 -0
  49. package/src/decorators/collections/maxProperties.spec.ts +109 -0
  50. package/src/decorators/collections/maxProperties.ts +82 -0
  51. package/src/decorators/collections/minItems.spec.ts +40 -0
  52. package/src/decorators/collections/minItems.ts +63 -0
  53. package/src/decorators/collections/minProperties.spec.ts +109 -0
  54. package/src/decorators/collections/minProperties.ts +81 -0
  55. package/src/decorators/collections/uniqueItems.spec.ts +28 -0
  56. package/src/decorators/collections/uniqueItems.ts +59 -0
  57. package/src/decorators/common/__snapshots__/groups.spec.ts.snap +321 -0
  58. package/src/decorators/common/additionalProperties.spec.ts +133 -0
  59. package/src/decorators/common/additionalProperties.ts +17 -0
  60. package/src/decorators/common/allOf.spec.ts +276 -0
  61. package/src/decorators/common/allOf.ts +19 -0
  62. package/src/decorators/common/allow.spec.ts +130 -0
  63. package/src/decorators/common/allow.ts +50 -0
  64. package/src/decorators/common/any.spec.ts +109 -0
  65. package/src/decorators/common/any.ts +45 -0
  66. package/src/decorators/common/anyOf.spec.ts +270 -0
  67. package/src/decorators/common/anyOf.ts +23 -0
  68. package/src/decorators/common/const.spec.ts +98 -0
  69. package/src/decorators/common/const.ts +68 -0
  70. package/src/decorators/common/customKey.spec.ts +105 -0
  71. package/src/decorators/common/customKey.ts +39 -0
  72. package/src/decorators/common/default.spec.ts +25 -0
  73. package/src/decorators/common/default.ts +47 -0
  74. package/src/decorators/common/defaultMsg.spec.ts +22 -0
  75. package/src/decorators/common/defaultMsg.ts +38 -0
  76. package/src/decorators/common/description.spec.ts +166 -0
  77. package/src/decorators/common/description.ts +77 -0
  78. package/src/decorators/common/discriminatorKey.ts +14 -0
  79. package/src/decorators/common/enum.spec.ts +305 -0
  80. package/src/decorators/common/enum.ts +111 -0
  81. package/src/decorators/common/errorMsg.spec.ts +57 -0
  82. package/src/decorators/common/errorMsg.ts +14 -0
  83. package/src/decorators/common/example.spec.ts +93 -0
  84. package/src/decorators/common/example.ts +18 -0
  85. package/src/decorators/common/examples.spec.ts +101 -0
  86. package/src/decorators/common/examples.ts +20 -0
  87. package/src/decorators/common/exclusiveMaximum.spec.ts +47 -0
  88. package/src/decorators/common/exclusiveMaximum.ts +75 -0
  89. package/src/decorators/common/exclusiveMinimum.spec.ts +47 -0
  90. package/src/decorators/common/exclusiveMinimum.ts +75 -0
  91. package/src/decorators/common/format.spec.ts +149 -0
  92. package/src/decorators/common/format.ts +486 -0
  93. package/src/decorators/common/forwardGroups.spec.ts +270 -0
  94. package/src/decorators/common/forwardGroups.ts +16 -0
  95. package/src/decorators/common/groups.spec.ts +968 -0
  96. package/src/decorators/common/groups.ts +66 -0
  97. package/src/decorators/common/hidden.spec.ts +52 -0
  98. package/src/decorators/common/hidden.ts +36 -0
  99. package/src/decorators/common/ignore.spec.ts +88 -0
  100. package/src/decorators/common/ignore.ts +66 -0
  101. package/src/decorators/common/integer.spec.ts +110 -0
  102. package/src/decorators/common/integer.ts +69 -0
  103. package/src/decorators/common/jsonEntityFn.spec.ts +16 -0
  104. package/src/decorators/common/jsonEntityFn.ts +33 -0
  105. package/src/decorators/common/maxLength.spec.ts +61 -0
  106. package/src/decorators/common/maxLength.ts +88 -0
  107. package/src/decorators/common/maximum.spec.ts +73 -0
  108. package/src/decorators/common/maximum.ts +146 -0
  109. package/src/decorators/common/minLength.spec.ts +186 -0
  110. package/src/decorators/common/minLength.ts +89 -0
  111. package/src/decorators/common/minimum.spec.ts +74 -0
  112. package/src/decorators/common/minimum.ts +145 -0
  113. package/src/decorators/common/multipleOf.spec.ts +33 -0
  114. package/src/decorators/common/multipleOf.ts +80 -0
  115. package/src/decorators/common/name.spec.ts +56 -0
  116. package/src/decorators/common/name.ts +50 -0
  117. package/src/decorators/common/nullable.spec.ts +818 -0
  118. package/src/decorators/common/nullable.ts +32 -0
  119. package/src/decorators/common/oneOf.spec.ts +301 -0
  120. package/src/decorators/common/oneOf.ts +24 -0
  121. package/src/decorators/common/optional.spec.ts +43 -0
  122. package/src/decorators/common/optional.ts +26 -0
  123. package/src/decorators/common/pattern.spec.ts +27 -0
  124. package/src/decorators/common/pattern.ts +102 -0
  125. package/src/decorators/common/property.ts +24 -0
  126. package/src/decorators/common/readOnly.spec.ts +135 -0
  127. package/src/decorators/common/readOnly.ts +17 -0
  128. package/src/decorators/common/recordOf.spec.ts +41 -0
  129. package/src/decorators/common/recordOf.ts +59 -0
  130. package/src/decorators/common/required.spec.ts +188 -0
  131. package/src/decorators/common/required.ts +50 -0
  132. package/src/decorators/common/requiredGroups.spec.ts +127 -0
  133. package/src/decorators/common/requiredGroups.ts +23 -0
  134. package/src/decorators/common/schema.spec.ts +23 -0
  135. package/src/decorators/common/schema.ts +64 -0
  136. package/src/decorators/common/title.spec.ts +20 -0
  137. package/src/decorators/common/title.ts +45 -0
  138. package/src/decorators/common/typeError.spec.ts +22 -0
  139. package/src/decorators/common/typeError.ts +15 -0
  140. package/src/decorators/common/writeOnly.spec.ts +105 -0
  141. package/src/decorators/common/writeOnly.ts +17 -0
  142. package/src/decorators/config/jsonEntityComponent.ts +16 -0
  143. package/src/decorators/generics/genericOf.spec.ts +246 -0
  144. package/src/decorators/generics/genericOf.ts +112 -0
  145. package/src/decorators/generics/generics.ts +22 -0
  146. package/src/decorators/operations/acceptMime.spec.ts +39 -0
  147. package/src/decorators/operations/acceptMime.ts +25 -0
  148. package/src/decorators/operations/consumes.spec.ts +89 -0
  149. package/src/decorators/operations/consumes.ts +37 -0
  150. package/src/decorators/operations/contentType.spec.ts +40 -0
  151. package/src/decorators/operations/contentType.ts +24 -0
  152. package/src/decorators/operations/deprecated.spec.ts +223 -0
  153. package/src/decorators/operations/deprecated.ts +49 -0
  154. package/src/decorators/operations/header.spec.ts +214 -0
  155. package/src/decorators/operations/header.ts +62 -0
  156. package/src/decorators/operations/in.spec.ts +321 -0
  157. package/src/decorators/operations/in.ts +141 -0
  158. package/src/decorators/operations/inFile.spec.ts +209 -0
  159. package/src/decorators/operations/inFile.ts +55 -0
  160. package/src/decorators/operations/location.spec.ts +51 -0
  161. package/src/decorators/operations/location.ts +26 -0
  162. package/src/decorators/operations/operation.ts +119 -0
  163. package/src/decorators/operations/operationId.spec.ts +43 -0
  164. package/src/decorators/operations/operationId.ts +22 -0
  165. package/src/decorators/operations/operationPath.spec.ts +42 -0
  166. package/src/decorators/operations/operationPath.ts +33 -0
  167. package/src/decorators/operations/partial.spec.ts +249 -0
  168. package/src/decorators/operations/partial.ts +14 -0
  169. package/src/decorators/operations/path.spec.ts +47 -0
  170. package/src/decorators/operations/path.ts +27 -0
  171. package/src/decorators/operations/produces.spec.ts +89 -0
  172. package/src/decorators/operations/produces.ts +44 -0
  173. package/src/decorators/operations/publish.spec.ts +26 -0
  174. package/src/decorators/operations/publish.ts +6 -0
  175. package/src/decorators/operations/redirect.spec.ts +146 -0
  176. package/src/decorators/operations/redirect.ts +82 -0
  177. package/src/decorators/operations/returns.spec.ts +1393 -0
  178. package/src/decorators/operations/returns.ts +662 -0
  179. package/src/decorators/operations/route.spec.ts +211 -0
  180. package/src/decorators/operations/route.ts +154 -0
  181. package/src/decorators/operations/security.spec.ts +162 -0
  182. package/src/decorators/operations/security.ts +83 -0
  183. package/src/decorators/operations/status.spec.ts +582 -0
  184. package/src/decorators/operations/status.ts +130 -0
  185. package/src/decorators/operations/subscribe.spec.ts +32 -0
  186. package/src/decorators/operations/subscribe.ts +6 -0
  187. package/src/decorators/operations/summary.spec.ts +45 -0
  188. package/src/decorators/operations/summary.ts +32 -0
  189. package/src/decorators/operations/tags.spec.ts +113 -0
  190. package/src/decorators/operations/tags.ts +53 -0
  191. package/src/decorators/operations/view.spec.ts +16 -0
  192. package/src/decorators/operations/view.ts +27 -0
  193. package/src/domain/DecoratorContext.ts +116 -0
  194. package/src/domain/JsonAliasMap.ts +19 -0
  195. package/src/domain/JsonClassStore.ts +35 -0
  196. package/src/domain/JsonDiscriminator.spec.ts +65 -0
  197. package/src/domain/JsonDiscriminator.ts +52 -0
  198. package/src/domain/JsonEntityStore.spec.ts +277 -0
  199. package/src/domain/JsonEntityStore.ts +284 -0
  200. package/src/domain/JsonFormatTypes.ts +17 -0
  201. package/src/domain/JsonLazyRef.ts +21 -0
  202. package/src/domain/JsonMap.spec.ts +15 -0
  203. package/src/domain/JsonMap.ts +33 -0
  204. package/src/domain/JsonMedia.ts +24 -0
  205. package/src/domain/JsonMethodStore.spec.ts +301 -0
  206. package/src/domain/JsonMethodStore.ts +212 -0
  207. package/src/domain/JsonOperation.spec.ts +126 -0
  208. package/src/domain/JsonOperation.ts +219 -0
  209. package/src/domain/JsonOperationPathsMap.ts +16 -0
  210. package/src/domain/JsonOperationRoute.spec.ts +109 -0
  211. package/src/domain/JsonOperationRoute.ts +75 -0
  212. package/src/domain/JsonParameter.ts +70 -0
  213. package/src/domain/JsonParameterStore.spec.ts +212 -0
  214. package/src/domain/JsonParameterStore.ts +138 -0
  215. package/src/domain/JsonParameterTypes.spec.ts +12 -0
  216. package/src/domain/JsonParameterTypes.ts +20 -0
  217. package/src/domain/JsonPropertyStore.spec.ts +177 -0
  218. package/src/domain/JsonPropertyStore.ts +92 -0
  219. package/src/domain/JsonRequestBody.ts +47 -0
  220. package/src/domain/JsonResponse.spec.ts +33 -0
  221. package/src/domain/JsonResponse.ts +64 -0
  222. package/src/domain/JsonSchema.spec.ts +1470 -0
  223. package/src/domain/JsonSchema.ts +1049 -0
  224. package/src/domain/SpecTypes.ts +6 -0
  225. package/src/hooks/alterIgnore.ts +25 -0
  226. package/src/hooks/alterOneOf.ts +67 -0
  227. package/src/hooks/alterRequiredGroups.ts +12 -0
  228. package/src/index.ts +185 -0
  229. package/src/interfaces/IgnoreCallback.ts +5 -0
  230. package/src/interfaces/JsonHookContext.ts +18 -0
  231. package/src/interfaces/JsonOpenSpec.ts +7 -0
  232. package/src/interfaces/JsonSchemaOptions.ts +35 -0
  233. package/src/registries/JsonSchemaMapperContainer.ts +52 -0
  234. package/src/registries/enumRegistries.ts +3 -0
  235. package/src/utils/__snapshots__/generateSpec.spec.ts.snap +109 -0
  236. package/src/utils/buildPath.ts +8 -0
  237. package/src/utils/concatParameters.ts +12 -0
  238. package/src/utils/concatPath.spec.ts +23 -0
  239. package/src/utils/concatPath.ts +24 -0
  240. package/src/utils/defineStatusModel.ts +11 -0
  241. package/src/utils/from.spec.ts +243 -0
  242. package/src/utils/from.ts +281 -0
  243. package/src/utils/generateSpec.spec.ts +1262 -0
  244. package/src/utils/generateSpec.ts +54 -0
  245. package/src/utils/generics.ts +83 -0
  246. package/src/utils/getComputedType.spec.ts +45 -0
  247. package/src/utils/getComputedType.ts +39 -0
  248. package/src/utils/getInheritedStores.spec.ts +47 -0
  249. package/src/utils/getInheritedStores.ts +23 -0
  250. package/src/utils/getJsonEntityStore.ts +36 -0
  251. package/src/utils/getJsonPathParameters.spec.ts +126 -0
  252. package/src/utils/getJsonPathParameters.ts +97 -0
  253. package/src/utils/getJsonSchema.spec.ts +695 -0
  254. package/src/utils/getJsonSchema.ts +69 -0
  255. package/src/utils/getJsonType.spec.ts +28 -0
  256. package/src/utils/getJsonType.ts +46 -0
  257. package/src/utils/getOperationsRoutes.spec.ts +284 -0
  258. package/src/utils/getOperationsRoutes.ts +56 -0
  259. package/src/utils/getOperationsStore.spec.ts +47 -0
  260. package/src/utils/getOperationsStores.ts +37 -0
  261. package/src/utils/getPropertiesStores.spec.ts +138 -0
  262. package/src/utils/getPropertiesStores.ts +66 -0
  263. package/src/utils/getSpec.spec.ts +610 -0
  264. package/src/utils/getSpec.ts +117 -0
  265. package/src/utils/getSpecType.spec.ts +16 -0
  266. package/src/utils/getSpecType.ts +19 -0
  267. package/src/utils/isSuccessStatus.spec.ts +22 -0
  268. package/src/utils/isSuccessStatus.ts +2 -0
  269. package/src/utils/mapHeaders.spec.ts +57 -0
  270. package/src/utils/mapHeaders.ts +33 -0
  271. package/src/utils/mapOpenSpec.ts +44 -0
  272. package/src/utils/mapOpenSpec2.ts +50 -0
  273. package/src/utils/mapOpenSpec3.ts +25 -0
  274. package/src/utils/mapOpenSpecInfo.ts +19 -0
  275. package/src/utils/mapOperationOptions.ts +34 -0
  276. package/src/utils/matchGroups.spec.ts +85 -0
  277. package/src/utils/matchGroups.ts +64 -0
  278. package/src/utils/mergeSpec.spec.ts +533 -0
  279. package/src/utils/mergeSpec.ts +54 -0
  280. package/src/utils/operationIdFormatter.spec.ts +11 -0
  281. package/src/utils/operationIdFormatter.ts +49 -0
  282. package/src/utils/ref.ts +65 -0
  283. package/src/utils/removeHiddenOperation.ts +5 -0
  284. package/src/utils/serializeEnumValues.ts +27 -0
  285. package/src/utils/somethingOf.ts +11 -0
  286. package/src/utils/toJsonMapCollection.spec.ts +18 -0
  287. package/src/utils/toJsonMapCollection.ts +12 -0
  288. package/src/utils/toJsonRegex.ts +7 -0
  289. package/src/utils/transformToOS2.spec.ts +177 -0
  290. package/src/utils/transformToOS2.ts +607 -0
  291. package/src/utils/withErrorMsg.spec.ts +16 -0
  292. package/src/utils/withErrorMsg.ts +56 -0
@@ -0,0 +1,301 @@
1
+ import {StoreSet} from "@tsed/core";
2
+ // @ts-ignore
3
+ import {Use, UseAfter, UseBefore} from "@tsed/platform-middlewares";
4
+
5
+ import {OperationVerbs} from "../constants/OperationVerbs.js";
6
+ import {Property} from "../decorators/common/property.js";
7
+ import {In} from "../decorators/operations/in.js";
8
+ import {Returns} from "../decorators/operations/returns.js";
9
+ import {Get} from "../decorators/operations/route.js";
10
+ import {JsonEntityStore} from "./JsonEntityStore.js";
11
+ import {EndpointMetadata, JsonMethodStore} from "./JsonMethodStore.js";
12
+ import {JsonOperation} from "./JsonOperation.js";
13
+ import {JsonParameter} from "./JsonParameter.js";
14
+
15
+ describe("JsonMethodStore", () => {
16
+ describe("endpoint declaration", () => {
17
+ it("should return an endpoint metadata", () => {
18
+ // GIVEN
19
+ const middleware1 = () => {};
20
+ const middleware2 = () => {};
21
+ const middleware3 = () => {};
22
+
23
+ class Test {
24
+ @StoreSet("test", "value")
25
+ @(Get("/").Use(middleware3).UseAfter(middleware1).UseBefore(middleware2))
26
+ method(): any {}
27
+ }
28
+
29
+ const endpoint = JsonEntityStore.fromMethod(Test, "method");
30
+
31
+ // THEN
32
+ expect(endpoint.beforeMiddlewares).toHaveLength(1);
33
+ expect(endpoint.middlewares).toHaveLength(1);
34
+ expect(endpoint.beforeMiddlewares).toHaveLength(1);
35
+ expect(endpoint.token).toBe(Test);
36
+ expect(endpoint.store.get("test")).toEqual("value");
37
+ });
38
+ });
39
+ describe("view()", () => {
40
+ it("should return view value", () => {
41
+ // GIVEN
42
+ const middleware3 = () => {};
43
+
44
+ class Test {
45
+ @Use(middleware3)
46
+ method(): any {}
47
+ }
48
+
49
+ const endpoint = EndpointMetadata.get(Test, "method");
50
+
51
+ // @ts-ignore
52
+ endpoint.view = {path: "/", test: 1};
53
+
54
+ expect(endpoint.view).toEqual({path: "/", test: 1});
55
+ });
56
+ });
57
+ describe("acceptMimes()", () => {
58
+ it("should return acceptMimes value", () => {
59
+ // GIVEN
60
+ const middleware3 = () => {};
61
+
62
+ class Test {
63
+ @Use(middleware3)
64
+ method(): any {}
65
+ }
66
+
67
+ const endpoint = EndpointMetadata.get(Test, "method");
68
+
69
+ // @ts-ignore
70
+ endpoint.acceptMimes = [];
71
+
72
+ expect(endpoint.acceptMimes).toEqual([]);
73
+ });
74
+ });
75
+ describe("endpoint declaration (2)", () => {
76
+ it("should return an endpoint metadata", () => {
77
+ // GIVEN
78
+ const middleware1 = () => {};
79
+ const middleware2 = () => {};
80
+ const middleware3 = () => {};
81
+
82
+ class Test {
83
+ @UseAfter(middleware1)
84
+ @UseBefore(middleware2)
85
+ @Use(middleware3)
86
+ @StoreSet("test", "value")
87
+ method(): any {}
88
+ }
89
+
90
+ const endpoint = EndpointMetadata.get(Test, "method");
91
+
92
+ // THEN
93
+ expect(endpoint.beforeMiddlewares).toHaveLength(1);
94
+ expect(endpoint.middlewares).toHaveLength(1);
95
+ expect(endpoint.beforeMiddlewares).toHaveLength(1);
96
+ expect(endpoint.token).toEqual(Test);
97
+ expect(endpoint.store.get("test")).toEqual("value");
98
+ });
99
+ it("should add endpoint with path", () => {
100
+ // GIVEN
101
+ const middleware = () => {};
102
+
103
+ class Test {
104
+ @Use("/", middleware)
105
+ method(): any {}
106
+ }
107
+
108
+ const endpoint = EndpointMetadata.get(Test, "method");
109
+
110
+ // THEN
111
+ expect(endpoint.middlewares).toHaveLength(1);
112
+ });
113
+ it("should add endpoint with path and method", () => {
114
+ // GIVEN
115
+ const middleware = () => {};
116
+
117
+ class Test {
118
+ @Use("get", "/", middleware)
119
+ method(): any {}
120
+ }
121
+
122
+ const endpoint = EndpointMetadata.get(Test, "method");
123
+
124
+ // THEN
125
+ expect(endpoint.middlewares).toHaveLength(1);
126
+
127
+ expect([...endpoint.operationPaths.values()]).toEqual([
128
+ {
129
+ method: OperationVerbs.GET,
130
+ path: "/"
131
+ }
132
+ ]);
133
+ });
134
+ });
135
+ describe("static get()", () => {
136
+ it("should return the endpoint metadata", () => {
137
+ const middleware1 = () => {};
138
+ const middleware2 = () => {};
139
+ const middleware3 = () => {};
140
+
141
+ class Test {
142
+ @UseAfter(middleware1)
143
+ @UseBefore(middleware2)
144
+ @Use(middleware3)
145
+ @StoreSet("test", "Test")
146
+ method(): any {}
147
+
148
+ @Use(middleware3)
149
+ @StoreSet("test", "Test")
150
+ method3() {}
151
+ }
152
+
153
+ const endpoint = JsonMethodStore.get(Test, "method");
154
+ expect(endpoint).toBeInstanceOf(JsonMethodStore);
155
+ });
156
+ });
157
+ describe("get()", () => {
158
+ it("should return the endpoint metadata", () => {
159
+ const middleware1 = () => {};
160
+ const middleware2 = () => {};
161
+ const middleware3 = () => {};
162
+
163
+ class Test {
164
+ @UseAfter(middleware1)
165
+ @UseBefore(middleware2)
166
+ @Use(middleware3)
167
+ @StoreSet("test", "Test")
168
+ method(): any {}
169
+
170
+ @Use(middleware3)
171
+ @StoreSet("test", "Test")
172
+ method3() {}
173
+ }
174
+
175
+ const endpoint = JsonMethodStore.get(Test, "method");
176
+ expect(endpoint.get("test")).toEqual("Test");
177
+ });
178
+ });
179
+
180
+ describe("getResponseOptions()", () => {
181
+ it("should return the response options (200/201)", () => {
182
+ class MyController {
183
+ @Get("/")
184
+ @Returns(200, Object)
185
+ method(@In("path") param: string) {}
186
+ }
187
+
188
+ // METHOD
189
+ const storeMethod = JsonEntityStore.from(MyController).children.get("method");
190
+
191
+ expect(storeMethod?.getResponseOptions(200)).toEqual({
192
+ groups: undefined,
193
+ type: Object
194
+ });
195
+ expect(storeMethod?.getResponseOptions(201)).toEqual({
196
+ type: Object
197
+ });
198
+ });
199
+ it("should return the response options (200 + includes)", () => {
200
+ class MyController {
201
+ @Get("/")
202
+ @(Returns(200, Object).AllowedGroups("summary"))
203
+ method(@In("path") param: string) {}
204
+ }
205
+
206
+ // METHOD
207
+ const storeMethod = JsonEntityStore.from(MyController).children.get("method");
208
+
209
+ expect(storeMethod?.getResponseOptions(200, {includes: undefined})).toEqual({
210
+ groups: undefined,
211
+ type: Object
212
+ });
213
+ expect(storeMethod?.getResponseOptions(200, {includes: []})).toEqual({
214
+ groups: [],
215
+ type: Object
216
+ });
217
+ expect(storeMethod?.getResponseOptions(200, {includes: ["summary", "admin"]})).toEqual({
218
+ groups: ["summary"],
219
+ type: Object
220
+ });
221
+ });
222
+ });
223
+
224
+ describe("stores", () => {
225
+ it("should return all stores", () => {
226
+ class Model {
227
+ @Property()
228
+ id: string;
229
+
230
+ @Get("/")
231
+ @Returns(200, Object)
232
+ method(@In("path") param: string) {}
233
+ }
234
+
235
+ // CLASS
236
+ const storeClass = JsonEntityStore.from(Model);
237
+ expect(storeClass).toBeInstanceOf(JsonEntityStore);
238
+ expect(storeClass.decoratorType).toBe("class");
239
+ expect(storeClass.propertyName).toBe("undefined");
240
+ expect(storeClass.propertyKey).toBeUndefined();
241
+ expect(storeClass.index).toBeUndefined();
242
+ expect(storeClass.parent).toBe(storeClass);
243
+
244
+ // PROPERTY
245
+ const storeProp = JsonEntityStore.from(Model).children.get("id");
246
+ expect(storeProp).toBeInstanceOf(JsonEntityStore);
247
+ expect(storeProp?.decoratorType).toBe("property");
248
+ expect(storeProp?.propertyKey).toBe("id");
249
+ expect(storeProp?.propertyName).toBe("id");
250
+ expect(storeProp?.index).toBeUndefined();
251
+ expect(storeProp?.parameter).toBeUndefined();
252
+ expect(storeProp?.operation).toBeUndefined();
253
+ expect(storeProp?.nestedGenerics).toEqual([]);
254
+ expect(storeProp?.parent).toEqual(storeClass);
255
+
256
+ // METHOD
257
+ const storeMethod = JsonEntityStore.from(Model).children.get("method");
258
+ expect(storeMethod).toBeInstanceOf(JsonEntityStore);
259
+ expect(storeMethod?.propertyKey).toBe("method");
260
+ expect(storeMethod?.propertyName).toBe("method");
261
+ expect(storeMethod?.decoratorType).toBe("method");
262
+ expect(storeMethod?.index).toBeUndefined();
263
+ expect(storeMethod?.parameters.length).toEqual(1);
264
+ expect(storeMethod?.params.length).toEqual(1);
265
+
266
+ expect([...storeMethod?.operationPaths.entries()]).toEqual([
267
+ [
268
+ "GET/",
269
+ {
270
+ method: "GET",
271
+ path: "/"
272
+ }
273
+ ]
274
+ ]);
275
+ expect(storeMethod?.getResponseOptions(200)).toEqual({
276
+ groups: undefined,
277
+ type: Object
278
+ });
279
+ expect(storeMethod?.getResponseOptions(201)).toEqual({
280
+ type: Object
281
+ });
282
+ expect(storeMethod?.operation).toBeInstanceOf(JsonOperation);
283
+ expect(storeMethod?.nestedGenerics).toEqual([]);
284
+ expect(storeProp?.parent).toEqual(storeClass);
285
+
286
+ // PARAMETERS
287
+ const storeParam = JsonEntityStore.from(Model).children.get("method")?.children.get(0);
288
+
289
+ expect(storeParam).toBeInstanceOf(JsonEntityStore);
290
+ expect(storeParam?.propertyKey).toBe("method");
291
+ expect(storeParam?.propertyName).toBe("method");
292
+ expect(storeParam?.index).toBe(0);
293
+ expect(storeParam?.decoratorType).toBe("parameter");
294
+ expect(storeParam?.parameter).toBeInstanceOf(JsonParameter);
295
+ expect(storeParam?.operation).toBeUndefined();
296
+ expect(storeParam?.nestedGenerics).toEqual([]);
297
+ expect(storeParam?.nestedGenerics).toEqual([]);
298
+ expect(storeParam?.parent).toEqual(storeMethod);
299
+ });
300
+ });
301
+ });
@@ -0,0 +1,212 @@
1
+ import {DecoratorTypes, deepMerge, descriptorOf, isCollection, isFunction, isPromise, Metadata, prototypeOf, Store, Type} from "@tsed/core";
2
+
3
+ import {JsonEntityComponent} from "../decorators/config/jsonEntityComponent.js";
4
+ import type {JsonClassStore} from "./JsonClassStore.js";
5
+ import {JsonEntityStore, JsonEntityStoreOptions} from "./JsonEntityStore.js";
6
+ import {JsonOperation} from "./JsonOperation.js";
7
+ import type {JsonParameterStore} from "./JsonParameterStore.js";
8
+ import {JsonSchema} from "./JsonSchema.js";
9
+
10
+ export interface JsonViewOptions {
11
+ path: string;
12
+ options: any;
13
+ }
14
+
15
+ export interface JsonRedirectOptions {
16
+ status: number | undefined;
17
+ url: string;
18
+ }
19
+
20
+ @JsonEntityComponent(DecoratorTypes.METHOD)
21
+ export class JsonMethodStore extends JsonEntityStore {
22
+ readonly parent: JsonClassStore = JsonEntityStore.from(this.target);
23
+ public middlewares: any[] = [];
24
+ public beforeMiddlewares: any[] = [];
25
+ public afterMiddlewares: any[] = [];
26
+ /**
27
+ * Ref to JsonOperation when the decorated object is a method.
28
+ */
29
+ readonly operation: JsonOperation = new JsonOperation();
30
+ /**
31
+ * List of children JsonEntityStore (properties or methods or params)
32
+ */
33
+ readonly children: Map<string | number, JsonParameterStore> = new Map();
34
+
35
+ constructor(options: JsonEntityStoreOptions) {
36
+ super({
37
+ store: Store.fromMethod(options.target, options.propertyKey!),
38
+ descriptor: descriptorOf(options.target, options.propertyKey!),
39
+ ...options
40
+ });
41
+
42
+ const {beforeMiddlewares = [], middlewares = [], afterMiddlewares = []} = options;
43
+
44
+ this.after(afterMiddlewares);
45
+ this.before(beforeMiddlewares);
46
+ this.use(middlewares);
47
+ }
48
+
49
+ get params(): JsonParameterStore[] {
50
+ return this.parameters;
51
+ }
52
+
53
+ get view(): JsonViewOptions {
54
+ return this.store.get("view") as JsonViewOptions;
55
+ }
56
+
57
+ set view(view: JsonViewOptions) {
58
+ this.store.set("view", view);
59
+ }
60
+
61
+ get acceptMimes(): string[] {
62
+ return this.store.get<string[]>("acceptMimes", []);
63
+ }
64
+
65
+ set acceptMimes(mimes: string[]) {
66
+ this.store.set("acceptMimes", mimes);
67
+ }
68
+
69
+ get parameters(): JsonParameterStore[] {
70
+ return [...this.children.values()] as JsonParameterStore[];
71
+ }
72
+
73
+ get operationPaths() {
74
+ return this.operation.operationPaths;
75
+ }
76
+
77
+ /**
78
+ * Get an endpoint.
79
+ * @param target
80
+ * @param propertyKey
81
+ * @param descriptor
82
+ */
83
+ static get(target: Type<any>, propertyKey: string | symbol, descriptor?: PropertyDescriptor): JsonMethodStore {
84
+ descriptor = descriptor || descriptorOf(prototypeOf(target), propertyKey);
85
+
86
+ return JsonEntityStore.from<JsonMethodStore>(prototypeOf(target), propertyKey, descriptor);
87
+ }
88
+
89
+ getResponseOptions(
90
+ status: number,
91
+ {contentType = "application/json", includes}: {contentType?: string; includes?: string[]} = {}
92
+ ): undefined | any {
93
+ const media = this.operation.getResponseOf(status).getMedia(contentType, false);
94
+
95
+ if (media && media.has("schema")) {
96
+ const schema = media.get("schema") as JsonSchema;
97
+ let groups = media.groups;
98
+
99
+ if (includes && media.allowedGroups?.size) {
100
+ groups = [...(groups || []), ...includes.filter((include) => media.allowedGroups!.has(include))];
101
+ }
102
+
103
+ return {type: schema.getComputedItemType(), groups};
104
+ }
105
+
106
+ return {type: this.type};
107
+ }
108
+
109
+ /**
110
+ * Append middlewares to the beforeMiddlewares list.
111
+ * @param args
112
+ * @returns {EndpointMetadata}
113
+ */
114
+ public before(args: Function[]): this {
115
+ this.beforeMiddlewares = this.beforeMiddlewares.concat(args).filter(isFunction);
116
+
117
+ return this;
118
+ }
119
+
120
+ /**
121
+ * Append middlewares to the afterMiddlewares list.
122
+ * @param args
123
+ * @returns {EndpointMetadata}
124
+ */
125
+ public after(args: Function[]): this {
126
+ this.afterMiddlewares = this.afterMiddlewares.concat(args).filter(isFunction);
127
+
128
+ return this;
129
+ }
130
+
131
+ /**
132
+ * Store all arguments collected via Annotation.
133
+ * @param args
134
+ */
135
+ public use(args: Function[]) {
136
+ this.middlewares = this.middlewares.concat(args).filter(isFunction);
137
+
138
+ return this;
139
+ }
140
+
141
+ /**
142
+ * Find the value at the controller level. Let this value be extended or overridden by the endpoint itself.
143
+ *
144
+ * @param key
145
+ * @returns {any}
146
+ */
147
+ public get<T = any>(key: any): T {
148
+ const ctrlValue = Store.from(this.target).get(key);
149
+
150
+ return deepMerge<T>(ctrlValue, this.store.get(key));
151
+ }
152
+
153
+ public getParamTypes(): Record<string, boolean> {
154
+ return [...this.children.values()].reduce(
155
+ (obj, item) => ({
156
+ ...obj,
157
+ [item.paramType]: true
158
+ }),
159
+ {}
160
+ );
161
+ }
162
+
163
+ protected build() {
164
+ if (!this._type) {
165
+ let type: any = Metadata.getReturnType(this.target, this.propertyKey);
166
+ type = isPromise(type) ? undefined : type;
167
+
168
+ this.buildType(type);
169
+ }
170
+
171
+ this._type = this._type || Object;
172
+
173
+ this.parent.children.set(this.propertyName, this);
174
+
175
+ if (isCollection(this._type)) {
176
+ this.collectionType = this._type;
177
+ // @ts-ignore
178
+ delete this._type;
179
+ }
180
+
181
+ this._schema = JsonSchema.from({
182
+ type: this.collectionType || this.type
183
+ });
184
+
185
+ if (this.collectionType) {
186
+ this._schema.itemSchema(this.type);
187
+ }
188
+
189
+ this.parent.schema.addProperty(this.propertyName, this.schema);
190
+ }
191
+ }
192
+
193
+ /**
194
+ * EndpointMetadata contains metadata about a controller and his method.
195
+ * Each annotation (@Get, @Body...) attached to a method are stored into endpoint.
196
+ * EndpointMetadata convert this metadata to an array which contain arguments to call an Express method.
197
+ *
198
+ * Example :
199
+ *```typescript
200
+ * @Controller("/my-path")
201
+ * provide MyClass {
202
+ *
203
+ * @Get("/")
204
+ * @Authenticated()
205
+ * public myMethod(){}
206
+ * }
207
+ *```
208
+ *
209
+ * @alias JsonMethodStore
210
+ */
211
+ export type EndpointMetadata = JsonMethodStore;
212
+ export const EndpointMetadata = JsonMethodStore;
@@ -0,0 +1,126 @@
1
+ import {descriptorOf} from "@tsed/core";
2
+
3
+ import {execMapper, Get, getSpec, In, JsonEntityStore, OperationPath, Path, Redirect, Returns, SpecTypes} from "../index.js";
4
+
5
+ describe("JsonOperation", () => {
6
+ describe("getStatus()", () => {
7
+ it("should return the status", () => {
8
+ class MyController {
9
+ @OperationPath("GET", "/")
10
+ @(Returns(200).Header("x-token", "token"))
11
+ @(Returns(200).Header("x-token2", "token2"))
12
+ method() {}
13
+ }
14
+
15
+ const entity = JsonEntityStore.from(MyController.prototype, "method", descriptorOf(MyController, "method"));
16
+
17
+ expect(entity.operation?.getStatus()).toBe(200);
18
+ expect(entity.operation?.status).toBe(200);
19
+ expect(execMapper("operationResponse", [entity.operation?.response], {})).toEqual({
20
+ content: {
21
+ "*/*": {
22
+ schema: {
23
+ type: "object"
24
+ }
25
+ }
26
+ },
27
+ description: "Success",
28
+ headers: {
29
+ "x-token": {
30
+ example: "token",
31
+ schema: {
32
+ type: "string"
33
+ }
34
+ },
35
+ "x-token2": {
36
+ example: "token2",
37
+ schema: {
38
+ type: "string"
39
+ }
40
+ }
41
+ }
42
+ });
43
+ expect(entity.operation?.getHeadersOf(200)).toEqual({
44
+ "x-token": {
45
+ example: "token",
46
+ type: "string"
47
+ },
48
+ "x-token2": {
49
+ example: "token2",
50
+ type: "string"
51
+ }
52
+ });
53
+ });
54
+ });
55
+ describe("isRedirection()", () => {
56
+ it("should return redirection status", () => {
57
+ @Path("/")
58
+ class MyController {
59
+ @Redirect("/path/to")
60
+ @Get("/")
61
+ test() {}
62
+ }
63
+
64
+ const entity = JsonEntityStore.fromMethod(MyController, "test");
65
+
66
+ expect(entity.operation.isRedirection()).toBe(true);
67
+ expect(entity.operation.isRedirection(302)).toBe(true);
68
+ expect(entity.operation.isRedirection(200)).toBe(false);
69
+ });
70
+ });
71
+ describe("getContentTypeOf()", () => {
72
+ it("should return the content type of", () => {
73
+ @Path("/")
74
+ class MyController {
75
+ @Get("/")
76
+ @(Returns(200, String).ContentType("text/html"))
77
+ test() {}
78
+ }
79
+
80
+ const entity = JsonEntityStore.fromMethod(MyController, "test");
81
+
82
+ expect(entity.operation.getContentTypeOf(200)).toEqual("text/html");
83
+ expect(entity.operation.getContentTypeOf(201)).toEqual(undefined);
84
+ });
85
+ });
86
+ describe("when custom parameter is used", () => {
87
+ it("should not generate invalid parameter", () => {
88
+ class MyController {
89
+ @OperationPath("GET", "/")
90
+ method(@In("custom") req: any, @In("body") type: string) {}
91
+ }
92
+
93
+ expect(getSpec(MyController, {specType: SpecTypes.OPENAPI})).toEqual({
94
+ paths: {
95
+ "/": {
96
+ get: {
97
+ operationId: "myControllerMethod",
98
+ parameters: [],
99
+ requestBody: {
100
+ content: {
101
+ "application/json": {
102
+ schema: {
103
+ type: "string"
104
+ }
105
+ }
106
+ },
107
+ required: false
108
+ },
109
+ responses: {
110
+ "200": {
111
+ description: "Success"
112
+ }
113
+ },
114
+ tags: ["MyController"]
115
+ }
116
+ }
117
+ },
118
+ tags: [
119
+ {
120
+ name: "MyController"
121
+ }
122
+ ]
123
+ });
124
+ });
125
+ });
126
+ });