-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
873 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
}); |
Oops, something went wrong.