Merging Rpc Routers
Hi, how can I make this two routers into one?
The example is oversimplified, I have multiple routers on different files.
The example is oversimplified, I have multiple routers on different files.
import "server-only"
import type { NextRequest } from "next/server"
import { Rpc, RpcRouter } from "@effect/rpc"
import { Effect, Layer, ManagedRuntime } from "effect"
import { SayHelloReq, SayHelloReq1 } from "@/app/router/schema"
import { NextService } from "@/effect/providers/next"
import { RequestService } from "@/effect/providers/request"
import { NodeSdkLive } from "@/effect/providers/tracing"
const helloRouter = RpcRouter.make(
Rpc.effect(SayHelloReq, ({ name }) => {
return Effect.gen(function* () {
return yield* Effect.succeed({
name: "Hello " + name,
})
}).pipe(Effect.withSpan("SayHelloReq", { attributes: { name } }))
})
)
const helloRouter2 = RpcRouter.make(
Rpc.effect(SayHelloReq1, ({ name }) => {
return Effect.gen(function* () {
return yield* Effect.succeed({
name: "Hello " + name,
})
}).pipe(Effect.withSpan("SayHelloReq", { attributes: { name } }))
})
)
export type HelloRouter = typeof helloRouter
const handler = RpcRouter.toHandlerNoStream(helloRouter)
export const dynamic = "force-dynamic"
const MainLayer = Layer.mergeAll(NextService.Live, NodeSdkLive, RequestService.Live)
const LiveRuntime = ManagedRuntime.make(MainLayer)
export const POST = async (req: NextRequest) => {
const data = await req.json()
return await handler(data).pipe(
Effect.andThen(Response.json),
Effect.tapErrorCause(Effect.logError),
Effect.withSpan("rpc-call"),
LiveRuntime.runPromise
)
}import "server-only"
import type { NextRequest } from "next/server"
import { Rpc, RpcRouter } from "@effect/rpc"
import { Effect, Layer, ManagedRuntime } from "effect"
import { SayHelloReq, SayHelloReq1 } from "@/app/router/schema"
import { NextService } from "@/effect/providers/next"
import { RequestService } from "@/effect/providers/request"
import { NodeSdkLive } from "@/effect/providers/tracing"
const helloRouter = RpcRouter.make(
Rpc.effect(SayHelloReq, ({ name }) => {
return Effect.gen(function* () {
return yield* Effect.succeed({
name: "Hello " + name,
})
}).pipe(Effect.withSpan("SayHelloReq", { attributes: { name } }))
})
)
const helloRouter2 = RpcRouter.make(
Rpc.effect(SayHelloReq1, ({ name }) => {
return Effect.gen(function* () {
return yield* Effect.succeed({
name: "Hello " + name,
})
}).pipe(Effect.withSpan("SayHelloReq", { attributes: { name } }))
})
)
export type HelloRouter = typeof helloRouter
const handler = RpcRouter.toHandlerNoStream(helloRouter)
export const dynamic = "force-dynamic"
const MainLayer = Layer.mergeAll(NextService.Live, NodeSdkLive, RequestService.Live)
const LiveRuntime = ManagedRuntime.make(MainLayer)
export const POST = async (req: NextRequest) => {
const data = await req.json()
return await handler(data).pipe(
Effect.andThen(Response.json),
Effect.tapErrorCause(Effect.logError),
Effect.withSpan("rpc-call"),
LiveRuntime.runPromise
)
}