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

swagger-spec-middleware

v1.2.0

Published

swagger-spec-middleware ========

Downloads

1

Readme

swagger-spec-middleware

This is simple middleware for ExpressJS to handle resources given by the Swagger spec file.

##Features

  • Easy installation
  • Support default handlers comosed with method + path, for example: getPets will handle paths -> '/pets' -> get
  • Support named handlers customized by operationId in specification file
  • Support get, post, put and delete operation methods
  • Parameters are automatically passed to handlers as arguments
  • Support query, header, path, formData, body parameter sources
  • Parameters are automatically converted to the target type and format
  • Handling exceptions, generic, custom and global exceptions customization

##Planned features

  • Support parameters default value
  • Input parameters validation, support for:
    • required
    • maximum
    • exclusiveMaximum
    • minimum
    • exclusiveMinimum
    • maxLength
    • minLength
    • pattern
    • maxItems
    • minItems
    • uniqueItems
    • enum
    • multipleOf
  • Arrays type support
  • Collection format support
  • Schema validation
  • Meta information passing to handler
  • Support for consumes and produces; input and output format determined by Accept and Content-Type header fields
  • Support inheritance for specification configuration model
  • Definitions, Parameters, Responses and References support
  • Support other Swagger Spec versions than 2.0
  • Add support for lifecycle like turn off validation, preprocessors and afterprocessors for handlers etc.

##Not planned features

  • Support for options, head, patch operation methods
  • Support security layer
  • Support for XML Objects

Hello world

Install it:

npm install --save swagger-spec-middleware

use it next to ExpressJS app:

var express = require('express');
var app = express();
var swaggerSpecMiddleware = require('swagger-spec-middleware');
swaggerSpecMiddleware.host(app, {
    spec: 'spec.json',
    handlers: {
        'petsGet': function (req, resp) {
            resp.send(200);
        }
    }
});

where spec.json:

{
    "swagger": "2.0",
    "info": {
        "version": "1.0.0",
        "title": "Swagger ToyStore"
    },
    "host": "petstore.swagger.wordnik.com",
    "basePath": "/api",
    "schemes": [
        "http"
    ],
    "paths": {
        "/pets": {
            "get": {
                "operationId": "petsGet",
                "responses": {
                    "200": {
                    }
                }
            }
        }
    }
    
}

#Documentation ##Installation

Install dependency in the project by:

npm install --save swagger-spec-middleware

Add to your ExpressJS application and start using it:

var swaggerSpecMiddleware = require('swagger-spec-middleware');
swaggerSpecMiddleware.host(app, {
    spec: 'spec.json',
    handlers: {
        'petsGet': function (req, resp) {
            resp.send(200);
        }
    }
});

where spec.json is Swagger specification file with resources description that needs to be handled.

##Handlers ###Default handlers By default, default handlers are being looking first. When given operation is defined in spec file:

"paths": {
    "/pets": {
        "get": {
            "responses": {
                "200": {
                }
            }
        }
    }
}

then this resource can be handled by the default handler getPets:

'getCars': function (req, resp) {
        resp.send(200);
}

###Named handlers Resource can be explicitly named by operationId in spec operation:

"/pets": {
    "get": {
        "operationId": "petsGet",
        "responses": {
            "200": {
            }
        }
    }
}

then supported handler for this operation is petsGet.

'petsGet': function (req, resp) {
        resp.send(200);
}

OperationId handler overrides default handler. When also getPets handler specified, only petsGet will be handled.

##Operations Framework supports following operation:

  • get
  • post
  • put
  • delete

##Parameters If specification declares parameters for the operation, resolved values will be passed as handler arguments in the same order as in spec. For example if tags and limit parameter are specified as follow:

"/pets": {
      "get": {
        "description": "Returns all pets from the system that the user has access to",
        "operationId": "findPets",
        "produces": [
          "application/json",
          "application/xml",
          "text/xml",
          "text/html"
        ],
        "parameters": [
          {
            "name": "tag",
            "in": "query",
            "type": "string"
          },
          {
            "name": "limit",
            "in": "query",
            "type": "integer",
            "format": "int32"
          }
        ],
        "responses": {
          "200": {
            }
          },
          "default": {
            "description": "unexpected error",
            "schema": {
              "$ref": "#/definitions/errorModel"
            }
          }
        }
      }
      }

then in handler they will be passed after meta object:

swaggerSpecMiddleware.host(app, {
    spec: 'spec.json',
    handlers: {
        getPets: function(meta, tags, limit){
            return {
                tags: tags,
                limit: limit
            }
        }
    }

});

Parameter inputs

Framework supports following parameter input sources:

  • query
  • heade
  • path
  • formData
  • body

Parameter types and formats

Parameters are automatically converted to target types as follow:

|Common Name|type |format |Comments | |:---------:|:-------------:|:------------:|:-------------------------------:| |integer |integer |int32 |signed 32 bits | |long |integer |int64 |signed 64 bits | |float |number |float | | |double |number |double | | |boolean |boolean | | | |string |string | | | |byte |string |byte | | |date |string |date |As defined by full-date - RFC3339| |dateTime |string |date-time |As defined by date-time - RFC3339|

Errors and exceptions

Default behaviour

Following ruless obey regarding exceptions:

  • When the called resource is not described in the spec, then next router declared in ExpressJS configuration can catch it
  • When the called resource hits described in the spec operation which is unhandled, then it results in 404 page with message: "Unhandled operation"
  • When the called resource hits described in the spec operation which throws generic exception, then it results in 404 page with message: "Unknown exception occured"
  • When the called resource hits described in the spec operation which customized exception then it might results in error page with custom exception status or custom exception message

Customizing

Statuses and messages for above exceptions can be overridden with the configuration. Following attributes can be overridden:

  • defaultExceptionStatus (default: 404) - request status for handled exceptions
  • defaultExceptionMessage (default: "Unknown exception occured") - request message for handled exceptions
  • unhandledOperationHandler (default: function () { throw {status: 404, message: 'Unhandled operation'}; }) - handler for unhandled operations
swaggerSpecMiddleware.host(app, {
    spec: 'spec.json',
    handlers: {...},
    defaultExceptionStatus: 403,
    defaultExceptionMessage: 'Customized unknown exception occured',
    unhandledOperationHandler: function () {
        throw {status: 403, message: 'Customized unhandled operation'};
    }

});