@effect-app/infra 2.74.0 → 2.76.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 +17 -0
- package/dist/api/layerUtils.d.ts +22 -0
- package/dist/api/layerUtils.d.ts.map +1 -0
- package/dist/api/layerUtils.js +2 -0
- package/dist/api/routing/middleware/ContextProvider.d.ts +41 -0
- package/dist/api/routing/middleware/ContextProvider.d.ts.map +1 -0
- package/dist/api/routing/middleware/ContextProvider.js +27 -0
- package/dist/api/routing/middleware/DynamicMiddleware.d.ts +61 -0
- package/dist/api/routing/middleware/DynamicMiddleware.d.ts.map +1 -0
- package/dist/api/routing/middleware/DynamicMiddleware.js +45 -0
- package/dist/api/routing/middleware/dynamic-middleware.d.ts +25 -0
- package/dist/api/routing/middleware/dynamic-middleware.d.ts.map +1 -0
- package/dist/api/routing/middleware/dynamic-middleware.js +39 -0
- package/dist/api/routing/middleware/generic-middleware.d.ts +9 -0
- package/dist/api/routing/middleware/generic-middleware.d.ts.map +1 -0
- package/dist/api/routing/middleware/generic-middleware.js +20 -0
- package/dist/api/routing/middleware/middleware.d.ts +28 -0
- package/dist/api/routing/middleware/middleware.d.ts.map +1 -0
- package/dist/api/routing/middleware/middleware.js +101 -0
- package/dist/api/routing/middleware.d.ts +6 -0
- package/dist/api/routing/middleware.d.ts.map +1 -0
- package/dist/api/routing/middleware.js +8 -0
- package/dist/api/routing.d.ts +18 -28
- package/dist/api/routing.d.ts.map +1 -1
- package/dist/api/routing.js +3 -3
- package/package.json +27 -7
- package/src/api/layerUtils.ts +33 -0
- package/src/api/routing/middleware/ContextProvider.ts +136 -0
- package/src/api/routing/middleware/DynamicMiddleware.ts +317 -0
- package/src/api/routing/{dynamic-middleware.ts → middleware/dynamic-middleware.ts} +29 -63
- package/src/api/routing/middleware/generic-middleware.ts +38 -0
- package/src/api/routing/middleware/middleware.ts +134 -0
- package/src/api/routing/middleware.ts +7 -0
- package/src/api/routing.ts +37 -56
- package/test/controller.test.ts +132 -15
- package/test/dist/controller.test.d.ts.map +1 -1
- package/dist/api/routing/DynamicMiddleware.d.ts +0 -104
- package/dist/api/routing/DynamicMiddleware.d.ts.map +0 -1
- package/dist/api/routing/DynamicMiddleware.js +0 -122
- package/dist/api/routing/dynamic-middleware.d.ts +0 -24
- package/dist/api/routing/dynamic-middleware.d.ts.map +0 -1
- package/dist/api/routing/dynamic-middleware.js +0 -39
- package/src/api/routing/DynamicMiddleware.ts +0 -527
|
@@ -1,527 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
3
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
4
|
-
import { Array, Cause, Context, Effect, Layer, type NonEmptyArray, ParseResult, pipe, type Request, type S, type Scope } from "effect-app"
|
|
5
|
-
import type { GetEffectContext, RPCContextMap } from "effect-app/client/req"
|
|
6
|
-
import { HttpHeaders, type HttpRouter, HttpServerRequest } from "effect-app/http"
|
|
7
|
-
|
|
8
|
-
import { pretty } from "effect-app/utils"
|
|
9
|
-
import type * as EffectRequest from "effect/Request"
|
|
10
|
-
import { logError, reportError } from "../../errorReporter.js"
|
|
11
|
-
import { InfraLogger } from "../../logger.js"
|
|
12
|
-
import { type LayersUtils } from "../routing.js"
|
|
13
|
-
import { type AnyContextWithLayer, implementMiddleware, mergeContexts } from "./dynamic-middleware.js"
|
|
14
|
-
|
|
15
|
-
// utils:
|
|
16
|
-
//
|
|
17
|
-
type GetContext<T> = T extends Context.Context<infer Y> ? Y : never
|
|
18
|
-
|
|
19
|
-
// module:
|
|
20
|
-
//
|
|
21
|
-
export type MakeRPCHandlerFactory<
|
|
22
|
-
RequestContextMap extends Record<string, RPCContextMap.Any>,
|
|
23
|
-
MiddlewareR
|
|
24
|
-
> = <
|
|
25
|
-
T extends {
|
|
26
|
-
config?: Partial<Record<keyof RequestContextMap, any>>
|
|
27
|
-
},
|
|
28
|
-
Req extends S.TaggedRequest.All,
|
|
29
|
-
HandlerR
|
|
30
|
-
>(
|
|
31
|
-
schema: T & S.Schema<Req, any, never>,
|
|
32
|
-
handler: (
|
|
33
|
-
request: Req,
|
|
34
|
-
headers: any
|
|
35
|
-
) => Effect.Effect<
|
|
36
|
-
EffectRequest.Request.Success<Req>,
|
|
37
|
-
EffectRequest.Request.Error<Req>,
|
|
38
|
-
// dynamic middlewares removes the dynamic context from HandlerR
|
|
39
|
-
Exclude<HandlerR, GetEffectContext<RequestContextMap, (T & S.Schema<Req, any, never>)["config"]>>
|
|
40
|
-
>,
|
|
41
|
-
moduleName: string
|
|
42
|
-
) => (
|
|
43
|
-
req: Req,
|
|
44
|
-
headers: any
|
|
45
|
-
) => Effect.Effect<
|
|
46
|
-
Request.Request.Success<Req>,
|
|
47
|
-
Request.Request.Error<Req> | RequestContextMapErrors<RequestContextMap>,
|
|
48
|
-
// the middleware will remove from HandlerR the dynamic context, but will also add some requirements
|
|
49
|
-
| MiddlewareR
|
|
50
|
-
// & S.Schema<Req, any, never> is useless here but useful when creating the middleware
|
|
51
|
-
| Exclude<HandlerR, GetEffectContext<RequestContextMap, (T & S.Schema<Req, any, never>)["config"]>>
|
|
52
|
-
>
|
|
53
|
-
|
|
54
|
-
export type RPCHandlerFactory<
|
|
55
|
-
RequestContextMap extends Record<string, RPCContextMap.Any>,
|
|
56
|
-
ContextProviderA
|
|
57
|
-
> = <
|
|
58
|
-
T extends {
|
|
59
|
-
config?: Partial<Record<keyof RequestContextMap, any>>
|
|
60
|
-
},
|
|
61
|
-
Req extends S.TaggedRequest.All,
|
|
62
|
-
HandlerR
|
|
63
|
-
>(
|
|
64
|
-
schema: T & S.Schema<Req, any, never>,
|
|
65
|
-
handler: (
|
|
66
|
-
request: Req,
|
|
67
|
-
headers: any
|
|
68
|
-
) => Effect.Effect<
|
|
69
|
-
EffectRequest.Request.Success<Req>,
|
|
70
|
-
EffectRequest.Request.Error<Req>,
|
|
71
|
-
HandlerR
|
|
72
|
-
>,
|
|
73
|
-
moduleName: string
|
|
74
|
-
) => (
|
|
75
|
-
req: Req,
|
|
76
|
-
headers: any
|
|
77
|
-
) => Effect.Effect<
|
|
78
|
-
Request.Request.Success<Req>,
|
|
79
|
-
Request.Request.Error<Req> | RequestContextMapErrors<RequestContextMap>,
|
|
80
|
-
| HttpRouter.HttpRouter.Provided // because of the context provider and the middleware (Middleware)
|
|
81
|
-
| Exclude<
|
|
82
|
-
// the middleware will remove from HandlerR the dynamic context
|
|
83
|
-
// & S.Schema<Req, any, never> is useless here but useful when creating the middleware
|
|
84
|
-
Exclude<HandlerR, GetEffectContext<RequestContextMap, (T & S.Schema<Req, any, never>)["config"]>>,
|
|
85
|
-
// the context provider provides additional stuff both to the middleware and the handler
|
|
86
|
-
ContextProviderA
|
|
87
|
-
>
|
|
88
|
-
>
|
|
89
|
-
|
|
90
|
-
// the context provider provides additional stuff
|
|
91
|
-
export type ContextProviderShape<ContextProviderA, ContextProviderR extends HttpRouter.HttpRouter.Provided> = Effect<
|
|
92
|
-
Context.Context<ContextProviderA>,
|
|
93
|
-
never, // no errors are allowed
|
|
94
|
-
ContextProviderR
|
|
95
|
-
>
|
|
96
|
-
|
|
97
|
-
export interface ContextProviderId {
|
|
98
|
-
_tag: "ContextProvider"
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
type RequestContextMapProvider<RequestContextMap extends Record<string, RPCContextMap.Any>> = {
|
|
102
|
-
[K in keyof RequestContextMap]: AnyContextWithLayer<
|
|
103
|
-
{ [K in keyof RequestContextMap]?: RequestContextMap[K]["contextActivation"] },
|
|
104
|
-
RequestContextMap[K]["service"],
|
|
105
|
-
S.Schema.Type<RequestContextMap[K]["error"]>
|
|
106
|
-
>
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export interface MiddlewareMake<
|
|
110
|
-
RequestContextMap extends Record<string, RPCContextMap.Any>, // what services will the middleware provide dynamically to the handler, or raise errors.
|
|
111
|
-
MakeMiddlewareE, // what the middleware construction can fail with
|
|
112
|
-
MakeMiddlewareR, // what the middleware requires to be constructed
|
|
113
|
-
MiddlewareDependencies extends NonEmptyArray<Layer.Layer.Any>, // layers provided for the middleware to be constructed
|
|
114
|
-
//
|
|
115
|
-
// ContextProvider is a service that builds additional context for each request.
|
|
116
|
-
ContextProviderA, // what the context provider provides
|
|
117
|
-
ContextProviderR extends HttpRouter.HttpRouter.Provided, // what the context provider requires
|
|
118
|
-
MakeContextProviderE, // what the context provider construction can fail with
|
|
119
|
-
MakeContextProviderR, // what the context provider construction requires
|
|
120
|
-
TI extends RequestContextMapProvider<RequestContextMap> // how to resolve the dynamic middleware
|
|
121
|
-
> {
|
|
122
|
-
dependencies?: MiddlewareDependencies
|
|
123
|
-
dynamicMiddlewares: TI
|
|
124
|
-
contextProvider:
|
|
125
|
-
& Context.Tag<
|
|
126
|
-
ContextProviderId,
|
|
127
|
-
ContextProviderShape<ContextProviderA, ContextProviderR>
|
|
128
|
-
>
|
|
129
|
-
& {
|
|
130
|
-
Default: Layer.Layer<ContextProviderId, MakeContextProviderE, MakeContextProviderR>
|
|
131
|
-
}
|
|
132
|
-
// this actually builds "the middleware", i.e. returns the augmented handler factory when yielded...
|
|
133
|
-
execute: (
|
|
134
|
-
maker: (
|
|
135
|
-
// MiddlewareR is set to ContextProviderA | HttpRouter.HttpRouter.Provided because that's what, at most
|
|
136
|
-
// a middleware can additionally require to get executed
|
|
137
|
-
cb: MakeRPCHandlerFactory<RequestContextMap, ContextProviderA | HttpRouter.HttpRouter.Provided>
|
|
138
|
-
) => MakeRPCHandlerFactory<RequestContextMap, ContextProviderA | HttpRouter.HttpRouter.Provided>
|
|
139
|
-
) => Effect<
|
|
140
|
-
MakeRPCHandlerFactory<RequestContextMap, ContextProviderA | HttpRouter.HttpRouter.Provided>,
|
|
141
|
-
MakeMiddlewareE,
|
|
142
|
-
MakeMiddlewareR | Scope // ...that's why MakeMiddlewareR is here
|
|
143
|
-
>
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// Note: the type here must be aligned with MergedContextProvider
|
|
147
|
-
export const mergeContextProviders = <
|
|
148
|
-
// TDeps is an array of services whit Default implementation
|
|
149
|
-
// each service is an effect which builds some context for each request
|
|
150
|
-
TDeps extends Array.NonEmptyReadonlyArray<
|
|
151
|
-
& (
|
|
152
|
-
// E = never => the context provided cannot trigger errors
|
|
153
|
-
// can't put HttpRouter.HttpRouter.Provided as R here because of variance
|
|
154
|
-
// (TDeps is an input type parameter so it's contravariant therefore Effect's R becomes contravariant too)
|
|
155
|
-
| Context.Tag<any, Effect<Context.Context<any>, never, any> & { _tag: any }>
|
|
156
|
-
| Context.Tag<any, Effect<Context.Context<any>, never, never> & { _tag: any }>
|
|
157
|
-
)
|
|
158
|
-
& {
|
|
159
|
-
new(...args: any[]): any
|
|
160
|
-
Default: Layer.Layer<Effect<Context.Context<any>> & { _tag: any }, any, any>
|
|
161
|
-
}
|
|
162
|
-
>
|
|
163
|
-
>(
|
|
164
|
-
...deps: {
|
|
165
|
-
[K in keyof TDeps]: TDeps[K]["Service"] extends Effect<Context.Context<any>, never, HttpRouter.HttpRouter.Provided>
|
|
166
|
-
? TDeps[K]
|
|
167
|
-
: `HttpRouter.HttpRouter.Provided are the only requirements ${TDeps[K]["Service"][
|
|
168
|
-
"_tag"
|
|
169
|
-
]}'s returned effect can have`
|
|
170
|
-
}
|
|
171
|
-
): {
|
|
172
|
-
dependencies: { [K in keyof TDeps]: TDeps[K]["Default"] }
|
|
173
|
-
effect: Effect.Effect<
|
|
174
|
-
Effect.Effect<
|
|
175
|
-
Context.Context<GetContext<Effect.Success<InstanceType<TDeps[number]>>>>,
|
|
176
|
-
never,
|
|
177
|
-
Effect.Context<InstanceType<TDeps[number]>>
|
|
178
|
-
>,
|
|
179
|
-
LayersUtils.GetLayersError<{ [K in keyof TDeps]: TDeps[K]["Default"] }>,
|
|
180
|
-
LayersUtils.GetLayersSuccess<{ [K in keyof TDeps]: TDeps[K]["Default"] }>
|
|
181
|
-
>
|
|
182
|
-
} => ({
|
|
183
|
-
dependencies: deps.map((_) => _.Default) as any,
|
|
184
|
-
effect: Effect.gen(function*() {
|
|
185
|
-
const makers = yield* Effect.all(deps)
|
|
186
|
-
return Effect
|
|
187
|
-
.gen(function*() {
|
|
188
|
-
const services = (makers as any[]).map((handle, i) => ({ maker: deps[i], handle }))
|
|
189
|
-
// services are effects which return some Context.Context<...>
|
|
190
|
-
const context = yield* mergeContexts(services as any)
|
|
191
|
-
return context
|
|
192
|
-
})
|
|
193
|
-
}) as any
|
|
194
|
-
})
|
|
195
|
-
|
|
196
|
-
export interface MiddlewareMakerId {
|
|
197
|
-
_tag: "MiddlewareMaker"
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
export const ContextProvider = <
|
|
201
|
-
ContextProviderA,
|
|
202
|
-
MakeContextProviderE,
|
|
203
|
-
MakeContextProviderR,
|
|
204
|
-
ContextProviderR extends HttpRouter.HttpRouter.Provided,
|
|
205
|
-
Dependencies extends NonEmptyArray<Layer.Layer.Any>
|
|
206
|
-
>(
|
|
207
|
-
input: {
|
|
208
|
-
effect: Effect<
|
|
209
|
-
Effect<ContextProviderA, never, ContextProviderR>,
|
|
210
|
-
MakeContextProviderE,
|
|
211
|
-
MakeContextProviderR | Scope
|
|
212
|
-
>
|
|
213
|
-
dependencies?: Dependencies
|
|
214
|
-
}
|
|
215
|
-
) => {
|
|
216
|
-
const ctx = Context.GenericTag<
|
|
217
|
-
ContextProviderId,
|
|
218
|
-
Effect<ContextProviderA, never, ContextProviderR>
|
|
219
|
-
>(
|
|
220
|
-
"ContextProvider"
|
|
221
|
-
)
|
|
222
|
-
const l = Layer.scoped(ctx, input.effect)
|
|
223
|
-
return Object.assign(ctx, {
|
|
224
|
-
Default: l.pipe(
|
|
225
|
-
input.dependencies ? Layer.provide(input.dependencies) as any : (_) => _
|
|
226
|
-
) as Layer.Layer<
|
|
227
|
-
ContextProviderId,
|
|
228
|
-
| MakeContextProviderE
|
|
229
|
-
| LayersUtils.GetLayersError<Dependencies>,
|
|
230
|
-
| Exclude<MakeContextProviderR, LayersUtils.GetLayersSuccess<Dependencies>>
|
|
231
|
-
| LayersUtils.GetLayersContext<Dependencies>
|
|
232
|
-
>
|
|
233
|
-
})
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// Note: the type here must be aligned with mergeContextProviders
|
|
237
|
-
export const MergedContextProvider = <
|
|
238
|
-
// TDeps is an array of services whit Default implementation
|
|
239
|
-
// each service is an effect which builds some context for each request
|
|
240
|
-
TDeps extends Array.NonEmptyReadonlyArray<
|
|
241
|
-
& (
|
|
242
|
-
// E = never => the context provided cannot trigger errors
|
|
243
|
-
// can't put HttpRouter.HttpRouter.Provided as R here because of variance
|
|
244
|
-
// (TDeps is an input type parameter so it's contravariant therefore Effect's R becomes contravariant too)
|
|
245
|
-
| Context.Tag<any, Effect<Context.Context<any>, never, any> & { _tag: any }>
|
|
246
|
-
| Context.Tag<any, Effect<Context.Context<any>, never, never> & { _tag: any }>
|
|
247
|
-
)
|
|
248
|
-
& {
|
|
249
|
-
new(...args: any[]): any
|
|
250
|
-
Default: Layer.Layer<Effect<Context.Context<any>> & { _tag: any }, any, any>
|
|
251
|
-
}
|
|
252
|
-
>
|
|
253
|
-
>(
|
|
254
|
-
...deps: {
|
|
255
|
-
[K in keyof TDeps]: TDeps[K]["Service"] extends Effect<Context.Context<any>, never, HttpRouter.HttpRouter.Provided>
|
|
256
|
-
? TDeps[K]
|
|
257
|
-
: `HttpRouter.HttpRouter.Provided are the only requirements ${TDeps[K]["Service"][
|
|
258
|
-
"_tag"
|
|
259
|
-
]}'s returned effect can have`
|
|
260
|
-
}
|
|
261
|
-
) =>
|
|
262
|
-
pipe(
|
|
263
|
-
deps as [Parameters<typeof mergeContextProviders>[0]],
|
|
264
|
-
(_) => mergeContextProviders(..._),
|
|
265
|
-
(_) => ContextProvider(_ as any)
|
|
266
|
-
) as unknown as
|
|
267
|
-
& Context.Tag<
|
|
268
|
-
ContextProviderId,
|
|
269
|
-
Effect.Effect<
|
|
270
|
-
Context.Context<GetContext<Effect.Success<InstanceType<TDeps[number]>>>>,
|
|
271
|
-
never,
|
|
272
|
-
Effect.Context<InstanceType<TDeps[number]>>
|
|
273
|
-
>
|
|
274
|
-
>
|
|
275
|
-
& {
|
|
276
|
-
Default: Layer.Layer<
|
|
277
|
-
ContextProviderId,
|
|
278
|
-
LayersUtils.GetLayersError<{ [K in keyof TDeps]: TDeps[K]["Default"] }>,
|
|
279
|
-
| Exclude<
|
|
280
|
-
InstanceType<TDeps[number]>,
|
|
281
|
-
LayersUtils.GetLayersSuccess<{ [K in keyof TDeps]: TDeps[K]["Default"] }>
|
|
282
|
-
>
|
|
283
|
-
| LayersUtils.GetLayersContext<{ [K in keyof TDeps]: TDeps[K]["Default"] }>
|
|
284
|
-
>
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
export const EmptyContextProvider = ContextProvider({ effect: Effect.succeed(Effect.succeed(Context.empty())) })
|
|
288
|
-
|
|
289
|
-
export type Middleware<
|
|
290
|
-
RequestContextMap extends Record<string, RPCContextMap.Any>, // what services will the middlware provide dynamically to the handler, or raise errors.
|
|
291
|
-
MakeMiddlewareE, // what the middleware construction can fail with
|
|
292
|
-
MakeMiddlewareR, // what the middlware requires to be constructed
|
|
293
|
-
ContextProviderA // what the context provider provides
|
|
294
|
-
> =
|
|
295
|
-
& Context.Tag<
|
|
296
|
-
MiddlewareMakerId,
|
|
297
|
-
MiddlewareMakerId & {
|
|
298
|
-
effect: RPCHandlerFactory<RequestContextMap, ContextProviderA>
|
|
299
|
-
}
|
|
300
|
-
>
|
|
301
|
-
& {
|
|
302
|
-
Default: Layer.Layer<
|
|
303
|
-
MiddlewareMakerId,
|
|
304
|
-
MakeMiddlewareE,
|
|
305
|
-
MakeMiddlewareR
|
|
306
|
-
>
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
export type RequestContextMapErrors<RequestContextMap extends Record<string, RPCContextMap.Any>> = S.Schema.Type<
|
|
310
|
-
RequestContextMap[keyof RequestContextMap]["error"]
|
|
311
|
-
>
|
|
312
|
-
|
|
313
|
-
const logRequestError = logError("Request")
|
|
314
|
-
const reportRequestError = reportError("Request")
|
|
315
|
-
|
|
316
|
-
export class DevMode extends Context.Reference<DevMode>()("DevMode", { defaultValue: () => false }) {}
|
|
317
|
-
|
|
318
|
-
// TODO: pull out to a generic middleware system..
|
|
319
|
-
export const requestMiddleware = <A, E, R>(
|
|
320
|
-
handle: (input: any, headers: HttpHeaders.Headers) => Effect.Effect<A, E, R>,
|
|
321
|
-
moduleName: string
|
|
322
|
-
) =>
|
|
323
|
-
Effect.fnUntraced(function*(input: any, rpcHeaders: HttpHeaders.Headers) {
|
|
324
|
-
const devMode = yield* DevMode
|
|
325
|
-
// merge in the request headers
|
|
326
|
-
// we should consider if we should merge them into rpc headers on the Protocol layer instead.
|
|
327
|
-
const httpReq = yield* HttpServerRequest.HttpServerRequest
|
|
328
|
-
const headers = HttpHeaders.merge(httpReq.headers, rpcHeaders)
|
|
329
|
-
|
|
330
|
-
return yield* Effect
|
|
331
|
-
.annotateCurrentSpan(
|
|
332
|
-
"requestInput",
|
|
333
|
-
Object.entries(input).reduce((prev, [key, value]: [string, unknown]) => {
|
|
334
|
-
prev[key] = key === "password"
|
|
335
|
-
? "<redacted>"
|
|
336
|
-
: typeof value === "string" || typeof value === "number" || typeof value === "boolean"
|
|
337
|
-
? typeof value === "string" && value.length > 256
|
|
338
|
-
? (value.substring(0, 253) + "...")
|
|
339
|
-
: value
|
|
340
|
-
: Array.isArray(value)
|
|
341
|
-
? `Array[${value.length}]`
|
|
342
|
-
: value === null || value === undefined
|
|
343
|
-
? `${value}`
|
|
344
|
-
: typeof value === "object" && value
|
|
345
|
-
? `Object[${Object.keys(value).length}]`
|
|
346
|
-
: typeof value
|
|
347
|
-
return prev
|
|
348
|
-
}, {} as Record<string, string | number | boolean>)
|
|
349
|
-
)
|
|
350
|
-
.pipe(
|
|
351
|
-
// can't use andThen due to some being a function and effect
|
|
352
|
-
Effect.zipRight(handle(input, headers)),
|
|
353
|
-
// TODO: support ParseResult if the error channel of the request allows it.. but who would want that?
|
|
354
|
-
Effect.catchAll((_) => ParseResult.isParseError(_) ? Effect.die(_) : Effect.fail(_)),
|
|
355
|
-
Effect.tapErrorCause((cause) => Cause.isFailure(cause) ? logRequestError(cause) : Effect.void),
|
|
356
|
-
Effect.tapDefect((cause) =>
|
|
357
|
-
Effect
|
|
358
|
-
.all([
|
|
359
|
-
reportRequestError(cause, {
|
|
360
|
-
action: `${moduleName}.${input._tag}`
|
|
361
|
-
}),
|
|
362
|
-
InfraLogger
|
|
363
|
-
.logError("Finished request", cause)
|
|
364
|
-
.pipe(Effect.annotateLogs({
|
|
365
|
-
action: `${moduleName}.${input._tag}`,
|
|
366
|
-
req: pretty(input),
|
|
367
|
-
headers: pretty(headers)
|
|
368
|
-
// resHeaders: pretty(
|
|
369
|
-
// Object
|
|
370
|
-
// .entries(headers)
|
|
371
|
-
// .reduce((prev, [key, value]) => {
|
|
372
|
-
// prev[key] = value && typeof value === "string" ? snipString(value) : value
|
|
373
|
-
// return prev
|
|
374
|
-
// }, {} as Record<string, any>)
|
|
375
|
-
// )
|
|
376
|
-
}))
|
|
377
|
-
])
|
|
378
|
-
),
|
|
379
|
-
devMode ? (_) => _ : Effect.catchAllDefect(() => Effect.die("Internal Server Error"))
|
|
380
|
-
)
|
|
381
|
-
})
|
|
382
|
-
|
|
383
|
-
// factory for middlewares
|
|
384
|
-
export const makeMiddleware =
|
|
385
|
-
// by setting RequestContextMap beforehand, execute contextual typing does not fuck up itself to anys
|
|
386
|
-
<
|
|
387
|
-
RequestContextMap extends Record<string, RPCContextMap.Any>
|
|
388
|
-
>() =>
|
|
389
|
-
<
|
|
390
|
-
MakeMiddlewareE, // what the middleware construction can fail with
|
|
391
|
-
MakeMiddlewareR, // what the middlware requires to be constructed
|
|
392
|
-
MiddlewareDependencies extends NonEmptyArray<Layer.Layer.Any>, // layers provided for the middlware to be constructed
|
|
393
|
-
//
|
|
394
|
-
// ContextProvider is a service that builds additional context for each request.
|
|
395
|
-
ContextProviderA, // what the context provider provides
|
|
396
|
-
ContextProviderR extends HttpRouter.HttpRouter.Provided, // what the context provider requires
|
|
397
|
-
MakeContextProviderE, // what the context provider construction can fail with
|
|
398
|
-
MakeContextProviderR, // what the context provider construction requires
|
|
399
|
-
TI extends RequestContextMapProvider<RequestContextMap> // how to resolve the dynamic middleware
|
|
400
|
-
>(
|
|
401
|
-
make: MiddlewareMake<
|
|
402
|
-
RequestContextMap,
|
|
403
|
-
MakeMiddlewareE,
|
|
404
|
-
MakeMiddlewareR,
|
|
405
|
-
MiddlewareDependencies,
|
|
406
|
-
ContextProviderA,
|
|
407
|
-
ContextProviderR,
|
|
408
|
-
MakeContextProviderE,
|
|
409
|
-
MakeContextProviderR,
|
|
410
|
-
TI
|
|
411
|
-
>
|
|
412
|
-
) => {
|
|
413
|
-
// type Id = MiddlewareMakerId &
|
|
414
|
-
const MiddlewareMaker = Context.GenericTag<
|
|
415
|
-
MiddlewareMakerId,
|
|
416
|
-
{
|
|
417
|
-
effect: RPCHandlerFactory<RequestContextMap, ContextProviderA>
|
|
418
|
-
_tag: "MiddlewareMaker"
|
|
419
|
-
}
|
|
420
|
-
>(
|
|
421
|
-
"MiddlewareMaker"
|
|
422
|
-
)
|
|
423
|
-
|
|
424
|
-
const dynamicMiddlewares = implementMiddleware<RequestContextMap>()(make.dynamicMiddlewares)
|
|
425
|
-
|
|
426
|
-
const l = Layer.scoped(
|
|
427
|
-
MiddlewareMaker,
|
|
428
|
-
Effect
|
|
429
|
-
.all({
|
|
430
|
-
dynamicMiddlewares: dynamicMiddlewares.effect,
|
|
431
|
-
middleware: make.execute((
|
|
432
|
-
cb: MakeRPCHandlerFactory<RequestContextMap, HttpRouter.HttpRouter.Provided | ContextProviderA>
|
|
433
|
-
) => cb),
|
|
434
|
-
contextProvider: make.contextProvider // uses the middleware.contextProvider tag to get the context provider service
|
|
435
|
-
})
|
|
436
|
-
.pipe(
|
|
437
|
-
Effect.map(({ contextProvider, dynamicMiddlewares, middleware }) => ({
|
|
438
|
-
_tag: "MiddlewareMaker" as const,
|
|
439
|
-
effect: makeRpcEffect<RequestContextMap, ContextProviderA>()(
|
|
440
|
-
(schema, handler, moduleName) => {
|
|
441
|
-
const h = middleware(schema, handler as any, moduleName)
|
|
442
|
-
return requestMiddleware(
|
|
443
|
-
Effect.fnUntraced(function*(req, headers) {
|
|
444
|
-
yield* Effect.annotateCurrentSpan(
|
|
445
|
-
"request.name",
|
|
446
|
-
moduleName ? `${moduleName}.${req._tag}` : req._tag
|
|
447
|
-
)
|
|
448
|
-
|
|
449
|
-
// the contextProvider is an Effect that builds the context for the request
|
|
450
|
-
return yield* contextProvider.pipe(
|
|
451
|
-
Effect.flatMap((contextProviderContext) =>
|
|
452
|
-
// the dynamicMiddlewares is an Effect that builds the dynamiuc context for the request
|
|
453
|
-
dynamicMiddlewares(schema.config ?? {}, headers).pipe(
|
|
454
|
-
Effect.flatMap((dynamicContext) => h(req, headers).pipe(Effect.provide(dynamicContext))),
|
|
455
|
-
Effect.provide(contextProviderContext)
|
|
456
|
-
)
|
|
457
|
-
)
|
|
458
|
-
)
|
|
459
|
-
}) as any,
|
|
460
|
-
moduleName
|
|
461
|
-
)
|
|
462
|
-
}
|
|
463
|
-
)
|
|
464
|
-
}))
|
|
465
|
-
)
|
|
466
|
-
)
|
|
467
|
-
const middlewareLayer = l
|
|
468
|
-
.pipe(
|
|
469
|
-
Layer.provide(
|
|
470
|
-
Layer.mergeAll(
|
|
471
|
-
make.dependencies ? make.dependencies as any : Layer.empty,
|
|
472
|
-
...(dynamicMiddlewares.dependencies as any),
|
|
473
|
-
make.contextProvider.Default
|
|
474
|
-
)
|
|
475
|
-
)
|
|
476
|
-
) as Layer.Layer<
|
|
477
|
-
MiddlewareMakerId,
|
|
478
|
-
| MakeMiddlewareE // what the middleware construction can fail with
|
|
479
|
-
| LayersUtils.GetLayersContext<typeof dynamicMiddlewares.dependencies> // what could go wrong when building the dynamic middleware provider
|
|
480
|
-
| Layer.Error<typeof make.contextProvider.Default>, // what could go wrong when building the context provider
|
|
481
|
-
| LayersUtils.GetLayersContext<MiddlewareDependencies> // what's needed to build layers
|
|
482
|
-
| LayersUtils.GetLayersContext<typeof dynamicMiddlewares.dependencies> // what's needed to build dynamic middleware layers
|
|
483
|
-
| Exclude<MakeMiddlewareR, LayersUtils.GetLayersSuccess<MiddlewareDependencies>> // what layers provides
|
|
484
|
-
| Layer.Context<typeof make.contextProvider.Default> // what's needed to build the contextProvider
|
|
485
|
-
>
|
|
486
|
-
|
|
487
|
-
return Object.assign(MiddlewareMaker, { Default: middlewareLayer })
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
// it just provides the right types without cluttering the implementation with them
|
|
491
|
-
function makeRpcEffect<
|
|
492
|
-
RequestContextMap extends Record<string, RPCContextMap.Any>,
|
|
493
|
-
ContextProviderA
|
|
494
|
-
>() {
|
|
495
|
-
return (
|
|
496
|
-
cb: <
|
|
497
|
-
T extends {
|
|
498
|
-
config?: Partial<Record<keyof RequestContextMap, any>>
|
|
499
|
-
},
|
|
500
|
-
Req extends S.TaggedRequest.All,
|
|
501
|
-
HandlerR
|
|
502
|
-
>(
|
|
503
|
-
schema: T & S.Schema<Req, any, never>,
|
|
504
|
-
handler: (
|
|
505
|
-
request: Req,
|
|
506
|
-
headers: any
|
|
507
|
-
) => Effect.Effect<
|
|
508
|
-
EffectRequest.Request.Success<Req>,
|
|
509
|
-
EffectRequest.Request.Error<Req>,
|
|
510
|
-
HandlerR
|
|
511
|
-
>,
|
|
512
|
-
moduleName: string
|
|
513
|
-
) => (
|
|
514
|
-
req: Req,
|
|
515
|
-
headers: any
|
|
516
|
-
) => Effect.Effect<
|
|
517
|
-
Request.Request.Success<Req>,
|
|
518
|
-
Request.Request.Error<Req> | RequestContextMapErrors<RequestContextMap>,
|
|
519
|
-
| HttpRouter.HttpRouter.Provided // the context provider may require HttpRouter.Provided to run
|
|
520
|
-
| Exclude<
|
|
521
|
-
// it can also be removed from HandlerR
|
|
522
|
-
Exclude<HandlerR, GetEffectContext<RequestContextMap, (T & S.Schema<Req, any, never>)["config"]>>,
|
|
523
|
-
ContextProviderA
|
|
524
|
-
>
|
|
525
|
-
>
|
|
526
|
-
) => cb
|
|
527
|
-
}
|