Skip to content

Commit 075812f

Browse files
Mathias KowalzikMathias Kowalzik
authored andcommitted
Release 0.2.0
1 parent cd803e4 commit 075812f

File tree

4 files changed

+69
-39
lines changed

4 files changed

+69
-39
lines changed

README.md

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ Generate typescript services and type interfaces from spring annotated @RestCont
1515

1616
Get strongly typed interfaces for your spring-boot microservices in no time.
1717

18+
# Release 0.2.0 is here, What's new?
19+
* The default templates now generate TypeScript-Services using HttpClient from Angular 4.3
20+
* We added support for @RequestParam
21+
* All templates can now be configured in a single place through the use of a single Annotation [@TypeScriptTemplatesConfiguration](https://github.com/leandreck/spring-typescript-services/blob/development/annotations/src/main/java/org/leandreck/endpoints/annotations/TypeScriptTemplatesConfiguration.java)<br>
22+
Now you can even provide your own templates for index.ts and api.module.ts
23+
24+
1825
# What is it?
1926
A Java annotation processor to generate a service and TypeScript types to access your spring @RestControllers.
2027

@@ -38,64 +45,87 @@ Just specify the dependency in your maven based build.
3845
```
3946

4047
# Example
41-
The following snippet will produce a TestTypeScriptEndpoint.ts and a ISubType.model.ts file.
48+
The following snippet will produce an Angular Module.
4249
```java
4350
import org.leandreck.endpoints.annotations.TypeScriptEndpoint;
44-
import org.springframework.web.bind.annotation.PathVariable;
45-
import org.springframework.web.bind.annotation.RequestBody;
46-
import org.springframework.web.bind.annotation.RequestMapping;
47-
import org.springframework.web.bind.annotation.RestController;
48-
49-
import java.util.Collections;
50-
import java.util.List;
51+
import org.springframework.web.bind.annotation.*;
5152

5253
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
53-
import static org.springframework.web.bind.annotation.RequestMethod.POST;
5454

55-
@TypeScriptEndpoint
5655
@RestController
57-
@RequestMapping("/api")
58-
public class TestTypeScriptEndpoint {
59-
60-
@RequestMapping(value = "/type/{id}/{typeRef}", method = POST,
61-
consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
62-
public List<SubType> setId(@PathVariable Long id, @RequestBody SubType body) {
63-
// do something
64-
return Collections.singletonList(body);
56+
@TypeScriptEndpoint
57+
public class Controller {
58+
59+
@RequestMapping(value = "/api/get", method = RequestMethod.GET, produces = APPLICATION_JSON_VALUE)
60+
public ReturnType get(@RequestParam String someValue) {
61+
return new ReturnType("method: get");
6562
}
63+
// ...
6664
}
6765
```
6866
and the produced TypeScript files from the default templates look like:
6967

68+
**controller.generated.ts:**
7069
```typescript
71-
import { ISubType } from './ISubType.model';
70+
import { ReturnType } from './returntype.model.generated';
7271

73-
import { Http, Response, RequestOptions, Headers, RequestOptionsArgs } from "@angular/http";
72+
import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http';
7473
import { Injectable } from '@angular/core';
7574

76-
import { Observable } from "rxjs/Observable";
77-
import { ErrorObservable } from "rxjs/observable/ErrorObservable";
78-
import "rxjs/add/operator/do";
79-
import "rxjs/add/operator/catch";
80-
import "rxjs/add/observable/throw";
75+
import { Observable } from 'rxjs/Observable';
76+
import 'rxjs/add/operator/catch';
77+
import 'rxjs/add/observable/throw';
78+
import 'rxjs/add/operator/map';
8179

8280
@Injectable()
83-
export class TestTypeScriptEndpoint {
84-
private serviceBaseURL = '/api'
85-
constructor(private http: Http) { }
86-
/* POST */
87-
public setIdPost(id: number, body: ISubType): Observable<ISubType[]> {
88-
let url = this.serviceBaseURL + '/type/' + id + '/' + typeRef + '';
89-
return this.httpPost(url, body)
90-
.map((response: Response) => <ISubType[]>response.json())
81+
export class Controller {
82+
private serviceBaseURL = '';
83+
constructor(private httpClient: HttpClient) { }
84+
/* GET */
85+
public getGet(someValue: string): Observable<ReturnType> {
86+
const url = this.serviceBaseURL + '/api/get';
87+
const params = new HttpParams().set('someValue', someValue);
88+
return this.httpClient.get<ReturnType>(url, {params: params})
9189
.catch((error: Response) => this.handleError(error));
9290
}
93-
private httpPost(url: string, body: any): Observable<Response> {
94-
console.info('httpPost: ' + url);
95-
return this.http.post(url, body);
91+
92+
/* .. */
93+
94+
private handleError(error: Response) {
95+
// in a real world app, we may send the error to some remote logging infrastructure
96+
// instead of just logging it to the console
97+
console.error(error);
98+
return Observable.throw(error);
9699
}
100+
97101
}
98102
```
103+
**returntype.model.generated.ts:**
104+
```typescript
105+
export interface ReturnType {
106+
method: string;
107+
}
108+
```
109+
110+
**api.module.ts:**
111+
```typescript
112+
import { NgModule } from '@angular/core';
113+
import { Controller } from './controller.generated';
114+
115+
@NgModule({
116+
providers: [
117+
Controller
118+
],
119+
})
120+
export class APIModule { }
121+
```
122+
**index.ts:**
123+
```typescript
124+
export { BodyType } from './bodytype.model.generated';
125+
export { ReturnType } from './returntype.model.generated';
126+
export { Controller } from './controller.generated';
127+
export { APIModule } from './api.module';
128+
```
99129

100130
[freemarker]: http://freemarker.org/
101131

annotations/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<parent>
2525
<groupId>org.leandreck.endpoints</groupId>
2626
<artifactId>parent</artifactId>
27-
<version>0.1.1-SNAPSHOT</version>
27+
<version>0.2.0</version>
2828
<relativePath>../</relativePath>
2929
</parent>
3030
<artifactId>annotations</artifactId>

examples/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<parent>
2525
<groupId>org.leandreck.endpoints</groupId>
2626
<artifactId>parent</artifactId>
27-
<version>0.1.1-SNAPSHOT</version>
27+
<version>0.2.0</version>
2828
<relativePath>../</relativePath>
2929
</parent>
3030

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
<groupId>org.leandreck.endpoints</groupId>
2525
<artifactId>parent</artifactId>
26-
<version>0.1.1-SNAPSHOT</version>
26+
<version>0.2.0</version>
2727
<packaging>pom</packaging>
2828

2929
<!-- More Project Information -->

0 commit comments

Comments
 (0)