0.1.17 #18
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"
|
||||
|
||||
|
||||
export { TRPCErrorCause } from "./createTRCPErrorMapper"
|
||||
export { TRPCContextTransactionEnum } from "./TRPCContext"
|
||||
export type { TRPCContext, TRPCContextTransaction } from "./TRPCContext"
|
||||
|
||||
|
||||
export const make = <R>() => {
|
||||
class TRPCContextCreator extends Context.Tag("TRPCContextCreator")<TRPCContextCreator, {
|
||||
export interface TRPCContextCreatorService<R> {
|
||||
readonly createContext: (transaction: TRPCContextTransaction) => TRPCContext<R>
|
||||
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>() => {
|
||||
class TRPCContextCreator extends Context.Tag("@thilalib/TRPC/TRPCContextCreator")<TRPCContextCreator,
|
||||
TRPCContextCreatorService<R>
|
||||
>() {}
|
||||
|
||||
const TRPCContextCreatorLive = Layer.effect(TRPCContextCreator, Effect.gen(function*() {
|
||||
const runtime = yield* Effect.runtime<R>()
|
||||
@@ -37,14 +41,12 @@ export const make = <R>() => {
|
||||
options,
|
||||
)
|
||||
|
||||
|
||||
const createContext = (transaction: TRPCContextTransaction) => ({
|
||||
runtime,
|
||||
run,
|
||||
fork,
|
||||
transaction,
|
||||
})
|
||||
|
||||
const createExpressContext = (context: CreateExpressContextOptions) => createContext(TRPCContextTransactionEnum.Express(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>) {
|
||||
super()
|
||||
}
|
||||
|
||||
@@ -1 +1,4 @@
|
||||
export * as TRPCBuilder from "./TRPCBuilder"
|
||||
export * as TRPCContext from "./TRPCContext"
|
||||
export * as TRPCContextCreator from "./TRPCContextCreator"
|
||||
export * as TRPCRouter from "./TRPCRouter"
|
||||
|
||||
Reference in New Issue
Block a user