@alagoni97/cli 0.72.1
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/LICENSE +201 -0
- package/README.md +336 -0
- package/bin/dev.cmd +3 -0
- package/bin/dev.mjs +3 -0
- package/bin/run.cmd +3 -0
- package/bin/run.mjs +4 -0
- package/dist/LoggingInterface.d.ts +123 -0
- package/dist/LoggingInterface.js +300 -0
- package/dist/PersistedConfig.d.ts +46 -0
- package/dist/PersistedConfig.js +94 -0
- package/dist/browser/adapters/output.d.ts +69 -0
- package/dist/browser/adapters/output.js +91 -0
- package/dist/browser/config.d.ts +18 -0
- package/dist/browser/config.js +59 -0
- package/dist/browser/generate.d.ts +20 -0
- package/dist/browser/generate.js +129 -0
- package/dist/browser/index.d.ts +17 -0
- package/dist/browser/index.js +26 -0
- package/dist/browser/parser.d.ts +9 -0
- package/dist/browser/parser.js +61 -0
- package/dist/browser/shims/asyncapi-parser.d.ts +30 -0
- package/dist/browser/shims/asyncapi-parser.js +52 -0
- package/dist/browser/shims/fs.d.ts +190 -0
- package/dist/browser/shims/fs.js +188 -0
- package/dist/browser/shims/json-schema-ref-parser.d.ts +73 -0
- package/dist/browser/shims/json-schema-ref-parser.js +246 -0
- package/dist/browser/shims/swagger-parser.d.ts +20 -0
- package/dist/browser/shims/swagger-parser.js +40 -0
- package/dist/codegen/configurationSchemaBuilder.d.ts +15 -0
- package/dist/codegen/configurationSchemaBuilder.js +48 -0
- package/dist/codegen/configurations.d.ts +29 -0
- package/dist/codegen/configurations.js +260 -0
- package/dist/codegen/detection.d.ts +19 -0
- package/dist/codegen/detection.js +174 -0
- package/dist/codegen/errors.d.ts +111 -0
- package/dist/codegen/errors.js +352 -0
- package/dist/codegen/generators/generic/custom.d.ts +75 -0
- package/dist/codegen/generators/generic/custom.js +66 -0
- package/dist/codegen/generators/index.d.ts +14 -0
- package/dist/codegen/generators/index.js +42 -0
- package/dist/codegen/generators/typescript/channels/asyncapi.d.ts +10 -0
- package/dist/codegen/generators/typescript/channels/asyncapi.js +131 -0
- package/dist/codegen/generators/typescript/channels/index.d.ts +8 -0
- package/dist/codegen/generators/typescript/channels/index.js +142 -0
- package/dist/codegen/generators/typescript/channels/openapi.d.ts +9 -0
- package/dist/codegen/generators/typescript/channels/openapi.js +168 -0
- package/dist/codegen/generators/typescript/channels/protocols/amqp/index.d.ts +7 -0
- package/dist/codegen/generators/typescript/channels/protocols/amqp/index.js +149 -0
- package/dist/codegen/generators/typescript/channels/protocols/amqp/publishExchange.d.ts +5 -0
- package/dist/codegen/generators/typescript/channels/protocols/amqp/publishExchange.js +106 -0
- package/dist/codegen/generators/typescript/channels/protocols/amqp/publishQueue.d.ts +3 -0
- package/dist/codegen/generators/typescript/channels/protocols/amqp/publishQueue.js +102 -0
- package/dist/codegen/generators/typescript/channels/protocols/amqp/subscribeQueue.d.ts +3 -0
- package/dist/codegen/generators/typescript/channels/protocols/amqp/subscribeQueue.js +133 -0
- package/dist/codegen/generators/typescript/channels/protocols/eventsource/express.d.ts +3 -0
- package/dist/codegen/generators/typescript/channels/protocols/eventsource/express.js +100 -0
- package/dist/codegen/generators/typescript/channels/protocols/eventsource/fetch.d.ts +5 -0
- package/dist/codegen/generators/typescript/channels/protocols/eventsource/fetch.js +144 -0
- package/dist/codegen/generators/typescript/channels/protocols/eventsource/index.d.ts +6 -0
- package/dist/codegen/generators/typescript/channels/protocols/eventsource/index.js +132 -0
- package/dist/codegen/generators/typescript/channels/protocols/http/client.d.ts +10 -0
- package/dist/codegen/generators/typescript/channels/protocols/http/client.js +224 -0
- package/dist/codegen/generators/typescript/channels/protocols/http/common-types.d.ts +14 -0
- package/dist/codegen/generators/typescript/channels/protocols/http/common-types.js +800 -0
- package/dist/codegen/generators/typescript/channels/protocols/http/index.d.ts +8 -0
- package/dist/codegen/generators/typescript/channels/protocols/http/index.js +96 -0
- package/dist/codegen/generators/typescript/channels/protocols/http/security.d.ts +48 -0
- package/dist/codegen/generators/typescript/channels/protocols/http/security.js +491 -0
- package/dist/codegen/generators/typescript/channels/protocols/kafka/index.d.ts +6 -0
- package/dist/codegen/generators/typescript/channels/protocols/kafka/index.js +127 -0
- package/dist/codegen/generators/typescript/channels/protocols/kafka/publish.d.ts +3 -0
- package/dist/codegen/generators/typescript/channels/protocols/kafka/publish.js +108 -0
- package/dist/codegen/generators/typescript/channels/protocols/kafka/subscribe.d.ts +3 -0
- package/dist/codegen/generators/typescript/channels/protocols/kafka/subscribe.js +154 -0
- package/dist/codegen/generators/typescript/channels/protocols/kafka/utils.d.ts +11 -0
- package/dist/codegen/generators/typescript/channels/protocols/kafka/utils.js +58 -0
- package/dist/codegen/generators/typescript/channels/protocols/mqtt/index.d.ts +6 -0
- package/dist/codegen/generators/typescript/channels/protocols/mqtt/index.js +113 -0
- package/dist/codegen/generators/typescript/channels/protocols/mqtt/publish.d.ts +3 -0
- package/dist/codegen/generators/typescript/channels/protocols/mqtt/publish.js +99 -0
- package/dist/codegen/generators/typescript/channels/protocols/mqtt/subscribe.d.ts +3 -0
- package/dist/codegen/generators/typescript/channels/protocols/mqtt/subscribe.js +186 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/corePublish.d.ts +3 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/corePublish.js +90 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/coreReply.d.ts +3 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/coreReply.js +142 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/coreRequest.d.ts +3 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/coreRequest.js +100 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/coreSubscribe.d.ts +3 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/coreSubscribe.js +143 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/index.d.ts +11 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/index.js +224 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/jetstreamPublish.d.ts +3 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/jetstreamPublish.js +89 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/jetstreamPullSubscribe.d.ts +3 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/jetstreamPullSubscribe.js +146 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/jetstreamPushSubscription.d.ts +3 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/jetstreamPushSubscription.js +149 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/utils.d.ts +35 -0
- package/dist/codegen/generators/typescript/channels/protocols/nats/utils.js +120 -0
- package/dist/codegen/generators/typescript/channels/protocols/websocket/index.d.ts +7 -0
- package/dist/codegen/generators/typescript/channels/protocols/websocket/index.js +152 -0
- package/dist/codegen/generators/typescript/channels/protocols/websocket/publish.d.ts +3 -0
- package/dist/codegen/generators/typescript/channels/protocols/websocket/publish.js +64 -0
- package/dist/codegen/generators/typescript/channels/protocols/websocket/register.d.ts +3 -0
- package/dist/codegen/generators/typescript/channels/protocols/websocket/register.js +115 -0
- package/dist/codegen/generators/typescript/channels/protocols/websocket/subscribe.d.ts +3 -0
- package/dist/codegen/generators/typescript/channels/protocols/websocket/subscribe.js +151 -0
- package/dist/codegen/generators/typescript/channels/types.d.ts +181 -0
- package/dist/codegen/generators/typescript/channels/types.js +131 -0
- package/dist/codegen/generators/typescript/channels/utils.d.ts +64 -0
- package/dist/codegen/generators/typescript/channels/utils.js +163 -0
- package/dist/codegen/generators/typescript/client/index.d.ts +8 -0
- package/dist/codegen/generators/typescript/client/index.js +61 -0
- package/dist/codegen/generators/typescript/client/protocols/nats/corePublish.d.ts +6 -0
- package/dist/codegen/generators/typescript/client/protocols/nats/corePublish.js +53 -0
- package/dist/codegen/generators/typescript/client/protocols/nats/coreSubscribe.d.ts +6 -0
- package/dist/codegen/generators/typescript/client/protocols/nats/coreSubscribe.js +90 -0
- package/dist/codegen/generators/typescript/client/protocols/nats/jetStreamPullSubscription.d.ts +6 -0
- package/dist/codegen/generators/typescript/client/protocols/nats/jetStreamPullSubscription.js +83 -0
- package/dist/codegen/generators/typescript/client/protocols/nats/jetstreamPublish.d.ts +6 -0
- package/dist/codegen/generators/typescript/client/protocols/nats/jetstreamPublish.js +54 -0
- package/dist/codegen/generators/typescript/client/protocols/nats/jetstreamPushSubscription.d.ts +6 -0
- package/dist/codegen/generators/typescript/client/protocols/nats/jetstreamPushSubscription.js +82 -0
- package/dist/codegen/generators/typescript/client/protocols/nats.d.ts +2 -0
- package/dist/codegen/generators/typescript/client/protocols/nats.js +182 -0
- package/dist/codegen/generators/typescript/client/types.d.ts +49 -0
- package/dist/codegen/generators/typescript/client/types.js +37 -0
- package/dist/codegen/generators/typescript/headers.d.ts +54 -0
- package/dist/codegen/generators/typescript/headers.js +138 -0
- package/dist/codegen/generators/typescript/index.d.ts +7 -0
- package/dist/codegen/generators/typescript/index.js +38 -0
- package/dist/codegen/generators/typescript/models.d.ts +21761 -0
- package/dist/codegen/generators/typescript/models.js +64 -0
- package/dist/codegen/generators/typescript/parameters.d.ts +37 -0
- package/dist/codegen/generators/typescript/parameters.js +101 -0
- package/dist/codegen/generators/typescript/payloads.d.ts +73 -0
- package/dist/codegen/generators/typescript/payloads.js +250 -0
- package/dist/codegen/generators/typescript/types.d.ts +34 -0
- package/dist/codegen/generators/typescript/types.js +70 -0
- package/dist/codegen/generators/typescript/utils.d.ts +38 -0
- package/dist/codegen/generators/typescript/utils.js +195 -0
- package/dist/codegen/index.d.ts +7 -0
- package/dist/codegen/index.js +38 -0
- package/dist/codegen/inputs/asyncapi/generators/headers.d.ts +3 -0
- package/dist/codegen/inputs/asyncapi/generators/headers.js +41 -0
- package/dist/codegen/inputs/asyncapi/generators/parameters.d.ts +14 -0
- package/dist/codegen/inputs/asyncapi/generators/parameters.js +120 -0
- package/dist/codegen/inputs/asyncapi/generators/payloads.d.ts +16 -0
- package/dist/codegen/inputs/asyncapi/generators/payloads.js +124 -0
- package/dist/codegen/inputs/asyncapi/generators/types.d.ts +9 -0
- package/dist/codegen/inputs/asyncapi/generators/types.js +61 -0
- package/dist/codegen/inputs/asyncapi/index.d.ts +1 -0
- package/dist/codegen/inputs/asyncapi/index.js +7 -0
- package/dist/codegen/inputs/asyncapi/parser.d.ts +4 -0
- package/dist/codegen/inputs/asyncapi/parser.js +90 -0
- package/dist/codegen/inputs/index.d.ts +3 -0
- package/dist/codegen/inputs/index.js +19 -0
- package/dist/codegen/inputs/jsonschema/generators/index.d.ts +1 -0
- package/dist/codegen/inputs/jsonschema/generators/index.js +5 -0
- package/dist/codegen/inputs/jsonschema/generators/models.d.ts +7 -0
- package/dist/codegen/inputs/jsonschema/generators/models.js +26 -0
- package/dist/codegen/inputs/jsonschema/index.d.ts +1 -0
- package/dist/codegen/inputs/jsonschema/index.js +7 -0
- package/dist/codegen/inputs/jsonschema/parser.d.ts +31 -0
- package/dist/codegen/inputs/jsonschema/parser.js +128 -0
- package/dist/codegen/inputs/openapi/generators/headers.d.ts +3 -0
- package/dist/codegen/inputs/openapi/generators/headers.js +101 -0
- package/dist/codegen/inputs/openapi/generators/parameters.d.ts +10 -0
- package/dist/codegen/inputs/openapi/generators/parameters.js +852 -0
- package/dist/codegen/inputs/openapi/generators/payloads.d.ts +3 -0
- package/dist/codegen/inputs/openapi/generators/payloads.js +231 -0
- package/dist/codegen/inputs/openapi/generators/types.d.ts +9 -0
- package/dist/codegen/inputs/openapi/generators/types.js +86 -0
- package/dist/codegen/inputs/openapi/index.d.ts +2 -0
- package/dist/codegen/inputs/openapi/index.js +8 -0
- package/dist/codegen/inputs/openapi/parser.d.ts +4 -0
- package/dist/codegen/inputs/openapi/parser.js +115 -0
- package/dist/codegen/inputs/openapi/security.d.ts +56 -0
- package/dist/codegen/inputs/openapi/security.js +193 -0
- package/dist/codegen/modelina/index.d.ts +2 -0
- package/dist/codegen/modelina/index.js +20 -0
- package/dist/codegen/modelina/presets/index.d.ts +3 -0
- package/dist/codegen/modelina/presets/index.js +14 -0
- package/dist/codegen/modelina/presets/primitives.d.ts +34 -0
- package/dist/codegen/modelina/presets/primitives.js +211 -0
- package/dist/codegen/modelina/presets/union.d.ts +33 -0
- package/dist/codegen/modelina/presets/union.js +197 -0
- package/dist/codegen/modelina/presets/validation.d.ts +66 -0
- package/dist/codegen/modelina/presets/validation.js +183 -0
- package/dist/codegen/modelina/types.d.ts +61845 -0
- package/dist/codegen/modelina/types.js +412 -0
- package/dist/codegen/output/filesystem.d.ts +30 -0
- package/dist/codegen/output/filesystem.js +61 -0
- package/dist/codegen/output/index.d.ts +16 -0
- package/dist/codegen/output/index.js +57 -0
- package/dist/codegen/output/memory.d.ts +54 -0
- package/dist/codegen/output/memory.js +81 -0
- package/dist/codegen/output/modelina.d.ts +30 -0
- package/dist/codegen/output/modelina.js +30 -0
- package/dist/codegen/output/types.d.ts +47 -0
- package/dist/codegen/output/types.js +6 -0
- package/dist/codegen/renderer.d.ts +13 -0
- package/dist/codegen/renderer.js +309 -0
- package/dist/codegen/schemaPostProcess.d.ts +25 -0
- package/dist/codegen/schemaPostProcess.js +38 -0
- package/dist/codegen/types.d.ts +285187 -0
- package/dist/codegen/types.js +177 -0
- package/dist/codegen/utils.d.ts +126 -0
- package/dist/codegen/utils.js +373 -0
- package/dist/commands/base.d.ts +18 -0
- package/dist/commands/base.js +59 -0
- package/dist/commands/generate.d.ts +46 -0
- package/dist/commands/generate.js +305 -0
- package/dist/commands/init.d.ts +55 -0
- package/dist/commands/init.js +544 -0
- package/dist/commands/telemetry.d.ts +25 -0
- package/dist/commands/telemetry.js +115 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +35 -0
- package/dist/telemetry/anonymize.d.ts +19 -0
- package/dist/telemetry/anonymize.js +73 -0
- package/dist/telemetry/collector.d.ts +26 -0
- package/dist/telemetry/collector.js +57 -0
- package/dist/telemetry/config.d.ts +32 -0
- package/dist/telemetry/config.js +97 -0
- package/dist/telemetry/events.d.ts +72 -0
- package/dist/telemetry/events.js +5 -0
- package/dist/telemetry/index.d.ts +46 -0
- package/dist/telemetry/index.js +91 -0
- package/dist/telemetry/notice.d.ts +8 -0
- package/dist/telemetry/notice.js +50 -0
- package/dist/telemetry/sender.d.ts +14 -0
- package/dist/telemetry/sender.js +125 -0
- package/dist/utils/gitignore.d.ts +66 -0
- package/dist/utils/gitignore.js +111 -0
- package/dist/utils/inputSource.d.ts +3 -0
- package/dist/utils/inputSource.js +26 -0
- package/dist/utils/refResolvers.d.ts +29 -0
- package/dist/utils/refResolvers.js +83 -0
- package/dist/utils/remoteFetch.d.ts +18 -0
- package/dist/utils/remoteFetch.js +64 -0
- package/oclif.manifest.json +510 -0
- package/package.json +155 -0
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createUnionPreset = void 0;
|
|
4
|
+
const modelina_1 = require("@asyncapi/modelina");
|
|
5
|
+
const LoggingInterface_1 = require("../../../LoggingInterface");
|
|
6
|
+
const validation_1 = require("./validation");
|
|
7
|
+
/**
|
|
8
|
+
* Find the best possible discriminator value along side the properties using;
|
|
9
|
+
* - Enum value
|
|
10
|
+
* - Constant
|
|
11
|
+
*/
|
|
12
|
+
function findBestDiscriminatorOption(model, renderer) {
|
|
13
|
+
//find first const or enum value since no explicit discriminator property found
|
|
14
|
+
const firstFound = Object.values(model.properties)
|
|
15
|
+
.map((property) => {
|
|
16
|
+
const enumModel = property.property instanceof modelina_1.ConstrainedReferenceModel &&
|
|
17
|
+
property.property.ref instanceof modelina_1.ConstrainedEnumModel
|
|
18
|
+
? property.property.ref
|
|
19
|
+
: undefined;
|
|
20
|
+
const constValue = property.property.options.const
|
|
21
|
+
? property.property.options.const.value
|
|
22
|
+
: undefined;
|
|
23
|
+
return {
|
|
24
|
+
isEnumModel: enumModel !== undefined,
|
|
25
|
+
isConst: constValue !== undefined,
|
|
26
|
+
constValue,
|
|
27
|
+
enumModel,
|
|
28
|
+
property
|
|
29
|
+
};
|
|
30
|
+
})
|
|
31
|
+
.filter(({ isConst, isEnumModel }) => {
|
|
32
|
+
return isConst || isEnumModel;
|
|
33
|
+
});
|
|
34
|
+
if (firstFound.length > 1) {
|
|
35
|
+
const potentialProperties = firstFound
|
|
36
|
+
.map(({ property }) => {
|
|
37
|
+
return property.propertyName;
|
|
38
|
+
})
|
|
39
|
+
.join(', ');
|
|
40
|
+
LoggingInterface_1.Logger.warn(`More then one property could be discriminator for union model ${model.name}, found property ${potentialProperties}`);
|
|
41
|
+
}
|
|
42
|
+
if (firstFound.length >= 1) {
|
|
43
|
+
const firstIsBest = firstFound[0];
|
|
44
|
+
const discriminatorValue = firstIsBest.property.unconstrainedPropertyName;
|
|
45
|
+
if (firstIsBest.isEnumModel) {
|
|
46
|
+
const enumModel = firstIsBest.enumModel;
|
|
47
|
+
renderer.dependencyManager.addTypeScriptDependency(`{${enumModel.type}}`, `./${enumModel.type}`);
|
|
48
|
+
return {
|
|
49
|
+
objCheck: `if(json.${discriminatorValue} === ${enumModel.type}.${enumModel.values[0].key}) {
|
|
50
|
+
return ${model.name}.unmarshal(json);
|
|
51
|
+
}`
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
else if (firstIsBest.isConst) {
|
|
55
|
+
return {
|
|
56
|
+
objCheck: `if(json.${discriminatorValue} === ${firstIsBest.constValue}) {
|
|
57
|
+
return ${model.name}.unmarshal(json);
|
|
58
|
+
}`
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
LoggingInterface_1.Logger.warn(`Could not determine discriminator for ${model.name}, as part of ${model.name}, will not be able to serialize or deserialize messages with this payload`);
|
|
62
|
+
return {};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function findDiscriminatorChecks(model, renderer) {
|
|
66
|
+
if (model instanceof modelina_1.ConstrainedReferenceModel &&
|
|
67
|
+
model.ref instanceof modelina_1.ConstrainedObjectModel) {
|
|
68
|
+
const discriminatorValue = model.options.discriminator?.type;
|
|
69
|
+
if (!discriminatorValue) {
|
|
70
|
+
return findBestDiscriminatorOption(model.ref, renderer);
|
|
71
|
+
}
|
|
72
|
+
// Use discriminatorValue to figure out if we unmarshal it
|
|
73
|
+
return {
|
|
74
|
+
objCheck: `if(json.${model.options.discriminator?.discriminator} === ${discriminatorValue}}) {
|
|
75
|
+
return ${model.type}.unmarshal(json);
|
|
76
|
+
}`
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return {};
|
|
80
|
+
}
|
|
81
|
+
function renderUnionMarshal(model) {
|
|
82
|
+
const unmarshalChecks = model.union.map((unionModel) => {
|
|
83
|
+
if (unionModel instanceof modelina_1.ConstrainedReferenceModel &&
|
|
84
|
+
unionModel.ref instanceof modelina_1.ConstrainedObjectModel) {
|
|
85
|
+
return `if(payload instanceof ${unionModel.type}) {
|
|
86
|
+
return payload.marshal();
|
|
87
|
+
}`;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
return `export function marshal(payload: ${model.name}) {
|
|
91
|
+
${unmarshalChecks.join('\n')}
|
|
92
|
+
return JSON.stringify(payload);
|
|
93
|
+
}`;
|
|
94
|
+
}
|
|
95
|
+
function renderUnionUnmarshal(model, renderer) {
|
|
96
|
+
const discriminatorChecks = model.union.map((model) => {
|
|
97
|
+
return findDiscriminatorChecks(model, renderer);
|
|
98
|
+
});
|
|
99
|
+
const hasObjValues = discriminatorChecks.filter((value) => value?.objCheck).length >= 1;
|
|
100
|
+
return `export function unmarshal(json: any): ${model.name} {
|
|
101
|
+
${hasObjValues
|
|
102
|
+
? `if(typeof json === 'object') {
|
|
103
|
+
${discriminatorChecks
|
|
104
|
+
.filter((value) => value?.objCheck)
|
|
105
|
+
.map((value) => value?.objCheck)
|
|
106
|
+
.join('\n ')}
|
|
107
|
+
}`
|
|
108
|
+
: ''}
|
|
109
|
+
return JSON.parse(json);
|
|
110
|
+
}`;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Extract status code value from union member
|
|
114
|
+
*/
|
|
115
|
+
function extractStatusCodeValue(unionMember) {
|
|
116
|
+
if (!(unionMember instanceof modelina_1.ConstrainedReferenceModel &&
|
|
117
|
+
unionMember.ref instanceof modelina_1.ConstrainedObjectModel)) {
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
const memberOriginalInput = unionMember.ref.originalInput;
|
|
121
|
+
const statusCode = memberOriginalInput?.['x-modelina-status-codes'];
|
|
122
|
+
if (!statusCode) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
if (typeof statusCode === 'object' && statusCode.code !== undefined) {
|
|
126
|
+
return statusCode.code;
|
|
127
|
+
}
|
|
128
|
+
if (typeof statusCode === 'number') {
|
|
129
|
+
return statusCode;
|
|
130
|
+
}
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Generate status code check string for a union member
|
|
135
|
+
*/
|
|
136
|
+
function generateStatusCodeCheck(unionMember, codeValue) {
|
|
137
|
+
return ` if (statusCode === ${codeValue}) {
|
|
138
|
+
return ${unionMember.type}.unmarshal(json);
|
|
139
|
+
}`;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Render status code based unmarshal function for union models
|
|
143
|
+
*/
|
|
144
|
+
function renderUnionUnmarshalByStatusCode(model) {
|
|
145
|
+
if (!model.originalInput?.['x-modelina-has-status-codes']) {
|
|
146
|
+
return '';
|
|
147
|
+
}
|
|
148
|
+
const statusCodeChecks = model.union
|
|
149
|
+
.map((unionMember) => {
|
|
150
|
+
const codeValue = extractStatusCodeValue(unionMember);
|
|
151
|
+
return codeValue !== null
|
|
152
|
+
? generateStatusCodeCheck(unionMember, codeValue)
|
|
153
|
+
: null;
|
|
154
|
+
})
|
|
155
|
+
.filter((check) => check !== null);
|
|
156
|
+
if (statusCodeChecks.length === 0) {
|
|
157
|
+
return '';
|
|
158
|
+
}
|
|
159
|
+
return `
|
|
160
|
+
export function unmarshalByStatusCode(json: any, statusCode: number): ${model.name} {
|
|
161
|
+
${statusCodeChecks.join('\n')}
|
|
162
|
+
throw new Error(\`No matching type found for status code: \${statusCode}\`);
|
|
163
|
+
}`;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Creates a union preset that adds marshalling/unmarshalling methods to union types
|
|
167
|
+
*
|
|
168
|
+
* @param options Configuration for union generation
|
|
169
|
+
* @param context Generator context containing input type information
|
|
170
|
+
* @returns Modelina preset object with union marshalling functionality
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```typescript
|
|
174
|
+
* const preset = createUnionPreset({
|
|
175
|
+
* includeValidation: true
|
|
176
|
+
* }, context);
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
function createUnionPreset(options, context) {
|
|
180
|
+
return {
|
|
181
|
+
type: {
|
|
182
|
+
self({ model, content, renderer }) {
|
|
183
|
+
if (model instanceof modelina_1.ConstrainedUnionModel) {
|
|
184
|
+
return `${content}
|
|
185
|
+
|
|
186
|
+
${renderUnionUnmarshal(model, renderer)}
|
|
187
|
+
${renderUnionMarshal(model)}
|
|
188
|
+
${renderUnionUnmarshalByStatusCode(model)}
|
|
189
|
+
${options.includeValidation ? (0, validation_1.generateTypescriptValidationCode)({ model, renderer, asClassMethods: false, context: context }) : ''}
|
|
190
|
+
`;
|
|
191
|
+
}
|
|
192
|
+
return content;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
exports.createUnionPreset = createUnionPreset;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { ConstrainedMetaModel } from '@asyncapi/modelina';
|
|
2
|
+
import { TypeScriptRenderer } from '@asyncapi/modelina/lib/types/generators/typescript/TypeScriptRenderer';
|
|
3
|
+
import { TypescriptHeadersContext, TypeScriptPayloadContext } from '../../generators';
|
|
4
|
+
/**
|
|
5
|
+
* Safe stringify that removes x- properties and circular references by assuming true
|
|
6
|
+
*/
|
|
7
|
+
export declare function safeStringify(value: any): string;
|
|
8
|
+
export declare function generateTypescriptValidationCode({ model, renderer, asClassMethods, context }: {
|
|
9
|
+
model: ConstrainedMetaModel;
|
|
10
|
+
renderer: TypeScriptRenderer;
|
|
11
|
+
asClassMethods?: boolean;
|
|
12
|
+
context: TypeScriptPayloadContext | TypescriptHeadersContext;
|
|
13
|
+
}): string;
|
|
14
|
+
export interface BaseGeneratorContext {
|
|
15
|
+
inputType: 'asyncapi' | 'openapi';
|
|
16
|
+
generator: {
|
|
17
|
+
includeValidation?: boolean;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Configuration options for the validation preset
|
|
22
|
+
*/
|
|
23
|
+
export interface ValidationPresetOptions {
|
|
24
|
+
/** Whether to include validation methods in generated models */
|
|
25
|
+
includeValidation: boolean;
|
|
26
|
+
/** Whether to generate as class methods (true) or standalone functions (false) */
|
|
27
|
+
asClassMethods?: boolean;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Creates a validation preset that adds AJV validation methods to generated TypeScript models
|
|
31
|
+
*
|
|
32
|
+
* @param options Configuration for validation generation
|
|
33
|
+
* @param context Generator context containing input type information
|
|
34
|
+
* @returns Modelina preset object with validation functionality
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const preset = createValidationPreset({
|
|
39
|
+
* includeValidation: true,
|
|
40
|
+
* asClassMethods: true
|
|
41
|
+
* }, context);
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function createValidationPreset(options: ValidationPresetOptions, context: BaseGeneratorContext): {
|
|
45
|
+
class: {
|
|
46
|
+
additionalContent: ({ content, model, renderer }: {
|
|
47
|
+
content: string;
|
|
48
|
+
model: ConstrainedMetaModel;
|
|
49
|
+
renderer: TypeScriptRenderer;
|
|
50
|
+
}) => string;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Creates a validation preset for union types (generates standalone functions)
|
|
55
|
+
*
|
|
56
|
+
* @param options Configuration for validation generation
|
|
57
|
+
* @param context Generator context containing input type information
|
|
58
|
+
* @returns Modelina preset object with validation functionality for union types
|
|
59
|
+
*/
|
|
60
|
+
export declare function createUnionValidationPreset(options: ValidationPresetOptions, context: BaseGeneratorContext): {
|
|
61
|
+
type?: undefined;
|
|
62
|
+
} | {
|
|
63
|
+
type: {
|
|
64
|
+
additionalValidation: (model: ConstrainedMetaModel, renderer: TypeScriptRenderer) => string;
|
|
65
|
+
};
|
|
66
|
+
};
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createUnionValidationPreset = exports.createValidationPreset = exports.generateTypescriptValidationCode = exports.safeStringify = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Safe stringify that removes x- properties and circular references by assuming true
|
|
6
|
+
*/
|
|
7
|
+
function safeStringify(value) {
|
|
8
|
+
let depth = 0;
|
|
9
|
+
const maxDepth = 255;
|
|
10
|
+
const maxRepetitions = 5; // Allow up to 5 repetitions of the same object
|
|
11
|
+
let isRoot = true;
|
|
12
|
+
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
13
|
+
function stringify(val, currentPath = []) {
|
|
14
|
+
// Check depth limit
|
|
15
|
+
if (depth > maxDepth) {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
switch (typeof val) {
|
|
19
|
+
case 'function':
|
|
20
|
+
return true;
|
|
21
|
+
case 'boolean':
|
|
22
|
+
case 'number':
|
|
23
|
+
case 'string':
|
|
24
|
+
return val;
|
|
25
|
+
case 'object': {
|
|
26
|
+
if (val === null) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
// Check for immediate circular reference (direct self-reference)
|
|
30
|
+
if (currentPath.length > 0 &&
|
|
31
|
+
currentPath[currentPath.length - 1] === val) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
// Count how many times this object appears in the current path
|
|
35
|
+
const repetitionCount = currentPath.filter((obj) => obj === val).length;
|
|
36
|
+
// If we've seen this object too many times in the current path, cut it off
|
|
37
|
+
if (repetitionCount >= maxRepetitions) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
depth++;
|
|
41
|
+
const newPath = [...currentPath, val];
|
|
42
|
+
const atRoot = isRoot;
|
|
43
|
+
isRoot = false;
|
|
44
|
+
let result;
|
|
45
|
+
if (Array.isArray(val)) {
|
|
46
|
+
result = val.map((item) => stringify(item, newPath));
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
result = {};
|
|
50
|
+
// Check if this is a root-level schema with oneOf/anyOf that has conflicting type:object
|
|
51
|
+
// Modelina adds type:object to union models, but this conflicts with primitive oneOf/anyOf
|
|
52
|
+
const hasCompositionKeyword = val.oneOf !== undefined || val.anyOf !== undefined;
|
|
53
|
+
const hasConflictingObjectType = val.type === 'object' && hasCompositionKeyword;
|
|
54
|
+
for (const [key, value] of Object.entries(val)) {
|
|
55
|
+
// Skip extension properties
|
|
56
|
+
if (key.startsWith('x-modelina') ||
|
|
57
|
+
key.startsWith('x-the-codegen-project') ||
|
|
58
|
+
key.startsWith('x-parser-') ||
|
|
59
|
+
key.startsWith('x-modelgen-') ||
|
|
60
|
+
key.startsWith('discriminator')) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
// Skip type:object at root level when there's oneOf/anyOf with primitives
|
|
64
|
+
if (atRoot && key === 'type' && hasConflictingObjectType) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
// eslint-disable-next-line security/detect-object-injection
|
|
68
|
+
result[key] = stringify(value, newPath);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
depth--;
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
case 'undefined':
|
|
75
|
+
return undefined;
|
|
76
|
+
default:
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return JSON.stringify(stringify(value));
|
|
81
|
+
}
|
|
82
|
+
exports.safeStringify = safeStringify;
|
|
83
|
+
function generateTypescriptValidationCode({ model, renderer, asClassMethods = true, context }) {
|
|
84
|
+
renderer.dependencyManager.addTypeScriptDependency('{Ajv, Options as AjvOptions, ErrorObject, ValidateFunction}', 'ajv');
|
|
85
|
+
// Use named import with default alias for compatibility without esModuleInterop
|
|
86
|
+
// This works with both esModuleInterop: true and verbatimModuleSyntax: true
|
|
87
|
+
renderer.dependencyManager.addTypeScriptDependency('{default as addFormats}', 'ajv-formats');
|
|
88
|
+
const schemaProperty = asClassMethods
|
|
89
|
+
? 'public static theCodeGenSchema'
|
|
90
|
+
: 'export const theCodeGenSchema';
|
|
91
|
+
const methodPrefix = asClassMethods ? 'public static ' : 'export function ';
|
|
92
|
+
const createValidatorCall = asClassMethods
|
|
93
|
+
? 'this.createValidator(context)'
|
|
94
|
+
: 'createValidator(context)';
|
|
95
|
+
const compileCall = asClassMethods
|
|
96
|
+
? 'this.theCodeGenSchema'
|
|
97
|
+
: 'theCodeGenSchema';
|
|
98
|
+
const vocabularies = context.inputType === 'openapi'
|
|
99
|
+
? 'ajvInstance.addVocabulary(["xml", "example"])'
|
|
100
|
+
: '';
|
|
101
|
+
return `${schemaProperty} = ${safeStringify(model.originalInput)};
|
|
102
|
+
${methodPrefix}validate(context?: {data: any, ajvValidatorFunction?: ValidateFunction, ajvInstance?: Ajv, ajvOptions?: AjvOptions}): { valid: boolean; errors?: ErrorObject[]; } {
|
|
103
|
+
const {data, ajvValidatorFunction} = context ?? {};
|
|
104
|
+
// Intentionally parse JSON strings to support validation of marshalled output.
|
|
105
|
+
// Example: validate({data: marshal(obj)}) works because marshal returns JSON string.
|
|
106
|
+
// Note: String 'true' will be coerced to boolean true due to JSON.parse.
|
|
107
|
+
const parsedData = typeof data === 'string' ? JSON.parse(data) : data;
|
|
108
|
+
const validate = ajvValidatorFunction ?? ${createValidatorCall}
|
|
109
|
+
return {
|
|
110
|
+
valid: validate(parsedData),
|
|
111
|
+
errors: validate.errors ?? undefined,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
${methodPrefix}createValidator(context?: {ajvInstance?: Ajv, ajvOptions?: AjvOptions}): ValidateFunction {
|
|
115
|
+
const {ajvInstance} = {...context ?? {}, ajvInstance: new Ajv(context?.ajvOptions ?? {})};
|
|
116
|
+
addFormats(ajvInstance);
|
|
117
|
+
${vocabularies}
|
|
118
|
+
const validate = ajvInstance.compile(${compileCall});
|
|
119
|
+
return validate;
|
|
120
|
+
}
|
|
121
|
+
`;
|
|
122
|
+
}
|
|
123
|
+
exports.generateTypescriptValidationCode = generateTypescriptValidationCode;
|
|
124
|
+
/**
|
|
125
|
+
* Creates a validation preset that adds AJV validation methods to generated TypeScript models
|
|
126
|
+
*
|
|
127
|
+
* @param options Configuration for validation generation
|
|
128
|
+
* @param context Generator context containing input type information
|
|
129
|
+
* @returns Modelina preset object with validation functionality
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```typescript
|
|
133
|
+
* const preset = createValidationPreset({
|
|
134
|
+
* includeValidation: true,
|
|
135
|
+
* asClassMethods: true
|
|
136
|
+
* }, context);
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
function createValidationPreset(options, context) {
|
|
140
|
+
return {
|
|
141
|
+
class: {
|
|
142
|
+
additionalContent: ({ content, model, renderer }) => {
|
|
143
|
+
if (!options.includeValidation) {
|
|
144
|
+
return content;
|
|
145
|
+
}
|
|
146
|
+
return `${content}
|
|
147
|
+
${generateTypescriptValidationCode({
|
|
148
|
+
model,
|
|
149
|
+
renderer,
|
|
150
|
+
asClassMethods: options.asClassMethods ?? true,
|
|
151
|
+
context: context
|
|
152
|
+
})}`;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
exports.createValidationPreset = createValidationPreset;
|
|
158
|
+
/**
|
|
159
|
+
* Creates a validation preset for union types (generates standalone functions)
|
|
160
|
+
*
|
|
161
|
+
* @param options Configuration for validation generation
|
|
162
|
+
* @param context Generator context containing input type information
|
|
163
|
+
* @returns Modelina preset object with validation functionality for union types
|
|
164
|
+
*/
|
|
165
|
+
function createUnionValidationPreset(options, context) {
|
|
166
|
+
if (!options.includeValidation) {
|
|
167
|
+
return {};
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
type: {
|
|
171
|
+
// This will be used in the union preset to add validation functions
|
|
172
|
+
additionalValidation: (model, renderer) => {
|
|
173
|
+
return generateTypescriptValidationCode({
|
|
174
|
+
model,
|
|
175
|
+
renderer,
|
|
176
|
+
asClassMethods: false,
|
|
177
|
+
context: context
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
exports.createUnionValidationPreset = createUnionValidationPreset;
|