brick.ts

@brickkit/core/client

The typed HTTP client a frontend uses to call a Brick application, inferred from its contract.

llms.txt

@brickkit/core/client exports 29 names: 5 functions, 3 classes, 9 interfaces, 9 types, 3 variables.

import { ATTR, BrickServerError, BrickSocketError } from "@brickkit/core/client";

Functions

FunctionDescription
createBrickClientCreate an Eden/tRPC-style typed client for the application contract TApp.
isBrickServerErrorWhether an error from the typed client is a BrickServerError: the server answered with a 5xx that is not one of the contract's declared errors, and the error…
isBrickTransportErrorWhether an error from the typed client is a BrickTransportError: the request got no meaningful answer from the server (a network failure, a body that was not…
isControlEventEvent names starting with $ are reserved for control messages.
kebabCaseThe URL segment for a declared name: orderBilling becomes order-billing, importCSV becomes import-csv.

createBrickClient

function createBrickClient<TApp extends Record<string, any>>(
  options: BrickClientOptions & { contract: TApp },
): BrickClient<TApp>

function createBrickClient<TApp extends Record<string, any>>(
  options: BrickClientOptions,
): BrickClient<TApp>

Create an Eden/tRPC-style typed client for the application contract TApp. TApp is a type-only parameter: import type { AppServer } keeps backend implementations out of the frontend bundle.

Parameters

NameTypeDescription
optionsBrickClientOptions & { contract: TApp }

Returns BrickClient<TApp>

Source: packages/core/src/client.ts:623

isBrickServerError

function isBrickServerError(err: unknown): err is BrickServerError

Whether an error from the typed client is a BrickServerError: the server answered with a 5xx that is not one of the contract's declared errors, and the error carries its status, url, and body.

Parameters

NameTypeDescription
errunknownWhat a client call threw.

Returns err is BrickServerError — true for a server error, narrowing its type.

Source: packages/core/src/client.ts:215

isBrickTransportError

function isBrickTransportError(err: unknown): err is BrickTransportError

Whether an error from the typed client is a BrickTransportError: the request got no meaningful answer from the server (a network failure, a body that was not JSON, or a 4xx outside the declared contract).

Parameters

NameTypeDescription
errunknownWhat a client call threw.

Returns err is BrickTransportError — true for a transport error, narrowing its type.

Source: packages/core/src/client.ts:201

isControlEvent

function isControlEvent(event: string): boolean

Event names starting with $ are reserved for control messages.

Parameters

NameTypeDescription
eventstring

Returns boolean

Source: packages/core/src/protocol.ts:73

kebabCase

function kebabCase(name: string): string

The URL segment for a declared name: orderBilling becomes order-billing, importCSV becomes import-csv. Dots and existing dashes stay.

Parameters

NameTypeDescription
namestring

Returns string

Source: packages/core/src/kebab.ts:6

Classes

BrickServerError

class BrickServerError extends Error

The server answered 5xx without a declared code / ValidationError shape, i.e. an unexpected failure rather than a modelled domain error.

Constructor

NameTypeDescription
constructornew (message: string, opts: { status: number; url: string; body: unknown })

Properties

NameTypeDescription
readonly kind"server"
readonly statusnumber
readonly urlstring
readonly bodyunknown

Source: packages/core/src/client.ts:175

BrickSocketError

class BrickSocketError extends Error

Constructor

NameTypeDescription
constructornew ( readonly kind: "connection" | "protocol" | "backpressure" | "refused", message: string, options?: ErrorOptions, )

Source: packages/core/src/socket-types.ts:164

BrickTransportError

class BrickTransportError extends Error

The request never received a meaningful server answer: DNS / connection failure, a malformed body, or a 4xx response outside the declared contract (no code and no ValidationError shape).

Constructor

NameTypeDescription
constructornew ( message: string, opts: { status?: number; url: string; body?: unknown; cause?: unknown }, )

Properties

NameTypeDescription
readonly kind"transport"
readonly status?number
readonly urlstring
readonly body?unknown
readonly cause?unknown

Source: packages/core/src/client.ts:151

Interfaces

BrickCallOptions

interface BrickCallOptions

Properties

NameTypeDescription
headers?Record<string, string>Extra headers merged over the client defaults for this call only.
signal?AbortSignalAbort signal for this call only. Never wrapped: aborts reject with AbortError.
timeoutMs?numberPer-call timeout in milliseconds. Implemented with AbortSignal.timeout.

Source: packages/core/src/client.ts:137

BrickClientAction

interface BrickClientAction<TInput = unknown, TOutput = unknown, TErrors = {}>

Browser-safe action metadata generated by the Brick Vite plugin.

Properties

NameTypeDescription
kind"action"
namestring
serviceNamestring
plugin?stringOwning plugin descriptor name for plugin actions (e.g. @brickkit/crud).
resource?stringOwning resource name for resource actions (e.g. page).
config{ path: string; method: HttpMethod }
readonly __types?{ input: TInput; output: TOutput; errors: TErrors }Type information only; never emitted at runtime.

Source: packages/core/src/client.ts:120

BrickClientOptions

interface BrickClientOptions

Eden/tRPC-style typed HTTP client inferred from Brick-TS application contracts.

The frontend imports only the contract type — never a backend implementation:

import { createBrickClient } from "@brickkit/core/client";
import type { AppServer } from "./server"; // `import type`: erased at build

const api = createBrickClient<AppServer>({ baseUrl: "http://localhost:4000" });
const pages = await api.page.list({ limit: 10, sort: "-createdAt" });
const created = await api.page.create({ title: "Hello", content: "World" });

Build the contract from individually-typed actions (each defineAction call preserves its input/output/error literal types):

// server.ts (backend)
export const listPages = pagesService.action({ name: "page.list", ... });
export const createPage = pagesService.action({ name: "page.create", ... });
export const contract = { page: { list: listPages, create: createPage } };
export type AppServer = typeof contract;

Result / error convention

The client uses a throw-based convention that mirrors local action.execute semantics, because TypeScript promises cannot encode thrown exception types:

  • Success → the promise resolves with the action output.
  • Declared domain failures (your errors: { SLUG_EXISTS: ... }) and framework guards (UNAUTHORIZED/FORBIDDEN/NOT_FOUND/...) → rejected with ActionExecutionError carrying the declared code, status, message, and details. Narrow with instanceof and read err.code.
  • Input/output validation failures → rejected with ValidationError, exactly as local execution throws.
  • Network failures, unparsable bodies, and non-OK responses outside the declared contract → BrickTransportError (4xx) or BrickServerError (5xx).
  • Cancellation via AbortSignal → the native AbortError propagates unwrapped so err.name === "AbortError" keeps working.

URL mapping

A client path is a namespace walk ending in an action call. Generated browser contracts carry each action's HTTP method and path, including plugin routes, resource routes and consumer overrides. Without runtime metadata, callers use POST /api/<service>/<action> with the names in kebab-case (getSummary → get-summary); resource and plugin routes need the contract. GET/DELETE input is serialized as a query string; other methods send JSON.

Properties

NameTypeDescription
baseUrlstringServer origin, e.g. "http://localhost:4000". No trailing slash required.
prefix?stringRoute prefix the server was mounted with (default "/api"). Must match the prefix passed to brick({ prefix }).
headers?| Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>)Default headers sent with every call (e.g. an Authorization bearer). A thunk is re-evaluated per call, so rotated tokens stay fresh.
fetch?(input: RequestInfo | URL, init?: RequestInit) => Promise<Response>fetch implementation. Defaults to globalThis.fetch.
signal?AbortSignalDefault signal applied to every call (per-call signal wins).
timeoutMs?numberDefault per-call timeout in milliseconds (per-call timeoutMs wins).
contract?Record<string, any>Browser-safe route metadata generated by the Brick Vite plugin. Required for custom methods/paths and plugin routes. Without a contract, calls use POST and service/action names inferred from namespace keys.

Source: packages/core/src/client.ts:84

BrickClientSocket

interface BrickClientSocket<Input = unknown, Output = unknown, Errors = {}>

Browser metadata. Schemas retain TypeBox kinds as JSON-safe annotations.

Properties

NameTypeDescription
kind"socket"
namestring
serviceNamestring
config{ path: string }
outputRecord<string, unknown>
readonly __types?{ input: Input; output: Output; errors: Errors }

Source: packages/core/src/socket-types.ts:217

BrickServices

interface BrickServices

The contract of each service by name, which types useService. The generated contract (_brick/contract.ts) fills it in; nothing needs to be written by hand.

Source: packages/core/src/client.ts:117

BrickSocketClient

interface BrickSocketClient<Input, Output, Errors = {}> extends BrickSocketSender<Input>

Properties

NameTypeDescription
readonly readyPromise<void>Resolves on the first open; rejects if the client closes before it.
readonly statusBrickSocketStatus

Methods

NameTypeDescription
onStatusonStatus(listener: (status: BrickSocketStatus) => void): () => void
onReconnectonReconnect(listener: () => void): () => voidAfter each reconnect. Messages sent while offline are not replayed.
onon<K extends keyof Output & string>( event: K, listener: (payload: Output[K]) => void, ): () => void
joinjoin(room: string): Promise<void>Resolves when the server accepts the room; rejects with kind refused otherwise.
leaveleave(room: string): void
onLeaveonLeave(listener: (room: string, reason?: string) => void): () => voidThe server removed this connection from a room.
onGaponGap(listener: (room: string) => void): () => voidReplay could not cover what this client missed in a room while away: refetch it.
onErroronError(listener: (error: BrickSocketError) => void): () => void
onHandlerErroronHandlerError( listener: (error: BrickSocketHandlerError<Input, Errors>) => void, ): () => voidA handler raised a declared error for a message this client sent. The connection stays open.
onCloseonClose(listener: (info: { code: number; reason: string }) => void): () => void

Source: packages/core/src/socket-types.ts:184

BrickSocketConnectOptions

interface BrickSocketConnectOptions

Properties

NameTypeDescription
signal?AbortSignal
timeoutMs?numberLimits each opening attempt.
token?() => string | Promise<string>Called on every connect and reconnect; sent as the brick.token.<token> subprotocol.
reconnect?false | BrickSocketReconnectOptionsOn by default. false ends the client at the first close.

Source: packages/core/src/socket-types.ts:154

BrickSocketReconnectOptions

interface BrickSocketReconnectOptions

Properties

NameTypeDescription
minDelayMs?numberDefault 500.
maxDelayMs?numberDefault 30 000.
jitter?numberRandom spread around each delay, 0 to 1. Default 0.2.

Source: packages/core/src/socket-types.ts:139

BrickSocketSender

interface BrickSocketSender<Events>

Methods

NameTypeDescription
sendsend(...args: SocketEventArgs<Events>): voidLocally accepts a message. Does not acknowledge remote delivery.
closeclose(): void

Source: packages/core/src/socket-types.ts:17

Types

BrickActionCaller

type BrickActionCaller<A> =
  undefined extends InferActionInput<A>
    ? (
        input?: InferActionInput<A>,
        options?: BrickCallOptions,
      ) => Promise<InferActionOutput<A>>
    : (
        input: InferActionInput<A>,
        options?: BrickCallOptions,
      ) => Promise<InferActionOutput<A>>;

Typed caller for one action. Required inputs stay required; actions whose input includes undefined (no schema / all-optional) can be called bare.

Source: packages/core/src/client.ts:279

BrickClient

type BrickClient<T> =
  T extends BrickClientSocket<infer I, infer O, infer E>
    ? {
        connect(
          options?: BrickSocketConnectOptions,
        ): BrickSocketClient<I, O, E>;
      }
    : T extends BrickClientAction<any, any, any> | Action<any, any, any, any>
      ? BrickActionCaller<T>
      : T extends (...args: any[]) => any
        ? BrickActionCaller<T>
        : T extends object
          ? { [K in keyof T]: BrickClient<T[K]> }
          : T;

Recursively maps action leaves and their namespaces to browser callers.

Source: packages/core/src/client.ts:291

BrickSocketHandlerError

type BrickSocketHandlerError<
  Input = unknown,
  Errors = {},
> = ActionExecutionError & {
  event: keyof Input & string;
  code: Extract<keyof Errors, string> | StandardErrorCode;
};

An error a socket handler raised with error.CODE(), for the message event it was handling.

Source: packages/core/src/socket-types.ts:176

BrickSocketStatus

type BrickSocketStatus =
  | "connecting"
  | "open"
  | "reconnecting"
  | "closed";

Source: packages/core/src/socket-types.ts:148

InferActionErrorCodes

type InferActionErrorCodes<A> =
  A extends BrickClientAction<any, any, infer E>
    ? Extract<keyof E, string>
    : A extends {
          config: { errors?: infer E };
        }
      ? E extends Record<string, ActionErrorDefinition>
        ? Extract<keyof E, string>
        : never
      : never;

Union of error codes declared in an action's errors map.

Source: packages/core/src/client.ts:248

InferActionFailure

type InferActionFailure<A> = ActionExecutionError & {
  code: InferActionErrorCodes<A> | StandardErrorCode | "VALIDATION_FAILED";
};

The declared failure shape of an action for catch narrowing:

try { await api.page.create(input); }
catch (err) {
  if (err instanceof ActionExecutionError) {
    const code: InferActionErrorCodes<typeof createPage> | "UNAUTHORIZED" | ... = err.code;
  }
}

(Standard framework codes are always possible alongside declared ones.)

Source: packages/core/src/client.ts:271

InferActionInput

type InferActionInput<A> =
  A extends BrickClientAction<infer I, any, any>
    ? I
    : A extends {
          config: { input?: infer S extends TSchema };
        }
      ? Static<S>
      : A extends {
            execute(params: { input?: infer I; ctx?: any }): any;
          }
        ? I
        : A extends { run(input?: infer I, ctx?: any): any }
          ? I
          : A extends (params: { input?: infer I; ctx?: any }) => any
            ? I
            : unknown;

Static input type from the schema, with a fallback for structural actions.

Source: packages/core/src/client.ts:220

InferActionOutput

type InferActionOutput<A> =
  A extends BrickClientAction<any, infer O, any>
    ? O
    : A extends (...args: any[]) => Promise<infer O>
      ? O
      : A extends { execute(params: any): Promise<infer O> }
        ? O
        : unknown;

Resolved output type of an action.

Source: packages/core/src/client.ts:238

SocketEventArgs

type SocketEventArgs<Events> = {
  [K in keyof Events & string]: [event: K, payload: Events[K]];
}[keyof Events & string];

Source: packages/core/src/socket-types.ts:13

Variables

ATTR

const ATTR: { readonly httpMethod: "http.request.method"; readonly httpRoute: "http.route"; readonly httpStatus: "http.response.status_code"; readonly urlPath: "url.path"; readonly service: "brick.service"; readonly action: "brick.…

Source: packages/core/src/telemetry-names.ts:14

SPAN

const SPAN: { readonly actionPrefix: "action "; readonly rpcPrefix: "rpc "; readonly exceptionEvent: "exception"; readonly logEvent: "log"; }

Source: packages/core/src/telemetry-names.ts:4

STANDARD_ERRORS

const STANDARD_ERRORS: { readonly BAD_REQUEST: { readonly status: 400; readonly message: "Bad Request"; }; readonly UNAUTHORIZED: { readonly status: 401; readonly message: "Unauthorized"; }; readonly FORBIDDEN: { readonly status: 403; readonl…

The codes every action, listener and socket handler can raise, with their status and default message.

Source: packages/core/src/protocol.ts:10