/** * A typed Effect `RpcClient` over a Durable Object namespace whose stubs * expose an Effect `fetch` — the client half of `RpcDurableObject`, shared * by every engine. * * @internal */ import * as Effect from "effect/Effect"; import * as HttpClient from "effect/http/HttpClient"; import * as Layer from "effect/Layer"; import { RpcClient, RpcSerialization, type Rpc, type RpcGroup } from "effect/rpc"; import type * as RpcClientError from "effect/rpc/RpcClientError"; export const bindEffectRpc = ( namespace: { readonly getByName: (id: string, options?: any) => { readonly fetch: any }; }, group: RpcGroup.RpcGroup, options?: { /** * Override the rpc serialization layer. Defaults to NDJSON, which * is required when any rpc in the group is a streaming rpc. */ readonly serialization?: Layer.Layer; }, ): { readonly getByName: ( id: string, options?: any, ) => Effect.Effect< RpcClient.RpcClient, never, Rpc.MiddlewareClient >; } => { const serialization = options?.serialization ?? RpcSerialization.layerNdjson; return { getByName: Effect.fn(function* (id: string, options?: any) { const httpClient = HttpClient.layerMergedContext( Effect.sync(() => { const stub = namespace.getByName(id, options); return HttpClient.make((request) => stub.fetch(request)); }), ); const protocol = RpcClient.layerProtocolHttp({ url: "http://alchemy-rpc/" }).pipe( Layer.provide(serialization), Layer.provide(httpClient), ); return yield* RpcClient.make(group).pipe(Effect.provide(protocol)); }) as any, }; };