diff --git a/lib/cartodb/controllers/map.js b/lib/cartodb/controllers/map.js index 96a66e37..ab65b604 100644 --- a/lib/cartodb/controllers/map.js +++ b/lib/cartodb/controllers/map.js @@ -49,49 +49,34 @@ function MapController(prepareContext, pgConnection, templateMaps, mapBackend, m module.exports = MapController; MapController.prototype.register = function(app) { - app.get(app.base_url_mapconfig, this.composeCreateLayergroupMiddleware({ - parseConfigQueryParam: true, - includeQuery: true, - label: 'ANONYMOUS LAYERGROUP', - addContext: true + app.get(app.base_url_mapconfig, this.composeCreateMapMiddleware()); + + app.post(app.base_url_mapconfig, this.composeCreateMapMiddleware()); + + app.get(app.base_url_templated + '/:template_id/jsonp', this.composeCreateMapMiddleware({ + useTemplate: true })); - app.post(app.base_url_mapconfig, this.composeCreateLayergroupMiddleware({ - includeQuery: true, - label: 'ANONYMOUS LAYERGROUP', - addContext: true - })); - - app.get(app.base_url_templated + '/:template_id/jsonp', this.composeInstantiateLayergroupMiddleware({ - parseConfigQueryParam: true, - useTemplateHash: true, - label: 'NAMED MAP LAYERGROUP' - })); - - app.post(app.base_url_templated + '/:template_id', this.composeInstantiateLayergroupMiddleware({ - useTemplateHash: true, - label: 'NAMED MAP LAYERGROUP' + app.post(app.base_url_templated + '/:template_id', this.composeCreateMapMiddleware({ + useTemplate: true })); app.options(app.base_url_mapconfig, cors('Content-Type')); }; -MapController.prototype.composeCreateLayergroupMiddleware = function (options) { - const { - parseConfigQueryParam = false, - useTemplateHash = false, - includeQuery = false, - label, - addContext = false - } = options; +MapController.prototype.composeCreateMapMiddleware = function ({ useTemplate = false } = {}) { + const useTemplateHash = useTemplate; + const includeQuery = !useTemplate; + const label = useTemplate ? 'NAMED MAP LAYERGROUP' : 'ANONYMOUS LAYERGROUP'; + const addContext = !useTemplate; return [ cors(), userMiddleware, this.prepareContext, - parseConfigQueryParam ? createGetPrepareConfig : createPostPrepareConfig, - this.prepareAdapterMapConfig.bind(this), - this.createLayergroup.bind(this), + useTemplate ? checkIntantiteLayergroup : checkCreateLayergroup, + useTemplate ? this.getTemplate.bind(this) : this.prepareAdapterMapConfig.bind(this), + useTemplate ? this.instantiateLayergroup.bind(this) : this.createLayergroup.bind(this), this.incrementMapViewCount.bind(this), this.augmentLayergroupData.bind(this), this.getAffectedTables.bind(this), @@ -109,91 +94,51 @@ MapController.prototype.composeCreateLayergroupMiddleware = function (options) { ]; }; -MapController.prototype.composeInstantiateLayergroupMiddleware = function (options) { - const { - parseConfigQueryParam = false, - useTemplateHash = false, - includeQuery = false, - label, - addContext = false - } = options; +function checkCreateLayergroup (req, res, next) { + req.profiler.start(`windshaft.createmap_${req.method.toLowerCase()}`); - return [ - cors(), - userMiddleware, - this.prepareContext, - parseConfigQueryParam ? prepareJsonTemplateParams : prepareTemplateParams, - this.getTemplate.bind(this), - this.instantiateLayergroup.bind(this), - this.incrementMapViewCount.bind(this), - this.augmentLayergroupData.bind(this), - this.getAffectedTables.bind(this), - this.setCacheChannel.bind(this), - this.setLastUpdatedTimeToLayergroup.bind(this), - this.setCacheControl.bind(this), - this.setLayerStats.bind(this), - this.setLayergroupIdHeaderBuilder(useTemplateHash), - this.setDataviewsAndWidgetsUrlsToLayergroupMetadata.bind(this), - this.setAnalysesMetadataToLayergroupBuilder(includeQuery), - this.setTurboCartoMetadataToLayergroup.bind(this), - this.setSurrogateKeyHeader.bind(this), - sendResponse, - augmentError({ label, addContext }) - ]; -}; - -function createGetPrepareConfig (req, res, next) { - req.profiler.start(`windshaft.createmap_get`); - - const { config } = res.locals; - - if (!config) { - return next(new Error('layergroup GET needs a "config" parameter')); - } - - try { - req.body = JSON.parse(config); - } catch (err) { - return next(err); - } - - return next(); -} - -function createPostPrepareConfig(req, res, next) { - req.profiler.start('windshaft.createmap_post'); - - if (!req.is('application/json')) { + if (req.method === 'POST' && !req.is('application/json')) { return next(new Error('layergroup POST data must be of type application/json')); } - next(); -} + if (req.method === 'GET') { + const { config } = res.locals; -function prepareTemplateParams(req, res, next) { - req.profiler.start('windshaft-cartodb.instance_template_post'); + if (!config) { + return next(new Error('layergroup GET needs a "config" parameter')); + } - if (!req.is('application/json')) { - return next(new Error('Template POST data must be of type application/json')); + try { + req.body = JSON.parse(config); + } catch (err) { + return next(err); + } } return next(); } -function prepareJsonTemplateParams(req, res, next) { - req.profiler.start('windshaft-cartodb.instance_template_get'); +function checkIntantiteLayergroup(req, res, next) { + // jshint maxcomplexity: 7 + req.profiler.start(`windshaft-cartodb.instance_template_${req.method.toLowerCase()}`); - const { callback, config } = req.query; - - if (callback === undefined || callback.length === 0) { - return next(new Error('callback parameter should be present and be a function name')); + if (req.method === 'POST' && !req.is('application/json')) { + return next(new Error('Template POST data must be of type application/json')); } - if (config) { - try { - req.body = JSON.parse(config); - } catch(e) { - return next(new Error('Invalid config parameter, should be a valid JSON')); + if (req.method === 'GET') { + const { callback, config } = req.query; + + if (callback === undefined || callback.length === 0) { + return next(new Error('callback parameter should be present and be a function name')); + } + + if (config) { + try { + req.body = JSON.parse(config); + } catch(e) { + return next(new Error('Invalid config parameter, should be a valid JSON')); + } } }