import { type Locale } from "@artu-ai/shared"; import type { Environment } from "./environment"; export type SessionType = "cookie" | "apiKey"; /** * Base session info shared across all auth modes. */ interface SessionInfoBase { organizationId: string; permissions: string[]; environment: Environment; } /** * Session info for cookie auth mode. * Includes userId since there's an authenticated user. */ export interface CookieSessionInfo extends SessionInfoBase { type: "cookie"; userId: string; } /** * Session info for API key auth mode. * No userId since API keys represent service accounts, not users. */ export interface ApiKeySessionInfo extends SessionInfoBase { type: "apiKey"; } /** * Discriminated union for session info. * Use `session.type` to narrow and access mode-specific fields. * * @example * ```typescript * if (session.type === "cookie") { * console.log(session.userId); // TypeScript knows userId exists * } * ``` */ export type SessionInfo = CookieSessionInfo | ApiKeySessionInfo; /** * Base options for fetchSession. */ interface SessionOptionsBase { /** * Base URL for the API (e.g. https://api.artu.ai) */ baseUrl: string; /** * Environment header value ("test" | "live") */ environment: Environment; /** * Credentials policy for fetch. Defaults to "include". */ credentials?: "include" | "omit" | "same-origin"; /** * Custom headers to include with the request. * Useful for server-side cookie forwarding. */ headers?: Record; /** * Custom fetch implementation (optional). */ fetchImpl?: typeof fetch; /** * Optional organization ID to lock the login to a specific organization. * When provided, users will only be able to log in to this organization. * Useful for custom apps to prevent users from logging into the wrong organization. */ organizationId?: string; /** * API key for authorization (API key auth mode). * When provided, adds Authorization header to the request. */ apiKey?: string; /** * Locale to advertise to the API. * * @default "en" */ locale?: Locale; } /** * Options for fetchSession when returnTo is provided. */ export interface SessionOptionsWithReturnTo extends SessionOptionsBase { /** * Full URL to return to after authentication. * When provided, redirectUrl will be included in the result when not authenticated. */ returnTo: string; } /** * Options for fetchSession when returnTo is not provided. */ export interface SessionOptionsWithoutReturnTo extends SessionOptionsBase { returnTo?: never; } /** * Combined session options type. */ export type SessionOptions = SessionOptionsWithReturnTo | SessionOptionsWithoutReturnTo; /** * Base SDK session options shared across all variants. */ interface SDKSessionOptionsBase { /** * Optional organization ID to lock the login to a specific organization. * When provided, users will only be able to log in to this organization. * Useful for custom apps to prevent users from logging into the wrong organization. */ organizationId?: string; } /** * Options for sdk.session() when returnTo is provided. * Result will include redirectUrl when not authenticated. */ export interface SDKSessionOptionsWithReturnTo extends SDKSessionOptionsBase { /** * Full URL to return to after authentication. * When provided, redirectUrl will be included in the result when not authenticated. */ returnTo: string; } /** * Options for sdk.session() when returnTo is not provided. */ export interface SDKSessionOptionsWithoutReturnTo extends SDKSessionOptionsBase { returnTo?: never; } /** * Combined SDK session options type. */ export type SDKSessionOptions = SDKSessionOptionsWithReturnTo | SDKSessionOptionsWithoutReturnTo; /** * Result when user is authenticated. */ export interface SessionResultAuthenticated { session: SessionInfo; isAuthenticated: true; redirectUrl?: never; } /** * Result when user is NOT authenticated and returnTo was provided. */ export interface SessionResultUnauthenticatedWithRedirect { session: null; isAuthenticated: false; redirectUrl: string; } /** * Result when user is NOT authenticated and returnTo was NOT provided. */ export interface SessionResultUnauthenticatedNoRedirect { session: null; isAuthenticated: false; redirectUrl?: never; } /** * Session result when returnTo is provided. * Either authenticated (session present, no redirect) or unauthenticated (no session, redirect present). * This allows TypeScript to narrow: if redirectUrl exists, session is null; otherwise session is present. */ export type SessionResultWithRedirect = SessionResultAuthenticated | SessionResultUnauthenticatedWithRedirect; /** * Session result when returnTo is NOT provided. */ export type SessionResultWithoutRedirect = SessionResultAuthenticated | SessionResultUnauthenticatedNoRedirect; /** * Generic session result type. */ export type SessionResult = SessionResultAuthenticated | SessionResultUnauthenticatedWithRedirect | SessionResultUnauthenticatedNoRedirect; export declare function createAuthLoginUrl(options: { apiBaseUrl: string; returnTo: string; organizationId?: string; }): string; /** * Fetches session from /auth/me. * Returns a result object with session info if authenticated, * or null session with optional redirectUrl if not. * * @example With returnTo (includes redirectUrl when not authenticated) * ```typescript * const result = await fetchSession({ * baseUrl: "https://api.artu.ai", * environment: "test", * returnTo: "https://dashboard.artu.ai/env/test/dashboard", * }); * * if (result.redirectUrl) { * redirect(result.redirectUrl); * } * * // TypeScript knows session is guaranteed here * console.log("User:", result.session.userId); * ``` * * @example Without returnTo (just check if authenticated) * ```typescript * const result = await fetchSession({ * baseUrl: "https://api.artu.ai", * environment: "test", * }); * * if (result.isAuthenticated) { * console.log("User:", result.session.userId); * } * ``` */ export declare function fetchSession(options: SessionOptionsWithReturnTo): Promise; export declare function fetchSession(options: SessionOptionsWithoutReturnTo): Promise; export {};