Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS][Inverisify] Adding support for RxJS 6 #2793

Merged
merged 11 commits into from
May 27, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.openapitools.codegen.*;
import org.openapitools.codegen.utils.ModelUtils;
import org.openapitools.codegen.utils.StringUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

import static org.openapitools.codegen.utils.StringUtils.camelize;
Expand All @@ -39,6 +37,7 @@ public class TypeScriptInversifyClientCodegen extends AbstractTypeScriptClientCo
public static final String SNAPSHOT = "snapshot";
public static final String WITH_INTERFACES = "withInterfaces";
public static final String USE_PROMISE = "usePromise";
public static final String USE_RXJS6 = "useRxJS6";
public static final String TAGGED_UNIONS = "taggedUnions";

protected String npmVersion = null;
Expand Down Expand Up @@ -71,6 +70,9 @@ public TypeScriptInversifyClientCodegen() {
this.cliOptions.add(new CliOption(USE_PROMISE,
"Setting this property to use promise instead of observable inside every service.",
SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(USE_RXJS6,
"Setting this property to use rxjs 6 instead of rxjs 5.",
SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(TAGGED_UNIONS,
"Use discriminators to create tagged unions instead of extending interfaces.",
SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
Expand All @@ -95,7 +97,7 @@ public String getHelp() {
@Override
public void processOpts() {
super.processOpts();
// HttpClient
// HttpCliens
supportingFiles.add(new SupportingFile("IHttpClient.mustache", getIndexDirectory(), "IHttpClient.ts"));
supportingFiles.add(new SupportingFile("IAPIConfiguration.mustache", getIndexDirectory(), "IAPIConfiguration.ts"));
supportingFiles.add(new SupportingFile("HttpClient.mustache", getIndexDirectory(), "HttpClient.ts"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
"@angular/compiler": "^{{ngVersion}}",
"core-js": "^2.4.0",
"reflect-metadata": "^0.1.3",
"rxjs": "^{{rxjsVersion}}"
{{^useRxJS6}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this changed? it is not related to typescript-inversify

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a config's propertyuseRxJS6 to allow the user to decide the using of rxjs6 or rxjs5

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here you are modifying the typescript-angular file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I did a big mistake

"rxjs": "^6.0.0"
{{/useRxJS6}}
{{#useRxJS6}}
"rxjs": "^5.0.0"
{{/useRxJS6}}
},
"devDependencies": {
"@angular/common": "^{{ngVersion}}",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
import IHttpClient from "./IHttpClient";
import { Observable } from "rxjs/Observable";

{{^useRxJS6}}
import { Observable } from 'rxjs/Observable';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i suggest to use double quotes as in the rest of the code

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

import "whatwg-fetch";
import HttpResponse from "./HttpResponse";
import {injectable} from "inversify";
import "rxjs/add/observable/fromPromise";
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));
}


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 Down Expand Up @@ -56,7 +67,13 @@ class HttpClient implements IHttpClient {
return httpResponse;
});
});
return Observable.fromPromise(promise);

{{^useRxJS6}}
return Observable.fromPromise(promise);
{{/useRxJS6}}
{{#useRxJS6}}
return from(promise);
{{/useRxJS6}}
}
}

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

interface IHttpClient {
get(url:string, headers?: Headers):Observable<HttpResponse>
post(url:string, body:{}|FormData, headers?: Headers):Observable<HttpResponse>
put(url:string, body:{}, headers?: Headers):Observable<HttpResponse>
patch(url:string, body:{}, headers?: Headers):Observable<HttpResponse>
delete(url:string, headers?: Headers):Observable<HttpResponse>
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
{{>licenseInfo}}
/* tslint:disable:no-unused-variable member-ordering */

import { Observable } from "rxjs/Observable";
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
{{^useRxJS6}}
import { Observable } from 'rxjs/Observable';
{{/useRxJS6}}
{{#useRxJS6}}
import { Observable } from 'rxjs';
{{/useRxJS6}}

import { map } from 'rxjs/operators';
import IHttpClient from "../IHttpClient";
import { inject, injectable } from "inversify";
import { IAPIConfiguration } from "../IAPIConfiguration";
Expand All @@ -16,7 +21,7 @@ import { {{classname}} } from '../{{filename}}';

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

{{#operations}}
Expand Down Expand Up @@ -171,7 +176,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}}, body{{/hasFormParams}}, headers);
if (observe == 'body') {
return response.map(httpResponse => <{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>(httpResponse.response)){{#usePromise}}.toPromise(){{/usePromise}};
return response.pipe(
map(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,7 +1,14 @@
{{>licenseInfo}}
import { Headers } from "../Headers";
import { Observable } from "rxjs/Observable";
import * as models from "../model/models";
{{^useRxJS6}}
import { Observable } from 'rxjs/Observable';
{{/useRxJS6}}
{{#useRxJS6}}
import { Observable } from 'rxjs';
{{/useRxJS6}}
{{#imports}}
import { {{classname}} } from '../{{filename}}';
{{/imports}}
import HttpResponse from "../HttpResponse";

{{#operations}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"dependencies": {
"inversify": "^4.3.0",
"rxjs": "~5.5.7",
"rxjs": "^6.0.0",
"whatwg-fetch": "~2.0.1",
"reflect-metadata": "0.1.8"
},
Expand Down
21 changes: 14 additions & 7 deletions samples/client/petstore/typescript-inversify/HttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
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 "rxjs/add/observable/fromPromise";
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));
}


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 Down Expand Up @@ -56,7 +62,8 @@ class HttpClient implements IHttpClient {
return httpResponse;
});
});
return Observable.fromPromise(promise);

return Observable.fromPromise(promise);
}
}

Expand Down
3 changes: 2 additions & 1 deletion samples/client/petstore/typescript-inversify/IHttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Observable } from "rxjs/Observable";
import { Observable } from 'rxjs/Observable';
import HttpResponse from "./HttpResponse";
import { Headers } from "./Headers";

interface IHttpClient {
get(url:string, headers?: Headers):Observable<HttpResponse>
post(url:string, body:{}|FormData, headers?: Headers):Observable<HttpResponse>
put(url:string, body:{}, headers?: Headers):Observable<HttpResponse>
patch(url:string, body:{}, headers?: Headers):Observable<HttpResponse>
delete(url:string, headers?: Headers):Observable<HttpResponse>
}

Expand Down
Loading