Skip to main content

Result Agent - JS/TS

Finalise task result in aggregator logic.

Logic TypeAvailable
Generic logic
Aggregator logic

The task result will be part of the execution result, and will be returned to user if the trigger type is synchronized API route or message queue.

Import and Usage

import {
ResultAgent,
} from "@fstnetwork/loc-logic-sdk";

export async function run(ctx) {
ResultAgent.finalize({
status: "ok",
});
}

export async function handleError(ctx, error) {
ResultAgent.finalize({
status: "error",
});
}

Class Reference

Type

  • ResultAgent
  • IResultAgent (alias)

Method: Finalise Task Result

finalize(value: object): IResultAgent
ParameterDescription
valueA JavaScript JSON object

Write an JSON object as the content of task result, which will be part of the execution result and may be returned to the user. The JSON data may be converted to other formats depending on the trigger configuration.

warning

If an execution contains multiple tasks, fields with the same name in different task results will be overwritten. Consider to use unique field names to avoid the issue.

Runtime will throw a JSON parsing error if the object cannot be serialised properly to JSON. You can try using JSON.parse(JSON.stringify(object) to transform an object with methods into a proper JSON object, although some fields may be different or discarded.

Method: Set HTTP Status Code

httpStatusCode(statusCode: number): IResultAgent
ParameterDescription
statusCodeHTTP status code (default: 200)

Set HTTP status code for the HTTP response when the trigger is API route.

info

The HTTP code will also be applied to the _status field in the execution result if the execution is invoked by an actual API route. The _status field will always 20x in manual executions.

The task will still return status code 202 for timed out execution.

Setting HTTP status code does not affect triggers other than API routes.

If an execution contains multiple tasks, the highest HTTP status code value will be used for the API route response.

Examples

Finalise Task Result

ResultAgent.finalize({
status: "ok",
taskId: ctx.task.taskKey.taskId,
data: {
name: "Arthur Dent",
age: 42,
job: {
title: "Sandwich-maker",
salary: 0,
},
quotes: [
"Is there any tea on this spaceship?",
"This must be Thursday. I never could get the hang of Thursdays.",
"Would it save you a lot of time if I just gave up and went mad now?",
],
},
});
info

The field status: "ok" will be part of the task result, not to be confused with HTTP status code in the HTTP response.

Finalise Task Result and Set HTTP Status Code

ResultAgent.finalize({
// ...
}).httpStatusCode(200);
tip

Since all methods return the Result Agent itself, they can be chain in different order or invoked separately:

ResultAgent.httpStatusCode(200).finalize({
// ...
});

// or

ResultAgent.finalize({
// ...
};)
ResultAgent.httpStatusCode(200);

Finalise Task Result with Error

ResultAgent.finalize({
status: "error",
taskId: ctx.task.taskKey.taskId,
error: error.message,
stack: error.stack,
}).httpStatusCode(500);