Skip to content

Commit f88f968

Browse files
committed
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
1 parent eca29b8 commit f88f968

File tree

1 file changed

+11
-26
lines changed

1 file changed

+11
-26
lines changed

open-sse/services/usage.ts

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,6 @@ async function getClaudeUsage(accessToken) {
434434
method: "GET",
435435
headers: {
436436
Authorization: `Bearer ${accessToken}`,
437-
"Content-Type": "application/json",
438437
"anthropic-beta": "oauth-2025-04-20",
439438
"anthropic-version": "2023-06-01",
440439
},
@@ -444,29 +443,24 @@ async function getClaudeUsage(accessToken) {
444443
const data = await oauthResponse.json();
445444
const quotas: Record<string, any> = {};
446445

447-
// Parse five_hour window (session limit)
448446
// utilization = percentage remaining, not used
449-
if (data.five_hour !== undefined) {
450-
const remaining = data.five_hour.utilization ?? 0;
451-
quotas["session (5h)"] = {
447+
const createQuotaObject = (window: any) => {
448+
const remaining = window.utilization ?? 0;
449+
return {
452450
used: 100 - remaining,
453451
total: 100,
454-
resetAt: data.five_hour.resets_at || null,
452+
resetAt: parseResetTime(window.resets_at) || null,
455453
remainingPercentage: remaining,
456454
unlimited: false,
457455
};
456+
};
457+
458+
if (data.five_hour !== undefined) {
459+
quotas["session (5h)"] = createQuotaObject(data.five_hour);
458460
}
459461

460-
// Parse seven_day window (weekly limit)
461462
if (data.seven_day !== undefined) {
462-
const remaining = data.seven_day.utilization ?? 0;
463-
quotas["weekly (7d)"] = {
464-
used: 100 - remaining,
465-
total: 100,
466-
resetAt: data.seven_day.resets_at || null,
467-
remainingPercentage: remaining,
468-
unlimited: false,
469-
};
463+
quotas["weekly (7d)"] = createQuotaObject(data.seven_day);
470464
}
471465

472466
// Parse model-specific weekly windows (e.g., seven_day_sonnet, seven_day_opus)
@@ -478,14 +472,7 @@ async function getClaudeUsage(accessToken) {
478472
typeof value === "object"
479473
) {
480474
const modelName = key.replace("seven_day_", "");
481-
const remaining = (value as any).utilization ?? 0;
482-
quotas[`weekly ${modelName} (7d)`] = {
483-
used: 100 - remaining,
484-
total: 100,
485-
resetAt: (value as any).resets_at || null,
486-
remainingPercentage: remaining,
487-
unlimited: false,
488-
};
475+
quotas[`weekly ${modelName} (7d)`] = createQuotaObject(value);
489476
}
490477
}
491478

@@ -513,7 +500,6 @@ async function getClaudeUsageLegacy(accessToken) {
513500
method: "GET",
514501
headers: {
515502
Authorization: `Bearer ${accessToken}`,
516-
"Content-Type": "application/json",
517503
"anthropic-version": "2023-06-01",
518504
},
519505
});
@@ -523,12 +509,11 @@ async function getClaudeUsageLegacy(accessToken) {
523509

524510
if (settings.organization_id) {
525511
const usageResponse = await fetch(
526-
`https://api.anthropic.com/v1/organizations/${settings.organization_id}/usage`,
512+
CLAUDE_CONFIG.usageUrl.replace("{org_id}", settings.organization_id),
527513
{
528514
method: "GET",
529515
headers: {
530516
Authorization: `Bearer ${accessToken}`,
531-
"Content-Type": "application/json",
532517
"anthropic-version": "2023-06-01",
533518
},
534519
}

0 commit comments

Comments
 (0)