11import type { UsageV2Event , UsageV2Source } from "../core/usageV2.js" ;
22
3+ type UsageV2Options = {
4+ environment : "development" | "production" ;
5+ source : UsageV2Source ;
6+ } & (
7+ | { serviceKey : string ; thirdwebClientId ?: never ; thirdwebSecretKey ?: never }
8+ | { serviceKey ?: never ; thirdwebClientId : string ; thirdwebSecretKey ?: never }
9+ | { serviceKey ?: never ; thirdwebClientId ?: never ; thirdwebSecretKey : string }
10+ ) ;
11+
312/**
4- * Send events to Kafka.
5- * This method may throw. To call this non-blocking:
13+ * Send usageV2 events from either internal services or public clients.
14+ *
15+ * Exactly one authentication method must be provided:
16+ * - serviceKey: for internal services
17+ * - thirdwebClientId: for public clients (MUST be the user's project)
18+ * - thirdwebSecretKey: for public clients (MUST be the user's project)
619 *
20+ * This method may throw. To call this non-blocking:
721 * ```ts
8- * void sendUsageV2Events(events, {
9- * environment: "production",
10- * serviceKey: "..."
11- * }).catch(console.error)
22+ * void sendUsageV2Events(...).catch((e) => console.error(e))
1223 * ```
13- *
14- * @param events - The events to send.
15- * @param options.environment - The environment the service is running in.
16- * @param options.serviceKey - The service key required for authentication.
1724 */
1825export async function sendUsageV2Events (
1926 events : UsageV2Event [ ] ,
20- options : {
21- environment : "development" | "production" ;
22- source : UsageV2Source ;
23- serviceKey ?: string ;
24- } ,
27+ options : UsageV2Options ,
2528) : Promise < void > {
2629 const baseUrl =
2730 options . environment === "production"
2831 ? "https://u.thirdweb.com"
2932 : "https://u.thirdweb-dev.com" ;
3033
31- // Unauthed calls are routed to the /client path
34+ // Determine endpoint and auth header based on provided credentials.
3235 let url : string ;
33- let headers : HeadersInit ;
36+ const headers : HeadersInit = { "Content-Type" : "application/json" } ;
37+
3438 if ( options . serviceKey ) {
3539 url = `${ baseUrl } /usage-v2/${ options . source } ` ;
36- headers = {
37- "Content-Type" : "application/json" ,
38- "x-service-api-key" : options . serviceKey ,
39- } ;
40- } else {
40+ headers [ "x-service-api-key" ] = options . serviceKey ;
41+ } else if ( options . thirdwebSecretKey ) {
4142 url = `${ baseUrl } /usage-v2/${ options . source } /client` ;
42- headers = {
43- "Content-Type" : "application/json" ,
44- } ;
43+ headers [ "x-secret-key" ] = options . thirdwebSecretKey ;
44+ } else if ( options . thirdwebClientId ) {
45+ url = `${ baseUrl } /usage-v2/${ options . source } /client` ;
46+ headers [ "x-client-id" ] = options . thirdwebClientId ;
47+ } else {
48+ throw new Error ( "[UsageV2] No authentication method provided." ) ;
4549 }
4650
4751 const resp = await fetch ( url , {
@@ -52,7 +56,7 @@ export async function sendUsageV2Events(
5256
5357 if ( ! resp . ok ) {
5458 throw new Error (
55- `[UsageV2] unexpected response ${ resp . status } : ${ await resp . text ( ) } ` ,
59+ `[UsageV2] Unexpected response ${ resp . status } : ${ await resp . text ( ) } ` ,
5660 ) ;
5761 }
5862 resp . body ?. cancel ( ) ;
0 commit comments