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

* Add support to http patch method

* Add support to rxjs6

* Align sample

* Add sample for openapi3

* Change usage of single quote to use only double ones

* Fix wrong changes of typescript-angular package.json template

* Add `map` keyword inside reservedWords

* Add typescript-inversify inside README


Add typescript-inversify inside README

* fix merge issue, update petstore

* update doc
This commit is contained in:
Gualtieri Mario
2019-05-27 17:33:07 +02:00
committed by William Cheng
parent 5167955ee0
commit 50878fbc2e
15 changed files with 199 additions and 57 deletions

View File

@@ -23,10 +23,8 @@ import io.swagger.v3.oas.models.media.Schema;
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;
@@ -36,6 +34,7 @@ public class TypeScriptInversifyClientCodegen extends AbstractTypeScriptClientCo
public static final String NPM_REPOSITORY = "npmRepository";
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 npmRepository = null;
@@ -53,6 +52,8 @@ public class TypeScriptInversifyClientCodegen extends AbstractTypeScriptClientCo
apiPackage = "api";
modelPackage = "model";
this.reservedWords.add("map");
this.cliOptions.add(new CliOption(NPM_REPOSITORY,
"Use this property to set an url your private npmRepo in the package.json"));
this.cliOptions.add(new CliOption(WITH_INTERFACES,
@@ -61,6 +62,9 @@ public class TypeScriptInversifyClientCodegen extends AbstractTypeScriptClientCo
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()));
@@ -85,7 +89,7 @@ public class TypeScriptInversifyClientCodegen extends AbstractTypeScriptClientCo
@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"));

View File

@@ -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) {
@@ -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}}
}
}

View File

@@ -1,4 +1,9 @@
{{^useRxJS6}}
import { Observable } from "rxjs/Observable";
{{/useRxJS6}}
{{#useRxJS6}}
import { Observable, from } from "rxjs";
{{/useRxJS6}}
import HttpResponse from "./HttpResponse";
import { Headers } from "./Headers";
@@ -6,6 +11,7 @@ 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>
}

View File

@@ -1,9 +1,14 @@
{{>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";
@@ -11,12 +16,12 @@ 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}}
@@ -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}};
}

View File

@@ -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}}

View File

@@ -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}}