type-crafter 0.12.0 → 0.12.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/dist/index.js CHANGED
@@ -21428,23 +21428,43 @@ async function generateAdditionalPropertiesType(typeName, typeInfo, parentTypes)
21428
21428
  valueType: getPrimitiveType(typeName, {
21429
21429
  ...placeholderTypeInfo,
21430
21430
  type: 'unknown'
21431
- }).templateInput.type
21431
+ }).templateInput.type,
21432
+ valueTypeReferenced: false,
21433
+ valuePrimitiveType: 'unknown',
21434
+ valueComposerType: null
21432
21435
  };
21433
21436
  }
21434
21437
  else if (valueIsKeyedAdditionalProperties(typeInfo.additionalProperties)) {
21438
+ const valueTypeInfo = typeInfo.additionalProperties.valueType;
21439
+ const generatedValueType = await generateType(typeName + 'ValueType', valueTypeInfo, parentTypes);
21440
+ const isReferenced = valueTypeInfo.$ref !== null;
21441
+ const isArray = valueTypeInfo.type === 'array';
21435
21442
  result = {
21436
21443
  keyType: getPrimitiveType(typeName, {
21437
21444
  ...placeholderTypeInfo,
21438
21445
  type: typeInfo.additionalProperties.keyType
21439
21446
  }).templateInput.type,
21440
- valueType: (await generateType(typeName + 'ValueType', typeInfo.additionalProperties.valueType, parentTypes)).templateInput.type
21447
+ valueType: generatedValueType.templateInput.type,
21448
+ valueTypeReferenced: isReferenced,
21449
+ valuePrimitiveType: isArray ? 'array' : (valueTypeInfo.type ?? 'object'),
21450
+ valueComposerType: 'composerType' in generatedValueType.templateInput
21451
+ ? (generatedValueType.templateInput.composerType ?? null)
21452
+ : null
21441
21453
  };
21442
21454
  }
21443
21455
  else if (valueIsTypeInfo(typeInfo.additionalProperties)) {
21444
- const valueType = await generateType(typeName + 'ValueType', typeInfo.additionalProperties, parentTypes);
21456
+ const valueTypeInfo = typeInfo.additionalProperties;
21457
+ const generatedValueType = await generateType(typeName + 'ValueType', valueTypeInfo, parentTypes);
21458
+ const isReferenced = valueTypeInfo.$ref !== null;
21459
+ const isArray = valueTypeInfo.type === 'array';
21445
21460
  result = {
21446
21461
  keyType: stringKeyType,
21447
- valueType: valueType.templateInput.type
21462
+ valueType: generatedValueType.templateInput.type,
21463
+ valueTypeReferenced: isReferenced,
21464
+ valuePrimitiveType: isArray ? 'array' : (valueTypeInfo.type ?? 'object'),
21465
+ valueComposerType: 'composerType' in generatedValueType.templateInput
21466
+ ? (generatedValueType.templateInput.composerType ?? null)
21467
+ : null
21448
21468
  };
21449
21469
  }
21450
21470
  return result;
@@ -22195,7 +22215,7 @@ async function runner(language, inputFilePath, outputDirectory, _typesWriterMode
22195
22215
  }
22196
22216
  }
22197
22217
  greeting();
22198
- const program = new Command().version('0.12.0');
22218
+ const program = new Command().version('0.12.1');
22199
22219
  program
22200
22220
  .command('generate')
22201
22221
  .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
- return {
65
- {{#if additionalProperties}}
66
- ...rawInput,
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
- return {
104
- {{#if additionalProperties}}
105
- ...rawInput,
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
  }
@@ -118,6 +118,9 @@ 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;
121
124
  };
122
125
  export type ExporterModuleTemplateInput = {
123
126
  modules: string[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-crafter",
3
- "version": "0.12.0",
3
+ "version": "0.12.1",
4
4
  "description": "A tool to generate types from a yaml schema for any language",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",