Skip to content

Commit baefa8f

Browse files
committed
feat(type-names): omitted unnecessary type naming postfix
closes #9
1 parent 4eeece6 commit baefa8f

File tree

5 files changed

+23
-35
lines changed

5 files changed

+23
-35
lines changed

.npmignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ node_modules/
55
.gitignore
66
.idea
77
tsconfig.json
8-
yarn.lock
8+
yarn.lock
9+
CODE_OF_CONDUCT.md
10+
PULL_REQUEST_TEMPLATE.md

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@flowup/ngx-swagger-client-generator",
3-
"version": "2.0.0-alpha-3",
3+
"version": "2.0.0-beta-1",
44
"description": "Angular REST API client generator from Swagger YAML or JSON file with camel case settigs",
55
"homepage": "https://github.com/flowup/ngx-swagger-client-generator/blob/master/README.md",
66
"main": "src/main.ts",

src/generator.ts

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export interface Definition {
1414
imports: string[];
1515
isEnum?: boolean;
1616
renderFileName?: () => RenderFileName; // generate dash-case file names to templates
17-
last?: boolean;
1817
}
1918

2019
export interface MustacheData {
@@ -41,7 +40,6 @@ export interface Parameter {
4140
readonly isRef?: boolean;
4241
readonly isQueryParameter?: boolean;
4342
readonly isSingleton?: boolean;
44-
readonly last?: boolean;
4543
readonly 'in'?: In;
4644
readonly 'enum'?: any[];
4745
readonly items?: Parameter;
@@ -54,14 +52,13 @@ export interface Parameter {
5452

5553
export interface Method {
5654
readonly path?: string; // path appended to base in method
57-
readonly backTickPath?: string;
5855
readonly methodName?: any; // mane of the generated method
5956
readonly methodType?: MethodType; // type of the http method
6057
readonly summaryLines?: any[];
6158
readonly isSecure?: boolean; // currently unused TODO
6259
readonly parameters: Parameter[];
6360
readonly hasJsonResponse?: boolean; // if false, default toJson() should not be called TODO
64-
readonly response?: string; // method return type // todo make readonly
61+
readonly response?: string; // method return type
6562
}
6663

6764
export class Generator {
@@ -106,33 +103,27 @@ export class Generator {
106103
return 'any';
107104
} else if (/^array$/i.test(type)) {
108105
if (items) {
109-
return Generator.modelName(items.type, true);
106+
return Generator.typeName(items.type, true);
110107
} else {
111108
return 'any[]';
112109
}
113110
} else {
114-
return Generator.modelName(type);
111+
return Generator.typeName(type);
115112
}
116113
}
117114

118-
private static modelName(typeName: string = '', isArray: boolean = false): string {
115+
private static typeName(typeName: string = '', isArray: boolean = false): string {
119116
let type: string;
120117

121-
if (/.+Model$/.test(typeName)) {
122-
type = typeName;
123-
} else if (/^(?:string)|(?:number)|(?:integer)|(?:boolean)|(?:undefined)|(?:any)|(?:object)$/i.test(typeName)) {
118+
if (/^(?:string)|(?:number)|(?:integer)|(?:boolean)|(?:undefined)|(?:any)|(?:object)$/i.test(typeName)) {
124119
type = typeName;
125120
} else {
126-
type = `${Generator.camelCase(typeName, false)}Model`;
121+
type = Generator.camelCase(typeName, false);
127122
}
128123

129124
return `${type}${isArray ? '[]' : ''}`;
130125
};
131126

132-
private static enumName(typeName: string = ''): string {
133-
return `${Generator.camelCase(typeName, false)}Enum`;
134-
};
135-
136127
private static fileName(name: string = '', type: 'model' | 'enum' = 'model'): string {
137128
return `${Generator.dashCase(name.replace(/model|enum/i, ''))}.${type}`;
138129
};
@@ -153,7 +144,7 @@ export class Generator {
153144

154145
if (defIn.enum && defIn.enum.length !== 0) {
155146
return {
156-
name: Generator.enumName(defVal),
147+
name: Generator.typeName(defVal),
157148
properties: defIn.enum.map((val) => ({
158149
name: val.toString(),
159150
camelCaseName: Generator.camelCase(val.toString())
@@ -181,14 +172,14 @@ export class Generator {
181172

182173
if (property.isArray) {
183174
if (propIn.items && propIn.items.$ref) {
184-
property.type = Generator.modelName(Generator.dereferenceType(propIn.items.$ref));
175+
property.type = Generator.typeName(Generator.dereferenceType(propIn.items.$ref));
185176
} else if (propIn.items && propIn.items.type) {
186-
property.type = Generator.modelName(propIn.items.type);
177+
property.type = Generator.typeName(propIn.items.type);
187178
} else {
188179
property.type = propIn.type;
189180
}
190181
} else {
191-
property.type = Generator.modelName(
182+
property.type = Generator.typeName(
192183
propIn.$ref
193184
? Generator.dereferenceType(propIn.$ref)
194185
: propIn.type
@@ -203,7 +194,7 @@ export class Generator {
203194
.sort((a, b) => a.name && b.name ? a.name.localeCompare(b.name) : -1);
204195

205196
return {
206-
name: Generator.modelName(defVal),
197+
name: Generator.typeName(defVal),
207198
properties: properties,
208199
imports: properties
209200
.filter(({isRef}) => isRef)
@@ -227,16 +218,16 @@ export class Generator {
227218
const items = responseSchema.items;
228219
if (!Array.isArray(items)) {
229220
if (items && items.$ref) {
230-
return Generator.modelName(Generator.dereferenceType(items.$ref), true);
221+
return Generator.typeName(Generator.dereferenceType(items.$ref), true);
231222
} else if (items) {
232-
return Generator.modelName(items.type, true);
223+
return Generator.typeName(items.type, true);
233224
}
234225
} else {
235226
console.warn('Multiple type arrays are not supported');
236227
}
237228
}
238229
} else if (responseSchema && responseSchema.$ref) {
239-
return Generator.modelName(Generator.dereferenceType(responseSchema.$ref));
230+
return Generator.typeName(Generator.dereferenceType(responseSchema.$ref));
240231
}
241232
}
242233

@@ -343,8 +334,7 @@ export class Generator {
343334
.filter(([method,]) => authorizedMethods.indexOf(method.toUpperCase()) !== -1) // skip unsupported methods
344335
.map(
345336
([method, op]) => ({
346-
path: path,
347-
backTickPath: path.replace(/({.*?})/g, '$$$1'), //todo rename this
337+
path: path.replace(/({.*?})/g, '$$$1'),
348338
methodName: Generator.camelCase(
349339
op.operationId
350340
? op.operationId
@@ -362,10 +352,6 @@ export class Generator {
362352
definitions: Generator.generateDefinitions(swagger.definitions)
363353
};
364354

365-
if (data.definitions.length > 0) {
366-
data.definitions[data.definitions.length - 1].last = true;
367-
}
368-
369355
return data;
370356
}
371357
}

templates/ngx-model.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
{{#imports.length}}
44
import {
55
{{#imports}}
6-
{{.}}
6+
{{.}},
77
{{/imports}}
8-
} from '..';
8+
} from './..';
99
{{/imports.length}}
1010

1111
export interface {{&name}} {

templates/ngx-service.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Observable } from 'rxjs/Observable';
66

77
import {
88
{{#definitions}}
9-
{{name}}{{^last}},{{/last}}
9+
{{name}},
1010
{{/definitions}}
1111
} from '.';
1212

@@ -45,7 +45,7 @@ export class ApiClientService {
4545

4646
{{#methods}}
4747
{{&methodName}}({{#parameters}}{{&camelCaseName}}: {{typescriptType}}, {{/parameters}}options: HttpOptions = this.httpOptions): Observable<{{&response}}> {
48-
const path = `{{&backTickPath}}`;
48+
const path = `{{&path}}`;
4949

5050
{{#parameters}}
5151
{{#isQueryParameter}}

0 commit comments

Comments
 (0)