This repo demonstrates some simple hand-rolled tooling to bind an interface to a backend implementation, and generate a Typescript client for the API.
The demonstration is left as limited as possible - e.g. no build step beyond tsc
- Java 17+
- Node 16+ (probably earlier will work too)
- Maven 3.8+
cd frontend && npm install
to install Typescript
- Generate the API with
./build_api.sh
(generatestarget/ts/api.ts
andtarget/ts/types.ts
) - Build the frontend with
./build_frontend.sh
(compiles TypeScript infrontend/src
) - Run the server with
./run_server.sh
(runs Javalin server on localhost:8080)
- The API is defined in
com.malcolmcrum.typescriptapigenerator.demo.api
, e.g. UsersApi - Implementation is in
com.malcolmcrum.typescriptapigenerator.demo.services
, e.g. UsersService - API endpoints are exposed for each service in com.malcolmcrum.typescriptapigenerator.demo.App.exposeService
- Classes shared between the frontend and backend are in
com.malcolmcrum.typescriptapigenerator.demo.dtos
, e.g. UserDto - These shared classes are translated into TypeScript with typescript-generator
- The API client is generated in TypeScriptApiGenerator.java
- An extremely simple user of the client is in
frontend/src
. If you build and run using the scripts mentioned above, Javalin will serve files from this folder which you can view with your browser to try out the API.
If you'd like to read more on this topic, please read the blog post.