Skip to content

51seven/swagspress

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

swagspress

Notice: This repo won't be maintained anymore. I'm not happy with splitting the doc and the controller into two parts.

swagspress is a middleware to implement an API with Swagger into an express.js server. This only supports Swagger Spec 2.0.

I made this Swagger Implementation for the sssnap Server API. At the moment this won't work for every usecase.

Features

  • Validation of the swagger file
  • Creating all routes defined in the swagger file
  • Mapping routes to the defined controller
  • Global Authentication and individual Authentication for each route (WIP)
  • Only allows requests with the defined mimetypes
  • Validate the request parameters (WIP)
  • Convert the request parameters into the defined types (WIP)
  • Support of req.files when using multer, connect-multiparty or other multipart/form-data parser

How to use

var express = require('express');
var path = require('path');
var swagger = require('swagspress');

var app = express();

app.use(swagger({
  controller: path.join(__dirname, 'controller'),
  doc: path.join(__dirname, 'swagger.json')
}));

Be sure to include swagspress after method-override, body-parser and other middleware parsing the incoming request.

Options

controller required
Path to the directory where your controllers are located. Be sure to use __dirname to build the path.

doc required
Path to your swagger documentation file. This don't has to be the whole JSON, you can also build it yourself out of different parts. Be sure to use __dirname to build the path.

In your swagger doc

Use x-controller to define which controller to use.

{
  "paths": {
    "/pets": {
      "get": {
        "description": "Returns all pets from the system that the user has access to",
        "x-controller": "pets",
        "operationId": "listAll",
        "produces": [
          "application/json",
        ]

This will call the listAll function of pets.js in your defined controller directory.

controller/pets.js

exports.listAll = function(req, res) {
  res.send('Hello world!');
}

TODO

Check the milestones to find out what we are working on. Feel free to report issues or request features.

License

MIT

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Node.js module for a Swagger API in express.js

Resources

License

Stars

Watchers

Forks

Packages

No packages published