mirror of
https://github.com/bckelley/cgm-remote-monitor.git
synced 2026-08-24 03:14:12 -05:00
* APIv3: isolating documents from tests (not allowing clashes of calculated identifiers) * removing unused async keyword * fixing api v3 swagger and moving it to /api3-docs * APIv3: adding cachedCollection stub of cachedCollection storage implementation * APIv3: mongo cachedCollection storage implementation * APIv3: testing and debugging cache updates * APIv3: more testing on cache updates * APIv3: fixing bad async functions * APIv3: finishing cache invalidation tests * APIv3: wrapping VERSION result * APIv3: wrapping STATUS result * APIv3: wrapping DELETE result * APIv3: wrapping READ result + partially SEARCH and HISTORY * APIv3: wrapping CREATE result * APIv3: wrapping UPDATE + PATCH result * APIv3: wrapping LAST MODIFIED result * APIv3: updating swagger doc * APIv3: updating tutorial.md * APIv3: tuning tests * APIv3: merge dev Co-authored-by: Petr Ondrusek <petr.ondrusek@seznam.cz> Co-authored-by: Petr Ondrůšek <petr.ondrusek@okin.eu> Co-authored-by: Sulka Haro <sulka@sulka.net>
113 lines
2.2 KiB
JavaScript
113 lines
2.2 KiB
JavaScript
'use strict';
|
|
|
|
const apiConst = require('../const.json')
|
|
, mime = require('mime')
|
|
, url = require('url')
|
|
, opTools = require('./operationTools')
|
|
, EasyXml = require('easyxml')
|
|
, csvStringify = require('csv-stringify')
|
|
;
|
|
|
|
|
|
/**
|
|
* Middleware that converts url's extension to Accept HTTP request header
|
|
* @param {Object} req
|
|
* @param {Object} res
|
|
* @param {Function} next
|
|
*/
|
|
function extension2accept (req, res, next) {
|
|
|
|
const pathSplit = req.path.split('.');
|
|
|
|
if (pathSplit.length < 2)
|
|
return next();
|
|
|
|
const pathBase = pathSplit[0]
|
|
, extension = pathSplit.slice(1).join('.');
|
|
|
|
if (!extension)
|
|
return next();
|
|
|
|
const mimeType = mime.getType(extension);
|
|
if (!mimeType)
|
|
return opTools.sendJSONStatus(res, apiConst.HTTP.NOT_ACCEPTABLE, apiConst.MSG.HTTP_406_UNSUPPORTED_FORMAT);
|
|
|
|
req.extToAccept = {
|
|
url: req.url,
|
|
accept: req.headers.accept
|
|
};
|
|
|
|
req.headers.accept = mimeType;
|
|
const parsed = url.parse(req.url);
|
|
parsed.pathname = pathBase;
|
|
req.url = url.format(parsed);
|
|
|
|
next();
|
|
}
|
|
|
|
|
|
/**
|
|
* Sends data to output using the client's desired format
|
|
* @param {Object} res
|
|
* @param {any} data
|
|
*/
|
|
function render (res, data) {
|
|
res.format({
|
|
'json': () => renderJson(res, data),
|
|
'csv': () => renderCsv(res, data),
|
|
'xml': () => renderXml(res, data),
|
|
'default': () =>
|
|
opTools.sendJSONStatus(res, apiConst.HTTP.NOT_ACCEPTABLE, apiConst.MSG.HTTP_406_UNSUPPORTED_FORMAT)
|
|
});
|
|
}
|
|
|
|
|
|
/**
|
|
* Format data to output as JSON
|
|
* @param {Object} res
|
|
* @param {any} data
|
|
*/
|
|
function renderJson (res, data) {
|
|
res.send({
|
|
status: apiConst.HTTP.OK,
|
|
result: data
|
|
});
|
|
}
|
|
|
|
|
|
/**
|
|
* Format data to output as .csv
|
|
* @param {Object} res
|
|
* @param {any} data
|
|
*/
|
|
function renderCsv (res, data) {
|
|
const csvSource = Array.isArray(data) ? data : [data];
|
|
csvStringify(csvSource, {
|
|
header: true
|
|
},
|
|
function csvStringified (err, output) {
|
|
res.send(output);
|
|
});
|
|
}
|
|
|
|
|
|
/**
|
|
* Format data to output as .xml
|
|
* @param {Object} res
|
|
* @param {any} data
|
|
*/
|
|
function renderXml (res, data) {
|
|
const serializer = new EasyXml({
|
|
rootElement: 'item',
|
|
dateFormat: 'ISO',
|
|
manifest: true
|
|
});
|
|
res.send(serializer.render(data));
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
extension2accept,
|
|
render
|
|
};
|