Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
process.on('message', (data) => {
const {model, type} = data;
amf.Core.init()
.then(() => modelToDoc(model))
.then((doc) => generateEditingResolvedModel(doc, type))
.then((result) => {
process.send({api: result});
})
.catch((cause) => {
let m = `AMF parser: Unable to resolve AMF ld+json model.\n`;
if (cause.message) {
m += cause.message;
} else if (cause.toString) {
m += cause.toString();
}
process.send({error: m});
});
});
run() {
let msg = 'Generating API model from ' + this.apiFile;
msg += ' using ' + this.apiType + ' parser';
this.logger.info(msg);
amf.plugins.document.WebApi.register();
amf.plugins.document.Vocabularies.register();
amf.plugins.features.AMFValidation.register();
return amf.Core.init()
.then(() => this.gaUsage())
.then(() => this._parse(this.apiFile, this.apiType))
.then((doc) => this._validate(doc, this.apiType))
.then((doc) => this._resolve(doc, this.apiType))
.then((doc) => this._save(doc, this.output));
}
/**
run() {
let msg = 'Generating API model from ' + this.apiFile;
msg += ' using ' + this.apiType + ' parser';
this.logger.info(msg);
amf.plugins.document.WebApi.register();
amf.plugins.document.Vocabularies.register();
amf.plugins.features.AMFValidation.register();
return amf.Core.init()
.then(() => this.gaUsage())
.then(() => this._parse(this.apiFile, this.apiType))
.then((doc) => this._validate(doc, this.apiType))
.then((doc) => this._resolve(doc, this.apiType))
.then((doc) => this._save(doc, this.output));
}
/**
function generateEditingResolvedModel(doc, type) {
const resolver = amf.Core.resolver(type);
doc = resolver.resolve(doc, 'editing');
const generator = amf.Core.generator('AMF Graph', 'application/ld+json');
const opts = amf.render.RenderOptions().withSourceMaps.withCompactUris;
return generator.generateString(doc, opts);
}
_resolve(doc, type) {
this.logger.info('Resolving API model for API components...');
const resolver = amf.Core.resolver(type);
return resolver.resolve(doc, 'editing');
}
/**
const amf = require('amf-client-js');
amf.plugins.document.WebApi.register();
amf.plugins.document.Vocabularies.register();
amf.plugins.features.AMFValidation.register();
/**
* Parses AMF ld+json model to AMF document.
* The model has to be unresolved.
*
* @param {String} model
* @return {Promise} AMF document.
*/
function modelToDoc(model) {
const parser = amf.Core.parser('AMF Graph', 'application/ld+json');
return parser.parseStringAsync(model);
}
/**
* Generates resolved AMF model using editing pipeline (required by API console).
* @param {Object} doc Parsed API document to AMF object.
const amf = require('amf-client-js');
amf.plugins.document.WebApi.register();
amf.plugins.document.Vocabularies.register();
amf.plugins.features.AMFValidation.register();
/**
* Parses AMF ld+json model to AMF document.
* The model has to be unresolved.
*
* @param {String} model
* @return {Promise} AMF document.
*/
function modelToDoc(model) {
const parser = amf.Core.parser('AMF Graph', 'application/ld+json');
return parser.parseStringAsync(model);
}
/**
* Generates resolved AMF model using editing pipeline (required by API console).
* @param {Object} doc Parsed API document to AMF object.
* @param {String} type API original type (RAML 1.0, OAS 2.0, etc)
* @return {Promise} A promise resolved to AMF object.
function generateEditingResolvedModel(doc, type) {
const resolver = amf.Core.resolver(type);
doc = resolver.resolve(doc, 'editing');
const generator = amf.Core.generator('AMF Graph', 'application/ld+json');
const opts = amf.render.RenderOptions().withSourceMaps.withCompactUris;
return generator.generateString(doc, opts);
}
_save(doc, file) {
this.logger.info('Generating json-ld model...');
const opts = amf.render.RenderOptions().withSourceMaps.withCompactUris;
const generator = amf.Core.generator('AMF Graph', 'application/ld+json');
const start = Date.now();
return generator.generateString(doc, opts)
.then((data) => {
const time = Date.now() - start;
this.logger.info(`Model ready in ${time} milliseconds`);
this.logger.info('Storing API data model to file: ' + file);
const dir = path.dirname(file);
return fs.ensureDir(dir)
.then(() => fs.writeFile(file, data, 'utf8'));
});
}
_validate(doc, type) {
this.logger.info('API parsed.');
this.logger.info('Validating API...');
let validateProfile;
switch (type) {
case 'RAML 1.0': validateProfile = amf.ProfileNames.RAML; break;
case 'RAML 0.8': validateProfile = amf.ProfileNames.RAML08; break;
case 'OAS 2.0':
case 'OAS 3.0':
validateProfile = amf.ProfileNames.OAS;
break;
}
return amf.AMF.validate(doc, validateProfile)
.then((report) => {
if (!report.conforms) {
this.logger.warn(report.toString());
} else {
this.logger.info('API valid.');
}
return doc;
});
}
/**