@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
@@ -170,12 +170,22 @@ var HttpError = class extends Error {
170
170
  status;
171
171
  data;
172
172
  };
173
+ var CircuitOpenError = class extends Error {
174
+ constructor(endpoint) {
175
+ super(`Circuit open for ${endpoint}: failing fast until the cooldown passes`);
176
+ this.endpoint = endpoint;
177
+ this.name = "CircuitOpenError";
178
+ }
179
+ endpoint;
180
+ };
181
+ var breakers = /* @__PURE__ */ new Map();
173
182
  var HttpClient = class {
174
183
  baseUrl;
175
184
  defaultHeaders;
176
185
  timeout;
177
186
  retry;
178
187
  debug;
188
+ breaker;
179
189
  constructor(opts) {
180
190
  this.baseUrl = (opts.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
181
191
  this.defaultHeaders = opts.defaultHeaders ?? {};
@@ -185,8 +195,37 @@ var HttpClient = class {
185
195
  baseDelayMs: opts.retry?.baseDelayMs ?? 300
186
196
  };
187
197
  this.debug = opts.debug ?? false;
198
+ const cb = opts.circuitBreaker === true ? {} : opts.circuitBreaker || null;
199
+ this.breaker = cb && { failureThreshold: cb.failureThreshold ?? 3, cooldownMs: cb.cooldownMs ?? 3e4 };
188
200
  }
189
201
  async request(opts) {
202
+ if (!this.breaker) return this.send(opts);
203
+ const key = buildUrl(this.baseUrl + opts.path, opts.query);
204
+ const b = breakers.get(key) ?? { failures: 0, openedAt: 0 };
205
+ if (b.openedAt) {
206
+ if (Date.now() - b.openedAt < this.breaker.cooldownMs) throw new CircuitOpenError(key);
207
+ b.openedAt = 0;
208
+ b.failures = this.breaker.failureThreshold - 1;
209
+ }
210
+ try {
211
+ const result = await this.send(opts);
212
+ breakers.delete(key);
213
+ return result;
214
+ } catch (err) {
215
+ if (err instanceof HttpError && !this.isRetryableStatus(err.status)) {
216
+ breakers.delete(key);
217
+ throw err;
218
+ }
219
+ b.failures += 1;
220
+ if (b.failures >= this.breaker.failureThreshold) {
221
+ b.openedAt = Date.now();
222
+ this.log(`circuit opened for ${opts.path} after ${b.failures} failures`);
223
+ }
224
+ breakers.set(key, b);
225
+ throw err;
226
+ }
227
+ }
228
+ async send(opts) {
190
229
  const url = buildUrl(this.baseUrl + opts.path, opts.query);
191
230
  const method = opts.method ?? "GET";
192
231
  const headers = {
@@ -339,6 +378,7 @@ var XeniaApi = class {
339
378
  defaultHeaders: config.headers,
340
379
  timeout: config.timeout,
341
380
  retry: config.retry,
381
+ circuitBreaker: config.circuitBreaker,
342
382
  debug: config.debug
343
383
  });
344
384
  }
@@ -511,6 +551,18 @@ var XeniaApi = class {
511
551
  }
512
552
  };
513
553
 
554
+ // src/core/cookie-names.ts
555
+ var DEFAULT_PREFIX = "xenia";
556
+ function visitorCookieName(prefix = DEFAULT_PREFIX) {
557
+ return `${prefix || DEFAULT_PREFIX}_vid`;
558
+ }
559
+ function consentCookieName(prefix = DEFAULT_PREFIX) {
560
+ return `${prefix || DEFAULT_PREFIX}_consent`;
561
+ }
562
+ function consentCookie(state, prefix, maxAgeDays = 365) {
563
+ return `${consentCookieName(prefix)}=${encodeURIComponent(JSON.stringify(state))}; path=/; max-age=${Math.floor(maxAgeDays * 86400)}; SameSite=Lax`;
564
+ }
565
+
514
566
  // src/core/event-payload.ts
515
567
  var RESERVED = /* @__PURE__ */ new Set([
516
568
  "eventId",
@@ -940,12 +992,6 @@ var AutoTracker = class {
940
992
  }
941
993
  };
942
994
 
943
- // src/core/cookie-names.ts
944
- var DEFAULT_PREFIX = "xenia";
945
- function visitorCookieName(prefix = DEFAULT_PREFIX) {
946
- return `${prefix || DEFAULT_PREFIX}_vid`;
947
- }
948
-
949
995
  // src/client/identity.ts
950
996
  var SESSION_TIMEOUT_MS = 30 * 60 * 1e3;
951
997
  var Identity = class {
@@ -2223,6 +2269,12 @@ var XeniaClient = class {
2223
2269
  async consent(state) {
2224
2270
  const wasAllowed = this.analyticsAllowed();
2225
2271
  this.consentState = { ...this.consentState, ...state };
2272
+ if (isBrowser()) {
2273
+ try {
2274
+ document.cookie = consentCookie(this.consentState, this.options.prefix);
2275
+ } catch {
2276
+ }
2277
+ }
2226
2278
  if (this.analyticsAllowed() && !wasAllowed) {
2227
2279
  this.analyticsQueue.start();
2228
2280
  void this.analyticsQueue.flush();