type-crafter 0.7.0 → 0.8.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 +105 -11
- package/dist/runtime.d.ts +3 -1
- package/dist/templates/typescript/allOf-syntax.hbs +19 -0
- package/dist/templates/typescript/oneOf-syntax.hbs +1 -1
- package/dist/templates/typescript-with-decoders/allOf-syntax.hbs +42 -0
- package/dist/templates/typescript-with-decoders/object-syntax.hbs +4 -1
- package/dist/templates/typescript-with-decoders/oneOf-syntax.hbs +8 -8
- package/dist/types/index.d.ts +15 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3558,6 +3558,7 @@ function decodeTypeInfo(rawInput) {
|
|
|
3558
3558
|
description: decodeString(rawInput.description),
|
|
3559
3559
|
example: decodeString(rawInput.example) ?? decodeNumber(rawInput.example),
|
|
3560
3560
|
oneOf: decodeArray(rawInput.oneOf, decodeTypeInfo),
|
|
3561
|
+
allOf: decodeArray(rawInput.allOf, decodeTypeInfo),
|
|
3561
3562
|
additionalProperties,
|
|
3562
3563
|
enum: _type === 'string'
|
|
3563
3564
|
? decodeArray(rawInput.enum, decodeString)
|
|
@@ -11982,6 +11983,7 @@ let exporterModuleSyntaxTemplate = null;
|
|
|
11982
11983
|
let typesFileSyntaxTemplate = null;
|
|
11983
11984
|
let oneOfSyntaxTemplate = null;
|
|
11984
11985
|
let enumSyntaxTemplate = null;
|
|
11986
|
+
let allOfSyntaxTemplate = null;
|
|
11985
11987
|
const cachedReferencedTypes = new Map();
|
|
11986
11988
|
let expectedOutputFiles = null;
|
|
11987
11989
|
function setConfig(newConfig) {
|
|
@@ -12009,6 +12011,7 @@ function compileTemplates() {
|
|
|
12009
12011
|
typesFileSyntaxTemplate = Handlebars.compile(config.template.typesFileSyntax);
|
|
12010
12012
|
enumSyntaxTemplate = Handlebars.compile(config.template.enumSyntax);
|
|
12011
12013
|
oneOfSyntaxTemplate = Handlebars.compile(config.template.oneOfSyntax);
|
|
12014
|
+
allOfSyntaxTemplate = Handlebars.compile(config.template.allOfSyntax);
|
|
12012
12015
|
}
|
|
12013
12016
|
function getObjectTemplate() {
|
|
12014
12017
|
if (objectSyntaxTemplate === null) {
|
|
@@ -12064,6 +12067,12 @@ function getInputFilePath(absolutePath = true) {
|
|
|
12064
12067
|
}
|
|
12065
12068
|
return absolutePath ? resolveFilePath(config$2.input) : config$2.input;
|
|
12066
12069
|
}
|
|
12070
|
+
function getAllOfTemplate() {
|
|
12071
|
+
if (allOfSyntaxTemplate === null) {
|
|
12072
|
+
throw new RuntimeError('AllOf template not compiled!');
|
|
12073
|
+
}
|
|
12074
|
+
return allOfSyntaxTemplate;
|
|
12075
|
+
}
|
|
12067
12076
|
var Runtime = {
|
|
12068
12077
|
getConfig,
|
|
12069
12078
|
setConfig,
|
|
@@ -12080,7 +12089,8 @@ var Runtime = {
|
|
|
12080
12089
|
getCachedReferencedTypes,
|
|
12081
12090
|
getCachedReferenceType,
|
|
12082
12091
|
cacheReferenceType,
|
|
12083
|
-
getInputFilePath
|
|
12092
|
+
getInputFilePath,
|
|
12093
|
+
getAllOfTemplate
|
|
12084
12094
|
};
|
|
12085
12095
|
|
|
12086
12096
|
const ALIAS = Symbol.for('yaml.alias');
|
|
@@ -19399,7 +19409,16 @@ function resolveFilePath(filePath, useCurrentDirectory = true) {
|
|
|
19399
19409
|
: path$1.join(useCurrentDirectory ? process.cwd() : getSourceFileDirectory(), filePath);
|
|
19400
19410
|
return path$1.resolve(normalizedPath);
|
|
19401
19411
|
}
|
|
19412
|
+
async function checkFileNameCase(filePath) {
|
|
19413
|
+
const directory = path$1.dirname(filePath);
|
|
19414
|
+
const fileName = path$1.basename(filePath);
|
|
19415
|
+
const files = await fs$1.readdir(directory);
|
|
19416
|
+
return files.includes(fileName);
|
|
19417
|
+
}
|
|
19402
19418
|
async function readFile(filePath, useCurrentWorkingDirectory = true) {
|
|
19419
|
+
if (!(await checkFileNameCase(filePath))) {
|
|
19420
|
+
throw new Error(`File not found: ${filePath}`);
|
|
19421
|
+
}
|
|
19403
19422
|
const data = await fs$1.readFile(resolveFilePath(filePath, useCurrentWorkingDirectory), 'utf-8');
|
|
19404
19423
|
return data;
|
|
19405
19424
|
}
|
|
@@ -19563,9 +19582,21 @@ function registerTemplateHelpers() {
|
|
|
19563
19582
|
Handlebars.registerHelper('toPascalCase', toPascalCaseHelper);
|
|
19564
19583
|
Handlebars.registerHelper('isNonEmptyArray', (value) => Array.isArray(value) && value.length === 0);
|
|
19565
19584
|
Handlebars.registerHelper('eq', (value1, value2) => value1 === value2);
|
|
19585
|
+
Handlebars.registerHelper('notEq', (value1, value2) => value1 !== value2);
|
|
19586
|
+
Handlebars.registerHelper('isEmptyObject', (value) => typeof value === 'object' && value !== null && Object.keys(value).length === 0);
|
|
19566
19587
|
Handlebars.registerHelper('jsonKey', refineJSONKey);
|
|
19567
19588
|
Handlebars.registerHelper('variableName', refineVariableName);
|
|
19568
19589
|
Handlebars.registerHelper('indexKey', refineIndexKey);
|
|
19590
|
+
Handlebars.registerHelper('not', (value) => {
|
|
19591
|
+
if (typeof value === 'boolean') {
|
|
19592
|
+
return !value;
|
|
19593
|
+
}
|
|
19594
|
+
});
|
|
19595
|
+
Handlebars.registerHelper('or', (value1, value2) => {
|
|
19596
|
+
if (typeof value1 === 'boolean' && typeof value2 === 'boolean') {
|
|
19597
|
+
return value1 || value2;
|
|
19598
|
+
}
|
|
19599
|
+
});
|
|
19569
19600
|
}
|
|
19570
19601
|
function readNestedValue(json, keyPath) {
|
|
19571
19602
|
if (!isJSON(json)) {
|
|
@@ -19689,6 +19720,7 @@ const placeholderTypeInfo = {
|
|
|
19689
19720
|
properties: null,
|
|
19690
19721
|
$ref: null,
|
|
19691
19722
|
oneOf: null,
|
|
19723
|
+
allOf: null,
|
|
19692
19724
|
enum: null,
|
|
19693
19725
|
additionalProperties: null,
|
|
19694
19726
|
summary: null,
|
|
@@ -19778,7 +19810,7 @@ async function generateObjectType(typeName, typeInfo, parentTypes) {
|
|
|
19778
19810
|
summary: typeInfo.summary,
|
|
19779
19811
|
properties: {}
|
|
19780
19812
|
};
|
|
19781
|
-
|
|
19813
|
+
const compositions = [];
|
|
19782
19814
|
let dynamicGeneratedType = '';
|
|
19783
19815
|
const primitives = [];
|
|
19784
19816
|
const references = [];
|
|
@@ -19821,8 +19853,9 @@ async function generateObjectType(typeName, typeInfo, parentTypes) {
|
|
|
19821
19853
|
composerType = arrayDataGenOutput.templateInput.composerType ?? null;
|
|
19822
19854
|
}
|
|
19823
19855
|
else if (propertyType === 'object') {
|
|
19824
|
-
recursivePropertyName = toPascalCase(propertyName);
|
|
19825
|
-
recursiveTypeGenOutput = await generateObjectType(recursivePropertyName, typeInfo.properties[propertyName], parentTypes);
|
|
19856
|
+
recursivePropertyName = typeName + toPascalCase(propertyName);
|
|
19857
|
+
const recursiveTypeGenOutput = await generateObjectType(recursivePropertyName, typeInfo.properties[propertyName], parentTypes);
|
|
19858
|
+
compositions.push(recursiveTypeGenOutput);
|
|
19826
19859
|
languageDataType = recursivePropertyName;
|
|
19827
19860
|
references.push(...recursiveTypeGenOutput.references);
|
|
19828
19861
|
primitives.push(...recursiveTypeGenOutput.primitives);
|
|
@@ -19858,7 +19891,9 @@ async function generateObjectType(typeName, typeInfo, parentTypes) {
|
|
|
19858
19891
|
}
|
|
19859
19892
|
const result = {
|
|
19860
19893
|
content: Runtime.getObjectTemplate()(templateInput) +
|
|
19861
|
-
(
|
|
19894
|
+
compositions.reduce((acc, curr) => {
|
|
19895
|
+
return acc + curr.content;
|
|
19896
|
+
}, '') +
|
|
19862
19897
|
dynamicGeneratedType,
|
|
19863
19898
|
primitives: new Set(primitives),
|
|
19864
19899
|
references: new Set(references),
|
|
@@ -19982,13 +20017,13 @@ async function generateOneOfTypes(typeName, typeInfo, parentTypes) {
|
|
|
19982
20017
|
for (let index = 0; index < typeInfo.oneOf.length; index++) {
|
|
19983
20018
|
const oneOfItem = typeInfo.oneOf[index];
|
|
19984
20019
|
if (oneOfItem.$ref !== null) {
|
|
19985
|
-
const referenceData = await
|
|
20020
|
+
const referenceData = await generateReferencedType(typeName + (index + 1), oneOfItem, parentTypes);
|
|
19986
20021
|
const composition = {
|
|
19987
20022
|
source: 'referenced',
|
|
19988
|
-
referencedType: referenceData.
|
|
20023
|
+
referencedType: referenceData.templateInput.typeName
|
|
19989
20024
|
};
|
|
19990
20025
|
templateInput.compositions.push(composition);
|
|
19991
|
-
result.references.add(referenceData.
|
|
20026
|
+
result.references.add(referenceData.templateInput.typeName);
|
|
19992
20027
|
}
|
|
19993
20028
|
else {
|
|
19994
20029
|
const generatedType = await generateType(typeName + (index + 1), oneOfItem, parentTypes);
|
|
@@ -20013,6 +20048,58 @@ async function generateOneOfTypes(typeName, typeInfo, parentTypes) {
|
|
|
20013
20048
|
result.content = Runtime.getOneOfTemplate()(templateInput);
|
|
20014
20049
|
return result;
|
|
20015
20050
|
}
|
|
20051
|
+
async function generateAllOfTypes(typeName, typeInfo, parentTypes) {
|
|
20052
|
+
if (typeInfo.allOf === null || typeInfo.allOf.length === 0) {
|
|
20053
|
+
throw new InvalidSpecFileError('Invalid allOf type for: ' + typeName);
|
|
20054
|
+
}
|
|
20055
|
+
const templateInput = {
|
|
20056
|
+
typeName,
|
|
20057
|
+
type: typeName,
|
|
20058
|
+
compositions: [],
|
|
20059
|
+
description: typeInfo.description,
|
|
20060
|
+
example: typeInfo.example,
|
|
20061
|
+
summary: typeInfo.summary
|
|
20062
|
+
};
|
|
20063
|
+
const result = {
|
|
20064
|
+
content: '',
|
|
20065
|
+
references: new Set(),
|
|
20066
|
+
primitives: new Set(),
|
|
20067
|
+
templateInput
|
|
20068
|
+
};
|
|
20069
|
+
for (let index = 0; index < typeInfo.allOf.length; index++) {
|
|
20070
|
+
const allOfItem = typeInfo.allOf[index];
|
|
20071
|
+
if (allOfItem.$ref !== null) {
|
|
20072
|
+
const referenceData = await generateReferencedType(typeName + 'AllOf' + index, allOfItem, parentTypes);
|
|
20073
|
+
const composition = {
|
|
20074
|
+
source: 'referenced',
|
|
20075
|
+
referencedType: referenceData.templateInput.typeName
|
|
20076
|
+
};
|
|
20077
|
+
templateInput.compositions.push(composition);
|
|
20078
|
+
result.references.add(referenceData.templateInput.typeName);
|
|
20079
|
+
}
|
|
20080
|
+
else {
|
|
20081
|
+
const generatedType = await generateType(typeName + (index + 1), allOfItem, parentTypes);
|
|
20082
|
+
if (generatedType === null) {
|
|
20083
|
+
throw new InvalidSpecFileError('Invalid allOf type for: ' + typeName);
|
|
20084
|
+
}
|
|
20085
|
+
const composition = {
|
|
20086
|
+
dataType: allOfItem.type,
|
|
20087
|
+
templateInput: generatedType.templateInput,
|
|
20088
|
+
source: 'inline',
|
|
20089
|
+
content: generatedType.content
|
|
20090
|
+
};
|
|
20091
|
+
templateInput.compositions.push(composition);
|
|
20092
|
+
for (const reference of generatedType.references.values()) {
|
|
20093
|
+
result.references.add(reference);
|
|
20094
|
+
}
|
|
20095
|
+
for (const primitive of generatedType.primitives.values()) {
|
|
20096
|
+
result.primitives.add(primitive);
|
|
20097
|
+
}
|
|
20098
|
+
}
|
|
20099
|
+
}
|
|
20100
|
+
result.content = Runtime.getAllOfTemplate()(templateInput);
|
|
20101
|
+
return result;
|
|
20102
|
+
}
|
|
20016
20103
|
function returnCyclicReference(typeName) {
|
|
20017
20104
|
const templateInput = {
|
|
20018
20105
|
typeName,
|
|
@@ -20043,6 +20130,9 @@ async function generateType(typeName, typeInfo, parentTypes) {
|
|
|
20043
20130
|
if (typeInfo.oneOf !== null) {
|
|
20044
20131
|
return await generateOneOfTypes(typeName, typeInfo, parentTypes);
|
|
20045
20132
|
}
|
|
20133
|
+
if (typeInfo.allOf !== null) {
|
|
20134
|
+
return await generateAllOfTypes(typeName, typeInfo, parentTypes);
|
|
20135
|
+
}
|
|
20046
20136
|
if (typeInfo.type === 'array') {
|
|
20047
20137
|
return await generateArrayType(typeName, typeInfo, parentTypes);
|
|
20048
20138
|
}
|
|
@@ -20292,6 +20382,7 @@ async function config$1(inputFilePath, outputDirectory, typesWriterMode, grouped
|
|
|
20292
20382
|
const typesFileSyntax = await readFile(directoryPrefix + 'templates/typescript/types-file-syntax.hbs', devMode);
|
|
20293
20383
|
const enumSyntax = await readFile(directoryPrefix + 'templates/typescript/enum-syntax.hbs', devMode);
|
|
20294
20384
|
const oneOfSyntax = await readFile(directoryPrefix + 'templates/typescript/oneOf-syntax.hbs', devMode);
|
|
20385
|
+
const allOfSyntax = await readFile(directoryPrefix + 'templates/typescript/allOf-syntax.hbs', devMode);
|
|
20295
20386
|
const config = {
|
|
20296
20387
|
input: inputFilePath,
|
|
20297
20388
|
output: {
|
|
@@ -20308,7 +20399,8 @@ async function config$1(inputFilePath, outputDirectory, typesWriterMode, grouped
|
|
|
20308
20399
|
exporterModuleSyntax,
|
|
20309
20400
|
typesFileSyntax,
|
|
20310
20401
|
enumSyntax,
|
|
20311
|
-
oneOfSyntax
|
|
20402
|
+
oneOfSyntax,
|
|
20403
|
+
allOfSyntax
|
|
20312
20404
|
},
|
|
20313
20405
|
language: {
|
|
20314
20406
|
exporterModuleName: 'index',
|
|
@@ -20334,6 +20426,7 @@ async function config(inputFilePath, outputDirectory, typesWriterMode, groupedTy
|
|
|
20334
20426
|
const typesFileSyntax = await readFile(directoryPrefix + 'templates/typescript-with-decoders/types-file-syntax.hbs', devMode);
|
|
20335
20427
|
const enumSyntax = await readFile(directoryPrefix + 'templates/typescript-with-decoders/enum-syntax.hbs', devMode);
|
|
20336
20428
|
const oneOfSyntax = await readFile(directoryPrefix + 'templates/typescript-with-decoders/oneOf-syntax.hbs', devMode);
|
|
20429
|
+
const allOfSyntax = await readFile(directoryPrefix + 'templates/typescript-with-decoders/allOf-syntax.hbs', devMode);
|
|
20337
20430
|
const config = {
|
|
20338
20431
|
input: inputFilePath,
|
|
20339
20432
|
output: {
|
|
@@ -20350,7 +20443,8 @@ async function config(inputFilePath, outputDirectory, typesWriterMode, groupedTy
|
|
|
20350
20443
|
exporterModuleSyntax,
|
|
20351
20444
|
typesFileSyntax,
|
|
20352
20445
|
enumSyntax,
|
|
20353
|
-
oneOfSyntax
|
|
20446
|
+
oneOfSyntax,
|
|
20447
|
+
allOfSyntax
|
|
20354
20448
|
},
|
|
20355
20449
|
language: {
|
|
20356
20450
|
exporterModuleName: 'index',
|
|
@@ -20414,7 +20508,7 @@ async function runner(language, inputFilePath, outputDirectory, _typesWriterMode
|
|
|
20414
20508
|
}
|
|
20415
20509
|
}
|
|
20416
20510
|
greeting();
|
|
20417
|
-
const program = new Command().version('0.
|
|
20511
|
+
const program = new Command().version('0.8.1');
|
|
20418
20512
|
program
|
|
20419
20513
|
.command('generate')
|
|
20420
20514
|
.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, EnumTemplateInput, ExporterModuleTemplateInput, GeneratedReferencedType, ObjectTemplateInput, OneOfTemplateInput, SpecFileData, TypeFilePath, TypesFileTemplateInput } from '$types';
|
|
1
|
+
import type { AllOfTemplateInput, 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;
|
|
@@ -15,6 +15,7 @@ declare function getCachedReferencedTypes(): Map<string, GeneratedReferencedType
|
|
|
15
15
|
declare function getCachedReferenceType(referencePath: string): GeneratedReferencedType | null;
|
|
16
16
|
declare function cacheReferenceType(referencePath: string, generatedData: GeneratedReferencedType): void;
|
|
17
17
|
declare function getInputFilePath(absolutePath?: boolean): string;
|
|
18
|
+
declare function getAllOfTemplate(): HandlebarsTemplateDelegate<AllOfTemplateInput>;
|
|
18
19
|
declare const _default: {
|
|
19
20
|
getConfig: typeof getConfig;
|
|
20
21
|
setConfig: typeof setConfig;
|
|
@@ -32,5 +33,6 @@ declare const _default: {
|
|
|
32
33
|
getCachedReferenceType: typeof getCachedReferenceType;
|
|
33
34
|
cacheReferenceType: typeof cacheReferenceType;
|
|
34
35
|
getInputFilePath: typeof getInputFilePath;
|
|
36
|
+
getAllOfTemplate: typeof getAllOfTemplate;
|
|
35
37
|
};
|
|
36
38
|
export default _default;
|
|
@@ -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
|
+
{{this.templateInput.typeName}}
|
|
7
|
+
{{else if (eq this.dataType 'object')}}
|
|
8
|
+
{{this.templateInput.typeName}}
|
|
9
|
+
{{else}}
|
|
10
|
+
{{this.templateInput.type}}
|
|
11
|
+
{{/if}}
|
|
12
|
+
{{/if}} {{#unless @last}} & {{/unless}}
|
|
13
|
+
{{/each}};
|
|
14
|
+
|
|
15
|
+
{{#each compositions}}
|
|
16
|
+
{{#if (eq this.dataType 'object')}}
|
|
17
|
+
{{{this.content}}}
|
|
18
|
+
{{/if}}
|
|
19
|
+
{{/each}}
|
|
@@ -4,7 +4,7 @@ export type {{typeName}} =
|
|
|
4
4
|
{{else if (eq this.source 'inline')}}
|
|
5
5
|
{{#if this.templateInput.values}}
|
|
6
6
|
{{#each this.templateInput.values}}
|
|
7
|
-
{{#unless @first}}|{{/unless}} {{#if (eq ../this.templateInput.
|
|
7
|
+
{{#unless @first}}|{{/unless}} {{#if (eq ../this.templateInput.type 'string') }}'{{/if}}{{{this}}}{{#if (eq ../this.templateInput/type 'string')}}'{{/if}}
|
|
8
8
|
{{/each}}
|
|
9
9
|
{{else if (eq this.dataType 'object')}}
|
|
10
10
|
{
|
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
{{this.templateInput.typeName}}
|
|
7
|
+
{{else if (eq this.dataType 'object')}}
|
|
8
|
+
{{this.templateInput.typeName}}
|
|
9
|
+
{{else}}
|
|
10
|
+
{{this.templateInput.type}}
|
|
11
|
+
{{/if}}
|
|
12
|
+
{{/if}} {{#unless @last}} & {{/unless}}
|
|
13
|
+
{{/each}};
|
|
14
|
+
|
|
15
|
+
{{#each compositions}}
|
|
16
|
+
{{#if (eq this.dataType 'object')}}
|
|
17
|
+
{{{this.content}}}
|
|
18
|
+
{{/if}}
|
|
19
|
+
{{/each}}
|
|
20
|
+
|
|
21
|
+
export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
|
|
22
|
+
{{#each compositions}}
|
|
23
|
+
{{#if (eq this.source 'referenced')}}
|
|
24
|
+
const decoded{{referencedType}} = decode{{referencedType}}(rawInput);
|
|
25
|
+
{{else if (eq this.source 'inline')}}
|
|
26
|
+
const decoded{{toPascalCase this.templateInput.typeName}} = decode{{this.templateInput.typeName}}(rawInput);
|
|
27
|
+
{{/if}}
|
|
28
|
+
{{/each}}
|
|
29
|
+
|
|
30
|
+
if (
|
|
31
|
+
{{#each compositions}}decoded{{#if (eq this.source 'referenced')}}{{referencedType}}{{else}}{{toPascalCase this.templateInput.typeName}}{{/if}} !== null {{#unless @last}} && {{/unless}} {{/each}}
|
|
32
|
+
) {
|
|
33
|
+
return {
|
|
34
|
+
{{#each compositions}}
|
|
35
|
+
{{#if (eq this.source 'referenced')}}...decoded{{referencedType}},
|
|
36
|
+
{{else}}...decoded{{toPascalCase this.templateInput.typeName}},
|
|
37
|
+
{{/if}}
|
|
38
|
+
{{/each}}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* @example {{{example}}}
|
|
8
8
|
{{/if}}
|
|
9
9
|
*/
|
|
10
|
-
export type {{typeName}} = {
|
|
10
|
+
export type {{typeName}} = {{#if (not (isEmptyObject properties))}} {
|
|
11
11
|
{{#each properties}}
|
|
12
12
|
/**
|
|
13
13
|
{{#if this.description}}
|
|
@@ -25,6 +25,9 @@ export type {{typeName}} = {
|
|
|
25
25
|
[keys: {{jsonKey additionalProperties.keyType}}]: {{additionalProperties.valueType}};
|
|
26
26
|
{{/if}}
|
|
27
27
|
};
|
|
28
|
+
{{else if (or (notEq length additionalProperties 0) (not (isEmptyObject additionalProperties)))}}
|
|
29
|
+
Record<{{jsonKey additionalProperties.keyType}}, {{additionalProperties.valueType}}>;
|
|
30
|
+
{{/if}}
|
|
28
31
|
|
|
29
32
|
export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
|
|
30
33
|
if (isJSON(rawInput)) {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
export type {{typeName}} =
|
|
2
2
|
{{#each compositions}}
|
|
3
|
-
| {{#if (eq this.source 'referenced')}}C{{referencedType}}
|
|
3
|
+
| {{#if (eq this.source 'referenced')}}C{{../typeName}}{{referencedType}}
|
|
4
4
|
{{else if (eq this.source 'inline')}}
|
|
5
5
|
{{#if this.templateInput.values}}
|
|
6
6
|
{{this.templateInput.typeName}}
|
|
7
7
|
{{else if (eq this.dataType 'object')}}
|
|
8
|
-
C{{this.templateInput.typeName}}
|
|
8
|
+
C{{typeName}}{{this.templateInput.typeName}}
|
|
9
9
|
{{else}}
|
|
10
10
|
{{this.templateInput.type}}
|
|
11
11
|
{{/if}}
|
|
@@ -16,7 +16,7 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
|
|
|
16
16
|
const result: {{typeName}} | null =
|
|
17
17
|
{{#each compositions}}
|
|
18
18
|
{{#if (eq this.source 'referenced')}}
|
|
19
|
-
decodeC{{referencedType}}(rawInput)
|
|
19
|
+
decodeC{{../typeName}}{{referencedType}}(rawInput)
|
|
20
20
|
{{else if (eq this.dataType 'object')}}
|
|
21
21
|
decodeC{{this.templateInput.typeName}}(rawInput)
|
|
22
22
|
{{else if (eq this.dataType 'array')}}
|
|
@@ -36,26 +36,26 @@ export function decode{{typeName}}(rawInput: unknown): {{typeName}} | null {
|
|
|
36
36
|
{{#if this.templateInput.values}}
|
|
37
37
|
{{{this.content}}}
|
|
38
38
|
{{else if (eq this.source 'referenced')}}
|
|
39
|
-
export class C{{referencedType}} {
|
|
39
|
+
export class C{{../typeName}}{{referencedType}} {
|
|
40
40
|
data: {{referencedType}};
|
|
41
41
|
constructor(data: {{referencedType}}) {
|
|
42
42
|
this.data = data;
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
export function decodeC{{referencedType}}(rawInput: unknown): C{{referencedType}} | null {
|
|
46
|
+
export function decodeC{{../typeName}}{{referencedType}}(rawInput: unknown): C{{../typeName}}{{referencedType}} | null {
|
|
47
47
|
const result = decode{{referencedType}}(rawInput);
|
|
48
48
|
if (result === null) {
|
|
49
49
|
return null;
|
|
50
50
|
}
|
|
51
|
-
return new C{{referencedType}}(result);
|
|
51
|
+
return new C{{../typeName}}{{referencedType}}(result);
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
{{else if (eq this.dataType 'object')}}
|
|
55
55
|
|
|
56
56
|
{{{this.content}}}
|
|
57
57
|
|
|
58
|
-
export class C{{this.templateInput.typeName}} {
|
|
58
|
+
export class C{{../typeName}}{{this.templateInput.typeName}} {
|
|
59
59
|
data: {{this.templateInput.typeName}};
|
|
60
60
|
constructor(data: {{this.templateInput.typeName}}) {
|
|
61
61
|
this.data = data;
|
|
@@ -67,7 +67,7 @@ export function decodeC{{this.templateInput.typeName}}(rawInput: unknown) {
|
|
|
67
67
|
if (result === null) {
|
|
68
68
|
return null;
|
|
69
69
|
}
|
|
70
|
-
return new C{{this.templateInput.typeName}}(result);
|
|
70
|
+
return new C{{../typeName}}{{this.templateInput.typeName}}(result);
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
{{/if}}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -22,6 +22,7 @@ export type Template = {
|
|
|
22
22
|
typesFileSyntax: string;
|
|
23
23
|
enumSyntax: string;
|
|
24
24
|
oneOfSyntax: string;
|
|
25
|
+
allOfSyntax: string;
|
|
25
26
|
};
|
|
26
27
|
export type LanguageConfig = {
|
|
27
28
|
exporterModuleName: string;
|
|
@@ -78,6 +79,7 @@ export type TypeInfo = TypeDescriptors & {
|
|
|
78
79
|
properties: TypeProperties | null;
|
|
79
80
|
$ref: string | null;
|
|
80
81
|
oneOf: TypeInfo[] | null;
|
|
82
|
+
allOf: TypeInfo[] | null;
|
|
81
83
|
enum: string[] | number[] | null;
|
|
82
84
|
additionalProperties: AdditionalProperties | null;
|
|
83
85
|
};
|
|
@@ -144,12 +146,24 @@ export type OneOfTemplateInputComposition = {
|
|
|
144
146
|
referencedType?: string;
|
|
145
147
|
content?: string;
|
|
146
148
|
};
|
|
149
|
+
export type AllOfTemplateInput = TypeDescriptors & {
|
|
150
|
+
typeName: string;
|
|
151
|
+
type: string;
|
|
152
|
+
compositions: AllOfTemplateInputComposition[];
|
|
153
|
+
};
|
|
154
|
+
export type AllOfTemplateInputComposition = {
|
|
155
|
+
dataType?: TypeDataType | null;
|
|
156
|
+
templateInput?: TemplateInput;
|
|
157
|
+
source: 'inline' | 'referenced';
|
|
158
|
+
referencedType?: string;
|
|
159
|
+
content?: string;
|
|
160
|
+
};
|
|
147
161
|
export type VariableTemplateInput = TypeDescriptors & {
|
|
148
162
|
typeName: string;
|
|
149
163
|
type: string;
|
|
150
164
|
composerType?: string;
|
|
151
165
|
};
|
|
152
|
-
export type TemplateInput = ObjectTemplateInput | EnumTemplateInput | OneOfTemplateInput | VariableTemplateInput;
|
|
166
|
+
export type TemplateInput = ObjectTemplateInput | EnumTemplateInput | OneOfTemplateInput | AllOfTemplateInput | VariableTemplateInput;
|
|
153
167
|
export type GenerationResult = {
|
|
154
168
|
groupedTypes: GroupedTypesOutput;
|
|
155
169
|
types: GeneratedTypes;
|