Skip to content

Commit

Permalink
chore: started building parting API
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrodek committed Aug 30, 2019
1 parent 8d5d1e9 commit 28e0acf
Show file tree
Hide file tree
Showing 11 changed files with 873 additions and 21 deletions.
15 changes: 13 additions & 2 deletions demo/demo-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class DemoBase {
]);

this._selectApi = this._selectApi.bind(this);
this._processApiFile = this._processApiFile.bind(this);
this._processApiFileUpload = this._processApiFileUpload.bind(this);
}

get loading() {
Expand Down Expand Up @@ -156,13 +156,24 @@ export class DemoBase {
body: value
});
const data = await response.json();
console.log(data);
if (response.status === 200) {
this.model = JSON.parse(data.data.api);
} else if (response.status === 300) {
this._requestSelectApi(data.data);
} else {
this.notifyError(data.message);
}
} catch (e) {
this.notifyError(e.message);
}
this.loading = false;
}

_requestSelectApi(data) {
const { key, candidates } = data;
console.log(key, candidates);
}

firstRendered() {}

menuSelectorTemplate() {
Expand Down
41 changes: 34 additions & 7 deletions demo/demo-server/api.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
import { ApiParser } from './api/api-parser.js';
const parser = new ApiParser();

function wrapError(cause) {
return {
error: true,
code: 500,
message: cause.message,
detail: 'The server misbehave. That is all we know.'
};
}

async function apiRequest(ctx) {
if (ctx.request.url === '/api/parse/api') {
ctx.body = 'PARSING SERVICE';
} else {
ctx.response.status = 404;
ctx.body = 'unknown';
let body;
try {
if (ctx.request.url === '/api/parse/api') {
body = await parser.parseFile(ctx.req);
} else if (ctx.request.url.indexOf('/api/parse/candidate') === 0) {
body = await parser.parseCandidate(ctx.req);
} else if (ctx.request.url.indexOf('/api/parse/result/') === 0) {
const id = ctx.request.url.substr(18);
body = await parser.checkStatus(id);
} else {
ctx.response.status = 404;
body = 'unknown';
}
} catch (e) {
body = wrapError(e);
}
/* eslint-disable require-atomic-updates */
if (body.code) {
ctx.status = body.code;
}
// console.log('response', body);
ctx.body = body;
}

export async function demoApi(ctx, next) {
if (ctx.request.url.indexOf('/api') === 0) {
if (ctx.request.url.indexOf('/api/') === 0) {
await apiRequest(ctx);
} else {
await next();
return next();
}
}
67 changes: 67 additions & 0 deletions demo/demo-server/api/amf-service/amf-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const amf = require('amf-client-js');
// import amf from 'amf-client-js';
amf.plugins.document.WebApi.register();
amf.plugins.document.Vocabularies.register();
amf.plugins.features.AMFValidation.register();
let initied = false;

async function validateDoc(type, doc) {
let validateProfile;
switch (type) {
case 'RAML 1.0': validateProfile = amf.ProfileNames.RAML; break;
case 'RAML 0.8': validateProfile = amf.ProfileNames.RAML08; break;
case 'OAS 1.0':
case 'OAS 2.0':
case 'OAS 3.0':
validateProfile = amf.ProfileNames.OAS;
break;
}
const result = await amf.AMF.validate(doc, validateProfile);
process.send({ validation: result.toString() });
}
/**
* AMF parser to be called in a child process.
*
* AMF can in extreme cases takes forever to parse API data if, for example,
* RAML type us defined as a number of union types. It may sometimes cause
* the process to crash. To protect the renderer proces this is run as forked
* process.
*
* @param {Object} data
*/
async function processData(data) {
const sourceFile = data.source;
const type = data.from.type;
const contentType = data.from.contentType;
const validate = data.validate;
if (!initied) {
await amf.Core.init();
}
/* eslint-disable-next-line require-atomic-updates */
initied = true;
const file = `file://${sourceFile}`;
const parser = amf.Core.parser(type, contentType);
let doc = await parser.parseFileAsync(file);
if (validate) {
await validateDoc(type, doc);
}

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 await generator.generateString(doc, opts);
}

process.on('message', async (data) => {
try {
const api = await processData(data);
process.send({
api
});
} catch (cause) {
let m = `AMF parser: Unable to parse API ${data.source}.\n`;
m += cause.s$1 || cause.message;
process.send({ error: m });
}
});
Loading

0 comments on commit 28e0acf

Please sign in to comment.