From 4e55a699378526aa5812b47e7fff057263d4d50c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Fri, 6 Sep 2024 07:38:02 +0200 Subject: [PATCH] WebSocketOnly --- src/Layers/trpc/middlewares.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Layers/trpc/middlewares.ts b/src/Layers/trpc/middlewares.ts index c63b0d1..fcd57e3 100644 --- a/src/Layers/trpc/middlewares.ts +++ b/src/Layers/trpc/middlewares.ts @@ -17,7 +17,27 @@ export const ExpressOnly = importTRPCServer.pipe(Effect.map(({ Match.orElse(() => { throw new TRPCError({ code: "BAD_REQUEST", - message: "Only Express backend is supported by this procedure", + message: "Only Express transport is supported by this procedure", + }) + }), + ) +))) + +export const WebSocketOnly = importTRPCServer.pipe(Effect.map(({ + experimental_standaloneMiddleware, + TRPCError, +}) => experimental_standaloneMiddleware<{ + ctx: { readonly transaction: TRPCContextTransaction } +}>().create(opts => + Match.value(opts.ctx.transaction).pipe( + Match.tag("WebSocket", transaction => + opts.next({ ctx: { transaction } }) + ), + + Match.orElse(() => { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Only WebSocket transport is supported by this procedure", }) }), )