effect-rpc
    Preparing search index...

    Function createRouteHandler

    • Creates a Layer containing all RPC endpoint implementations for a given router.

      This function ensures:

      • All endpoints in the router are implemented (compile-time error if any are missing)
      • Each handler can require dependencies, which are provided via the additionalLayers argument
      • The resulting Layer is ready to be passed to createServerHandler

      Type Parameters

      • T extends RpcGroup<any>
      • V extends {
            readonly [key: string]: <
                const AsMailbox extends boolean = false,
                const Discard = false,
            >(
                input: any,
                options?:
                    | {
                        asMailbox?: AsMailbox;
                        context?: Context<never>;
                        headers?: Input;
                        streamBufferSize?: number;
                    }
                    | { context?: Context<never>; discard?: Discard; headers?: Input },
            ) => Effect<
                Discard extends true ? void : any,
                Discard extends true ? never : any,
                any,
            > & From<any, never, string>;
        }
      • R

      Parameters

      • router: T

        The RPC router group defining all endpoints.

      • reqImplementations: RequestImplementations<T, V, R>

        An object mapping every endpoint name to its implementation. All endpoints are required.

      • additionalLayers: Layer<R>

        A Layer providing all dependencies required by the handlers (e.g., service implementations).

      Returns Layer<ExtractRoutes<T>, never, never>

      A Layer suitable for use with createServerHandler.

      You can also use the createRPCHandler function for a more streamlined approach that combines route handler creation and server setup.

      const handlers = createRouteHandler(router, {
      SayHelloReq: ({ name }) => HelloService.sayHello(name),
      SayByeReq: ({ name }) => HelloService.sayBye(name),
      PingPongReq: (req) => HelloService.pingPong(req),
      }, HelloService.Default);

      // Pass to createServerHandler
      export const POST = createServerHandler(router, handlers);

      Use createRPCHandler for a more streamlined approach that combines route handler creation and server setup. This function is kept for backward compatibility but will be turned into an internal utility in the next minor version.