mirror of
https://github.com/postmanlabs/openapi-to-postman.git
synced 2022-11-29 22:05:00 +03:00
Removed lint errors
This commit is contained in:
228
lib/convert.js
228
lib/convert.js
@@ -1,145 +1,115 @@
|
||||
var sdk = require('postman-collection'),
|
||||
fs = require('fs'),
|
||||
jsf = require('json-schema-faker'),
|
||||
util = require('./util.js'),
|
||||
_ = require('lodash'),
|
||||
$RefParser = require('json-schema-ref-parser');
|
||||
async = require('async');
|
||||
util = require('./util.js'),
|
||||
_ = require('lodash'),
|
||||
$RefParser = require('json-schema-ref-parser'),
|
||||
converter = {
|
||||
|
||||
// async function resolveOpenApiSpec(openapi){
|
||||
// var resolvedSchema = await $RefParser.dereference(openapi);
|
||||
// return resolvedSchema;
|
||||
// }
|
||||
staticFolder: {},
|
||||
POSTMAN: {},
|
||||
|
||||
var converter = {
|
||||
generateCollection: function(status) {
|
||||
var folderTree = status.tree,
|
||||
openapi = status.spec,
|
||||
child;
|
||||
|
||||
staticFolder: {},
|
||||
POSTMAN: {},
|
||||
for (child in folderTree.root.children) {
|
||||
if (folderTree.root.children.hasOwnProperty(child)) {
|
||||
this.POSTMAN.collection.items.add(
|
||||
util.convertChildToItemGroup(openapi, folderTree.root.children[child])
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
generateCollection: function(status){
|
||||
var folderTree = status.tree,
|
||||
openapi = status.spec;
|
||||
resolveOpenApiSpec: async function(openapi) {
|
||||
var resolvedSchema = await $RefParser.dereference(openapi);
|
||||
return resolvedSchema;
|
||||
},
|
||||
|
||||
for(var child in folderTree.root.children){
|
||||
this.POSTMAN.collection.items.add(
|
||||
util.convertChildToItemGroup(openapi, folderTree.root.children[child])
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
resolveOpenApiSpec: async function(openapi){
|
||||
var resolvedSchema = await $RefParser.dereference(openapi);
|
||||
return resolvedSchema;
|
||||
},
|
||||
|
||||
convert: function (data, callback){
|
||||
convert: function (data, callback) {
|
||||
// console.log("tuhin",data);
|
||||
var validation = util.parseSpec(data),
|
||||
var validation = util.parseSpec(data),
|
||||
openapi = {},
|
||||
spec,
|
||||
mycollection,
|
||||
description,
|
||||
contact,
|
||||
folderTree,
|
||||
noVarUrl,
|
||||
deRefObj = {},
|
||||
baseUri;
|
||||
contact;
|
||||
|
||||
if(!validation.result){
|
||||
callback(validation);
|
||||
}
|
||||
|
||||
//TODO - Have to handle global level security scheme
|
||||
|
||||
// changing the openapi spec for ease of access
|
||||
openapi = validation.openapi;
|
||||
openapi.securityDefs = openapi.components.securitySchemes || {};
|
||||
openapi.baseUrl = openapi.servers[0].url;
|
||||
openapi.baseUrlVariables = openapi.servers[0].variables;
|
||||
|
||||
// handling path templating in request url if any
|
||||
openapi.baseUrl = openapi.baseUrl.replace(/{/g, ':').replace(/}/g, '');
|
||||
|
||||
|
||||
// creating a new instance of the collection
|
||||
this.POSTMAN.collection = new sdk.Collection({
|
||||
info: {
|
||||
name: openapi.info.title,
|
||||
version: openapi.info.version,
|
||||
},
|
||||
});
|
||||
|
||||
// adding the collection variables for all the necessary root level variables
|
||||
|
||||
this.POSTMAN.collection = util.addCollectionVariables(
|
||||
this.POSTMAN.collection,
|
||||
openapi.baseUrlVariables,
|
||||
'base-url',
|
||||
openapi.baseUrl
|
||||
);
|
||||
|
||||
// if(openapi.baseUrlVariables){
|
||||
// _.forOwn(openapi.baseUrlVariables, (value, key) => {
|
||||
// this.POSTMAN.collection.variables.add(new sdk.Variable({
|
||||
// id: key,
|
||||
// value: value.default || '',
|
||||
// description: value.description + (value.enum || ''),
|
||||
// }));
|
||||
// });
|
||||
// } else {
|
||||
// this.POSTMAN.collection.variables.add(new sdk.Variable({
|
||||
// id: 'base-url',
|
||||
// value: openapi.baseUrl,
|
||||
// type: 'string',
|
||||
// description: openapi.servers[0].description,
|
||||
// }));
|
||||
// }
|
||||
|
||||
// adding the collection description
|
||||
description = openapi.info.description;
|
||||
|
||||
if(openapi.info.contact){
|
||||
contact = 'Name : ' + openapi.info.contact.name + '\n' + 'Email : ' + openapi.info.contact.email +'\n';
|
||||
description += '\nContact Support + \n{' + '\n' + contact + '\n}'
|
||||
}
|
||||
|
||||
this.POSTMAN.collection.describe(description);
|
||||
|
||||
// the main callback exists in an asynchronous context,
|
||||
this.resolveOpenApiSpec(openapi).then((fromResolve) => {
|
||||
let status = {};
|
||||
let folderObj = util.generateTrieFromPaths(fromResolve);
|
||||
|
||||
status = {
|
||||
spec: fromResolve,
|
||||
tree: folderObj.tree,
|
||||
if (!validation.result) {
|
||||
callback(validation);
|
||||
}
|
||||
// adding path level variables which would be used
|
||||
if(folderObj.variables){
|
||||
_.forEach(folderObj.variables, (collectionVariable) => {
|
||||
this.POSTMAN.collection = util.addCollectionVariables(
|
||||
this.POSTMAN.collection,
|
||||
collectionVariable,
|
||||
'path-level-uri'
|
||||
);
|
||||
|
||||
// TODO - Have to handle global level security scheme
|
||||
|
||||
// changing the openapi spec for ease of access
|
||||
openapi = validation.openapi;
|
||||
openapi.securityDefs = openapi.components.securitySchemes || {};
|
||||
openapi.baseUrl = openapi.servers[0].url;
|
||||
openapi.baseUrlVariables = openapi.servers[0].variables;
|
||||
|
||||
// handling path templating in request url if any
|
||||
openapi.baseUrl = openapi.baseUrl.replace(/{/g, ':').replace(/}/g, '');
|
||||
|
||||
|
||||
// creating a new instance of the collection
|
||||
this.POSTMAN.collection = new sdk.Collection({
|
||||
info: {
|
||||
name: openapi.info.title,
|
||||
version: openapi.info.version
|
||||
}
|
||||
});
|
||||
|
||||
// adding the collection variables for all the necessary root level variables
|
||||
|
||||
this.POSTMAN.collection = util.addCollectionVariables(
|
||||
this.POSTMAN.collection,
|
||||
openapi.baseUrlVariables,
|
||||
'base-url',
|
||||
openapi.baseUrl
|
||||
);
|
||||
// adding the collection description
|
||||
description = openapi.info.description;
|
||||
|
||||
if (openapi.info.contact) {
|
||||
contact = 'Name : ' + openapi.info.contact.name + '\nEmail : ' + openapi.info.contact.email + '\n';
|
||||
description += '\nContact Support + \n{\n' + contact + '\n}';
|
||||
}
|
||||
|
||||
this.POSTMAN.collection.describe(description);
|
||||
|
||||
// the main callback exists in an asynchronous context,
|
||||
this.resolveOpenApiSpec(openapi).then((fromResolve) => {
|
||||
let status = {};
|
||||
folderObj = util.generateTrieFromPaths(fromResolve);
|
||||
|
||||
status = {
|
||||
spec: fromResolve,
|
||||
tree: folderObj.tree
|
||||
};
|
||||
// adding path level variables which would be used
|
||||
if (folderObj.variables) {
|
||||
_.forEach(folderObj.variables, (collectionVariable) => {
|
||||
this.POSTMAN.collection = util.addCollectionVariables(
|
||||
this.POSTMAN.collection,
|
||||
collectionVariable,
|
||||
'path-level-uri'
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
this.generateCollection(status);
|
||||
callback({
|
||||
result: true,
|
||||
collection: this.POSTMAN.collection
|
||||
});
|
||||
}).catch((err) => {
|
||||
callback({
|
||||
result: false,
|
||||
reason: err
|
||||
});
|
||||
}
|
||||
|
||||
this.generateCollection(status);
|
||||
callback({
|
||||
result: true,
|
||||
collection: this.POSTMAN.collection,
|
||||
});
|
||||
}).catch((err) => {
|
||||
callback({
|
||||
result: false,
|
||||
reason: err,
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// module.exports = converter;
|
||||
module.exports = function(json, callback){
|
||||
module.exports = function(json, callback) {
|
||||
converter.convert(json, callback);
|
||||
}
|
||||
};
|
||||
|
||||
69
lib/parse.js
69
lib/parse.js
@@ -1,70 +1,67 @@
|
||||
var yaml = require('js-yaml');
|
||||
|
||||
module.exports = {
|
||||
|
||||
asJson: function(spec){
|
||||
try{
|
||||
|
||||
asJson: function(spec) {
|
||||
try {
|
||||
return JSON.parse(spec);
|
||||
}
|
||||
catch (jsonException){
|
||||
catch (jsonException) {
|
||||
throw new SyntaxError(`Specification is not a valid JSON. ${jsonException}`);
|
||||
}
|
||||
},
|
||||
|
||||
asYaml: function(spec){
|
||||
try{
|
||||
asYaml: function(spec) {
|
||||
try {
|
||||
return yaml.safeLoad(spec);
|
||||
}
|
||||
catch(yamlException){
|
||||
catch (yamlException) {
|
||||
throw new SyntaxError(`Specification is not a valid YAML. ${yamlException}`);
|
||||
}
|
||||
},
|
||||
|
||||
validateRoot: function(spec){
|
||||
validateRoot: function(spec) {
|
||||
|
||||
// Checking for the all the required properties in the specificatio
|
||||
if(!spec.hasOwnProperty('openapi')){
|
||||
if (!spec.hasOwnProperty('openapi')) {
|
||||
return {
|
||||
result: false,
|
||||
reason: 'Specification must contain a semantic version number of the OAS specification',
|
||||
}
|
||||
reason: 'Specification must contain a semantic version number of the OAS specification'
|
||||
};
|
||||
}
|
||||
|
||||
if(!spec.paths){
|
||||
if (!spec.paths) {
|
||||
return {
|
||||
result: false,
|
||||
reason: 'Specification must contain Paths Object for the available operational paths',
|
||||
}
|
||||
reason: 'Specification must contain Paths Object for the available operational paths'
|
||||
};
|
||||
}
|
||||
|
||||
if(!spec.hasOwnProperty('info')){
|
||||
if (!spec.hasOwnProperty('info')) {
|
||||
return {
|
||||
result: false,
|
||||
reason: 'Specification must contain an Info Object for the meta-data of the API',
|
||||
}
|
||||
} else {
|
||||
if(!spec.info.hasOwnProperty('title')){
|
||||
return {
|
||||
result: false,
|
||||
reason: 'Specification must contain a title in order to generate a collection'
|
||||
}
|
||||
}
|
||||
|
||||
if(!spec.info.hasOwnProperty('version')){
|
||||
return {
|
||||
result: false,
|
||||
reason: 'Specification must contain a semantic version number of the API in the Info Object'
|
||||
}
|
||||
}
|
||||
reason: 'Specification must contain an Info Object for the meta-data of the API'
|
||||
};
|
||||
}
|
||||
if (!spec.info.hasOwnProperty('title')) {
|
||||
return {
|
||||
result: false,
|
||||
reason: 'Specification must contain a title in order to generate a collection'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
if (!spec.info.hasOwnProperty('version')) {
|
||||
return {
|
||||
result: false,
|
||||
reason: 'Specification must contain a semantic version number of the API in the Info Object'
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
// Valid specification
|
||||
return {
|
||||
result: true,
|
||||
openapi: spec,
|
||||
}
|
||||
},
|
||||
}
|
||||
openapi: spec
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
917
lib/util.js
917
lib/util.js
File diff suppressed because it is too large
Load Diff
@@ -1,16 +1,16 @@
|
||||
var util = require('./util.js');
|
||||
|
||||
module.exports = function(jsonOrString){
|
||||
module.exports = function(jsonOrString) {
|
||||
var parseResult = util.parseSpec(jsonOrString);
|
||||
|
||||
if(!parseResult.result){
|
||||
if (!parseResult.result) {
|
||||
return {
|
||||
result: false,
|
||||
reason: parseResult.reason,
|
||||
reason: parseResult.reason
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
result: true,
|
||||
result: true
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user