eventTrigger() jsdocs

This commit is contained in:
Matt Aitken
2023-07-05 10:48:46 +01:00
parent 3b90739087
commit aaa70a9aab
3 changed files with 38 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@trigger.dev/sdk": patch
---
eventTrigger() jsdocs
@@ -1,12 +1,17 @@
import { z } from "zod";
const EventMatcherSchema = z.union([
/** Match against a string */
z.array(z.string()),
/** Match against a number */
z.array(z.number()),
/** Match against a boolean */
z.array(z.boolean()),
]);
type EventMatcher = z.infer<typeof EventMatcherSchema>;
/** A filter for matching against data */
export type EventFilter = { [key: string]: EventMatcher | EventFilter };
export const EventFilterSchema: z.ZodType<EventFilter> = z.lazy(() =>
@@ -54,13 +54,41 @@ export class EventTrigger<TEventSpecification extends EventSpecification<any>>
}
}
/** Configuration options for an EventTrigger */
type TriggerOptions<TEvent> = {
/** The name of the event you are subscribing to. Must be an exact match (case sensitive). */
name: string;
/** A [Zod](https://trigger.dev/docs/documentation/guides/zod) schema that defines the shape of the event payload.
* The default is `z.any()` which is `any`.
* */
schema?: z.Schema<TEvent>;
/** You can use this to filter events based on the source. */
source?: string;
/** Used to filter which events trigger the Job
* @example
* filter:
* ```ts
* {
* name: ["John", "Jane"],
* age: [18, 21]
* }
* ```
*
* This filter would match against an event with the following data:
* ```json
* {
* "name": "Jane",
* "age": 18,
* "location": "San Francisco"
* }
* ```
*/
filter?: EventFilter;
};
/** `eventTrigger()` is set as a [Job's trigger](https://trigger.dev/docs/sdk/job) to subscribe to an event a Job from [a sent event](https://trigger.dev/docs/sdk/triggerclient/instancemethods/sendevent)
* @param options options for the EventTrigger
*/
export function eventTrigger<TEvent extends any = any>(
options: TriggerOptions<TEvent>
): Trigger<EventSpecification<TEvent>> {