Throttler env

Environment variables for the throttler.

After the throttler was set up, environment variables were created for it. Let's then check it out.

First, they have been written in the .env and .env.example files. Nothing out of the ordinary so far.

THROTTLER_TTL = 60
THROTTLER_LIMIT = 10

Then, in the env.constants file, we can enforce that they must be positive integers. Note that this improved validation can also be applied to the DATASOURCE_PORT variable.

Joi.number().integer().positive().required(),

Next, we may create its configuration namespace in auth -> config -> throttler.config.

export default registerAs('throttler', () => {
  const config = [
    {
      ttl: +process.env.THROTTLER_TTL * 1000,
      limit: +process.env.THROTTLER_LIMIT,
    } as const,
  ] satisfies ThrottlerModuleOptions;
  return config;
});

Note that:

  • The variables must be cast to number, as they are strings

  • The ttl is converted here to ms, as to avoid doing this in the .env file

Finally, all that's left to do is to use it when configuring the ThrottlerModule in the AuthModule.

ThrottlerModule.forRootAsync(throttlerConfig.asProvider()),

Remember to delete the auth.constants file, no longer used.

Commit - Environment variables for throttler

Last updated