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

trailpack-swagger

v2.0.6

Published

Use Swagger UI in your Trails project

Downloads

20

Readme

trailpack-swagger

NPM version Build status Dependency Status Code Climate Donate

Use Swagger UI in your Trails project.

Dependencies and Compatibilities

  • TrailsJS
  • Waterline
  • Express
  • Footprint
  • Passport (optional)
  • Swagger UI (the /dist of the repository will do)
  • Swagger 2.0

Install

$ npm install --save trailpack-swagger

Configure

Main

// config/main.js
module.exports = {
  packs: [
    // ... other trailpacks
    require('trailpack-swagger')
  ]
}

Swagger Config

All these values are explained in the Swagger Specification.

// config/swagger.js
module.exports = {

  /**
   * The title of the application.
   * @type {String}
   */
  title: 'Project API',
  /**
   * A short description of the application. GFM syntax can be used for rich text representation.
   * @type {String}
   */
  description: 'My APIs',
  /**
   * The Terms of Service for the API.
   * @type {String}
   */
  termsOfService: 'My ToS',
  /**
   * The contact information for the exposed API.
   * @type {Object}
   */
  contact: {
    /**
     * The identifying name of the contact person/organization.
     * @type {String}
     */
    name: 'Matteo Zambon',
    /**
     * The URL pointing to the contact information. MUST be in the format of a URL.
     * @type {String}
     */
    url: 'https://github.com/matteozambon89',
    /**
     * The email address of the contact person/organization. MUST be in the format of an email address.
     * @type {String}
     */
    email: '[email protected]'
  },
  /**
   * The license information for the exposed API.
   * @type {Object}
   */
  license: {
    /**
     * The license name used for the API.
     * @type {String}
     */
    name: 'MIT',
    /**
     * A URL to the license used for the API. MUST be in the format of a URL.
     * @type {String}
     */
    url: 'https://opensource.org/licenses/MIT'
  },
  /**
   * Provides the version of the application API (not to be confused with the specification version).
   * @type {String}
   */
  version: '1.0.0',
  /**
   * The base path on which the API is served, which is relative to the host. If it is not included, the API is served directly under the host. The value MUST start with a leading slash (/). The basePath does not support path templating.
   * @type {String}
   */
  basePath: '/api/v1',
  /**
   * The transfer protocol of the API. Values MUST be from the list: "http", "https", "ws", "wss". If the schemes is not included, the default scheme to be used is the one used to access the Swagger definition itself.
   * @type {Array}
   */
  schemes: ['http'],
  /**
   * A list of MIME types the APIs can consume. This is global to all APIs but can be overridden on specific API calls. Value MUST be as described under Mime Types.
   * @type {Array}
   */
  consumes: ['application/json'],
  /**
   * A list of MIME types the APIs can produce. This is global to all APIs but can be overridden on specific API calls. Value MUST be as described under Mime Types.
   * @type {Array}
   */
  produces: ['application/json'],
  /**
   * The host (name or ip) serving the API. This MUST be the host only and does not include the scheme nor sub-paths. It MAY include a port. If the host is not included, the host serving the documentation is to be used (including the port). The host does not support path templating.
   * @type {String}
   */
  host: '0.0.0.0',
  /**
   * The host port serving the API. This MUST be the port only.
   * @type {Number}
   */
  port: 3000,
  /**
   * Security scheme definitions that can be used across the specification.
   * @type {Object}
   */
  securityDefinitions: {
    apiKey: {
      type: 'apiKey',
      name: 'api_key',
      in: 'header'
    },
    basic: {
      type: 'basic'
    },
    oauth: {
      type: 'oauth2',
      authorizationUrl: 'http://swagger.io/api/oauth/dialog',
      flow: 'implicit',
      scopes: {
        'write:pets': 'modify pets in your account',
        'read:pets': 'read your pets'
      }
    }
  },
  /**
   * A declaration of which security schemes are applied for the API as a whole. The list of values describes alternative security schemes that can be used (that is, there is a logical OR between the security requirements). Individual operations can override this definition.
   * @type {Object}
   */
  security: {
    api_key: [],
    basic: [],
    oauth: [
      'write:pets',
      'read:pets'
    ]
  }
}

All the config properties are optional so if you wish let trailpack-swagger parse these for you simply pass an empty config:

// config/swagger.js
module.exports = {
};

Swagger UI

  • Get the latest Swagger UI
  • Copy the content of swagger-ui/dist into my-project/views/docs
  • Inside my-project/views/docs you should see a file index.html
  • Open my-project/views/index.html
  • Change line :41 from url = "http://petstore.swagger.io/v2/swagger.json"; to url = "/api/docs";

Routes

  • /api/doc will return the computed Swagger object as JSON
  • /api/explorer will display the Swagger UI

Credits

Please Contribute!

I'm happy to receive contributions of any kind!

Contribute in what matters

  • [x] Compatibility with TrailsJS 2.x
  • [ ] Test the support of hapi.js
  • [ ] Test the support of Sequelize
  • [ ] Generate Swagger Endpoints from TrailsJS Routes
  • [ ] Swagger Editor to generate Models and Footprint APIs from a JSON

Did you like my work?

Help me out with a little donation, press on the button below. Donate