effect-rpc
    Preparing search index...

    Type Alias TaggedRPCGroup<K, V>

    Type representing a tagged handler with methods to access requests and perform RPC calls.

    0.8.0

    type TaggedRPCGroup<K extends RegistryKey, V extends RpcGroup.RpcGroup<any>> = {
        createServerHandler: <R>(
            requestImplementations: RequestImplementations<V, InferClient<V>, R>,
            config: RPCHandlerConfig<R>,
        ) => (
            request: globalThis.Request,
            context?: Context<never>,
        ) => Promise<Response>;
        getRequest: <N extends keyof InferClient<V>>(
            name: N,
            payload: Parameters<InferClient<V>[N]>[0],
        ) => ReturnType<InferClient<V>[N]>;
        getRequests: () => (
            V extends { requests: ReadonlyMap<string, infer R> } ? R : never
        )[];
        groups: V;
        tag: K;
    }

    Type Parameters

    • K extends RegistryKey
    • V extends RpcGroup.RpcGroup<any>
    Index

    Properties

    createServerHandler: <R>(
        requestImplementations: RequestImplementations<V, InferClient<V>, R>,
        config: RPCHandlerConfig<R>,
    ) => (
        request: globalThis.Request,
        context?: Context<never>,
    ) => Promise<Response>

    Creates a server handler for this RPC group.

    This method generates a handler function suitable for use in server environments (such as HTTP endpoints or serverless functions) that can process incoming requests and route them to the appropriate implementation based on the registered RPC group.

    Type declaration

      • <R>(
            requestImplementations: RequestImplementations<V, InferClient<V>, R>,
            config: RPCHandlerConfig<R>,
        ): (
            request: globalThis.Request,
            context?: Context<never>,
        ) => Promise<Response>
      • Type Parameters

        • R

          The environment type required by the request implementations.

        Parameters

        • requestImplementations: RequestImplementations<V, InferClient<V>, R>

          An object mapping request names to their implementation functions. Each implementation should match the signature expected by the corresponding request.

        • config: RPCHandlerConfig<R>

          Configuration options for the RPC handler, such as environment injection, error handling, or custom response formatting.

        Returns (request: globalThis.Request, context?: Context<never>) => Promise<Response>

        A function that takes a globalThis.Request and an optional Context, and returns a Promise<Response>.

    import { helloRequests } from './requests';
    import { HelloService } from './service';
    import { RpcSerialization } from '@effect/rpc';

    const handler = helloRequests.createServerHandler(
    {
    SayByeReq: ({ name }) => HelloService.sayBye(name),
    SayHelloReq: ({ name }) => HelloService.sayHello(name),
    },
    {
    serviceLayers: HelloService.Default,
    serialization: RpcSerialization.layerNdjson,
    },
    );

    // Use in a server route
    export const POST = handler;

    0.8.0

    getRequest: <N extends keyof InferClient<V>>(
        name: N,
        payload: Parameters<InferClient<V>[N]>[0],
    ) => ReturnType<InferClient<V>[N]>

    Gets a specific request by name.

    It can be used to send it to the custom runtime to execute the request.

    Type declaration

      • <N extends keyof InferClient<V>>(
            name: N,
            payload: Parameters<InferClient<V>[N]>[0],
        ): ReturnType<InferClient<V>[N]>
      • Type Parameters

        Parameters

        • name: N

          The name of the request to retrieve.

        • payload: Parameters<InferClient<V>[N]>[0]

          The payload to send with the request.

        Returns ReturnType<InferClient<V>[N]>

        The response from the request.

    const request = handler.getRequest('SayHelloReq', { name: 'Alice' });
    // request is an Effect that, when run, will perform the RPC call and return the response.
    const result = await AppRuntime.runPromise(request); // result is the response from the RPC call

    0.8.0

    getRequests: () => (
        V extends { requests: ReadonlyMap<string, infer R> } ? R : never
    )[]

    Returns all request names available in this handler. This is useful for introspection or dynamic request handling.

    Type declaration

      • (): (V extends { requests: ReadonlyMap<string, infer R> } ? R : never)[]
      • Returns (V extends { requests: ReadonlyMap<string, infer R> } ? R : never)[]

        Array of requests available in this handler

    const requests = handler.getRequests();
    // requests is an array of all request objects available in this handler

    0.8.0

    This will be removed in future versions. Use getRequest to access specific requests directly. There's no need to get all request names at once.

    groups: V
    tag: K