Move prepeareConfigFn to a middleware
This commit is contained in:
parent
6bbaeaa286
commit
8ed5df0072
@ -56,6 +56,7 @@ MapController.prototype.register = function(app) {
|
||||
cors(),
|
||||
userMiddleware,
|
||||
this.prepareContext,
|
||||
createGetPrepareConfig,
|
||||
this.createGet.bind(this),
|
||||
mapErrorMiddleware({
|
||||
label: 'ANONYMOUS LAYERGROUP',
|
||||
@ -67,6 +68,7 @@ MapController.prototype.register = function(app) {
|
||||
cors(),
|
||||
userMiddleware,
|
||||
this.prepareContext,
|
||||
createPostPrepareConfig,
|
||||
this.createPost.bind(this),
|
||||
mapErrorMiddleware({
|
||||
label: 'ANONYMOUS LAYERGROUP',
|
||||
@ -78,6 +80,7 @@ MapController.prototype.register = function(app) {
|
||||
cors(),
|
||||
userMiddleware,
|
||||
this.prepareContext,
|
||||
prepareJsonTemplateParams,
|
||||
this.jsonp.bind(this),
|
||||
mapErrorMiddleware({
|
||||
label: 'NAMED MAP LAYERGROUP'
|
||||
@ -88,6 +91,7 @@ MapController.prototype.register = function(app) {
|
||||
cors(),
|
||||
userMiddleware,
|
||||
this.prepareContext,
|
||||
prepareTemplateParams,
|
||||
this.instantiate.bind(this),
|
||||
mapErrorMiddleware({
|
||||
label: 'NAMED MAP LAYERGROUP'
|
||||
@ -96,62 +100,80 @@ MapController.prototype.register = function(app) {
|
||||
app.options(app.base_url_mapconfig, cors('Content-Type'));
|
||||
};
|
||||
|
||||
function createGetPrepareConfig (req, res, next) {
|
||||
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) {
|
||||
if (!req.is('application/json')) {
|
||||
return next(new Error('layergroup POST data must be of type application/json'));
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
function prepareTemplateParams(req, res, next) {
|
||||
if (!req.is('application/json')) {
|
||||
return next(new Error('Template POST data must be of type application/json'));
|
||||
}
|
||||
|
||||
return next();
|
||||
}
|
||||
|
||||
function prepareJsonTemplateParams(req, res, next) {
|
||||
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'));
|
||||
}
|
||||
}
|
||||
|
||||
return next();
|
||||
}
|
||||
|
||||
MapController.prototype.createGet = function(req, res, next){
|
||||
req.profiler.start('windshaft.createmap_get');
|
||||
|
||||
this.create(req, res, function createGet$prepareConfig(req, config) {
|
||||
if ( ! config ) {
|
||||
throw new Error('layergroup GET needs a "config" parameter');
|
||||
}
|
||||
return JSON.parse(config);
|
||||
}, next);
|
||||
this.create(req, res, next);
|
||||
};
|
||||
|
||||
MapController.prototype.createPost = function(req, res, next) {
|
||||
req.profiler.start('windshaft.createmap_post');
|
||||
|
||||
this.create(req, res, function createPost$prepareConfig(req) {
|
||||
if (!req.is('application/json')) {
|
||||
throw new Error('layergroup POST data must be of type application/json');
|
||||
}
|
||||
return req.body;
|
||||
}, next);
|
||||
this.create(req, res, next);
|
||||
};
|
||||
|
||||
MapController.prototype.instantiate = function(req, res, next) {
|
||||
req.profiler.start('windshaft-cartodb.instance_template_post');
|
||||
|
||||
this.instantiateTemplate(req, res, function prepareTemplateParams(callback) {
|
||||
if (!req.is('application/json')) {
|
||||
return callback(new Error('Template POST data must be of type application/json'));
|
||||
}
|
||||
return callback(null, req.body);
|
||||
}, next);
|
||||
this.instantiateTemplate(req, res, next);
|
||||
};
|
||||
|
||||
MapController.prototype.jsonp = function(req, res, next) {
|
||||
req.profiler.start('windshaft-cartodb.instance_template_get');
|
||||
|
||||
this.instantiateTemplate(req, res, function prepareJsonTemplateParams(callback) {
|
||||
var err = null;
|
||||
if ( req.query.callback === undefined || req.query.callback.length === 0) {
|
||||
err = new Error('callback parameter should be present and be a function name');
|
||||
}
|
||||
|
||||
var templateParams = {};
|
||||
if (req.query.config) {
|
||||
try {
|
||||
templateParams = JSON.parse(req.query.config);
|
||||
} catch(e) {
|
||||
err = new Error('Invalid config parameter, should be a valid JSON');
|
||||
}
|
||||
}
|
||||
|
||||
return callback(err, templateParams);
|
||||
}, next);
|
||||
this.instantiateTemplate(req, res, next);
|
||||
};
|
||||
|
||||
MapController.prototype.create = function(req, res, prepareConfigFn, next) {
|
||||
MapController.prototype.create = function(req, res, next) {
|
||||
var self = this;
|
||||
|
||||
var mapConfig;
|
||||
@ -159,12 +181,9 @@ MapController.prototype.create = function(req, res, prepareConfigFn, next) {
|
||||
var context = {};
|
||||
|
||||
step(
|
||||
function prepareConfig () {
|
||||
const requestMapConfig = prepareConfigFn(req, res.locals.config);
|
||||
return requestMapConfig;
|
||||
},
|
||||
function prepareAdapterMapConfig(err, requestMapConfig) {
|
||||
assert.ifError(err);
|
||||
function prepareAdapterMapConfig() {
|
||||
const requestMapConfig = req.body;
|
||||
|
||||
context.analysisConfiguration = {
|
||||
user: res.locals.user,
|
||||
db: {
|
||||
@ -223,7 +242,7 @@ MapController.prototype.create = function(req, res, prepareConfigFn, next) {
|
||||
);
|
||||
};
|
||||
|
||||
MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn, next) {
|
||||
MapController.prototype.instantiateTemplate = function(req, res, next) {
|
||||
var self = this;
|
||||
|
||||
var cdbuser = res.locals.user;
|
||||
@ -231,11 +250,9 @@ MapController.prototype.instantiateTemplate = function(req, res, prepareParamsFn
|
||||
var mapConfigProvider;
|
||||
var mapConfig;
|
||||
step(
|
||||
function getTemplateParams() {
|
||||
prepareParamsFn(this);
|
||||
},
|
||||
function getTemplate(err, templateParams) {
|
||||
assert.ifError(err);
|
||||
function getTemplate() {
|
||||
const templateParams = req.body;
|
||||
|
||||
mapConfigProvider = new NamedMapMapConfigProvider(
|
||||
self.templateMaps,
|
||||
self.pgConnection,
|
||||
|
Loading…
Reference in New Issue
Block a user