@salla.sa/embedded-sdk 0.1.0-beta.3 → 0.1.0-beta.4

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/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
- const $ = "0.1.0-beta.3", k = {
1
+ const $ = "0.1.0-beta.4", k = {
2
2
  version: $
3
3
  }, s = "embedded::", g = {
4
4
  /** Initialize handshake - iframe signals it's ready to receive context */
@@ -35,8 +35,6 @@ const $ = "0.1.0-beta.3", k = {
35
35
  }, u = {
36
36
  /** Set loading state */
37
37
  LOADING: `${s}ui.loading`,
38
- /** Control overlay state */
39
- OVERLAY: `${s}ui.overlay`,
40
38
  /** Show toast notification */
41
39
  TOAST: `${s}ui.toast`,
42
40
  /** Control modal state */
@@ -50,26 +48,26 @@ const $ = "0.1.0-beta.3", k = {
50
48
  }, p = {
51
49
  /** Initialize checkout flow */
52
50
  CREATE: `${s}checkout.create`
53
- }, T = k.version, x = 1e4, V = [
51
+ }, y = k.version, x = 1e4, z = [
54
52
  "localhost",
55
53
  "merchants.workers.dev",
56
54
  "s.salla.sa",
57
55
  ".salla.group",
58
56
  ".salla.sa"
59
57
  ];
60
- function z(e) {
58
+ function K(e) {
61
59
  try {
62
60
  const n = new URL(e).hostname;
63
- return V.some((i) => i.startsWith(".") ? n.endsWith(i) || n === i.slice(1) : n === i || n.startsWith(`${i}:`));
61
+ return z.some((i) => i.startsWith(".") ? n.endsWith(i) || n === i.slice(1) : n === i || n.startsWith(`${i}:`));
64
62
  } catch {
65
63
  return !1;
66
64
  }
67
65
  }
68
- function K() {
66
+ function V() {
69
67
  return typeof window > "u" || window.parent === window ? null : window.parent;
70
68
  }
71
- function a(e, t, n = "*") {
72
- const i = K();
69
+ function o(e, t, n = "*") {
70
+ const i = V();
73
71
  if (!i) {
74
72
  console.warn("[EmbeddedSDK] Not running in an iframe, cannot post to host");
75
73
  return;
@@ -81,9 +79,9 @@ function a(e, t, n = "*") {
81
79
  i.postMessage(r, n);
82
80
  }
83
81
  const d = /* @__PURE__ */ new Map();
84
- let y = !1;
85
- function N(e) {
86
- if (process.env.NODE_ENV === "production" && !z(e.origin))
82
+ let T = !1;
83
+ function L(e) {
84
+ if (process.env.NODE_ENV === "production" && !K(e.origin))
87
85
  return;
88
86
  const t = e.data;
89
87
  if (!t || typeof t.event != "string")
@@ -106,7 +104,7 @@ function N(e) {
106
104
  });
107
105
  }
108
106
  function U() {
109
- y || typeof window > "u" || (window.addEventListener("message", N), y = !0);
107
+ T || typeof window > "u" || (window.addEventListener("message", L), T = !0);
110
108
  }
111
109
  function E(e, t) {
112
110
  U(), d.has(e) || d.set(e, /* @__PURE__ */ new Set());
@@ -119,13 +117,13 @@ function F(e, t = x) {
119
117
  return new Promise((n, i) => {
120
118
  const r = setTimeout(() => {
121
119
  l(), i(new Error(`[EmbeddedSDK] Timeout waiting for "${e}" message`));
122
- }, t), l = E(e, (o) => {
123
- clearTimeout(r), l(), n(o);
120
+ }, t), l = E(e, (a) => {
121
+ clearTimeout(r), l(), n(a);
124
122
  });
125
123
  });
126
124
  }
127
125
  function q() {
128
- d.clear(), y && typeof window < "u" && (window.removeEventListener("message", N), y = !1);
126
+ d.clear(), T && typeof window < "u" && (window.removeEventListener("message", L), T = !1);
129
127
  }
130
128
  function P() {
131
129
  return typeof window > "u" ? !1 : window.parent !== window;
@@ -138,7 +136,7 @@ function H() {
138
136
  function j(e, t = {}, n = G) {
139
137
  const i = H();
140
138
  return new Promise((r, l) => {
141
- const o = setTimeout(() => {
139
+ const a = setTimeout(() => {
142
140
  f.get(i) && (f.delete(i), l(
143
141
  new Error(
144
142
  `[EmbeddedSDK] Request "${e}" timed out after ${n}ms`
@@ -148,9 +146,9 @@ function j(e, t = {}, n = G) {
148
146
  f.set(i, {
149
147
  resolve: r,
150
148
  reject: l,
151
- timeout: o,
149
+ timeout: a,
152
150
  event: e
153
- }), a(e, { ...t, requestId: i });
151
+ }), o(e, { ...t, requestId: i });
154
152
  });
155
153
  }
156
154
  function S(e, t, n) {
@@ -163,14 +161,14 @@ function S(e, t, n) {
163
161
  }
164
162
  clearTimeout(i.timeout), f.delete(e), n ? i.reject(new Error(n)) : i.resolve(t);
165
163
  }
166
- function Y(e = "SDK cleanup") {
164
+ function W(e = "SDK cleanup") {
167
165
  f.forEach((t, n) => {
168
166
  clearTimeout(t.timeout), t.reject(
169
167
  new Error(`[EmbeddedSDK] Request ${n} cancelled: ${e}`)
170
168
  );
171
169
  }), f.clear();
172
170
  }
173
- function W(e) {
171
+ function Y(e) {
174
172
  return {
175
173
  /**
176
174
  * Get the token from the URL query parameter.
@@ -198,7 +196,7 @@ function W(e) {
198
196
  * ```
199
197
  */
200
198
  refresh() {
201
- a(M.REFRESH, {});
199
+ o(M.REFRESH, {});
202
200
  }
203
201
  };
204
202
  }
@@ -245,9 +243,7 @@ function J(e) {
245
243
  `Extended action at index ${i} is missing required "title" property`
246
244
  ), r.subTitle !== void 0 && typeof r.subTitle != "string" && t.push(
247
245
  `Extended action at index ${i} subTitle must be a string`
248
- ), r.url !== void 0 && typeof r.url != "string" && t.push(
249
- `Extended action at index ${i} url must be a string`
250
- ), r.value !== void 0 && typeof r.value != "string" && t.push(
246
+ ), r.url !== void 0 && typeof r.url != "string" && t.push(`Extended action at index ${i} url must be a string`), r.value !== void 0 && typeof r.value != "string" && t.push(
251
247
  `Extended action at index ${i} value must be a string`
252
248
  ), r.icon !== void 0 && typeof r.icon != "string" && t.push(
253
249
  `Extended action at index ${i} icon must be a string`
@@ -281,7 +277,7 @@ function te() {
281
277
  c(h.NAVIGATE, n.errors);
282
278
  return;
283
279
  }
284
- a(h.NAVIGATE, {
280
+ o(h.NAVIGATE, {
285
281
  path: e,
286
282
  state: t == null ? void 0 : t.state,
287
283
  replace: t == null ? void 0 : t.replace
@@ -296,7 +292,7 @@ function te() {
296
292
  c(h.REDIRECT, t.errors);
297
293
  return;
298
294
  }
299
- a(h.REDIRECT, { url: e });
295
+ o(h.REDIRECT, { url: e });
300
296
  },
301
297
  /**
302
298
  * Navigate to a path - auto-detects internal vs external.
@@ -318,7 +314,7 @@ function te() {
318
314
  ]);
319
315
  return;
320
316
  }
321
- a(g.RESIZE, { height: e });
317
+ o(g.RESIZE, { height: e });
322
318
  },
323
319
  /**
324
320
  * Auto-resize iframe to content height.
@@ -337,7 +333,7 @@ function te() {
337
333
  ]);
338
334
  return;
339
335
  }
340
- a(h.SET_TITLE, { title: e });
336
+ o(h.SET_TITLE, { title: e });
341
337
  }
342
338
  };
343
339
  }
@@ -369,20 +365,20 @@ function ie() {
369
365
  c(b.SET_ACTION, r.errors);
370
366
  return;
371
367
  }
372
- i.onClick ? t = i.onClick : t = null, a(b.SET_ACTION, {
368
+ i.onClick ? t = i.onClick : t = null, o(b.SET_ACTION, {
373
369
  title: i.title,
374
370
  onClick: i.onClick ? !0 : void 0,
375
371
  value: i.value,
376
372
  subTitle: i.subTitle,
377
373
  icon: i.icon,
378
374
  disabled: i.disabled,
379
- extendedActions: (l = i.extendedActions) == null ? void 0 : l.map((o) => ({
380
- title: o.title,
381
- subTitle: o.subTitle,
382
- url: o.url,
383
- value: o.value,
384
- icon: o.icon,
385
- disabled: o.disabled
375
+ extendedActions: (l = i.extendedActions) == null ? void 0 : l.map((a) => ({
376
+ title: a.title,
377
+ subTitle: a.subTitle,
378
+ url: a.url,
379
+ value: a.value,
380
+ icon: a.icon,
381
+ disabled: a.disabled
386
382
  }))
387
383
  });
388
384
  },
@@ -390,7 +386,7 @@ function ie() {
390
386
  * Clear the primary action button.
391
387
  */
392
388
  clearAction() {
393
- t = null, a(b.SET_ACTION, {
389
+ t = null, o(b.SET_ACTION, {
394
390
  title: ""
395
391
  });
396
392
  },
@@ -418,40 +414,24 @@ function ne() {
418
414
  * @param mode - Display mode ('full' for full page, 'component' for inline)
419
415
  */
420
416
  show(e = "full") {
421
- a(u.LOADING, { status: !1, mode: e });
417
+ o(u.LOADING, { status: !1, mode: e });
422
418
  },
423
419
  /**
424
420
  * Hide loading indicator.
425
421
  */
426
422
  hide() {
427
- a(u.LOADING, { status: !0, mode: "full" });
423
+ o(u.LOADING, { status: !0, mode: "full" });
428
424
  }
429
425
  };
430
426
  }
431
427
  function re() {
432
- return {
433
- /**
434
- * Open overlay mode.
435
- */
436
- open() {
437
- a(u.OVERLAY, { action: "open" });
438
- },
439
- /**
440
- * Close overlay mode.
441
- */
442
- close() {
443
- a(u.OVERLAY, { action: "close" });
444
- }
445
- };
446
- }
447
- function se() {
448
428
  const e = (t) => {
449
429
  const n = Z(t);
450
430
  if (!n.valid) {
451
431
  c(u.TOAST, n.errors);
452
432
  return;
453
433
  }
454
- a(u.TOAST, {
434
+ o(u.TOAST, {
455
435
  type: t.type,
456
436
  message: t.message,
457
437
  duration: t.duration
@@ -488,13 +468,13 @@ function se() {
488
468
  }
489
469
  };
490
470
  }
491
- function ae() {
471
+ function se() {
492
472
  return {
493
473
  /**
494
474
  * Open a modal.
495
475
  */
496
476
  open(e, t) {
497
- a(u.MODAL, {
477
+ o(u.MODAL, {
498
478
  action: "open",
499
479
  id: e,
500
480
  content: t
@@ -504,14 +484,14 @@ function ae() {
504
484
  * Close a modal.
505
485
  */
506
486
  close(e) {
507
- a(u.MODAL, {
487
+ o(u.MODAL, {
508
488
  action: "close",
509
489
  id: e
510
490
  });
511
491
  }
512
492
  };
513
493
  }
514
- function oe() {
494
+ function ae() {
515
495
  return async (e) => {
516
496
  const t = ee(e);
517
497
  return t.valid ? j(u.CONFIRM, {
@@ -523,16 +503,15 @@ function oe() {
523
503
  }) : (c(u.CONFIRM, t.errors), Promise.reject(new Error(t.errors.join(", "))));
524
504
  };
525
505
  }
526
- function le() {
506
+ function oe() {
527
507
  return {
528
508
  loading: ne(),
529
- overlay: re(),
530
- toast: se(),
531
- modal: ae(),
532
- confirm: oe()
509
+ toast: re(),
510
+ modal: se(),
511
+ confirm: ae()
533
512
  };
534
513
  }
535
- function ue() {
514
+ function le() {
536
515
  return {
537
516
  /**
538
517
  * Create/initiate a checkout.
@@ -543,26 +522,26 @@ function ue() {
543
522
  c(p.CREATE, t.errors);
544
523
  return;
545
524
  }
546
- a(p.CREATE, { payload: e });
525
+ o(p.CREATE, { payload: e });
547
526
  }
548
527
  };
549
528
  }
550
529
  const R = {
551
530
  debug: !1,
552
531
  initialized: !1
553
- }, ce = {
532
+ }, ue = {
554
533
  theme: "light",
555
534
  width: 0,
556
535
  locale: "ar",
557
536
  currency: "SAR"
558
- }, L = {
537
+ }, I = {
559
538
  ready: !1,
560
539
  initializing: !1,
561
- layout: { ...ce }
540
+ layout: { ...ue }
562
541
  };
563
- class de {
542
+ class ce {
564
543
  constructor() {
565
- this.config = { ...R }, this.state = { ...L }, this.themeCallbacks = /* @__PURE__ */ new Set(), this.initCallbacks = /* @__PURE__ */ new Set(), this.appReady = !1, this.auth = W(), this.page = te(), this.nav = ie(), this.ui = le(), this.checkout = ue(), this.setupThemeListener(), this.setupResponseListeners();
544
+ this.config = { ...R }, this.state = { ...I }, this.themeCallbacks = /* @__PURE__ */ new Set(), this.initCallbacks = /* @__PURE__ */ new Set(), this.appReady = !1, this.auth = Y(), this.page = te(), this.nav = ie(), this.ui = oe(), this.checkout = le(), this.setupThemeListener(), this.setupResponseListeners();
566
545
  }
567
546
  /**
568
547
  * Get current SDK state (layout info only, no token).
@@ -590,13 +569,13 @@ class de {
590
569
  * Log debug messages if debug mode is enabled.
591
570
  */
592
571
  debugLog(...t) {
593
- this.config.debug && console.log(`[EmbeddedSDK v${T}]`, ...t);
572
+ this.config.debug && console.log(`[EmbeddedSDK v${y}]`, ...t);
594
573
  }
595
574
  /**
596
575
  * Log warnings.
597
576
  */
598
577
  warn(...t) {
599
- console.warn(`[EmbeddedSDK v${T}]`, ...t);
578
+ console.warn(`[EmbeddedSDK v${y}]`, ...t);
600
579
  }
601
580
  /**
602
581
  * Set up listener for theme changes from host.
@@ -677,7 +656,7 @@ class de {
677
656
  * ```
678
657
  */
679
658
  log(t, n, i) {
680
- a(_.LOG, {
659
+ o(_.LOG, {
681
660
  level: t,
682
661
  message: n,
683
662
  context: i
@@ -702,7 +681,7 @@ class de {
702
681
  this.warn("Cannot signal ready before init() is called");
703
682
  return;
704
683
  }
705
- this.appReady = !0, a(g.READY, {}), this.debugLog("Sent ready signal to host");
684
+ this.appReady = !0, o(g.READY, {}), this.debugLog("Sent ready signal to host");
706
685
  }
707
686
  /**
708
687
  * Initialize the SDK and establish connection with the host.
@@ -728,32 +707,32 @@ class de {
728
707
  initialized: !1
729
708
  }, this.state.initializing = !0, this.debugLog("Initializing SDK...");
730
709
  try {
731
- a(g.INIT, {
710
+ o(g.INIT, {
732
711
  height: document.documentElement.scrollHeight
733
712
  }), this.debugLog("Sent iframe.ready message, waiting for context...");
734
- const o = await F(
713
+ const a = await F(
735
714
  w.PROVIDE
736
715
  );
737
- this.debugLog("Received context from host:", o), this.state = {
716
+ this.debugLog("Received context from host:", a), this.state = {
738
717
  ready: !0,
739
718
  initializing: !1,
740
719
  layout: {
741
- theme: ((n = o.layout) == null ? void 0 : n.theme) ?? "light",
742
- width: ((i = o.layout) == null ? void 0 : i.width) ?? 0,
743
- locale: ((r = o.layout) == null ? void 0 : r.locale) ?? "ar",
744
- currency: ((l = o.layout) == null ? void 0 : l.currency) ?? "SAR"
720
+ theme: ((n = a.layout) == null ? void 0 : n.theme) ?? "light",
721
+ width: ((i = a.layout) == null ? void 0 : i.width) ?? 0,
722
+ locale: ((r = a.layout) == null ? void 0 : r.locale) ?? "ar",
723
+ currency: ((l = a.layout) == null ? void 0 : l.currency) ?? "SAR"
745
724
  }
746
725
  }, this.config.initialized = !0, this.debugLog("Initialization complete. Layout:", this.state.layout);
747
726
  const v = this.getState();
748
- return this.initCallbacks.forEach((O) => {
727
+ return this.initCallbacks.forEach((D) => {
749
728
  try {
750
- O(v);
751
- } catch (D) {
752
- console.error("[EmbeddedSDK] Error in init callback:", D);
729
+ D(v);
730
+ } catch (O) {
731
+ console.error("[EmbeddedSDK] Error in init callback:", O);
753
732
  }
754
733
  }), { layout: { ...this.state.layout } };
755
- } catch (o) {
756
- throw this.state.initializing = !1, this.state.ready = !1, o;
734
+ } catch (a) {
735
+ throw this.state.initializing = !1, this.state.ready = !1, a;
757
736
  }
758
737
  }
759
738
  /**
@@ -779,23 +758,23 @@ class de {
779
758
  * ```
780
759
  */
781
760
  destroy() {
782
- this.debugLog("Destroying SDK instance"), this.config.initialized && (a(g.DESTROY, {}), this.debugLog("Sent destroy event to host")), Y("SDK destroyed"), q(), this.themeCallbacks.clear(), this.initCallbacks.clear(), this.config = { ...R }, this.state = { ...L }, this.appReady = !1;
761
+ this.debugLog("Destroying SDK instance"), this.config.initialized && (o(g.DESTROY, {}), this.debugLog("Sent destroy event to host")), W("SDK destroyed"), q(), this.themeCallbacks.clear(), this.initCallbacks.clear(), this.config = { ...R }, this.state = { ...I }, this.appReady = !1;
783
762
  }
784
763
  }
785
764
  let m = null;
786
- function fe() {
787
- return m || (m = new de()), m;
765
+ function de() {
766
+ return m || (m = new ce()), m;
788
767
  }
789
- function he() {
768
+ function fe() {
790
769
  m && (m.destroy(), m = null);
791
770
  }
792
- const I = fe(), me = T;
793
- typeof window < "u" && (window.salla = window.salla || window.Salla || {}, window.Salla = window.salla, window.salla.embedded || (window.salla.embedded = I), window.Salla.embedded || (window.Salla.embedded = I));
771
+ const N = de(), he = y;
772
+ typeof window < "u" && (window.salla = window.salla || window.Salla || {}, window.Salla = window.salla, window.salla.embedded || (window.salla.embedded = N), window.Salla.embedded || (window.Salla.embedded = N));
794
773
  export {
795
- de as EmbeddedApp,
796
- I as embedded,
797
- fe as getEmbeddedApp,
798
- he as resetEmbeddedApp,
799
- me as version
774
+ ce as EmbeddedApp,
775
+ N as embedded,
776
+ de as getEmbeddedApp,
777
+ fe as resetEmbeddedApp,
778
+ he as version
800
779
  };
801
780
  //# sourceMappingURL=index.js.map