Skip to content

Commit e0e3182

Browse files
authored
chore: make req optional when calling usage from cloudflare (#6556)
1 parent fe8b3ce commit e0e3182

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

packages/service-utils/src/cf-worker/usageV2.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { extractAuthorizationData } from "./index.js";
1010
type UsageV2Options = {
1111
usageBaseUrl: string;
1212
source: UsageV2Source;
13-
authInput: CoreAuthInput & { req: Request };
13+
authInput: CoreAuthInput & { req?: Request };
1414
serviceKey?: string;
1515
};
1616

@@ -29,12 +29,25 @@ export async function sendUsageV2Events<T extends UsageV2Options>(
2929
options: T,
3030
): Promise<void> {
3131
const { usageBaseUrl, source, authInput, serviceKey } = options;
32-
const { clientId, secretKey } = await extractAuthorizationData(authInput);
3332

34-
// Forward headers from the origin request.
33+
// Extract clientId and secretKey from the request, if provided.
34+
let clientId = authInput.clientId ?? null;
35+
let secretKey: string | null = null;
36+
if (authInput.req) {
37+
const authData = await extractAuthorizationData({
38+
...authInput,
39+
req: authInput.req,
40+
});
41+
clientId = authData.clientId;
42+
secretKey = authData.secretKey;
43+
}
44+
45+
// Forward headers from the origin request, if provided.
3546
// Determine endpoint and auth header based on provided credentials.
3647
let url: string;
37-
const headers = new Headers(Object.fromEntries(authInput.req.headers));
48+
const headers = authInput.req
49+
? new Headers(Object.fromEntries(authInput.req.headers))
50+
: new Headers();
3851
headers.set("Content-Type", "application/json");
3952
if (serviceKey) {
4053
// If a service key is provided, call the non-public usage endpoint.

0 commit comments

Comments
 (0)