@tiledesk/tiledesk-tybot-connector 0.5.0 → 2.0.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 (100) hide show
  1. package/ExtApi.js +4 -83
  2. package/ExtUtil.js +0 -1
  3. package/Logger.js +105 -0
  4. package/TdCache.js +5 -3
  5. package/TiledeskClientTest.js +5 -28
  6. package/TiledeskExpression.js +6 -47
  7. package/{models → engine}/IntentForm.js +30 -44
  8. package/{models → engine}/IntentsMachineFactory.js +5 -4
  9. package/{models → engine}/MongodbBotsDataSource.js +31 -55
  10. package/{models → engine}/MongodbIntentsMachine.js +6 -5
  11. package/{models → engine}/TiledeskChatbot.js +82 -259
  12. package/{models → engine}/TiledeskChatbotConst.js +9 -0
  13. package/{models → engine}/TiledeskIntentsMachine.js +5 -15
  14. package/{models → engine/mock}/MockBotsDataSource.js +3 -19
  15. package/{models → engine/mock}/MockTdCache.js +0 -9
  16. package/index.js +103 -380
  17. package/logs/app.log +12723 -277
  18. package/logs/app1.log +62258 -0
  19. package/logs/app2.log +46280 -0
  20. package/logs/app3.log +31686 -0
  21. package/logs/app4.log +46163 -0
  22. package/logs/app5.log +33173 -0
  23. package/models/faq.js +2 -5
  24. package/package.json +5 -2
  25. package/{TiledeskServices → services}/AIService.js +4 -3
  26. package/{models/faqKbService.js → services/FaqKbService.js} +1 -2
  27. package/{models/faqService.js → services/FaqService.js} +4 -3
  28. package/services/IntegrationService.js +43 -0
  29. package/services/TilebotService.js +47 -0
  30. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +49 -135
  31. package/tiledeskChatbotPlugs/FillParamsChatbotPlug.js +8 -14
  32. package/tiledeskChatbotPlugs/Filler.js +0 -1
  33. package/tiledeskChatbotPlugs/MarkbotChatbotPlug.js +12 -20
  34. package/tiledeskChatbotPlugs/MessagePipeline.js +5 -15
  35. package/tiledeskChatbotPlugs/SplitsChatbotPlug.js +4 -13
  36. package/tiledeskChatbotPlugs/TiledeskRequestVariables.js +1 -1
  37. package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +21 -89
  38. package/tiledeskChatbotPlugs/directives/DEPRECATED_DirSetAttribute.js +1 -1
  39. package/tiledeskChatbotPlugs/directives/DirAddTags.js +23 -140
  40. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +42 -128
  41. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +37 -127
  42. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +52 -145
  43. package/tiledeskChatbotPlugs/directives/DirAssign.js +12 -23
  44. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +16 -12
  45. package/tiledeskChatbotPlugs/directives/DirAssistant.js +56 -198
  46. package/tiledeskChatbotPlugs/directives/DirBrevo.js +41 -101
  47. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +9 -19
  48. package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +1 -1
  49. package/tiledeskChatbotPlugs/directives/DirClose.js +4 -2
  50. package/tiledeskChatbotPlugs/directives/DirCode.js +16 -23
  51. package/tiledeskChatbotPlugs/directives/DirCondition.js +19 -25
  52. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +9 -82
  53. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +10 -22
  54. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +34 -89
  55. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +17 -22
  56. package/tiledeskChatbotPlugs/directives/DirDeleteVariable.js +8 -8
  57. package/tiledeskChatbotPlugs/directives/DirDepartment.js +13 -12
  58. package/tiledeskChatbotPlugs/directives/DirDisableInputText.js +2 -6
  59. package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +4 -4
  60. package/tiledeskChatbotPlugs/directives/DirForm.js +17 -24
  61. package/tiledeskChatbotPlugs/directives/DirGptTask.js +49 -139
  62. package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +8 -53
  63. package/tiledeskChatbotPlugs/directives/DirHubspot.js +31 -91
  64. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +19 -26
  65. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +24 -78
  66. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +17 -68
  67. package/tiledeskChatbotPlugs/directives/DirIntent.js +6 -100
  68. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +15 -14
  69. package/tiledeskChatbotPlugs/directives/DirLockIntent.js +7 -12
  70. package/tiledeskChatbotPlugs/directives/DirMake.js +21 -53
  71. package/tiledeskChatbotPlugs/directives/DirMessage.js +35 -28
  72. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +8 -87
  73. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +6 -4
  74. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +6 -4
  75. package/tiledeskChatbotPlugs/directives/DirQapla.js +26 -120
  76. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +11 -19
  77. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +4 -0
  78. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +5 -2
  79. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +12 -58
  80. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +11 -57
  81. package/tiledeskChatbotPlugs/directives/DirReply.js +36 -30
  82. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +30 -72
  83. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +9 -10
  84. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +15 -40
  85. package/tiledeskChatbotPlugs/directives/DirSetAttribute.js +8 -5
  86. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +25 -113
  87. package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +14 -13
  88. package/tiledeskChatbotPlugs/directives/DirUnlockIntent.js +3 -3
  89. package/tiledeskChatbotPlugs/directives/DirWait.js +5 -6
  90. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +14 -49
  91. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +22 -77
  92. package/tiledeskChatbotPlugs/directives/DirWebRequestV2_old.js +1 -1
  93. package/tiledeskChatbotPlugs/directives/DirWebResponse.js +11 -14
  94. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +13 -42
  95. package/tiledeskChatbotPlugs/directives/Directives.js +0 -3
  96. package/utils/HttpUtils.js +128 -0
  97. package/{models → utils}/TiledeskChatbotUtil.js +164 -234
  98. package/utils/winston.js +42 -0
  99. package/TiledeskServices/utils.js +0 -99
  100. /package/{models → engine/mock}/MockIntentsMachine.js +0 -0
@@ -1,8 +1,11 @@
1
1
  let axios = require('axios');
2
2
  let https = require("https");
3
3
  const { Filler } = require('../Filler');
4
- const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
4
+ const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
5
5
  const { DirIntent } = require('./DirIntent');
6
+ const winston = require('../../utils/winston')
7
+ const httpUtils = require('../../utils/HttpUtils');
8
+ const integrationService = require('../../services/IntegrationService');
6
9
 
7
10
  class DirAssistant {
8
11
  constructor(context) {
@@ -12,29 +15,31 @@ class DirAssistant {
12
15
  this.context = context;
13
16
  this.tdcache = context.tdcache;
14
17
  this.requestId = context.requestId;
18
+ this.projectId = this.context.projectId;
19
+ this.token = this.context.token;
15
20
  this.intentDir = new DirIntent(context);
16
21
  this.API_ENDPOINT = context.API_ENDPOINT;
17
22
  this.log = context.log;
18
23
  }
19
24
 
20
25
  execute(directive, callback) {
26
+ winston.verbose("Execute Assistant directive");
21
27
  let action;
22
28
  if (directive.action) {
23
29
  action = directive.action;
24
30
  }
25
31
  else {
26
- console.error("Incorrect directive:", JSON.stringify(directive));
32
+ winston.warn("Incorrect directive: ", directive);
27
33
  callback();
28
34
  return;
29
35
  }
30
36
  this.go(action, (stop) => {
31
- if (this.log) {console.log("(DirAssistant, stop?", stop); }
32
37
  callback(stop);
33
38
  });
34
39
  }
35
40
 
36
41
  async go(action, callback) {
37
- if (this.log) {console.log("DirAssistant action:", JSON.stringify(action));}
42
+ winston.debug("(DirAssistant) Action: ", action);
38
43
  let requestAttributes = null;
39
44
  if (this.tdcache) {
40
45
  requestAttributes =
@@ -73,7 +78,7 @@ class DirAssistant {
73
78
  }
74
79
  else {
75
80
  // TODO: LOG SETTINGS ERROR
76
- console.error("DirAssistant error: no assistantId.");
81
+ winston.error("(DirAssistant) Error: no assistantId.");
77
82
  callback();
78
83
  return;
79
84
  }
@@ -84,7 +89,7 @@ class DirAssistant {
84
89
  }
85
90
  else {
86
91
  // TODO: LOG SETTINGS ERROR
87
- console.error("DirAssistant error: no prompt.");
92
+ winston.error("(DirAssistant) Error: no prompt.");
88
93
  callback();
89
94
  return;
90
95
  }
@@ -94,7 +99,7 @@ class DirAssistant {
94
99
  assistantId = filler.fill(_assistantId, requestAttributes);
95
100
  }
96
101
  catch(error) {
97
- console.error("Error while filling assistantId:", error);
102
+ winston.debug("(DirAssistant) Error while filling assistantId:", error);
98
103
  }
99
104
 
100
105
  let prompt = _prompt;
@@ -102,14 +107,13 @@ class DirAssistant {
102
107
  prompt = filler.fill(_prompt, requestAttributes);
103
108
  }
104
109
  catch(error) {
105
- console.error("Error while filling prompt:", error);
110
+ winston.debug("(DirAssistant) Error while filling prompt:", error);
106
111
  }
107
112
 
108
- if (this.log) {
109
- console.log("settings ok");
110
- console.log("prompt:", prompt);
111
- console.log("assistantId:", assistantId);
112
- }
113
+ winston.debug("(DirAssistant) settings ok");
114
+ winston.debug("(DirAssistant) prompt: " + prompt);
115
+ winston.debug("(DirAssistant) assistantId: " + assistantId);
116
+
113
117
  // Condition branches
114
118
  let trueIntent = action.trueIntent;
115
119
  let falseIntent = action.falseIntent;
@@ -123,10 +127,9 @@ class DirAssistant {
123
127
  this.timeout = this.#webrequest_timeout(action, 20000, 1, 300000);
124
128
 
125
129
  let apikey = await this.getGPT_APIKEY();
126
- if (this.log) {console.log("apikey:", apikey);}
127
130
  if (!apikey) {
128
131
  const reply = "OpenAI APIKEY is mandatory for ChatGPT Assistants. Add your personal OpenAI APIKEY in Settings > Integrations";
129
- if (this.log) { console.error(reply); };
132
+ winston.debug("(DirAssistant) Error: " + reply)
130
133
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, assignErrorTo, reply);
131
134
  if (falseIntent) {
132
135
  await this.#executeCondition(false, trueIntent, null, falseIntent, null);
@@ -142,21 +145,19 @@ class DirAssistant {
142
145
  threadId = requestAttributes[threadIdAttribute];
143
146
  if (!threadId || (threadId && threadId.trim() === '') ) {
144
147
  // create thread if it doesn't exist
145
- if (this.log) {console.log("Creating thread");}
148
+ winston.debug("(DirAssistant) Creating thread");
146
149
  const thread = await this.createThread(apikey);
147
- if (this.log) {console.log("Thread crated.");}
150
+ winston.debug("(DirAssistant) Thread crated.");
148
151
  threadId = thread.id;
149
152
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, threadIdAttribute, threadId);
150
- if (this.log) {
151
- console.log("thread:", thread);
152
- console.log("threadId:", threadId);
153
- }
153
+ winston.debug("(DirAssistant) thread: ", thread);
154
+ winston.debug("(DirAssistant) threadId: " + threadId);
154
155
  }
155
156
  else {
156
- if (this.log) { console.log("Reusing threadId (used flow attribute:" + threadIdAttribute + "):", threadId); }
157
+ winston.debug("(DirAssistant) Reusing threadId (used flow attribute: " + threadIdAttribute + "):" + threadId);
157
158
  }
158
159
  await this.addMessage(prompt, threadId, apikey);
159
- if (this.log) {console.log("Message added.");}
160
+ winston.debug("(DirAssistant) Message added.");
160
161
  await this.runThreadOnAssistant(assistantId, threadId, apikey);
161
162
  let messages = await this.threadMessages(threadId, apikey);
162
163
  let lastMessage = null;
@@ -192,7 +193,7 @@ class DirAssistant {
192
193
  }
193
194
  }
194
195
  catch (error) {
195
- if (this.log) { console.error("error:", error); }
196
+ winston.debug("(DirAssistant) error:", error);
196
197
  await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, assignErrorTo, error);
197
198
  if (falseIntent) {
198
199
  await this.#executeCondition(false, trueIntent, null, falseIntent, null);
@@ -221,7 +222,7 @@ class DirAssistant {
221
222
  });
222
223
  }
223
224
  else {
224
- if (this.log) {console.log("No trueIntentDirective specified");}
225
+ winston.debug("(DirAssistant) No trueIntentDirective specified");
225
226
  if (callback) {
226
227
  callback();
227
228
  }
@@ -236,7 +237,7 @@ class DirAssistant {
236
237
  });
237
238
  }
238
239
  else {
239
- if (this.log) {console.log("No falseIntentDirective specified");}
240
+ winston.debug("(DirAssistant) No falseIntentDirective specified");
240
241
  if (callback) {
241
242
  callback();
242
243
  }
@@ -253,7 +254,6 @@ class DirAssistant {
253
254
  if (action.settings.timeout) {
254
255
  if ((typeof action.settings.timeout === "number") && action.settings.timeout > min && action.settings.timeout < max) {
255
256
  timeout = Math.round(action.settings.timeout)
256
- // console.log("new timeout:", timeout);
257
257
  }
258
258
  }
259
259
  return timeout
@@ -264,12 +264,12 @@ class DirAssistant {
264
264
  return process.env.TEST_OPENAI_APIKEY
265
265
  }
266
266
  else {
267
- return await this.getKeyFromIntegrations();
267
+ return await integrationService.getKeyFromIntegrations(this.projectId, 'openai', this.token);
268
268
  }
269
269
  }
270
270
 
271
271
  async createThread(apikey) {
272
- if (this.log) { console.log("creating thread..."); }
272
+ winston.debug("(DirAssistant) creating thread...");
273
273
  return new Promise( async (resolve, reject) => {
274
274
  const url = "https://api.openai.com/v1/threads";
275
275
  const headers = {
@@ -283,16 +283,16 @@ class DirAssistant {
283
283
  method: "POST",
284
284
  timeout: this.timeout
285
285
  };
286
- if (this.log) {console.log("DirAssistant HTTPREQUEST", HTTPREQUEST);}
287
- this.#myrequest(
286
+ winston.debug("(DirAssistant) DirAssistant HttpRequest", HTTPREQUEST);
287
+ httpUtils.request(
288
288
  HTTPREQUEST, async (err, res) => {
289
289
  let status = res.status;
290
290
  if (err) {
291
- if (this.log) {console.error("DirAssistant error:", err);}
291
+ winston.error("(DirAssistant) error: ", err);
292
292
  reject(err);
293
293
  }
294
294
  else if(res.status >= 200 && res.status <= 299) {
295
- if (this.log) {console.log("got threadid res:", res);}
295
+ winston.debug("(DirAssistant) got threadid res: ", res);
296
296
  let thread = res.data;
297
297
  resolve(thread)
298
298
  }
@@ -343,16 +343,16 @@ class DirAssistant {
343
343
  method: "POST",
344
344
  timeout: this.timeout
345
345
  };
346
- if (this.log) {console.log("DirAssistant HTTPREQUEST", HTTPREQUEST);}
347
- this.#myrequest(
346
+ winston.debug("(DirAssistant) HttpRequest: ", HTTPREQUEST);
347
+ httpUtils.request(
348
348
  HTTPREQUEST, async (err, res) => {
349
349
  let status = res.status;
350
350
  if (err) {
351
- if (this.log) {console.error("DirAssistant error:", err);}
351
+ winston.error("(DirAssistant) error: ", err);
352
352
  reject(err);
353
353
  }
354
354
  else if(res.status >= 200 && res.status <= 299) {
355
- if (this.log) {console.log("got response data:", res.data);}
355
+ winston.debug("(DirAddTags) got response data: ", res.data);
356
356
  // let return_body = res.data;
357
357
  resolve();
358
358
  }
@@ -366,23 +366,23 @@ class DirAssistant {
366
366
 
367
367
  async runThreadOnAssistant(assistantId, threadId, apikey) {
368
368
  let _run = await this.createRun(threadId, assistantId, apikey);
369
- if (this.log) {console.log("Got run:", _run);}
369
+ winston.debug("(DirAssistant) Got run: ", _run);
370
370
  let runId = _run.id;
371
- if (this.log) {console.log("runId:", runId);}
371
+ winston.debug("(DirAssistant) runId: " + runId);
372
372
  let status = null;
373
373
  do {
374
- if (this.log) {console.log("Getting run...");}
374
+ winston.debug("(DirAssistant) Getting run...");
375
375
  const wait_for = 2000;
376
- if (this.log) {console.log("Waiting:", wait_for);}
376
+ winston.debug("(DirAssistant) Waiting: " + wait_for);
377
377
  await new Promise(resolve => setTimeout(resolve, wait_for));
378
378
  let run = await this.getRun(threadId, runId, apikey);
379
379
  status = run.status;
380
- if (this.log) {console.log("Run status:", status);}
380
+ winston.debug("(DirAssistant) Run status: " + status);
381
381
  }
382
382
  while (status === "queued" || status === "in_progress" || status === "requires_action" && status === "cancelling");
383
383
  // while (status != "completed" && status != "cancelled" && status != "failed" && status != "expired");
384
384
  // queued, in_progress, requires_action, cancelling
385
- if (this.log) {console.log("Run end.");}
385
+ winston.debug("(DirAssistant) Run end.");
386
386
  }
387
387
 
388
388
  async createRun(threadId, assistantId, apikey) {
@@ -391,7 +391,7 @@ class DirAssistant {
391
391
  }
392
392
 
393
393
  return new Promise( async (resolve, reject) => {
394
- if (this.log) {console.log("adding message to thread...");}
394
+ winston.debug("(DirAssistant) adding message to thread...");
395
395
  const url = `https://api.openai.com/v1/threads/${threadId}/runs`;
396
396
  const headers = {
397
397
  "Authorization": apikey,
@@ -404,15 +404,15 @@ class DirAssistant {
404
404
  method: "POST",
405
405
  timeout: this.timeout
406
406
  };
407
- if (this.log) {console.log("DirAssistant HTTPREQUEST", HTTPREQUEST);}
408
- this.#myrequest(
407
+ winston.debug("(DirAssistant) HttpRequest: ", HTTPREQUEST);
408
+ httpUtils.request(
409
409
  HTTPREQUEST, async (err, res) => {
410
410
  if (err) {
411
- if (this.log) {console.error("DirAssistant error:", err);}
411
+ winston.error("(DirAssistant) error: ", err);
412
412
  reject(err);
413
413
  }
414
414
  else if(res?.status >= 200 && res?.status <= 299) {
415
- if (this.log) {console.log("got response data:", res.data);}
415
+ winston.debug("(DirAddTags) got response data: ", res.data);
416
416
  // let return_body = res.data;
417
417
  resolve(res.data);
418
418
  }
@@ -439,15 +439,15 @@ class DirAssistant {
439
439
  method: "GET",
440
440
  timeout: this.timeout
441
441
  };
442
- if (this.log) {console.log("DirAssistant HTTPREQUEST", HTTPREQUEST);}
443
- this.#myrequest(
442
+ winston.debug("(DirAssistant) HttpRequest: ", HTTPREQUEST);
443
+ httpUtils.request(
444
444
  HTTPREQUEST, async (err, res) => {
445
445
  if (err) {
446
- if (this.log) {console.error("DirAssistant error:", err);}
446
+ winston.error("(DirAssistant) error: ", err);
447
447
  reject(err);
448
448
  }
449
449
  else if(res?.status >= 200 && res?.status <= 299) {
450
- if (this.log) {console.log("got response data:", res.data);}
450
+ winston.debug("(DirAddTags) got response data: ", res.data);
451
451
  // let return_body = res.data;
452
452
  resolve(res.data);
453
453
  }
@@ -474,15 +474,15 @@ class DirAssistant {
474
474
  method: "GET",
475
475
  timeout: this.timeout
476
476
  };
477
- if (this.log) {console.log("DirAssistant HTTPREQUEST", HTTPREQUEST);}
478
- this.#myrequest(
477
+ winston.debug("(DirAssistant) HttpRequest: ", HTTPREQUEST);
478
+ httpUtils.request(
479
479
  HTTPREQUEST, async (err, res) => {
480
480
  if (err) {
481
- if (this.log) {console.error("DirAssistant error:", err);}
481
+ winston.error("(DirAssistant) error: ", err);
482
482
  reject(err);
483
483
  }
484
484
  else if(res?.status >= 200 && res?.status <= 299) {
485
- if (this.log) {console.log("got response data:", res.data);}
485
+ winston.debug("(DirAddTags) got response data: ", res.data);
486
486
  // let return_body = res.data;
487
487
  resolve(res.data);
488
488
  }
@@ -494,148 +494,6 @@ class DirAssistant {
494
494
  );
495
495
  });
496
496
  }
497
-
498
- async getKeyFromIntegrations() {
499
- return new Promise((resolve) => {
500
-
501
- const INTEGRATIONS_HTTPREQUEST = {
502
- url: this.API_ENDPOINT + "/" + this.context.projectId + "/integration/name/openai",
503
- headers: {
504
- 'Content-Type': 'application/json',
505
- 'Authorization': 'JWT ' + this.context.token
506
- },
507
- method: "GET"
508
- }
509
- if (this.log) { console.log("DirAssistant INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
510
-
511
- this.#myrequest(
512
- INTEGRATIONS_HTTPREQUEST, async (err, res) => {
513
- if (err) {
514
- resolve(null);
515
- } else {
516
- let integration = res.data;
517
- if (integration &&
518
- integration.value) {
519
- resolve(integration.value.apikey)
520
- }
521
- else {
522
- resolve(null)
523
- }
524
- }
525
- })
526
- })
527
- }
528
-
529
- #myrequest(options, callback) {
530
- try {
531
- if (this.log) {
532
- console.log("API URL:", options.url);
533
- //console.log("** Options:", JSON.stringify(options));
534
- // Stringify "options". FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - START
535
- let cache = [];
536
- let str_Options = JSON.stringify(options, function(key, value) { // try to use a separate function
537
- if (typeof value === 'object' && value != null) {
538
- if (cache.indexOf(value) !== -1) {
539
- return;
540
- }
541
- cache.push(value);
542
- }
543
- return value;
544
- });
545
- console.log("** Options:", str_Options);
546
-
547
-
548
- }
549
- let axios_options = {
550
- url: options.url,
551
- method: options.method,
552
- params: options.params,
553
- headers: options.headers,
554
- timeout: options.timeout
555
- }
556
-
557
- if (options.json !== null) {
558
- axios_options.data = options.json
559
- }
560
- // if (this.log) {
561
- // console.log("axios_options:", JSON.stringify(axios_options));
562
- // }
563
- if (options.url.startsWith("https:")) {
564
- const httpsAgent = new https.Agent({
565
- rejectUnauthorized: false,
566
- });
567
- axios_options.httpsAgent = httpsAgent;
568
- }
569
-
570
- axios(axios_options)
571
- .then((res) => {
572
- if (this.log) {
573
- // console.log("Success Response:", res);
574
- console.log("Response for url:", options.url);
575
- console.log("Response headers:\n", JSON.stringify(res.headers));
576
- }
577
- if (callback) {
578
- callback(null, res);
579
- }
580
- })
581
- .catch( (err) => {
582
- if (this.log) {
583
- if (err.response) {
584
- console.log("Error Response data:", err.response.data);
585
- }
586
- // FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - START
587
- let cache = [];
588
- let error_log = JSON.stringify(err, function(key, value) { // try to use a separate function
589
- if (typeof value === 'object' && value != null) {
590
- if (cache.indexOf(value) !== -1) {
591
- return;
592
- }
593
- cache.push(value);
594
- }
595
- return value;
596
- });
597
- console.error("(DirAssistant) An error occurred: ", error_log);
598
- // FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - END
599
- }
600
- if (callback) {
601
- let status = 1000;
602
- let cache = [];
603
- let str_error = JSON.stringify(err, function(key, value) { // try to use a separate function
604
- if (typeof value === 'object' && value != null) {
605
- if (cache.indexOf(value) !== -1) {
606
- return;
607
- }
608
- cache.push(value);
609
- }
610
- return value;
611
- });
612
- let error = JSON.parse(str_error) // "status" disappears without this trick
613
- let errorMessage = JSON.stringify(error);
614
- if (error.status) {
615
- status = error.status;
616
- }
617
- if (error.message) {
618
- errorMessage = error.message;
619
- }
620
- let data = null;
621
- if (err.response) {
622
- data = err.response.data;
623
- }
624
- callback(
625
- {
626
- status: status,
627
- data: data,
628
- error: errorMessage
629
- }, data
630
- );
631
- }
632
- });
633
- }
634
- catch(error) {
635
- console.error("Error:", error);
636
- }
637
- }
638
-
639
497
  }
640
498
 
641
499