@tsed/schema 8.0.0 → 8.0.2

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 (291) hide show
  1. package/package.json +11 -10
  2. package/src/components/async-api/channelsMapper.ts +81 -0
  3. package/src/components/async-api/generate.ts +24 -0
  4. package/src/components/async-api/messageMapper.ts +62 -0
  5. package/src/components/async-api/payloadMapper.ts +61 -0
  6. package/src/components/async-api/responseMapper.ts +70 -0
  7. package/src/components/default/anyMapper.spec.ts +7 -0
  8. package/src/components/default/anyMapper.ts +33 -0
  9. package/src/components/default/classMapper.ts +55 -0
  10. package/src/components/default/discriminatorMappingMapper.ts +36 -0
  11. package/src/components/default/enumsMapper.spec.ts +64 -0
  12. package/src/components/default/enumsMapper.ts +17 -0
  13. package/src/components/default/genericsMapper.ts +70 -0
  14. package/src/components/default/inheritedClassMapper.ts +24 -0
  15. package/src/components/default/itemMapper.ts +8 -0
  16. package/src/components/default/lazyRefMapper.ts +21 -0
  17. package/src/components/default/mapMapper.ts +25 -0
  18. package/src/components/default/nullableMapper.ts +55 -0
  19. package/src/components/default/objectMapper.ts +36 -0
  20. package/src/components/default/ofMapper.ts +16 -0
  21. package/src/components/default/propertiesMapper.ts +25 -0
  22. package/src/components/default/requiredMapper.ts +69 -0
  23. package/src/components/default/schemaMapper.ts +151 -0
  24. package/src/components/index.ts +36 -0
  25. package/src/components/open-spec/discriminatorMappingMapper.ts +7 -0
  26. package/src/components/open-spec/enumsMapper.ts +20 -0
  27. package/src/components/open-spec/generate.ts +29 -0
  28. package/src/components/open-spec/nullableMapper.ts +51 -0
  29. package/src/components/open-spec/operationInFilesMapper.ts +32 -0
  30. package/src/components/open-spec/operationInParameterMapper.ts +56 -0
  31. package/src/components/open-spec/operationInParametersMapper.ts +9 -0
  32. package/src/components/open-spec/operationInQueryMapper.ts +74 -0
  33. package/src/components/open-spec/operationMapper.ts +65 -0
  34. package/src/components/open-spec/operationMediaMapper.ts +11 -0
  35. package/src/components/open-spec/operationRequestBodyMapper.ts +82 -0
  36. package/src/components/open-spec/operationResponseMapper.ts +26 -0
  37. package/src/components/open-spec/pathsMapper.ts +132 -0
  38. package/src/constants/OperationVerbs.ts +29 -0
  39. package/src/constants/httpStatusMessages.ts +21 -0
  40. package/src/constants/jsonSchemaProperties.ts +17 -0
  41. package/src/decorators/class/children.ts +22 -0
  42. package/src/decorators/class/discriminatorValue.ts +53 -0
  43. package/src/decorators/collections/collectionContains.ts +25 -0
  44. package/src/decorators/collections/collectionOf.spec.ts +327 -0
  45. package/src/decorators/collections/collectionOf.ts +182 -0
  46. package/src/decorators/collections/maxItems.spec.ts +40 -0
  47. package/src/decorators/collections/maxItems.ts +59 -0
  48. package/src/decorators/collections/maxProperties.spec.ts +109 -0
  49. package/src/decorators/collections/maxProperties.ts +82 -0
  50. package/src/decorators/collections/minItems.spec.ts +40 -0
  51. package/src/decorators/collections/minItems.ts +63 -0
  52. package/src/decorators/collections/minProperties.spec.ts +109 -0
  53. package/src/decorators/collections/minProperties.ts +81 -0
  54. package/src/decorators/collections/uniqueItems.spec.ts +28 -0
  55. package/src/decorators/collections/uniqueItems.ts +59 -0
  56. package/src/decorators/common/__snapshots__/groups.spec.ts.snap +321 -0
  57. package/src/decorators/common/additionalProperties.spec.ts +133 -0
  58. package/src/decorators/common/additionalProperties.ts +17 -0
  59. package/src/decorators/common/allOf.spec.ts +276 -0
  60. package/src/decorators/common/allOf.ts +19 -0
  61. package/src/decorators/common/allow.spec.ts +130 -0
  62. package/src/decorators/common/allow.ts +50 -0
  63. package/src/decorators/common/any.spec.ts +109 -0
  64. package/src/decorators/common/any.ts +45 -0
  65. package/src/decorators/common/anyOf.spec.ts +270 -0
  66. package/src/decorators/common/anyOf.ts +23 -0
  67. package/src/decorators/common/const.spec.ts +98 -0
  68. package/src/decorators/common/const.ts +68 -0
  69. package/src/decorators/common/customKey.spec.ts +105 -0
  70. package/src/decorators/common/customKey.ts +39 -0
  71. package/src/decorators/common/default.spec.ts +25 -0
  72. package/src/decorators/common/default.ts +47 -0
  73. package/src/decorators/common/defaultMsg.spec.ts +22 -0
  74. package/src/decorators/common/defaultMsg.ts +38 -0
  75. package/src/decorators/common/description.spec.ts +166 -0
  76. package/src/decorators/common/description.ts +77 -0
  77. package/src/decorators/common/discriminatorKey.ts +14 -0
  78. package/src/decorators/common/enum.spec.ts +305 -0
  79. package/src/decorators/common/enum.ts +111 -0
  80. package/src/decorators/common/errorMsg.spec.ts +57 -0
  81. package/src/decorators/common/errorMsg.ts +14 -0
  82. package/src/decorators/common/example.spec.ts +93 -0
  83. package/src/decorators/common/example.ts +18 -0
  84. package/src/decorators/common/examples.spec.ts +101 -0
  85. package/src/decorators/common/examples.ts +20 -0
  86. package/src/decorators/common/exclusiveMaximum.spec.ts +47 -0
  87. package/src/decorators/common/exclusiveMaximum.ts +75 -0
  88. package/src/decorators/common/exclusiveMinimum.spec.ts +47 -0
  89. package/src/decorators/common/exclusiveMinimum.ts +75 -0
  90. package/src/decorators/common/format.spec.ts +149 -0
  91. package/src/decorators/common/format.ts +486 -0
  92. package/src/decorators/common/forwardGroups.spec.ts +270 -0
  93. package/src/decorators/common/forwardGroups.ts +16 -0
  94. package/src/decorators/common/groups.spec.ts +968 -0
  95. package/src/decorators/common/groups.ts +66 -0
  96. package/src/decorators/common/hidden.spec.ts +52 -0
  97. package/src/decorators/common/hidden.ts +36 -0
  98. package/src/decorators/common/ignore.spec.ts +88 -0
  99. package/src/decorators/common/ignore.ts +66 -0
  100. package/src/decorators/common/integer.spec.ts +110 -0
  101. package/src/decorators/common/integer.ts +69 -0
  102. package/src/decorators/common/jsonEntityFn.spec.ts +16 -0
  103. package/src/decorators/common/jsonEntityFn.ts +33 -0
  104. package/src/decorators/common/maxLength.spec.ts +61 -0
  105. package/src/decorators/common/maxLength.ts +88 -0
  106. package/src/decorators/common/maximum.spec.ts +73 -0
  107. package/src/decorators/common/maximum.ts +146 -0
  108. package/src/decorators/common/minLength.spec.ts +186 -0
  109. package/src/decorators/common/minLength.ts +89 -0
  110. package/src/decorators/common/minimum.spec.ts +74 -0
  111. package/src/decorators/common/minimum.ts +145 -0
  112. package/src/decorators/common/multipleOf.spec.ts +33 -0
  113. package/src/decorators/common/multipleOf.ts +80 -0
  114. package/src/decorators/common/name.spec.ts +56 -0
  115. package/src/decorators/common/name.ts +50 -0
  116. package/src/decorators/common/nullable.spec.ts +818 -0
  117. package/src/decorators/common/nullable.ts +32 -0
  118. package/src/decorators/common/oneOf.spec.ts +301 -0
  119. package/src/decorators/common/oneOf.ts +24 -0
  120. package/src/decorators/common/optional.spec.ts +43 -0
  121. package/src/decorators/common/optional.ts +26 -0
  122. package/src/decorators/common/pattern.spec.ts +27 -0
  123. package/src/decorators/common/pattern.ts +102 -0
  124. package/src/decorators/common/property.ts +24 -0
  125. package/src/decorators/common/readOnly.spec.ts +135 -0
  126. package/src/decorators/common/readOnly.ts +17 -0
  127. package/src/decorators/common/recordOf.spec.ts +41 -0
  128. package/src/decorators/common/recordOf.ts +59 -0
  129. package/src/decorators/common/required.spec.ts +188 -0
  130. package/src/decorators/common/required.ts +50 -0
  131. package/src/decorators/common/requiredGroups.spec.ts +127 -0
  132. package/src/decorators/common/requiredGroups.ts +23 -0
  133. package/src/decorators/common/schema.spec.ts +23 -0
  134. package/src/decorators/common/schema.ts +64 -0
  135. package/src/decorators/common/title.spec.ts +20 -0
  136. package/src/decorators/common/title.ts +45 -0
  137. package/src/decorators/common/typeError.spec.ts +22 -0
  138. package/src/decorators/common/typeError.ts +15 -0
  139. package/src/decorators/common/writeOnly.spec.ts +105 -0
  140. package/src/decorators/common/writeOnly.ts +17 -0
  141. package/src/decorators/config/jsonEntityComponent.ts +16 -0
  142. package/src/decorators/generics/genericOf.spec.ts +246 -0
  143. package/src/decorators/generics/genericOf.ts +112 -0
  144. package/src/decorators/generics/generics.ts +22 -0
  145. package/src/decorators/operations/acceptMime.spec.ts +39 -0
  146. package/src/decorators/operations/acceptMime.ts +25 -0
  147. package/src/decorators/operations/consumes.spec.ts +89 -0
  148. package/src/decorators/operations/consumes.ts +37 -0
  149. package/src/decorators/operations/contentType.spec.ts +40 -0
  150. package/src/decorators/operations/contentType.ts +24 -0
  151. package/src/decorators/operations/deprecated.spec.ts +223 -0
  152. package/src/decorators/operations/deprecated.ts +49 -0
  153. package/src/decorators/operations/header.spec.ts +214 -0
  154. package/src/decorators/operations/header.ts +62 -0
  155. package/src/decorators/operations/in.spec.ts +321 -0
  156. package/src/decorators/operations/in.ts +141 -0
  157. package/src/decorators/operations/inFile.spec.ts +209 -0
  158. package/src/decorators/operations/inFile.ts +55 -0
  159. package/src/decorators/operations/location.spec.ts +51 -0
  160. package/src/decorators/operations/location.ts +26 -0
  161. package/src/decorators/operations/operation.ts +119 -0
  162. package/src/decorators/operations/operationId.spec.ts +43 -0
  163. package/src/decorators/operations/operationId.ts +22 -0
  164. package/src/decorators/operations/operationPath.spec.ts +42 -0
  165. package/src/decorators/operations/operationPath.ts +33 -0
  166. package/src/decorators/operations/partial.spec.ts +249 -0
  167. package/src/decorators/operations/partial.ts +14 -0
  168. package/src/decorators/operations/path.spec.ts +47 -0
  169. package/src/decorators/operations/path.ts +27 -0
  170. package/src/decorators/operations/produces.spec.ts +89 -0
  171. package/src/decorators/operations/produces.ts +44 -0
  172. package/src/decorators/operations/publish.spec.ts +26 -0
  173. package/src/decorators/operations/publish.ts +6 -0
  174. package/src/decorators/operations/redirect.spec.ts +146 -0
  175. package/src/decorators/operations/redirect.ts +82 -0
  176. package/src/decorators/operations/returns.spec.ts +1393 -0
  177. package/src/decorators/operations/returns.ts +662 -0
  178. package/src/decorators/operations/route.spec.ts +211 -0
  179. package/src/decorators/operations/route.ts +154 -0
  180. package/src/decorators/operations/security.spec.ts +162 -0
  181. package/src/decorators/operations/security.ts +83 -0
  182. package/src/decorators/operations/status.spec.ts +582 -0
  183. package/src/decorators/operations/status.ts +130 -0
  184. package/src/decorators/operations/subscribe.spec.ts +32 -0
  185. package/src/decorators/operations/subscribe.ts +6 -0
  186. package/src/decorators/operations/summary.spec.ts +45 -0
  187. package/src/decorators/operations/summary.ts +32 -0
  188. package/src/decorators/operations/tags.spec.ts +113 -0
  189. package/src/decorators/operations/tags.ts +53 -0
  190. package/src/decorators/operations/view.spec.ts +16 -0
  191. package/src/decorators/operations/view.ts +27 -0
  192. package/src/domain/DecoratorContext.ts +116 -0
  193. package/src/domain/JsonAliasMap.ts +19 -0
  194. package/src/domain/JsonClassStore.ts +35 -0
  195. package/src/domain/JsonDiscriminator.spec.ts +65 -0
  196. package/src/domain/JsonDiscriminator.ts +52 -0
  197. package/src/domain/JsonEntityStore.spec.ts +277 -0
  198. package/src/domain/JsonEntityStore.ts +284 -0
  199. package/src/domain/JsonFormatTypes.ts +17 -0
  200. package/src/domain/JsonLazyRef.ts +21 -0
  201. package/src/domain/JsonMap.spec.ts +15 -0
  202. package/src/domain/JsonMap.ts +33 -0
  203. package/src/domain/JsonMedia.ts +24 -0
  204. package/src/domain/JsonMethodStore.spec.ts +301 -0
  205. package/src/domain/JsonMethodStore.ts +212 -0
  206. package/src/domain/JsonOperation.spec.ts +126 -0
  207. package/src/domain/JsonOperation.ts +219 -0
  208. package/src/domain/JsonOperationPathsMap.ts +16 -0
  209. package/src/domain/JsonOperationRoute.spec.ts +109 -0
  210. package/src/domain/JsonOperationRoute.ts +75 -0
  211. package/src/domain/JsonParameter.ts +70 -0
  212. package/src/domain/JsonParameterStore.spec.ts +212 -0
  213. package/src/domain/JsonParameterStore.ts +138 -0
  214. package/src/domain/JsonParameterTypes.spec.ts +12 -0
  215. package/src/domain/JsonParameterTypes.ts +20 -0
  216. package/src/domain/JsonPropertyStore.spec.ts +177 -0
  217. package/src/domain/JsonPropertyStore.ts +92 -0
  218. package/src/domain/JsonRequestBody.ts +47 -0
  219. package/src/domain/JsonResponse.spec.ts +33 -0
  220. package/src/domain/JsonResponse.ts +64 -0
  221. package/src/domain/JsonSchema.spec.ts +1470 -0
  222. package/src/domain/JsonSchema.ts +1049 -0
  223. package/src/domain/SpecTypes.ts +6 -0
  224. package/src/hooks/alterIgnore.ts +25 -0
  225. package/src/hooks/alterOneOf.ts +67 -0
  226. package/src/hooks/alterRequiredGroups.ts +12 -0
  227. package/src/index.ts +185 -0
  228. package/src/interfaces/IgnoreCallback.ts +5 -0
  229. package/src/interfaces/JsonHookContext.ts +18 -0
  230. package/src/interfaces/JsonOpenSpec.ts +7 -0
  231. package/src/interfaces/JsonSchemaOptions.ts +35 -0
  232. package/src/registries/JsonSchemaMapperContainer.ts +52 -0
  233. package/src/registries/enumRegistries.ts +3 -0
  234. package/src/utils/__snapshots__/generateSpec.spec.ts.snap +109 -0
  235. package/src/utils/buildPath.ts +8 -0
  236. package/src/utils/concatParameters.ts +12 -0
  237. package/src/utils/concatPath.spec.ts +23 -0
  238. package/src/utils/concatPath.ts +24 -0
  239. package/src/utils/defineStatusModel.ts +11 -0
  240. package/src/utils/from.spec.ts +243 -0
  241. package/src/utils/from.ts +281 -0
  242. package/src/utils/generateSpec.spec.ts +1262 -0
  243. package/src/utils/generateSpec.ts +54 -0
  244. package/src/utils/generics.ts +83 -0
  245. package/src/utils/getComputedType.spec.ts +45 -0
  246. package/src/utils/getComputedType.ts +39 -0
  247. package/src/utils/getInheritedStores.spec.ts +47 -0
  248. package/src/utils/getInheritedStores.ts +23 -0
  249. package/src/utils/getJsonEntityStore.ts +36 -0
  250. package/src/utils/getJsonPathParameters.spec.ts +126 -0
  251. package/src/utils/getJsonPathParameters.ts +97 -0
  252. package/src/utils/getJsonSchema.spec.ts +695 -0
  253. package/src/utils/getJsonSchema.ts +69 -0
  254. package/src/utils/getJsonType.spec.ts +28 -0
  255. package/src/utils/getJsonType.ts +46 -0
  256. package/src/utils/getOperationsRoutes.spec.ts +284 -0
  257. package/src/utils/getOperationsRoutes.ts +56 -0
  258. package/src/utils/getOperationsStore.spec.ts +47 -0
  259. package/src/utils/getOperationsStores.ts +37 -0
  260. package/src/utils/getPropertiesStores.spec.ts +138 -0
  261. package/src/utils/getPropertiesStores.ts +66 -0
  262. package/src/utils/getSpec.spec.ts +610 -0
  263. package/src/utils/getSpec.ts +117 -0
  264. package/src/utils/getSpecType.spec.ts +16 -0
  265. package/src/utils/getSpecType.ts +19 -0
  266. package/src/utils/isSuccessStatus.spec.ts +22 -0
  267. package/src/utils/isSuccessStatus.ts +2 -0
  268. package/src/utils/mapHeaders.spec.ts +57 -0
  269. package/src/utils/mapHeaders.ts +33 -0
  270. package/src/utils/mapOpenSpec.ts +44 -0
  271. package/src/utils/mapOpenSpec2.ts +50 -0
  272. package/src/utils/mapOpenSpec3.ts +25 -0
  273. package/src/utils/mapOpenSpecInfo.ts +19 -0
  274. package/src/utils/mapOperationOptions.ts +34 -0
  275. package/src/utils/matchGroups.spec.ts +85 -0
  276. package/src/utils/matchGroups.ts +64 -0
  277. package/src/utils/mergeSpec.spec.ts +533 -0
  278. package/src/utils/mergeSpec.ts +54 -0
  279. package/src/utils/operationIdFormatter.spec.ts +11 -0
  280. package/src/utils/operationIdFormatter.ts +49 -0
  281. package/src/utils/ref.ts +65 -0
  282. package/src/utils/removeHiddenOperation.ts +5 -0
  283. package/src/utils/serializeEnumValues.ts +27 -0
  284. package/src/utils/somethingOf.ts +11 -0
  285. package/src/utils/toJsonMapCollection.spec.ts +18 -0
  286. package/src/utils/toJsonMapCollection.ts +12 -0
  287. package/src/utils/toJsonRegex.ts +7 -0
  288. package/src/utils/transformToOS2.spec.ts +177 -0
  289. package/src/utils/transformToOS2.ts +607 -0
  290. package/src/utils/withErrorMsg.spec.ts +16 -0
  291. package/src/utils/withErrorMsg.ts +56 -0
@@ -0,0 +1,55 @@
1
+ import {DecoratorParameters, Metadata, useDecorators, useMethodDecorators} from "@tsed/core";
2
+
3
+ import {JsonParameterTypes} from "../../domain/JsonParameterTypes.js";
4
+ import {Name} from "../common/name.js";
5
+ import {Consumes} from "./consumes.js";
6
+ import {In} from "./in.js";
7
+ import {Returns} from "./returns.js";
8
+
9
+ /**
10
+ * Add a input file parameter.
11
+ *
12
+ * ::: warning
13
+ * Don't use decorator with Ts.ED application to decorate parameters. Use @@BodyParams@@, @@PathParams@@, etc... instead.
14
+ * But you can use this decorator on Method, to add extra in parameters like Authorization header.
15
+ *
16
+ * ```typescript
17
+ * @Controller("/")
18
+ * class MyController {
19
+ * @Post("/")
20
+ * method(@InFile("file1") file: any) {
21
+ * }
22
+ * }
23
+ * ```
24
+ * :::
25
+ *
26
+ * @decorator
27
+ * @swagger
28
+ * @schema
29
+ * @input
30
+ * @operation
31
+ * @param name
32
+ */
33
+ export function InFile(name: string): ParameterDecorator {
34
+ return (...args: DecoratorParameters): void => {
35
+ const [target, propertyKey, index] = args;
36
+ const multiple = Metadata.getParamTypes(target, propertyKey)[index as number] === Array;
37
+
38
+ name = (typeof name === "object" ? undefined : name)!;
39
+
40
+ const expression = [name, !multiple && "0"].filter(Boolean).join(".");
41
+
42
+ const decorators = useDecorators(
43
+ In(JsonParameterTypes.FILES),
44
+ Name(expression),
45
+ useMethodDecorators(
46
+ Returns(400).Description(
47
+ `<File too long | Too many parts | Too many files | Field name too long | Field value too long | Too many fields | Unexpected field> [fieldName] Example: File too long file1`
48
+ ),
49
+ Consumes("multipart/form-data")
50
+ )
51
+ );
52
+
53
+ decorators(...args);
54
+ };
55
+ }
@@ -0,0 +1,51 @@
1
+ import {getSpec, Location, OperationPath, SpecTypes} from "../../index.js";
2
+
3
+ describe("Location", () => {
4
+ it("should set Header", () => {
5
+ class MyController {
6
+ @(Location("/path/to/test", {description: "Path to next step"}).Status(301))
7
+ @OperationPath("GET", "/")
8
+ test() {}
9
+ }
10
+
11
+ const spec = getSpec(MyController, {specType: SpecTypes.OPENAPI});
12
+
13
+ expect(spec).toEqual({
14
+ paths: {
15
+ "/": {
16
+ get: {
17
+ operationId: "myControllerTest",
18
+ parameters: [],
19
+ responses: {
20
+ "301": {
21
+ content: {
22
+ "*/*": {
23
+ schema: {
24
+ type: "object"
25
+ }
26
+ }
27
+ },
28
+ description: "Moved Permanently",
29
+ headers: {
30
+ Location: {
31
+ description: "Path to next step",
32
+ example: "/path/to/test",
33
+ schema: {
34
+ type: "string"
35
+ }
36
+ }
37
+ }
38
+ }
39
+ },
40
+ tags: ["MyController"]
41
+ }
42
+ }
43
+ },
44
+ tags: [
45
+ {
46
+ name: "MyController"
47
+ }
48
+ ]
49
+ });
50
+ });
51
+ });
@@ -0,0 +1,26 @@
1
+ import {JsonHeader} from "../../interfaces/JsonOpenSpec.js";
2
+ import {Returns} from "./returns.js";
3
+
4
+ /**
5
+ * Sets the response Location HTTP header to the specified path parameter.
6
+ *
7
+ * ```typescript
8
+ * @Location('/foo/bar', {description: 'Location to the next step'})
9
+ * @Location('http://example.com')
10
+ * @Location('back')
11
+ * private myMethod() {
12
+ *
13
+ * }
14
+ * ```
15
+ *
16
+ * A path value of “back” has a special meaning, it refers to the URL specified in the `Referer` header of the request. If the `Referer` header was not specified, it refers to “/”.
17
+ *
18
+ * @param location
19
+ * @param meta
20
+ * @returns {Function}
21
+ * @decorator
22
+ * @operation
23
+ */
24
+ export function Location(location: string, meta?: JsonHeader) {
25
+ return Returns().Location(location, meta);
26
+ }
@@ -0,0 +1,119 @@
1
+ import {OperationVerbs} from "../../constants/OperationVerbs.js";
2
+ import {DecoratorContext} from "../../domain/DecoratorContext.js";
3
+ import {JsonMethodStore} from "../../domain/JsonMethodStore.js";
4
+ import {mapOperationOptions} from "../../utils/mapOperationOptions.js";
5
+
6
+ export interface RouteChainedDecorators {
7
+ <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T> | void;
8
+
9
+ /**
10
+ * @param string
11
+ * @constructor
12
+ */
13
+ Path(string: string): this;
14
+
15
+ /**
16
+ * Set the operation method
17
+ * @param method
18
+ */
19
+ Method(method: OperationVerbs | string): this;
20
+
21
+ /**
22
+ * Set the operation id
23
+ * @param id
24
+ */
25
+ Id(id: string): this;
26
+
27
+ /**
28
+ * Set the operation id
29
+ * @param name
30
+ */
31
+ Name(name: string): this;
32
+
33
+ /**
34
+ *
35
+ * @param description
36
+ */
37
+ Description(description: string): this;
38
+
39
+ /**
40
+ * Summary
41
+ * @constructor
42
+ * @param Summary
43
+ */
44
+ Summary(Summary: string): this;
45
+
46
+ Use(...args: any[]): this;
47
+
48
+ UseAfter(...args: any[]): this;
49
+
50
+ UseBefore(...args: any[]): this;
51
+ }
52
+
53
+ class OperationDecoratorContext extends DecoratorContext<RouteChainedDecorators> {
54
+ readonly methods: string[] = ["name", "description", "summary", "method", "id", "use", "useAfter", "useBefore"];
55
+ protected declare entity: JsonMethodStore;
56
+
57
+ protected beforeInit() {
58
+ const path: string = this.get("path");
59
+ const method: string = OperationVerbs[this.get("method") as OperationVerbs] || OperationVerbs.CUSTOM;
60
+
61
+ path && this.entity.operation.addOperationPath(method, path);
62
+ }
63
+
64
+ protected onMapKey(key: string, value: any) {
65
+ switch (key) {
66
+ case "name":
67
+ case "id":
68
+ this.entity.operation.operationId(value);
69
+ return;
70
+ case "summary":
71
+ this.entity.operation.summary(value);
72
+ return;
73
+ case "description":
74
+ this.entity.operation.description(value);
75
+ return;
76
+ case "use":
77
+ this.entity.use(value);
78
+ return;
79
+ case "useAfter":
80
+ this.entity.after(value);
81
+ return;
82
+ case "useBefore":
83
+ this.entity.before(value);
84
+ return;
85
+ }
86
+
87
+ return super.onMapKey(key, value);
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Describe a new route with a method and path.
93
+ *
94
+ * ```typescript
95
+ * @Controller('/')
96
+ * export class Ctrl {
97
+ *
98
+ * @Route('GET', '/')
99
+ * get() { }
100
+ * }
101
+ *
102
+ * ```
103
+ *
104
+ * @returns {Function}
105
+ * @param method
106
+ * @param path
107
+ * @param args
108
+ * @decorator
109
+ * @operation
110
+ */
111
+ export function Operation(method: string, path: string, ...args: any[]): RouteChainedDecorators;
112
+ export function Operation(...args: any[]): RouteChainedDecorators;
113
+ export function Operation(...args: any[]): RouteChainedDecorators {
114
+ const routeOptions = mapOperationOptions(args);
115
+
116
+ const context = new OperationDecoratorContext(routeOptions);
117
+
118
+ return context.build();
119
+ }
@@ -0,0 +1,43 @@
1
+ import {getSpec, OperationId, OperationPath} from "../../index.js";
2
+
3
+ describe("OperationId", () => {
4
+ it("should store metadata", () => {
5
+ class MyController {
6
+ @OperationPath("POST", "/")
7
+ @OperationId("id")
8
+ get() {}
9
+ }
10
+
11
+ expect(getSpec(MyController)).toEqual({
12
+ tags: [
13
+ {
14
+ name: "MyController"
15
+ }
16
+ ],
17
+ paths: {
18
+ "/": {
19
+ post: {
20
+ operationId: "id",
21
+ parameters: [],
22
+ responses: {
23
+ "200": {
24
+ description: "Success"
25
+ }
26
+ },
27
+ tags: ["MyController"]
28
+ }
29
+ }
30
+ }
31
+ });
32
+ });
33
+ it("should throw error for unsupported usage", () => {
34
+ let actualError: any;
35
+ try {
36
+ OperationId("id")(class Test {});
37
+ } catch (er) {
38
+ actualError = er;
39
+ }
40
+
41
+ expect(actualError.message).toEqual("OperationId cannot be used as class decorator on Test");
42
+ });
43
+ });
@@ -0,0 +1,22 @@
1
+ import {DecoratorTypes, UnsupportedDecoratorType} from "@tsed/core";
2
+
3
+ import {JsonEntityFn} from "../common/jsonEntityFn.js";
4
+
5
+ /**
6
+ * Set a swagger operationId explicitly.
7
+ *
8
+ * @param operationId
9
+ * @decorator
10
+ * @swagger
11
+ * @schema
12
+ * @operation
13
+ */
14
+ export function OperationId(operationId: string) {
15
+ return JsonEntityFn((store, args) => {
16
+ if (store.decoratorType !== DecoratorTypes.METHOD) {
17
+ throw new UnsupportedDecoratorType(OperationId, args);
18
+ }
19
+
20
+ store.operation!.operationId(operationId);
21
+ });
22
+ }
@@ -0,0 +1,42 @@
1
+ import {getSpec, OperationPath, OperationVerbs} from "../../index.js";
2
+
3
+ describe("OperationPath", () => {
4
+ it("should store metadata", () => {
5
+ class MyController {
6
+ @OperationPath(OperationVerbs.OPTIONS, "/")
7
+ options() {}
8
+ }
9
+
10
+ expect(getSpec(MyController)).toEqual({
11
+ tags: [
12
+ {
13
+ name: "MyController"
14
+ }
15
+ ],
16
+ paths: {
17
+ "/": {
18
+ options: {
19
+ operationId: "myControllerOptions",
20
+ parameters: [],
21
+ responses: {
22
+ "200": {
23
+ description: "Success"
24
+ }
25
+ },
26
+ tags: ["MyController"]
27
+ }
28
+ }
29
+ }
30
+ });
31
+ });
32
+ it("should throw error for unsupported usage", () => {
33
+ let actualError: any;
34
+ try {
35
+ OperationPath("GET", "/")(class Test {});
36
+ } catch (er) {
37
+ actualError = er;
38
+ }
39
+
40
+ expect(actualError.message).toEqual("OperationPath cannot be used as class decorator on Test");
41
+ });
42
+ });
@@ -0,0 +1,33 @@
1
+ import {DecoratorTypes, UnsupportedDecoratorType} from "@tsed/core";
2
+
3
+ import {OperationVerbs} from "../../constants/OperationVerbs.js";
4
+ import {JsonEntityFn} from "../common/jsonEntityFn.js";
5
+
6
+ /**
7
+ * Declare new Operation with his path and http method.
8
+ *
9
+ * ::: warning
10
+ * Don't use decorator with Ts.ED application.
11
+ *
12
+ * Use theses decorators instead:
13
+ *
14
+ * <ApiList query="status.includes('decorator') && status.includes('httpMethod')" />
15
+ *
16
+ * :::
17
+ *
18
+ * @param method
19
+ * @param path
20
+ * @decorator
21
+ * @swagger
22
+ * @schema
23
+ * @operation
24
+ */
25
+ export function OperationPath(method: OperationVerbs | string, path: string | RegExp = "/") {
26
+ return JsonEntityFn((store, args) => {
27
+ if (store.decoratorType !== DecoratorTypes.METHOD) {
28
+ throw new UnsupportedDecoratorType(OperationPath, args);
29
+ }
30
+
31
+ store.operation!.addOperationPath(method.toUpperCase(), path);
32
+ });
33
+ }
@@ -0,0 +1,249 @@
1
+ import {Ajv} from "ajv";
2
+
3
+ import {
4
+ CollectionOf,
5
+ getJsonSchema,
6
+ getSpec,
7
+ Groups,
8
+ In,
9
+ OperationPath,
10
+ Path,
11
+ Property,
12
+ Required,
13
+ Returns,
14
+ SpecTypes
15
+ } from "../../index.js";
16
+ import {Partial} from "./partial.js";
17
+
18
+ class ChildModel {
19
+ @Groups("!creation")
20
+ id: string;
21
+
22
+ @Required()
23
+ prop1: string;
24
+ }
25
+
26
+ class MyModel {
27
+ @Groups("!creation")
28
+ id: string;
29
+
30
+ @Groups("group.summary")
31
+ @Required()
32
+ prop1: string;
33
+
34
+ @Groups("group.extended")
35
+ @Required()
36
+ prop2: string;
37
+
38
+ @Property()
39
+ @Required()
40
+ prop3: string;
41
+
42
+ @CollectionOf(ChildModel)
43
+ prop4: ChildModel[];
44
+ }
45
+
46
+ describe("@Partial", () => {
47
+ describe("OpenSpec", () => {
48
+ it("should display fields when a group match with (OS3)", () => {
49
+ @Path("/")
50
+ class MyController {
51
+ @OperationPath("PATCH", "/")
52
+ @(Returns(200, MyModel).Groups("group.*"))
53
+ patch(@In("body") @Partial() payload: MyModel) {
54
+ return new MyModel();
55
+ }
56
+ }
57
+
58
+ const spec = getSpec(MyController, {specType: SpecTypes.OPENAPI});
59
+
60
+ expect(spec).toEqual({
61
+ components: {
62
+ schemas: {
63
+ ChildModel: {
64
+ properties: {
65
+ id: {
66
+ type: "string"
67
+ },
68
+ prop1: {
69
+ minLength: 1,
70
+ type: "string"
71
+ }
72
+ },
73
+ required: ["prop1"],
74
+ type: "object"
75
+ },
76
+ MyModelPartial: {
77
+ properties: {
78
+ id: {
79
+ type: "string"
80
+ },
81
+ prop3: {
82
+ minLength: 1,
83
+ type: "string"
84
+ },
85
+ prop4: {
86
+ items: {
87
+ $ref: "#/components/schemas/ChildModel"
88
+ },
89
+ type: "array"
90
+ }
91
+ },
92
+ type: "object"
93
+ },
94
+ MyModelGroup: {
95
+ properties: {
96
+ id: {
97
+ type: "string"
98
+ },
99
+ prop1: {
100
+ minLength: 1,
101
+ type: "string"
102
+ },
103
+ prop2: {
104
+ minLength: 1,
105
+ type: "string"
106
+ },
107
+ prop3: {
108
+ minLength: 1,
109
+ type: "string"
110
+ },
111
+ prop4: {
112
+ items: {
113
+ $ref: "#/components/schemas/ChildModel"
114
+ },
115
+ type: "array"
116
+ }
117
+ },
118
+ required: ["prop1", "prop2", "prop3"],
119
+ type: "object"
120
+ }
121
+ }
122
+ },
123
+ paths: {
124
+ "/": {
125
+ patch: {
126
+ operationId: "myControllerPatch",
127
+ parameters: [],
128
+ requestBody: {
129
+ content: {
130
+ "application/json": {
131
+ schema: {
132
+ $ref: "#/components/schemas/MyModelPartial"
133
+ }
134
+ }
135
+ },
136
+ required: false
137
+ },
138
+ responses: {
139
+ "200": {
140
+ content: {
141
+ "application/json": {
142
+ schema: {
143
+ $ref: "#/components/schemas/MyModelGroup"
144
+ }
145
+ }
146
+ },
147
+ description: "Success"
148
+ }
149
+ },
150
+ tags: ["MyController"]
151
+ }
152
+ }
153
+ },
154
+ tags: [
155
+ {
156
+ name: "MyController"
157
+ }
158
+ ]
159
+ });
160
+ });
161
+ it("should return a valid json-schema", () => {
162
+ const schema = getJsonSchema(MyModel, {});
163
+
164
+ expect(schema).toEqual({
165
+ definitions: {
166
+ ChildModel: {
167
+ properties: {
168
+ id: {
169
+ type: "string"
170
+ },
171
+ prop1: {
172
+ minLength: 1,
173
+ type: "string"
174
+ }
175
+ },
176
+ required: ["prop1"],
177
+ type: "object"
178
+ }
179
+ },
180
+ properties: {
181
+ id: {
182
+ type: "string"
183
+ },
184
+ prop3: {
185
+ minLength: 1,
186
+ type: "string"
187
+ },
188
+ prop4: {
189
+ items: {
190
+ $ref: "#/definitions/ChildModel"
191
+ },
192
+ type: "array"
193
+ }
194
+ },
195
+ required: ["prop3"],
196
+ type: "object"
197
+ });
198
+
199
+ const ajv = new Ajv({strict: true});
200
+
201
+ expect(ajv.validate(schema, {})).toBe(false);
202
+ expect(ajv.validate(schema, {prop3: "test"})).toBe(true);
203
+ });
204
+ it("should return a valid json-schema (partial)", () => {
205
+ const schema = getJsonSchema(MyModel, {
206
+ groups: ["partial"]
207
+ });
208
+
209
+ expect(schema).toEqual({
210
+ definitions: {
211
+ ChildModel: {
212
+ properties: {
213
+ id: {
214
+ type: "string"
215
+ },
216
+ prop1: {
217
+ minLength: 1,
218
+ type: "string"
219
+ }
220
+ },
221
+ required: ["prop1"],
222
+ type: "object"
223
+ }
224
+ },
225
+ properties: {
226
+ id: {
227
+ type: "string"
228
+ },
229
+ prop3: {
230
+ minLength: 1,
231
+ type: "string"
232
+ },
233
+ prop4: {
234
+ items: {
235
+ $ref: "#/definitions/ChildModel"
236
+ },
237
+ type: "array"
238
+ }
239
+ },
240
+ type: "object"
241
+ });
242
+
243
+ const ajv = new Ajv({strict: true});
244
+
245
+ expect(ajv.validate(schema, {})).toBe(true);
246
+ expect(ajv.validate(schema, {prop3: "test"})).toBe(true);
247
+ });
248
+ });
249
+ });
@@ -0,0 +1,14 @@
1
+ import {Groups} from "../common/groups.js";
2
+
3
+ /**
4
+ * Apply Partial group strategy on the input model
5
+ *
6
+ * @decorator
7
+ * @validation
8
+ * @swagger
9
+ * @schema
10
+ * @input
11
+ */
12
+ export function Partial(): ParameterDecorator {
13
+ return Groups("partial") as any;
14
+ }
@@ -0,0 +1,47 @@
1
+ import {getSpec, OperationPath, Path} from "../../index.js";
2
+
3
+ describe("Path", () => {
4
+ it("should declare a path", () => {
5
+ @Path("/path")
6
+ class MyController {
7
+ @OperationPath("POST", "/")
8
+ get() {}
9
+ }
10
+
11
+ expect(getSpec(MyController)).toEqual({
12
+ tags: [
13
+ {
14
+ name: "MyController"
15
+ }
16
+ ],
17
+ paths: {
18
+ "/path": {
19
+ post: {
20
+ operationId: "myControllerGet",
21
+ parameters: [],
22
+ responses: {
23
+ "200": {
24
+ description: "Success"
25
+ }
26
+ },
27
+ tags: ["MyController"]
28
+ }
29
+ }
30
+ }
31
+ });
32
+ });
33
+ it("should throw error for unsupported usage", () => {
34
+ class Test {
35
+ test() {}
36
+ }
37
+
38
+ let actualError: any;
39
+ try {
40
+ Path("/")(Test.prototype, "test", 0);
41
+ } catch (er) {
42
+ actualError = er;
43
+ }
44
+
45
+ expect(actualError.message).toEqual("Path cannot be used as parameter decorator on Test.test.[0]");
46
+ });
47
+ });