Skip to content

Commit 96c51df

Browse files
committed
improve router
1 parent 53b28ec commit 96c51df

4 files changed

Lines changed: 20 additions & 17 deletions

File tree

.changeset/cyan-facts-look.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@effect-app/infra": minor
3+
---
4+
5+
replace execute with executeContextual

.changeset/fair-webs-send.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@effect-app/infra": patch
3+
---
4+
5+
add support for error

packages/infra/src/api/routing/DynamicMiddleware.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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
125118
export 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<

packages/infra/test/controller.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const Str2 = Context.GenericTag<"str2", "str">("str2")
4747
const middleware = makeMiddleware<RequestContextMap, HttpServerRequest.HttpServerRequest>()({
4848
dependencies: [Layer.effect(Str2, Str)],
4949
contextProvider: ContextMaker,
50-
executeContextual: (maker) =>
50+
execute: (maker) =>
5151
Effect.gen(function*() {
5252
return maker((_schema, handler, moduleName) => (req, headers) => {
5353
return Effect

0 commit comments

Comments
 (0)