Specta LogoSpecta
RspcTypescript apiTs rspc client

@rspc/client

TypeScript API reference for @rspc/client, version 0.0.0-main-799eec5d.

Public exports of @rspc/client at version 0.0.0-main-799eec5d.

This page documents 0.0.0-main-799eec5d. The documented version is 0.3.1.

Classes

Client

declare class Client<TProcedures extends ProceduresDef> {
    _rspc_def: ProceduresDef;
    private transport;
    private subscriptionMap;
    private onError?;
    constructor(args: ClientArgs);
    query<K extends TProcedures["queries"]["key"] & string>(keyAndInput: [
        key: K,
        ...input: _inferProcedureHandlerInput<TProcedures, "queries", K>
    ]): Promise<inferQueryResult<TProcedures, K>>;
    mutation<K extends TProcedures["mutations"]["key"] & string>(keyAndInput: [
        key: K,
        ...input: _inferProcedureHandlerInput<TProcedures, "mutations", K>
    ]): Promise<inferMutationResult<TProcedures, K>>;
    addSubscription<K extends TProcedures["subscriptions"]["key"] & string, TData = inferSubscriptionResult<TProcedures, K>>(keyAndInput: [
        K,
        ..._inferProcedureHandlerInput<TProcedures, "subscriptions", K>
    ], opts: SubscriptionOptions<TData>): () => void;
}

View source ↗

FetchTransport

declare class FetchTransport implements Transport {
    private url;
    clientSubscriptionCallback?: (id: string, key: string, value: any) => void;
    private fetch;
    constructor(url: string, fetch?: typeof globalThis.fetch);
    doRequest(operation: OperationType, key: string, input: any): Promise<any>;
}

View source ↗

NoOpTransport

declare class NoOpTransport implements Transport {
    constructor();
    doRequest(operation: OperationType, key: string, input: string): Promise<any>;
}

View source ↗

RSPCError

declare class RSPCError {
    code: number;
    message: string;
    constructor(code: number, message: string);
}

View source ↗

v2:AlphaClient

declare class AlphaClient<P extends ProceduresDef> {
    _rspc_def: ProceduresDef;
    private links;
    private onError?;
    private mapQueryKey?;
    constructor(args: ClientArgs);
    query<K extends P["queries"]["key"] & string>(keyAndInput: [
        key: K,
        ...input: _inferProcedureHandlerInput<P, "queries", K>
    ], opts?: OperationOpts): Promise<inferQueryResult<P, K>>;
    mutation<K extends P["mutations"]["key"] & string>(keyAndInput: [
        key: K,
        ...input: _inferProcedureHandlerInput<P, "mutations", K>
    ], opts?: OperationOpts): Promise<inferMutationResult<P, K>>;
    addSubscription<K extends P["subscriptions"]["key"] & string, TData = inferProcedureResult<P, "subscriptions", K>>(keyAndInput: [
        K,
        ..._inferProcedureHandlerInput<P, "subscriptions", K>
    ], opts: SubscriptionOptions<TData> & {
        context?: OperationContext;
    }): () => void;
    dangerouslyHookIntoInternals<P2 extends ProceduresDef = P>(opts?: {
// … truncated, see the source link

View source ↗

v2:AlphaRSPCError

declare class AlphaRSPCError extends Error {
    code: number;
    message: string;
    constructor(code: number, message: string);
}

View source ↗

WebsocketTransport

declare class WebsocketTransport implements Transport {
    private url;
    private ws;
    private requestMap;
    clientSubscriptionCallback?: (id: string, value: any) => void;
    constructor(url: string);
    attachEventListeners(): void;
    reconnect(timeoutIndex?: number): Promise<void>;
    doRequest(operation: OperationType, key: string, input: any, opts?: {
        id?: string;
    }): Promise<any>;
}

View source ↗

Interfaces

ClientArgs

interface ClientArgs {
    transport: Transport;
    onError?: (err: RSPCError) => void | Promise<void>;
}

View source ↗

SubscriptionOptions

interface SubscriptionOptions<TOutput> {
    onStarted?: () => void;
    onData: (data: TOutput) => void;
    onError?: (err: RSPCError) => void;
}

View source ↗

Transport

interface Transport {
    clientSubscriptionCallback?: (id: string, key: string, value: any) => void;
    doRequest(operation: OperationType, key: string, input: any): Promise<any>;
}

View source ↗

v2:LinkOperation

/**
 * The argument to a link. Contains information about the current operation and a function to call the next link.
 *
 * @internal
 */
interface LinkOperation {
    op: Operation;
    next(op: {
        op: Operation;
    }): LinkResult;
}

The argument to a link. Contains information about the current operation and a function to call the next link. @internal

View source ↗

v2:Operation

/**
 * TODO
 *
 * @internal
 */
interface Operation {
    id: string;
    type: "query" | "mutation" | "subscription" | "subscriptionStop";
    input: unknown;
    path: string;
    context: OperationContext;
}

TODO @internal

View source ↗

v2:SubscriptionOptions

interface SubscriptionOptions<TOutput> {
    onData: (data: TOutput) => void;
    onError?: (err: AlphaRSPCError | Error) => void;
}

View source ↗

Type aliases

_inferInfiniteQueryProcedureHandlerInput

type _inferInfiniteQueryProcedureHandlerInput<TProcedures extends ProceduresLike, K extends inferInfiniteQueries<TProcedures>["key"]> = inferInfiniteQueryInput<TProcedures, K> extends never ? [
] : [
    inferInfiniteQueryInput<TProcedures, K>
];

View source ↗

_inferProcedureHandlerInput

type _inferProcedureHandlerInput<TProcedures extends ProceduresLike, TOperation extends keyof ProceduresDef, K extends inferProcedures<TProcedures>[TOperation]["key"]> = inferProcedure<TProcedures, TOperation, K>["input"] extends never ? [
] : [
    inferProcedure<TProcedures, TOperation, K>["input"]
];

View source ↗

inferInfiniteQueries

type inferInfiniteQueries<TProcedures extends ProceduresLike> = Exclude<Extract<inferProcedures<TProcedures>["queries"], {
    input: {
        cursor: any;
    };
}>, {
    input: never;
}>;

View source ↗

inferInfiniteQuery

type inferInfiniteQuery<TProcedures extends ProceduresLike, K extends inferInfiniteQueries<TProcedures>["key"]> = Extract<inferInfiniteQueries<TProcedures>, {
    key: K;
}>;

View source ↗

inferInfiniteQueryInput

type inferInfiniteQueryInput<TProcedures extends ProceduresLike, K extends inferInfiniteQueries<TProcedures>["key"]> = EmptyObjToNever<Omit<inferInfiniteQuery<TProcedures, K>["input"], "cursor">>;

// Referenced local context (not public exports):
type EmptyObjToNever<T> = keyof T extends never ? never : T;

View source ↗

inferInfiniteQueryResult

type inferInfiniteQueryResult<TProcedures extends ProceduresLike, K extends inferInfiniteQueries<TProcedures>["key"]> = inferInfiniteQuery<TProcedures, K>["result"];

View source ↗

inferMutationInput

type inferMutationInput<TProcedures extends ProceduresLike, T extends inferProcedures<TProcedures>["mutations"]["key"][0]> = inferProcedureInput<inferProcedures<TProcedures>, "mutations", T>;

View source ↗

inferMutationResult

type inferMutationResult<TProcedures extends ProceduresLike, T extends inferProcedures<TProcedures>["mutations"]["key"][0]> = inferProcedureResult<inferProcedures<TProcedures>, "mutations", T>;

View source ↗

inferProcedure

type inferProcedure<TProcedures extends ProceduresLike, TOperation extends keyof ProceduresDef, K extends inferProcedureKey<TProcedures, TOperation>> = Extract<inferProcedures<TProcedures>[TOperation], {
    key: K;
}>;

View source ↗

inferProcedureInput

type inferProcedureInput<TProcedures extends ProceduresLike, TOperation extends keyof ProceduresDef, K extends inferProcedureKey<TProcedures, TOperation>> = inferProcedure<TProcedures, TOperation, K>["input"];

View source ↗

inferProcedureKey

type inferProcedureKey<TProcedures extends ProceduresLike, TOperation extends keyof ProceduresDef> = inferProcedures<TProcedures>[TOperation]["key"];

View source ↗

inferProcedureResult

type inferProcedureResult<TProcedures extends ProceduresLike, TOperation extends keyof ProceduresDef, K extends inferProcedureKey<TProcedures, TOperation>> = inferProcedure<TProcedures, TOperation, K>["result"];

View source ↗

inferProcedures

/**
 * This is a helper method to infer the type of bindings for this router off other rspc types.
 * The inferred type is what is generated by Rust.
 * @example
 * const rspc = createReactQueryHooks<Operations>();
 * type Bindings = inferBindingsType<typeof rspc>
 */
type inferProcedures<TProcedures extends ProceduresLike> = TProcedures extends {
    _rspc_def: ProceduresDef;
} ? TProcedures["_rspc_def"] : TProcedures;

This is a helper method to infer the type of bindings for this router off other rspc types. The inferred type is what is generated by Rust. @example const rspc = createReactQueryHooks<Operations>(); type Bindings = inferBindingsType<typeof rspc>

View source ↗

inferQueryInput

type inferQueryInput<TProcedures extends ProceduresLike, T extends inferProcedures<TProcedures>["queries"]["key"][0]> = inferProcedureInput<inferProcedures<TProcedures>, "queries", T>;

View source ↗

inferQueryResult

type inferQueryResult<TProcedures extends ProceduresLike, T extends inferProcedures<TProcedures>["queries"]["key"][0]> = inferProcedureResult<inferProcedures<TProcedures>, "queries", T>;

View source ↗

inferSubscriptionInput

type inferSubscriptionInput<TProcedures extends ProceduresLike, T extends inferProcedures<TProcedures>["subscriptions"]["key"][0]> = inferProcedureInput<inferProcedures<TProcedures>, "subscriptions", T>;

View source ↗

inferSubscriptionResult

type inferSubscriptionResult<TProcedures extends ProceduresLike, T extends inferProcedures<TProcedures>["subscriptions"]["key"][0]> = inferProcedureResult<inferProcedures<TProcedures>, "subscriptions", T>;

View source ↗

OperationType

type OperationType = "query" | "mutation" | "subscription" | "subscriptionStop";

View source ↗

ProcedureDef

type ProcedureDef = {
    key: string;
    input: any;
    result: any;
};

View source ↗

ProceduresDef

/**
 * This type represents the Typescript bindings which are generated from the router by Rust.
 */
type ProceduresDef = {
    queries: ProcedureDef;
    mutations: ProcedureDef;
    subscriptions: ProcedureDef;
};

This type represents the Typescript bindings which are generated from the router by Rust.

View source ↗

ProceduresLike

/**
 * A type which allows inferring the type of the bindings
 */
type ProceduresLike = {
    _rspc_def: ProceduresDef;
} | ProceduresDef;

A type which allows inferring the type of the bindings

View source ↗

v2:AbortFn

/**
 * TODO
 *
 * @internal
 */
type AbortFn = () => void;

TODO @internal

View source ↗

v2:FakeObservable

/**
 * TODO
 *
 * @internal
 */
type FakeObservable = {
    exec: () => PromiseAndCancel<any>;
};

TODO @internal

View source ↗

/**
 * TODO
 *
 * @internal
 */
type Link = (p: LinkOperation) => LinkResult;

TODO @internal

View source ↗

v2:LinkResult

/**
 * TODO
 *
 * @internal
 */
type LinkResult = {
    exec: (resolve: (result: any) => void, reject: (error: Error | AlphaRSPCError) => void) => void;
    abort: () => void;
};

TODO @internal

View source ↗

v2:OperationContext

/**
 * A map of data that can be used by links to store metadata about the current operation.
 * This allows links to communicate with each other.
 *
 * @internal
 */
type OperationContext = Record<string, unknown>;

A map of data that can be used by links to store metadata about the current operation. This allows links to communicate with each other. @internal

View source ↗

v2:PromiseAndCancel

/**
 * TODO
 *
 * @internal
 */
type PromiseAndCancel<TValue> = {
    promise: Promise<TValue>;
    abort: AbortFn;
};

TODO @internal

View source ↗

v2:RspcRequest

/**
 * TODO
 *
 * @internal
 */
type Request = ({
    method: "query";
    params: {
        path: string;
        input: any | null;
    };
} | {
    method: "mutation";
    params: {
        path: string;
        input: any | null;
    };
} | {
    method: "subscription";
    params: {
        path: string;
        input: NewOrOldInput;
    };
} | {
    method: "subscriptionStop";
    params: SubscriptionStop | null;
}) & {
    jsonrpc: string | null;
    id: RequestId;
};

// Referenced local context (not public exports):
type NewOrOldInput = any | null | [
    RequestId,
    any | null
];

type SubscriptionStop = {
    input: RequestId;
};

/**
 * TODO
 *
 * @internal
 */
type RequestId = null | number | string;

TODO @internal

View source ↗

Functions

createClient

declare function createClient<TProcedures extends ProceduresLike>(args: ClientArgs): Client<inferProcedures<TProcedures>>;

View source ↗

/**
 * Wrapper around httpLink that applies request batching. This is great for performance but may be problematic if your using HTTP caching.
 */
declare function httpBatchLink(opts: HttpBatchLinkOpts): Link;

// Referenced local context (not public exports):
type HttpBatchLinkOpts = BaseHttpLinkOpts & {
    /**
     * Headers to be set on outgoing requests or a callback that of said headers
     */
    headers?: HTTPHeaders | ((opts: {
        ops: Operation[];
    }) => HTTPHeaders | Promise<HTTPHeaders>);
};

type BaseHttpLinkOpts = {
    url: string;
    /**
     * Add ponyfill for fetch
     */
    fetch?: typeof fetch;
    /**
     * Add ponyfill for AbortController
     */
    AbortController?: typeof AbortController | null;
};

type HTTPHeaders = Record<string, string | string[] | undefined>;

Wrapper around httpLink that applies request batching. This is great for performance but may be problematic if your using HTTP caching.

View source ↗

/**
 * HTTP Fetch link for rspc
 */
declare function httpLink(opts: HttpLinkOpts): Link;

// Referenced local context (not public exports):
type HttpLinkOpts = BaseHttpLinkOpts & {
    /**
     * Headers to be set on outgoing requests or a callback that of said headers
     */
    headers?: HTTPHeaders | ((opts: {
        op: Operation;
    }) => HTTPHeaders | Promise<HTTPHeaders>);
};

type BaseHttpLinkOpts = {
    url: string;
    /**
     * Add ponyfill for fetch
     */
    fetch?: typeof fetch;
    /**
     * Add ponyfill for AbortController
     */
    AbortController?: typeof AbortController | null;
};

type HTTPHeaders = Record<string, string | string[] | undefined>;

HTTP Fetch link for rspc

View source ↗

v2:initRspc

declare function initRspc<P extends ProceduresDef>(args: ClientArgs): AlphaClient<P>;

// Referenced local context (not public exports):
interface ClientArgs {
    links: Link[];
    onError?: (err: AlphaRSPCError) => void | Promise<void>;
}

View source ↗

/**
 * Link for logging operations.
 *
 * This must go before the terminating link for it to work!
 *
 */
declare function loggerLink(): Link;

Link for logging operations.

This must go before the terminating link for it to work!

View source ↗

/**
 * Wrapper around wsLink that applies request batching.
 */
declare function wsBatchLink(opts: WsLinkOpts): Link;

// Referenced local context (not public exports):
type WsLinkOpts = {
    url: string;
    /**
     * Add ponyfill for WebSocket
     */
    WebSocket?: typeof WebSocket;
};

Wrapper around wsLink that applies request batching.

View source ↗

/**
 * Websocket link for rspc
 */
declare function wsLink(opts: WsLinkOpts): Link;

// Referenced local context (not public exports):
type WsLinkOpts = {
    url: string;
    /**
     * Add ponyfill for WebSocket
     */
    WebSocket?: typeof WebSocket;
};

Websocket link for rspc

View source ↗

Variables

randomId

randomId: () => string

View source ↗

On this page