npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@winkgroup/ts-swagger

v1.0.2

Published

library to transform typescript declarations into OpenAPI document

Downloads

5

Readme

Ts-Swagger

Ts-Swagger is a Javascript library that converts Typescript interfaces and Express APIs into OpenAPI/Swagger JSON.

Ts-Swagger library scans the Typescript files of interest and converts interfaces and APIs marked with specific comments. As a result it provides a JSON composed according to the OpenAPI 3 specification. Optionally generate a file with a .json extension in the project root.

Installation

npm i @winkgroup/ts-swagger

Usage

In order to be properly used, Ts-Swagger requires a configuration file with the paths containing all of your interfaces/API, along with some other info such as the documentation title (apiName) and version. Other optional info is the description and list of servers.

This file must be placed at the root of your project folder.

{
    "pathList": [
        "./model/User.ts",
        "./model/Cart.ts",
        "./routes/api/user.js"
    ],
    "apiName": "",
    "version": "",
    "description": "",
    "servers": [
        {
            "url": "http://localhost:3000/",
            "description": "Staging"
        },
    ]
}

In order for the library to work, you need to add a comment inside every interface that you want to generate Swagger for.

// swagger

// This interface will be converted 
export interface User {
    // swagger
    name: string,
    id: number
}

// This interface won't be converted
export interface Cart {
    productName: string,
    price: number,
    quantity: number,
    productId: number
}

Your APIs will also need a comment that specifies which interface they're using, otherwise the swagger for them won't be generated.

app.get('/users', function(req, res) {
    // schema: User
    res.send(Users);
});

Other optional comments are the description of the API and its response.

app.get('/users', function(req, res) {
    // schema: User
    // description: Get all users
    // response_description: Array of users
    res.send(Users);
});

With the following syntax you can also add the description of status codes other than 200 and if you want with an optional comment, you can provide a reference to the Typescript interface that describes the related response.

app.get('/users/:userId', function(req, res) {
    // schema: User
    // {404}: Not found
    // {500}: Some server error
    // error_schema: Error
    res.send(User);
});

The library exposes a method called getSwagger() that returns the Swagger JSON. If a filename is provided as an argument, a new file containing the JSON will be created in the root of your project.

import { TsSwagger } from "@winkgroup/ts-swagger";

// Configuration file path must be passed inside class constructor
const tsswg = new TsSwagger("../tsswagger.config.json");

// Returns the JSON Swagger
const swaggerObj = tsswg.getSwagger();

// Creates the JSON Swagger file
tsswg.getSwagger('swagger.json');

Maintainers

License

MIT