Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate swagger API doc from TypeScript based Express app?

I am able to configure swagger url using the Express API with autogenerated OpenAPI doc through Swagger article.

I am using TypeScript and it generates .js files under dist which does not have any API doc comments added. Pointing apis: ['../dist/*.js'] nor to Route.ts generates the API details. I am not using any rest decorator.

/**
 * @swagger
 * /:
 *    get:
 *      description: This should return ok
 */
this.router.get("/", (req: Request, res: Response) => this.api.process(req, res));

The Routes.ts API doc looks like the above. How to generate swagger doc from this?

like image 612
johndoe Avatar asked Aug 13 '19 12:08

johndoe


People also ask

How do I create API document with Swagger?

How to generate OpenAPI from existing APIs. Head over to Swagger Inspector, and insert the end point of the resource you want to have documented. You can then navigate to the right panel from the History section of Swagger Inspector, and click "Create API definition" to create the OAS definition.

How do I Export API document from Swagger?

You can use the "Export" button in the top right corner of the Swagger UI. This will give you the option to download your API document as a JSON or YAML file. You can use the "Generate Server" or "Generate Client" buttons to generate a server or client stub in your desired language.


1 Answers

I would recommend that you use a library that handles everything for you such as tsoa which can easily generate Swagger/OpenAPI documents from your TypeScript types. It also does the runtime validation for you so that you know the request actually is the type that TypeScript says it should be. The readme contains all of the setup information that you would need to start using it. It's compatible with express, hapi, koa, and more:

https://github.com/lukeautry/tsoa


(Full Transparency: I am one of the maintainers of tsoa. But I was first a consumer of tsoa and I find it to be a great product... that's why I asked to help maintain it! :) )

like image 54
GreeneCreations Avatar answered Nov 15 '22 18:11

GreeneCreations