Skip to content

Commit 9ed2870

Browse files
author
Ferdi Koomen
committed
- Generating unique types for unions
1 parent e3ff627 commit 9ed2870

File tree

7 files changed

+27
-30
lines changed

7 files changed

+27
-30
lines changed

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = {
99
'<rootDir>/src/**/*.spec.ts',
1010
'<rootDir>/test/index.spec.js',
1111
],
12+
moduleFileExtensions: ['js', 'ts', 'd.ts'],
1213
moduleNameMapper: {
1314
'\\.hbs$': '<rootDir>/src/templates/__mocks__/index.js',
1415
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openapi-typescript-codegen",
3-
"version": "0.7.1",
3+
"version": "0.7.2",
44
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
55
"author": "Ferdi Koomen",
66
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",

rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const handlebarsPlugin = () => ({
3535
equals: true,
3636
notEquals: true,
3737
containsSpaces: true,
38+
union: true,
3839
},
3940
});
4041
return `export default ${templateSpec};`;

src/templates/partials/exportEnum.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ export enum {{{name}}} {
1010
* {{{description}}}
1111
*/
1212
{{/if}}
13-
{{#if (containsSpaces name)}}
13+
{{#containsSpaces name}}
1414
"{{{name}}}" = {{{value}}},
1515
{{else}}
1616
{{{name}}} = {{{value}}},
17-
{{/if}}
17+
{{/containsSpaces}}
1818
{{/each}}
1919
}

src/templates/partials/typeUnion.hbs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{{~#if parent~}}
2-
({{#each properties}}{{>type parent=../parent}}{{#unless @last}} | {{/unless}}{{/each}}){{>isNullable}}
3-
{{~else~}}
4-
({{#each properties}}{{>type}}{{#unless @last}} | {{/unless}}{{/each}}){{>isNullable}}
5-
{{~/if~}}
1+
({{#union properties parent}}{{this}}{{/union}}){{>isNullable}}

src/utils/registerHandlebarHelpers.spec.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,6 @@ describe('registerHandlebarHelpers', () => {
99
expect(helpers).toContain('equals');
1010
expect(helpers).toContain('notEquals');
1111
expect(helpers).toContain('containsSpaces');
12-
});
13-
14-
describe('containsSpaces', () => {
15-
it('should return true when string with spaces is passed', () => {
16-
registerHandlebarHelpers();
17-
const containsSpaces = Handlebars.helpers['containsSpaces'];
18-
const result = containsSpaces('I have spaces insideme');
19-
expect(result).toBeTruthy();
20-
});
21-
22-
it('should return false when string without spaces is passed', () => {
23-
registerHandlebarHelpers();
24-
const containsSpaces = Handlebars.helpers['containsSpaces'];
25-
const result = containsSpaces('Ihavespacesinsideme');
26-
expect(result).toBeFalsy();
27-
});
12+
expect(helpers).toContain('union');
2813
});
2914
});

src/utils/registerHandlebarHelpers.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
1-
// @ts-nocheck
2-
31
import * as Handlebars from 'handlebars/runtime';
42

3+
import { Model } from '../client/interfaces/Model';
4+
import { unique } from './unique';
5+
56
export function registerHandlebarHelpers(): void {
6-
Handlebars.registerHelper('equals', function (a: string, b: string, options: Handlebars.HelperOptions): string {
7+
Handlebars.registerHelper('equals', function (this: any, a: string, b: string, options: Handlebars.HelperOptions): string {
78
return a === b ? options.fn(this) : options.inverse(this);
89
});
9-
Handlebars.registerHelper('notEquals', function (a: string, b: string, options: Handlebars.HelperOptions): string {
10+
11+
Handlebars.registerHelper('notEquals', function (this: any, a: string, b: string, options: Handlebars.HelperOptions): string {
1012
return a !== b ? options.fn(this) : options.inverse(this);
1113
});
12-
Handlebars.registerHelper('containsSpaces', function (value: string): boolean {
13-
return /\s+/.test(value);
14+
15+
Handlebars.registerHelper('containsSpaces', function (this: any, value: string, options: Handlebars.HelperOptions): string {
16+
return /\s+/.test(value) ? options.fn(this) : options.inverse(this);
17+
});
18+
19+
Handlebars.registerHelper('union', function (this: any, properties: Model[], parent: string | undefined, options: Handlebars.HelperOptions) {
20+
const type = Handlebars.partials['type'];
21+
const types = properties.map(property =>
22+
type({
23+
...property,
24+
parent,
25+
})
26+
);
27+
return options.fn(types.filter(unique).join(' | '));
1428
});
1529
}

0 commit comments

Comments
 (0)