Skip to content

Commit

Permalink
fix(json response): 8 array response
Browse files Browse the repository at this point in the history
* relax json pre-parse, pass any value through including arrays
* update docker file, build scripts
* build packages, yarn lock, lint rules updated
  • Loading branch information
cdcabrera committed Oct 14, 2018
1 parent f68dbc0 commit b9d073d
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 582 deletions.
33 changes: 22 additions & 11 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"parser": "babel-eslint",
"env": {
"node": true,
"es6": true
Expand All @@ -16,27 +17,37 @@
"ecmaVersion": 2018
},
"rules": {
"one-var": ["warn", { "var": "never", "let": "never", "const": "never" }],
"no-var": 1,
"comma-dangle": 1,
"arrow-parens": [
"error",
"as-needed"
],
"comma-dangle": 0,
"max-len": [
"error",
{
"code": 200,
"ignoreUrls": true
}
],
"no-plusplus": 0,
"prefer-const": 1,
"padded-blocks": 0,
"space-before-function-paren": 0,
"prefer-arrow-callback": 0,
"func-names": 0,
"prefer-destructuring": 0,
"no-undef": 0,
"no-multi-spaces": 0,
"no-console": 0,
"no-debugger": 1,
"prettier/prettier": [
"warn",
"error",
{
"singleQuote": true,
"printWidth": 100
}
],
"quotes": [0, "single"]
"valid-jsdoc": [
1,
{
"requireParamDescription": false,
"requireReturnDescription": false,
"requireReturn": false
}
]
}
}
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ COPY . /app
VOLUME /data
VOLUME /docs

RUN yarn --production --non-interactive --silent \
RUN yarn --production --non-interactive \
&& yarn cache clean

ENV PORT=8000

EXPOSE ${PORT}
CMD ["yarn", "start"]
CMD ["yarn", "start:container"]
28 changes: 15 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apidoc-mock",
"version": "2.0.0",
"version": "2.0.1",
"description": "Creates a mock server from apiDoc comments.",
"author": "cdcabrera",
"private": true,
Expand All @@ -18,12 +18,14 @@
"main": "src/index.js",
"scripts": {
"docker:run": "docker stop mock-api-test; docker run -i --rm -p 8080:8000 -v \"$(pwd)/data:/app/data\" --name mock-api-test apidoc-mock-test",
"docker:stop": "docker stop mock-api-test",
"docker:build": "docker build -t apidoc-mock-test .",
"serve": "node src/index.js",
"test": "eslint ./src/",
"build": "apidoc --parse-parsers apimock=./src/api_mock.js -i ./data -o ./docs",
"build:serve": "npm run build && npm run serve",
"start": "nodemon --watch data npm run build:serve",
"start": "nodemon --watch data --watch src npm run build:serve",
"start:container": "nodemon --watch data npm run build:serve",
"opts": "apidoc --help"
},
"apidoc": {
Expand All @@ -33,19 +35,19 @@
},
"dependencies": {
"apidoc": "^0.17.6",
"express": "^4.16.3",
"nodemon": "^1.18.3"
"express": "^4.16.4",
"nodemon": "^1.18.4"
},
"devDependencies": {
"babel-eslint": "^8.2.6",
"eslint": "^5.3.0",
"eslint-config-esnext": "^3.0.0",
"eslint-config-node": "^3.0.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-babel": "^5.1.0",
"eslint-plugin-import": "^2.13.0",
"babel-eslint": "^10.0.1",
"eslint": "^5.7.0",
"eslint-config-esnext": "^4.0.0",
"eslint-config-node": "^4.0.0",
"eslint-config-prettier": "^3.1.0",
"eslint-plugin-babel": "^5.2.1",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^7.0.1",
"eslint-plugin-prettier": "^2.6.2",
"prettier": "^1.14.2"
"eslint-plugin-prettier": "^3.0.0",
"prettier": "^1.14.3"
}
}
59 changes: 25 additions & 34 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class LoadApi {
/**
* Create a mock API server from ApiDoc.
*
* @param apiDocsConfig {Object}
* @param apiJsonFile {String}
* @param port {Number}
* @param apiDocRoute {String}
* @param {object} apiDocsConfig
* @param {string} apiJsonFile
* @param {number} port
* @param {string} apiDocRoute
*/
constructor({ apiDocsConfig, apiJsonFile, port, apiDocRoute }) {
const apiJson = LoadApi.buildApiDocs(apiDocsConfig, apiJsonFile);
Expand All @@ -39,11 +39,11 @@ class LoadApi {
/**
* Set http status
*
* @param examples {Array}
* @param response {String}
* @param type {String}
* @param url {String}
* @returns {Object}
* @param {array} examples
* @param {string} response
* @param {string} type
* @param {string} url
* @returns {object}
*/
static parseStatus(examples = [], response = null, type = null, url = null) {
return examples.map(val => {
Expand All @@ -69,8 +69,8 @@ class LoadApi {
/**
* Parse custom mock settings.
*
* @param mockSettings {Array}
* @returns {Object}
* @param {array} mockSettings
* @returns {object}
*/
static parseMockSettings(mockSettings = {}) {
const settings = {};
Expand Down Expand Up @@ -126,8 +126,8 @@ class LoadApi {
/**
* Return passed mock mime type and parsed content
*
* @param content {String}
* @param type {String}
* @param {string} content
* @param {string} type
* @returns {{parsedContent: string, parsedType: string}}
*/
static parseContentAndType(content = '', type = 'text') {
Expand All @@ -145,24 +145,15 @@ class LoadApi {
switch (type) {
case 'json':
parsedType = 'application/json';

if (parsable) {
parsedContent = `{ ${content
.split('{')
.slice(1)
.join('{')}`;
}
break;
case 'xml':
case 'html':
case 'csv':
parsedType = `text/${type}`;
break;
case 'svg':
parsedType = 'image/svg+xml';
break;
case 'csv':
parsedType = 'text/csv';
break;
default:
parsedType = 'text/plain';
break;
Expand All @@ -174,9 +165,9 @@ class LoadApi {
/**
* Compile/build ApiDoc documentation.
*
* @param apiDocsConfig {Object}
* @param apiJsonFile {String}
* @returns {Object}
* @param {object} apiDocsConfig
* @param {string} apiJsonFile
* @returns {object}
*/
static buildApiDocs(apiDocsConfig = {}, apiJsonFile = null) {
let result;
Expand All @@ -199,10 +190,10 @@ class LoadApi {
/**
* Return forced response, or a general example.
*
* @param mockSettings {Object}
* @param exampleObjects {Array}
* @param successObjects {Array}
* @param errorObjects {Array}
* @param {object} mockSettings
* @param {array} exampleObjects
* @param {array} successObjects
* @param {array} errorObjects
* @returns {{type: string, status: number, content: string}}
*/
static exampleResponse(mockSettings, exampleObjects, successObjects, errorObjects) {
Expand Down Expand Up @@ -269,7 +260,7 @@ class LoadApi {
/**
* Return a 401 specific example.
*
* @param errorObjects {Array}
* @param {array} errorObjects
* @returns {{type: string, status: number, content: string}}
*/
static parseAuthExample(errorObjects = []) {
Expand All @@ -285,7 +276,7 @@ class LoadApi {
/**
* Open request headers up. Parse OPTIONS or continue
*
* @param apiDocRoute {String}
* @param {string} apiDocRoute
*/
setupResponse(apiDocRoute) {
this.app.use(`/${apiDocRoute}`, express.static(apiDocRoute));
Expand Down Expand Up @@ -314,8 +305,8 @@ class LoadApi {
/**
* Setup API response.
*
* @param apiJson {Array}
* @param port {Number}
* @param {array} apiJson
* @param {number} port
*/
setupApi(apiJson = [], port = 8000) {
let routesLoaded = 0;
Expand Down
Loading

0 comments on commit b9d073d

Please sign in to comment.