type-crafter 0.3.0 → 0.5.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, 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;
@@ -8,8 +8,12 @@ declare function getObjectTemplate(): HandlebarsTemplateDelegate<ObjectTemplateI
8
8
  declare function getExporterModuleTemplate(): HandlebarsTemplateDelegate<ExporterModuleTemplateInput>;
9
9
  declare function getTypesFileTemplate(): HandlebarsTemplateDelegate<TypesFileTemplateInput>;
10
10
  declare function getEnumTemplate(): HandlebarsTemplateDelegate<EnumTemplateInput>;
11
+ declare function getOneOfTemplate(): HandlebarsTemplateDelegate<OneOfTemplateInput>;
11
12
  declare function setExpectedOutputFiles(newExpectedOutputFiles: Map<string, TypeFilePath>): void;
12
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;
13
17
  declare const _default: {
14
18
  getConfig: typeof getConfig;
15
19
  setConfig: typeof setConfig;
@@ -22,5 +26,9 @@ declare const _default: {
22
26
  getEnumTemplate: typeof getEnumTemplate;
23
27
  getExpectedOutputFiles: typeof getExpectedOutputFiles;
24
28
  compileTemplates: typeof compileTemplates;
29
+ getOneOfTemplate: typeof getOneOfTemplate;
30
+ getCachedReferencedTypes: typeof getCachedReferencedTypes;
31
+ getCachedReferenceType: typeof getCachedReferenceType;
32
+ cacheReferenceType: typeof cacheReferenceType;
25
33
  };
26
34
  export default _default;
@@ -1,4 +1,13 @@
1
- export type {{enumName}} =
1
+ /**
2
+ * @type { {{typeName}} }
3
+ {{#if description}}
4
+ * @description {{{description}}}
5
+ {{/if}}
6
+ {{#if example}}
7
+ * @example {{{example}}}
8
+ {{/if}}
9
+ */
10
+ export type {{typeName}} =
2
11
  {{#each values}}
3
- | {{#if (eq ../enumType 'string') }}'{{/if}}{{{this}}}{{#if (eq ../enumType 'string')}}'{{/if}}
12
+ | {{#if (eq ../type 'string') }}'{{/if}}{{{this}}}{{#if (eq ../type 'string')}}'{{/if}}
4
13
  {{/each}};
@@ -1,5 +1,24 @@
1
+ /**
2
+ * @type { {{typeName}} }
3
+ {{#if description}}
4
+ * @description {{{description}}}
5
+ {{/if}}
6
+ {{#if example}}
7
+ * @example {{{example}}}
8
+ {{/if}}
9
+ */
1
10
  export type {{typeName}} = {
2
11
  {{#each properties}}
12
+ /**
13
+ {{#if this.description}}
14
+ * @description {{{this.description}}}
15
+ {{/if}}
16
+ * @type { {{this.type}} }
17
+ * @memberof {{../typeName}}
18
+ {{#if this.example}}
19
+ * @example {{{this.example}}}
20
+ {{/if}}
21
+ */
3
22
  {{@key}}: {{{this.type}}}{{#unless this.required}} | null{{/unless}};
4
23
  {{/each}}
5
24
  };
@@ -0,0 +1,19 @@
1
+ export type {{typeName}} =
2
+ {{#each compositions}}
3
+ | {{#if (eq this.source 'referenced')}}{{referencedType}}
4
+ {{else if (eq this.source 'inline')}}
5
+ {{#if this.templateInput.values}}
6
+ {{#each this.templateInput.values}}
7
+ {{#unless @first}}|{{/unless}} {{#if (eq ../this.templateInput.enumType 'string') }}'{{/if}}{{{this}}}{{#if (eq ../this.templateInput/enumType 'string')}}'{{/if}}
8
+ {{/each}}
9
+ {{else if (eq this.dataType 'object')}}
10
+ {
11
+ {{#each this.templateInput.properties}}
12
+ {{@key}}: {{{this.type}}}{{#unless this.required}} | null{{/unless}};
13
+ {{/each}}
14
+ }
15
+ {{else}}
16
+ {{this.templateInput.dataType}}
17
+ {{/if}}
18
+ {{/if}}
19
+ {{/each}};
@@ -1,13 +1,21 @@
1
- export type {{enumName}} =
1
+ /**
2
+ * @type { {{typeName}} }
3
+ {{#if description}}
4
+ * @description {{{description}}}
5
+ {{/if}}
6
+ {{#if example}}
7
+ * @example {{{example}}}
8
+ {{/if}}
9
+ */
10
+ export type {{typeName}} =
2
11
  {{#each values}}
3
- | {{#if (eq ../enumType 'string') }}'{{/if}}{{{this}}}{{#if (eq ../enumType 'string')}}'{{/if}}
12
+ | {{#if (eq ../type 'string') }}'{{/if}}{{{this}}}{{#if (eq ../type 'string')}}'{{/if}}
4
13
  {{/each}};
5
14
 
6
-
7
- export function decode{{enumName}}(rawInput: unknown): {{enumName}} | null {
15
+ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
8
16
  switch (rawInput) {
9
17
  {{#each values}}
10
- 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}}:
11
19
  {{/each}}
12
20
  return rawInput;
13
21
  }
@@ -1,5 +1,24 @@
1
+ /**
2
+ * @type { {{typeName}} }
3
+ {{#if description}}
4
+ * @description {{{description}}}
5
+ {{/if}}
6
+ {{#if example}}
7
+ * @example {{{example}}}
8
+ {{/if}}
9
+ */
1
10
  export type {{typeName}} = {
2
11
  {{#each properties}}
12
+ /**
13
+ {{#if this.description}}
14
+ * @description {{{this.description}}}
15
+ {{/if}}
16
+ * @type { {{this.type}} }
17
+ * @memberof {{../typeName}}
18
+ {{#if this.example}}
19
+ * @example {{{this.example}}}
20
+ {{/if}}
21
+ */
3
22
  {{@key}}: {{{this.type}}}{{#unless this.required}} | null{{/unless}};
4
23
  {{/each}}
5
24
  };
@@ -0,0 +1,74 @@
1
+ export type {{typeName}} =
2
+ {{#each compositions}}
3
+ | {{#if (eq this.source 'referenced')}}C{{referencedType}}
4
+ {{else if (eq this.source 'inline')}}
5
+ {{#if this.templateInput.values}}
6
+ {{this.templateInput.typeName}}
7
+ {{else if (eq this.dataType 'object')}}
8
+ C{{this.templateInput.typeName}}
9
+ {{else}}
10
+ {{this.templateInput.dataType}}
11
+ {{/if}}
12
+ {{/if}}
13
+ {{/each}};
14
+
15
+ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
16
+ const result: {{typeName}} | null =
17
+ {{#each compositions}}
18
+ {{#if (eq this.source 'referenced')}}
19
+ decodeC{{referencedType}}(rawInput)
20
+ {{else if (eq this.dataType 'object')}}
21
+ decodeC{{this.templateInput.typeName}}(rawInput)
22
+ {{else if (eq this.templateInput.primitiveType 'array')}}
23
+ decodeArray(rawInput, decode{{{toPascalCase this.templateInput.itemType}}})
24
+ {{else if this.templateInput.values}}
25
+ decode{{this.templateInput.typeName}}(rawInput)
26
+ {{else}}
27
+ decode{{{toPascalCase this.templateInput.dataType}}}(rawInput)
28
+ {{/if}}{{#unless @last}}??{{/unless}}
29
+ {{/each}};
30
+ return result;
31
+ }
32
+
33
+
34
+ {{#each compositions}}
35
+
36
+ {{#if this.templateInput.values}}
37
+ {{{this.content}}}
38
+ {{else if (eq this.source 'referenced')}}
39
+ export class C{{referencedType}} {
40
+ data: {{referencedType}};
41
+ constructor(data: {{referencedType}}) {
42
+ this.data = data;
43
+ }
44
+ }
45
+
46
+ export function decodeC{{referencedType}}(rawInput: unknown): C{{referencedType}} | null {
47
+ const result = decode{{referencedType}}(rawInput);
48
+ if (result === null) {
49
+ return null;
50
+ }
51
+ return new C{{referencedType}}(result);
52
+ }
53
+
54
+ {{else if (eq this.dataType 'object')}}
55
+
56
+ {{{this.content}}}
57
+
58
+ export class C{{this.templateInput.typeName}} {
59
+ data: {{this.templateInput.typeName}};
60
+ constructor(data: {{this.templateInput.typeName}}) {
61
+ this.data = data;
62
+ }
63
+ }
64
+
65
+ export function decodeC{{this.templateInput.typeName}}(rawInput: unknown) {
66
+ const result = decode{{this.templateInput.typeName}}(rawInput);
67
+ if (result === null) {
68
+ return null;
69
+ }
70
+ return new C{{this.templateInput.typeName}}(result);
71
+ }
72
+
73
+ {{/if}}
74
+ {{/each}}
@@ -1,6 +1,7 @@
1
- import type { EnumTemplateInput, GroupedTypesWriterMode, ObjectTemplateInputProperties, SpecFileData, TypesWriterMode } from '.';
1
+ import type { EnumTemplateInput, GroupedTypesWriterMode, ObjectTemplateInputProperties, SpecFileData, TypeInfo, TypesWriterMode } 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 decodeTypeInfo(rawInput: unknown): TypeInfo | null;
5
6
  export declare function decodeObjectTemplateInputProperties(rawInput: unknown): ObjectTemplateInputProperties | null;
6
7
  export declare function decodeEnumTemplateInput(rawInput: unknown): EnumTemplateInput | null;
@@ -21,6 +21,7 @@ export type Template = {
21
21
  exporterModuleSyntax: string;
22
22
  typesFileSyntax: string;
23
23
  enumSyntax: string;
24
+ oneOfSyntax: string;
24
25
  };
25
26
  export type LanguageConfig = {
26
27
  exporterModuleName: string;
@@ -35,6 +36,14 @@ export type FormatType = {
35
36
  default: string;
36
37
  [format: string]: string | undefined;
37
38
  };
39
+ export type ResolveReferenceData = {
40
+ typeInfo: TypeInfo;
41
+ referenceName: string;
42
+ sourceFile?: string;
43
+ };
44
+ export type GeneratedReferencedType = ResolveReferenceData & {
45
+ templateData: GeneratedType<TemplateInput>;
46
+ };
38
47
  export type SpecFileData = {
39
48
  info: SpecInfo;
40
49
  groupedTypes: GroupedTypes | null;
@@ -48,24 +57,31 @@ type GroupName = string;
48
57
  export type GroupedTypes = Record<GroupName, Types>;
49
58
  type TypeName = string;
50
59
  export type Types = Record<TypeName, TypeInfo>;
51
- export type TypeInfo = {
60
+ export type TypeInfo = TypeDescriptors & {
52
61
  required: string[] | null;
53
62
  type: TypeDataType | null;
54
63
  format: string | null;
55
64
  items: TypeInfo | null;
56
65
  properties: TypeProperties | null;
57
66
  $ref: string | null;
67
+ oneOf: TypeInfo[] | null;
58
68
  enum: string[] | number[] | null;
59
69
  };
60
70
  type PropertyName = string;
61
71
  export type TypeProperties = Record<PropertyName, TypeInfo>;
62
72
  export type TypeDataType = 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'object' | 'unknown';
63
- export type ObjectTemplateInput = {
73
+ export type TypeDescriptors = {
74
+ summary: string | null;
75
+ example: string | number | null;
76
+ description: string | null;
77
+ };
78
+ export type ObjectTemplateInput = TypeDescriptors & {
64
79
  typeName: string;
80
+ type: string;
65
81
  properties: ObjectTemplateInputProperties;
66
82
  };
67
83
  export type ObjectTemplateInputProperties = Record<PropertyName, ObjectTemplateInputProperty>;
68
- export type ObjectTemplateInputProperty = {
84
+ export type ObjectTemplateInputProperty = TypeDescriptors & {
69
85
  type: string;
70
86
  required: boolean;
71
87
  referenced: boolean;
@@ -86,22 +102,41 @@ export type ReferencedModule = {
86
102
  moduleRelativePath: string;
87
103
  referencedTypes: string[];
88
104
  };
89
- export type EnumTemplateInput = {
90
- enumName: string;
91
- enumType: string;
105
+ export type EnumTemplateInput = TypeDescriptors & {
106
+ typeName: string;
107
+ type: string;
92
108
  values: string[] | number[];
93
109
  };
110
+ export type OneOfTemplateInput = TypeDescriptors & {
111
+ typeName: string;
112
+ type: string;
113
+ compositions: OneOfTemplateInputComposition[];
114
+ };
115
+ export type OneOfTemplateInputComposition = {
116
+ dataType?: TypeDataType | null;
117
+ templateInput?: TemplateInput;
118
+ source: 'inline' | 'referenced';
119
+ referencedType?: string;
120
+ content?: string;
121
+ };
122
+ export type VariableTemplateInput = TypeDescriptors & {
123
+ typeName: string;
124
+ type: string;
125
+ composerType?: string;
126
+ };
127
+ export type TemplateInput = ObjectTemplateInput | EnumTemplateInput | OneOfTemplateInput | VariableTemplateInput;
94
128
  export type GenerationResult = {
95
129
  groupedTypes: GroupedTypesOutput;
96
130
  types: GeneratedTypes;
97
131
  };
98
- export type GeneratedType = {
132
+ export type GeneratedType<TemplateType extends TemplateInput> = {
99
133
  content: string;
100
134
  references: Set<string>;
101
135
  primitives: Set<string>;
136
+ templateInput: TemplateType;
102
137
  };
103
138
  export type GroupedTypesOutput = Record<GroupName, GeneratedTypes>;
104
- export type GeneratedTypes = Record<TypeName, GeneratedType>;
139
+ export type GeneratedTypes = Record<TypeName, GeneratedType<TemplateInput>>;
105
140
  export type TypeFilePath = {
106
141
  modulePath: string;
107
142
  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>;
@@ -7,7 +7,8 @@ export declare function getRequiredKeys(object: unknown): string[];
7
7
  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
+ export declare function toPascalCaseHelper(input: unknown): string | unknown;
10
11
  export declare function registerTemplateHelpers(): void;
11
- export declare function readNestedValue(json: JSONObject, keyPath: string[]): JSONObject;
12
+ export declare function readNestedValue(json: unknown, keyPath: string[]): JSONObject;
12
13
  export declare function generateRelativePath(fromPath: string, toPath: string): string;
13
14
  export declare function stripPrefix(value: string, prefix: string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-crafter",
3
- "version": "0.3.0",
3
+ "version": "0.5.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
  }