This is the source code for the Rubics workshop on OpenAPI with React and Typescript by Johannes Brodwall.
The project contains two modules:
- The Java code for the backend. You will not need to examine on this for the workshop, as there will be a server running on https://openapi-workshop.azurewebsites.net
- The React code for the client. This is where the work will be happening
First, make sure that you can access the backend.
- Go to https://openapi-workshop.azurewebsites.net
- Click Authorize. This will open a new tab to log you on with Active Directory. Use your normal Active Directory account
- When you return to the OpenAPI spec page, click on the "GET /user/current" operation and Try it out. Verify that you get back your own identity
In the react-frontend directory:
- Run
npm installto download dependencies - Run
npm startto start the development server with Parcel - Go to
http://localhost:1234(NB: it's important to run on this exact port as the backend requires it) - Click "Login" to log in
- Verify that you are logged in with the correct name
In the workshop, we will be adding a field to the Pet-data structure
- As a warm-up: Update the pet listing
react-client/src/pets/ListPets.tsxto display more of the Pet properties - The workshop facilitator will demonstrate adding the new field to the API and backend and deploy a new backend version
- The workshop facilitator will publish a new API version
- Update the API by running
npm install -P @jhannes/openapi-workshop. This requires you to restart Parcel - The NewPet view will now fail to compile as there is a missing field
- Update
react-client/src/pets/NewPet.tsxandreact-client/src/pets/ListPets.tsxto submit and retrieve the new field
The frontend app is created with React 17.0.3 and built with Parcel. It uses Typescript 4.2.4. The front-end routing is done with React Router 5.2.0
The API to the Petstore app is build using openapi-generator-typescript-fetch-api and is distributed as the NPM package @jhannes/openapi-workshop
It has a few helper functions:
useLoader- handles the state of an asynchronous loading operation, such as an API calluseSubmit- handles the state of an asynchronous submit operationuseLocalStorage- stores a normal React state hook tolocalStorage
The Java backend is created as a self-contained Jetty-server which implements the Petstore sample app. The controllers are implemented with action-controller and the database interaction with fluent-jdbc. The Java DTOs are generated using openapi-generator-java-annotationfree. The serialization is done with jsonbuddy
Requires Azure Account and Azure CLI tools
- Login to Azure CLI:
az login - Deploy to your personal AppService:
mvn azure-webapp:deploy3Deploy production:mvn azure-webapp:deploy -DopenapiWorkshop.azure.appName=openapi-workshop