import type { JsMsg, MsgHdrs as NatsHeaders, Msg, Subscription } from 'nats.ws'; export type { JsMsg, Msg, Subscription } from 'nats.ws'; export type JetStreamDeliverPolicy = 'new' | 'byStartSequence'; export interface JetStreamOrderedSubscribeOptions { streamName: string; filterSubject: string; deliverPolicy: JetStreamDeliverPolicy; /** Required when deliverPolicy === 'byStartSequence'. */ optStartSeq?: number; /** * Auto-cleanup the ephemeral consumer after this idle time. Defaults to * nats.ws's own. Applies to the consumer created first: nats.ws only reads it * when the start sequence is the one it was built with, so every consumer it * recreates afterwards reverts to the library default. */ inactiveThresholdMs?: number; /** AbortSignal to tear down the consumer. */ signal?: AbortSignal; /** * The ordered consumer had to be rebuilt: the server dropped the ephemeral * consumer (its inactivity threshold elapsed while the page was suspended or * offline), heartbeats were missed, or a sequence gap was detected. nats.ws * recreates it from the last delivered sequence, so this is NOT an error — * but everything that aged out of the stream while it was gone is * unrecoverable from the tail, and no connection event announces it (the * WebSocket never dropped). Callers that keep persisted history alongside * the live tail must refetch it here, or they keep showing a snapshot from * before the gap. */ onRecovered?: () => void; } export interface JetStreamSubscriptionHandle { unsubscribe(): void; } export interface NatsClientOptions { /** * NATS server URL(s), for example: * - "wss://nats.example.com:443" * - ["wss://nats-1.example.com:443", "wss://nats-2.example.com:443"] */ servers: string | string[]; /** * Connection name (shows up in NATS monitoring). */ name?: string; /** * Auth options (pick one: token or user/pass). */ token?: string; user?: string; pass?: string; /** * Reconnect behavior. */ reconnect?: boolean; maxReconnectAttempts?: number; reconnectTimeWaitMs?: number; /** * Exponential backoff for reconnect delays. * When set, uses `reconnectDelayHandler` from nats.ws under the hood, * overriding `reconnectTimeWaitMs`. */ exponentialBackoff?: { /** Initial delay in ms (default: 1000) */ initialDelayMs?: number; /** Maximum delay cap in ms (default: 30000) */ maxDelayMs?: number; /** Multiplier per attempt (default: 2) */ multiplier?: number; /** Add random jitter 0-50% of delay to prevent thundering herd (default: true) */ jitter?: boolean; }; /** * Ping behavior (keep-alive). */ pingIntervalMs?: number; maxPingOut?: number; /** * Optional inbox prefix (useful if you want to isolate request/reply inboxes). */ inboxPrefix?: string; /** * Connection timeout in milliseconds (maps to `nats.ws` connect option `timeout`). * If you see `NatsError: TIMEOUT` during connect, increase this. */ connectTimeoutMs?: number; } export interface NatsSubscribeOptions { /** * Queue group for load-balancing messages across subscribers. */ queue?: string; /** * Auto-unsubscribe after receiving this many messages. */ max?: number; /** * Abort signal to stop message iteration and unsubscribe. */ signal?: AbortSignal; } export type NatsHeadersInit = Record | NatsHeaders | undefined; export interface NatsPublishOptions { headers?: NatsHeadersInit; } export interface NatsRequestOptions { timeoutMs?: number; headers?: NatsHeadersInit; } export interface NatsSubscriptionHandle { readonly subscription: Subscription; unsubscribe(): void; } export type NatsStatus = 'connecting' | 'connected' | 'disconnected' | 'reconnecting' | 'closed' | 'error'; export interface NatsStatusEvent { status: NatsStatus; data?: unknown; } export interface NatsClient { connect(): Promise; close(): Promise; isConnected(): boolean; publishBytes(subject: string, payload: Uint8Array, options?: NatsPublishOptions): void; publishString(subject: string, payload: string, options?: NatsPublishOptions): void; publishJson(subject: string, payload: T, options?: NatsPublishOptions): void; requestBytes(subject: string, payload: Uint8Array, options?: NatsRequestOptions): Promise; requestString(subject: string, payload: string, options?: NatsRequestOptions): Promise; requestJson(subject: string, payload: TRequest, options?: NatsRequestOptions): Promise; subscribeBytes(subject: string, onMessage: (msg: Msg) => void | Promise, options?: NatsSubscribeOptions): NatsSubscriptionHandle; subscribeString(subject: string, onMessage: (payload: string, msg: Msg) => void | Promise, options?: NatsSubscribeOptions): NatsSubscriptionHandle; subscribeJson(subject: string, onMessage: (payload: T, msg: Msg) => void | Promise, options?: NatsSubscribeOptions): NatsSubscriptionHandle; /** * Subscribe to a JetStream subject via an ephemeral OrderedConsumer (no acks). * Use `optStartSeq` with `deliverPolicy: 'byStartSequence'` to resume from a known offset, * or `deliverPolicy: 'new'` to live-tail. */ subscribeJetStreamOrdered(onMessage: (msg: JsMsg) => void | Promise, options: JetStreamOrderedSubscribeOptions): Promise; onStatus(listener: (event: NatsStatusEvent) => void): () => void; } export declare function createNatsClient(options: NatsClientOptions): NatsClient; //# sourceMappingURL=nats.d.ts.map