@salla.sa/embedded-sdk 0.1.0 → 0.2.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/esm/index.js CHANGED
@@ -21,9 +21,7 @@ const s = "embedded::", T = {
21
21
  CONFIRM: `${s}ui.confirm`,
22
22
  /** Confirm dialog response (from host) */
23
23
  CONFIRM_RESPONSE: `${s}ui.confirm.response`
24
- }, P = "0.1.0-beta.12", j = {
25
- version: P
26
- }, V = {
24
+ }, P = {}, V = {
27
25
  /** Request token refresh (re-renders iframe with new token) */
28
26
  REFRESH: `${s}auth.refresh`
29
27
  }, m = {
@@ -43,7 +41,7 @@ const s = "embedded::", T = {
43
41
  }, k = {
44
42
  /** Initialize checkout flow */
45
43
  CREATE: `${s}checkout.create`
46
- }, L = j.version, H = 1e4, K = [
44
+ }, L = P.version, j = 1e4, H = [
47
45
  "localhost",
48
46
  "merchants.workers.dev",
49
47
  "s.salla.sa",
@@ -54,7 +52,7 @@ let w = {
54
52
  showVersion: !0,
55
53
  debug: !1
56
54
  };
57
- function G(e) {
55
+ function K(e) {
58
56
  w = { ...w, ...e };
59
57
  }
60
58
  function _(e) {
@@ -88,19 +86,19 @@ const u = {
88
86
  b("debug", ...e);
89
87
  }
90
88
  };
91
- function X(e) {
89
+ function G(e) {
92
90
  try {
93
91
  const r = new URL(e).hostname;
94
- return K.some((i) => i.startsWith(".") ? r.endsWith(i) || r === i.slice(1) : r === i || r.startsWith(`${i}:`));
92
+ return H.some((i) => i.startsWith(".") ? r.endsWith(i) || r === i.slice(1) : r === i || r.startsWith(`${i}:`));
95
93
  } catch {
96
94
  return !1;
97
95
  }
98
96
  }
99
- function W() {
97
+ function X() {
100
98
  return typeof window > "u" || window.parent === window ? null : window.parent;
101
99
  }
102
100
  function o(e, t, r = "*", i, n) {
103
- const a = W();
101
+ const a = X();
104
102
  if (!a) {
105
103
  u.warn("Not running in an iframe, cannot post to host");
106
104
  return;
@@ -120,7 +118,7 @@ function o(e, t, r = "*", i, n) {
120
118
  const f = /* @__PURE__ */ new Map();
121
119
  let A = !1;
122
120
  function z(e) {
123
- if (process.env.NODE_ENV === "production" && !X(e.origin))
121
+ if (process.env.NODE_ENV === "production" && !G(e.origin))
124
122
  return;
125
123
  const t = e.data;
126
124
  if (!t || typeof t.event != "string" || !t.payload || typeof t.timestamp != "number" || !t.source) {
@@ -144,17 +142,17 @@ function z(e) {
144
142
  }
145
143
  });
146
144
  }
147
- function Y() {
145
+ function W() {
148
146
  A || typeof window > "u" || (window.addEventListener("message", z), A = !0);
149
147
  }
150
148
  function R(e, t) {
151
- Y(), f.has(e) || f.set(e, /* @__PURE__ */ new Set());
149
+ W(), f.has(e) || f.set(e, /* @__PURE__ */ new Set());
152
150
  const r = f.get(e);
153
151
  return r.add(t), () => {
154
152
  r.delete(t), r.size === 0 && f.delete(e);
155
153
  };
156
154
  }
157
- function B(e, t = H) {
155
+ function Y(e, t = j) {
158
156
  return new Promise((r, i) => {
159
157
  const n = setTimeout(() => {
160
158
  a(), i(new Error(`[EmbeddedSDK] Timeout waiting for "${e}" message`));
@@ -163,19 +161,19 @@ function B(e, t = H) {
163
161
  });
164
162
  });
165
163
  }
166
- function Z() {
164
+ function B() {
167
165
  f.clear(), A && typeof window < "u" && (window.removeEventListener("message", z), A = !1);
168
166
  }
169
- function J() {
167
+ function Z() {
170
168
  return typeof window > "u" ? !1 : window.parent !== window;
171
169
  }
172
- const h = /* @__PURE__ */ new Map(), Q = 3e4;
173
- function ee() {
170
+ const h = /* @__PURE__ */ new Map(), J = 3e4;
171
+ function Q() {
174
172
  const e = Date.now(), t = Math.random().toString(36).slice(2, 9);
175
173
  return `req_${e}_${t}`;
176
174
  }
177
- function te(e, t = {}, r = Q) {
178
- const i = ee();
175
+ function ee(e, t = {}, r = J) {
176
+ const i = Q();
179
177
  return new Promise((n, a) => {
180
178
  const l = setTimeout(() => {
181
179
  h.get(i) && (h.delete(i), a(
@@ -192,7 +190,7 @@ function te(e, t = {}, r = Q) {
192
190
  }), o(e, t, "*", i);
193
191
  });
194
192
  }
195
- function re(e, t, r) {
193
+ function te(e, t, r) {
196
194
  const i = h.get(e);
197
195
  if (!i) {
198
196
  u.warn(`Received response for unknown request: ${e}`);
@@ -200,21 +198,21 @@ function re(e, t, r) {
200
198
  }
201
199
  clearTimeout(i.timeout), h.delete(e), i.resolve(t);
202
200
  }
203
- function ie(e = "SDK cleanup") {
201
+ function re(e = "SDK cleanup") {
204
202
  h.forEach((t, r) => {
205
203
  clearTimeout(t.timeout), t.reject(
206
204
  new Error(`[EmbeddedSDK] Request ${r} cancelled: ${e}`)
207
205
  );
208
206
  }), h.clear();
209
207
  }
210
- const ne = "https://api.salla.dev";
208
+ const ie = "https://api.salla.dev";
211
209
  class E extends Error {
212
210
  constructor(t, r, i) {
213
211
  super(t), this.status = r, this.response = i, this.name = "ApiError";
214
212
  }
215
213
  }
216
- async function ae(e, t = {}) {
217
- const { method: r = "GET", headers: i = {}, body: n, timeout: a = 3e4 } = t, l = `${ne}${e}`, S = new AbortController(), N = setTimeout(() => {
214
+ async function ne(e, t = {}) {
215
+ const { method: r = "GET", headers: i = {}, body: n, timeout: a = 3e4 } = t, l = `${ie}${e}`, S = new AbortController(), N = setTimeout(() => {
218
216
  S.abort();
219
217
  }, a);
220
218
  try {
@@ -249,7 +247,7 @@ function C(e) {
249
247
  data: null
250
248
  };
251
249
  }
252
- async function se(e) {
250
+ async function ae(e) {
253
251
  const { token: t, appId: r, refreshOnError: i = !0 } = e;
254
252
  if (!t) {
255
253
  const n = "Token is required. Provide it as a parameter or in URL as ?token=XXX";
@@ -260,7 +258,7 @@ async function se(e) {
260
258
  return u.error("Error in introspect:", n), C(n);
261
259
  }
262
260
  try {
263
- const n = await ae(
261
+ const n = await ne(
264
262
  "/exchange-authority/v1/introspect",
265
263
  {
266
264
  method: "POST",
@@ -288,7 +286,7 @@ async function se(e) {
288
286
  return C(a);
289
287
  }
290
288
  }
291
- function oe(e) {
289
+ function se(e) {
292
290
  return {
293
291
  /**
294
292
  * Get the token from the URL query parameter.
@@ -334,7 +332,7 @@ function oe(e) {
334
332
  */
335
333
  async introspect(t = {}) {
336
334
  const r = t.token ?? this.getToken() ?? "", i = t.appId ?? this.getAppId() ?? "";
337
- return se({
335
+ return ae({
338
336
  token: r,
339
337
  appId: i,
340
338
  refreshOnError: t.refreshOnError
@@ -343,21 +341,21 @@ function oe(e) {
343
341
  };
344
342
  }
345
343
  const x = ["success", "error", "warning", "info"];
346
- function ue(e) {
344
+ function oe(e) {
347
345
  const t = [];
348
346
  return e.type === void 0 || e.type === null ? t.push("Toast type is required") : (typeof e.type != "string" || !x.includes(e.type)) && t.push(
349
347
  `Invalid toast type "${e.type}". Expected: ${x.join(" | ")}`
350
348
  ), e.message === void 0 || e.message === null ? t.push("Toast message is required") : typeof e.message != "string" ? t.push("Toast message must be a string") : e.message.trim() === "" && t.push("Toast message cannot be empty"), e.duration !== void 0 && e.duration !== null && (typeof e.duration != "number" ? t.push("Toast duration must be a number") : e.duration < 0 && t.push("Toast duration cannot be negative")), { valid: t.length === 0, errors: t };
351
349
  }
352
- function ce(e) {
350
+ function ue(e) {
353
351
  const t = [];
354
352
  return typeof e != "object" || e === null ? (t.push("Checkout payload must be an object"), { valid: !1, errors: t }) : (e.amount !== void 0 && e.amount !== null && (typeof e.amount != "number" ? t.push("Checkout amount must be a number") : e.amount < 0 && t.push("Checkout amount cannot be negative")), e.currency !== void 0 && e.currency !== null && (typeof e.currency != "string" ? t.push("Checkout currency must be a string") : e.currency.trim() === "" && t.push("Checkout currency cannot be empty")), e.items !== void 0 && e.items !== null && (Array.isArray(e.items) || t.push("Checkout items must be an array")), { valid: t.length === 0, errors: t });
355
353
  }
356
- function le(e) {
354
+ function ce(e) {
357
355
  const t = [];
358
356
  return e.path === void 0 || e.path === null ? t.push("Navigation path is required") : typeof e.path != "string" ? t.push("Navigation path must be a string") : e.path.trim() === "" && t.push("Navigation path cannot be empty"), e.replace !== void 0 && typeof e.replace != "boolean" && t.push("Navigation replace option must be a boolean"), { valid: t.length === 0, errors: t };
359
357
  }
360
- function de(e) {
358
+ function le(e) {
361
359
  const t = [];
362
360
  if (e.url === void 0 || e.url === null)
363
361
  t.push("Redirect URL is required");
@@ -373,7 +371,7 @@ function de(e) {
373
371
  }
374
372
  return { valid: t.length === 0, errors: t };
375
373
  }
376
- function fe(e) {
374
+ function de(e) {
377
375
  const t = [];
378
376
  return e.title ? typeof e.title != "string" && t.push("Nav action title must be a string") : t.push("Nav action title is required"), e.value ? typeof e.value != "string" && t.push("Nav action value must be a string") : t.push("Nav action value is required"), e.subTitle !== void 0 && e.subTitle !== null && typeof e.subTitle != "string" && t.push("Nav action subTitle must be a string"), e.icon !== void 0 && e.icon !== null && typeof e.icon != "string" && t.push("Nav action icon must be a string"), e.disabled !== void 0 && e.disabled !== null && typeof e.disabled != "boolean" && t.push("Nav action disabled must be a boolean"), e.extendedActions !== void 0 && e.extendedActions !== null && (Array.isArray(e.extendedActions) ? e.extendedActions.forEach((r, i) => {
379
377
  if (typeof r != "object" || r === null) {
@@ -395,7 +393,7 @@ function fe(e) {
395
393
  }) : t.push("Nav action extendedActions must be an array")), { valid: t.length === 0, errors: t };
396
394
  }
397
395
  const D = ["danger", "warning", "info"];
398
- function he(e) {
396
+ function fe(e) {
399
397
  const t = [];
400
398
  return e.title === void 0 || e.title === null ? t.push("Confirm dialog title is required") : typeof e.title != "string" ? t.push("Confirm dialog title must be a string") : e.title.trim() === "" && t.push("Confirm dialog title cannot be empty"), e.message === void 0 || e.message === null ? t.push("Confirm dialog message is required") : typeof e.message != "string" ? t.push("Confirm dialog message must be a string") : e.message.trim() === "" && t.push("Confirm dialog message cannot be empty"), e.confirmText !== void 0 && e.confirmText !== null && typeof e.confirmText != "string" && t.push("Confirm dialog confirmText must be a string"), e.cancelText !== void 0 && e.cancelText !== null && typeof e.cancelText != "string" && t.push("Confirm dialog cancelText must be a string"), e.variant !== void 0 && e.variant !== null && (typeof e.variant != "string" || !D.includes(e.variant)) && t.push(
401
399
  `Invalid confirm variant "${e.variant}". Expected: ${D.join(" | ")}`
@@ -408,13 +406,13 @@ function d(e, t) {
408
406
  `)
409
407
  );
410
408
  }
411
- function ge() {
409
+ function he() {
412
410
  return {
413
411
  /**
414
412
  * Navigate to a path using React Router (SPA navigation).
415
413
  */
416
414
  navigate(e, t) {
417
- const r = le({ path: e, ...t });
415
+ const r = ce({ path: e, ...t });
418
416
  if (!r.valid) {
419
417
  d(m.NAVIGATE, r.errors);
420
418
  return;
@@ -429,7 +427,7 @@ function ge() {
429
427
  * Redirect to a URL (full page reload).
430
428
  */
431
429
  redirect(e) {
432
- const t = de({ url: e });
430
+ const t = le({ url: e });
433
431
  if (!t.valid) {
434
432
  d(m.REDIRECT, t.errors);
435
433
  return;
@@ -479,7 +477,7 @@ function ge() {
479
477
  }
480
478
  };
481
479
  }
482
- function me() {
480
+ function ge() {
483
481
  const e = /* @__PURE__ */ new Set();
484
482
  return R(v.ACTION_CLICK, (r) => {
485
483
  e.forEach((i) => {
@@ -495,7 +493,7 @@ function me() {
495
493
  */
496
494
  setAction(r) {
497
495
  var n;
498
- const i = fe(r);
496
+ const i = de(r);
499
497
  if (!i.valid) {
500
498
  d(v.SET_ACTION, i.errors);
501
499
  return;
@@ -531,7 +529,7 @@ function me() {
531
529
  }
532
530
  };
533
531
  }
534
- function pe() {
532
+ function me() {
535
533
  return {
536
534
  /**
537
535
  * Show loading indicator.
@@ -547,9 +545,9 @@ function pe() {
547
545
  }
548
546
  };
549
547
  }
550
- function be() {
548
+ function pe() {
551
549
  const e = (t) => {
552
- const r = ue(t);
550
+ const r = oe(t);
553
551
  if (!r.valid) {
554
552
  d(g.TOAST, r.errors);
555
553
  return;
@@ -591,10 +589,10 @@ function be() {
591
589
  }
592
590
  };
593
591
  }
594
- function Ee() {
592
+ function be() {
595
593
  return async (e) => {
596
- const t = he(e);
597
- return t.valid ? te(g.CONFIRM, {
594
+ const t = fe(e);
595
+ return t.valid ? ee(g.CONFIRM, {
598
596
  title: e.title,
599
597
  message: e.message,
600
598
  confirmText: e.confirmText ?? "Confirm",
@@ -603,20 +601,20 @@ function Ee() {
603
601
  }) : (d(g.CONFIRM, t.errors), Promise.reject(new Error(t.errors.join(", "))));
604
602
  };
605
603
  }
606
- function Te() {
604
+ function Ee() {
607
605
  return {
608
- loading: pe(),
609
- toast: be(),
610
- confirm: Ee()
606
+ loading: me(),
607
+ toast: pe(),
608
+ confirm: be()
611
609
  };
612
610
  }
613
- function ye() {
611
+ function Te() {
614
612
  return {
615
613
  /**
616
614
  * Create/initiate a checkout.
617
615
  */
618
616
  create(e) {
619
- const t = ce(e);
617
+ const t = ue(e);
620
618
  if (!t.valid) {
621
619
  d(k.CREATE, t.errors);
622
620
  return;
@@ -628,7 +626,7 @@ function ye() {
628
626
  const M = {
629
627
  debug: !1,
630
628
  initialized: !1
631
- }, ve = {
629
+ }, ye = {
632
630
  theme: "light",
633
631
  width: 0,
634
632
  locale: "ar",
@@ -636,11 +634,11 @@ const M = {
636
634
  }, U = {
637
635
  ready: !1,
638
636
  initializing: !1,
639
- layout: { ...ve }
637
+ layout: { ...ye }
640
638
  };
641
- class we {
639
+ class ve {
642
640
  constructor() {
643
- this.config = { ...M }, this.state = { ...U }, this.themeCallbacks = /* @__PURE__ */ new Set(), this.initCallbacks = /* @__PURE__ */ new Set(), this.appReady = !1, this.auth = oe(), this.page = ge(), this.nav = me(), this.ui = Te(), this.checkout = ye(), this.setupThemeListener(), this.setupResponseListeners();
641
+ this.config = { ...M }, this.state = { ...U }, this.themeCallbacks = /* @__PURE__ */ new Set(), this.initCallbacks = /* @__PURE__ */ new Set(), this.appReady = !1, this.auth = se(), this.page = he(), this.nav = ge(), this.ui = Ee(), this.checkout = Te(), this.setupThemeListener(), this.setupResponseListeners();
644
642
  }
645
643
  /**
646
644
  * Get current SDK state (layout info only, no token).
@@ -708,7 +706,7 @@ class we {
708
706
  */
709
707
  setupResponseListeners() {
710
708
  R(g.CONFIRM_RESPONSE, (t) => {
711
- this.internalLog("debug", "Received confirm response:", t), t.requestId && re(t.requestId, { confirmed: t.payload.confirmed });
709
+ this.internalLog("debug", "Received confirm response:", t), t.requestId && te(t.requestId, { confirmed: t.payload.confirmed });
712
710
  });
713
711
  }
714
712
  /**
@@ -795,13 +793,13 @@ class we {
795
793
  ), { layout: { ...this.state.layout } };
796
794
  if (this.state.initializing)
797
795
  return this.internalLog("warn", "Initialization already in progress"), this.waitForInit();
798
- J() || this.internalLog(
796
+ Z() || this.internalLog(
799
797
  "warn",
800
798
  "Not running in an iframe. Some features may not work."
801
799
  ), this.config = {
802
800
  debug: t.debug ?? !1,
803
801
  initialized: !1
804
- }, G({
802
+ }, K({
805
803
  debug: this.config.debug
806
804
  }), this.state.initializing = !0, this.internalLog("debug", "Initializing SDK...");
807
805
  try {
@@ -811,7 +809,7 @@ class we {
811
809
  "debug",
812
810
  "Sent iframe.ready message, waiting for context..."
813
811
  );
814
- const r = await B(
812
+ const r = await Y(
815
813
  $.PROVIDE
816
814
  );
817
815
  this.internalLog("debug", "Received context from host:", r);
@@ -864,23 +862,23 @@ class we {
864
862
  * ```
865
863
  */
866
864
  destroy() {
867
- this.internalLog("debug", "Destroying SDK instance"), this.config.initialized && (o(T.DESTROY, {}), this.internalLog("debug", "Sent destroy event to host")), ie("SDK destroyed"), Z(), this.themeCallbacks.clear(), this.initCallbacks.clear(), this.config = { ...M }, this.state = { ...U }, this.appReady = !1;
865
+ this.internalLog("debug", "Destroying SDK instance"), this.config.initialized && (o(T.DESTROY, {}), this.internalLog("debug", "Sent destroy event to host")), re("SDK destroyed"), B(), this.themeCallbacks.clear(), this.initCallbacks.clear(), this.config = { ...M }, this.state = { ...U }, this.appReady = !1;
868
866
  }
869
867
  }
870
868
  let p = null;
871
869
  function F() {
872
- return p || (p = new we()), p;
870
+ return p || (p = new ve()), p;
873
871
  }
874
- function Ae() {
872
+ function we() {
875
873
  p && (p.destroy(), p = null);
876
874
  }
877
- const q = F(), Re = L;
875
+ const q = F(), Ae = L;
878
876
  typeof window < "u" && (window.salla = window.salla || window.Salla || {}, window.Salla = window.salla, window.salla.embedded || (window.salla.embedded = q), window.Salla.embedded || (window.Salla.embedded = q));
879
877
  export {
880
- we as EmbeddedApp,
878
+ ve as EmbeddedApp,
881
879
  q as embedded,
882
880
  F as getEmbeddedApp,
883
- Ae as resetEmbeddedApp,
884
- Re as version
881
+ we as resetEmbeddedApp,
882
+ Ae as version
885
883
  };
886
884
  //# sourceMappingURL=index.js.map