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 All @@ -58,6 +57,8 @@ public TypeScriptInversifyClientCodegen() {
apiPackage = "api";
modelPackage = "model";

this.reservedWords.add("map");

this.cliOptions.add(new CliOption(NPM_NAME, "The name under which you want to publish generated npm package"));
this.cliOptions.add(new CliOption(NPM_VERSION, "The version of your npm package"));
this.cliOptions.add(new CliOption(NPM_REPOSITORY,
Expand All @@ -71,6 +72,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 +99,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
@@ -1,28 +1,39 @@
import IHttpClient from "./IHttpClient";

{{^useRxJS6}}
import { Observable } from "rxjs/Observable";
{{/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 @@
{{^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,22 +1,27 @@
{{>licenseInfo}}
/* tslint:disable:no-unused-variable member-ordering */

{{^useRxJS6}}
import { Observable } from "rxjs/Observable";
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
{{/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";
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 './{{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";
{{^useRxJS6}}
import { Observable } from "rxjs/Observable";
import * as models from "../model/models";
{{/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,9 +16,14 @@
},
"dependencies": {
"inversify": "^4.3.0",
"rxjs": "~5.5.7",
"whatwg-fetch": "~2.0.1",
"reflect-metadata": "0.1.8"
"reflect-metadata": "0.1.8",
{{^useRxJS6}}
"rxjs": "^6.0.0"
{{/useRxJS6}}
{{#useRxJS6}}
"rxjs": "^5.0.0"
{{/useRxJS6}}
},
"devDependencies": {
}{{#npmRepository}},{{/npmRepository}}
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