@nathapp/nestjs-common 1.0.1-rc.23 → 1.0.1-rc.24
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/decorator/client-info.decorator.js +2 -2
- package/dist/decorator/client-info.decorator.js.map +1 -1
- package/dist/decorator/real-ip.decorator.js +2 -2
- package/dist/decorator/real-ip.decorator.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/ip/index.d.ts +2 -0
- package/dist/ip/index.js +7 -0
- package/dist/ip/index.js.map +1 -0
- package/dist/ip/is.d.ts +17 -0
- package/dist/ip/is.js +37 -0
- package/dist/ip/is.js.map +1 -0
- package/dist/ip/request-ip.d.ts +8 -0
- package/dist/ip/request-ip.js +181 -0
- package/dist/ip/request-ip.js.map +1 -0
- package/dist/middleware/index.d.ts +4 -1
- package/dist/middleware/index.js +11 -1
- package/dist/middleware/index.js.map +1 -1
- package/dist/middleware/log.provider.d.ts +34 -0
- package/dist/middleware/log.provider.js +125 -0
- package/dist/middleware/log.provider.js.map +1 -0
- package/dist/middleware/logger-option.factory.d.ts +15 -0
- package/dist/middleware/logger-option.factory.js +48 -0
- package/dist/middleware/logger-option.factory.js.map +1 -0
- package/dist/middleware/logger.middleware.d.ts +8 -0
- package/dist/middleware/logger.middleware.js +92 -0
- package/dist/middleware/logger.middleware.js.map +1 -0
- package/package.json +2 -4
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ReqClientInfo = void 0;
|
|
4
|
-
const requestIp = require("request-ip");
|
|
5
4
|
const common_1 = require("@nestjs/common");
|
|
6
5
|
const common_2 = require("@nathapp/common");
|
|
6
|
+
const ip_1 = require("../ip");
|
|
7
7
|
exports.ReqClientInfo = (0, common_1.createParamDecorator)((data, ctx) => {
|
|
8
8
|
const request = ctx.switchToHttp().getRequest();
|
|
9
9
|
const clientInfo = {
|
|
@@ -13,7 +13,7 @@ exports.ReqClientInfo = (0, common_1.createParamDecorator)((data, ctx) => {
|
|
|
13
13
|
os: "",
|
|
14
14
|
appId: "",
|
|
15
15
|
deviceId: "",
|
|
16
|
-
ip:
|
|
16
|
+
ip: (0, ip_1.getClientIp)(request),
|
|
17
17
|
};
|
|
18
18
|
if (request.headers) {
|
|
19
19
|
if (!(0, common_2.isUndefinedOrNullOrEmpty)(request.headers["user-agent"])) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client-info.decorator.js","sourceRoot":"","sources":["../../lib/decorator/client-info.decorator.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"client-info.decorator.js","sourceRoot":"","sources":["../../lib/decorator/client-info.decorator.ts"],"names":[],"mappings":";;;AAAA,2CAAwE;AACxE,4CAA2D;AAC3D,8BAAoC;AAkBvB,QAAA,aAAa,GAAG,IAAA,6BAAoB,EAC/C,CAAC,IAAa,EAAE,GAAqB,EAAc,EAAE;IACnD,MAAM,OAAO,GAAG,GAAG,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;IAChD,MAAM,UAAU,GAAe;QAC7B,OAAO,EAAE,EAAE;QACX,SAAS,EAAE,EAAE;QACb,IAAI,EAAE,OAAO;QACb,EAAE,EAAE,EAAE;QACN,KAAK,EAAE,EAAE;QACT,QAAQ,EAAE,EAAE;QACZ,EAAE,EAAE,IAAA,gBAAW,EAAC,OAAO,CAAC;KACzB,CAAC;IAEF,IAAI,OAAO,CAAC,OAAO,EAAE;QACnB,IAAI,CAAC,IAAA,iCAAwB,EAAC,OAAO,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,EAAE;YAC5D,UAAU,CAAC,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;SACtD;QAED,IAAI,CAAC,IAAA,iCAAwB,EAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,IAAA,iCAAwB,EAAC,OAAO,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC,EAAE;YACvH,UAAU,CAAC,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC;SACjF;QAED,IAAI,CAAC,IAAA,iCAAwB,EAAC,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,EAAE;YACzD,UAAU,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,SAAS,CAAW,CAAC;SAC3D;QAED,IAAI,CAAC,IAAA,iCAAwB,EAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE;YACpD,UAAU,CAAC,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAW,CAAC;SACjD;QAED,IACE,CAAC,IAAA,iCAAwB,EAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;YACpD,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,EAClC;YACA,UAAU,CAAC,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAW,CAAC;SACxD;KACF;IAED,OAAO,UAAU,CAAC;AACpB,CAAC,CACF,CAAC"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RealIP = void 0;
|
|
4
|
-
const
|
|
4
|
+
const ip_1 = require("../ip");
|
|
5
5
|
const common_1 = require("@nestjs/common");
|
|
6
6
|
exports.RealIP = (0, common_1.createParamDecorator)((data, ctx) => {
|
|
7
7
|
const request = ctx.switchToHttp().getRequest();
|
|
8
8
|
if (request.clientIp)
|
|
9
9
|
return request.clientIp;
|
|
10
|
-
return
|
|
10
|
+
return (0, ip_1.getClientIp)(request);
|
|
11
11
|
});
|
|
12
12
|
//# sourceMappingURL=real-ip.decorator.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"real-ip.decorator.js","sourceRoot":"","sources":["../../lib/decorator/real-ip.decorator.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"real-ip.decorator.js","sourceRoot":"","sources":["../../lib/decorator/real-ip.decorator.ts"],"names":[],"mappings":";;;AAAA,8BAAoC;AACpC,2CAAwE;AAE3D,QAAA,MAAM,GAAG,IAAA,6BAAoB,EACxC,CAAC,IAAa,EAAE,GAAqB,EAAE,EAAE;IACvC,MAAM,OAAO,GAAG,GAAG,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;IAChD,IAAI,OAAO,CAAC,QAAQ;QAAE,OAAO,OAAO,CAAC,QAAQ,CAAC;IAE9C,OAAO,IAAA,gBAAW,EAAC,OAAO,CAAC,CAAC;AAC9B,CAAC,CACF,CAAC"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -21,6 +21,7 @@ __exportStar(require("./exception"), exports);
|
|
|
21
21
|
__exportStar(require("./http"), exports);
|
|
22
22
|
__exportStar(require("./i18n"), exports);
|
|
23
23
|
__exportStar(require("./interceptor"), exports);
|
|
24
|
+
__exportStar(require("./ip"), exports);
|
|
24
25
|
__exportStar(require("./logging"), exports);
|
|
25
26
|
__exportStar(require("./middleware"), exports);
|
|
26
27
|
__exportStar(require("./serialize"), exports);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,6CAA2B;AAC3B,8CAA4B;AAC5B,8CAA4B;AAC5B,yCAAuB;AACvB,yCAAuB;AACvB,gDAA8B;AAC9B,4CAA0B;AAC1B,+CAA6B;AAC7B,8CAA4B;AAC5B,0CAAwB"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAyB;AACzB,6CAA2B;AAC3B,8CAA4B;AAC5B,8CAA4B;AAC5B,yCAAuB;AACvB,yCAAuB;AACvB,gDAA8B;AAC9B,uCAAqB;AACrB,4CAA0B;AAC1B,+CAA6B;AAC7B,8CAA4B;AAC5B,0CAAwB"}
|
package/dist/ip/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequestIpMiddleware = exports.getClientIp = void 0;
|
|
4
|
+
const request_ip_1 = require("./request-ip");
|
|
5
|
+
Object.defineProperty(exports, "getClientIp", { enumerable: true, get: function () { return request_ip_1.getClientIp; } });
|
|
6
|
+
Object.defineProperty(exports, "RequestIpMiddleware", { enumerable: true, get: function () { return request_ip_1.mw; } });
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/ip/index.ts"],"names":[],"mappings":";;;AAAA,6CAA+C;AAG7C,4FAHO,wBAAW,OAGP;AACL,oGAJc,eAAE,OAIG"}
|
package/dist/ip/is.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
declare function existy(value: any): boolean;
|
|
2
|
+
declare function ip(value: any): boolean;
|
|
3
|
+
declare function object(value: any): boolean;
|
|
4
|
+
declare function string(value: any): boolean;
|
|
5
|
+
declare const is: {
|
|
6
|
+
existy: typeof existy;
|
|
7
|
+
ip: typeof ip;
|
|
8
|
+
object: typeof object;
|
|
9
|
+
string: typeof string;
|
|
10
|
+
not: {
|
|
11
|
+
existy: (...args: any[]) => boolean;
|
|
12
|
+
ip: (...args: any[]) => boolean;
|
|
13
|
+
object: (...args: any[]) => boolean;
|
|
14
|
+
string: (...args: any[]) => boolean;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export default is;
|
package/dist/ip/is.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const regexes = {
|
|
4
|
+
ipv4: /^(?:(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$/,
|
|
5
|
+
ipv6: /^((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\3)::|:\b|$))|(?!\2\3)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i,
|
|
6
|
+
};
|
|
7
|
+
function not(func) {
|
|
8
|
+
return function (...args) {
|
|
9
|
+
return !func.apply(null, Array.prototype.slice.call(args));
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function existy(value) {
|
|
13
|
+
return value != null;
|
|
14
|
+
}
|
|
15
|
+
function ip(value) {
|
|
16
|
+
return ((existy(value) && regexes.ipv4.test(value)) || regexes.ipv6.test(value));
|
|
17
|
+
}
|
|
18
|
+
function object(value) {
|
|
19
|
+
return Object(value) === value;
|
|
20
|
+
}
|
|
21
|
+
function string(value) {
|
|
22
|
+
return Object.prototype.toString.call(value) === '[object String]';
|
|
23
|
+
}
|
|
24
|
+
const is = {
|
|
25
|
+
existy: existy,
|
|
26
|
+
ip: ip,
|
|
27
|
+
object: object,
|
|
28
|
+
string: string,
|
|
29
|
+
not: {
|
|
30
|
+
existy: not(existy),
|
|
31
|
+
ip: not(ip),
|
|
32
|
+
object: not(object),
|
|
33
|
+
string: not(string),
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
exports.default = is;
|
|
37
|
+
//# sourceMappingURL=is.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is.js","sourceRoot":"","sources":["../../lib/ip/is.ts"],"names":[],"mappings":";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ardedFor(value: string): string | null;
|
|
2
|
+
declare function getClientIpByHeader(req: any, header: string): string | undefined | null;
|
|
3
|
+
declare function getClientIp(req: any): string;
|
|
4
|
+
declare function mw(options: {
|
|
5
|
+
attributeName?: string;
|
|
6
|
+
prioritize?: string[];
|
|
7
|
+
}): any;
|
|
8
|
+
export { getClientIpFromXForwardedFor, getClientIpByHeader, getClientIp, mw, };
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mw = exports.getClientIp = exports.getClientIpByHeader = exports.getClientIpFromXForwardedFor = void 0;
|
|
4
|
+
const is_1 = require("./is");
|
|
5
|
+
const DEFAULT = [
|
|
6
|
+
'x-client-ip',
|
|
7
|
+
'x-forwarded-for',
|
|
8
|
+
'cf-connecting-ip',
|
|
9
|
+
'do-connecting-ip',
|
|
10
|
+
'fastly-client-ip',
|
|
11
|
+
'true-client-ip',
|
|
12
|
+
'x-real-ip',
|
|
13
|
+
'x-cluster-client-ip',
|
|
14
|
+
'x-forwarded',
|
|
15
|
+
'forwarded-for',
|
|
16
|
+
'forwarded',
|
|
17
|
+
'x-appengine-user-ip',
|
|
18
|
+
];
|
|
19
|
+
let headerPriority = DEFAULT;
|
|
20
|
+
function getClientIpFromXForwardedFor(value) {
|
|
21
|
+
if (!is_1.default.existy(value)) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
if (!is_1.default.string(value)) {
|
|
25
|
+
throw new TypeError(`Expected a string, got "${typeof value}"`);
|
|
26
|
+
}
|
|
27
|
+
const forwardedIps = value.split(',').map((e) => {
|
|
28
|
+
const ip = e.trim();
|
|
29
|
+
if (ip.includes(':')) {
|
|
30
|
+
const splitted = ip.split(':');
|
|
31
|
+
if (splitted.length === 2) {
|
|
32
|
+
return splitted[0];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return ip;
|
|
36
|
+
});
|
|
37
|
+
for (let i = 0; i < forwardedIps.length; i++) {
|
|
38
|
+
if (is_1.default.ip(forwardedIps[i])) {
|
|
39
|
+
return forwardedIps[i];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
exports.getClientIpFromXForwardedFor = getClientIpFromXForwardedFor;
|
|
45
|
+
function getClientIpByHeader(req, header) {
|
|
46
|
+
switch (header) {
|
|
47
|
+
case 'x-client-ip':
|
|
48
|
+
if (is_1.default.ip(req === null || req === void 0 ? void 0 : req.headers['x-client-ip'])) {
|
|
49
|
+
return req.headers['x-client-ip'];
|
|
50
|
+
}
|
|
51
|
+
break;
|
|
52
|
+
case 'x-forwarded-for':
|
|
53
|
+
const xForwardedFor = getClientIpFromXForwardedFor(req === null || req === void 0 ? void 0 : req.headers['x-forwarded-for']);
|
|
54
|
+
if (is_1.default.ip(xForwardedFor)) {
|
|
55
|
+
return xForwardedFor;
|
|
56
|
+
}
|
|
57
|
+
break;
|
|
58
|
+
case 'cf-connecting-ip':
|
|
59
|
+
if (is_1.default.ip(req === null || req === void 0 ? void 0 : req.headers['cf-connecting-ip'])) {
|
|
60
|
+
return req.headers['cf-connecting-ip'];
|
|
61
|
+
}
|
|
62
|
+
break;
|
|
63
|
+
case 'do-connecting-ip':
|
|
64
|
+
if (is_1.default.ip(req === null || req === void 0 ? void 0 : req.headers['do-connecting-ip'])) {
|
|
65
|
+
return req.headers['do-connecting-ip'];
|
|
66
|
+
}
|
|
67
|
+
break;
|
|
68
|
+
case 'fastly-client-ip':
|
|
69
|
+
if (is_1.default.ip(req === null || req === void 0 ? void 0 : req.headers['fastly-client-ip'])) {
|
|
70
|
+
return req.headers['fastly-client-ip'];
|
|
71
|
+
}
|
|
72
|
+
break;
|
|
73
|
+
case 'true-client-ip':
|
|
74
|
+
if (is_1.default.ip(req === null || req === void 0 ? void 0 : req.headers['true-client-ip'])) {
|
|
75
|
+
return req.headers['true-client-ip'];
|
|
76
|
+
}
|
|
77
|
+
break;
|
|
78
|
+
case 'x-real-ip':
|
|
79
|
+
if (is_1.default.ip(req === null || req === void 0 ? void 0 : req.headers['x-real-ip'])) {
|
|
80
|
+
return req.headers['x-real-ip'];
|
|
81
|
+
}
|
|
82
|
+
break;
|
|
83
|
+
case 'x-cluster-client-ip':
|
|
84
|
+
if (is_1.default.ip(req === null || req === void 0 ? void 0 : req.headers['x-cluster-client-ip'])) {
|
|
85
|
+
return req.headers['x-cluster-client-ip'];
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
case 'x-forwarded':
|
|
89
|
+
if (is_1.default.ip(req === null || req === void 0 ? void 0 : req.headers['x-forwarded'])) {
|
|
90
|
+
return req.headers['x-forwarded'];
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
case 'forwarded-for':
|
|
94
|
+
if (is_1.default.ip(req === null || req === void 0 ? void 0 : req.headers['forwarded-for'])) {
|
|
95
|
+
return req.headers['forwarded-for'];
|
|
96
|
+
}
|
|
97
|
+
break;
|
|
98
|
+
case 'forwarded':
|
|
99
|
+
if (is_1.default.ip(req === null || req === void 0 ? void 0 : req.headers['forwarded'])) {
|
|
100
|
+
return req.headers['forwarded'];
|
|
101
|
+
}
|
|
102
|
+
break;
|
|
103
|
+
case 'x-appengine-user-ip':
|
|
104
|
+
if (is_1.default.ip(req === null || req === void 0 ? void 0 : req.headers['x-appengine-user-ip'])) {
|
|
105
|
+
return req === null || req === void 0 ? void 0 : req.headers['x-appengine-user-ip'];
|
|
106
|
+
}
|
|
107
|
+
break;
|
|
108
|
+
default:
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
exports.getClientIpByHeader = getClientIpByHeader;
|
|
114
|
+
function getClientIp(req) {
|
|
115
|
+
if (req === null || req === void 0 ? void 0 : req.headers) {
|
|
116
|
+
for (const header of headerPriority) {
|
|
117
|
+
const value = getClientIpByHeader(req, header);
|
|
118
|
+
if (value)
|
|
119
|
+
return value;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (is_1.default.existy(req.socket) && is_1.default.ip(req.socket.remoteAddress)) {
|
|
123
|
+
return req.socket.remoteAddress;
|
|
124
|
+
}
|
|
125
|
+
if (is_1.default.existy(req.connection)) {
|
|
126
|
+
if (is_1.default.ip(req.connection.remoteAddress)) {
|
|
127
|
+
return req.connection.remoteAddress;
|
|
128
|
+
}
|
|
129
|
+
if (is_1.default.existy(req.connection.socket) && is_1.default.ip(req.connection.socket.remoteAddress)) {
|
|
130
|
+
return req.connection.socket.remoteAddress;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (is_1.default.existy(req.info) && is_1.default.ip(req.info.remoteAddress)) {
|
|
134
|
+
return req.info.remoteAddress;
|
|
135
|
+
}
|
|
136
|
+
if (is_1.default.existy(req.requestContext) && is_1.default.existy(req.requestContext.identity) && is_1.default.ip(req.requestContext.identity.sourceIp)) {
|
|
137
|
+
return req.requestContext.identity.sourceIp;
|
|
138
|
+
}
|
|
139
|
+
if (req === null || req === void 0 ? void 0 : req.headers) {
|
|
140
|
+
if (is_1.default.ip(req === null || req === void 0 ? void 0 : req.headers['Cf-Pseudo-IPv4'])) {
|
|
141
|
+
return req.headers['Cf-Pseudo-IPv4'];
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (is_1.default.existy(req.raw)) {
|
|
145
|
+
return getClientIp(req.raw);
|
|
146
|
+
}
|
|
147
|
+
return '';
|
|
148
|
+
}
|
|
149
|
+
exports.getClientIp = getClientIp;
|
|
150
|
+
function mw(options) {
|
|
151
|
+
var _a;
|
|
152
|
+
const configuration = is_1.default.not.existy(options) ? {} : options;
|
|
153
|
+
if (is_1.default.not.object(configuration)) {
|
|
154
|
+
throw new TypeError('Options must be an object!');
|
|
155
|
+
}
|
|
156
|
+
if ((_a = configuration === null || configuration === void 0 ? void 0 : configuration.prioritize) === null || _a === void 0 ? void 0 : _a.length) {
|
|
157
|
+
if (configuration === null || configuration === void 0 ? void 0 : configuration.prioritize.find((item) => typeof item !== 'string')) {
|
|
158
|
+
throw new TypeError('Prioritize list must be an array of string!');
|
|
159
|
+
}
|
|
160
|
+
for (const prioritizedHeader of configuration.prioritize) {
|
|
161
|
+
for (let i = 0; i < headerPriority.length; i++) {
|
|
162
|
+
if (prioritizedHeader === headerPriority[i]) {
|
|
163
|
+
headerPriority.splice(i, 1);
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
headerPriority.unshift(...configuration.prioritize);
|
|
169
|
+
}
|
|
170
|
+
const attributeName = configuration.attributeName || 'clientIp';
|
|
171
|
+
return (req, res, next) => {
|
|
172
|
+
const ip = getClientIp(req);
|
|
173
|
+
Object.defineProperty(req, attributeName, {
|
|
174
|
+
get: () => ip,
|
|
175
|
+
configurable: true,
|
|
176
|
+
});
|
|
177
|
+
next();
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
exports.mw = mw;
|
|
181
|
+
//# sourceMappingURL=request-ip.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-ip.js","sourceRoot":"","sources":["../../lib/ip/request-ip.ts"],"names":[],"mappings":";;;AAKA,6BAAsB;AAEtB,MAAM,OAAO,GAAG;IACd,aAAa;IACb,iBAAiB;IACjB,kBAAkB;IAClB,kBAAkB;IAClB,kBAAkB;IAClB,gBAAgB;IAChB,WAAW;IACX,qBAAqB;IACrB,aAAa;IACb,eAAe;IACf,WAAW;IACX,qBAAqB;CACtB,CAAC;AAEF,IAAI,cAAc,GAAG,OAAO,CAAC;AAQ7B,SAAS,4BAA4B,CAAC,KAAa;IACjD,IAAI,CAAC,YAAE,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE;QACrB,OAAO,IAAI,CAAC;KACb;IAED,IAAI,CAAC,YAAE,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE;QACrB,MAAM,IAAI,SAAS,CAAC,2BAA2B,OAAO,KAAK,GAAG,CAAC,CAAC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EAAE,EAAE;QACvC,MAAM,EAAE,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC;QAC5B,MAAM,CAAC,cAAc,CAAC,GAAG,EAAE,aAAa,EAAE;YACxC,GAAG,EAAE,GAAG,EAAE,CAAC,EAAE;YACb,YAAY,EAAE,IAAI;SACnB,CAAC,CAAC;QACH,IAAI,EAAE,CAAC;IACT,CAAC,CAAC;AACJ,CAAC;AAMC,gBAAE"}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
import { JsonBodyMiddleware } from "./json-body.middleware";
|
|
2
|
+
import { DefaultLogProvider, EnvLogProvider, LogProvider, LogRequest, LogResponse } from "./log.provider";
|
|
3
|
+
import { DefaultLoggerOptionFactory, EnvLoggerOptionFactory, LoggerOptionFactory, LoggerOptions } from "./logger-option.factory";
|
|
4
|
+
import { LoggerMiddleware } from "./logger.middleware";
|
|
2
5
|
import { RawBodyMiddleware } from "./raw-body.middleware";
|
|
3
|
-
export { JsonBodyMiddleware, RawBodyMiddleware };
|
|
6
|
+
export { JsonBodyMiddleware, RawBodyMiddleware, LoggerMiddleware, LogRequest, LogResponse, LogProvider, DefaultLogProvider, EnvLogProvider, LoggerOptions, LoggerOptionFactory, DefaultLoggerOptionFactory, EnvLoggerOptionFactory };
|
package/dist/middleware/index.js
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RawBodyMiddleware = exports.JsonBodyMiddleware = void 0;
|
|
3
|
+
exports.EnvLoggerOptionFactory = exports.DefaultLoggerOptionFactory = exports.LoggerOptionFactory = exports.EnvLogProvider = exports.DefaultLogProvider = exports.LogProvider = exports.LoggerMiddleware = exports.RawBodyMiddleware = exports.JsonBodyMiddleware = void 0;
|
|
4
4
|
const json_body_middleware_1 = require("./json-body.middleware");
|
|
5
5
|
Object.defineProperty(exports, "JsonBodyMiddleware", { enumerable: true, get: function () { return json_body_middleware_1.JsonBodyMiddleware; } });
|
|
6
|
+
const log_provider_1 = require("./log.provider");
|
|
7
|
+
Object.defineProperty(exports, "DefaultLogProvider", { enumerable: true, get: function () { return log_provider_1.DefaultLogProvider; } });
|
|
8
|
+
Object.defineProperty(exports, "EnvLogProvider", { enumerable: true, get: function () { return log_provider_1.EnvLogProvider; } });
|
|
9
|
+
Object.defineProperty(exports, "LogProvider", { enumerable: true, get: function () { return log_provider_1.LogProvider; } });
|
|
10
|
+
const logger_option_factory_1 = require("./logger-option.factory");
|
|
11
|
+
Object.defineProperty(exports, "DefaultLoggerOptionFactory", { enumerable: true, get: function () { return logger_option_factory_1.DefaultLoggerOptionFactory; } });
|
|
12
|
+
Object.defineProperty(exports, "EnvLoggerOptionFactory", { enumerable: true, get: function () { return logger_option_factory_1.EnvLoggerOptionFactory; } });
|
|
13
|
+
Object.defineProperty(exports, "LoggerOptionFactory", { enumerable: true, get: function () { return logger_option_factory_1.LoggerOptionFactory; } });
|
|
14
|
+
const logger_middleware_1 = require("./logger.middleware");
|
|
15
|
+
Object.defineProperty(exports, "LoggerMiddleware", { enumerable: true, get: function () { return logger_middleware_1.LoggerMiddleware; } });
|
|
6
16
|
const raw_body_middleware_1 = require("./raw-body.middleware");
|
|
7
17
|
Object.defineProperty(exports, "RawBodyMiddleware", { enumerable: true, get: function () { return raw_body_middleware_1.RawBodyMiddleware; } });
|
|
8
18
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/middleware/index.ts"],"names":[],"mappings":";;;AAAA,iEAA4D;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/middleware/index.ts"],"names":[],"mappings":";;;AAAA,iEAA4D;AAMnD,mGANA,yCAAkB,OAMA;AAL3B,iDAA0G;AAKF,mGAL/F,iCAAkB,OAK+F;AAAE,+FAL/F,6BAAc,OAK+F;AAA/C,4FAL9C,0BAAW,OAK8C;AAJtG,mEAAiI;AAI+C,2GAJvK,kDAA0B,OAIuK;AAAE,uGAJvK,8CAAsB,OAIuK;AAAvE,oGAJ9F,2CAAmB,OAI8F;AAH9K,2DAAuD;AAGP,iGAHvC,oCAAgB,OAGuC;AAFhE,+DAA0D;AAE7B,kGAFpB,uCAAiB,OAEoB"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { LoggerOptionFactory, LoggerOptions } from "./logger-option.factory";
|
|
2
|
+
export interface LogRequest {
|
|
3
|
+
method: string;
|
|
4
|
+
originalUrl: string;
|
|
5
|
+
data?: any;
|
|
6
|
+
userAgent: string;
|
|
7
|
+
ip: string;
|
|
8
|
+
}
|
|
9
|
+
export interface LogResponse {
|
|
10
|
+
method: string;
|
|
11
|
+
originalUrl: string;
|
|
12
|
+
data?: any;
|
|
13
|
+
statusCode: number;
|
|
14
|
+
responseTime: number;
|
|
15
|
+
}
|
|
16
|
+
export declare class LogProvider {
|
|
17
|
+
private logger;
|
|
18
|
+
protected readonly _options: LoggerOptions;
|
|
19
|
+
constructor(optionsFactory: LoggerOptionFactory);
|
|
20
|
+
get options(): LoggerOptions;
|
|
21
|
+
processOutput(data: any): any;
|
|
22
|
+
protected censorSensitiveKeysData(sensitiveKeys: string[], data: any): any;
|
|
23
|
+
logRequest(request: LogRequest): void;
|
|
24
|
+
logResponse(response: LogResponse): void;
|
|
25
|
+
private toPrintableOutput;
|
|
26
|
+
private toPrintableOutputResponseBody;
|
|
27
|
+
private parseJson;
|
|
28
|
+
}
|
|
29
|
+
export declare class DefaultLogProvider extends LogProvider {
|
|
30
|
+
constructor();
|
|
31
|
+
}
|
|
32
|
+
export declare class EnvLogProvider extends LogProvider {
|
|
33
|
+
constructor();
|
|
34
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.EnvLogProvider = exports.DefaultLogProvider = exports.LogProvider = void 0;
|
|
13
|
+
const common_1 = require("@nathapp/common");
|
|
14
|
+
const common_2 = require("@nestjs/common");
|
|
15
|
+
const logger_option_factory_1 = require("./logger-option.factory");
|
|
16
|
+
let LogProvider = class LogProvider {
|
|
17
|
+
constructor(optionsFactory) {
|
|
18
|
+
this.logger = new common_2.Logger('HTTP');
|
|
19
|
+
this._options = optionsFactory.createOption();
|
|
20
|
+
}
|
|
21
|
+
get options() {
|
|
22
|
+
return this._options;
|
|
23
|
+
}
|
|
24
|
+
processOutput(data) {
|
|
25
|
+
if ((0, common_1.isUndefinedOrNullOrEmpty)(data)) {
|
|
26
|
+
return {};
|
|
27
|
+
}
|
|
28
|
+
if (typeof data === 'string') {
|
|
29
|
+
return data;
|
|
30
|
+
}
|
|
31
|
+
if ('object' == typeof data) {
|
|
32
|
+
return this.censorSensitiveKeysData(this.options.sensitiveKeys, data);
|
|
33
|
+
}
|
|
34
|
+
return data;
|
|
35
|
+
}
|
|
36
|
+
censorSensitiveKeysData(sensitiveKeys, data) {
|
|
37
|
+
if (sensitiveKeys.length == 0)
|
|
38
|
+
return data;
|
|
39
|
+
if ('object' == typeof data) {
|
|
40
|
+
const obj = {};
|
|
41
|
+
Object.keys(data).forEach((key) => {
|
|
42
|
+
if ("object" == typeof data[key] &&
|
|
43
|
+
0 < Object.keys(data[key]).length) {
|
|
44
|
+
obj[key] = this.censorSensitiveKeysData(sensitiveKeys, data[key]);
|
|
45
|
+
}
|
|
46
|
+
else if (sensitiveKeys.includes(key)) {
|
|
47
|
+
obj[key] = "<NOT DISPLAY>";
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
obj[key] = data[key];
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return obj;
|
|
54
|
+
}
|
|
55
|
+
return data;
|
|
56
|
+
}
|
|
57
|
+
logRequest(request) {
|
|
58
|
+
const { method, originalUrl, data, userAgent, ip } = request;
|
|
59
|
+
const rawUserAgent = this._options.logUserAgent ? userAgent : '';
|
|
60
|
+
const rawData = this._options.logRequest ? this.toPrintableOutput(data) : '';
|
|
61
|
+
this.logger.log(`[REQ] ${method} ${originalUrl} req: ${rawData} - ${rawUserAgent} ${ip}`);
|
|
62
|
+
}
|
|
63
|
+
logResponse(response) {
|
|
64
|
+
const { method, originalUrl, data, statusCode, responseTime } = response;
|
|
65
|
+
let responseBody;
|
|
66
|
+
if (this._options.logResponse) {
|
|
67
|
+
responseBody = this.toPrintableOutputResponseBody(data);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
responseBody = '-';
|
|
71
|
+
}
|
|
72
|
+
this.logger.log(`[RES] ${method} ${originalUrl} ${statusCode} ${responseTime.toFixed(2)}ms - res: ${responseBody}`);
|
|
73
|
+
}
|
|
74
|
+
toPrintableOutput(data) {
|
|
75
|
+
const output = this.processOutput(data);
|
|
76
|
+
if (typeof output === 'string') {
|
|
77
|
+
return output;
|
|
78
|
+
}
|
|
79
|
+
if (typeof output === 'object') {
|
|
80
|
+
return JSON.stringify(output);
|
|
81
|
+
}
|
|
82
|
+
return '';
|
|
83
|
+
}
|
|
84
|
+
toPrintableOutputResponseBody(chunks) {
|
|
85
|
+
const rawResponse = Buffer.concat(chunks).toString('utf8');
|
|
86
|
+
const parsed = this.parseJson(rawResponse);
|
|
87
|
+
return (0, common_1.isUndefinedOrNullOrEmpty)(parsed)
|
|
88
|
+
? rawResponse
|
|
89
|
+
: this.processOutput(parsed);
|
|
90
|
+
}
|
|
91
|
+
parseJson(value) {
|
|
92
|
+
try {
|
|
93
|
+
return JSON.parse(value);
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
return '';
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
LogProvider = __decorate([
|
|
101
|
+
(0, common_2.Injectable)(),
|
|
102
|
+
__metadata("design:paramtypes", [logger_option_factory_1.LoggerOptionFactory])
|
|
103
|
+
], LogProvider);
|
|
104
|
+
exports.LogProvider = LogProvider;
|
|
105
|
+
let DefaultLogProvider = class DefaultLogProvider extends LogProvider {
|
|
106
|
+
constructor() {
|
|
107
|
+
super(new logger_option_factory_1.DefaultLoggerOptionFactory());
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
DefaultLogProvider = __decorate([
|
|
111
|
+
(0, common_2.Injectable)(),
|
|
112
|
+
__metadata("design:paramtypes", [])
|
|
113
|
+
], DefaultLogProvider);
|
|
114
|
+
exports.DefaultLogProvider = DefaultLogProvider;
|
|
115
|
+
let EnvLogProvider = class EnvLogProvider extends LogProvider {
|
|
116
|
+
constructor() {
|
|
117
|
+
super(new logger_option_factory_1.EnvLoggerOptionFactory());
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
EnvLogProvider = __decorate([
|
|
121
|
+
(0, common_2.Injectable)(),
|
|
122
|
+
__metadata("design:paramtypes", [])
|
|
123
|
+
], EnvLogProvider);
|
|
124
|
+
exports.EnvLogProvider = EnvLogProvider;
|
|
125
|
+
//# sourceMappingURL=log.provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log.provider.js","sourceRoot":"","sources":["../../lib/middleware/log.provider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,4CAA2D;AAC3D,2C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GAAE;;GACA,cAAc,CAI1B;AAJY,wCAAc"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface LoggerOptions {
|
|
2
|
+
logUserAgent?: boolean;
|
|
3
|
+
logRequest?: boolean;
|
|
4
|
+
logResponse?: boolean;
|
|
5
|
+
sensitiveKeys?: string[];
|
|
6
|
+
}
|
|
7
|
+
export declare abstract class LoggerOptionFactory {
|
|
8
|
+
abstract createOption(): LoggerOptions;
|
|
9
|
+
}
|
|
10
|
+
export declare class DefaultLoggerOptionFactory extends LoggerOptionFactory {
|
|
11
|
+
createOption(): LoggerOptions;
|
|
12
|
+
}
|
|
13
|
+
export declare class EnvLoggerOptionFactory extends LoggerOptionFactory {
|
|
14
|
+
createOption(): LoggerOptions;
|
|
15
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.EnvLoggerOptionFactory = exports.DefaultLoggerOptionFactory = exports.LoggerOptionFactory = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
const defaultOption = {
|
|
12
|
+
logUserAgent: false,
|
|
13
|
+
logRequest: true,
|
|
14
|
+
logResponse: true,
|
|
15
|
+
sensitiveKeys: ['pwd', 'password', 'accessToken', 'refreshToken', 'secret', 'apiSecret'],
|
|
16
|
+
};
|
|
17
|
+
class LoggerOptionFactory {
|
|
18
|
+
}
|
|
19
|
+
exports.LoggerOptionFactory = LoggerOptionFactory;
|
|
20
|
+
let DefaultLoggerOptionFactory = class DefaultLoggerOptionFactory extends LoggerOptionFactory {
|
|
21
|
+
createOption() {
|
|
22
|
+
return defaultOption;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
DefaultLoggerOptionFactory = __decorate([
|
|
26
|
+
(0, common_1.Injectable)()
|
|
27
|
+
], DefaultLoggerOptionFactory);
|
|
28
|
+
exports.DefaultLoggerOptionFactory = DefaultLoggerOptionFactory;
|
|
29
|
+
let EnvLoggerOptionFactory = class EnvLoggerOptionFactory extends LoggerOptionFactory {
|
|
30
|
+
createOption() {
|
|
31
|
+
const logUserAgent = process.env.HTTP_LOG_USER_AGENT ? process.env.HTTP_LOG_USER_AGENT === '1' : false;
|
|
32
|
+
const logRequest = process.env.HTTP_LOG_REQUEST ? process.env.HTTP_LOG_REQUEST === '1' : true;
|
|
33
|
+
const logResponse = process.env.HTTP_LOG_RESPONSE ? process.env.HTTP_LOG_RESPONSE === '1' : false;
|
|
34
|
+
const sensitiveKeyString = process.env.HTTP_LOG_SENSITIVEKEYS ? process.env.HTTP_LOG_SENSITIVEKEYS || '' : 'pwd,password,accessToken,refreshToken,secret,apiSecret';
|
|
35
|
+
const sensitiveKeys = sensitiveKeyString.split(",");
|
|
36
|
+
return {
|
|
37
|
+
logRequest,
|
|
38
|
+
logUserAgent,
|
|
39
|
+
logResponse,
|
|
40
|
+
sensitiveKeys,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
EnvLoggerOptionFactory = __decorate([
|
|
45
|
+
(0, common_1.Injectable)()
|
|
46
|
+
], EnvLoggerOptionFactory);
|
|
47
|
+
exports.EnvLoggerOptionFactory = EnvLoggerOptionFactory;
|
|
48
|
+
//# sourceMappingURL=logger-option.factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger-option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AC,KAAK,CAAC,GAAG,CAAC,CAAC;QACpD,OAAO;YACL,UAAU;YACV,YAAY;YACZ,WAAW;YACX,aAAa;SACd,CAAA;IACH,CAAC;CACF,CAAA;AAfY,sBAAsB;IADlC,IAAA,mBAAU,GAAE;GACA,sBAAsB,CAelC;AAfY,wDAAsB"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { NestMiddleware } from '@nestjs/common';
|
|
2
|
+
import { LogProvider } from './log.provider';
|
|
3
|
+
export declare class LoggerMiddleware implements NestMiddleware {
|
|
4
|
+
private readonly logProvider;
|
|
5
|
+
constructor(logProvider?: LogProvider);
|
|
6
|
+
use(request: any, response: any, next: () => void): void;
|
|
7
|
+
private subscribeResponseChunk;
|
|
8
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.LoggerMiddleware = void 0;
|
|
16
|
+
const common_1 = require("@nathapp/common");
|
|
17
|
+
const common_2 = require("@nestjs/common");
|
|
18
|
+
const log_provider_1 = require("./log.provider");
|
|
19
|
+
const ip_1 = require("../ip");
|
|
20
|
+
let LoggerMiddleware = class LoggerMiddleware {
|
|
21
|
+
constructor(logProvider) {
|
|
22
|
+
this.logProvider = logProvider || new log_provider_1.EnvLogProvider();
|
|
23
|
+
}
|
|
24
|
+
use(request, response, next) {
|
|
25
|
+
const startAt = process.hrtime();
|
|
26
|
+
const { method, query, params, body } = request;
|
|
27
|
+
const originalUrl = request.originalUrl || request.url;
|
|
28
|
+
const loggerOption = this.logProvider.options;
|
|
29
|
+
const { logRequest, logResponse } = loggerOption;
|
|
30
|
+
let data = {};
|
|
31
|
+
const ip = (0, ip_1.getClientIp)(request);
|
|
32
|
+
const userAgent = request.headers['user-agent'];
|
|
33
|
+
if (logRequest) {
|
|
34
|
+
if (query && !(0, common_1.isUndefinedOrNullOrEmpty)(query)) {
|
|
35
|
+
data = query;
|
|
36
|
+
}
|
|
37
|
+
if (params && !(0, common_1.isUndefinedOrNullOrEmpty)(params)) {
|
|
38
|
+
data = params;
|
|
39
|
+
}
|
|
40
|
+
if (body && !(0, common_1.isUndefinedOrNullOrEmpty)(body)) {
|
|
41
|
+
data = body;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
this.logProvider.logRequest({
|
|
45
|
+
method,
|
|
46
|
+
data,
|
|
47
|
+
userAgent,
|
|
48
|
+
ip,
|
|
49
|
+
originalUrl,
|
|
50
|
+
});
|
|
51
|
+
const chunks = [];
|
|
52
|
+
if (logResponse) {
|
|
53
|
+
this.subscribeResponseChunk(response, chunks);
|
|
54
|
+
}
|
|
55
|
+
response.on('finish', () => {
|
|
56
|
+
const { statusCode } = response;
|
|
57
|
+
const diff = process.hrtime(startAt);
|
|
58
|
+
const responseTime = diff[0] * 1e3 + diff[1] * 1e-6;
|
|
59
|
+
this.logProvider.logResponse({
|
|
60
|
+
method,
|
|
61
|
+
originalUrl,
|
|
62
|
+
statusCode,
|
|
63
|
+
responseTime,
|
|
64
|
+
data: chunks,
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
if (next) {
|
|
68
|
+
next();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
subscribeResponseChunk(response, chunks) {
|
|
72
|
+
const oldWrite = response.write;
|
|
73
|
+
const oldEnd = response.end;
|
|
74
|
+
response.write = function (chunk) {
|
|
75
|
+
chunks.push(Buffer.from(chunk));
|
|
76
|
+
return oldWrite.apply(response, arguments);
|
|
77
|
+
};
|
|
78
|
+
response.end = function (chunk) {
|
|
79
|
+
if (chunk) {
|
|
80
|
+
chunks.push(Buffer.from(chunk));
|
|
81
|
+
}
|
|
82
|
+
return oldEnd.apply(response, arguments);
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
LoggerMiddleware = __decorate([
|
|
87
|
+
(0, common_2.Injectable)(),
|
|
88
|
+
__param(0, (0, common_2.Optional)()),
|
|
89
|
+
__metadata("design:paramtypes", [log_provider_1.LogProvider])
|
|
90
|
+
], LoggerMiddleware);
|
|
91
|
+
exports.LoggerMiddleware = LoggerMiddleware;
|
|
92
|
+
//# sourceMappingURL=logger.middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.middleware.js","sourceRoot":"","sources":["../../lib/middleware/logger.middleware.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,4CAA2D;AAC3D,2CAAsE;AACtE,iDAA6D;AAC7D,8BAAoC;AAS7B,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;IAG3B,YAAwB,WAAyB;QAC/C,IAAI,CAAC,WAAW,GAAG,WAAW,IAAI,IAAI,6BAAc,EAAE,CAAC;IACzD,CAAC;IAED,GAAG,CAAC,OAAY,EAAE,QAAa,EAAE,IAAgB;QAC/C,MAAM,OAAO,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;QACjC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAA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,GAAG,IAAI,CAAC;YAEpD,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC;gBAC3B,MAAM;gBACN,WAAW;gBACX,UAAU;gBACV,YAAY;gBACZ,IAAI,EAAE,MAAM;aACb,CAAC,CAAA;QACJ,CAAC,CAAC,CAAC;QAEH,IAAI,IAAI,EAAE;YACR,IAAI,EAAE,CAAC;SACR;IACH,CAAC;IAEO,sBAAsB,CAAC,QAAa,EAAE,MAAa;QACzD,MAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC;QAChC,MAAM,MAAM,GAAG,QAAQ,CAAC,GAAG,CAAC;QAC5B,QAAQ,CAAC,KAAK,GAAG,UAAU,KAAU;YACnC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;YAEhC,OAAO,QAAQ,CAAC,KAAK,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC;QAC7C,CAAC,CAAC;QACF,QAAQ,CAAC,GAAG,GAAG,UAAU,KAAU;YACjC,IAAI,KAAK,EAAE;gBACT,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;aACjC;YAED,OAAO,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC;QAC3C,CAAC,CAAC;IACJ,CAAC;CACF,CAAA;AA/EY,gBAAgB;IAD5B,IAAA,mBAAU,GAAE;IAIE,WAAA,IAAA,iBAAQ,GAAE,CAAA;qCAAe,0BAAW;GAHtC,gBAAgB,CA+E5B;AA/EY,4CAAgB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nathapp/nestjs-common",
|
|
3
|
-
"version": "1.0.1-rc.
|
|
3
|
+
"version": "1.0.1-rc.24",
|
|
4
4
|
"description": "NestJs Common Library",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "rimraf dist && tsc -p tsconfig.json",
|
|
@@ -25,7 +25,6 @@
|
|
|
25
25
|
"@types/express": "^4.17.17",
|
|
26
26
|
"@types/jest": "^29.5.2",
|
|
27
27
|
"@types/node": "^17.0.22",
|
|
28
|
-
"@types/request-ip": "^0.0.38",
|
|
29
28
|
"@types/supertest": "^2.0.12",
|
|
30
29
|
"@typescript-eslint/eslint-plugin": "^4",
|
|
31
30
|
"@typescript-eslint/parser": "^4",
|
|
@@ -47,8 +46,7 @@
|
|
|
47
46
|
"@nathapp/common": "^0.1.12",
|
|
48
47
|
"body-parser": "^1.20.2",
|
|
49
48
|
"joi": "^17.6.0",
|
|
50
|
-
"nestjs-i18n": "^10.0.0"
|
|
51
|
-
"request-ip": "^3.3.0"
|
|
49
|
+
"nestjs-i18n": "^10.0.0"
|
|
52
50
|
},
|
|
53
51
|
"peerDependencies": {
|
|
54
52
|
"@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0",
|