Skip to content

Commit 50878fb

Browse files
gualtierimwing328
authored andcommitted
[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
1 parent 5167955 commit 50878fb

15 files changed

Lines changed: 199 additions & 57 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,7 @@ Here is a list of template creators:
672672
* TypeScript (jQuery): @bherila
673673
* TypeScript (Node): @mhardorf
674674
* TypeScript (Rxjs): @denyo
675+
* TypeScript (Inversify): @gualtierim
675676
* Server Stubs
676677
* Ada: @stcarrez
677678
* C# ASP.NET5: @jimschubert [:heart:](https://www.patreon.com/jimschubert)

docs/generators/typescript-inversify.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ sidebar_label: typescript-inversify
1919
|npmRepository|Use this property to set an url your private npmRepo in the package.json| |null|
2020
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|
2121
|usePromise|Setting this property to use promise instead of observable inside every service.| |false|
22+
|useRxJS6|Setting this property to use rxjs 6 instead of rxjs 5.| |false|
2223
|taggedUnions|Use discriminators to create tagged unions instead of extending interfaces.| |false|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptInversifyClientCodegen.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
import io.swagger.v3.parser.util.SchemaTypeUtil;
2424
import org.openapitools.codegen.*;
2525
import org.openapitools.codegen.utils.ModelUtils;
26-
import org.openapitools.codegen.utils.StringUtils;
2726

2827
import java.io.File;
29-
import java.text.SimpleDateFormat;
3028
import java.util.*;
3129

3230
import static org.openapitools.codegen.utils.StringUtils.camelize;
@@ -36,6 +34,7 @@ public class TypeScriptInversifyClientCodegen extends AbstractTypeScriptClientCo
3634
public static final String NPM_REPOSITORY = "npmRepository";
3735
public static final String WITH_INTERFACES = "withInterfaces";
3836
public static final String USE_PROMISE = "usePromise";
37+
public static final String USE_RXJS6 = "useRxJS6";
3938
public static final String TAGGED_UNIONS = "taggedUnions";
4039

4140
protected String npmRepository = null;
@@ -53,6 +52,8 @@ public TypeScriptInversifyClientCodegen() {
5352
apiPackage = "api";
5453
modelPackage = "model";
5554

55+
this.reservedWords.add("map");
56+
5657
this.cliOptions.add(new CliOption(NPM_REPOSITORY,
5758
"Use this property to set an url your private npmRepo in the package.json"));
5859
this.cliOptions.add(new CliOption(WITH_INTERFACES,
@@ -61,6 +62,9 @@ public TypeScriptInversifyClientCodegen() {
6162
this.cliOptions.add(new CliOption(USE_PROMISE,
6263
"Setting this property to use promise instead of observable inside every service.",
6364
SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
65+
this.cliOptions.add(new CliOption(USE_RXJS6,
66+
"Setting this property to use rxjs 6 instead of rxjs 5.",
67+
SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
6468
this.cliOptions.add(new CliOption(TAGGED_UNIONS,
6569
"Use discriminators to create tagged unions instead of extending interfaces.",
6670
SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
@@ -85,7 +89,7 @@ public String getHelp() {
8589
@Override
8690
public void processOpts() {
8791
super.processOpts();
88-
// HttpClient
92+
// HttpCliens
8993
supportingFiles.add(new SupportingFile("IHttpClient.mustache", getIndexDirectory(), "IHttpClient.ts"));
9094
supportingFiles.add(new SupportingFile("IAPIConfiguration.mustache", getIndexDirectory(), "IAPIConfiguration.ts"));
9195
supportingFiles.add(new SupportingFile("HttpClient.mustache", getIndexDirectory(), "HttpClient.ts"));

modules/openapi-generator/src/main/resources/typescript-inversify/HttpClient.mustache

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
import IHttpClient from "./IHttpClient";
2+
3+
{{^useRxJS6}}
24
import { Observable } from "rxjs/Observable";
5+
{{/useRxJS6}}
6+
{{#useRxJS6}}
7+
import { Observable, from } from "rxjs";
8+
{{/useRxJS6}}
9+
310
import "whatwg-fetch";
411
import HttpResponse from "./HttpResponse";
512
import {injectable} from "inversify";
6-
import "rxjs/add/observable/fromPromise";
713
import { Headers } from "./Headers";
814

915
@injectable()
1016
class HttpClient implements IHttpClient {
1117
1218
get(url:string, headers?: Headers):Observable<HttpResponse> {
13-
return this.performNetworkCall(url, "get", undefined, headers);
19+
return this.performNetworkCall(url, "GET", undefined, headers);
1420
}
1521

1622
post(url: string, body: {}|FormData, headers?: Headers): Observable<HttpResponse> {
17-
return this.performNetworkCall(url, "post", this.getJsonBody(body), this.addJsonHeaders(headers));
23+
return this.performNetworkCall(url, "POST", this.getJsonBody(body), this.addJsonHeaders(headers));
1824
}
1925

2026
put(url: string, body: {}, headers?: Headers): Observable<HttpResponse> {
21-
return this.performNetworkCall(url, "put", this.getJsonBody(body), this.addJsonHeaders(headers));
27+
return this.performNetworkCall(url, "PUT", this.getJsonBody(body), this.addJsonHeaders(headers));
28+
}
29+
30+
patch(url: string, body: {}, headers?: Headers): Observable<HttpResponse> {
31+
return this.performNetworkCall(url, "PATCH", this.getJsonBody(body), this.addJsonHeaders(headers));
2232
}
2333

34+
2435
delete(url: string, headers?: Headers): Observable<HttpResponse> {
25-
return this.performNetworkCall(url, "delete", undefined, headers);
36+
return this.performNetworkCall(url, "DELETE", undefined, headers);
2637
}
2738

2839
private getJsonBody(body: {}|FormData) {
@@ -56,7 +67,13 @@ class HttpClient implements IHttpClient {
5667
return httpResponse;
5768
});
5869
});
59-
return Observable.fromPromise(promise);
70+
71+
{{^useRxJS6}}
72+
return Observable.fromPromise(promise);
73+
{{/useRxJS6}}
74+
{{#useRxJS6}}
75+
return from(promise);
76+
{{/useRxJS6}}
6077
}
6178
}
6279

modules/openapi-generator/src/main/resources/typescript-inversify/IHttpClient.mustache

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
{{^useRxJS6}}
12
import { Observable } from "rxjs/Observable";
3+
{{/useRxJS6}}
4+
{{#useRxJS6}}
5+
import { Observable, from } from "rxjs";
6+
{{/useRxJS6}}
27
import HttpResponse from "./HttpResponse";
38
import { Headers } from "./Headers";
49

510
interface IHttpClient {
611
get(url:string, headers?: Headers):Observable<HttpResponse>
712
post(url:string, body:{}|FormData, headers?: Headers):Observable<HttpResponse>
813
put(url:string, body:{}, headers?: Headers):Observable<HttpResponse>
14+
patch(url:string, body:{}, headers?: Headers):Observable<HttpResponse>
915
delete(url:string, headers?: Headers):Observable<HttpResponse>
1016
}
1117

modules/openapi-generator/src/main/resources/typescript-inversify/api.service.mustache

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
{{>licenseInfo}}
22
/* tslint:disable:no-unused-variable member-ordering */
33

4+
{{^useRxJS6}}
45
import { Observable } from "rxjs/Observable";
5-
import 'rxjs/add/operator/map';
6-
import 'rxjs/add/operator/toPromise';
6+
{{/useRxJS6}}
7+
{{#useRxJS6}}
8+
import { Observable } from "rxjs";
9+
{{/useRxJS6}}
10+
11+
import { map } from "rxjs/operators";
712
import IHttpClient from "../IHttpClient";
813
import { inject, injectable } from "inversify";
914
import { IAPIConfiguration } from "../IAPIConfiguration";
1015
import { Headers } from "../Headers";
1116
import HttpResponse from "../HttpResponse";
1217

1318
{{#imports}}
14-
import { {{classname}} } from '../{{filename}}';
19+
import { {{classname}} } from "../{{filename}}";
1520
{{/imports}}
1621

17-
import { COLLECTION_FORMATS } from '../variables';
22+
import { COLLECTION_FORMATS } from "../variables";
1823
{{#withInterfaces}}
19-
import { {{classname}}Interface } from './{{classname}}Interface';
24+
import { {{classname}}Interface } from "./{{classFilename}}Interface";
2025
{{/withInterfaces}}
2126

2227
{{#operations}}
@@ -171,7 +176,9 @@ export class {{classname}} {
171176
{{/hasFormParams}}
172177
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);
173178
if (observe == 'body') {
174-
return response.map(httpResponse => <{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>(httpResponse.response)){{#usePromise}}.toPromise(){{/usePromise}};
179+
return response.pipe(
180+
map(httpResponse => <{{#returnType}}{{{returnType}}}{{#isResponseTypeFile}}|undefined{{/isResponseTypeFile}}{{/returnType}}{{^returnType}}any{{/returnType}}>(httpResponse.response))
181+
){{#usePromise}}.toPromise(){{/usePromise}};
175182
}
176183
return response{{#usePromise}}.toPromise(){{/usePromise}};
177184
}

modules/openapi-generator/src/main/resources/typescript-inversify/apiInterface.mustache

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
{{>licenseInfo}}
22
import { Headers } from "../Headers";
3+
{{^useRxJS6}}
34
import { Observable } from "rxjs/Observable";
4-
import * as models from "../model/models";
5+
{{/useRxJS6}}
6+
{{#useRxJS6}}
7+
import { Observable } from "rxjs";
8+
{{/useRxJS6}}
9+
{{#imports}}
10+
import { {{classname}} } from "../{{filename}}";
11+
{{/imports}}
512
import HttpResponse from "../HttpResponse";
613

714
{{#operations}}

modules/openapi-generator/src/main/resources/typescript-inversify/package.mustache

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
},
1717
"dependencies": {
1818
"inversify": "^4.3.0",
19-
"rxjs": "~5.5.7",
2019
"whatwg-fetch": "~2.0.1",
21-
"reflect-metadata": "0.1.8"
20+
"reflect-metadata": "0.1.8",
21+
{{^useRxJS6}}
22+
"rxjs": "^6.0.0"
23+
{{/useRxJS6}}
24+
{{#useRxJS6}}
25+
"rxjs": "^5.0.0"
26+
{{/useRxJS6}}
2227
},
2328
"devDependencies": {
2429
}{{#npmRepository}},{{/npmRepository}}

samples/client/petstore/typescript-inversify/HttpClient.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
import IHttpClient from "./IHttpClient";
2+
23
import { Observable } from "rxjs/Observable";
4+
35
import "whatwg-fetch";
46
import HttpResponse from "./HttpResponse";
57
import {injectable} from "inversify";
6-
import "rxjs/add/observable/fromPromise";
78
import { Headers } from "./Headers";
89

910
@injectable()
1011
class HttpClient implements IHttpClient {
1112

1213
get(url:string, headers?: Headers):Observable<HttpResponse> {
13-
return this.performNetworkCall(url, "get", undefined, headers);
14+
return this.performNetworkCall(url, "GET", undefined, headers);
1415
}
1516

1617
post(url: string, body: {}|FormData, headers?: Headers): Observable<HttpResponse> {
17-
return this.performNetworkCall(url, "post", this.getJsonBody(body), this.addJsonHeaders(headers));
18+
return this.performNetworkCall(url, "POST", this.getJsonBody(body), this.addJsonHeaders(headers));
1819
}
1920

2021
put(url: string, body: {}, headers?: Headers): Observable<HttpResponse> {
21-
return this.performNetworkCall(url, "put", this.getJsonBody(body), this.addJsonHeaders(headers));
22+
return this.performNetworkCall(url, "PUT", this.getJsonBody(body), this.addJsonHeaders(headers));
23+
}
24+
25+
patch(url: string, body: {}, headers?: Headers): Observable<HttpResponse> {
26+
return this.performNetworkCall(url, "PATCH", this.getJsonBody(body), this.addJsonHeaders(headers));
2227
}
2328

29+
2430
delete(url: string, headers?: Headers): Observable<HttpResponse> {
25-
return this.performNetworkCall(url, "delete", undefined, headers);
31+
return this.performNetworkCall(url, "DELETE", undefined, headers);
2632
}
2733

2834
private getJsonBody(body: {}|FormData) {
@@ -56,7 +62,8 @@ class HttpClient implements IHttpClient {
5662
return httpResponse;
5763
});
5864
});
59-
return Observable.fromPromise(promise);
65+
66+
return Observable.fromPromise(promise);
6067
}
6168
}
6269

samples/client/petstore/typescript-inversify/IHttpClient.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface IHttpClient {
66
get(url:string, headers?: Headers):Observable<HttpResponse>
77
post(url:string, body:{}|FormData, headers?: Headers):Observable<HttpResponse>
88
put(url:string, body:{}, headers?: Headers):Observable<HttpResponse>
9+
patch(url:string, body:{}, headers?: Headers):Observable<HttpResponse>
910
delete(url:string, headers?: Headers):Observable<HttpResponse>
1011
}
1112

0 commit comments

Comments
 (0)