()
| 33 | } |
| 34 | |
| 35 | export async function fetchUtilization(): Promise<Utilization | null> { |
| 36 | if (!isClaudeAISubscriber() || !hasProfileScope()) { |
| 37 | return {} |
| 38 | } |
| 39 | |
| 40 | // Skip API call if OAuth token is expired to avoid 401 errors |
| 41 | const tokens = getClaudeAIOAuthTokens() |
| 42 | if (tokens && isOAuthTokenExpired(tokens.expiresAt)) { |
| 43 | return null |
| 44 | } |
| 45 | |
| 46 | const authResult = getAuthHeaders() |
| 47 | if (authResult.error) { |
| 48 | throw new Error(`Auth error: ${authResult.error}`) |
| 49 | } |
| 50 | |
| 51 | const headers = { |
| 52 | 'Content-Type': 'application/json', |
| 53 | 'User-Agent': getClaudeCodeUserAgent(), |
| 54 | ...authResult.headers, |
| 55 | } |
| 56 | |
| 57 | const url = `${getOauthConfig().BASE_API_URL}/api/oauth/usage` |
| 58 | |
| 59 | const response = await axios.get<Utilization>(url, { |
| 60 | headers, |
| 61 | timeout: 5000, // 5 second timeout |
| 62 | }) |
| 63 | |
| 64 | return response.data |
| 65 | } |
no test coverage detected