cavi-ai/
GitHub ↗

@cavi-ai/api-client/core/transport

Package subpath: ./core/transport

abortableSleep#

Kind: function

ts
export declare function abortableSleep(delayMs: number, signal?: AbortSignal): Promise<void>;

computeBackoffDelay#

Kind: function

ts
export declare function computeBackoffDelay(policy: TransportRetryPolicy, attempt: number, random: number, retryAfterMs?: number): number;

contentLengthCodec#

Kind: function

ts
export declare function contentLengthCodec<T = unknown>(options?: ContentLengthCodecOptions): TransportFrameCodec<T>;

ContentLengthCodecOptions#

Kind: type

ts
export type ContentLengthCodecOptions = Readonly<{
    maxHeaderBytes?: number;
    maxBodyBytes?: number;
}>;

createFramedMessageChannel#

Kind: function

ts
export declare function createFramedMessageChannel<T>(bytes: TransportByteChannel, codec: TransportFrameCodec<T>): TransportMessageChannel<T>;

createHttpTransport#

Kind: function

ts
export declare function createHttpTransport(options: HttpTransportOptions): HttpTransport;

createJsonRpcTransport#

Kind: function

ts
export declare function createJsonRpcTransport(options: CreateJsonRpcTransportOptions): JsonRpcTransport;

CreateJsonRpcTransportOptions#

Kind: type

ts
export type CreateJsonRpcTransportOptions = Readonly<{
    channel: TransportMessageChannel<unknown>;
    id?: () => JsonRpcId;
    onProtocolError?: (error: TransportError) => void;
}>;

createSseTransport#

Kind: function

ts
export declare function createSseTransport(transportOptions: SseTransportOptions): SseTransport;

createTransportLifecycle#

Kind: function

ts
export declare function createTransportLifecycle(listener?: (event: TransportLifecycleEvent) => void): TransportLifecycle;

createWebSocketTransport#

Kind: function

ts
export declare function createWebSocketTransport(transportOptions?: WebSocketTransportOptions): WebSocketTransport;

getTransportErrorMetadata#

Kind: function

ts
export declare function getTransportErrorMetadata(error: unknown): TransportErrorMetadata | undefined;

HttpTransport#

Kind: interface

ts
export interface HttpTransport {
    request<T = unknown>(request: HttpTransportRequest): Promise<T>;
}

HttpTransportOptions#

Kind: type

ts
export type HttpTransportOptions = Readonly<{
    baseUrl: string;
    defaultHeaders?: Readonly<Record<string, string>>;
    auth?: TransportAuthResolver;
    fetchImpl?: typeof fetch;
    dependencies?: Partial<TransportDependencies>;
    onLifecycleEvent?: (event: TransportLifecycleEvent) => void;
}>;

HttpTransportRequest#

Kind: type

ts
export type HttpTransportRequest = Readonly<{
    method: "GET" | "HEAD" | "POST" | "PUT" | "PATCH" | "DELETE";
    path: string;
    headers?: Readonly<Record<string, string>>;
    body?: BodyInit | null;
    response?: "response" | "json" | "text" | "bytes";
    idempotencyKey?: string;
    retry?: TransportRetryPolicy;
    signal?: AbortSignal;
}>;

JsonFrameCodecOptions#

Kind: type

ts
export type JsonFrameCodecOptions = Readonly<{
    maxFrameBytes?: number;
}>;

jsonLinesCodec#

Kind: function

ts
export declare function jsonLinesCodec<T = unknown>(options?: JsonFrameCodecOptions): TransportFrameCodec<T>;

JsonRpcTransport#

Kind: interface

ts
export interface JsonRpcTransport {
    request<T = unknown>(method: string, params?: unknown, options?: {
        signal?: AbortSignal;
    }): Promise<T>;
    notify(method: string, params?: unknown, options?: {
        signal?: AbortSignal;
    }): Promise<void>;
    onNotification(listener: (method: string, params: unknown) => void): () => void;
    close(): Promise<void>;
}

jsonTextCodec#

Kind: function

ts
export declare function jsonTextCodec<T = unknown>(options?: JsonFrameCodecOptions): TransportFrameCodec<T>;

normalizeTransportAbort#

Kind: function

ts
export declare function normalizeTransportAbort(signal?: AbortSignal, cause?: unknown): ApiClientError;

resolveTransportHeaders#

Kind: function

ts
export declare function resolveTransportHeaders(defaults?: Readonly<Record<string, string>>, resolver?: TransportAuthResolver): Promise<Record<string, string>>;

runTransportAttempts#

Kind: function

ts
export declare function runTransportAttempts<T>(options: Readonly<{
    kind: TransportKind;
    operation: string;
    safety: TransportOperationSafety;
    policy: TransportRetryPolicy;
    execute: (context: TransportAttemptContext) => Promise<T>;
    auth?: TransportAuthResolver;
    headers?: Readonly<Record<string, string>>;
    dependencies?: Partial<TransportDependencies>;
    lifecycle?: TransportLifecycle;
    signal?: AbortSignal;
}>): Promise<T>;

SseConnectOptions#

Kind: type

ts
export type SseConnectOptions = Readonly<{
    path: string;
    headers?: Readonly<Record<string, string>>;
    cursor?: string;
    reconnect?: TransportReconnectPolicy;
    signal?: AbortSignal;
    onMessage: (message: SseMessage) => void;
}>;

SseSubscription#

Kind: interface

ts
export interface SseSubscription {
    readonly done: Promise<void>;
    close(): void;
}

SseTransport#

Kind: interface

ts
export interface SseTransport {
    subscribe(options: SseConnectOptions): SseSubscription;
}

SseTransportOptions#

Kind: type

ts
export type SseTransportOptions = Readonly<{
    baseUrl: string;
    defaultHeaders?: Readonly<Record<string, string>>;
    auth?: TransportAuthResolver;
    fetchImpl?: typeof fetch;
    dependencies?: Partial<TransportDependencies>;
    onLifecycleEvent?: (event: TransportLifecycleEvent) => void;
}>;

TransportAttemptContext#

Kind: type

ts
export type TransportAttemptContext = Readonly<{
    attempt: number;
    headers: Readonly<Record<string, string>>;
    signal?: AbortSignal;
}>;

TransportAuth#

Kind: type

ts
export type TransportAuth = Readonly<{
    headers?: Readonly<Record<string, string>>;
}>;

TransportAuthResolver#

Kind: type

ts
export type TransportAuthResolver = () => TransportAuth | Promise<TransportAuth>;

TransportByteChannel#

Kind: interface

ts
export interface TransportByteChannel {
    write(chunk: Uint8Array, signal?: AbortSignal): Promise<void>;
    subscribe(listener: (chunk: Uint8Array) => void): () => void;
    /** Invokes once on local or remote close, immediately when already closed. */
    subscribeClose(listener: (error?: unknown) => void): () => void;
    close(): Promise<void>;
}

TransportDependencies#

Kind: type

ts
export type TransportDependencies = Readonly<{
    now: () => number;
    random: () => number;
    sleep: (delayMs: number, signal?: AbortSignal) => Promise<void>;
}>;

TransportError#

Kind: class

ts
export declare class TransportError extends ApiClientError {
    readonly transport: TransportErrorMetadata;
    constructor(message: string, options: {
        metadata: TransportErrorMetadata;
        cause?: unknown;
    });
}

TransportErrorMetadata#

Kind: type

ts
export type TransportErrorMetadata = Readonly<{
    kind: TransportKind;
    phase: TransportPhase;
    operation: string;
    retryable: boolean;
    attempt: number;
    status?: number;
    code?: string | number;
    retryAfterMs?: number;
}>;

TransportFrameCodec#

Kind: interface

ts
export interface TransportFrameCodec<T> {
    encode(value: T): Uint8Array;
    createDecoder(): TransportFrameDecoder<T>;
}

TransportFrameDecoder#

Kind: interface

ts
export interface TransportFrameDecoder<T> {
    push(chunk: Uint8Array): readonly T[];
    finish(): readonly T[];
}

TransportKind#

Kind: type

ts
export type TransportKind = "http" | "sse" | "websocket" | "json-rpc" | "stdio" | "unix";

TransportLifecycle#

Kind: type

ts
export type TransportLifecycle = Readonly<{
    emit: (event: TransportLifecycleEvent) => void;
    subscribe: (listener: (event: TransportLifecycleEvent) => void) => () => void;
}>;

TransportLifecycleEvent#

Kind: type

ts
export type TransportLifecycleEvent = Readonly<{
    state: "connecting" | "connected" | "retrying" | "reconnected" | "closed";
    kind: TransportKind;
    operation: string;
    attempt: number;
    delayMs?: number;
}>;

TransportMessageChannel#

Kind: interface

ts
export interface TransportMessageChannel<T = unknown> {
    send(message: T, signal?: AbortSignal): Promise<void>;
    subscribe(listener: (message: T) => void): () => void;
    /** Invokes once on local or remote close, immediately when already closed. */
    subscribeClose(listener: (error?: unknown) => void): () => void;
    close(reason?: string): Promise<void>;
}

TransportOperationSafety#

Kind: type

ts
export type TransportOperationSafety = "read" | "idempotent" | "connection" | "mutation";

TransportPhase#

Kind: type

ts
export type TransportPhase = "configure" | "authenticate" | "connect" | "request" | "decode" | "close";

TransportReconnectPolicy#

Kind: type

ts
export type TransportReconnectPolicy = TransportRetryPolicy & Readonly<{
    dedupeCapacity?: number;
}>;

TransportRetryPolicy#

Kind: type

ts
export type TransportRetryPolicy = Readonly<{
    maxAttempts: number;
    baseDelayMs: number;
    maxDelayMs: number;
    jitterRatio?: number;
    deadlineMs?: number;
}>;

validateTransportRetryPolicy#

Kind: function

ts
export declare function validateTransportRetryPolicy(policy: TransportRetryPolicy): void;

WebSocketConnectOptions#

Kind: type

ts
export type WebSocketConnectOptions = Readonly<{
    url: string | (() => string | Promise<string>);
    protocols?: readonly string[] | (() => readonly string[] | Promise<readonly string[]>);
    reconnect?: TransportReconnectPolicy;
    signal?: AbortSignal;
    decode?: (data: unknown) => unknown | Promise<unknown>;
    encode?: (message: unknown) => string | ArrayBufferLike | Blob | ArrayBufferView;
}>;

WebSocketLike#

Kind: interface

ts
export interface WebSocketLike {
    readonly readyState: number;
    send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void;
    close(code?: number, reason?: string): void;
    addEventListener(type: string, listener: EventListener): void;
    removeEventListener(type: string, listener: EventListener): void;
}

WebSocketTransport#

Kind: interface

ts
export interface WebSocketTransport {
    connect(options: WebSocketConnectOptions): TransportMessageChannel<unknown> & Readonly<{
        ready: Promise<void>;
    }>;
}

WebSocketTransportOptions#

Kind: type

ts
export type WebSocketTransportOptions = Readonly<{
    webSocketFactory?: (url: string, protocols?: readonly string[]) => WebSocketLike;
    dependencies?: Partial<TransportDependencies>;
    onLifecycleEvent?: (event: TransportLifecycleEvent) => void;
}>;