swagger-mock-api
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -15,3 +15,20 @@ 'use strict'; | ||
mockApi({ | ||
swaggerFile: path.join(__dirname, './test.yaml') | ||
swaggerFile: path.join(__dirname, './test.yaml'), | ||
// | ||
// Example ways to ignore paths (i.e. to allow proxying to a live server) | ||
// | ||
ignorePaths: [ | ||
// | ||
'GET /pets/{id}', // ignore specific methods in a path | ||
// | ||
'/pets' // ignore the entire path | ||
// | ||
] | ||
// | ||
// Alternatively, specify ONLY the paths you want mocked: | ||
// | ||
// mockPaths: [ | ||
// '/pets/{id}', // all methods | ||
// 'GET DELETE /pets' // only specific methods | ||
// ] | ||
}) | ||
@@ -18,0 +35,0 @@ ], |
@@ -62,3 +62,3 @@ 'use strict'; | ||
v: function () { | ||
return (0, _MockData2['default'])(responseSchema); | ||
return _MockData2['default'](responseSchema); | ||
} | ||
@@ -83,3 +83,3 @@ }; | ||
return function () { | ||
return (0, _MockData2['default'])(potentialResponses['default']); | ||
return _MockData2['default'](potentialResponses['default']); | ||
}; | ||
@@ -86,0 +86,0 @@ } |
@@ -17,11 +17,28 @@ 'use strict'; | ||
var _PrunePaths = require('./PrunePaths'); | ||
var _PrunePaths2 = _interopRequireDefault(_PrunePaths); | ||
module.exports = function (config) { | ||
var router = undefined; | ||
var basePath = undefined; | ||
if (!config.swaggerFile) { | ||
throw new Error('Config is missing `swaggerFile` parameter'); | ||
} | ||
_swaggerParser2['default'].parse(config.swaggerFile, function (err, api, metadata) { | ||
if (config.ignorePaths && config.mockRoutes) { | ||
throw new Error('Cannot specify both ignorePaths and mockPaths in config'); | ||
} | ||
_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); | ||
} | ||
basePath = api.basePath || ''; | ||
router = (0, _ConfigureRouter2['default'])(api.paths); | ||
router = _ConfigureRouter2['default'](api.paths); | ||
}); | ||
@@ -28,0 +45,0 @@ |
{ | ||
"name": "swagger-mock-api", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Creates a connect middleware mock API from a Swagger 2.0 YAML file", | ||
@@ -24,7 +24,9 @@ "main": "dist/index.js", | ||
"routes": "~2.0.0", | ||
"swagger-parser": "^2.4.2" | ||
"swagger-parser": "^2.4.2", | ||
"babel-runtime": "^5.5.6" | ||
}, | ||
"devDependencies": { | ||
"babel": "^5.5.6", | ||
"babel-runtime": "^5.5.6", | ||
"babel": "^5.6.14", | ||
"babel-eslint": "^3.1.19", | ||
"eslint": "^0.24.0", | ||
"grunt": "^0.4.5", | ||
@@ -31,0 +33,0 @@ "grunt-contrib-connect": "^0.10.1" |
@@ -5,2 +5,9 @@ #mock-api | ||
- [mock-api](#) | ||
- [installation](#) | ||
- [Using the middleware](#) | ||
- [Ignoring specific paths](#) | ||
- [Specifying custom Chance options](#) | ||
- [A note on types:](#) | ||
## installation | ||
@@ -56,2 +63,18 @@ | ||
### Ignoring specific paths | ||
There are two mutually exlusive options: `ignorePaths` and `mockPaths`; the former specifies which paths to ignore, while the latter specifies the only paths that should be mocked. Depending on what state of completion the backend API is in, you may want to start with ignorePaths (adding as the API improves) and eventually switch over to `mockPaths` and remove until the API is complete. | ||
```javascript | ||
// .... | ||
mockApi({ | ||
swaggerFiles: 'path-to-file', | ||
ignorePaths: [ | ||
'PUT DELETE /pets/{id}', // you can ignore specific methods of a path | ||
'/pets/{id}' // or ignore EVERY method for a path | ||
] | ||
}) | ||
``` | ||
## Specifying custom Chance options | ||
@@ -97,5 +120,1 @@ | ||
All of the primitive types defined in the [Swagger specification](https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#data-types) are supported except for `file` and `password`. Currently, the `format` property is ignored; use `x-chance-type` instead. The server will error on any request with a type other than one of the primitive types if there is no valid x-chance-type also defined. | ||
## Todos: | ||
* Actual unit tests |
@@ -1,3 +0,1 @@ | ||
'use strict'; | ||
import url from 'url'; | ||
@@ -7,2 +5,3 @@ import parser from 'swagger-parser'; | ||
import ConfigureRouter from './ConfigureRouter'; | ||
import PrunePaths from './PrunePaths'; | ||
@@ -12,6 +11,19 @@ module.exports = function(config) { | ||
let basePath; | ||
if (!config.swaggerFile) { | ||
throw new Error('Config is missing `swaggerFile` parameter'); | ||
} | ||
parser.parse(config.swaggerFile, function(err, api, metadata) { | ||
if (config.ignorePaths && config.mockRoutes) { | ||
throw new Error('Cannot specify both ignorePaths and mockPaths in config'); | ||
} | ||
parser.parse(config.swaggerFile, function(err, api) { | ||
if (err) throw err; | ||
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 || ''; | ||
@@ -21,5 +33,4 @@ router = ConfigureRouter(api.paths); | ||
return function(req, res, next) { | ||
let method = req.method.toLowerCase(); | ||
const method = req.method.toLowerCase(); | ||
@@ -33,3 +44,3 @@ let path = url.parse(req.url).pathname; | ||
console.log('Request: %s %s', req.method, path); | ||
let matchingRoute = router.match('/' + method + path); | ||
const matchingRoute = router.match('/' + method + path); | ||
@@ -39,3 +50,3 @@ if (!matchingRoute) return next(); | ||
res.setHeader('Content-Type', 'application/json'); | ||
let response = matchingRoute.fn(); | ||
const response = matchingRoute.fn(); | ||
@@ -45,2 +56,2 @@ res.write(response !== null ? JSON.stringify(response) : ''); | ||
}; | ||
} | ||
}; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
51183
99
18
966
118
0
5
5
+ Addedbabel-runtime@^5.5.6
+ Addedbabel-runtime@5.8.38(transitive)
+ Addedcore-js@1.2.7(transitive)