apiconnect-project
Version:
Configuration module IBM API Connect Developer Toolkit
107 lines (88 loc) • 3.13 kB
JavaScript
/********************************************************* {COPYRIGHT-TOP} ***
* Licensed Materials - Property of IBM
* 5725-Z22, 5725-Z63, 5725-U33, 5725-Z63
*
* (C) Copyright IBM Corporation 2016, 2018
*
* All Rights Reserved.
* US Government Users Restricted Rights - Use, duplication or disclosure
* restricted by GSA ADP Schedule Contract with IBM Corp.
********************************************************** {COPYRIGHT-END} **/
// Node module: apiconnect-project
;
var Promise = require('bluebird');
var _ = require('lodash');
var JsonRefs = require('json-refs');
var jsYaml = require('js-yaml');
function derefArtifacts(artifacts) {
if (!Array.isArray(artifacts)) {
artifacts = [ artifacts ];
}
return Promise.map(artifacts, processArtifact);
};
function processArtifact(artifact) {
if (artifact.type === 'swagger') {
return processSwagger(artifact);
} else if (artifact.type === 'product') {
return Promise.map(artifact.refs, processSwagger)
.then(function(processed) {
artifact.refs = processed;
return artifact;
});
} else {
return Promise.resolve(artifact);
}
};
function processSwagger(swaggerArtifact) {
// If there was already an error attached to this artifact, don't try to expand. It's possible that this artifact
// doesn't exist
if (swaggerArtifact.err) {
return Promise.resolve(swaggerArtifact);
}
return expandExternalRefs(swaggerArtifact.data, swaggerArtifact.filePath).then(function(res) {
swaggerArtifact.data = res.resolved;
swaggerArtifact.errors = processErrors(res);
// It is possible that we expanded name/version from a ref. Set the value from the fully expanded object
// into the returned artifact
if (swaggerArtifact.data && swaggerArtifact.data.info) {
swaggerArtifact.version = swaggerArtifact.data.info.version || swaggerArtifact.version;
swaggerArtifact.name = swaggerArtifact.data.info['x-ibm-name'] || swaggerArtifact.name;
}
return swaggerArtifact;
}).catch(function(err) {
if (swaggerArtifact.errors == null) {
swaggerArtifact.errors = [];
}
swaggerArtifact.errors.push(err.message);
return swaggerArtifact;
});
};
function expandExternalRefs(obj, objFileName, state) {
var jsonRefOptions = {};
// Include invalid reference information
jsonRefOptions.includeInvalid = true;
// Resolve only relative/remote references
jsonRefOptions.filter = [ 'relative', 'remote' ];
jsonRefOptions.location = objFileName;
// Resolve circular references
jsonRefOptions.resolveCirculars = true;
// Update the json-refs options to process YAML
jsonRefOptions.loaderOptions = {};
jsonRefOptions.loaderOptions.processContent = function(res, cb) {
cb(undefined, jsYaml.safeLoad(res.text));
};
return JsonRefs.resolveRefs(obj, jsonRefOptions);
};
function processErrors(res) {
var errors = [];
if (!res.refs) {
return errors;
}
_.mapValues(res.refs, function(ref) {
if (ref.error != null) {
errors.push(ref.error);
}
});
return errors;
}
module.exports = derefArtifacts;