mirror of
https://github.com/strapi/strapi.git
synced 2025-12-26 22:54:31 +00:00
Refactor sanitizers module to be maintained easily
This commit is contained in:
parent
4429381ec9
commit
5959788352
@ -310,6 +310,10 @@ class Strapi {
|
||||
this.app = await loaders.loadSrcIndex(this);
|
||||
}
|
||||
|
||||
async loadSanitizers() {
|
||||
await loaders.loadSanitizers(this);
|
||||
}
|
||||
|
||||
registerInternalHooks() {
|
||||
this.container.get('hooks').set('strapi::content-types.beforeSync', createAsyncParallelHook());
|
||||
this.container.get('hooks').set('strapi::content-types.afterSync', createAsyncParallelHook());
|
||||
@ -321,6 +325,7 @@ class Strapi {
|
||||
async register() {
|
||||
await Promise.all([
|
||||
this.loadApp(),
|
||||
this.loadSanitizers(),
|
||||
this.loadPlugins(),
|
||||
this.loadAdmin(),
|
||||
this.loadAPIs(),
|
||||
|
||||
@ -8,4 +8,5 @@ module.exports = {
|
||||
loadPolicies: require('./policies'),
|
||||
loadPlugins: require('./plugins'),
|
||||
loadAdmin: require('./admin'),
|
||||
loadSanitizers: require('./sanitizers'),
|
||||
};
|
||||
|
||||
5
packages/core/strapi/lib/core/loaders/sanitizers.js
Normal file
5
packages/core/strapi/lib/core/loaders/sanitizers.js
Normal file
@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = strapi => {
|
||||
strapi.container.get('sanitizers').set('content-api', { input: [], output: [] });
|
||||
};
|
||||
@ -3,19 +3,19 @@
|
||||
const _ = require('lodash');
|
||||
|
||||
const sanitizersRegistry = () => {
|
||||
const sanitizers = {
|
||||
'content-api': {
|
||||
input: [],
|
||||
output: [],
|
||||
},
|
||||
};
|
||||
const sanitizers = {};
|
||||
|
||||
return {
|
||||
get(path) {
|
||||
return _.get(sanitizers, path);
|
||||
return _.get(sanitizers, path, []);
|
||||
},
|
||||
add(path, sanitizer) {
|
||||
this.get(path).push(sanitizer);
|
||||
return this;
|
||||
},
|
||||
set(path, value = []) {
|
||||
_.set(sanitizers, path, value);
|
||||
return this;
|
||||
},
|
||||
has(path) {
|
||||
return _.has(sanitizers, path);
|
||||
|
||||
@ -29,10 +29,9 @@ module.exports = {
|
||||
}
|
||||
|
||||
// Apply sanitizers from registry if exists
|
||||
const sanitizersRegistry = strapi.container.get('sanitizers').get('content-api.input');
|
||||
if (Array.isArray(sanitizersRegistry)) {
|
||||
sanitizersRegistry.forEach(sanitizer => transforms.push(sanitizer(schema)));
|
||||
}
|
||||
strapi.sanitizers
|
||||
.get('content-api.input')
|
||||
.forEach(sanitizer => transforms.push(sanitizer(schema)));
|
||||
|
||||
return pipeAsync(...transforms)(data);
|
||||
},
|
||||
@ -49,10 +48,9 @@ module.exports = {
|
||||
}
|
||||
|
||||
// Apply sanitizers from registry if exists
|
||||
const sanitizersRegistry = strapi.container.get('sanitizers').get('content-api.output');
|
||||
if (Array.isArray(sanitizersRegistry)) {
|
||||
sanitizersRegistry.forEach(sanitizer => transforms.push(sanitizer(schema)));
|
||||
}
|
||||
strapi.sanitizers
|
||||
.get('content-api.output')
|
||||
.forEach(sanitizer => transforms.push(sanitizer(schema)));
|
||||
|
||||
return pipeAsync(...transforms)(data);
|
||||
},
|
||||
|
||||
@ -5,7 +5,7 @@ const sanitizers = require('./utils/sanitize/sanitizers');
|
||||
|
||||
module.exports = ({ strapi }) => {
|
||||
strapi.container.get('auth').register('content-api', authStrategy);
|
||||
strapi.container.get('sanitizers').add('content-api.output', sanitizers.defaultSanitizeOutput);
|
||||
strapi.sanitizers.add('content-api.output', sanitizers.defaultSanitizeOutput);
|
||||
|
||||
if (strapi.plugin('graphql')) {
|
||||
require('./graphql')({ strapi });
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user