45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
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>;
|
|
}
|