type-crafter 0.12.0 → 0.12.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/dist/index.js
CHANGED
|
@@ -21417,37 +21417,65 @@ function getPrimitiveType(typeName, typeInfo) {
|
|
|
21417
21417
|
return result;
|
|
21418
21418
|
}
|
|
21419
21419
|
async function generateAdditionalPropertiesType(typeName, typeInfo, parentTypes) {
|
|
21420
|
-
let result = null;
|
|
21421
21420
|
const stringKeyType = getPrimitiveType(typeName, {
|
|
21422
21421
|
...placeholderTypeInfo,
|
|
21423
21422
|
type: 'string'
|
|
21424
21423
|
}).templateInput.type;
|
|
21425
21424
|
if (typeof typeInfo.additionalProperties === 'boolean') {
|
|
21426
|
-
|
|
21427
|
-
|
|
21428
|
-
|
|
21429
|
-
|
|
21430
|
-
|
|
21431
|
-
|
|
21425
|
+
return {
|
|
21426
|
+
templateInput: {
|
|
21427
|
+
keyType: stringKeyType,
|
|
21428
|
+
valueType: getPrimitiveType(typeName, {
|
|
21429
|
+
...placeholderTypeInfo,
|
|
21430
|
+
type: 'unknown'
|
|
21431
|
+
}).templateInput.type,
|
|
21432
|
+
valueTypeReferenced: false,
|
|
21433
|
+
valuePrimitiveType: 'unknown',
|
|
21434
|
+
valueComposerType: null
|
|
21435
|
+
},
|
|
21436
|
+
primitives: new Set()
|
|
21432
21437
|
};
|
|
21433
21438
|
}
|
|
21434
21439
|
else if (valueIsKeyedAdditionalProperties(typeInfo.additionalProperties)) {
|
|
21435
|
-
|
|
21436
|
-
|
|
21437
|
-
|
|
21438
|
-
|
|
21439
|
-
|
|
21440
|
-
|
|
21440
|
+
const valueTypeInfo = typeInfo.additionalProperties.valueType;
|
|
21441
|
+
const generatedValueType = await generateType(typeName + 'ValueType', valueTypeInfo, parentTypes);
|
|
21442
|
+
const isReferenced = valueTypeInfo.$ref !== null;
|
|
21443
|
+
const isArray = valueTypeInfo.type === 'array';
|
|
21444
|
+
return {
|
|
21445
|
+
templateInput: {
|
|
21446
|
+
keyType: getPrimitiveType(typeName, {
|
|
21447
|
+
...placeholderTypeInfo,
|
|
21448
|
+
type: typeInfo.additionalProperties.keyType
|
|
21449
|
+
}).templateInput.type,
|
|
21450
|
+
valueType: generatedValueType.templateInput.type,
|
|
21451
|
+
valueTypeReferenced: isReferenced,
|
|
21452
|
+
valuePrimitiveType: isArray ? 'array' : (valueTypeInfo.type ?? 'object'),
|
|
21453
|
+
valueComposerType: 'composerType' in generatedValueType.templateInput
|
|
21454
|
+
? (generatedValueType.templateInput.composerType ?? null)
|
|
21455
|
+
: null
|
|
21456
|
+
},
|
|
21457
|
+
primitives: generatedValueType.primitives
|
|
21441
21458
|
};
|
|
21442
21459
|
}
|
|
21443
21460
|
else if (valueIsTypeInfo(typeInfo.additionalProperties)) {
|
|
21444
|
-
const
|
|
21445
|
-
|
|
21446
|
-
|
|
21447
|
-
|
|
21461
|
+
const valueTypeInfo = typeInfo.additionalProperties;
|
|
21462
|
+
const generatedValueType = await generateType(typeName + 'ValueType', valueTypeInfo, parentTypes);
|
|
21463
|
+
const isReferenced = valueTypeInfo.$ref !== null;
|
|
21464
|
+
const isArray = valueTypeInfo.type === 'array';
|
|
21465
|
+
return {
|
|
21466
|
+
templateInput: {
|
|
21467
|
+
keyType: stringKeyType,
|
|
21468
|
+
valueType: generatedValueType.templateInput.type,
|
|
21469
|
+
valueTypeReferenced: isReferenced,
|
|
21470
|
+
valuePrimitiveType: isArray ? 'array' : (valueTypeInfo.type ?? 'object'),
|
|
21471
|
+
valueComposerType: 'composerType' in generatedValueType.templateInput
|
|
21472
|
+
? (generatedValueType.templateInput.composerType ?? null)
|
|
21473
|
+
: null
|
|
21474
|
+
},
|
|
21475
|
+
primitives: generatedValueType.primitives
|
|
21448
21476
|
};
|
|
21449
21477
|
}
|
|
21450
|
-
return
|
|
21478
|
+
return null;
|
|
21451
21479
|
}
|
|
21452
21480
|
async function generateObjectType(typeName, typeInfo, parentTypes) {
|
|
21453
21481
|
const templateInput = {
|
|
@@ -21536,9 +21564,10 @@ async function generateObjectType(typeName, typeInfo, parentTypes) {
|
|
|
21536
21564
|
};
|
|
21537
21565
|
}
|
|
21538
21566
|
// Generating additional property types
|
|
21539
|
-
const
|
|
21540
|
-
if (
|
|
21541
|
-
templateInput.additionalProperties =
|
|
21567
|
+
const additionalPropertiesResult = await generateAdditionalPropertiesType(typeName + 'AdditionalProperty', typeInfo, parentTypes);
|
|
21568
|
+
if (additionalPropertiesResult !== null) {
|
|
21569
|
+
templateInput.additionalProperties = additionalPropertiesResult.templateInput;
|
|
21570
|
+
primitives.push(...additionalPropertiesResult.primitives);
|
|
21542
21571
|
}
|
|
21543
21572
|
const result = {
|
|
21544
21573
|
content: Runtime.getObjectTemplate()(templateInput) +
|
|
@@ -22195,7 +22224,7 @@ async function runner(language, inputFilePath, outputDirectory, _typesWriterMode
|
|
|
22195
22224
|
}
|
|
22196
22225
|
}
|
|
22197
22226
|
greeting();
|
|
22198
|
-
const program = new Command().version('0.12.
|
|
22227
|
+
const program = new Command().version('0.12.2');
|
|
22199
22228
|
program
|
|
22200
22229
|
.command('generate')
|
|
22201
22230
|
.description('Generate types for your language from a type spec file')
|
|
@@ -61,14 +61,67 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
|
|
|
61
61
|
}
|
|
62
62
|
{{/if}}
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
{{#if additionalProperties}}
|
|
65
|
+
{{#if (isEmptyObject properties)}}
|
|
66
|
+
const decodedAdditionalProperties: {{typeName}} = {};
|
|
67
|
+
for (const key in rawInput) {
|
|
68
|
+
{{#if additionalProperties.valueTypeReferenced}}
|
|
69
|
+
const decodedValue = {{#if (eq additionalProperties.valuePrimitiveType 'array')~}}
|
|
70
|
+
decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
|
|
71
|
+
{{~else~}}
|
|
72
|
+
decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
|
|
73
|
+
{{~/if}}
|
|
74
|
+
{{else}}
|
|
75
|
+
{{#if (eq additionalProperties.valuePrimitiveType 'array')}}
|
|
76
|
+
const decodedValue = decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
|
|
77
|
+
{{else}}
|
|
78
|
+
const decodedValue = decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
|
|
67
79
|
{{/if}}
|
|
80
|
+
{{/if}}
|
|
81
|
+
if (decodedValue === null) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
decodedAdditionalProperties[key] = decodedValue;
|
|
85
|
+
}
|
|
86
|
+
return decodedAdditionalProperties;
|
|
87
|
+
{{else}}
|
|
88
|
+
const decodedAdditionalProperties: Record<{{additionalProperties.keyType}}, {{additionalProperties.valueType}}> = {};
|
|
89
|
+
const knownKeys = new Set([{{#each properties}}{{{indexKey @key}}}{{#unless @last}}, {{/unless}}{{/each}}]);
|
|
90
|
+
for (const key in rawInput) {
|
|
91
|
+
if (!knownKeys.has(key)) {
|
|
92
|
+
{{#if additionalProperties.valueTypeReferenced}}
|
|
93
|
+
const decodedValue = {{#if (eq additionalProperties.valuePrimitiveType 'array')~}}
|
|
94
|
+
decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
|
|
95
|
+
{{~else~}}
|
|
96
|
+
decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
|
|
97
|
+
{{~/if}}
|
|
98
|
+
{{else}}
|
|
99
|
+
{{#if (eq additionalProperties.valuePrimitiveType 'array')}}
|
|
100
|
+
const decodedValue = decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
|
|
101
|
+
{{else}}
|
|
102
|
+
const decodedValue = decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
|
|
103
|
+
{{/if}}
|
|
104
|
+
{{/if}}
|
|
105
|
+
if (decodedValue === null) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
decodedAdditionalProperties[key] = decodedValue;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
...decodedAdditionalProperties,
|
|
68
113
|
{{#each properties}}
|
|
69
114
|
{{{jsonKey @key}}}: decoded{{{variableName @key}}}{{#unless @last}},{{/unless}}
|
|
70
115
|
{{/each}}
|
|
71
116
|
};
|
|
117
|
+
{{/if}}
|
|
118
|
+
{{else}}
|
|
119
|
+
return {
|
|
120
|
+
{{#each properties}}
|
|
121
|
+
{{{jsonKey @key}}}: decoded{{{variableName @key}}}{{#unless @last}},{{/unless}}
|
|
122
|
+
{{/each}}
|
|
123
|
+
};
|
|
124
|
+
{{/if}}
|
|
72
125
|
}
|
|
73
126
|
return null;
|
|
74
127
|
}
|
|
@@ -100,14 +153,67 @@ export function _decode{{typeName}}(rawInput: unknown): {{typeName}} | undefined
|
|
|
100
153
|
}
|
|
101
154
|
{{/if}}
|
|
102
155
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
156
|
+
{{#if additionalProperties}}
|
|
157
|
+
{{#if (isEmptyObject properties)}}
|
|
158
|
+
const decodedAdditionalProperties: {{typeName}} = {};
|
|
159
|
+
for (const key in rawInput) {
|
|
160
|
+
{{#if additionalProperties.valueTypeReferenced}}
|
|
161
|
+
const decodedValue = {{#if (eq additionalProperties.valuePrimitiveType 'array')~}}
|
|
162
|
+
decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
|
|
163
|
+
{{~else~}}
|
|
164
|
+
decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
|
|
165
|
+
{{~/if}}
|
|
166
|
+
{{else}}
|
|
167
|
+
{{#if (eq additionalProperties.valuePrimitiveType 'array')}}
|
|
168
|
+
const decodedValue = decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
|
|
169
|
+
{{else}}
|
|
170
|
+
const decodedValue = decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
|
|
106
171
|
{{/if}}
|
|
172
|
+
{{/if}}
|
|
173
|
+
if (decodedValue === null) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
decodedAdditionalProperties[key] = decodedValue;
|
|
177
|
+
}
|
|
178
|
+
return decodedAdditionalProperties;
|
|
179
|
+
{{else}}
|
|
180
|
+
const decodedAdditionalProperties: Record<{{additionalProperties.keyType}}, {{additionalProperties.valueType}}> = {};
|
|
181
|
+
const knownKeys = new Set([{{#each properties}}{{{indexKey @key}}}{{#unless @last}}, {{/unless}}{{/each}}]);
|
|
182
|
+
for (const key in rawInput) {
|
|
183
|
+
if (!knownKeys.has(key)) {
|
|
184
|
+
{{#if additionalProperties.valueTypeReferenced}}
|
|
185
|
+
const decodedValue = {{#if (eq additionalProperties.valuePrimitiveType 'array')~}}
|
|
186
|
+
decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
|
|
187
|
+
{{~else~}}
|
|
188
|
+
decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
|
|
189
|
+
{{~/if}}
|
|
190
|
+
{{else}}
|
|
191
|
+
{{#if (eq additionalProperties.valuePrimitiveType 'array')}}
|
|
192
|
+
const decodedValue = decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
|
|
193
|
+
{{else}}
|
|
194
|
+
const decodedValue = decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
|
|
195
|
+
{{/if}}
|
|
196
|
+
{{/if}}
|
|
197
|
+
if (decodedValue === null) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
decodedAdditionalProperties[key] = decodedValue;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return {
|
|
204
|
+
...decodedAdditionalProperties,
|
|
107
205
|
{{#each properties}}
|
|
108
206
|
{{{jsonKey @key}}}: decoded{{{variableName @key}}}{{#unless @last}},{{/unless}}
|
|
109
207
|
{{/each}}
|
|
110
208
|
};
|
|
209
|
+
{{/if}}
|
|
210
|
+
{{else}}
|
|
211
|
+
return {
|
|
212
|
+
{{#each properties}}
|
|
213
|
+
{{{jsonKey @key}}}: decoded{{{variableName @key}}}{{#unless @last}},{{/unless}}
|
|
214
|
+
{{/each}}
|
|
215
|
+
};
|
|
216
|
+
{{/if}}
|
|
111
217
|
}
|
|
112
218
|
return;
|
|
113
219
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -118,6 +118,13 @@ export type ObjectTemplateInputProperty = TypeDescriptors & {
|
|
|
118
118
|
export type AdditionalPropertiesTemplateInput = {
|
|
119
119
|
keyType: string;
|
|
120
120
|
valueType: string;
|
|
121
|
+
valueTypeReferenced: boolean;
|
|
122
|
+
valuePrimitiveType: string;
|
|
123
|
+
valueComposerType: string | null;
|
|
124
|
+
};
|
|
125
|
+
export type AdditionalPropertiesGenerationResult = {
|
|
126
|
+
templateInput: AdditionalPropertiesTemplateInput;
|
|
127
|
+
primitives: Set<string>;
|
|
121
128
|
};
|
|
122
129
|
export type ExporterModuleTemplateInput = {
|
|
123
130
|
modules: string[];
|