[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

@ -672,6 +672,7 @@ Here is a list of template creators:
* TypeScript (jQuery): @bherila
* TypeScript (Node): @mhardorf
* TypeScript (Rxjs): @denyo
* TypeScript (Inversify): @gualtierim
* Server Stubs
* Ada: @stcarrez
* C# ASP.NET5: @jimschubert [:heart:](https://www.patreon.com/jimschubert)

View File

@ -19,4 +19,5 @@ sidebar_label: typescript-inversify
|npmRepository|Use this property to set an url your private npmRepo in the package.json| |null|
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|
|usePromise|Setting this property to use promise instead of observable inside every service.| |false|
|useRxJS6|Setting this property to use rxjs 6 instead of rxjs 5.| |false|
|taggedUnions|Use discriminators to create tagged unions instead of extending interfaces.| |false|

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

View File

@ -1,28 +1,34 @@
import IHttpClient from "./IHttpClient";
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) {
@ -56,7 +62,8 @@ class HttpClient implements IHttpClient {
return httpResponse;
});
});
return Observable.fromPromise(promise);
return Observable.fromPromise(promise);
}
}

View File

@ -6,6 +6,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

@ -12,18 +12,18 @@
/* tslint:disable:no-unused-variable member-ordering */
import { Observable } from "rxjs/Observable";
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
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 { ApiResponse } from '../model/apiResponse';
import { Pet } from '../model/pet';
import { ApiResponse } from "../model/apiResponse";
import { Pet } from "../model/pet";
import { COLLECTION_FORMATS } from '../variables';
import { COLLECTION_FORMATS } from "../variables";
@ -62,7 +62,9 @@ export class PetService {
const response: Observable<HttpResponse<any>> = this.httpClient.post(`${this.basePath}/pet`, body , headers);
if (observe == 'body') {
return response.map(httpResponse => <any>(httpResponse.response));
return response.pipe(
map(httpResponse => <any>(httpResponse.response))
);
}
return response;
}
@ -97,7 +99,9 @@ export class PetService {
const response: Observable<HttpResponse<any>> = this.httpClient.delete(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`, headers);
if (observe == 'body') {
return response.map(httpResponse => <any>(httpResponse.response));
return response.pipe(
map(httpResponse => <any>(httpResponse.response))
);
}
return response;
}
@ -132,7 +136,9 @@ export class PetService {
const response: Observable<HttpResponse<Array<Pet>>> = this.httpClient.get(`${this.basePath}/pet/findByStatus?${queryParameters.join('&')}`, headers);
if (observe == 'body') {
return response.map(httpResponse => <Array<Pet>>(httpResponse.response));
return response.pipe(
map(httpResponse => <Array<Pet>>(httpResponse.response))
);
}
return response;
}
@ -167,7 +173,9 @@ export class PetService {
const response: Observable<HttpResponse<Array<Pet>>> = this.httpClient.get(`${this.basePath}/pet/findByTags?${queryParameters.join('&')}`, headers);
if (observe == 'body') {
return response.map(httpResponse => <Array<Pet>>(httpResponse.response));
return response.pipe(
map(httpResponse => <Array<Pet>>(httpResponse.response))
);
}
return response;
}
@ -194,7 +202,9 @@ export class PetService {
const response: Observable<HttpResponse<Pet>> = this.httpClient.get(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`, headers);
if (observe == 'body') {
return response.map(httpResponse => <Pet>(httpResponse.response));
return response.pipe(
map(httpResponse => <Pet>(httpResponse.response))
);
}
return response;
}
@ -225,7 +235,9 @@ export class PetService {
const response: Observable<HttpResponse<any>> = this.httpClient.put(`${this.basePath}/pet`, body , headers);
if (observe == 'body') {
return response.map(httpResponse => <any>(httpResponse.response));
return response.pipe(
map(httpResponse => <any>(httpResponse.response))
);
}
return response;
}
@ -266,7 +278,9 @@ export class PetService {
const response: Observable<HttpResponse<any>> = this.httpClient.post(`${this.basePath}/pet/${encodeURIComponent(String(petId))}`, body, headers);
if (observe == 'body') {
return response.map(httpResponse => <any>(httpResponse.response));
return response.pipe(
map(httpResponse => <any>(httpResponse.response))
);
}
return response;
}
@ -307,7 +321,9 @@ export class PetService {
const response: Observable<HttpResponse<ApiResponse>> = this.httpClient.post(`${this.basePath}/pet/${encodeURIComponent(String(petId))}/uploadImage`, body, headers);
if (observe == 'body') {
return response.map(httpResponse => <ApiResponse>(httpResponse.response));
return response.pipe(
map(httpResponse => <ApiResponse>(httpResponse.response))
);
}
return response;
}

View File

@ -12,17 +12,17 @@
/* tslint:disable:no-unused-variable member-ordering */
import { Observable } from "rxjs/Observable";
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
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 { Order } from '../model/order';
import { Order } from "../model/order";
import { COLLECTION_FORMATS } from '../variables';
import { COLLECTION_FORMATS } from "../variables";
@ -53,7 +53,9 @@ export class StoreService {
const response: Observable<HttpResponse<any>> = this.httpClient.delete(`${this.basePath}/store/order/${encodeURIComponent(String(orderId))}`, headers);
if (observe == 'body') {
return response.map(httpResponse => <any>(httpResponse.response));
return response.pipe(
map(httpResponse => <any>(httpResponse.response))
);
}
return response;
}
@ -75,7 +77,9 @@ export class StoreService {
const response: Observable<HttpResponse<{ [key: string]: number; }>> = this.httpClient.get(`${this.basePath}/store/inventory`, headers);
if (observe == 'body') {
return response.map(httpResponse => <{ [key: string]: number; }>(httpResponse.response));
return response.pipe(
map(httpResponse => <{ [key: string]: number; }>(httpResponse.response))
);
}
return response;
}
@ -98,7 +102,9 @@ export class StoreService {
const response: Observable<HttpResponse<Order>> = this.httpClient.get(`${this.basePath}/store/order/${encodeURIComponent(String(orderId))}`, headers);
if (observe == 'body') {
return response.map(httpResponse => <Order>(httpResponse.response));
return response.pipe(
map(httpResponse => <Order>(httpResponse.response))
);
}
return response;
}
@ -122,7 +128,9 @@ export class StoreService {
const response: Observable<HttpResponse<Order>> = this.httpClient.post(`${this.basePath}/store/order`, body , headers);
if (observe == 'body') {
return response.map(httpResponse => <Order>(httpResponse.response));
return response.pipe(
map(httpResponse => <Order>(httpResponse.response))
);
}
return response;
}

View File

@ -12,17 +12,17 @@
/* tslint:disable:no-unused-variable member-ordering */
import { Observable } from "rxjs/Observable";
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
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 { User } from '../model/user';
import { User } from "../model/user";
import { COLLECTION_FORMATS } from '../variables';
import { COLLECTION_FORMATS } from "../variables";
@ -54,7 +54,9 @@ export class UserService {
const response: Observable<HttpResponse<any>> = this.httpClient.post(`${this.basePath}/user`, body , headers);
if (observe == 'body') {
return response.map(httpResponse => <any>(httpResponse.response));
return response.pipe(
map(httpResponse => <any>(httpResponse.response))
);
}
return response;
}
@ -78,7 +80,9 @@ export class UserService {
const response: Observable<HttpResponse<any>> = this.httpClient.post(`${this.basePath}/user/createWithArray`, body , headers);
if (observe == 'body') {
return response.map(httpResponse => <any>(httpResponse.response));
return response.pipe(
map(httpResponse => <any>(httpResponse.response))
);
}
return response;
}
@ -102,7 +106,9 @@ export class UserService {
const response: Observable<HttpResponse<any>> = this.httpClient.post(`${this.basePath}/user/createWithList`, body , headers);
if (observe == 'body') {
return response.map(httpResponse => <any>(httpResponse.response));
return response.pipe(
map(httpResponse => <any>(httpResponse.response))
);
}
return response;
}
@ -125,7 +131,9 @@ export class UserService {
const response: Observable<HttpResponse<any>> = this.httpClient.delete(`${this.basePath}/user/${encodeURIComponent(String(username))}`, headers);
if (observe == 'body') {
return response.map(httpResponse => <any>(httpResponse.response));
return response.pipe(
map(httpResponse => <any>(httpResponse.response))
);
}
return response;
}
@ -148,7 +156,9 @@ export class UserService {
const response: Observable<HttpResponse<User>> = this.httpClient.get(`${this.basePath}/user/${encodeURIComponent(String(username))}`, headers);
if (observe == 'body') {
return response.map(httpResponse => <User>(httpResponse.response));
return response.pipe(
map(httpResponse => <User>(httpResponse.response))
);
}
return response;
}
@ -184,7 +194,9 @@ export class UserService {
const response: Observable<HttpResponse<string>> = this.httpClient.get(`${this.basePath}/user/login?${queryParameters.join('&')}`, headers);
if (observe == 'body') {
return response.map(httpResponse => <string>(httpResponse.response));
return response.pipe(
map(httpResponse => <string>(httpResponse.response))
);
}
return response;
}
@ -202,7 +214,9 @@ export class UserService {
const response: Observable<HttpResponse<any>> = this.httpClient.get(`${this.basePath}/user/logout`, headers);
if (observe == 'body') {
return response.map(httpResponse => <any>(httpResponse.response));
return response.pipe(
map(httpResponse => <any>(httpResponse.response))
);
}
return response;
}
@ -231,7 +245,9 @@ export class UserService {
const response: Observable<HttpResponse<any>> = this.httpClient.put(`${this.basePath}/user/${encodeURIComponent(String(username))}`, body , headers);
if (observe == 'body') {
return response.map(httpResponse => <any>(httpResponse.response));
return response.pipe(
map(httpResponse => <any>(httpResponse.response))
);
}
return response;
}

View File

@ -0,0 +1,23 @@
/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
export interface InlineObject {
/**
* Updated name of the pet
*/
name?: string;
/**
* Updated status of the pet
*/
status?: string;
}

View File

@ -0,0 +1,23 @@
/**
* OpenAPI Petstore
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* OpenAPI spec version: 1.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
export interface InlineObject1 {
/**
* Additional data to pass to server
*/
additionalMetadata?: string;
/**
* file to upload
*/
file?: Blob;
}