@trigger.dev/sdk: deliverAt and timestamp event properties are now dates

This commit is contained in:
Eric Allam
2023-07-04 10:32:04 +01:00
parent e26923eb19
commit 2cbf50b1cc
6 changed files with 49 additions and 12 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/sdk": patch
---
deliverAt and timestamp event properties are now dates
@@ -20,12 +20,8 @@ export class IngestSendEvent {
#calculateDeliverAt(options?: SendEventOptions) {
// If deliverAt is a string and a valid date, convert it to a Date object
if (options?.deliverAt && typeof options.deliverAt === "string") {
const deliverAt = new Date(options.deliverAt);
if (deliverAt.toString() !== "Invalid Date") {
return deliverAt;
}
if (options?.deliverAt) {
return options?.deliverAt;
}
// deliverAfter is the number of seconds to wait before delivering the event
@@ -0,0 +1,28 @@
import { client } from "@/trigger";
import { Job, eventTrigger } from "@trigger.dev/sdk";
import { z } from "zod";
new Job(client, {
id: "test-event-trigger-1",
name: "Test Event Trigger 1",
version: "0.0.1",
logLevel: "debug",
trigger: eventTrigger({
name: "test-event-trigger-1",
schema: z.object({
name: z.string(),
payload: z.any(),
}),
}),
run: async (payload, io, ctx) => {
await io.sendEvent(
"send",
{
name: payload.name,
payload: payload.payload,
timestamp: new Date(),
},
{ deliverAt: new Date(Date.now() + 1000 * 30) }
);
},
});
@@ -6,6 +6,7 @@ import "@/jobs/general";
import "@/jobs/slack";
import "@/jobs/logging";
import "@/jobs/schedules";
import "@/jobs/events";
import { createPagesRoute } from "@trigger.dev/nextjs";
const { handler, config } = createPagesRoute(client);
+6 -6
View File
@@ -203,7 +203,7 @@ export const RawEventSchema = z.object({
only set this if you have a timestamp that you wish to pass through, e.g.
you receive a timestamp from a service and you want the same timestamp to
be used in your Job. */
timestamp: z.string().datetime().optional(),
timestamp: z.coerce.date().optional(),
/** This is optional, it defaults to "trigger.dev". It can be useful to set
this as you can filter events using this in the `eventTrigger()`. */
source: z.string().optional(),
@@ -241,15 +241,15 @@ export type ApiEventLog = z.infer<typeof ApiEventLogSchema>;
/** Options to control the delivery of the event */
export const SendEventOptionsSchema = z.object({
/** An optional Date when you want the event to Trigger Jobs. The event will
/** An optional Date when you want the event to trigger Jobs. The event will
be sent to the platform immediately but won't be acted upon until the
specified time. */
deliverAt: z.string().datetime().optional(),
/** An optional number of seconds you want to wait for the event to Trigger
deliverAt: z.coerce.date().optional(),
/** An optional number of seconds you want to wait for the event to trigger
any relevant Jobs. The event will be sent to the platform immediately but
won't be acted upon until the specified time. */
won't be delivered until after the elapsed number of seconds. */
deliverAfter: z.number().int().optional(),
/** This optional param will be used by the Trigger.dev Connect feature, which
/** This optional param will be used by Trigger.dev Connect, which
is coming soon. */
accountId: z.string().optional(),
});
+7
View File
@@ -213,6 +213,13 @@ export class IO {
{
name: "sendEvent",
params: { event, options },
properties: [
{
label: "name",
text: event.name,
},
...(event?.id ? [{ label: "ID", text: event.id }] : []),
],
},
async (task) => {
return await this._triggerClient.sendEvent(event, options);