diff --git a/.changeset/ten-llamas-develop.md b/.changeset/ten-llamas-develop.md new file mode 100644 index 000000000..f7f781be8 --- /dev/null +++ b/.changeset/ten-llamas-develop.md @@ -0,0 +1,5 @@ +--- +"@trigger.dev/sdk": patch +--- + +Adding some type helpers for getting the payload and IO types from jobs and triggers diff --git a/examples/nextjs-example/src/jobs/supabase.ts b/examples/nextjs-example/src/jobs/supabase.ts index c9063e730..ee45838d2 100644 --- a/examples/nextjs-example/src/jobs/supabase.ts +++ b/examples/nextjs-example/src/jobs/supabase.ts @@ -1,6 +1,14 @@ import { Database } from "@/supabase.types"; import { client } from "@/trigger"; -import { Job, eventTrigger } from "@trigger.dev/sdk"; +import { + type IntegrationIO, + Job, + eventTrigger, + type JobPayload, + type JobIO, + type TriggerPayload, + type IOWithIntegrations, +} from "@trigger.dev/sdk"; import { SupabaseManagement, Supabase } from "@trigger.dev/supabase"; import { z } from "zod"; @@ -25,6 +33,15 @@ const supabaseDB = new Supabase({ supabaseKey: process.env.SUPABASE_KEY!, }); +async function doPlaygroundStuff( + io: IntegrationIO, + ref: string +) { + await io.getPGConfig("get-pg-config", { + ref, + }); +} + new Job(client, { id: "supabase-playground", name: "Supabase Playground", @@ -87,7 +104,7 @@ new Job(client, { }, }); -new Job(client, { +const createTodoJob = new Job(client, { id: "supabase-create-todo", name: "Supabase Create Todo", version: "0.1.1", @@ -126,20 +143,63 @@ new Job(client, { }, }); +type CreateTodo = JobPayload; +type CreateTodoIO = JobIO; + +async function runCreateTodo(payload: CreateTodo, io: CreateTodoIO) { + const newTodo = await io.supabaseDB.runTask( + "create-todo", + async (db) => { + const { data, error } = await db + .from("todos") + .insert({ + contents: payload.contents, + user_id: payload.user_id, + is_complete: false, + }) + .select(); + + if (error) throw error; + + return data; + }, + { + name: "Create Todo", + properties: [{ label: "Contents", text: payload.contents }], + } + ); +} + +const createProjectTrigger = eventTrigger({ + name: "supabase.create", + schema: z.object({ + name: z.string(), + organization_id: z.string(), + plan: z.enum(["free", "pro"]), + region: z.enum(["us-east-1", "us-west-1"]), + password: z.string(), + }), +}); + +async function doRun( + payload: TriggerPayload, + io: IOWithIntegrations<{ supabase: typeof supabase }> +) { + await io.supabase.createProject("create-project", { + name: payload.name, + organization_id: payload.organization_id, + plan: payload.plan, + region: payload.region, + kps_enabled: true, + db_pass: payload.password, + }); +} + new Job(client, { id: "supabase-create-project", name: "Supabase Create Project", version: "0.1.1", - trigger: eventTrigger({ - name: "supabase.create", - schema: z.object({ - name: z.string(), - organization_id: z.string(), - plan: z.enum(["free", "pro"]), - region: z.enum(["us-east-1", "us-west-1"]), - password: z.string(), - }), - }), + trigger: createProjectTrigger, integrations: { supabase, }, diff --git a/integrations/supabase/src/management/tasks.ts b/integrations/supabase/src/management/tasks.ts index eb9ccef55..ed15c3f74 100644 --- a/integrations/supabase/src/management/tasks.ts +++ b/integrations/supabase/src/management/tasks.ts @@ -161,6 +161,7 @@ export const getPostgRESTConfig: AuthenticatedTask< }, }; +/** Gets project's Postgres config */ export const getPGConfig: AuthenticatedTask< SupabaseManagementAPI, { ref: string }, diff --git a/packages/trigger-sdk/src/integrations.ts b/packages/trigger-sdk/src/integrations.ts index 327187cb1..fa68b931b 100644 --- a/packages/trigger-sdk/src/integrations.ts +++ b/packages/trigger-sdk/src/integrations.ts @@ -113,6 +113,10 @@ type ExtractIntegrationClient< > = ExtractIntegrationClientClient & ExtractTasks; +export type IntegrationIO< + TIntegration extends TriggerIntegration> +> = ExtractIntegrationClient; + type ExtractIntegrations< TIntegrations extends Record< string, diff --git a/packages/trigger-sdk/src/job.ts b/packages/trigger-sdk/src/job.ts index f9162683c..2843785fe 100644 --- a/packages/trigger-sdk/src/job.ts +++ b/packages/trigger-sdk/src/job.ts @@ -77,6 +77,17 @@ export type JobOptions< ) => Promise; }; +export type JobPayload = TJob extends Job< + Trigger>, + any +> + ? TEvent + : never; + +export type JobIO = TJob extends Job + ? IOWithIntegrations + : never; + /** A [Job](https://trigger.dev/docs/documentation/concepts/jobs) is used to define the [Trigger](https://trigger.dev/docs/documentation/concepts/triggers), metadata, and what happens when it runs. */ export class Job< TTrigger extends Trigger>, diff --git a/packages/trigger-sdk/src/types.ts b/packages/trigger-sdk/src/types.ts index 5222a3839..1b9010563 100644 --- a/packages/trigger-sdk/src/types.ts +++ b/packages/trigger-sdk/src/types.ts @@ -67,6 +67,12 @@ export interface Trigger> { preprocessRuns: boolean; } +export type TriggerPayload = TTrigger extends Trigger< + EventSpecification +> + ? TEvent + : never; + export type EventSpecificationExample = { id: string; name: string;