Adding swagger to Ts.ED project

Ramachandran Mani
2 min readJun 16, 2021

Ts.ED is a framework for Node js and typescript. This allows you to create a server side API application. If you are trying to build one, the steps are easy that you just execute the below commands to setup the project.

The first step is to globally install the Ts.ED cli tool

npm install -g @tsed/cli

Once done, create a new folder for your project and initialise the project used init command

tsed init .

This will create all necessary files, installs node modules for you including a sample controller. Execute the below command to start the server

npm start

The logs will indicate the port in which the application is running. You can open them in your browser to verify. Look the sample controller and frame the end point. The default routing will be “http://localhost:8083/rest/”. The rest of the routing needs to be determined by looking at the controller. Below is an extract from my controller. So to access the GET endpoint the URL would “http://localhost:8083/rest/image/fetch”

@Controller("/image")export class ImageController {@Get("/fetch")get() {return "hello";}

Now, you will be able to access all the endpoints available in the controller through any API tools.

Let us see, how to add swagger to this project. One of the important file that contains the configurations is the server.ts file which was auto created by the tsed init command.

Open the server.ts file and look for Configuration. This contains lots of configs already such as httpPort, acceptMimes etc. What we need to do is, add swagger configuration to it.

Now, install swagger plugin provided by tsed

npm i @tsed/swagger

Import the same in the server.ts file

import "@tsed/swagger";

Now enable swagger option in configuration in the server.ts file

@Configuration({...config,acceptMimes: ["application/json", "multipart/form-data"],httpPort: process.env.PORT || 8083,swagger: [{path: "/api/swagger",}]})

Save the changes and restart the server. Once the services is up, access the swagger documentation through “http://localhost:8083/api/swagger”

--

--

Ramachandran Mani

Blogs about day to day issues faced as a Developer. Twitter @chandrumani