-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(buildDocs): consolidate setupDocs (#214)
* index, move setupDocs, case CACHE * buildDocs, consolidate setup, build docs funcs
- Loading branch information
Showing
7 changed files
with
173 additions
and
184 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,48 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`BuildDocs should fail a build gracefully: fail 1`] = `{}`; | ||
exports[`BuildDocs should fail a build gracefully: fail 1`] = `[]`; | ||
|
||
exports[`BuildDocs should handle additional response content types: html mock 1`] = ` | ||
{ | ||
"error": undefined, | ||
"filename": "html.js", | ||
"group": undefined, | ||
"groupTitle": undefined, | ||
"name": "GetHelloWorldHtmlHtml", | ||
"success": "{"examples":[{"title":"Success-Response:","content":"HTTP/1.1 200 OK\\n<!DOCTYPE html>\\n<html>\\n <head>hello</head>\\n <body>world</body>\\n</html>","type":"html"}]}", | ||
"title": "", | ||
"type": "get", | ||
"url": "/hello/world/html.html", | ||
"version": "0.0.0", | ||
} | ||
`; | ||
|
||
exports[`BuildDocs should handle additional response content types: svg mock 1`] = ` | ||
{ | ||
"error": undefined, | ||
"filename": "svg.js", | ||
"group": undefined, | ||
"groupTitle": undefined, | ||
"name": "GetHelloWorldSvgSvg", | ||
"success": "{"examples":[{"title":"Success-Response:","content":"HTTP/1.1 200 OK\\n<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>\\n<!DOCTYPE svg PUBLIC \\"-//W3C//DTD SVG 1.1//EN\\" \\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\\">\\n<svg version=\\"1.1\\" xmlns=\\"http://www.w3.org/2000/svg\\" x=\\"0px\\" y=\\"0px\\" width=\\"10px\\" height=\\"10px\\" viewBox=\\"0 0 10 10\\">\\n<g/>\\n</svg>","type":"svg"}]}", | ||
"title": "", | ||
"type": "get", | ||
"url": "/hello/world/svg.svg", | ||
"version": "0.0.0", | ||
} | ||
`; | ||
|
||
exports[`BuildDocs should setup api docs and create a predictable output: setupDocs 1`] = ` | ||
{ | ||
"error": "{"examples":[{"title":"Error-Response:","content":"HTTP/1.1 400 OK\\n{\\n \\"error\\": \\"test\\"\\n}","type":"json"}]}", | ||
"filename": "test.js", | ||
"group": undefined, | ||
"groupTitle": undefined, | ||
"name": "GetHelloWorld", | ||
"success": "{"examples":[{"title":"Success-Response:","content":"HTTP/1.1 200 OK\\n{\\n \\"success\\": \\"test\\"\\n}","type":"json"}]}", | ||
"title": "", | ||
"type": "get", | ||
"url": "/hello/world/", | ||
"version": "0.0.0", | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,89 @@ | ||
const { buildDocs } = require('../buildDocs'); | ||
const { setupDocs } = require('../buildDocs'); | ||
const { OPTIONS } = require('../../global'); | ||
|
||
describe('BuildDocs', () => { | ||
it('should have specific defined properties', () => { | ||
expect(buildDocs).toBeDefined(); | ||
expect(setupDocs()).toBeDefined(); | ||
}); | ||
|
||
it('should fail a build gracefully', () => { | ||
expect(buildDocs({})).toMatchSnapshot('fail'); | ||
expect(setupDocs({})).toMatchSnapshot('fail'); | ||
}); | ||
|
||
it('should setup api docs and create a predictable output', () => { | ||
const apiFixture = generateFixture( | ||
`/** | ||
* @api {get} /hello/world/ | ||
* @apiSuccessExample {json} Success-Response: | ||
* HTTP/1.1 200 OK | ||
* { | ||
* "success": "test" | ||
* } | ||
* @apiErrorExample {json} Error-Response: | ||
* HTTP/1.1 400 OK | ||
* { | ||
* "error": "test" | ||
* } | ||
*/`, | ||
{ dir: './.fixtures/predictable', filename: 'test.js' } | ||
); | ||
|
||
const [helloWorld] = setupDocs({ ...OPTIONS, watchPath: [apiFixture.dir], docsPath: 'lorem-ipsum' }); | ||
|
||
expect({ | ||
...helloWorld, | ||
success: JSON.stringify(helloWorld.success), | ||
error: JSON.stringify(helloWorld.error), | ||
group: undefined, | ||
groupTitle: undefined | ||
}).toMatchSnapshot('setupDocs'); | ||
}); | ||
|
||
it('should handle additional response content types', () => { | ||
const htmlFixture = generateFixture( | ||
`/** | ||
* @api {get} /hello/world/html.html | ||
* @apiSuccessExample {html} Success-Response: | ||
* HTTP/1.1 200 OK | ||
* <!DOCTYPE html> | ||
* <html> | ||
* <head>hello</head> | ||
* <body>world</body> | ||
* </html> | ||
*/`, | ||
{ dir: './.fixtures/content-types', filename: 'html.js' } | ||
); | ||
|
||
generateFixture( | ||
`/** | ||
* @api {get} /hello/world/svg.svg | ||
* @apiSuccessExample {svg} Success-Response: | ||
* HTTP/1.1 200 OK | ||
* <?xml version="1.0" encoding="utf-8"?> | ||
* <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> | ||
* <svg version="1.1" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="10px" height="10px" viewBox="0 0 10 10"> | ||
* <g/> | ||
* </svg> | ||
*/`, | ||
{ dir: './.fixtures/content-types', filename: 'svg.js', resetDir: false } | ||
); | ||
|
||
const [html, svg] = setupDocs({ ...OPTIONS, watchPath: [htmlFixture.dir], docsPath: 'lorem-ipsum' }); | ||
|
||
expect({ | ||
...html, | ||
success: JSON.stringify(html.success), | ||
error: JSON.stringify(html.error), | ||
group: undefined, | ||
groupTitle: undefined | ||
}).toMatchSnapshot('html mock'); | ||
|
||
expect({ | ||
...svg, | ||
success: JSON.stringify(svg.success), | ||
error: JSON.stringify(svg.error), | ||
group: undefined, | ||
groupTitle: undefined | ||
}).toMatchSnapshot('svg mock'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,39 @@ | ||
const apidoc = require('apidoc'); | ||
const { logger } = require('../logger/configLogger'); | ||
const { OPTIONS } = require('../global'); | ||
|
||
/** | ||
* Compile/build ApiDoc documentation. | ||
* Build ApiDoc documentation. | ||
* | ||
* @param {object} params | ||
* @param {object} params.apiDocsConfig | ||
* @returns {object} | ||
* @param {object} options | ||
* @param {OPTIONS.apiDocBaseConfig} options.apiDocBaseConfig | ||
* @param {string[]} options.watchPath | ||
* @param {string} options.docsPath | ||
* @param {string} options.silent | ||
* @returns {*|{}|null} | ||
*/ | ||
const buildDocs = ({ apiDocsConfig = null } = {}) => { | ||
if (apiDocsConfig) { | ||
try { | ||
const { data } = apidoc.createDoc(apiDocsConfig); | ||
const updatedResult = JSON.parse(data); | ||
logger.info('buildDocs.read.apiJsonFile'); | ||
return updatedResult; | ||
} catch (e) { | ||
logger.error(`buildDocs.apiDoc.createDoc[${e.message}]`); | ||
} | ||
const setupDocs = ({ apiDocBaseConfig, watchPath: src, docsPath: dest, silent } = OPTIONS) => { | ||
if ((!Array.isArray(src) && !src?.length) || !dest) { | ||
return []; | ||
} | ||
|
||
return {}; | ||
const apiDocsConfig = { | ||
...apiDocBaseConfig, | ||
src, | ||
dest, | ||
silent: apiDocBaseConfig.silent || silent | ||
}; | ||
|
||
try { | ||
const { data } = apidoc.createDoc(apiDocsConfig); | ||
const updatedResult = JSON.parse(data); | ||
logger.info('buildDocs.read.apiJsonFile'); | ||
return updatedResult; | ||
} catch (e) { | ||
logger.error(`buildDocs.apiDoc.createDoc[${e.message}]`); | ||
} | ||
|
||
return []; | ||
}; | ||
|
||
module.exports = { buildDocs }; | ||
module.exports = { setupDocs }; |
Oops, something went wrong.