TIDE • Referensi

API Reference

createTide, TideProvider, and the WebSocket hooks.

API Reference

createTide, TideProvider, and the WebSocket hooks.

createTide(options)

Two variants: url shorthand (recommended) or custom fetcher. Both share the same base options.

TypeScript
// Variant A: url shorthand (recommended)
createTide<T>({
  key: string | (() => string),       // reactive key supported
  url: string | (() => string),       // reactive url supported
  transform?: (body: any) => T,       // reshape response
  etag?: boolean,                     // ETag/304 support (default: false)
  wsPath?: string,                    // dot-notation WS extraction
  staleTime?: number,                 // default 30000
  cacheTime?: number,                 // default 300000
  pollInterval?: number,              // default 10000
  retries?: number,                   // default 3
  persist?: boolean,                  // default true (sessionStorage)
  hashCompare?: boolean,              // skip update if identical
  enabled?: () => boolean,            // reactive pause/resume
})

// Variant B: custom fetcher
createTide<T>({
  key: string | (() => string),
  fetcher: (o?: { signal?: AbortSignal }) => Promise<T>,
  ws?: (data: any) => T | null,
  wsPath?: string,
  // ...same base options as above
})

TideResult

TypeScript
{
  data: Accessor<T | null>
  loading: Accessor<boolean>     // first load, no cache
  refreshing: Accessor<boolean>  // background SWR refresh
  stale: Accessor<boolean>
  error: Accessor<string | null>
  refresh: () => Promise<void>
  mutate: (fn: (prev: T | null) => T) => void  // optimistic
  pause: () => void            // NEW v1.2: stop polling + abort inflight
  resume: () => void           // NEW v1.2: restart polling + fetch
  prefetch: () => void
}

TideProvider

tsx
<TideProvider
  ws={{ url: string, topics?: string[] }}
  defaults={{ staleTime, cacheTime, pollInterval, retries, persist }}
  reconnect={{ baseMs?: number, maxMs?: number } | false}  // exponential backoff
  heartbeat={number | false}                                // WS keepalive
>
  {children}
</TideProvider>

WebSocket hooks

TypeScript
import { useTideWS, useTideWSConnected } from '@uikode/tide'

const lastMessage = useTideWS()        // Accessor<any>
const connected = useTideWSConnected() // Accessor<boolean>