Skip to content
forma.
v3.0
☰
Browse documentation
Start here
Build interfaces
Native rendering
Tooling
Security and operations
Architecture
Application guides
API reference

Docs/API reference

FORMA 3.0

@getforma/core/http

Reactive fetch, server-sent events and WebSocket clients with their option and result types.

View source ↗Markdown ↓Source 2006423
On this page

Generated from the published 3.0.0 TypeScript declarations. Signatures, overloads and JSDoc are taken from the package; private class members are omitted. Declaration links point to version-pinned package files.

These browser helpers use the platform's network APIs. Type parameters describe expected values; they do not replace application input validation or server authorization.

Read the usage guide or all entry points.

11 exports · published 3.0.0 declarations

ts
import { createFetch } from '@getforma/core/http';

Values and functions

createFetch#

Function export · Declaration, line 30

ts
declare function createFetch<T>(url: string | (() => string), options?: FetchOptions<T>): FetchResult<T>;

Create a reactive fetch that exposes data/error/loading as signals.

If url is a signal getter (function), an effect auto-refetches when it changes.

ts
const { data, loading, error, refetch, abort } = createFetch<User[]>('/api/users');

Related declarations: FetchOptions, FetchResult

createSSE#

Function export · Declaration, line 70

ts
declare function createSSE<T = unknown>(url: string, options?: SSEOptions<T>): SSEConnection<T>;

Create a reactive Server-Sent Events connection.

ts
const sse = createSSE<{ message: string }>('/api/events');
createEffect(() => {
  const msg = sse.data();
  if (msg) console.log(msg.message);
});

Related declarations: SSEConnection, SSEOptions

createWebSocket#

Function export · Declaration, line 121

ts
declare function createWebSocket<TSend = unknown, TReceive = unknown>(url: string, options?: WSOptions<TReceive>): WSConnection<TSend, TReceive>;

Create a reactive WebSocket connection with auto-reconnect.

A closed connection is retried up to maxReconnects times. The n-th retry waits a delay drawn uniformly between 0 and reconnectInterval * 2^(n-1) (full jitter), so the clients of a server that restarts do not all reconnect in the same instant. A successful open restarts the count.

Verified by: src/http/tests/ws-reconnect.test.ts > "with a seeded random source, the n-th reconnect delay lies between 0 and 2^(n-1) s and differs across simulated clients" Verified by: src/http/tests/ws-reconnect.test.ts > "a successful connection restarts the cap at reconnectInterval"

ts
const ws = createWebSocket<string, ChatMessage>('wss://chat.example.com');
ws.send('hello');
createEffect(() => {
  const msg = ws.data();
  if (msg) console.log(msg);
});

Related declarations: WSConnection, WSOptions

fetchJSON#

Function export · Declaration, line 38

ts
declare function fetchJSON<T>(url: string, options?: RequestInit): Promise<T>;

One-shot fetch that returns parsed JSON.

ts
const users = await fetchJSON<User[]>('/api/users');

Exported types

FetchOptions#

Interface export · Declaration, line 7

ts
interface FetchOptions<T> extends Omit<RequestInit, 'signal'> {
    base?: string;
    params?: Record<string, string>;
    timeout?: number;
    transform?: (data: unknown) => T;
}

Forma HTTP - Fetch

Typed fetch wrapper with reactive signal integration. Zero dependencies — native browser APIs only.

FetchResult#

Interface export · Declaration, line 13

ts
interface FetchResult<T> {
    data: () => T | null;
    error: () => Error | null;
    loading: () => boolean;
    refetch: () => Promise<void>;
    abort: () => void;
}

SSEConnection#

Interface export · Declaration, line 52

ts
interface SSEConnection<T = unknown> {
    data: () => T | null;
    error: () => Event | null;
    connected: () => boolean;
    close: () => void;
    on(event: string, handler: (data: unknown) => void): () => void;
}

SSEOptions#

Interface export · Declaration, line 46

ts
interface SSEOptions<T = unknown> {
    withCredentials?: boolean;
    headers?: Record<string, string>;
    parse?: (data: string) => T;
}

Forma HTTP - Server-Sent Events

Reactive SSE wrapper with signal integration. Zero dependencies — native browser APIs only.

Member notes

parse

Custom parser for incoming messages. Defaults to JSON.parse with raw data fallback.

WSConnection#

Interface export · Declaration, line 94

ts
interface WSConnection<TSend = unknown, TReceive = unknown> {
    data: () => TReceive | null;
    status: () => WSStatus;
    send(data: TSend): void;
    close(): void;
    on(handler: (data: TReceive) => void): () => void;
}

Related declarations: WSStatus

WSOptions#

Interface export · Declaration, line 79

ts
interface WSOptions<TReceive = unknown> {
    protocols?: string | string[];
    reconnect?: boolean;
    reconnectInterval?: number;
    maxReconnects?: number;
    random?: () => number;
    parse?: (data: string) => TReceive;
}

Member notes

reconnectInterval

The cap of the first reconnect delay, in ms (default 1000); it doubles each attempt.

random

The uniform source in [0, 1) the reconnect delay is drawn from. Defaults to Math.random; a test passes a seeded one to replay a run. A value that is not a function, or a draw outside [0, 1), falls back to Math.random.

parse

Custom parser for incoming messages. Defaults to JSON.parse with raw data fallback.

WSStatus#

Type alias export · Declaration, line 78

ts
type WSStatus = 'connecting' | 'open' | 'closed' | 'error';

Forma HTTP - WebSocket

Reactive WebSocket wrapper with auto-reconnect and signal integration. Zero dependencies — native browser APIs only.

Other entry points

@getforma/core · @getforma/core/runtime · @getforma/core/runtime-hardened · @getforma/core/runtime-csp · @getforma/core/tc39 · @getforma/core/storage · JSX namespace

Find your next answer.

Search Forma 3.0 documentation, guides and examples.