{"version":3,"file":"index.cjs","names":["NextRequest"],"sources":["../../../src/utils/server/getClientIpOrUrl.ts"],"sourcesContent":["import \"@rzl-zone/node-only\";\n\nimport { NextRequest } from \"next/server\";\n\nimport { assertIsBoolean } from \"@rzl-zone/utils-js/assertions\";\nimport { isFunction } from \"@rzl-zone/utils-js/predicates\";\n\n/** ------------------------------------------------------------------\n * * ***Utility for NextJS Server: `getClientIpOrUrl`.***\n * -------------------------------------------------------------------\n *\n * **Retrieves the real client IP address and constructs the full URL using headers like `x-forwarded-for`, `x-forwarded-proto`, and `x-forwarded-port`.**\n * - **ℹ️ Note:**\n *    - Only supported in **Next.js** environments (specifically in `server-only` contexts).\n *    - Should be used in **middleware**, **route-handler** or **server actions** that have access to ***[`NextRequest - NextJS`](https://nextjs.org/docs/app/api-reference/functions/next-request)***.\n *\n * @param {NextRequest} request - The incoming ***`NextJS`*** request object, must be instanceof `NextRequest` from `next/server`.\n * @param {boolean|undefined} [includeFullUrl=true] - Whether to return the full URL (`protocol`, `IP`, and `port` like `protocol://ip:port`) or just the IP address, defaultValue: `true`.\n *\n * @returns {string} The extracted client IP address or the full constructed URL.\n *\n * @throws **{@link Error | `Error`}** if the function is used outside a Next.js server environment.\n * @throws **{@link TypeError | `TypeError`}** if the arguments do not match the expected types.\n *\n * @example\n * // Basic usage in Next.js middleware\n * import { NextRequest } from \"next/server\";\n * import { getClientIpOrUrl } from \"@rzl-zone/utils-js/next/server\";\n *\n * export function middleware(request: NextRequest) {\n *   const clientIp = getClientIpOrUrl(request, false);\n *   console.log(\"Client IP:\", clientIp);\n * }\n *\n * // Get full URL\n * const url = getClientIpOrUrl(request);\n * console.log(\"Client full URL:\", url);\n */\nexport const getClientIpOrUrl = (\n  request: NextRequest,\n  includeFullUrl: boolean = true\n): string => {\n  // Ensure we're in a Next.js edge/server environment\n  if (!isFunction(NextRequest)) {\n    throw new Error(\n      \"Function `getClientIpOrUrl` is designed to be used in a `NextJS` environment.\"\n    );\n  }\n\n  if (!(request instanceof NextRequest)) {\n    throw new TypeError(\n      \"First parameter (`request`) must be an `instance of NextRequest` from `NextJS`.\"\n    );\n  }\n\n  assertIsBoolean(includeFullUrl, {\n    message: ({ currentType, validType }) =>\n      `Second parameter (\\`includeFullUrl\\`) must be of type \\`${validType}\\`, but received: \\`${currentType}\\`.`\n  });\n\n  const forwardedIps = (request.headers.get(\"x-forwarded-for\") ?? \"127.0.0.1\")\n    .trim()\n    .split(\",\");\n\n  // Normalize IPv6 loopback addresses\n  if (forwardedIps[0] === \"::ffff:127.0.0.1\" || forwardedIps[0] === \"::1\") {\n    forwardedIps[0] = \"127.0.0.1\";\n  }\n\n  // Get the last non-empty IP from the list (more reliable for real client IP)\n  const clientIp: string =\n    forwardedIps.length > 1\n      ? forwardedIps[forwardedIps.length - 1]?.trim() || \"\"\n      : forwardedIps[0] || \"\";\n\n  if (!includeFullUrl) {\n    return clientIp;\n  }\n\n  // Construct full URL using protocol, IP, and port\n  const protocol = request.headers.get(\"x-forwarded-proto\") || \"http\";\n  // const protocol = \"http\";\n  const port = request.headers.get(\"x-forwarded-port\") || \"3000\";\n\n  return `${\n    process.env.NODE_ENV === \"production\" ? protocol : \"http\"\n  }://${clientIp}:${port}`;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,MAAa,oBACX,SACA,iBAA0B,SACf;CAEX,IAAI,+CAAYA,0BAAW,GACzB,MAAM,IAAI,MACR,+EACF;CAGF,IAAI,EAAE,mBAAmBA,6BACvB,MAAM,IAAI,UACR,iFACF;CAGF,mDAAgB,gBAAgB,EAC9B,UAAU,EAAE,aAAa,gBACvB,2DAA2D,UAAU,sBAAsB,YAAY,KAC3G,CAAC;CAED,MAAM,gBAAgB,QAAQ,QAAQ,IAAI,iBAAiB,KAAK,YAAW,CACxE,KAAK,CAAC,CACN,MAAM,GAAG;CAGZ,IAAI,aAAa,OAAO,sBAAsB,aAAa,OAAO,OAChE,aAAa,KAAK;CAIpB,MAAM,WACJ,aAAa,SAAS,IAClB,aAAa,aAAa,SAAS,EAAE,EAAE,KAAK,KAAK,KACjD,aAAa,MAAM;CAEzB,IAAI,CAAC,gBACH,OAAO;CAIT,MAAM,WAAW,QAAQ,QAAQ,IAAI,mBAAmB,KAAK;CAE7D,MAAM,OAAO,QAAQ,QAAQ,IAAI,kBAAkB,KAAK;CAExD,OAAO,GACL,QAAQ,IAAI,aAAa,eAAe,WAAW,OACpD,KAAK,SAAS,GAAG;AACpB"}
