Files
codexbar-mobile/lib/api/codexApi.ts
T
space ba7d957155
CodexBar Mobile Build / build-android (push) Failing after 1m38s
CodexBar Mobile Build / release (push) Has been skipped
Maximus upgradus
2026-06-24 20:16:34 +02:00

27 lines
784 B
TypeScript

import type { CodexAuth, CodexUsageResponse } from "@/types/codex";
const USAGE_URL = "https://chatgpt.com/backend-api/wham/usage";
export async function fetchCodexUsage(auth: CodexAuth): Promise<CodexUsageResponse> {
const headers: Record<string, string> = {
Authorization: `Bearer ${auth.accessToken}`,
"Content-Type": "application/json",
};
if (auth.accountId) {
headers["ChatGPT-Account-Id"] = auth.accountId;
}
const response = await fetch(USAGE_URL, { headers });
if (response.status === 401 || response.status === 403) {
throw new Error("TOKEN_EXPIRED");
}
if (!response.ok) {
await response.text().catch(() => null);
throw new Error(`HTTP_ERROR_${response.status}`);
}
return response.json() as Promise<CodexUsageResponse>;
}