all files / arrow-docgen/lib/ request.js

67.44% Statements 29/43
69.57% Branches 16/23
100% Functions 5/5
67.44% Lines 29/43
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                                                                                                               
// 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']
		}
	});
}