Context adapter work

This commit is contained in:
Julien Valverdé
2024-07-14 04:31:13 +02:00
parent 69d9ffb3e0
commit c001738f46
4 changed files with 19 additions and 28 deletions

View File

@@ -3,7 +3,6 @@ import { Effect, Layer } from "effect"
import { rpcHTTPRoot } from "../config"
import { ExpressApp } from "../http/ExpressApp"
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
import { contextCreatorExpressAdapter } from "../trpc/adapters"
import { RPCRouter } from "./RPCRouter"
@@ -14,7 +13,7 @@ export module RPCRoute {
app.use(yield* rpcHTTPRoot,
createExpressMiddleware({
router: yield* RPCRouter,
createContext: contextCreatorExpressAdapter(yield* TRPCContextCreator),
createContext: (yield* TRPCContextCreator).createExpressContext,
}),
)
}))

View File

@@ -2,7 +2,6 @@ import { applyWSSHandler } from "@trpc/server/adapters/ws"
import { Effect, Layer } from "effect"
import { WebSocketServer } from "../http/WebSocketServer"
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
import { contextCreatorWSSAdapter } from "../trpc/adapters"
import { RPCRouter } from "./RPCRouter"
@@ -12,7 +11,7 @@ export module RPCWebSocketHandler {
return applyWSSHandler({
wss: yield* WebSocketServer,
router: yield* RPCRouter,
createContext: contextCreatorWSSAdapter(yield* TRPCContextCreator),
createContext: (yield* TRPCContextCreator).createWebSocketContext,
})
}),

View File

@@ -1,15 +1,19 @@
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, TRPCContextTransaction } from "./TRPCContext"
import { TRPCContextTransactionEnum, type TRPCContext, type TRPCContextTransaction } from "./TRPCContext"
/**
* Provides a function that instantiates a fresh context for each tRPC procedure call
* Provides functions that instantiate a fresh context for each tRPC procedure call
*/
export class TRPCContextCreator extends Context.Tag("TRPCContextCreator")<TRPCContextCreator,
(transaction: TRPCContextTransaction) => TRPCContext
>() {}
export class TRPCContextCreator extends Context.Tag("TRPCContextCreator")<TRPCContextCreator, {
createContext: (transaction: TRPCContextTransaction) => TRPCContext
createExpressContext: (context: CreateExpressContextOptions) => TRPCContext
createWebSocketContext: (context: CreateWSSContextFnOptions) => TRPCContext
}>() {}
export module TRPCContextCreator {
export const Live = Layer.effect(TRPCContextCreator, Effect.gen(function*() {
@@ -31,12 +35,19 @@ export module TRPCContextCreator {
options,
)
return transaction => ({
const createContext = (transaction: TRPCContextTransaction) => ({
runtime,
run,
fork,
transaction,
})
return {
createContext,
createExpressContext: context => createContext(TRPCContextTransactionEnum.Express(context)),
createWebSocketContext: context => createContext(TRPCContextTransactionEnum.WebSocket(context)),
}
}))
}

View File

@@ -1,18 +0,0 @@
import { type CreateExpressContextOptions } from "@trpc/server/adapters/express"
import type { CreateWSSContextFnOptions } from "@trpc/server/adapters/ws"
import type { Context } from "effect"
import { TRPCContextTransactionEnum } from "./TRPCContext"
import { TRPCContextCreator } from "./TRPCContextCreator"
export const contextCreatorExpressAdapter = (
createContext: Context.Tag.Service<typeof TRPCContextCreator>
) =>
(opts: CreateExpressContextOptions) =>
createContext(TRPCContextTransactionEnum.Express(opts))
export const contextCreatorWSSAdapter = (
createContext: Context.Tag.Service<typeof TRPCContextCreator>
) =>
(opts: CreateWSSContextFnOptions) =>
createContext(TRPCContextTransactionEnum.WebSocket(opts))