type-crafter 0.14.0 → 0.16.0

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
@@ -21308,6 +21308,7 @@ function registerTemplateHelpers() {
21308
21308
  return value1 || value2;
21309
21309
  }
21310
21310
  });
21311
+ Handlebars.registerHelper('subtract', (a, b) => typeof a === 'number' && typeof b === 'number' ? a - b : 0);
21311
21312
  }
21312
21313
  function readNestedValue(json, keyPath) {
21313
21314
  if (!isJSON(json)) {
@@ -21552,7 +21553,10 @@ async function generateAdditionalPropertiesType(typeName, typeInfo, parentTypes)
21552
21553
  valuePrimitiveType: isArray ? 'array' : (valueTypeInfo.type ?? 'object'),
21553
21554
  valueComposerType: 'composerType' in generatedValueType.templateInput
21554
21555
  ? (generatedValueType.templateInput.composerType ?? null)
21555
- : null
21556
+ : null,
21557
+ valueArrayNestingDepth: 'arrayNestingDepth' in generatedValueType.templateInput
21558
+ ? (generatedValueType.templateInput.arrayNestingDepth ?? 0)
21559
+ : 0
21556
21560
  },
21557
21561
  primitives: generatedValueType.primitives
21558
21562
  };
@@ -21570,7 +21574,10 @@ async function generateAdditionalPropertiesType(typeName, typeInfo, parentTypes)
21570
21574
  valuePrimitiveType: isArray ? 'array' : (valueTypeInfo.type ?? 'object'),
21571
21575
  valueComposerType: 'composerType' in generatedValueType.templateInput
21572
21576
  ? (generatedValueType.templateInput.composerType ?? null)
21573
- : null
21577
+ : null,
21578
+ valueArrayNestingDepth: 'arrayNestingDepth' in generatedValueType.templateInput
21579
+ ? (generatedValueType.templateInput.arrayNestingDepth ?? 0)
21580
+ : 0
21574
21581
  },
21575
21582
  primitives: generatedValueType.primitives
21576
21583
  };
@@ -21606,6 +21613,7 @@ async function generateObjectType(typeName, typeInfo, parentTypes) {
21606
21613
  }
21607
21614
  const primitiveType = propertyType ?? 'object';
21608
21615
  let composerType = null;
21616
+ let arrayNestingDepth = 0;
21609
21617
  let recursivePropertyName;
21610
21618
  let languageDataType = null;
21611
21619
  let isReferenced = false;
@@ -21627,6 +21635,7 @@ async function generateObjectType(typeName, typeInfo, parentTypes) {
21627
21635
  references.push(...arrayDataGenOutput.references);
21628
21636
  languageDataType = arrayDataGenOutput.templateInput.type;
21629
21637
  composerType = arrayDataGenOutput.templateInput.composerType ?? null;
21638
+ arrayNestingDepth = arrayDataGenOutput.templateInput.arrayNestingDepth ?? 0;
21630
21639
  dynamicGeneratedType += arrayDataGenOutput.content;
21631
21640
  }
21632
21641
  else if (propertyType === 'object') {
@@ -21655,6 +21664,7 @@ async function generateObjectType(typeName, typeInfo, parentTypes) {
21655
21664
  referenced: isReferenced,
21656
21665
  primitiveType,
21657
21666
  composerType,
21667
+ arrayNestingDepth,
21658
21668
  example: propertyDetails.example,
21659
21669
  description: propertyDetails.description,
21660
21670
  summary: propertyDetails.summary,
@@ -21697,7 +21707,8 @@ function generateEnumType(typeName, typeInfo) {
21697
21707
  values: typeInfo.enum,
21698
21708
  example: typeInfo.example,
21699
21709
  description: typeInfo.description,
21700
- summary: typeInfo.summary
21710
+ summary: typeInfo.summary,
21711
+ customAttributes: typeInfo.customAttributes ?? null
21701
21712
  };
21702
21713
  const result = {
21703
21714
  content: '',
@@ -21756,6 +21767,13 @@ async function generateArrayType(typeName, typeInfo, parentTypes) {
21756
21767
  throw new InvalidSpecFileError('Invalid array type for: ' + typeName);
21757
21768
  }
21758
21769
  const dataType = fillPatterns(arrayTypeMap, fillerPatterns);
21770
+ const innerComposerType = 'composerType' in arrayItemsType.templateInput
21771
+ ? (arrayItemsType.templateInput.composerType ?? arrayItemsType.templateInput.type)
21772
+ : arrayItemsType.templateInput.type;
21773
+ const innerNestingDepth = 'arrayNestingDepth' in arrayItemsType.templateInput
21774
+ ? (arrayItemsType.templateInput.arrayNestingDepth ?? 0)
21775
+ : 0;
21776
+ const arrayNestingDepth = typeInfo.items?.type === 'array' ? innerNestingDepth + 1 : 0;
21759
21777
  const result = {
21760
21778
  content: dynamicGeneratedType,
21761
21779
  references: arrayItemsType.references,
@@ -21763,7 +21781,8 @@ async function generateArrayType(typeName, typeInfo, parentTypes) {
21763
21781
  templateInput: {
21764
21782
  typeName,
21765
21783
  type: dataType,
21766
- composerType: arrayItemsType.templateInput.type,
21784
+ composerType: innerComposerType,
21785
+ arrayNestingDepth,
21767
21786
  description: typeInfo.description,
21768
21787
  example: typeInfo.example,
21769
21788
  summary: typeInfo.summary
@@ -21823,7 +21842,8 @@ async function generateOneOfTypes(typeName, typeInfo, parentTypes) {
21823
21842
  compositions: [],
21824
21843
  description: typeInfo.description,
21825
21844
  example: typeInfo.example,
21826
- summary: typeInfo.summary
21845
+ summary: typeInfo.summary,
21846
+ customAttributes: typeInfo.customAttributes ?? null
21827
21847
  };
21828
21848
  const result = {
21829
21849
  content: '',
@@ -21875,7 +21895,8 @@ async function generateAllOfTypes(typeName, typeInfo, parentTypes) {
21875
21895
  compositions: [],
21876
21896
  description: typeInfo.description,
21877
21897
  example: typeInfo.example,
21878
- summary: typeInfo.summary
21898
+ summary: typeInfo.summary,
21899
+ customAttributes: typeInfo.customAttributes ?? null
21879
21900
  };
21880
21901
  const result = {
21881
21902
  content: '',
@@ -22526,7 +22547,7 @@ async function runner(language, inputFilePath, outputDirectory, _typesWriterMode
22526
22547
  }
22527
22548
  }
22528
22549
  greeting();
22529
- const program = new Command().version('0.14.0');
22550
+ const program = new Command().version('0.16.0');
22530
22551
  program
22531
22552
  .command('generate')
22532
22553
  .description('Generate types for your language from a type spec file')
@@ -18,6 +18,7 @@ export type {{typeName}} =
18
18
  {{/if}}
19
19
  {{/each}}
20
20
 
21
+ {{#unless customAttributes.skipDecoderGeneration}}
21
22
  export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
22
23
  {{#each compositions}}
23
24
  {{#if (eq this.source 'referenced')}}
@@ -40,3 +41,4 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
40
41
  }
41
42
  return null;
42
43
  }
44
+ {{/unless}}
@@ -12,6 +12,7 @@ export type {{typeName}} =
12
12
  | {{#if (eq ../type 'string') }}'{{/if}}{{{this}}}{{#if (eq ../type 'string')}}'{{/if}}
13
13
  {{/each}};
14
14
 
15
+ {{#unless customAttributes.skipDecoderGeneration}}
15
16
  export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
16
17
  switch (rawInput) {
17
18
  {{#each values}}
@@ -21,7 +22,9 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
21
22
  }
22
23
  return null;
23
24
  }
25
+ {{/unless}}
24
26
 
27
+ {{#if customAttributes.generateOptionalDecoder}}
25
28
  export function _decode{{typeName}}(rawInput: unknown): {{typeName}} | undefined {
26
29
  switch (rawInput) {
27
30
  {{#each values}}
@@ -31,3 +34,4 @@ export function _decode{{typeName}}(rawInput: unknown): {{typeName}} | undefined
31
34
  }
32
35
  return;
33
36
  }
37
+ {{/if}}
@@ -1,3 +1,10 @@
1
+ {{#*inline "nestedArrayDecoder"}}
2
+ {{~#if depth~}}
3
+ (item: unknown) => decodeArray(item, {{~> nestedArrayDecoder depth=(subtract depth 1) composerType=composerType~}})
4
+ {{~else~}}
5
+ decode{{{toPascalCase composerType}}}
6
+ {{~/if~}}
7
+ {{/inline}}
1
8
  /**
2
9
  * @type { {{typeName}} }
3
10
  {{#if description}}
@@ -42,8 +49,8 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
42
49
  const decoded{{variableName @key}} = {{#if this.referenced ~}}
43
50
  {{#if this.optional}}_{{/if}}decode{{{toPascalCase this.type}}}(rawInput[{{{indexKey @key}}}]);
44
51
  {{~else~}}
45
- {{~#if (eq this.primitiveType 'array')~}}
46
- decodeArray(rawInput[{{{indexKey @key}}}], decode{{{toPascalCase this.composerType}}})
52
+ {{~#if (eq this.primitiveType 'array')~}}
53
+ decodeArray(rawInput[{{{indexKey @key}}}], {{~> nestedArrayDecoder depth=this.arrayNestingDepth composerType=this.composerType~}})
47
54
  {{~else~}}
48
55
  {{#if this.optional}}_{{/if}}decode{{{toPascalCase this.type}}}(rawInput[{{{indexKey @key}}}])
49
56
  {{~/if~}};
@@ -67,13 +74,13 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
67
74
  for (const key in rawInput) {
68
75
  {{#if additionalProperties.valueTypeReferenced}}
69
76
  const decodedValue = {{#if (eq additionalProperties.valuePrimitiveType 'array')~}}
70
- decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
77
+ decodeArray(rawInput[key], {{~> nestedArrayDecoder depth=additionalProperties.valueArrayNestingDepth composerType=additionalProperties.valueComposerType~}});
71
78
  {{~else~}}
72
79
  decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
73
80
  {{~/if}}
74
81
  {{else}}
75
82
  {{#if (eq additionalProperties.valuePrimitiveType 'array')}}
76
- const decodedValue = decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
83
+ const decodedValue = decodeArray(rawInput[key], {{~> nestedArrayDecoder depth=additionalProperties.valueArrayNestingDepth composerType=additionalProperties.valueComposerType~}});
77
84
  {{else}}
78
85
  const decodedValue = decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
79
86
  {{/if}}
@@ -91,13 +98,13 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
91
98
  if (!knownKeys.has(key)) {
92
99
  {{#if additionalProperties.valueTypeReferenced}}
93
100
  const decodedValue = {{#if (eq additionalProperties.valuePrimitiveType 'array')~}}
94
- decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
101
+ decodeArray(rawInput[key], {{~> nestedArrayDecoder depth=additionalProperties.valueArrayNestingDepth composerType=additionalProperties.valueComposerType~}});
95
102
  {{~else~}}
96
103
  decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
97
104
  {{~/if}}
98
105
  {{else}}
99
106
  {{#if (eq additionalProperties.valuePrimitiveType 'array')}}
100
- const decodedValue = decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
107
+ const decodedValue = decodeArray(rawInput[key], {{~> nestedArrayDecoder depth=additionalProperties.valueArrayNestingDepth composerType=additionalProperties.valueComposerType~}});
101
108
  {{else}}
102
109
  const decodedValue = decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
103
110
  {{/if}}
@@ -135,7 +142,7 @@ export function _decode{{typeName}}(rawInput: unknown): {{typeName}} | undefined
135
142
  {{#if this.optional}}_{{/if}}decode{{{toPascalCase this.type}}}(rawInput[{{{indexKey @key}}}]);
136
143
  {{~else~}}
137
144
  {{~#if (eq this.primitiveType 'array')~}}
138
- decodeArray(rawInput[{{{indexKey @key}}}], decode{{{toPascalCase this.composerType}}})
145
+ decodeArray(rawInput[{{{indexKey @key}}}], {{~> nestedArrayDecoder depth=this.arrayNestingDepth composerType=this.composerType~}})
139
146
  {{~else~}}
140
147
  {{#if this.optional}}_{{/if}}decode{{{toPascalCase this.type}}}(rawInput[{{{indexKey @key}}}])
141
148
  {{~/if~}};
@@ -159,13 +166,13 @@ export function _decode{{typeName}}(rawInput: unknown): {{typeName}} | undefined
159
166
  for (const key in rawInput) {
160
167
  {{#if additionalProperties.valueTypeReferenced}}
161
168
  const decodedValue = {{#if (eq additionalProperties.valuePrimitiveType 'array')~}}
162
- decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
169
+ decodeArray(rawInput[key], {{~> nestedArrayDecoder depth=additionalProperties.valueArrayNestingDepth composerType=additionalProperties.valueComposerType~}});
163
170
  {{~else~}}
164
171
  decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
165
172
  {{~/if}}
166
173
  {{else}}
167
174
  {{#if (eq additionalProperties.valuePrimitiveType 'array')}}
168
- const decodedValue = decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
175
+ const decodedValue = decodeArray(rawInput[key], {{~> nestedArrayDecoder depth=additionalProperties.valueArrayNestingDepth composerType=additionalProperties.valueComposerType~}});
169
176
  {{else}}
170
177
  const decodedValue = decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
171
178
  {{/if}}
@@ -183,13 +190,13 @@ export function _decode{{typeName}}(rawInput: unknown): {{typeName}} | undefined
183
190
  if (!knownKeys.has(key)) {
184
191
  {{#if additionalProperties.valueTypeReferenced}}
185
192
  const decodedValue = {{#if (eq additionalProperties.valuePrimitiveType 'array')~}}
186
- decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
193
+ decodeArray(rawInput[key], {{~> nestedArrayDecoder depth=additionalProperties.valueArrayNestingDepth composerType=additionalProperties.valueComposerType~}});
187
194
  {{~else~}}
188
195
  decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
189
196
  {{~/if}}
190
197
  {{else}}
191
198
  {{#if (eq additionalProperties.valuePrimitiveType 'array')}}
192
- const decodedValue = decodeArray(rawInput[key], decode{{{toPascalCase additionalProperties.valueComposerType}}});
199
+ const decodedValue = decodeArray(rawInput[key], {{~> nestedArrayDecoder depth=additionalProperties.valueArrayNestingDepth composerType=additionalProperties.valueComposerType~}});
193
200
  {{else}}
194
201
  const decodedValue = decode{{{toPascalCase additionalProperties.valueType}}}(rawInput[key]);
195
202
  {{/if}}
@@ -1,3 +1,10 @@
1
+ {{#*inline "nestedArrayDecoder"}}
2
+ {{~#if depth~}}
3
+ (item: unknown) => decodeArray(item, {{~> nestedArrayDecoder depth=(subtract depth 1) composerType=composerType~}})
4
+ {{~else~}}
5
+ decode{{{toPascalCase composerType}}}
6
+ {{~/if~}}
7
+ {{/inline}}
1
8
  export type {{typeName}} =
2
9
  {{#each compositions}}
3
10
  | {{#if (eq this.source 'referenced')}}C{{../typeName}}{{referencedType}}
@@ -12,6 +19,7 @@ export type {{typeName}} =
12
19
  {{/if}}
13
20
  {{/each}};
14
21
 
22
+ {{#unless customAttributes.skipDecoderGeneration}}
15
23
  export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
16
24
  const result: {{typeName}} | null =
17
25
  {{#each compositions}}
@@ -20,7 +28,7 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
20
28
  {{else if (eq this.dataType 'object')}}
21
29
  decodeC{{this.templateInput.typeName}}(rawInput)
22
30
  {{else if (eq this.dataType 'array')}}
23
- decodeArray(rawInput, decode{{{toPascalCase this.templateInput.composerType}}})
31
+ decodeArray(rawInput, {{~> nestedArrayDecoder depth=this.templateInput.arrayNestingDepth composerType=this.templateInput.composerType~}})
24
32
  {{else if this.templateInput.values}}
25
33
  decode{{this.templateInput.typeName}}(rawInput)
26
34
  {{else}}
@@ -29,6 +37,7 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
29
37
  {{/each}};
30
38
  return result;
31
39
  }
40
+ {{/unless}}
32
41
 
33
42
 
34
43
  {{#each compositions}}
@@ -43,6 +52,7 @@ export class C{{../typeName}}{{referencedType}} {
43
52
  }
44
53
  }
45
54
 
55
+ {{#unless ../customAttributes.skipDecoderGeneration}}
46
56
  export function decodeC{{../typeName}}{{referencedType}}(rawInput: unknown): C{{../typeName}}{{referencedType}} | null {
47
57
  const result = decode{{referencedType}}(rawInput);
48
58
  if (result === null) {
@@ -50,6 +60,7 @@ export function decodeC{{../typeName}}{{referencedType}}(rawInput: unknown): C{{
50
60
  }
51
61
  return new C{{../typeName}}{{referencedType}}(result);
52
62
  }
63
+ {{/unless}}
53
64
 
54
65
  {{else if (eq this.dataType 'object')}}
55
66
 
@@ -62,6 +73,7 @@ export class C{{../typeName}}{{this.templateInput.typeName}} {
62
73
  }
63
74
  }
64
75
 
76
+ {{#unless ../customAttributes.skipDecoderGeneration}}
65
77
  export function decodeC{{this.templateInput.typeName}}(rawInput: unknown) {
66
78
  const result = decode{{this.templateInput.typeName}}(rawInput);
67
79
  if (result === null) {
@@ -69,6 +81,7 @@ export function decodeC{{this.templateInput.typeName}}(rawInput: unknown) {
69
81
  }
70
82
  return new C{{../typeName}}{{this.templateInput.typeName}}(result);
71
83
  }
84
+ {{/unless}}
72
85
 
73
86
  {{/if}}
74
87
  {{/each}}
@@ -129,6 +129,7 @@ export type ObjectTemplateInputProperty = TypeDescriptors & {
129
129
  referenced: boolean;
130
130
  primitiveType: string;
131
131
  composerType: string | null;
132
+ arrayNestingDepth?: number;
132
133
  optional: boolean;
133
134
  customAttributes?: Record<string, unknown> | null;
134
135
  };
@@ -138,6 +139,7 @@ export type AdditionalPropertiesTemplateInput = {
138
139
  valueTypeReferenced: boolean;
139
140
  valuePrimitiveType: string;
140
141
  valueComposerType: string | null;
142
+ valueArrayNestingDepth?: number;
141
143
  };
142
144
  export type AdditionalPropertiesGenerationResult = {
143
145
  templateInput: AdditionalPropertiesTemplateInput;
@@ -163,11 +165,13 @@ export type EnumTemplateInput = TypeDescriptors & {
163
165
  typeName: string;
164
166
  type: string;
165
167
  values: string[] | number[];
168
+ customAttributes?: Record<string, unknown> | null;
166
169
  };
167
170
  export type OneOfTemplateInput = TypeDescriptors & {
168
171
  typeName: string;
169
172
  type: string;
170
173
  compositions: OneOfTemplateInputComposition[];
174
+ customAttributes?: Record<string, unknown> | null;
171
175
  };
172
176
  export type OneOfTemplateInputComposition = {
173
177
  dataType?: TypeDataType | null;
@@ -180,6 +184,7 @@ export type AllOfTemplateInput = TypeDescriptors & {
180
184
  typeName: string;
181
185
  type: string;
182
186
  compositions: AllOfTemplateInputComposition[];
187
+ customAttributes?: Record<string, unknown> | null;
183
188
  };
184
189
  export type AllOfTemplateInputComposition = {
185
190
  dataType?: TypeDataType | null;
@@ -192,6 +197,7 @@ export type VariableTemplateInput = TypeDescriptors & {
192
197
  typeName: string;
193
198
  type: string;
194
199
  composerType?: string;
200
+ arrayNestingDepth?: number;
195
201
  };
196
202
  export type TemplateInput = ObjectTemplateInput | EnumTemplateInput | OneOfTemplateInput | AllOfTemplateInput | VariableTemplateInput;
197
203
  export type GenerationResult = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-crafter",
3
- "version": "0.14.0",
3
+ "version": "0.16.0",
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",