Skip to content

Commit 6e7b663

Browse files
committed
chore: change webhook back to object
1 parent e814248 commit 6e7b663

File tree

2 files changed

+51
-43
lines changed

2 files changed

+51
-43
lines changed

.changeset/mean-jars-cheat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@thirdweb-dev/service-utils": patch
3+
---
4+
5+
chore: data in webhook payload is an object

packages/service-utils/src/node/webhookProducer.ts

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@ import assert from "node:assert";
22
import { createId } from "@paralleldrive/cuid2";
33
import type { KafkaProducer } from "./kafka.js";
44

5+
/**
6+
* The event schema for webhook events.
7+
* See more: https://github.com/thirdweb-dev/api-server/blob/dev/docs/webhooks.md
8+
*/
59
interface WebhookEvent extends Record<string, unknown> {
6-
id?: `evt_${string}`;
7-
teamId: string;
8-
projectId?: string;
9-
/**
10-
* The timestamp your event was triggered at.
11-
*/
12-
createdAt?: Date;
13-
/**
14-
* An array of your model (defined in api-server). Must not be empty.
15-
*/
16-
payload: Record<string, unknown>[];
10+
id?: `evt_${string}`;
11+
teamId: string;
12+
projectId?: string;
13+
/**
14+
* The timestamp your event was triggered at.
15+
*/
16+
createdAt?: Date;
17+
/**
18+
* Your model defined in api-server.
19+
*/
20+
data: Record<string, unknown>;
1721
}
1822

1923
/**
@@ -27,39 +31,38 @@ interface WebhookEvent extends Record<string, unknown> {
2731
* ```
2832
*/
2933
export class WebhookEventProducer {
30-
private kafkaProducer: KafkaProducer;
34+
private kafkaProducer: KafkaProducer;
3135

32-
constructor(config: { kafkaProducer: KafkaProducer }) {
33-
this.kafkaProducer = config.kafkaProducer;
34-
}
36+
constructor(config: { kafkaProducer: KafkaProducer }) {
37+
this.kafkaProducer = config.kafkaProducer;
38+
}
3539

36-
/**
37-
* Emit a webhook event.
38-
* This method may throw. To call this non-blocking:
39-
* ```ts
40-
* void webhookProducer.sendEvents(events).catch((e) => console.error(e))
41-
* ```
42-
*/
43-
async sendEvents(topic: string, events: WebhookEvent[]): Promise<void> {
44-
const parsedEvents: WebhookEvent[] = events.map((event) => {
45-
assert(event.payload.length > 0, "payload must not be empty");
46-
assert(
47-
event.teamId.startsWith("team_"),
48-
"teamId must start with 'team_'",
49-
);
50-
assert(
51-
!event.projectId || event.projectId.startsWith("prj_"),
52-
"projectId must start with 'prj_'",
53-
);
40+
/**
41+
* Emit a webhook event.
42+
* This method may throw. To call this non-blocking:
43+
* ```ts
44+
* void webhookProducer.sendEvents(events).catch((e) => console.error(e))
45+
* ```
46+
*/
47+
async sendEvents(topic: string, events: WebhookEvent[]): Promise<void> {
48+
const parsedEvents: WebhookEvent[] = events.map((event) => {
49+
assert(
50+
event.teamId.startsWith("team_"),
51+
"teamId must start with 'team_'",
52+
);
53+
assert(
54+
!event.projectId || event.projectId.startsWith("prj_"),
55+
"projectId must start with 'prj_'",
56+
);
5457

55-
return {
56-
...event,
57-
// Default to now.
58-
createdAt: event.createdAt ?? new Date(),
59-
// Default to a generated UUID.
60-
id: event.id ?? `evt_${createId()}`,
61-
};
62-
});
63-
await this.kafkaProducer.send(topic, parsedEvents);
64-
}
58+
return {
59+
...event,
60+
// Default to now.
61+
createdAt: event.createdAt ?? new Date(),
62+
// Default to a generated UUID.
63+
id: event.id ?? `evt_${createId()}`,
64+
};
65+
});
66+
await this.kafkaProducer.send(topic, parsedEvents);
67+
}
6568
}

0 commit comments

Comments
 (0)