JordyBaylac / typescript-from-swagger

A Koa API using an auto-generated TypeScript client library based on an external Swagger API definition

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Auto-generated TypeScript Swagger client (TS)

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.

Swagger Pets API

The Swagger API for this example can be found here.

Example

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]);

Autogenerating TypeScript from Swagger API

This process is pretty straight forward, you can find how this is achieved in next links:

After we generated the code for the Pets API, some results were: IPetStoreApi

About

A Koa API using an auto-generated TypeScript client library based on an external Swagger API definition


Languages

Language:TypeScript 100.0%