Providing a middleware to another middleware in the Effect Typescript library involves ensuring t...
Is it possible to provide a middleware to another middleware?
I have an issue where I can not satisfy the requirements of a middleware from another that generates it:
I have an issue where I can not satisfy the requirements of a middleware from another that generates it:
// Requirement Middleware
export class CorrelationId extends Effect.Tag("CorrelationId")<CorrelationId, string>() {}
export class CorrelatorMiddleware extends HttpApiMiddleware.Tag<CorrelatorMiddleware>()("Correlator", {
provides: CorrelationId
}) {}
export const CorrelatorMiddlewareLive = Layer.succeed(CorrelatorMiddleware, Effect.succeed(crypto.randomUUID()))
// Target Middleware
export class Authentication extends HttpApiMiddleware.Tag<Authentication>()(
"Authentication",
{
provides: CurrentProvider,
export const AuthenticationLive: Layer.Layer<Authentication, never, CorrelationId> = Layer.effect(
Authentication,
Effect.gen(function*() {
const providerRepo = yield* ProviderRepo
const correlationId = yield* CorrelationId
{...
}
})
).pipe(Layer.provide([ProviderRepo.Default]))
// HTTP Layers
export const HttpCoreLive = HttpApiBuilder.group(Api, "core", (handlers) =>
Effect.gen(function*() {
const correlationId = yield* CorrelationId
return handlers
{....}
}))
const ApiLive = HttpApiBuilder.api(Api).pipe(Layer.provide(HttpCoreLive), Layer.provide(CorrelatorMiddlewareLive))
export const HttpLive = HttpApiBuilder.serve(HttpMiddleware.logger).pipe(
Layer.provide(HttpApiSwagger.layer()),
Layer.provide(HttpApiBuilder.middlewareOpenApi()),
Layer.provide(HttpApiBuilder.middlewareCors()),
Layer.provide(ApiLive),
HttpServer.withLogAddress,
Layer.provide(NodeHttpServer.layer(createServer, { port: 8080 }))
)
HttpLive.pipe(Layer.launch,// Requirement Middleware
export class CorrelationId extends Effect.Tag("CorrelationId")<CorrelationId, string>() {}
export class CorrelatorMiddleware extends HttpApiMiddleware.Tag<CorrelatorMiddleware>()("Correlator", {
provides: CorrelationId
}) {}
export const CorrelatorMiddlewareLive = Layer.succeed(CorrelatorMiddleware, Effect.succeed(crypto.randomUUID()))
// Target Middleware
export class Authentication extends HttpApiMiddleware.Tag<Authentication>()(
"Authentication",
{
provides: CurrentProvider,
export const AuthenticationLive: Layer.Layer<Authentication, never, CorrelationId> = Layer.effect(
Authentication,
Effect.gen(function*() {
const providerRepo = yield* ProviderRepo
const correlationId = yield* CorrelationId
{...
}
})
).pipe(Layer.provide([ProviderRepo.Default]))
// HTTP Layers
export const HttpCoreLive = HttpApiBuilder.group(Api, "core", (handlers) =>
Effect.gen(function*() {
const correlationId = yield* CorrelationId
return handlers
{....}
}))
const ApiLive = HttpApiBuilder.api(Api).pipe(Layer.provide(HttpCoreLive), Layer.provide(CorrelatorMiddlewareLive))
export const HttpLive = HttpApiBuilder.serve(HttpMiddleware.logger).pipe(
Layer.provide(HttpApiSwagger.layer()),
Layer.provide(HttpApiBuilder.middlewareOpenApi()),
Layer.provide(HttpApiBuilder.middlewareCors()),
Layer.provide(ApiLive),
HttpServer.withLogAddress,
Layer.provide(NodeHttpServer.layer(createServer, { port: 8080 }))
)
HttpLive.pipe(Layer.launch,