Effect CommunityEC
Effect Community11mo ago
6 replies
kristo

How to Add Middleware to an HttpApi with `HttpApiBuilder.toWebHandler`

What is the right way to add middleware to an HttpApi when using
HttpApiBuilder.toWebHandler
? For example, how would I provide CORS middleware:

const MainLive = Layer.mergeAll(
  ApiLive.pipe(Layer.provide(Db.Default)),
  HttpApiBuilder.middlewareCors({
    allowedOrigins: ["*"],
    allowedMethods: ["*"],
  }),
  HttpServer.layerContext,
);

const { handler, dispose } = HttpApiBuilder.toWebHandler(MainLive, {
  middleware: HttpMiddleware.cors({
    allowedOrigins: ["*"],
    allowedMethods: ["*"],
  }),
});

^ Here I provided in two locations, neither of which does anything
Was this page helpful?