# 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](https://www.npmjs.com/package/ejs) to hydrate template string.

[EJS: Documentation](https://ejs.co/)

### 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

```javascript
const { getEnrichedData } = require('@teamfabric/xpm')

/**
 * Hydrates component data and returns it.
 * @param {Object} componentData The component data possibly contains template string.
 * @param {Object} data The data to map template string.
 * @param {Object} schema The schema validates data type.
 * @returns {Object} Hydrated or none-hydrated component data
 */
const data = getEnrichedData({ componentData, data, schema })
```
