@webiny/api-headless-cms-ddb
Version:
DynamoDB storage operations plugin for Headless CMS API.
108 lines (106 loc) • 2.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createFields = void 0;
var _systemFields = require("./systemFields");
var _plugins = require("../../../plugins");
var _mapPlugins = require("./mapPlugins");
const createFieldCollection = params => {
const {
fields,
parents,
transformValuePlugins,
valuePathPlugins,
system
} = params;
return fields.reduce((collection, field) => {
const transformPlugin = transformValuePlugins[field.type];
const valuePathPlugin = valuePathPlugins[field.type];
const basePath = system ? [] : ["values"];
/**
* The required fieldId is a product of all of its parents and its own fieldId.
*/
const fieldId = [...parents, {
fieldId: field.fieldId,
multipleValues: field.multipleValues
}].map(f => f.fieldId).join(".");
collection[fieldId] = {
...field,
parents,
system,
createPath: params => {
if (valuePathPlugin && valuePathPlugin.canUse(field, parents.map(p => p.fieldId))) {
return valuePathPlugin.createPath(params);
}
return basePath.concat(parents.map(parent => parent.fieldId)).concat([params.field.fieldId]).join(".");
},
transform: value => {
if (!transformPlugin) {
return value;
}
return transformPlugin.transform({
field,
value
});
}
};
const childFields = field.settings?.fields;
if (!childFields?.length) {
return collection;
}
const result = createFieldCollection({
fields: childFields,
parents: [...parents, {
fieldId: field.fieldId,
multipleValues: field.multipleValues
}],
transformValuePlugins,
valuePathPlugins,
system
});
Object.assign(collection, result);
return collection;
}, {});
};
/**
* This method will map the fieldId (fieldId -> field) to the actual field.
*
* In case of nested fields, fieldId is all the parent fieldIds + current one, joined by the dot (.).
*/
const createFields = params => {
const {
fields,
plugins
} = params;
const transformValuePlugins = (0, _mapPlugins.getMappedPlugins)({
plugins,
type: "cms-field-filter-value-transform",
property: "fieldType"
});
const valuePathPlugins = (0, _mapPlugins.getMappedPlugins)({
plugins,
type: _plugins.CmsEntryFieldFilterPathPlugin.type,
property: "fieldType"
});
const collection = createFieldCollection({
fields: (0, _systemFields.createSystemFields)(),
transformValuePlugins,
valuePathPlugins,
parents: [],
system: true
});
const result = createFieldCollection({
fields,
transformValuePlugins,
valuePathPlugins,
parents: [],
system: false
});
return {
...collection,
...result
};
};
exports.createFields = createFields;
//# sourceMappingURL=createFields.js.map