@meltwater/conversations-api-services 1.1.6 → 1.1.8

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.
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.getInsights = getInsights;
7
+ exports.getMediaFromId = getMediaFromId;
7
8
  exports.getProfile = getProfile;
8
9
  exports.manageReply = manageReply;
9
10
  exports.quote = quote;
@@ -234,6 +235,26 @@ async function manageReply(token, externalId) {
234
235
  return response.body;
235
236
  }
236
237
 
238
+ /**
239
+ * Retrieves media information from Threads.
240
+ * @param {string} token - The access token for authentication.
241
+ * @param {string} externalId - The external ID of the media.
242
+ * @param {string} fields - The fields to retrieve from the media.
243
+ * @param {Object} logger - Logger instance for logging.
244
+ * @returns {Promise<Object>} The API response containing the media information.
245
+ */
246
+ async function getMediaFromId(token, externalId, fields, logger) {
247
+ const mediaId = (0, _externalIdHelpers.removePrefix)(externalId);
248
+ let response = await sendRequest(`${THREADS_URL}/${mediaId}`, {
249
+ access_token: token,
250
+ fields
251
+ }, logger);
252
+ (0, _loggerHelpers.loggerInfo)(logger, `Native Threads API getMediaFromId Response`, {
253
+ responseBody: JSON.stringify(response.body)
254
+ });
255
+ return response.body;
256
+ }
257
+
237
258
  /**
238
259
  * Constructs the query parameters for replying to a post.
239
260
  * @param {string} accessToken - The access token for authentication.
@@ -249,7 +270,7 @@ async function constructReplyQuery(accessToken, inReplyToId, text, asset, logger
249
270
  const baseQuery = {
250
271
  access_token: accessToken,
251
272
  text,
252
- [isQuote ? 'quote_post_id' : 'in_reply_to_id']: inReplyToId
273
+ [isQuote ? 'quote_post_id' : 'reply_to_id']: inReplyToId
253
274
  };
254
275
  const mediaQuery = getMediaQuery(asset);
255
276
  const query = {
@@ -261,7 +282,7 @@ async function constructReplyQuery(accessToken, inReplyToId, text, asset, logger
261
282
  await confirmCreationId(accessToken, containerResponse.body.id, logger);
262
283
  return {
263
284
  ...query,
264
- containerResponse
285
+ creation_id: containerResponse.body.id
265
286
  };
266
287
  } catch (error) {
267
288
  (0, _loggerHelpers.loggerError)(logger, 'Error constructing reply query', error);
@@ -222,6 +222,26 @@ export async function manageReply(token, externalId) {
222
222
  return response.body;
223
223
  }
224
224
 
225
+ /**
226
+ * Retrieves media information from Threads.
227
+ * @param {string} token - The access token for authentication.
228
+ * @param {string} externalId - The external ID of the media.
229
+ * @param {string} fields - The fields to retrieve from the media.
230
+ * @param {Object} logger - Logger instance for logging.
231
+ * @returns {Promise<Object>} The API response containing the media information.
232
+ */
233
+ export async function getMediaFromId(token, externalId, fields, logger) {
234
+ const mediaId = removePrefix(externalId);
235
+ let response = await sendRequest(`${THREADS_URL}/${mediaId}`, {
236
+ access_token: token,
237
+ fields
238
+ }, logger);
239
+ loggerInfo(logger, `Native Threads API getMediaFromId Response`, {
240
+ responseBody: JSON.stringify(response.body)
241
+ });
242
+ return response.body;
243
+ }
244
+
225
245
  /**
226
246
  * Constructs the query parameters for replying to a post.
227
247
  * @param {string} accessToken - The access token for authentication.
@@ -237,7 +257,7 @@ async function constructReplyQuery(accessToken, inReplyToId, text, asset, logger
237
257
  const baseQuery = {
238
258
  access_token: accessToken,
239
259
  text,
240
- [isQuote ? 'quote_post_id' : 'in_reply_to_id']: inReplyToId
260
+ [isQuote ? 'quote_post_id' : 'reply_to_id']: inReplyToId
241
261
  };
242
262
  const mediaQuery = getMediaQuery(asset);
243
263
  const query = {
@@ -249,7 +269,7 @@ async function constructReplyQuery(accessToken, inReplyToId, text, asset, logger
249
269
  await confirmCreationId(accessToken, containerResponse.body.id, logger);
250
270
  return {
251
271
  ...query,
252
- containerResponse
272
+ creation_id: containerResponse.body.id
253
273
  };
254
274
  } catch (error) {
255
275
  loggerError(logger, 'Error constructing reply query', error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meltwater/conversations-api-services",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "description": "Repository to contain all conversations api services shared across our services",
5
5
  "main": "dist/cjs/data-access/index.js",
6
6
  "module": "dist/esm/data-access/index.js",
@@ -22,7 +22,12 @@ const LONG_WAIT_TIME_MS = 60000; // 60 seconds
22
22
  * @returns {Promise<any>} The result of the operation.
23
23
  * @throws {Error} If the operation fails after all retries.
24
24
  */
25
- async function retryWithBackoff(operation, maxRetries, initialWaitTime, logger) {
25
+ async function retryWithBackoff(
26
+ operation,
27
+ maxRetries,
28
+ initialWaitTime,
29
+ logger
30
+ ) {
26
31
  let waitTime = initialWaitTime;
27
32
 
28
33
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
@@ -35,7 +40,9 @@ async function retryWithBackoff(operation, maxRetries, initialWaitTime, logger)
35
40
 
36
41
  loggerInfo(
37
42
  logger,
38
- `Retry attempt ${attempt + 1} failed. Retrying in ${waitTime / 1000} seconds...`
43
+ `Retry attempt ${attempt + 1} failed. Retrying in ${
44
+ waitTime / 1000
45
+ } seconds...`
39
46
  );
40
47
  await new Promise((resolve) => setTimeout(resolve, waitTime));
41
48
  waitTime *= 2; // Exponential backoff
@@ -72,9 +79,18 @@ async function confirmCreationId(accessToken, creationId, logger) {
72
79
  };
73
80
 
74
81
  try {
75
- return await retryWithBackoff(operation, MAX_RETRY_COUNT, SHORT_WAIT_TIME_MS, logger);
82
+ return await retryWithBackoff(
83
+ operation,
84
+ MAX_RETRY_COUNT,
85
+ SHORT_WAIT_TIME_MS,
86
+ logger
87
+ );
76
88
  } catch (error) {
77
- loggerError(logger, `Creation ID ${creationId} confirmation failed: ${error.message}`, error);
89
+ loggerError(
90
+ logger,
91
+ `Creation ID ${creationId} confirmation failed: ${error.message}`,
92
+ error
93
+ );
78
94
  error.code = 408; // Request Timeout
79
95
  throw error;
80
96
  }
@@ -278,6 +294,28 @@ export async function manageReply(token, externalId, hide = false, logger) {
278
294
  return response.body;
279
295
  }
280
296
 
297
+ /**
298
+ * Retrieves media information from Threads.
299
+ * @param {string} token - The access token for authentication.
300
+ * @param {string} externalId - The external ID of the media.
301
+ * @param {string} fields - The fields to retrieve from the media.
302
+ * @param {Object} logger - Logger instance for logging.
303
+ * @returns {Promise<Object>} The API response containing the media information.
304
+ */
305
+ export async function getMediaFromId(token, externalId, fields, logger) {
306
+ const mediaId = removePrefix(externalId);
307
+ let response = await sendRequest(
308
+ `${THREADS_URL}/${mediaId}`,
309
+ { access_token: token, fields },
310
+ logger
311
+ );
312
+
313
+ loggerInfo(logger, `Native Threads API getMediaFromId Response`, {
314
+ responseBody: JSON.stringify(response.body),
315
+ });
316
+ return response.body;
317
+ }
318
+
281
319
  /**
282
320
  * Constructs the query parameters for replying to a post.
283
321
  * @param {string} accessToken - The access token for authentication.
@@ -299,7 +337,7 @@ async function constructReplyQuery(
299
337
  const baseQuery = {
300
338
  access_token: accessToken,
301
339
  text,
302
- [isQuote ? 'quote_post_id' : 'in_reply_to_id']: inReplyToId,
340
+ [isQuote ? 'quote_post_id' : 'reply_to_id']: inReplyToId,
303
341
  };
304
342
 
305
343
  const mediaQuery = getMediaQuery(asset);
@@ -315,7 +353,7 @@ async function constructReplyQuery(
315
353
 
316
354
  await confirmCreationId(accessToken, containerResponse.body.id, logger);
317
355
 
318
- return { ...query, containerResponse };
356
+ return { ...query, creation_id: containerResponse.body.id };
319
357
  } catch (error) {
320
358
  loggerError(logger, 'Error constructing reply query', error);
321
359
  throw error;