Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/metal-crabs-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/service-utils": patch
---

[service-utils] Allow client-side usageV2 reporting
28 changes: 20 additions & 8 deletions packages/service-utils/src/cf-worker/usageV2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { ServiceName } from "../core/services.js";
import type { UsageV2Event } from "../core/usageV2.js";
import type { UsageV2Event, UsageV2Source } from "../core/usageV2.js";

/**
* Send events to Kafka.
Expand All @@ -20,21 +19,34 @@ export async function sendUsageV2Events(
events: UsageV2Event[],
options: {
environment: "development" | "production";
productName: ServiceName;
serviceKey: string;
source: UsageV2Source;
serviceKey?: string;
},
): Promise<void> {
const baseUrl =
options.environment === "production"
? "https://u.thirdweb.com"
: "https://u.thirdweb-dev.com";

const resp = await fetch(`${baseUrl}/usage-v2/${options.productName}`, {
method: "POST",
headers: {
// Unauthed calls are routed to the /client path
let url: string;
let headers: HeadersInit;
if (options.serviceKey) {
url = `${baseUrl}/usage-v2/${options.source}`;
headers = {
"Content-Type": "application/json",
"x-service-api-key": options.serviceKey,
},
};
} else {
url = `${baseUrl}/usage-v2/${options.source}/client`;
headers = {
"Content-Type": "application/json",
};
}

const resp = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify({ events }),
});

Expand Down
Loading