@@ -30,7 +30,7 @@ export type RPCHandlerFactory<RequestContextMap extends Record<string, RPCContex
3030 headers : any
3131) => Effect . Effect <
3232 Request . Request . Success < Req > ,
33- Request . Request . Error < Req > ,
33+ Request . Request . Error < Req > | RequestContextMapErrors < RequestContextMap > ,
3434 // the middleware will remove from HandlerR the dynamic context, but will also add the MiddlewareR
3535 | MiddlewareR
3636 // & S.Schema<Req, any, never> is useless here but useful when creating the middleware
@@ -64,15 +64,10 @@ export const makeMiddlewareLayer = <
6464 MakeContextProviderR
6565 >
6666) => {
67- if ( ! middleware . execute && ! middleware . executeContextual ) {
68- throw new Error ( "No execute or executeContextual provided in middleware" )
69- }
7067 const middlewareLayer = Layer
7168 . effect (
7269 MiddlewareMaker ,
73- middleware . execute
74- ? middleware . execute
75- : middleware . executeContextual ! ( makeRpcHandler < RequestContextMap , MiddlewareR > ( ) )
70+ middleware . execute ! ( makeRpcHandler < RequestContextMap , MiddlewareR > ( ) )
7671 )
7772 . pipe ( middleware . dependencies ? Layer . provide ( middleware . dependencies ) as any : ( _ ) => _ )
7873
@@ -106,13 +101,7 @@ export interface Middleware<
106101 & {
107102 Default : Layer . Layer < ContextProviderId , MakeContextProviderE , MakeContextProviderR >
108103 }
109- execute ?: Effect <
110- RPCHandlerFactory < RequestContextMap , MiddlewareR > ,
111- never ,
112- MakeMiddlewareR
113- >
114- // better DX because types are contextually provided
115- executeContextual ?: (
104+ execute : (
116105 maker : ( cb : RPCHandlerFactory < RequestContextMap , MiddlewareR > ) => RPCHandlerFactory < RequestContextMap , MiddlewareR >
117106 ) => Effect <
118107 RPCHandlerFactory < RequestContextMap , MiddlewareR > ,
@@ -121,9 +110,13 @@ export interface Middleware<
121110 >
122111}
123112
113+ export type RequestContextMapErrors < RequestContextMap extends Record < string , RPCContextMap . Any > > = S . Schema . Type <
114+ RequestContextMap [ keyof RequestContextMap ] [ "error" ]
115+ >
116+
124117// identity factory for Middleware
125118export const makeMiddleware =
126- // by setting MiddlewareR and RequestContextMap beforehand, executeContextual contextual typing does not fuck up itself to anys
119+ // by setting MiddlewareR and RequestContextMap beforehand, execute contextual typing does not fuck up itself to anys
127120 < RequestContextMap extends Record < string , RPCContextMap . Any > , MiddlewareR > ( ) =>
128121 < M extends Middleware < MiddlewareR , RequestContextMap , any , NonEmptyArray < Layer . Layer . Any > , any , any , any , any , any > > (
129122 content : M
@@ -154,7 +147,7 @@ function makeRpcEffect<RequestContextMap extends Record<string, RPCContextMap.An
154147 headers : any
155148 ) => Effect . Effect <
156149 Request . Request . Success < Req > ,
157- Request . Request . Error < Req > ,
150+ Request . Request . Error < Req > | RequestContextMapErrors < RequestContextMap > ,
158151 | Scope . Scope // the context provider may require a Scope to run
159152 | Exclude < MiddlewareR , ContextProviderA > // for sure ContextProviderA is provided, so it can be removed from the MiddlewareR
160153 | Exclude <
0 commit comments