type-crafter 0.4.0 → 0.6.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/runtime.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Configuration, EnumTemplateInput, ExporterModuleTemplateInput, ObjectTemplateInput, OneOfTemplateInput, SpecFileData, TypeFilePath, TypesFileTemplateInput } from '$types';
1
+ import type { Configuration, EnumTemplateInput, ExporterModuleTemplateInput, GeneratedReferencedType, ObjectTemplateInput, OneOfTemplateInput, SpecFileData, TypeFilePath, TypesFileTemplateInput } from '$types';
2
2
  declare function setConfig(newConfig: Configuration): void;
3
3
  declare function getConfig(): Configuration;
4
4
  declare function setSpecFileData(newSpecFileData: SpecFileData): void;
@@ -11,6 +11,10 @@ declare function getEnumTemplate(): HandlebarsTemplateDelegate<EnumTemplateInput
11
11
  declare function getOneOfTemplate(): HandlebarsTemplateDelegate<OneOfTemplateInput>;
12
12
  declare function setExpectedOutputFiles(newExpectedOutputFiles: Map<string, TypeFilePath>): void;
13
13
  declare function getExpectedOutputFiles(): Map<string, TypeFilePath>;
14
+ declare function getCachedReferencedTypes(): Map<string, GeneratedReferencedType>;
15
+ declare function getCachedReferenceType(referencePath: string): GeneratedReferencedType | null;
16
+ declare function cacheReferenceType(referencePath: string, generatedData: GeneratedReferencedType): void;
17
+ declare function getInputFilePath(absolutePath?: boolean): string;
14
18
  declare const _default: {
15
19
  getConfig: typeof getConfig;
16
20
  setConfig: typeof setConfig;
@@ -24,5 +28,9 @@ declare const _default: {
24
28
  getExpectedOutputFiles: typeof getExpectedOutputFiles;
25
29
  compileTemplates: typeof compileTemplates;
26
30
  getOneOfTemplate: typeof getOneOfTemplate;
31
+ getCachedReferencedTypes: typeof getCachedReferencedTypes;
32
+ getCachedReferenceType: typeof getCachedReferenceType;
33
+ cacheReferenceType: typeof cacheReferenceType;
34
+ getInputFilePath: typeof getInputFilePath;
27
35
  };
28
36
  export default _default;
@@ -9,5 +9,5 @@
9
9
  */
10
10
  export type {{typeName}} =
11
11
  {{#each values}}
12
- | {{#if (eq ../enumType 'string') }}'{{/if}}{{{this}}}{{#if (eq ../enumType 'string')}}'{{/if}}
12
+ | {{#if (eq ../type 'string') }}'{{/if}}{{{this}}}{{#if (eq ../type 'string')}}'{{/if}}
13
13
  {{/each}};
@@ -19,6 +19,6 @@ export type {{typeName}} = {
19
19
  * @example {{{this.example}}}
20
20
  {{/if}}
21
21
  */
22
- {{@key}}: {{{this.type}}}{{#unless this.required}} | null{{/unless}};
22
+ {{{jsonKey @key}}}: {{{this.type}}}{{#unless this.required}} | null{{/unless}};
23
23
  {{/each}}
24
24
  };
@@ -13,7 +13,7 @@ export type {{typeName}} =
13
13
  {{/each}}
14
14
  }
15
15
  {{else}}
16
- {{this.templateInput.dataType}}
16
+ {{this.templateInput.type}}
17
17
  {{/if}}
18
18
  {{/if}}
19
19
  {{/each}};
@@ -9,13 +9,13 @@
9
9
  */
10
10
  export type {{typeName}} =
11
11
  {{#each values}}
12
- | {{#if (eq ../enumType 'string') }}'{{/if}}{{{this}}}{{#if (eq ../enumType 'string')}}'{{/if}}
12
+ | {{#if (eq ../type 'string') }}'{{/if}}{{{this}}}{{#if (eq ../type 'string')}}'{{/if}}
13
13
  {{/each}};
14
14
 
15
15
  export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
16
16
  switch (rawInput) {
17
17
  {{#each values}}
18
- case {{#if (eq ../enumType 'string') }}'{{/if}}{{this}}{{#if (eq ../enumType 'string')}}'{{/if}}:
18
+ case {{#if (eq ../type 'string') }}'{{/if}}{{this}}{{#if (eq ../type 'string')}}'{{/if}}:
19
19
  {{/each}}
20
20
  return rawInput;
21
21
  }
@@ -19,20 +19,20 @@ export type {{typeName}} = {
19
19
  * @example {{{this.example}}}
20
20
  {{/if}}
21
21
  */
22
- {{@key}}: {{{this.type}}}{{#unless this.required}} | null{{/unless}};
22
+ {{{jsonKey @key}}}: {{{this.type}}}{{#unless this.required}} | null{{/unless}};
23
23
  {{/each}}
24
24
  };
25
25
 
26
26
  export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
27
27
  if (isJSON(rawInput)) {
28
28
  {{#each properties}}
29
- const {{@key}} = {{#if (eq this.primitiveType 'array') }} decodeArray(rawInput.{{@key}}, decode{{{toPascalCase this.composerType}}}){{else}} decode{{{toPascalCase this.type}}}(rawInput.{{@key}}){{/if}};
29
+ 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}};
30
30
  {{/each}}
31
31
 
32
32
  {{#if (areRequiredKeysPresent properties)}}
33
33
  if (
34
34
  {{#each (getRequiredKeys properties)}}
35
- {{this}} === null{{#unless @last}} ||{{/unless}}
35
+ decoded{{toPascalCase this}} === null{{#unless @last}} ||{{/unless}}
36
36
  {{/each}}
37
37
  ) {
38
38
  return null;
@@ -41,7 +41,7 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
41
41
 
42
42
  return {
43
43
  {{#each properties}}
44
- {{@key}},
44
+ {{{jsonKey @key}}}: decoded{{{variableName @key}}},
45
45
  {{/each}}
46
46
  };
47
47
  }
@@ -7,7 +7,7 @@ export type {{typeName}} =
7
7
  {{else if (eq this.dataType 'object')}}
8
8
  C{{this.templateInput.typeName}}
9
9
  {{else}}
10
- {{this.templateInput.dataType}}
10
+ {{this.templateInput.type}}
11
11
  {{/if}}
12
12
  {{/if}}
13
13
  {{/each}};
@@ -19,12 +19,12 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
19
19
  decodeC{{referencedType}}(rawInput)
20
20
  {{else if (eq this.dataType 'object')}}
21
21
  decodeC{{this.templateInput.typeName}}(rawInput)
22
- {{else if (eq this.templateInput.primitiveType 'array')}}
23
- decodeArray(rawInput, decode{{{toPascalCase this.templateInput.itemType}}})
22
+ {{else if (eq this.dataType 'array')}}
23
+ decodeArray(rawInput, decode{{{toPascalCase this.templateInput.composerType}}})
24
24
  {{else if this.templateInput.values}}
25
25
  decode{{this.templateInput.typeName}}(rawInput)
26
26
  {{else}}
27
- decode{{{toPascalCase this.templateInput.dataType}}}(rawInput)
27
+ decode{{{toPascalCase this.templateInput.type}}}(rawInput)
28
28
  {{/if}}{{#unless @last}}??{{/unless}}
29
29
  {{/each}};
30
30
  return result;
@@ -1,6 +1,9 @@
1
- import type { EnumTemplateInput, GroupedTypesWriterMode, ObjectTemplateInputProperties, SpecFileData, TypesWriterMode } from '.';
1
+ import type { GroupRef, EnumTemplateInput, GroupedTypesWriterMode, ObjectTemplateInputProperties, SpecFileData, TypeInfo, Types, TypesWriterMode, GroupTypesData } from '.';
2
2
  export declare function decodeGroupedTypesWriterMode(rawInput: unknown): GroupedTypesWriterMode | null;
3
3
  export declare function decodeTypesWriterMode(rawInput: unknown): TypesWriterMode | null;
4
4
  export declare function decodeSpecFileData(rawInput: unknown): SpecFileData | null;
5
+ export declare function valueIsGroupRef(value: GroupTypesData): value is GroupRef;
6
+ export declare function decodeTypes(rawInput: unknown): Types | null;
7
+ export declare function decodeTypeInfo(rawInput: unknown): TypeInfo | null;
5
8
  export declare function decodeObjectTemplateInputProperties(rawInput: unknown): ObjectTemplateInputProperties | null;
6
9
  export declare function decodeEnumTemplateInput(rawInput: unknown): EnumTemplateInput | null;
@@ -36,6 +36,23 @@ export type FormatType = {
36
36
  default: string;
37
37
  [format: string]: string | undefined;
38
38
  };
39
+ export type ReferenceType = 'local' | 'remote' | 'url';
40
+ export type ReferenceMeta = {
41
+ completeSource: string;
42
+ sourceFile: string;
43
+ path: string;
44
+ type: ReferenceType;
45
+ name: string;
46
+ };
47
+ export type ResolvedTypeReferenceData = ReferenceMeta & {
48
+ typeInfo: TypeInfo;
49
+ };
50
+ export type ResolvedGroupReferenceData = ReferenceMeta & {
51
+ groupedTypes: Types;
52
+ };
53
+ export type GeneratedReferencedType = ResolvedTypeReferenceData & {
54
+ templateData: GeneratedType<TemplateInput>;
55
+ };
39
56
  export type SpecFileData = {
40
57
  info: SpecInfo;
41
58
  groupedTypes: GroupedTypes | null;
@@ -46,10 +63,14 @@ export type SpecInfo = {
46
63
  title: string;
47
64
  };
48
65
  type GroupName = string;
49
- export type GroupedTypes = Record<GroupName, Types>;
66
+ export type GroupedTypes = Record<GroupName, GroupTypesData>;
67
+ export type GroupTypesData = Types | GroupRef;
68
+ export type GroupRef = {
69
+ $ref: string;
70
+ };
50
71
  type TypeName = string;
51
72
  export type Types = Record<TypeName, TypeInfo>;
52
- export type TypeInfo = {
73
+ export type TypeInfo = TypeDescriptors & {
53
74
  required: string[] | null;
54
75
  type: TypeDataType | null;
55
76
  format: string | null;
@@ -58,27 +79,27 @@ export type TypeInfo = {
58
79
  $ref: string | null;
59
80
  oneOf: TypeInfo[] | null;
60
81
  enum: string[] | number[] | null;
61
- description: string | null;
62
- example: string | number | null;
63
82
  };
64
83
  type PropertyName = string;
65
84
  export type TypeProperties = Record<PropertyName, TypeInfo>;
66
85
  export type TypeDataType = 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'object' | 'unknown';
67
- export type ObjectTemplateInput = {
68
- typeName: string;
86
+ export type TypeDescriptors = {
87
+ summary: string | null;
69
88
  example: string | number | null;
70
89
  description: string | null;
90
+ };
91
+ export type ObjectTemplateInput = TypeDescriptors & {
92
+ typeName: string;
93
+ type: string;
71
94
  properties: ObjectTemplateInputProperties;
72
95
  };
73
96
  export type ObjectTemplateInputProperties = Record<PropertyName, ObjectTemplateInputProperty>;
74
- export type ObjectTemplateInputProperty = {
97
+ export type ObjectTemplateInputProperty = TypeDescriptors & {
75
98
  type: string;
76
99
  required: boolean;
77
100
  referenced: boolean;
78
101
  primitiveType: string;
79
102
  composerType: string | null;
80
- example: string | number | null;
81
- description: string | null;
82
103
  };
83
104
  export type ExporterModuleTemplateInput = {
84
105
  modules: string[];
@@ -94,15 +115,14 @@ export type ReferencedModule = {
94
115
  moduleRelativePath: string;
95
116
  referencedTypes: string[];
96
117
  };
97
- export type EnumTemplateInput = {
118
+ export type EnumTemplateInput = TypeDescriptors & {
98
119
  typeName: string;
99
- enumType: string;
120
+ type: string;
100
121
  values: string[] | number[];
101
- example: string | number | null;
102
- description: string | null;
103
122
  };
104
- export type OneOfTemplateInput = {
123
+ export type OneOfTemplateInput = TypeDescriptors & {
105
124
  typeName: string;
125
+ type: string;
106
126
  compositions: OneOfTemplateInputComposition[];
107
127
  };
108
128
  export type OneOfTemplateInputComposition = {
@@ -112,25 +132,24 @@ export type OneOfTemplateInputComposition = {
112
132
  referencedType?: string;
113
133
  content?: string;
114
134
  };
115
- export type VariableTemplateInput = {
135
+ export type VariableTemplateInput = TypeDescriptors & {
116
136
  typeName: string;
117
- dataType: string;
118
- primitiveType: string;
119
- itemType?: string;
137
+ type: string;
138
+ composerType?: string;
120
139
  };
121
- export type TemplateInput = ObjectTemplateInput | EnumTemplateInput | VariableTemplateInput;
140
+ export type TemplateInput = ObjectTemplateInput | EnumTemplateInput | OneOfTemplateInput | VariableTemplateInput;
122
141
  export type GenerationResult = {
123
142
  groupedTypes: GroupedTypesOutput;
124
143
  types: GeneratedTypes;
125
144
  };
126
- export type GeneratedType = {
145
+ export type GeneratedType<TemplateType extends TemplateInput> = {
127
146
  content: string;
128
147
  references: Set<string>;
129
148
  primitives: Set<string>;
130
- templateInput?: TemplateInput;
149
+ templateInput: TemplateType;
131
150
  };
132
151
  export type GroupedTypesOutput = Record<GroupName, GeneratedTypes>;
133
- export type GeneratedTypes = Record<TypeName, GeneratedType>;
152
+ export type GeneratedTypes = Record<TypeName, GeneratedType<TemplateInput>>;
134
153
  export type TypeFilePath = {
135
154
  modulePath: string;
136
155
  filePath: string;
@@ -6,3 +6,4 @@ export declare function deleteFolder(folderPath: string): Promise<void>;
6
6
  export declare function getCompleteFolderPath(folderName: string): Promise<string>;
7
7
  export declare function getExpectedWrittenPath(basePath: string, fileName: string): Promise<string>;
8
8
  export declare function writeFile(basePath: string, fileName: string, content: string): Promise<void>;
9
+ export declare function readYaml(filePath: string): Promise<unknown>;
@@ -8,7 +8,10 @@ export declare function getReferencedTypes(object: unknown): string[];
8
8
  export declare function getReferencedTypeModules(_referencedTypes: unknown, _writtenAt: string): unknown[];
9
9
  export declare function toPascalCase(input: string): string;
10
10
  export declare function toPascalCaseHelper(input: unknown): string | unknown;
11
+ export declare function refineJSONKey(input: unknown): unknown;
12
+ export declare function refineVariableName(input: unknown): unknown;
13
+ export declare function refineIndexKey(input: unknown): unknown;
11
14
  export declare function registerTemplateHelpers(): void;
12
- export declare function readNestedValue(json: JSONObject, keyPath: string[]): JSONObject;
15
+ export declare function readNestedValue(json: unknown, keyPath: string[]): JSONObject;
13
16
  export declare function generateRelativePath(fromPath: string, toPath: string): string;
14
17
  export declare function stripPrefix(value: string, prefix: string): string;
@@ -1,3 +1,3 @@
1
- import type { TypeFilePath, Types } from '$types';
1
+ import { type TypeFilePath, type Types } from '$types';
2
2
  export declare function generateTypesOutputFiles(types: Types | null, groupName?: string | null): Map<string, TypeFilePath>;
3
3
  export declare function generateExpectedOutputFile(): Map<string, TypeFilePath>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-crafter",
3
- "version": "0.4.0",
3
+ "version": "0.6.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",
@@ -68,7 +68,7 @@
68
68
  "rollup": "^4.5.1",
69
69
  "rollup-plugin-copy": "^3.5.0",
70
70
  "tslib": "^2.6.2",
71
- "type-decoder": "^1.2.0",
71
+ "type-decoder": "^2.0.1",
72
72
  "typescript": "^5.2.2",
73
73
  "yaml": "^2.3.2"
74
74
  }