@monetize.software/sdk 3.0.0-alpha.13 → 3.0.0-alpha.15

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/core.js CHANGED
@@ -20,24 +20,24 @@ class E {
20
20
  n.set("Accept", "application/json"), n.set("X-SDK-Version", m), n.set("X-Paywall-Id", this.opts.paywallId), this.opts.capabilities?.length && n.set("X-SDK-Capabilities", this.opts.capabilities.join(","));
21
21
  const o = await this.opts.getAuthToken?.();
22
22
  o && n.set("Authorization", `Bearer ${o}`);
23
- const u = typeof FormData < "u" && e.body instanceof FormData;
24
- e.body && !n.has("Content-Type") && !u && n.set("Content-Type", "application/json");
25
- let d;
23
+ const d = typeof FormData < "u" && e.body instanceof FormData;
24
+ e.body && !n.has("Content-Type") && !d && n.set("Content-Type", "application/json");
25
+ let f;
26
26
  try {
27
- d = await a(s, {
27
+ f = await a(s, {
28
28
  ...e,
29
29
  headers: n,
30
30
  credentials: "omit"
31
31
  });
32
- } catch (c) {
33
- throw (c && typeof c == "object" && "name" in c ? c.name : void 0) === "AbortError" ? new r("aborted", "Request aborted", { cause: c }) : new r("network_error", "Network request failed", { cause: c });
32
+ } catch (h) {
33
+ throw (h && typeof h == "object" && "name" in h ? h.name : void 0) === "AbortError" ? new r("aborted", "Request aborted", { cause: h }) : new r("network_error", "Network request failed", { cause: h });
34
34
  }
35
- const y = (d.headers.get("content-type") ?? "").includes("application/json") ? await d.json().catch(() => null) : null;
36
- if (!d.ok) {
37
- const c = y && typeof y == "object" && "code" in y && String(y.code) || `http_${d.status}`, g = y && typeof y == "object" && "message" in y && String(y.message) || d.statusText || "Request failed";
38
- throw new r(c, g, { status: d.status, cause: y });
35
+ const l = (f.headers.get("content-type") ?? "").includes("application/json") ? await f.json().catch(() => null) : null;
36
+ if (!f.ok) {
37
+ const h = l && typeof l == "object" && "code" in l && String(l.code) || `http_${f.status}`, g = l && typeof l == "object" && "message" in l && String(l.message) || f.statusText || "Request failed";
38
+ throw new r(h, g, { status: f.status, cause: l });
39
39
  }
40
- return y;
40
+ return l;
41
41
  }
42
42
  }
43
43
  class D {
@@ -63,37 +63,37 @@ class D {
63
63
  a.set("X-SDK-Version", m), a.set("X-Paywall-Id", this.paywallId), this.capabilities?.length && a.set("X-SDK-Capabilities", this.capabilities.join(","));
64
64
  const n = await this.auth?.getAccessToken();
65
65
  n ? a.set("Authorization", `Bearer ${n}`) : this.userId && a.set("X-User-ID", this.userId);
66
- const o = typeof FormData < "u" && t.body instanceof FormData, u = typeof Blob < "u" && t.body instanceof Blob, d = typeof ReadableStream < "u" && t.body instanceof ReadableStream, f = typeof t.body == "string";
67
- let h;
68
- t.body === void 0 || t.body === null ? h = void 0 : o || u || d || f ? h = t.body : (h = JSON.stringify(t.body), a.has("Content-Type") || a.set("Content-Type", "application/json"));
69
- const y = this.customFetch ?? fetch;
66
+ const o = typeof FormData < "u" && t.body instanceof FormData, d = typeof Blob < "u" && t.body instanceof Blob, f = typeof ReadableStream < "u" && t.body instanceof ReadableStream, p = typeof t.body == "string";
70
67
  let c;
68
+ t.body === void 0 || t.body === null ? c = void 0 : o || d || f || p ? c = t.body : (c = JSON.stringify(t.body), a.has("Content-Type") || a.set("Content-Type", "application/json"));
69
+ const l = this.customFetch ?? fetch;
70
+ let h;
71
71
  try {
72
- c = await y(s.toString(), {
72
+ h = await l(s.toString(), {
73
73
  method: t.method ?? "POST",
74
74
  headers: a,
75
- body: h,
75
+ body: c,
76
76
  signal: t.signal,
77
77
  credentials: "omit"
78
78
  });
79
- } catch (p) {
80
- const P = p instanceof Error ? p.message : String(p);
81
- throw new r("network_error", `Network request failed: ${P}`, { cause: p });
79
+ } catch (y) {
80
+ const P = y instanceof Error ? y.message : String(y);
81
+ throw new r("network_error", `Network request failed: ${P}`, { cause: y });
82
82
  }
83
- if (c.status === 402) {
84
- const p = await q(c);
85
- throw this.onQuotaExceeded?.(p), p;
83
+ if (h.status === 402) {
84
+ const y = await q(h);
85
+ throw this.onQuotaExceeded?.(y), y;
86
86
  }
87
- if (!c.ok) {
88
- const p = await $(c.clone());
87
+ if (!h.ok) {
88
+ const y = await $(h.clone());
89
89
  throw new r(
90
- p ?? `http_${c.status}`,
91
- c.statusText || "Gateway request failed",
92
- { status: c.status }
90
+ y ?? `http_${h.status}`,
91
+ h.statusText || "Gateway request failed",
92
+ { status: h.status }
93
93
  );
94
94
  }
95
- const g = c.headers.get("X-Query-Type") ?? void 0;
96
- return this.onChargeSuccess?.(g), c;
95
+ const g = h.headers.get("X-Query-Type") ?? void 0;
96
+ return this.onChargeSuccess?.(g), h;
97
97
  }
98
98
  }
99
99
  async function q(i) {
@@ -198,7 +198,7 @@ const M = {
198
198
  function L(i) {
199
199
  return i || (F() ? M : typeof window < "u" && "localStorage" in window ? x : J);
200
200
  }
201
- const l = {
201
+ const u = {
202
202
  visitorId: "pw-visitor-id",
203
203
  lastLoginMethod: (i) => `pw-${i}-last-login-method`,
204
204
  lastLoginEmail: (i) => `pw-${i}-last-login-email`,
@@ -242,13 +242,13 @@ function C() {
242
242
  }
243
243
  async function b(i) {
244
244
  try {
245
- const e = await i.getItem(l.visitorId);
245
+ const e = await i.getItem(u.visitorId);
246
246
  if (e && typeof e == "string" && e.length >= 16) return e;
247
247
  } catch {
248
248
  }
249
249
  const t = C();
250
250
  try {
251
- await i.setItem(l.visitorId, t);
251
+ await i.setItem(u.visitorId, t);
252
252
  } catch {
253
253
  }
254
254
  return t;
@@ -444,7 +444,7 @@ class yt {
444
444
  async hydrateBootstrapFromStorage() {
445
445
  if (!this.cachedBootstrap)
446
446
  try {
447
- const t = await this.storage.getItem(l.bootstrap(this.paywallId));
447
+ const t = await this.storage.getItem(u.bootstrap(this.paywallId));
448
448
  if (!t) return;
449
449
  const e = JSON.parse(t);
450
450
  if (!e?.bootstrap || Date.now() - e.at > _ || this.cachedBootstrap) return;
@@ -463,7 +463,7 @@ class yt {
463
463
  try {
464
464
  const { user: e, ...s } = t;
465
465
  await this.storage.setItem(
466
- l.bootstrap(this.paywallId),
466
+ u.bootstrap(this.paywallId),
467
467
  JSON.stringify({ at: Date.now(), bootstrap: s })
468
468
  );
469
469
  } catch {
@@ -474,7 +474,7 @@ class yt {
474
474
  // no-op, всё работает как раньше через сеть.
475
475
  subscribeBootstrapStorage() {
476
476
  typeof this.storage.watch == "function" && (this.bootstrapStorageUnwatch = this.storage.watch(
477
- l.bootstrap(this.paywallId),
477
+ u.bootstrap(this.paywallId),
478
478
  (t) => {
479
479
  if (t)
480
480
  try {
@@ -620,7 +620,7 @@ class yt {
620
620
  }
621
621
  }
622
622
  storageKey() {
623
- return l.userState(this.paywallId, B(this.identity));
623
+ return u.userState(this.paywallId, B(this.identity));
624
624
  }
625
625
  async hydrateUserFromStorage() {
626
626
  if (!this.cachedUser)
@@ -775,7 +775,7 @@ class yt {
775
775
  }
776
776
  }
777
777
  balancesStorageKey() {
778
- return l.balances(this.paywallId, B(this.identity));
778
+ return u.balances(this.paywallId, B(this.identity));
779
779
  }
780
780
  async hydrateBalancesFromStorage() {
781
781
  if (!this.cachedBalances)
@@ -832,7 +832,9 @@ class yt {
832
832
  "Idempotency-Key": t.idempotencyKey ?? C()
833
833
  };
834
834
  this.apiKey && (n["X-Api-Key"] = this.apiKey);
835
- const o = this.cachedBootstrap?.settings, u = t.successUrl ?? o?.success_redirect_url ?? void 0, d = t.shopUrl ?? o?.checkout_shop_url ?? void 0, f = this.api.request(`/api/v1/paywall/${this.paywallId}/start-checkout`, {
835
+ const o = this.cachedBootstrap?.settings, d = t.successUrl ?? o?.success_redirect_url ?? void 0, f = t.shopUrl ?? o?.checkout_shop_url ?? void 0, c = this.cachedBootstrap?.prices.find(
836
+ (h) => h.id === t.priceId
837
+ )?.local?.currency ?? void 0, l = this.api.request(`/api/v1/paywall/${this.paywallId}/start-checkout`, {
836
838
  method: "POST",
837
839
  headers: n,
838
840
  signal: t.signal,
@@ -840,13 +842,14 @@ class yt {
840
842
  email: this.identity.email,
841
843
  priceId: Number(t.priceId),
842
844
  offerId: t.offerId,
843
- successUrl: u,
845
+ successUrl: d,
844
846
  errorUrl: t.errorUrl,
845
- shopUrl: d,
847
+ shopUrl: f,
846
848
  productName: o?.checkout_product_name ?? void 0,
847
849
  trial_days: t.trialDays,
848
850
  ignoreActivePurchase: t.ignoreActivePurchase ? !0 : void 0,
849
- userMeta: this.identity.userId ? { userId: this.identity.userId } : void 0
851
+ userMeta: this.identity.userId ? { userId: this.identity.userId } : void 0,
852
+ localCurrency: c
850
853
  })
851
854
  }).then((h) => ({ url: h.checkoutUrl, acquiring: h.acquiring })).catch((h) => {
852
855
  throw h instanceof r && h.status === 409 && h.cause && typeof h.cause == "object" && h.cause.hasActivePurchase === !0 ? new r(
@@ -855,10 +858,10 @@ class yt {
855
858
  { status: 409, cause: h.cause }
856
859
  ) : h;
857
860
  });
858
- return this.inflightCheckouts.set(e, f), f.finally(() => {
859
- this.inflightCheckouts.get(e) === f && this.inflightCheckouts.delete(e);
861
+ return this.inflightCheckouts.set(e, l), l.finally(() => {
862
+ this.inflightCheckouts.get(e) === l && this.inflightCheckouts.delete(e);
860
863
  }).catch(() => {
861
- }), f;
864
+ }), l;
862
865
  }
863
866
  /**
864
867
  * URL Stripe/Paddle/Chargebee customer portal — место, где залогиненный
@@ -1454,8 +1457,8 @@ class pt {
1454
1457
  id: a.user.id,
1455
1458
  email: a.user.email,
1456
1459
  is_anonymous: a.user.is_anonymous ?? !1
1457
- }, u = { ...n, user: o };
1458
- return this.setSession(u, { event: "USER_UPDATED" }), await this.clearAnonRefreshToken(), { kind: "updated", session: u };
1460
+ }, d = { ...n, user: o };
1461
+ return this.setSession(d, { event: "USER_UPDATED" }), await this.clearAnonRefreshToken(), { kind: "updated", session: d };
1459
1462
  }
1460
1463
  /**
1461
1464
  * OAuth signin через popup с PKCE. Жизненный цикл:
@@ -1514,7 +1517,7 @@ class pt {
1514
1517
  await this.hydrated, this.gcOAuthFlows();
1515
1518
  const e = Z(), s = await tt(e), a = et(), n = {}, o = await this.getAccessToken().catch(() => null);
1516
1519
  o && (n.Authorization = `Bearer ${o}`);
1517
- const { authorize_url: u } = await this.api.request(
1520
+ const { authorize_url: d } = await this.api.request(
1518
1521
  `/api/v1/paywall/${this.paywallId}/auth/oauth/init`,
1519
1522
  {
1520
1523
  method: "POST",
@@ -1532,7 +1535,7 @@ class pt {
1532
1535
  userMeta: t.userMeta,
1533
1536
  provider: t.provider,
1534
1537
  startedAt: Date.now()
1535
- }), this.recordLastLoginMethod(t.provider), { authorize_url: u, state: a };
1538
+ }), this.recordLastLoginMethod(t.provider), { authorize_url: d, state: a };
1536
1539
  }
1537
1540
  /**
1538
1541
  * Шаг 2 OAuth split-API: обменивает code (полученный из popup) на session,
@@ -1719,7 +1722,7 @@ class pt {
1719
1722
  }
1720
1723
  }
1721
1724
  storageKey() {
1722
- return l.authSession(this.paywallId);
1725
+ return u.authSession(this.paywallId);
1723
1726
  }
1724
1727
  async hydrate() {
1725
1728
  try {
@@ -1773,7 +1776,7 @@ class pt {
1773
1776
  }
1774
1777
  async readAnonRefreshToken() {
1775
1778
  try {
1776
- const t = await this.storage.getItem(l.anonRefreshToken(this.paywallId));
1779
+ const t = await this.storage.getItem(u.anonRefreshToken(this.paywallId));
1777
1780
  return typeof t == "string" && t.length > 0 ? t : null;
1778
1781
  } catch {
1779
1782
  return null;
@@ -1782,7 +1785,7 @@ class pt {
1782
1785
  async writeAnonRefreshToken(t) {
1783
1786
  try {
1784
1787
  await this.storage.setItem(
1785
- l.anonRefreshToken(this.paywallId),
1788
+ u.anonRefreshToken(this.paywallId),
1786
1789
  t
1787
1790
  );
1788
1791
  } catch {
@@ -1791,7 +1794,7 @@ class pt {
1791
1794
  async clearAnonRefreshToken() {
1792
1795
  try {
1793
1796
  await this.storage.removeItem(
1794
- l.anonRefreshToken(this.paywallId)
1797
+ u.anonRefreshToken(this.paywallId)
1795
1798
  );
1796
1799
  } catch {
1797
1800
  }
@@ -1804,8 +1807,8 @@ class pt {
1804
1807
  async getLastLogin() {
1805
1808
  try {
1806
1809
  const [t, e] = await Promise.all([
1807
- this.storage.getItem(l.lastLoginMethod(this.paywallId)),
1808
- this.storage.getItem(l.lastLoginEmail(this.paywallId))
1810
+ this.storage.getItem(u.lastLoginMethod(this.paywallId)),
1811
+ this.storage.getItem(u.lastLoginEmail(this.paywallId))
1809
1812
  ]);
1810
1813
  return !t || !ot(t) ? null : { method: t, email: typeof e == "string" && e ? e : null };
1811
1814
  } catch {
@@ -1819,11 +1822,11 @@ class pt {
1819
1822
  this.recordLastLoginMethod(t), e && this.recordLastLoginEmail(e);
1820
1823
  }
1821
1824
  recordLastLoginMethod(t) {
1822
- this.storage.setItem(l.lastLoginMethod(this.paywallId), t).catch(() => {
1825
+ this.storage.setItem(u.lastLoginMethod(this.paywallId), t).catch(() => {
1823
1826
  });
1824
1827
  }
1825
1828
  recordLastLoginEmail(t) {
1826
- this.storage.setItem(l.lastLoginEmail(this.paywallId), t).catch(() => {
1829
+ this.storage.setItem(u.lastLoginEmail(this.paywallId), t).catch(() => {
1827
1830
  });
1828
1831
  }
1829
1832
  /**
@@ -1835,7 +1838,7 @@ class pt {
1835
1838
  */
1836
1839
  async readVisitorId() {
1837
1840
  try {
1838
- const t = await this.storage.getItem(l.visitorId);
1841
+ const t = await this.storage.getItem(u.visitorId);
1839
1842
  return typeof t == "string" && t.length >= 16 ? t : void 0;
1840
1843
  } catch {
1841
1844
  return;
@@ -1847,19 +1850,19 @@ function rt(i, t) {
1847
1850
  return new Promise((e, s) => {
1848
1851
  let a = !1;
1849
1852
  const n = () => {
1850
- a = !0, window.removeEventListener("message", o), clearInterval(u), clearTimeout(d);
1851
- }, o = (f) => {
1853
+ a = !0, window.removeEventListener("message", o), clearInterval(d), clearTimeout(f);
1854
+ }, o = (p) => {
1852
1855
  if (a) return;
1853
- const h = f.data;
1854
- if (!(!h || h.type !== "pw-oauth") && h.messageId === t) {
1855
- if (h.status === "success" && h.code) {
1856
+ const c = p.data;
1857
+ if (!(!c || c.type !== "pw-oauth") && c.messageId === t) {
1858
+ if (c.status === "success" && c.code) {
1856
1859
  n();
1857
1860
  try {
1858
1861
  i.close();
1859
1862
  } catch {
1860
1863
  }
1861
- e(h.code);
1862
- } else if (h.status === "error") {
1864
+ e(c.code);
1865
+ } else if (c.status === "error") {
1863
1866
  n();
1864
1867
  try {
1865
1868
  i.close();
@@ -1868,21 +1871,21 @@ function rt(i, t) {
1868
1871
  s(
1869
1872
  new r(
1870
1873
  "oauth_failed",
1871
- h.description || h.error || "OAuth provider returned error"
1874
+ c.description || c.error || "OAuth provider returned error"
1872
1875
  )
1873
1876
  );
1874
1877
  }
1875
1878
  }
1876
- }, u = setInterval(() => {
1879
+ }, d = setInterval(() => {
1877
1880
  if (a) return;
1878
- let f;
1881
+ let p;
1879
1882
  try {
1880
- f = i.closed;
1883
+ p = i.closed;
1881
1884
  } catch {
1882
1885
  return;
1883
1886
  }
1884
- f && (n(), s(new r("oauth_cancelled", "auth popup was closed")));
1885
- }, nt), d = setTimeout(() => {
1887
+ p && (n(), s(new r("oauth_cancelled", "auth popup was closed")));
1888
+ }, nt), f = setTimeout(() => {
1886
1889
  if (!a) {
1887
1890
  n();
1888
1891
  try {
@@ -2051,7 +2054,7 @@ export {
2051
2054
  r as PaywallError,
2052
2055
  R as QuotaExceededError,
2053
2056
  m as SDK_VERSION,
2054
- l as STORAGE_KEYS,
2057
+ u as STORAGE_KEYS,
2055
2058
  L as createStorage,
2056
2059
  b as ensureVisitorId,
2057
2060
  wt as findApplicableOffer,