This commit is contained in:
18
src/Layers/trpc/TRPCBuilder.ts
Normal file
18
src/Layers/trpc/TRPCBuilder.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { initTRPC } from "@trpc/server"
|
||||||
|
import { Context, Layer } from "effect"
|
||||||
|
import { type TRPCContext } from "./TRPCContext"
|
||||||
|
|
||||||
|
|
||||||
|
const createTRPC = <R>() => initTRPC.context<TRPCContext<R>>().create()
|
||||||
|
|
||||||
|
export class TRPCUnknownBuilder extends Context.Tag("@thilalib/TRPC/TRPCBuilder")<TRPCUnknownBuilder,
|
||||||
|
ReturnType<typeof createTRPC<unknown>>
|
||||||
|
>() {}
|
||||||
|
|
||||||
|
|
||||||
|
export const make = <R>() => {
|
||||||
|
class TRPCBuilder extends Context.Tag("@thilalib/TRPC/TRPCBuilder")<TRPCBuilder, ReturnType<typeof createTRPC<R>>>() {}
|
||||||
|
const TRPCBuilderLive = Layer.sync(TRPCBuilder, createTRPC)
|
||||||
|
|
||||||
|
return { TRPCBuilder, TRPCBuilderLive }
|
||||||
|
}
|
||||||
@@ -5,17 +5,21 @@ import { createTRCPErrorMapper } from "./createTRCPErrorMapper"
|
|||||||
import { TRPCContextTransactionEnum, type TRPCContext, type TRPCContextTransaction } from "./TRPCContext"
|
import { TRPCContextTransactionEnum, type TRPCContext, type TRPCContextTransaction } from "./TRPCContext"
|
||||||
|
|
||||||
|
|
||||||
export { TRPCErrorCause } from "./createTRCPErrorMapper"
|
export interface TRPCContextCreatorService<R> {
|
||||||
export { TRPCContextTransactionEnum } from "./TRPCContext"
|
readonly createContext: (transaction: TRPCContextTransaction) => TRPCContext<R>
|
||||||
export type { TRPCContext, TRPCContextTransaction } from "./TRPCContext"
|
readonly createExpressContext: (context: CreateExpressContextOptions) => TRPCContext<R>
|
||||||
|
readonly createWebSocketContext: (context: CreateWSSContextFnOptions) => TRPCContext<R>
|
||||||
|
}
|
||||||
|
|
||||||
|
export class TRPCUnknownContextCreator extends Context.Tag("@thilalib/TRPC/TRPCContextCreator")<TRPCUnknownContextCreator,
|
||||||
|
TRPCContextCreatorService<unknown>
|
||||||
|
>() {}
|
||||||
|
|
||||||
|
|
||||||
export const make = <R>() => {
|
export const make = <R>() => {
|
||||||
class TRPCContextCreator extends Context.Tag("TRPCContextCreator")<TRPCContextCreator, {
|
class TRPCContextCreator extends Context.Tag("@thilalib/TRPC/TRPCContextCreator")<TRPCContextCreator,
|
||||||
readonly createContext: (transaction: TRPCContextTransaction) => TRPCContext<R>
|
TRPCContextCreatorService<R>
|
||||||
readonly createExpressContext: (context: CreateExpressContextOptions) => TRPCContext<R>
|
>() {}
|
||||||
readonly createWebSocketContext: (context: CreateWSSContextFnOptions) => TRPCContext<R>
|
|
||||||
}>() {}
|
|
||||||
|
|
||||||
const TRPCContextCreatorLive = Layer.effect(TRPCContextCreator, Effect.gen(function*() {
|
const TRPCContextCreatorLive = Layer.effect(TRPCContextCreator, Effect.gen(function*() {
|
||||||
const runtime = yield* Effect.runtime<R>()
|
const runtime = yield* Effect.runtime<R>()
|
||||||
@@ -37,14 +41,12 @@ export const make = <R>() => {
|
|||||||
options,
|
options,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
const createContext = (transaction: TRPCContextTransaction) => ({
|
const createContext = (transaction: TRPCContextTransaction) => ({
|
||||||
runtime,
|
runtime,
|
||||||
run,
|
run,
|
||||||
fork,
|
fork,
|
||||||
transaction,
|
transaction,
|
||||||
})
|
})
|
||||||
|
|
||||||
const createExpressContext = (context: CreateExpressContextOptions) => createContext(TRPCContextTransactionEnum.Express(context))
|
const createExpressContext = (context: CreateExpressContextOptions) => createContext(TRPCContextTransactionEnum.Express(context))
|
||||||
const createWebSocketContext = (context: CreateWSSContextFnOptions) => createContext(TRPCContextTransactionEnum.WebSocket(context))
|
const createWebSocketContext = (context: CreateWSSContextFnOptions) => createContext(TRPCContextTransactionEnum.WebSocket(context))
|
||||||
|
|
||||||
|
|||||||
18
src/Layers/trpc/TRPCRouter.ts
Normal file
18
src/Layers/trpc/TRPCRouter.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import type { AnyRouterDef, Router } from "@trpc/server"
|
||||||
|
import { Context, Effect, Layer } from "effect"
|
||||||
|
|
||||||
|
|
||||||
|
export class TRPCAnyRouter extends Context.Tag("@thilalib/TRCP/TRPCRouter")<TRPCAnyRouter,
|
||||||
|
Router<AnyRouterDef>
|
||||||
|
>() {}
|
||||||
|
|
||||||
|
|
||||||
|
export const make = <
|
||||||
|
A extends Router<AnyRouterDef>,
|
||||||
|
E, R,
|
||||||
|
>(router: Effect.Effect<A, E, R>) => {
|
||||||
|
class TRPCRouter extends Context.Tag("@thilalib/TRCP/TRPCRouter")<TRPCRouter, A>() {}
|
||||||
|
const TRPCRouterLive = Layer.effect(TRPCRouter, router)
|
||||||
|
|
||||||
|
return { TRPCRouter, TRPCRouterLive }
|
||||||
|
}
|
||||||
@@ -60,7 +60,7 @@ export const createTRCPErrorMapper = importTRPCServer.pipe(Effect.map(({ TRPCErr
|
|||||||
)
|
)
|
||||||
))
|
))
|
||||||
|
|
||||||
export class TRPCErrorCause<E> extends Error {
|
class TRPCErrorCause<E> extends Error {
|
||||||
constructor(readonly cause: Cause.Cause<E>) {
|
constructor(readonly cause: Cause.Cause<E>) {
|
||||||
super()
|
super()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,4 @@
|
|||||||
|
export * as TRPCBuilder from "./TRPCBuilder"
|
||||||
|
export * as TRPCContext from "./TRPCContext"
|
||||||
export * as TRPCContextCreator from "./TRPCContextCreator"
|
export * as TRPCContextCreator from "./TRPCContextCreator"
|
||||||
|
export * as TRPCRouter from "./TRPCRouter"
|
||||||
|
|||||||
Reference in New Issue
Block a user