@vasrefil/api-toolkit 1.8.3 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -14,6 +14,7 @@ const logger_util_1 = require("./utilities/logger.util");
14
14
  // import { airbrake, airbrakeExpress } from './app-middlewares/airbrake';
15
15
  const port = process.env.PORT || 8012;
16
16
  const app = (0, express_1.default)();
17
+ app.set('trust proxy', true);
17
18
  //configure application
18
19
  app.use(express_1.default.static(path_1.default.join(__dirname, "public")));
19
20
  //mount json form parser
@@ -5,3 +5,4 @@ export * from './log.util';
5
5
  export * from './go-mailer.util';
6
6
  export * from './report.util';
7
7
  export * from './email.util';
8
+ export * from './ip.util';
@@ -21,3 +21,4 @@ __exportStar(require("./log.util"), exports);
21
21
  __exportStar(require("./go-mailer.util"), exports);
22
22
  __exportStar(require("./report.util"), exports);
23
23
  __exportStar(require("./email.util"), exports);
24
+ __exportStar(require("./ip.util"), exports);
@@ -0,0 +1,2 @@
1
+ import { Request } from 'express';
2
+ export declare const getClientIp: (req: Request) => string;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getClientIp = void 0;
4
+ const getClientIp = (req) => {
5
+ // Use req.ip (which Express populates if 'trust proxy' is set)
6
+ // or fallback to headers/connection
7
+ let ip = req.ip || req.headers['x-forwarded-for'] || req.connection.remoteAddress || '';
8
+ // If x-forwarded-for contains multiple IPs, the first one is the client
9
+ if (Array.isArray(ip)) {
10
+ ip = ip[0];
11
+ }
12
+ else if (typeof ip === 'string' && ip.includes(',')) {
13
+ ip = ip.split(',')[0].trim();
14
+ }
15
+ // Handle IPv6 mapped IPv4 (e.g., ::ffff:127.0.0.1 -> 127.0.0.1)
16
+ if (typeof ip === 'string' && ip.startsWith('::ffff:')) {
17
+ ip = ip.substring(7);
18
+ }
19
+ return ip;
20
+ };
21
+ exports.getClientIp = getClientIp;
@@ -80,7 +80,7 @@ declare class LogUtil_ {
80
80
  statusCode: number | undefined;
81
81
  originalUrl: string;
82
82
  baseUrl: string;
83
- ip: string | undefined;
83
+ ip: string;
84
84
  body: any;
85
85
  };
86
86
  private get_user_event_response;
@@ -8,6 +8,7 @@ const airbrake_1 = require("../app-middlewares/airbrake");
8
8
  const api_request_util_1 = require("./api-request.util");
9
9
  const json_stringify_safe = require('json-stringify-safe');
10
10
  const env_1 = __importDefault(require("../env"));
11
+ const ip_util_1 = require("./ip.util");
11
12
  class LogUtil_ {
12
13
  constructor() {
13
14
  this.user_event = async (dto) => {
@@ -61,7 +62,7 @@ class LogUtil_ {
61
62
  const { password, pin, old_pin, new_pin, bvn, nin, file, ...body_ } = req.body;
62
63
  const { params, query, method, url, statusCode, hostname, originalUrl, baseUrl } = req;
63
64
  delete headers_['pin-token'];
64
- const ip = req.headers["x-forwarded-for"]?.split(",")[0]?.trim() || req.socket.remoteAddress;
65
+ const ip = (0, ip_util_1.getClientIp)(req);
65
66
  const request = {
66
67
  params,
67
68
  query,
@@ -99,6 +100,9 @@ class LogUtil_ {
99
100
  // if(this.cronjobs_events.includes(actionType)) {
100
101
  // distinct_id = 'cron-job.org';
101
102
  // }
103
+ if (req.user_token_data) {
104
+ distinct_id = req.user_token_data.email;
105
+ }
102
106
  if (actionType.toLowerCase().includes('webhook')) {
103
107
  distinct_id = actionType.toLowerCase();
104
108
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vasrefil/api-toolkit",
3
3
  "description": "This is Vasrefil API toolkit",
4
- "version": "1.8.3",
4
+ "version": "1.9.0",
5
5
  "author": "Sodiq Alabi",
6
6
  "main": "dist/public-api.js",
7
7
  "types": "dist/public-api.d.ts",