Skip to content

Commit

Permalink
typescript-inversify: client compile error, type casting warning and …
Browse files Browse the repository at this point in the history
…apostrophes (#6970)
  • Loading branch information
michalzubkowicz authored Jul 23, 2020
1 parent 90d8c32 commit 1bfd86a
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 164 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {interfaces} from "inversify";
import {interfaces} from 'inversify';

{{#apiInfo}}
{{#apis}}
Expand All @@ -13,7 +13,7 @@ export class ApiServiceBinder {
public static with(container: interfaces.Container) {
{{#apiInfo}}
{{#apis}}
container.bind<{{classname}}{{#withInterfaces}}Interface{{/withInterfaces}}>("{{classname}}").to({{classname}}).inSingletonScope();
container.bind<{{classname}}{{#withInterfaces}}Interface{{/withInterfaces}}>('{{classname}}').to({{classname}}).inSingletonScope();
{{/apis}}
{{/apiInfo}}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import IHttpClient from "./IHttpClient";
import IHttpClient from './IHttpClient';

{{^useRxJS6}}
import { Observable } from "rxjs/Observable";
import { Observable } from 'rxjs/Observable';
{{/useRxJS6}}
{{#useRxJS6}}
import { Observable, from } from "rxjs";
import { Observable, from } from 'rxjs';
{{/useRxJS6}}

import "whatwg-fetch";
import HttpResponse from "./HttpResponse";
import {injectable} from "inversify";
import { Headers } from "./Headers";
import 'whatwg-fetch';
import HttpResponse from './HttpResponse';
import {injectable} from 'inversify';
import { Headers } from './Headers';

@injectable()
class HttpClient implements IHttpClient {
get(url:string, headers?: Headers):Observable<HttpResponse> {
return this.performNetworkCall(url, "GET", undefined, headers);
return this.performNetworkCall(url, 'GET', undefined, headers);
}

post(url: string, body?: {}|FormData, headers?: Headers): Observable<HttpResponse> {
return this.performNetworkCall(url, "POST", this.getJsonBody(body), this.addJsonHeaders(headers));
return this.performNetworkCall(url, 'POST', this.getJsonBody(body), this.addJsonHeaders(headers));
}

put(url: string, body?: {}, headers?: Headers): Observable<HttpResponse> {
return this.performNetworkCall(url, "PUT", this.getJsonBody(body), this.addJsonHeaders(headers));
return this.performNetworkCall(url, 'PUT', this.getJsonBody(body), this.addJsonHeaders(headers));
}

patch(url: string, body?: {}, headers?: Headers): Observable<HttpResponse> {
return this.performNetworkCall(url, "PATCH", this.getJsonBody(body), this.addJsonHeaders(headers));
return this.performNetworkCall(url, 'PATCH', this.getJsonBody(body), this.addJsonHeaders(headers));
}


delete(url: string, headers?: Headers): Observable<HttpResponse> {
return this.performNetworkCall(url, "DELETE", undefined, headers);
return this.performNetworkCall(url, 'DELETE', undefined, headers);
}

private getJsonBody(body?: {}|FormData) {
Expand All @@ -45,16 +45,16 @@ class HttpClient implements IHttpClient {

private addJsonHeaders(headers?: Headers) {
return Object.assign({}, {
"Accept": "application/json",
"Content-Type": "application/json"
'Accept': 'application/json',
'Content-Type': 'application/json'
}, headers);
};

private performNetworkCall(url: string, method: string, body?: any, headers?: Headers): Observable<HttpResponse> {
// when using fetch & a multipart upload, the requests content-type is handled by the browser, so should be left unset otherwise the multipart boundry is not added
if(headers && headers["Content-Type"] === "multipart/form-data") {
delete headers["Content-Type"];
if(headers && headers['Content-Type'] === 'multipart/form-data') {
delete headers['Content-Type'];
}

let promise = window.fetch(url, {
Expand All @@ -67,8 +67,8 @@ class HttpClient implements IHttpClient {
headers[name.toString().toLowerCase()] = value;
});
return response.text().then(text => {
let contentType = headers["content-type"] || "";
let payload = contentType.match("application/json") ? JSON.parse(text) : text;
let contentType = headers['content-type'] || '';
let payload = contentType.match('application/json') ? JSON.parse(text) : text;
let httpResponse = new HttpResponse(payload, response.status, headers);
if (response.status >= 400)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Headers } from "./Headers"
import { Headers } from './Headers'

class HttpResponse<T = any> {
constructor(public response: T, public status:number, public headers?: Headers) {
}
}

export default HttpResponse
export default HttpResponse
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{{^useRxJS6}}
import { Observable } from "rxjs/Observable";
import { Observable } from 'rxjs/Observable';
{{/useRxJS6}}
{{#useRxJS6}}
import { Observable, from } from "rxjs";
import { Observable, from } from 'rxjs';
{{/useRxJS6}}
import HttpResponse from "./HttpResponse";
import { Headers } from "./Headers";
import HttpResponse from './HttpResponse';
import { Headers } from './Headers';

interface IHttpClient {
get(url:string, headers?: Headers):Observable<HttpResponse>
Expand All @@ -15,4 +15,4 @@ interface IHttpClient {
delete(url:string, headers?: Headers):Observable<HttpResponse>
}

export default IHttpClient
export default IHttpClient
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
/* tslint:disable:no-unused-variable member-ordering */

{{^useRxJS6}}
import { Observable } from "rxjs/Observable";
import { Observable } from 'rxjs/Observable';
{{/useRxJS6}}
{{#useRxJS6}}
import { Observable } from "rxjs";
import { Observable } from 'rxjs';
{{/useRxJS6}}

import { map } from "rxjs/operators";
import IHttpClient from "../IHttpClient";
import { inject, injectable } from "inversify";
import { IAPIConfiguration } from "../IAPIConfiguration";
import { Headers } from "../Headers";
import HttpResponse from "../HttpResponse";
import { map } from 'rxjs/operators';
import IHttpClient from '../IHttpClient';
import { inject, injectable } from 'inversify';
import { IAPIConfiguration } from '../IAPIConfiguration';
import { Headers } from '../Headers';
import HttpResponse from '../HttpResponse';

{{#imports}}
import { {{classname}} } from "../{{filename}}";
import { {{classname}} } from '../{{filename}}';
{{/imports}}

import { COLLECTION_FORMATS } from "../variables";
import { COLLECTION_FORMATS } from '../variables';
{{#withInterfaces}}
import { {{classname}}Interface } from "./{{classFilename}}Interface";
import { {{classname}}Interface } from './{{classFilename}}Interface';
{{/withInterfaces}}

{{#operations}}
Expand All @@ -41,8 +41,8 @@ export class {{classname}} {
{{/withInterfaces}}
private basePath: string = '{{{basePath}}}';

constructor(@inject("IApiHttpClient") private httpClient: IHttpClient,
@inject("IAPIConfiguration") private APIConfiguration: IAPIConfiguration ) {
constructor(@inject('IApiHttpClient') private httpClient: IHttpClient,
@inject('IAPIConfiguration') private APIConfiguration: IAPIConfiguration ) {
if(this.APIConfiguration.basePath)
this.basePath = this.APIConfiguration.basePath;
}
Expand Down Expand Up @@ -73,21 +73,21 @@ export class {{classname}} {
if ({{paramName}}) {
{{#isCollectionFormatMulti}}
{{paramName}}.forEach((element) => {
queryParameters.push("{{paramName}}="+encodeURIComponent(String({{paramName}})));
queryParameters.push('{{paramName}}='+encodeURIComponent(String({{paramName}})));
})
{{/isCollectionFormatMulti}}
{{^isCollectionFormatMulti}}
queryParameters.push("{{paramName}}="+encodeURIComponent({{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}'])));
queryParameters.push('{{paramName}}='+encodeURIComponent({{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}'])));
{{/isCollectionFormatMulti}}
}
{{/isListContainer}}
{{^isListContainer}}
if ({{paramName}} !== undefined) {
{{#isDateTime}}
queryParameters.push("{{paramName}}="+encodeURIComponent(<any>{{paramName}}.toISOString()));
queryParameters.push('{{paramName}}='+encodeURIComponent(({{paramName}} as Date).toISOString()));
{{/isDateTime}}
{{^isDateTime}}
queryParameters.push("{{paramName}}="+encodeURIComponent(String({{paramName}})));
queryParameters.push('{{paramName}}='+encodeURIComponent(String({{paramName}})));
{{/isDateTime}}
}
{{/isListContainer}}
Expand All @@ -111,13 +111,13 @@ export class {{classname}} {
// authentication ({{name}}) required
{{#isApiKey}}
{{#isKeyInHeader}}
if (this.APIConfiguration.apiKeys && this.APIConfiguration.apiKeys["{{keyParamName}}"]) {
headers['{{keyParamName}}'] = this.APIConfiguration.apiKeys["{{keyParamName}}"];
if (this.APIConfiguration.apiKeys && this.APIConfiguration.apiKeys['{{keyParamName}}']) {
headers['{{keyParamName}}'] = this.APIConfiguration.apiKeys['{{keyParamName}}'];
}
{{/isKeyInHeader}}
{{#isKeyInQuery}}
if (this.APIConfiguration.apiKeys && this.APIConfiguration.apiKeys["{{keyParamName}}"]) {
queryParameters.push("{{paramName}}="+encodeURIComponent(String(this.APIConfiguration.apiKeys["{{keyParamName}}"])));
if (this.APIConfiguration.apiKeys && this.APIConfiguration.apiKeys['{{keyParamName}}']) {
queryParameters.push('{{paramName}}='+encodeURIComponent(String(this.APIConfiguration.apiKeys['{{keyParamName}}'])));
}
{{/isKeyInQuery}}
{{/isApiKey}}
Expand Down Expand Up @@ -180,9 +180,9 @@ export class {{classname}} {

{{/hasFormParams}}
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}}, formData{{/hasFormParams}}, headers);
if (observe == 'body') {
if (observe === 'body') {
return response.pipe(
map(httpResponse => <{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>(httpResponse.response))
map((httpResponse: HttpResponse) => <{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>(httpResponse.response))
){{#usePromise}}.toPromise(){{/usePromise}};
}
return response{{#usePromise}}.toPromise(){{/usePromise}};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{{>licenseInfo}}
import { Headers } from "../Headers";
import { Headers } from '../Headers';
{{^useRxJS6}}
import { Observable } from "rxjs/Observable";
import { Observable } from 'rxjs/Observable';
{{/useRxJS6}}
{{#useRxJS6}}
import { Observable } from "rxjs";
import { Observable } from 'rxjs';
{{/useRxJS6}}
{{#imports}}
import { {{classname}} } from "../{{filename}}";
import { {{classname}} } from '../{{filename}}';
{{/imports}}
import HttpResponse from "../HttpResponse";
import HttpResponse from '../HttpResponse';

{{#operations}}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {interfaces} from "inversify";
import {interfaces} from 'inversify';

import { PetService } from './api/pet.service';
import { StoreService } from './api/store.service';
import { UserService } from './api/user.service';

export class ApiServiceBinder {
public static with(container: interfaces.Container) {
container.bind<PetService>("PetService").to(PetService).inSingletonScope();
container.bind<StoreService>("StoreService").to(StoreService).inSingletonScope();
container.bind<UserService>("UserService").to(UserService).inSingletonScope();
container.bind<PetService>('PetService').to(PetService).inSingletonScope();
container.bind<StoreService>('StoreService').to(StoreService).inSingletonScope();
container.bind<UserService>('UserService').to(UserService).inSingletonScope();
}
}
34 changes: 17 additions & 17 deletions samples/client/petstore/typescript-inversify/HttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
import IHttpClient from "./IHttpClient";
import IHttpClient from './IHttpClient';

import { Observable } from "rxjs/Observable";
import { Observable } from 'rxjs/Observable';

import "whatwg-fetch";
import HttpResponse from "./HttpResponse";
import {injectable} from "inversify";
import { Headers } from "./Headers";
import 'whatwg-fetch';
import HttpResponse from './HttpResponse';
import {injectable} from 'inversify';
import { Headers } from './Headers';

@injectable()
class HttpClient implements IHttpClient {

get(url:string, headers?: Headers):Observable<HttpResponse> {
return this.performNetworkCall(url, "GET", undefined, headers);
return this.performNetworkCall(url, 'GET', undefined, headers);
}

post(url: string, body?: {}|FormData, headers?: Headers): Observable<HttpResponse> {
return this.performNetworkCall(url, "POST", this.getJsonBody(body), this.addJsonHeaders(headers));
return this.performNetworkCall(url, 'POST', this.getJsonBody(body), this.addJsonHeaders(headers));
}

put(url: string, body?: {}, headers?: Headers): Observable<HttpResponse> {
return this.performNetworkCall(url, "PUT", this.getJsonBody(body), this.addJsonHeaders(headers));
return this.performNetworkCall(url, 'PUT', this.getJsonBody(body), this.addJsonHeaders(headers));
}

patch(url: string, body?: {}, headers?: Headers): Observable<HttpResponse> {
return this.performNetworkCall(url, "PATCH", this.getJsonBody(body), this.addJsonHeaders(headers));
return this.performNetworkCall(url, 'PATCH', this.getJsonBody(body), this.addJsonHeaders(headers));
}


delete(url: string, headers?: Headers): Observable<HttpResponse> {
return this.performNetworkCall(url, "DELETE", undefined, headers);
return this.performNetworkCall(url, 'DELETE', undefined, headers);
}

private getJsonBody(body?: {}|FormData) {
Expand All @@ -40,16 +40,16 @@ class HttpClient implements IHttpClient {

private addJsonHeaders(headers?: Headers) {
return Object.assign({}, {
"Accept": "application/json",
"Content-Type": "application/json"
'Accept': 'application/json',
'Content-Type': 'application/json'
}, headers);
};

private performNetworkCall(url: string, method: string, body?: any, headers?: Headers): Observable<HttpResponse> {

// when using fetch & a multipart upload, the requests content-type is handled by the browser, so should be left unset otherwise the multipart boundry is not added
if(headers && headers["Content-Type"] === "multipart/form-data") {
delete headers["Content-Type"];
if(headers && headers['Content-Type'] === 'multipart/form-data') {
delete headers['Content-Type'];
}

let promise = window.fetch(url, {
Expand All @@ -62,8 +62,8 @@ class HttpClient implements IHttpClient {
headers[name.toString().toLowerCase()] = value;
});
return response.text().then(text => {
let contentType = headers["content-type"] || "";
let payload = contentType.match("application/json") ? JSON.parse(text) : text;
let contentType = headers['content-type'] || '';
let payload = contentType.match('application/json') ? JSON.parse(text) : text;
let httpResponse = new HttpResponse(payload, response.status, headers);

if (response.status >= 400)
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/typescript-inversify/HttpResponse.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Headers } from "./Headers"
import { Headers } from './Headers'

class HttpResponse<T = any> {
constructor(public response: T, public status:number, public headers?: Headers) {
}
}

export default HttpResponse
export default HttpResponse
8 changes: 4 additions & 4 deletions samples/client/petstore/typescript-inversify/IHttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Observable } from "rxjs/Observable";
import HttpResponse from "./HttpResponse";
import { Headers } from "./Headers";
import { Observable } from 'rxjs/Observable';
import HttpResponse from './HttpResponse';
import { Headers } from './Headers';

interface IHttpClient {
get(url:string, headers?: Headers):Observable<HttpResponse>
Expand All @@ -10,4 +10,4 @@ interface IHttpClient {
delete(url:string, headers?: Headers):Observable<HttpResponse>
}

export default IHttpClient
export default IHttpClient
Loading

0 comments on commit 1bfd86a

Please sign in to comment.