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

swaggerize-docs

v1.0.0

Published

A node module to help build more dynamic swaggerize doc files. Compliments swaggerize-express very well

Downloads

5

Readme

swaggerize-docs

NPM version Dependency Status Dev Dependency Status Code Climate Build Status Coverage Status

A complimentary swagger api docs that works especially well in conjunction with swaggerize-express.

NOTE: pre 1.0 did it a completely different way. This now is just a wrapper around swagger-parser with some default options in place.

Installation

npm install swaggerize-docs --save

You should also read up on the Swagger spec.

Usage

In pre 1.0 versions, the directory structure was automatically loaded into your paths property for your api docs. In this version, it leverages the path dereferencing used by swagger-parser.

The following code snippets layout an app that looks like this:

project/
├─ app.js
├─ docs/
│  ├─ main.yaml
│  ├─ paths/
│  │  ├─ users.yaml
│  │  └─ users/
│  │     └─ {id}.yaml
│  └─ definitions/
│     └─ User.yaml
└─ routes/
   ├─ users.js
   └─ users/
      └─ {id}.js
// app.js
'use strict';

var path = require('path');
var express = require('express');
var swagger = require('swaggerize-express');
var swaggerDocs = require('swaggerize-docs');
var app = express();

var DOCS_PATH = path.join(__dirname, 'docs', 'main.yaml');

swaggerDocs(DOCS_PATH).then(function(api) {
  app.use(swagger({
    api: api,
    docspath: '/api-docs'
    handlers: './routes'
  }));
  app.listen(8000, function() {
    console.log('server started');
  });
});

Documentation:

# docs/main.yaml
swagger: '2.0'
info:
  title: My API
  description: 'My API description'
  version: 1.0.0
paths:
  /users:
    $ref: './paths/users.yaml'
  /users/{id}:
    $ref: './paths/users/{id}.yaml'
definitions:
  User:
    $ref: './definitions/User.yaml'
# docs/paths/users.yaml
get:
  summary: List Users
  description: Gets a list of users
  responses:
    200:
      description: Success
      schema:
        type: array
        items:
          $ref: '#/definitions/User'
# docs/paths/users/{id}.yaml
get:
  summary: Get a User
  description: Gets a single user by id
  parameters:
    - name: id
      in: path
      type: string
      required: true
      description: The Id of the user to get
  responses:
    200:
      description: Success
      schema:
        $ref: '#/definitions/User'
    404:
      description: Not Found
# docs/definitions/User.yaml
properties:
  email:
    type: string
  createdAt:
    type: date
  updatedAt:
    type: date

Actual Routes:

// routes/users.js
'use strict';

var User = require('../models/user');

module.exports = {
  get: function(req, res, next) {
    User.find()
      .then(function(users) {
        res.json(users);
      })
      .catch(next);
  }
};
// routes/users/{id}.js
'use strict';

var User = require('../../models/user');

module.exports = {
  get: function(req, res, next) {
    User.findById(req.params.id)
      .then(function(user) {
        if (!user) { return res.sendStatus(404); }
        res.json(user);
      })
      .catch(next);
  }
};

API Configuration

swaggerDocs(docs, options)

  • docs (String|Object) If a string is passed, it will read that as a filepath. If you pass on object, it will read that as your swagger docs object.

  • options (Object) This is just the options object as documented here