@relab/nestjs-common
v1.1.1
Published
Common helpers for Nest.js
Readme
@relab/nestjs-common
Common decorators for NestJS (Fastify) applications to simplify access to cookies, client IP, and user-agent information in your controllers.
Features
- @Cookie: Access cookies from incoming requests easily.
- @Ip: Retrieve the client IP address, supporting proxies via
x-forwarded-for. - @UserAgent: Get the user-agent string from the request headers.
Installation
pnpm add @relab/nestjs-common
# or
yarn add @relab/nestjs-common
# or
npm install @relab/nestjs-commonNote: This package is designed for use with NestJS and Fastify. Make sure you have
@nestjs/common,@nestjs/core,fastify, and@fastify/cookieinstalled as peer dependencies.
Usage
Import the decorators in your controller and use them as parameter decorators:
import { Controller, Get } from '@nestjs/common';
import { Cookie, Ip, UserAgent } from '@relab/nestjs-common';
@Controller('example')
export class ExampleController {
@Get('info')
getInfo(
@Cookie('sessionId') sessionId: string,
@Ip() ip: string,
@UserAgent() userAgent: string,
) {
return {
sessionId,
ip,
userAgent,
};
}
}API Reference
@Cookie(name?: string)
- Retrieves the value of the specified cookie from the request. If no name is provided, returns the entire cookies object.
@Ip()
- Returns the client's IP address. If the
x-forwarded-forheader is present, it uses the first value from that header.
@UserAgent()
- Returns the user-agent string from the request headers.
License
MIT
