Router work
This commit is contained in:
@@ -6,4 +6,4 @@ export const ServicesLive = Layer.mergeAll(
|
||||
TodoRepositoryLive
|
||||
)
|
||||
|
||||
export type Services = Layer.Layer.Success<typeof ServicesLive>
|
||||
export interface Services extends Layer.Layer.Success<typeof ServicesLive> {}
|
||||
|
||||
@@ -11,6 +11,14 @@ export const todosRouter = Effect.gen(function*() {
|
||||
const procedure = yield* RPCProcedureBuilder
|
||||
|
||||
return t.router({
|
||||
all: procedure.query(({ ctx }) => ctx.run(Effect.gen(function*() {
|
||||
const todos = yield* TodoRepository
|
||||
|
||||
return yield* S.encode(S.Array(JsonifiableTodo))(
|
||||
yield* todos.todos.get
|
||||
)
|
||||
}))),
|
||||
|
||||
get: procedure
|
||||
.input(S.decodeUnknownSync(
|
||||
S.Struct({ id: S.String })
|
||||
@@ -22,17 +30,5 @@ export const todosRouter = Effect.gen(function*() {
|
||||
yield* todos.get(input.id)
|
||||
)
|
||||
}))),
|
||||
|
||||
getOrThrow: procedure
|
||||
.input(S.decodeUnknownSync(
|
||||
S.Struct({ id: S.String })
|
||||
))
|
||||
.query(({ ctx, input }) => ctx.run(Effect.gen(function*() {
|
||||
const todos = yield* TodoRepository
|
||||
|
||||
return yield* S.encode(JsonifiableTodo)(
|
||||
yield* yield* todos.get(input.id)
|
||||
)
|
||||
}))),
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import type { Effect } from "effect"
|
||||
import type { Effect, Runtime } from "effect"
|
||||
import type { Request } from "express"
|
||||
import type { Services } from "../Services"
|
||||
|
||||
|
||||
export interface TRPCContext {
|
||||
req: Request
|
||||
runtime: Runtime.Runtime<Services>
|
||||
run: <A, E>(
|
||||
effect: Effect.Effect<A, E, Services>,
|
||||
options?: { readonly signal?: AbortSignal }
|
||||
) => Promise<A>
|
||||
|
||||
req: Request
|
||||
}
|
||||
|
||||
@@ -13,13 +13,13 @@ export class TRPCContextCreator extends Context.Tag("TRPCContextCreator")<TRPCCo
|
||||
|
||||
export module TRPCContextCreator {
|
||||
export const Live = Layer.effect(TRPCContextCreator, Effect.gen(function*() {
|
||||
const run = yield* Effect.runtime<Services>().pipe(
|
||||
Effect.map(Runtime.runPromise)
|
||||
)
|
||||
const runtime = yield* Effect.runtime<Services>()
|
||||
const run = Runtime.runPromise(runtime)
|
||||
|
||||
return ({ req }) => ({
|
||||
req,
|
||||
runtime,
|
||||
run,
|
||||
req,
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user