type-crafter 0.7.0 → 0.8.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
@@ -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');
@@ -19689,6 +19699,7 @@ const placeholderTypeInfo = {
19689
19699
  properties: null,
19690
19700
  $ref: null,
19691
19701
  oneOf: null,
19702
+ allOf: null,
19692
19703
  enum: null,
19693
19704
  additionalProperties: null,
19694
19705
  summary: null,
@@ -20013,6 +20024,58 @@ async function generateOneOfTypes(typeName, typeInfo, parentTypes) {
20013
20024
  result.content = Runtime.getOneOfTemplate()(templateInput);
20014
20025
  return result;
20015
20026
  }
20027
+ async function generateAllOfTypes(typeName, typeInfo, parentTypes) {
20028
+ if (typeInfo.allOf === null || typeInfo.allOf.length === 0) {
20029
+ throw new InvalidSpecFileError('Invalid allOf type for: ' + typeName);
20030
+ }
20031
+ const templateInput = {
20032
+ typeName,
20033
+ type: typeName,
20034
+ compositions: [],
20035
+ description: typeInfo.description,
20036
+ example: typeInfo.example,
20037
+ summary: typeInfo.summary
20038
+ };
20039
+ const result = {
20040
+ content: '',
20041
+ references: new Set(),
20042
+ primitives: new Set(),
20043
+ templateInput
20044
+ };
20045
+ for (let index = 0; index < typeInfo.allOf.length; index++) {
20046
+ const allOfItem = typeInfo.allOf[index];
20047
+ if (allOfItem.$ref !== null) {
20048
+ const referenceData = await resolveTypeReference(allOfItem.$ref);
20049
+ const composition = {
20050
+ source: 'referenced',
20051
+ referencedType: referenceData.name
20052
+ };
20053
+ templateInput.compositions.push(composition);
20054
+ result.references.add(referenceData.name);
20055
+ }
20056
+ else {
20057
+ const generatedType = await generateType(typeName + (index + 1), allOfItem, parentTypes);
20058
+ if (generatedType === null) {
20059
+ throw new InvalidSpecFileError('Invalid allOf type for: ' + typeName);
20060
+ }
20061
+ const composition = {
20062
+ dataType: allOfItem.type,
20063
+ templateInput: generatedType.templateInput,
20064
+ source: 'inline',
20065
+ content: generatedType.content
20066
+ };
20067
+ templateInput.compositions.push(composition);
20068
+ for (const reference of generatedType.references.values()) {
20069
+ result.references.add(reference);
20070
+ }
20071
+ for (const primitive of generatedType.primitives.values()) {
20072
+ result.primitives.add(primitive);
20073
+ }
20074
+ }
20075
+ }
20076
+ result.content = Runtime.getAllOfTemplate()(templateInput);
20077
+ return result;
20078
+ }
20016
20079
  function returnCyclicReference(typeName) {
20017
20080
  const templateInput = {
20018
20081
  typeName,
@@ -20043,6 +20106,9 @@ async function generateType(typeName, typeInfo, parentTypes) {
20043
20106
  if (typeInfo.oneOf !== null) {
20044
20107
  return await generateOneOfTypes(typeName, typeInfo, parentTypes);
20045
20108
  }
20109
+ if (typeInfo.allOf !== null) {
20110
+ return await generateAllOfTypes(typeName, typeInfo, parentTypes);
20111
+ }
20046
20112
  if (typeInfo.type === 'array') {
20047
20113
  return await generateArrayType(typeName, typeInfo, parentTypes);
20048
20114
  }
@@ -20292,6 +20358,7 @@ async function config$1(inputFilePath, outputDirectory, typesWriterMode, grouped
20292
20358
  const typesFileSyntax = await readFile(directoryPrefix + 'templates/typescript/types-file-syntax.hbs', devMode);
20293
20359
  const enumSyntax = await readFile(directoryPrefix + 'templates/typescript/enum-syntax.hbs', devMode);
20294
20360
  const oneOfSyntax = await readFile(directoryPrefix + 'templates/typescript/oneOf-syntax.hbs', devMode);
20361
+ const allOfSyntax = await readFile(directoryPrefix + 'templates/typescript/allOf-syntax.hbs', devMode);
20295
20362
  const config = {
20296
20363
  input: inputFilePath,
20297
20364
  output: {
@@ -20308,7 +20375,8 @@ async function config$1(inputFilePath, outputDirectory, typesWriterMode, grouped
20308
20375
  exporterModuleSyntax,
20309
20376
  typesFileSyntax,
20310
20377
  enumSyntax,
20311
- oneOfSyntax
20378
+ oneOfSyntax,
20379
+ allOfSyntax
20312
20380
  },
20313
20381
  language: {
20314
20382
  exporterModuleName: 'index',
@@ -20334,6 +20402,7 @@ async function config(inputFilePath, outputDirectory, typesWriterMode, groupedTy
20334
20402
  const typesFileSyntax = await readFile(directoryPrefix + 'templates/typescript-with-decoders/types-file-syntax.hbs', devMode);
20335
20403
  const enumSyntax = await readFile(directoryPrefix + 'templates/typescript-with-decoders/enum-syntax.hbs', devMode);
20336
20404
  const oneOfSyntax = await readFile(directoryPrefix + 'templates/typescript-with-decoders/oneOf-syntax.hbs', devMode);
20405
+ const allOfSyntax = await readFile(directoryPrefix + 'templates/typescript-with-decoders/allOf-syntax.hbs', devMode);
20337
20406
  const config = {
20338
20407
  input: inputFilePath,
20339
20408
  output: {
@@ -20350,7 +20419,8 @@ async function config(inputFilePath, outputDirectory, typesWriterMode, groupedTy
20350
20419
  exporterModuleSyntax,
20351
20420
  typesFileSyntax,
20352
20421
  enumSyntax,
20353
- oneOfSyntax
20422
+ oneOfSyntax,
20423
+ allOfSyntax
20354
20424
  },
20355
20425
  language: {
20356
20426
  exporterModuleName: 'index',
@@ -20414,7 +20484,7 @@ async function runner(language, inputFilePath, outputDirectory, _typesWriterMode
20414
20484
  }
20415
20485
  }
20416
20486
  greeting();
20417
- const program = new Command().version('0.7.0');
20487
+ const program = new Command().version('0.8.0');
20418
20488
  program
20419
20489
  .command('generate')
20420
20490
  .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.enumType 'string') }}'{{/if}}{{{this}}}{{#if (eq ../this.templateInput/enumType 'string')}}'{{/if}}
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
+ }
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-crafter",
3
- "version": "0.7.0",
3
+ "version": "0.8.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",