[skip ci] Initial commit — CodexBar Mobile Expo app
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import type { ClaudeOrg, ClaudeUsageResponse } from "@/types/claude";
|
||||
|
||||
const BASE_URL = "https://claude.ai/api";
|
||||
|
||||
function cookieHeader(sessionKey: string): Record<string, string> {
|
||||
return {
|
||||
Cookie: `sessionKey=${sessionKey}`,
|
||||
"Accept": "application/json",
|
||||
"Origin": "https://claude.ai",
|
||||
};
|
||||
}
|
||||
|
||||
export async function fetchClaudeOrgs(sessionKey: string): Promise<ClaudeOrg[]> {
|
||||
const response = await fetch(`${BASE_URL}/organizations`, {
|
||||
headers: cookieHeader(sessionKey),
|
||||
});
|
||||
|
||||
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<ClaudeOrg[]>;
|
||||
}
|
||||
|
||||
export async function fetchClaudeUsage(
|
||||
sessionKey: string,
|
||||
orgUuid: string
|
||||
): Promise<ClaudeUsageResponse> {
|
||||
const response = await fetch(`${BASE_URL}/organizations/${orgUuid}/usage`, {
|
||||
headers: cookieHeader(sessionKey),
|
||||
});
|
||||
|
||||
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<ClaudeUsageResponse>;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
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>;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import * as DocumentPicker from "expo-document-picker";
|
||||
import { File } from "expo-file-system/next";
|
||||
import type { CodexAuth } from "@/types/codex";
|
||||
|
||||
export async function pickAndReadCodexAuth(): Promise<CodexAuth> {
|
||||
const result = await DocumentPicker.getDocumentAsync({
|
||||
type: "application/json",
|
||||
copyToCacheDirectory: true,
|
||||
});
|
||||
|
||||
if (result.canceled) {
|
||||
throw new Error("PICKER_CANCELLED");
|
||||
}
|
||||
|
||||
const { uri } = result.assets[0];
|
||||
const file = new File(uri);
|
||||
const text = file.text();
|
||||
|
||||
let parsed: Record<string, unknown>;
|
||||
try {
|
||||
parsed = JSON.parse(text);
|
||||
} catch {
|
||||
throw new Error("INVALID_JSON");
|
||||
}
|
||||
|
||||
const accessToken = parsed["accessToken"] as string | undefined;
|
||||
const accountId = parsed["accountId"] as string | undefined;
|
||||
|
||||
if (!accessToken || typeof accessToken !== "string") {
|
||||
throw new Error("MISSING_ACCESS_TOKEN");
|
||||
}
|
||||
|
||||
return { accessToken, accountId };
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import * as SecureStore from "expo-secure-store";
|
||||
|
||||
const KEYS = {
|
||||
CODEX_AUTH: "codexbar_codex_auth",
|
||||
CLAUDE_SESSION_KEY: "codexbar_claude_session_key",
|
||||
} as const;
|
||||
|
||||
export async function saveCodexAuth(auth: {
|
||||
accessToken: string;
|
||||
accountId?: string;
|
||||
}): Promise<void> {
|
||||
await SecureStore.setItemAsync(KEYS.CODEX_AUTH, JSON.stringify(auth));
|
||||
}
|
||||
|
||||
export async function loadCodexAuth(): Promise<{
|
||||
accessToken: string;
|
||||
accountId?: string;
|
||||
} | null> {
|
||||
const raw = await SecureStore.getItemAsync(KEYS.CODEX_AUTH);
|
||||
if (!raw) return null;
|
||||
return JSON.parse(raw) as { accessToken: string; accountId?: string };
|
||||
}
|
||||
|
||||
export async function saveClaudeSessionKey(key: string): Promise<void> {
|
||||
await SecureStore.setItemAsync(KEYS.CLAUDE_SESSION_KEY, key);
|
||||
}
|
||||
|
||||
export async function loadClaudeSessionKey(): Promise<string | null> {
|
||||
return SecureStore.getItemAsync(KEYS.CLAUDE_SESSION_KEY);
|
||||
}
|
||||
|
||||
export async function clearCodexAuth(): Promise<void> {
|
||||
await SecureStore.deleteItemAsync(KEYS.CODEX_AUTH);
|
||||
}
|
||||
|
||||
export async function clearClaudeSessionKey(): Promise<void> {
|
||||
await SecureStore.deleteItemAsync(KEYS.CLAUDE_SESSION_KEY);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
export function formatResetCountdown(resetAtSeconds: number): string {
|
||||
const nowSeconds = Math.floor(Date.now() / 1000);
|
||||
const diffSeconds = resetAtSeconds - nowSeconds;
|
||||
|
||||
if (diffSeconds <= 0) return "resetting now";
|
||||
if (diffSeconds < 60) return "resets in <1m";
|
||||
|
||||
const hours = Math.floor(diffSeconds / 3600);
|
||||
const minutes = Math.floor((diffSeconds % 3600) / 60);
|
||||
|
||||
if (hours > 0 && minutes > 0) return `resets in ${hours}h ${minutes}m`;
|
||||
if (hours > 0) return `resets in ${hours}h`;
|
||||
return `resets in ${minutes}m`;
|
||||
}
|
||||
|
||||
export function formatResetCountdownISO(isoString: string): string {
|
||||
const resetAtSeconds = Math.floor(new Date(isoString).getTime() / 1000);
|
||||
return formatResetCountdown(resetAtSeconds);
|
||||
}
|
||||
|
||||
export function windowLabel(limitWindowSeconds: number): string {
|
||||
if (limitWindowSeconds <= 3600) return "1h window";
|
||||
if (limitWindowSeconds <= 86400) return "24h window";
|
||||
return `${Math.round(limitWindowSeconds / 3600)}h window`;
|
||||
}
|
||||
Reference in New Issue
Block a user