Fix code review, review error message format

This commit is contained in:
Jim Laurie 2017-07-13 17:09:44 +02:00
parent 9507c999eb
commit a09e25620e
3 changed files with 136 additions and 143 deletions

View File

@ -18,15 +18,7 @@
},
{
"method": "GET",
"path": "/configs/:slug",
"handler": "SettingsManager.get",
"config": {
"policies": []
}
},
{
"method": "GET",
"path": "/configs/:slug/:env",
"path": "/configurations/:slug/:env?",
"handler": "SettingsManager.get",
"config": {
"policies": []
@ -34,15 +26,7 @@
},
{
"method": "PUT",
"path": "/configs/:slug",
"handler": "SettingsManager.update",
"config": {
"policies": []
}
},
{
"method": "PUT",
"path": "/configs/:slug/:env",
"path": "/configurations/:slug/:env?",
"handler": "SettingsManager.update",
"config": {
"policies": []

View File

@ -17,12 +17,12 @@ module.exports = {
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
const { slug, env } = ctx.params;
if (env && _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData('request.error.environment.unknow');
if (env && _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
const model = env ? Service[slug](env) : Service[slug];
if (_.isUndefined(model)) return ctx.badData('request.error.config');
if (_.isFunction(model)) return ctx.badData('request.error.environment.required');
if (_.isUndefined(model)) return ctx.badData(null, [{ messages: [{ id: 'request.error.config' }] }]);
if (_.isFunction(model)) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.required' }] }]);
ctx.send(model);
},
@ -32,24 +32,28 @@ module.exports = {
const { slug, env } = ctx.params;
let params = ctx.request.body;
if (env && _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData('request.error.environment.unknow');
if (env && _.isEmpty(_.find(Service.getEnvironments(), { name: env }))) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.unknow' }] }]);
const model = env ? Service[slug](env) : Service[slug];
if (_.isUndefined(model)) return ctx.badData('request.error.config');
if (_.isFunction(model)) return ctx.badData('request.error.environment.required');
if (_.isUndefined(model)) return ctx.badData(null, [{ messages: [{ id: 'request.error.config' }] }]);
if (_.isFunction(model)) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.required' }] }]);
const items = Service.getItems(model);
params = Service.cleanParams(params, items);
let validationErrors = Service.paramsValidation(params, items);
const validationErrors = Service.paramsValidation(params, items);
if (!_.isEmpty(validationErrors)) {
return ctx.badData(null, validationErrors);
return ctx.badData(null, Service.formatErrors(validationErrors));
}
Service.updateSettings(params, items, env);
const updateErrors = Service.updateSettings(params, items, env);
if (!_.isEmpty(updateErrors)) {
return ctx.badData(null, Service.formatErrors(updateErrors));
}
ctx.send();
},

View File

@ -122,102 +122,98 @@ module.exports = {
]
},
security: env => {
return {
name: 'form.security.name',
description: 'form.security.description',
sections: [
{
name: 'form.security.item.session',
items: [
{
name: 'form.security.item.session.key',
target: 'security.session.key',
type: 'string',
value: _.get(strapi.config, `environments.${env}.security.session.key`, null),
validations: {
required: true
}
},
{
name: 'form.security.item.session.maxAge',
target: 'security.session.maxAge',
type: 'number',
value: _.get(strapi.config, `environments.${env}.security.session.maxAge`, null)
security: env => ({
name: 'form.security.name',
description: 'form.security.description',
sections: [
{
name: 'form.security.item.session',
items: [
{
name: 'form.security.item.session.key',
target: 'security.session.key',
type: 'string',
value: _.get(strapi.config, `environments.${env}.security.session.key`, null),
validations: {
required: true
}
]
},
{
name: '',
items: [
{
name: 'form.security.item.xframe',
target: 'security.xframe',
type: 'enum',
value: _.get(strapi.config, `environments.${env}.security.xframe`, null),
items: [
{
name: 'form.security.item.xframe.deny',
value: 'DENY',
},
{
name: 'form.security.item.xframe.sameorigin',
value: 'SAMEORIGIN',
},
{
name: 'form.security.item.xframe.allow-from',
value: 'ALLOW-FROM',
},
]
},
{
name: 'form.security.item.xssProtection',
target: 'security.xssProtection',
type: 'boolean',
value: _.get(strapi.config, `environments.${env}.security.xssProtection`, null)
}
]
},
{
name: 'form.security.item.cors',
items: [
{
name: 'form.security.item.cors.origin',
target: 'security.cors.origin',
type: 'string',
value: _.get(strapi.config, `environments.${env}.security.cors.origin`, null)
}
]
}
]
};
},
},
{
name: 'form.security.item.session.maxAge',
target: 'security.session.maxAge',
type: 'number',
value: _.get(strapi.config, `environments.${env}.security.session.maxAge`, null)
}
]
},
{
name: '',
items: [
{
name: 'form.security.item.xframe',
target: 'security.xframe',
type: 'enum',
value: _.get(strapi.config, `environments.${env}.security.xframe`, null),
items: [
{
name: 'form.security.item.xframe.deny',
value: 'DENY',
},
{
name: 'form.security.item.xframe.sameorigin',
value: 'SAMEORIGIN',
},
{
name: 'form.security.item.xframe.allow-from',
value: 'ALLOW-FROM',
},
]
},
{
name: 'form.security.item.xssProtection',
target: 'security.xssProtection',
type: 'boolean',
value: _.get(strapi.config, `environments.${env}.security.xssProtection`, null)
}
]
},
{
name: 'form.security.item.cors',
items: [
{
name: 'form.security.item.cors.origin',
target: 'security.cors.origin',
type: 'string',
value: _.get(strapi.config, `environments.${env}.security.cors.origin`, null)
}
]
}
]
}),
server: env => {
return {
name: 'form.server.name',
description: 'form.server.description',
sections: [
{
name: '',
items: [
{
name: 'form.server.item.host',
target: 'server.host',
type: 'string',
value: _.get(strapi.config, `environments.${env}.server.host`, null)
},
{
name: 'form.server.item.port',
target: 'server.port',
type: 'number',
value: _.get(strapi.config, `environments.${env}.server.port`, null)
}
]
}
]
};
},
server: env => ({
name: 'form.server.name',
description: 'form.server.description',
sections: [
{
name: '',
items: [
{
name: 'form.server.item.host',
target: 'server.host',
type: 'string',
value: _.get(strapi.config, `environments.${env}.server.host`, null)
},
{
name: 'form.server.item.port',
target: 'server.port',
type: 'number',
value: _.get(strapi.config, `environments.${env}.server.port`, null)
}
]
}
]
}),
getEnvironments: () => {
return _.map(_.keys(strapi.config.environments), environment => {
@ -228,12 +224,7 @@ module.exports = {
});
},
getItems: model => {
let items = [];
_.forEach(model.sections, section => items = _.concat(items, section.items));
return items;
},
getItems: model => _.flatten(_.map(model.sections, section => section.items)),
cleanParams: (params, items) => {
const cleanParams = {};
@ -243,6 +234,18 @@ module.exports = {
return cleanParams;
},
formatErrors: errors => _.map(_.groupBy(errors, 'target'), (errs, target) => {
return {
target,
messages: _.map(errs, err => {
return {
id: err.message,
params: _.get(err, 'params', undefined)
}
})
}
}),
paramsValidation: (params, items) => {
let errors = [];
@ -306,36 +309,38 @@ module.exports = {
}
});
if (!_.isEmpty(errors)) {
const grpTarget = _.groupBy(errors, 'target');
errors = _.map(grpTarget, (errs, target) => {
return {
target,
messages: _.map(errs, err => err.message)
}
});
}
return errors;
},
updateSettings: (params, items, env = '') => {
const appPath = process.cwd();
const appPath = strapi.config.appPath;
const errors = [];
_.forEach(items, ({ target }) => {
if (_.has(params, target)) {
const input = _.get(params, target, null);
const [file, ...objPath] = target.split('.');
let filePath = (file === 'package') ? path.join(appPath, 'package.json') : path.join(appPath, 'config', `${env ? `environments/${env}` : ''}`, `${_.replace(file, '.', '/')}.json`);
const filePath = (file === 'package') ? path.join(appPath, 'package.json') : path.join(appPath, 'config', `${env ? `environments/${env}` : ''}`, `${_.replace(file, '.', '/')}.json`);
const fileContent = require(filePath);
try {
const fileContent = require('coucou');
_.set(fileContent, objPath, input);
_.set(fileContent, objPath, input);
fs.writeFileSync(filePath, JSON.stringify(fileContent, null, 2), 'utf8');
fs.writeFileSync(filePath, JSON.stringify(fileContent, null, 2), 'utf8');
} catch (e) {
errors.push({
target,
message: 'coucou',
params: {
filePath: filePath
}
});
}
}
});
return errors;
}
};