Files
codexbar-mobile/lib/api/codexApi.ts
T

26 lines
739 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) {
throw new Error(`HTTP_ERROR_${response.status}`);
}
return response.json() as Promise<CodexUsageResponse>;
}