@zaplier/sdk 1.0.1 → 1.0.2

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/sdk.js CHANGED
@@ -6310,8 +6310,8 @@
6310
6310
  */
6311
6311
  async makeRequest(endpoint, data, method = "POST") {
6312
6312
  const url = `${this.config.apiEndpoint}${endpoint}`;
6313
- // Always use standard fetch to get the response JSON (which contains visitorId)
6314
- // Use anti-adblock as a parallel fallback method if standard fetch fails
6313
+ // Always try standard fetch first to get the response JSON (which contains visitorId)
6314
+ // Use anti-adblock as fallback if standard fetch is blocked or fails
6315
6315
  const options = {
6316
6316
  method,
6317
6317
  headers: {
@@ -6322,7 +6322,12 @@
6322
6322
  options.body = JSON.stringify(data);
6323
6323
  }
6324
6324
  try {
6325
- const response = await fetch(url, options);
6325
+ // Add timeout to detect if fetch is blocked/hanging
6326
+ const fetchWithTimeout = Promise.race([
6327
+ fetch(url, options),
6328
+ new Promise((_, reject) => setTimeout(() => reject(new Error("Fetch timeout - possibly blocked")), 5000)),
6329
+ ]);
6330
+ const response = await fetchWithTimeout;
6326
6331
  if (!response.ok) {
6327
6332
  throw new Error(`HTTP ${response.status}: ${response.statusText}`);
6328
6333
  }
@@ -6338,23 +6343,30 @@
6338
6343
  return jsonResponse;
6339
6344
  }
6340
6345
  catch (error) {
6341
- // If standard fetch fails, try anti-adblock as fallback
6346
+ // If standard fetch fails (blocked, timeout, or network error), try anti-adblock as fallback
6342
6347
  if (this.antiAdblockManager && method === "POST") {
6348
+ if (this.config.debug) {
6349
+ console.warn(`[Zaplier] Standard fetch failed (${error instanceof Error ? error.message : String(error)}), trying anti-adblock fallback`);
6350
+ }
6343
6351
  try {
6344
6352
  const result = await this.antiAdblockManager.send(data, endpoint);
6345
6353
  if (result.success) {
6346
6354
  if (this.config.debug) {
6347
6355
  console.log(`[Zaplier] Request sent via ${result.method} (${result.latency}ms) as fallback`);
6348
6356
  }
6349
- // Try to get response from a separate request
6350
- // Note: This is a limitation - we can't get visitorId from anti-adblock transports
6351
- // But at least the event was sent
6357
+ // Event was successfully sent via anti-adblock fallback
6358
+ // Note: visitorId will be obtained on the next successful request
6359
+ // (either standard fetch or anti-adblock) when the backend responds with it
6360
+ // This is acceptable because:
6361
+ // 1. The event was successfully tracked
6362
+ // 2. The visitorId will be available after the next event is sent
6363
+ // 3. Making an additional GET request here could also be blocked
6352
6364
  return { success: true, method: result.method };
6353
6365
  }
6354
6366
  }
6355
6367
  catch (antiAdblockError) {
6356
6368
  if (this.config.debug) {
6357
- console.warn("[Zaplier] Both standard fetch and anti-adblock failed");
6369
+ console.warn("[Zaplier] Both standard fetch and anti-adblock failed:", antiAdblockError);
6358
6370
  }
6359
6371
  }
6360
6372
  }