- Generating unique types for unions

This commit is contained in:
Ferdi Koomen 2020-12-19 13:28:59 +01:00
parent e3ff6277d1
commit 9ed2870d1d
7 changed files with 27 additions and 30 deletions

View File

@ -9,6 +9,7 @@ module.exports = {
'<rootDir>/src/**/*.spec.ts',
'<rootDir>/test/index.spec.js',
],
moduleFileExtensions: ['js', 'ts', 'd.ts'],
moduleNameMapper: {
'\\.hbs$': '<rootDir>/src/templates/__mocks__/index.js',
},

View File

@ -1,6 +1,6 @@
{
"name": "openapi-typescript-codegen",
"version": "0.7.1",
"version": "0.7.2",
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
"author": "Ferdi Koomen",
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",

View File

@ -35,6 +35,7 @@ const handlebarsPlugin = () => ({
equals: true,
notEquals: true,
containsSpaces: true,
union: true,
},
});
return `export default ${templateSpec};`;

View File

@ -10,10 +10,10 @@ export enum {{{name}}} {
* {{{description}}}
*/
{{/if}}
{{#if (containsSpaces name)}}
{{#containsSpaces name}}
"{{{name}}}" = {{{value}}},
{{else}}
{{{name}}} = {{{value}}},
{{/if}}
{{/containsSpaces}}
{{/each}}
}

View File

@ -1,5 +1 @@
{{~#if parent~}}
({{#each properties}}{{>type parent=../parent}}{{#unless @last}} | {{/unless}}{{/each}}){{>isNullable}}
{{~else~}}
({{#each properties}}{{>type}}{{#unless @last}} | {{/unless}}{{/each}}){{>isNullable}}
{{~/if~}}
({{#union properties parent}}{{this}}{{/union}}){{>isNullable}}

View File

@ -9,21 +9,6 @@ describe('registerHandlebarHelpers', () => {
expect(helpers).toContain('equals');
expect(helpers).toContain('notEquals');
expect(helpers).toContain('containsSpaces');
});
describe('containsSpaces', () => {
it('should return true when string with spaces is passed', () => {
registerHandlebarHelpers();
const containsSpaces = Handlebars.helpers['containsSpaces'];
const result = containsSpaces('I have spaces insideme');
expect(result).toBeTruthy();
});
it('should return false when string without spaces is passed', () => {
registerHandlebarHelpers();
const containsSpaces = Handlebars.helpers['containsSpaces'];
const result = containsSpaces('Ihavespacesinsideme');
expect(result).toBeFalsy();
});
expect(helpers).toContain('union');
});
});

View File

@ -1,15 +1,29 @@
// @ts-nocheck
import * as Handlebars from 'handlebars/runtime';
import { Model } from '../client/interfaces/Model';
import { unique } from './unique';
export function registerHandlebarHelpers(): void {
Handlebars.registerHelper('equals', function (a: string, b: string, options: Handlebars.HelperOptions): string {
Handlebars.registerHelper('equals', function (this: any, a: string, b: string, options: Handlebars.HelperOptions): string {
return a === b ? options.fn(this) : options.inverse(this);
});
Handlebars.registerHelper('notEquals', function (a: string, b: string, options: Handlebars.HelperOptions): string {
Handlebars.registerHelper('notEquals', function (this: any, a: string, b: string, options: Handlebars.HelperOptions): string {
return a !== b ? options.fn(this) : options.inverse(this);
});
Handlebars.registerHelper('containsSpaces', function (value: string): boolean {
return /\s+/.test(value);
Handlebars.registerHelper('containsSpaces', function (this: any, value: string, options: Handlebars.HelperOptions): string {
return /\s+/.test(value) ? options.fn(this) : options.inverse(this);
});
Handlebars.registerHelper('union', function (this: any, properties: Model[], parent: string | undefined, options: Handlebars.HelperOptions) {
const type = Handlebars.partials['type'];
const types = properties.map(property =>
type({
...property,
parent,
})
);
return options.fn(types.filter(unique).join(' | '));
});
}