Effect CommunityEC
Effect Community16mo ago
9 replies
jrmdayn

### Decoupling Frontend from Backend in RPC Client with Shared Type Definitions

In order to make a rpc client, I have to call:
import type { RpcRouter } from "@myserver/rpc"
const makeClient = HttpRpcResolver.makeClient<RpcRouter>(myUrl)


This type import is bothering me, because it means my frontend package depends on my backend package.

I am wondering if it would be possible to break this dependency by introducing a type helper that would only need to know about the requests schemas (GetUserIds and GetUser to follow the example in rpc package).

For ex:

// frontend package
import type { RpcRouterLike } from "@shared"
const makeClient = HttpRpcResolver.makeClient<RpcRouter>(myUrl)

// shared package
export class GetUserIds ...
export class GetUser ...
export type RpcRouterLike = InferRpcRouter<[GetUserIds, GetUser]> // where the magic happens

// backend package
import { GetUserIds, GetUser } from "@shared"
const router = RpcRouter.make(
  Rpc.stream(GetUserIds, ...),
  Rpc.effect(GetUser, ...)
)


That way, frontend and backend packages only know about limited types located in shared package.

Can this be done or there is a fundamental blocker with how the RpcRouter type is inferred at the moment? I'm thinking the R (Requirements) parameter could be a prob, but from client side, it seems to be never, at least in the repo example it is.
Was this page helpful?