Referensi API
createTide, TideProvider, dan hook WebSocket.
createTide(options)
Dua varian: shorthand url (v1.1, direkomendasikan) atau fetcher kustom. Keduanya berbagi opsi dasar yang sama.
ts
// Variant A: url shorthand (v1.1)
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
ts
{
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
prefetch: () => void
} TideProvider
tsx
<TideProvider
ws={{ url: string, topics?: string[] }}
defaults={{ staleTime, cacheTime, pollInterval, retries, persist }}
reconnect={{ baseMs?: number, maxMs?: number } | false} // v1.1
heartbeat={number | false} // v1.1
>
{children}
</TideProvider> Hook WebSocket
ts
import { useTideWS, useTideWSConnected } from '@uikode/tide'
const lastMessage = useTideWS() // Accessor<any>
const connected = useTideWSConnected() // Accessor<boolean>