Hi all!
Would anyone know how data loaded by an API call can be processed with custom code, functions, formula… ideally in JavaScript, otherwise something very flexible, before being used by an element?
Let’s say I’m loading data from an API that’s not in the format expected by an element (e.g. a chart element), but contains everything I need. If possible, I’d like to avoid proxying the data through another API just to transform it.
How can I transform it? Map, process… whatever makes my data in the format expected by the element.
Having some JavaScript code in a plugin is fine, but the API calls section is only pure configuration, there is no JavaScript we can add to transform it (it would be a great feature to add!). Or, when using this data in the properties panel of my element, having a way to input custom JS would work as well, but I haven’t found an equivalent.
To illustrate, let’s say I want to apply below data transformation: a JSON.parse + map all values of an object.
// API response with raw data I need to format
let apiResponse = {
description: 'this is a lowercase sentence.',
raw: '{"a":1,"b":2,"c":3}'
};
// Process the data
apiResponse.description = capitalize(apiResponse.description);
apiResponse.parsed = JSON.parse(apiResponse.raw);
for (const [key, value] of Object.entries(apiResponse.parsed)) {
apiResponse.parsed[key] = value ** 2;
}
console.log(apiResponse);
// Output:
// {
// description:"This is a lowercase sentence.",
// raw:"{"a":1,"b":2,"c":3}",
// parsed: {
// a:1,
// b:4,
// c:9
// }
// }
Thanks a lot!