Context adapter work
This commit is contained in:
@@ -3,7 +3,6 @@ import { Effect, Layer } from "effect"
|
|||||||
import { rpcHTTPRoot } from "../config"
|
import { rpcHTTPRoot } from "../config"
|
||||||
import { ExpressApp } from "../http/ExpressApp"
|
import { ExpressApp } from "../http/ExpressApp"
|
||||||
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
|
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
|
||||||
import { contextCreatorExpressAdapter } from "../trpc/adapters"
|
|
||||||
import { RPCRouter } from "./RPCRouter"
|
import { RPCRouter } from "./RPCRouter"
|
||||||
|
|
||||||
|
|
||||||
@@ -14,7 +13,7 @@ export module RPCRoute {
|
|||||||
app.use(yield* rpcHTTPRoot,
|
app.use(yield* rpcHTTPRoot,
|
||||||
createExpressMiddleware({
|
createExpressMiddleware({
|
||||||
router: yield* RPCRouter,
|
router: yield* RPCRouter,
|
||||||
createContext: contextCreatorExpressAdapter(yield* TRPCContextCreator),
|
createContext: (yield* TRPCContextCreator).createExpressContext,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}))
|
}))
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import { applyWSSHandler } from "@trpc/server/adapters/ws"
|
|||||||
import { Effect, Layer } from "effect"
|
import { Effect, Layer } from "effect"
|
||||||
import { WebSocketServer } from "../http/WebSocketServer"
|
import { WebSocketServer } from "../http/WebSocketServer"
|
||||||
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
|
import { TRPCContextCreator } from "../trpc/TRPCContextCreator"
|
||||||
import { contextCreatorWSSAdapter } from "../trpc/adapters"
|
|
||||||
import { RPCRouter } from "./RPCRouter"
|
import { RPCRouter } from "./RPCRouter"
|
||||||
|
|
||||||
|
|
||||||
@@ -12,7 +11,7 @@ export module RPCWebSocketHandler {
|
|||||||
return applyWSSHandler({
|
return applyWSSHandler({
|
||||||
wss: yield* WebSocketServer,
|
wss: yield* WebSocketServer,
|
||||||
router: yield* RPCRouter,
|
router: yield* RPCRouter,
|
||||||
createContext: contextCreatorWSSAdapter(yield* TRPCContextCreator),
|
createContext: (yield* TRPCContextCreator).createWebSocketContext,
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,19 @@
|
|||||||
import { TRPCError } from "@trpc/server"
|
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 { Context, Effect, Layer, Runtime } from "effect"
|
||||||
import type { Services } from "../Services"
|
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,
|
export class TRPCContextCreator extends Context.Tag("TRPCContextCreator")<TRPCContextCreator, {
|
||||||
(transaction: TRPCContextTransaction) => TRPCContext
|
createContext: (transaction: TRPCContextTransaction) => TRPCContext
|
||||||
>() {}
|
createExpressContext: (context: CreateExpressContextOptions) => TRPCContext
|
||||||
|
createWebSocketContext: (context: CreateWSSContextFnOptions) => TRPCContext
|
||||||
|
}>() {}
|
||||||
|
|
||||||
export module TRPCContextCreator {
|
export module TRPCContextCreator {
|
||||||
export const Live = Layer.effect(TRPCContextCreator, Effect.gen(function*() {
|
export const Live = Layer.effect(TRPCContextCreator, Effect.gen(function*() {
|
||||||
@@ -31,12 +35,19 @@ export module TRPCContextCreator {
|
|||||||
options,
|
options,
|
||||||
)
|
)
|
||||||
|
|
||||||
return transaction => ({
|
|
||||||
|
const createContext = (transaction: TRPCContextTransaction) => ({
|
||||||
runtime,
|
runtime,
|
||||||
run,
|
run,
|
||||||
fork,
|
fork,
|
||||||
transaction,
|
transaction,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
createContext,
|
||||||
|
createExpressContext: context => createContext(TRPCContextTransactionEnum.Express(context)),
|
||||||
|
createWebSocketContext: context => createContext(TRPCContextTransactionEnum.WebSocket(context)),
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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))
|
|
||||||
Reference in New Issue
Block a user