We will create a basic API using the Koa framework. This API will then communicate with an external Pets API using an auto-generated TypeScript Client. The Client is based on a external Swagger API definition
This project is composed of:
- src/app: the NodeJS API using Koa. It exposes only one method (find-pets)
- src/services: interface and implementation of the auto-generated TypeScript Client.
The Swagger API for this example can be found here.
Let's obtain the available pets!
For that, we can test in POSTMAN and send a call to the REST endpoint:
http://petstore.swagger.io/v2/pet/findByStatus?status=available
Above call returns an array with details about available pets.
The good news is that we can have type-safety and intellisense with the help of TypeScript. With an autogenerated client library (discussed in the next section) we ended up having this code:
const pets = await petStoreApi.findPetsByStatus([Status.Available]);
This process is pretty straight forward, you can find how this is achieved in next links:
- https://github.com/RSuter/NSwag
- https://github.com/RSuter/NSwag/wiki/SwaggerToTypeScriptClientGenerator
- https://github.com/RSuter/NSwag/wiki/NSwagStudio
After we generated the code for the Pets API, some results were: