@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 +3 -2
- package/dist/lib/products.js +55 -32
- package/package.json +2 -2
- package/src/lib/products.ts +40 -5
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`, …)
|
|
136
|
-
|
|
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.
|
package/dist/lib/products.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
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
|
-
},
|
|
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:
|
|
24
|
+
return new Intl.NumberFormat("en-US", { style: "currency", currency: r }).format(e);
|
|
25
25
|
} catch {
|
|
26
|
-
return `${
|
|
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
|
-
},
|
|
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
|
-
},
|
|
39
|
-
const t =
|
|
61
|
+
}, b = (e, r) => {
|
|
62
|
+
const t = P[e];
|
|
40
63
|
if (t)
|
|
41
64
|
return {
|
|
42
|
-
periodDays: t.days *
|
|
43
|
-
periodWeeks: Math.round(t.weeks *
|
|
44
|
-
periodMonths: Math.floor(t.months *
|
|
45
|
-
periodYears: Math.floor(t.years *
|
|
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
|
-
},
|
|
48
|
-
if (
|
|
49
|
-
return { period:
|
|
50
|
-
if (
|
|
51
|
-
const t = `${
|
|
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
|
-
},
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
const t =
|
|
57
|
-
if (!t || typeof t.amount != "number" || !t.currency) return
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
if (!
|
|
62
|
-
const
|
|
63
|
-
Object.assign(
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
return typeof
|
|
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
|
-
|
|
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.
|
|
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.
|
|
59
|
+
"@superwall/host": "2.5.2",
|
|
60
60
|
"qrcode.react": "^4.2.0",
|
|
61
61
|
"sonner": "^2.0.7"
|
|
62
62
|
},
|
package/src/lib/products.ts
CHANGED
|
@@ -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
|
|
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
|
|
120
|
-
variables.trialPeriodText = `${trialDays}-day`;
|
|
155
|
+
Object.assign(variables, trialVariables(product, currency, trialDays) ?? {});
|
|
121
156
|
}
|
|
122
157
|
|
|
123
158
|
return variables;
|