Skip to content

v3: Adding SDK functions for triggering tasks in a typesafe way #1177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 24, 2024

Conversation

ericallam
Copy link
Member

@ericallam ericallam commented Jun 24, 2024

You can now trigger tasks in a typesafe way, without importing the task file at run time:

import { tasks } from "@trigger.dev/sdk/v3";
import type { emailSequence } from "~/trigger/emails";
//     👆 **type-only** import

//app/email/route.ts
export async function POST(request: Request) {
  //get the JSON from the request
  const data = await request.json();

  // Pass the task type to `trigger()` as a generic argument, giving you full type checking
  const handle = await tasks.trigger<typeof emailSequence>("email-sequence", {
    to: data.email,
    name: data.name,
  });

  //return a success response with the handle
  return Response.json(handle);
}

You can also now use the handle returned from tasks.trigger or myTask.trigger to retrieve the output of a run in a typesafe manner:

import { tasks, runs } from "@trigger.dev/sdk/v3";
import type { emailSequence } from "~/trigger/emails";

const handle = await tasks.trigger<typeof emailSequence>("email-sequence", {
  to: data.email,
  name: data.name,
});

const run = await runs.retrieve(handle); // Pass in the handle here, not handle.id
const completedRun = await runs.poll(handle, { pollIntervalMs: 5000 )); // Poll for the run until it's completed

console.log(run.output); // run.output is typed to the return value of `emailSequence` task
console.log(completedRun.output); // completedRun.output is typed to the return value of `emailSequence` task

You can also combine tasks.trigger and run.poll into a single call, triggerAndPoll:

import { tasks, runs } from "@trigger.dev/sdk/v3";
import type { emailSequence } from "~/trigger/emails";

const completedRun = await tasks.triggerAndPoll<typeof emailSequence>("email-sequence", {
  to: data.email,
  name: data.name,
}, { pollIntervalMs: 5000 });

console.log(completedRun.output); // completedRun.output is typed to the return value of `emailSequence` task

Type helpers

This PR also exports some new handy type helpers to extract the type information embedded in a task instance:

import { TaskOutput, TaskPayload, TaskIdentifier } from "@trigger.dev/sdk/v3";
import { emailSequence } from "./trigger/emails";

type emailSequencePayload = TaskPayload<typeof emailSequence>; // retrieves the payload type of the task
type emailSequenceOutput = TaskOutput<typeof emailSequence>; // retrieves the output type of the task
type emailSequenceIdentifier = TaskIdentifier<typeof emailSequence>; // retrieves the identifier of the task
type emailSequenceHandle = TaskOutputHandle<typeof emailSequence>; // retrieves the handle of the task

Copy link

changeset-bot bot commented Jun 24, 2024

🦋 Changeset detected

Latest commit: 271eb54

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ericallam ericallam requested a review from matt-aitken June 24, 2024 08:15
@ericallam ericallam merged commit 7c36a1a into main Jun 24, 2024
2 checks passed
@nicktrn nicktrn deleted the v3/tasks-trigger-functions branch July 17, 2024 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant