@tsed/schema 8.0.1 → 8.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +11 -10
- package/readme.md +6 -6
- 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,105 @@
|
|
|
1
|
+
import {getJsonSchema, getSpec, OperationPath, Path, Returns, SpecTypes} from "../../index.js";
|
|
2
|
+
import {WriteOnly} from "./writeOnly.js";
|
|
3
|
+
|
|
4
|
+
describe("@WriteOnly", () => {
|
|
5
|
+
it("should declare writeOnly field (with boolean)", () => {
|
|
6
|
+
// WHEN
|
|
7
|
+
class Model {
|
|
8
|
+
@WriteOnly(true)
|
|
9
|
+
num: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// THEN
|
|
13
|
+
const schema = getJsonSchema(Model);
|
|
14
|
+
|
|
15
|
+
expect(schema).toEqual({
|
|
16
|
+
properties: {
|
|
17
|
+
num: {
|
|
18
|
+
writeOnly: true,
|
|
19
|
+
type: "string"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
type: "object"
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
it("should declare writeOnly field (default value)", () => {
|
|
26
|
+
// WHEN
|
|
27
|
+
class Model {
|
|
28
|
+
@WriteOnly()
|
|
29
|
+
num: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// THEN
|
|
33
|
+
const schema = getJsonSchema(Model);
|
|
34
|
+
|
|
35
|
+
expect(schema).toEqual({
|
|
36
|
+
properties: {
|
|
37
|
+
num: {
|
|
38
|
+
writeOnly: true,
|
|
39
|
+
type: "string"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
type: "object"
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should return the spec (OS3)", () => {
|
|
47
|
+
// WHEN
|
|
48
|
+
class Model {
|
|
49
|
+
@WriteOnly(true)
|
|
50
|
+
num: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@Path("/")
|
|
54
|
+
class MyController {
|
|
55
|
+
@OperationPath("GET", "/")
|
|
56
|
+
@Returns(200, Model)
|
|
57
|
+
get() {}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// THEN
|
|
61
|
+
const spec = getSpec(MyController, {specType: SpecTypes.OPENAPI});
|
|
62
|
+
|
|
63
|
+
expect(spec).toEqual({
|
|
64
|
+
components: {
|
|
65
|
+
schemas: {
|
|
66
|
+
Model: {
|
|
67
|
+
properties: {
|
|
68
|
+
num: {
|
|
69
|
+
type: "string",
|
|
70
|
+
writeOnly: true
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
type: "object"
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
paths: {
|
|
78
|
+
"/": {
|
|
79
|
+
get: {
|
|
80
|
+
operationId: "myControllerGet",
|
|
81
|
+
parameters: [],
|
|
82
|
+
responses: {
|
|
83
|
+
"200": {
|
|
84
|
+
content: {
|
|
85
|
+
"application/json": {
|
|
86
|
+
schema: {
|
|
87
|
+
$ref: "#/components/schemas/Model"
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
description: "Success"
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
tags: ["MyController"]
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
tags: [
|
|
99
|
+
{
|
|
100
|
+
name: "MyController"
|
|
101
|
+
}
|
|
102
|
+
]
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {JsonEntityStore} from "../../domain/JsonEntityStore.js";
|
|
2
|
+
import {JsonEntityFn} from "./jsonEntityFn.js";
|
|
3
|
+
/**
|
|
4
|
+
* Set the property as WriteOnly.
|
|
5
|
+
*
|
|
6
|
+
* @returns {Function}
|
|
7
|
+
* @decorator
|
|
8
|
+
* @validation
|
|
9
|
+
* @property
|
|
10
|
+
* @parameter
|
|
11
|
+
* @schema
|
|
12
|
+
*/
|
|
13
|
+
export function WriteOnly(writeOnly: boolean = true) {
|
|
14
|
+
return JsonEntityFn((store: JsonEntityStore) => {
|
|
15
|
+
store.itemSchema.writeOnly(writeOnly);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {DecoratorTypes, Type} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {JsonEntitiesContainer, JsonEntityStore} from "../../domain/JsonEntityStore.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Declare a new JsonEntityStore class for a specific decorator type.
|
|
7
|
+
*
|
|
8
|
+
* @ignore
|
|
9
|
+
* @param type
|
|
10
|
+
* @decorator
|
|
11
|
+
*/
|
|
12
|
+
export function JsonEntityComponent(type: DecoratorTypes) {
|
|
13
|
+
return (target: Type<JsonEntityStore>) => {
|
|
14
|
+
JsonEntitiesContainer.set(type, target);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import {CollectionOf, getJsonSchema, Property, string} from "../../index.js";
|
|
2
|
+
import {GenericOf} from "./genericOf.js";
|
|
3
|
+
import {Generics} from "./generics.js";
|
|
4
|
+
|
|
5
|
+
describe("@GenericOf", () => {
|
|
6
|
+
it("should generate Generic pagination with nested model (array)", () => {
|
|
7
|
+
@Generics("T")
|
|
8
|
+
class Paginated<T> {
|
|
9
|
+
@CollectionOf("T")
|
|
10
|
+
data: T[];
|
|
11
|
+
|
|
12
|
+
@Property()
|
|
13
|
+
totalCount: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
class Product {
|
|
17
|
+
@Property()
|
|
18
|
+
label: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
class Content {
|
|
22
|
+
@GenericOf(Product)
|
|
23
|
+
submissions: Paginated<Product>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
expect(getJsonSchema(Content)).toEqual({
|
|
27
|
+
definitions: {
|
|
28
|
+
Product: {
|
|
29
|
+
properties: {
|
|
30
|
+
label: {
|
|
31
|
+
type: "string"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
type: "object"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
properties: {
|
|
38
|
+
submissions: {
|
|
39
|
+
properties: {
|
|
40
|
+
data: {
|
|
41
|
+
items: {
|
|
42
|
+
$ref: "#/definitions/Product"
|
|
43
|
+
},
|
|
44
|
+
type: "array"
|
|
45
|
+
},
|
|
46
|
+
totalCount: {
|
|
47
|
+
type: "number"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
type: "object"
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
type: "object"
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
it("should generate Generic pagination with nested model", () => {
|
|
57
|
+
@Generics("T")
|
|
58
|
+
class Submission<T> {
|
|
59
|
+
@Property()
|
|
60
|
+
_id: string;
|
|
61
|
+
|
|
62
|
+
@Property("T")
|
|
63
|
+
data: T;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
class Product {
|
|
67
|
+
@Property()
|
|
68
|
+
label: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
class Content {
|
|
72
|
+
@GenericOf(Product)
|
|
73
|
+
submission: Submission<Product>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
expect(getJsonSchema(Content)).toEqual({
|
|
77
|
+
definitions: {
|
|
78
|
+
Product: {
|
|
79
|
+
properties: {
|
|
80
|
+
label: {
|
|
81
|
+
type: "string"
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
type: "object"
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
properties: {
|
|
88
|
+
submission: {
|
|
89
|
+
properties: {
|
|
90
|
+
_id: {
|
|
91
|
+
type: "string"
|
|
92
|
+
},
|
|
93
|
+
data: {
|
|
94
|
+
$ref: "#/definitions/Product"
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
type: "object"
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
type: "object"
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
it("should generate Generic pagination with nested model (deep generics)", () => {
|
|
104
|
+
@Generics("T")
|
|
105
|
+
class Paginated<T> {
|
|
106
|
+
@CollectionOf("T")
|
|
107
|
+
data: T[];
|
|
108
|
+
|
|
109
|
+
@Property()
|
|
110
|
+
totalCount: number;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
@Generics("S")
|
|
114
|
+
class Submission<S> {
|
|
115
|
+
@Property()
|
|
116
|
+
_id: string;
|
|
117
|
+
|
|
118
|
+
@Property("S")
|
|
119
|
+
data: S;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
class Product {
|
|
123
|
+
@Property()
|
|
124
|
+
label: string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
class Content {
|
|
128
|
+
@(GenericOf(Submission).Nested(Product))
|
|
129
|
+
submissions: Paginated<Submission<Product>>;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
expect(getJsonSchema(Content)).toEqual({
|
|
133
|
+
definitions: {
|
|
134
|
+
Product: {
|
|
135
|
+
properties: {
|
|
136
|
+
label: {
|
|
137
|
+
type: "string"
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
type: "object"
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
properties: {
|
|
144
|
+
submissions: {
|
|
145
|
+
properties: {
|
|
146
|
+
data: {
|
|
147
|
+
items: {
|
|
148
|
+
properties: {
|
|
149
|
+
_id: {
|
|
150
|
+
type: "string"
|
|
151
|
+
},
|
|
152
|
+
data: {
|
|
153
|
+
$ref: "#/definitions/Product"
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
type: "object"
|
|
157
|
+
},
|
|
158
|
+
type: "array"
|
|
159
|
+
},
|
|
160
|
+
totalCount: {
|
|
161
|
+
type: "number"
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
type: "object"
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
type: "object"
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
it("should generate Generic with an enum", () => {
|
|
171
|
+
@Generics("T")
|
|
172
|
+
class Submission<T> {
|
|
173
|
+
@Property()
|
|
174
|
+
_id: string;
|
|
175
|
+
|
|
176
|
+
@Property("T")
|
|
177
|
+
data: T;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
enum MyEnum {
|
|
181
|
+
READ = "read",
|
|
182
|
+
WRITE = "write"
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
class Content {
|
|
186
|
+
@GenericOf(MyEnum)
|
|
187
|
+
submission: Submission<MyEnum>;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
expect(getJsonSchema(Content)).toEqual({
|
|
191
|
+
properties: {
|
|
192
|
+
submission: {
|
|
193
|
+
properties: {
|
|
194
|
+
_id: {
|
|
195
|
+
type: "string"
|
|
196
|
+
},
|
|
197
|
+
data: {
|
|
198
|
+
type: "string",
|
|
199
|
+
enum: ["read", "write"]
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
type: "object"
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
type: "object"
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
it("should generate Generic with raw json schema", () => {
|
|
209
|
+
@Generics("T")
|
|
210
|
+
class Submission<T> {
|
|
211
|
+
@Property()
|
|
212
|
+
_id: string;
|
|
213
|
+
|
|
214
|
+
@Property("T")
|
|
215
|
+
data: T;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
enum MyEnum {
|
|
219
|
+
READ = "read",
|
|
220
|
+
WRITE = "write"
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
class Content {
|
|
224
|
+
@GenericOf(string().enum(["read", "write"]))
|
|
225
|
+
submission: Submission<MyEnum>;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
expect(getJsonSchema(Content)).toEqual({
|
|
229
|
+
properties: {
|
|
230
|
+
submission: {
|
|
231
|
+
properties: {
|
|
232
|
+
_id: {
|
|
233
|
+
type: "string"
|
|
234
|
+
},
|
|
235
|
+
data: {
|
|
236
|
+
type: "string",
|
|
237
|
+
enum: ["read", "write"]
|
|
238
|
+
}
|
|
239
|
+
},
|
|
240
|
+
type: "object"
|
|
241
|
+
}
|
|
242
|
+
},
|
|
243
|
+
type: "object"
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
});
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import {isObject, Type} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {JsonEntityStore} from "../../domain/JsonEntityStore.js";
|
|
4
|
+
import {string} from "../../utils/from.js";
|
|
5
|
+
import {GenericValue} from "../../utils/generics.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @ignore
|
|
9
|
+
*/
|
|
10
|
+
function isEnum(type: any) {
|
|
11
|
+
return isObject(type) && !("toJSON" in type);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface GenericOfChainedDecorators {
|
|
15
|
+
(...args: any): any;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Declare a nested generic models
|
|
19
|
+
* @param generics
|
|
20
|
+
*/
|
|
21
|
+
Nested(...generics: GenericValue[]): this;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Set the types of a Generic class.
|
|
26
|
+
*
|
|
27
|
+
* ## Example
|
|
28
|
+
*
|
|
29
|
+
* ```typescript
|
|
30
|
+
* class Product {
|
|
31
|
+
* @Property()
|
|
32
|
+
* label: string;
|
|
33
|
+
* }
|
|
34
|
+
*
|
|
35
|
+
* @Generics("T")
|
|
36
|
+
* class Paginated<T> {
|
|
37
|
+
* @CollectionOf("T")
|
|
38
|
+
* data: T[];
|
|
39
|
+
*
|
|
40
|
+
* @Property()
|
|
41
|
+
* totalCount: number;
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* class Payload {
|
|
45
|
+
* @GenericOf(Product)
|
|
46
|
+
* products: Paginated<Product>;
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* ## Example with nested generics
|
|
51
|
+
*
|
|
52
|
+
* ```typescript
|
|
53
|
+
* class Product {
|
|
54
|
+
* @Property()
|
|
55
|
+
* label: string;
|
|
56
|
+
* }
|
|
57
|
+
*
|
|
58
|
+
* @Generics("T")
|
|
59
|
+
* class Paginated<T> {
|
|
60
|
+
* @CollectionOf("T")
|
|
61
|
+
* data: T[];
|
|
62
|
+
*
|
|
63
|
+
* @Property()
|
|
64
|
+
* totalCount: number;
|
|
65
|
+
* }
|
|
66
|
+
*
|
|
67
|
+
* @Generics("D")
|
|
68
|
+
* class Submission<D> {
|
|
69
|
+
* @Property()
|
|
70
|
+
* _id: string;
|
|
71
|
+
*
|
|
72
|
+
* @Property("D")
|
|
73
|
+
* data: D;
|
|
74
|
+
* }
|
|
75
|
+
*
|
|
76
|
+
* class Payload {
|
|
77
|
+
* @GenericOf(Submissions).Nested(Product)
|
|
78
|
+
* submissions: Paginated<Submission<Product>>;
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* @param {Type<any>[]} generics
|
|
83
|
+
* @decorator
|
|
84
|
+
* @validation
|
|
85
|
+
* @swagger
|
|
86
|
+
* @schema
|
|
87
|
+
* @input
|
|
88
|
+
* @generics
|
|
89
|
+
*/
|
|
90
|
+
export function GenericOf(...generics: GenericValue[]): GenericOfChainedDecorators {
|
|
91
|
+
const nestedGenerics: GenericValue[][] = [
|
|
92
|
+
generics.map((type) => {
|
|
93
|
+
if (isEnum(type)) {
|
|
94
|
+
return string().enum(Object.values(type));
|
|
95
|
+
}
|
|
96
|
+
return type;
|
|
97
|
+
})
|
|
98
|
+
];
|
|
99
|
+
|
|
100
|
+
const decorator = (...args: any) => {
|
|
101
|
+
const store = JsonEntityStore.from(...args);
|
|
102
|
+
store.nestedGenerics = nestedGenerics;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
decorator.Nested = (...generics: any) => {
|
|
106
|
+
nestedGenerics.push(generics);
|
|
107
|
+
|
|
108
|
+
return decorator;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
return decorator;
|
|
112
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import {JsonEntityStore} from "../../domain/JsonEntityStore.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Define generics list. This list is used by @@GenericOf@@ and the @@getJsonSchema@@ function to build the correct JsonSchema.
|
|
5
|
+
*
|
|
6
|
+
* See @@GenericOf@@ decorator for more details.
|
|
7
|
+
*
|
|
8
|
+
* @param generics
|
|
9
|
+
* @decorator
|
|
10
|
+
* @validation
|
|
11
|
+
* @swagger
|
|
12
|
+
* @schema
|
|
13
|
+
* @input
|
|
14
|
+
* @generics
|
|
15
|
+
*/
|
|
16
|
+
export function Generics(...generics: string[]) {
|
|
17
|
+
return (target: any) => {
|
|
18
|
+
const storedSchema = JsonEntityStore.from(target);
|
|
19
|
+
|
|
20
|
+
storedSchema.schema.genericLabels = generics;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {Get, getSpec, JsonMethodStore, SpecTypes} from "../../index.js";
|
|
2
|
+
import {AcceptMime} from "./acceptMime.js";
|
|
3
|
+
|
|
4
|
+
describe("AcceptMime", () => {
|
|
5
|
+
it("should set metadata", () => {
|
|
6
|
+
class Test {
|
|
7
|
+
@Get("/")
|
|
8
|
+
@AcceptMime("application/json")
|
|
9
|
+
test() {}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const endpoint = JsonMethodStore.get(Test, "test");
|
|
13
|
+
expect(endpoint.acceptMimes).toEqual(["application/json"]);
|
|
14
|
+
|
|
15
|
+
const spec = getSpec(Test, {specType: SpecTypes.OPENAPI});
|
|
16
|
+
|
|
17
|
+
expect(spec).toEqual({
|
|
18
|
+
paths: {
|
|
19
|
+
"/": {
|
|
20
|
+
get: {
|
|
21
|
+
operationId: "testTest",
|
|
22
|
+
parameters: [],
|
|
23
|
+
responses: {
|
|
24
|
+
"200": {
|
|
25
|
+
description: "Success"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
tags: ["Test"]
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
tags: [
|
|
33
|
+
{
|
|
34
|
+
name: "Test"
|
|
35
|
+
}
|
|
36
|
+
]
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {StoreSet, useDecorators} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {Produces} from "./produces.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Set a mime list which are acceptable and checks if the specified content types are acceptable, based on the request’s Accept HTTP header field.
|
|
7
|
+
*
|
|
8
|
+
* ```typescript
|
|
9
|
+
* @Controller('/mypath')
|
|
10
|
+
* export class MyCtrl {
|
|
11
|
+
*
|
|
12
|
+
* @Get('/')
|
|
13
|
+
* @AcceptMime('application/json')
|
|
14
|
+
* public getResource(){}
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @param mimes
|
|
19
|
+
* @decorator
|
|
20
|
+
* @operation
|
|
21
|
+
* @response
|
|
22
|
+
*/
|
|
23
|
+
export function AcceptMime(...mimes: string[]): Function {
|
|
24
|
+
return useDecorators(Produces(...mimes), StoreSet("acceptMimes", mimes));
|
|
25
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import {Consumes, getSpec, OperationPath, SpecTypes} from "../../index.js";
|
|
2
|
+
|
|
3
|
+
describe("Consumes", () => {
|
|
4
|
+
it("should store metadata", () => {
|
|
5
|
+
class MyController {
|
|
6
|
+
@OperationPath("POST", "/")
|
|
7
|
+
@Consumes("text/json")
|
|
8
|
+
get() {}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
expect(getSpec(MyController, {specType: SpecTypes.OPENAPI})).toEqual({
|
|
12
|
+
tags: [
|
|
13
|
+
{
|
|
14
|
+
name: "MyController"
|
|
15
|
+
}
|
|
16
|
+
],
|
|
17
|
+
paths: {
|
|
18
|
+
"/": {
|
|
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 store metadata (class)", () => {
|
|
34
|
+
@Consumes("text/json")
|
|
35
|
+
class MyController {
|
|
36
|
+
@OperationPath("GET", "/")
|
|
37
|
+
get() {}
|
|
38
|
+
|
|
39
|
+
@OperationPath("POST", "/")
|
|
40
|
+
post() {}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
expect(getSpec(MyController, {specType: SpecTypes.OPENAPI})).toEqual({
|
|
44
|
+
paths: {
|
|
45
|
+
"/": {
|
|
46
|
+
get: {
|
|
47
|
+
operationId: "myControllerGet",
|
|
48
|
+
parameters: [],
|
|
49
|
+
responses: {
|
|
50
|
+
"200": {
|
|
51
|
+
description: "Success"
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
tags: ["MyController"]
|
|
55
|
+
},
|
|
56
|
+
post: {
|
|
57
|
+
operationId: "myControllerPost",
|
|
58
|
+
parameters: [],
|
|
59
|
+
responses: {
|
|
60
|
+
"200": {
|
|
61
|
+
description: "Success"
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
tags: ["MyController"]
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
tags: [
|
|
69
|
+
{
|
|
70
|
+
name: "MyController"
|
|
71
|
+
}
|
|
72
|
+
]
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
it("should throw error for unsupported usage", () => {
|
|
76
|
+
class Test {
|
|
77
|
+
test() {}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
let actualError: any;
|
|
81
|
+
try {
|
|
82
|
+
Consumes("text/json")(Test.prototype, "test", 0);
|
|
83
|
+
} catch (er) {
|
|
84
|
+
actualError = er;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
expect(actualError.message).toEqual("Consumes cannot be used as parameter decorator on Test.test.[0]");
|
|
88
|
+
});
|
|
89
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {decorateMethodsOf, DecoratorTypes, UnsupportedDecoratorType} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {JsonEntityFn} from "../common/jsonEntityFn.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Add consumes metadata on the decorated element.
|
|
7
|
+
*
|
|
8
|
+
* ## Examples
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* class Model {
|
|
12
|
+
* @Consumes("application/x-www-form-urlencoded")
|
|
13
|
+
* id: string;
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @param consumes
|
|
18
|
+
* @decorator
|
|
19
|
+
* @swagger
|
|
20
|
+
* @classDecorator
|
|
21
|
+
* @operation
|
|
22
|
+
*/
|
|
23
|
+
export function Consumes(...consumes: string[]) {
|
|
24
|
+
return JsonEntityFn((store, args) => {
|
|
25
|
+
switch (store.decoratorType) {
|
|
26
|
+
case DecoratorTypes.METHOD:
|
|
27
|
+
store.operation!.consumes(consumes);
|
|
28
|
+
break;
|
|
29
|
+
case DecoratorTypes.CLASS:
|
|
30
|
+
decorateMethodsOf(args[0], Consumes(...consumes));
|
|
31
|
+
break;
|
|
32
|
+
|
|
33
|
+
default:
|
|
34
|
+
throw new UnsupportedDecoratorType(Consumes, args);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|