mirror of
https://github.com/strapi/strapi.git
synced 2025-09-03 13:50:38 +00:00
Fix code review, review error message format
This commit is contained in:
parent
9507c999eb
commit
a09e25620e
@ -18,15 +18,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"path": "/configs/:slug",
|
"path": "/configurations/:slug/:env?",
|
||||||
"handler": "SettingsManager.get",
|
|
||||||
"config": {
|
|
||||||
"policies": []
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"method": "GET",
|
|
||||||
"path": "/configs/:slug/:env",
|
|
||||||
"handler": "SettingsManager.get",
|
"handler": "SettingsManager.get",
|
||||||
"config": {
|
"config": {
|
||||||
"policies": []
|
"policies": []
|
||||||
@ -34,15 +26,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"method": "PUT",
|
"method": "PUT",
|
||||||
"path": "/configs/:slug",
|
"path": "/configurations/:slug/:env?",
|
||||||
"handler": "SettingsManager.update",
|
|
||||||
"config": {
|
|
||||||
"policies": []
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"method": "PUT",
|
|
||||||
"path": "/configs/:slug/:env",
|
|
||||||
"handler": "SettingsManager.update",
|
"handler": "SettingsManager.update",
|
||||||
"config": {
|
"config": {
|
||||||
"policies": []
|
"policies": []
|
||||||
|
@ -17,12 +17,12 @@ module.exports = {
|
|||||||
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
|
const Service = strapi.plugins['settings-manager'].services.settingsmanager;
|
||||||
const { slug, env } = ctx.params;
|
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];
|
const model = env ? Service[slug](env) : Service[slug];
|
||||||
|
|
||||||
if (_.isUndefined(model)) return ctx.badData('request.error.config');
|
if (_.isUndefined(model)) return ctx.badData(null, [{ messages: [{ id: 'request.error.config' }] }]);
|
||||||
if (_.isFunction(model)) return ctx.badData('request.error.environment.required');
|
if (_.isFunction(model)) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.required' }] }]);
|
||||||
|
|
||||||
ctx.send(model);
|
ctx.send(model);
|
||||||
},
|
},
|
||||||
@ -32,24 +32,28 @@ module.exports = {
|
|||||||
const { slug, env } = ctx.params;
|
const { slug, env } = ctx.params;
|
||||||
let params = ctx.request.body;
|
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];
|
const model = env ? Service[slug](env) : Service[slug];
|
||||||
|
|
||||||
if (_.isUndefined(model)) return ctx.badData('request.error.config');
|
if (_.isUndefined(model)) return ctx.badData(null, [{ messages: [{ id: 'request.error.config' }] }]);
|
||||||
if (_.isFunction(model)) return ctx.badData('request.error.environment.required');
|
if (_.isFunction(model)) return ctx.badData(null, [{ messages: [{ id: 'request.error.environment.required' }] }]);
|
||||||
|
|
||||||
const items = Service.getItems(model);
|
const items = Service.getItems(model);
|
||||||
|
|
||||||
params = Service.cleanParams(params, items);
|
params = Service.cleanParams(params, items);
|
||||||
|
|
||||||
let validationErrors = Service.paramsValidation(params, items);
|
const validationErrors = Service.paramsValidation(params, items);
|
||||||
|
|
||||||
if (!_.isEmpty(validationErrors)) {
|
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();
|
ctx.send();
|
||||||
},
|
},
|
||||||
|
@ -122,102 +122,98 @@ module.exports = {
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
||||||
security: env => {
|
security: env => ({
|
||||||
return {
|
name: 'form.security.name',
|
||||||
name: 'form.security.name',
|
description: 'form.security.description',
|
||||||
description: 'form.security.description',
|
sections: [
|
||||||
sections: [
|
{
|
||||||
{
|
name: 'form.security.item.session',
|
||||||
name: 'form.security.item.session',
|
items: [
|
||||||
items: [
|
{
|
||||||
{
|
name: 'form.security.item.session.key',
|
||||||
name: 'form.security.item.session.key',
|
target: 'security.session.key',
|
||||||
target: 'security.session.key',
|
type: 'string',
|
||||||
type: 'string',
|
value: _.get(strapi.config, `environments.${env}.security.session.key`, null),
|
||||||
value: _.get(strapi.config, `environments.${env}.security.session.key`, null),
|
validations: {
|
||||||
validations: {
|
required: true
|
||||||
required: true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'form.security.item.session.maxAge',
|
|
||||||
target: 'security.session.maxAge',
|
|
||||||
type: 'number',
|
|
||||||
value: _.get(strapi.config, `environments.${env}.security.session.maxAge`, null)
|
|
||||||
}
|
}
|
||||||
]
|
},
|
||||||
},
|
{
|
||||||
{
|
name: 'form.security.item.session.maxAge',
|
||||||
name: '',
|
target: 'security.session.maxAge',
|
||||||
items: [
|
type: 'number',
|
||||||
{
|
value: _.get(strapi.config, `environments.${env}.security.session.maxAge`, null)
|
||||||
name: 'form.security.item.xframe',
|
}
|
||||||
target: 'security.xframe',
|
]
|
||||||
type: 'enum',
|
},
|
||||||
value: _.get(strapi.config, `environments.${env}.security.xframe`, null),
|
{
|
||||||
items: [
|
name: '',
|
||||||
{
|
items: [
|
||||||
name: 'form.security.item.xframe.deny',
|
{
|
||||||
value: 'DENY',
|
name: 'form.security.item.xframe',
|
||||||
},
|
target: 'security.xframe',
|
||||||
{
|
type: 'enum',
|
||||||
name: 'form.security.item.xframe.sameorigin',
|
value: _.get(strapi.config, `environments.${env}.security.xframe`, null),
|
||||||
value: 'SAMEORIGIN',
|
items: [
|
||||||
},
|
{
|
||||||
{
|
name: 'form.security.item.xframe.deny',
|
||||||
name: 'form.security.item.xframe.allow-from',
|
value: 'DENY',
|
||||||
value: 'ALLOW-FROM',
|
},
|
||||||
},
|
{
|
||||||
]
|
name: 'form.security.item.xframe.sameorigin',
|
||||||
},
|
value: 'SAMEORIGIN',
|
||||||
{
|
},
|
||||||
name: 'form.security.item.xssProtection',
|
{
|
||||||
target: 'security.xssProtection',
|
name: 'form.security.item.xframe.allow-from',
|
||||||
type: 'boolean',
|
value: 'ALLOW-FROM',
|
||||||
value: _.get(strapi.config, `environments.${env}.security.xssProtection`, null)
|
},
|
||||||
}
|
]
|
||||||
]
|
},
|
||||||
},
|
{
|
||||||
{
|
name: 'form.security.item.xssProtection',
|
||||||
name: 'form.security.item.cors',
|
target: 'security.xssProtection',
|
||||||
items: [
|
type: 'boolean',
|
||||||
{
|
value: _.get(strapi.config, `environments.${env}.security.xssProtection`, null)
|
||||||
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.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 => {
|
server: env => ({
|
||||||
return {
|
name: 'form.server.name',
|
||||||
name: 'form.server.name',
|
description: 'form.server.description',
|
||||||
description: 'form.server.description',
|
sections: [
|
||||||
sections: [
|
{
|
||||||
{
|
name: '',
|
||||||
name: '',
|
items: [
|
||||||
items: [
|
{
|
||||||
{
|
name: 'form.server.item.host',
|
||||||
name: 'form.server.item.host',
|
target: 'server.host',
|
||||||
target: 'server.host',
|
type: 'string',
|
||||||
type: 'string',
|
value: _.get(strapi.config, `environments.${env}.server.host`, null)
|
||||||
value: _.get(strapi.config, `environments.${env}.server.host`, null)
|
},
|
||||||
},
|
{
|
||||||
{
|
name: 'form.server.item.port',
|
||||||
name: 'form.server.item.port',
|
target: 'server.port',
|
||||||
target: 'server.port',
|
type: 'number',
|
||||||
type: 'number',
|
value: _.get(strapi.config, `environments.${env}.server.port`, null)
|
||||||
value: _.get(strapi.config, `environments.${env}.server.port`, null)
|
}
|
||||||
}
|
]
|
||||||
]
|
}
|
||||||
}
|
]
|
||||||
]
|
}),
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
getEnvironments: () => {
|
getEnvironments: () => {
|
||||||
return _.map(_.keys(strapi.config.environments), environment => {
|
return _.map(_.keys(strapi.config.environments), environment => {
|
||||||
@ -228,12 +224,7 @@ module.exports = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
getItems: model => {
|
getItems: model => _.flatten(_.map(model.sections, section => section.items)),
|
||||||
let items = [];
|
|
||||||
_.forEach(model.sections, section => items = _.concat(items, section.items));
|
|
||||||
|
|
||||||
return items;
|
|
||||||
},
|
|
||||||
|
|
||||||
cleanParams: (params, items) => {
|
cleanParams: (params, items) => {
|
||||||
const cleanParams = {};
|
const cleanParams = {};
|
||||||
@ -243,6 +234,18 @@ module.exports = {
|
|||||||
return cleanParams;
|
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) => {
|
paramsValidation: (params, items) => {
|
||||||
let errors = [];
|
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;
|
return errors;
|
||||||
},
|
},
|
||||||
|
|
||||||
updateSettings: (params, items, env = '') => {
|
updateSettings: (params, items, env = '') => {
|
||||||
const appPath = process.cwd();
|
const appPath = strapi.config.appPath;
|
||||||
|
const errors = [];
|
||||||
|
|
||||||
_.forEach(items, ({ target }) => {
|
_.forEach(items, ({ target }) => {
|
||||||
if (_.has(params, target)) {
|
if (_.has(params, target)) {
|
||||||
const input = _.get(params, target, null);
|
const input = _.get(params, target, null);
|
||||||
const [file, ...objPath] = target.split('.');
|
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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user