asdf
Squash the current repository state back into one baseline commit while preserving the README reframing and repository contents. Constraint: User explicitly requested a single squashed commit with subject "asdf" Confidence: high Scope-risk: broad Reversibility: clean Directive: This commit intentionally rewrites published history; coordinate before future force-pushes Tested: git status clean; local history rewritten to one commit; force-pushed main to origin and instructkr Not-tested: Fresh clone verification after push
This commit is contained in:
commit
d2542c9a62
63
src/services/api/usage.ts
Normal file
63
src/services/api/usage.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import axios from 'axios'
|
||||
import { getOauthConfig } from '../../constants/oauth.js'
|
||||
import {
|
||||
getClaudeAIOAuthTokens,
|
||||
hasProfileScope,
|
||||
isClaudeAISubscriber,
|
||||
} from '../../utils/auth.js'
|
||||
import { getAuthHeaders } from '../../utils/http.js'
|
||||
import { getClaudeCodeUserAgent } from '../../utils/userAgent.js'
|
||||
import { isOAuthTokenExpired } from '../oauth/client.js'
|
||||
|
||||
export type RateLimit = {
|
||||
utilization: number | null // a percentage from 0 to 100
|
||||
resets_at: string | null // ISO 8601 timestamp
|
||||
}
|
||||
|
||||
export type ExtraUsage = {
|
||||
is_enabled: boolean
|
||||
monthly_limit: number | null
|
||||
used_credits: number | null
|
||||
utilization: number | null
|
||||
}
|
||||
|
||||
export type Utilization = {
|
||||
five_hour?: RateLimit | null
|
||||
seven_day?: RateLimit | null
|
||||
seven_day_oauth_apps?: RateLimit | null
|
||||
seven_day_opus?: RateLimit | null
|
||||
seven_day_sonnet?: RateLimit | null
|
||||
extra_usage?: ExtraUsage | null
|
||||
}
|
||||
|
||||
export async function fetchUtilization(): Promise<Utilization | null> {
|
||||
if (!isClaudeAISubscriber() || !hasProfileScope()) {
|
||||
return {}
|
||||
}
|
||||
|
||||
// Skip API call if OAuth token is expired to avoid 401 errors
|
||||
const tokens = getClaudeAIOAuthTokens()
|
||||
if (tokens && isOAuthTokenExpired(tokens.expiresAt)) {
|
||||
return null
|
||||
}
|
||||
|
||||
const authResult = getAuthHeaders()
|
||||
if (authResult.error) {
|
||||
throw new Error(`Auth error: ${authResult.error}`)
|
||||
}
|
||||
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'User-Agent': getClaudeCodeUserAgent(),
|
||||
...authResult.headers,
|
||||
}
|
||||
|
||||
const url = `${getOauthConfig().BASE_API_URL}/api/oauth/usage`
|
||||
|
||||
const response = await axios.get<Utilization>(url, {
|
||||
headers,
|
||||
timeout: 5000, // 5 second timeout
|
||||
})
|
||||
|
||||
return response.data
|
||||
}
|
||||
Reference in New Issue
Block a user