Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/big-apples-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@trigger.dev/sdk": patch
---

Make the schema an optional param for customEvent and webhookEvent
8 changes: 8 additions & 0 deletions apps/webapp/app/presenters/testPresenter.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ export class WorkflowTestPresenter {
}

if (workflow.jsonSchema) {
// If jsonSchema is just { "$schema": "http://json-schema.org/draft-07/schema#" }, then return an empty object
if (
Object.keys(workflow.jsonSchema).length === 1 &&
// @ts-ignore
workflow.jsonSchema["$schema"]
) {
return {};
}
// @ts-ignore
return JSONSchemaFaker.generate(workflow.jsonSchema);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export default function TemplatePage() {
if (events !== null) {
revalidator.revalidate();
}
}, [events, revalidator]);
// WARNING Don't put the revalidator in the useEffect deps array or bad things will happen
}, [events]); // eslint-disable-line react-hooks/exhaustive-deps

const organizationTemplateByStatus = (
<OrganizationTemplateByStatus {...loaderData} />
Expand Down
118 changes: 22 additions & 96 deletions examples/generic-webhook/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,106 +1,32 @@
import * as slack from "@trigger.dev/slack";
import { Trigger, webhookEvent } from "@trigger.dev/sdk";
import { z } from "zod";

export const bookingPayloadSchema = z.object({
triggerEvent: z.string(),
createdAt: z.coerce.date(),
payload: z.object({
type: z.string(),
title: z.string(),
description: z.string(),
additionalNotes: z.string(),
customInputs: z.object({}),
startTime: z.coerce.date(),
endTime: z.coerce.date(),
organizer: z.object({
id: z.number(),
name: z.string(),
email: z.string(),
timeZone: z.string(),
language: z.object({ locale: z.string() }),
}),
attendees: z.array(
z.object({
email: z.string(),
name: z.string(),
timeZone: z.string(),
language: z.object({ locale: z.string() }),
})
),
location: z.string(),
destinationCalendar: z.object({
id: z.number(),
integration: z.string(),
externalId: z.string(),
userId: z.number(),
eventTypeId: z.null(),
credentialId: z.number(),
}),
hideCalendarNotes: z.boolean(),
requiresConfirmation: z.null(),
eventTypeId: z.number(),
seatsShowAttendees: z.boolean(),
uid: z.string(),
conferenceData: z.object({
createRequest: z.object({ requestId: z.string() }),
}),
videoCallData: z.object({
type: z.string(),
id: z.string(),
password: z.string(),
url: z.string(),
}),
appsStatus: z.array(
z.object({
appName: z.string(),
type: z.string(),
success: z.number(),
failures: z.number(),
errors: z.array(z.any()).optional(),
warnings: z.array(z.any()).optional(),
})
),
eventTitle: z.string(),
eventDescription: z.null(),
price: z.number(),
currency: z.string(),
length: z.number(),
bookingId: z.number(),
metadata: z.object({ videoCallUrl: z.string() }),
status: z.string(),
}),
});
import { Trigger, webhookEvent, customEvent } from "@trigger.dev/sdk";

new Trigger({
id: "caldotcom-to-slack-2",
name: "Cal.com To Slack",
apiKey: "trigger_development_lwlXEjyhSNF4",
id: "typeform-webhook",
name: "Typeform Webhook",
apiKey: "trigger_development_qthXXiRnLJuM",
endpoint: "ws://localhost:8889/ws",
logLevel: "debug",
on: webhookEvent({
service: "cal.com",
eventName: "BOOKING_CREATED",
filter: {
triggerEvent: ["BOOKING_CREATED"],
},
schema: bookingPayloadSchema,
verifyPayload: {
enabled: true,
header: "X-Cal-Signature-256",
},
service: "typeform.com",
eventName: "form_response",
}),
run: async (event, ctx) => {
await ctx.logger.info("Received a cal.com booking", {
event,
wallTime: new Date(),
});
// Do something with the event
await ctx.logger.info("Received event", event);
},
}).listen();

await slack.postMessage(`Cal.com booking yo`, {
channelName: "customers",
text: `New Booking: ${
event.payload.title
} at ${event.payload.startTime.toLocaleDateString()}`,
});
new Trigger({
id: "new-user",
name: "New User",
apiKey: "trigger_development_qthXXiRnLJuM",
endpoint: "ws://localhost:8889/ws",
logLevel: "debug",
on: customEvent({
name: "new.user",
}),
run: async (event, ctx) => {
// Do something with the event
await ctx.logger.info("Received event", event);
},
}).listen();
16 changes: 11 additions & 5 deletions packages/trigger-sdk/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,25 @@ export type TriggerEvent<TSchema extends z.ZodTypeAny> = {

export type TriggerCustomEventOptions<TSchema extends z.ZodTypeAny> = {
name: string;
schema: TSchema;
schema?: TSchema;
filter?: EventFilter;
};

export function customEvent<TSchema extends z.ZodTypeAny>(
options: TriggerCustomEventOptions<TSchema>
): TriggerEvent<TSchema> {
const schema = options.schema ?? z.any();

return {
metadata: {
type: "CUSTOM_EVENT",
service: "trigger",
name: options.name,
filter: { event: [options.name], payload: options.filter ?? {} },
schema: zodToJsonSchema(options.schema) as CustomEventTrigger["schema"],
schema: zodToJsonSchema(schema) as CustomEventTrigger["schema"],
},
schema: options.schema,
// @ts-ignore
schema,
};
}

Expand All @@ -58,7 +61,7 @@ export function scheduleEvent(
}

export type TriggerWebhookEventOptions<TSchema extends z.ZodTypeAny> = {
schema: TSchema;
schema?: TSchema;
service: string;
eventName: string;
filter?: EventFilter;
Expand All @@ -71,6 +74,8 @@ export type TriggerWebhookEventOptions<TSchema extends z.ZodTypeAny> = {
export function webhookEvent<TSchema extends z.ZodTypeAny>(
options: TriggerWebhookEventOptions<TSchema>
): TriggerEvent<TSchema> {
const schema = options.schema ?? z.any();

return {
metadata: {
type: "WEBHOOK",
Expand All @@ -86,8 +91,9 @@ export function webhookEvent<TSchema extends z.ZodTypeAny>(
event: options.eventName,
},
manualRegistration: true,
schema: zodToJsonSchema(options.schema) as WebhookEventTrigger["schema"],
schema: zodToJsonSchema(schema) as WebhookEventTrigger["schema"],
},
// @ts-ignore
schema: options.schema,
};
}