2021-11-04 15:47:53 +01:00
|
|
|
'use strict';
|
|
|
|
|
2023-03-21 18:37:21 +01:00
|
|
|
const { clone, isObject, isArray, isNil, curry } = require('lodash/fp');
|
|
|
|
|
|
|
|
const traverseMorphRelationTarget = async (visitor, path, entry) => {
|
|
|
|
const targetSchema = strapi.getModel(entry.__type);
|
|
|
|
|
|
|
|
const traverseOptions = { schema: targetSchema, path };
|
|
|
|
|
|
|
|
return traverseEntity(visitor, traverseOptions, entry);
|
|
|
|
};
|
|
|
|
|
|
|
|
const traverseRelationTarget = (schema) => async (visitor, path, entry) => {
|
|
|
|
const traverseOptions = { schema, path };
|
|
|
|
|
|
|
|
return traverseEntity(visitor, traverseOptions, entry);
|
|
|
|
};
|
|
|
|
|
|
|
|
const traverseMediaTarget = async (visitor, path, entry) => {
|
|
|
|
const targetSchemaUID = 'plugin::upload.file';
|
|
|
|
const targetSchema = strapi.getModel(targetSchemaUID);
|
|
|
|
|
|
|
|
const traverseOptions = { schema: targetSchema, path };
|
|
|
|
|
|
|
|
return traverseEntity(visitor, traverseOptions, entry);
|
|
|
|
};
|
|
|
|
|
|
|
|
const traverseComponent = async (visitor, path, schema, entry) => {
|
|
|
|
const traverseOptions = { schema, path };
|
|
|
|
|
|
|
|
return traverseEntity(visitor, traverseOptions, entry);
|
|
|
|
};
|
|
|
|
|
|
|
|
const visitDynamicZoneEntry = async (visitor, path, entry) => {
|
|
|
|
const targetSchema = strapi.getModel(entry.__component);
|
|
|
|
const traverseOptions = { schema: targetSchema, path };
|
|
|
|
|
|
|
|
return traverseEntity(visitor, traverseOptions, entry);
|
|
|
|
};
|
2021-11-04 15:47:53 +01:00
|
|
|
|
|
|
|
const traverseEntity = async (visitor, options, entity) => {
|
2023-02-09 11:35:50 +01:00
|
|
|
const { path = { raw: null, attribute: null }, schema } = options;
|
2021-11-04 15:47:53 +01:00
|
|
|
|
|
|
|
// End recursion
|
|
|
|
if (!isObject(entity) || isNil(schema)) {
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't mutate the original entity object
|
2023-03-21 18:37:21 +01:00
|
|
|
// only clone at 1st level as the next level will get clone when traversed
|
|
|
|
const copy = clone(entity);
|
|
|
|
const visitorUtils = createVisitorUtils({ data: copy });
|
2021-11-04 15:47:53 +01:00
|
|
|
|
2023-03-21 18:37:21 +01:00
|
|
|
const keys = Object.keys(copy);
|
|
|
|
for (let i = 0; i < keys.length; i += 1) {
|
|
|
|
const key = keys[i];
|
2021-11-04 15:47:53 +01:00
|
|
|
// Retrieve the attribute definition associated to the key from the schema
|
|
|
|
const attribute = schema.attributes[key];
|
|
|
|
|
|
|
|
// If the attribute doesn't exist within the schema, ignore it
|
|
|
|
if (isNil(attribute)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-02-09 11:35:50 +01:00
|
|
|
const newPath = { ...path };
|
|
|
|
|
|
|
|
newPath.raw = isNil(path.raw) ? key : `${path.raw}.${key}`;
|
|
|
|
|
|
|
|
if (!isNil(attribute)) {
|
|
|
|
newPath.attribute = isNil(path.attribute) ? key : `${path.attribute}.${key}`;
|
|
|
|
}
|
2021-11-04 15:47:53 +01:00
|
|
|
|
|
|
|
// Visit the current attribute
|
|
|
|
const visitorOptions = { data: copy, schema, key, value: copy[key], attribute, path: newPath };
|
|
|
|
|
|
|
|
await visitor(visitorOptions, visitorUtils);
|
|
|
|
|
|
|
|
// Extract the value for the current key (after calling the visitor)
|
|
|
|
const value = copy[key];
|
|
|
|
|
|
|
|
// Ignore Nil values
|
|
|
|
if (isNil(value)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isRelation = attribute.type === 'relation';
|
|
|
|
const isComponent = attribute.type === 'component';
|
|
|
|
const isDynamicZone = attribute.type === 'dynamiczone';
|
2022-03-08 17:15:43 +01:00
|
|
|
const isMedia = attribute.type === 'media';
|
2021-11-04 15:47:53 +01:00
|
|
|
|
|
|
|
if (isRelation) {
|
|
|
|
const isMorphRelation = attribute.relation.toLowerCase().startsWith('morph');
|
|
|
|
|
2023-03-21 18:37:21 +01:00
|
|
|
const method = isMorphRelation
|
|
|
|
? traverseMorphRelationTarget
|
|
|
|
: traverseRelationTarget(strapi.getModel(attribute.target));
|
2021-11-04 15:47:53 +01:00
|
|
|
|
2023-03-21 18:37:21 +01:00
|
|
|
if (isArray(value)) {
|
|
|
|
const res = new Array(value.length);
|
|
|
|
for (let i = 0; i < value.length; i += 1) {
|
|
|
|
res[i] = await method(visitor, newPath, value[i]);
|
|
|
|
}
|
|
|
|
copy[key] = res;
|
|
|
|
} else {
|
|
|
|
copy[key] = await method(visitor, newPath, value);
|
|
|
|
}
|
2021-11-04 15:47:53 +01:00
|
|
|
|
2023-03-21 18:37:21 +01:00
|
|
|
continue;
|
2022-03-08 17:15:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isMedia) {
|
|
|
|
// need to update copy
|
2023-03-21 18:37:21 +01:00
|
|
|
if (isArray(value)) {
|
|
|
|
const res = new Array(value.length);
|
|
|
|
for (let i = 0; i < value.length; i += 1) {
|
|
|
|
res[i] = await traverseMediaTarget(visitor, newPath, value[i]);
|
|
|
|
}
|
|
|
|
copy[key] = res;
|
|
|
|
} else {
|
|
|
|
copy[key] = await traverseMediaTarget(visitor, newPath, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
2021-11-04 15:47:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isComponent) {
|
|
|
|
const targetSchema = strapi.getModel(attribute.component);
|
|
|
|
|
2023-03-21 18:37:21 +01:00
|
|
|
if (isArray(value)) {
|
|
|
|
const res = new Array(value.length);
|
|
|
|
for (let i = 0; i < value.length; i += 1) {
|
|
|
|
res[i] = await traverseComponent(visitor, newPath, targetSchema, value[i]);
|
|
|
|
}
|
|
|
|
copy[key] = res;
|
|
|
|
} else {
|
|
|
|
copy[key] = await traverseComponent(visitor, newPath, targetSchema, value);
|
|
|
|
}
|
2021-11-04 15:47:53 +01:00
|
|
|
|
2023-03-21 18:37:21 +01:00
|
|
|
continue;
|
2021-11-04 15:47:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isDynamicZone && isArray(value)) {
|
2023-03-21 18:37:21 +01:00
|
|
|
const res = new Array(value.length);
|
|
|
|
for (let i = 0; i < value.length; i += 1) {
|
|
|
|
res[i] = await visitDynamicZoneEntry(visitor, newPath, value[i]);
|
|
|
|
}
|
|
|
|
copy[key] = res;
|
2021-11-04 15:47:53 +01:00
|
|
|
|
2023-03-21 18:37:21 +01:00
|
|
|
continue;
|
2021-11-04 15:47:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
};
|
|
|
|
|
|
|
|
const createVisitorUtils = ({ data }) => ({
|
|
|
|
remove(key) {
|
|
|
|
delete data[key];
|
|
|
|
},
|
|
|
|
|
|
|
|
set(key, value) {
|
|
|
|
data[key] = value;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = curry(traverseEntity);
|