Effect CommunityEC
Effect Community15mo ago
27 replies
debaser

Issue with Middleware Application in Effect Typescript Library

Followup-Question ig:
export const addRedirect = (redirectTo: string) => HttpMiddleware.make((app) => 
  Effect.gen(function* () {
      yield* Effect.log("Redirecting to", redirectTo);
      const res = yield* app;
      return res.pipe(HttpServerResponse.setHeader("Location", redirectTo));
  }));


When I implement it like this:
const live = HttpApiBuilder.server().pipe(
  ...
  Layer.provide(APILive);
  Layer.provide(NodeHttpServer.layer(createServer, { port: 9753 })),
  Layer.provide(
    HttpApiBuilder.middleware((a) => pipe(a, addRedirect("https://www.google.de"))),
   )
);

Its working perfectly. However of course, this gets propagated through all routes in the API.
When I implement it like this:
export const APILive = HttpApiBuilder.api(API).pipe(
  Layer.provide(HttpUserLive),
  Layer.provide(HttpAuthLive),
  Layer.provide(
    HttpApiBuilder.middleware((a) => pipe(a, addRedirect("https://effect.website/"))),
  )
);

It doesnt work at all.
In Theory I would provide the Middleware to only one of the Group Implementations (for example HttpAuthLive), and not all of them.
Also explaining, why its not working in the second case would be much appreciated. Do I mix up some concepts/types? through the beloved "effect magic" I don't get any errors.
Was this page helpful?