effect-rpc
    Preparing search index...

    Function createRPCHandler

    • Creates a web-compatible handler that combines route implementations and server setup in one function.

      This is a convenience function that internally calls both createRouteHandler and createServerHandler to provide a streamlined API for creating RPC handlers.

      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 available endpoints.

      • reqImplementations: RequestImplementations<T, V, R>

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

      • config: RPCHandlerConfig<R>

        Configuration object

        Configuration object for the RPC handler. This is used to provide the service layers, serialization, and additional layers.

        • OptionaladditionalLayers?: Layer.Layer<any, any, never>[]

          Additional Layer instances to merge into the environment. This can be used to provide additional dependencies required by the handlers, such as authentication middleware or other services. This is optional and can be omitted or an empty array if no additional layers are needed.

        • Optionalserialization?: SerializationLayer

          The serialization layer to use for RPC communication. Defaults to RpcSerialization.layerNdjson. Must be of type SerializationLayer. If you use a custom serialization layer, make sure it is compatible with the RPC server you are communicating with. This means, you most likely want to modify the createEffectRPC function invocation to use the same serialization layer!

        • serviceLayers: Layer.Layer<R>

          The Layer providing all dependencies required by the handlers (e.g., service implementations). This is typically the default service layer for the service being used.

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

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

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

      export const POST = handler;

      0.3.0