1+ import { Headers , type Request , fetch } from "@cloudflare/workers-types" ;
2+ import type { CoreAuthInput } from "src/core/types.js" ;
13import type {
24 ClientUsageV2Event ,
35 UsageV2Event ,
46 UsageV2Source ,
57} from "../core/usageV2.js" ;
8+ import { extractAuthorizationData } from "./index.js" ;
69
710type UsageV2Options = {
811 usageBaseUrl : string ;
912 source : UsageV2Source ;
10- } & (
11- | { serviceKey : string ; thirdwebClientId ?: never ; thirdwebSecretKey ?: never }
12- | { serviceKey ?: never ; thirdwebClientId : string ; thirdwebSecretKey ?: never }
13- | { serviceKey ?: never ; thirdwebClientId ?: never ; thirdwebSecretKey : string }
14- ) ;
13+ authInput : CoreAuthInput & { req : Request } ;
14+ serviceKey ?: string ;
15+ } ;
1516
1617/**
1718 * Send usageV2 events from either internal services or public clients.
1819 *
19- * Exactly one authentication method must be provided:
20- * - serviceKey: for internal services
21- * - thirdwebClientId: for public clients (MUST be the user's project)
22- * - thirdwebSecretKey: for public clients (MUST be the user's project)
23- *
24- * NOTE: `team_id` is required if `serviceKey` is provided.
25- *
2620 * This method may throw. To call this non-blocking:
2721 * ```ts
2822 * void sendUsageV2Events(...).catch((e) => console.error(e))
@@ -34,19 +28,23 @@ export async function sendUsageV2Events<T extends UsageV2Options>(
3428 : ClientUsageV2Event [ ] ,
3529 options : T ,
3630) : Promise < void > {
31+ const { usageBaseUrl, source, authInput, serviceKey } = options ;
32+ const { clientId, secretKey } = await extractAuthorizationData ( authInput ) ;
33+
34+ // Forward headers from the origin request.
3735 // Determine endpoint and auth header based on provided credentials.
3836 let url : string ;
39- const headers : HeadersInit = { "Content-Type" : "application/json" } ;
40-
41- if ( options . serviceKey ) {
42- url = ` ${ options . usageBaseUrl } /usage-v2/ ${ options . source } ` ;
43- headers [ "x-service-api-key" ] = options . serviceKey ;
44- } else if ( options . thirdwebSecretKey ) {
45- url = ` ${ options . usageBaseUrl } /usage-v2/ ${ options . source } /client` ;
46- headers [ "x-secret-key" ] = options . thirdwebSecretKey ;
47- } else if ( options . thirdwebClientId ) {
48- url = ` ${ options . usageBaseUrl } /usage-v2/ ${ options . source } /client` ;
49- headers [ "x-client-id" ] = options . thirdwebClientId ;
37+ const headers = new Headers ( authInput . req . headers ) ;
38+ headers . set ( "Content-Type" , "application/json" ) ;
39+ if ( serviceKey ) {
40+ // If a service key is provided, call the non-public usage endpoint.
41+ url = ` ${ usageBaseUrl } /usage-v2/ ${ source } ` ;
42+ headers . set ( "x-service-api-key" , serviceKey ) ;
43+ } else if ( clientId ) {
44+ url = ` ${ usageBaseUrl } /usage-v2/ ${ source } /client` ;
45+ headers . set ( "x-client-id" , clientId ) ;
46+ } else if ( secretKey ) {
47+ url = ` ${ usageBaseUrl } /usage-v2/ ${ source } /client` ;
5048 } else {
5149 throw new Error ( "[UsageV2] No authentication method provided." ) ;
5250 }
@@ -56,7 +54,6 @@ export async function sendUsageV2Events<T extends UsageV2Options>(
5654 headers,
5755 body : JSON . stringify ( { events } ) ,
5856 } ) ;
59-
6057 if ( ! resp . ok ) {
6158 throw new Error (
6259 `[UsageV2] Unexpected response ${ resp . status } : ${ await resp . text ( ) } ` ,
0 commit comments