sm-utility 1.1.0 → 1.2.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/logger/index.js +13 -3
- package/logger/types.d.ts +2 -0
- package/package.json +1 -1
- package/request/index.js +15 -7
package/logger/index.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
|
|
3
|
+
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
|
|
4
|
+
to[j] = from[i];
|
|
5
|
+
return to;
|
|
6
|
+
};
|
|
2
7
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
8
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
9
|
};
|
|
@@ -15,6 +20,10 @@ catch (_a) {
|
|
|
15
20
|
throw Error('Logger configs could not be found on app root\'s directory.');
|
|
16
21
|
}
|
|
17
22
|
function formatReqResLog(info) {
|
|
23
|
+
// in case the info itself is not from a request/response pair
|
|
24
|
+
if (!info.meta || (info === null || info === void 0 ? void 0 : info.meta.req)) {
|
|
25
|
+
return JSON.stringify(info);
|
|
26
|
+
}
|
|
18
27
|
var level = info.level, _a = info.meta, req = _a.req, res = _a.res, responseTime = _a.responseTime;
|
|
19
28
|
var logData = Object.assign({}, { req: req, res: res, responseTime: responseTime + "ms" });
|
|
20
29
|
return level + " - [" + req.method + "] " + req.url + ": " + JSON.stringify(logData);
|
|
@@ -55,8 +64,9 @@ function getSplitedRouteParts(route) {
|
|
|
55
64
|
return filteredParts;
|
|
56
65
|
}
|
|
57
66
|
function createLoggerMiddleware() {
|
|
58
|
-
|
|
59
|
-
express_winston_1.default.
|
|
67
|
+
var _a, _b;
|
|
68
|
+
(_a = express_winston_1.default.requestWhitelist).push.apply(_a, __spreadArray(["body"], configs.requestWhitelist || []));
|
|
69
|
+
(_b = express_winston_1.default.responseWhitelist).push.apply(_b, __spreadArray(["body"], configs.responseWhitelist || []));
|
|
60
70
|
return express_winston_1.default.logger({
|
|
61
71
|
statusLevels: true,
|
|
62
72
|
transports: [
|
|
@@ -64,7 +74,7 @@ function createLoggerMiddleware() {
|
|
|
64
74
|
],
|
|
65
75
|
requestFilter: filterRequestHeaders(configs.confidentialHeaders || []),
|
|
66
76
|
ignoreRoute: routeFilter,
|
|
67
|
-
format: winston_1.default.format.combine(winston_1.default.format.colorize(), winston_1.default.format.printf(formatReqResLog))
|
|
77
|
+
format: winston_1.default.format.combine(winston_1.default.format.colorize(), winston_1.default.format.printf(formatReqResLog)),
|
|
68
78
|
});
|
|
69
79
|
}
|
|
70
80
|
function createErrorLoggerMiddleware() {
|
package/logger/types.d.ts
CHANGED
package/package.json
CHANGED
package/request/index.js
CHANGED
|
@@ -9,11 +9,12 @@ var uuid_1 = require("uuid");
|
|
|
9
9
|
function createApi(config) {
|
|
10
10
|
var axiosApi = axios_1.default.create(config);
|
|
11
11
|
axiosApi.interceptors.request.use(function (request) {
|
|
12
|
-
var
|
|
12
|
+
var data = request.data, params = request.params, method = request.method;
|
|
13
|
+
var fullUrl = getFullUrlFromConfig(request);
|
|
13
14
|
request.meta = request.meta || {};
|
|
14
15
|
request.meta.requestId = uuid_1.v4();
|
|
15
16
|
request.meta.requestStartedAt = new Date().getTime();
|
|
16
|
-
logApiInfo(request.meta.requestId, 'Request', method,
|
|
17
|
+
logApiInfo(request.meta.requestId, 'Request', method, fullUrl, {
|
|
17
18
|
data: data,
|
|
18
19
|
params: params,
|
|
19
20
|
});
|
|
@@ -21,9 +22,10 @@ function createApi(config) {
|
|
|
21
22
|
});
|
|
22
23
|
axiosApi.interceptors.response.use(function (response) {
|
|
23
24
|
var status = response.status, data = response.data, config = response.config;
|
|
24
|
-
var method = config.method,
|
|
25
|
+
var method = config.method, meta = config.meta;
|
|
26
|
+
var fullUrl = getFullUrlFromConfig(config);
|
|
25
27
|
var responseTime = getResponseTimeFromConfig(config);
|
|
26
|
-
logApiInfo(meta === null || meta === void 0 ? void 0 : meta.requestId, 'Response', method,
|
|
28
|
+
logApiInfo(meta === null || meta === void 0 ? void 0 : meta.requestId, 'Response', method, fullUrl, {
|
|
27
29
|
status: status,
|
|
28
30
|
data: data,
|
|
29
31
|
responseTime: responseTime,
|
|
@@ -31,15 +33,16 @@ function createApi(config) {
|
|
|
31
33
|
return response;
|
|
32
34
|
}, function (error) {
|
|
33
35
|
var config = error.config, message = error.message, response = error.response;
|
|
34
|
-
var method = config.method,
|
|
36
|
+
var method = config.method, meta = config.meta;
|
|
37
|
+
var fullUrl = getFullUrlFromConfig(config);
|
|
35
38
|
var responseTime = getResponseTimeFromConfig(config);
|
|
36
|
-
logApiInfo(meta === null || meta === void 0 ? void 0 : meta.requestId, 'Error', method,
|
|
39
|
+
logApiInfo(meta === null || meta === void 0 ? void 0 : meta.requestId, 'Error', method, fullUrl, {
|
|
37
40
|
status: response === null || response === void 0 ? void 0 : response.status,
|
|
38
41
|
message: message,
|
|
39
42
|
data: response === null || response === void 0 ? void 0 : response.data,
|
|
40
43
|
responseTime: responseTime,
|
|
41
44
|
});
|
|
42
|
-
|
|
45
|
+
throw error;
|
|
43
46
|
});
|
|
44
47
|
return axiosApi;
|
|
45
48
|
}
|
|
@@ -57,3 +60,8 @@ function getResponseTimeFromConfig(config) {
|
|
|
57
60
|
}
|
|
58
61
|
return responseTime + ' ms';
|
|
59
62
|
}
|
|
63
|
+
function getFullUrlFromConfig(config) {
|
|
64
|
+
var baseURL = config.baseURL, url = config.url;
|
|
65
|
+
var fullUrl = baseURL && url ? new String().concat(baseURL, url) : '';
|
|
66
|
+
return fullUrl;
|
|
67
|
+
}
|