What is the correct way to create a swagger web service in TypeScript

I am part of a project written in TypeScipt, and I am trying to add a TypeScript Web server that will be compatible with Swagger.
What is the most basic strategy for its implementation, given the ease of maintenance.

  • For TypeScript, I noticed that there is a Typson library for creating a JSON model from the TypeScript interface.

  • For Swagger, I tried using the library 'swagger-node -restify', because it supports adding JSON models to swagger.

However, I am facing some problems:

  • Typson does not support TypeScript import syntax - ( import {Server} from "restify" )
  • I tried to implement the 'swagger-node -restify' example (Pet example), however RESPONSE localhost: 8080 / api-docs.json GET Request skips all SPEC API data. {"apiVersion":"0.1","swaggerVersion":"1.1","basePath":"http://localhost:8080","apis":[{"path":"/api-docs.{format}/pet","description":"none"}]}
+7
typescript swagger
source share
2 answers

I suggest describing a Swagger compatible API using yaml or json and generating a server .

swagger-server can create APIs at the top of express in real time (without generating source code).

There are JavaScript code generators:

  • Access the swagger-codegen client using -l nodejs-server

  • swagger-node is a great alternative, but hard to integrate with TypeScript

+3
source share

Yes, you can easily create Swagger and OpenAPI documents from TypeScript types using tsoa . The readme file contains all the configuration information that you will need to get started. It is compatible with express, hapi, koa, etc. (Due to the ability to add your own template for your preferred server type):

https://github.com/lukeautry/tsoa

Advantages tsoa has over other libraries:

  • it generates a swagger / OpenAPI document and also checks types at runtime

(Full transparency: I am one of the accompanying tsoa. ​​But I was the first consumer of tsoa, ​​and I think this is a great product ... that's why I asked for help to save it! :))

0
source share

All Articles