type-crafter 0.2.1 → 0.3.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
|
@@ -10665,6 +10665,17 @@ function __decodeString(value) {
|
|
|
10665
10665
|
}
|
|
10666
10666
|
return errorDelimiter;
|
|
10667
10667
|
}
|
|
10668
|
+
/**
|
|
10669
|
+
* @description A function which attempts to safely cast any value to a valid number
|
|
10670
|
+
* @param value Value with unknown type
|
|
10671
|
+
* @returns Attempts to convert the passed value to a valid number, returns null in case cast fails
|
|
10672
|
+
*/
|
|
10673
|
+
function decodeNumber(value) {
|
|
10674
|
+
if (typeof value === 'number') {
|
|
10675
|
+
return value;
|
|
10676
|
+
}
|
|
10677
|
+
return null;
|
|
10678
|
+
}
|
|
10668
10679
|
/**
|
|
10669
10680
|
* @description A function which attempts to safely cast any value to a valid boolean
|
|
10670
10681
|
* @param value Value with unknown type
|
|
@@ -10809,13 +10820,19 @@ function decodeTypes(rawInput) {
|
|
|
10809
10820
|
}
|
|
10810
10821
|
function decodeTypeInfo(rawInput) {
|
|
10811
10822
|
if (isJSON(rawInput)) {
|
|
10823
|
+
const _type = decodeTypeDataType(rawInput.type);
|
|
10812
10824
|
const result = {
|
|
10813
|
-
type:
|
|
10825
|
+
type: _type,
|
|
10814
10826
|
required: decodeArray(rawInput.required, decodeString),
|
|
10815
10827
|
properties: decodeTypeProperties(rawInput.properties),
|
|
10816
10828
|
items: decodeTypeInfo(rawInput.items),
|
|
10817
10829
|
format: decodeString(rawInput.format),
|
|
10818
|
-
$ref: decodeString(rawInput.$ref)
|
|
10830
|
+
$ref: decodeString(rawInput.$ref),
|
|
10831
|
+
enum: _type === 'string'
|
|
10832
|
+
? decodeArray(rawInput.enum, decodeString)
|
|
10833
|
+
: _type === 'number'
|
|
10834
|
+
? decodeArray(rawInput.enum, decodeNumber)
|
|
10835
|
+
: null
|
|
10819
10836
|
};
|
|
10820
10837
|
return result;
|
|
10821
10838
|
}
|
|
@@ -19204,6 +19221,7 @@ let specFileData = null;
|
|
|
19204
19221
|
let objectSyntaxTemplate = null;
|
|
19205
19222
|
let exporterModuleSyntaxTemplate = null;
|
|
19206
19223
|
let typesFileSyntaxTemplate = null;
|
|
19224
|
+
let enumSyntaxTemplate = null;
|
|
19207
19225
|
let expectedOutputFiles = null;
|
|
19208
19226
|
function setConfig(newConfig) {
|
|
19209
19227
|
config$2 = newConfig;
|
|
@@ -19228,6 +19246,7 @@ function compileTemplates() {
|
|
|
19228
19246
|
objectSyntaxTemplate = Handlebars.compile(config.template.objectSyntax);
|
|
19229
19247
|
exporterModuleSyntaxTemplate = Handlebars.compile(config.template.exporterModuleSyntax);
|
|
19230
19248
|
typesFileSyntaxTemplate = Handlebars.compile(config.template.typesFileSyntax);
|
|
19249
|
+
enumSyntaxTemplate = Handlebars.compile(config.template.enumSyntax);
|
|
19231
19250
|
}
|
|
19232
19251
|
function getObjectTemplate() {
|
|
19233
19252
|
if (objectSyntaxTemplate === null) {
|
|
@@ -19247,6 +19266,12 @@ function getTypesFileTemplate() {
|
|
|
19247
19266
|
}
|
|
19248
19267
|
return typesFileSyntaxTemplate;
|
|
19249
19268
|
}
|
|
19269
|
+
function getEnumTemplate() {
|
|
19270
|
+
if (enumSyntaxTemplate === null) {
|
|
19271
|
+
throw new RuntimeError('Enum template not compiled!');
|
|
19272
|
+
}
|
|
19273
|
+
return enumSyntaxTemplate;
|
|
19274
|
+
}
|
|
19250
19275
|
function setExpectedOutputFiles(newExpectedOutputFiles) {
|
|
19251
19276
|
expectedOutputFiles = newExpectedOutputFiles;
|
|
19252
19277
|
}
|
|
@@ -19265,6 +19290,7 @@ var Runtime = {
|
|
|
19265
19290
|
getObjectTemplate,
|
|
19266
19291
|
getExporterModuleTemplate,
|
|
19267
19292
|
getTypesFileTemplate,
|
|
19293
|
+
getEnumTemplate,
|
|
19268
19294
|
getExpectedOutputFiles,
|
|
19269
19295
|
compileTemplates
|
|
19270
19296
|
};
|
|
@@ -19521,7 +19547,7 @@ function getLanguageDataType(dataType, format, items) {
|
|
|
19521
19547
|
}
|
|
19522
19548
|
return dataType;
|
|
19523
19549
|
}
|
|
19524
|
-
function
|
|
19550
|
+
function generateObjectType(typeName, typeInfo) {
|
|
19525
19551
|
const result = {
|
|
19526
19552
|
content: '',
|
|
19527
19553
|
references: new Set(),
|
|
@@ -19532,11 +19558,14 @@ function generateType(typeName, typeInfo, groupedTypes = false) {
|
|
|
19532
19558
|
properties: {}
|
|
19533
19559
|
};
|
|
19534
19560
|
let recursiveTypeGenOutput = null;
|
|
19561
|
+
let dynamicGeneratedType = '';
|
|
19535
19562
|
for (const propertyName in typeInfo.properties) {
|
|
19536
|
-
const
|
|
19537
|
-
const
|
|
19538
|
-
const
|
|
19539
|
-
const
|
|
19563
|
+
const propertyDetails = typeInfo.properties[propertyName];
|
|
19564
|
+
const propertyType = propertyDetails.type;
|
|
19565
|
+
const propertyFormat = propertyDetails.format;
|
|
19566
|
+
const propertyItems = propertyDetails.items ?? null;
|
|
19567
|
+
const reference = propertyDetails.$ref ?? null;
|
|
19568
|
+
const enumValues = propertyDetails.enum ?? null;
|
|
19540
19569
|
// Throwing error in case neither property type nor reference to a different type is present
|
|
19541
19570
|
if (propertyType === null && reference === null) {
|
|
19542
19571
|
throw new InvalidSpecFileError('Invalid property type for: ' + typeName + '.' + propertyName);
|
|
@@ -19553,9 +19582,14 @@ function generateType(typeName, typeInfo, groupedTypes = false) {
|
|
|
19553
19582
|
isReferenced = true;
|
|
19554
19583
|
result.references.add(recursivePropertyName);
|
|
19555
19584
|
}
|
|
19585
|
+
else if (enumValues !== null) {
|
|
19586
|
+
const enumName = toPascalCase(propertyName) + 'Enum';
|
|
19587
|
+
dynamicGeneratedType = generateEnumType(enumName, propertyDetails).content;
|
|
19588
|
+
languageDataType = enumName;
|
|
19589
|
+
}
|
|
19556
19590
|
else if (propertyType === 'object') {
|
|
19557
19591
|
recursivePropertyName = toPascalCase(propertyName);
|
|
19558
|
-
recursiveTypeGenOutput =
|
|
19592
|
+
recursiveTypeGenOutput = generateObjectType(recursivePropertyName, typeInfo.properties[propertyName]);
|
|
19559
19593
|
languageDataType = recursivePropertyName;
|
|
19560
19594
|
for (const reference of recursiveTypeGenOutput.references.values()) {
|
|
19561
19595
|
result.references.add(reference);
|
|
@@ -19588,15 +19622,40 @@ function generateType(typeName, typeInfo, groupedTypes = false) {
|
|
|
19588
19622
|
};
|
|
19589
19623
|
}
|
|
19590
19624
|
result.content =
|
|
19591
|
-
Runtime.getObjectTemplate()(templateInput) +
|
|
19625
|
+
Runtime.getObjectTemplate()(templateInput) +
|
|
19626
|
+
(recursiveTypeGenOutput?.content ?? '') +
|
|
19627
|
+
dynamicGeneratedType;
|
|
19628
|
+
return result;
|
|
19629
|
+
}
|
|
19630
|
+
function generateEnumType(typeName, typeInfo) {
|
|
19631
|
+
const result = {
|
|
19632
|
+
content: '',
|
|
19633
|
+
references: new Set(),
|
|
19634
|
+
primitives: new Set()
|
|
19635
|
+
};
|
|
19636
|
+
if (typeInfo.enum === null || typeInfo.enum.length === 0 || typeInfo.type === null) {
|
|
19637
|
+
throw new InvalidSpecFileError('Invalid enum type for: ' + typeName);
|
|
19638
|
+
}
|
|
19639
|
+
const templateInput = {
|
|
19640
|
+
enumName: typeName,
|
|
19641
|
+
enumType: typeInfo.type,
|
|
19642
|
+
values: typeInfo.enum
|
|
19643
|
+
};
|
|
19644
|
+
result.content = Runtime.getEnumTemplate()(templateInput);
|
|
19592
19645
|
return result;
|
|
19593
19646
|
}
|
|
19594
|
-
function generateTypes(types
|
|
19647
|
+
function generateTypes(types) {
|
|
19595
19648
|
const result = {};
|
|
19596
19649
|
for (const type in types) {
|
|
19597
19650
|
const typeInfo = types[type];
|
|
19598
|
-
const genType =
|
|
19599
|
-
|
|
19651
|
+
const genType = typeInfo.type === 'object'
|
|
19652
|
+
? generateObjectType(type, typeInfo)
|
|
19653
|
+
: typeInfo.enum !== null
|
|
19654
|
+
? generateEnumType(type, typeInfo)
|
|
19655
|
+
: null;
|
|
19656
|
+
if (genType !== null) {
|
|
19657
|
+
result[type] = genType;
|
|
19658
|
+
}
|
|
19600
19659
|
}
|
|
19601
19660
|
return result;
|
|
19602
19661
|
}
|
|
@@ -19612,7 +19671,7 @@ function generator(specFileData) {
|
|
|
19612
19671
|
// generating grouped types
|
|
19613
19672
|
const groupedTypes = {};
|
|
19614
19673
|
for (const groupName in specFileData.groupedTypes) {
|
|
19615
|
-
groupedTypes[groupName] = generateTypes(specFileData.groupedTypes[groupName]
|
|
19674
|
+
groupedTypes[groupName] = generateTypes(specFileData.groupedTypes[groupName]);
|
|
19616
19675
|
}
|
|
19617
19676
|
result.groupedTypes = groupedTypes;
|
|
19618
19677
|
return result;
|
|
@@ -19762,6 +19821,7 @@ async function config$1(inputFilePath, outputDirectory, typesWriterMode, grouped
|
|
|
19762
19821
|
const objectSyntax = await readFile(directoryPrefix + 'templates/typescript/object-syntax.hbs', devMode);
|
|
19763
19822
|
const exporterModuleSyntax = await readFile(directoryPrefix + 'templates/typescript/exporter-module-syntax.hbs', devMode);
|
|
19764
19823
|
const typesFileSyntax = await readFile(directoryPrefix + 'templates/typescript/types-file-syntax.hbs', devMode);
|
|
19824
|
+
const enumSyntax = await readFile(directoryPrefix + 'templates/typescript/enum-syntax.hbs', devMode);
|
|
19765
19825
|
const config = {
|
|
19766
19826
|
input: inputFilePath,
|
|
19767
19827
|
output: {
|
|
@@ -19776,7 +19836,8 @@ async function config$1(inputFilePath, outputDirectory, typesWriterMode, grouped
|
|
|
19776
19836
|
template: {
|
|
19777
19837
|
objectSyntax,
|
|
19778
19838
|
exporterModuleSyntax,
|
|
19779
|
-
typesFileSyntax
|
|
19839
|
+
typesFileSyntax,
|
|
19840
|
+
enumSyntax
|
|
19780
19841
|
},
|
|
19781
19842
|
language: {
|
|
19782
19843
|
exporterModuleName: 'index',
|
|
@@ -19800,6 +19861,7 @@ async function config(inputFilePath, outputDirectory, typesWriterMode, groupedTy
|
|
|
19800
19861
|
const objectSyntax = await readFile(directoryPrefix + 'templates/typescript-with-decoders/object-syntax.hbs', devMode);
|
|
19801
19862
|
const exporterModuleSyntax = await readFile(directoryPrefix + 'templates/typescript/exporter-module-syntax.hbs', devMode);
|
|
19802
19863
|
const typesFileSyntax = await readFile(directoryPrefix + 'templates/typescript-with-decoders/types-file-syntax.hbs', devMode);
|
|
19864
|
+
const enumSyntax = await readFile(directoryPrefix + 'templates/typescript-with-decoders/enum-syntax.hbs', devMode);
|
|
19803
19865
|
const config = {
|
|
19804
19866
|
input: inputFilePath,
|
|
19805
19867
|
output: {
|
|
@@ -19814,7 +19876,8 @@ async function config(inputFilePath, outputDirectory, typesWriterMode, groupedTy
|
|
|
19814
19876
|
template: {
|
|
19815
19877
|
objectSyntax,
|
|
19816
19878
|
exporterModuleSyntax,
|
|
19817
|
-
typesFileSyntax
|
|
19879
|
+
typesFileSyntax,
|
|
19880
|
+
enumSyntax
|
|
19818
19881
|
},
|
|
19819
19882
|
language: {
|
|
19820
19883
|
exporterModuleName: 'index',
|
|
@@ -19879,7 +19942,7 @@ async function runner(language, inputFilePath, outputDirectory, _typesWriterMode
|
|
|
19879
19942
|
}
|
|
19880
19943
|
}
|
|
19881
19944
|
greeting();
|
|
19882
|
-
const program = new Command().version('0.
|
|
19945
|
+
const program = new Command().version('0.3.0');
|
|
19883
19946
|
program
|
|
19884
19947
|
.command('generate')
|
|
19885
19948
|
.description('Generate types for your language from a type spec file')
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Configuration, ExporterModuleTemplateInput, ObjectTemplateInput, SpecFileData, TypeFilePath, TypesFileTemplateInput } from '$types';
|
|
1
|
+
import type { Configuration, EnumTemplateInput, ExporterModuleTemplateInput, ObjectTemplateInput, 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;
|
|
@@ -7,6 +7,7 @@ declare function compileTemplates(): void;
|
|
|
7
7
|
declare function getObjectTemplate(): HandlebarsTemplateDelegate<ObjectTemplateInput>;
|
|
8
8
|
declare function getExporterModuleTemplate(): HandlebarsTemplateDelegate<ExporterModuleTemplateInput>;
|
|
9
9
|
declare function getTypesFileTemplate(): HandlebarsTemplateDelegate<TypesFileTemplateInput>;
|
|
10
|
+
declare function getEnumTemplate(): HandlebarsTemplateDelegate<EnumTemplateInput>;
|
|
10
11
|
declare function setExpectedOutputFiles(newExpectedOutputFiles: Map<string, TypeFilePath>): void;
|
|
11
12
|
declare function getExpectedOutputFiles(): Map<string, TypeFilePath>;
|
|
12
13
|
declare const _default: {
|
|
@@ -18,6 +19,7 @@ declare const _default: {
|
|
|
18
19
|
getObjectTemplate: typeof getObjectTemplate;
|
|
19
20
|
getExporterModuleTemplate: typeof getExporterModuleTemplate;
|
|
20
21
|
getTypesFileTemplate: typeof getTypesFileTemplate;
|
|
22
|
+
getEnumTemplate: typeof getEnumTemplate;
|
|
21
23
|
getExpectedOutputFiles: typeof getExpectedOutputFiles;
|
|
22
24
|
compileTemplates: typeof compileTemplates;
|
|
23
25
|
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type {{enumName}} =
|
|
2
|
+
{{#each values}}
|
|
3
|
+
| {{#if (eq ../enumType 'string') }}'{{/if}}{{{this}}}{{#if (eq ../enumType 'string')}}'{{/if}}
|
|
4
|
+
{{/each}};
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export function decode{{enumName}}(rawInput: unknown): {{enumName}} | null {
|
|
8
|
+
switch (rawInput) {
|
|
9
|
+
{{#each values}}
|
|
10
|
+
case {{#if (eq ../enumType 'string') }}'{{/if}}{{this}}{{#if (eq ../enumType 'string')}}'{{/if}}:
|
|
11
|
+
{{/each}}
|
|
12
|
+
return rawInput;
|
|
13
|
+
}
|
|
14
|
+
return null;
|
|
15
|
+
}
|
package/dist/types/decoders.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { GroupedTypesWriterMode, ObjectTemplateInputProperties, SpecFileData, TypesWriterMode } from '.';
|
|
1
|
+
import type { EnumTemplateInput, GroupedTypesWriterMode, ObjectTemplateInputProperties, SpecFileData, 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
5
|
export declare function decodeObjectTemplateInputProperties(rawInput: unknown): ObjectTemplateInputProperties | null;
|
|
6
|
+
export declare function decodeEnumTemplateInput(rawInput: unknown): EnumTemplateInput | null;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ export type Template = {
|
|
|
20
20
|
objectSyntax: string;
|
|
21
21
|
exporterModuleSyntax: string;
|
|
22
22
|
typesFileSyntax: string;
|
|
23
|
+
enumSyntax: string;
|
|
23
24
|
};
|
|
24
25
|
export type LanguageConfig = {
|
|
25
26
|
exporterModuleName: string;
|
|
@@ -54,6 +55,7 @@ export type TypeInfo = {
|
|
|
54
55
|
items: TypeInfo | null;
|
|
55
56
|
properties: TypeProperties | null;
|
|
56
57
|
$ref: string | null;
|
|
58
|
+
enum: string[] | number[] | null;
|
|
57
59
|
};
|
|
58
60
|
type PropertyName = string;
|
|
59
61
|
export type TypeProperties = Record<PropertyName, TypeInfo>;
|
|
@@ -84,6 +86,11 @@ export type ReferencedModule = {
|
|
|
84
86
|
moduleRelativePath: string;
|
|
85
87
|
referencedTypes: string[];
|
|
86
88
|
};
|
|
89
|
+
export type EnumTemplateInput = {
|
|
90
|
+
enumName: string;
|
|
91
|
+
enumType: string;
|
|
92
|
+
values: string[] | number[];
|
|
93
|
+
};
|
|
87
94
|
export type GenerationResult = {
|
|
88
95
|
groupedTypes: GroupedTypesOutput;
|
|
89
96
|
types: GeneratedTypes;
|