27 lines
784 B
TypeScript
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>;
|
|
}
|