- Introduced a new contract for script returns, requiring an object with `output`, `context`, and `skipRemaining` fields. - Refactored existing scripts to align with the new return structure, ensuring compatibility with the updated context management. - Added utility functions for normalizing context and step results, improving the handling of script execution context. - Updated various scripts (e.g., fetch-binary, fetch-html, fetch-http) to return structured output and context, enhancing data flow and usability. - Improved error handling for script returns, ensuring clearer feedback when invalid structures are returned. This update significantly enhances the flexibility and clarity of script interactions within workflows.
37 lines
1.0 KiB
JavaScript
37 lines
1.0 KiB
JavaScript
import jsonata from "jsonata";
|
|
|
|
async function jsonataFn(ctx) {
|
|
log.info({ ctx }, "jsonata: context");
|
|
log.info("jsonata: evaluating expression %s", ctx.config.expression);
|
|
const expression = jsonata(ctx.config.expression);
|
|
const result = await expression.evaluate(ctx);
|
|
log.info({ result }, "jsonata: expression result");
|
|
return {
|
|
output: result,
|
|
context:
|
|
ctx.context != null && typeof ctx.context === "object" && !Array.isArray(ctx.context)
|
|
? ctx.context
|
|
: {},
|
|
};
|
|
}
|
|
|
|
jsonataFn.meta = {
|
|
description:
|
|
"Evaluate a JSONata expression against the full ctx (data, context, config); the result is the next data",
|
|
previewConfigKey: "expression",
|
|
reads: "ctx",
|
|
config: {
|
|
expression: { type: "string", required: true, description: "JSONata expression against ctx" },
|
|
},
|
|
input: {},
|
|
output: {},
|
|
example: {
|
|
data: { title: "Hello", url: "https://example.com" },
|
|
config: {
|
|
expression: '{"title": data.title, "message": data.title, "attach": data.url}',
|
|
},
|
|
},
|
|
};
|
|
|
|
export default jsonataFn;
|