@slickteam/nestjs-utils 1.4.5 → 1.4.6
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/dist/index.d.ts +14 -0
- package/dist/index.js +95 -0
- package/dist/index.js.map +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { HttpStatus, LogLevel, Logger } from '@nestjs/common';
|
|
2
|
+
interface LoggerParamsOptions {
|
|
3
|
+
level?: LogLevel;
|
|
4
|
+
paramNames?: string[];
|
|
5
|
+
}
|
|
6
|
+
declare function LoggerParams(options?: LogLevel | LoggerParamsOptions): (target: {
|
|
7
|
+
constructor: {
|
|
8
|
+
name: string;
|
|
9
|
+
};
|
|
10
|
+
}, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
11
|
+
declare function throwErrorAndLog(message: any, typeError: HttpStatus | undefined, logger: Logger | undefined): never;
|
|
12
|
+
declare function throwErrorAndLogWithContext(message: any, typeError?: HttpStatus, context?: string): never;
|
|
13
|
+
declare function logLevel(level: LogLevel): LogLevel[];
|
|
14
|
+
export { LoggerParams, throwErrorAndLog, throwErrorAndLogWithContext, logLevel };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LoggerParams = LoggerParams;
|
|
4
|
+
exports.throwErrorAndLog = throwErrorAndLog;
|
|
5
|
+
exports.throwErrorAndLogWithContext = throwErrorAndLogWithContext;
|
|
6
|
+
exports.logLevel = logLevel;
|
|
7
|
+
const common_1 = require("@nestjs/common");
|
|
8
|
+
function LoggerParams(options = 'verbose') {
|
|
9
|
+
const level = typeof options === 'string' ? options : options.level || 'verbose';
|
|
10
|
+
const paramNames = typeof options === 'object' && options.paramNames ? options.paramNames : undefined;
|
|
11
|
+
return (target, propertyKey, descriptor) => {
|
|
12
|
+
const original = descriptor.value;
|
|
13
|
+
descriptor.value = function (...args) {
|
|
14
|
+
let message = `${propertyKey}(`;
|
|
15
|
+
for (let i = 0; i < args.length; i++) {
|
|
16
|
+
const arg = args[i];
|
|
17
|
+
const paramName = paramNames && paramNames[i] ? paramNames[i] : `[${i}]`;
|
|
18
|
+
if (Array.isArray(arg)) {
|
|
19
|
+
message += `${paramName}=[Array(${arg.length})]`;
|
|
20
|
+
}
|
|
21
|
+
else if (arg === null) {
|
|
22
|
+
message += `${paramName}=null`;
|
|
23
|
+
}
|
|
24
|
+
else if (arg === undefined) {
|
|
25
|
+
message += `${paramName}=undefined`;
|
|
26
|
+
}
|
|
27
|
+
else if (typeof arg === 'object') {
|
|
28
|
+
message += `${paramName}={...}`;
|
|
29
|
+
}
|
|
30
|
+
else if (typeof arg === 'string') {
|
|
31
|
+
message += `${paramName}="${arg}"`;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
message += `${paramName}=${String(arg)}`;
|
|
35
|
+
}
|
|
36
|
+
if (i < args.length - 1) {
|
|
37
|
+
message += ', ';
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
message += ')';
|
|
41
|
+
const className = target.constructor.name;
|
|
42
|
+
switch (level) {
|
|
43
|
+
case 'verbose':
|
|
44
|
+
common_1.Logger.verbose(message, className);
|
|
45
|
+
break;
|
|
46
|
+
case 'debug':
|
|
47
|
+
common_1.Logger.debug(message, className);
|
|
48
|
+
break;
|
|
49
|
+
case 'log':
|
|
50
|
+
common_1.Logger.log(message, className);
|
|
51
|
+
break;
|
|
52
|
+
case 'warn':
|
|
53
|
+
common_1.Logger.warn(message, className);
|
|
54
|
+
break;
|
|
55
|
+
default:
|
|
56
|
+
common_1.Logger.error(message, className);
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
const value = original.apply(this, args);
|
|
60
|
+
return value;
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function throwErrorAndLog(message, typeError = common_1.HttpStatus.INTERNAL_SERVER_ERROR, logger) {
|
|
65
|
+
const error = new common_1.HttpException(message, typeError);
|
|
66
|
+
if (logger) {
|
|
67
|
+
logger?.error(message);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
common_1.Logger.error(message);
|
|
71
|
+
}
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
function throwErrorAndLogWithContext(message, typeError = common_1.HttpStatus.INTERNAL_SERVER_ERROR, context) {
|
|
75
|
+
const error = new common_1.HttpException(message, typeError);
|
|
76
|
+
common_1.Logger.error(message, context);
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
function logLevel(level) {
|
|
80
|
+
const levelTab = [];
|
|
81
|
+
switch (level) {
|
|
82
|
+
case 'verbose':
|
|
83
|
+
levelTab.push('verbose');
|
|
84
|
+
case 'debug':
|
|
85
|
+
levelTab.push('debug');
|
|
86
|
+
case 'log':
|
|
87
|
+
levelTab.push('log');
|
|
88
|
+
case 'warn':
|
|
89
|
+
levelTab.push('warn');
|
|
90
|
+
default:
|
|
91
|
+
levelTab.push('error');
|
|
92
|
+
}
|
|
93
|
+
return levelTab;
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AA+FS,oCAAY;AAAE,4CAAgB;AAAE,kEAA2B;AAAE,4BAAQ;AA/F9E,2CAA6E;AAO7E,SAAS,YAAY,CAAC,UAA0C,SAAS;IACvE,MAAM,KAAK,GAAG,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,IAAI,SAAS,CAAC;IACjF,MAAM,UAAU,GAAG,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS,C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