Skip to content

Commit

Permalink
feat(): add ip route param
Browse files Browse the repository at this point in the history
  • Loading branch information
Svetlana Kolosova committed Oct 24, 2019
1 parent 2205f2c commit 2024b2a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/common/decorators/http/route-params.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ export const Next: () => ParameterDecorator = createRouteParamDecorator(
RouteParamtypes.NEXT,
);

/**
* Route handler parameter decorator. Extracts the `Ip` property
* from the `req` object and populates the decorated
* parameter with the value of `ip`.
*
* @see [Request object](https://docs.nestjs.com/controllers#request-object)
*
* @publicApi
*/
export const Ip: () => ParameterDecorator = createRouteParamDecorator(
RouteParamtypes.IP,
);

/**
* Route handler parameter decorator. Extracts the `Session` object
* from the underlying platform and populates the decorated
Expand Down
1 change: 1 addition & 0 deletions packages/common/enums/route-paramtypes.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export enum RouteParamtypes {
SESSION,
FILE,
FILES,
IP,
}
2 changes: 2 additions & 0 deletions packages/core/router/route-params-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export class RouteParamsFactory implements IRouteParamsFactory {
return req[data || 'file'];
case RouteParamtypes.FILES:
return req.files;
case RouteParamtypes.IP:
return req.ip;
default:
return null;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/core/test/router/route-params-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('RouteParamsFactory', () => {
const res = {};
const next = () => ({});
const req = {
ip: 'ip',
session: null,
body: {
foo: 'bar',
Expand Down Expand Up @@ -73,6 +74,13 @@ describe('RouteParamsFactory', () => {
).to.be.eql(req.headers);
});
});
describe(`RouteParamtypes.IP`, () => {
it('should return ip property', () => {
expect(
(factory as any).exchangeKeyForValue(RouteParamtypes.IP, ...args),
).to.be.equal(req.ip);
});
});
describe(`RouteParamtypes.SESSION`, () => {
it('should return session object', () => {
expect(
Expand Down

0 comments on commit 2024b2a

Please sign in to comment.