@green-api/greenapi-integration 0.5.0 → 0.6.1
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/LICENSE +21 -0
- package/README.md +1080 -858
- package/README.ru.md +1092 -870
- package/dist/core/base-adapter.d.ts +3 -1
- package/dist/core/base-adapter.js +11 -3
- package/dist/core/green-api.client.d.ts +1 -0
- package/dist/core/green-api.client.js +10 -0
- package/dist/core/guard.d.ts +2 -1
- package/dist/core/guard.js +4 -1
- package/dist/core/logger.d.ts +128 -0
- package/dist/core/logger.js +217 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -1
- package/dist/types/types.d.ts +15 -3
- package/package.json +42 -41
|
@@ -2,6 +2,7 @@ import { GreenApiClient } from "./green-api.client";
|
|
|
2
2
|
import { MessageTransformer } from "./message-transformer";
|
|
3
3
|
import { BaseUser, ForwardMessagesResponse, GreenApiWebhook, Instance, SendResponse, StateInstanceWebhook, WebhookType } from "../types/types";
|
|
4
4
|
import { StorageProvider } from "./storage-provider";
|
|
5
|
+
import { GreenApiLogger } from "./logger";
|
|
5
6
|
/**
|
|
6
7
|
* Base adapter for platform integrations with GREEN-API.
|
|
7
8
|
* This class handles the core integration logic between your platform and GREEN-API's WhatsApp gateway.
|
|
@@ -29,13 +30,14 @@ import { StorageProvider } from "./storage-provider";
|
|
|
29
30
|
export declare abstract class BaseAdapter<TPlatformWebhook, TPlatformMessage, TUser extends BaseUser = BaseUser, TInstance extends Instance = Instance> {
|
|
30
31
|
protected transformer: MessageTransformer<TPlatformWebhook, TPlatformMessage>;
|
|
31
32
|
protected storage: StorageProvider<TUser, TInstance>;
|
|
33
|
+
protected readonly gaLogger: GreenApiLogger;
|
|
32
34
|
/**
|
|
33
35
|
* Creates an instance of BaseAdapter.
|
|
34
36
|
*
|
|
35
37
|
* @param transformer - Message transformer for converting between platform and GREEN-API formats
|
|
36
38
|
* @param storage - Storage provider for user and instance data
|
|
37
39
|
*/
|
|
38
|
-
constructor(transformer: MessageTransformer<TPlatformWebhook, TPlatformMessage>, storage: StorageProvider<TUser, TInstance>);
|
|
40
|
+
protected constructor(transformer: MessageTransformer<TPlatformWebhook, TPlatformMessage>, storage: StorageProvider<TUser, TInstance>);
|
|
39
41
|
/**
|
|
40
42
|
* Sends a message to your platform. This method must be implemented to define how
|
|
41
43
|
* messages are sent to your specific platform.
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.BaseAdapter = void 0;
|
|
4
4
|
const green_api_client_1 = require("./green-api.client");
|
|
5
5
|
const errors_1 = require("./errors");
|
|
6
|
+
const logger_1 = require("./logger");
|
|
6
7
|
/**
|
|
7
8
|
* Base adapter for platform integrations with GREEN-API.
|
|
8
9
|
* This class handles the core integration logic between your platform and GREEN-API's WhatsApp gateway.
|
|
@@ -37,6 +38,7 @@ class BaseAdapter {
|
|
|
37
38
|
constructor(transformer, storage) {
|
|
38
39
|
this.transformer = transformer;
|
|
39
40
|
this.storage = storage;
|
|
41
|
+
this.gaLogger = logger_1.GreenApiLogger.getInstance(this.constructor.name);
|
|
40
42
|
}
|
|
41
43
|
/**
|
|
42
44
|
* Handles instance state change webhooks from GREEN-API.
|
|
@@ -86,6 +88,7 @@ class BaseAdapter {
|
|
|
86
88
|
*/
|
|
87
89
|
async handlePlatformWebhook(message, idInstance) {
|
|
88
90
|
try {
|
|
91
|
+
this.gaLogger.info("Handling platform webhook", { platformWebhook: message, idInstance });
|
|
89
92
|
const instance = await this.storage.getInstance(idInstance);
|
|
90
93
|
if (!instance) {
|
|
91
94
|
throw new errors_1.IntegrationError("Instance not found", "INSTANCE_NOT_FOUND", 404);
|
|
@@ -124,10 +127,15 @@ class BaseAdapter {
|
|
|
124
127
|
* @throws {IntegrationError} If webhook handling fails
|
|
125
128
|
*/
|
|
126
129
|
async handleGreenApiWebhook(webhook, allowedTypes) {
|
|
127
|
-
if (!allowedTypes.includes(webhook.typeWebhook)) {
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
130
|
try {
|
|
131
|
+
this.gaLogger.info("Handling GREEN-API webhook", { webhook, allowedTypes });
|
|
132
|
+
if (!allowedTypes.includes(webhook.typeWebhook)) {
|
|
133
|
+
this.gaLogger.warn(`Skipping GREEN-API webhook because the ${webhook.typeWebhook} is not allowed`, {
|
|
134
|
+
webhook,
|
|
135
|
+
allowedTypes,
|
|
136
|
+
});
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
131
139
|
if (webhook.typeWebhook === "stateInstanceChanged") {
|
|
132
140
|
await this.handleStateInstanceWebhook(webhook);
|
|
133
141
|
}
|
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.GreenApiClient = void 0;
|
|
7
7
|
const axios_1 = __importDefault(require("axios"));
|
|
8
8
|
const errors_1 = require("./errors");
|
|
9
|
+
const logger_1 = require("./logger");
|
|
9
10
|
/**
|
|
10
11
|
* Client for direct interaction with GREEN-API's WhatsApp gateway.
|
|
11
12
|
* Provides methods for sending messages, managing instances, and handling files.
|
|
@@ -35,6 +36,7 @@ class GreenApiClient {
|
|
|
35
36
|
constructor(instance) {
|
|
36
37
|
this.instance = instance;
|
|
37
38
|
this.baseUrl = "https://api.green-api.com";
|
|
39
|
+
this.gaLogger = logger_1.GreenApiLogger.getInstance(GreenApiClient.name);
|
|
38
40
|
this.client = axios_1.default.create({
|
|
39
41
|
baseURL: this.buildUrl(),
|
|
40
42
|
});
|
|
@@ -47,6 +49,14 @@ class GreenApiClient {
|
|
|
47
49
|
}
|
|
48
50
|
async makeRequest(method, endpoint, data, queryParams, config) {
|
|
49
51
|
try {
|
|
52
|
+
this.gaLogger.info("Making a request", {
|
|
53
|
+
idInstance: this.instance.idInstance,
|
|
54
|
+
endpoint,
|
|
55
|
+
method,
|
|
56
|
+
data,
|
|
57
|
+
queryParams,
|
|
58
|
+
config,
|
|
59
|
+
});
|
|
50
60
|
const url = this.buildEndpoint(endpoint) + (queryParams ? "?" + new URLSearchParams(Object.entries(queryParams).map(([key, value]) => [key, value.toString()])).toString() : "");
|
|
51
61
|
const response = await (method === "get"
|
|
52
62
|
? this.client.get(url, config)
|
package/dist/core/guard.d.ts
CHANGED
|
@@ -31,12 +31,13 @@ import { StorageProvider } from "./storage-provider";
|
|
|
31
31
|
*/
|
|
32
32
|
export declare abstract class BaseGreenApiAuthGuard<T extends BaseRequest = BaseRequest> {
|
|
33
33
|
protected storage: StorageProvider;
|
|
34
|
+
private readonly gaLogger;
|
|
34
35
|
/**
|
|
35
36
|
* Creates an instance of BaseGreenApiAuthGuard.
|
|
36
37
|
*
|
|
37
38
|
* @param storage - Storage provider for accessing instance data
|
|
38
39
|
*/
|
|
39
|
-
constructor(storage: StorageProvider);
|
|
40
|
+
protected constructor(storage: StorageProvider);
|
|
40
41
|
/**
|
|
41
42
|
* Validates an incoming webhook request.
|
|
42
43
|
* Checks for presence of authorization token and validates it against instance settings.
|
package/dist/core/guard.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BaseGreenApiAuthGuard = void 0;
|
|
4
4
|
const errors_1 = require("./errors");
|
|
5
|
+
const logger_1 = require("./logger");
|
|
5
6
|
/**
|
|
6
7
|
* Base authentication guard for validating incoming GREEN-API webhooks.
|
|
7
8
|
* Ensures that webhooks are authenticated and come from valid instances.
|
|
@@ -39,6 +40,7 @@ class BaseGreenApiAuthGuard {
|
|
|
39
40
|
*/
|
|
40
41
|
constructor(storage) {
|
|
41
42
|
this.storage = storage;
|
|
43
|
+
this.gaLogger = logger_1.GreenApiLogger.getInstance(this.constructor.name);
|
|
42
44
|
}
|
|
43
45
|
/**
|
|
44
46
|
* Validates an incoming webhook request.
|
|
@@ -56,13 +58,14 @@ class BaseGreenApiAuthGuard {
|
|
|
56
58
|
if (!token) {
|
|
57
59
|
throw new errors_1.AuthenticationError("Authentication header is missing");
|
|
58
60
|
}
|
|
61
|
+
this.gaLogger.info("Request from GREEN-API", { body: request.body });
|
|
59
62
|
const idInstance = request.body?.instanceData?.idInstance;
|
|
60
63
|
if (!idInstance) {
|
|
61
64
|
throw new errors_1.AuthenticationError("Invalid webhook format");
|
|
62
65
|
}
|
|
63
66
|
const instance = await this.storage.getInstance(idInstance);
|
|
64
67
|
if (!instance) {
|
|
65
|
-
throw new errors_1.AuthenticationError(
|
|
68
|
+
throw new errors_1.AuthenticationError(`No instance with such ID ${idInstance}`);
|
|
66
69
|
}
|
|
67
70
|
if (instance.settings?.webhookUrlToken !== token.split(" ")[1]) {
|
|
68
71
|
throw new errors_1.AuthenticationError("Invalid token");
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logger for GREEN-API integration library.
|
|
3
|
+
* Provides structured JSON logging with colored output and error handling.
|
|
4
|
+
* Uses singleton pattern to maintain consistent logging instances across the application.
|
|
5
|
+
*
|
|
6
|
+
* @category Core
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const logger = GreenApiLogger.getInstance("MyComponent");
|
|
11
|
+
*
|
|
12
|
+
* // Basic logging
|
|
13
|
+
* logger.info("Operation successful", { userId: 123 });
|
|
14
|
+
*
|
|
15
|
+
* // Error logging
|
|
16
|
+
* try {
|
|
17
|
+
* // ... some code
|
|
18
|
+
* } catch (error) {
|
|
19
|
+
* logger.logErrorResponse(error, "Failed to process request");
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare class GreenApiLogger {
|
|
24
|
+
private readonly context;
|
|
25
|
+
private static instances;
|
|
26
|
+
private readonly colors;
|
|
27
|
+
/**
|
|
28
|
+
* Private constructor to enforce singleton pattern
|
|
29
|
+
* @param context - The context (usually component name) for this logger instance
|
|
30
|
+
*/
|
|
31
|
+
private constructor();
|
|
32
|
+
/**
|
|
33
|
+
* Gets a logger instance for the specified context.
|
|
34
|
+
* Creates a new instance if one doesn't exist, otherwise returns existing instance.
|
|
35
|
+
*
|
|
36
|
+
* @param context - The context for the logger (default: "Global")
|
|
37
|
+
* @returns Logger instance for the specified context
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const logger = GreenApiLogger.getInstance("MyService");
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
static getInstance(context?: string): GreenApiLogger;
|
|
45
|
+
/**
|
|
46
|
+
* Formats timestamp in locale-specific format
|
|
47
|
+
* @returns Formatted timestamp string
|
|
48
|
+
* @private
|
|
49
|
+
*/
|
|
50
|
+
private formatTimestamp;
|
|
51
|
+
/**
|
|
52
|
+
* Sanitizes values for JSON serialization.
|
|
53
|
+
* Handles special cases like Error objects and BigInt values.
|
|
54
|
+
*
|
|
55
|
+
* @param value - Value to sanitize
|
|
56
|
+
* @returns Sanitized value safe for JSON serialization
|
|
57
|
+
* @private
|
|
58
|
+
*/
|
|
59
|
+
private sanitizeValue;
|
|
60
|
+
/**
|
|
61
|
+
* Creates and outputs a log entry
|
|
62
|
+
* @param level - Log level
|
|
63
|
+
* @param message - Log message
|
|
64
|
+
* @param additionalContext - Additional context data
|
|
65
|
+
* @private
|
|
66
|
+
*/
|
|
67
|
+
private logEntry;
|
|
68
|
+
/**
|
|
69
|
+
* Logs a debug message
|
|
70
|
+
* @param message - Debug message
|
|
71
|
+
* @param context - Additional context data
|
|
72
|
+
*/
|
|
73
|
+
debug(message: string, context?: Record<string, any>): void;
|
|
74
|
+
/**
|
|
75
|
+
* Logs an info message
|
|
76
|
+
* @param message - Info message
|
|
77
|
+
* @param context - Additional context data
|
|
78
|
+
*/
|
|
79
|
+
info(message: string, context?: Record<string, any>): void;
|
|
80
|
+
/**
|
|
81
|
+
* Logs a warning message
|
|
82
|
+
* @param message - Warning message
|
|
83
|
+
* @param context - Additional context data
|
|
84
|
+
*/
|
|
85
|
+
warn(message: string, context?: Record<string, any>): void;
|
|
86
|
+
/**
|
|
87
|
+
* Logs an error message
|
|
88
|
+
* @param message - Error message
|
|
89
|
+
* @param context - Additional context data
|
|
90
|
+
*/
|
|
91
|
+
error(message: string, context?: Record<string, any>): void;
|
|
92
|
+
/**
|
|
93
|
+
* Alternative method for logging info messages
|
|
94
|
+
* @param message - Log message
|
|
95
|
+
* @param context - Context string
|
|
96
|
+
*/
|
|
97
|
+
log(message: string, context?: string): void;
|
|
98
|
+
/**
|
|
99
|
+
* Logs a verbose debug message
|
|
100
|
+
* @param message - Debug message
|
|
101
|
+
* @param context - Context string
|
|
102
|
+
*/
|
|
103
|
+
verbose(message: string, context?: string): void;
|
|
104
|
+
/**
|
|
105
|
+
* Logs a fatal error message
|
|
106
|
+
* @param message - Fatal error message
|
|
107
|
+
* @param context - Additional context data
|
|
108
|
+
*/
|
|
109
|
+
fatal(message: string, context?: Record<string, any>): void;
|
|
110
|
+
/**
|
|
111
|
+
* Logs detailed error information, handling both Axios errors and regular errors.
|
|
112
|
+
* Particularly useful for API errors and exceptions.
|
|
113
|
+
*
|
|
114
|
+
* @param error - Error object (Axios error or regular error)
|
|
115
|
+
* @param context - Error context description
|
|
116
|
+
* @param additionalContext - Additional context data
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```typescript
|
|
120
|
+
* try {
|
|
121
|
+
* await api.request();
|
|
122
|
+
* } catch (error) {
|
|
123
|
+
* logger.logErrorResponse(error, "API Request failed", { requestId: "123" });
|
|
124
|
+
* }
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
logErrorResponse(error: any, context: string, additionalContext?: Record<string, any>): void;
|
|
128
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
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.GreenApiLogger = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
/**
|
|
9
|
+
* Logger for GREEN-API integration library.
|
|
10
|
+
* Provides structured JSON logging with colored output and error handling.
|
|
11
|
+
* Uses singleton pattern to maintain consistent logging instances across the application.
|
|
12
|
+
*
|
|
13
|
+
* @category Core
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const logger = GreenApiLogger.getInstance("MyComponent");
|
|
18
|
+
*
|
|
19
|
+
* // Basic logging
|
|
20
|
+
* logger.info("Operation successful", { userId: 123 });
|
|
21
|
+
*
|
|
22
|
+
* // Error logging
|
|
23
|
+
* try {
|
|
24
|
+
* // ... some code
|
|
25
|
+
* } catch (error) {
|
|
26
|
+
* logger.logErrorResponse(error, "Failed to process request");
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
class GreenApiLogger {
|
|
31
|
+
/**
|
|
32
|
+
* Private constructor to enforce singleton pattern
|
|
33
|
+
* @param context - The context (usually component name) for this logger instance
|
|
34
|
+
*/
|
|
35
|
+
constructor(context) {
|
|
36
|
+
this.context = context;
|
|
37
|
+
this.colors = {
|
|
38
|
+
log: "\x1b[32m", // Same as info (green)
|
|
39
|
+
debug: "\x1b[36m", // Cyan
|
|
40
|
+
info: "\x1b[32m", // Green
|
|
41
|
+
warn: "\x1b[33m", // Yellow
|
|
42
|
+
error: "\x1b[31m", // Red
|
|
43
|
+
fatal: "\x1b[35m", // Magenta/Purple for fatal
|
|
44
|
+
reset: "\x1b[0m", // Reset
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Gets a logger instance for the specified context.
|
|
49
|
+
* Creates a new instance if one doesn't exist, otherwise returns existing instance.
|
|
50
|
+
*
|
|
51
|
+
* @param context - The context for the logger (default: "Global")
|
|
52
|
+
* @returns Logger instance for the specified context
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* const logger = GreenApiLogger.getInstance("MyService");
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
static getInstance(context = "Global") {
|
|
60
|
+
if (!GreenApiLogger.instances.has(context)) {
|
|
61
|
+
GreenApiLogger.instances.set(context, new GreenApiLogger(context));
|
|
62
|
+
}
|
|
63
|
+
return GreenApiLogger.instances.get(context);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Formats timestamp in locale-specific format
|
|
67
|
+
* @returns Formatted timestamp string
|
|
68
|
+
* @private
|
|
69
|
+
*/
|
|
70
|
+
formatTimestamp() {
|
|
71
|
+
return new Date().toLocaleString("en-GB");
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Sanitizes values for JSON serialization.
|
|
75
|
+
* Handles special cases like Error objects and BigInt values.
|
|
76
|
+
*
|
|
77
|
+
* @param value - Value to sanitize
|
|
78
|
+
* @returns Sanitized value safe for JSON serialization
|
|
79
|
+
* @private
|
|
80
|
+
*/
|
|
81
|
+
sanitizeValue(value) {
|
|
82
|
+
if (value instanceof Error) {
|
|
83
|
+
return {
|
|
84
|
+
message: value.message,
|
|
85
|
+
stack: value.stack?.split("\n").map(line => line.trim()),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (typeof value === "bigint") {
|
|
89
|
+
return value.toString();
|
|
90
|
+
}
|
|
91
|
+
if (Array.isArray(value)) {
|
|
92
|
+
return value.map(item => this.sanitizeValue(item));
|
|
93
|
+
}
|
|
94
|
+
if (value && typeof value === "object") {
|
|
95
|
+
return Object.fromEntries(Object.entries(value).map(([key, val]) => [key, this.sanitizeValue(val)]));
|
|
96
|
+
}
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Creates and outputs a log entry
|
|
101
|
+
* @param level - Log level
|
|
102
|
+
* @param message - Log message
|
|
103
|
+
* @param additionalContext - Additional context data
|
|
104
|
+
* @private
|
|
105
|
+
*/
|
|
106
|
+
logEntry(level, message, additionalContext = {}) {
|
|
107
|
+
const entry = {
|
|
108
|
+
timestamp: this.formatTimestamp(),
|
|
109
|
+
level,
|
|
110
|
+
context: this.context,
|
|
111
|
+
message,
|
|
112
|
+
...this.sanitizeValue(additionalContext),
|
|
113
|
+
};
|
|
114
|
+
const color = this.colors[level];
|
|
115
|
+
const jsonString = JSON.stringify(entry);
|
|
116
|
+
console.log(`${color}${jsonString}${this.colors.reset}`);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Logs a debug message
|
|
120
|
+
* @param message - Debug message
|
|
121
|
+
* @param context - Additional context data
|
|
122
|
+
*/
|
|
123
|
+
debug(message, context = {}) {
|
|
124
|
+
this.logEntry("debug", message, context);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Logs an info message
|
|
128
|
+
* @param message - Info message
|
|
129
|
+
* @param context - Additional context data
|
|
130
|
+
*/
|
|
131
|
+
info(message, context = {}) {
|
|
132
|
+
this.logEntry("info", message, context);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Logs a warning message
|
|
136
|
+
* @param message - Warning message
|
|
137
|
+
* @param context - Additional context data
|
|
138
|
+
*/
|
|
139
|
+
warn(message, context = {}) {
|
|
140
|
+
this.logEntry("warn", message, context);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Logs an error message
|
|
144
|
+
* @param message - Error message
|
|
145
|
+
* @param context - Additional context data
|
|
146
|
+
*/
|
|
147
|
+
error(message, context = {}) {
|
|
148
|
+
this.logEntry("error", message, context);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Alternative method for logging info messages
|
|
152
|
+
* @param message - Log message
|
|
153
|
+
* @param context - Context string
|
|
154
|
+
*/
|
|
155
|
+
log(message, context) {
|
|
156
|
+
this.logEntry("info", message, { context });
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Logs a verbose debug message
|
|
160
|
+
* @param message - Debug message
|
|
161
|
+
* @param context - Context string
|
|
162
|
+
*/
|
|
163
|
+
verbose(message, context) {
|
|
164
|
+
this.logEntry("debug", message, { context, level: "verbose" });
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Logs a fatal error message
|
|
168
|
+
* @param message - Fatal error message
|
|
169
|
+
* @param context - Additional context data
|
|
170
|
+
*/
|
|
171
|
+
fatal(message, context = {}) {
|
|
172
|
+
this.logEntry("error", message, context);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Logs detailed error information, handling both Axios errors and regular errors.
|
|
176
|
+
* Particularly useful for API errors and exceptions.
|
|
177
|
+
*
|
|
178
|
+
* @param error - Error object (Axios error or regular error)
|
|
179
|
+
* @param context - Error context description
|
|
180
|
+
* @param additionalContext - Additional context data
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```typescript
|
|
184
|
+
* try {
|
|
185
|
+
* await api.request();
|
|
186
|
+
* } catch (error) {
|
|
187
|
+
* logger.logErrorResponse(error, "API Request failed", { requestId: "123" });
|
|
188
|
+
* }
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
logErrorResponse(error, context, additionalContext = {}) {
|
|
192
|
+
if (axios_1.default.isAxiosError(error)) {
|
|
193
|
+
const axiosError = error;
|
|
194
|
+
this.error(`${context} - API Error:`, {
|
|
195
|
+
status: axiosError.response?.status,
|
|
196
|
+
statusText: axiosError.response?.statusText,
|
|
197
|
+
data: axiosError.response?.data,
|
|
198
|
+
url: axiosError.config?.url,
|
|
199
|
+
method: axiosError.config?.method,
|
|
200
|
+
headers: axiosError.response?.headers,
|
|
201
|
+
timestamp: new Date().toISOString(),
|
|
202
|
+
...additionalContext,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
const errorObject = {
|
|
207
|
+
message: error instanceof Error ? error.message : String(error),
|
|
208
|
+
stack: error instanceof Error ? error.stack?.split("\n").map(line => line.trim()) : undefined,
|
|
209
|
+
timestamp: new Date().toISOString(),
|
|
210
|
+
...additionalContext,
|
|
211
|
+
};
|
|
212
|
+
this.error(`${context} - Non-API Error:`, errorObject);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
exports.GreenApiLogger = GreenApiLogger;
|
|
217
|
+
GreenApiLogger.instances = new Map();
|
package/dist/index.d.ts
CHANGED
|
@@ -4,5 +4,6 @@ export { GreenApiClient } from "./core/green-api.client";
|
|
|
4
4
|
export { MessageTransformer } from "./core/message-transformer";
|
|
5
5
|
export { BaseGreenApiAuthGuard } from "./core/guard";
|
|
6
6
|
export { StorageProvider } from "./core/storage-provider";
|
|
7
|
+
export { GreenApiLogger } from "./core/logger";
|
|
7
8
|
export * from "./utils/helpers";
|
|
8
9
|
export * from "./core/errors";
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.StorageProvider = exports.BaseGreenApiAuthGuard = exports.MessageTransformer = exports.GreenApiClient = exports.BaseAdapter = void 0;
|
|
17
|
+
exports.GreenApiLogger = exports.StorageProvider = exports.BaseGreenApiAuthGuard = exports.MessageTransformer = exports.GreenApiClient = exports.BaseAdapter = void 0;
|
|
18
18
|
__exportStar(require("./types/types"), exports);
|
|
19
19
|
var base_adapter_1 = require("./core/base-adapter");
|
|
20
20
|
Object.defineProperty(exports, "BaseAdapter", { enumerable: true, get: function () { return base_adapter_1.BaseAdapter; } });
|
|
@@ -26,5 +26,7 @@ var guard_1 = require("./core/guard");
|
|
|
26
26
|
Object.defineProperty(exports, "BaseGreenApiAuthGuard", { enumerable: true, get: function () { return guard_1.BaseGreenApiAuthGuard; } });
|
|
27
27
|
var storage_provider_1 = require("./core/storage-provider");
|
|
28
28
|
Object.defineProperty(exports, "StorageProvider", { enumerable: true, get: function () { return storage_provider_1.StorageProvider; } });
|
|
29
|
+
var logger_1 = require("./core/logger");
|
|
30
|
+
Object.defineProperty(exports, "GreenApiLogger", { enumerable: true, get: function () { return logger_1.GreenApiLogger; } });
|
|
29
31
|
__exportStar(require("./utils/helpers"), exports);
|
|
30
32
|
__exportStar(require("./core/errors"), exports);
|
package/dist/types/types.d.ts
CHANGED
|
@@ -109,7 +109,7 @@ export interface QueueMessage {
|
|
|
109
109
|
export interface ClearMessagesQueue {
|
|
110
110
|
isCleared: boolean;
|
|
111
111
|
}
|
|
112
|
-
export type MessageType = "textMessage" | "extendedTextMessage" | "imageMessage" | "videoMessage" | "documentMessage" | "audioMessage" | "contactMessage" | "locationMessage" | "pollMessage" | "reactionMessage" | "pollUpdateMessage" | "quotedMessage" | "stickerMessage";
|
|
112
|
+
export type MessageType = "textMessage" | "extendedTextMessage" | "imageMessage" | "videoMessage" | "documentMessage" | "audioMessage" | "contactMessage" | "locationMessage" | "contactsArrayMessage" | "pollMessage" | "reactionMessage" | "pollUpdateMessage" | "quotedMessage" | "stickerMessage";
|
|
113
113
|
export interface GetMessage {
|
|
114
114
|
chatId: string;
|
|
115
115
|
idMessage: string;
|
|
@@ -191,13 +191,22 @@ export interface PollMessageData {
|
|
|
191
191
|
options: PollOption[];
|
|
192
192
|
multipleAnswers: boolean;
|
|
193
193
|
}
|
|
194
|
-
type QuotedMessage = {
|
|
194
|
+
export type QuotedMessage = {
|
|
195
195
|
stanzaId: string;
|
|
196
196
|
participant: string;
|
|
197
197
|
typeMessage: MessageType;
|
|
198
198
|
} & ({
|
|
199
199
|
typeMessage: "textMessage";
|
|
200
200
|
textMessage: string;
|
|
201
|
+
} | {
|
|
202
|
+
typeMessage: "extendedTextMessage";
|
|
203
|
+
textMessage: string;
|
|
204
|
+
extendedTextMessage: {
|
|
205
|
+
description?: string;
|
|
206
|
+
title?: string;
|
|
207
|
+
previewType?: string;
|
|
208
|
+
jpegThumbnail?: string | null;
|
|
209
|
+
};
|
|
201
210
|
} | {
|
|
202
211
|
typeMessage: "contactMessage";
|
|
203
212
|
contact: {
|
|
@@ -274,6 +283,10 @@ export type WebhookMessageData = {
|
|
|
274
283
|
} | {
|
|
275
284
|
typeMessage: "imageMessage" | "videoMessage" | "documentMessage" | "audioMessage";
|
|
276
285
|
fileMessageData: FileMessageData;
|
|
286
|
+
} | {
|
|
287
|
+
typeMessage: "quotedMessage";
|
|
288
|
+
extendedTextMessageData: ExtendedTextMessageData;
|
|
289
|
+
quotedMessage: QuotedMessage;
|
|
277
290
|
} | {
|
|
278
291
|
typeMessage: "locationMessage";
|
|
279
292
|
locationMessageData: LocationMessageData;
|
|
@@ -542,4 +555,3 @@ export interface BaseUser {
|
|
|
542
555
|
id: number | bigint;
|
|
543
556
|
[key: string]: any;
|
|
544
557
|
}
|
|
545
|
-
export {};
|
package/package.json
CHANGED
|
@@ -1,41 +1,42 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@green-api/greenapi-integration",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "GREEN-API Integration library",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/index.d.ts",
|
|
7
|
-
"files": [
|
|
8
|
-
"dist"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"docs
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
"@types/
|
|
28
|
-
"@types/
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"ts-
|
|
34
|
-
"
|
|
35
|
-
"typedoc
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@green-api/greenapi-integration",
|
|
3
|
+
"version": "0.6.1",
|
|
4
|
+
"description": "GREEN-API Integration library",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"LICENSE"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"clean": "rimraf dist",
|
|
14
|
+
"prebuild": "npm run clean",
|
|
15
|
+
"prepare": "npm run build",
|
|
16
|
+
"docs": "typedoc",
|
|
17
|
+
"docs:watch": "typedoc --watch"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"green-api",
|
|
21
|
+
"whatsapp",
|
|
22
|
+
"integration"
|
|
23
|
+
],
|
|
24
|
+
"author": "GREEN-API team",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/express": "^5.0.0",
|
|
28
|
+
"@types/jest": "^29.5.14",
|
|
29
|
+
"@types/node": "^22.10.2",
|
|
30
|
+
"express": "^4.21.2",
|
|
31
|
+
"jest": "^29.7.0",
|
|
32
|
+
"rimraf": "^6.0.1",
|
|
33
|
+
"ts-jest": "^29.2.5",
|
|
34
|
+
"ts-node": "^10.9.2",
|
|
35
|
+
"typedoc": "^0.27.6",
|
|
36
|
+
"typedoc-plugin-markdown": "^4.4.1",
|
|
37
|
+
"typescript": "^5.7.2"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"axios": "^1.7.9"
|
|
41
|
+
}
|
|
42
|
+
}
|