Skip to content

Commit

Permalink
feat(api spec): issues/17 local run open api spec (#21)
Browse files Browse the repository at this point in the history
* local run api documentation
* minor fix for standalone npm script
  • Loading branch information
cdcabrera authored Jun 27, 2019
1 parent 032296f commit 58182e0
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ public/locales/*.json
.chrome
.proxy
.docs
.openapi
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
},
"scripts": {
"api:dev": "mock -p 5000 -w ./src/services",
"api:docs": "node ./scripts/openapi.docs.js",
"api:proxy": "sh ./scripts/proxy.api.sh -d \"ci.foo.redhat.com\" -p 1337 -c \"$(pwd)/config/spandx.config.js\"",
"api:proxyClean": "sh ./scripts/proxy.api.sh -u",
"build": "run-s -l build:pre build:version build:js build:post test:integration",
Expand All @@ -63,7 +64,7 @@
"start": "sh ./scripts/dev.chrome.sh; run-p -l api:dev start:js",
"start:js": "react-scripts start",
"start:proxy": "sh -ac '. ./.env.proxy; open https://ci.foo.redhat.com:1337/; run-p -l start:js api:proxy api:dev'",
"start:standalone": "rm ./.env.development.local; run-s -l api:dev start:js",
"start:standalone": "rm ./.env.development.local; run-p -l api:dev start:js",
"test": "run-s test:lint test:ci",
"test:ci": "export CI=true; react-scripts test --env=jsdom --roots=./src --coverage",
"test:clearCache": "react-scripts test --clearCache",
Expand Down Expand Up @@ -116,12 +117,15 @@
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-prettier": "^3.1.0",
"eslint-plugin-react": "^7.13.0",
"express": "^4.17.1",
"gettext-extractor": "^3.4.3",
"moxios": "^0.4.0",
"npm-run-all": "^4.1.5",
"prettier": "^1.17.1",
"redux-mock-store": "^1.5.3",
"standard-version": "^6.0.1"
"standard-version": "^6.0.1",
"swagger-ui-express": "^4.0.6",
"yamljs": "^0.3.0"
},
"resolutions": {
"**/eslint": "5.16.0"
Expand Down
66 changes: 66 additions & 0 deletions scripts/openapi.docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const express = require('express');
const swaggerUi = require('swagger-ui-express');
const YAML = require('yamljs');
const openApiSpecs = [
{
file:
'https://raw.githubusercontent.com/RedHatInsights/rhsm-subscriptions/master/api/rhsm-subscriptions-api-spec.yaml',
outputDir: `${process.cwd()}/.openapi`,
outputFileName: 'rhsm.yaml',
port: 5050
}
];
const cache = {
tryAgainCount: 0
};

const serveDocs = (files = []) => {
files.forEach(yamlFile => {
if (fs.existsSync(yamlFile.file)) {
const app = express();

app.use('/docs/api', swaggerUi.serve, swaggerUi.setup(YAML.load(yamlFile.file)));

app.listen(yamlFile.port, () => {
console.log(
`\nYou can now view API docs in the browser.\n Open: http://localhost:${yamlFile.port}/docs/api\n`
);
});
} else if (cache.tryAgainCount < 10) {
setTimeout(() => {
console.info(`Locating ${yamlFile.file}...`);
cache.tryAgainCount += 1;
serveDocs(yamlFile.file, yamlFile.port);
}, 1000);
} else {
console.info(`${yamlFile.file} doesn't exist`);
}
});
};

const getLocalApiSpec = (inputPaths = []) => {
const outputPaths = [];

inputPaths.forEach(inputPath => {
const outputPath = path.join(inputPath.outputDir, inputPath.outputFileName);

if (fs.existsSync(outputPath)) {
outputPaths.push({ file: outputPath, port: inputPath.port });
} else {
if (!fs.existsSync(inputPath.outputDir)) {
fs.mkdirSync(inputPath.outputDir);
}

execSync(`curl ${inputPath.file} > ${outputPath}`);

outputPaths.push({ file: outputPath, port: inputPath.port });
}
});

return outputPaths;
};

serveDocs(getLocalApiSpec(openApiSpecs));
24 changes: 22 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5283,7 +5283,7 @@ expect@^24.8.0:
jest-message-util "^24.8.0"
jest-regex-util "^24.3.0"

express@^4.16.2, express@^4.16.4:
express@^4.16.2, express@^4.16.4, express@^4.17.1:
version "4.17.1"
resolved "https://registry.yarnpkg.com/express/-/express-4.17.1.tgz#4491fc38605cf51f8629d39c2b5d026f98a4c134"
integrity sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==
Expand Down Expand Up @@ -5951,7 +5951,7 @@ glob-to-regexp@^0.3.0:
resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.3.0.tgz#8c5a1494d2066c570cc3bfe4496175acc4d502ab"
integrity sha1-jFoUlNIGbFcMw7/kSWF1rMTVAqs=

"glob@5 - 7", glob@^7.0.0, glob@^7.0.3, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@~7.1.1:
"glob@5 - 7", glob@^7.0.0, glob@^7.0.3, glob@^7.0.5, glob@^7.1.1, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@~7.1.1:
version "7.1.4"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.4.tgz#aa608a2f6c577ad357e1ae5a5c26d9a8d1969255"
integrity sha512-hkLPepehmnKk41pUGm3sYxoFs/umurYfYJCerbXEyFIWcAzvpipAgVkBqqT9RBKMGjnq6kMuyYwha6csxbiM1A==
Expand Down Expand Up @@ -11729,6 +11729,18 @@ svgo@^1.0.0, svgo@^1.2.1:
unquote "~1.1.1"
util.promisify "~1.0.0"

swagger-ui-dist@^3.18.1:
version "3.22.3"
resolved "https://registry.yarnpkg.com/swagger-ui-dist/-/swagger-ui-dist-3.22.3.tgz#f2042966f8e0eef0c5730cf90891a89dab7810e1"
integrity sha512-tmjAsqT43pqg5UoiQ2805c+juX0ASSoI/Ash/0c19jjAOFtTfE93ZrzmFd9hjqVgre935CYeXT0uaku42Lu8xg==

swagger-ui-express@^4.0.6:
version "4.0.6"
resolved "https://registry.yarnpkg.com/swagger-ui-express/-/swagger-ui-express-4.0.6.tgz#c7a1cc3952a01017483b382d67e27196c82577c3"
integrity sha512-7YkBfVWRYjvnGITs7vygM8VNF91iUwX8ReHQaTD9I7q8Ky8SYXZ81gyFc+kovE34iSbCC1yW+n2oII3b3uSxXQ==
dependencies:
swagger-ui-dist "^3.18.1"

symbol-observable@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804"
Expand Down Expand Up @@ -13243,6 +13255,14 @@ yallist@^3.0.0, yallist@^3.0.2, yallist@^3.0.3:
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.3.tgz#b4b049e314be545e3ce802236d6cd22cd91c3de9"
integrity sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==

yamljs@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/yamljs/-/yamljs-0.3.0.tgz#dc060bf267447b39f7304e9b2bfbe8b5a7ddb03b"
integrity sha512-C/FsVVhht4iPQYXOInoxUM/1ELSf9EsgKH34FofQOp6hwCPrW4vG4w5++TED3xRUo8gD7l0P1J1dLlDYzODsTQ==
dependencies:
argparse "^1.0.7"
glob "^7.0.5"

yargs-parser@^10.1.0:
version "10.1.0"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8"
Expand Down

0 comments on commit 58182e0

Please sign in to comment.