sm-utility 2.4.22 → 2.4.23
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/package.json +3 -1
- package/request/index.d.ts +1 -0
- package/request/index.js +1 -0
- package/request/requestExtractor.d.ts +25 -0
- package/request/requestExtractor.js +49 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sm-utility",
|
|
3
|
-
"version": "2.4.
|
|
3
|
+
"version": "2.4.23",
|
|
4
4
|
"description": "reusable utility codes for sm projects",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@types/express": "^4.17.11",
|
|
23
23
|
"@types/node": "^14.14.40",
|
|
24
|
+
"@types/request-ip": "0.0.41",
|
|
24
25
|
"@types/uuid": "^8.3.1",
|
|
25
26
|
"copyfiles": "^2.4.1",
|
|
26
27
|
"typescript": "^4.2.4"
|
|
@@ -41,6 +42,7 @@
|
|
|
41
42
|
"nanoid": "^3.3.6",
|
|
42
43
|
"node-xlsx": "^0.24.0",
|
|
43
44
|
"redis": "^4.1.0",
|
|
45
|
+
"request-ip": "3.3.0",
|
|
44
46
|
"ts-node": "^10.9.1",
|
|
45
47
|
"uuid": "^8.3.2",
|
|
46
48
|
"winston": "^3.3.3"
|
package/request/index.d.ts
CHANGED
package/request/index.js
CHANGED
|
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./axios-custom-client"), exports);
|
|
18
18
|
__exportStar(require("./axios"), exports);
|
|
19
19
|
__exportStar(require("./types"), exports);
|
|
20
|
+
__exportStar(require("./requestExtractor"), exports);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Request } from 'express';
|
|
2
|
+
export declare class IpAddress {
|
|
3
|
+
private readonly ip;
|
|
4
|
+
constructor(ip: string | null);
|
|
5
|
+
raw(): string;
|
|
6
|
+
sanitized(): string;
|
|
7
|
+
}
|
|
8
|
+
export declare class RequestExtractor {
|
|
9
|
+
private readonly req;
|
|
10
|
+
constructor(req: Request);
|
|
11
|
+
/**
|
|
12
|
+
* Extracts the ip address from the request headers.
|
|
13
|
+
*
|
|
14
|
+
* If the ip address is not in the cf-connecting-ip header (Cloudflare - HML and PROD), it will be extracted from the x-forwarded-for header (local development)
|
|
15
|
+
*/
|
|
16
|
+
extractIp(): IpAddress;
|
|
17
|
+
/**
|
|
18
|
+
* Extracts the trace id (x-ray trace id) from the request headers
|
|
19
|
+
*/
|
|
20
|
+
extractTraceId(): string | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Extracts the user agent from the request headers
|
|
23
|
+
*/
|
|
24
|
+
extractUserAgent(): string | undefined;
|
|
25
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.RequestExtractor = exports.IpAddress = void 0;
|
|
7
|
+
const request_ip_1 = __importDefault(require("request-ip"));
|
|
8
|
+
class IpAddress {
|
|
9
|
+
constructor(ip) {
|
|
10
|
+
this.ip = ip;
|
|
11
|
+
}
|
|
12
|
+
raw() {
|
|
13
|
+
return this.ip || 'unknown';
|
|
14
|
+
}
|
|
15
|
+
sanitized() {
|
|
16
|
+
var _a;
|
|
17
|
+
return ((_a = this.ip) === null || _a === void 0 ? void 0 : _a.replace(/^::ffff:/, '')) || 'unknown';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.IpAddress = IpAddress;
|
|
21
|
+
class RequestExtractor {
|
|
22
|
+
constructor(req) {
|
|
23
|
+
this.req = req;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Extracts the ip address from the request headers.
|
|
27
|
+
*
|
|
28
|
+
* If the ip address is not in the cf-connecting-ip header (Cloudflare - HML and PROD), it will be extracted from the x-forwarded-for header (local development)
|
|
29
|
+
*/
|
|
30
|
+
extractIp() {
|
|
31
|
+
const cloudflareIp = this.req.headers['cf-connecting-ip'];
|
|
32
|
+
const ip = cloudflareIp || request_ip_1.default.getClientIp(this.req);
|
|
33
|
+
return new IpAddress(ip);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Extracts the trace id (x-ray trace id) from the request headers
|
|
37
|
+
*/
|
|
38
|
+
extractTraceId() {
|
|
39
|
+
return this.req.headers['x-amzn-trace-id'];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Extracts the user agent from the request headers
|
|
43
|
+
*/
|
|
44
|
+
extractUserAgent() {
|
|
45
|
+
var _a;
|
|
46
|
+
return (_a = this.req) === null || _a === void 0 ? void 0 : _a.headers['user-agent'];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.RequestExtractor = RequestExtractor;
|