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