import { TRPCError } from "@trpc/server" import type { CreateExpressContextOptions } from "@trpc/server/adapters/express" import type { CreateWSSContextFnOptions } from "@trpc/server/adapters/ws" import { Context, Effect, Layer, Runtime } from "effect" import type { Services } from "../Services" import type { TRPCContext } from "./TRPCContext" /** * Provides a function that instantiates a fresh context for each tRPC procedure call */ export class TRPCContextCreator extends Context.Tag("TRPCContextCreator") TRPCContext >() {} export module TRPCContextCreator { export const Live = Layer.effect(TRPCContextCreator, Effect.gen(function*() { const runtime = yield* Effect.runtime() const run = Runtime.runPromise(runtime) return ({ req }) => ({ runtime, run, // req, }) })) } const mapErrorsToTRPC = (effect: Effect.Effect) => Effect.sandbox(effect).pipe( Effect.catchTags({ Die: cause => Effect.fail( new TRPCError({ code: "INTERNAL_SERVER_ERROR", cause }) ), Interrupt: cause => Effect.fail( new TRPCError({ code: "INTERNAL_SERVER_ERROR", cause }) ), Fail: cause => Effect.fail( new TRPCError({ code: "INTERNAL_SERVER_ERROR", cause }) ), }) )