diff --git a/package.json b/package.json index 0ce34123..7e90726d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "openapi-typescript-codegen", - "version": "0.7.0-beta-2", + "version": "0.7.0-beta-3", "description": "Library that generates Typescript clients based on the OpenAPI specification.", "author": "Ferdi Koomen", "homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen", diff --git a/src/templates/exportModel.hbs b/src/templates/exportModel.hbs index f5985aa2..2b3a082d 100644 --- a/src/templates/exportModel.hbs +++ b/src/templates/exportModel.hbs @@ -9,6 +9,12 @@ import type { {{{this}}} } from './{{{this}}}'; {{#equals export 'interface'}} {{>exportInterface}} +{{else equals export 'one-of'}} +{{>exportComposition}} +{{else equals export 'any-of'}} +{{>exportComposition}} +{{else equals export 'all-of'}} +{{>exportComposition}} {{else equals export 'enum'}} {{>exportEnum}} {{else}} diff --git a/src/templates/partials/exportComposition.hbs b/src/templates/partials/exportComposition.hbs new file mode 100644 index 00000000..b8b3f4a2 --- /dev/null +++ b/src/templates/partials/exportComposition.hbs @@ -0,0 +1,28 @@ +{{#if description}} +/** + * {{{description}}} + */ +{{/if}} +export type {{{name}}} = {{>type parent=name}}; +{{#if enums}} +{{#unless @root.useUnionTypes}} + +export namespace {{{name}}} { + + {{#each enums}} + {{#if description}} + /** + * {{{description}}} + */ + {{/if}} + export enum {{{name}}} { + {{#each enum}} + {{{name}}} = {{{value}}}, + {{/each}} + } + + {{/each}} + +} +{{/unless}} +{{/if}} diff --git a/src/templates/partials/typeInterface.hbs b/src/templates/partials/typeInterface.hbs index d44dab6a..1ca35e27 100644 --- a/src/templates/partials/typeInterface.hbs +++ b/src/templates/partials/typeInterface.hbs @@ -6,7 +6,11 @@ * {{{description}}} */ {{/if}} +{{#if ../parent}} +{{>isReadOnly}}{{{name}}}{{>isRequired}}: {{>type parent=../parent}}, +{{else}} {{>isReadOnly}}{{{name}}}{{>isRequired}}: {{>type}}, +{{/if}} {{/each}} }{{>isNullable}} {{~else~}} diff --git a/src/templates/partials/typeIntersection.hbs b/src/templates/partials/typeIntersection.hbs index d966489f..2328f936 100644 --- a/src/templates/partials/typeIntersection.hbs +++ b/src/templates/partials/typeIntersection.hbs @@ -1 +1,5 @@ +{{~#if parent~}} +({{#each properties}}{{>type parent=../parent}}{{#unless @last}} & {{/unless}}{{/each}}){{>isNullable}} +{{~else~}} ({{#each properties}}{{>type}}{{#unless @last}} & {{/unless}}{{/each}}){{>isNullable}} +{{~/if~}} diff --git a/src/templates/partials/typeUnion.hbs b/src/templates/partials/typeUnion.hbs index b2561f52..1533d4ac 100644 --- a/src/templates/partials/typeUnion.hbs +++ b/src/templates/partials/typeUnion.hbs @@ -1 +1,5 @@ +{{~#if parent~}} +({{#each properties}}{{>type parent=../parent}}{{#unless @last}} | {{/unless}}{{/each}}){{>isNullable}} +{{~else~}} ({{#each properties}}{{>type}}{{#unless @last}} | {{/unless}}{{/each}}){{>isNullable}} +{{~/if~}} diff --git a/src/utils/registerHandlebarTemplates.ts b/src/utils/registerHandlebarTemplates.ts index febe94f1..6772eebd 100644 --- a/src/utils/registerHandlebarTemplates.ts +++ b/src/utils/registerHandlebarTemplates.ts @@ -39,6 +39,7 @@ import templateExportSchema from '../templates/exportSchema.hbs'; import templateExportService from '../templates/exportService.hbs'; import templateIndex from '../templates/index.hbs'; import partialBase from '../templates/partials/base.hbs'; +import partialExportComposition from '../templates/partials/exportComposition.hbs'; import partialExportEnum from '../templates/partials/exportEnum.hbs'; import partialExportInterface from '../templates/partials/exportInterface.hbs'; import partialExportType from '../templates/partials/exportType.hbs'; @@ -109,6 +110,7 @@ export function registerHandlebarTemplates(): Templates { // Partials for the generations of the models, services, etc. Handlebars.registerPartial('exportEnum', Handlebars.template(partialExportEnum)); Handlebars.registerPartial('exportInterface', Handlebars.template(partialExportInterface)); + Handlebars.registerPartial('exportComposition', Handlebars.template(partialExportComposition)); Handlebars.registerPartial('exportType', Handlebars.template(partialExportType)); Handlebars.registerPartial('header', Handlebars.template(partialHeader)); Handlebars.registerPartial('isNullable', Handlebars.template(partialIsNullable)); diff --git a/test/__snapshots__/index.spec.js.snap b/test/__snapshots__/index.spec.js.snap index f2cf4329..5f230fc2 100644 --- a/test/__snapshots__/index.spec.js.snap +++ b/test/__snapshots__/index.spec.js.snap @@ -619,7 +619,8 @@ import type { ModelWithString } from './ModelWithString'; export type ModelThatExtends = (ModelWithString & { propExtendsA?: string, propExtendsB?: ModelWithString, -});" +}); +" `; exports[`v2 should generate: ./test/generated/v2/models/ModelThatExtendsExtends.ts 1`] = ` @@ -636,7 +637,8 @@ import type { ModelWithString } from './ModelWithString'; export type ModelThatExtendsExtends = (ModelWithString & ModelThatExtends & { propExtendsC?: string, propExtendsD?: ModelWithString, -});" +}); +" `; exports[`v2 should generate: ./test/generated/v2/models/ModelWithArray.ts 1`] = ` @@ -2992,7 +2994,8 @@ import type { ModelWithString } from './ModelWithString'; export type ModelThatExtends = (ModelWithString & { propExtendsA?: string, propExtendsB?: ModelWithString, -});" +}); +" `; exports[`v3 should generate: ./test/generated/v3/models/ModelThatExtendsExtends.ts 1`] = ` @@ -3009,7 +3012,8 @@ import type { ModelWithString } from './ModelWithString'; export type ModelThatExtendsExtends = (ModelWithString & ModelThatExtends & { propExtendsC?: string, propExtendsD?: ModelWithString, -});" +}); +" `; exports[`v3 should generate: ./test/generated/v3/models/ModelWithArray.ts 1`] = ` diff --git a/test/index.js b/test/index.js index 115c033c..7c88334b 100644 --- a/test/index.js +++ b/test/index.js @@ -7,8 +7,8 @@ async function generateV2() { input: './test/spec/v2.json', output: './test/generated/v2/', httpClient: OpenAPI.HttpClient.FETCH, - useOptions: true, - useUnionTypes: true, + useOptions: false, + useUnionTypes: false, exportCore: true, exportSchemas: true, exportModels: true, @@ -21,8 +21,8 @@ async function generateV3() { input: './test/spec/v3.json', output: './test/generated/v3/', httpClient: OpenAPI.HttpClient.FETCH, - useOptions: true, - useUnionTypes: true, + useOptions: false, + useUnionTypes: false, exportCore: true, exportSchemas: true, exportModels: true,