@superwall/studio 2.5.1 → 2.5.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/README.md CHANGED
@@ -132,8 +132,9 @@ version? }, onStep)` performs one and streams its steps, `status(surface)`
132
132
  the sheet's `@property` rules at document level, where browsers expect them.
133
133
  - `@superwall/studio/products` is the pure mapping from a dashboard product
134
134
  (the v2 API shape) to the variables a paywall reads (`price`, `periodly`,
135
- `trialPeriodDays`, …). The CLI and the hosted studio share it, so the
136
- Variables panel shows the same numbers in both.
135
+ `trialPeriodDays`, `trialPeriodPrice`, …), with amounts in the currency's
136
+ own minor units. `superwall dev` uses it; the dashboard builds its samples
137
+ with its own pricing engine, so the two must be kept in step by hand.
137
138
  - `@superwall/studio/contract` is a types-only entry: the action, status,
138
139
  samples and app-scheme shapes the CLI implements for the framework's
139
140
  preview server, importable from node without DOM types.
@@ -1,4 +1,4 @@
1
- const u = {
1
+ const m = {
2
2
  USD: "$",
3
3
  CAD: "$",
4
4
  AUD: "$",
@@ -19,53 +19,76 @@ const u = {
19
19
  TRY: "₺",
20
20
  RUB: "₽",
21
21
  ZAR: "R"
22
- }, m = 100, p = { day: 365, week: 52, month: 12, year: 1 }, l = (r) => u[r.toUpperCase()] ?? "", o = (r, e) => {
22
+ }, p = { day: 365, week: 52, month: 12, year: 1 }, y = (e) => m[e.toUpperCase()] ?? "", o = (e, r) => {
23
23
  try {
24
- return new Intl.NumberFormat("en-US", { style: "currency", currency: e }).format(r);
24
+ return new Intl.NumberFormat("en-US", { style: "currency", currency: r }).format(e);
25
25
  } catch {
26
- return `${l(e)}${r.toFixed(2)}`;
26
+ return `${y(r)}${e.toFixed(2)}`;
27
27
  }
28
+ }, u = (e) => {
29
+ try {
30
+ return new Intl.NumberFormat("en-US", { style: "currency", currency: e }).formatToParts(0).find((t) => t.type === "fraction")?.value.length ?? 0;
31
+ } catch {
32
+ return 2;
33
+ }
34
+ }, d = (e, r) => e / 10 ** u(r), f = (e, r, t) => {
35
+ const i = e.trial_price, a = i?.amount ?? 0;
36
+ if (a !== 0 && i?.currency?.toUpperCase() !== r) return;
37
+ const n = d(a, r), s = n * 365 / t;
38
+ return {
39
+ trialPeriodDays: t,
40
+ trialPeriodWeeks: Math.floor(t / 7),
41
+ trialPeriodMonths: Math.floor(t / 30),
42
+ trialPeriodYears: Math.floor(t / 365),
43
+ trialPeriodText: `${t}-day`,
44
+ rawTrialPeriodPrice: n,
45
+ trialPeriodPrice: o(n, r),
46
+ trialPeriodDailyPrice: o(n / t, r),
47
+ trialPeriodWeeklyPrice: o(s / 52, r),
48
+ trialPeriodMonthlyPrice: o(s / 12, r),
49
+ trialPeriodYearlyPrice: o(s, r)
50
+ };
28
51
  }, h = {
29
52
  day: "daily",
30
53
  week: "weekly",
31
54
  month: "monthly",
32
55
  year: "yearly"
33
- }, d = {
56
+ }, P = {
34
57
  day: { days: 1, weeks: 1 / 7, months: 1 / 30, years: 1 / 365 },
35
58
  week: { days: 7, weeks: 1, months: 1 / 4, years: 1 / 52 },
36
59
  month: { days: 30, weeks: 4, months: 1, years: 1 / 12 },
37
60
  year: { days: 365, weeks: 52, months: 12, years: 1 }
38
- }, f = (r, e) => {
39
- const t = d[r];
61
+ }, b = (e, r) => {
62
+ const t = P[e];
40
63
  if (t)
41
64
  return {
42
- periodDays: t.days * e,
43
- periodWeeks: Math.round(t.weeks * e),
44
- periodMonths: Math.floor(t.months * e),
45
- periodYears: Math.floor(t.years * e)
65
+ periodDays: t.days * r,
66
+ periodWeeks: Math.round(t.weeks * r),
67
+ periodMonths: Math.floor(t.months * r),
68
+ periodYears: Math.floor(t.years * r)
46
69
  };
47
- }, b = (r, e) => {
48
- if (e === 1 || !d[r])
49
- return { period: r, periodly: h[r] ?? `${r}ly` };
50
- if (r === "month" && e === 3) return { period: "quarter", periodly: "quarterly" };
51
- const t = `${e} ${r}s`;
70
+ }, k = (e, r) => {
71
+ if (r === 1 || !P[e])
72
+ return { period: e, periodly: h[e] ?? `${e}ly` };
73
+ if (e === "month" && r === 3) return { period: "quarter", periodly: "quarterly" };
74
+ const t = `${r} ${e}s`;
52
75
  return { period: t, periodly: `every ${t}` };
53
- }, k = (r) => {
54
- const e = {};
55
- r.identifier && (e.identifier = r.identifier);
56
- const t = r.price;
57
- if (!t || typeof t.amount != "number" || !t.currency) return e;
58
- const s = t.currency.toUpperCase(), n = t.amount / m;
59
- e.price = o(n, s), e.rawPrice = n, e.currencyCode = s, e.currencySymbol = l(s);
60
- const i = r.subscription?.period;
61
- if (!i) return e;
62
- const c = r.subscription?.period_count ?? 1;
63
- Object.assign(e, b(i, c)), Object.assign(e, f(i, c) ?? {});
64
- const a = (p[i] ?? 1) / c;
65
- e.yearlyPrice = o(n * a, s), e.monthlyPrice = o(n * a / 12, s), e.weeklyPrice = o(n * a / 52, s), e.dailyPrice = o(n * a / 365, s);
66
- const y = r.subscription?.trial_period_days;
67
- return typeof y == "number" && y > 0 && (e.trialPeriodDays = y, e.trialPeriodText = `${y}-day`), e;
76
+ }, w = (e) => {
77
+ const r = {};
78
+ e.identifier && (r.identifier = e.identifier);
79
+ const t = e.price;
80
+ if (!t || typeof t.amount != "number" || !t.currency) return r;
81
+ const i = t.currency.toUpperCase(), a = d(t.amount, i);
82
+ r.price = o(a, i), r.rawPrice = a, r.currencyCode = i, r.currencySymbol = y(i);
83
+ const n = e.subscription?.period;
84
+ if (!n) return r;
85
+ const s = e.subscription?.period_count ?? 1;
86
+ Object.assign(r, k(n, s)), Object.assign(r, b(n, s) ?? {});
87
+ const l = (p[n] ?? 1) / s;
88
+ r.yearlyPrice = o(a * l, i), r.monthlyPrice = o(a * l / 12, i), r.weeklyPrice = o(a * l / 52, i), r.dailyPrice = o(a * l / 365, i);
89
+ const c = e.subscription?.trial_period_days;
90
+ return typeof c == "number" && c > 0 && Object.assign(r, f(e, i, c) ?? {}), r;
68
91
  };
69
92
  export {
70
- k as productVariables
93
+ w as productVariables
71
94
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superwall/studio",
3
- "version": "2.5.1",
3
+ "version": "2.5.2",
4
4
  "description": "The Superwall studio: paywall browsing, device preview, and editing surfaces. Shared between `superwall dev` (local studio) and the hosted dashboard.",
5
5
  "keywords": [
6
6
  "paywall",
@@ -56,7 +56,7 @@
56
56
  "clean": "rm -rf dist coverage"
57
57
  },
58
58
  "dependencies": {
59
- "@superwall/host": "2.5.1",
59
+ "@superwall/host": "2.5.2",
60
60
  "qrcode.react": "^4.2.0",
61
61
  "sonner": "^2.0.7"
62
62
  },
@@ -38,8 +38,6 @@ const SYMBOLS: Record<string, string> = {
38
38
  ZAR: "R",
39
39
  };
40
40
 
41
- const MINOR = 100;
42
-
43
41
  const PER_YEAR: Record<string, number> = { day: 365, week: 52, month: 12, year: 1 };
44
42
 
45
43
  const symbolFor = (currency: string): string => SYMBOLS[currency.toUpperCase()] ?? "";
@@ -52,6 +50,44 @@ const money = (amount: number, currency: string): string => {
52
50
  }
53
51
  };
54
52
 
53
+ const fractionDigits = (currency: string): number => {
54
+ try {
55
+ const parts = new Intl.NumberFormat("en-US", { style: "currency", currency }).formatToParts(0);
56
+ return parts.find((part) => part.type === "fraction")?.value.length ?? 0;
57
+ } catch {
58
+ return 2;
59
+ }
60
+ };
61
+
62
+ const majorUnits = (amount: number, currency: string): number =>
63
+ amount / 10 ** fractionDigits(currency);
64
+
65
+ const trialVariables = (
66
+ product: DashboardProduct,
67
+ currency: string,
68
+ days: number,
69
+ ): Record<string, unknown> | undefined => {
70
+ const trialPrice = product.trial_price;
71
+ const amount = trialPrice?.amount ?? 0;
72
+ if (amount !== 0 && trialPrice?.currency?.toUpperCase() !== currency) return undefined;
73
+
74
+ const raw = majorUnits(amount, currency);
75
+ const yearly = (raw * 365) / days;
76
+ return {
77
+ trialPeriodDays: days,
78
+ trialPeriodWeeks: Math.floor(days / 7),
79
+ trialPeriodMonths: Math.floor(days / 30),
80
+ trialPeriodYears: Math.floor(days / 365),
81
+ trialPeriodText: `${days}-day`,
82
+ rawTrialPeriodPrice: raw,
83
+ trialPeriodPrice: money(raw, currency),
84
+ trialPeriodDailyPrice: money(raw / days, currency),
85
+ trialPeriodWeeklyPrice: money(yearly / 52, currency),
86
+ trialPeriodMonthlyPrice: money(yearly / 12, currency),
87
+ trialPeriodYearlyPrice: money(yearly, currency),
88
+ };
89
+ };
90
+
55
91
  const PERIODLY: Record<string, string> = {
56
92
  day: "daily",
57
93
  week: "weekly",
@@ -95,7 +131,7 @@ export const productVariables = (product: DashboardProduct): Record<string, unkn
95
131
  if (!price || typeof price.amount !== "number" || !price.currency) return variables;
96
132
 
97
133
  const currency = price.currency.toUpperCase();
98
- const value = price.amount / MINOR;
134
+ const value = majorUnits(price.amount, currency);
99
135
  variables.price = money(value, currency);
100
136
  variables.rawPrice = value;
101
137
  variables.currencyCode = currency;
@@ -116,8 +152,7 @@ export const productVariables = (product: DashboardProduct): Record<string, unkn
116
152
 
117
153
  const trialDays = product.subscription?.trial_period_days;
118
154
  if (typeof trialDays === "number" && trialDays > 0) {
119
- variables.trialPeriodDays = trialDays;
120
- variables.trialPeriodText = `${trialDays}-day`;
155
+ Object.assign(variables, trialVariables(product, currency, trialDays) ?? {});
121
156
  }
122
157
 
123
158
  return variables;