Specta LogoSpecta
RspcTypescript apiTs rspc client

@rspc/client

TypeScript API reference for @rspc/client, version 0.0.7.

Public exports of @rspc/client at version 0.0.7.

This page documents 0.0.7. The documented version is 0.3.1.

Classes

Client

declare class Client<T extends Procedures> {
    private transport;
    private subscriptionMap;
    private onError?;
    constructor(args: ClientArgs);
    query<K extends T["queries"]["key"]>(key: K): Promise<Extract<T["queries"], {
        key: K;
    }>["result"]>;
    mutation<K extends T["mutations"]["key"][0]>(key: [
        K,
        Extract<T["mutations"], {
            key: [
                K,
                any
            ];
        }>["key"][1]
    ]): Promise<Extract<T["mutations"], {
        key: K;
    }>["result"]>;
    addSubscription<K extends T["subscriptions"]["key"][0]>(key: Extract<T["subscriptions"]["key"], {
        key: [
            K,
            any
        ];
    }>[1] extends undefined ? [
        K
    ] : [
        K,
        Extract<T["subscriptions"]["key"], {
            key: [
                K,
                any
            ];
        }>[1]
    ], options: {
        onNext(msg: Extract<T["subscriptions"], {
            key: [
// … truncated, see the source link

View source ↗

FetchTransport

declare class FetchTransport implements Transport {
    private url;
    transformer?: ClientTransformer;
    clientSubscriptionCallback?: (id: string, key: string, value: any) => void;
    constructor(url: string);
    doRequest(operation: OperationType, key: ProcedureKey): Promise<any>;
}

View source ↗

RSPCError

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

View source ↗

WebsocketTransport

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

View source ↗

Interfaces

ClientArgs

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

View source ↗

ClientTransformer

interface ClientTransformer {
    serialize(type: OperationType, key: ProcedureKey): ProcedureKey;
    deserialize(type: OperationType, key: ProcedureKey, data: any): any;
}

View source ↗

Transport

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

View source ↗

Type aliases

inferBindingsType

/**
 * 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>
 */
declare type inferBindingsType<TBindings extends Procedures | ProcedureLike> = TBindings extends ProcedureLike ? TBindings["_rspc_def"] : TBindings;

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 ↗

inferMutationInput

declare type inferMutationInput<TBindings extends Procedures | ProcedureLike, T extends inferBindingsType<TBindings>["mutations"]["key"][0]> = inferProcedureInput<inferBindingsType<TBindings>, "mutations", T>;

View source ↗

inferMutationResult

declare type inferMutationResult<TBindings extends Procedures | ProcedureLike, T extends inferBindingsType<TBindings>["mutations"]["key"][0]> = inferProcedureResult<inferBindingsType<TBindings>, "mutations", T>;

View source ↗

inferProcedureInput

declare type inferProcedureInput<TBindings extends Procedures | ProcedureLike, TOperation extends keyof Procedures, T extends inferBindingsType<TBindings>[TOperation]["key"][0]> = inferProcedureKey<TBindings, TOperation, T>[1];

View source ↗

inferProcedureKey

declare type inferProcedureKey<TBindings extends Procedures | ProcedureLike, TOperation extends keyof Procedures, T extends inferBindingsType<TBindings>[TOperation]["key"][0]> = Extract<inferBindingsType<TBindings>[TOperation], {
    key: procedureKey<T>;
}>["key"];

View source ↗

inferProcedureResult

declare type inferProcedureResult<TBindings extends Procedures | ProcedureLike, TOperation extends keyof Procedures, T extends inferBindingsType<TBindings>[TOperation]["key"][0]> = Extract<inferBindingsType<TBindings>[TOperation], {
    key: procedureKey<T>;
}>["result"];

View source ↗

inferQueryInput

declare type inferQueryInput<TBindings extends Procedures | ProcedureLike, T extends inferBindingsType<TBindings>["queries"]["key"][0]> = inferProcedureInput<inferBindingsType<TBindings>, "queries", T>;

View source ↗

inferQueryResult

declare type inferQueryResult<TBindings extends Procedures | ProcedureLike, T extends inferBindingsType<TBindings>["queries"]["key"][0]> = inferProcedureResult<inferBindingsType<TBindings>, "queries", T>;

View source ↗

inferSubscriptionInput

declare type inferSubscriptionInput<TBindings extends Procedures | ProcedureLike, T extends inferBindingsType<TBindings>["subscriptions"]["key"][0]> = inferProcedureInput<inferBindingsType<TBindings>, "subscriptions", T>;

View source ↗

inferSubscriptionResult

declare type inferSubscriptionResult<TBindings extends Procedures | ProcedureLike, T extends inferBindingsType<TBindings>["subscriptions"]["key"][0]> = inferProcedureResult<inferBindingsType<TBindings>, "subscriptions", T>;

View source ↗

OperationType

declare type OperationType = "query" | "mutation" | "subscriptionAdd" | "subscriptionRemove";

View source ↗

procedureKey

declare type procedureKey<T extends string> = [
    T
] | [
    T, /* input */
    any
];

View source ↗

ProcedureKey

/**
 * The key to an operation
 */
declare type ProcedureKey = [
    string
] | [
    string, /* input */
    any
];

The key to an operation

View source ↗

ProcedureLike

/**
 * A type which allows inferring the type of the bindings
 */
declare type ProcedureLike = {
    _rspc_def: Procedures;
};

A type which allows inferring the type of the bindings

View source ↗

Procedures

/**
 * This type represents the Typescript bindings which are generated from the router by Rust.
 */
declare type Procedures = {
    queries: {
        key: ProcedureKey;
        result: any;
    };
    mutations: {
        key: ProcedureKey;
        result: any;
    };
    subscriptions: {
        key: ProcedureKey;
        result: any;
    };
};

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

View source ↗

Functions

createClient

declare function createClient<T extends Procedures>(args: ClientArgs): Client<T>;

View source ↗

Variables

randomId

randomId: () => string

View source ↗

On this page