Skip to content

Commit

Permalink
Refactor sanitizers module to be maintained easily
Browse files Browse the repository at this point in the history
  • Loading branch information
iicdii committed May 7, 2022
1 parent 4429381 commit 5959788
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 16 deletions.
5 changes: 5 additions & 0 deletions packages/core/strapi/lib/Strapi.js
Expand Up @@ -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());
Expand All @@ -321,6 +325,7 @@ class Strapi {
async register() {
await Promise.all([
this.loadApp(),
this.loadSanitizers(),
this.loadPlugins(),
this.loadAdmin(),
this.loadAPIs(),
Expand Down
1 change: 1 addition & 0 deletions packages/core/strapi/lib/core/loaders/index.js
Expand Up @@ -8,4 +8,5 @@ module.exports = {
loadPolicies: require('./policies'),
loadPlugins: require('./plugins'),
loadAdmin: require('./admin'),
loadSanitizers: require('./sanitizers'),
};
5 changes: 5 additions & 0 deletions packages/core/strapi/lib/core/loaders/sanitizers.js
@@ -0,0 +1,5 @@
'use strict';

module.exports = strapi => {
strapi.container.get('sanitizers').set('content-api', { input: [], output: [] });
};
14 changes: 7 additions & 7 deletions packages/core/strapi/lib/core/registries/sanitizers.js
Expand Up @@ -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);
Expand Down
14 changes: 6 additions & 8 deletions packages/core/utils/lib/sanitize/index.js
Expand Up @@ -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);
},
Expand All @@ -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);
},
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/users-permissions/server/register.js
Expand Up @@ -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 });
Expand Down

0 comments on commit 5959788

Please sign in to comment.