@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,117 @@
1
+ import {cleanObject, isArray, Type, uniqBy} from "@tsed/core";
2
+ import {OpenSpec3} from "@tsed/openspec";
3
+
4
+ import {SpecTypes} from "../domain/SpecTypes.js";
5
+ import {JsonSchemaOptions} from "../interfaces/JsonSchemaOptions.js";
6
+ import {execMapper} from "../registries/JsonSchemaMapperContainer.js";
7
+ import {getJsonEntityStore} from "./getJsonEntityStore.js";
8
+ import {mergeSpec} from "./mergeSpec.js";
9
+ import {operationIdFormatter} from "./operationIdFormatter.js";
10
+
11
+ export type JsonTokenOptions = ({token: Type<any>} & Partial<SpecSerializerOptions>)[];
12
+
13
+ export interface SpecSerializerOptions extends JsonSchemaOptions {
14
+ /**
15
+ * Paths
16
+ */
17
+ paths?: Record<string, any>;
18
+ /**
19
+ * Channels
20
+ */
21
+ channels?: Record<string, any>;
22
+ /**
23
+ * Root path. This paths will be added to all generated paths Object.
24
+ */
25
+ rootPath?: string;
26
+ /**
27
+ * A function to generate the operationId.
28
+ */
29
+ operationIdFormatter?: (name: string, propertyKey: string, path: string) => string;
30
+ /**
31
+ * A pattern to generate the operationId.
32
+ */
33
+ operationIdPattern?: string;
34
+ }
35
+
36
+ /**
37
+ * @ignore
38
+ */
39
+ const caches: Map<Type<any>, Map<string, any>> = new Map();
40
+
41
+ /**
42
+ * @ignore
43
+ */
44
+ function get(model: Type<any>, options: any, cb: any) {
45
+ if (!caches.has(model)) {
46
+ caches.set(model, new Map());
47
+ }
48
+
49
+ const cache = caches.get(model)!;
50
+ const key = JSON.stringify(options);
51
+
52
+ if (!cache.has(key)) {
53
+ cache.set(key, cb());
54
+ }
55
+
56
+ return cache.get(key);
57
+ }
58
+
59
+ function generate(model: Type<any>, options: SpecSerializerOptions) {
60
+ const store = getJsonEntityStore(model);
61
+ const {rootPath = "/"} = options;
62
+
63
+ options = {
64
+ ...options,
65
+ rootPath,
66
+ defaultTags: [
67
+ cleanObject({
68
+ name: store.schema.getName(),
69
+ description: store.schema.get("description")
70
+ })
71
+ ]
72
+ };
73
+
74
+ return execMapper("generate", [store], options);
75
+ }
76
+
77
+ /**
78
+ * Return the swagger or open spec for the given class.
79
+ * @param model
80
+ * @param options
81
+ */
82
+ export function getSpec(model: Type<any> | JsonTokenOptions, options: SpecSerializerOptions = {}): Partial<OpenSpec3> {
83
+ options = {
84
+ specType: SpecTypes.OPENAPI,
85
+ ...options,
86
+ tags: [],
87
+ paths: {},
88
+ channels: {},
89
+ components: {},
90
+ operationIdFormatter: options.operationIdFormatter || operationIdFormatter(options.operationIdPattern),
91
+ root: false
92
+ };
93
+
94
+ if (isArray(model)) {
95
+ let finalSpec: any = {};
96
+
97
+ options = {
98
+ ...options,
99
+ append(spec: any) {
100
+ finalSpec = mergeSpec(finalSpec, spec);
101
+ }
102
+ };
103
+
104
+ model.forEach(({token, ...opts}) => {
105
+ const spec = getSpec(token, {
106
+ ...options,
107
+ ...opts
108
+ });
109
+
110
+ options.append(spec);
111
+ });
112
+
113
+ return finalSpec;
114
+ }
115
+
116
+ return get(model, options, () => generate(model, options));
117
+ }
@@ -0,0 +1,16 @@
1
+ import {SpecTypes} from "../domain/SpecTypes.js";
2
+ import {getSpecType, getSpecTypeFromSpec} from "./getSpecType.js";
3
+
4
+ describe("getSpecType", () => {
5
+ it("should return spect type from version", () => {
6
+ expect(getSpecType("2.0")).toEqual(SpecTypes.SWAGGER);
7
+ expect(getSpecType("3.0.1")).toEqual(SpecTypes.OPENAPI);
8
+ });
9
+ });
10
+
11
+ describe("getSpecTypeFromSpec", () => {
12
+ it("should return spect type from version", () => {
13
+ expect(getSpecTypeFromSpec({swagger: "2.0"})).toEqual(SpecTypes.SWAGGER);
14
+ expect(getSpecTypeFromSpec({openapi: "3.0.1"})).toEqual(SpecTypes.OPENAPI);
15
+ });
16
+ });
@@ -0,0 +1,19 @@
1
+ import {OpenSpec2, OpenSpec3} from "@tsed/openspec";
2
+
3
+ import {SpecTypes} from "../domain/SpecTypes.js";
4
+
5
+ /**
6
+ * @ignore
7
+ * @param version
8
+ */
9
+ export function getSpecType(version: string) {
10
+ return version.startsWith("3.") ? SpecTypes.OPENAPI : SpecTypes.SWAGGER;
11
+ }
12
+
13
+ /**
14
+ * @ignore
15
+ * @param spec
16
+ */
17
+ export function getSpecTypeFromSpec(spec: Partial<OpenSpec2 | OpenSpec3>) {
18
+ return "openapi" in spec ? SpecTypes.OPENAPI : SpecTypes.SWAGGER;
19
+ }
@@ -0,0 +1,22 @@
1
+ import {isRedirectionStatus, isSuccessStatus} from "./isSuccessStatus.js";
2
+
3
+ describe("isSuccessStatus", () => {
4
+ it("should be a valid success status", () => {
5
+ expect(isSuccessStatus(200)).toBe(true);
6
+ expect(isSuccessStatus("200")).toBe(true);
7
+ });
8
+
9
+ it("should not be a valid success status", () => {
10
+ expect(isSuccessStatus("300")).toBe(false);
11
+ expect(isSuccessStatus(101)).toBe(false);
12
+ });
13
+ });
14
+
15
+ describe("isRedirectionStatus", () => {
16
+ it("should return the expected value for", () => {
17
+ expect(isRedirectionStatus("300")).toBe(true);
18
+ expect(isRedirectionStatus(301)).toBe(true);
19
+ expect(isRedirectionStatus(200)).toBe(false);
20
+ expect(isRedirectionStatus(400)).toBe(false);
21
+ });
22
+ });
@@ -0,0 +1,2 @@
1
+ export const isSuccessStatus = (code: string | number | undefined) => Number(code) && 200 <= Number(code) && Number(code) < 300;
2
+ export const isRedirectionStatus = (code: string | number | undefined) => Number(code) && 300 <= Number(code) && Number(code) < 400;
@@ -0,0 +1,57 @@
1
+ import {mapHeaders} from "./mapHeaders.js";
2
+
3
+ describe("mapHeaders", () => {
4
+ it("should map headers", () => {
5
+ expect(
6
+ mapHeaders({
7
+ key: "value"
8
+ })
9
+ ).toEqual({
10
+ key: {
11
+ example: "value",
12
+ type: "string"
13
+ }
14
+ });
15
+
16
+ expect(
17
+ mapHeaders({
18
+ key: 1
19
+ })
20
+ ).toEqual({
21
+ key: {
22
+ example: 1,
23
+ type: "number"
24
+ }
25
+ });
26
+
27
+ expect(
28
+ mapHeaders({
29
+ key: {
30
+ description: "test",
31
+ value: 1
32
+ }
33
+ })
34
+ ).toEqual({
35
+ key: {
36
+ description: "test",
37
+ example: 1,
38
+ type: "number"
39
+ }
40
+ });
41
+
42
+ expect(
43
+ mapHeaders({
44
+ key: {
45
+ description: "test",
46
+ example: 1
47
+ }
48
+ } as any)
49
+ ).toEqual({
50
+ key: {
51
+ description: "test",
52
+ example: 1,
53
+ type: "number"
54
+ }
55
+ });
56
+ });
57
+ });
@@ -0,0 +1,33 @@
1
+ import {cleanObject} from "@tsed/core";
2
+
3
+ import {JsonHeader, JsonHeaders} from "../interfaces/JsonOpenSpec.js";
4
+
5
+ /**
6
+ * Map input header to a standard open spec header
7
+ * @param headers
8
+ * @ignore
9
+ */
10
+ export function mapHeaders(headers: JsonHeaders): {[key: string]: JsonHeader} {
11
+ return Object.keys(headers).reduce((newHeaders: {[key: string]: JsonHeader}, key: string) => {
12
+ const value: any = headers[key];
13
+
14
+ let type = typeof value;
15
+
16
+ let options: any = {
17
+ example: value
18
+ };
19
+
20
+ if (type === "object") {
21
+ options = value;
22
+ options.example = options.value === undefined ? options.example : options.value;
23
+ delete options.value;
24
+ type = typeof options.example;
25
+ }
26
+
27
+ options.type = options.type || type;
28
+
29
+ newHeaders[key] = cleanObject(options);
30
+
31
+ return newHeaders;
32
+ }, {});
33
+ }
@@ -0,0 +1,44 @@
1
+ import {getValue, setValue} from "@tsed/core";
2
+ import {OpenSpec2, OpenSpec3, OpenSpecVersions} from "@tsed/openspec";
3
+
4
+ import {SpecTypes} from "../domain/SpecTypes.js";
5
+ import {getSpecType} from "./getSpecType.js";
6
+ import {mapOpenSpec2} from "./mapOpenSpec2.js";
7
+ import {mapOpenSpec3} from "./mapOpenSpec3.js";
8
+ import {mergeSpec} from "./mergeSpec.js";
9
+
10
+ /**
11
+ * @ignore
12
+ * @param spec
13
+ * @param fileSpec
14
+ * @param version
15
+ * @param acceptMimes
16
+ * @param specVersion
17
+ */
18
+ export function mapOpenSpec(spec: any, {fileSpec, acceptMimes, specVersion, version}: any): Partial<OpenSpec2 | OpenSpec3> {
19
+ specVersion = specVersion || getValue<OpenSpecVersions>(spec, "openapi", getValue(spec, "swagger", "2.0"));
20
+
21
+ const options = {
22
+ specVersion,
23
+ acceptMimes
24
+ };
25
+
26
+ switch (getSpecType(specVersion)) {
27
+ case SpecTypes.OPENAPI:
28
+ spec = mapOpenSpec3(spec, options);
29
+ fileSpec = fileSpec ? mapOpenSpec3(fileSpec, options) : fileSpec;
30
+ break;
31
+ default:
32
+ case SpecTypes.SWAGGER:
33
+ spec = mapOpenSpec2(spec, options);
34
+ fileSpec = fileSpec ? mapOpenSpec2(fileSpec, options) : fileSpec;
35
+ break;
36
+ }
37
+
38
+ spec = mergeSpec(spec, fileSpec);
39
+
40
+ setValue(spec, "info.title", getValue(spec, "info.title", "Api documentation"));
41
+ setValue(spec, "info.version", getValue(spec, "info.version", version));
42
+
43
+ return spec;
44
+ }
@@ -0,0 +1,50 @@
1
+ import {getValue, uniq} from "@tsed/core";
2
+ import {OpenSpec2} from "@tsed/openspec";
3
+
4
+ import {mapOpenSpecInfo} from "./mapOpenSpecInfo.js";
5
+
6
+ /**
7
+ * @ignore
8
+ * @param spec
9
+ * @param version
10
+ * @param specVersion
11
+ * @param acceptMimes
12
+ */
13
+ export function mapOpenSpec2(
14
+ spec: Partial<OpenSpec2>,
15
+ {version, specVersion, acceptMimes = ["application/json"]}: any
16
+ ): Partial<OpenSpec2> {
17
+ const {
18
+ paths,
19
+ security,
20
+ tags,
21
+ host,
22
+ basePath,
23
+ schemes,
24
+ consumes = [],
25
+ produces = ["application/json"],
26
+ definitions,
27
+ parameters,
28
+ responses,
29
+ securityDefinitions,
30
+ externalDocs
31
+ } = spec;
32
+
33
+ return {
34
+ swagger: specVersion,
35
+ consumes: uniq([...acceptMimes, ...consumes]),
36
+ produces: uniq<string>([...produces]),
37
+ info: mapOpenSpecInfo(getValue(spec, "info", {})),
38
+ paths,
39
+ host,
40
+ basePath,
41
+ schemes,
42
+ definitions,
43
+ parameters,
44
+ responses,
45
+ security,
46
+ securityDefinitions,
47
+ tags,
48
+ externalDocs
49
+ };
50
+ }
@@ -0,0 +1,25 @@
1
+ import {getValue} from "@tsed/core";
2
+ import {OpenSpec3} from "@tsed/openspec";
3
+
4
+ import {mapOpenSpecInfo} from "./mapOpenSpecInfo.js";
5
+
6
+ /**
7
+ * @ignore
8
+ * @param spec
9
+ * @param version
10
+ * @param specVersion
11
+ */
12
+ export function mapOpenSpec3(spec: any, {specVersion}: any): Partial<OpenSpec3> {
13
+ const {servers, paths, components, security, tags, externalDocs} = spec;
14
+
15
+ return {
16
+ openapi: specVersion,
17
+ info: mapOpenSpecInfo(getValue(spec, "info", {})),
18
+ servers,
19
+ paths,
20
+ components,
21
+ security,
22
+ tags,
23
+ externalDocs
24
+ };
25
+ }
@@ -0,0 +1,19 @@
1
+ import {cleanObject} from "@tsed/core";
2
+ import {OpenSpecInfo} from "@tsed/openspec";
3
+
4
+ /**
5
+ * @ignore
6
+ * @param info
7
+ */
8
+ export function mapOpenSpecInfo(info: Partial<OpenSpecInfo>): OpenSpecInfo {
9
+ const {title, description, version, termsOfService, contact, license} = info;
10
+
11
+ return cleanObject({
12
+ version,
13
+ title,
14
+ description,
15
+ termsOfService,
16
+ contact,
17
+ license
18
+ });
19
+ }
@@ -0,0 +1,34 @@
1
+ import {OperationVerbs} from "../constants/OperationVerbs.js";
2
+
3
+ const ALLOWED_VERBS = new Set([
4
+ ...Object.keys(OperationVerbs)
5
+ .filter((v) => v !== OperationVerbs.CUSTOM)
6
+ .map((v) => v.toLowerCase())
7
+ ]);
8
+
9
+ export function mapOperationOptions(args: any[]) {
10
+ let method: string | undefined = undefined;
11
+ let path: string | RegExp | undefined = undefined;
12
+
13
+ const handlers = args.filter((arg: any) => {
14
+ if (typeof arg === "string" && ALLOWED_VERBS.has(arg.toLowerCase())) {
15
+ method = arg.toLocaleUpperCase();
16
+
17
+ return false;
18
+ }
19
+
20
+ if (typeof arg === "string" || arg instanceof RegExp) {
21
+ path = arg || "/";
22
+
23
+ return false;
24
+ }
25
+
26
+ return !!arg;
27
+ });
28
+
29
+ return {
30
+ path,
31
+ method,
32
+ use: handlers
33
+ };
34
+ }
@@ -0,0 +1,85 @@
1
+ import {matchGroups} from "./matchGroups.js";
2
+
3
+ describe("matchGroups", () => {
4
+ it.each([
5
+ [
6
+ {
7
+ groups: ["!creation"],
8
+ givenGroups: [],
9
+ expected: false
10
+ },
11
+ {
12
+ groups: ["group.summary"],
13
+ givenGroups: [],
14
+ expected: true
15
+ },
16
+ {
17
+ groups: ["group.extended"],
18
+ givenGroups: [],
19
+ expected: true
20
+ },
21
+ {
22
+ groups: ["!creation"],
23
+ givenGroups: [],
24
+ expected: false
25
+ },
26
+ {
27
+ groups: ["group.summary"],
28
+ givenGroups: [],
29
+ expected: true
30
+ },
31
+ {
32
+ groups: ["group.extended"],
33
+ givenGroups: [],
34
+ expected: true
35
+ },
36
+ {
37
+ groups: ["!creation"],
38
+ givenGroups: ["group.summary"],
39
+ expected: false
40
+ },
41
+ {
42
+ groups: ["group.summary"],
43
+ givenGroups: ["group.summary"],
44
+ expected: false
45
+ },
46
+ {
47
+ groups: ["group.extended"],
48
+ givenGroups: ["group.summary"],
49
+ expected: true
50
+ },
51
+ {
52
+ groups: ["!creation"],
53
+ givenGroups: ["creation"],
54
+ expected: true
55
+ },
56
+ {
57
+ groups: ["group.summary"],
58
+ givenGroups: ["creation"],
59
+ expected: true
60
+ },
61
+ {
62
+ groups: ["group.extended"],
63
+ givenGroups: ["creation"],
64
+ expected: true
65
+ },
66
+ {
67
+ groups: ["!creation"],
68
+ givenGroups: ["group.*"],
69
+ expected: false
70
+ },
71
+ {
72
+ groups: ["group.summary"],
73
+ givenGroups: ["group.*"],
74
+ expected: false
75
+ },
76
+ {
77
+ groups: ["group.extended"],
78
+ givenGroups: ["group.*"],
79
+ expected: false
80
+ }
81
+ ]
82
+ ])("should compare groups $groups with given groups $givenGroups and should return $expected", ({groups, givenGroups, expected}) => {
83
+ expect(matchGroups(groups, givenGroups)).toEqual(expected);
84
+ });
85
+ });
@@ -0,0 +1,64 @@
1
+ import picomatch from "picomatch";
2
+
3
+ function micromatch(list: string[], patterns: string[]) {
4
+ patterns = ([] as string[]).concat(patterns);
5
+ list = ([] as string[]).concat(list);
6
+
7
+ let omit = new Set();
8
+ let keep = new Set();
9
+ let items = new Set();
10
+ let negatives = 0;
11
+
12
+ let onResult = (state: any) => {
13
+ items.add(state.output);
14
+ };
15
+
16
+ for (let i = 0; i < patterns.length; i++) {
17
+ let isMatch: any = picomatch(String(patterns[i]), {onResult}, true);
18
+ let negated = isMatch.state.negated || isMatch.state.negatedExtglob;
19
+
20
+ if (negated) {
21
+ negatives++;
22
+ }
23
+
24
+ for (let item of list) {
25
+ let matched = isMatch(item, true);
26
+
27
+ let match = negated ? !matched.isMatch : matched.isMatch;
28
+ if (!match) continue;
29
+
30
+ if (negated) {
31
+ omit.add(matched.output);
32
+ } else {
33
+ omit.delete(matched.output);
34
+ keep.add(matched.output);
35
+ }
36
+ }
37
+ }
38
+
39
+ let result = negatives === patterns.length ? [...items] : [...keep];
40
+ return !!result.filter((item) => !omit.has(item)).length;
41
+ }
42
+
43
+ /**
44
+ * @ignore
45
+ */
46
+ export function matchGroups(groups: string[], compareWith: string[] = []) {
47
+ const groupsExcludes = groups.filter((group) => group.startsWith("!")).map((group) => group.replace("!", ""));
48
+ const groupsIncludes = groups.filter((group) => !group.startsWith("!"));
49
+
50
+ if (groupsExcludes.length) {
51
+ if (compareWith.length && micromatch(groupsExcludes, compareWith)) {
52
+ return true;
53
+ }
54
+ }
55
+
56
+ if (groupsIncludes.length) {
57
+ return !micromatch(
58
+ groups.filter((group) => !group.startsWith("!")),
59
+ compareWith
60
+ );
61
+ }
62
+
63
+ return false;
64
+ }