mirror of
https://github.com/postmanlabs/openapi-to-postman.git
synced 2022-11-29 22:05:00 +03:00
removed speccy and using oas-resolver
This commit is contained in:
3
index.js
3
index.js
@@ -2,7 +2,6 @@ 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');
|
||||
|
||||
@@ -35,7 +34,7 @@ module.exports = {
|
||||
rootFiles = parse.getRootFiles(filesPathArray);
|
||||
|
||||
async.eachSeries(rootFiles, (rootFile, callback) => {
|
||||
loader
|
||||
parse
|
||||
// will merge all the files in the folder
|
||||
.loadSpec(rootFile, loaderOptions)
|
||||
.then((spec) => {
|
||||
|
||||
81
lib/parse.js
81
lib/parse.js
@@ -1,9 +1,12 @@
|
||||
var yaml = require('js-yaml'),
|
||||
fs = require('fs');
|
||||
fetch = require('node-fetch'),
|
||||
fs = require('fs'),
|
||||
resolver = require('oas-resolver'),
|
||||
yamlParse = require('yaml');
|
||||
|
||||
module.exports = {
|
||||
|
||||
asJson: function(spec) {
|
||||
asJson: function (spec) {
|
||||
try {
|
||||
return JSON.parse(spec);
|
||||
}
|
||||
@@ -12,7 +15,7 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
asYaml: function(spec) {
|
||||
asYaml: function (spec) {
|
||||
try {
|
||||
return yaml.safeLoad(spec);
|
||||
}
|
||||
@@ -21,7 +24,7 @@ module.exports = {
|
||||
}
|
||||
},
|
||||
|
||||
validateSpec: function(spec) {
|
||||
validateSpec: function (spec) {
|
||||
|
||||
// Checking for the all the required properties in the specification
|
||||
if (!spec.hasOwnProperty('openapi')) {
|
||||
@@ -65,7 +68,7 @@ module.exports = {
|
||||
};
|
||||
},
|
||||
|
||||
validateRoot: function(spec) {
|
||||
validateRoot: function (spec) {
|
||||
|
||||
// Checking for the all the required properties needed in a root file
|
||||
if (!spec.hasOwnProperty('openapi')) {
|
||||
@@ -97,7 +100,7 @@ module.exports = {
|
||||
};
|
||||
},
|
||||
|
||||
getOasObject: function(file) {
|
||||
getOasObject: function (file) {
|
||||
let oasObj = file;
|
||||
|
||||
if (typeof file === 'string') {
|
||||
@@ -123,7 +126,7 @@ module.exports = {
|
||||
return oasObj;
|
||||
},
|
||||
|
||||
getRootFiles: function(filesPathArray) {
|
||||
getRootFiles: function (filesPathArray) {
|
||||
let rootFilesArray = [];
|
||||
|
||||
filesPathArray.forEach((filePath) => {
|
||||
@@ -136,5 +139,69 @@ module.exports = {
|
||||
});
|
||||
|
||||
return rootFilesArray;
|
||||
},
|
||||
|
||||
resolveContent: function (openapi, options) {
|
||||
return resolver.resolve(openapi, options.source, {
|
||||
options: Object.assign({}, options),
|
||||
resolve: true,
|
||||
cache: [],
|
||||
externals: [],
|
||||
externalRefs: {},
|
||||
rewriteRefs: true,
|
||||
openapi: openapi
|
||||
});
|
||||
},
|
||||
|
||||
loadSpec: function(source, options = {}) {
|
||||
options.source = source;
|
||||
options.origin = source;
|
||||
return this.readSpecFile(source)
|
||||
.then((content) => {
|
||||
try {
|
||||
return yamlParse.parse(content, { prettyErrors: true });
|
||||
}
|
||||
catch (err) {
|
||||
throw new ReadError('\nLine: ' + err.linePos.start.line + ', col: ' +
|
||||
err.linePos.start.col + ' ' + err.message);
|
||||
}
|
||||
}, (err) => {
|
||||
throw new OpenError(err.message);
|
||||
})
|
||||
.then((unresolved) => {
|
||||
if (options.resolve === true) {
|
||||
return this.resolveContent(unresolved, options);
|
||||
}
|
||||
}, (err) => {
|
||||
throw err;
|
||||
})
|
||||
.then((result) => {
|
||||
return result.openapi;
|
||||
}, (err) => {
|
||||
throw err;
|
||||
});
|
||||
},
|
||||
|
||||
readSpecFile: function (file) {
|
||||
if (file && file.startsWith('http')) {
|
||||
// remote file
|
||||
return fetch(file).then((res) => {
|
||||
if (res.status !== 200) {
|
||||
return `Received status code ${res.status}`;
|
||||
}
|
||||
return res.text();
|
||||
});
|
||||
}
|
||||
return this.readFileAsync(file, 'utf8');
|
||||
|
||||
},
|
||||
|
||||
readFileAsync: function(filename, encoding) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile(filename, encoding, (err, data) => {
|
||||
if (err) { reject(err); }
|
||||
else { resolve(data); }
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
1227
package-lock.json
generated
1227
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -120,8 +120,8 @@
|
||||
"commander": "2.3.0",
|
||||
"js-yaml": "3.13.1",
|
||||
"lodash": "4.17.13",
|
||||
"postman-collection": "3.5.1",
|
||||
"speccy": "0.11.0"
|
||||
"oas-resolver": "2.2.5",
|
||||
"postman-collection": "3.5.1"
|
||||
},
|
||||
"author": "Postman Labs <help@getpostman.com>",
|
||||
"license": "Apache-2.0",
|
||||
|
||||
Reference in New Issue
Block a user