Files
triggerdotdev--trigger.dev/docs/sdk/eventtrigger.mdx
T
Hemachandar 63a65dabe4 chore: add example for eventTrigger() doc (#541)
* chore: add example for eventTrigger() doc

* add documentation to the options parameter
2023-10-06 13:56:28 +01:00

115 lines
3.2 KiB
Plaintext

---
title: "eventTrigger()"
description: "`eventTrigger()` is set as a [Job's trigger](/sdk/job) to subscribe to an event a Job from [a sent event](/sdk/triggerclient/instancemethods/sendevent)."
---
<Snippet file="send-event-options.mdx" />
You can have multiple Jobs that subscribe to the same event, they will all trigger when the event gets delivered.
## Parameters
<ResponseField name="options" type="object" required>
<Expandable title="options" defaultOpen>
<ResponseField name="name" type="string" required>
The name of the event you are subscribing to. Must be an exact match (case
sensitive).
</ResponseField>
<ResponseField name="schema" type="object">
A [Zod](/documentation/guides/zod) schema that defines the shape of the
event payload. The default is `z.any()` which is `any`.
</ResponseField>
<ResponseField name="source" type="string">
You can use this to filter events based on the source.
</ResponseField>
<ResponseField name="filter" type="object">
Used to filter which events trigger the Job.
An example:
```typescript
{
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"
}
```
</ResponseField>
<ResponseField name="examples" type="array">
Used to provide example payloads that are accepted by the job.
This will be available in the dashboard and can be used to trigger test runs.
<Expandable title="example object properties" defaultOpen>
<ResponseField name="id" type="string" required>
The example's ID.
</ResponseField>
<ResponseField name="name" type="string" required>
The name that's displayed in the dashboard.
</ResponseField>
<ResponseField name="payload" type="object" required>
The payload that's accepted by the job.
</ResponseField>
<ResponseField name="icon" type="string">
The icon to use for this example in the dashboard.
</ResponseField>
</Expandable>
</ResponseField>
</Expandable>
</ResponseField>
<RequestExample>
```typescript eventTrigger()
//this Job subscribes to an event called new.user
client.defineJob({
id: "job-2",
name: "Second job",
version: "0.0.1",
//subscribes to the "new.user" event
trigger: eventTrigger({
//sent events with this name will trigger this Job
name: "new.user",
//the expected payload shape
schema: z.object({
userId: z.string(),
tier: z.union([z.literal("free"), z.literal("pro")]),
}),
//only events with the tier: "pro" will trigger this Job
filter: {
tier: ["pro"],
},
//(optional) example event object
examples: [
{
id: "issue.opened",
name: "Issue opened",
payload: {
userId: "1234",
tier: "free",
},
//optional
icon: "github",
},
],
}),
run: async (payload, io, ctx) => {
await io.logger.log("New pro user created", { userId: payload.userId });
//do stuff
},
});
```
</RequestExample>