Skip to content

Commit 7c2130b

Browse files
Moved DI classses from contructor to properties (api.service.mustache) Removed type casting when not needed (TS showed warning about that) Replaced == with === Removed redundant local variable basePath. fixed quotemarks
1 parent 8e056ef commit 7c2130b

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

modules/swagger-codegen/src/main/resources/typescript-inversify/api.service.mustache

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{{>licenseInfo}}
22
/* tslint:disable:no-unused-variable member-ordering */
33

4-
import { Observable } from "rxjs/Observable";
4+
import { Observable } from 'rxjs/Observable';
55
import 'rxjs/add/operator/map';
66
import 'rxjs/add/operator/toPromise';
7-
import IHttpClient from "../IHttpClient";
8-
import { inject, injectable } from "inversify";
9-
import { IAPIConfiguration } from "../IAPIConfiguration";
10-
import { Headers } from "../Headers";
11-
import HttpResponse from "../HttpResponse";
7+
import IHttpClient from '../IHttpClient';
8+
import { inject, injectable } from 'inversify';
9+
import { IAPIConfiguration } from '../IAPIConfiguration';
10+
import { Headers } from '../Headers';
11+
import HttpResponse from '../HttpResponse';
1212

1313
{{#imports}}
1414
import { {{classname}} } from '../{{filename}}';
@@ -34,13 +34,10 @@ export class {{classname}} implements {{classname}}Interface {
3434
{{^withInterfaces}}
3535
export class {{classname}} {
3636
{{/withInterfaces}}
37-
private basePath: string = '{{{basePath}}}';
37+
@inject('IAPIConfiguration') private APIConfiguration: IAPIConfiguration;
38+
@inject('IApiHttpClient') private httpClient: IHttpClient;
39+
3840

39-
constructor(@inject("IApiHttpClient") private httpClient: IHttpClient,
40-
@inject("IAPIConfiguration") private APIConfiguration: IAPIConfiguration ) {
41-
if(this.APIConfiguration.basePath)
42-
this.basePath = this.APIConfiguration.basePath;
43-
}
4441
{{#operation}}
4542

4643
/**
@@ -68,21 +65,21 @@ export class {{classname}} {
6865
if ({{paramName}}) {
6966
{{#isCollectionFormatMulti}}
7067
{{paramName}}.forEach((element) => {
71-
queryParameters.push("{{paramName}}="+encodeURIComponent(String({{paramName}})));
68+
queryParameters.push('{{paramName}}='+encodeURIComponent(String({{paramName}})));
7269
})
7370
{{/isCollectionFormatMulti}}
7471
{{^isCollectionFormatMulti}}
75-
queryParameters.push("{{paramName}}="+encodeURIComponent({{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}'])));
72+
queryParameters.push('{{paramName}}='+encodeURIComponent({{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}'])));
7673
{{/isCollectionFormatMulti}}
7774
}
7875
{{/isListContainer}}
7976
{{^isListContainer}}
8077
if ({{paramName}} !== undefined) {
8178
{{#isDateTime}}
82-
queryParameters.push("{{paramName}}="+encodeURIComponent(<any>{{paramName}}.toISOString()));
79+
queryParameters.push('{{paramName}}='+encodeURIComponent(<any>{{paramName}}.toISOString()));
8380
{{/isDateTime}}
8481
{{^isDateTime}}
85-
queryParameters.push("{{paramName}}="+encodeURIComponent(String({{paramName}})));
82+
queryParameters.push('{{paramName}}='+encodeURIComponent(String({{paramName}})));
8683
{{/isDateTime}}
8784
}
8885
{{/isListContainer}}
@@ -106,13 +103,13 @@ export class {{classname}} {
106103
// authentication ({{name}}) required
107104
{{#isApiKey}}
108105
{{#isKeyInHeader}}
109-
if (this.APIConfiguration.apiKeys["{{keyParamName}}"]) {
110-
headers['{{keyParamName}}'] = this.APIConfiguration.apiKeys["{{keyParamName}}"];
106+
if (this.APIConfiguration.apiKeys['{{keyParamName}}']) {
107+
headers['{{keyParamName}}'] = this.APIConfiguration.apiKeys['{{keyParamName}}'];
111108
}
112109
{{/isKeyInHeader}}
113110
{{#isKeyInQuery}}
114-
if (this.APIConfiguration.apiKeys["{{keyParamName}}"]) {
115-
queryParameters.push("{{paramName}}="+encodeURIComponent(String(this.APIConfiguration.apiKeys["{{keyParamName}}"])));
111+
if (this.APIConfiguration.apiKeys['{{keyParamName}}']) {
112+
queryParameters.push('{{paramName}}='+encodeURIComponent(String(this.APIConfiguration.apiKeys['{{keyParamName}}'])));
116113
}
117114
{{/isKeyInQuery}}
118115
{{/isApiKey}}
@@ -169,9 +166,9 @@ export class {{classname}} {
169166
{{/formParams}}
170167

171168
{{/hasFormParams}}
172-
const response: Observable<HttpResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>> = this.httpClient.{{httpMethod}}(`${this.basePath}{{{path}}}{{#hasQueryParams}}?${queryParameters.join('&')}{{/hasQueryParams}}`{{#bodyParam}}, {{paramName}} {{/bodyParam}}{{#hasFormParams}}, body{{/hasFormParams}}, headers);
173-
if (observe == 'body') {
174-
return response.map(httpResponse => <{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>(httpResponse.response)){{#usePromise}}.toPromise(){{/usePromise}};
169+
const response: Observable<HttpResponse<{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>> = this.httpClient.{{httpMethod}}(`${this.APIConfiguration.basePath}{{{path}}}{{#hasQueryParams}}?${queryParameters.join('&')}{{/hasQueryParams}}`{{#bodyParam}}, {{paramName}}{{/bodyParam}} as any{{#hasFormParams}}, body{{/hasFormParams}}, headers);
170+
if (observe === 'body') {
171+
return response.map(httpResponse => httpResponse.response){{#usePromise}}.toPromise(){{/usePromise}};
175172
}
176173
return response{{#usePromise}}.toPromise(){{/usePromise}};
177174
}

0 commit comments

Comments
 (0)