Skip to content

Latest commit

 

History

History
40 lines (31 loc) · 2.07 KB

README.md

File metadata and controls

40 lines (31 loc) · 2.07 KB

Java/Typescript Client Generation

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

Requirements

  • Java 17+
  • Node 16+ (probably earlier will work too)
  • Maven 3.8+
  • cd frontend && npm install to install Typescript

Usage

  1. Generate the API with ./build_api.sh (generates target/ts/api.ts and target/ts/types.ts)
  2. Build the frontend with ./build_frontend.sh (compiles TypeScript in frontend/src)
  3. Run the server with ./run_server.sh (runs Javalin server on localhost:8080)

Details

  • 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.