Effect CommunityEC
Effect Community3mo ago
16 replies
Maks

Disabling Error for Middleware in Effect Typescript Library

Hey Mattia, could we somehow disable this error for middlewares by default?

You are returning an Effect-able type inside a generator function, and will result in nested Effect<Effect<...>>.
Maybe you wanted to return yield* instead?
Nested Effect-able types may be intended if you plan to later manually flatten or unwrap this Effect, if so you can safely disable this diagnostic for this line through quickfixes.    effect(returnEffectInGen) (effect 11)


I'm testing a ratelimiting middleware which should return an Effect from the outer generator. However, the LSP still complains...

export const RateLimitLive = Layer.effect(
  RateLimiterMiddleware,
  Effect.scoped(
    Effect.gen(function* () {
      yield* Effect.log('creating RateLimit middleware');

      const rateLimiter = yield* RateLimiter.make({
        limit: 10,
        interval: Duration.seconds(1),
      });
      const connectionSem = yield* Effect.makeSemaphore(10);

      return Effect.gen(function* () {
        const request = yield* HttpServerRequest.HttpServerRequest;
        yield* Effect.log(`Rate limiting: ${request.method} ${request.url}`);

        // Apply both semaphore and rate limiter
        yield* connectionSem.withPermits(1)(rateLimiter(Effect.void));
      });
    }),
  ),
);
Was this page helpful?