Skip to content

Commit 3ee7cd6

Browse files
feat: parse callback result with zod before return (#342)
1 parent 74686c0 commit 3ee7cd6

File tree

1 file changed

+7
-4
lines changed
  • packages/trigger-sdk/src

1 file changed

+7
-4
lines changed

packages/trigger-sdk/src/io.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
SendEvent,
1313
SendEventOptions,
1414
SerializableJson,
15+
SerializableJsonSchema,
1516
ServerTask,
1617
UpdateTriggerSourceBody,
1718
} from "@trigger.dev/core";
@@ -431,10 +432,10 @@ export class IO {
431432
= * @param onError The callback that will be called when the Task fails. The callback receives the error, the Task and the IO as parameters. If you wish to retry then return an object with a `retryAt` property.
432433
* @returns A Promise that resolves with the returned value of the callback.
433434
*/
434-
async runTask<TResult extends SerializableJson | void = void>(
435+
async runTask<TResult extends SerializableJson | void = void, TCallbackResult extends unknown = TResult>(
435436
key: string | any[],
436437
options: RunTaskOptions,
437-
callback: (task: IOTask, io: IO) => Promise<TResult>,
438+
callback: (task: IOTask, io: IO) => Promise<TCallbackResult | TResult>,
438439
onError?: (
439440
error: unknown,
440441
task: IOTask,
@@ -523,21 +524,23 @@ export class IO {
523524
try {
524525
const result = await callback(task, this);
525526

527+
const output = SerializableJsonSchema.parse(result) as TResult;
528+
526529
this._logger.debug("Completing using output", {
527530
idempotencyKey,
528531
task,
529532
});
530533

531534
const completedTask = await this._apiClient.completeTask(this._id, task.id, {
532-
output: result ?? undefined,
535+
output: output ?? undefined,
533536
properties: task.outputProperties ?? undefined,
534537
});
535538

536539
if (completedTask.status === "CANCELED") {
537540
throw new CanceledWithTaskError(completedTask);
538541
}
539542

540-
return result;
543+
return output;
541544
} catch (error) {
542545
if (isTriggerError(error)) {
543546
throw error;

0 commit comments

Comments
 (0)