Skip to content
Merged
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
21 changes: 17 additions & 4 deletions packages/service-utils/src/cf-worker/usageV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { extractAuthorizationData } from "./index.js";
type UsageV2Options = {
usageBaseUrl: string;
source: UsageV2Source;
authInput: CoreAuthInput & { req: Request };
authInput: CoreAuthInput & { req?: Request };
serviceKey?: string;
};

Expand All @@ -29,12 +29,25 @@ 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.
// Extract clientId and secretKey from the request, if provided.
let clientId = authInput.clientId ?? null;
let secretKey: string | null = null;
if (authInput.req) {
const authData = await extractAuthorizationData({
...authInput,
req: authInput.req,
});
clientId = authData.clientId;
secretKey = authData.secretKey;
}

// Forward headers from the origin request, if provided.
// Determine endpoint and auth header based on provided credentials.
let url: string;
const headers = new Headers(Object.fromEntries(authInput.req.headers));
const headers = authInput.req
? new Headers(Object.fromEntries(authInput.req.headers))
: new Headers();
headers.set("Content-Type", "application/json");
if (serviceKey) {
// If a service key is provided, call the non-public usage endpoint.
Expand Down