@trigger.dev/sdk: Adding some type helpers for getting the payload and IO types from jobs and triggers

This commit is contained in:
Eric Allam
2023-07-28 14:42:28 +01:00
parent 8b09dfd697
commit 64477f6b3a
6 changed files with 99 additions and 12 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/sdk": patch
---
Adding some type helpers for getting the payload and IO types from jobs and triggers
+72 -12
View File
@@ -1,6 +1,14 @@
import { Database } from "@/supabase.types"; import { Database } from "@/supabase.types";
import { client } from "@/trigger"; 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 { SupabaseManagement, Supabase } from "@trigger.dev/supabase";
import { z } from "zod"; import { z } from "zod";
@@ -25,6 +33,15 @@ const supabaseDB = new Supabase<Database>({
supabaseKey: process.env.SUPABASE_KEY!, supabaseKey: process.env.SUPABASE_KEY!,
}); });
async function doPlaygroundStuff(
io: IntegrationIO<typeof supabaseManagementKey>,
ref: string
) {
await io.getPGConfig("get-pg-config", {
ref,
});
}
new Job(client, { new Job(client, {
id: "supabase-playground", id: "supabase-playground",
name: "Supabase Playground", name: "Supabase Playground",
@@ -87,7 +104,7 @@ new Job(client, {
}, },
}); });
new Job(client, { const createTodoJob = new Job(client, {
id: "supabase-create-todo", id: "supabase-create-todo",
name: "Supabase Create Todo", name: "Supabase Create Todo",
version: "0.1.1", version: "0.1.1",
@@ -126,20 +143,63 @@ new Job(client, {
}, },
}); });
type CreateTodo = JobPayload<typeof createTodoJob>;
type CreateTodoIO = JobIO<typeof createTodoJob>;
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<typeof createProjectTrigger>,
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, { new Job(client, {
id: "supabase-create-project", id: "supabase-create-project",
name: "Supabase Create Project", name: "Supabase Create Project",
version: "0.1.1", version: "0.1.1",
trigger: eventTrigger({ trigger: createProjectTrigger,
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(),
}),
}),
integrations: { integrations: {
supabase, supabase,
}, },
@@ -161,6 +161,7 @@ export const getPostgRESTConfig: AuthenticatedTask<
}, },
}; };
/** Gets project's Postgres config */
export const getPGConfig: AuthenticatedTask< export const getPGConfig: AuthenticatedTask<
SupabaseManagementAPI, SupabaseManagementAPI,
{ ref: string }, { ref: string },
+4
View File
@@ -113,6 +113,10 @@ type ExtractIntegrationClient<
> = ExtractIntegrationClientClient<TIntegrationClient> & > = ExtractIntegrationClientClient<TIntegrationClient> &
ExtractTasks<TIntegrationClient["tasks"]>; ExtractTasks<TIntegrationClient["tasks"]>;
export type IntegrationIO<
TIntegration extends TriggerIntegration<IntegrationClient<any, any>>
> = ExtractIntegrationClient<TIntegration["client"]>;
type ExtractIntegrations< type ExtractIntegrations<
TIntegrations extends Record< TIntegrations extends Record<
string, string,
+11
View File
@@ -77,6 +77,17 @@ export type JobOptions<
) => Promise<any>; ) => Promise<any>;
}; };
export type JobPayload<TJob> = TJob extends Job<
Trigger<EventSpecification<infer TEvent>>,
any
>
? TEvent
: never;
export type JobIO<TJob> = TJob extends Job<any, infer TIntegrations>
? IOWithIntegrations<TIntegrations>
: 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. */ /** 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< export class Job<
TTrigger extends Trigger<EventSpecification<any>>, TTrigger extends Trigger<EventSpecification<any>>,
+6
View File
@@ -67,6 +67,12 @@ export interface Trigger<TEventSpec extends EventSpecification<any>> {
preprocessRuns: boolean; preprocessRuns: boolean;
} }
export type TriggerPayload<TTrigger> = TTrigger extends Trigger<
EventSpecification<infer TEvent>
>
? TEvent
: never;
export type EventSpecificationExample = { export type EventSpecificationExample = {
id: string; id: string;
name: string; name: string;