(path: string)
| 353 | } |
| 354 | |
| 355 | export async function get<T>(path: string): Promise<T> { |
| 356 | const auth = await Auth.get("codex") |
| 357 | if (!auth || auth.type !== "codex") { |
| 358 | throw new Error("Codex token not available") |
| 359 | } |
| 360 | |
| 361 | if (Date.now() > auth.expiresAt) { |
| 362 | try { |
| 363 | const tokens = await refresh(auth.refreshToken) |
| 364 | const idTokenInfo = parseIdToken(tokens.id_token) |
| 365 | await Auth.set("codex", { |
| 366 | ...auth, |
| 367 | accessToken: tokens.access_token, |
| 368 | refreshToken: tokens.refresh_token, |
| 369 | idToken: tokens.id_token, |
| 370 | expiresAt: Date.now() + (tokens.expires_in ?? 3600) * 1000, |
| 371 | accountId: idTokenInfo?.chatgpt_account_id ?? auth.accountId, |
| 372 | }) |
| 373 | auth.accessToken = tokens.access_token |
| 374 | } catch (e) { |
| 375 | throw new Error("Session expired, please login again") |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | const url = `${normalizeBaseUrl(Config.chatgptBaseUrl)}${path}` |
| 380 | const accountId = auth.accountId |
| 381 | |
| 382 | if (!accountId) { |
| 383 | throw new Error("ChatGPT account ID not available, please re-run login") |
| 384 | } |
| 385 | |
| 386 | const response = await fetch(url, { |
| 387 | method: "GET", |
| 388 | headers: { |
| 389 | Authorization: `Bearer ${auth.accessToken}`, |
| 390 | "chatgpt-account-id": accountId, |
| 391 | "Content-Type": "application/json", |
| 392 | }, |
| 393 | }) |
| 394 | |
| 395 | if (!response.ok) { |
| 396 | const body = await response.text() |
| 397 | throw new Error(`Request failed with status ${response.status}: ${body}`) |
| 398 | } |
| 399 | |
| 400 | return (await response.json()) as T |
| 401 | } |
| 402 | |
| 403 | export async function ensureValidTokenFor( |
| 404 | authKey: string, |
nothing calls this directly
no test coverage detected