Make the schema an optional param for customEvent and webhookEvent

This commit is contained in:
Eric Allam
2023-02-23 12:33:51 +00:00
parent 566fb54615
commit ee20f9212f
4 changed files with 47 additions and 102 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/sdk": patch
---
Make the schema an optional param for customEvent and webhookEvent
@@ -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);
}
+23 -97
View File
@@ -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(),
});
await slack.postMessage(`Cal.com booking yo`, {
channelName: "customers",
text: `New Booking: ${
event.payload.title
} at ${event.payload.startTime.toLocaleDateString()}`,
});
// Do something with the event
await ctx.logger.info("Received event", event);
},
}).listen();
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();
+11 -5
View File
@@ -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,
};
}
@@ -58,7 +61,7 @@ export function scheduleEvent(
}
export type TriggerWebhookEventOptions<TSchema extends z.ZodTypeAny> = {
schema: TSchema;
schema?: TSchema;
service: string;
eventName: string;
filter?: EventFilter;
@@ -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",
@@ -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,
};
}