Skip to content

Commit 61ecd2a

Browse files
committed
add support for service request type
1 parent 1508588 commit 61ecd2a

File tree

6 files changed

+344
-0
lines changed

6 files changed

+344
-0
lines changed

rollup.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ const handlebarsPlugin = () => ({
3232
preventIndent: true,
3333
knownHelpersOnly: true,
3434
knownHelpers: {
35+
capitalizeFirstLetter: true,
36+
hasProperty: true,
37+
hasLength: true,
3538
equals: true,
3639
notEquals: true,
3740
containsSpaces: true,

src/templates/exportService.hbs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ import { request as __request } from '../core/request';
1111
import { OpenAPI } from '../core/OpenAPI';
1212
{{/if}}
1313

14+
{{#each operations}}
15+
{{#hasLength parameters}}
16+
export type {{capitalizeFirstLetter name}}Request = {
17+
{{>parametersType}}
18+
}
19+
20+
{{/hasLength}}
21+
{{/each}}
1422
export class {{{name}}}{{{@root.postfix}}} {
1523

1624
{{#each operations}}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{{#if parameters}}
2+
{{#hasProperty parameters 'path'}}
3+
path: {
4+
{{#each parameters}}
5+
{{#equals in 'path'}}
6+
{{{name}}}{{>isRequired}}: {{>type}}{{#if default}} = {{{default}}}{{/if}},
7+
{{/equals}}
8+
{{/each}}
9+
}
10+
{{/hasProperty}}
11+
{{#hasProperty parameters 'query'}}
12+
query: {
13+
{{#each parameters}}
14+
{{#equals in 'query'}}
15+
{{{name}}}{{>isRequired}}: {{>type}}{{#if default}} = {{{default}}}{{/if}},
16+
{{/equals}}
17+
{{/each}}
18+
}
19+
{{/hasProperty}}
20+
{{#hasProperty parameters 'body'}}
21+
body: {
22+
{{#each parameters}}
23+
{{#equals in 'body'}}
24+
{{{name}}}{{>isRequired}}: {{>type}}{{#if default}} = {{{default}}}{{/if}},
25+
{{/equals}}
26+
{{/each}}
27+
}
28+
{{/hasProperty}}
29+
{{/if}}

src/utils/registerHandlebarHelpers.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as Handlebars from 'handlebars/runtime';
22

33
import { Enum } from '../client/interfaces/Enum';
44
import { Model } from '../client/interfaces/Model';
5+
import { OperationParameter } from '../client/interfaces/OperationParameter';
56
import { HttpClient } from '../HttpClient';
67
import { unique } from './unique';
78

@@ -10,6 +11,31 @@ export function registerHandlebarHelpers(root: {
1011
useOptions: boolean;
1112
useUnionTypes: boolean;
1213
}): void {
14+
Handlebars.registerHelper(
15+
'capitalizeFirstLetter',
16+
function (this: any, a: string): string {
17+
return a.charAt(0).toUpperCase() + a.slice(1)
18+
}
19+
);
20+
21+
Handlebars.registerHelper(
22+
'hasProperty',
23+
function (this: any, a: OperationParameter[], b: string, options: Handlebars.HelperOptions): string {
24+
return a.map(item => item.in).find(value => value === b)
25+
? options.fn(this)
26+
: options.inverse(this);
27+
}
28+
);
29+
30+
Handlebars.registerHelper(
31+
'hasLength',
32+
function (this: any, a: any[], options: Handlebars.HelperOptions): string {
33+
return a.length > 0
34+
? options.fn(this)
35+
: options.inverse(this);
36+
}
37+
);
38+
1339
Handlebars.registerHelper(
1440
'equals',
1541
function (this: any, a: string, b: string, options: Handlebars.HelperOptions): string {

src/utils/registerHandlebarTemplates.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import partialIsNullable from '../templates/partials/isNullable.hbs';
5656
import partialIsReadOnly from '../templates/partials/isReadOnly.hbs';
5757
import partialIsRequired from '../templates/partials/isRequired.hbs';
5858
import partialParameters from '../templates/partials/parameters.hbs';
59+
import partialParametersType from '../templates/partials/parametersType.hbs';
5960
import partialResult from '../templates/partials/result.hbs';
6061
import partialSchema from '../templates/partials/schema.hbs';
6162
import partialSchemaArray from '../templates/partials/schemaArray.hbs';
@@ -131,6 +132,7 @@ export function registerHandlebarTemplates(root: {
131132
Handlebars.registerPartial('isReadOnly', Handlebars.template(partialIsReadOnly));
132133
Handlebars.registerPartial('isRequired', Handlebars.template(partialIsRequired));
133134
Handlebars.registerPartial('parameters', Handlebars.template(partialParameters));
135+
Handlebars.registerPartial('parametersType', Handlebars.template(partialParametersType));
134136
Handlebars.registerPartial('result', Handlebars.template(partialResult));
135137
Handlebars.registerPartial('schema', Handlebars.template(partialSchema));
136138
Handlebars.registerPartial('schemaArray', Handlebars.template(partialSchemaArray));

0 commit comments

Comments
 (0)