Using a yaml configuration you can transform a payload to another payload
config:
mapping:
name: name
last_name: lastName
full_name:
$concat: [name, ' ', lastname]input:
{
"name": "Jhon",
"lastName": "Doe"
}output:
{
"name": "Jhon",
"last_name": "Doe",
"full_name": "Jhon Doe"
}npm install payload-transformation
import { AdapterService } from 'payload-transformation';
const output = new AdapterService(config, input).run();Apply an or operation between context values
billingAddress:
$or: [addresses.billing, addresses.main]equivalent:
const billingAddress = context.addresses?.billing || context.addresses?.main;Concat context values
Example:
fullName:
$concat: [name, ' ', lastName]equivalent:
const fullName = `${context.name} ${$context.lastName}`;Return the harcoded value without read from the context
Example:
country:
$value: USequivalent:
const country = 'US';Call a function with the context
roles:
$fnc: fetchRolesInitialization:
const functions = {
fetchRoles(context) {
return context.roles.split(',');
},
};
const adapter = new AdapterService(config, context, {
functions,
});equivalent:
function fetchRoles(context) {
return context.roles.split(',');
}
const roles = fetchRoles(context);The yaml syntax allow add a custom operation, this custom operation is ussefull when you need a call a function with an specific context value.
Example:
budget: 100
budget_in_cents:
$to_cents: budgetconst processors = {
$to_cents: function (contextValue) {
return contextValue * 1000;
},
};
const adapter = new AdapterService(config, context, {
processors,
});equivalent:
function toCents(value) {
return value * 1000;
}
const budget_in_cents = toCents(context.budget);