Skip to content

Commit cbfe9ec

Browse files
authored
[service-utils] Allow sending client-side usageV2 events (#6207)
1 parent fecd3a6 commit cbfe9ec

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

.changeset/metal-crabs-beg.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+
[service-utils] Allow client-side usageV2 reporting

packages/service-utils/src/cf-worker/usageV2.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { ServiceName } from "../core/services.js";
2-
import type { UsageV2Event } from "../core/usageV2.js";
1+
import type { UsageV2Event, UsageV2Source } from "../core/usageV2.js";
32

43
/**
54
* Send events to Kafka.
@@ -20,21 +19,34 @@ export async function sendUsageV2Events(
2019
events: UsageV2Event[],
2120
options: {
2221
environment: "development" | "production";
23-
productName: ServiceName;
24-
serviceKey: string;
22+
source: UsageV2Source;
23+
serviceKey?: string;
2524
},
2625
): Promise<void> {
2726
const baseUrl =
2827
options.environment === "production"
2928
? "https://u.thirdweb.com"
3029
: "https://u.thirdweb-dev.com";
3130

32-
const resp = await fetch(`${baseUrl}/usage-v2/${options.productName}`, {
33-
method: "POST",
34-
headers: {
31+
// Unauthed calls are routed to the /client path
32+
let url: string;
33+
let headers: HeadersInit;
34+
if (options.serviceKey) {
35+
url = `${baseUrl}/usage-v2/${options.source}`;
36+
headers = {
3537
"Content-Type": "application/json",
3638
"x-service-api-key": options.serviceKey,
37-
},
39+
};
40+
} else {
41+
url = `${baseUrl}/usage-v2/${options.source}/client`;
42+
headers = {
43+
"Content-Type": "application/json",
44+
};
45+
}
46+
47+
const resp = await fetch(url, {
48+
method: "POST",
49+
headers,
3850
body: JSON.stringify({ events }),
3951
});
4052

0 commit comments

Comments
 (0)