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

sails-hook-swagger-generator-e

v0.0.0-development

Published

This is just a sails hook for generating swagger documentation json

Downloads

2

Readme

Swagger Generator Sails Hook

Travis npm npm semantic-release

This helps to create swagger documentation json which is based entirely on swagger specification Specifications.

Installation

$ npm install sails-hook-swagger-generator --save

Usage

Just install the library and sails lift, the moment you do, that's all check your ./swagger folder, it should be there and make sure this folder is already existing.

Example

check the ./swagger/swagger.json for sample generated swagger documentation json, then head to Swagger Editor

Configurations

It comes with some default settings which can be override by creating config/swaggergenerator.js

module.exports["swagger-generator"] = {
    disabled: false,
    swaggerJsonPath: "./swagger/swagger.json",
    parameters: { //we can add up custom parameters here
        PerPageQueryParam: {
            in: 'query',
            name: 'perPage',
            required: false,
            type: 'integer',
            description: 'This helps with pagination and when the limit is known for pagify'
        }
    },
    blueprint_parameters: {list: [{$ref: '#/parameters/PerPageQueryParam'}]},//we can add custom blueprint action to parameters binding here, any specified overrides default created
    swagger: {
        swagger: '2.0',
        info: {
            title: 'Swagger Json',
            description: 'This is a generated swagger json for your sails project',
            termsOfService: 'http://example.com/terms',
            contact: {name: 'Theophilus Omoregbee', url: 'http://github.com/theo4u', email: '[email protected]'},
            license: {name: 'Apache 2.0', url: 'http://www.apache.org/licenses/LICENSE-2.0.html'},
            version: '1.0.0'
        },
        host: 'localhost:1337',
        basePath: '/',
        externalDocs: {url: 'http://theophilus.ziippii.com'}
    }
};
  • disabled attribute is used to disable the module. (e.g you may want to disable it on production)
  • swaggerJsonPath where to generate the swagger.json file to, default to sails.config.appPath + "/swagger/swagger.json"
  • parameters we can create your own custom parameter to be referenced by api service methods and it's totally based on swagger specification for parameter object. Any one added here is added to the default parameters which are
 //default parameters-> where, limit, skip, sort, populate, select, page
    params.WhereQueryParam = {
        in: 'query',
        name: 'where',
        required: false,
        type: 'string',
        description: 'This follows the standard from http://sailsjs.com/documentation/reference/blueprint-api/find-where'
    };
    params.LimitQueryParam = {
        in: 'query',
        name: 'limit',
        required: false,
        type: 'integer',
        description: 'The maximum number of records to send back (useful for pagination). Defaults to ' + config.blueprints.defaultLimit
    };
    params.SkipQueryParam = {
        in: 'query',
        name: 'skip',
        required: false,
        type: 'integer',
        description: 'The number of records to skip (useful for pagination).'
    };
    params.SortQueryParam = {
        in: 'query',
        name: 'sort',
        required: false,
        type: 'string',
        description: 'The sort order. By default, returned records are sorted by primary key value in ascending order. e.g. ?sort=lastName%20ASC'
    };

    params.PopulateQueryParam = {
        in: 'query',
        name: 'populate',
        required: false,
        type: 'string',
        description: 'check for better understanding -> http://sailsjs.com/documentation/reference/blueprint-api/find-where'
    };

    params.PageQueryParam = {
        in: 'query',
        name: 'page',
        required: false,
        type: 'integer',
        description: 'This helps with pagination and when the limit is known'
    };
    params.SelectQueryParam = {
        in: 'query',
        name: 'select',
        required: false,
        type: 'string',
        description: 'This helps with what to return for the user and its "," delimited'
    };

    params.TokenHeaderParam = {
        in: 'header',
        name: 'token',
        required: false,
        type: 'string',
        description: 'Incase we want to send header inforamtion along our request'
    };

    params.IDPathParam = {
        in: 'path',
        name: 'id',
        required: true,
        type: 'string',
        description: 'This is to identify a particular object out'
    };
  • blueprint_parameters is a map between blueprint action and list of parameters and it's based on referencing of a particular parameter which can be any of this WhereQueryParam, LimitQueryParam, SkipQueryParam, SortQueryParam, PopulateQueryParam, PageQueryParam, SelectQueryParam, TokenHeaderParam, IDPathParam and the ones created under the parameters above. Any array of parameter mapped to a blueprint action overrides the default mapped parameters.
  • Swagger object is used to hold necessary information needed by swagger to run our mock test of api services. Check swagger specification for more, incase you want to extend it.

Custom Route Configuration

If you want to add extra configuration to a route, it can be done via the config/routes.js, since sails uses any of this two route pattern

  • 'http_method route':'controller.action' or
  • 'http_method route':{controller:'controller', action:'action'}

We can extend and add extra information on the second above for sails hook swagger generator to override default properties(which is created from the first). Adding an object with a key swagger will do the trick.

{
  'post /user/login': {
    controller: 'UserController',
    action: 'login',
    swagger: {
      summary: 'Authentication',
      description: 'This is for authentication of any user',
      body: {
        username: { type: 'string', required: true },
        password: { type: 'password', required: true }
      },
      tag: { name: 'User' },
      query: [{
        $ref: '#/parameters/IDPathParam'
      }],
      parameters: [{
        in: 'query',
        name: 'firstName',
        required: true,
        type: 'string',
        description: 'This is a custom required parameter'
      }],
      excludeParameters: ['#/parameters/TokenHeaderParam']
    }
  }
}

Properties of swagger

  • summary: for adding your own custom summary, e.g Authentication
  • description: for giving detailed description about the api service method, e.g This is for authentication of any type of user
  • body: if the route is taking in form post, and it follows our normal attribute from sails model
  • tag: allows for overriding the autogenerated tags from the controller identity. Useful if you are using standalone actions.
  • query: for any query you expecting from the user consuming the service method, you can also reference the default created above (WhereQueryParam, LimitQueryParam, SkipQueryParam, SortQueryParam, PopulateQueryParam, PageQueryParam, SelectQueryParam, TokenHeaderParam, IDPathParam) and add your own following the swagger specification.
  • We can also add custom personalized parameters if required.
  • excludeParameters: if you want to exclude some of the defaults paramters from the generated swagger json.

NB it overrides the default route params attached from the sails generator

Testing

  • Clone this repository

  • Install all development dependencies

$ npm install
  • Then run test
$ npm test

Road Map

Since the release of v1.0.0 of sailsjs

  • [x] test compatibility with v1.0.0
  • [ ] read info about a controller using machine-as-action #4

Contribute

Fork this repo and push in your ideas. Do not forget to add a bit of test(s) of what value you adding.

Changelog

See the different releases here

License

MIT License (MIT)