openapi-mock-server
Advanced tools
Comparing version 0.1.1 to 0.1.2
50
index.js
@@ -1,28 +0,46 @@ | ||
const Port = require("get-port") | ||
const App = require("express")() | ||
const OpenApiMock = require("openapi-mockk") | ||
const Opts = require('mri'); | ||
const Port = require('get-port') | ||
const App = require('express')() | ||
const OpenApiMock = require('openapi-mockk') | ||
const args = require('args') | ||
const debug = require('debug')('openapi-mock-server') | ||
const chalk = require('chalk') | ||
const args = Opts(process.argv.slice(2)) | ||
args.option(['a', 'api'], 'Path to OpenAPI specification file. JSON or YAML.') | ||
args.example('openapi-mock-server --api=petstore.yaml', 'To run a server serving examples from specification, simply point to a file') | ||
const opts = args.parse(process.argv) | ||
if (args.api) { | ||
App.all("*", (req, res, next) => { | ||
const content = req.headers["content-type"] || "application/json" | ||
debug(`Reading OpenAPI in ${opts.api}`) | ||
const mock = OpenApiMock(opts.api) | ||
mock.openapi.then(api => { | ||
debug(`OpenAPI specification for ${api.info.title} loaded successfuly`) | ||
}) | ||
if (opts.api) { | ||
App.all('*', (req, res, next) => { | ||
const content = req.headers['content-type'] || 'application/json' | ||
const path = req.path | ||
const operation = req.method.toLowerCase() | ||
const mock = OpenApiMock(args.api).responses({ | ||
debug(`HTTP ${req.method} ${req.path}; content-type=${content}`) | ||
mock.responses({ | ||
path, | ||
operation, | ||
response: '200', | ||
content, | ||
content | ||
}).then(mock => { | ||
res.send(mock[path][operation].responses) | ||
if (path in mock) { | ||
const response = mock[path][operation].responses | ||
debug(`200: ${response}`) | ||
res.send(response) | ||
} else { | ||
debug(`404: path not found`) | ||
res.sendStatus(404) | ||
} | ||
}) | ||
}) | ||
Port({port: 3000}).then(port => { | ||
App.listen(port, () => console.log(`OpenApiMock server listening on port ${port}`)) | ||
}); | ||
App.listen(port, () => console.log(chalk`{cyan openapi-mock-server: listening @ {underline http://localhost:${port}}}`)) | ||
}) | ||
} else { | ||
console.log("OpenAPI file required, --api=api.yaml. Supports json and yaml.") | ||
} | ||
args.showHelp() | ||
} |
{ | ||
"name": "openapi-mock-server", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Create mock server based on Open API 3.0 specification file ", | ||
"main": "index.js", | ||
"repository": "unjello/openapi-mock-server", | ||
"repository": "unjello/openapi-mock-server", | ||
"scripts": { | ||
@@ -19,5 +19,7 @@ "test": "standard" | ||
"dependencies": { | ||
"args": "^3.0.8", | ||
"chalk": "^2.3.2", | ||
"debug": "^3.1.0", | ||
"express": "^4.16.3", | ||
"get-port": "^3.2.0", | ||
"mri": "^1.1.0", | ||
"openapi-mockk": "^0.1.0" | ||
@@ -24,0 +26,0 @@ }, |
@@ -9,2 +9,4 @@ [![npm version][npm_badge]][npm_link] [![Build Status][travis_badge]][travis_link] [![downloads][npm_dl_badge]][npm_dl_link] [![js-standard-style][js_standard_badge]][js_standard_link] | ||
Requires Node 8 or newer. | ||
```bash | ||
@@ -11,0 +13,0 @@ $ pnpm i -g openapi-mock-server |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
66800
7
42
34
6
+ Addedargs@^3.0.8
+ Addedchalk@^2.3.2
+ Addeddebug@^3.1.0
+ Addedansi-styles@3.2.1(transitive)
+ Addedargs@3.0.8(transitive)
+ Addedcamelcase@4.1.0(transitive)
+ Addedchalk@2.1.02.4.2(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedhas-flag@2.0.03.0.0(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedmri@1.1.0(transitive)
+ Addedpkginfo@0.4.1(transitive)
+ Addedstring-similarity@1.2.0(transitive)
+ Addedsupports-color@4.5.05.5.0(transitive)
- Removedmri@^1.1.0
- Removedmri@1.2.0(transitive)