b2b-platform-utils 1.1.9 → 1.1.11
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/errorsMap.js +7 -7
- package/logger.js +21 -5
- package/package.json +1 -1
package/errorsMap.js
CHANGED
|
@@ -50,7 +50,8 @@ const STATIC_ERRORS = [
|
|
|
50
50
|
{ errorCode: 1037, errorKey: 'bonusConfirmationRequired', httpCode: 404, description: 'For bonus activation confirmation of personal user data requited.' },
|
|
51
51
|
{ errorCode: 1038, errorKey: 'promoNotFound', httpCode: 404, description: 'Promo was not found.' },
|
|
52
52
|
{ errorCode: 1039, errorKey: 'badSignature', httpCode: 401, description: 'Untrusted request, bad signature.' },
|
|
53
|
-
{ errorCode: 1040, errorKey: 'mailTemplateNotFound', httpCode: 404, description: 'Email template was not found.' }
|
|
53
|
+
{ errorCode: 1040, errorKey: 'mailTemplateNotFound', httpCode: 404, description: 'Email template was not found.' },
|
|
54
|
+
{ errorCode: 1040, errorKey: 'domainNotFound', httpCode: 404, description: 'Domain was not found.' }
|
|
54
55
|
];
|
|
55
56
|
|
|
56
57
|
const STATIC_BY_KEY = Object.fromEntries(STATIC_ERRORS.map(e => [e.errorKey, e]));
|
|
@@ -89,24 +90,23 @@ const api = {
|
|
|
89
90
|
list: () => [...STATIC_ERRORS]
|
|
90
91
|
};
|
|
91
92
|
|
|
92
|
-
// --- Dynamic shortcuts: errorsMap.<errorKey>() returns
|
|
93
|
-
// This keeps old style calls working across all services without hand-written wrappers.
|
|
93
|
+
// --- Dynamic shortcuts: errorsMap.<errorKey>() returns OBJECT, not string ---
|
|
94
94
|
module.exports = new Proxy(api, {
|
|
95
95
|
get(target, prop, receiver) {
|
|
96
96
|
// If method exists (get, getString, etc.) → return it
|
|
97
97
|
if (prop in target) return Reflect.get(target, prop, receiver);
|
|
98
98
|
|
|
99
|
-
// If property is a known errorKey → return a function () =>
|
|
99
|
+
// If property is a known errorKey → return a function () => get(errorKey)
|
|
100
100
|
if (typeof prop === 'string' && STATIC_BY_KEY[prop]) {
|
|
101
|
-
return () => target.
|
|
101
|
+
return () => target.get(prop); // ✅ return object, not string
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
// Otherwise
|
|
104
|
+
// Otherwise undefined
|
|
105
105
|
return undefined;
|
|
106
106
|
},
|
|
107
107
|
|
|
108
|
-
// Optional: make `prop in errorsMap` truthy for known keys
|
|
109
108
|
has(target, prop) {
|
|
110
109
|
return prop in target || (typeof prop === 'string' && !!STATIC_BY_KEY[prop]);
|
|
111
110
|
}
|
|
112
111
|
});
|
|
112
|
+
|
package/logger.js
CHANGED
|
@@ -74,6 +74,24 @@ function serializeJSON(level, message, extra = {}) {
|
|
|
74
74
|
function colorize(colorCode, icon, message, label) {
|
|
75
75
|
return `${colorCode}${icon} ${message} [${label}] \x1b[0m`;
|
|
76
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Safe stringify with support for Set, Map, Error, Buffer, etc.
|
|
79
|
+
* @param {any} value
|
|
80
|
+
* @returns {string}
|
|
81
|
+
*/
|
|
82
|
+
function safeStringify(value) {
|
|
83
|
+
return JSON.stringify(
|
|
84
|
+
value,
|
|
85
|
+
(key, val) => {
|
|
86
|
+
if (val instanceof Set) return Array.from(val); // convert Set → array
|
|
87
|
+
if (val instanceof Map) return Object.fromEntries(val); // Map → object
|
|
88
|
+
if (val instanceof Error) return { name: val.name, message: val.message, stack: val.stack };
|
|
89
|
+
if (Buffer.isBuffer(val)) return `Buffer[${val.length}]`;
|
|
90
|
+
return val;
|
|
91
|
+
},
|
|
92
|
+
2 // pretty print with 2 spaces (optional)
|
|
93
|
+
);
|
|
94
|
+
}
|
|
77
95
|
|
|
78
96
|
/**
|
|
79
97
|
* Print a debug payload (object) as magenta JSON or structured JSON in prod.
|
|
@@ -81,16 +99,14 @@ function colorize(colorCode, icon, message, label) {
|
|
|
81
99
|
*/
|
|
82
100
|
function debug(data) {
|
|
83
101
|
const { isProd } = resolveContext();
|
|
102
|
+
const text = safeStringify(data);
|
|
103
|
+
|
|
84
104
|
if (isProd) {
|
|
85
105
|
asyncWrite('stdout', serializeJSON('debug', 'debug payload', { data }));
|
|
86
106
|
} else {
|
|
87
|
-
|
|
88
|
-
try { return JSON.stringify(data); } catch { return String(data); }
|
|
89
|
-
})();
|
|
90
|
-
asyncWrite('stdout', colorize('\x1b[35m', '🐛', payload, 'DEBUG'));
|
|
107
|
+
asyncWrite('stdout', colorize('\x1b[35m', '🐛', text, 'DEBUG'));
|
|
91
108
|
}
|
|
92
109
|
}
|
|
93
|
-
|
|
94
110
|
/**
|
|
95
111
|
* Print a green success note.
|
|
96
112
|
* @param {string|number|boolean} message
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "b2b-platform-utils",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.11",
|
|
4
4
|
"description": "Shared utilities for Node.js microservices: errors map, local cache, logger, numbers, dates, filesystem, media optimization, paginator, slugger, crypto wrapper, sanitize HTML, sorting.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"license": "KingSizer",
|