Router work

This commit is contained in:
Julien Valverdé
2024-07-05 19:31:35 +02:00
parent a49843ec89
commit bb1a422097
4 changed files with 17 additions and 19 deletions

View File

@@ -6,4 +6,4 @@ export const ServicesLive = Layer.mergeAll(
TodoRepositoryLive TodoRepositoryLive
) )
export type Services = Layer.Layer.Success<typeof ServicesLive> export interface Services extends Layer.Layer.Success<typeof ServicesLive> {}

View File

@@ -11,6 +11,14 @@ export const todosRouter = Effect.gen(function*() {
const procedure = yield* RPCProcedureBuilder const procedure = yield* RPCProcedureBuilder
return t.router({ 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 get: procedure
.input(S.decodeUnknownSync( .input(S.decodeUnknownSync(
S.Struct({ id: S.String }) S.Struct({ id: S.String })
@@ -22,17 +30,5 @@ export const todosRouter = Effect.gen(function*() {
yield* todos.get(input.id) 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)
)
}))),
}) })
}) })

View File

@@ -1,12 +1,14 @@
import type { Effect } from "effect" import type { Effect, Runtime } from "effect"
import type { Request } from "express" import type { Request } from "express"
import type { Services } from "../Services" import type { Services } from "../Services"
export interface TRPCContext { export interface TRPCContext {
req: Request runtime: Runtime.Runtime<Services>
run: <A, E>( run: <A, E>(
effect: Effect.Effect<A, E, Services>, effect: Effect.Effect<A, E, Services>,
options?: { readonly signal?: AbortSignal } options?: { readonly signal?: AbortSignal }
) => Promise<A> ) => Promise<A>
req: Request
} }

View File

@@ -13,13 +13,13 @@ export class TRPCContextCreator extends Context.Tag("TRPCContextCreator")<TRPCCo
export module TRPCContextCreator { export module TRPCContextCreator {
export const Live = Layer.effect(TRPCContextCreator, Effect.gen(function*() { export const Live = Layer.effect(TRPCContextCreator, Effect.gen(function*() {
const run = yield* Effect.runtime<Services>().pipe( const runtime = yield* Effect.runtime<Services>()
Effect.map(Runtime.runPromise) const run = Runtime.runPromise(runtime)
)
return ({ req }) => ({ return ({ req }) => ({
req, runtime,
run, run,
req,
}) })
})) }))
} }