28 lines
952 B
TypeScript
28 lines
952 B
TypeScript
import { Config, Effect, Layer } from "effect"
|
|
import { ImportError } from "../../ImportError"
|
|
import { ExpressApp } from "../express"
|
|
import { TRPCUnknownContextCreator } from "./TRPCContextCreator"
|
|
import { TRPCAnyRouter } from "./TRPCRouter"
|
|
|
|
|
|
const importTRPCServerExpressAdapter = Effect.tryPromise({
|
|
try: () => import("@trpc/server/adapters/express"),
|
|
catch: cause => new ImportError({ path: "@trpc/server/adapters/express", cause }),
|
|
})
|
|
|
|
export const TRPCExpressRouteLive = (
|
|
config: {
|
|
readonly path: Config.Config<string>
|
|
}
|
|
) => Layer.effectDiscard(Effect.gen(function*() {
|
|
const { createExpressMiddleware } = yield* importTRPCServerExpressAdapter
|
|
const app = yield* ExpressApp.ExpressApp
|
|
|
|
app.use(yield* config.path,
|
|
createExpressMiddleware({
|
|
router: yield* TRPCAnyRouter,
|
|
createContext: (yield* TRPCUnknownContextCreator).createExpressContext,
|
|
})
|
|
)
|
|
}))
|