Skip to content

Commit a6d35e8

Browse files
committed
chore: Update _decorators module to lazy load 'request-ip' dependency
1 parent 55c4404 commit a6d35e8

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/_decorators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
12
export * from './real-ip.decorator'

src/_decorators/real-ip.decorator.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import { getClientIp } from 'request-ip'
22
import { createParamDecorator, ExecutionContext } from '@nestjs/common'
33

4-
// noinspection JSUnusedGlobalSymbols
4+
let getClientIp: (request: any) => string | null
5+
6+
(async () => {
7+
try {
8+
const requestIpModule = await import('request-ip')
9+
getClientIp = requestIpModule.getClientIp
10+
} catch (error) {
11+
getClientIp = (_: any): string | null => {
12+
console.error("Le package 'request-ip' n'est pas installé. Veuillez l'installer pour utiliser le décorateur RealIp.")
13+
return null
14+
}
15+
}
16+
})()
17+
518
export const RealIp = createParamDecorator((data: unknown, ctx: ExecutionContext) => {
619
const request = ctx.switchToHttp().getRequest()
720
return getClientIp(request)

0 commit comments

Comments
 (0)