Skip to content

devmasx/payload-transformation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Payload transformation

Using a yaml configuration you can transform a payload to another payload

Example:

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"
}

Install

npm install payload-transformation

Usage

import { AdapterService } from 'payload-transformation';

const output = new AdapterService(config, input).run();

Available operators

$or

Apply an or operation between context values

billingAddress:
  $or: [addresses.billing, addresses.main]

equivalent:

const billingAddress = context.addresses?.billing || context.addresses?.main;

$concat

Concat context values

Example:

fullName:
  $concat: [name, ' ', lastName]

equivalent:

const fullName = `${context.name} ${$context.lastName}`;

$value

Return the harcoded value without read from the context

Example:

country:
  $value: US

equivalent:

const country = 'US';

$fnc

Call a function with the context

roles:
  $fnc: fetchRoles

Initialization:

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);

Custom operation

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: budget
const 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);

About

Yaml syntax for transform a json payload

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published