-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ryan10132
committed
Jan 7, 2016
1 parent
c8047f9
commit 26976da
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
typescript-service-generator | ||
==================== | ||
typescript-service-generator is a tool for creating strongly typed typescript http interfaces from jxrs annotated java interfaces. | ||
|
||
For each such java interface, a corresponding .ts class is generated that contains | ||
- Interfaces for all DTO objects used by the java interface, created using the typescript-generator project (https://github.com/vojtechhabarta/typescript-generator/) | ||
- A class, ready to be instantiated with a simple "bridge" object (defined later) that has a callable method for each endpoint in the java class | ||
|
||
For example for these Java files: | ||
|
||
``` java | ||
package com.palantir.code.ts.generator.examples; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
@Path("/myservice") | ||
public interface MyService { | ||
@GET | ||
@Path("/foo_get") | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
MyObject helloWord(); | ||
} | ||
``` | ||
``` java | ||
package com.palantir.code.ts.generator.examples; | ||
|
||
/** | ||
* Note that MyObject is jackson serializable | ||
*/ | ||
public class MyObject { | ||
private String payload = "hello world"; | ||
|
||
public String getPayload() { | ||
return payload; | ||
} | ||
} | ||
``` | ||
|
||
typescript-service-generator outputs this TypeScript file: | ||
``` typescript | ||
// A potential copyright header | ||
// A desired generated message | ||
module Foundry.Http.MyService { | ||
|
||
export interface IMyObject { | ||
payload: string; | ||
} | ||
|
||
export interface IMyService { | ||
helloWord(): HttpTypeWrapper<IMyObject>; | ||
} | ||
|
||
export class MyService implements IMyService { | ||
|
||
private httpApiBridge: IHttpApiBridge; | ||
constructor(restApiBridge: IHttpApiBridge) { | ||
this.httpApiBridge = restApiBridge; | ||
} | ||
|
||
public helloWord() { | ||
var httpCallData = <IHttpEndpointOptions> { | ||
serviceIdentifier: "myService", | ||
endpointPath: "myservice/foo_get", | ||
method: "GET", | ||
mediaType: "application/json", | ||
requiredHeaders: [], | ||
pathArguments: [], | ||
queryArguments: { | ||
}, | ||
data: null | ||
}; | ||
return this.httpApiBridge.callEndpoint<IMyObject>(httpCallData); | ||
} | ||
} | ||
} | ||
``` | ||
See MyServiceGenerator.java for all details on this example | ||
|
||
Instantiating the generated class requires an implementation of IHttpApiBridge | ||
|
||
IHttpApiBridge | ||
----- | ||
This is an interface that serves as a "bridge" between the generated typescript service classes. The contract of this interface is that it should know how to issue http calls given the inputs, and returns an object of a configurable type. | ||
|
||
TODO: hint at creating a bridge for angular |