1+ import type { Request } 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,26 @@ 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+
3734 // Determine endpoint and auth header based on provided credentials.
3835 let url : string ;
3936 const headers : HeadersInit = { "Content-Type" : "application/json" } ;
37+ const origin = authInput . req . headers . get ( "Origin" ) ;
38+ if ( origin ) {
39+ headers [ "Origin" ] = origin ;
40+ }
4041
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 ;
42+ if ( serviceKey ) {
43+ url = `${ usageBaseUrl } /usage-v2/${ source } ` ;
44+ headers [ "x-service-api-key" ] = serviceKey ;
45+ } else if ( secretKey ) {
46+ url = `${ usageBaseUrl } /usage-v2/${ source } /client` ;
47+ headers [ "x-secret-key" ] = secretKey ;
48+ } else if ( clientId ) {
49+ url = `${ usageBaseUrl } /usage-v2/${ source } /client` ;
50+ headers [ "x-client-id" ] = clientId ;
5051 } else {
5152 throw new Error ( "[UsageV2] No authentication method provided." ) ;
5253 }
@@ -56,7 +57,6 @@ export async function sendUsageV2Events<T extends UsageV2Options>(
5657 headers,
5758 body : JSON . stringify ( { events } ) ,
5859 } ) ;
59-
6060 if ( ! resp . ok ) {
6161 throw new Error (
6262 `[UsageV2] Unexpected response ${ resp . status } : ${ await resp . text ( ) } ` ,
0 commit comments