all files / swagger-node-runner/lib/ restify_middleware.js

100% Statements 22/22
100% Branches 4/4
100% Functions 8/8
100% Lines 22/22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45                      60× 28× 28×       36× 32× 32×     28×              
'use strict';
 
module.exports = init;
 
var debug = require('debug')('swagger:restify_middleware');
var ALL_METHODS = ['del', 'get', 'head', 'opts', 'post', 'put', 'patch'];
 
function init(runner) {
  return new Middleware(runner);
}
 
function Middleware(runner) {
 
  this.runner = runner;
 
  var connectMiddleware = runner.connectMiddleware();
  var chain = connectMiddleware.middleware();
 
  this.register = function register(app) {
 
    var api = connectMiddleware.runner.api;
 
    function registerHandler(path, method) {
      app[method](path, function(req, res, next) {
        req.query = undefined; // oddly, req.query is a function in Restify, kill it
        chain(req, res, next);
      });
    }
 
    api.pathObjects.forEach(function(path) {
      if (path.operationObjects && path.operationObjects.length > 0) {
        path.operationObjects.forEach(function(operation) {
          registerHandler(path.path, operation.method);
        });
      } else {
        ALL_METHODS.forEach(function(method) {
          registerHandler(path.path, method);
        });
      }
    });
 
    connectMiddleware.register(app);
  };
}