AncrelDocumentationv0.3.0

Search documentation

    Playground / fe.client-core

    client-core playground

    Read HTTP responses, resume a GET event feed by cursor, or submit a single POST command and stream its result.

    Preview

    Example controls

    State matrix

    dark / comfortable
    VariantDefault
    Sample

    Published example

    API reference
    import { createHttpClient, subscribe, type HttpClientOptions } from '@ancrel/client-core'
    
    // The snapshot and event feed share an opaque cursor. Resume immediately after the snapshot.
    export default async function readUpdates(client: HttpClientOptions, signal?: AbortSignal) {
      const snapshot = await createHttpClient(client).json<{ cursor: string; messages: string[] }>('snapshot', { signal })
      const messages = [...snapshot.messages]
      const frames = subscribe({
        url: 'updates',
        client,
        signal,
        cursor: snapshot.cursor,
        commitCursor: 'on-dispatch',
        onUnexpectedEof: 'error',
        isTerminal: frame => frame.event === 'done',
      })
      for await (const frame of frames) {
        if (frame.event === 'message') messages.push(frame.data)
      }
      return messages
    }