Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

1.1 KB
Raw Blame
const STATE_REQUEST_TIMEOUT_MS = 15_000;

export type TraeAuthorizeStateResult =
  { ok: true; state: string } | { ok: false; message: string | null };

/**
 * Asks the server for the one-time state the Trae login is tied to. On failure `message`
 * is what the server said (or the HTTP status), and null when the request itself failed
 * or timed out, so the caller can fall back to its own wording.
 */
export async function requestTraeAuthorizeState(
  fetchImpl: typeof fetch = fetch
): Promise<TraeAuthorizeStateResult> {
  try {
    const res = await fetchImpl("/api/oauth/trae/authorize-state", {
      method: "POST",
      signal: AbortSignal.timeout(STATE_REQUEST_TIMEOUT_MS),
    });
    const data = await res.json().catch(() => null);
    if (res.ok && typeof data?.state === "string") return { ok: true, state: data.state };
    const serverMessage =
      typeof data?.error?.message === "string"
        ? data.error.message
        : typeof data?.error === "string"
          ? data.error
          : null;
    return { ok: false, message: serverMessage || `HTTP ${res.status}` };
  } catch {
    return { ok: false, message: null };
  }
}