@tsed/platform-log-middleware 7.0.0-beta.2 → 7.0.0-beta.20
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/lib/cjs/index.js +5 -2
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/middlewares/PlatformLogMiddleware.js +42 -35
- package/lib/cjs/middlewares/PlatformLogMiddleware.js.map +1 -1
- package/lib/cjs/package.json +3 -0
- package/lib/esm/index.js +5 -2
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/middlewares/PlatformLogMiddleware.js +26 -19
- package/lib/esm/middlewares/PlatformLogMiddleware.js.map +1 -1
- package/lib/esm/package.json +3 -0
- package/lib/types/index.d.ts +3 -0
- package/lib/types/middlewares/PlatformLogMiddleware.d.ts +1 -4
- package/package.json +28 -14
- package/readme.md +4 -4
- package/tsconfig.compile.esm.json +2 -12
package/lib/cjs/index.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file Automatically generated by barrelsby.
|
|
4
|
+
*/
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
const tslib_1 = require("tslib");
|
|
4
|
-
|
|
5
|
-
|
|
7
|
+
tslib_1.__exportStar(require("./domain/PlatformLogMiddlewareSettings"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./middlewares/PlatformLogMiddleware"), exports);
|
|
6
9
|
//# sourceMappingURL=index.js.map
|
package/lib/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAEH,iFAAuD;AACvD,8EAAoD","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./domain/PlatformLogMiddlewareSettings\";\nexport * from \"./middlewares/PlatformLogMiddleware\";\n"]}
|
|
@@ -10,11 +10,11 @@ const platform_params_1 = require("@tsed/platform-params");
|
|
|
10
10
|
* @platform
|
|
11
11
|
*/
|
|
12
12
|
let PlatformLogMiddleware = class PlatformLogMiddleware {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
requestFields;
|
|
14
|
+
logRequest;
|
|
15
|
+
logStart;
|
|
16
|
+
logEnd;
|
|
17
|
+
logLevel;
|
|
18
18
|
get settings() {
|
|
19
19
|
return this;
|
|
20
20
|
}
|
|
@@ -24,15 +24,17 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
|
|
|
24
24
|
use(ctx) {
|
|
25
25
|
this.configureRequest(ctx);
|
|
26
26
|
this.onLogStart(ctx);
|
|
27
|
+
ctx.response.onEnd(() => this.onLogEnd(ctx));
|
|
27
28
|
}
|
|
28
29
|
/**
|
|
29
30
|
* The separate onLogStart() function will allow developer to overwrite the initial request log.
|
|
30
31
|
* @param ctx
|
|
31
32
|
*/
|
|
32
33
|
onLogStart(ctx) {
|
|
33
|
-
const {
|
|
34
|
+
const { logRequest, logLevel, logStart } = this.settings;
|
|
35
|
+
ctx.logStarted = true;
|
|
34
36
|
if (logStart) {
|
|
35
|
-
if (debug) {
|
|
37
|
+
if (logLevel === "debug") {
|
|
36
38
|
ctx.logger.debug({
|
|
37
39
|
event: "request.start"
|
|
38
40
|
});
|
|
@@ -48,9 +50,10 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
|
|
|
48
50
|
* Called when the `$onResponse` is called by Ts.ED (through Express.end).
|
|
49
51
|
*/
|
|
50
52
|
onLogEnd(ctx) {
|
|
51
|
-
const {
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
const { logRequest, logEnd, logLevel } = this.settings;
|
|
54
|
+
const started = ctx.logStarted;
|
|
55
|
+
if (logEnd && started) {
|
|
56
|
+
if (logLevel === "debug") {
|
|
54
57
|
ctx.logger.debug({
|
|
55
58
|
event: "request.end",
|
|
56
59
|
status: ctx.response.statusCode,
|
|
@@ -70,8 +73,16 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
|
|
|
70
73
|
* Attach all information that will be necessary to log the request. Attach a new `request.log` object.
|
|
71
74
|
*/
|
|
72
75
|
configureRequest(ctx) {
|
|
73
|
-
ctx.logger.
|
|
74
|
-
|
|
76
|
+
ctx.logger.alterLog((obj, level, withRequest) => {
|
|
77
|
+
switch (level) {
|
|
78
|
+
case "info":
|
|
79
|
+
return { ...this.minimalRequestPicker(ctx), ...obj };
|
|
80
|
+
case "debug":
|
|
81
|
+
return withRequest ? { ...this.requestToObject(ctx), ...obj } : obj;
|
|
82
|
+
default:
|
|
83
|
+
return { ...this.requestToObject(ctx), ...obj };
|
|
84
|
+
}
|
|
85
|
+
});
|
|
75
86
|
}
|
|
76
87
|
/**
|
|
77
88
|
* Return complete request info.
|
|
@@ -83,6 +94,7 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
|
|
|
83
94
|
return {
|
|
84
95
|
method: request.method,
|
|
85
96
|
url: request.url,
|
|
97
|
+
route: request.route,
|
|
86
98
|
headers: request.headers,
|
|
87
99
|
body: request.body,
|
|
88
100
|
query: request.query,
|
|
@@ -103,39 +115,34 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
|
|
|
103
115
|
}, {});
|
|
104
116
|
}
|
|
105
117
|
};
|
|
106
|
-
|
|
107
|
-
(0, di_1.Constant)("logger.requestFields", ["reqId", "method", "url", "duration"]),
|
|
108
|
-
|
|
118
|
+
tslib_1.__decorate([
|
|
119
|
+
(0, di_1.Constant)("logger.requestFields", ["reqId", "method", "url", "duration", "route"]),
|
|
120
|
+
tslib_1.__metadata("design:type", Array)
|
|
109
121
|
], PlatformLogMiddleware.prototype, "requestFields", void 0);
|
|
110
|
-
|
|
122
|
+
tslib_1.__decorate([
|
|
111
123
|
(0, di_1.Constant)("logger.logRequest", true),
|
|
112
|
-
|
|
124
|
+
tslib_1.__metadata("design:type", Boolean)
|
|
113
125
|
], PlatformLogMiddleware.prototype, "logRequest", void 0);
|
|
114
|
-
|
|
126
|
+
tslib_1.__decorate([
|
|
115
127
|
(0, di_1.Constant)("logger.logStart", true),
|
|
116
|
-
|
|
128
|
+
tslib_1.__metadata("design:type", Boolean)
|
|
117
129
|
], PlatformLogMiddleware.prototype, "logStart", void 0);
|
|
118
|
-
|
|
130
|
+
tslib_1.__decorate([
|
|
119
131
|
(0, di_1.Constant)("logger.logEnd", true),
|
|
120
|
-
|
|
132
|
+
tslib_1.__metadata("design:type", Boolean)
|
|
121
133
|
], PlatformLogMiddleware.prototype, "logEnd", void 0);
|
|
122
|
-
|
|
134
|
+
tslib_1.__decorate([
|
|
123
135
|
(0, di_1.Constant)("logger.level"),
|
|
124
|
-
|
|
136
|
+
tslib_1.__metadata("design:type", String)
|
|
125
137
|
], PlatformLogMiddleware.prototype, "logLevel", void 0);
|
|
126
|
-
|
|
127
|
-
(0,
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
(
|
|
131
|
-
(0, tslib_1.__param)(0, (0, platform_params_1.Context)()),
|
|
132
|
-
(0, tslib_1.__metadata)("design:type", Function),
|
|
133
|
-
(0, tslib_1.__metadata)("design:paramtypes", [Object]),
|
|
134
|
-
(0, tslib_1.__metadata)("design:returntype", void 0)
|
|
138
|
+
tslib_1.__decorate([
|
|
139
|
+
tslib_1.__param(0, (0, platform_params_1.Context)()),
|
|
140
|
+
tslib_1.__metadata("design:type", Function),
|
|
141
|
+
tslib_1.__metadata("design:paramtypes", [Object]),
|
|
142
|
+
tslib_1.__metadata("design:returntype", void 0)
|
|
135
143
|
], PlatformLogMiddleware.prototype, "use", null);
|
|
136
|
-
PlatformLogMiddleware =
|
|
137
|
-
(0, platform_middlewares_1.Middleware)()
|
|
138
|
-
(0, tslib_1.__metadata)("design:paramtypes", [])
|
|
144
|
+
PlatformLogMiddleware = tslib_1.__decorate([
|
|
145
|
+
(0, platform_middlewares_1.Middleware)()
|
|
139
146
|
], PlatformLogMiddleware);
|
|
140
147
|
exports.PlatformLogMiddleware = PlatformLogMiddleware;
|
|
141
148
|
//# sourceMappingURL=PlatformLogMiddleware.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformLogMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/PlatformLogMiddleware.ts"],"names":[],"mappings":";;;;AAAA,iCAAkC;AAClC,qEAAyE;AACzE,2DAA8C;AAG9C;;;GAGG;AAEH,IAAa,qBAAqB,GAAlC,MAAa,qBAAqB;
|
|
1
|
+
{"version":3,"file":"PlatformLogMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/PlatformLogMiddleware.ts"],"names":[],"mappings":";;;;AAAA,iCAAkC;AAClC,qEAAyE;AACzE,2DAA8C;AAG9C;;;GAGG;AAEH,IAAa,qBAAqB,GAAlC,MAAa,qBAAqB;IAEtB,aAAa,CAAsB;IAGnC,UAAU,CAAU;IAGpB,QAAQ,CAAU;IAGlB,MAAM,CAAU;IAGhB,QAAQ,CAAS;IAE3B,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;OAEG;IACI,GAAG,CAAY,GAAY;QAChC,IAAI,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC;QAC3B,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;QAErB,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;IAC/C,CAAC;IAED;;;OAGG;IACO,UAAU,CAAC,GAAY;QAC/B,MAAM,EAAC,UAAU,EAAE,QAAQ,EAAE,QAAQ,EAAC,GAAG,IAAI,CAAC,QAAQ,CAAC;QAEvD,GAAG,CAAC,UAAU,GAAG,IAAI,CAAC;QAEtB,IAAI,QAAQ,EAAE;YACZ,IAAI,QAAQ,KAAK,OAAO,EAAE;gBACxB,GAAG,CAAC,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,eAAe,EAAE,IAAI,CAAC;;qDACN;AAG1B;IADC,IAAA,aAAQ,EAAC,cAAc,CAAC;;uDACE;AAS3B;IAAY,mBAAA,IAAA,yBAAO,GAAE,CAAA;;;;gDAKpB;AA5BU,qBAAqB;IADjC,IAAA,iCAAU,GAAE;GACA,qBAAqB,CA+HjC;AA/HY,sDAAqB","sourcesContent":["import {Constant} from \"@tsed/di\";\nimport {Middleware, MiddlewareMethods} from \"@tsed/platform-middlewares\";\nimport {Context} from \"@tsed/platform-params\";\nimport type {LoggerRequestFields} from \"../domain/PlatformLogMiddlewareSettings\";\n\n/**\n * @middleware\n * @platform\n */\n@Middleware()\nexport class PlatformLogMiddleware implements MiddlewareMethods {\n @Constant(\"logger.requestFields\", [\"reqId\", \"method\", \"url\", \"duration\", \"route\"])\n protected requestFields: LoggerRequestFields;\n\n @Constant(\"logger.logRequest\", true)\n protected logRequest: boolean;\n\n @Constant(\"logger.logStart\", true)\n protected logStart: boolean;\n\n @Constant(\"logger.logEnd\", true)\n protected logEnd: boolean;\n\n @Constant(\"logger.level\")\n protected logLevel: string;\n\n get settings() {\n return this;\n }\n\n /**\n * Handle the request.\n */\n public use(@Context() ctx: Context): void {\n this.configureRequest(ctx);\n this.onLogStart(ctx);\n\n ctx.response.onEnd(() => this.onLogEnd(ctx));\n }\n\n /**\n * The separate onLogStart() function will allow developer to overwrite the initial request log.\n * @param ctx\n */\n protected onLogStart(ctx: Context) {\n const {logRequest, logLevel, logStart} = this.settings;\n\n ctx.logStarted = true;\n\n if (logStart) {\n if (logLevel === \"debug\") {\n ctx.logger.debug({\n event: \"request.start\"\n });\n } else if (logRequest) {\n ctx.logger.info({\n event: \"request.start\"\n });\n }\n }\n }\n\n /**\n * Called when the `$onResponse` is called by Ts.ED (through Express.end).\n */\n onLogEnd(ctx: Context) {\n const {logRequest, logEnd, logLevel} = this.settings;\n const started = ctx.logStarted;\n\n if (logEnd && started) {\n if (logLevel === \"debug\") {\n ctx.logger.debug({\n event: \"request.end\",\n status: ctx.response.statusCode,\n data: ctx.data\n });\n } else if (logRequest) {\n ctx.logger.info({\n event: \"request.end\",\n status: ctx.response.statusCode\n });\n }\n }\n\n ctx.logger.flush();\n }\n\n /**\n * Attach all information that will be necessary to log the request. Attach a new `request.log` object.\n */\n protected configureRequest(ctx: Context) {\n ctx.logger.alterLog((obj: any, level, withRequest) => {\n switch (level) {\n case \"info\":\n return {...this.minimalRequestPicker(ctx), ...obj};\n case \"debug\":\n return withRequest ? {...this.requestToObject(ctx), ...obj} : obj;\n default:\n return {...this.requestToObject(ctx), ...obj};\n }\n });\n }\n\n /**\n * Return complete request info.\n * @returns {Object}\n * @param ctx\n */\n protected requestToObject(ctx: Context): any {\n const {request} = ctx;\n\n return {\n method: request.method,\n url: request.url,\n route: request.route,\n headers: request.headers,\n body: request.body,\n query: request.query,\n params: request.params\n };\n }\n\n /**\n * Return a filtered request from global configuration.\n * @returns {Object}\n * @param ctx\n */\n protected minimalRequestPicker(ctx: Context): any {\n const {requestFields} = this;\n const info = this.requestToObject(ctx);\n\n return requestFields!.reduce((acc: any, key: string) => {\n acc[key] = info[key];\n\n return acc;\n }, {});\n }\n}\n"]}
|
package/lib/esm/index.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @file Automatically generated by barrelsby.
|
|
3
|
+
*/
|
|
4
|
+
export * from "./domain/PlatformLogMiddlewareSettings.js";
|
|
5
|
+
export * from "./middlewares/PlatformLogMiddleware.js";
|
|
3
6
|
//# sourceMappingURL=index.js.map
|
package/lib/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wCAAwC,CAAC;AACvD,cAAc,qCAAqC,CAAC","sourcesContent":["
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,wCAAwC,CAAC;AACvD,cAAc,qCAAqC,CAAC","sourcesContent":["/**\n * @file Automatically generated by barrelsby.\n */\n\nexport * from \"./domain/PlatformLogMiddlewareSettings\";\nexport * from \"./middlewares/PlatformLogMiddleware\";\n"]}
|
|
@@ -7,11 +7,11 @@ import { Context } from "@tsed/platform-params";
|
|
|
7
7
|
* @platform
|
|
8
8
|
*/
|
|
9
9
|
let PlatformLogMiddleware = class PlatformLogMiddleware {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
requestFields;
|
|
11
|
+
logRequest;
|
|
12
|
+
logStart;
|
|
13
|
+
logEnd;
|
|
14
|
+
logLevel;
|
|
15
15
|
get settings() {
|
|
16
16
|
return this;
|
|
17
17
|
}
|
|
@@ -21,15 +21,17 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
|
|
|
21
21
|
use(ctx) {
|
|
22
22
|
this.configureRequest(ctx);
|
|
23
23
|
this.onLogStart(ctx);
|
|
24
|
+
ctx.response.onEnd(() => this.onLogEnd(ctx));
|
|
24
25
|
}
|
|
25
26
|
/**
|
|
26
27
|
* The separate onLogStart() function will allow developer to overwrite the initial request log.
|
|
27
28
|
* @param ctx
|
|
28
29
|
*/
|
|
29
30
|
onLogStart(ctx) {
|
|
30
|
-
const {
|
|
31
|
+
const { logRequest, logLevel, logStart } = this.settings;
|
|
32
|
+
ctx.logStarted = true;
|
|
31
33
|
if (logStart) {
|
|
32
|
-
if (debug) {
|
|
34
|
+
if (logLevel === "debug") {
|
|
33
35
|
ctx.logger.debug({
|
|
34
36
|
event: "request.start"
|
|
35
37
|
});
|
|
@@ -45,9 +47,10 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
|
|
|
45
47
|
* Called when the `$onResponse` is called by Ts.ED (through Express.end).
|
|
46
48
|
*/
|
|
47
49
|
onLogEnd(ctx) {
|
|
48
|
-
const {
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
const { logRequest, logEnd, logLevel } = this.settings;
|
|
51
|
+
const started = ctx.logStarted;
|
|
52
|
+
if (logEnd && started) {
|
|
53
|
+
if (logLevel === "debug") {
|
|
51
54
|
ctx.logger.debug({
|
|
52
55
|
event: "request.end",
|
|
53
56
|
status: ctx.response.statusCode,
|
|
@@ -67,8 +70,16 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
|
|
|
67
70
|
* Attach all information that will be necessary to log the request. Attach a new `request.log` object.
|
|
68
71
|
*/
|
|
69
72
|
configureRequest(ctx) {
|
|
70
|
-
ctx.logger.
|
|
71
|
-
|
|
73
|
+
ctx.logger.alterLog((obj, level, withRequest) => {
|
|
74
|
+
switch (level) {
|
|
75
|
+
case "info":
|
|
76
|
+
return { ...this.minimalRequestPicker(ctx), ...obj };
|
|
77
|
+
case "debug":
|
|
78
|
+
return withRequest ? { ...this.requestToObject(ctx), ...obj } : obj;
|
|
79
|
+
default:
|
|
80
|
+
return { ...this.requestToObject(ctx), ...obj };
|
|
81
|
+
}
|
|
82
|
+
});
|
|
72
83
|
}
|
|
73
84
|
/**
|
|
74
85
|
* Return complete request info.
|
|
@@ -80,6 +91,7 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
|
|
|
80
91
|
return {
|
|
81
92
|
method: request.method,
|
|
82
93
|
url: request.url,
|
|
94
|
+
route: request.route,
|
|
83
95
|
headers: request.headers,
|
|
84
96
|
body: request.body,
|
|
85
97
|
query: request.query,
|
|
@@ -101,7 +113,7 @@ let PlatformLogMiddleware = class PlatformLogMiddleware {
|
|
|
101
113
|
}
|
|
102
114
|
};
|
|
103
115
|
__decorate([
|
|
104
|
-
Constant("logger.requestFields", ["reqId", "method", "url", "duration"]),
|
|
116
|
+
Constant("logger.requestFields", ["reqId", "method", "url", "duration", "route"]),
|
|
105
117
|
__metadata("design:type", Array)
|
|
106
118
|
], PlatformLogMiddleware.prototype, "requestFields", void 0);
|
|
107
119
|
__decorate([
|
|
@@ -120,10 +132,6 @@ __decorate([
|
|
|
120
132
|
Constant("logger.level"),
|
|
121
133
|
__metadata("design:type", String)
|
|
122
134
|
], PlatformLogMiddleware.prototype, "logLevel", void 0);
|
|
123
|
-
__decorate([
|
|
124
|
-
Constant("debug"),
|
|
125
|
-
__metadata("design:type", Boolean)
|
|
126
|
-
], PlatformLogMiddleware.prototype, "debug", void 0);
|
|
127
135
|
__decorate([
|
|
128
136
|
__param(0, Context()),
|
|
129
137
|
__metadata("design:type", Function),
|
|
@@ -131,8 +139,7 @@ __decorate([
|
|
|
131
139
|
__metadata("design:returntype", void 0)
|
|
132
140
|
], PlatformLogMiddleware.prototype, "use", null);
|
|
133
141
|
PlatformLogMiddleware = __decorate([
|
|
134
|
-
Middleware()
|
|
135
|
-
__metadata("design:paramtypes", [])
|
|
142
|
+
Middleware()
|
|
136
143
|
], PlatformLogMiddleware);
|
|
137
144
|
export { PlatformLogMiddleware };
|
|
138
145
|
//# sourceMappingURL=PlatformLogMiddleware.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PlatformLogMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/PlatformLogMiddleware.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,UAAU,CAAC;AAClC,OAAO,EAAC,UAAU,EAAoB,MAAM,4BAA4B,CAAC;AACzE,OAAO,EAAC,OAAO,EAAC,MAAM,uBAAuB,CAAC;AAG9C;;;GAGG;AAEH,IAAa,qBAAqB,GAAlC,MAAa,qBAAqB;
|
|
1
|
+
{"version":3,"file":"PlatformLogMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/PlatformLogMiddleware.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,UAAU,CAAC;AAClC,OAAO,EAAC,UAAU,EAAoB,MAAM,4BAA4B,CAAC;AACzE,OAAO,EAAC,OAAO,EAAC,MAAM,uBAAuB,CAAC;AAG9C;;;GAGG;AAEH,IAAa,qBAAqB,GAA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CAAC;IACJ,CAAC;IAED;;;;OAIG;IACO,oBAAoB,CAAC,GAAY;QACzC,MAAM,EAAC,aAAa,EAAC,GAAG,IAAI,CAAC;QAC7B,MAAM,IAAI,GAAG,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC;QAEvC,OAAO,aAAc,CAAC,MAAM,CAAC,CAAC,GAAQ,EAAE,GAAW,EAAE,EAAE;YACrD,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC;YAErB,OAAO,GAAG,CAAC;QACb,CAAC,EAAE,EAAE,CAAC,CAAC;IACT,CAAC;CACF,CAAA;AA7HC;IADC,QAAQ,CAAC,sBAAsB,EAAE,CAAC,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;;4DACrC;AAG7C;IADC,QAAQ,CAAC,mBAAmB,EAAE,IAAI,CAAC;;yDACN;AAG9B;IADC,QAAQ,CAAC,iBAAiB,EAAE,IAAI,CAAC;;uDACN;AAG5B;IADC,QAAQ,CAAC,eAAe,EAAE,IAAI,CAAC;;qDACN;AAG1B;IADC,QAAQ,CAAC,cAAc,CAAC;;uDACE;AAS3B;IAAY,WAAA,OAAO,EAAE,CAAA;;;;gDAKpB;AA5BU,qBAAqB;IADjC,UAAU,EAAE;GACA,qBAAqB,CA+HjC;SA/HY,qBAAqB","sourcesContent":["import {Constant} from \"@tsed/di\";\nimport {Middleware, MiddlewareMethods} from \"@tsed/platform-middlewares\";\nimport {Context} from \"@tsed/platform-params\";\nimport type {LoggerRequestFields} from \"../domain/PlatformLogMiddlewareSettings\";\n\n/**\n * @middleware\n * @platform\n */\n@Middleware()\nexport class PlatformLogMiddleware implements MiddlewareMethods {\n @Constant(\"logger.requestFields\", [\"reqId\", \"method\", \"url\", \"duration\", \"route\"])\n protected requestFields: LoggerRequestFields;\n\n @Constant(\"logger.logRequest\", true)\n protected logRequest: boolean;\n\n @Constant(\"logger.logStart\", true)\n protected logStart: boolean;\n\n @Constant(\"logger.logEnd\", true)\n protected logEnd: boolean;\n\n @Constant(\"logger.level\")\n protected logLevel: string;\n\n get settings() {\n return this;\n }\n\n /**\n * Handle the request.\n */\n public use(@Context() ctx: Context): void {\n this.configureRequest(ctx);\n this.onLogStart(ctx);\n\n ctx.response.onEnd(() => this.onLogEnd(ctx));\n }\n\n /**\n * The separate onLogStart() function will allow developer to overwrite the initial request log.\n * @param ctx\n */\n protected onLogStart(ctx: Context) {\n const {logRequest, logLevel, logStart} = this.settings;\n\n ctx.logStarted = true;\n\n if (logStart) {\n if (logLevel === \"debug\") {\n ctx.logger.debug({\n event: \"request.start\"\n });\n } else if (logRequest) {\n ctx.logger.info({\n event: \"request.start\"\n });\n }\n }\n }\n\n /**\n * Called when the `$onResponse` is called by Ts.ED (through Express.end).\n */\n onLogEnd(ctx: Context) {\n const {logRequest, logEnd, logLevel} = this.settings;\n const started = ctx.logStarted;\n\n if (logEnd && started) {\n if (logLevel === \"debug\") {\n ctx.logger.debug({\n event: \"request.end\",\n status: ctx.response.statusCode,\n data: ctx.data\n });\n } else if (logRequest) {\n ctx.logger.info({\n event: \"request.end\",\n status: ctx.response.statusCode\n });\n }\n }\n\n ctx.logger.flush();\n }\n\n /**\n * Attach all information that will be necessary to log the request. Attach a new `request.log` object.\n */\n protected configureRequest(ctx: Context) {\n ctx.logger.alterLog((obj: any, level, withRequest) => {\n switch (level) {\n case \"info\":\n return {...this.minimalRequestPicker(ctx), ...obj};\n case \"debug\":\n return withRequest ? {...this.requestToObject(ctx), ...obj} : obj;\n default:\n return {...this.requestToObject(ctx), ...obj};\n }\n });\n }\n\n /**\n * Return complete request info.\n * @returns {Object}\n * @param ctx\n */\n protected requestToObject(ctx: Context): any {\n const {request} = ctx;\n\n return {\n method: request.method,\n url: request.url,\n route: request.route,\n headers: request.headers,\n body: request.body,\n query: request.query,\n params: request.params\n };\n }\n\n /**\n * Return a filtered request from global configuration.\n * @returns {Object}\n * @param ctx\n */\n protected minimalRequestPicker(ctx: Context): any {\n const {requestFields} = this;\n const info = this.requestToObject(ctx);\n\n return requestFields!.reduce((acc: any, key: string) => {\n acc[key] = info[key];\n\n return acc;\n }, {});\n }\n}\n"]}
|
package/lib/types/index.d.ts
CHANGED
|
@@ -6,14 +6,11 @@ import type { LoggerRequestFields } from "../domain/PlatformLogMiddlewareSetting
|
|
|
6
6
|
* @platform
|
|
7
7
|
*/
|
|
8
8
|
export declare class PlatformLogMiddleware implements MiddlewareMethods {
|
|
9
|
-
$onResponse: any;
|
|
10
9
|
protected requestFields: LoggerRequestFields;
|
|
11
10
|
protected logRequest: boolean;
|
|
12
11
|
protected logStart: boolean;
|
|
13
12
|
protected logEnd: boolean;
|
|
14
13
|
protected logLevel: string;
|
|
15
|
-
protected debug: boolean;
|
|
16
|
-
constructor();
|
|
17
14
|
get settings(): this;
|
|
18
15
|
/**
|
|
19
16
|
* Handle the request.
|
|
@@ -27,7 +24,7 @@ export declare class PlatformLogMiddleware implements MiddlewareMethods {
|
|
|
27
24
|
/**
|
|
28
25
|
* Called when the `$onResponse` is called by Ts.ED (through Express.end).
|
|
29
26
|
*/
|
|
30
|
-
|
|
27
|
+
onLogEnd(ctx: Context): void;
|
|
31
28
|
/**
|
|
32
29
|
* Attach all information that will be necessary to log the request. Attach a new `request.log` object.
|
|
33
30
|
*/
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/platform-log-middleware",
|
|
3
|
-
"version": "7.0.0-beta.
|
|
3
|
+
"version": "7.0.0-beta.20",
|
|
4
4
|
"description": "Log request middleware module for Ts.ED Framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
7
|
"main": "./lib/cjs/index.js",
|
|
8
|
+
"module": "./lib/esm/index.js",
|
|
8
9
|
"typings": "./lib/types/index.d.ts",
|
|
9
10
|
"exports": {
|
|
10
11
|
"types": "./lib/types/index.d.ts",
|
|
@@ -13,31 +14,44 @@
|
|
|
13
14
|
"default": "./lib/esm/index.js"
|
|
14
15
|
},
|
|
15
16
|
"scripts": {
|
|
16
|
-
"build": "yarn run build:esm && yarn run build:cjs",
|
|
17
|
+
"build": "yarn barrels && yarn run build:esm && yarn run build:cjs",
|
|
17
18
|
"build:cjs": "tsc --build tsconfig.compile.json",
|
|
18
19
|
"build:esm": "tsc --build tsconfig.compile.esm.json",
|
|
19
|
-
"
|
|
20
|
+
"barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\"",
|
|
21
|
+
"test": "cross-env NODE_ENV=test yarn jest --max-workers=2"
|
|
20
22
|
},
|
|
21
23
|
"dependencies": {
|
|
22
|
-
"tslib": "2.
|
|
24
|
+
"tslib": "2.4.0"
|
|
23
25
|
},
|
|
24
26
|
"devDependencies": {
|
|
25
|
-
"@tsed/
|
|
26
|
-
"@tsed/
|
|
27
|
-
"@tsed/platform-
|
|
28
|
-
"@tsed/platform-params": "7.0.0-beta.2"
|
|
27
|
+
"@tsed/di": "7.0.0-beta.20",
|
|
28
|
+
"@tsed/platform-middlewares": "7.0.0-beta.20",
|
|
29
|
+
"@tsed/platform-params": "7.0.0-beta.20"
|
|
29
30
|
},
|
|
30
31
|
"peerDependencies": {
|
|
31
|
-
"@tsed/
|
|
32
|
-
"@tsed/
|
|
33
|
-
"@tsed/platform-
|
|
34
|
-
|
|
32
|
+
"@tsed/di": "^7.0.0-beta.20",
|
|
33
|
+
"@tsed/platform-middlewares": "^7.0.0-beta.20",
|
|
34
|
+
"@tsed/platform-params": "^7.0.0-beta.20"
|
|
35
|
+
},
|
|
36
|
+
"peerDependenciesMeta": {
|
|
37
|
+
"@tsed/di": {
|
|
38
|
+
"optional": false
|
|
39
|
+
},
|
|
40
|
+
"@tsed/platform-middlewares": {
|
|
41
|
+
"optional": false
|
|
42
|
+
},
|
|
43
|
+
"@tsed/platform-params": {
|
|
44
|
+
"optional": false
|
|
45
|
+
}
|
|
35
46
|
},
|
|
36
47
|
"repository": "https://github.com/tsedio/tsed",
|
|
37
48
|
"bugs": {
|
|
38
49
|
"url": "https://github.com/tsedio/tsed/issues"
|
|
39
50
|
},
|
|
40
|
-
"homepage": "https://github.com/tsedio/tsed/tree/
|
|
51
|
+
"homepage": "https://github.com/tsedio/tsed/tree/beta/packages/platform/platform-log-middleware",
|
|
41
52
|
"author": "Romain Lenzotti",
|
|
42
|
-
"license": "MIT"
|
|
53
|
+
"license": "MIT",
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"tag": "beta"
|
|
56
|
+
}
|
|
43
57
|
}
|
package/readme.md
CHANGED
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
[](https://badge.fury.io/js/%40tsed%2Fcommon)
|
|
12
12
|
[](https://github.com/semantic-release/semantic-release)
|
|
13
13
|
[](https://github.com/prettier/prettier)
|
|
14
|
-
[](https://github.com/sponsors/romakita)
|
|
15
|
+
[](https://opencollective.com/tsed)
|
|
15
16
|
|
|
16
17
|
</div>
|
|
17
18
|
|
|
@@ -36,18 +37,17 @@ npm install --save @tsed/platform-params
|
|
|
36
37
|
```
|
|
37
38
|
|
|
38
39
|
## Contributors
|
|
40
|
+
|
|
39
41
|
Please read [contributing guidelines here](https://tsed.io/CONTRIBUTING.html).
|
|
40
42
|
|
|
41
43
|
<a href="https://github.com/tsedio/ts-express-decorators/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
|
|
42
44
|
|
|
43
|
-
|
|
44
45
|
## Backers
|
|
45
46
|
|
|
46
47
|
Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]
|
|
47
48
|
|
|
48
49
|
<a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/tiers/backer.svg?width=890"></a>
|
|
49
50
|
|
|
50
|
-
|
|
51
51
|
## Sponsors
|
|
52
52
|
|
|
53
53
|
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]
|
|
@@ -56,7 +56,7 @@ Support this project by becoming a sponsor. Your logo will show up here with a l
|
|
|
56
56
|
|
|
57
57
|
The MIT License (MIT)
|
|
58
58
|
|
|
59
|
-
Copyright (c) 2016 -
|
|
59
|
+
Copyright (c) 2016 - 2022 Romain Lenzotti
|
|
60
60
|
|
|
61
61
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
62
62
|
|
|
@@ -8,16 +8,6 @@
|
|
|
8
8
|
"declaration": true,
|
|
9
9
|
"declarationDir": "./lib/types"
|
|
10
10
|
},
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
"test",
|
|
14
|
-
"lib",
|
|
15
|
-
"benchmark",
|
|
16
|
-
"coverage",
|
|
17
|
-
"spec",
|
|
18
|
-
"**/*.benchmark.ts",
|
|
19
|
-
"**/*.spec.ts",
|
|
20
|
-
"keys",
|
|
21
|
-
"jest.config.js"
|
|
22
|
-
]
|
|
11
|
+
"include": ["src"],
|
|
12
|
+
"exclude": ["node_modules", "test", "lib", "benchmark", "coverage", "spec", "**/*.benchmark.ts", "**/*.spec.ts", "keys", "jest.config.js"]
|
|
23
13
|
}
|