mirror of
https://github.com/postmanlabs/openapi-to-postman.git
synced 2022-11-29 22:05:00 +03:00
added support for multifile
This commit is contained in:
90
index.js
90
index.js
@@ -1,7 +1,17 @@
|
||||
var converter = require('./lib/convert.js'),
|
||||
validate = require('./lib/validate.js'),
|
||||
parse = require('./lib/parse.js'),
|
||||
async = require('async'),
|
||||
loader = require('speccy/lib/loader'),
|
||||
_ = require('lodash'),
|
||||
fs = require('fs');
|
||||
|
||||
// options for speccy loader
|
||||
const loaderOptions = {
|
||||
resolve: true, // Resolve external references
|
||||
jsonSchema: true // Treat $ref like JSON Schema and convert to OpenAPI Schema Objects
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
convert: function(input, options, cb) {
|
||||
if (input.type === 'string' || input.type === 'json') {
|
||||
@@ -19,10 +29,82 @@ module.exports = {
|
||||
return converter.convert(data, options, cb);
|
||||
});
|
||||
}
|
||||
return cb(null, {
|
||||
result: false,
|
||||
reason: 'input type:' + input.type + ' is not valid'
|
||||
});
|
||||
else if (input.type === 'folder') {
|
||||
let filesPathArray = input.data,
|
||||
convertedSpecs = [],
|
||||
rootFiles = parse.getRootFiles(filesPathArray);
|
||||
|
||||
async.each(rootFiles, (rootFile, callback) => {
|
||||
loader
|
||||
// will merge all the files in the folder
|
||||
.loadSpec(rootFile, loaderOptions)
|
||||
.then((spec) => {
|
||||
converter.convert(spec, options, (err, result) => {
|
||||
if (err) {
|
||||
return callback(null, {
|
||||
result: false,
|
||||
reason: err
|
||||
});
|
||||
}
|
||||
// eslint-disable-next-line no-else-return
|
||||
else {
|
||||
convertedSpecs.push(result);
|
||||
return callback(null);
|
||||
}
|
||||
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
return callback(null, {
|
||||
result: false,
|
||||
reason: err
|
||||
});
|
||||
});
|
||||
}, (err) => {
|
||||
if (err) {
|
||||
return cb(null, {
|
||||
result: false,
|
||||
reason: _.toString(err.reason)
|
||||
});
|
||||
}
|
||||
|
||||
var conversionResult = false,
|
||||
convertedCollections = [],
|
||||
reasonForFail;
|
||||
|
||||
_.forEach(convertedSpecs, (convertedSpec) => {
|
||||
if (convertedSpec.result) {
|
||||
conversionResult = convertedSpec.result;
|
||||
convertedCollections.push(convertedSpec.output[0]);
|
||||
}
|
||||
else {
|
||||
conversionResult = convertedSpec.result;
|
||||
reasonForFail = convertedSpec.reason;
|
||||
}
|
||||
});
|
||||
|
||||
if (conversionResult) {
|
||||
return cb(null, {
|
||||
result: true,
|
||||
output: convertedCollections
|
||||
});
|
||||
}
|
||||
// eslint-disable-next-line no-else-return
|
||||
else {
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user