()
| 25 | } |
| 26 | |
| 27 | export async function fetchUtilization(): Promise<Utilization | null> { |
| 28 | const session = getAuthRuntime().getCurrentSession() |
| 29 | |
| 30 | const isManagedSubscriber = |
| 31 | session.principalSource === 'managed_oauth' && |
| 32 | session.subscription.subscriptionType != null |
| 33 | const hasProfileScope = session.scopes.includes('user:profile') |
| 34 | |
| 35 | if (!isManagedSubscriber || !hasProfileScope) { |
| 36 | return {} |
| 37 | } |
| 38 | |
| 39 | // Skip API call if the managed session is not currently usable to avoid 401s. |
| 40 | if (session.sessionState !== 'usable' || !session.accessToken) { |
| 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': getNcodeUserAgent(), |
| 52 | ...authResult.headers, |
| 53 | } |
| 54 | |
| 55 | return await getIdentityClient().fetchUtilization({ |
| 56 | headers, |
| 57 | timeout: 5000, // 5 second timeout |
| 58 | }) |
| 59 | } |
no test coverage detected