TRPCContextCreator
This commit is contained in:
25
packages/server/src/trpc/TRPCContextCreator.ts
Normal file
25
packages/server/src/trpc/TRPCContextCreator.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import type { CreateExpressContextOptions } from "@trpc/server/adapters/express"
|
||||
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")<TRPCContextCreator,
|
||||
(opts: CreateExpressContextOptions) => TRPCContext
|
||||
>() {}
|
||||
|
||||
export module TRPCContextCreator {
|
||||
export const Live = Layer.effect(TRPCContextCreator, Effect.gen(function*() {
|
||||
const run = yield* Effect.runtime<Services>().pipe(
|
||||
Effect.map(Runtime.runFork)
|
||||
)
|
||||
|
||||
return ({ req }) => ({
|
||||
req,
|
||||
run,
|
||||
})
|
||||
}))
|
||||
}
|
||||
Reference in New Issue
Block a user