From 5deb2c9e0fb753a895d1c43a3446b6f2e5e68c7b Mon Sep 17 00:00:00 2001 From: Dhroov7 Date: Mon, 9 Dec 2019 18:48:40 +0530 Subject: [PATCH] added check for https, changed behaviour of callback back to err throwing in convert function --- index.js | 26 +++++++++++++------------- lib/convert.js | 5 +---- lib/parse.js | 31 ++++++++++++++----------------- 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/index.js b/index.js index b8e4f3d..f373ef8 100644 --- a/index.js +++ b/index.js @@ -40,17 +40,10 @@ module.exports = { .then((spec) => { converter.convert(spec, options, (err, result) => { if (err) { - return callback(null, { - result: false, - reason: err - }); + return callback(err); } - // eslint-disable-next-line no-else-return - else { - convertedSpecs.push(result); - return callback(null); - } - + convertedSpecs.push(result); + return callback(null); }); }) .catch((err) => { @@ -59,7 +52,14 @@ module.exports = { reason: err }); }); - }, () => { + }, (err) => { + + if (err) { + return cb({ + result: false, + reason: 'input type:' + input.type + ' is not valid' + }); + } var conversionResult = false, convertedCollections = [], @@ -73,6 +73,7 @@ module.exports = { else { conversionResult = convertedSpec.result; reasonForFail = convertedSpec.reason; + return false; // it will break out from the loop } }); @@ -116,8 +117,7 @@ module.exports = { else if (input.type === 'folder') { if (!_.isEmpty(parse.getRootFiles(input.data))) { return { - result: true, - reason: 'valid input type' + result: true }; } } diff --git a/lib/convert.js b/lib/convert.js index d0f7705..21e08d3 100644 --- a/lib/convert.js +++ b/lib/convert.js @@ -273,10 +273,7 @@ module.exports = { } else { // Unhandled exception, rethrow - callback(null, { - result: false, - reason: e.message - }); + callback(e); } } }, diff --git a/lib/parse.js b/lib/parse.js index d804b45..87d57c8 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -102,25 +102,22 @@ module.exports = { getOasObject: function (file) { let oasObj = file; - - if (typeof oasObj === 'string') { + try { + oasObj = JSON.parse(oasObj); + } + catch (jsonEx) { + // Not direct JSON. Could be YAML try { - oasObj = JSON.parse(oasObj); + oasObj = yaml.safeLoad(oasObj); } - catch (jsonEx) { - // Not direct JSON. Could be YAML - try { - oasObj = yaml.safeLoad(oasObj); - } - catch (yamlEx) { - // Not JSON or YAML - return { - result: false, - reason: 'The input must be valid JSON or YAML' - }; - } - // valid YAML + catch (yamlEx) { + // Not JSON or YAML + return { + result: false, + reason: 'The input must be valid JSON or YAML' + }; } + // valid YAML } return oasObj; @@ -187,7 +184,7 @@ module.exports = { }, readSpecFile: function (file) { - if (file && file.startsWith('http')) { + if (file.startsWith('http') || file.startsWith('https')) { // remote file return fetch(file).then((res) => { if (res.status !== 200) {