Context work

This commit is contained in:
Julien Valverdé
2024-07-04 23:58:49 +02:00
parent f3a29d7831
commit cb0973b5ed
3 changed files with 20 additions and 3 deletions

View File

@@ -8,6 +8,6 @@ export const router = Effect.gen(function*() {
const proc = yield* procedure
return t.router({
ping: proc.query(() => "pong")
ping: proc.query(({ ctx }) => ctx.run(Effect.succeed("pong")))
})
})

View File

@@ -1,5 +1,6 @@
import type { CreateExpressContextOptions } from "@trpc/server/adapters/express"
import { Context, Layer } from "effect"
import { Context, Effect, Layer, Runtime } from "effect"
import type { Services } from "../Services"
import type { TRPCContext } from "./TRPCContext"
@@ -11,5 +12,14 @@ export class CreateTRPCContext extends Context.Tag("CreateTRPCContext")<CreateTR
>() {}
export module CreateTRPCContext {
export const Live = Layer.sync(CreateTRPCContext, () => ({ req }) => ({ req }))
export const Live = Layer.effect(CreateTRPCContext, Effect.gen(function*() {
const run = yield* Effect.runtime<Services>().pipe(
Effect.map(Runtime.runFork)
)
return ({ req }) => ({
req,
run,
})
}))
}

View File

@@ -1,6 +1,13 @@
import type { Effect, Runtime } from "effect"
import type { RuntimeFiber } from "effect/Fiber"
import type { Request } from "express"
import type { Services } from "../Services"
export interface TRPCContext {
req: Request
run: <A, E>(
self: Effect.Effect<A, E, Services>,
options?: Runtime.RunForkOptions,
) => RuntimeFiber<A, E>
}