// jscs:disable jsDoc
var sanitizeHtml = require('sanitize-html');
/*
Public API.
*/
exports.generate = generate;
/*
Implementation.
*/
function generate(object, api, endpoint, opts) {
var code = [];
code.push('### HTTP Request\n');
code.push(endpoint.method + ' ' + opts.url + '\n');
code.push();
if (endpoint.parameters) {
Object.keys(endpoint.parameters).forEach(function (param) {
var entry = endpoint.parameters[param];
switch (entry.type) {
case 'path':
opts.pathParams[param] = entry;
break;
case 'query':
opts.queryParams[param] = entry;
break;
case 'body':
opts.bodyParams[param] = entry;
break;
}
});
Eif (opts.pathParams && Object.keys(opts.pathParams).length) {
code.push('### Path Parameters\n');
code.push('Parameter | Type | Required | Default | Description');
code.push('--------- | ---- | -------- | ------- | -----------');
pushParams(opts.pathParams, code);
code.push();
}
Iif (opts.queryParams && Object.keys(opts.queryParams).length) {
code.push('### Query Parameters\n');
code.push('Parameter | Type | Required | Default | Description');
code.push('--------- | ---- | -------- | ------- | -----------');
pushParams(opts.queryParams, code);
code.push();
}
Iif (opts.bodyParams && Object.keys(opts.bodyParams).length) {
code.push('### Body Parameters\n');
code.push('Parameter | Type | Required | Default | Description');
code.push('--------- | ---- | -------- | ------- | -----------');
pushParams(opts.bodyParams, code);
code.push();
}
}
return code.join('\n');
}
/*
Utility.
*/
function pushParams(obj, code) {
Object.keys(obj).forEach(function (param) {
var entry = obj[param];
code.push([
param.replace(/_/g, '\\_'),
entry.dataType || 'string',
!entry.optional,
entry.default || 'N/A',
description(entry.description)
].join(' | '));
});
}
function description(content) {
content = (content || '').replace(/\n/g, '<br />');
return sanitizeHtml(content, {
allowedTags: ['b', 'i', 'em', 'strong', 'a', 'br'],
allowedAttributes: {
'a': ['href']
}
});
}
|