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

@tsed/swagger

v8.0.1

Published

Swagger package for Ts.ED framework

Downloads

49,807

Readme

Build & Release PR Welcome npm version semantic-release code style: prettier github opencollective

A package of Ts.ED framework. See website: https://tsed.io/#/tutorials/swagger

Installation

Before using the Swagger, we have to install the swagger-ui-express module.

npm install --save @tsed/swagger

Then add the following configuration in your Server:

import {Configuration} from "@tsed/di";
import "@tsed/swagger"; // import swagger Ts.ED module
import {resolve} from "path";

@Configuration({
  swagger: [
    {
      path: "/v2/docs",
      specVersion: "2.0"
    },
    {
      path: "/v3/docs",
      specVersion: "3.0.1"
    }
  ]
})
export class Server {}

The path option for swagger will be used to expose the documentation (ex: http://localhost:8000/api-docs).

Normally, Swagger-ui is ready. You can start your server and check if it work fine.

Note: Ts.ED will print the swagger url in the console.

Swagger options

Some options is available to configure Swagger-ui, Ts.ED and the default spec information.

| Key | Example | Description | | -------------------- | ------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- | | path | /api-doc | The url subpath to access to the documentation. | | specVersion | 2.0, 3.0.1 | The spec version. | | doc | hidden-doc | The documentation key used by @Docs decorator to create several swagger documentations. | | cssPath | ${rootDir}/spec/style.css | The path to the CSS file. | | jsPath | ${rootDir}/spec/main.js | The path to the JS file. | | showExplorer | true | Display the search field in the navbar. | | spec | {swagger: "2.0"} | The default information spec. | | specPath | ${rootDir}/spec/swagger.base.json | Load the base spec documentation from the specified path. | | outFile | ${rootDir}/spec/swagger.json | Write the swagger.json spec documentation on the specified path. | | hidden | true | Hide the documentation in the dropdown explorer list. | | options | Swagger-UI options | SwaggerUI options. See (https://github.com/swagger-api/swagger-ui/blob/HEAD/docs/usage/configuration.md) | | operationIdFormatter | (name: string, propertyKey: string, path: string) => string | A function to generate the operationId. | | operationIdPattern | %c_%m | A pattern to generate the operationId. Format of operationId field (%c: class name, %m: method name). |

Multi documentations

It also possible to create several swagger documentations with doc option:

import {Configuration} from "@tsed/di";
import "@tsed/swagger"; // import swagger Ts.ED module

@Configuration({
  swagger: [
    {
      path: "/api-docs-v1",
      doc: "api-v1"
    },
    {
      path: "/api-docs-v2",
      doc: "api-v2"
    }
  ]
})
export class Server {}

Then use @Docs decorators on your controllers to specify where the controllers should be displayed.

import {Controller} from "@tsed/di";
import {Docs} from "@tsed/swagger";

@Controller("/calendars")
@Docs("api-v2") // display this controllers only for api-docs-v2
export class CalendarCtrlV2 {}
// OR
@Controller("/calendars")
@Docs("api-v2", "api-v1") // display this controllers for api-docs-v2 and api-docs-v1
export class CalendarCtrl {}

Examples

Model documentation

One of the feature of Ts.ED is the model definition to serialize or deserialize a JSON Object (see converters section).

This model can used on a method controller along with @BodyParams or other decorators.

import {JsonProperty, Title, Description, Example} from "@tsed/schema";

export class CalendarModel {
  @Title("iD")
  @Description("Description of calendar model id")
  @Example("Example value")
  @JsonProperty()
  public id: string;

  @JsonProperty()
  public name: string;
}

Endpoint documentation

import {Controller} from "@tsed/di";
import {BodyParams, QueryParams} from "@tsed/platform-params";
import {Get, Post, Returns, ReturnsArray, Description, Summary, Deprecated, Security} from "@tsed/schema";
import {CalendarModel} from "../models/CalendarModel.js";

@Controller("/calendars")
export class Calendar {
  @Get("/:id")
  @Summary("Summary of this route")
  @Description("Description of this route")
  @Returns(CalendarModel)
  @Returns(404, {description: "Not found"})
  async getCalendar(@QueryParams("id") id: string): Promise<CalendarModel> {
    //...
  }

  @Get("/v0/:id")
  @Deprecated()
  @Description("Deprecated route, use /rest/calendars/:id instead of.")
  @Returns(CalendarModel)
  @Returns(404, {description: "Not found"})
  getCalendarDeprecated(@QueryParams("id") id: string): Promise<CalendarModel> {
    //...
  }

  @Get("/")
  @Description("Description of this route")
  @ReturnsArray(CalendarModel)
  getCalendars(): Promise<CalendarModel[]> {
    // ...
  }

  @Post("/")
  @Security("calendar_auth", "write:calendar", "read:calendar")
  @Returns(CalendarModel)
  async createCalendar(@BodyParams() body: any): Promise<CalendarModel> {
    //...
  }
}

::: warninig To update the swagger.json you need to reload the server before. :::

Import Javascript

It possible to import a Javascript in the Swagger-ui documentation. This script let you customize the swagger-ui instance.

import {Configuration} from "@tsed/di";
import "@tsed/swagger"; // import swagger Ts.ED module

@Configuration({
  swagger: [
    {
      path: "/api-docs",
      jsPath: "/spec/main.js"
    }
  ]
})
export class Server {}

In your JavaScript file, you can handle Swagger-ui configuration and the instance:

console.log(SwaggerUIBuilder.config); //Swagger-ui config

document.addEventListener("swagger.init", (evt) => {
  console.log(SwaggerUIBuilder.ui); //Swagger-ui instance
});

Documentation

See our documentation https://tsed.io/#/api/index

Contributors

Please read contributing guidelines here.

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

License

The MIT License (MIT)

Copyright (c) 2016 - 2022 Romain Lenzotti

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.