@brickkit/core/client
The typed HTTP client a frontend uses to call a Brick application, inferred from its contract.
@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
| Function | Description |
|---|---|
createBrickClient | Create an Eden/tRPC-style typed client for the application contract TApp. |
isBrickServerError | 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… |
isBrickTransportError | 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… |
isControlEvent | Event names starting with $ are reserved for control messages. |
kebabCase | The 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
| Name | Type | Description |
|---|---|---|
options | BrickClientOptions & { contract: TApp } |
Returns BrickClient<TApp>
Source: packages/core/src/client.ts:623
isBrickServerError
function isBrickServerError(err: unknown): err is BrickServerErrorWhether 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
| Name | Type | Description |
|---|---|---|
err | unknown | What 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 BrickTransportErrorWhether 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
| Name | Type | Description |
|---|---|---|
err | unknown | What 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): booleanEvent names starting with $ are reserved for control messages.
Parameters
| Name | Type | Description |
|---|---|---|
event | string |
Returns boolean
Source: packages/core/src/protocol.ts:73
kebabCase
function kebabCase(name: string): stringThe URL segment for a declared name: orderBilling becomes order-billing,
importCSV becomes import-csv. Dots and existing dashes stay.
Parameters
| Name | Type | Description |
|---|---|---|
name | string |
Returns string
Source: packages/core/src/kebab.ts:6
Classes
BrickServerError
class BrickServerError extends ErrorThe server answered 5xx without a declared code / ValidationError shape,
i.e. an unexpected failure rather than a modelled domain error.
Constructor
| Name | Type | Description |
|---|---|---|
constructor | new (message: string, opts: { status: number; url: string; body: unknown }) |
Properties
| Name | Type | Description |
|---|---|---|
readonly kind | "server" | |
readonly status | number | |
readonly url | string | |
readonly body | unknown |
Source: packages/core/src/client.ts:175
BrickSocketError
class BrickSocketError extends ErrorConstructor
| Name | Type | Description |
|---|---|---|
constructor | new ( readonly kind: "connection" | "protocol" | "backpressure" | "refused", message: string, options?: ErrorOptions, ) |
Source: packages/core/src/socket-types.ts:164
BrickTransportError
class BrickTransportError extends ErrorThe 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
| Name | Type | Description |
|---|---|---|
constructor | new ( message: string, opts: { status?: number; url: string; body?: unknown; cause?: unknown }, ) |
Properties
| Name | Type | Description |
|---|---|---|
readonly kind | "transport" | |
readonly status? | number | |
readonly url | string | |
readonly body? | unknown | |
readonly cause? | unknown |
Source: packages/core/src/client.ts:151
Interfaces
BrickCallOptions
interface BrickCallOptionsProperties
| Name | Type | Description |
|---|---|---|
headers? | Record<string, string> | Extra headers merged over the client defaults for this call only. |
signal? | AbortSignal | Abort signal for this call only. Never wrapped: aborts reject with AbortError. |
timeoutMs? | number | Per-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
| Name | Type | Description |
|---|---|---|
kind | "action" | |
name | string | |
serviceName | string | |
plugin? | string | Owning plugin descriptor name for plugin actions (e.g. @brickkit/crud). |
resource? | string | Owning 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 BrickClientOptionsEden/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 withActionExecutionErrorcarrying the declaredcode,status,message, anddetails. Narrow withinstanceofand readerr.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) orBrickServerError(5xx). - Cancellation via
AbortSignal→ the nativeAbortErrorpropagates unwrapped soerr.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
| Name | Type | Description |
|---|---|---|
baseUrl | string | Server origin, e.g. "http://localhost:4000". No trailing slash required. |
prefix? | string | Route 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? | AbortSignal | Default signal applied to every call (per-call signal wins). |
timeoutMs? | number | Default 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
| Name | Type | Description |
|---|---|---|
kind | "socket" | |
name | string | |
serviceName | string | |
config | { path: string } | |
output | Record<string, unknown> | |
readonly __types? | { input: Input; output: Output; errors: Errors } |
Source: packages/core/src/socket-types.ts:217
BrickServices
interface BrickServicesThe 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
| Name | Type | Description |
|---|---|---|
readonly ready | Promise<void> | Resolves on the first open; rejects if the client closes before it. |
readonly status | BrickSocketStatus |
Methods
| Name | Type | Description |
|---|---|---|
onStatus | onStatus(listener: (status: BrickSocketStatus) => void): () => void | |
onReconnect | onReconnect(listener: () => void): () => void | After each reconnect. Messages sent while offline are not replayed. |
on | on<K extends keyof Output & string>( event: K, listener: (payload: Output[K]) => void, ): () => void | |
join | join(room: string): Promise<void> | Resolves when the server accepts the room; rejects with kind refused otherwise. |
leave | leave(room: string): void | |
onLeave | onLeave(listener: (room: string, reason?: string) => void): () => void | The server removed this connection from a room. |
onGap | onGap(listener: (room: string) => void): () => void | Replay could not cover what this client missed in a room while away: refetch it. |
onError | onError(listener: (error: BrickSocketError) => void): () => void | |
onHandlerError | onHandlerError( listener: (error: BrickSocketHandlerError<Input, Errors>) => void, ): () => void | A handler raised a declared error for a message this client sent. The connection stays open. |
onClose | onClose(listener: (info: { code: number; reason: string }) => void): () => void |
Source: packages/core/src/socket-types.ts:184
BrickSocketConnectOptions
interface BrickSocketConnectOptionsProperties
| Name | Type | Description |
|---|---|---|
signal? | AbortSignal | |
timeoutMs? | number | Limits each opening attempt. |
token? | () => string | Promise<string> | Called on every connect and reconnect; sent as the brick.token.<token> subprotocol. |
reconnect? | false | BrickSocketReconnectOptions | On by default. false ends the client at the first close. |
Source: packages/core/src/socket-types.ts:154
BrickSocketReconnectOptions
interface BrickSocketReconnectOptionsProperties
| Name | Type | Description |
|---|---|---|
minDelayMs? | number | Default 500. |
maxDelayMs? | number | Default 30 000. |
jitter? | number | Random spread around each delay, 0 to 1. Default 0.2. |
Source: packages/core/src/socket-types.ts:139
BrickSocketSender
interface BrickSocketSender<Events>Methods
| Name | Type | Description |
|---|---|---|
send | send(...args: SocketEventArgs<Events>): void | Locally accepts a message. Does not acknowledge remote delivery. |
close | close(): 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