micronodelib 1.0.1 → 1.0.4
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/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Micronodelib
|
2
|
+
|
3
|
+
Common core for NodeJs project.
|
4
|
+
|
5
|
+
## Features
|
6
|
+
|
7
|
+
- Request context injected
|
8
|
+
- Http request log
|
9
|
+
- Standard logging format
|
10
|
+
|
11
|
+
## Install
|
12
|
+
|
13
|
+
```shell
|
14
|
+
npm install micronodelib
|
15
|
+
|
16
|
+
# Or
|
17
|
+
|
18
|
+
yarn add micronodelib
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```shell
|
24
|
+
import express, {Application} from "express";
|
25
|
+
import {Logger, httpContextMiddleware, requestLogMiddleware} from "micronodelib";
|
26
|
+
const app: Application = express();
|
27
|
+
|
28
|
+
# Enable request context
|
29
|
+
app.use(httpContextMiddleware());
|
30
|
+
|
31
|
+
# Enable http request log
|
32
|
+
app.use(requestLogMiddleware({
|
33
|
+
# Ignore request log for specific routes
|
34
|
+
ignoreRoutes: ["/actuator/health"]
|
35
|
+
}));
|
36
|
+
|
37
|
+
app.listen(3000, () => {
|
38
|
+
# Using standard log level: debug, info, warn, error
|
39
|
+
Logger.info('App started');
|
40
|
+
})
|
41
|
+
```
|
@@ -1,2 +1,2 @@
|
|
1
1
|
import { NextFunction, Request, Response } from "express";
|
2
|
-
export declare const httpContextMiddleware: ((req: Request, res: Response, next: NextFunction) => void)[];
|
2
|
+
export declare const httpContextMiddleware: () => ((req: Request, res: Response, next: NextFunction) => void)[];
|
@@ -12,7 +12,10 @@ const requestIdMiddleware = (req, res, next) => {
|
|
12
12
|
(0, express_http_context_1.set)("reqId", requestId);
|
13
13
|
next();
|
14
14
|
};
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
const httpContextMiddleware = function () {
|
16
|
+
return [
|
17
|
+
express_http_context_1.middleware,
|
18
|
+
requestIdMiddleware,
|
19
|
+
];
|
20
|
+
};
|
21
|
+
exports.httpContextMiddleware = httpContextMiddleware;
|
package/dist/logging/logger.js
CHANGED
@@ -2,4 +2,4 @@ import { Handler } from "express";
|
|
2
2
|
export interface RequestLogOptions {
|
3
3
|
ignoreRoutes: string[];
|
4
4
|
}
|
5
|
-
export declare const requestLogMiddleware: (opt
|
5
|
+
export declare const requestLogMiddleware: (opt?: RequestLogOptions | undefined) => Handler;
|
@@ -24,18 +24,20 @@ const requestLogMiddleware = function (opt) {
|
|
24
24
|
request_method: req.method,
|
25
25
|
query: new URLSearchParams(req.query).toString(),
|
26
26
|
url: req.url,
|
27
|
+
device_id: req.headers['device-id'],
|
28
|
+
device_session_id: req.headers['device-session-id'],
|
27
29
|
request_id: req.headers['x-request-id'],
|
28
30
|
client_ip: req.headers['x-forwarded-for'] || (req.connection ? req.connection.remoteAddress : null),
|
29
31
|
user_agent: req.headers['user-agent']
|
30
32
|
};
|
31
|
-
info.message =
|
33
|
+
info.message = null;
|
32
34
|
delete info.timestamp;
|
33
35
|
delete info.meta;
|
34
36
|
return info;
|
35
37
|
}),
|
36
38
|
transports: configuration_1.transports,
|
37
39
|
ignoreRoute: function (req, res) {
|
38
|
-
return opt.ignoreRoutes.length > 0 && opt.ignoreRoutes.indexOf(req.url) >= 0;
|
40
|
+
return opt !== undefined && opt.ignoreRoutes.length > 0 && opt.ignoreRoutes.indexOf(req.url) >= 0;
|
39
41
|
}
|
40
42
|
});
|
41
43
|
};
|
package/package.json
CHANGED