micronodelib 1.0.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.
@@ -0,0 +1,2 @@
1
+ import { NextFunction, Request, Response } from "express";
2
+ export declare const httpContextMiddleware: ((req: Request, res: Response, next: NextFunction) => void)[];
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.httpContextMiddleware = void 0;
4
+ const express_http_context_1 = require("express-http-context");
5
+ const uuid_1 = require("uuid");
6
+ const requestIdMiddleware = (req, res, next) => {
7
+ let requestId = req.headers['x-request-id'];
8
+ if (!requestId) {
9
+ requestId = (0, uuid_1.v4)();
10
+ }
11
+ req.headers['x-request-id'] = requestId;
12
+ (0, express_http_context_1.set)("reqId", requestId);
13
+ next();
14
+ };
15
+ exports.httpContextMiddleware = [
16
+ express_http_context_1.middleware,
17
+ requestIdMiddleware,
18
+ ];
@@ -0,0 +1,4 @@
1
+ import winston from 'winston';
2
+ import { Format, TransformFunction } from "logform";
3
+ export declare const formatter: (transform: TransformFunction) => Format;
4
+ export declare const transports: winston.transports.ConsoleTransportInstance[];
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.transports = exports.formatter = void 0;
7
+ const winston_1 = __importDefault(require("winston"));
8
+ const formatter = function (transform) {
9
+ return winston_1.default.format.combine(winston_1.default.format.errors({ stack: true }), winston_1.default.format.timestamp(), winston_1.default.format.json(), winston_1.default.format(transform)());
10
+ };
11
+ exports.formatter = formatter;
12
+ exports.transports = [
13
+ new winston_1.default.transports.Console({ format: winston_1.default.format.json() }),
14
+ ];
@@ -0,0 +1,7 @@
1
+ export declare const Logger: {
2
+ log: (level: string, message: any, ...args: any[]) => void;
3
+ error: (message: any, ...args: any[]) => void;
4
+ warn: (message: any, ...args: any[]) => void;
5
+ info: (message: any, ...args: any[]) => void;
6
+ debug: (message: any, ...args: any[]) => void;
7
+ };
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.Logger = void 0;
30
+ const winston_1 = __importDefault(require("winston"));
31
+ const configuration_1 = require("./configuration");
32
+ const express_http_context_1 = require("express-http-context");
33
+ const util = __importStar(require("util"));
34
+ // Create the logger instance that has to be exported
35
+ // and used to log messages.
36
+ const winstonLogger = winston_1.default.createLogger({
37
+ format: (0, configuration_1.formatter)(info => {
38
+ info.level = info.level.toUpperCase();
39
+ info.msg = info.message;
40
+ info.ts = Date.parse(info.timestamp) / 1000;
41
+ info.request_meta = {
42
+ request_id: (0, express_http_context_1.get)('reqId')
43
+ };
44
+ info.stacktrace = info.stack;
45
+ info.message = "";
46
+ delete info.timestamp;
47
+ delete info.stack;
48
+ return info;
49
+ }),
50
+ transports: configuration_1.transports,
51
+ });
52
+ exports.Logger = {
53
+ log: function (level, message, ...args) {
54
+ winstonLogger.log(level, util.format(message, ...args));
55
+ },
56
+ error: function (message, ...args) {
57
+ let errMsg = message;
58
+ if (typeof message == "string" && args) {
59
+ let err = null;
60
+ for (let i = 0; i < args.length; i++) {
61
+ if (args[i] instanceof Error) {
62
+ if (err == null) {
63
+ err = args[i];
64
+ }
65
+ args[i] = args[i].message;
66
+ }
67
+ }
68
+ errMsg = new Error(util.format(message, ...args));
69
+ if (err != null) {
70
+ errMsg.stack = err.stack;
71
+ }
72
+ }
73
+ winstonLogger.error(errMsg);
74
+ },
75
+ warn: function (message, ...args) {
76
+ winstonLogger.warn(util.format(message, ...args));
77
+ },
78
+ info: function (message, ...args) {
79
+ winstonLogger.info(util.format(message, ...args));
80
+ },
81
+ debug: function (message, ...args) {
82
+ winstonLogger.debug(util.format(message, ...args));
83
+ }
84
+ };
@@ -0,0 +1,5 @@
1
+ import { Handler } from "express";
2
+ export interface RequestLogOptions {
3
+ ignoreRoutes: string[];
4
+ }
5
+ export declare const requestLogMiddleware: (opt: RequestLogOptions) => Handler;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.requestLogMiddleware = void 0;
7
+ const express_winston_1 = __importDefault(require("express-winston"));
8
+ const configuration_1 = require("./configuration");
9
+ const url_1 = __importDefault(require("url"));
10
+ // Place this middleware before the router.
11
+ const requestLogMiddleware = function (opt) {
12
+ return express_winston_1.default.logger({
13
+ msg: 'finish router',
14
+ format: (0, configuration_1.formatter)(info => {
15
+ const req = info.meta.req;
16
+ info.level = info.level.toUpperCase();
17
+ info.msg = info.message;
18
+ info.ts = Date.parse(info.timestamp) / 1000;
19
+ info.request_meta = {
20
+ status: info.meta.res.statusCode,
21
+ execution_time: info.meta.responseTime,
22
+ request_pattern: url_1.default.parse(req.url).pathname,
23
+ request_path: url_1.default.parse(req.url).pathname,
24
+ request_method: req.method,
25
+ query: new URLSearchParams(req.query).toString(),
26
+ url: req.url,
27
+ request_id: req.headers['x-request-id'],
28
+ client_ip: req.headers['x-forwarded-for'] || (req.connection ? req.connection.remoteAddress : null),
29
+ user_agent: req.headers['user-agent']
30
+ };
31
+ info.message = "";
32
+ delete info.timestamp;
33
+ delete info.meta;
34
+ return info;
35
+ }),
36
+ transports: configuration_1.transports,
37
+ ignoreRoute: function (req, res) {
38
+ return opt.ignoreRoutes.length > 0 && opt.ignoreRoutes.indexOf(req.url) >= 0;
39
+ }
40
+ });
41
+ };
42
+ exports.requestLogMiddleware = requestLogMiddleware;
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "micronodelib",
3
+ "version": "1.0.0",
4
+ "description": "Common package for NodeJs application",
5
+ "author": "Thang Bui",
6
+ "license": "MIT",
7
+ "main": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "files": [
10
+ "/dist"
11
+ ],
12
+ "dependencies": {
13
+ "express-http-context": "^1.2.4",
14
+ "express-winston": "^4.2.0",
15
+ "uuid": "^8.3.2"
16
+ },
17
+ "devDependencies": {
18
+ "@types/uuid": "^8.3.4",
19
+ "typescript": "^4.6.4"
20
+ }
21
+ }