From d19f336286613d7cedff7103f54e00bbf6af095c Mon Sep 17 00:00:00 2001 From: DavyMassoneto Date: Wed, 4 Mar 2026 19:27:40 -0300 Subject: [PATCH] refactor: address PR review feedback - Use parseResetTime() for resetAt fields (consistency with other fetchers) - Extract createQuotaObject() helper to reduce duplication - Remove unnecessary Content-Type header from GET requests - Use CLAUDE_CONFIG.usageUrl with template interpolation in legacy fallback --- open-sse/services/usage.ts | 37 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/open-sse/services/usage.ts b/open-sse/services/usage.ts index e824d4f6ef..b9166d062d 100644 --- a/open-sse/services/usage.ts +++ b/open-sse/services/usage.ts @@ -434,7 +434,6 @@ async function getClaudeUsage(accessToken) { method: "GET", headers: { Authorization: `Bearer ${accessToken}`, - "Content-Type": "application/json", "anthropic-beta": "oauth-2025-04-20", "anthropic-version": "2023-06-01", }, @@ -444,29 +443,24 @@ async function getClaudeUsage(accessToken) { const data = await oauthResponse.json(); const quotas: Record = {}; - // Parse five_hour window (session limit) // utilization = percentage remaining, not used - if (data.five_hour !== undefined) { - const remaining = data.five_hour.utilization ?? 0; - quotas["session (5h)"] = { + const createQuotaObject = (window: any) => { + const remaining = window.utilization ?? 0; + return { used: 100 - remaining, total: 100, - resetAt: data.five_hour.resets_at || null, + resetAt: parseResetTime(window.resets_at) || null, remainingPercentage: remaining, unlimited: false, }; + }; + + if (data.five_hour !== undefined) { + quotas["session (5h)"] = createQuotaObject(data.five_hour); } - // Parse seven_day window (weekly limit) if (data.seven_day !== undefined) { - const remaining = data.seven_day.utilization ?? 0; - quotas["weekly (7d)"] = { - used: 100 - remaining, - total: 100, - resetAt: data.seven_day.resets_at || null, - remainingPercentage: remaining, - unlimited: false, - }; + quotas["weekly (7d)"] = createQuotaObject(data.seven_day); } // Parse model-specific weekly windows (e.g., seven_day_sonnet, seven_day_opus) @@ -478,14 +472,7 @@ async function getClaudeUsage(accessToken) { typeof value === "object" ) { const modelName = key.replace("seven_day_", ""); - const remaining = (value as any).utilization ?? 0; - quotas[`weekly ${modelName} (7d)`] = { - used: 100 - remaining, - total: 100, - resetAt: (value as any).resets_at || null, - remainingPercentage: remaining, - unlimited: false, - }; + quotas[`weekly ${modelName} (7d)`] = createQuotaObject(value); } } @@ -513,7 +500,6 @@ async function getClaudeUsageLegacy(accessToken) { method: "GET", headers: { Authorization: `Bearer ${accessToken}`, - "Content-Type": "application/json", "anthropic-version": "2023-06-01", }, }); @@ -523,12 +509,11 @@ async function getClaudeUsageLegacy(accessToken) { if (settings.organization_id) { const usageResponse = await fetch( - `https://api.anthropic.com/v1/organizations/${settings.organization_id}/usage`, + CLAUDE_CONFIG.usageUrl.replace("{org_id}", settings.organization_id), { method: "GET", headers: { Authorization: `Bearer ${accessToken}`, - "Content-Type": "application/json", "anthropic-version": "2023-06-01", }, }