@tsed/schema 8.0.1 → 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.
- package/package.json +11 -10
- package/src/components/async-api/channelsMapper.ts +81 -0
- package/src/components/async-api/generate.ts +24 -0
- package/src/components/async-api/messageMapper.ts +62 -0
- package/src/components/async-api/payloadMapper.ts +61 -0
- package/src/components/async-api/responseMapper.ts +70 -0
- package/src/components/default/anyMapper.spec.ts +7 -0
- package/src/components/default/anyMapper.ts +33 -0
- package/src/components/default/classMapper.ts +55 -0
- package/src/components/default/discriminatorMappingMapper.ts +36 -0
- package/src/components/default/enumsMapper.spec.ts +64 -0
- package/src/components/default/enumsMapper.ts +17 -0
- package/src/components/default/genericsMapper.ts +70 -0
- package/src/components/default/inheritedClassMapper.ts +24 -0
- package/src/components/default/itemMapper.ts +8 -0
- package/src/components/default/lazyRefMapper.ts +21 -0
- package/src/components/default/mapMapper.ts +25 -0
- package/src/components/default/nullableMapper.ts +55 -0
- package/src/components/default/objectMapper.ts +36 -0
- package/src/components/default/ofMapper.ts +16 -0
- package/src/components/default/propertiesMapper.ts +25 -0
- package/src/components/default/requiredMapper.ts +69 -0
- package/src/components/default/schemaMapper.ts +151 -0
- package/src/components/index.ts +36 -0
- package/src/components/open-spec/discriminatorMappingMapper.ts +7 -0
- package/src/components/open-spec/enumsMapper.ts +20 -0
- package/src/components/open-spec/generate.ts +29 -0
- package/src/components/open-spec/nullableMapper.ts +51 -0
- package/src/components/open-spec/operationInFilesMapper.ts +32 -0
- package/src/components/open-spec/operationInParameterMapper.ts +56 -0
- package/src/components/open-spec/operationInParametersMapper.ts +9 -0
- package/src/components/open-spec/operationInQueryMapper.ts +74 -0
- package/src/components/open-spec/operationMapper.ts +65 -0
- package/src/components/open-spec/operationMediaMapper.ts +11 -0
- package/src/components/open-spec/operationRequestBodyMapper.ts +82 -0
- package/src/components/open-spec/operationResponseMapper.ts +26 -0
- package/src/components/open-spec/pathsMapper.ts +132 -0
- package/src/constants/OperationVerbs.ts +29 -0
- package/src/constants/httpStatusMessages.ts +21 -0
- package/src/constants/jsonSchemaProperties.ts +17 -0
- package/src/decorators/class/children.ts +22 -0
- package/src/decorators/class/discriminatorValue.ts +53 -0
- package/src/decorators/collections/collectionContains.ts +25 -0
- package/src/decorators/collections/collectionOf.spec.ts +327 -0
- package/src/decorators/collections/collectionOf.ts +182 -0
- package/src/decorators/collections/maxItems.spec.ts +40 -0
- package/src/decorators/collections/maxItems.ts +59 -0
- package/src/decorators/collections/maxProperties.spec.ts +109 -0
- package/src/decorators/collections/maxProperties.ts +82 -0
- package/src/decorators/collections/minItems.spec.ts +40 -0
- package/src/decorators/collections/minItems.ts +63 -0
- package/src/decorators/collections/minProperties.spec.ts +109 -0
- package/src/decorators/collections/minProperties.ts +81 -0
- package/src/decorators/collections/uniqueItems.spec.ts +28 -0
- package/src/decorators/collections/uniqueItems.ts +59 -0
- package/src/decorators/common/__snapshots__/groups.spec.ts.snap +321 -0
- package/src/decorators/common/additionalProperties.spec.ts +133 -0
- package/src/decorators/common/additionalProperties.ts +17 -0
- package/src/decorators/common/allOf.spec.ts +276 -0
- package/src/decorators/common/allOf.ts +19 -0
- package/src/decorators/common/allow.spec.ts +130 -0
- package/src/decorators/common/allow.ts +50 -0
- package/src/decorators/common/any.spec.ts +109 -0
- package/src/decorators/common/any.ts +45 -0
- package/src/decorators/common/anyOf.spec.ts +270 -0
- package/src/decorators/common/anyOf.ts +23 -0
- package/src/decorators/common/const.spec.ts +98 -0
- package/src/decorators/common/const.ts +68 -0
- package/src/decorators/common/customKey.spec.ts +105 -0
- package/src/decorators/common/customKey.ts +39 -0
- package/src/decorators/common/default.spec.ts +25 -0
- package/src/decorators/common/default.ts +47 -0
- package/src/decorators/common/defaultMsg.spec.ts +22 -0
- package/src/decorators/common/defaultMsg.ts +38 -0
- package/src/decorators/common/description.spec.ts +166 -0
- package/src/decorators/common/description.ts +77 -0
- package/src/decorators/common/discriminatorKey.ts +14 -0
- package/src/decorators/common/enum.spec.ts +305 -0
- package/src/decorators/common/enum.ts +111 -0
- package/src/decorators/common/errorMsg.spec.ts +57 -0
- package/src/decorators/common/errorMsg.ts +14 -0
- package/src/decorators/common/example.spec.ts +93 -0
- package/src/decorators/common/example.ts +18 -0
- package/src/decorators/common/examples.spec.ts +101 -0
- package/src/decorators/common/examples.ts +20 -0
- package/src/decorators/common/exclusiveMaximum.spec.ts +47 -0
- package/src/decorators/common/exclusiveMaximum.ts +75 -0
- package/src/decorators/common/exclusiveMinimum.spec.ts +47 -0
- package/src/decorators/common/exclusiveMinimum.ts +75 -0
- package/src/decorators/common/format.spec.ts +149 -0
- package/src/decorators/common/format.ts +486 -0
- package/src/decorators/common/forwardGroups.spec.ts +270 -0
- package/src/decorators/common/forwardGroups.ts +16 -0
- package/src/decorators/common/groups.spec.ts +968 -0
- package/src/decorators/common/groups.ts +66 -0
- package/src/decorators/common/hidden.spec.ts +52 -0
- package/src/decorators/common/hidden.ts +36 -0
- package/src/decorators/common/ignore.spec.ts +88 -0
- package/src/decorators/common/ignore.ts +66 -0
- package/src/decorators/common/integer.spec.ts +110 -0
- package/src/decorators/common/integer.ts +69 -0
- package/src/decorators/common/jsonEntityFn.spec.ts +16 -0
- package/src/decorators/common/jsonEntityFn.ts +33 -0
- package/src/decorators/common/maxLength.spec.ts +61 -0
- package/src/decorators/common/maxLength.ts +88 -0
- package/src/decorators/common/maximum.spec.ts +73 -0
- package/src/decorators/common/maximum.ts +146 -0
- package/src/decorators/common/minLength.spec.ts +186 -0
- package/src/decorators/common/minLength.ts +89 -0
- package/src/decorators/common/minimum.spec.ts +74 -0
- package/src/decorators/common/minimum.ts +145 -0
- package/src/decorators/common/multipleOf.spec.ts +33 -0
- package/src/decorators/common/multipleOf.ts +80 -0
- package/src/decorators/common/name.spec.ts +56 -0
- package/src/decorators/common/name.ts +50 -0
- package/src/decorators/common/nullable.spec.ts +818 -0
- package/src/decorators/common/nullable.ts +32 -0
- package/src/decorators/common/oneOf.spec.ts +301 -0
- package/src/decorators/common/oneOf.ts +24 -0
- package/src/decorators/common/optional.spec.ts +43 -0
- package/src/decorators/common/optional.ts +26 -0
- package/src/decorators/common/pattern.spec.ts +27 -0
- package/src/decorators/common/pattern.ts +102 -0
- package/src/decorators/common/property.ts +24 -0
- package/src/decorators/common/readOnly.spec.ts +135 -0
- package/src/decorators/common/readOnly.ts +17 -0
- package/src/decorators/common/recordOf.spec.ts +41 -0
- package/src/decorators/common/recordOf.ts +59 -0
- package/src/decorators/common/required.spec.ts +188 -0
- package/src/decorators/common/required.ts +50 -0
- package/src/decorators/common/requiredGroups.spec.ts +127 -0
- package/src/decorators/common/requiredGroups.ts +23 -0
- package/src/decorators/common/schema.spec.ts +23 -0
- package/src/decorators/common/schema.ts +64 -0
- package/src/decorators/common/title.spec.ts +20 -0
- package/src/decorators/common/title.ts +45 -0
- package/src/decorators/common/typeError.spec.ts +22 -0
- package/src/decorators/common/typeError.ts +15 -0
- package/src/decorators/common/writeOnly.spec.ts +105 -0
- package/src/decorators/common/writeOnly.ts +17 -0
- package/src/decorators/config/jsonEntityComponent.ts +16 -0
- package/src/decorators/generics/genericOf.spec.ts +246 -0
- package/src/decorators/generics/genericOf.ts +112 -0
- package/src/decorators/generics/generics.ts +22 -0
- package/src/decorators/operations/acceptMime.spec.ts +39 -0
- package/src/decorators/operations/acceptMime.ts +25 -0
- package/src/decorators/operations/consumes.spec.ts +89 -0
- package/src/decorators/operations/consumes.ts +37 -0
- package/src/decorators/operations/contentType.spec.ts +40 -0
- package/src/decorators/operations/contentType.ts +24 -0
- package/src/decorators/operations/deprecated.spec.ts +223 -0
- package/src/decorators/operations/deprecated.ts +49 -0
- package/src/decorators/operations/header.spec.ts +214 -0
- package/src/decorators/operations/header.ts +62 -0
- package/src/decorators/operations/in.spec.ts +321 -0
- package/src/decorators/operations/in.ts +141 -0
- package/src/decorators/operations/inFile.spec.ts +209 -0
- package/src/decorators/operations/inFile.ts +55 -0
- package/src/decorators/operations/location.spec.ts +51 -0
- package/src/decorators/operations/location.ts +26 -0
- package/src/decorators/operations/operation.ts +119 -0
- package/src/decorators/operations/operationId.spec.ts +43 -0
- package/src/decorators/operations/operationId.ts +22 -0
- package/src/decorators/operations/operationPath.spec.ts +42 -0
- package/src/decorators/operations/operationPath.ts +33 -0
- package/src/decorators/operations/partial.spec.ts +249 -0
- package/src/decorators/operations/partial.ts +14 -0
- package/src/decorators/operations/path.spec.ts +47 -0
- package/src/decorators/operations/path.ts +27 -0
- package/src/decorators/operations/produces.spec.ts +89 -0
- package/src/decorators/operations/produces.ts +44 -0
- package/src/decorators/operations/publish.spec.ts +26 -0
- package/src/decorators/operations/publish.ts +6 -0
- package/src/decorators/operations/redirect.spec.ts +146 -0
- package/src/decorators/operations/redirect.ts +82 -0
- package/src/decorators/operations/returns.spec.ts +1393 -0
- package/src/decorators/operations/returns.ts +662 -0
- package/src/decorators/operations/route.spec.ts +211 -0
- package/src/decorators/operations/route.ts +154 -0
- package/src/decorators/operations/security.spec.ts +162 -0
- package/src/decorators/operations/security.ts +83 -0
- package/src/decorators/operations/status.spec.ts +582 -0
- package/src/decorators/operations/status.ts +130 -0
- package/src/decorators/operations/subscribe.spec.ts +32 -0
- package/src/decorators/operations/subscribe.ts +6 -0
- package/src/decorators/operations/summary.spec.ts +45 -0
- package/src/decorators/operations/summary.ts +32 -0
- package/src/decorators/operations/tags.spec.ts +113 -0
- package/src/decorators/operations/tags.ts +53 -0
- package/src/decorators/operations/view.spec.ts +16 -0
- package/src/decorators/operations/view.ts +27 -0
- package/src/domain/DecoratorContext.ts +116 -0
- package/src/domain/JsonAliasMap.ts +19 -0
- package/src/domain/JsonClassStore.ts +35 -0
- package/src/domain/JsonDiscriminator.spec.ts +65 -0
- package/src/domain/JsonDiscriminator.ts +52 -0
- package/src/domain/JsonEntityStore.spec.ts +277 -0
- package/src/domain/JsonEntityStore.ts +284 -0
- package/src/domain/JsonFormatTypes.ts +17 -0
- package/src/domain/JsonLazyRef.ts +21 -0
- package/src/domain/JsonMap.spec.ts +15 -0
- package/src/domain/JsonMap.ts +33 -0
- package/src/domain/JsonMedia.ts +24 -0
- package/src/domain/JsonMethodStore.spec.ts +301 -0
- package/src/domain/JsonMethodStore.ts +212 -0
- package/src/domain/JsonOperation.spec.ts +126 -0
- package/src/domain/JsonOperation.ts +219 -0
- package/src/domain/JsonOperationPathsMap.ts +16 -0
- package/src/domain/JsonOperationRoute.spec.ts +109 -0
- package/src/domain/JsonOperationRoute.ts +75 -0
- package/src/domain/JsonParameter.ts +70 -0
- package/src/domain/JsonParameterStore.spec.ts +212 -0
- package/src/domain/JsonParameterStore.ts +138 -0
- package/src/domain/JsonParameterTypes.spec.ts +12 -0
- package/src/domain/JsonParameterTypes.ts +20 -0
- package/src/domain/JsonPropertyStore.spec.ts +177 -0
- package/src/domain/JsonPropertyStore.ts +92 -0
- package/src/domain/JsonRequestBody.ts +47 -0
- package/src/domain/JsonResponse.spec.ts +33 -0
- package/src/domain/JsonResponse.ts +64 -0
- package/src/domain/JsonSchema.spec.ts +1470 -0
- package/src/domain/JsonSchema.ts +1049 -0
- package/src/domain/SpecTypes.ts +6 -0
- package/src/hooks/alterIgnore.ts +25 -0
- package/src/hooks/alterOneOf.ts +67 -0
- package/src/hooks/alterRequiredGroups.ts +12 -0
- package/src/index.ts +185 -0
- package/src/interfaces/IgnoreCallback.ts +5 -0
- package/src/interfaces/JsonHookContext.ts +18 -0
- package/src/interfaces/JsonOpenSpec.ts +7 -0
- package/src/interfaces/JsonSchemaOptions.ts +35 -0
- package/src/registries/JsonSchemaMapperContainer.ts +52 -0
- package/src/registries/enumRegistries.ts +3 -0
- package/src/utils/__snapshots__/generateSpec.spec.ts.snap +109 -0
- package/src/utils/buildPath.ts +8 -0
- package/src/utils/concatParameters.ts +12 -0
- package/src/utils/concatPath.spec.ts +23 -0
- package/src/utils/concatPath.ts +24 -0
- package/src/utils/defineStatusModel.ts +11 -0
- package/src/utils/from.spec.ts +243 -0
- package/src/utils/from.ts +281 -0
- package/src/utils/generateSpec.spec.ts +1262 -0
- package/src/utils/generateSpec.ts +54 -0
- package/src/utils/generics.ts +83 -0
- package/src/utils/getComputedType.spec.ts +45 -0
- package/src/utils/getComputedType.ts +39 -0
- package/src/utils/getInheritedStores.spec.ts +47 -0
- package/src/utils/getInheritedStores.ts +23 -0
- package/src/utils/getJsonEntityStore.ts +36 -0
- package/src/utils/getJsonPathParameters.spec.ts +126 -0
- package/src/utils/getJsonPathParameters.ts +97 -0
- package/src/utils/getJsonSchema.spec.ts +695 -0
- package/src/utils/getJsonSchema.ts +69 -0
- package/src/utils/getJsonType.spec.ts +28 -0
- package/src/utils/getJsonType.ts +46 -0
- package/src/utils/getOperationsRoutes.spec.ts +284 -0
- package/src/utils/getOperationsRoutes.ts +56 -0
- package/src/utils/getOperationsStore.spec.ts +47 -0
- package/src/utils/getOperationsStores.ts +37 -0
- package/src/utils/getPropertiesStores.spec.ts +138 -0
- package/src/utils/getPropertiesStores.ts +66 -0
- package/src/utils/getSpec.spec.ts +610 -0
- package/src/utils/getSpec.ts +117 -0
- package/src/utils/getSpecType.spec.ts +16 -0
- package/src/utils/getSpecType.ts +19 -0
- package/src/utils/isSuccessStatus.spec.ts +22 -0
- package/src/utils/isSuccessStatus.ts +2 -0
- package/src/utils/mapHeaders.spec.ts +57 -0
- package/src/utils/mapHeaders.ts +33 -0
- package/src/utils/mapOpenSpec.ts +44 -0
- package/src/utils/mapOpenSpec2.ts +50 -0
- package/src/utils/mapOpenSpec3.ts +25 -0
- package/src/utils/mapOpenSpecInfo.ts +19 -0
- package/src/utils/mapOperationOptions.ts +34 -0
- package/src/utils/matchGroups.spec.ts +85 -0
- package/src/utils/matchGroups.ts +64 -0
- package/src/utils/mergeSpec.spec.ts +533 -0
- package/src/utils/mergeSpec.ts +54 -0
- package/src/utils/operationIdFormatter.spec.ts +11 -0
- package/src/utils/operationIdFormatter.ts +49 -0
- package/src/utils/ref.ts +65 -0
- package/src/utils/removeHiddenOperation.ts +5 -0
- package/src/utils/serializeEnumValues.ts +27 -0
- package/src/utils/somethingOf.ts +11 -0
- package/src/utils/toJsonMapCollection.spec.ts +18 -0
- package/src/utils/toJsonMapCollection.ts +12 -0
- package/src/utils/toJsonRegex.ts +7 -0
- package/src/utils/transformToOS2.spec.ts +177 -0
- package/src/utils/transformToOS2.ts +607 -0
- package/src/utils/withErrorMsg.spec.ts +16 -0
- package/src/utils/withErrorMsg.ts +56 -0
|
@@ -0,0 +1,662 @@
|
|
|
1
|
+
import {
|
|
2
|
+
decorateMethodsOf,
|
|
3
|
+
decoratorTypeOf,
|
|
4
|
+
DecoratorTypes,
|
|
5
|
+
isArray,
|
|
6
|
+
isCollection,
|
|
7
|
+
isObject,
|
|
8
|
+
isPlainObject,
|
|
9
|
+
isPrimitiveOrPrimitiveClass,
|
|
10
|
+
isString,
|
|
11
|
+
Type
|
|
12
|
+
} from "@tsed/core";
|
|
13
|
+
import {OS3Example} from "@tsed/openspec";
|
|
14
|
+
|
|
15
|
+
import {getStatusMessage} from "../../constants/httpStatusMessages.js";
|
|
16
|
+
import {DecoratorContext} from "../../domain/DecoratorContext.js";
|
|
17
|
+
import {JsonEntityStore} from "../../domain/JsonEntityStore.js";
|
|
18
|
+
import {JsonParameter} from "../../domain/JsonParameter.js";
|
|
19
|
+
import {JsonResponse} from "../../domain/JsonResponse.js";
|
|
20
|
+
import {JsonSchema, JsonSchemaObject} from "../../domain/JsonSchema.js";
|
|
21
|
+
import {JsonHeader, JsonHeaders} from "../../interfaces/JsonOpenSpec.js";
|
|
22
|
+
import {getStatusModel} from "../../utils/defineStatusModel.js";
|
|
23
|
+
import {string} from "../../utils/from.js";
|
|
24
|
+
import {GenericValue} from "../../utils/generics.js";
|
|
25
|
+
import {isSuccessStatus} from "../../utils/isSuccessStatus.js";
|
|
26
|
+
import {mapHeaders} from "../../utils/mapHeaders.js";
|
|
27
|
+
|
|
28
|
+
export interface ReturnsChainedDecorators {
|
|
29
|
+
<T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T> | void;
|
|
30
|
+
|
|
31
|
+
(target: Function): void;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Set a Content-Type for the current response
|
|
35
|
+
* @param value
|
|
36
|
+
* @constructor
|
|
37
|
+
*/
|
|
38
|
+
ContentType(value: string): this;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Add a description
|
|
42
|
+
* @param description
|
|
43
|
+
*/
|
|
44
|
+
Description(description: string): this;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Add examples
|
|
48
|
+
* @param examples
|
|
49
|
+
*/
|
|
50
|
+
Examples(examples: Record<string, OS3Example>): this;
|
|
51
|
+
|
|
52
|
+
Examples(examples: Record<string, any>): this;
|
|
53
|
+
|
|
54
|
+
Examples(examples: any): this;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Change the model type
|
|
58
|
+
* @param type
|
|
59
|
+
*/
|
|
60
|
+
Type(type: Type<any> | any): this;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Change the status
|
|
64
|
+
* @param status
|
|
65
|
+
* @constructor
|
|
66
|
+
*/
|
|
67
|
+
Status(status: string | number): this;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Add the nested types
|
|
71
|
+
* @param types
|
|
72
|
+
*/
|
|
73
|
+
Of(...types: GenericValue[]): this;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* For the integer type
|
|
77
|
+
*/
|
|
78
|
+
OfInteger(): this;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Add the nested types
|
|
82
|
+
* @param types
|
|
83
|
+
*/
|
|
84
|
+
OneOf(...types: GenericValue[]): this;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Add the nested types
|
|
88
|
+
* @param types
|
|
89
|
+
*/
|
|
90
|
+
AllOf(...types: GenericValue[]): this;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Add the nested types
|
|
94
|
+
* @param types
|
|
95
|
+
*/
|
|
96
|
+
AnyOf(...types: GenericValue[]): this;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Declare a nested generic models
|
|
100
|
+
* @param generics
|
|
101
|
+
*/
|
|
102
|
+
Nested(...generics: GenericValue[]): this;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Add header.
|
|
106
|
+
* @param key
|
|
107
|
+
* @param value
|
|
108
|
+
*/
|
|
109
|
+
Header(key: string, value?: number | string | (JsonHeader & {value?: string | number | boolean})): this;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Add headers
|
|
113
|
+
*/
|
|
114
|
+
Headers(headers: JsonHeaders): this;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Add location hea
|
|
118
|
+
*/
|
|
119
|
+
Location(location: string, meta?: JsonHeader): this;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Assign partial schema
|
|
123
|
+
* @param schema
|
|
124
|
+
*/
|
|
125
|
+
Schema(schema: Partial<JsonSchemaObject> | JsonSchema): this;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Add an inline title for the return model.
|
|
129
|
+
* @param title
|
|
130
|
+
*/
|
|
131
|
+
Title(title: string): this;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Use group to filter model
|
|
135
|
+
* @param groups
|
|
136
|
+
*/
|
|
137
|
+
Groups(...groups: string[]): this;
|
|
138
|
+
|
|
139
|
+
Groups(groupName: string, groups: string[]): this;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Add a list of allowed groups to filter dynamically fields. Listed groups can be used by the consumer to change
|
|
143
|
+
* the mapped response.
|
|
144
|
+
* @param allowedGroups
|
|
145
|
+
*/
|
|
146
|
+
AllowedGroups(...allowedGroups: string[]): this;
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Apply the application/octet-stream and binary format on Open API documentation
|
|
150
|
+
*/
|
|
151
|
+
Binary(): this;
|
|
152
|
+
|
|
153
|
+
[key: string]: any;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @ignore
|
|
158
|
+
*/
|
|
159
|
+
function isEnum(type: any) {
|
|
160
|
+
return isObject(type) && !("toJSON" in type);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function mapGenerics(types: GenericValue[]) {
|
|
164
|
+
return types.map((type) => {
|
|
165
|
+
if (isEnum(type)) {
|
|
166
|
+
return string().enum(Object.values(type));
|
|
167
|
+
}
|
|
168
|
+
return type;
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* @ignore
|
|
174
|
+
*/
|
|
175
|
+
class ReturnDecoratorContext extends DecoratorContext<ReturnsChainedDecorators> {
|
|
176
|
+
readonly methods: string[] = [
|
|
177
|
+
"contentType",
|
|
178
|
+
"description",
|
|
179
|
+
"examples",
|
|
180
|
+
"type",
|
|
181
|
+
"status",
|
|
182
|
+
"of",
|
|
183
|
+
"ofInteger",
|
|
184
|
+
"oneOf",
|
|
185
|
+
"allOf",
|
|
186
|
+
"anyOf",
|
|
187
|
+
"nested",
|
|
188
|
+
"header",
|
|
189
|
+
"headers",
|
|
190
|
+
"schema",
|
|
191
|
+
"title",
|
|
192
|
+
"groups",
|
|
193
|
+
"allowedGroups",
|
|
194
|
+
"location",
|
|
195
|
+
"binary"
|
|
196
|
+
];
|
|
197
|
+
|
|
198
|
+
private hasOfTypes: boolean;
|
|
199
|
+
|
|
200
|
+
constructor({status, model}: any) {
|
|
201
|
+
super();
|
|
202
|
+
|
|
203
|
+
this.model(model);
|
|
204
|
+
this.status(status);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
type(model: any) {
|
|
208
|
+
return this.model(model);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
model(model: any) {
|
|
212
|
+
model && this.set("model", model);
|
|
213
|
+
return this;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
status(status: number | string) {
|
|
217
|
+
this.set("status", status);
|
|
218
|
+
|
|
219
|
+
if (status && getStatusMessage(status) && !this.get("description")) {
|
|
220
|
+
this.set("description", getStatusMessage(status));
|
|
221
|
+
|
|
222
|
+
if (!this.get("model")) {
|
|
223
|
+
this.model(getStatusModel(+status));
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return this;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
headers(headers: JsonHeaders) {
|
|
231
|
+
this.set("headers", {
|
|
232
|
+
...(this.get("headers") || {}),
|
|
233
|
+
...mapHeaders(headers)
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
header(key: string, value?: string | JsonHeader) {
|
|
240
|
+
if (value === undefined && isString(key)) {
|
|
241
|
+
return this.headers({[key]: {type: "string"}});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return this.headers({[key]: value!});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
location(path: string, meta: JsonHeaders = {}) {
|
|
248
|
+
this.headers({
|
|
249
|
+
Location: {
|
|
250
|
+
...meta,
|
|
251
|
+
value: path
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
return this;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
groups(groupName: string, groups: string[]): this;
|
|
259
|
+
groups(...groups: string[]): this;
|
|
260
|
+
groups(...groups: string[] | [string, string[]]) {
|
|
261
|
+
if (groups.length === 2 && isArray(groups[1])) {
|
|
262
|
+
this.set("groupsName", groups[0]);
|
|
263
|
+
this.set("groups", groups[1]);
|
|
264
|
+
} else {
|
|
265
|
+
this.set("groups", groups);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return this;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
allowedGroups(...allowedGroups: string[]) {
|
|
272
|
+
this.set("allowedGroups", new Set(allowedGroups));
|
|
273
|
+
return this;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
nested(...generics: GenericValue[]) {
|
|
277
|
+
const model = this.get("model");
|
|
278
|
+
this.checkPrimitive(model);
|
|
279
|
+
this.checkCollection(model);
|
|
280
|
+
|
|
281
|
+
this.addAction((ctx) => {
|
|
282
|
+
(this.get("schema") as JsonSchema).nestedGenerics.push(mapGenerics(generics));
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
return this;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
of(...types: (Type<any> | any)[]) {
|
|
289
|
+
const model = this.get("model");
|
|
290
|
+
this.checkPrimitive(model);
|
|
291
|
+
|
|
292
|
+
this.addAction(() => {
|
|
293
|
+
const schema = this.get("schema") as JsonSchema;
|
|
294
|
+
|
|
295
|
+
if (isCollection(model)) {
|
|
296
|
+
schema?.itemSchema({type: types[0]});
|
|
297
|
+
} else {
|
|
298
|
+
schema?.nestedGenerics.push(mapGenerics(types));
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
return this;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
ofInteger() {
|
|
306
|
+
return this.of("integer");
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
oneOf(...types: (Type<any> | any)[]) {
|
|
310
|
+
return this.manyOf("oneOf", types);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
allOf(...types: (Type<any> | any)[]) {
|
|
314
|
+
return this.manyOf("allOf", types);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
anyOf(...types: (Type<any> | any)[]) {
|
|
318
|
+
return this.manyOf("anyOf", types);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
schema(partial: Partial<JsonSchemaObject> | JsonSchema) {
|
|
322
|
+
this.addAction(() => {
|
|
323
|
+
const schema = this.get("schema") as JsonSchema;
|
|
324
|
+
|
|
325
|
+
schema!.assign(partial);
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
return this;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
examples(examples: any) {
|
|
332
|
+
this.set("examples", isString(examples) ? [examples] : examples);
|
|
333
|
+
return this;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
title(title: string) {
|
|
337
|
+
return this.schema({title});
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
binary() {
|
|
341
|
+
this.type(String);
|
|
342
|
+
this.set("contentType", "application/octet-stream");
|
|
343
|
+
this.schema({format: "binary"});
|
|
344
|
+
|
|
345
|
+
return this;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
protected checkPrimitive(model: any) {
|
|
349
|
+
if (isPrimitiveOrPrimitiveClass(model)) {
|
|
350
|
+
throw new Error("Returns.Of cannot be used with the following primitive classes: String, Number, Boolean");
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
protected checkCollection(model: any) {
|
|
355
|
+
if (isCollection(model)) {
|
|
356
|
+
throw new Error("Returns.Nested cannot be used with the following classes: Map, Set, Array, String, Number, Boolean");
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
protected getContentType() {
|
|
361
|
+
const model = this.get("model");
|
|
362
|
+
let contentType = this.get("contentType");
|
|
363
|
+
|
|
364
|
+
if ((model && !isPlainObject(model) && !isPrimitiveOrPrimitiveClass(model)) || this.hasOfTypes) {
|
|
365
|
+
contentType = contentType || "application/json";
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return contentType;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
protected getStatus() {
|
|
372
|
+
return this.get("status") || "default";
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
protected onInit(args: any[], decorator: any) {
|
|
376
|
+
const type = decoratorTypeOf(args);
|
|
377
|
+
switch (type) {
|
|
378
|
+
case DecoratorTypes.METHOD:
|
|
379
|
+
this.entity = JsonEntityStore.from(...args);
|
|
380
|
+
if (this.entity.operation) {
|
|
381
|
+
this.map();
|
|
382
|
+
}
|
|
383
|
+
break;
|
|
384
|
+
case DecoratorTypes.CLASS:
|
|
385
|
+
this.decoratorType = DecoratorTypes.CLASS;
|
|
386
|
+
decorateMethodsOf(args[0], decorator);
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
protected map() {
|
|
392
|
+
const model = this.get("model");
|
|
393
|
+
const {entity, decoratorType} = this;
|
|
394
|
+
const operation = this.entity.operation!;
|
|
395
|
+
const currentStatus = this.getStatus();
|
|
396
|
+
const response = operation.ensureResponseOf(currentStatus);
|
|
397
|
+
const contentType = this.getContentType();
|
|
398
|
+
|
|
399
|
+
let {description = response.get("description")} = this.toObject();
|
|
400
|
+
|
|
401
|
+
if (description) {
|
|
402
|
+
description = decoratorType === DecoratorTypes.CLASS ? response.get("description") || description : description;
|
|
403
|
+
response.description(description);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
const headers = this.getMergedKey("headers", response.get("headers"));
|
|
407
|
+
|
|
408
|
+
if (headers) {
|
|
409
|
+
response.headers(headers);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
this.mapMedia(response);
|
|
413
|
+
|
|
414
|
+
if (isSuccessStatus(this.get("status")) || currentStatus === "default") {
|
|
415
|
+
if (model) {
|
|
416
|
+
entity.type = model;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// additional info for OS2
|
|
421
|
+
contentType && operation.addProduce(contentType);
|
|
422
|
+
|
|
423
|
+
// run additional actions
|
|
424
|
+
return this.runActions();
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
protected mapMedia(response: JsonResponse) {
|
|
428
|
+
const contentType = this.getContentType();
|
|
429
|
+
const model = this.get("model");
|
|
430
|
+
const media = response.getMedia(contentType || "*/*");
|
|
431
|
+
const schema = media.get("schema") || new JsonSchema({type: model});
|
|
432
|
+
const groups = this.get("groups");
|
|
433
|
+
const groupsName = this.get("groupsName");
|
|
434
|
+
const allowedGroups = this.get("allowedGroups");
|
|
435
|
+
const operation = this.entity.operation!;
|
|
436
|
+
|
|
437
|
+
if (model) {
|
|
438
|
+
if (isArray(model)) {
|
|
439
|
+
schema.oneOf(model.map((type) => ({type})));
|
|
440
|
+
} else {
|
|
441
|
+
schema.type(model);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
this.set("schema", schema);
|
|
446
|
+
|
|
447
|
+
media.schema(schema);
|
|
448
|
+
|
|
449
|
+
media.groups = groups;
|
|
450
|
+
media.groupsName = groupsName;
|
|
451
|
+
|
|
452
|
+
if (allowedGroups) {
|
|
453
|
+
media.allowedGroups = allowedGroups;
|
|
454
|
+
|
|
455
|
+
const jsonParameter = new JsonParameter();
|
|
456
|
+
jsonParameter.in("query").name("includes");
|
|
457
|
+
jsonParameter.schema(
|
|
458
|
+
JsonSchema.from({
|
|
459
|
+
type: "array",
|
|
460
|
+
items: {
|
|
461
|
+
type: "string",
|
|
462
|
+
enum: [...allowedGroups]
|
|
463
|
+
}
|
|
464
|
+
})
|
|
465
|
+
);
|
|
466
|
+
|
|
467
|
+
operation.addParameter(-1, jsonParameter);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
const examples = this.get("examples");
|
|
471
|
+
|
|
472
|
+
if (examples) {
|
|
473
|
+
media.examples(examples);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
return media;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
private manyOf(kind: string, types: any[]) {
|
|
480
|
+
const model = this.get("model");
|
|
481
|
+
this.hasOfTypes = true;
|
|
482
|
+
|
|
483
|
+
this.addAction(() => {
|
|
484
|
+
const schema = this.get("schema") as JsonSchema;
|
|
485
|
+
|
|
486
|
+
if (isCollection(model)) {
|
|
487
|
+
schema.type(model || Object);
|
|
488
|
+
|
|
489
|
+
schema.itemSchema().set(kind, types);
|
|
490
|
+
} else {
|
|
491
|
+
schema.set(kind, types);
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
return this;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Add responses documentation for a specific status code.
|
|
501
|
+
*
|
|
502
|
+
* ## Usage
|
|
503
|
+
*
|
|
504
|
+
* Ts.ED v5/v6 API introducing the chaining decorator concept. Now a decorator like Returns can be used with another decorators like Description.
|
|
505
|
+
*
|
|
506
|
+
* ::: warning
|
|
507
|
+
* v5 has a basic support of the chaining decorator to facilitate the migration to v6!
|
|
508
|
+
* :::
|
|
509
|
+
*
|
|
510
|
+
* ```typescript
|
|
511
|
+
* import {Returns} from "@tsed/schema";
|
|
512
|
+
*
|
|
513
|
+
* @Controller("/")
|
|
514
|
+
* class MyController {
|
|
515
|
+
* @Returns(404, String).Description("Not Found")
|
|
516
|
+
* @Returns(200, Model).Description("Success")
|
|
517
|
+
* async myMethod(): Promise<Model> {}
|
|
518
|
+
* }
|
|
519
|
+
* ```
|
|
520
|
+
*
|
|
521
|
+
* ::: tip
|
|
522
|
+
* TypeScript and your IDE will discover automatically the chained decorators. But for more details you can look on @@ReturnsChainedDecorators@@ interface, to know
|
|
523
|
+
* what chained decorators are available under Returns decorator.
|
|
524
|
+
* :::
|
|
525
|
+
*
|
|
526
|
+
* This example will produce this documentation in swagger:
|
|
527
|
+
*
|
|
528
|
+
* ```json
|
|
529
|
+
* {
|
|
530
|
+
* "responses": {
|
|
531
|
+
* "404": {
|
|
532
|
+
* "description": "Description",
|
|
533
|
+
* "schema": {"type": "string"}
|
|
534
|
+
* },
|
|
535
|
+
* "2OO": {
|
|
536
|
+
* "description": "Description",
|
|
537
|
+
* "schema": {"$ref": "..."}
|
|
538
|
+
* }
|
|
539
|
+
* }
|
|
540
|
+
* }
|
|
541
|
+
* ```
|
|
542
|
+
*
|
|
543
|
+
* ## Declaring an Array
|
|
544
|
+
*
|
|
545
|
+
* Use chained decorators to declare an array with model as following:
|
|
546
|
+
*
|
|
547
|
+
* ```typescript
|
|
548
|
+
* import {Returns} from "@tsed/schema";
|
|
549
|
+
*
|
|
550
|
+
* @Controller("/models")
|
|
551
|
+
* class ModelCtrl {
|
|
552
|
+
* @Get("/")
|
|
553
|
+
* @Returns(200, Array).Of(Model).Description("Success")
|
|
554
|
+
* async myMethod(): Promise<Model> {
|
|
555
|
+
* }
|
|
556
|
+
* }
|
|
557
|
+
* ```
|
|
558
|
+
*
|
|
559
|
+
* Deprecated version:
|
|
560
|
+
*
|
|
561
|
+
* ```typescript
|
|
562
|
+
* import {ReturnsArray} from "@tsed/platform-http";
|
|
563
|
+
* import {Returns} from "@tsed/schema";
|
|
564
|
+
*
|
|
565
|
+
* @Controller("/")
|
|
566
|
+
* class MyController {
|
|
567
|
+
* @ReturnsArray(200, Model) // deprecated
|
|
568
|
+
* async myMethod(): Promise<Model> {}
|
|
569
|
+
* }
|
|
570
|
+
* ```
|
|
571
|
+
*
|
|
572
|
+
* ### Declaring a generic model <Badge text="6+"/>
|
|
573
|
+
*
|
|
574
|
+
* Sometime, it might be useful to use generic models. TypeScript doesn't store the generic type in the metadata. This is why we need to
|
|
575
|
+
* declare explicitly the generic models with the decorators.
|
|
576
|
+
*
|
|
577
|
+
* One of the generic's usage, can be a paginated list. With Returns decorator it's now possible to declare generic type and generate the appropriate OpenSpec documentation.
|
|
578
|
+
*
|
|
579
|
+
* Starting with the pagination model, by using @@Generics@@ and @@CollectionOf@@:
|
|
580
|
+
*
|
|
581
|
+
* ```typescript
|
|
582
|
+
* @Generics("T")
|
|
583
|
+
* class Pagination<T> {
|
|
584
|
+
* @CollectionOf("T")
|
|
585
|
+
* data: T[];
|
|
586
|
+
*
|
|
587
|
+
* @Property()
|
|
588
|
+
* totalCount: number;
|
|
589
|
+
* }
|
|
590
|
+
* ```
|
|
591
|
+
*
|
|
592
|
+
* Now, we need a model to be used with the generic Pagination model:
|
|
593
|
+
*
|
|
594
|
+
* ```typescript
|
|
595
|
+
* class Product {
|
|
596
|
+
* @Property()
|
|
597
|
+
* id: string;
|
|
598
|
+
*
|
|
599
|
+
* @Property()
|
|
600
|
+
* title: string;
|
|
601
|
+
* }
|
|
602
|
+
* ```
|
|
603
|
+
*
|
|
604
|
+
* Finally, we can use our models on a method as following:
|
|
605
|
+
*
|
|
606
|
+
* ```typescript
|
|
607
|
+
* class Controller {
|
|
608
|
+
* @OperationPath("POST", "/")
|
|
609
|
+
* @Returns(200, Pagination).Of(Product).Description("description")
|
|
610
|
+
* async method(): Promise<Pagination<Product> | null> {
|
|
611
|
+
* return null;
|
|
612
|
+
* }
|
|
613
|
+
* }
|
|
614
|
+
* ```
|
|
615
|
+
*
|
|
616
|
+
* ### Declaring a nested generics models <Badge text="6+"/>
|
|
617
|
+
*
|
|
618
|
+
* It's also possible to declare a nested generics models in order to have this type `Pagination<Submission<Product>>`:
|
|
619
|
+
*
|
|
620
|
+
* ```typescript
|
|
621
|
+
* import {Post, Generics, Property, Returns} from "@tsed/schema";
|
|
622
|
+
*
|
|
623
|
+
* class Controller {
|
|
624
|
+
* @Post("/")
|
|
625
|
+
* @Returns(200, Pagination).Of(Submission).Nested(Product).Description("description")
|
|
626
|
+
* async method(): Promise<Pagination<Submission<Product>> | null> {
|
|
627
|
+
* return null;
|
|
628
|
+
* }
|
|
629
|
+
* }
|
|
630
|
+
* ```
|
|
631
|
+
*
|
|
632
|
+
* And here is the Submission model:
|
|
633
|
+
*
|
|
634
|
+
* ```typescript
|
|
635
|
+
* import {Generics, Property} from "@tsed/schema";
|
|
636
|
+
*
|
|
637
|
+
* @Generics("T")
|
|
638
|
+
* class Submission<T> {
|
|
639
|
+
* @Property()
|
|
640
|
+
* _id: string;
|
|
641
|
+
* @Property("T")
|
|
642
|
+
* data: T;
|
|
643
|
+
* }
|
|
644
|
+
* ```
|
|
645
|
+
*
|
|
646
|
+
* @param status
|
|
647
|
+
* @param model
|
|
648
|
+
* @decorator
|
|
649
|
+
* @swagger
|
|
650
|
+
* @schema
|
|
651
|
+
* @response
|
|
652
|
+
* @operation
|
|
653
|
+
*/
|
|
654
|
+
export function Returns(status?: string | number, model?: Type<any> | Type<any>[]): ReturnsChainedDecorators;
|
|
655
|
+
export function Returns(status?: string | number, model?: Type<any> | Type<any>[] | any): ReturnsChainedDecorators {
|
|
656
|
+
const context = new ReturnDecoratorContext({
|
|
657
|
+
status,
|
|
658
|
+
model
|
|
659
|
+
});
|
|
660
|
+
|
|
661
|
+
return context.build();
|
|
662
|
+
}
|