Skip to content

Commit ee20664

Browse files
Merge pull request nestjs#3249 from iniel/add-ip-route-param
feat(): add ip route param decorator
2 parents 5e51395 + 2024b2a commit ee20664

4 files changed

Lines changed: 24 additions & 0 deletions

File tree

packages/common/decorators/http/route-params.decorator.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ export const Next: () => ParameterDecorator = createRouteParamDecorator(
106106
RouteParamtypes.NEXT,
107107
);
108108

109+
/**
110+
* Route handler parameter decorator. Extracts the `Ip` property
111+
* from the `req` object and populates the decorated
112+
* parameter with the value of `ip`.
113+
*
114+
* @see [Request object](https://docs.nestjs.com/controllers#request-object)
115+
*
116+
* @publicApi
117+
*/
118+
export const Ip: () => ParameterDecorator = createRouteParamDecorator(
119+
RouteParamtypes.IP,
120+
);
121+
109122
/**
110123
* Route handler parameter decorator. Extracts the `Session` object
111124
* from the underlying platform and populates the decorated

packages/common/enums/route-paramtypes.enum.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export enum RouteParamtypes {
99
SESSION,
1010
FILE,
1111
FILES,
12+
IP,
1213
}

packages/core/router/route-params-factory.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export class RouteParamsFactory implements IRouteParamsFactory {
3232
return req[data || 'file'];
3333
case RouteParamtypes.FILES:
3434
return req.files;
35+
case RouteParamtypes.IP:
36+
return req.ip;
3537
default:
3638
return null;
3739
}

packages/core/test/router/route-params-factory.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('RouteParamsFactory', () => {
1111
const res = {};
1212
const next = () => ({});
1313
const req = {
14+
ip: 'ip',
1415
session: null,
1516
body: {
1617
foo: 'bar',
@@ -73,6 +74,13 @@ describe('RouteParamsFactory', () => {
7374
).to.be.eql(req.headers);
7475
});
7576
});
77+
describe(`RouteParamtypes.IP`, () => {
78+
it('should return ip property', () => {
79+
expect(
80+
(factory as any).exchangeKeyForValue(RouteParamtypes.IP, ...args),
81+
).to.be.equal(req.ip);
82+
});
83+
});
7684
describe(`RouteParamtypes.SESSION`, () => {
7785
it('should return session object', () => {
7886
expect(

0 commit comments

Comments
 (0)