@magicfeedback/popup-sdk 1.16.0 → 1.16.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.mjs CHANGED
@@ -1724,12 +1724,12 @@ var EngagementTracker = class {
1724
1724
  };
1725
1725
 
1726
1726
  // src/contact/contact-manager.ts
1727
- function stableSerialize(attrs) {
1727
+ function stableSerialize(userId, attrs) {
1728
1728
  const sorted = {};
1729
1729
  for (const key of Object.keys(attrs).sort()) {
1730
1730
  sorted[key] = attrs[key];
1731
1731
  }
1732
- return JSON.stringify(sorted);
1732
+ return JSON.stringify({ userId, attributes: sorted });
1733
1733
  }
1734
1734
  var ContactManager = class {
1735
1735
  constructor(options) {
@@ -1739,18 +1739,23 @@ var ContactManager = class {
1739
1739
  this.post = options.post;
1740
1740
  }
1741
1741
  /**
1742
- * Envía los atributos si cambiaron respecto a lo último enviado.
1743
- * @returns `true` si se envió, `false` si no hubo cambios.
1742
+ * Envía los atributos si cambiaron respecto a lo último enviado. La caché solo se guarda
1743
+ * cuando la API acepta el envío; si falla, la llamada siguiente reintenta. Nunca rechaza.
1744
+ * @returns `true` si se envió, `false` si no hubo cambios o el envío falló.
1744
1745
  */
1745
1746
  async setAttributes(attributes) {
1746
- const serialized = stableSerialize(attributes);
1747
+ const serialized = stableSerialize(this.userId, attributes);
1747
1748
  const last = this.storage.getItem(STORAGE_KEYS.contactAttributes);
1748
1749
  if (last === serialized) return false;
1749
- await this.post({
1750
- publicKey: this.publicKey,
1751
- userId: this.userId,
1752
- userAttributes: attributes
1753
- });
1750
+ try {
1751
+ await this.post({
1752
+ publicKey: this.publicKey,
1753
+ userId: this.userId,
1754
+ userAttributes: attributes
1755
+ });
1756
+ } catch {
1757
+ return false;
1758
+ }
1754
1759
  this.storage.setItem(STORAGE_KEYS.contactAttributes, serialized);
1755
1760
  return true;
1756
1761
  }
@@ -3144,25 +3149,31 @@ var DeepdotsPopups = class {
3144
3149
  return null;
3145
3150
  }
3146
3151
  }
3147
- /** Envía los atributos de contact a la API (`POST /sdk/popups/contact`). */
3152
+ /**
3153
+ * Envía los atributos de contact a la API (`POST /sdk/popups/contact`). Rechaza si no se
3154
+ * pudo enviar (sin red o respuesta no-2xx), para que `ContactManager` no guarde la caché
3155
+ * y la llamada siguiente reintente.
3156
+ */
3148
3157
  async postContact(body) {
3149
3158
  const baseUrl = this.baseUrl;
3150
- if (!baseUrl) return;
3159
+ if (!baseUrl) throw new Error("no base URL");
3151
3160
  const endpoint = `${baseUrl}/sdk/popups/contact`;
3161
+ let response;
3152
3162
  try {
3153
- const response = await fetch(endpoint, {
3163
+ response = await fetch(endpoint, {
3154
3164
  method: "POST",
3155
3165
  headers: { "Content-Type": "application/json" },
3156
3166
  body: JSON.stringify(body)
3157
3167
  });
3158
- if (!response.ok) {
3159
- this.log("Failed to post contact", response.status, response.statusText);
3160
- return;
3161
- }
3162
- this.log("contact \xB7 sent attributes for user:", body.userId);
3163
3168
  } catch (error) {
3164
3169
  this.log("Error posting contact", error);
3170
+ throw error;
3171
+ }
3172
+ if (!response.ok) {
3173
+ this.log("Failed to post contact", response.status, response.statusText);
3174
+ throw new Error(`HTTP ${response.status}`);
3165
3175
  }
3176
+ this.log("contact \xB7 sent attributes for user:", body.userId);
3166
3177
  }
3167
3178
  /** Notifica eventos de popup a la API */
3168
3179
  async postPopupEvent(status, popupId, userId) {