Need to define the class like EventTrigger

This commit is contained in:
Matt Aitken
2023-10-20 15:11:21 +01:00
parent 1ad4fdc26b
commit c8b11df10e
2 changed files with 60 additions and 4 deletions
+6
View File
@@ -40,10 +40,16 @@ export const ScheduledTriggerMetadataSchema = z.object({
schedule: ScheduleMetadataSchema,
});
export const AssetTriggerMetadataSchema = z.object({
type: z.literal("modular"),
id: z.string(),
});
export const TriggerMetadataSchema = z.discriminatedUnion("type", [
DynamicTriggerMetadataSchema,
StaticTriggerMetadataSchema,
ScheduledTriggerMetadataSchema,
AssetTriggerMetadataSchema,
]);
export type TriggerMetadata = z.infer<typeof TriggerMetadataSchema>;
@@ -1,12 +1,30 @@
import { RequestFilterSchema } from "@trigger.dev/core";
import {
DisplayPropertiesSchema,
EventFilter,
EventFilterSchema,
RequestFilterSchema,
} from "@trigger.dev/core";
import { z } from "zod";
import { TriggerClient } from "../triggerClient";
import { EventSpecification, EventSpecificationExampleSchema, Trigger } from "../types";
import { Job } from "../job";
const HttpTriggerOptionsSchema = z.object({
id: z.string(),
title: z.string().optional(),
icon: z.string().optional(),
properties: DisplayPropertiesSchema.optional(),
verify: z
.object({
filter: RequestFilterSchema.optional(),
requestFilter: RequestFilterSchema.optional(),
})
.optional(),
payload: z
.object({
requestFilter: RequestFilterSchema.optional(),
schema: z.any().optional(),
examples: z.array(EventSpecificationExampleSchema).optional(),
filter: EventFilterSchema.optional(),
})
.optional(),
});
@@ -17,11 +35,43 @@ type HttpTriggerOptions = z.infer<typeof HttpTriggerOptionsSchema> & {
};
};
export class HttpTrigger {
export class HttpTrigger<TEventSpecification extends EventSpecification<TEvent>, TEvent = any>
implements Trigger<TEventSpecification>
{
constructor(
private readonly client: TriggerClient,
options: HttpTriggerOptions
private readonly options: HttpTriggerOptions
) {}
get event() {
return {
name: this.options.id,
title: this.options.title ?? "http",
source: "http",
icon: this.options.icon ?? "world-www",
properties: this.options.properties,
schema: this.options.payload?.schema,
examples: this.options.payload?.examples,
filter: this.options.payload?.filter,
parsePayload: (payload: unknown) => payload as TEvent,
runProperties: (payload: TEvent) => [],
};
}
toJSON() {
return {
type: "modular" as const,
id: this.options.id,
};
}
attachToJob(triggerClient: TriggerClient, job: Job<Trigger<TEventSpecification>, any>): void {
throw new Error("Method not implemented.");
}
get preprocessRuns() {
return true;
}
}
const trigger = new HttpTrigger(