Skip to content

Commit efe3c11

Browse files
committed
Extending 2
1 parent b1830dd commit efe3c11

File tree

13 files changed

+397
-172
lines changed

13 files changed

+397
-172
lines changed

rollup.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ const handlebarsPlugin = () => ({
3030
strict: true,
3131
noEscape: true,
3232
preventIndent: true,
33-
knownHelpersOnly: true,
33+
knownHelpersOnly: false,
3434
knownHelpers: {
35+
// This is a manual solution which is not good enough
3536
equals: true,
3637
notEquals: true,
3738
containsSpaces: true,

src/openApi/v3/parser/getServices.ts

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,47 @@ import { getOperationParameters } from './getOperationParameters';
88
*/
99
export function getServices(openApi: OpenApi): Service[] {
1010
const services = new Map<string, Service>();
11+
1112
for (const url in openApi.paths) {
12-
if (openApi.paths.hasOwnProperty(url)) {
13-
// Grab path and parse any global path parameters
14-
const path = openApi.paths[url];
15-
const pathParams = getOperationParameters(openApi, path.parameters || []);
16-
17-
// Parse all the methods for this path
18-
for (const method in path) {
19-
if (path.hasOwnProperty(method)) {
20-
switch (method) {
21-
case 'get':
22-
case 'put':
23-
case 'post':
24-
case 'delete':
25-
case 'options':
26-
case 'head':
27-
case 'patch':
28-
// Each method contains an OpenAPI operation, we parse the operation
29-
const op = path[method]!;
30-
const operation = getOperation(openApi, url, method, op, pathParams);
31-
32-
// If we have already declared a service, then we should fetch that and
33-
// append the new method to it. Otherwise we should create a new service object.
34-
const service =
35-
services.get(operation.service) ||
36-
({
37-
name: operation.service,
38-
operations: [],
39-
imports: [],
40-
} as Service);
41-
42-
// Push the operation in the service
43-
service.operations.push(operation);
44-
service.imports.push(...operation.imports);
45-
services.set(operation.service, service);
46-
break;
47-
}
48-
}
13+
if (!openApi.paths.hasOwnProperty(url)) { continue; }
14+
15+
// Grab path and parse any global path parameters
16+
const path = openApi.paths[url];
17+
const pathParams = getOperationParameters(openApi, path.parameters || []);
18+
19+
// Parse all the methods for this path
20+
for (const method in path) {
21+
// This for the Typescript check
22+
if (!path.hasOwnProperty(method)) { continue; }
23+
24+
switch (method) {
25+
case 'get':
26+
case 'put':
27+
case 'post':
28+
case 'delete':
29+
case 'options':
30+
case 'head':
31+
case 'patch':
32+
// Each method contains an OpenAPI operation, we parse the operation
33+
const op = path[method]!;
34+
const operation = getOperation(openApi, url, method, op, pathParams);
35+
36+
// If we have already declared a service, then we should fetch that and
37+
// append the new method to it. Otherwise we should create a new service object.
38+
const service = services.get(operation.service) || {
39+
name: operation.service,
40+
operations: [],
41+
imports: [],
42+
};
43+
44+
// Push the operation in the service
45+
service.operations.push(operation);
46+
service.imports.push(...operation.imports);
47+
services.set(operation.service, service);
48+
break;
4949
}
5050
}
5151
}
52+
5253
return Array.from(services.values());
5354
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
3+
@{{{Capital method}}}('{{{path}}}')
4+
public {{{name}}}(
5+
{{#each parameters}}
6+
{{MethodInput this }},
7+
{{/each}}
8+
): {{>result}} {
9+
const result = __request({
10+
{{#if parametersCookie}}
11+
cookies: {
12+
{{#each parametersCookie}}
13+
'{{{prop}}}': {{{name}}},
14+
{{/each}}
15+
},
16+
{{/if}}
17+
{{#if parametersHeader}}
18+
headers: {
19+
{{#each parametersHeader}}
20+
'{{{prop}}}': {{{name}}},
21+
{{/each}}
22+
},
23+
{{/if}}
24+
{{#if parametersQuery}}
25+
query: {
26+
{{#each parametersQuery}}
27+
'{{{prop}}}': {{{name}}},
28+
{{/each}}
29+
},
30+
{{/if}}
31+
{{#if parametersForm}}
32+
formData: {
33+
{{#each parametersForm}}
34+
'{{{prop}}}': {{{name}}},
35+
{{/each}}
36+
},
37+
{{/if}}
38+
{{#if parametersBody}}
39+
body: {{{parametersBody.name}}},
40+
{{#if parametersBody.mediaType}}
41+
mediaType: '{{{parametersBody.mediaType}}}',
42+
{{/if}}
43+
{{/if}}
44+
{{#if responseHeader}}
45+
responseHeader: '{{{responseHeader}}}',
46+
{{/if}}
47+
{{#if errors}}
48+
errors: {
49+
{{#each errors}}
50+
{{{code}}}: `{{{description}}}`,
51+
{{/each}}
52+
},
53+
{{/if}}
54+
});
55+
return result.body;
56+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
{{#if deprecated}}
3+
* @deprecated
4+
{{/if}}
5+
{{#if summary}}
6+
* {{{summary}}}
7+
{{/if}}
8+
{{#if description}}
9+
* {{{description}}}
10+
{{/if}}
11+
{{#unless @root.useOptions}}
12+
{{#if parameters}}
13+
{{#each parameters}}
14+
* @param {{{name}}} {{{description}}}
15+
{{/each}}
16+
{{/if}}
17+
{{/unless}}
18+
{{#each results}}
19+
* @returns {{{type}}} {{{description}}}
20+
{{/each}}
21+
* @throws ApiError
22+
*/

src/templates/exportController.hbs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1-
console.log('Hello world');
1+
{{>header}}
2+
3+
4+
{{#if imports}}
5+
{{#each imports}}
6+
import type { {{{this}}} } from '../models/{{{this}}}';
7+
{{/each}}
8+
{{/if}}
9+
import { request as __request } from '../core/request';
10+
import { Get, Post, Delete, Put, Option, Head, Patch, Method,
11+
RequestObj, ResponseObj, Next, Body, PathVariable, Param, Query, Cookie,
12+
Router, Path} from 'havas-express';
13+
14+
15+
{{#if @root.useVersion}}
16+
import { OpenAPI } from '../core/OpenAPI';
17+
{{/if}}
18+
19+
@Path('/')
20+
export class {{{name}}} extends Router {
21+
22+
{{#each operations}}
23+
{{>controller_method_comment}}
24+
25+
{{>controller_method }}
26+
27+
28+
{{/each}}
29+
30+
}

0 commit comments

Comments
 (0)