RPC server work
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { Context, Layer } from "effect"
|
||||
import express from "express"
|
||||
import express, { type Express } from "express"
|
||||
|
||||
|
||||
export class ExpressApp extends Context.Tag("Express")<ExpressApp, ReturnType<typeof express>>() {}
|
||||
export class ExpressApp extends Context.Tag("Express")<ExpressApp, Express>() {}
|
||||
|
||||
export module ExpressApp {
|
||||
export const Live = Layer.sync(ExpressApp, () => express())
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { createExpressMiddleware } from "@trpc/server/adapters/express"
|
||||
import { Config, Effect, Layer } from "effect"
|
||||
import { ExpressApp } from "../express/ExpressApp"
|
||||
import { createTRPCContext } from "../trpc/context"
|
||||
import { CreateTRPCContext } from "../trpc/CreateTRPCContext"
|
||||
import { router } from "./routers"
|
||||
|
||||
|
||||
export const RPCServerLive = Layer.effectDiscard(Effect.gen(function*() {
|
||||
export module RPCServer {
|
||||
export const Live = Layer.effectDiscard(Effect.gen(function*() {
|
||||
const app = yield* ExpressApp
|
||||
|
||||
app.use(
|
||||
@@ -13,7 +14,8 @@ export const RPCServerLive = Layer.effectDiscard(Effect.gen(function*() {
|
||||
|
||||
createExpressMiddleware({
|
||||
router: yield* router,
|
||||
createContext: createTRPCContext,
|
||||
createContext: yield* CreateTRPCContext,
|
||||
}),
|
||||
)
|
||||
}))
|
||||
}
|
||||
|
||||
12
packages/server/src/trpc/CreateTRPCContext.ts
Normal file
12
packages/server/src/trpc/CreateTRPCContext.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { CreateExpressContextOptions } from "@trpc/server/adapters/express"
|
||||
import { Context, Layer } from "effect"
|
||||
import type { TRPCContext } from "./TRPCContext"
|
||||
|
||||
|
||||
export class CreateTRPCContext extends Context.Tag("CreateTRPCContext")<CreateTRPCContext,
|
||||
(opts: CreateExpressContextOptions) => TRPCContext
|
||||
>() {}
|
||||
|
||||
export module CreateTRPCContext {
|
||||
export const Live = Layer.sync(CreateTRPCContext, () => ({ req }) => ({ req }))
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { initTRPC } from "@trpc/server"
|
||||
import { Context, Layer } from "effect"
|
||||
import type { TRPCContext } from "./context"
|
||||
import type { TRPCContext } from "./TRPCContext"
|
||||
|
||||
|
||||
const createTRPC = () => initTRPC.context<TRPCContext>().create()
|
||||
|
||||
6
packages/server/src/trpc/TRPCContext.ts
Normal file
6
packages/server/src/trpc/TRPCContext.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import type { Request } from "express"
|
||||
|
||||
|
||||
export interface TRPCContext {
|
||||
req: Request
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
import type { CreateExpressContextOptions } from "@trpc/server/adapters/express"
|
||||
|
||||
|
||||
export const createTRPCContext = ({ req }: CreateExpressContextOptions) => ({ req })
|
||||
export type TRPCContext = Awaited<ReturnType<typeof createTRPCContext>>
|
||||
Reference in New Issue
Block a user