Files
fastapi-openapi-to-postman/index.js
2020-01-07 15:26:12 +05:30

110 lines
2.8 KiB
JavaScript

'use strict';
const SchemaPack = require('./lib/schemapack.js').SchemaPack,
async = require('async'),
parse = require('./lib/parse.js'),
// options for oas-resolver
OasResolverOptions = {
resolve: true, // Resolve external references
jsonSchema: true // Treat $ref like JSON Schema and convert to OpenAPI Schema Objects
};
module.exports = {
// Old API wrapping the new API
convert: function(input, options, cb) {
var schema = new SchemaPack(input, options);
if (schema.validated) {
return schema.convert(cb);
}
else if (input.type === 'folder') {
let filesPathArray = input.data,
convertedSpecs = [],
rootFiles = parse.getRootFiles(filesPathArray);
async.eachSeries(rootFiles, (rootFile, callback) => {
parse
// will merge all the files in the folder
.mergeFiles(rootFile, OasResolverOptions)
.then((spec) => {
converter.convert(spec, options, (err, result) => {
if (err) {
return callback(err);
}
convertedSpecs.push(result);
return callback(null);
});
})
.catch((err) => {
return callback(err);
});
}, (err) => {
if (err) {
return cb({
result: false,
reason: 'input type:' + input.type + ' is not valid'
});
}
var conversionResult = false,
convertedCollections = [],
reasonForFail;
_.forEach(convertedSpecs, (convertedSpec) => {
if (convertedSpec.result) {
let result;
conversionResult = conversionResult || convertedSpec.result;
function returnCollection (resultSpec) {
if (resultSpec.type === 'collection') {
return true;
}
}
// filtering out the collections from the convertedSpec
result = convertedSpec.output.filter(returnCollection);
convertedCollections.push(result[0]);
}
else {
conversionResult = conversionResult || convertedSpec.result;
reasonForFail = convertedSpec.reason;
}
});
if (conversionResult) {
return cb(null, {
result: true,
output: convertedCollections
});
}
return cb(null, {
result: false,
reason: reasonForFail
});
});
}
// eslint-disable-next-line no-else-return
else {
return cb(null, {
result: false,
reason: 'input type:' + input.type + ' is not valid'
});
}
},
validate: function(input) {
var schema = new SchemaPack(input);
return schema.validationResult;
},
getOptions: function() {
return SchemaPack.getOptions();
},
// new API
SchemaPack
};