Extract sendResponse middleware

This commit is contained in:
Daniel García Aubert 2018-03-19 19:48:14 +01:00
parent bb170ee208
commit 9211fa065b
6 changed files with 25 additions and 65 deletions

View File

@ -7,6 +7,7 @@ const layergroupToken = require('../middleware/layergroup-token');
const credentials = require('../middleware/credentials');
const authorize = require('../middleware/authorize');
const dbConnSetup = require('../middleware/db-conn-setup');
const sendResponse = require('../middleware/send-response');
function AnalysesController(pgConnection, authApi) {
this.pgConnection = pgConnection;
@ -114,18 +115,6 @@ function setCacheControlHeader () {
};
}
function sendResponse () {
return function sendResponseMiddleware (req, res) {
res.status(200);
if (req.query && req.query.callback) {
res.jsonp(res.body);
} else {
res.json(res.body);
}
};
}
function unauthorizedError () {
return function unathorizedErrorMiddleware(err, req, res, next) {
if (err.message.match(/permission\sdenied/)) {

View File

@ -7,6 +7,7 @@ const layergroupToken = require('../middleware/layergroup-token');
const credentials = require('../middleware/credentials');
const dbConnSetup = require('../middleware/db-conn-setup');
const authorize = require('../middleware/authorize');
const sendResponse = require('../middleware/send-response');
const DataviewBackend = require('../backends/dataview');
const AnalysisStatusBackend = require('../backends/analysis-status');
const MapStoreMapConfigProvider = require('../models/mapconfig/provider/map-store-provider');
@ -644,24 +645,6 @@ function incrementSuccessMetrics (statsClient) {
};
}
function sendResponse () {
return function sendResponseMiddleware (req, res) {
req.profiler.done('res');
res.status(res.statusCode || 200);
if (!Buffer.isBuffer(res.body) && typeof res.body === 'object') {
if (req.query && req.query.callback) {
res.jsonp(res.body);
} else {
res.json(res.body);
}
} else {
res.send(res.body);
}
};
}
function incrementErrorMetrics (statsClient) {
return function incrementErrorMetricsMiddleware (err, req, res, next) {
const formatStat = parseFormat(req.params.format);

View File

@ -12,6 +12,7 @@ const layergroupToken = require('../middleware/layergroup-token');
const credentials = require('../middleware/credentials');
const dbConnSetup = require('../middleware/db-conn-setup');
const authorize = require('../middleware/authorize');
const sendResponse = require('../middleware/send-response');
const NamedMapsCacheEntry = require('../cache/model/named_maps_entry');
const NamedMapMapConfigProvider = require('../models/mapconfig/provider/named-map-provider');
const CreateLayergroupMapConfigProvider = require('../models/mapconfig/provider/create-layergroup-provider');
@ -557,20 +558,6 @@ function setSurrogateKeyHeader (surrogateKeysCache) {
};
}
function sendResponse () {
return function sendResponseMiddleware (req, res) {
req.profiler.done('res');
res.status(res.statusCode || 200);
if (req.query && req.query.callback) {
res.jsonp(res.body);
} else {
res.json(res.body);
}
};
}
function augmentError (options) {
const { addContext = false, label = 'MAPS CONTROLLER' } = options;

View File

@ -7,6 +7,7 @@ const layergroupToken = require('../middleware/layergroup-token');
const credentials = require('../middleware/credentials');
const dbConnSetup = require('../middleware/db-conn-setup');
const authorize = require('../middleware/authorize');
const sendResponse = require('../middleware/send-response');
const vectorError = require('../middleware/vector-error');
const DEFAULT_ZOOM_CENTER = {
@ -214,10 +215,11 @@ function prepareLayerFilterFromPreviewLayers ({ namedMapProviderCache, label })
function getTile ({ tileBackend, label }) {
return function getTileMiddleware (req, res, next) {
const { namedMapProvider } = res.locals;
const { namedMapProvider, format } = res.locals;
tileBackend.getTile(namedMapProvider, req.params, (err, tile, headers, stats) => {
req.profiler.add(stats);
req.profiler.done('render-' + format);
if (err) {
err.label = label;
@ -371,6 +373,7 @@ function getImage({ previewBackend, label }) {
previewBackend.getImage(namedMapProvider, format, width, height, bounds, (err, image, headers, stats) => {
req.profiler.add(stats);
req.profiler.done('render-' + format);
if (err) {
err.label = label;
@ -514,14 +517,3 @@ function setContentTypeHeader () {
next();
};
}
function sendResponse () {
return function sendResponseMiddleware (req, res) {
const { format } = res.locals;
req.profiler.done('render-' + format);
res.status(200);
res.send(res.body);
};
}

View File

@ -3,6 +3,7 @@ const cors = require('../middleware/cors');
const user = require('../middleware/user');
const locals = require('../middleware/locals');
const credentials = require('../middleware/credentials');
const sendResponse = require('../middleware/send-response');
/**
* @param {AuthApi} authApi
@ -216,12 +217,3 @@ function listTemplates ({ templateMaps }) {
});
};
}
function sendResponse () {
return function sendResponseMiddleware (req, res) {
res.status(res.statusCode || 200);
const method = req.query.callback ? 'jsonp' : 'json';
res[method](res.body);
};
}

View File

@ -0,0 +1,17 @@
module.exports = function sendResponse () {
return function sendResponseMiddleware (req, res) {
req.profiler.done('res');
res.status(res.statusCode || 200);
if (Buffer.isBuffer(res.body)) {
return res.send(res.body);
}
if (req.query.callback) {
return res.jsonp(res.body);
}
res.json(res.body);
};
};