type-crafter 0.8.2 → 0.9.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
@@ -3550,6 +3550,7 @@ function decodeTypeInfo(rawInput) {
3550
3550
  const result = {
3551
3551
  type: _type,
3552
3552
  required: decodeArray(rawInput.required, decodeString),
3553
+ optional: decodeArray(rawInput.optional, decodeString),
3553
3554
  properties: decodeTypeProperties(rawInput.properties),
3554
3555
  items: decodeTypeInfo(rawInput.items),
3555
3556
  format: decodeString(rawInput.format),
@@ -3627,6 +3628,7 @@ function decodeTypeDataType(rawInput) {
3627
3628
  function decodeObjectTemplateInputProperty(rawInput) {
3628
3629
  if (isJSON(rawInput)) {
3629
3630
  const required = decodeBoolean(rawInput.required);
3631
+ const optional = decodeBoolean(rawInput.optional);
3630
3632
  const _type = decodeString(rawInput.type);
3631
3633
  const referenced = decodeBoolean(rawInput.referenced);
3632
3634
  const primitiveType = decodeString(rawInput.primitiveType);
@@ -3634,10 +3636,11 @@ function decodeObjectTemplateInputProperty(rawInput) {
3634
3636
  const summary = decodeString(rawInput.summary);
3635
3637
  const example = decodeString(rawInput.example) ?? decodeNumber(rawInput.example);
3636
3638
  const description = decodeString(rawInput.description);
3637
- if (required !== null && _type !== null && referenced !== null && primitiveType !== null) {
3639
+ if (required !== null && _type !== null && referenced !== null && primitiveType !== null && optional !== null) {
3638
3640
  return {
3639
3641
  type: _type,
3640
3642
  required,
3643
+ optional,
3641
3644
  referenced,
3642
3645
  primitiveType,
3643
3646
  composerType,
@@ -19714,6 +19717,7 @@ function fillPatterns(input, patterns) {
19714
19717
 
19715
19718
  const placeholderTypeInfo = {
19716
19719
  required: null,
19720
+ optional: null,
19717
19721
  type: null,
19718
19722
  format: null,
19719
19723
  items: null,
@@ -19880,7 +19884,8 @@ async function generateObjectType(typeName, typeInfo, parentTypes) {
19880
19884
  composerType,
19881
19885
  example: propertyDetails.example,
19882
19886
  description: propertyDetails.description,
19883
- summary: propertyDetails.summary
19887
+ summary: propertyDetails.summary,
19888
+ optional: typeInfo.optional?.includes(propertyName) ?? false
19884
19889
  }
19885
19890
  };
19886
19891
  }
@@ -20508,7 +20513,7 @@ async function runner(language, inputFilePath, outputDirectory, _typesWriterMode
20508
20513
  }
20509
20514
  }
20510
20515
  greeting();
20511
- const program = new Command().version('0.8.2');
20516
+ const program = new Command().version('0.9.1');
20512
20517
  program
20513
20518
  .command('generate')
20514
20519
  .description('Generate types for your language from a type spec file')
@@ -21,3 +21,14 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
21
21
  }
22
22
  return null;
23
23
  }
24
+
25
+
26
+ export function _decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
27
+ switch (rawInput) {
28
+ {{#each values}}
29
+ case {{#if (eq ../type 'string') }}'{{/if}}{{this}}{{#if (eq ../type 'string')}}'{{/if}}:
30
+ {{/each}}
31
+ return rawInput;
32
+ }
33
+ return;
34
+ }
@@ -7,7 +7,7 @@
7
7
  * @example {{{example}}}
8
8
  {{/if}}
9
9
  */
10
- export type {{typeName}} = {{#if (not (isEmptyObject properties))}} {
10
+ export type {{typeName}} = {{#if (not (isEmptyObject properties))}}{
11
11
  {{#each properties}}
12
12
  /**
13
13
  {{#if this.description}}
@@ -18,8 +18,14 @@ export type {{typeName}} = {{#if (not (isEmptyObject properties))}} {
18
18
  {{#if this.example}}
19
19
  * @example {{{this.example}}}
20
20
  {{/if}}
21
- */
22
- {{{jsonKey @key}}}: {{{this.type}}}{{#unless this.required}} | null{{/unless}};
21
+ */
22
+ {{#if this.required}}
23
+ {{{jsonKey @key}}}: {{{this.type}}};
24
+ {{else if this.optional}}
25
+ {{{jsonKey @key}}}?: {{{this.type}}};
26
+ {{else}}
27
+ {{{jsonKey @key}}}: {{{this.type}}} | null;
28
+ {{/if}}
23
29
  {{/each}}
24
30
  {{#if additionalProperties}}
25
31
  [keys: {{jsonKey additionalProperties.keyType}}]: {{additionalProperties.valueType}};
@@ -32,7 +38,16 @@ Record<{{jsonKey additionalProperties.keyType}}, {{additionalProperties.valueTyp
32
38
  export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
33
39
  if (isJSON(rawInput)) {
34
40
  {{#each properties}}
35
- const decoded{{variableName @key}} = {{#if (eq this.primitiveType 'array') }} decodeArray(rawInput[{{{indexKey @key}}}], decode{{{toPascalCase this.composerType}}}){{else}} decode{{{toPascalCase this.type}}}(rawInput[{{{indexKey @key}}}]){{/if}};
41
+ const decoded{{variableName @key}} = {{#if this.referenced ~}}
42
+ decode{{{toPascalCase this.type}}}(rawInput[{{{indexKey @key}}}]);
43
+ {{~else~}}
44
+ {{~#if (eq this.primitiveType 'array')~}}
45
+ decodeArray(rawInput[{{{indexKey @key}}}], decode{{{toPascalCase this.composerType}}})
46
+ {{~else~}}
47
+ {{#if this.optional}}_{{/if}}decode{{{toPascalCase this.type}}}(rawInput[{{{indexKey @key}}}])
48
+ {{~/if~}};
49
+ {{~/if}}
50
+
36
51
  {{/each}}
37
52
 
38
53
  {{#if (areRequiredKeysPresent properties)}}
@@ -50,7 +65,7 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
50
65
  ...rawInput,
51
66
  {{/if}}
52
67
  {{#each properties}}
53
- {{{jsonKey @key}}}: decoded{{{variableName @key}}},
68
+ {{{jsonKey @key}}}: decoded{{{variableName @key}}}{{#unless @last}},{{/unless}}
54
69
  {{/each}}
55
70
  };
56
71
  }
@@ -1,7 +1,7 @@
1
1
  {{#each (getReferencedTypeModules referencedTypes writtenAt)}}
2
2
  import { {{#each this.referencedTypes}}type {{this}}, decode{{this}}{{#unless @last}}, {{/unless}} {{/each}} } from '{{this.moduleRelativePath}}';
3
3
  {{/each}}
4
- import { isJSON, {{#each primitives}}decode{{{toPascalCase this}}}{{#unless @last}}, {{/unless}}{{/each}} } from 'type-decoder';
4
+ import { isJSON, {{#each primitives}}decode{{{toPascalCase this}}}, _decode{{{toPascalCase this}}} {{#unless @last}}, {{/unless}}{{/each}} } from 'type-decoder';
5
5
 
6
6
  {{{typesContent}}}
7
7
 
@@ -73,6 +73,7 @@ type TypeName = string;
73
73
  export type Types = Record<TypeName, TypeInfo>;
74
74
  export type TypeInfo = TypeDescriptors & {
75
75
  required: string[] | null;
76
+ optional: string[] | null;
76
77
  type: TypeDataType | null;
77
78
  format: string | null;
78
79
  items: TypeInfo | null;
@@ -110,6 +111,7 @@ export type ObjectTemplateInputProperty = TypeDescriptors & {
110
111
  referenced: boolean;
111
112
  primitiveType: string;
112
113
  composerType: string | null;
114
+ optional: boolean;
113
115
  };
114
116
  export type AdditionalPropertiesTemplateInput = {
115
117
  keyType: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-crafter",
3
- "version": "0.8.2",
3
+ "version": "0.9.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",