Skip to content

Commit 332d69e

Browse files
committed
Adjust source code
1 parent c94d2ab commit 332d69e

File tree

11 files changed

+86
-150
lines changed

11 files changed

+86
-150
lines changed

src/openApi/v2/parser/getServiceName.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export const getServiceName = (value: string): string => {
88
const clean = value
99
.replace(/^[^a-zA-Z]+/g, '')
1010
.replace(/[^\w\-]+/g, '-')
11+
.replace('_controller', '')
12+
.replace('_web_api', '')
1113
.trim();
1214
return camelCase(clean, { pascalCase: true });
1315
};

src/openApi/v3/parser/getOperation.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { getOperationResults } from './getOperationResults';
1313
import { getRef } from './getRef';
1414
import { getServiceName } from './getServiceName';
1515
import { sortByRequired } from './sortByRequired';
16+
import { getOperationPath } from './getOperationPath';
1617

1718
export const getOperation = (
1819
openApi: OpenApi,
@@ -24,6 +25,7 @@ export const getOperation = (
2425
): Operation => {
2526
const serviceName = getServiceName(tag);
2627
const operationName = getOperationName(url, method, op.operationId);
28+
const operationPath = getOperationPath(url);
2729

2830
// Create a new operation object for this method.
2931
const operation: Operation = {
@@ -33,7 +35,7 @@ export const getOperation = (
3335
description: op.description || null,
3436
deprecated: op.deprecated === true,
3537
method: method.toUpperCase(),
36-
path: url,
38+
path: operationPath,
3739
parameters: [...pathParams.parameters],
3840
parametersPath: [...pathParams.parametersPath],
3941
parametersQuery: [...pathParams.parametersQuery],

src/openApi/v3/parser/getOperationName.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const getOperationName = (url: string, method: string, operationId?: stri
1111
operationId
1212
.replace(/^[^a-zA-Z]+/g, '')
1313
.replace(/[^\w\-]+/g, '-')
14+
.replace(/_?\d+$/, '')
1415
.trim()
1516
);
1617
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { getOperationParameterName } from './getOperationParameterName';
2+
3+
/**
4+
* We return the correct parameter names to replace in the URL.
5+
* @param path
6+
*/
7+
export function getOperationPath(path: string): string {
8+
return path.replace(/\{(.*?)\}/g, (_, w: string) => {
9+
return `\${${getOperationParameterName(w)}}`;
10+
});
11+
}

src/openApi/v3/parser/getServiceName.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export const getServiceName = (value: string): string => {
88
const clean = value
99
.replace(/^[^a-zA-Z]+/g, '')
1010
.replace(/[^\w\-]+/g, '-')
11+
.replace('_controller', '')
12+
.replace('_web_api', '')
1113
.trim();
1214
return camelCase(clean, { pascalCase: true });
1315
};

src/templates/exportService.hbs

Lines changed: 60 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,152 +1,73 @@
11
{{>header}}
22

3-
{{#equals @root.httpClient 'angular'}}
4-
{{#if @root.exportClient}}
5-
import { Injectable } from '@angular/core';
6-
import type { Observable } from 'rxjs';
7-
{{else}}
8-
import { Injectable } from '@angular/core';
9-
import { HttpClient } from '@angular/common/http';
10-
import type { Observable } from 'rxjs';
11-
{{/if}}
12-
13-
{{/equals}}
143
{{#if imports}}
154
{{#each imports}}
16-
import type { {{{this}}} } from '../models/{{{this}}}';
5+
import type { {{~this~}} } from '../models/{{{this}}}';
176
{{/each}}
18-
197
{{/if}}
20-
{{#notEquals @root.httpClient 'angular'}}
21-
import type { CancelablePromise } from '../core/CancelablePromise';
22-
{{/notEquals}}
23-
{{#if @root.exportClient}}
24-
{{#equals @root.httpClient 'angular'}}
25-
import { BaseHttpRequest } from '../core/BaseHttpRequest';
26-
{{else}}
27-
import type { BaseHttpRequest } from '../core/BaseHttpRequest';
28-
{{/equals}}
29-
{{else}}
8+
import {request as __request} from '../core/request';
9+
{{#if @root.useVersion}}
3010
import { OpenAPI } from '../core/OpenAPI';
31-
import { request as __request } from '../core/request';
3211
{{/if}}
3312

34-
{{#equals @root.httpClient 'angular'}}
35-
@Injectable({
36-
providedIn: 'root',
37-
})
38-
{{/equals}}
3913
export class {{{name}}}{{{@root.postfix}}} {
40-
{{#if @root.exportClient}}
41-
42-
constructor(public readonly httpRequest: BaseHttpRequest) {}
43-
{{else}}
44-
{{#equals @root.httpClient 'angular'}}
45-
46-
constructor(public readonly http: HttpClient) {}
47-
{{/equals}}
48-
{{/if}}
4914

50-
{{#each operations}}
51-
/**
52-
{{#if deprecated}}
53-
* @deprecated
54-
{{/if}}
55-
{{#if summary}}
56-
* {{{escapeComment summary}}}
57-
{{/if}}
58-
{{#if description}}
59-
* {{{escapeComment description}}}
60-
{{/if}}
61-
{{#unless @root.useOptions}}
62-
{{#if parameters}}
63-
{{#each parameters}}
64-
* @param {{{name}}} {{#if description}}{{{escapeComment description}}}{{/if}}
65-
{{/each}}
66-
{{/if}}
67-
{{/unless}}
68-
{{#each results}}
69-
* @returns {{{type}}} {{#if description}}{{{escapeComment description}}}{{/if}}
70-
{{/each}}
71-
* @throws ApiError
72-
*/
73-
{{#if @root.exportClient}}
74-
{{#equals @root.httpClient 'angular'}}
75-
public {{{name}}}({{>parameters}}): Observable<{{>result}}> {
76-
return this.httpRequest.request({
77-
{{else}}
78-
public {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> {
79-
return this.httpRequest.request({
80-
{{/equals}}
81-
{{else}}
82-
{{#equals @root.httpClient 'angular'}}
83-
public {{{name}}}({{>parameters}}): Observable<{{>result}}> {
84-
return __request(OpenAPI, this.http, {
85-
{{else}}
86-
public static {{{name}}}({{>parameters}}): CancelablePromise<{{>result}}> {
87-
return __request(OpenAPI, {
88-
{{/equals}}
89-
{{/if}}
90-
method: '{{{method}}}',
91-
url: '{{{path}}}',
92-
{{#if parametersPath}}
93-
path: {
94-
{{#each parametersPath}}
95-
'{{{prop}}}': {{{name}}},
96-
{{/each}}
97-
},
98-
{{/if}}
99-
{{#if parametersCookie}}
100-
cookies: {
101-
{{#each parametersCookie}}
102-
'{{{prop}}}': {{{name}}},
103-
{{/each}}
104-
},
105-
{{/if}}
106-
{{#if parametersHeader}}
107-
headers: {
108-
{{#each parametersHeader}}
109-
'{{{prop}}}': {{{name}}},
110-
{{/each}}
111-
},
112-
{{/if}}
113-
{{#if parametersQuery}}
114-
query: {
115-
{{#each parametersQuery}}
116-
'{{{prop}}}': {{{name}}},
117-
{{/each}}
118-
},
119-
{{/if}}
120-
{{#if parametersForm}}
121-
formData: {
122-
{{#each parametersForm}}
123-
'{{{prop}}}': {{{name}}},
124-
{{/each}}
125-
},
126-
{{/if}}
127-
{{#if parametersBody}}
128-
{{#equals parametersBody.in 'formData'}}
129-
formData: {{{parametersBody.name}}},
130-
{{/equals}}
131-
{{#equals parametersBody.in 'body'}}
132-
body: {{{parametersBody.name}}},
133-
{{/equals}}
134-
{{#if parametersBody.mediaType}}
135-
mediaType: '{{{parametersBody.mediaType}}}',
136-
{{/if}}
137-
{{/if}}
138-
{{#if responseHeader}}
139-
responseHeader: '{{{responseHeader}}}',
140-
{{/if}}
141-
{{#if errors}}
142-
errors: {
143-
{{#each errors}}
144-
{{{code}}}: `{{{escapeDescription description}}}`,
145-
{{/each}}
146-
},
147-
{{/if}}
148-
});
149-
}
15+
{{#each operations}}
16+
public static {{{name}}}({{>parameters}}) {
17+
return __request<{{>result}}>({
18+
method: '{{{method}}}',
19+
path: `{{{path}}}`,
20+
{{#if parametersCookie}}
21+
cookies: {
22+
{{#each parametersCookie}}
23+
'{{{prop}}}': {{{name}}},
24+
{{/each}}
25+
},
26+
{{/if}}
27+
{{#if parametersHeader}}
28+
headers: {
29+
{{#each parametersHeader}}
30+
'{{{prop}}}': {{{name}}},
31+
{{/each}}
32+
},
33+
{{/if}}
34+
{{#if parametersQuery}}
35+
query: {
36+
{{#each parametersQuery}}
37+
'{{{prop}}}': {{{name}}},
38+
{{/each}}
39+
},
40+
{{/if}}
41+
{{#if parametersForm}}
42+
formData: {
43+
{{#each parametersForm}}
44+
'{{{prop}}}': {{{name}}},
45+
{{/each}}
46+
},
47+
{{/if}}
48+
{{#if parametersBody}}
49+
{{#equals parametersBody.in 'formData'}}
50+
formData: {{{parametersBody.name}}},
51+
{{/equals}}
52+
{{#equals parametersBody.in 'body'}}
53+
body: {{{parametersBody.name}}},
54+
{{/equals}}
55+
{{#if parametersBody.mediaType}}
56+
mediaType: '{{{parametersBody.mediaType}}}',
57+
{{/if}}
58+
{{/if}}
59+
{{#if responseHeader}}
60+
responseHeader: '{{{responseHeader}}}',
61+
{{/if}}
62+
{{#if errors}}
63+
errors: {
64+
{{#each errors}}
65+
{{{code}}}: `{{{description}}}`,
66+
{{/each}}
67+
},
68+
{{/if}}
69+
});
70+
}
15071

151-
{{/each}}
72+
{{/each}}
15273
}

src/templates/partials/exportInterface.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export type {{{name}}} = {
2222
{{/ifdef}}
2323
{{>isReadOnly}}{{{name}}}{{>isRequired}}: {{>type parent=../name}};
2424
{{/each}}
25-
};
25+
}
2626
{{#if enums}}
2727
{{#unless @root.useUnionTypes}}
2828

src/templates/partials/header.hbs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* generated using openapi-typescript-codegen -- do not edit */
21
/* istanbul ignore file */
32
/* tslint:disable */
43
/* eslint-disable */

src/utils/formatCode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { EOL } from 'os';
22

33
export const formatCode = (s: string): string => {
44
let indent: number = 0;
5-
let lines = s.split(/[\r\n]+/);
5+
let lines = s.split(EOL);
66
lines = lines.map(line => {
77
line = line.trim().replace(/^\*/g, ' *');
88
let i = indent;
@@ -13,7 +13,7 @@ export const formatCode = (s: string): string => {
1313
indent--;
1414
i--;
1515
}
16-
const result = `${'\t'.repeat(i)}${line}`;
16+
const result = `${' '.repeat(i)}${line}`;
1717
if (result.trim() === '') {
1818
return '';
1919
}

src/utils/formatIndentation.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,5 @@ export const formatIndentation = (s: string, indent: Indent): string => {
1414
return line; // Default output is tab formatted
1515
}
1616
});
17-
// Make sure we have a blank line at the end
18-
const content = lines.join(EOL);
19-
return `${content}${EOL}`;
17+
return lines.join(EOL);
2018
};

0 commit comments

Comments
 (0)