@tsed/platform-log-middleware 6.88.1 → 6.89.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/lib/{domain → cjs/domain}/PlatformLogMiddlewareSettings.d.ts +0 -0
- package/lib/cjs/domain/PlatformLogMiddlewareSettings.js +3 -0
- package/lib/cjs/domain/PlatformLogMiddlewareSettings.js.map +1 -0
- package/lib/{index.d.ts → cjs/index.d.ts} +0 -0
- package/lib/cjs/index.js +6 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/{middlewares → cjs/middlewares}/PlatformLogMiddleware.d.ts +0 -0
- package/lib/cjs/middlewares/PlatformLogMiddleware.js +141 -0
- package/lib/cjs/middlewares/PlatformLogMiddleware.js.map +1 -0
- package/lib/esm/domain/PlatformLogMiddlewareSettings.js +2 -0
- package/lib/esm/domain/PlatformLogMiddlewareSettings.js.map +1 -0
- package/lib/esm/index.js +3 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/middlewares/PlatformLogMiddleware.js +138 -0
- package/lib/esm/middlewares/PlatformLogMiddleware.js.map +1 -0
- package/lib/types/domain/PlatformLogMiddlewareSettings.d.ts +25 -0
- package/lib/types/index.d.ts +2 -0
- package/lib/types/middlewares/PlatformLogMiddleware.d.ts +47 -0
- package/package.json +18 -14
- package/tsconfig.compile.esm.json +23 -0
- package/lib/index.js +0 -167
- package/lib/index.js.map +0 -1
- package/lib/index.modern.js +0 -169
- package/lib/index.modern.js.map +0 -1
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlatformLogMiddlewareSettings.js","sourceRoot":"","sources":["../../../src/domain/PlatformLogMiddlewareSettings.ts"],"names":[],"mappings":"","sourcesContent":["export type LoggerRequestFields = (\"reqId\" | \"method\" | \"url\" | \"headers\" | \"body\" | \"query\" | \"params\" | \"duration\" | string)[];\n\nexport interface PlatformLogMiddlewareSettings {\n /**\n * Fields displayed when a request is logged. Possible values: `reqId`, `method`, `url`, `headers`, `body`, `query`,`params`, `duration`.\n */\n requestFields?: LoggerRequestFields;\n /**\n * Log all incoming request. By default is true and print the configured `logger.requestFields`.\n */\n logRequest?: boolean;\n /**\n * Log start of all incoming request. By default is true\n */\n logStart?: boolean;\n /**\n * Log end of all incoming request. By default is true\n */\n logEnd?: boolean;\n}\n\ndeclare global {\n namespace TsED {\n interface LoggerConfiguration extends Partial<PlatformLogMiddlewareSettings> {}\n }\n}\n"]}
|
|
File without changes
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
(0, tslib_1.__exportStar)(require("./domain/PlatformLogMiddlewareSettings"), exports);
|
|
5
|
+
(0, tslib_1.__exportStar)(require("./middlewares/PlatformLogMiddleware"), exports);
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,sFAAuD;AACvD,mFAAoD","sourcesContent":["export * from \"./domain/PlatformLogMiddlewareSettings\";\nexport * from \"./middlewares/PlatformLogMiddleware\";\n"]}
|
|
File without changes
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PlatformLogMiddleware = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const di_1 = require("@tsed/di");
|
|
6
|
+
const platform_middlewares_1 = require("@tsed/platform-middlewares");
|
|
7
|
+
const platform_params_1 = require("@tsed/platform-params");
|
|
8
|
+
/**
|
|
9
|
+
* @middleware
|
|
10
|
+
* @platform
|
|
11
|
+
*/
|
|
12
|
+
let PlatformLogMiddleware = class PlatformLogMiddleware {
|
|
13
|
+
constructor() {
|
|
14
|
+
if (this.logLevel !== "off") {
|
|
15
|
+
this.$onResponse = this.onLogEnd.bind(this);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
get settings() {
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Handle the request.
|
|
23
|
+
*/
|
|
24
|
+
use(ctx) {
|
|
25
|
+
this.configureRequest(ctx);
|
|
26
|
+
this.onLogStart(ctx);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The separate onLogStart() function will allow developer to overwrite the initial request log.
|
|
30
|
+
* @param ctx
|
|
31
|
+
*/
|
|
32
|
+
onLogStart(ctx) {
|
|
33
|
+
const { debug, logRequest, logStart } = this.settings;
|
|
34
|
+
if (logStart) {
|
|
35
|
+
if (debug) {
|
|
36
|
+
ctx.logger.debug({
|
|
37
|
+
event: "request.start"
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
else if (logRequest) {
|
|
41
|
+
ctx.logger.info({
|
|
42
|
+
event: "request.start"
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Called when the `$onResponse` is called by Ts.ED (through Express.end).
|
|
49
|
+
*/
|
|
50
|
+
onLogEnd(ctx) {
|
|
51
|
+
const { debug, logRequest, logEnd } = this.settings;
|
|
52
|
+
if (logEnd) {
|
|
53
|
+
if (debug) {
|
|
54
|
+
ctx.logger.debug({
|
|
55
|
+
event: "request.end",
|
|
56
|
+
status: ctx.response.statusCode,
|
|
57
|
+
data: ctx.data
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
else if (logRequest) {
|
|
61
|
+
ctx.logger.info({
|
|
62
|
+
event: "request.end",
|
|
63
|
+
status: ctx.response.statusCode
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
ctx.logger.flush();
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Attach all information that will be necessary to log the request. Attach a new `request.log` object.
|
|
71
|
+
*/
|
|
72
|
+
configureRequest(ctx) {
|
|
73
|
+
ctx.logger.minimalRequestPicker = (obj) => ({ ...this.minimalRequestPicker(ctx), ...obj });
|
|
74
|
+
ctx.logger.completeRequestPicker = (obj) => ({ ...this.requestToObject(ctx), ...obj });
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Return complete request info.
|
|
78
|
+
* @returns {Object}
|
|
79
|
+
* @param ctx
|
|
80
|
+
*/
|
|
81
|
+
requestToObject(ctx) {
|
|
82
|
+
const { request } = ctx;
|
|
83
|
+
return {
|
|
84
|
+
method: request.method,
|
|
85
|
+
url: request.url,
|
|
86
|
+
headers: request.headers,
|
|
87
|
+
body: request.body,
|
|
88
|
+
query: request.query,
|
|
89
|
+
params: request.params
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Return a filtered request from global configuration.
|
|
94
|
+
* @returns {Object}
|
|
95
|
+
* @param ctx
|
|
96
|
+
*/
|
|
97
|
+
minimalRequestPicker(ctx) {
|
|
98
|
+
const { requestFields } = this;
|
|
99
|
+
const info = this.requestToObject(ctx);
|
|
100
|
+
return requestFields.reduce((acc, key) => {
|
|
101
|
+
acc[key] = info[key];
|
|
102
|
+
return acc;
|
|
103
|
+
}, {});
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
(0, tslib_1.__decorate)([
|
|
107
|
+
(0, di_1.Constant)("logger.requestFields", ["reqId", "method", "url", "duration"]),
|
|
108
|
+
(0, tslib_1.__metadata)("design:type", Array)
|
|
109
|
+
], PlatformLogMiddleware.prototype, "requestFields", void 0);
|
|
110
|
+
(0, tslib_1.__decorate)([
|
|
111
|
+
(0, di_1.Constant)("logger.logRequest", true),
|
|
112
|
+
(0, tslib_1.__metadata)("design:type", Boolean)
|
|
113
|
+
], PlatformLogMiddleware.prototype, "logRequest", void 0);
|
|
114
|
+
(0, tslib_1.__decorate)([
|
|
115
|
+
(0, di_1.Constant)("logger.logStart", true),
|
|
116
|
+
(0, tslib_1.__metadata)("design:type", Boolean)
|
|
117
|
+
], PlatformLogMiddleware.prototype, "logStart", void 0);
|
|
118
|
+
(0, tslib_1.__decorate)([
|
|
119
|
+
(0, di_1.Constant)("logger.logEnd", true),
|
|
120
|
+
(0, tslib_1.__metadata)("design:type", Boolean)
|
|
121
|
+
], PlatformLogMiddleware.prototype, "logEnd", void 0);
|
|
122
|
+
(0, tslib_1.__decorate)([
|
|
123
|
+
(0, di_1.Constant)("logger.level"),
|
|
124
|
+
(0, tslib_1.__metadata)("design:type", String)
|
|
125
|
+
], PlatformLogMiddleware.prototype, "logLevel", void 0);
|
|
126
|
+
(0, tslib_1.__decorate)([
|
|
127
|
+
(0, di_1.Constant)("debug"),
|
|
128
|
+
(0, tslib_1.__metadata)("design:type", Boolean)
|
|
129
|
+
], PlatformLogMiddleware.prototype, "debug", void 0);
|
|
130
|
+
(0, tslib_1.__decorate)([
|
|
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)
|
|
135
|
+
], PlatformLogMiddleware.prototype, "use", null);
|
|
136
|
+
PlatformLogMiddleware = (0, tslib_1.__decorate)([
|
|
137
|
+
(0, platform_middlewares_1.Middleware)(),
|
|
138
|
+
(0, tslib_1.__metadata)("design:paramtypes", [])
|
|
139
|
+
], PlatformLogMiddleware);
|
|
140
|
+
exports.PlatformLogMiddleware = PlatformLogMiddleware;
|
|
141
|
+
//# sourceMappingURL=PlatformLogMiddleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlatformLogMiddleware.js","sourceRoot":"","sources":["../../../src/middlewares/PlatformLogMiddleware.ts"],"names":[],"mappings":";;;;AAAA,iCAAkC;AAClC,qEAAyE;AACzE,2DAA8C;AAG9C;;;GA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;IACT,CAAC;CACF,CAAA;AAxHC;IADC,IAAA,aAAQ,EAAC,sBAAsB,EAAE,CAAC,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC;;4DAC5B;AAG7C;IADC,IAAA,aAAQ,EAAC,mBAAmB,EAAE,IAAI,CAAC;;yDACN;AAG9B;IADC,IAAA,aAAQ,EAAC,iBAAiB,EAAE,IAAI,CAAC;;uDACN;AAG5B;IADC,IAAA,aAAQ,EAAC,eAAe,EAAE,IAAI,CAAC;;qDACN;AAG1B;IADC,IAAA,aAAQ,EAAC,cAAc,CAAC;;uDACE;AAG3B;IADC,IAAA,aAAQ,EAAC,OAAO,CAAC;;oDACO;AAezB;IAAY,wBAAA,IAAA,yBAAO,GAAE,CAAA;;;;gDAGpB;AArCU,qBAAqB;IADjC,IAAA,iCAAU,GAAE;;GACA,qBAAqB,CA4HjC;AA5HY,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 $onResponse: any;\n\n @Constant(\"logger.requestFields\", [\"reqId\", \"method\", \"url\", \"duration\"])\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 @Constant(\"debug\")\n protected debug: boolean;\n\n constructor() {\n if (this.logLevel !== \"off\") {\n this.$onResponse = this.onLogEnd.bind(this);\n }\n }\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\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 {debug, logRequest, logStart} = this.settings;\n\n if (logStart) {\n if (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 protected onLogEnd(ctx: Context) {\n const {debug, logRequest, logEnd} = this.settings;\n\n if (logEnd) {\n if (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.minimalRequestPicker = (obj: any) => ({...this.minimalRequestPicker(ctx), ...obj});\n ctx.logger.completeRequestPicker = (obj: any) => ({...this.requestToObject(ctx), ...obj});\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 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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PlatformLogMiddlewareSettings.js","sourceRoot":"","sources":["../../../src/domain/PlatformLogMiddlewareSettings.ts"],"names":[],"mappings":"","sourcesContent":["export type LoggerRequestFields = (\"reqId\" | \"method\" | \"url\" | \"headers\" | \"body\" | \"query\" | \"params\" | \"duration\" | string)[];\n\nexport interface PlatformLogMiddlewareSettings {\n /**\n * Fields displayed when a request is logged. Possible values: `reqId`, `method`, `url`, `headers`, `body`, `query`,`params`, `duration`.\n */\n requestFields?: LoggerRequestFields;\n /**\n * Log all incoming request. By default is true and print the configured `logger.requestFields`.\n */\n logRequest?: boolean;\n /**\n * Log start of all incoming request. By default is true\n */\n logStart?: boolean;\n /**\n * Log end of all incoming request. By default is true\n */\n logEnd?: boolean;\n}\n\ndeclare global {\n namespace TsED {\n interface LoggerConfiguration extends Partial<PlatformLogMiddlewareSettings> {}\n }\n}\n"]}
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,wCAAwC,CAAC;AACvD,cAAc,qCAAqC,CAAC","sourcesContent":["export * from \"./domain/PlatformLogMiddlewareSettings\";\nexport * from \"./middlewares/PlatformLogMiddleware\";\n"]}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { __decorate, __metadata, __param } from "tslib";
|
|
2
|
+
import { Constant } from "@tsed/di";
|
|
3
|
+
import { Middleware } from "@tsed/platform-middlewares";
|
|
4
|
+
import { Context } from "@tsed/platform-params";
|
|
5
|
+
/**
|
|
6
|
+
* @middleware
|
|
7
|
+
* @platform
|
|
8
|
+
*/
|
|
9
|
+
let PlatformLogMiddleware = class PlatformLogMiddleware {
|
|
10
|
+
constructor() {
|
|
11
|
+
if (this.logLevel !== "off") {
|
|
12
|
+
this.$onResponse = this.onLogEnd.bind(this);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
get settings() {
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Handle the request.
|
|
20
|
+
*/
|
|
21
|
+
use(ctx) {
|
|
22
|
+
this.configureRequest(ctx);
|
|
23
|
+
this.onLogStart(ctx);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The separate onLogStart() function will allow developer to overwrite the initial request log.
|
|
27
|
+
* @param ctx
|
|
28
|
+
*/
|
|
29
|
+
onLogStart(ctx) {
|
|
30
|
+
const { debug, logRequest, logStart } = this.settings;
|
|
31
|
+
if (logStart) {
|
|
32
|
+
if (debug) {
|
|
33
|
+
ctx.logger.debug({
|
|
34
|
+
event: "request.start"
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
else if (logRequest) {
|
|
38
|
+
ctx.logger.info({
|
|
39
|
+
event: "request.start"
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Called when the `$onResponse` is called by Ts.ED (through Express.end).
|
|
46
|
+
*/
|
|
47
|
+
onLogEnd(ctx) {
|
|
48
|
+
const { debug, logRequest, logEnd } = this.settings;
|
|
49
|
+
if (logEnd) {
|
|
50
|
+
if (debug) {
|
|
51
|
+
ctx.logger.debug({
|
|
52
|
+
event: "request.end",
|
|
53
|
+
status: ctx.response.statusCode,
|
|
54
|
+
data: ctx.data
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
else if (logRequest) {
|
|
58
|
+
ctx.logger.info({
|
|
59
|
+
event: "request.end",
|
|
60
|
+
status: ctx.response.statusCode
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
ctx.logger.flush();
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Attach all information that will be necessary to log the request. Attach a new `request.log` object.
|
|
68
|
+
*/
|
|
69
|
+
configureRequest(ctx) {
|
|
70
|
+
ctx.logger.minimalRequestPicker = (obj) => ({ ...this.minimalRequestPicker(ctx), ...obj });
|
|
71
|
+
ctx.logger.completeRequestPicker = (obj) => ({ ...this.requestToObject(ctx), ...obj });
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Return complete request info.
|
|
75
|
+
* @returns {Object}
|
|
76
|
+
* @param ctx
|
|
77
|
+
*/
|
|
78
|
+
requestToObject(ctx) {
|
|
79
|
+
const { request } = ctx;
|
|
80
|
+
return {
|
|
81
|
+
method: request.method,
|
|
82
|
+
url: request.url,
|
|
83
|
+
headers: request.headers,
|
|
84
|
+
body: request.body,
|
|
85
|
+
query: request.query,
|
|
86
|
+
params: request.params
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Return a filtered request from global configuration.
|
|
91
|
+
* @returns {Object}
|
|
92
|
+
* @param ctx
|
|
93
|
+
*/
|
|
94
|
+
minimalRequestPicker(ctx) {
|
|
95
|
+
const { requestFields } = this;
|
|
96
|
+
const info = this.requestToObject(ctx);
|
|
97
|
+
return requestFields.reduce((acc, key) => {
|
|
98
|
+
acc[key] = info[key];
|
|
99
|
+
return acc;
|
|
100
|
+
}, {});
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
__decorate([
|
|
104
|
+
Constant("logger.requestFields", ["reqId", "method", "url", "duration"]),
|
|
105
|
+
__metadata("design:type", Array)
|
|
106
|
+
], PlatformLogMiddleware.prototype, "requestFields", void 0);
|
|
107
|
+
__decorate([
|
|
108
|
+
Constant("logger.logRequest", true),
|
|
109
|
+
__metadata("design:type", Boolean)
|
|
110
|
+
], PlatformLogMiddleware.prototype, "logRequest", void 0);
|
|
111
|
+
__decorate([
|
|
112
|
+
Constant("logger.logStart", true),
|
|
113
|
+
__metadata("design:type", Boolean)
|
|
114
|
+
], PlatformLogMiddleware.prototype, "logStart", void 0);
|
|
115
|
+
__decorate([
|
|
116
|
+
Constant("logger.logEnd", true),
|
|
117
|
+
__metadata("design:type", Boolean)
|
|
118
|
+
], PlatformLogMiddleware.prototype, "logEnd", void 0);
|
|
119
|
+
__decorate([
|
|
120
|
+
Constant("logger.level"),
|
|
121
|
+
__metadata("design:type", String)
|
|
122
|
+
], PlatformLogMiddleware.prototype, "logLevel", void 0);
|
|
123
|
+
__decorate([
|
|
124
|
+
Constant("debug"),
|
|
125
|
+
__metadata("design:type", Boolean)
|
|
126
|
+
], PlatformLogMiddleware.prototype, "debug", void 0);
|
|
127
|
+
__decorate([
|
|
128
|
+
__param(0, Context()),
|
|
129
|
+
__metadata("design:type", Function),
|
|
130
|
+
__metadata("design:paramtypes", [Object]),
|
|
131
|
+
__metadata("design:returntype", void 0)
|
|
132
|
+
], PlatformLogMiddleware.prototype, "use", null);
|
|
133
|
+
PlatformLogMiddleware = __decorate([
|
|
134
|
+
Middleware(),
|
|
135
|
+
__metadata("design:paramtypes", [])
|
|
136
|
+
], PlatformLogMiddleware);
|
|
137
|
+
export { PlatformLogMiddleware };
|
|
138
|
+
//# sourceMappingURL=PlatformLogMiddleware.js.map
|
|
@@ -0,0 +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;IAqBhC;QACE,IAAI,IAAI,CAAC,QAAQ,KAAK,KAAK,EAAE;YAC3B,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SAC7C;IACH,CAAC;IAED,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;IACvB,CAAC;IAED;;;OAGG;IACO,UAAU,CAAC,GAAY;QAC/B,MAAM,EAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAC,GAAG,IAAI,CAAC,QAAQ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ent":["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 $onResponse: any;\n\n @Constant(\"logger.requestFields\", [\"reqId\", \"method\", \"url\", \"duration\"])\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 @Constant(\"debug\")\n protected debug: boolean;\n\n constructor() {\n if (this.logLevel !== \"off\") {\n this.$onResponse = this.onLogEnd.bind(this);\n }\n }\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\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 {debug, logRequest, logStart} = this.settings;\n\n if (logStart) {\n if (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 protected onLogEnd(ctx: Context) {\n const {debug, logRequest, logEnd} = this.settings;\n\n if (logEnd) {\n if (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.minimalRequestPicker = (obj: any) => ({...this.minimalRequestPicker(ctx), ...obj});\n ctx.logger.completeRequestPicker = (obj: any) => ({...this.requestToObject(ctx), ...obj});\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 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"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare type LoggerRequestFields = ("reqId" | "method" | "url" | "headers" | "body" | "query" | "params" | "duration" | string)[];
|
|
2
|
+
export interface PlatformLogMiddlewareSettings {
|
|
3
|
+
/**
|
|
4
|
+
* Fields displayed when a request is logged. Possible values: `reqId`, `method`, `url`, `headers`, `body`, `query`,`params`, `duration`.
|
|
5
|
+
*/
|
|
6
|
+
requestFields?: LoggerRequestFields;
|
|
7
|
+
/**
|
|
8
|
+
* Log all incoming request. By default is true and print the configured `logger.requestFields`.
|
|
9
|
+
*/
|
|
10
|
+
logRequest?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Log start of all incoming request. By default is true
|
|
13
|
+
*/
|
|
14
|
+
logStart?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Log end of all incoming request. By default is true
|
|
17
|
+
*/
|
|
18
|
+
logEnd?: boolean;
|
|
19
|
+
}
|
|
20
|
+
declare global {
|
|
21
|
+
namespace TsED {
|
|
22
|
+
interface LoggerConfiguration extends Partial<PlatformLogMiddlewareSettings> {
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { MiddlewareMethods } from "@tsed/platform-middlewares";
|
|
2
|
+
import { Context } from "@tsed/platform-params";
|
|
3
|
+
import type { LoggerRequestFields } from "../domain/PlatformLogMiddlewareSettings";
|
|
4
|
+
/**
|
|
5
|
+
* @middleware
|
|
6
|
+
* @platform
|
|
7
|
+
*/
|
|
8
|
+
export declare class PlatformLogMiddleware implements MiddlewareMethods {
|
|
9
|
+
$onResponse: any;
|
|
10
|
+
protected requestFields: LoggerRequestFields;
|
|
11
|
+
protected logRequest: boolean;
|
|
12
|
+
protected logStart: boolean;
|
|
13
|
+
protected logEnd: boolean;
|
|
14
|
+
protected logLevel: string;
|
|
15
|
+
protected debug: boolean;
|
|
16
|
+
constructor();
|
|
17
|
+
get settings(): this;
|
|
18
|
+
/**
|
|
19
|
+
* Handle the request.
|
|
20
|
+
*/
|
|
21
|
+
use(ctx: Context): void;
|
|
22
|
+
/**
|
|
23
|
+
* The separate onLogStart() function will allow developer to overwrite the initial request log.
|
|
24
|
+
* @param ctx
|
|
25
|
+
*/
|
|
26
|
+
protected onLogStart(ctx: Context): void;
|
|
27
|
+
/**
|
|
28
|
+
* Called when the `$onResponse` is called by Ts.ED (through Express.end).
|
|
29
|
+
*/
|
|
30
|
+
protected onLogEnd(ctx: Context): void;
|
|
31
|
+
/**
|
|
32
|
+
* Attach all information that will be necessary to log the request. Attach a new `request.log` object.
|
|
33
|
+
*/
|
|
34
|
+
protected configureRequest(ctx: Context): void;
|
|
35
|
+
/**
|
|
36
|
+
* Return complete request info.
|
|
37
|
+
* @returns {Object}
|
|
38
|
+
* @param ctx
|
|
39
|
+
*/
|
|
40
|
+
protected requestToObject(ctx: Context): any;
|
|
41
|
+
/**
|
|
42
|
+
* Return a filtered request from global configuration.
|
|
43
|
+
* @returns {Object}
|
|
44
|
+
* @param ctx
|
|
45
|
+
*/
|
|
46
|
+
protected minimalRequestPicker(ctx: Context): any;
|
|
47
|
+
}
|
package/package.json
CHANGED
|
@@ -1,33 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/platform-log-middleware",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.89.0",
|
|
4
4
|
"description": "Log request middleware module for Ts.ED Framework",
|
|
5
5
|
"private": false,
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
|
-
"main": "./lib/index.js",
|
|
8
|
-
"typings": "./lib/index.d.ts",
|
|
7
|
+
"main": "./lib/cjs/index.js",
|
|
8
|
+
"typings": "./lib/types/index.d.ts",
|
|
9
9
|
"exports": {
|
|
10
|
-
"
|
|
11
|
-
"
|
|
10
|
+
"types": "./lib/types/index.d.ts",
|
|
11
|
+
"import": "./lib/esm/index.js",
|
|
12
|
+
"require": "./lib/cjs/index.js",
|
|
13
|
+
"default": "./lib/esm/index.js"
|
|
12
14
|
},
|
|
13
15
|
"scripts": {
|
|
14
|
-
"build": "
|
|
16
|
+
"build": "yarn run build:esm && yarn run build:cjs",
|
|
17
|
+
"build:cjs": "tsc --build tsconfig.compile.json",
|
|
18
|
+
"build:esm": "tsc --build tsconfig.compile.esm.json",
|
|
15
19
|
"test": "cross-env NODE_ENV=test jest"
|
|
16
20
|
},
|
|
17
21
|
"dependencies": {
|
|
18
22
|
"tslib": "2.2.0"
|
|
19
23
|
},
|
|
20
24
|
"devDependencies": {
|
|
21
|
-
"@tsed/core": "6.
|
|
22
|
-
"@tsed/di": "6.
|
|
23
|
-
"@tsed/platform-middlewares": "6.
|
|
24
|
-
"@tsed/platform-params": "6.
|
|
25
|
+
"@tsed/core": "6.89.0",
|
|
26
|
+
"@tsed/di": "6.89.0",
|
|
27
|
+
"@tsed/platform-middlewares": "6.89.0",
|
|
28
|
+
"@tsed/platform-params": "6.89.0"
|
|
25
29
|
},
|
|
26
30
|
"peerDependencies": {
|
|
27
|
-
"@tsed/core": "^6.
|
|
28
|
-
"@tsed/di": "^6.
|
|
29
|
-
"@tsed/platform-middlewares": "^6.
|
|
30
|
-
"@tsed/platform-params": "^6.
|
|
31
|
+
"@tsed/core": "^6.89.0",
|
|
32
|
+
"@tsed/di": "^6.89.0",
|
|
33
|
+
"@tsed/platform-middlewares": "^6.89.0",
|
|
34
|
+
"@tsed/platform-params": "^6.89.0"
|
|
31
35
|
},
|
|
32
36
|
"repository": "https://github.com/tsedio/tsed",
|
|
33
37
|
"bugs": {
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.compile.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": ".",
|
|
5
|
+
"module": "ES2020",
|
|
6
|
+
"rootDir": "src",
|
|
7
|
+
"outDir": "./lib/esm",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"declarationDir": "./lib/types"
|
|
10
|
+
},
|
|
11
|
+
"exclude": [
|
|
12
|
+
"node_modules",
|
|
13
|
+
"test",
|
|
14
|
+
"lib",
|
|
15
|
+
"benchmark",
|
|
16
|
+
"coverage",
|
|
17
|
+
"spec",
|
|
18
|
+
"**/*.benchmark.ts",
|
|
19
|
+
"**/*.spec.ts",
|
|
20
|
+
"keys",
|
|
21
|
+
"jest.config.js"
|
|
22
|
+
]
|
|
23
|
+
}
|
package/lib/index.js
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
var tslib = require('tslib');
|
|
2
|
-
var di = require('@tsed/di');
|
|
3
|
-
var platformMiddlewares = require('@tsed/platform-middlewares');
|
|
4
|
-
var platformParams = require('@tsed/platform-params');
|
|
5
|
-
|
|
6
|
-
function _extends() {
|
|
7
|
-
_extends = Object.assign || function (target) {
|
|
8
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
9
|
-
var source = arguments[i];
|
|
10
|
-
|
|
11
|
-
for (var key in source) {
|
|
12
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
13
|
-
target[key] = source[key];
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return target;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
return _extends.apply(this, arguments);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* @middleware
|
|
26
|
-
* @platform
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
|
-
exports.PlatformLogMiddleware = class PlatformLogMiddleware {
|
|
30
|
-
constructor() {
|
|
31
|
-
if (this.logLevel !== "off") {
|
|
32
|
-
this.$onResponse = this.onLogEnd.bind(this);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
get settings() {
|
|
37
|
-
return this;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Handle the request.
|
|
41
|
-
*/
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
use(ctx) {
|
|
45
|
-
this.configureRequest(ctx);
|
|
46
|
-
this.onLogStart(ctx);
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* The separate onLogStart() function will allow developer to overwrite the initial request log.
|
|
50
|
-
* @param ctx
|
|
51
|
-
*/
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
onLogStart(ctx) {
|
|
55
|
-
const {
|
|
56
|
-
debug,
|
|
57
|
-
logRequest,
|
|
58
|
-
logStart
|
|
59
|
-
} = this.settings;
|
|
60
|
-
|
|
61
|
-
if (logStart) {
|
|
62
|
-
if (debug) {
|
|
63
|
-
ctx.logger.debug({
|
|
64
|
-
event: "request.start"
|
|
65
|
-
});
|
|
66
|
-
} else if (logRequest) {
|
|
67
|
-
ctx.logger.info({
|
|
68
|
-
event: "request.start"
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Called when the `$onResponse` is called by Ts.ED (through Express.end).
|
|
75
|
-
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
onLogEnd(ctx) {
|
|
79
|
-
const {
|
|
80
|
-
debug,
|
|
81
|
-
logRequest,
|
|
82
|
-
logEnd
|
|
83
|
-
} = this.settings;
|
|
84
|
-
|
|
85
|
-
if (logEnd) {
|
|
86
|
-
if (debug) {
|
|
87
|
-
ctx.logger.debug({
|
|
88
|
-
event: "request.end",
|
|
89
|
-
status: ctx.response.statusCode,
|
|
90
|
-
data: ctx.data
|
|
91
|
-
});
|
|
92
|
-
} else if (logRequest) {
|
|
93
|
-
ctx.logger.info({
|
|
94
|
-
event: "request.end",
|
|
95
|
-
status: ctx.response.statusCode
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
ctx.logger.flush();
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Attach all information that will be necessary to log the request. Attach a new `request.log` object.
|
|
104
|
-
*/
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
configureRequest(ctx) {
|
|
108
|
-
ctx.logger.minimalRequestPicker = obj => _extends({}, this.minimalRequestPicker(ctx), obj);
|
|
109
|
-
|
|
110
|
-
ctx.logger.completeRequestPicker = obj => _extends({}, this.requestToObject(ctx), obj);
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Return complete request info.
|
|
114
|
-
* @returns {Object}
|
|
115
|
-
* @param ctx
|
|
116
|
-
*/
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
requestToObject(ctx) {
|
|
120
|
-
const {
|
|
121
|
-
request
|
|
122
|
-
} = ctx;
|
|
123
|
-
return {
|
|
124
|
-
method: request.method,
|
|
125
|
-
url: request.url,
|
|
126
|
-
headers: request.headers,
|
|
127
|
-
body: request.body,
|
|
128
|
-
query: request.query,
|
|
129
|
-
params: request.params
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Return a filtered request from global configuration.
|
|
134
|
-
* @returns {Object}
|
|
135
|
-
* @param ctx
|
|
136
|
-
*/
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
minimalRequestPicker(ctx) {
|
|
140
|
-
const {
|
|
141
|
-
requestFields
|
|
142
|
-
} = this;
|
|
143
|
-
const info = this.requestToObject(ctx);
|
|
144
|
-
return requestFields.reduce((acc, key) => {
|
|
145
|
-
acc[key] = info[key];
|
|
146
|
-
return acc;
|
|
147
|
-
}, {});
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
tslib.__decorate([di.Constant("logger.requestFields", ["reqId", "method", "url", "duration"]), tslib.__metadata("design:type", Array)], exports.PlatformLogMiddleware.prototype, "requestFields", void 0);
|
|
153
|
-
|
|
154
|
-
tslib.__decorate([di.Constant("logger.logRequest", true), tslib.__metadata("design:type", Boolean)], exports.PlatformLogMiddleware.prototype, "logRequest", void 0);
|
|
155
|
-
|
|
156
|
-
tslib.__decorate([di.Constant("logger.logStart", true), tslib.__metadata("design:type", Boolean)], exports.PlatformLogMiddleware.prototype, "logStart", void 0);
|
|
157
|
-
|
|
158
|
-
tslib.__decorate([di.Constant("logger.logEnd", true), tslib.__metadata("design:type", Boolean)], exports.PlatformLogMiddleware.prototype, "logEnd", void 0);
|
|
159
|
-
|
|
160
|
-
tslib.__decorate([di.Constant("logger.level"), tslib.__metadata("design:type", String)], exports.PlatformLogMiddleware.prototype, "logLevel", void 0);
|
|
161
|
-
|
|
162
|
-
tslib.__decorate([di.Constant("debug"), tslib.__metadata("design:type", Boolean)], exports.PlatformLogMiddleware.prototype, "debug", void 0);
|
|
163
|
-
|
|
164
|
-
tslib.__decorate([tslib.__param(0, platformParams.Context()), tslib.__metadata("design:type", Function), tslib.__metadata("design:paramtypes", [Object]), tslib.__metadata("design:returntype", void 0)], exports.PlatformLogMiddleware.prototype, "use", null);
|
|
165
|
-
|
|
166
|
-
exports.PlatformLogMiddleware = tslib.__decorate([platformMiddlewares.Middleware(), tslib.__metadata("design:paramtypes", [])], exports.PlatformLogMiddleware);
|
|
167
|
-
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/middlewares/PlatformLogMiddleware.ts"],"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 $onResponse: any;\n\n @Constant(\"logger.requestFields\", [\"reqId\", \"method\", \"url\", \"duration\"])\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 @Constant(\"debug\")\n protected debug: boolean;\n\n constructor() {\n if (this.logLevel !== \"off\") {\n this.$onResponse = this.onLogEnd.bind(this);\n }\n }\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\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 {debug, logRequest, logStart} = this.settings;\n\n if (logStart) {\n if (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 protected onLogEnd(ctx: Context) {\n const {debug, logRequest, logEnd} = this.settings;\n\n if (logEnd) {\n if (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.minimalRequestPicker = (obj: any) => ({...this.minimalRequestPicker(ctx), ...obj});\n ctx.logger.completeRequestPicker = (obj: any) => ({...this.requestToObject(ctx), ...obj});\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 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"],"names":["PlatformLogMiddleware","constructor","logLevel","$onResponse","onLogEnd","bind","settings","use","ctx","configureRequest","onLogStart","debug","logRequest","logStart","logger","event","info","logEnd","status","response","statusCode","data","flush","minimalRequestPicker","obj","completeRequestPicker","requestToObject","request","method","url","headers","body","query","params","requestFields","reduce","acc","key","__decorate","Constant","__param","Context","Middleware"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAKA;;;;;AAKaA,6BAAqB,GAAlC,MAAaA,qBAAb;AAqBEC,EAAAA;AACE,QAAI,KAAKC,QAAL,KAAkB,KAAtB,EAA6B;AAC3B,WAAKC,WAAL,GAAmB,KAAKC,QAAL,CAAcC,IAAd,CAAmB,IAAnB,CAAnB;AACD;AACF;;AAEW,MAARC,QAAQ;AACV,WAAO,IAAP;AACD;AAED;;;;;AAGOC,EAAAA,GAAG,CAAYC,GAAZ;AACR,SAAKC,gBAAL,CAAsBD,GAAtB;AACA,SAAKE,UAAL,CAAgBF,GAAhB;AACD;AAED;;;;;;AAIUE,EAAAA,UAAU,CAACF,GAAD;AAClB,UAAM;AAACG,MAAAA,KAAD;AAAQC,MAAAA,UAAR;AAAoBC,MAAAA;AAApB,QAAgC,KAAKP,QAA3C;;AAEA,QAAIO,QAAJ,EAAc;AACZ,UAAIF,KAAJ,EAAW;AACTH,QAAAA,G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ib';
|
|
2
|
-
import { Constant } from '@tsed/di';
|
|
3
|
-
import { Middleware } from '@tsed/platform-middlewares';
|
|
4
|
-
import { Context } from '@tsed/platform-params';
|
|
5
|
-
|
|
6
|
-
function _extends() {
|
|
7
|
-
_extends = Object.assign || function (target) {
|
|
8
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
9
|
-
var source = arguments[i];
|
|
10
|
-
|
|
11
|
-
for (var key in source) {
|
|
12
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
13
|
-
target[key] = source[key];
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return target;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
return _extends.apply(this, arguments);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* @middleware
|
|
26
|
-
* @platform
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
|
-
let PlatformLogMiddleware = class PlatformLogMiddleware {
|
|
30
|
-
constructor() {
|
|
31
|
-
if (this.logLevel !== "off") {
|
|
32
|
-
this.$onResponse = this.onLogEnd.bind(this);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
get settings() {
|
|
37
|
-
return this;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Handle the request.
|
|
41
|
-
*/
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
use(ctx) {
|
|
45
|
-
this.configureRequest(ctx);
|
|
46
|
-
this.onLogStart(ctx);
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* The separate onLogStart() function will allow developer to overwrite the initial request log.
|
|
50
|
-
* @param ctx
|
|
51
|
-
*/
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
onLogStart(ctx) {
|
|
55
|
-
const {
|
|
56
|
-
debug,
|
|
57
|
-
logRequest,
|
|
58
|
-
logStart
|
|
59
|
-
} = this.settings;
|
|
60
|
-
|
|
61
|
-
if (logStart) {
|
|
62
|
-
if (debug) {
|
|
63
|
-
ctx.logger.debug({
|
|
64
|
-
event: "request.start"
|
|
65
|
-
});
|
|
66
|
-
} else if (logRequest) {
|
|
67
|
-
ctx.logger.info({
|
|
68
|
-
event: "request.start"
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Called when the `$onResponse` is called by Ts.ED (through Express.end).
|
|
75
|
-
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
onLogEnd(ctx) {
|
|
79
|
-
const {
|
|
80
|
-
debug,
|
|
81
|
-
logRequest,
|
|
82
|
-
logEnd
|
|
83
|
-
} = this.settings;
|
|
84
|
-
|
|
85
|
-
if (logEnd) {
|
|
86
|
-
if (debug) {
|
|
87
|
-
ctx.logger.debug({
|
|
88
|
-
event: "request.end",
|
|
89
|
-
status: ctx.response.statusCode,
|
|
90
|
-
data: ctx.data
|
|
91
|
-
});
|
|
92
|
-
} else if (logRequest) {
|
|
93
|
-
ctx.logger.info({
|
|
94
|
-
event: "request.end",
|
|
95
|
-
status: ctx.response.statusCode
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
ctx.logger.flush();
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Attach all information that will be necessary to log the request. Attach a new `request.log` object.
|
|
104
|
-
*/
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
configureRequest(ctx) {
|
|
108
|
-
ctx.logger.minimalRequestPicker = obj => _extends({}, this.minimalRequestPicker(ctx), obj);
|
|
109
|
-
|
|
110
|
-
ctx.logger.completeRequestPicker = obj => _extends({}, this.requestToObject(ctx), obj);
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Return complete request info.
|
|
114
|
-
* @returns {Object}
|
|
115
|
-
* @param ctx
|
|
116
|
-
*/
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
requestToObject(ctx) {
|
|
120
|
-
const {
|
|
121
|
-
request
|
|
122
|
-
} = ctx;
|
|
123
|
-
return {
|
|
124
|
-
method: request.method,
|
|
125
|
-
url: request.url,
|
|
126
|
-
headers: request.headers,
|
|
127
|
-
body: request.body,
|
|
128
|
-
query: request.query,
|
|
129
|
-
params: request.params
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Return a filtered request from global configuration.
|
|
134
|
-
* @returns {Object}
|
|
135
|
-
* @param ctx
|
|
136
|
-
*/
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
minimalRequestPicker(ctx) {
|
|
140
|
-
const {
|
|
141
|
-
requestFields
|
|
142
|
-
} = this;
|
|
143
|
-
const info = this.requestToObject(ctx);
|
|
144
|
-
return requestFields.reduce((acc, key) => {
|
|
145
|
-
acc[key] = info[key];
|
|
146
|
-
return acc;
|
|
147
|
-
}, {});
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
__decorate([Constant("logger.requestFields", ["reqId", "method", "url", "duration"]), __metadata("design:type", Array)], PlatformLogMiddleware.prototype, "requestFields", void 0);
|
|
153
|
-
|
|
154
|
-
__decorate([Constant("logger.logRequest", true), __metadata("design:type", Boolean)], PlatformLogMiddleware.prototype, "logRequest", void 0);
|
|
155
|
-
|
|
156
|
-
__decorate([Constant("logger.logStart", true), __metadata("design:type", Boolean)], PlatformLogMiddleware.prototype, "logStart", void 0);
|
|
157
|
-
|
|
158
|
-
__decorate([Constant("logger.logEnd", true), __metadata("design:type", Boolean)], PlatformLogMiddleware.prototype, "logEnd", void 0);
|
|
159
|
-
|
|
160
|
-
__decorate([Constant("logger.level"), __metadata("design:type", String)], PlatformLogMiddleware.prototype, "logLevel", void 0);
|
|
161
|
-
|
|
162
|
-
__decorate([Constant("debug"), __metadata("design:type", Boolean)], PlatformLogMiddleware.prototype, "debug", void 0);
|
|
163
|
-
|
|
164
|
-
__decorate([__param(0, Context()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0)], PlatformLogMiddleware.prototype, "use", null);
|
|
165
|
-
|
|
166
|
-
PlatformLogMiddleware = __decorate([Middleware(), __metadata("design:paramtypes", [])], PlatformLogMiddleware);
|
|
167
|
-
|
|
168
|
-
export { PlatformLogMiddleware };
|
|
169
|
-
//# sourceMappingURL=index.modern.js.map
|
package/lib/index.modern.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.modern.js","sources":["../src/middlewares/PlatformLogMiddleware.ts"],"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 $onResponse: any;\n\n @Constant(\"logger.requestFields\", [\"reqId\", \"method\", \"url\", \"duration\"])\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 @Constant(\"debug\")\n protected debug: boolean;\n\n constructor() {\n if (this.logLevel !== \"off\") {\n this.$onResponse = this.onLogEnd.bind(this);\n }\n }\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\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 {debug, logRequest, logStart} = this.settings;\n\n if (logStart) {\n if (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 protected onLogEnd(ctx: Context) {\n const {debug, logRequest, logEnd} = this.settings;\n\n if (logEnd) {\n if (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.minimalRequestPicker = (obj: any) => ({...this.minimalRequestPicker(ctx), ...obj});\n ctx.logger.completeRequestPicker = (obj: any) => ({...this.requestToObject(ctx), ...obj});\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 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"],"names":["PlatformLogMiddleware","constructor","logLevel","$onResponse","onLogEnd","bind","settings","use","ctx","configureRequest","onLogStart","debug","logRequest","logStart","logger","event","info","logEnd","status","response","statusCode","data","flush","minimalRequestPicker","obj","completeRequestPicker","requestToObject","request","method","url","headers","body","query","params","requestFields","reduce","acc","key","__decorate","Constant","__param","Context","Middleware"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAKA;;;;;IAKaA,qBAAqB,GAAlC,MAAaA,qBAAb;AAqBEC,EAAAA;AACE,QAAI,KAAKC,QAAL,KAAkB,KAAtB,EAA6B;AAC3B,WAAKC,WAAL,GAAmB,KAAKC,QAAL,CAAcC,IAAd,CAAmB,IAAnB,CAAnB;A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