- Removed yaml loader in favor of refparser

This commit is contained in:
Ferdi Koomen 2021-10-11 14:23:12 +02:00
parent b3a08a7c69
commit 92ad2ae772
3 changed files with 1 additions and 55 deletions

View File

@ -66,7 +66,6 @@
"commander": "^8.0.0",
"form-data": "^4.0.0",
"handlebars": "^4.7.6",
"js-yaml": "^4.0.0",
"json-schema-ref-parser": "^9.0.7",
"mkdirp": "^1.0.4",
"node-fetch": "^2.6.5",
@ -82,7 +81,6 @@
"@types/express": "4.17.13",
"@types/glob": "7.1.4",
"@types/jest": "27.0.2",
"@types/js-yaml": "4.0.3",
"@types/node": "16.10.3",
"@types/qs": "6.9.7",
"@typescript-eslint/eslint-plugin": "4.33.0",

View File

@ -1,27 +0,0 @@
import { exists, readFile } from './fileSystem';
import { getOpenApiSpec } from './getOpenApiSpec';
jest.mock('./fileSystem');
const existsMocked = exists as jest.MockedFunction<typeof exists>;
const readFileMocked = readFile as jest.MockedFunction<typeof readFile>;
function mockPromise<T>(value: T): Promise<T> {
return new Promise<T>(resolve => resolve(value));
}
describe('getOpenApiSpec', () => {
it('should read the json file', async () => {
existsMocked.mockReturnValue(mockPromise(true));
readFileMocked.mockReturnValue(mockPromise('{"message": "Hello World!"}'));
const spec = await getOpenApiSpec('spec.json');
expect(spec.message).toEqual('Hello World!');
});
it('should read the yaml file', async () => {
existsMocked.mockReturnValue(mockPromise(true));
readFileMocked.mockReturnValue(mockPromise('message: "Hello World!"'));
const spec = await getOpenApiSpec('spec.yaml');
expect(spec.message).toEqual('Hello World!');
});
});

View File

@ -1,8 +1,4 @@
import { load } from 'js-yaml';
import RefParser from 'json-schema-ref-parser';
import { extname } from 'path';
import { readSpec } from './readSpec';
/**
* Load and parse te open api spec. If the file extension is ".yml" or ".yaml"
@ -11,26 +7,5 @@ import { readSpec } from './readSpec';
* @param input
*/
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 {
rootObject = load(content);
} catch (e) {
throw new Error(`Could not parse OpenApi YAML: "${input}"`);
}
break;
default:
try {
rootObject = JSON.parse(content);
} catch (e) {
throw new Error(`Could not parse OpenApi JSON: "${input}"`);
}
break;
}
return await RefParser.bundle(rootObject);
return await RefParser.bundle(input);
}