effect-rpc
    Preparing search index...

    Function createServerHandler

    • Creates a web-compatible handler for your RPC router and effectful service layer.

      This function merges your provided handler layer, any additional layers, and the default server layers, then produces a handler suitable for use in serverless, edge, or traditional Node.js environments.

      Type Parameters

      • T extends RpcGroup<any>
      • Routes = ExtractRoutes<T>

      Parameters

      • router: T

        The RPC router group defining all available endpoints.

      • rpcHandler: Layer<Routes, never, never>

        The Layer containing all endpoint implementations and their dependencies.

      • serialization: SerializationLayer = RpcSerialization.layerNdjson

        (Optional) The serialization layer to use for RPC communication. Defaults to RpcSerialization.layerNdjson.

      • ...additionalLayers: Layer<any, any, never>[]

        (Optional) Additional Layer instances to merge into the environment.

      Returns (request: Request, context?: undefined) => Promise<Response>

      A function that takes a Request (and optional context) and returns a Promise<Response>.

      This is the final step for wiring up your effect-rpc API to a web framework (e.g., Next.js route handler).

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

      export const POST = createServerHandler(router, handlers);

      // Or use the combined createRPCHandler function for a more streamlined approach:
      // export const POST = createRPCHandler(router, { ... }, HelloService.Default);

      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.