Skip to content

Commit 5201436

Browse files
authored
Merge pull request #71 from DouglasNeuroInformatics/throttler
add ability for user to override default throttler config and remove old magic string implementation for login throttle
2 parents c0db8e5 + 0681187 commit 5201436

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed

src/app/app.factory.ts

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { MiddlewareConsumer, Provider, Type } from '@nestjs/common';
22
import { APP_FILTER, APP_GUARD, APP_PIPE } from '@nestjs/core';
33
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
4+
import type { ThrottlerModuleOptions } from '@nestjs/throttler';
45
import type { Simplify } from 'type-fest';
56
import type { z } from 'zod/v4';
67

@@ -24,6 +25,7 @@ export type CreateAppOptions = Simplify<
2425
envSchema: z.ZodType<UserTypes.Env>;
2526
imports?: (ConditionalImport | ImportedModule)[];
2627
providers?: Provider[];
28+
throttler?: ThrottlerModuleOptions;
2729
}
2830
>;
2931

@@ -39,9 +41,26 @@ export class AppFactory {
3941
controllers,
4042
envConfig,
4143
imports: userImports = [],
42-
providers: userProviders = []
44+
providers: userProviders = [],
45+
throttler = [
46+
{
47+
limit: 25,
48+
name: 'short',
49+
ttl: 1000
50+
},
51+
{
52+
limit: 100,
53+
name: 'medium',
54+
ttl: 10_000
55+
},
56+
{
57+
limit: 250,
58+
name: 'long',
59+
ttl: 60_000
60+
}
61+
]
4362
}: Simplify<
44-
Pick<CreateAppOptions, 'configureMiddleware' | 'controllers' | 'imports' | 'providers'> & {
63+
Pick<CreateAppOptions, 'configureMiddleware' | 'controllers' | 'imports' | 'providers' | 'throttler'> & {
4564
envConfig: UserTypes.Env;
4665
}
4766
>): DynamicAppModule {
@@ -65,34 +84,7 @@ export class AppFactory {
6584
}
6685

6786
if (envConfig.THROTTLER_ENABLED) {
68-
coreImports.push(
69-
ThrottlerModule.forRoot([
70-
{
71-
limit: 25,
72-
name: 'short',
73-
ttl: 1000
74-
},
75-
{
76-
limit: 100,
77-
name: 'medium',
78-
ttl: 10_000
79-
},
80-
{
81-
limit: 250,
82-
name: 'long',
83-
ttl: 60_000
84-
},
85-
{
86-
limit: envConfig.LOGIN_REQUEST_TROTTLER_LIMIT ?? 50,
87-
name: 'login',
88-
skipIf: (context): boolean => {
89-
const handler = context.getHandler();
90-
return handler.name !== 'login';
91-
},
92-
ttl: envConfig.LOGIN_REQUEST_TROTTLER_TTL ?? 60_000
93-
}
94-
])
95-
);
87+
coreImports.push(ThrottlerModule.forRoot(throttler));
9688
coreProviders.push({
9789
provide: APP_GUARD,
9890
useClass: ThrottlerGuard

src/schemas/env.schema.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ export const $BaseEnv = z.object({
1616
DEBUG: $BooleanLike.optional(),
1717
/** enable log-level logs (default = true in development or production, false in tests) */
1818
LOG: $BooleanLike.optional(),
19-
LOGIN_REQUEST_TROTTLER_LIMIT: $NumberLike.pipe(z.number().int().positive()).optional(),
20-
LOGIN_REQUEST_TROTTLER_TTL: $NumberLike.pipe(z.number().int().positive()).optional(),
2119
NODE_ENV: z.enum(['development', 'production', 'test']),
2220
SECRET_KEY: z.string().min(16),
2321
THROTTLER_ENABLED: $BooleanLike.default(true),

0 commit comments

Comments
 (0)