@mhubcloud/tms_core 3.4.0 → 3.4.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/dist/index.js CHANGED
@@ -31,23 +31,12 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
33
  DefaultPublisher: () => defaultPublisher,
34
- broker: () => broker_exports,
35
34
  publisher: () => publisher,
36
35
  testUtils: () => testUtils_exports,
37
36
  utils: () => utils_exports
38
37
  });
39
38
  module.exports = __toCommonJS(index_exports);
40
39
 
41
- // src/broker.ts
42
- var broker_exports = {};
43
- __export(broker_exports, {
44
- dispatchEvent: () => dispatchEvent,
45
- logToCosmosDb: () => logToCosmosDb,
46
- resolveEvent: () => resolveEvent
47
- });
48
- var import_cosmos = require("@azure/cosmos");
49
- var import_service_bus = require("@azure/service-bus");
50
-
51
40
  // src/utils/utils.ts
52
41
  var utils_exports = {};
53
42
  __export(utils_exports, {
@@ -55,19 +44,19 @@ __export(utils_exports, {
55
44
  encodeEmail: () => encodeEmail,
56
45
  extractAdCode: () => extractAdCode,
57
46
  flatten: () => flatten,
58
- formatError: () => formatError,
59
47
  handlePlatformRequest: () => handlePlatformRequest,
60
48
  handleResponse: () => handleResponse,
61
49
  logEvent: () => logEvent,
62
50
  logException: () => logException,
63
51
  logMessage: () => logMessage,
52
+ parseErrorMessage: () => parseErrorMessage,
64
53
  prepareTestRequestHeaders: () => prepareTestRequestHeaders,
65
54
  removeUndefined: () => removeUndefined
66
55
  });
67
56
  var crypto = __toESM(require("crypto"));
68
57
  var appInsights = __toESM(require("applicationinsights"));
69
58
  var aiClient = null;
70
- if (process.env.APPLICATIONINSIGHTS_CONNECTION_STRING) {
59
+ if (process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || process.env.APPINSIGHTS_INSTRUMENTATIONKEY) {
71
60
  appInsights.setup().setAutoCollectDependencies(false).setAutoCollectConsole(true, false).setSendLiveMetrics(false).setAutoCollectRequests(false).setAutoCollectPerformance(false).setUseDiskRetryCaching(false).start();
72
61
  aiClient = appInsights.defaultClient;
73
62
  }
@@ -100,17 +89,15 @@ function logMessage(level, message, properties) {
100
89
  properties
101
90
  });
102
91
  }
103
- function logEvent(publisher2, event, region, status, payload) {
92
+ function logEvent(publisher2, event, region) {
104
93
  const properties = {
105
94
  publisher: publisher2,
106
95
  event,
107
- region,
108
- status,
109
- payload
96
+ region
110
97
  };
111
98
  logMessage(1, "Event dispatched to platform", properties);
112
99
  }
113
- function logException(exception) {
100
+ function logException(log2, exception) {
114
101
  aiClient?.trackException({ exception });
115
102
  }
116
103
  function removeUndefined(originalValue) {
@@ -224,10 +211,7 @@ async function handlePlatformRequest({
224
211
  // eslint-disable-next-line @typescript-eslint/naming-convention
225
212
  headers: { ...requestOptions.headers }
226
213
  };
227
- const rawPlatformResponse = await fetch(
228
- fullUrl,
229
- fetchRequestOptions
230
- );
214
+ const rawPlatformResponse = await fetch(fullUrl, fetchRequestOptions);
231
215
  if (process.env.PUBLISHER_TESTURL) {
232
216
  await handleTestRequest({
233
217
  fetchRequestOptions,
@@ -241,22 +225,10 @@ async function handlePlatformRequest({
241
225
  rawPlatformResponse,
242
226
  event.event
243
227
  );
244
- logEvent(
245
- source,
246
- event.event,
247
- event.region,
248
- rawPlatformResponse.status,
249
- requestOptions.body ? JSON.stringify(requestOptions.body) : void 0
250
- );
228
+ logEvent(source, event.event, event.region);
251
229
  return processedPlatformResponse;
252
230
  } else {
253
- logEvent(
254
- source,
255
- event.event,
256
- event.region,
257
- rawPlatformResponse.status,
258
- requestOptions.body ? JSON.stringify(requestOptions.body) : void 0
259
- );
231
+ logEvent(source, event.event, event.region);
260
232
  }
261
233
  }
262
234
  async function handleTestRequest(params) {
@@ -279,115 +251,18 @@ async function handleTestRequest(params) {
279
251
  } catch {
280
252
  }
281
253
  }
282
- function formatError(error) {
283
- let formattedError;
254
+ function parseErrorMessage(error) {
255
+ let errorMessage;
284
256
  if (error instanceof Error) {
285
- formattedError = error;
286
257
  if (error.cause) {
287
- formattedError.message = `${error.message} Cause: ${error.cause.toString()}`;
258
+ errorMessage = `${error.message} Cause: ${error.cause.toString()}`;
288
259
  } else {
289
- formattedError.message = error.message;
260
+ errorMessage = error.message;
290
261
  }
291
262
  } else {
292
- formattedError = new Error(error);
293
- }
294
- return formattedError;
295
- }
296
-
297
- // src/broker.ts
298
- var serviceBusConnectionString = process.env.ServiceBusConnectionString;
299
- var cosmosConnectionString = process.env.CosmosDbConnectionString;
300
- var cosmosDatabaseName = process.env.CosmosDbDatabaseName;
301
- var cosmosContainerName = process.env.CosmosDbEventsContainerName;
302
- var serviceBusClient;
303
- var cosmosClient;
304
- var senders = {};
305
- function resolveEvent(eventCode, publisherConfig) {
306
- if (!publisherConfig) {
307
- const envConfig = process.env.Broker_PublisherConfig;
308
- publisherConfig = envConfig ? JSON.parse(envConfig) : {};
309
- }
310
- const publishers = [];
311
- for (const publisher2 of Object.keys(publisherConfig)) {
312
- const publisherEvents = publisherConfig[publisher2];
313
- if (publisherEvents) {
314
- if (publisherEvents[0] === "*") {
315
- let matchesExcluded = false;
316
- if (Array.isArray(publisherEvents[1]) && publisherEvents[1].length > 0) {
317
- for (const event of publisherEvents[1]) {
318
- if (matchesExcluded) {
319
- break;
320
- }
321
- if (typeof event === "string") {
322
- const regex = new RegExp(event);
323
- matchesExcluded = regex.test(eventCode);
324
- }
325
- }
326
- }
327
- if (!matchesExcluded) {
328
- publishers.push(publisher2);
329
- }
330
- } else {
331
- let match = false;
332
- for (const event of publisherEvents) {
333
- if (typeof event === "string") {
334
- const regex = new RegExp(event);
335
- match = regex.test(eventCode);
336
- }
337
- if (match) {
338
- publishers.push(publisher2);
339
- }
340
- }
341
- }
342
- }
343
- }
344
- if (publishers.length === 0) {
345
- return null;
346
- }
347
- return publishers;
348
- }
349
- async function dispatchEvent(message, queues) {
350
- if (!serviceBusClient) {
351
- if (!serviceBusConnectionString) {
352
- throw new Error("Service Bus connection string is missing.");
353
- }
354
- serviceBusClient = new import_service_bus.ServiceBusClient(serviceBusConnectionString);
355
- }
356
- await Promise.all(
357
- queues.map(async (queue) => {
358
- try {
359
- let sender;
360
- if (senders[queue]) {
361
- sender = senders[queue];
362
- } else {
363
- senders[queue] = serviceBusClient.createSender(queue);
364
- sender = senders[queue];
365
- }
366
- await sender.sendMessages({ body: message });
367
- } catch (e) {
368
- console.log(`Sending to queue ${queue} failed`);
369
- }
370
- })
371
- );
372
- }
373
- async function logToCosmosDb(message) {
374
- if (!cosmosClient) {
375
- if (!cosmosConnectionString) {
376
- throw new Error("Cosmos DB connection string is missing.");
377
- }
378
- cosmosClient = new import_cosmos.CosmosClient(cosmosConnectionString);
379
- }
380
- if (!cosmosDatabaseName) {
381
- throw new Error("Cosmos DB database name is missing.");
382
- }
383
- if (!cosmosContainerName) {
384
- throw new Error("Cosmos DB events container name is missing.");
385
- }
386
- try {
387
- await cosmosClient.database(cosmosDatabaseName).container(cosmosContainerName).items.create(message);
388
- } catch (e) {
389
- logException(e);
263
+ errorMessage = error;
390
264
  }
265
+ return errorMessage;
391
266
  }
392
267
 
393
268
  // src/defaultPublisher.ts
@@ -408,21 +283,15 @@ async function defaultPublisher(azureFunctionContext, serviceBusMessage, source,
408
283
  for (const result of results) {
409
284
  if (result.status === "rejected") {
410
285
  if (result.reason instanceof Error) {
411
- const formattedError = formatError(result.reason);
412
- logException(
413
- azureFunctionContext.log,
414
- new Error(formattedError.message)
415
- );
416
- throw formattedError;
286
+ const errorMessage = parseErrorMessage(result.reason);
287
+ logException(azureFunctionContext.log, new Error(errorMessage));
288
+ throw new Error(errorMessage);
417
289
  } else {
418
- const formattedError = formatError(
290
+ const errorMessage = parseErrorMessage(
419
291
  typeof result.reason === "object" ? JSON.stringify(result.reason) : result.reason
420
292
  );
421
- logException(
422
- azureFunctionContext.log,
423
- new Error(formattedError.message)
424
- );
425
- throw formattedError;
293
+ logException(azureFunctionContext.log, new Error(errorMessage));
294
+ throw new Error(errorMessage);
426
295
  }
427
296
  }
428
297
  }
@@ -643,7 +512,7 @@ var googleAds = {
643
512
  },
644
513
  body: JSON.stringify(body)
645
514
  };
646
- const baseUrl = `https://googleads.googleapis.com/v17/customers/${config.customerId}:uploadClickConversions`;
515
+ const baseUrl = `https://googleads.googleapis.com/v20/customers/${config.customerId}:uploadClickConversions`;
647
516
  const response = await fetch(baseUrl, requestOptions);
648
517
  const responseJson = await response.json();
649
518
  const knownErrors = [
@@ -663,13 +532,7 @@ var googleAds = {
663
532
  } else if (responseJson.error) {
664
533
  throw new Error(responseJson.error.message);
665
534
  } else {
666
- logEvent(
667
- this.source,
668
- event.event,
669
- event.region,
670
- response.status,
671
- requestOptions.body
672
- );
535
+ logEvent(this.source, event.event, event.region);
673
536
  }
674
537
  },
675
538
  async getToken(config) {
@@ -891,7 +754,6 @@ var publisher = {
891
754
  // Annotate the CommonJS export names for ESM import in node:
892
755
  0 && (module.exports = {
893
756
  DefaultPublisher,
894
- broker,
895
757
  publisher,
896
758
  testUtils,
897
759
  utils
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mhubcloud/tms_core",
3
- "version": "3.4.0",
3
+ "version": "3.4.1",
4
4
  "description": "Core function for mHub Cloud TMS publishers",
5
5
  "homepage": "https://mhubcloud.com/",
6
6
  "license": "Apache 2.0",
@@ -101,7 +101,7 @@ export const googleAds: GoogleAdsPlatformPublisher = {
101
101
  body: JSON.stringify(body),
102
102
  };
103
103
 
104
- const baseUrl = `https://googleads.googleapis.com/v17/customers/${config.customerId}:uploadClickConversions`;
104
+ const baseUrl = `https://googleads.googleapis.com/v20/customers/${config.customerId}:uploadClickConversions`;
105
105
 
106
106
  const response = await fetch(baseUrl, requestOptions);
107
107
  const responseJson: GoogleAdsResponse = await response.json();