@xenia-martech/sdk-js 1.0.1-dev.1 → 1.1.0-dev.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.
Files changed (54) hide show
  1. package/README.md +54 -4
  2. package/dist/{actions-CcBYV--3.d.cts → actions-CKeiofxJ.d.cts} +19 -1
  3. package/dist/{actions-CcBYV--3.d.ts → actions-CKeiofxJ.d.ts} +19 -1
  4. package/dist/{cookie-names-B3b0XNc2.d.cts → api-URhxxi4x.d.cts} +19 -42
  5. package/dist/{cookie-names-B3b0XNc2.d.ts → api-URhxxi4x.d.ts} +19 -42
  6. package/dist/chunk-5D7SJIO4.cjs +109 -0
  7. package/dist/chunk-5D7SJIO4.cjs.map +1 -0
  8. package/dist/{chunk-XXNDHYAS.js → chunk-FYSEKFIY.js} +45 -103
  9. package/dist/chunk-FYSEKFIY.js.map +1 -0
  10. package/dist/{chunk-WYM4CQVG.cjs → chunk-JEKVN7HK.cjs} +46 -106
  11. package/dist/chunk-JEKVN7HK.cjs.map +1 -0
  12. package/dist/chunk-TLTG3BTC.js +104 -0
  13. package/dist/chunk-TLTG3BTC.js.map +1 -0
  14. package/dist/chunk-U2WBBOYT.cjs +233 -0
  15. package/dist/chunk-U2WBBOYT.cjs.map +1 -0
  16. package/dist/chunk-WYAMPEVV.js +227 -0
  17. package/dist/chunk-WYAMPEVV.js.map +1 -0
  18. package/dist/client/index.cjs +60 -6
  19. package/dist/client/index.cjs.map +1 -1
  20. package/dist/client/index.d.cts +25 -3
  21. package/dist/client/index.d.ts +25 -3
  22. package/dist/client/index.js +59 -7
  23. package/dist/client/index.js.map +1 -1
  24. package/dist/client/react/index.cjs +58 -6
  25. package/dist/client/react/index.cjs.map +1 -1
  26. package/dist/client/react/index.d.cts +2 -2
  27. package/dist/client/react/index.d.ts +2 -2
  28. package/dist/client/react/index.js +58 -6
  29. package/dist/client/react/index.js.map +1 -1
  30. package/dist/cookie-names-8FxbGKGS.d.ts +127 -0
  31. package/dist/cookie-names-QYMiEMAq.d.cts +127 -0
  32. package/dist/index.cjs +41 -32
  33. package/dist/index.d.cts +4 -71
  34. package/dist/index.d.ts +4 -71
  35. package/dist/index.js +2 -1
  36. package/dist/next/index.cjs +59 -0
  37. package/dist/next/index.cjs.map +1 -0
  38. package/dist/next/index.d.cts +45 -0
  39. package/dist/next/index.d.ts +45 -0
  40. package/dist/next/index.js +54 -0
  41. package/dist/next/index.js.map +1 -0
  42. package/dist/server/index.cjs +38 -214
  43. package/dist/server/index.cjs.map +1 -1
  44. package/dist/server/index.d.cts +5 -104
  45. package/dist/server/index.d.ts +5 -104
  46. package/dist/server/index.js +5 -204
  47. package/dist/server/index.js.map +1 -1
  48. package/dist/xenia-sdk.iife.js +3 -3
  49. package/dist/xenia-sdk.iife.js.map +1 -1
  50. package/dist/xenia-server-D-ozqSgv.d.ts +126 -0
  51. package/dist/xenia-server-rs3Gpbw7.d.cts +126 -0
  52. package/package.json +26 -7
  53. package/dist/chunk-WYM4CQVG.cjs.map +0 -1
  54. package/dist/chunk-XXNDHYAS.js.map +0 -1
@@ -163,12 +163,22 @@ var HttpError = class extends Error {
163
163
  status;
164
164
  data;
165
165
  };
166
+ var CircuitOpenError = class extends Error {
167
+ constructor(endpoint) {
168
+ super(`Circuit open for ${endpoint}: failing fast until the cooldown passes`);
169
+ this.endpoint = endpoint;
170
+ this.name = "CircuitOpenError";
171
+ }
172
+ endpoint;
173
+ };
174
+ var breakers = /* @__PURE__ */ new Map();
166
175
  var HttpClient = class {
167
176
  baseUrl;
168
177
  defaultHeaders;
169
178
  timeout;
170
179
  retry;
171
180
  debug;
181
+ breaker;
172
182
  constructor(opts) {
173
183
  this.baseUrl = (opts.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
174
184
  this.defaultHeaders = opts.defaultHeaders ?? {};
@@ -178,8 +188,37 @@ var HttpClient = class {
178
188
  baseDelayMs: opts.retry?.baseDelayMs ?? 300
179
189
  };
180
190
  this.debug = opts.debug ?? false;
191
+ const cb = opts.circuitBreaker === true ? {} : opts.circuitBreaker || null;
192
+ this.breaker = cb && { failureThreshold: cb.failureThreshold ?? 3, cooldownMs: cb.cooldownMs ?? 3e4 };
181
193
  }
182
194
  async request(opts) {
195
+ if (!this.breaker) return this.send(opts);
196
+ const key = buildUrl(this.baseUrl + opts.path, opts.query);
197
+ const b = breakers.get(key) ?? { failures: 0, openedAt: 0 };
198
+ if (b.openedAt) {
199
+ if (Date.now() - b.openedAt < this.breaker.cooldownMs) throw new CircuitOpenError(key);
200
+ b.openedAt = 0;
201
+ b.failures = this.breaker.failureThreshold - 1;
202
+ }
203
+ try {
204
+ const result = await this.send(opts);
205
+ breakers.delete(key);
206
+ return result;
207
+ } catch (err) {
208
+ if (err instanceof HttpError && !this.isRetryableStatus(err.status)) {
209
+ breakers.delete(key);
210
+ throw err;
211
+ }
212
+ b.failures += 1;
213
+ if (b.failures >= this.breaker.failureThreshold) {
214
+ b.openedAt = Date.now();
215
+ this.log(`circuit opened for ${opts.path} after ${b.failures} failures`);
216
+ }
217
+ breakers.set(key, b);
218
+ throw err;
219
+ }
220
+ }
221
+ async send(opts) {
183
222
  const url = buildUrl(this.baseUrl + opts.path, opts.query);
184
223
  const method = opts.method ?? "GET";
185
224
  const headers = {
@@ -332,6 +371,7 @@ var XeniaApi = class {
332
371
  defaultHeaders: config.headers,
333
372
  timeout: config.timeout,
334
373
  retry: config.retry,
374
+ circuitBreaker: config.circuitBreaker,
335
375
  debug: config.debug
336
376
  });
337
377
  }
@@ -504,6 +544,18 @@ var XeniaApi = class {
504
544
  }
505
545
  };
506
546
 
547
+ // src/core/cookie-names.ts
548
+ var DEFAULT_PREFIX = "xenia";
549
+ function visitorCookieName(prefix = DEFAULT_PREFIX) {
550
+ return `${prefix || DEFAULT_PREFIX}_vid`;
551
+ }
552
+ function consentCookieName(prefix = DEFAULT_PREFIX) {
553
+ return `${prefix || DEFAULT_PREFIX}_consent`;
554
+ }
555
+ function consentCookie(state, prefix, maxAgeDays = 365) {
556
+ return `${consentCookieName(prefix)}=${encodeURIComponent(JSON.stringify(state))}; path=/; max-age=${Math.floor(maxAgeDays * 86400)}; SameSite=Lax`;
557
+ }
558
+
507
559
  // src/core/event-payload.ts
508
560
  var RESERVED = /* @__PURE__ */ new Set([
509
561
  "eventId",
@@ -933,12 +985,6 @@ var AutoTracker = class {
933
985
  }
934
986
  };
935
987
 
936
- // src/core/cookie-names.ts
937
- var DEFAULT_PREFIX = "xenia";
938
- function visitorCookieName(prefix = DEFAULT_PREFIX) {
939
- return `${prefix || DEFAULT_PREFIX}_vid`;
940
- }
941
-
942
988
  // src/client/identity.ts
943
989
  var SESSION_TIMEOUT_MS = 30 * 60 * 1e3;
944
990
  var Identity = class {
@@ -2216,6 +2262,12 @@ var XeniaClient = class {
2216
2262
  async consent(state) {
2217
2263
  const wasAllowed = this.analyticsAllowed();
2218
2264
  this.consentState = { ...this.consentState, ...state };
2265
+ if (isBrowser()) {
2266
+ try {
2267
+ document.cookie = consentCookie(this.consentState, this.options.prefix);
2268
+ } catch {
2269
+ }
2270
+ }
2219
2271
  if (this.analyticsAllowed() && !wasAllowed) {
2220
2272
  this.analyticsQueue.start();
2221
2273
  void this.analyticsQueue.flush();
@@ -2314,6 +2366,8 @@ exports.WIDGET_RENDERERS = WIDGET_RENDERERS;
2314
2366
  exports.XeniaClient = XeniaClient;
2315
2367
  exports.collectFields = collectFields;
2316
2368
  exports.computeSpinRotation = computeSpinRotation;
2369
+ exports.consentCookie = consentCookie;
2370
+ exports.consentCookieName = consentCookieName;
2317
2371
  exports.createXeniaClient = createXeniaClient;
2318
2372
  exports.extractContentSwaps = extractContentSwaps;
2319
2373
  exports.hideContent = hideContent;