2021-11-04 15:47:53 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { isArray } = require('lodash/fp');
|
|
|
|
|
|
|
|
const traverseEntity = require('../traverse-entity');
|
|
|
|
const { getNonWritableAttributes } = require('../content-types');
|
2021-11-04 16:43:27 +01:00
|
|
|
const pipeAsync = require('../pipe-async');
|
|
|
|
|
2021-11-04 15:47:53 +01:00
|
|
|
const visitors = require('./visitors');
|
2021-11-10 17:08:54 +01:00
|
|
|
const sanitizers = require('./sanitizers');
|
2021-11-04 15:47:53 +01:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
contentAPI: {
|
|
|
|
input(data, schema, { auth } = {}) {
|
|
|
|
if (isArray(data)) {
|
|
|
|
return Promise.all(data.map(entry => this.input(entry, schema, { auth })));
|
|
|
|
}
|
|
|
|
|
|
|
|
const nonWritableAttributes = getNonWritableAttributes(schema);
|
|
|
|
|
|
|
|
const transforms = [
|
|
|
|
// Remove non writable attributes
|
|
|
|
traverseEntity(visitors.restrictedFields(nonWritableAttributes), { schema }),
|
|
|
|
];
|
|
|
|
|
|
|
|
if (auth) {
|
|
|
|
// Remove restricted relations
|
|
|
|
transforms.push(traverseEntity(visitors.removeRestrictedRelations(auth), { schema }));
|
|
|
|
}
|
|
|
|
|
2021-11-04 16:43:27 +01:00
|
|
|
return pipeAsync(...transforms)(data);
|
2021-11-04 15:47:53 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
output(data, schema, { auth } = {}) {
|
|
|
|
if (isArray(data)) {
|
|
|
|
return Promise.all(data.map(entry => this.output(entry, schema, { auth })));
|
|
|
|
}
|
|
|
|
|
2022-03-03 22:45:55 +09:00
|
|
|
const transforms = [
|
|
|
|
sanitizers.defaultSanitizeOutput(schema),
|
|
|
|
sanitizers.sanitizeUserRelationFromRoleEntities(schema),
|
|
|
|
];
|
2021-11-04 15:47:53 +01:00
|
|
|
|
|
|
|
if (auth) {
|
|
|
|
transforms.push(traverseEntity(visitors.removeRestrictedRelations(auth), { schema }));
|
|
|
|
}
|
|
|
|
|
2021-11-04 16:43:27 +01:00
|
|
|
return pipeAsync(...transforms)(data);
|
2021-11-04 15:47:53 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2021-11-10 17:08:54 +01:00
|
|
|
sanitizers,
|
2021-11-04 15:47:53 +01:00
|
|
|
visitors,
|
|
|
|
};
|