import { $Fetch, FetchOptions } from 'ofetch'; import { RequestCasing, ResponseCasing } from '../common/constants/http.constant'; import { TokenManager } from './credentials/TokenManager'; import { CmsError } from './errors'; export interface IDeviceContext { deviceId?: string | (() => string | null); deviceIp?: string | (() => string | null); } export interface ICreateApiOptions { baseURL: string; tokenManager: TokenManager; clientId?: string; device?: IDeviceContext; /** Default request timeout in ms. */ timeout?: number; /** Retry count for transient failures. */ retry?: number; /** Global handler invoked for every error AFTER normalisation. */ onError?: (error: CmsError) => void; /** Invoked when a 401 is observed (e.g. force logout / redirect). */ onUnauthorized?: (error: CmsError) => void; /** * Async hook called whenever the access token changes (login, refresh, 401). * When provided, the hook fully owns token storage updates (including the * underlying TokenManager). When omitted, createApi falls back to updating * TokenManager directly. */ onToken?: (token: string | null) => void | Promise; /** * Silent token refresh. When provided, a 401 triggers ONE refresh attempt: * the returned token replaces the access token and the request is replayed * (via ofetch's native retry). Return `null` to signal "refresh failed" * (the SDK then clears the session + fires `onUnauthorized`). */ refresh?: () => Promise; /** * Response casing to request from the backend. `'camel'` (default) sends no * header (matches the backend default + the SDK's DTOs); `'snake'` sends * `x-response-casing: snake`. */ responseCasing?: ResponseCasing; /** * Request casing, declared to the backend via the `x-request-casing` header. * Default `'camel'` (no header; bodies keep the SDK's camelCase keys). `'snake'` * deep-converts request body keys to snake_case and sends the header. */ requestCasing?: RequestCasing; /** Extra per-request middlewares (the "dynamic interceptor" escape hatch). */ requestMiddlewares?: RequestMiddleware[]; } export interface IRequestContext { tokenManager: TokenManager; } /** Single request middleware. Return value is ignored; mutate `options`. */ export type RequestMiddleware = (options: FetchOptions, ctx: IRequestContext) => void; /** * Build a typed `$Fetch` instance for one endpoint (CMS, report, …). * * Hooks: * - `onRequest` → inject Authorization / DeviceID / DeviceIP / client_id * - `onResponse` → sliding-session token refresh from response header * - `onResponseError` → normalise to `CmsError`; on 401, silently refresh + * replay once (when `refresh` is provided), else trigger `onUnauthorized` * * Multiple endpoints share the same `tokenManager` so a refresh from one * backend is visible to all. */ export declare function createApi(opts: ICreateApiOptions): $Fetch; //# sourceMappingURL=createApi.d.ts.map