Make the schema an optional param for customEvent and webhookEvent
This commit is contained in:
@@ -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 (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
|
// @ts-ignore
|
||||||
return JSONSchemaFaker.generate(workflow.jsonSchema);
|
return JSONSchemaFaker.generate(workflow.jsonSchema);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,106 +1,32 @@
|
|||||||
import * as slack from "@trigger.dev/slack";
|
import { Trigger, webhookEvent, customEvent } from "@trigger.dev/sdk";
|
||||||
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(),
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
new Trigger({
|
new Trigger({
|
||||||
id: "caldotcom-to-slack-2",
|
id: "typeform-webhook",
|
||||||
name: "Cal.com To Slack",
|
name: "Typeform Webhook",
|
||||||
apiKey: "trigger_development_lwlXEjyhSNF4",
|
apiKey: "trigger_development_qthXXiRnLJuM",
|
||||||
endpoint: "ws://localhost:8889/ws",
|
endpoint: "ws://localhost:8889/ws",
|
||||||
logLevel: "debug",
|
logLevel: "debug",
|
||||||
on: webhookEvent({
|
on: webhookEvent({
|
||||||
service: "cal.com",
|
service: "typeform.com",
|
||||||
eventName: "BOOKING_CREATED",
|
eventName: "form_response",
|
||||||
filter: {
|
|
||||||
triggerEvent: ["BOOKING_CREATED"],
|
|
||||||
},
|
|
||||||
schema: bookingPayloadSchema,
|
|
||||||
verifyPayload: {
|
|
||||||
enabled: true,
|
|
||||||
header: "X-Cal-Signature-256",
|
|
||||||
},
|
|
||||||
}),
|
}),
|
||||||
run: async (event, ctx) => {
|
run: async (event, ctx) => {
|
||||||
await ctx.logger.info("Received a cal.com booking", {
|
// Do something with the event
|
||||||
event,
|
await ctx.logger.info("Received event", event);
|
||||||
wallTime: new Date(),
|
},
|
||||||
});
|
}).listen();
|
||||||
|
|
||||||
await slack.postMessage(`Cal.com booking yo`, {
|
new Trigger({
|
||||||
channelName: "customers",
|
id: "new-user",
|
||||||
text: `New Booking: ${
|
name: "New User",
|
||||||
event.payload.title
|
apiKey: "trigger_development_qthXXiRnLJuM",
|
||||||
} at ${event.payload.startTime.toLocaleDateString()}`,
|
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();
|
}).listen();
|
||||||
|
|||||||
@@ -22,22 +22,25 @@ export type TriggerEvent<TSchema extends z.ZodTypeAny> = {
|
|||||||
|
|
||||||
export type TriggerCustomEventOptions<TSchema extends z.ZodTypeAny> = {
|
export type TriggerCustomEventOptions<TSchema extends z.ZodTypeAny> = {
|
||||||
name: string;
|
name: string;
|
||||||
schema: TSchema;
|
schema?: TSchema;
|
||||||
filter?: EventFilter;
|
filter?: EventFilter;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function customEvent<TSchema extends z.ZodTypeAny>(
|
export function customEvent<TSchema extends z.ZodTypeAny>(
|
||||||
options: TriggerCustomEventOptions<TSchema>
|
options: TriggerCustomEventOptions<TSchema>
|
||||||
): TriggerEvent<TSchema> {
|
): TriggerEvent<TSchema> {
|
||||||
|
const schema = options.schema ?? z.any();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
metadata: {
|
metadata: {
|
||||||
type: "CUSTOM_EVENT",
|
type: "CUSTOM_EVENT",
|
||||||
service: "trigger",
|
service: "trigger",
|
||||||
name: options.name,
|
name: options.name,
|
||||||
filter: { event: [options.name], payload: options.filter ?? {} },
|
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> = {
|
export type TriggerWebhookEventOptions<TSchema extends z.ZodTypeAny> = {
|
||||||
schema: TSchema;
|
schema?: TSchema;
|
||||||
service: string;
|
service: string;
|
||||||
eventName: string;
|
eventName: string;
|
||||||
filter?: EventFilter;
|
filter?: EventFilter;
|
||||||
@@ -71,6 +74,8 @@ export type TriggerWebhookEventOptions<TSchema extends z.ZodTypeAny> = {
|
|||||||
export function webhookEvent<TSchema extends z.ZodTypeAny>(
|
export function webhookEvent<TSchema extends z.ZodTypeAny>(
|
||||||
options: TriggerWebhookEventOptions<TSchema>
|
options: TriggerWebhookEventOptions<TSchema>
|
||||||
): TriggerEvent<TSchema> {
|
): TriggerEvent<TSchema> {
|
||||||
|
const schema = options.schema ?? z.any();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
metadata: {
|
metadata: {
|
||||||
type: "WEBHOOK",
|
type: "WEBHOOK",
|
||||||
@@ -86,8 +91,9 @@ export function webhookEvent<TSchema extends z.ZodTypeAny>(
|
|||||||
event: options.eventName,
|
event: options.eventName,
|
||||||
},
|
},
|
||||||
manualRegistration: true,
|
manualRegistration: true,
|
||||||
schema: zodToJsonSchema(options.schema) as WebhookEventTrigger["schema"],
|
schema: zodToJsonSchema(schema) as WebhookEventTrigger["schema"],
|
||||||
},
|
},
|
||||||
|
// @ts-ignore
|
||||||
schema: options.schema,
|
schema: options.schema,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user