@natyapp/meta 1.6.6 → 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.
Files changed (95) hide show
  1. package/.github/copilot-instructions.md +1540 -0
  2. package/README.md +513 -40
  3. package/dist/elements/index.d.ts +1 -0
  4. package/dist/elements/index.js +1 -0
  5. package/dist/elements/mediaTemplate.d.ts +45 -0
  6. package/dist/elements/mediaTemplate.js +47 -0
  7. package/dist/elements/textTemplate.d.ts +12 -1
  8. package/dist/elements/textTemplate.js +13 -6
  9. package/dist/index.d.ts +7 -2
  10. package/dist/index.js +11 -2
  11. package/dist/interfaces/IConnection.d.ts +2 -2
  12. package/dist/interfaces/ILog.d.ts +2 -2
  13. package/dist/interfaces/ILogger.d.ts +62 -0
  14. package/dist/interfaces/ILogger.js +2 -0
  15. package/dist/interfaces/ISdk.d.ts +4 -2
  16. package/dist/interfaces/IWebhook.d.ts +2 -2
  17. package/dist/interfaces/index.d.ts +1 -0
  18. package/dist/interfaces/index.js +1 -0
  19. package/dist/queue/messageQueue.d.ts +1 -1
  20. package/dist/queue/messageQueue.js +45 -0
  21. package/dist/routes/webhooks/methods/connection.js +78 -11
  22. package/dist/routes/webhooks/methods/messages.js +18 -3
  23. package/dist/services/axiosInstances.d.ts +14 -5
  24. package/dist/services/axiosInstances.js +111 -23
  25. package/dist/services/middlewares/validations.d.ts +2 -2
  26. package/dist/services/middlewares/validations.js +1 -2
  27. package/dist/services/mutations/connection.js +1 -1
  28. package/dist/services/mutations/logs.js +1 -1
  29. package/dist/services/mutations/messages.js +1 -1
  30. package/dist/services/mutations/validation.d.ts +1 -1
  31. package/dist/services/mutations/validation.js +1 -1
  32. package/dist/services/mutations/webhooks.js +1 -1
  33. package/dist/types/logs.d.ts +1 -1
  34. package/dist/types/requestTypes.d.ts +2 -0
  35. package/dist/useCases/connection/index.d.ts +9 -9
  36. package/dist/useCases/connection/index.js +156 -7
  37. package/dist/useCases/events/NatyEvents.d.ts +4 -2
  38. package/dist/useCases/events/NatyEvents.js +13 -1
  39. package/dist/useCases/log/index.d.ts +5 -5
  40. package/dist/useCases/log/index.js +65 -3
  41. package/dist/useCases/message/whatsappResponse.d.ts +48 -22
  42. package/dist/useCases/message/whatsappResponse.js +672 -105
  43. package/dist/useCases/messages/index.d.ts +5 -5
  44. package/dist/useCases/messages/index.js +66 -3
  45. package/dist/useCases/sdk/index.d.ts +8 -4
  46. package/dist/useCases/sdk/index.js +38 -6
  47. package/dist/useCases/webhook/index.d.ts +9 -9
  48. package/dist/useCases/webhook/index.js +154 -7
  49. package/dist/utils/consoleLogger.d.ts +20 -0
  50. package/dist/utils/consoleLogger.js +51 -0
  51. package/dist/utils/index.d.ts +6 -0
  52. package/dist/utils/index.js +6 -0
  53. package/dist/utils/loggerContext.d.ts +57 -0
  54. package/dist/utils/loggerContext.js +90 -0
  55. package/dist/utils/methodContext.d.ts +34 -0
  56. package/dist/utils/methodContext.js +48 -0
  57. package/dist/utils/parseError.d.ts +12 -0
  58. package/dist/utils/parseError.js +27 -3
  59. package/dist/utils/pinoAdapter.d.ts +30 -0
  60. package/dist/utils/pinoAdapter.js +68 -0
  61. package/dist/utils/sanitize.d.ts +42 -0
  62. package/dist/utils/sanitize.js +120 -0
  63. package/dist/utils/tryCatch.d.ts +10 -1
  64. package/dist/utils/tryCatch.js +40 -5
  65. package/docs/01-visao-geral.md +355 -0
  66. package/docs/02-contexto-negocio.md +596 -0
  67. package/docs/03-arquitetura.md +925 -0
  68. package/docs/04-fluxos-funcionais.md +887 -0
  69. package/docs/05-integracoes.md +960 -0
  70. package/docs/06-entidades.md +849 -0
  71. package/docs/07-guia-pratico.md +1133 -0
  72. package/docs/08-troubleshooting.md +816 -0
  73. package/docs/README.md +125 -0
  74. package/examples/logger-example.ts +279 -0
  75. package/package.json +2 -2
  76. /package/dist/{Entities → entities}/Logs.d.ts +0 -0
  77. /package/dist/{Entities → entities}/Logs.js +0 -0
  78. /package/dist/{Entities → entities}/connection.d.ts +0 -0
  79. /package/dist/{Entities → entities}/connection.js +0 -0
  80. /package/dist/{Entities → entities}/errorLogs.d.ts +0 -0
  81. /package/dist/{Entities → entities}/errorLogs.js +0 -0
  82. /package/dist/{Entities → entities}/index.d.ts +0 -0
  83. /package/dist/{Entities → entities}/index.js +0 -0
  84. /package/dist/{Entities → entities}/messages.d.ts +0 -0
  85. /package/dist/{Entities → entities}/messages.js +0 -0
  86. /package/dist/{Entities → entities}/webhooks.d.ts +0 -0
  87. /package/dist/{Entities → entities}/webhooks.js +0 -0
  88. /package/dist/{Entities → entities}/whatsappMessage.d.ts +0 -0
  89. /package/dist/{Entities → entities}/whatsappMessage.js +0 -0
  90. /package/dist/{Errors → errors}/Either.d.ts +0 -0
  91. /package/dist/{Errors → errors}/Either.js +0 -0
  92. /package/dist/{Errors → errors}/ErrorHandling.d.ts +0 -0
  93. /package/dist/{Errors → errors}/ErrorHandling.js +0 -0
  94. /package/dist/{Errors → errors}/index.d.ts +0 -0
  95. /package/dist/{Errors → errors}/index.js +0 -0
@@ -1,10 +1,10 @@
1
- import { MessageEntity } from "../../Entities/messages";
2
- import { Response } from "../../Errors";
1
+ import { LogsEntity } from "../../entities/Logs";
2
+ import { Either } from "../../errors";
3
3
  import { getAllProps, getAllReturn } from "../../types/requestTypes";
4
4
  declare class Messages {
5
- getSingle: (id: string) => Promise<Response<"", MessageEntity>>;
6
- getAll: (props: getAllProps) => Promise<Response<"", getAllReturn<MessageEntity>>>;
7
- searchOne: (key: string, value: string) => Promise<Response<"", MessageEntity>>;
5
+ getSingle: (id: string) => Promise<Either<string, LogsEntity>>;
6
+ getAll: (props: getAllProps) => Promise<Either<string, getAllReturn<LogsEntity>>>;
7
+ searchOne: (key: string, value: string) => Promise<Either<string, LogsEntity>>;
8
8
  }
9
9
  declare const _default: Messages;
10
10
  export default _default;
@@ -4,9 +4,72 @@ const messages_1 = require("../../services/mutations/messages");
4
4
  const utils_1 = require("../../utils");
5
5
  class Messages {
6
6
  constructor() {
7
- this.getSingle = (id) => (0, utils_1.Try)(messages_1.messagesMutations.getSingle, id);
8
- this.getAll = (props) => (0, utils_1.Try)(messages_1.messagesMutations.getAll, props);
9
- this.searchOne = (key, value) => (0, utils_1.Try)(messages_1.messagesMutations.searchOne, key, value);
7
+ this.getSingle = async (id) => {
8
+ utils_1.logger.debug("Fetching message", {
9
+ operation: (0, utils_1.getMethodContext)("Messages", "getSingle"),
10
+ messageId: id,
11
+ });
12
+ const result = await (0, utils_1.Try)(messages_1.messagesMutations.getSingle, id);
13
+ if (result.isError) {
14
+ utils_1.logger.error("Failed to fetch message", {
15
+ operation: (0, utils_1.getMethodContext)("Messages", "getSingle"),
16
+ messageId: id,
17
+ error: (0, utils_1.parseError)(result.isError),
18
+ });
19
+ return result;
20
+ }
21
+ utils_1.logger.info("Message fetched successfully", {
22
+ operation: (0, utils_1.getMethodContext)("Messages", "getSingle"),
23
+ messageId: result.isSuccess.id,
24
+ });
25
+ return result;
26
+ };
27
+ this.getAll = async (props) => {
28
+ utils_1.logger.debug("Fetching all messages", {
29
+ operation: (0, utils_1.getMethodContext)("Messages", "getAll"),
30
+ companyId: props.companyId,
31
+ limit: props.limit,
32
+ page: props.page,
33
+ });
34
+ const result = await (0, utils_1.Try)(messages_1.messagesMutations.getAll, props);
35
+ if (result.isError) {
36
+ utils_1.logger.error("Failed to fetch messages", {
37
+ operation: (0, utils_1.getMethodContext)("Messages", "getAll"),
38
+ companyId: props.companyId,
39
+ error: (0, utils_1.parseError)(result.isError),
40
+ });
41
+ return result;
42
+ }
43
+ utils_1.logger.info("Messages fetched successfully", {
44
+ operation: (0, utils_1.getMethodContext)("Messages", "getAll"),
45
+ companyId: props.companyId,
46
+ count: result.isSuccess.length,
47
+ });
48
+ return result;
49
+ };
50
+ this.searchOne = async (key, value) => {
51
+ utils_1.logger.debug("Searching for message", {
52
+ operation: (0, utils_1.getMethodContext)("Messages", "searchOne"),
53
+ searchKey: key,
54
+ searchValue: key.toLowerCase().includes("phone") || key === "to" || key === "from"
55
+ ? (0, utils_1.sanitizePhoneNumber)(value)
56
+ : value,
57
+ });
58
+ const result = await (0, utils_1.Try)(messages_1.messagesMutations.searchOne, key, value);
59
+ if (result.isError) {
60
+ utils_1.logger.error("Message search failed", {
61
+ operation: (0, utils_1.getMethodContext)("Messages", "searchOne"),
62
+ searchKey: key,
63
+ error: (0, utils_1.parseError)(result.isError),
64
+ });
65
+ return result;
66
+ }
67
+ utils_1.logger.info("Message found", {
68
+ operation: (0, utils_1.getMethodContext)("Messages", "searchOne"),
69
+ messageId: result.isSuccess.id,
70
+ });
71
+ return result;
72
+ };
10
73
  }
11
74
  }
12
75
  exports.default = new Messages();
@@ -1,11 +1,12 @@
1
- import { Response } from "../../Errors/ErrorHandling";
2
- import { ConnectErrors, ConnectProps, ISdk } from "../../interfaces/ISdk";
1
+ import { Either } from "../../errors/Either";
2
+ import { ConnectProps, ISdk } from "../../interfaces/ISdk";
3
3
  import Connection from "../connection";
4
4
  import Message from "../message";
5
5
  import Webhook from "../webhook";
6
6
  import Log from "../log";
7
7
  import { Express } from "express";
8
8
  import { NatyEvents } from "../events/NatyEvents";
9
+ import type { ILogger } from "../../interfaces/ILogger";
9
10
  export declare class NatyMeta extends NatyEvents implements ISdk {
10
11
  connected: boolean;
11
12
  Connection: typeof Connection;
@@ -13,7 +14,10 @@ export declare class NatyMeta extends NatyEvents implements ISdk {
13
14
  Webhook: typeof Webhook;
14
15
  Log: typeof Log;
15
16
  app: Express;
16
- constructor(app?: Express);
17
- connect({ appToken, pathname, app, }: ConnectProps): Promise<Response<ConnectErrors, boolean>>;
17
+ constructor(config?: {
18
+ app?: Express;
19
+ logger: ILogger;
20
+ });
21
+ connect({ appToken, pathname, app, logger, }: ConnectProps): Promise<Either<string, boolean>>;
18
22
  initializeRoutes(path?: string): void;
19
23
  }
@@ -13,18 +13,38 @@ const webhook_1 = __importDefault(require("../webhook"));
13
13
  const log_1 = __importDefault(require("../log"));
14
14
  const routes_1 = require("../../routes");
15
15
  const NatyEvents_1 = require("../events/NatyEvents");
16
+ //import { consoleLogger } from "../../utils/consoleLogger";
17
+ const loggerContext_1 = require("../../utils/loggerContext");
18
+ const utils_1 = require("../../utils");
16
19
  class NatyMeta extends NatyEvents_1.NatyEvents {
17
- constructor(app) {
18
- super();
20
+ constructor(config) {
21
+ const logger = (config === null || config === void 0 ? void 0 : config.logger) || utils_1.consoleLogger;
22
+ super(logger);
19
23
  this.connected = false;
20
- if (app) {
21
- this.app = app;
24
+ // Set global logger for use in other parts of the SDK
25
+ (0, loggerContext_1.setGlobalLogger)(logger);
26
+ this.logger.info("NatyMeta SDK initialized", {
27
+ operation: (0, utils_1.getMethodContext)("NatyMeta", "constructor"),
28
+ hasApp: !!(config === null || config === void 0 ? void 0 : config.app),
29
+ customLogger: !!(config === null || config === void 0 ? void 0 : config.logger),
30
+ });
31
+ if (config === null || config === void 0 ? void 0 : config.app) {
32
+ this.app = config.app;
22
33
  this.connect({ appToken: "" });
23
34
  }
24
35
  }
25
- async connect({ appToken, pathname, app, }) {
36
+ async connect({ appToken, pathname, app, logger, }) {
37
+ this.logger.info("Connecting to Naty API", {
38
+ operation: (0, utils_1.getMethodContext)("NatyMeta", "connect"),
39
+ hasAppToken: !!appToken,
40
+ });
26
41
  if (app)
27
42
  this.app = app;
43
+ // Update logger if provided
44
+ if (logger) {
45
+ this.logger = logger;
46
+ (0, loggerContext_1.setGlobalLogger)(logger);
47
+ }
28
48
  const confirmAppToken = await (0, tryCatch_1.Try)(validations_1.middlewares.confirmAppToken, `${appToken}`);
29
49
  // this.emit("connection", {companyId:''});
30
50
  if (confirmAppToken.isSuccess) {
@@ -34,15 +54,27 @@ class NatyMeta extends NatyEvents_1.NatyEvents {
34
54
  this.Message = message_1.default;
35
55
  this.Webhook = webhook_1.default;
36
56
  this.Log = log_1.default;
57
+ this.logger.info("Successfully connected to Naty API", {
58
+ operation: (0, utils_1.getMethodContext)("NatyMeta", "connect"),
59
+ });
37
60
  }
38
61
  else {
39
62
  this.connected = false;
63
+ this.logger.error("Failed to connect to Naty API", {
64
+ operation: (0, utils_1.getMethodContext)("NatyMeta", "connect"),
65
+ error: confirmAppToken.isError,
66
+ });
40
67
  }
41
68
  this.initializeRoutes(pathname);
42
69
  return confirmAppToken;
43
70
  }
44
71
  initializeRoutes(path) {
45
- this.app.use(path || "/", (0, routes_1.principalRoute)(this));
72
+ const routePath = path || "/";
73
+ this.logger.debug("Initializing routes", {
74
+ operation: (0, utils_1.getMethodContext)("NatyMeta", "initializeRoutes"),
75
+ path: routePath,
76
+ });
77
+ this.app.use(routePath, (0, routes_1.principalRoute)(this));
46
78
  }
47
79
  }
48
80
  exports.NatyMeta = NatyMeta;
@@ -1,15 +1,15 @@
1
- import { WebhooksEntity } from "../../Entities/webhooks";
2
- import { Either } from "../../Errors";
1
+ import { WebhooksEntity } from "../../entities/webhooks";
2
+ import { Either } from "../../errors";
3
3
  import { IWebhook } from "../../interfaces";
4
4
  import { getAllProps, getAllReturn } from "../../types/requestTypes";
5
5
  declare class Webhooks implements IWebhook {
6
- getSingle: (id: string) => Promise<Either<"", WebhooksEntity>>;
7
- getAll: (props: getAllProps) => Promise<Either<"", getAllReturn<WebhooksEntity>>>;
8
- searchOne: (key: string, value: string) => Promise<Either<"", WebhooksEntity>>;
9
- insert: (connection: Partial<WebhooksEntity>) => Promise<Either<"", WebhooksEntity>>;
10
- update: (id: string, connection: Partial<WebhooksEntity>) => Promise<Either<"", WebhooksEntity>>;
11
- updateMany: (webhooks: Partial<WebhooksEntity>[]) => Promise<Either<"", WebhooksEntity[]>>;
12
- deleteOne: (id: string) => Promise<Either<"", WebhooksEntity>>;
6
+ getSingle: (id: string) => Promise<Either<string, WebhooksEntity>>;
7
+ getAll: (props: getAllProps) => Promise<Either<string, getAllReturn<WebhooksEntity>>>;
8
+ searchOne: (key: string, value: string) => Promise<Either<string, WebhooksEntity>>;
9
+ insert: (connection: Partial<WebhooksEntity>) => Promise<Either<string, WebhooksEntity>>;
10
+ update: (id: string, connection: Partial<WebhooksEntity>) => Promise<Either<string, WebhooksEntity>>;
11
+ updateMany: (webhooks: Partial<WebhooksEntity>[]) => Promise<Either<string, WebhooksEntity[]>>;
12
+ deleteOne: (id: string) => Promise<Either<string, WebhooksEntity>>;
13
13
  }
14
14
  declare const _default: Webhooks;
15
15
  export default _default;
@@ -4,13 +4,160 @@ const services_1 = require("../../services");
4
4
  const utils_1 = require("../../utils");
5
5
  class Webhooks {
6
6
  constructor() {
7
- this.getSingle = (id) => (0, utils_1.Try)(services_1.webhooksMutations.getSingle, id);
8
- this.getAll = (props) => (0, utils_1.Try)(services_1.webhooksMutations.getAll, props);
9
- this.searchOne = (key, value) => (0, utils_1.Try)(services_1.webhooksMutations.searchOne, key, value);
10
- this.insert = (connection) => (0, utils_1.Try)(services_1.webhooksMutations.insert, connection);
11
- this.update = (id, connection) => (0, utils_1.Try)(services_1.webhooksMutations.update, id, connection);
12
- this.updateMany = (webhooks) => (0, utils_1.Try)(services_1.webhooksMutations.updateMany, webhooks);
13
- this.deleteOne = (id) => (0, utils_1.Try)(services_1.webhooksMutations.deleteOne, id);
7
+ this.getSingle = async (id) => {
8
+ utils_1.logger.debug("Fetching webhook", {
9
+ operation: (0, utils_1.getMethodContext)("Webhooks", "getSingle"),
10
+ webhookId: id,
11
+ });
12
+ const result = await (0, utils_1.Try)(services_1.webhooksMutations.getSingle, id);
13
+ if (result.isError) {
14
+ utils_1.logger.error("Failed to fetch webhook", {
15
+ operation: (0, utils_1.getMethodContext)("Webhooks", "getSingle"),
16
+ webhookId: id,
17
+ error: (0, utils_1.parseError)(result.isError),
18
+ });
19
+ return result;
20
+ }
21
+ utils_1.logger.debug("Webhook fetched successfully", {
22
+ operation: (0, utils_1.getMethodContext)("Webhooks", "getSingle"),
23
+ webhookId: result.isSuccess.id,
24
+ companyId: result.isSuccess.companyId,
25
+ });
26
+ return result;
27
+ };
28
+ this.getAll = async (props) => {
29
+ utils_1.logger.debug("Fetching all webhooks", {
30
+ operation: (0, utils_1.getMethodContext)("Webhooks", "getAll"),
31
+ companyId: props.companyId,
32
+ limit: props.limit,
33
+ page: props.page,
34
+ });
35
+ const result = await (0, utils_1.Try)(services_1.webhooksMutations.getAll, props);
36
+ if (result.isError) {
37
+ utils_1.logger.error("Failed to fetch webhooks", {
38
+ operation: (0, utils_1.getMethodContext)("Webhooks", "getAll"),
39
+ companyId: props.companyId,
40
+ error: (0, utils_1.parseError)(result.isError),
41
+ });
42
+ return result;
43
+ }
44
+ utils_1.logger.debug("Webhooks fetched successfully", {
45
+ operation: (0, utils_1.getMethodContext)("Webhooks", "getAll"),
46
+ companyId: props.companyId,
47
+ count: result.isSuccess.length,
48
+ });
49
+ return result;
50
+ };
51
+ this.searchOne = async (key, value) => {
52
+ utils_1.logger.debug("Searching for webhook", {
53
+ operation: (0, utils_1.getMethodContext)("Webhooks", "searchOne"),
54
+ searchKey: key,
55
+ searchValue: value,
56
+ });
57
+ const result = await (0, utils_1.Try)(services_1.webhooksMutations.searchOne, key, value);
58
+ if (result.isError) {
59
+ utils_1.logger.error("Webhook search failed", {
60
+ operation: (0, utils_1.getMethodContext)("Webhooks", "searchOne"),
61
+ searchKey: key,
62
+ error: (0, utils_1.parseError)(result.isError),
63
+ });
64
+ return result;
65
+ }
66
+ utils_1.logger.debug("Webhook found", {
67
+ operation: (0, utils_1.getMethodContext)("Webhooks", "searchOne"),
68
+ webhookId: result.isSuccess.id,
69
+ companyId: result.isSuccess.companyId,
70
+ });
71
+ return result;
72
+ };
73
+ this.insert = async (connection) => {
74
+ utils_1.logger.info("Creating new webhook", {
75
+ operation: (0, utils_1.getMethodContext)("Webhooks", "insert"),
76
+ companyId: connection.companyId,
77
+ });
78
+ const result = await (0, utils_1.Try)(services_1.webhooksMutations.insert, connection);
79
+ if (result.isError) {
80
+ utils_1.logger.error("Failed to create webhook", {
81
+ operation: (0, utils_1.getMethodContext)("Webhooks", "insert"),
82
+ companyId: connection.companyId,
83
+ error: (0, utils_1.parseError)(result.isError),
84
+ });
85
+ return result;
86
+ }
87
+ utils_1.logger.info("Webhook created successfully", {
88
+ operation: (0, utils_1.getMethodContext)("Webhooks", "insert"),
89
+ webhookId: result.isSuccess.id,
90
+ companyId: result.isSuccess.companyId,
91
+ });
92
+ return result;
93
+ };
94
+ this.update = async (id, connection) => {
95
+ const updatedFields = Object.keys(connection).filter((key) => key !== "id" && key !== "_id");
96
+ utils_1.logger.info("Updating webhook", {
97
+ operation: (0, utils_1.getMethodContext)("Webhooks", "update"),
98
+ webhookId: id,
99
+ companyId: connection.companyId,
100
+ updatedFields,
101
+ });
102
+ const result = await (0, utils_1.Try)(services_1.webhooksMutations.update, id, connection);
103
+ if (result.isError) {
104
+ utils_1.logger.error("Failed to update webhook", {
105
+ operation: (0, utils_1.getMethodContext)("Webhooks", "update"),
106
+ webhookId: id,
107
+ companyId: connection.companyId,
108
+ error: (0, utils_1.parseError)(result.isError),
109
+ });
110
+ return result;
111
+ }
112
+ utils_1.logger.info("Webhook updated successfully", {
113
+ operation: (0, utils_1.getMethodContext)("Webhooks", "update"),
114
+ webhookId: result.isSuccess.id,
115
+ companyId: result.isSuccess.companyId,
116
+ updatedFields,
117
+ });
118
+ return result;
119
+ };
120
+ this.updateMany = async (webhooks) => {
121
+ utils_1.logger.info("Updating multiple webhooks", {
122
+ operation: (0, utils_1.getMethodContext)("Webhooks", "updateMany"),
123
+ count: webhooks.length,
124
+ });
125
+ const result = await (0, utils_1.Try)(services_1.webhooksMutations.updateMany, webhooks);
126
+ if (result.isError) {
127
+ utils_1.logger.error("Failed to update multiple webhooks", {
128
+ operation: (0, utils_1.getMethodContext)("Webhooks", "updateMany"),
129
+ count: webhooks.length,
130
+ error: (0, utils_1.parseError)(result.isError),
131
+ });
132
+ return result;
133
+ }
134
+ utils_1.logger.info("Multiple webhooks updated successfully", {
135
+ operation: (0, utils_1.getMethodContext)("Webhooks", "updateMany"),
136
+ count: result.isSuccess.length,
137
+ });
138
+ return result;
139
+ };
140
+ this.deleteOne = async (id) => {
141
+ utils_1.logger.info("Deleting webhook", {
142
+ operation: (0, utils_1.getMethodContext)("Webhooks", "deleteOne"),
143
+ webhookId: id,
144
+ });
145
+ const result = await (0, utils_1.Try)(services_1.webhooksMutations.deleteOne, id);
146
+ if (result.isError) {
147
+ utils_1.logger.error("Failed to delete webhook", {
148
+ operation: (0, utils_1.getMethodContext)("Webhooks", "deleteOne"),
149
+ webhookId: id,
150
+ error: (0, utils_1.parseError)(result.isError),
151
+ });
152
+ return result;
153
+ }
154
+ utils_1.logger.info("Webhook deleted successfully", {
155
+ operation: (0, utils_1.getMethodContext)("Webhooks", "deleteOne"),
156
+ webhookId: result.isSuccess.id,
157
+ companyId: result.isSuccess.companyId,
158
+ });
159
+ return result;
160
+ };
14
161
  }
15
162
  }
16
163
  exports.default = new Webhooks();
@@ -0,0 +1,20 @@
1
+ import type { ILogger } from "../interfaces/ILogger";
2
+ /**
3
+ * Default console-based logger implementation
4
+ *
5
+ * This logger is used as a fallback when no custom logger is provided.
6
+ * It formats messages with timestamps and pretty-prints metadata.
7
+ */
8
+ export declare class ConsoleLogger implements ILogger {
9
+ private formatTimestamp;
10
+ private formatMeta;
11
+ debug(message: string, meta?: Record<string, any>): void;
12
+ info(message: string, meta?: Record<string, any>): void;
13
+ warn(message: string, meta?: Record<string, any>): void;
14
+ error(message: string, meta?: Record<string, any>): void;
15
+ }
16
+ /**
17
+ * Singleton instance of the console logger
18
+ * Used as default logger when no custom logger is provided
19
+ */
20
+ export declare const consoleLogger: ILogger;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.consoleLogger = exports.ConsoleLogger = void 0;
4
+ /**
5
+ * Default console-based logger implementation
6
+ *
7
+ * This logger is used as a fallback when no custom logger is provided.
8
+ * It formats messages with timestamps and pretty-prints metadata.
9
+ */
10
+ class ConsoleLogger {
11
+ formatTimestamp() {
12
+ return new Date().toISOString();
13
+ }
14
+ formatMeta(meta) {
15
+ if (!meta || Object.keys(meta).length === 0) {
16
+ return "";
17
+ }
18
+ try {
19
+ return " " + JSON.stringify(meta, null, 2);
20
+ }
21
+ catch (error) {
22
+ return " [Circular or non-serializable metadata]";
23
+ }
24
+ }
25
+ debug(message, meta) {
26
+ const timestamp = this.formatTimestamp();
27
+ const formattedMeta = this.formatMeta(meta);
28
+ console.debug(`[${timestamp}] [DEBUG] ${message}${formattedMeta}`);
29
+ }
30
+ info(message, meta) {
31
+ const timestamp = this.formatTimestamp();
32
+ const formattedMeta = this.formatMeta(meta);
33
+ console.log(`[${timestamp}] [INFO] ${message}${formattedMeta}`);
34
+ }
35
+ warn(message, meta) {
36
+ const timestamp = this.formatTimestamp();
37
+ const formattedMeta = this.formatMeta(meta);
38
+ console.warn(`[${timestamp}] [WARN] ${message}${formattedMeta}`);
39
+ }
40
+ error(message, meta) {
41
+ const timestamp = this.formatTimestamp();
42
+ const formattedMeta = this.formatMeta(meta);
43
+ console.error(`[${timestamp}] [ERROR] ${message}${formattedMeta}`);
44
+ }
45
+ }
46
+ exports.ConsoleLogger = ConsoleLogger;
47
+ /**
48
+ * Singleton instance of the console logger
49
+ * Used as default logger when no custom logger is provided
50
+ */
51
+ exports.consoleLogger = new ConsoleLogger();
@@ -1 +1,7 @@
1
1
  export * from "./tryCatch";
2
+ export * from "./consoleLogger";
3
+ export * from "./loggerContext";
4
+ export * from "./pinoAdapter";
5
+ export * from "./sanitize";
6
+ export * from "./parseError";
7
+ export * from "./methodContext";
@@ -15,3 +15,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./tryCatch"), exports);
18
+ __exportStar(require("./consoleLogger"), exports);
19
+ __exportStar(require("./loggerContext"), exports);
20
+ __exportStar(require("./pinoAdapter"), exports);
21
+ __exportStar(require("./sanitize"), exports);
22
+ __exportStar(require("./parseError"), exports);
23
+ __exportStar(require("./methodContext"), exports);
@@ -0,0 +1,57 @@
1
+ import type { ILogger } from "../interfaces/ILogger";
2
+ /**
3
+ * LoggerContext Singleton
4
+ *
5
+ * Provides a global logger instance for the entire SDK.
6
+ * The logger can be set once during SDK initialization and accessed from anywhere.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * // Using the global logger instance
11
+ * import { logger } from './utils/loggerContext';
12
+ *
13
+ * logger.info('Message sent', { to: phoneNumber });
14
+ * logger.error('Failed to send', { error: err.message });
15
+ *
16
+ * // Setting a custom logger
17
+ * import { setGlobalLogger } from './utils/loggerContext';
18
+ *
19
+ * setGlobalLogger(myPinoLogger);
20
+ * ```
21
+ */
22
+ declare class LoggerContext implements ILogger {
23
+ private static instance;
24
+ private _logger;
25
+ private constructor();
26
+ /**
27
+ * Get the singleton instance
28
+ */
29
+ static getInstance(): LoggerContext;
30
+ /**
31
+ * Set the global logger instance
32
+ * This is called during NatyMeta initialization
33
+ *
34
+ * @param logger - The logger instance to use globally
35
+ */
36
+ setLogger(logger: ILogger): void;
37
+ /**
38
+ * Get the current logger instance
39
+ *
40
+ * @returns The active logger instance
41
+ */
42
+ getLogger(): ILogger;
43
+ /**
44
+ * Reset the logger to the default console logger
45
+ * Useful for testing
46
+ */
47
+ reset(): void;
48
+ debug(message: string, meta?: Record<string, any>): void;
49
+ info(message: string, meta?: Record<string, any>): void;
50
+ warn(message: string, meta?: Record<string, any>): void;
51
+ error(message: string, meta?: Record<string, any>): void;
52
+ }
53
+ export declare const logger: LoggerContext;
54
+ export declare const setGlobalLogger: (loggerInstance: ILogger) => void;
55
+ export declare const getGlobalLogger: () => ILogger;
56
+ export declare const resetGlobalLogger: () => void;
57
+ export {};
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resetGlobalLogger = exports.getGlobalLogger = exports.setGlobalLogger = exports.logger = void 0;
4
+ const consoleLogger_1 = require("./consoleLogger");
5
+ /**
6
+ * LoggerContext Singleton
7
+ *
8
+ * Provides a global logger instance for the entire SDK.
9
+ * The logger can be set once during SDK initialization and accessed from anywhere.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * // Using the global logger instance
14
+ * import { logger } from './utils/loggerContext';
15
+ *
16
+ * logger.info('Message sent', { to: phoneNumber });
17
+ * logger.error('Failed to send', { error: err.message });
18
+ *
19
+ * // Setting a custom logger
20
+ * import { setGlobalLogger } from './utils/loggerContext';
21
+ *
22
+ * setGlobalLogger(myPinoLogger);
23
+ * ```
24
+ */
25
+ class LoggerContext {
26
+ constructor() {
27
+ this._logger = consoleLogger_1.consoleLogger;
28
+ }
29
+ /**
30
+ * Get the singleton instance
31
+ */
32
+ static getInstance() {
33
+ if (!LoggerContext.instance) {
34
+ LoggerContext.instance = new LoggerContext();
35
+ }
36
+ return LoggerContext.instance;
37
+ }
38
+ /**
39
+ * Set the global logger instance
40
+ * This is called during NatyMeta initialization
41
+ *
42
+ * @param logger - The logger instance to use globally
43
+ */
44
+ setLogger(logger) {
45
+ this._logger = logger;
46
+ }
47
+ /**
48
+ * Get the current logger instance
49
+ *
50
+ * @returns The active logger instance
51
+ */
52
+ getLogger() {
53
+ return this._logger;
54
+ }
55
+ /**
56
+ * Reset the logger to the default console logger
57
+ * Useful for testing
58
+ */
59
+ reset() {
60
+ this._logger = consoleLogger_1.consoleLogger;
61
+ }
62
+ // ILogger implementation - delegates to internal logger
63
+ debug(message, meta) {
64
+ this._logger.debug(message, meta);
65
+ }
66
+ info(message, meta) {
67
+ this._logger.info(message, meta);
68
+ }
69
+ warn(message, meta) {
70
+ this._logger.warn(message, meta);
71
+ }
72
+ error(message, meta) {
73
+ this._logger.error(message, meta);
74
+ }
75
+ }
76
+ // Export singleton instance
77
+ exports.logger = LoggerContext.getInstance();
78
+ // Export legacy functions for backward compatibility
79
+ const setGlobalLogger = (loggerInstance) => {
80
+ exports.logger.setLogger(loggerInstance);
81
+ };
82
+ exports.setGlobalLogger = setGlobalLogger;
83
+ const getGlobalLogger = () => {
84
+ return exports.logger.getLogger();
85
+ };
86
+ exports.getGlobalLogger = getGlobalLogger;
87
+ const resetGlobalLogger = () => {
88
+ exports.logger.reset();
89
+ };
90
+ exports.resetGlobalLogger = resetGlobalLogger;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Utility for generating consistent method context strings for logging.
3
+ *
4
+ * @module methodContext
5
+ * @example
6
+ * ```typescript
7
+ * import { getMethodContext } from '../utils';
8
+ *
9
+ * logger.info("Operation started", {
10
+ * operation: getMethodContext("WhatsappResponse", "send_text_message"),
11
+ * // ... other metadata
12
+ * });
13
+ * // Logs: operation: "WhatsappResponse.send_text_message"
14
+ * ```
15
+ */
16
+ /**
17
+ * Generates a standardized method context string for logging.
18
+ *
19
+ * @param className - The name of the class or module (e.g., "WhatsappResponse", "Webhooks")
20
+ * @param methodName - The name of the method (e.g., "send_text_message", "getSingle")
21
+ * @returns A formatted string in the format "ClassName.methodName"
22
+ *
23
+ * @throws {Error} If className or methodName is empty or whitespace-only
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * getMethodContext("WhatsappResponse", "send_text_message")
28
+ * // Returns: "WhatsappResponse.send_text_message"
29
+ *
30
+ * getMethodContext("Webhooks", "getSingle")
31
+ * // Returns: "Webhooks.getSingle"
32
+ * ```
33
+ */
34
+ export declare function getMethodContext(className: string, methodName: string): string;