UNPKG

@rzl-zone/utils-js

Version:

A modern, lightweight set of JavaScript utility functions for everyday development, crafted to enhance code readability and maintainability.

42 lines 1.66 kB
import{NextRequest}from 'next/server'; /** --------------------------------- * * ***Retrieves the real client IP address and constructs the full URL using headers like*** * ***`x-forwarded-for`, `x-forwarded-proto`, and `x-forwarded-port`.*** * --------------------------------- * * * ⚠️ **Notes:** * - Only supported in **Next.js** environments (specifically in `server-only` contexts). * - Should be used in **middleware** or **server actions** that have access to `NextRequest`. * * * @param {NextRequest} request - The incoming Next.js request object. * @param {boolean} [includeFullUrl=true] - Whether to return the full URL (`protocol://ip:port`) or just the IP address. * * @returns {string} The extracted client IP address or the full constructed URL. * * @throws {Error} If the function is used outside a Next.js server environment. * @throws {TypeError} If the arguments do not match the expected types. * * @example * // Basic usage in Next.js middleware * import { NextRequest } from "next/server"; * import { getClientIpOrUrl } from "@rzl-zone/utils-js/next/server"; * * export function middleware(request: NextRequest) { * const clientIp = getClientIpOrUrl(request, false); * console.log("Client IP:", clientIp); * } * * @example * // Get full URL * const url = getClientIpOrUrl(request); * console.log("Client full URL:", url); */ declare const getClientIpOrUrl:( /** * The incoming Next.js request object. */ request:NextRequest, /** * Whether to return the full URL (protocol, IP, and port) or just the IP address. * * @default true */ includeFullUrl?:boolean)=>string;export{getClientIpOrUrl};