Designing a Swappable Implementation for a Posts Service in TypeScript

If I want to have one tag that I can yield in a generator and swap out the implementations, would this be the best way to setup?

interface PostsServiceClient {
    readonly _tag: "client"
    readonly create: (
        tx: EffectTransaction<ZeroSchema>,
        input: typeof insertPostSchema.Type,
    ) => Effect.Effect<any>
    readonly list: (tx: EffectTransaction<ZeroSchema>) => Effect.Effect<any>
}

interface PostsServiceServer {
    readonly _tag: "server"
    readonly create: (
        input: typeof insertPostSchema.Type,
    ) => Effect.Effect<any, PostsServiceError>
    readonly list: () => Effect.Effect<any[]>
}

type PostsServiceUnion = PostsServiceClient | PostsServiceServer

class PostsServiceUnionTag extends Context.Tag("PostsServiceUnion")<
    PostsServiceUnionTag,
    PostsServiceUnion
>() {}
Was this page helpful?