lib-xpm
getEnrichedData
getEnrichedData
function hydrates component's content (component data) by replacing template string with provided data. The output is hydrated/non-hydrated data.
For example, if we have a component with content that says "TODAY ONLY: {%= product.title %} IS 50% OFF!", it returns "TODAY ONLY: HAIRBRUSH IS 50% OFF!" by replacing {%= product.title %}
with HAIRBRUSH
.
Dependency
getEnrichData
function uses npm package EJS to hydrate template string.
EJS: Documentation
Behaviors
Returns hydrated data if these conditions are satisfied
- Data validation against schema passes
- Component data contains template string
- Data contains corresponding value to each template string
Returns component data as it is when
- Provided component data doesn't contain template string
Throws error in these cases
- Data validation against schema fails
- Invalid component data (null | empty object) is provided
Usage Example
const { getEnrichedData } = require('@teamfabric/xpm')
const data = getEnrichedData({ componentData, data, schema })