Skip to content

Commit

Permalink
Merge pull request #4 from cdcabrera/development
Browse files Browse the repository at this point in the history
feat(custom responses): allow custom response override
  • Loading branch information
cdcabrera authored Jul 13, 2018
2 parents 110ae19 + 4f3c9eb commit 592c230
Show file tree
Hide file tree
Showing 7 changed files with 805 additions and 138 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"plugin:node/recommended",
"plugin:prettier/recommended"
],
"parserOptions":{
"ecmaVersion": 2018
},
"rules": {
"one-var": ["warn", { "var": "never", "let": "never", "const": "never" }],
"no-var": 1,
Expand Down
94 changes: 92 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,95 @@ From there you should be able to navigate to
```
From there navigate to
- the docs, http://localhost:[YOUR PORT]/docs/
- the api, http://localhost:[YOUR PORT]/[PATH TO API ENDPOINT]
- the docs, `http://localhost:[YOUR PORT]/docs/`
- the api, `http://localhost:[YOUR PORT]/[PATH TO API ENDPOINT]`
### More examples, and custom responses
1. Get random responses from both `success` and `error` examples with the `@apiMock {RandomResponse}` annotation
```js
/**
* @api {get} /hello/world/
* @apiMock {RandomResponse}
* @apiSuccess {String} foo
* @apiSuccess {String} bar
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "foo": "hello",
* "bar": "world",
* }
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "lorem": "dolor",
* "ipsum": "est",
* }
* @apiError {String} bad
* @apiError {String} request
* @apiErrorExample {json} Error-Response:
* HTTP/1.1 400 OK
* {
* "bad": "hello",
* "request": "world",
* }
*/
const getExample = () => {};
```
1. Get a random `success` response with the `@apiMock {RandomSuccess}` annotation. Or get a random `error` with the `@apiMock {RandomError}` annotation
```js
/**
* @api {get} /hello/world/
* @apiMock {RandomSuccess}
* @apiSuccess {String} foo
* @apiSuccess {String} bar
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "foo": "hello",
* "bar": "world",
* }
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "lorem": "dolor",
* "ipsum": "est",
* }
* @apiError {String} bad
* @apiError {String} request
* @apiErrorExample {json} Error-Response:
* HTTP/1.1 400 OK
* {
* "bad": "hello",
* "request": "world",
* }
*/
const getExample = () => {};
```
1. Force a specific response status with the `@apiMock {ForceStatus} [STATUS GOES HERE]` annotation. If you use a status without a supporting example the response status is still forced, but with fallback content.
```js
/**
* @api {get} /hello/world/
* @apiMock {ForceStatus} 400
* @apiSuccess {String} foo
* @apiSuccess {String} bar
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "foo": "hello",
* "bar": "world",
* }
* @apiError {String} bad
* @apiError {String} request
* @apiErrorExample {json} Error-Response:
* HTTP/1.1 400 OK
* {
* "bad": "hello",
* "request": "world",
* }
*/
const getExample = () => {};
```
32 changes: 27 additions & 5 deletions data/example.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* @api {get} /hello/world/
* @apiMock {RandomResponse}
* @apiSuccess {String} foo
* @apiSuccess {String} bar
* @apiSuccessExample {json} Success-Response:
Expand All @@ -8,19 +9,40 @@
* "foo": "hello",
* "bar": "world",
* }
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 201 OK
* {
* "lorem": "dolor",
* "ipsum": "est",
* }
* @apiError {String} bad
* @apiError {String} request
* @apiErrorExample {json} Error-Response:
* HTTP/1.1 400 OK
* {
* "bad": "hello",
* "request": "world",
* }
*/
const getExample = () => {};

/**
* @api {post} /hello/world/
* @apiMock {RandomResponse}
* @apiHeader {String} Authorization Authorization: Token AUTH_TOKEN
* @apiSuccess {String} foo
* @apiSuccess {String} bar
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* HTTP/1.1 201 OK
* {
* "foo": "hello",
* "bar": "world",
* "bar": "world"
* }
* @apiError {String} detail
* @apiErrorExample {json} Error-Response:
* HTTP/1.1 401 Unauthorized
* {
* "detail": "Authentication credentials were not provided."
* }
*/
const postExample = () => {};
Expand All @@ -34,7 +56,7 @@ const postExample = () => {};
* HTTP/1.1 200 OK
* {
* "foo": "hello",
* "bar": "world",
* "bar": "world"
* }
*/
const putExample = () => {};
Expand All @@ -48,7 +70,7 @@ const putExample = () => {};
* HTTP/1.1 200 OK
* {
* "foo": "hello",
* "bar": "world",
* "bar": "world"
* }
*/
const patchExample = () => {};
Expand All @@ -62,7 +84,7 @@ const patchExample = () => {};
* HTTP/1.1 200 OK
* {
* "foo": "hello",
* "bar": "world",
* "bar": "world"
* }
*/
const deleteExample = () => {};
Expand Down
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apidoc-mock",
"version": "0.0.1",
"version": "1.0.0",
"description": "Creates a mock server from apiDoc comments.",
"author": "cdcabrera",
"private": true,
Expand All @@ -21,9 +21,10 @@
"docker:build": "docker build -t apidoc-mock-test .",
"serve": "node src/index.js",
"test": "eslint ./src/",
"build": "apidoc -i ./data -o ./docs",
"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 npm run build:serve",
"opts": "apidoc --help"
},
"apidoc": {
"title": "Custom apiDocs",
Expand All @@ -33,18 +34,18 @@
"dependencies": {
"apidoc": "^0.17.6",
"express": "^4.16.3",
"nodemon": "^1.17.4"
"nodemon": "^1.18.1"
},
"devDependencies": {
"babel-eslint": "^8.2.3",
"eslint": "^4.19.1",
"eslint-config-esnext": "^2.0.0",
"eslint-config-node": "^2.0.0",
"eslint": "^5.1.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.11.0",
"eslint-plugin-import": "^2.13.0",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-prettier": "^2.6.0",
"prettier": "^1.12.1"
"eslint-plugin-prettier": "^2.6.2",
"prettier": "^1.13.7"
}
}
27 changes: 27 additions & 0 deletions src/api_mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
let group = '';

const parse = (content, source, defaultGroup) => {
group = defaultGroup || 'settings';

const keyValue = content.split('}');

let key = (keyValue[0] || '').replace(/({|^\s+|\s+$)/, '');
const value = (keyValue[1] || '').replace(/(^\s+|\s+$)/, '');

key = key.replace(/(?:^\w|[A-Z]|\b\w)/g, function(letter, index) {
return index === 0 ? letter.toLowerCase() : letter.toUpperCase();
});

return { [key]: value };
};

const getGroup = () => group;

const path = () => `local.mock.${getGroup()}`;

module.exports = {
parse,
path,
getGroup: getGroup,
method: 'push'
};
Loading

0 comments on commit 592c230

Please sign in to comment.