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-builder

v2.0.5

Published

Swagger based route building.

Downloads

59

Readme

swaggerize-builder

Lead Maintainer: Trevor Livingston

Build Status
NPM version

swaggerize-builder is a component used by swaggerize-express and swaggerize-hapi for parsing and building route definitions based on a Swagger 2.0 document.

swaggerize-builder provides the following features:

  • Schema validation.
  • Building route definitions from a Swagger 2.0 document.
  • Validation helpers for input parameters.

Usage

var builder = require('swaggerize-builder');

var routes = builder({
    api: require('./api.json'),
    handlers: './handlers'
}));

Options:

  • api - a valid Swagger 2.0 object.
  • handlers - either a directory structure for route handlers or a premade object (see Handlers Object below).
  • basedir - base directory to search for handlers path (defaults to dirname of caller).
  • schemas - an array of {name: string, schema: string|object} representing additional schemas to add to validation.

Returns: An array of the processed routes.

Handlers Directory

The options.handlers option specifies a directory to scan for handlers. These handlers are bound to the api paths defined in the swagger document.

handlers
  |--foo
  |    |--bar.js
  |--foo.js
  |--baz.js

Will route as:

foo.js => /foo
foo/bar.js => /foo/bar
baz.js => /baz

Path Parameters

The file and directory names in the handlers directory can also represent path parameters.

For example, to represent the path /users/{id}:

handlers
  |--users
  |    |--{id}.js

This works with directory names as well:

handlers
  |--users
  |    |--{id}.js
  |    |--{id}
  |        |--foo.js

To represent /users/{id}/foo.

Handlers File

Each provided javascript file should export an object containing functions with HTTP verbs as keys.

Example:

module.exports = {
    get: function (...) { ... },
    put: function (...) { ... },
    ...
}

Where the function signature is a handler for the target framework (e.g. express or hapi).

Handlers Object

The directory generation will yield this object, but it can be provided directly as options.handlers.

Note that if you are programatically constructing a handlers obj this way, you must namespace HTTP verbs with $ to avoid conflicts with path names. These keys should also be lowercase.

Example:

{
    'foo': {
        '$get': function (...) { ... },
        'bar': {
            '$get': function (...) { ... },
            '$post': function (...) { ... }
        }
    }
    ...
}

Handler keys in files do not have to be namespaced in this way.

Route Object

The routes array returned from the call to the builder will contain route objects. Each route has the following properties:

  • path - same as path from api definition.
  • name - same as operationId in api definition.
  • description - same as description in path for api definition.
  • method - same as method from api operation definition.
  • validators - an array of validation objects created from each parameter on the operation.
  • handler - a handler function appropriate to the target framework (e.g express).
  • produces - same as produces in path for api definition.

Validator Object

The validator object in the validators array will have the following properties:

  • parameter - same as the parameter from the operation on path.
  • validate(value, callback) - a function for validating the input data against the parameter definition.
  • schema - the joi schema being validated against.