use json-schema-ref-parser to bundle input from multiple files

This commit is contained in:
Chris Armstrong 2021-02-18 15:09:44 +11:00
parent f864007618
commit 87728f12bd
3 changed files with 35 additions and 2 deletions

View File

@ -65,6 +65,7 @@
"commander": "^7.0.0",
"handlebars": "^4.7.6",
"js-yaml": "^4.0.0",
"json-schema-ref-parser": "^9.0.7",
"mkdirp": "^1.0.4",
"rimraf": "^3.0.2"
},

View File

@ -1,3 +1,4 @@
import RefParser from 'json-schema-ref-parser';
import { load } from 'js-yaml';
import { extname } from 'path';
@ -12,20 +13,25 @@ import { readSpec } from './readSpec';
export async function getOpenApiSpec(input: string): Promise<any> {
const extension = extname(input).toLowerCase();
const content = await readSpec(input);
let rootObject: any;
switch (extension) {
case '.yml':
case '.yaml':
try {
return load(content);
rootObject = load(content);
} catch (e) {
throw new Error(`Could not parse OpenApi YAML: "${input}"`);
}
break;
default:
try {
return JSON.parse(content);
rootObject = JSON.parse(content);
} catch (e) {
throw new Error(`Could not parse OpenApi JSON: "${input}"`);
}
break;
}
const transformed = await RefParser.bundle(rootObject);
return transformed;
}

View File

@ -2,6 +2,15 @@
# yarn lockfile v1
"@apidevtools/json-schema-ref-parser@9.0.7":
version "9.0.7"
resolved "https://registry.yarnpkg.com/@apidevtools/json-schema-ref-parser/-/json-schema-ref-parser-9.0.7.tgz#64aa7f5b34e43d74ea9e408b90ddfba02050dde3"
integrity sha512-QdwOGF1+eeyFh+17v2Tz626WX0nucd1iKOm6JUTUvCZdbolblCOOQCxGrQPY0f7jEhn36PiAWqZnsC2r5vmUWg==
dependencies:
"@jsdevtools/ono" "^7.1.3"
call-me-maybe "^1.0.1"
js-yaml "^3.13.1"
"@babel/cli@7.12.16":
version "7.12.16"
resolved "https://registry.yarnpkg.com/@babel/cli/-/cli-7.12.16.tgz#bde5bc5118d90e539603abcd37938c5f0fd6c87a"
@ -1101,6 +1110,11 @@
"@types/yargs" "^15.0.0"
chalk "^4.0.0"
"@jsdevtools/ono@^7.1.3":
version "7.1.3"
resolved "https://registry.yarnpkg.com/@jsdevtools/ono/-/ono-7.1.3.tgz#9df03bbd7c696a5c58885c34aa06da41c8543796"
integrity sha512-4JQNk+3mVzK3xh2rqd6RB4J46qUR19azEHBneZyTZM+c456qOrbbM/5xcR8huNCCcbVt7+UmizG6GuUvPvKUYg==
"@nicolo-ribaudo/chokidar-2@2.1.8-no-fsevents":
version "2.1.8-no-fsevents"
resolved "https://registry.yarnpkg.com/@nicolo-ribaudo/chokidar-2/-/chokidar-2-2.1.8-no-fsevents.tgz#da7c3996b8e6e19ebd14d82eaced2313e7769f9b"
@ -1942,6 +1956,11 @@ call-bind@^1.0.0:
function-bind "^1.1.1"
get-intrinsic "^1.0.2"
call-me-maybe@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/call-me-maybe/-/call-me-maybe-1.0.1.tgz#26d208ea89e37b5cbde60250a15f031c16a4d66b"
integrity sha1-JtII6onje1y95gJQoV8DHBak1ms=
callsites@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/callsites/-/callsites-3.1.0.tgz#b3630abd8943432f54b3f0519238e33cd7df2f73"
@ -4005,6 +4024,13 @@ json-parse-even-better-errors@^2.3.0:
resolved "https://registry.yarnpkg.com/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz#7c47805a94319928e05777405dc12e1f7a4ee02d"
integrity sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==
json-schema-ref-parser@^9.0.7:
version "9.0.7"
resolved "https://registry.yarnpkg.com/json-schema-ref-parser/-/json-schema-ref-parser-9.0.7.tgz#c0ccc5aaee34844f0865889b67e0b67d616f7375"
integrity sha512-uxU9Ix+MVszvCTvBucQiIcNEny3oAEFg7EQHSZw2bquCCuqUqEPEczIdv/Uqo1Zv4/wDPZqOI+ulrMk1ncMtjQ==
dependencies:
"@apidevtools/json-schema-ref-parser" "9.0.7"
json-schema-traverse@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz#69f6a87d9513ab8bb8fe63bdb0979c448e684660"