/** * SDK Core Types * * Base types for SDK configuration that are shared across all scopeData. * Scope-specific types (like MexicoSDKOptions) are defined in their * respective namespace modules. */ import type { Locale, Scope } from "@artu-ai/shared"; import type { SDKSessionOptions, SDKSessionOptionsWithReturnTo, SessionResultWithoutRedirect, SessionResultWithRedirect } from "../utils/session"; import type { APIVersion } from "../utils/trpc-client"; /** * SDK environment mode * - "test": Uses test data, no real regulatory submissions * - "live": Uses production data, real regulatory submissions */ export type Environment = "test" | "live"; /** * Base SDK configuration options (common to all scopes) */ /** * Authentication configuration. * * - apiKey (default): use WorkOS API key (header) * - cookie: rely on browser/forwarded cookies (no auth header, credentials include) */ export type ComplianceAuthMode = "apiKey" | "cookie"; export interface ComplianceAPIKeyAuth { /** * Authentication mode * - "apiKey": use WorkOS API key (header) * - "cookie": rely on browser/forwarded cookies (no auth header, credentials include) */ mode?: "apiKey"; /** * API key for authentication. * Obtained from WorkOS dashboard. * * If not provided, falls back to the `ARTU_API_KEY` environment variable. * * @example "ak_test_abc123..." */ apiKey?: string; } export type FetchCredentials = "include" | "omit" | "same-origin"; export interface ComplianceCookieAuth { /** * Authentication mode * - "apiKey": use WorkOS API key (header) * - "cookie": rely on browser/forwarded cookies (no auth header, credentials include) */ mode?: "cookie"; /** * Optional fetch credentials policy override. * Defaults to "include" when using cookie mode. */ credentials?: FetchCredentials; /** * Custom headers to include with each request. * Useful for server-side cookie forwarding in Next.js. * * @example * ```typescript * // Forward cookies in Next.js server component * const cookieStore = await cookies(); * const sdk = new ComplianceSDK({ * mode: "cookie", * environment: "test", * headers: { Cookie: cookieStore.toString() }, * }); * ``` */ headers?: Record; } export type ComplianceAuthOptions = ComplianceAPIKeyAuth | ComplianceCookieAuth; export type ComplianceSDKOptionsBase = ComplianceAuthOptions & { /** * Target environment for data isolation. * - "test": Uses test data, no real regulatory submissions * - "live": Uses production data, real regulatory submissions * * Priority order: * 1. Explicit environment option * 2. `ARTU_ENVIRONMENT` environment variable * 3. Default: "test" * * The API key must have permissions for the requested environment. */ environment?: Environment; /** * Base URL for the API. * * Priority order: * 1. Explicit baseUrl option * 2. `ARTU_BASE_URL` environment variable * 3. Default production URL: https://api.artu.ai * * Override for local development or custom deployments. * * @example "http://localhost:3001" */ baseUrl?: string; /** * Organization ID for cookie authentication. * * When using cookie auth mode, this restricts login to a specific organization. * Useful for custom apps to prevent users from logging into the wrong organization. * * Priority order: * 1. Explicit organizationId option * 2. `ARTU_ORGANIZATION_ID` environment variable */ organizationId?: string; /** * API version to use. * Defaults to latest stable version. * * @default "v1" */ apiVersion?: APIVersion; /** * Request timeout in milliseconds. * * @default 30000 (30 seconds) */ timeout?: number; /** * Locale for SDK-owned API requests and SDK-thrown configuration errors. * Supports the SDK's `Locale` values and IETF tags whose primary language * maps to a supported locale (for example, "es-MX" resolves to "es"). * * @default "en" */ locale?: Locale | string; /** * Validation settings that mirror tenant-level settings. * * Set these to match your tenant's `settings` for consistent * client-side validation behavior. */ validation?: SDKValidationOptions; }; /** * Client-side validation options. * * These mirror the tenant-level `settings` object so the SDK can * enforce the same rules client-side before sending requests. */ export interface SDKValidationOptions { /** * Enable client-side postal code format validation on addresses. * * When true, the SDK validates postal code format against country rules * before sending requests to the API. * * @default false */ enablePostalCodeValidation?: boolean; } /** * Distributive version of Omit that preserves union members. * * Important when omitting keys from option types that include unions (like auth modes), * otherwise `Omit` can erase mode-specific keys (e.g. `apiKey`) due to `keyof` behavior on unions. */ export type DistributiveOmit = T extends unknown ? Omit : never; /** * SDK options - unscoped (no scope specified) */ export type ComplianceSDKOptionsUnscoped = ComplianceSDKOptionsBase & { scope?: undefined; }; /** * Base SDK properties shared across all SDK scopes. * Contains environment info, scope helpers, and supporting resources. */ export interface BaseComplianceSDK { readonly environment: Environment; readonly apiVersion: APIVersion; readonly locale: Locale; readonly isTest: boolean; readonly isLive: boolean; readonly scope: Scope | undefined; readonly isScoped: boolean; /** * Updates the locale used by subsequent SDK-owned API requests. */ setLocale(locale: Locale | string): void; /** * Fetches current session info from the API (/auth/me). * Only available in cookie auth mode. * * @example Basic usage (session can be null) * ```typescript * const result = await sdk.session(); * if (result.isAuthenticated) { * console.log("User:", result.session.userId); * } * ``` * * @example With returnTo (strongly typed - redirectUrl is present when not authenticated) * ```typescript * const result = await sdk.session({ returnTo: "https://..." }); * if (result.redirectUrl) { * redirect(result.redirectUrl); * } * // result.session is guaranteed here (TypeScript knows it's not null) * console.log("User:", result.session.userId); * ``` */ session(options: SDKSessionOptionsWithReturnTo): Promise; session(options?: SDKSessionOptions): Promise; }