forked from raml2html/raml2html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raml2html.js
418 lines (342 loc) · 15.9 KB
/
raml2html.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import raml2obj from 'raml2obj';
import handlebars from 'handlebars';
import marked from 'marked';
import pjson from './package.json';
import fs from 'fs';
import path from 'path';
import util from 'util';
const AVAILABLE_METHODS = ['get', 'post', 'put', 'delete', 'patch'];
/**
* Render the source RAML object using the config's processOutput function
*
* The config object should contain at least the following property:
* processRamlObj: function that takes the raw RAML object and returns a promise with the rendered HTML
*
* @param {(String|Object)} source - The source RAML file. Can be a filename, url, contents of the RAML file,
* or an already-parsed RAML object.
* @param {Object} config
* @param {Function} config.processRamlObj
* @returns a promise
*/
function render(source, config) {
config = config || {};
config.raml2HtmlVersion = pjson.version;
return raml2obj.parse(source)
.then(ramlObj => {
ramlObj.config = config;
if (config.processRamlObj) {
return config.processRamlObj(ramlObj).then(html => {
if (config.postProcessHtml) {
return config.postProcessHtml(html);
}
return html;
});
}
return ramlObj;
});
}
function processObj(obj) {
//console.dir(obj);
//console.log(util.inspect(obj, {showHidden: false, depth: null}));
let processResource = (res, url, securitySchemes, traits) => {
let ss = JSON.parse(JSON.stringify(securitySchemes)),
tr = JSON.parse(JSON.stringify(traits));
let keys = Object.keys(res);
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
if (key.substr(0, 1) != '/') {
if (key === 'securedBy') {
ss = res[key];
} else if (key === 'is') {
tr = res[key];
}
continue;
}
let subRes = res[key];
subRes = processResource(subRes, url + key, ss, tr);
subRes.parentUrl = url;
subRes.relativeUri = key;
subRes.methods = {};
for (let m of AVAILABLE_METHODS) {
if (subRes.hasOwnProperty(m)) {
subRes.methods[m] = subRes[m];
delete subRes[m];
}
}
res.resources = res.resources || [];
res.resources.push(subRes);
delete res[key];
}
return res;
};
obj = processResource(obj, '', null, null);
//console.log('--------------------------------------------------------------');
//console.log(obj);
//console.log(util.inspect(obj, {showHidden: false, depth: null}));
return obj;
}
/**
* @param {String} [mainTemplate] - The filename of the main template, leave empty to use default templates
* @param {String} [templatesPath] - Optional, by default it uses the current working directory
* @returns {{processRamlObj: Function, postProcessHtml: Function}}
*/
function getDefaultConfig(mainTemplate, templatesPath) {
if (!mainTemplate) {
mainTemplate = 'main.html';
templatesPath = path.join(__dirname, './templates/');
let dir = fs.readdirSync(templatesPath);
for (let i = 0; i < dir.length; i++) {
let filename = dir[i],
ext = path.extname(filename);
if (ext === '.html') {
handlebars.registerPartial(path.basename(filename, ext), fs.readFileSync(path.join(templatesPath, filename), {encoding: 'utf8'}));
}
}
}
// built-in js functions
handlebars.registerHelper('join', arr => arr.join(','));
handlebars.registerHelper('dot2underscore', str => str.replace(/\./g, '_'));
handlebars.registerHelper('isObject', obj => typeof obj === 'object');
handlebars.registerHelper('returnOr', (one, two) => one || two);
handlebars.registerHelper('returnAnd', (one, two) => one && two);
handlebars.registerHelper('firstEl', arr => arr[0]);
handlebars.registerHelper('firstElNotNull', arr => arr && arr[0] !== 'null');
handlebars.registerHelper('isEq', (one, two) => one == two);
handlebars.registerHelper('isNotEq', (one, two) => one != two);
handlebars.registerHelper('isString', one => typeof one === 'string');
// libraries
let renderer = new marked.Renderer();
// Render Bootstrap style tables
renderer.table = (thead, tbody) => `<table class="table"><thead>${thead}</thead><tbody>${tbody}</tbody></table>`;
handlebars.registerHelper('markdown', md => md ? new handlebars.SafeString(marked(md, {renderer: renderer, pedantic: true, sanitize: true})) : '');
// view logic
handlebars.registerHelper('describeResource', function() {
return this.methods || (this.description && this.parentUrl);
});
handlebars.registerHelper('hasRequest', function() {
return (this.allUriParameters && this.allUriParameters.length) || this.queryParameters || this.headers || this.body;
});
handlebars.registerHelper('typeString', function() {
return this.type == 'string';
});
return {
processRamlObj: ramlObj => {
ramlObj = processObj(ramlObj);
let processType = type => {
//console.log('pr t:', type);
let isObject = typeof type === 'object',
typeName = isObject ? type.type || type.parentType : type,
t = typeName ? typeName.replace(/^([.\w]+)\b(\[\])?$/, '$1') : '',
isArray = typeName && typeName.substr(-2, 2) === '[]',
exists = t && ramlObj.types.hasOwnProperty(t),
uses = false;
if (t.indexOf('.') >= 0) {
uses = t.split('.').shift();
t = t.split('.').pop();
exists = !!(uses && ramlObj.uses && ramlObj.uses[uses] && ramlObj.uses[uses].types && ramlObj.uses[uses].types[t]);
}
let typ;
if (exists) {
if (!uses) {
typ = JSON.parse(JSON.stringify(ramlObj.types[t]));
} else {
typ = JSON.parse(JSON.stringify(ramlObj.uses[uses].types[t]));
}
} else {
typ = JSON.parse(JSON.stringify(type));
}
if (typeof typ != 'object') {
return typ;
}
if (exists) {
typ.parentType = t;
typ.isArray = isArray;
// properties of parent type
if (typ.type) {
//console.log('par:', typ);
let extend = processType(typ.type);
if (extend.hasOwnProperty('properties')) {
let extendKeys = Object.keys(extend.properties);
for (let i = 0; i < extendKeys.length; i++) {
let extendKey = extendKeys[i];
if (!typ.properties[extendKey]) {
typ.properties[extendKey] = extend.properties[extendKey];
}
}
}
}
// properties of base type
if (typ.properties) {
let props = Object.keys(typ.properties);
for (let i = 0; i < props.length; i++) {
let key = props[i],
prop = typ.properties[key];
//console.log('key:', key);
typ.properties[key] = processType(prop);
}
}
}
if (!typ.displayName && !typ.parentType) {
typ.displayName = 'Объект';
}
// inline properties of object
if (type.properties) {
let props = Object.keys(type.properties);
for (let i = 0; i < props.length; i++) {
let key = props[i],
prop = type.properties[key];
//console.log('in key:', key);
typ.properties[key] = processType(prop);
}
}
return typ;
};
handlebars.registerHelper('getType', type => processType(type));
//handlebars.registerHelper('getType', function(type) {
//console.log('getType:', this, type);
//return processType(type);
//});
let processResourceTypePattern = (rtPart, pattern) => {
if (typeof rtPart !== 'object') {
return rtPart;
}
let availablePatternKeys = ['example', 'description'];
let keys = Object.keys(rtPart);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (!rtPart.hasOwnProperty(key)) {
continue;
}
if (availablePatternKeys.indexOf(key) >= 0) {
let patternKeys = Object.keys(pattern);
for (let j = 0; j < patternKeys.length; j++) {
let patternKey = patternKeys[j];
rtPart[key] = rtPart[key].replace(new RegExp(`<<${patternKey}>>`, 'g'), pattern[patternKey]);
}
} else if (typeof rtPart[key] === 'object') {
rtPart[key] = processResourceTypePattern(rtPart[key], pattern);
}
}
return rtPart;
};
let deepMerge = (toRaw, fromRaw) => {
let to = JSON.parse(JSON.stringify(toRaw)),
from = JSON.parse(JSON.stringify(fromRaw)),
fromKeys = Object.keys(from);
for (let i = 0; i < fromKeys.length; i++) {
let fromKey = fromKeys[i];
if (!to[fromKey]) {
to[fromKey] = from[fromKey];
} else if (typeof to[fromKey] === 'object') {
to[fromKey] = deepMerge(to[fromKey], from[fromKey]);
}
}
return to;
};
handlebars.registerHelper('getMethods', resource => {
let methods = JSON.parse(JSON.stringify(resource.methods));
// resource types
if (resource.type) {
let resourceTypes = Object.keys(resource.type);
for (let i = 0; i < resourceTypes.length; i++) {
let resourceTypeKey = resourceTypes[i],
resourceType = JSON.parse(JSON.stringify(ramlObj.resourceTypes[resourceTypeKey])),
rtMethods = Object.keys(resourceType);
for (let j = 0; j < rtMethods.length; j++) {
let rtMethodKey = rtMethods[j];
if (typeof methods[rtMethodKey] === 'string' && methods[rtMethodKey].length === 0) {
delete methods[rtMethodKey];
} else if (!methods[rtMethodKey]) {
methods[rtMethodKey] = processResourceTypePattern(resourceType[rtMethodKey], resource.type[resourceTypeKey]);
} else {
methods[rtMethodKey] = deepMerge(methods[rtMethodKey], processResourceTypePattern(resourceType[rtMethodKey], resource.type[resourceTypeKey]));
}
}
}
}
// security schemes
if (ramlObj.securedBy) {
let keys = Object.keys(methods);
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
if (AVAILABLE_METHODS.indexOf(key) === -1) {
continue;
}
if (!methods[key].securedBy) {
methods[key].securedBy = ramlObj.securedBy;
for (let j = 0; j < ramlObj.securedBy.length; j++) {
let secKey = ramlObj.securedBy[j],
sec = ramlObj.securitySchemes[secKey];
if (sec.describedBy) {
if (sec.describedBy.headers) {
methods[key].headers = methods[key].headers || {};
let hKeys = Object.keys(sec.describedBy.headers);
for (let k = 0; k < hKeys.length; k++) {
let hKey = hKeys[k];
methods[key].headers[hKey] = sec.describedBy.headers[hKey];
}
}
if (sec.describedBy.responses) {
methods[key].responses = methods[key].responses || {};
let hKeys = Object.keys(sec.describedBy.responses);
for (let k = 0; k < hKeys.length; k++) {
let hKey = hKeys[k];
methods[key].responses[hKey] = sec.describedBy.responses[hKey];
}
}
}
}
}
}
}
// traits
let methodNames = Object.keys(methods);
for (let i = 0; i < methodNames.length; i++) {
let methodName = methodNames[i],
method = methods[methodName];
if (method.is) {
for (let j = 0; j < method.is.length; j++) {
let traitName = method.is[j];
if (ramlObj.traits && ramlObj.traits[traitName]) {
methods[methodName] = deepMerge(methods[methodName], ramlObj.traits[traitName]);
}
}
}
}
return methods;
});
handlebars.registerHelper('getSecuritySchema', function(name) {
return ramlObj.securitySchemes[name];
});
let template = handlebars.compile(
fs.readFileSync(path.join(templatesPath, mainTemplate), {encoding: 'utf8'}),
{trackIds: true}
);
let html = template(ramlObj);
// Return the promise with the html
return new Promise(resolve => resolve(html));
},
postProcessHtml: html => {
// Minimize the generated html and return the promise with the result
let Minimize = require('minimize'),
minimize = new Minimize({});
return new Promise((resolve, reject) => {
minimize.parse(html, (error, result) => {
if (error) {
reject(new Error(error));
} else {
resolve(result);
}
});
});
}
};
}
module.exports = {
getDefaultConfig: getDefaultConfig,
render: render
};
if (require.main === module) {
console.log('This script is meant to be used as a library. You probably want to run bin/raml2html if you\'re looking for a CLI.');
process.exit(1);
}