-
Notifications
You must be signed in to change notification settings - Fork 1
/
responder.js
142 lines (134 loc) · 4.83 KB
/
responder.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
'use strict';
/**
*
* @param {ClientRequest} req The client request object
* @param {ServerResponse} res The server response object
* @param {Object} payload The data to be send to the client
* @param {number} [status] A status code that will be set if you don't want express to set one for you
*/
exports.respond = function(req, res, payload, status) {
status && res.status(status);
if (!payload || !Object.keys(payload).length) {
status || res.status(204);
return res.end();
}
var format = req.params && req.params['format'] || req.query && req.query['format'] || 'json';
switch(format.toLowerCase()) {
case 'json':
return res.jsonp ? res.jsonp(payload).end() : res.json(payload).end();
case 'tree':
return res.send('<html><body style="white-space: pre">' + JSON.stringify(payload, null, 4) + '</body></html>').end();
case 'table':
return res.send(exports.formatTable(payload)).end();
case 'csv':
return res.send(exports.formatCSV(payload)).end();
case 'xml':
return res.send('<xml version="1.0" encoding="UTF-8">' + exports.formatXML(payload)).end() + '</xml>';
default:
return res.status(422).send('Invalid format requested').end();
}
};
/**
* Creates an HTML table of the json data passed in.
* @param {Object} payload
* @returns {string}
*/
exports.formatTable = function (payload) {
var table = exports.flatten(payload);
var response = '<html><body><h1>' + table.title + '</h1><table><thead>';
for (let header of table.headers) {
response += '<th>' + header + '</th>';
}
response += '</thead><tbody>';
for (let row of table.rows) {
response += '<tr>';
for (let column of row) {
response += '<td>' + column + '</td>';
}
response += '</tr>';
}
response += '</tbody></table></body></html>';
return response;
};
/**
* Creates a CSV table of the json data passed in.
* @param {Object} payload
* @returns {string}
*/
exports.formatCSV = function (payload) {
var table = exports.flatten(payload);
var response = table.headers.join(',') + '\n';
for (let row of table.rows) {
for(let i = 0; i < row.length; i++) {
row[i].replace(/"/g, '\\"');
if (row[i].indexOf(',') !== -1) {
row[i] = '"' + row[i] + '"';
}
}
response += row.join(',') + '\n';
}
return response;
};
/**
* A response that can be used by the different formatters to construct a table response out of the JSON tree structure.
* @typedef {Object} TableResponse
* @property {string} title A title that should be printed at the head of a page
* @property {string[]} headers A list of headers to be printed before the actual data
* @property {Array.<string[]>} rows A list of rows represented by lists of strings to be printed in order
*/
/**
* Converts the known response formats into a list of map like structure to represent a table
* @param {Object} payload
* @returns {TableResponse} The response to be printed
*/
exports.flatten = function(payload) {
var response = {
rows: []
};
if (payload.error) {
response.title = payload.error;
response.headers = [ 'param', 'type', 'error', 'max', 'min'];
for (let param in payload.params) {
response.rows.push([
param,
payload.params[param].type,
payload.params[param].error,
isNaN(payload.params[param].max) ? '' : payload.params[param].max,
isNaN(payload.params[param].min) ? '' : payload.params[param].min
])
}
} else {
response.title = 'Api Map';
response.headers = [ 'path', 'method', 'description', 'param', 'type', 'paramDescription'];
for (let path in payload) {
for (let method in payload[path]) {
for (let param in payload[path][method].params) {
response.rows.push([
path,
method,
payload[path][method].description,
param,
payload[path][method].params[param].type,
payload[path][method].params[param].description
]);
}
}
}
}
return response;
};
/**
* Creates an XML response of the json data passed in.
* @param {Object|string} payload
* @returns {string}
*/
exports.formatXML = function (payload) {
var response = '';
if (typeof payload == 'object') {
for (let prop in payload) {
response += '<' + prop + '>' + exports.formatXML(payload[prop]) + '</' + prop + '>';
}
return response;
}
return payload;
};