Skip to content

Commit

Permalink
set client ID if passed in
Browse files Browse the repository at this point in the history
  • Loading branch information
arcoraven committed Mar 1, 2025
1 parent 26bac8a commit dfa1ddd
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/service-utils/src/cf-worker/usageV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
UsageV2Event,
UsageV2Source,
} from "../core/usageV2.js";
import { extractAuthorizationData } from "./index.js";

type UsageV2Options = {
usageBaseUrl: string;
Expand All @@ -28,6 +29,7 @@ export async function sendUsageV2Events<T extends UsageV2Options>(
options: T,
): Promise<void> {
const { usageBaseUrl, source, authInput, serviceKey } = options;
const { clientId, secretKey } = await extractAuthorizationData(authInput);

// Forward headers from the origin request.
// Determine endpoint and auth header based on provided credentials.
Expand All @@ -38,8 +40,13 @@ export async function sendUsageV2Events<T extends UsageV2Options>(
// If a service key is provided, call the non-public usage endpoint.
url = `${usageBaseUrl}/usage-v2/${source}`;
headers.set("x-service-api-key", serviceKey);
} else {
} else if (clientId) {
url = `${usageBaseUrl}/usage-v2/${source}/client`;
headers.set("x-client-id", clientId);
} else if (secretKey) {
url = `${usageBaseUrl}/usage-v2/${source}/client`;
} else {
throw new Error("[UsageV2] No authentication method provided.");
}

const resp = await fetch(url, {
Expand Down

0 comments on commit dfa1ddd

Please sign in to comment.