by codex: feat. General Improvements
CodexBar Mobile Build / build-android (push) Successful in 21m9s
CodexBar Mobile Build / release (push) Successful in 11s

This commit is contained in:
2026-06-25 14:27:58 +02:00
parent 9dd17cf565
commit 2c31be11c4
33 changed files with 1217 additions and 296 deletions
+60 -1
View File
@@ -2,6 +2,64 @@ import type { CodexAuth, CodexUsageResponse } from "@/types/codex";
const USAGE_URL = "https://chatgpt.com/backend-api/wham/usage";
function isNumber(value: unknown): value is number {
return typeof value === "number" && Number.isFinite(value);
}
export function parseCodexUsageResponse(data: unknown): CodexUsageResponse {
if (!data || typeof data !== "object") {
throw new Error("INVALID_CODEX_RESPONSE");
}
const value = data as Record<string, unknown>;
const rateLimit = value.rate_limit as Record<string, unknown> | undefined;
const primary = rateLimit?.primary_window as Record<string, unknown> | undefined;
const secondary = rateLimit?.secondary_window as Record<string, unknown> | undefined;
const credits = value.credits as Record<string, unknown> | undefined;
const validWindow = (window: Record<string, unknown> | undefined) =>
!!window &&
isNumber(window.used_percent) &&
isNumber(window.reset_at) &&
isNumber(window.limit_window_seconds);
if (
typeof value.plan_type !== "string" ||
!validWindow(primary) ||
!validWindow(secondary) ||
!credits ||
typeof credits.has_credits !== "boolean" ||
typeof credits.unlimited !== "boolean" ||
!isNumber(credits.balance)
) {
throw new Error("INVALID_CODEX_RESPONSE");
}
const primaryWindow = primary as Record<string, unknown>;
const secondaryWindow = secondary as Record<string, unknown>;
return {
plan_type: value.plan_type,
rate_limit: {
primary_window: {
used_percent: primaryWindow.used_percent as number,
reset_at: primaryWindow.reset_at as number,
limit_window_seconds: primaryWindow.limit_window_seconds as number,
},
secondary_window: {
used_percent: secondaryWindow.used_percent as number,
reset_at: secondaryWindow.reset_at as number,
limit_window_seconds: secondaryWindow.limit_window_seconds as number,
},
},
credits: {
has_credits: credits.has_credits,
unlimited: credits.unlimited,
balance: credits.balance,
},
};
}
export async function fetchCodexUsage(auth: CodexAuth): Promise<CodexUsageResponse> {
const headers: Record<string, string> = {
Authorization: `Bearer ${auth.accessToken}`,
@@ -22,5 +80,6 @@ export async function fetchCodexUsage(auth: CodexAuth): Promise<CodexUsageRespon
throw new Error(`HTTP_ERROR_${response.status}`);
}
return response.json() as Promise<CodexUsageResponse>;
const data: unknown = await response.json();
return parseCodexUsageResponse(data);
}