Specta LogoSpecta
RspcTypescript apiTs rspc client

@rspc/client

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

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

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

Classes

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 ↗

RSPCError

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

View source ↗

Interfaces

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 ↗

SubscriptionOptions

interface SubscriptionOptions<TOutput> {
    onData: (data: TOutput) => void;
    onError?: (err: RSPCError | 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 ↗

AbortFn

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

TODO @internal

View source ↗

FakeObservable

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

TODO @internal

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 ↗

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

TODO @internal

View source ↗

LinkResult

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

TODO @internal

View source ↗

Operation

/**
 * TODO
 *
 * @internal
 */
type Operation = {
    method: "query" | "mutation" | "subscription";
    path: string;
    input: any | null;
    context: OperationContext;
};

TODO @internal

View source ↗

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 ↗

OperationType

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

View source ↗

ProcedureDef

/**
 * Represents a Typescript procedure file which is generated by the Rust code.
 * This is codegenerated Typescript file is how we can validate the types on the frontend match Rust.
 *
 * @internal
 */
type ProcedureDef = {
    key: string;
    input: any;
    result: any;
};

Represents a Typescript procedure file which is generated by the Rust code. This is codegenerated Typescript file is how we can validate the types on the frontend match Rust. @internal

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 ↗

PromiseAndCancel

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

TODO @internal

View source ↗

Request

/**
 * The type of a request to rspc.
 *
 * @internal
 */
type Request = {
    method: "query";
    id: number;
    path: string;
    input: any | null;
} | {
    method: "mutation";
    id: number;
    path: string;
    input: any | null;
} | {
    method: "subscription";
    id: number;
    path: string;
    input: any | null;
} | {
    method: "subscriptionStop";
    id: number;
};

The type of a request to rspc. @internal

View source ↗

Response

/**
 * The type of a response from rspc.
 *
 * @internal
 */
type Response = ({
    type: "value";
    value: any;
} | {
    type: "error";
    value: ResponseError;
} | {
    type: "complete";
}) & {
    id: number;
};

The type of a response from rspc. @internal

View source ↗

ResponseError

/**
 * An error that can be returned by rspc.
 *
 * @internal
 */
type ResponseError = {
    code: number;
    message: string;
    data: any | null;
};

An error that can be returned by rspc. @internal

View source ↗

ResponseInner

/**
 * A value that can be a successful result or an error.
 *
 * @internal
 */
type ResponseInner = {
    type: "value";
    value: any;
} | {
    type: "error";
    value: ResponseError;
} | {
    type: "complete";
};

A value that can be a successful result or an error. @internal

View source ↗

Functions

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

// Referenced local context (not public exports):
type HttpLinkOpts = {
    url: string;
    /**
     * Add ponyfill for fetch
     */
    fetch?: typeof fetch;
    /**
     * Add ponyfill for AbortController
     */
    AbortController?: typeof AbortController | null;
} & ({
    /**
     * Headers to be set on outgoing requests or a callback that of said headers
     */
    headers?: HTTPHeaders | ((opts: {
        op: Operation;
    }) => HTTPHeaders);
} | {
    /**
     * Headers to be set on outgoing requests or a callback that of said headers
     */
    headers?: HTTPHeaders | ((opts: {
        ops: Operation[];
    }) => HTTPHeaders);
    /**
     * Batch multiple rspc queries into a single HTTP request. Disabled by default.
     *
     * NOTE: This is great for performance but may be problematic if your using HTTP caching.
     */
    batch: true | {
        maxBatchSize?: number;
// … truncated, see the source link

HTTP Fetch link for rspc

View source ↗

initRspc

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

// Referenced local context (not public exports):
interface ClientArgs {
    links: Link[];
    onError?: (err: RSPCError) => 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 ↗

/**
 * Websocket link for rspc
 *
 * Note: This link applies request batching by default.
 */
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

Note: This link applies request batching by default.

View source ↗

wsLinkInternal

/**
 * @internal
 */
declare function wsLinkInternal([activeMap, send]: ReturnType<typeof newWsManager>): Link;

// Referenced local context (not public exports):
declare function newWsManager(opts: WsLinkOpts): readonly [
    Map<number, {
        resolve: (result: any) => void;
        reject: (error: Error | RSPCError) => void;
    }>,
    (data: Request | Request[]) => Promise<void>
];

type WsLinkOpts = {
    url: string;
    /**
     * Add ponyfill for WebSocket
     */
    WebSocket?: typeof WebSocket;
};

@internal

View source ↗

On this page