@effect-app/infra 2.71.0 → 2.72.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/dist/api/routing/DynamicMiddleware.d.ts +4 -4
- package/dist/api/routing/DynamicMiddleware.d.ts.map +1 -1
- package/dist/api/routing/DynamicMiddleware.js +3 -8
- package/package.json +1 -1
- package/src/api/routing/DynamicMiddleware.ts +9 -16
- package/test/controller.test.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -5,7 +5,7 @@ import { type LayersUtils } from "../routing.js";
|
|
|
5
5
|
export declare const MiddlewareMaker: Context.Tag<"MiddlewareMaker", any>;
|
|
6
6
|
export type RPCHandlerFactory<RequestContextMap extends Record<string, RPCContextMap.Any>, MiddlewareR> = <T extends {
|
|
7
7
|
config?: Partial<Record<keyof RequestContextMap, any>>;
|
|
8
|
-
}, Req extends S.TaggedRequest.All, HandlerR>(schema: T & S.Schema<Req, any, never>, handler: (request: Req, headers: any) => Effect.Effect<EffectRequest.Request.Success<Req>, EffectRequest.Request.Error<Req>, HandlerR>, moduleName?: string) => (req: Req, headers: any) => Effect.Effect<Request.Request.Success<Req>, Request.Request.Error<Req>, MiddlewareR | Exclude<HandlerR, GetEffectContext<RequestContextMap, (T & S.Schema<Req, any, never>)["config"]>>>;
|
|
8
|
+
}, Req extends S.TaggedRequest.All, HandlerR>(schema: T & S.Schema<Req, any, never>, handler: (request: Req, headers: any) => Effect.Effect<EffectRequest.Request.Success<Req>, EffectRequest.Request.Error<Req>, HandlerR>, moduleName?: string) => (req: Req, headers: any) => Effect.Effect<Request.Request.Success<Req>, Request.Request.Error<Req> | RequestContextMapErrors<RequestContextMap>, MiddlewareR | Exclude<HandlerR, GetEffectContext<RequestContextMap, (T & S.Schema<Req, any, never>)["config"]>>>;
|
|
9
9
|
export declare const makeMiddlewareLayer: <RequestContextMap extends Record<string, RPCContextMap.Any>, MiddlewareR, MakeMiddlewareR, MiddlewareDependencies extends NonEmptyArray<Layer.Layer.Any>, ContextProviderId, ContextProviderKey extends string, ContextProviderA, MakeContextProviderE, MakeContextProviderR>(middleware: Middleware<MiddlewareR, RequestContextMap, MakeMiddlewareR, MiddlewareDependencies, ContextProviderId, ContextProviderKey, ContextProviderA, MakeContextProviderE, MakeContextProviderR>) => Layer.Layer<"MiddlewareMaker", never, Exclude<MiddlewareR, LayersUtils.GetLayersSuccess<MiddlewareDependencies>>>;
|
|
10
10
|
export type ContextProviderShape<ContextProviderA> = Effect<Context.Context<ContextProviderA>, never, Scope>;
|
|
11
11
|
export interface Middleware<MiddlewareR, // what the middlware requires to execute
|
|
@@ -23,13 +23,13 @@ MakeContextProviderR> {
|
|
|
23
23
|
}> & {
|
|
24
24
|
Default: Layer.Layer<ContextProviderId, MakeContextProviderE, MakeContextProviderR>;
|
|
25
25
|
};
|
|
26
|
-
execute
|
|
27
|
-
executeContextual?: (maker: (cb: RPCHandlerFactory<RequestContextMap, MiddlewareR>) => RPCHandlerFactory<RequestContextMap, MiddlewareR>) => Effect<RPCHandlerFactory<RequestContextMap, MiddlewareR>, never, MakeMiddlewareR>;
|
|
26
|
+
execute: (maker: (cb: RPCHandlerFactory<RequestContextMap, MiddlewareR>) => RPCHandlerFactory<RequestContextMap, MiddlewareR>) => Effect<RPCHandlerFactory<RequestContextMap, MiddlewareR>, never, MakeMiddlewareR>;
|
|
28
27
|
}
|
|
28
|
+
export type RequestContextMapErrors<RequestContextMap extends Record<string, RPCContextMap.Any>> = S.Schema.Type<RequestContextMap[keyof RequestContextMap]["error"]>;
|
|
29
29
|
export declare const makeMiddleware: <RequestContextMap extends Record<string, RPCContextMap.Any>, MiddlewareR>() => <M extends Middleware<MiddlewareR, RequestContextMap, any, NonEmptyArray<Layer.Layer.Any>, any, any, any, any, any>>(content: M) => M;
|
|
30
30
|
export declare const makeRpc: <MiddlewareR, RequestContextMap extends Record<string, RPCContextMap.Any>, MakeMiddlewareR, MiddlewareDependencies extends NonEmptyArray<Layer.Layer.Any>, ContextProviderId, ContextProviderKey extends string, ContextProviderA, MakeContextProviderE, MakeContextProviderR>(middleware: Middleware<MiddlewareR, RequestContextMap, MakeMiddlewareR, MiddlewareDependencies, ContextProviderId, ContextProviderKey, ContextProviderA, MakeContextProviderE, MakeContextProviderR>) => Effect.Effect<{
|
|
31
31
|
effect: <T extends {
|
|
32
32
|
config?: Partial<Record<keyof RequestContextMap, any>>;
|
|
33
|
-
}, Req extends S.TaggedRequest.All, HandlerR>(schema: T & S.Schema<Req, any, never>, handler: (request: Req, headers: any) => Effect.Effect<Request.Request.Success<Req>, Request.Request.Error<Req>, HandlerR>, moduleName?: string) => (req: Req, headers: any) => Effect.Effect<Request.Request.Success<Req>, Request.Request.Error<Req>, Scope.Scope | Exclude<MiddlewareR, ContextProviderA> | Exclude<Exclude<HandlerR, GetEffectContext<RequestContextMap, (T & S.Schema<Req, any, never>)["config"]>>, ContextProviderA>>;
|
|
33
|
+
}, Req extends S.TaggedRequest.All, HandlerR>(schema: T & S.Schema<Req, any, never>, handler: (request: Req, headers: any) => Effect.Effect<Request.Request.Success<Req>, Request.Request.Error<Req>, HandlerR>, moduleName?: string) => (req: Req, headers: any) => Effect.Effect<Request.Request.Success<Req>, Request.Request.Error<Req> | S.Schema.Type<RequestContextMap[keyof RequestContextMap]["error"]>, Scope.Scope | Exclude<MiddlewareR, ContextProviderA> | Exclude<Exclude<HandlerR, GetEffectContext<RequestContextMap, (T & S.Schema<Req, any, never>)["config"]>>, ContextProviderA>>;
|
|
34
34
|
}, never, "MiddlewareMaker" | ContextProviderId>;
|
|
35
35
|
//# sourceMappingURL=DynamicMiddleware.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DynamicMiddleware.d.ts","sourceRoot":"","sources":["../../../src/api/routing/DynamicMiddleware.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,aAAa,EAAE,KAAK,OA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AAiB,EACjB,kBAAkB,SAAS,MAAM,EACjC,gBAAgB,EAChB,oBAAoB,EACpB,oBAAoB,EAEpB,YAAY,UAAU,CACpB,WAAW,EACX,iBAAiB,EACjB,eAAe,EACf,sBAAsB,EACtB,iBAAiB,EACjB,kBAAkB,EAClB,gBAAgB,EAChB,oBAAoB,EACpB,oBAAoB,CACrB;aArDG,CAAC;;OAGD,GAAG,SAAS,CAAC,CAAC,aAAa,CAAC,GAAG,EAC/B,QAAQ,0EAKG,GAAG,qGAMD,MAAM,yBAGV,GAAG;gDA6DV,CAAA"}
|
|
@@ -7,19 +7,14 @@ function makeRpcHandler() {
|
|
|
7
7
|
return (cb) => cb;
|
|
8
8
|
}
|
|
9
9
|
export const makeMiddlewareLayer = (middleware) => {
|
|
10
|
-
if (!middleware.execute && !middleware.executeContextual) {
|
|
11
|
-
throw new Error("No execute or executeContextual provided in middleware");
|
|
12
|
-
}
|
|
13
10
|
const middlewareLayer = Layer
|
|
14
|
-
.effect(MiddlewareMaker, middleware.execute
|
|
15
|
-
? middleware.execute
|
|
16
|
-
: middleware.executeContextual(makeRpcHandler()))
|
|
11
|
+
.effect(MiddlewareMaker, middleware.execute(makeRpcHandler()))
|
|
17
12
|
.pipe(middleware.dependencies ? Layer.provide(middleware.dependencies) : (_) => _);
|
|
18
13
|
return middlewareLayer;
|
|
19
14
|
};
|
|
20
15
|
// identity factory for Middleware
|
|
21
16
|
export const makeMiddleware =
|
|
22
|
-
// by setting MiddlewareR and RequestContextMap beforehand,
|
|
17
|
+
// by setting MiddlewareR and RequestContextMap beforehand, execute contextual typing does not fuck up itself to anys
|
|
23
18
|
() => (content) => content;
|
|
24
19
|
// it just provides the right types without cluttering the implementation with them
|
|
25
20
|
function makeRpcEffect() {
|
|
@@ -41,4 +36,4 @@ export const makeRpc = (middleware) => Effect
|
|
|
41
36
|
Effect.uninterruptible)));
|
|
42
37
|
})
|
|
43
38
|
})));
|
|
44
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
39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BQUE7SUFDTCxDQUFDLENBQUM7Q0FDSCxDQUFDLENBQUMsQ0FBQyxDQUFBIn0=
|
package/package.json
CHANGED
|
@@ -30,7 +30,7 @@ export type RPCHandlerFactory<RequestContextMap extends Record<string, RPCContex
|
|
|
30
30
|
headers: any
|
|
31
31
|
) => Effect.Effect<
|
|
32
32
|
Request.Request.Success<Req>,
|
|
33
|
-
Request.Request.Error<Req>,
|
|
33
|
+
Request.Request.Error<Req> | RequestContextMapErrors<RequestContextMap>,
|
|
34
34
|
// the middleware will remove from HandlerR the dynamic context, but will also add the MiddlewareR
|
|
35
35
|
| MiddlewareR
|
|
36
36
|
// & S.Schema<Req, any, never> is useless here but useful when creating the middleware
|
|
@@ -64,15 +64,10 @@ export const makeMiddlewareLayer = <
|
|
|
64
64
|
MakeContextProviderR
|
|
65
65
|
>
|
|
66
66
|
) => {
|
|
67
|
-
if (!middleware.execute && !middleware.executeContextual) {
|
|
68
|
-
throw new Error("No execute or executeContextual provided in middleware")
|
|
69
|
-
}
|
|
70
67
|
const middlewareLayer = Layer
|
|
71
68
|
.effect(
|
|
72
69
|
MiddlewareMaker,
|
|
73
|
-
middleware.execute
|
|
74
|
-
? middleware.execute
|
|
75
|
-
: middleware.executeContextual!(makeRpcHandler<RequestContextMap, MiddlewareR>())
|
|
70
|
+
middleware.execute!(makeRpcHandler<RequestContextMap, MiddlewareR>())
|
|
76
71
|
)
|
|
77
72
|
.pipe(middleware.dependencies ? Layer.provide(middleware.dependencies) as any : (_) => _)
|
|
78
73
|
|
|
@@ -106,13 +101,7 @@ export interface Middleware<
|
|
|
106
101
|
& {
|
|
107
102
|
Default: Layer.Layer<ContextProviderId, MakeContextProviderE, MakeContextProviderR>
|
|
108
103
|
}
|
|
109
|
-
execute
|
|
110
|
-
RPCHandlerFactory<RequestContextMap, MiddlewareR>,
|
|
111
|
-
never,
|
|
112
|
-
MakeMiddlewareR
|
|
113
|
-
>
|
|
114
|
-
// better DX because types are contextually provided
|
|
115
|
-
executeContextual?: (
|
|
104
|
+
execute: (
|
|
116
105
|
maker: (cb: RPCHandlerFactory<RequestContextMap, MiddlewareR>) => RPCHandlerFactory<RequestContextMap, MiddlewareR>
|
|
117
106
|
) => Effect<
|
|
118
107
|
RPCHandlerFactory<RequestContextMap, MiddlewareR>,
|
|
@@ -121,9 +110,13 @@ export interface Middleware<
|
|
|
121
110
|
>
|
|
122
111
|
}
|
|
123
112
|
|
|
113
|
+
export type RequestContextMapErrors<RequestContextMap extends Record<string, RPCContextMap.Any>> = S.Schema.Type<
|
|
114
|
+
RequestContextMap[keyof RequestContextMap]["error"]
|
|
115
|
+
>
|
|
116
|
+
|
|
124
117
|
// identity factory for Middleware
|
|
125
118
|
export const makeMiddleware =
|
|
126
|
-
// by setting MiddlewareR and RequestContextMap beforehand,
|
|
119
|
+
// by setting MiddlewareR and RequestContextMap beforehand, execute contextual typing does not fuck up itself to anys
|
|
127
120
|
<RequestContextMap extends Record<string, RPCContextMap.Any>, MiddlewareR>() =>
|
|
128
121
|
<M extends Middleware<MiddlewareR, RequestContextMap, any, NonEmptyArray<Layer.Layer.Any>, any, any, any, any, any>>(
|
|
129
122
|
content: M
|
|
@@ -154,7 +147,7 @@ function makeRpcEffect<RequestContextMap extends Record<string, RPCContextMap.An
|
|
|
154
147
|
headers: any
|
|
155
148
|
) => Effect.Effect<
|
|
156
149
|
Request.Request.Success<Req>,
|
|
157
|
-
Request.Request.Error<Req>,
|
|
150
|
+
Request.Request.Error<Req> | RequestContextMapErrors<RequestContextMap>,
|
|
158
151
|
| Scope.Scope // the context provider may require a Scope to run
|
|
159
152
|
| Exclude<MiddlewareR, ContextProviderA> // for sure ContextProviderA is provided, so it can be removed from the MiddlewareR
|
|
160
153
|
| Exclude<
|
package/test/controller.test.ts
CHANGED
|
@@ -47,7 +47,7 @@ const Str2 = Context.GenericTag<"str2", "str">("str2")
|
|
|
47
47
|
const middleware = makeMiddleware<RequestContextMap, HttpServerRequest.HttpServerRequest>()({
|
|
48
48
|
dependencies: [Layer.effect(Str2, Str)],
|
|
49
49
|
contextProvider: ContextMaker,
|
|
50
|
-
|
|
50
|
+
execute: (maker) =>
|
|
51
51
|
Effect.gen(function*() {
|
|
52
52
|
return maker((_schema, handler, moduleName) => (req, headers) => {
|
|
53
53
|
return Effect
|