swagger-mock-api
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -21,5 +21,5 @@ 'use strict'; | ||
// | ||
'GET /pets/{id}', // ignore specific methods in a path | ||
// 'GET /pets/{id}', // ignore specific methods in a path | ||
// | ||
'/pets' // ignore the entire path | ||
// '/pets' // ignore the entire path | ||
// | ||
@@ -26,0 +26,0 @@ ] |
'use strict'; | ||
var _Promise = require('babel-runtime/core-js/promise')['default']; | ||
var _interopRequireDefault = require('babel-runtime/helpers/interop-require-default')['default']; | ||
@@ -22,4 +24,2 @@ | ||
module.exports = function (config) { | ||
var router = undefined; | ||
var basePath = undefined; | ||
if (!config.swaggerFile) { | ||
@@ -33,35 +33,42 @@ throw new Error('Config is missing `swaggerFile` parameter'); | ||
_swaggerParser2['default'].parse(config.swaggerFile, function (err, api) { | ||
if (err) throw err; | ||
var basePath = undefined; | ||
var router = undefined; | ||
var parserPromise = new _Promise(function (resolve) { | ||
_swaggerParser2['default'].parse(config.swaggerFile, function (err, api) { | ||
if (err) throw err; | ||
if (config.ignorePaths) { | ||
api.paths = _PrunePaths2['default'](api.paths, config.ignorePaths); | ||
} else if (config.mockPaths) { | ||
api.paths = _PrunePaths2['default'](api.paths, config.mockPaths, true); | ||
} | ||
if (config.ignorePaths) { | ||
api.paths = _PrunePaths2['default'](api.paths, config.ignorePaths); | ||
} else if (config.mockPaths) { | ||
api.paths = _PrunePaths2['default'](api.paths, config.mockPaths, true); | ||
} | ||
basePath = api.basePath || ''; | ||
router = _ConfigureRouter2['default'](api.paths); | ||
basePath = api.basePath || ''; | ||
router = _ConfigureRouter2['default'](api.paths); | ||
resolve(); | ||
}); | ||
}); | ||
return function (req, res, next) { | ||
var method = req.method.toLowerCase(); | ||
parserPromise.then(function () { | ||
var method = req.method.toLowerCase(); | ||
var path = _url2['default'].parse(req.url).pathname; | ||
path = path.replace(basePath + '/', ''); | ||
if (path.charAt(0) !== '/') { | ||
path = '/' + path; | ||
} | ||
var path = _url2['default'].parse(req.url).pathname; | ||
path = path.replace(basePath + '/', ''); | ||
if (path.charAt(0) !== '/') { | ||
path = '/' + path; | ||
} | ||
console.log('Request: %s %s', req.method, path); | ||
var matchingRoute = router.match('/' + method + path); | ||
console.log('Request: %s %s', req.method, path); | ||
var matchingRoute = router.match('/' + method + path); | ||
if (!matchingRoute) return next(); | ||
if (!matchingRoute) return next(); | ||
res.setHeader('Content-Type', 'application/json'); | ||
var response = matchingRoute.fn(); | ||
res.setHeader('Content-Type', 'application/json'); | ||
var response = matchingRoute.fn(); | ||
res.write(response !== null ? JSON.stringify(response) : ''); | ||
res.end(); | ||
res.write(response !== null ? JSON.stringify(response) : ''); | ||
res.end(); | ||
}); | ||
}; | ||
}; |
@@ -0,1 +1,2 @@ | ||
/* eslint no-loop-func:0*/ | ||
'use strict'; | ||
@@ -23,4 +24,7 @@ | ||
methods.forEach(function (m) { | ||
if (keep) { | ||
replacement[path] ? replacement[path][m] = paths[path][m] : (replacement[path] = {}) && (replacement[path][m] = paths[path][m]); | ||
if (keep && replacement[path]) { | ||
replacement[path][m] = paths[path][m]; | ||
} else if (keep) { | ||
replacement[path] = replacement[path] || {}; | ||
replacement[path][m] = paths[path][m]; | ||
} else { | ||
@@ -27,0 +31,0 @@ delete paths[path][m]; |
{ | ||
"name": "swagger-mock-api", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Creates a connect middleware mock API from a Swagger 2.0 YAML file", | ||
@@ -15,3 +15,3 @@ "main": "dist/index.js", | ||
"build": "node node_modules/babel/bin/babel/index.js --optional runtime src --out-dir dist", | ||
"start": "node demo/index.js", | ||
"start": "cd demo && grunt", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
@@ -22,7 +22,7 @@ }, | ||
"dependencies": { | ||
"babel-runtime": "^5.5.6", | ||
"chance": "~0.7.5", | ||
"hoek": "~2.14.0", | ||
"routes": "~2.0.0", | ||
"swagger-parser": "^2.4.2", | ||
"babel-runtime": "^5.5.6" | ||
"swagger-parser": "^2.4.2" | ||
}, | ||
@@ -29,0 +29,0 @@ "devDependencies": { |
@@ -8,4 +8,2 @@ import url from 'url'; | ||
module.exports = function(config) { | ||
let router; | ||
let basePath; | ||
if (!config.swaggerFile) { | ||
@@ -19,35 +17,43 @@ throw new Error('Config is missing `swaggerFile` parameter'); | ||
parser.parse(config.swaggerFile, function(err, api) { | ||
if (err) throw err; | ||
let basePath; | ||
let router; | ||
if (config.ignorePaths) { | ||
api.paths = PrunePaths(api.paths, config.ignorePaths); | ||
} else if (config.mockPaths) { | ||
api.paths = PrunePaths(api.paths, config.mockPaths, true); | ||
} | ||
let parserPromise = new Promise((resolve) => { | ||
parser.parse(config.swaggerFile, function(err, api) { | ||
if (err) throw err; | ||
basePath = api.basePath || ''; | ||
router = ConfigureRouter(api.paths); | ||
if (config.ignorePaths) { | ||
api.paths = PrunePaths(api.paths, config.ignorePaths); | ||
} else if (config.mockPaths) { | ||
api.paths = PrunePaths(api.paths, config.mockPaths, true); | ||
} | ||
basePath = api.basePath || ''; | ||
router = ConfigureRouter(api.paths); | ||
resolve(); | ||
}); | ||
}); | ||
return function(req, res, next) { | ||
const method = req.method.toLowerCase(); | ||
parserPromise.then(() => { | ||
const method = req.method.toLowerCase(); | ||
let path = url.parse(req.url).pathname; | ||
path = path.replace(basePath + '/', ''); | ||
if (path.charAt(0) !== '/') { | ||
path = '/' + path; | ||
} | ||
let path = url.parse(req.url).pathname; | ||
path = path.replace(basePath + '/', ''); | ||
if (path.charAt(0) !== '/') { | ||
path = '/' + path; | ||
} | ||
console.log('Request: %s %s', req.method, path); | ||
const matchingRoute = router.match('/' + method + path); | ||
console.log('Request: %s %s', req.method, path); | ||
const matchingRoute = router.match('/' + method + path); | ||
if (!matchingRoute) return next(); | ||
if (!matchingRoute) return next(); | ||
res.setHeader('Content-Type', 'application/json'); | ||
const response = matchingRoute.fn(); | ||
res.setHeader('Content-Type', 'application/json'); | ||
const response = matchingRoute.fn(); | ||
res.write(response !== null ? JSON.stringify(response) : ''); | ||
res.end(); | ||
res.write(response !== null ? JSON.stringify(response) : ''); | ||
res.end(); | ||
}); | ||
}; | ||
}; |
@@ -0,1 +1,2 @@ | ||
/* eslint no-loop-func:0*/ | ||
export default function PrunePaths(paths, passthroughPaths, keep) { | ||
@@ -11,6 +12,7 @@ let replacement = {}; | ||
methods.forEach(m => { | ||
if (keep) { | ||
replacement[path] | ||
? (replacement[path][m] = paths[path][m]) | ||
: ((replacement[path] = {}) && (replacement[path][m] = paths[path][m])); | ||
if (keep && replacement[path]) { | ||
replacement[path][m] = paths[path][m]; | ||
} else if (keep) { | ||
replacement[path] = replacement[path] || {}; | ||
replacement[path][m] = paths[path][m]; | ||
} else { | ||
@@ -17,0 +19,0 @@ delete paths[path][m]; |
Sorry, the diff of this file is not supported yet
51920
19
983