@natyapp/meta 1.6.7 → 1.7.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/.github/copilot-instructions.md +1540 -0
- package/README.md +122 -1
- package/dist/index.d.ts +7 -2
- package/dist/index.js +11 -2
- package/dist/interfaces/IConnection.d.ts +2 -2
- package/dist/interfaces/ILog.d.ts +2 -2
- package/dist/interfaces/ILogger.d.ts +62 -0
- package/dist/interfaces/ILogger.js +2 -0
- package/dist/interfaces/ISdk.d.ts +4 -2
- package/dist/interfaces/IWebhook.d.ts +2 -2
- package/dist/interfaces/index.d.ts +1 -0
- package/dist/interfaces/index.js +1 -0
- package/dist/queue/messageQueue.d.ts +1 -1
- package/dist/queue/messageQueue.js +45 -0
- package/dist/routes/webhooks/methods/connection.js +78 -11
- package/dist/routes/webhooks/methods/messages.js +18 -3
- package/dist/services/axiosInstances.d.ts +14 -5
- package/dist/services/axiosInstances.js +111 -23
- package/dist/services/middlewares/validations.d.ts +2 -2
- package/dist/services/middlewares/validations.js +1 -2
- package/dist/services/mutations/connection.js +1 -1
- package/dist/services/mutations/logs.js +1 -1
- package/dist/services/mutations/messages.js +1 -1
- package/dist/services/mutations/validation.d.ts +1 -1
- package/dist/services/mutations/validation.js +1 -1
- package/dist/services/mutations/webhooks.js +1 -1
- package/dist/types/logs.d.ts +1 -1
- package/dist/types/requestTypes.d.ts +2 -0
- package/dist/useCases/connection/index.d.ts +9 -9
- package/dist/useCases/connection/index.js +156 -7
- package/dist/useCases/events/NatyEvents.d.ts +4 -2
- package/dist/useCases/events/NatyEvents.js +13 -1
- package/dist/useCases/log/index.d.ts +5 -5
- package/dist/useCases/log/index.js +65 -3
- package/dist/useCases/message/whatsappResponse.d.ts +33 -23
- package/dist/useCases/message/whatsappResponse.js +655 -109
- package/dist/useCases/messages/index.d.ts +5 -5
- package/dist/useCases/messages/index.js +66 -3
- package/dist/useCases/sdk/index.d.ts +8 -4
- package/dist/useCases/sdk/index.js +38 -6
- package/dist/useCases/webhook/index.d.ts +9 -9
- package/dist/useCases/webhook/index.js +154 -7
- package/dist/utils/consoleLogger.d.ts +20 -0
- package/dist/utils/consoleLogger.js +51 -0
- package/dist/utils/index.d.ts +6 -0
- package/dist/utils/index.js +6 -0
- package/dist/utils/loggerContext.d.ts +57 -0
- package/dist/utils/loggerContext.js +90 -0
- package/dist/utils/methodContext.d.ts +34 -0
- package/dist/utils/methodContext.js +48 -0
- package/dist/utils/parseError.d.ts +12 -0
- package/dist/utils/parseError.js +27 -3
- package/dist/utils/pinoAdapter.d.ts +30 -0
- package/dist/utils/pinoAdapter.js +68 -0
- package/dist/utils/sanitize.d.ts +42 -0
- package/dist/utils/sanitize.js +120 -0
- package/dist/utils/tryCatch.d.ts +10 -1
- package/dist/utils/tryCatch.js +40 -5
- package/docs/01-visao-geral.md +355 -0
- package/docs/02-contexto-negocio.md +596 -0
- package/docs/03-arquitetura.md +925 -0
- package/docs/04-fluxos-funcionais.md +887 -0
- package/docs/05-integracoes.md +960 -0
- package/docs/06-entidades.md +849 -0
- package/docs/07-guia-pratico.md +1133 -0
- package/docs/08-troubleshooting.md +816 -0
- package/docs/README.md +125 -0
- package/examples/logger-example.ts +279 -0
- package/package.json +2 -2
- /package/dist/{Entities → entities}/Logs.d.ts +0 -0
- /package/dist/{Entities → entities}/Logs.js +0 -0
- /package/dist/{Entities → entities}/connection.d.ts +0 -0
- /package/dist/{Entities → entities}/connection.js +0 -0
- /package/dist/{Entities → entities}/errorLogs.d.ts +0 -0
- /package/dist/{Entities → entities}/errorLogs.js +0 -0
- /package/dist/{Entities → entities}/index.d.ts +0 -0
- /package/dist/{Entities → entities}/index.js +0 -0
- /package/dist/{Entities → entities}/messages.d.ts +0 -0
- /package/dist/{Entities → entities}/messages.js +0 -0
- /package/dist/{Entities → entities}/webhooks.d.ts +0 -0
- /package/dist/{Entities → entities}/webhooks.js +0 -0
- /package/dist/{Entities → entities}/whatsappMessage.d.ts +0 -0
- /package/dist/{Entities → entities}/whatsappMessage.js +0 -0
- /package/dist/{Errors → errors}/Either.d.ts +0 -0
- /package/dist/{Errors → errors}/Either.js +0 -0
- /package/dist/{Errors → errors}/ErrorHandling.d.ts +0 -0
- /package/dist/{Errors → errors}/ErrorHandling.js +0 -0
- /package/dist/{Errors → errors}/index.d.ts +0 -0
- /package/dist/{Errors → errors}/index.js +0 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Utility for generating consistent method context strings for logging.
|
|
4
|
+
*
|
|
5
|
+
* @module methodContext
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { getMethodContext } from '../utils';
|
|
9
|
+
*
|
|
10
|
+
* logger.info("Operation started", {
|
|
11
|
+
* operation: getMethodContext("WhatsappResponse", "send_text_message"),
|
|
12
|
+
* // ... other metadata
|
|
13
|
+
* });
|
|
14
|
+
* // Logs: operation: "WhatsappResponse.send_text_message"
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.getMethodContext = void 0;
|
|
19
|
+
/**
|
|
20
|
+
* Generates a standardized method context string for logging.
|
|
21
|
+
*
|
|
22
|
+
* @param className - The name of the class or module (e.g., "WhatsappResponse", "Webhooks")
|
|
23
|
+
* @param methodName - The name of the method (e.g., "send_text_message", "getSingle")
|
|
24
|
+
* @returns A formatted string in the format "ClassName.methodName"
|
|
25
|
+
*
|
|
26
|
+
* @throws {Error} If className or methodName is empty or whitespace-only
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* getMethodContext("WhatsappResponse", "send_text_message")
|
|
31
|
+
* // Returns: "WhatsappResponse.send_text_message"
|
|
32
|
+
*
|
|
33
|
+
* getMethodContext("Webhooks", "getSingle")
|
|
34
|
+
* // Returns: "Webhooks.getSingle"
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
function getMethodContext(className, methodName) {
|
|
38
|
+
const trimmedClass = className === null || className === void 0 ? void 0 : className.trim();
|
|
39
|
+
const trimmedMethod = methodName === null || methodName === void 0 ? void 0 : methodName.trim();
|
|
40
|
+
if (!trimmedClass) {
|
|
41
|
+
throw new Error("className cannot be empty");
|
|
42
|
+
}
|
|
43
|
+
if (!trimmedMethod) {
|
|
44
|
+
throw new Error("methodName cannot be empty");
|
|
45
|
+
}
|
|
46
|
+
return `${trimmedClass}.${trimmedMethod}`;
|
|
47
|
+
}
|
|
48
|
+
exports.getMethodContext = getMethodContext;
|
|
@@ -2,4 +2,16 @@ export declare const parseError: (err: any, fallbackMessage?: string) => {
|
|
|
2
2
|
message: string;
|
|
3
3
|
code: any;
|
|
4
4
|
};
|
|
5
|
+
/**
|
|
6
|
+
* Returns a parsed error as Either monad instead of throwing.
|
|
7
|
+
* Use this for functional error handling following SDK architecture.
|
|
8
|
+
* @param err - The error to parse
|
|
9
|
+
* @param contextMessage - Optional context to prepend to error message
|
|
10
|
+
* @returns Either with Error instance (not ErrorHandling)
|
|
11
|
+
*/
|
|
12
|
+
export declare const returnParsedError: (err: any, contextMessage?: string) => import("../errors/Either").isError<Error>;
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated Use returnParsedError instead to follow Either pattern.
|
|
15
|
+
* This function throws exceptions which violates SDK architecture.
|
|
16
|
+
*/
|
|
5
17
|
export declare const throwParsedError: (err: any, contextMessage?: string) => never;
|
package/dist/utils/parseError.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.throwParsedError = exports.parseError = void 0;
|
|
4
|
-
const
|
|
3
|
+
exports.throwParsedError = exports.returnParsedError = exports.parseError = void 0;
|
|
4
|
+
const Either_1 = require("../errors/Either");
|
|
5
5
|
const parseError = (err, fallbackMessage = "Erro inesperado") => {
|
|
6
6
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
7
7
|
const details = ((_d = (_c = (_b = (_a = err === null || err === void 0 ? void 0 : err.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.error) === null || _c === void 0 ? void 0 : _c.error_data) === null || _d === void 0 ? void 0 : _d.details) ||
|
|
@@ -19,8 +19,32 @@ const parseError = (err, fallbackMessage = "Erro inesperado") => {
|
|
|
19
19
|
};
|
|
20
20
|
};
|
|
21
21
|
exports.parseError = parseError;
|
|
22
|
+
/**
|
|
23
|
+
* Returns a parsed error as Either monad instead of throwing.
|
|
24
|
+
* Use this for functional error handling following SDK architecture.
|
|
25
|
+
* @param err - The error to parse
|
|
26
|
+
* @param contextMessage - Optional context to prepend to error message
|
|
27
|
+
* @returns Either with Error instance (not ErrorHandling)
|
|
28
|
+
*/
|
|
29
|
+
const returnParsedError = (err, contextMessage) => {
|
|
30
|
+
const { message, code } = (0, exports.parseError)(err);
|
|
31
|
+
const errorMsg = `${contextMessage ? contextMessage + ": " : ""}${message}`;
|
|
32
|
+
const error = new Error(errorMsg);
|
|
33
|
+
error.code = code; // Attach code as property
|
|
34
|
+
return (0, Either_1.throwError)(error);
|
|
35
|
+
};
|
|
36
|
+
exports.returnParsedError = returnParsedError;
|
|
37
|
+
/**
|
|
38
|
+
* @deprecated Use returnParsedError instead to follow Either pattern.
|
|
39
|
+
* This function throws exceptions which violates SDK architecture.
|
|
40
|
+
*/
|
|
22
41
|
const throwParsedError = (err, contextMessage) => {
|
|
42
|
+
if (process.env.NODE_ENV !== "production") {
|
|
43
|
+
console.warn("[DEPRECATED] throwParsedError is deprecated. Use returnParsedError instead to follow Either pattern.");
|
|
44
|
+
}
|
|
23
45
|
const { message, code } = (0, exports.parseError)(err);
|
|
24
|
-
|
|
46
|
+
const error = new Error(`${contextMessage ? contextMessage + ": " : ""}${message}`);
|
|
47
|
+
error.code = code;
|
|
48
|
+
throw error;
|
|
25
49
|
};
|
|
26
50
|
exports.throwParsedError = throwParsedError;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ILogger } from "../interfaces/ILogger";
|
|
2
|
+
/**
|
|
3
|
+
* Pino logger adapter
|
|
4
|
+
*
|
|
5
|
+
* Adapts a Pino logger instance to work with the SDK's ILogger interface.
|
|
6
|
+
* Note: Pino uses a different parameter order (meta, message) vs our interface (message, meta).
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import pino from 'pino';
|
|
11
|
+
* import { createPinoAdapter } from '@natyapp/meta/utils/pinoAdapter';
|
|
12
|
+
* import NatyMeta from '@natyapp/meta';
|
|
13
|
+
*
|
|
14
|
+
* const pinoLogger = pino({
|
|
15
|
+
* level: 'debug',
|
|
16
|
+
* transport: {
|
|
17
|
+
* target: 'pino-pretty',
|
|
18
|
+
* options: { colorize: true }
|
|
19
|
+
* }
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* const sdk = new NatyMeta({
|
|
23
|
+
* logger: createPinoAdapter(pinoLogger)
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @param pinoLogger - A Pino logger instance
|
|
28
|
+
* @returns An ILogger-compatible adapter
|
|
29
|
+
*/
|
|
30
|
+
export declare function createPinoAdapter(pinoLogger: any): ILogger;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPinoAdapter = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Pino logger adapter
|
|
6
|
+
*
|
|
7
|
+
* Adapts a Pino logger instance to work with the SDK's ILogger interface.
|
|
8
|
+
* Note: Pino uses a different parameter order (meta, message) vs our interface (message, meta).
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import pino from 'pino';
|
|
13
|
+
* import { createPinoAdapter } from '@natyapp/meta/utils/pinoAdapter';
|
|
14
|
+
* import NatyMeta from '@natyapp/meta';
|
|
15
|
+
*
|
|
16
|
+
* const pinoLogger = pino({
|
|
17
|
+
* level: 'debug',
|
|
18
|
+
* transport: {
|
|
19
|
+
* target: 'pino-pretty',
|
|
20
|
+
* options: { colorize: true }
|
|
21
|
+
* }
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* const sdk = new NatyMeta({
|
|
25
|
+
* logger: createPinoAdapter(pinoLogger)
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @param pinoLogger - A Pino logger instance
|
|
30
|
+
* @returns An ILogger-compatible adapter
|
|
31
|
+
*/
|
|
32
|
+
function createPinoAdapter(pinoLogger) {
|
|
33
|
+
return {
|
|
34
|
+
debug(message, meta) {
|
|
35
|
+
if (meta) {
|
|
36
|
+
pinoLogger.debug(meta, message);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
pinoLogger.debug(message);
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
info(message, meta) {
|
|
43
|
+
if (meta) {
|
|
44
|
+
pinoLogger.info(meta, message);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
pinoLogger.info(message);
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
warn(message, meta) {
|
|
51
|
+
if (meta) {
|
|
52
|
+
pinoLogger.warn(meta, message);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
pinoLogger.warn(message);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
error(message, meta) {
|
|
59
|
+
if (meta) {
|
|
60
|
+
pinoLogger.error(meta, message);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
pinoLogger.error(message);
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
exports.createPinoAdapter = createPinoAdapter;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utilities for sanitizing sensitive data in logs
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Sanitize phone number by masking the last 4 digits
|
|
6
|
+
* @param phoneNumber - Phone number in E.164 format (e.g., +5511987654321)
|
|
7
|
+
* @returns Masked phone number (e.g., +5511***4321)
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* sanitizePhoneNumber("+5511987654321") // Returns: "+5511***4321"
|
|
11
|
+
* sanitizePhoneNumber("+1234567890") // Returns: "+123456***7890"
|
|
12
|
+
*/
|
|
13
|
+
export declare function sanitizePhoneNumber(phoneNumber: string | undefined | null): string;
|
|
14
|
+
/**
|
|
15
|
+
* Sanitize access token by replacing with placeholder
|
|
16
|
+
* @param token - Access token or credential
|
|
17
|
+
* @returns Sanitized placeholder
|
|
18
|
+
*/
|
|
19
|
+
export declare function sanitizeToken(token: string | undefined | null): string;
|
|
20
|
+
/**
|
|
21
|
+
* Sanitize metadata object by masking sensitive fields
|
|
22
|
+
* @param metadata - Metadata object to sanitize
|
|
23
|
+
* @param sensitiveFields - Array of field names to sanitize (default: common sensitive fields)
|
|
24
|
+
* @returns Sanitized metadata object
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* sanitizeMetadata({
|
|
28
|
+
* phone: "+5511987654321",
|
|
29
|
+
* token: "abc123",
|
|
30
|
+
* name: "John"
|
|
31
|
+
* })
|
|
32
|
+
* // Returns: { phone: "+5511***4321", token: "[REDACTED]", name: "John" }
|
|
33
|
+
*/
|
|
34
|
+
export declare function sanitizeMetadata(metadata: Record<string, any> | undefined | null, sensitiveFields?: string[]): Record<string, any>;
|
|
35
|
+
/**
|
|
36
|
+
* Sanitize message body content for logging
|
|
37
|
+
* Returns type and size instead of actual content
|
|
38
|
+
*/
|
|
39
|
+
export declare function sanitizeMessageBody(body: string | undefined | null): {
|
|
40
|
+
type: string;
|
|
41
|
+
size: number;
|
|
42
|
+
};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Utilities for sanitizing sensitive data in logs
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.sanitizeMessageBody = exports.sanitizeMetadata = exports.sanitizeToken = exports.sanitizePhoneNumber = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Sanitize phone number by masking the last 4 digits
|
|
9
|
+
* @param phoneNumber - Phone number in E.164 format (e.g., +5511987654321)
|
|
10
|
+
* @returns Masked phone number (e.g., +5511***4321)
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* sanitizePhoneNumber("+5511987654321") // Returns: "+5511***4321"
|
|
14
|
+
* sanitizePhoneNumber("+1234567890") // Returns: "+123456***7890"
|
|
15
|
+
*/
|
|
16
|
+
function sanitizePhoneNumber(phoneNumber) {
|
|
17
|
+
if (!phoneNumber)
|
|
18
|
+
return "[NO_PHONE]";
|
|
19
|
+
// Remove non-digit characters except leading +
|
|
20
|
+
const cleaned = phoneNumber.toString().trim();
|
|
21
|
+
if (cleaned.length < 8) {
|
|
22
|
+
// Too short to mask meaningfully
|
|
23
|
+
return "***" + cleaned.slice(-2);
|
|
24
|
+
}
|
|
25
|
+
// Keep country code and first digits, mask middle, show last 4
|
|
26
|
+
const lastFour = cleaned.slice(-4);
|
|
27
|
+
const prefix = cleaned.slice(0, -7); // Keep country code + area
|
|
28
|
+
return `${prefix}***${lastFour}`;
|
|
29
|
+
}
|
|
30
|
+
exports.sanitizePhoneNumber = sanitizePhoneNumber;
|
|
31
|
+
/**
|
|
32
|
+
* Sanitize access token by replacing with placeholder
|
|
33
|
+
* @param token - Access token or credential
|
|
34
|
+
* @returns Sanitized placeholder
|
|
35
|
+
*/
|
|
36
|
+
function sanitizeToken(token) {
|
|
37
|
+
if (!token)
|
|
38
|
+
return "[NO_TOKEN]";
|
|
39
|
+
return "[REDACTED]";
|
|
40
|
+
}
|
|
41
|
+
exports.sanitizeToken = sanitizeToken;
|
|
42
|
+
/**
|
|
43
|
+
* Sanitize metadata object by masking sensitive fields
|
|
44
|
+
* @param metadata - Metadata object to sanitize
|
|
45
|
+
* @param sensitiveFields - Array of field names to sanitize (default: common sensitive fields)
|
|
46
|
+
* @returns Sanitized metadata object
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* sanitizeMetadata({
|
|
50
|
+
* phone: "+5511987654321",
|
|
51
|
+
* token: "abc123",
|
|
52
|
+
* name: "John"
|
|
53
|
+
* })
|
|
54
|
+
* // Returns: { phone: "+5511***4321", token: "[REDACTED]", name: "John" }
|
|
55
|
+
*/
|
|
56
|
+
function sanitizeMetadata(metadata, sensitiveFields = [
|
|
57
|
+
"phone",
|
|
58
|
+
"phoneNumber",
|
|
59
|
+
"phone_number",
|
|
60
|
+
"to",
|
|
61
|
+
"from",
|
|
62
|
+
"token",
|
|
63
|
+
"accessToken",
|
|
64
|
+
"access_token",
|
|
65
|
+
"systemUserAccessToken",
|
|
66
|
+
"password",
|
|
67
|
+
"secret",
|
|
68
|
+
"apiKey",
|
|
69
|
+
"api_key",
|
|
70
|
+
]) {
|
|
71
|
+
if (!metadata || typeof metadata !== "object") {
|
|
72
|
+
return {};
|
|
73
|
+
}
|
|
74
|
+
const sanitized = {};
|
|
75
|
+
for (const [key, value] of Object.entries(metadata)) {
|
|
76
|
+
const lowerKey = key.toLowerCase();
|
|
77
|
+
// Check if field should be sanitized
|
|
78
|
+
const shouldSanitize = sensitiveFields.some((field) => lowerKey.includes(field.toLowerCase()));
|
|
79
|
+
if (shouldSanitize) {
|
|
80
|
+
if (lowerKey.includes("phone") ||
|
|
81
|
+
lowerKey === "to" ||
|
|
82
|
+
lowerKey === "from") {
|
|
83
|
+
sanitized[key] = sanitizePhoneNumber(value);
|
|
84
|
+
}
|
|
85
|
+
else if (lowerKey.includes("token") ||
|
|
86
|
+
lowerKey.includes("password") ||
|
|
87
|
+
lowerKey.includes("secret") ||
|
|
88
|
+
lowerKey.includes("key")) {
|
|
89
|
+
sanitized[key] = sanitizeToken(value);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
sanitized[key] = "[REDACTED]";
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else if (typeof value === "object" &&
|
|
96
|
+
value !== null &&
|
|
97
|
+
!Array.isArray(value)) {
|
|
98
|
+
// Recursively sanitize nested objects
|
|
99
|
+
sanitized[key] = sanitizeMetadata(value, sensitiveFields);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
sanitized[key] = value;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return sanitized;
|
|
106
|
+
}
|
|
107
|
+
exports.sanitizeMetadata = sanitizeMetadata;
|
|
108
|
+
/**
|
|
109
|
+
* Sanitize message body content for logging
|
|
110
|
+
* Returns type and size instead of actual content
|
|
111
|
+
*/
|
|
112
|
+
function sanitizeMessageBody(body) {
|
|
113
|
+
if (!body)
|
|
114
|
+
return { type: "empty", size: 0 };
|
|
115
|
+
return {
|
|
116
|
+
type: typeof body,
|
|
117
|
+
size: body.length,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
exports.sanitizeMessageBody = sanitizeMessageBody;
|
package/dist/utils/tryCatch.d.ts
CHANGED
|
@@ -1 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
import { Either } from "../errors/Either";
|
|
2
|
+
export declare const Try: <TSuccess, TError = string>(fn: (...args: any[]) => Promise<Either<TError, TSuccess>>, ...args: any[]) => Promise<Either<TError, TSuccess>>;
|
|
3
|
+
/**
|
|
4
|
+
* Synchronous version of Try wrapper for operations that throw synchronously.
|
|
5
|
+
* Catches any thrown error and converts to Either monad.
|
|
6
|
+
* @param fn - Synchronous function that may throw
|
|
7
|
+
* @param args - Arguments to pass to function
|
|
8
|
+
* @returns Either with error or success value
|
|
9
|
+
*/
|
|
10
|
+
export declare const TrySync: <TSuccess, TError = string>(fn: (...args: any[]) => TSuccess, ...args: any[]) => Either<TError, TSuccess>;
|
package/dist/utils/tryCatch.js
CHANGED
|
@@ -1,19 +1,54 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Try = void 0;
|
|
4
|
-
const Either_1 = require("../
|
|
5
|
-
const
|
|
3
|
+
exports.TrySync = exports.Try = void 0;
|
|
4
|
+
const Either_1 = require("../errors/Either");
|
|
5
|
+
const loggerContext_1 = require("./loggerContext");
|
|
6
|
+
const parseError_1 = require("./parseError");
|
|
7
|
+
const methodContext_1 = require("./methodContext");
|
|
6
8
|
const Try = async (fn, ...args) => {
|
|
7
9
|
var _a;
|
|
8
10
|
try {
|
|
11
|
+
loggerContext_1.logger.debug("Try: Executing operation", {
|
|
12
|
+
operation: (0, methodContext_1.getMethodContext)("Try", fn.name || "anonymous"),
|
|
13
|
+
argsCount: args.length,
|
|
14
|
+
});
|
|
9
15
|
const resolver = await fn.apply(null, args);
|
|
10
16
|
if ((0, Either_1.isSuccess)(resolver))
|
|
11
17
|
(0, Either_1.unwrapEither)(resolver);
|
|
12
18
|
return resolver;
|
|
13
19
|
}
|
|
14
20
|
catch (err) {
|
|
15
|
-
const
|
|
16
|
-
|
|
21
|
+
const errorDetails = (0, parseError_1.parseError)(err, "Operation failed");
|
|
22
|
+
loggerContext_1.logger.error("Try: Operation failed", {
|
|
23
|
+
operation: (0, methodContext_1.getMethodContext)("Try", fn.name || "anonymous"),
|
|
24
|
+
error: errorDetails.message,
|
|
25
|
+
code: errorDetails.code,
|
|
26
|
+
});
|
|
27
|
+
return (0, Either_1.throwError)(((_a = err === null || err === void 0 ? void 0 : err.response) === null || _a === void 0 ? void 0 : _a.data) || errorDetails.message);
|
|
17
28
|
}
|
|
18
29
|
};
|
|
19
30
|
exports.Try = Try;
|
|
31
|
+
/**
|
|
32
|
+
* Synchronous version of Try wrapper for operations that throw synchronously.
|
|
33
|
+
* Catches any thrown error and converts to Either monad.
|
|
34
|
+
* @param fn - Synchronous function that may throw
|
|
35
|
+
* @param args - Arguments to pass to function
|
|
36
|
+
* @returns Either with error or success value
|
|
37
|
+
*/
|
|
38
|
+
const TrySync = (fn, ...args) => {
|
|
39
|
+
var _a;
|
|
40
|
+
try {
|
|
41
|
+
const result = fn.apply(null, args);
|
|
42
|
+
return (0, Either_1.throwSuccess)(result);
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
const errorDetails = (0, parseError_1.parseError)(err, "Operation failed");
|
|
46
|
+
loggerContext_1.logger.error("TrySync: Operation failed", {
|
|
47
|
+
operation: (0, methodContext_1.getMethodContext)("TrySync", fn.name || "anonymous"),
|
|
48
|
+
error: errorDetails.message,
|
|
49
|
+
code: errorDetails.code,
|
|
50
|
+
});
|
|
51
|
+
return (0, Either_1.throwError)(((_a = err === null || err === void 0 ? void 0 : err.response) === null || _a === void 0 ? void 0 : _a.data) || errorDetails.message);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
exports.TrySync = TrySync;
|