tRPC work
All checks were successful
Lint / lint (push) Successful in 12s

This commit is contained in:
Julien Valverdé
2024-09-06 03:54:18 +02:00
parent f232da4630
commit 9b80664c95
5 changed files with 52 additions and 11 deletions

View File

@@ -0,0 +1,18 @@
import { initTRPC } from "@trpc/server"
import { Context, Layer } from "effect"
import { type TRPCContext } from "./TRPCContext"
const createTRPC = <R>() => initTRPC.context<TRPCContext<R>>().create()
export class TRPCUnknownBuilder extends Context.Tag("@thilalib/TRPC/TRPCBuilder")<TRPCUnknownBuilder,
ReturnType<typeof createTRPC<unknown>>
>() {}
export const make = <R>() => {
class TRPCBuilder extends Context.Tag("@thilalib/TRPC/TRPCBuilder")<TRPCBuilder, ReturnType<typeof createTRPC<R>>>() {}
const TRPCBuilderLive = Layer.sync(TRPCBuilder, createTRPC)
return { TRPCBuilder, TRPCBuilderLive }
}

View File

@@ -5,17 +5,21 @@ import { createTRCPErrorMapper } from "./createTRCPErrorMapper"
import { TRPCContextTransactionEnum, type TRPCContext, type TRPCContextTransaction } from "./TRPCContext" import { TRPCContextTransactionEnum, type TRPCContext, type TRPCContextTransaction } from "./TRPCContext"
export { TRPCErrorCause } from "./createTRCPErrorMapper" export interface TRPCContextCreatorService<R> {
export { TRPCContextTransactionEnum } from "./TRPCContext"
export type { TRPCContext, TRPCContextTransaction } from "./TRPCContext"
export const make = <R>() => {
class TRPCContextCreator extends Context.Tag("TRPCContextCreator")<TRPCContextCreator, {
readonly createContext: (transaction: TRPCContextTransaction) => TRPCContext<R> readonly createContext: (transaction: TRPCContextTransaction) => TRPCContext<R>
readonly createExpressContext: (context: CreateExpressContextOptions) => TRPCContext<R> readonly createExpressContext: (context: CreateExpressContextOptions) => TRPCContext<R>
readonly createWebSocketContext: (context: CreateWSSContextFnOptions) => TRPCContext<R> readonly createWebSocketContext: (context: CreateWSSContextFnOptions) => TRPCContext<R>
}>() {} }
export class TRPCUnknownContextCreator extends Context.Tag("@thilalib/TRPC/TRPCContextCreator")<TRPCUnknownContextCreator,
TRPCContextCreatorService<unknown>
>() {}
export const make = <R>() => {
class TRPCContextCreator extends Context.Tag("@thilalib/TRPC/TRPCContextCreator")<TRPCContextCreator,
TRPCContextCreatorService<R>
>() {}
const TRPCContextCreatorLive = Layer.effect(TRPCContextCreator, Effect.gen(function*() { const TRPCContextCreatorLive = Layer.effect(TRPCContextCreator, Effect.gen(function*() {
const runtime = yield* Effect.runtime<R>() const runtime = yield* Effect.runtime<R>()
@@ -37,14 +41,12 @@ export const make = <R>() => {
options, options,
) )
const createContext = (transaction: TRPCContextTransaction) => ({ const createContext = (transaction: TRPCContextTransaction) => ({
runtime, runtime,
run, run,
fork, fork,
transaction, transaction,
}) })
const createExpressContext = (context: CreateExpressContextOptions) => createContext(TRPCContextTransactionEnum.Express(context)) const createExpressContext = (context: CreateExpressContextOptions) => createContext(TRPCContextTransactionEnum.Express(context))
const createWebSocketContext = (context: CreateWSSContextFnOptions) => createContext(TRPCContextTransactionEnum.WebSocket(context)) const createWebSocketContext = (context: CreateWSSContextFnOptions) => createContext(TRPCContextTransactionEnum.WebSocket(context))

View File

@@ -0,0 +1,18 @@
import type { AnyRouterDef, Router } from "@trpc/server"
import { Context, Effect, Layer } from "effect"
export class TRPCAnyRouter extends Context.Tag("@thilalib/TRCP/TRPCRouter")<TRPCAnyRouter,
Router<AnyRouterDef>
>() {}
export const make = <
A extends Router<AnyRouterDef>,
E, R,
>(router: Effect.Effect<A, E, R>) => {
class TRPCRouter extends Context.Tag("@thilalib/TRCP/TRPCRouter")<TRPCRouter, A>() {}
const TRPCRouterLive = Layer.effect(TRPCRouter, router)
return { TRPCRouter, TRPCRouterLive }
}

View File

@@ -60,7 +60,7 @@ export const createTRCPErrorMapper = importTRPCServer.pipe(Effect.map(({ TRPCErr
) )
)) ))
export class TRPCErrorCause<E> extends Error { class TRPCErrorCause<E> extends Error {
constructor(readonly cause: Cause.Cause<E>) { constructor(readonly cause: Cause.Cause<E>) {
super() super()
} }

View File

@@ -1 +1,4 @@
export * as TRPCBuilder from "./TRPCBuilder"
export * as TRPCContext from "./TRPCContext"
export * as TRPCContextCreator from "./TRPCContextCreator" export * as TRPCContextCreator from "./TRPCContextCreator"
export * as TRPCRouter from "./TRPCRouter"