Skip to content

Commit b29f71e

Browse files
committed
feat: add delay middleware
1 parent ef00001 commit b29f71e

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

apps/api/src/app.module.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { CryptoModule } from '@douglasneuroinformatics/libnest/crypto';
22
import { LoggingModule } from '@douglasneuroinformatics/libnest/logging';
33
import { Module } from '@nestjs/common';
4+
import type { MiddlewareConsumer, NestModule } from '@nestjs/common';
45
import { APP_GUARD } from '@nestjs/core';
56
import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
67

@@ -10,6 +11,7 @@ import { AuthenticationGuard } from './auth/guards/authentication.guard';
1011
import { AuthorizationGuard } from './auth/guards/authorization.guard';
1112
import { ConfigurationModule } from './configuration/configuration.module';
1213
import { ConfigurationService } from './configuration/configuration.service';
14+
import { DelayMiddleware } from './core/middleware/delay.middleware';
1315
import { GatewayModule } from './gateway/gateway.module';
1416
import { GroupsModule } from './groups/groups.module';
1517
import { InstrumentsModule } from './instruments/instruments.module';
@@ -93,4 +95,13 @@ import { UsersModule } from './users/users.module';
9395
}
9496
]
9597
})
96-
export class AppModule {}
98+
export class AppModule implements NestModule {
99+
constructor(private readonly configurationService: ConfigurationService) {}
100+
101+
configure(consumer: MiddlewareConsumer) {
102+
const isDev = this.configurationService.get('NODE_ENV') === 'development';
103+
if (isDev) {
104+
consumer.apply(DelayMiddleware).forRoutes('*');
105+
}
106+
}
107+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Injectable, type NestMiddleware } from '@nestjs/common';
2+
3+
import { ConfigurationService } from '@/configuration/configuration.service';
4+
5+
@Injectable()
6+
export class DelayMiddleware implements NestMiddleware {
7+
constructor(private readonly configurationService: ConfigurationService) {}
8+
9+
use(_req: any, _res: any, next: (error?: any) => void) {
10+
const responseDelay = this.configurationService.get('API_RESPONSE_DELAY');
11+
if (!responseDelay) {
12+
return next();
13+
}
14+
setTimeout(() => {
15+
next();
16+
}, responseDelay);
17+
}
18+
}

0 commit comments

Comments
 (0)