@startsimpli/billing 0.4.7 → 0.4.9
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/package.json +1 -1
- package/src/components/PricingDetailPage.integration.test.tsx +27 -27
- package/src/components/PricingDetailPage.test.tsx +36 -36
- package/src/components/PricingDetailPage.tsx +8 -8
- package/src/components/PricingPage.test.tsx +36 -36
- package/src/components/PricingPage.tsx +7 -7
- package/src/components/PricingSection.test.tsx +36 -36
- package/src/components/PricingSection.tsx +7 -7
- package/src/components/SubscriptionManager.test.tsx +58 -58
- package/src/components/SubscriptionManager.tsx +15 -15
- package/src/components/UpgradeModal.test.tsx +12 -12
- package/src/components/UpgradeModal.tsx +8 -8
- package/src/hooks/useCheckout.test.tsx +6 -6
- package/src/hooks/useProduct.test.tsx +24 -24
- package/src/hooks/useSubscription.test.tsx +16 -16
- package/src/server/index.ts +1 -1
- package/src/server/proxy.test.ts +93 -0
- package/src/server/proxy.ts +23 -2
- package/src/types/index.ts +30 -30
- package/src/utils/api.test.ts +3 -3
|
@@ -31,67 +31,67 @@ const mockProduct: BillingProduct = {
|
|
|
31
31
|
id: "offer-1",
|
|
32
32
|
name: "Starter",
|
|
33
33
|
slug: "starter-monthly",
|
|
34
|
-
|
|
34
|
+
unitPrice: "0.00",
|
|
35
35
|
currency: "USD",
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
36
|
+
pricingModel: "flat",
|
|
37
|
+
intervalUnit: "month",
|
|
38
|
+
intervalCount: 1,
|
|
39
|
+
intervalDisplay: "per month",
|
|
40
|
+
isRecurring: true,
|
|
41
|
+
isFree: true,
|
|
42
42
|
features: [
|
|
43
43
|
{ key: "seats", name: "Team seats", value: 1, limit: 1 },
|
|
44
44
|
{ key: "projects", name: "Projects", value: 3, limit: 3 },
|
|
45
45
|
],
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
isActive: true,
|
|
47
|
+
isFeatured: false,
|
|
48
|
+
trialDays: 0,
|
|
49
|
+
sortOrder: 0,
|
|
50
|
+
ctaText: "Start Free",
|
|
51
51
|
},
|
|
52
52
|
{
|
|
53
53
|
id: "offer-2",
|
|
54
54
|
name: "Pro",
|
|
55
55
|
slug: "pro-monthly",
|
|
56
|
-
|
|
56
|
+
unitPrice: "49.00",
|
|
57
57
|
currency: "USD",
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
58
|
+
pricingModel: "flat",
|
|
59
|
+
intervalUnit: "month",
|
|
60
|
+
intervalCount: 1,
|
|
61
|
+
intervalDisplay: "per month",
|
|
62
|
+
isRecurring: true,
|
|
63
|
+
isFree: false,
|
|
64
64
|
features: [
|
|
65
65
|
{ key: "seats", name: "Team seats", value: 5, limit: 5 },
|
|
66
66
|
{ key: "projects", name: "Projects", value: "Unlimited" },
|
|
67
67
|
],
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
68
|
+
isActive: true,
|
|
69
|
+
isFeatured: true,
|
|
70
|
+
trialDays: 14,
|
|
71
|
+
sortOrder: 1,
|
|
72
|
+
ctaText: "Start Trial",
|
|
73
73
|
},
|
|
74
74
|
{
|
|
75
75
|
id: "offer-3",
|
|
76
76
|
name: "Enterprise",
|
|
77
77
|
slug: "enterprise-monthly",
|
|
78
|
-
|
|
78
|
+
unitPrice: "199.00",
|
|
79
79
|
currency: "USD",
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
80
|
+
pricingModel: "per_seat",
|
|
81
|
+
intervalUnit: "month",
|
|
82
|
+
intervalCount: 1,
|
|
83
|
+
intervalDisplay: "per month",
|
|
84
|
+
isRecurring: true,
|
|
85
|
+
isFree: false,
|
|
86
86
|
features: [
|
|
87
87
|
{ key: "seats", name: "Team seats", value: "Unlimited" },
|
|
88
88
|
{ key: "projects", name: "Projects", value: "Unlimited" },
|
|
89
89
|
],
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
isActive: true,
|
|
91
|
+
isFeatured: false,
|
|
92
|
+
trialDays: 0,
|
|
93
|
+
sortOrder: 2,
|
|
94
|
+
ctaText: "Contact Sales",
|
|
95
95
|
},
|
|
96
96
|
],
|
|
97
97
|
};
|
|
@@ -100,40 +100,40 @@ const mockSubscriptionActive: SubscriptionInfo = {
|
|
|
100
100
|
id: "sub-123",
|
|
101
101
|
offer: mockProduct.offers[1], // Pro plan
|
|
102
102
|
status: "active",
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
103
|
+
currentPeriodStart: "2026-01-01T00:00:00Z",
|
|
104
|
+
currentPeriodEnd: "2026-02-01T00:00:00Z",
|
|
105
|
+
cancelAtPeriodEnd: false,
|
|
106
|
+
trialEnd: null,
|
|
107
107
|
};
|
|
108
108
|
|
|
109
109
|
const mockSubscriptionTrialing: SubscriptionInfo = {
|
|
110
110
|
id: "sub-456",
|
|
111
111
|
offer: mockProduct.offers[1], // Pro plan
|
|
112
112
|
status: "trialing",
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
113
|
+
currentPeriodStart: "2026-01-01T00:00:00Z",
|
|
114
|
+
currentPeriodEnd: "2026-02-01T00:00:00Z",
|
|
115
|
+
cancelAtPeriodEnd: false,
|
|
116
|
+
trialEnd: "2026-01-15T00:00:00Z",
|
|
117
117
|
};
|
|
118
118
|
|
|
119
119
|
const mockSubscriptionPastDue: SubscriptionInfo = {
|
|
120
120
|
id: "sub-789",
|
|
121
121
|
offer: mockProduct.offers[1], // Pro plan
|
|
122
122
|
status: "past_due",
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
123
|
+
currentPeriodStart: "2026-01-01T00:00:00Z",
|
|
124
|
+
currentPeriodEnd: "2026-02-01T00:00:00Z",
|
|
125
|
+
cancelAtPeriodEnd: false,
|
|
126
|
+
trialEnd: null,
|
|
127
127
|
};
|
|
128
128
|
|
|
129
129
|
const mockSubscriptionCancelled: SubscriptionInfo = {
|
|
130
130
|
id: "sub-999",
|
|
131
131
|
offer: mockProduct.offers[1], // Pro plan
|
|
132
132
|
status: "cancelled",
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
133
|
+
currentPeriodStart: "2026-01-01T00:00:00Z",
|
|
134
|
+
currentPeriodEnd: "2026-02-01T00:00:00Z",
|
|
135
|
+
cancelAtPeriodEnd: true,
|
|
136
|
+
trialEnd: null,
|
|
137
137
|
};
|
|
138
138
|
|
|
139
139
|
function createWrapper(fetcher: typeof fetch) {
|
|
@@ -495,15 +495,15 @@ describe("SubscriptionManager", () => {
|
|
|
495
495
|
expect(statusBadge.className).toMatch(/warning|error|danger|alert/i);
|
|
496
496
|
});
|
|
497
497
|
|
|
498
|
-
it("does not crash when
|
|
498
|
+
it("does not crash when currentPeriodEnd is null (free plan)", async () => {
|
|
499
499
|
const freeSubscription: SubscriptionInfo = {
|
|
500
500
|
id: "sub-free",
|
|
501
|
-
offer: { ...mockProduct.offers[0],
|
|
501
|
+
offer: { ...mockProduct.offers[0], isFree: true },
|
|
502
502
|
status: "active",
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
503
|
+
currentPeriodStart: null,
|
|
504
|
+
currentPeriodEnd: null,
|
|
505
|
+
cancelAtPeriodEnd: false,
|
|
506
|
+
trialEnd: null,
|
|
507
507
|
};
|
|
508
508
|
const fetcher = mockFetchRouter({
|
|
509
509
|
"/billing/subscription/current/": freeSubscription,
|
|
@@ -167,30 +167,30 @@ export function SubscriptionManager({
|
|
|
167
167
|
<div data-testid="current-plan" className="rounded-2xl border border-gray-200 bg-white shadow-sm p-8">
|
|
168
168
|
<div className="flex items-center justify-between mb-4">
|
|
169
169
|
<h2 className="text-xl font-bold text-gray-900">{subscription.offer.name}</h2>
|
|
170
|
-
<StatusBadge status={subscription.status} trialEnd={subscription.
|
|
170
|
+
<StatusBadge status={subscription.status} trialEnd={subscription.trialEnd} />
|
|
171
171
|
</div>
|
|
172
172
|
|
|
173
173
|
<div className="mb-4">
|
|
174
|
-
{subscription.offer.
|
|
174
|
+
{subscription.offer.isFree ? (
|
|
175
175
|
<span aria-label="Free">
|
|
176
176
|
<span aria-hidden="true" className="text-3xl font-extrabold text-gray-900">Free</span>
|
|
177
177
|
</span>
|
|
178
178
|
) : (
|
|
179
|
-
<span aria-label={`${Math.floor(parseFloat(subscription.offer.
|
|
179
|
+
<span aria-label={`${Math.floor(parseFloat(subscription.offer.unitPrice) || 0)} dollars ${subscription.offer.intervalDisplay}`}>
|
|
180
180
|
<span aria-hidden="true" className="text-3xl font-extrabold text-gray-900">
|
|
181
|
-
${Math.floor(parseFloat(subscription.offer.
|
|
181
|
+
${Math.floor(parseFloat(subscription.offer.unitPrice) || 0)}
|
|
182
182
|
</span>
|
|
183
183
|
<span aria-hidden="true" className="text-base text-gray-500 ml-1">
|
|
184
|
-
{subscription.offer.
|
|
184
|
+
{subscription.offer.intervalDisplay}
|
|
185
185
|
</span>
|
|
186
186
|
</span>
|
|
187
187
|
)}
|
|
188
188
|
</div>
|
|
189
189
|
|
|
190
|
-
{subscription.
|
|
190
|
+
{subscription.currentPeriodStart && subscription.currentPeriodEnd && (
|
|
191
191
|
<p className="text-sm text-gray-500">
|
|
192
|
-
Billing period: {formatDate(subscription.
|
|
193
|
-
{formatDate(subscription.
|
|
192
|
+
Billing period: {formatDate(subscription.currentPeriodStart)} to{" "}
|
|
193
|
+
{formatDate(subscription.currentPeriodEnd)}
|
|
194
194
|
</p>
|
|
195
195
|
)}
|
|
196
196
|
</div>
|
|
@@ -211,24 +211,24 @@ export function SubscriptionManager({
|
|
|
211
211
|
<div
|
|
212
212
|
key={offer.id}
|
|
213
213
|
className={`rounded-xl border p-6 ${
|
|
214
|
-
offer.
|
|
214
|
+
offer.isFeatured
|
|
215
215
|
? "border-blue-600 shadow-md"
|
|
216
216
|
: "border-gray-200 bg-white shadow-sm"
|
|
217
217
|
}`}
|
|
218
218
|
>
|
|
219
219
|
<h4 className="text-lg font-bold text-gray-900 mb-2">{offer.name}</h4>
|
|
220
220
|
<div className="mb-3">
|
|
221
|
-
{offer.
|
|
221
|
+
{offer.isFree ? (
|
|
222
222
|
<span aria-label="Free">
|
|
223
223
|
<span aria-hidden="true" className="text-2xl font-extrabold text-gray-900">Free</span>
|
|
224
224
|
</span>
|
|
225
225
|
) : (
|
|
226
|
-
<span aria-label={`${Math.floor(parseFloat(offer.
|
|
226
|
+
<span aria-label={`${Math.floor(parseFloat(offer.unitPrice) || 0)} dollars ${offer.intervalDisplay}`}>
|
|
227
227
|
<span aria-hidden="true" className="text-2xl font-extrabold text-gray-900">
|
|
228
|
-
${Math.floor(parseFloat(offer.
|
|
228
|
+
${Math.floor(parseFloat(offer.unitPrice) || 0)}
|
|
229
229
|
</span>
|
|
230
230
|
<span aria-hidden="true" className="text-sm text-gray-500 ml-1">
|
|
231
|
-
{offer.
|
|
231
|
+
{offer.intervalDisplay}
|
|
232
232
|
</span>
|
|
233
233
|
</span>
|
|
234
234
|
)}
|
|
@@ -237,12 +237,12 @@ export function SubscriptionManager({
|
|
|
237
237
|
data-testid={`upgrade-cta-${offer.slug}`}
|
|
238
238
|
onClick={() => onPlanChange?.(offer)}
|
|
239
239
|
className={`w-full py-2.5 px-4 text-sm font-bold rounded-xl cursor-pointer transition-colors ${
|
|
240
|
-
offer.
|
|
240
|
+
offer.isFeatured
|
|
241
241
|
? "bg-blue-600 text-white hover:bg-blue-700"
|
|
242
242
|
: "bg-gray-100 text-gray-800 hover:bg-gray-200"
|
|
243
243
|
}`}
|
|
244
244
|
>
|
|
245
|
-
{offer.
|
|
245
|
+
{offer.ctaText}
|
|
246
246
|
</button>
|
|
247
247
|
</div>
|
|
248
248
|
))}
|
|
@@ -30,20 +30,20 @@ const mockProduct: BillingProduct = {
|
|
|
30
30
|
id: "offer-1",
|
|
31
31
|
name: "Pro",
|
|
32
32
|
slug: "pro-monthly",
|
|
33
|
-
|
|
33
|
+
unitPrice: "49.00",
|
|
34
34
|
currency: "USD",
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
35
|
+
pricingModel: "flat",
|
|
36
|
+
intervalUnit: "month",
|
|
37
|
+
intervalCount: 1,
|
|
38
|
+
intervalDisplay: "per month",
|
|
39
|
+
isRecurring: true,
|
|
40
|
+
isFree: false,
|
|
41
41
|
features: [{ key: "seats", name: "Team seats", value: 5, limit: 5 }],
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
42
|
+
isActive: true,
|
|
43
|
+
isFeatured: true,
|
|
44
|
+
trialDays: 14,
|
|
45
|
+
sortOrder: 0,
|
|
46
|
+
ctaText: "Upgrade to Pro",
|
|
47
47
|
},
|
|
48
48
|
],
|
|
49
49
|
};
|
|
@@ -122,7 +122,7 @@ export function UpgradeModal({
|
|
|
122
122
|
{product && (
|
|
123
123
|
<div data-testid="upgrade-offers" className="flex flex-wrap justify-center gap-5">
|
|
124
124
|
{product.offers.map((offer) => {
|
|
125
|
-
const isFeatured = offer.
|
|
125
|
+
const isFeatured = offer.isFeatured;
|
|
126
126
|
return (
|
|
127
127
|
<div
|
|
128
128
|
key={offer.id}
|
|
@@ -138,23 +138,23 @@ export function UpgradeModal({
|
|
|
138
138
|
<h3 className="text-lg font-bold text-gray-900 mb-2">{offer.name}</h3>
|
|
139
139
|
|
|
140
140
|
<div className="mb-2">
|
|
141
|
-
{offer.
|
|
141
|
+
{offer.isFree ? (
|
|
142
142
|
<span aria-label="Free">
|
|
143
143
|
<span aria-hidden="true" className="text-2xl font-extrabold text-gray-900">Free</span>
|
|
144
144
|
</span>
|
|
145
145
|
) : (
|
|
146
|
-
<span aria-label={`${Math.floor(parseFloat(offer.
|
|
146
|
+
<span aria-label={`${Math.floor(parseFloat(offer.unitPrice) || 0)} dollars ${offer.intervalDisplay}`}>
|
|
147
147
|
<span aria-hidden="true" className="text-2xl font-extrabold text-gray-900">
|
|
148
|
-
${Math.floor(parseFloat(offer.
|
|
148
|
+
${Math.floor(parseFloat(offer.unitPrice) || 0)}
|
|
149
149
|
</span>
|
|
150
|
-
<span aria-hidden="true" className="text-sm text-gray-500"> {offer.
|
|
150
|
+
<span aria-hidden="true" className="text-sm text-gray-500"> {offer.intervalDisplay}</span>
|
|
151
151
|
</span>
|
|
152
152
|
)}
|
|
153
153
|
</div>
|
|
154
154
|
|
|
155
|
-
{offer.
|
|
155
|
+
{offer.trialDays > 0 && (
|
|
156
156
|
<span className="inline-block bg-blue-50 text-blue-700 text-xs font-semibold px-2.5 py-0.5 rounded-md mb-3 w-fit">
|
|
157
|
-
{offer.
|
|
157
|
+
{offer.trialDays}-day free trial
|
|
158
158
|
</span>
|
|
159
159
|
)}
|
|
160
160
|
|
|
@@ -182,7 +182,7 @@ export function UpgradeModal({
|
|
|
182
182
|
}
|
|
183
183
|
`}
|
|
184
184
|
>
|
|
185
|
-
{offer.
|
|
185
|
+
{offer.ctaText}
|
|
186
186
|
</button>
|
|
187
187
|
</div>
|
|
188
188
|
);
|
|
@@ -23,7 +23,7 @@ describe("useCheckout", () => {
|
|
|
23
23
|
ok: true,
|
|
24
24
|
json: () =>
|
|
25
25
|
Promise.resolve({
|
|
26
|
-
|
|
26
|
+
sessionId: "cs_test_123",
|
|
27
27
|
url: "https://checkout.stripe.com/test",
|
|
28
28
|
}),
|
|
29
29
|
});
|
|
@@ -41,7 +41,7 @@ describe("useCheckout", () => {
|
|
|
41
41
|
});
|
|
42
42
|
|
|
43
43
|
expect(checkoutResult).toEqual({
|
|
44
|
-
|
|
44
|
+
sessionId: "cs_test_123",
|
|
45
45
|
url: "https://checkout.stripe.com/test",
|
|
46
46
|
});
|
|
47
47
|
expect(result.current.loading).toBe(false);
|
|
@@ -63,7 +63,7 @@ describe("useCheckout", () => {
|
|
|
63
63
|
const mockFetch = vi.fn().mockResolvedValue({
|
|
64
64
|
ok: true,
|
|
65
65
|
json: () =>
|
|
66
|
-
Promise.resolve({
|
|
66
|
+
Promise.resolve({ sessionId: "cs_1", url: "https://test.com" }),
|
|
67
67
|
});
|
|
68
68
|
const wrapper = createWrapper(mockFetch as unknown as typeof fetch);
|
|
69
69
|
|
|
@@ -120,7 +120,7 @@ describe("useCheckout", () => {
|
|
|
120
120
|
ok: true,
|
|
121
121
|
json: () =>
|
|
122
122
|
Promise.resolve({
|
|
123
|
-
|
|
123
|
+
sessionId: "cs_redirect_1",
|
|
124
124
|
url: "https://checkout.stripe.com/pay/cs_redirect_1",
|
|
125
125
|
}),
|
|
126
126
|
});
|
|
@@ -148,7 +148,7 @@ describe("useCheckout", () => {
|
|
|
148
148
|
ok: true,
|
|
149
149
|
json: () =>
|
|
150
150
|
Promise.resolve({
|
|
151
|
-
|
|
151
|
+
subscriptionId: "sub_free_123",
|
|
152
152
|
status: "active",
|
|
153
153
|
}),
|
|
154
154
|
});
|
|
@@ -162,7 +162,7 @@ describe("useCheckout", () => {
|
|
|
162
162
|
});
|
|
163
163
|
|
|
164
164
|
expect(freeResult).toEqual({
|
|
165
|
-
|
|
165
|
+
subscriptionId: "sub_free_123",
|
|
166
166
|
status: "active",
|
|
167
167
|
});
|
|
168
168
|
expect(result.current.loading).toBe(false);
|
|
@@ -15,39 +15,39 @@ const mockProduct: BillingProduct = {
|
|
|
15
15
|
id: "offer-1",
|
|
16
16
|
name: "Starter",
|
|
17
17
|
slug: "starter-monthly",
|
|
18
|
-
|
|
18
|
+
unitPrice: "0.00",
|
|
19
19
|
currency: "USD",
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
pricingModel: "flat",
|
|
21
|
+
intervalUnit: "month",
|
|
22
|
+
intervalCount: 1,
|
|
23
|
+
intervalDisplay: "per month",
|
|
24
|
+
isRecurring: true,
|
|
25
|
+
isFree: true,
|
|
26
26
|
features: [],
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
isActive: true,
|
|
28
|
+
isFeatured: false,
|
|
29
|
+
trialDays: 0,
|
|
30
|
+
sortOrder: 0,
|
|
31
|
+
ctaText: "Start Free",
|
|
32
32
|
},
|
|
33
33
|
{
|
|
34
34
|
id: "offer-2",
|
|
35
35
|
name: "Pro",
|
|
36
36
|
slug: "pro-monthly",
|
|
37
|
-
|
|
37
|
+
unitPrice: "49.00",
|
|
38
38
|
currency: "USD",
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
39
|
+
pricingModel: "flat",
|
|
40
|
+
intervalUnit: "month",
|
|
41
|
+
intervalCount: 1,
|
|
42
|
+
intervalDisplay: "per month",
|
|
43
|
+
isRecurring: true,
|
|
44
|
+
isFree: false,
|
|
45
45
|
features: [{ key: "seats", name: "Team seats", value: 5, limit: 5 }],
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
isActive: true,
|
|
47
|
+
isFeatured: true,
|
|
48
|
+
trialDays: 14,
|
|
49
|
+
sortOrder: 1,
|
|
50
|
+
ctaText: "Start Trial",
|
|
51
51
|
},
|
|
52
52
|
],
|
|
53
53
|
};
|
|
@@ -9,30 +9,30 @@ const mockOffer: ProductOffer = {
|
|
|
9
9
|
id: "offer-2",
|
|
10
10
|
name: "Pro",
|
|
11
11
|
slug: "pro-monthly",
|
|
12
|
-
|
|
12
|
+
unitPrice: "49.00",
|
|
13
13
|
currency: "USD",
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
14
|
+
pricingModel: "flat",
|
|
15
|
+
intervalUnit: "month",
|
|
16
|
+
intervalCount: 1,
|
|
17
|
+
intervalDisplay: "per month",
|
|
18
|
+
isRecurring: true,
|
|
19
|
+
isFree: false,
|
|
20
20
|
features: [{ key: "seats", name: "Team seats", value: 5, limit: 5 }],
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
isActive: true,
|
|
22
|
+
isFeatured: true,
|
|
23
|
+
trialDays: 14,
|
|
24
|
+
sortOrder: 1,
|
|
25
|
+
ctaText: "Start Trial",
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
const mockSubscription: SubscriptionInfo = {
|
|
29
29
|
id: "sub-123",
|
|
30
30
|
offer: mockOffer,
|
|
31
31
|
status: "active",
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
currentPeriodStart: "2026-01-01T00:00:00Z",
|
|
33
|
+
currentPeriodEnd: "2026-02-01T00:00:00Z",
|
|
34
|
+
cancelAtPeriodEnd: false,
|
|
35
|
+
trialEnd: null,
|
|
36
36
|
};
|
|
37
37
|
|
|
38
38
|
function createWrapper(fetcher: typeof fetch) {
|
package/src/server/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { camelToSnake, proxyBillingRequest, fetchDjangoServiceToken } from './proxy';
|
|
1
|
+
export { camelToSnake, snakeToCamel, proxyBillingRequest, fetchDjangoServiceToken } from './proxy';
|
|
2
2
|
export type { BillingProxyOptions, DjangoServiceTokenOptions } from './proxy';
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { snakeToCamel, camelToSnake, proxyBillingRequest } from './proxy';
|
|
3
|
+
|
|
4
|
+
describe('snakeToCamel', () => {
|
|
5
|
+
it('converts snake_case keys to camelCase recursively', () => {
|
|
6
|
+
const input = {
|
|
7
|
+
unit_price: '9.00',
|
|
8
|
+
is_free: false,
|
|
9
|
+
nested_obj: { interval_display: 'per month', is_recurring: true },
|
|
10
|
+
list_of_things: [{ cta_text: 'Get Started' }],
|
|
11
|
+
};
|
|
12
|
+
expect(snakeToCamel(input)).toEqual({
|
|
13
|
+
unitPrice: '9.00',
|
|
14
|
+
isFree: false,
|
|
15
|
+
nestedObj: { intervalDisplay: 'per month', isRecurring: true },
|
|
16
|
+
listOfThings: [{ ctaText: 'Get Started' }],
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('leaves string values untouched even when they contain underscores', () => {
|
|
21
|
+
expect(snakeToCamel({ feature_key: 'patterns_per_month' })).toEqual({
|
|
22
|
+
featureKey: 'patterns_per_month',
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('is idempotent for camelCase keys', () => {
|
|
27
|
+
const input = { alreadyCamel: true, nested: { stillCamel: 1 } };
|
|
28
|
+
expect(snakeToCamel(input)).toEqual(input);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('is the inverse of camelToSnake', () => {
|
|
32
|
+
const camel = { unitPrice: '9.00', isFree: true, nested: { ctaText: 'Go' } };
|
|
33
|
+
expect(snakeToCamel(camelToSnake(camel))).toEqual(camel);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('proxyBillingRequest with fromSnake option', () => {
|
|
38
|
+
const originalFetch = globalThis.fetch;
|
|
39
|
+
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
globalThis.fetch = vi.fn();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
afterEach(() => {
|
|
45
|
+
globalThis.fetch = originalFetch;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('transforms snake_case response to camelCase when fromSnake is true', async () => {
|
|
49
|
+
(globalThis.fetch as ReturnType<typeof vi.fn>).mockResolvedValueOnce(
|
|
50
|
+
new Response(
|
|
51
|
+
JSON.stringify({ unit_price: '9.00', is_free: false, offers: [{ cta_text: 'Go' }] }),
|
|
52
|
+
{ status: 200, headers: { 'Content-Type': 'application/json' } }
|
|
53
|
+
)
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
const response = await proxyBillingRequest(
|
|
57
|
+
'http://django',
|
|
58
|
+
'/api/v1/billing/products/x/',
|
|
59
|
+
undefined,
|
|
60
|
+
{ public: true, fromSnake: true }
|
|
61
|
+
);
|
|
62
|
+
const body = await response.json();
|
|
63
|
+
expect(body).toEqual({
|
|
64
|
+
unitPrice: '9.00',
|
|
65
|
+
isFree: false,
|
|
66
|
+
offers: [{ ctaText: 'Go' }],
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('leaves response untouched when no transform option is set', async () => {
|
|
71
|
+
(globalThis.fetch as ReturnType<typeof vi.fn>).mockResolvedValueOnce(
|
|
72
|
+
new Response(JSON.stringify({ alreadyCamel: true }), { status: 200 })
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const response = await proxyBillingRequest('http://django', '/path/', 'token');
|
|
76
|
+
const body = await response.json();
|
|
77
|
+
expect(body).toEqual({ alreadyCamel: true });
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('preserves non-OK status and error shape', async () => {
|
|
81
|
+
(globalThis.fetch as ReturnType<typeof vi.fn>).mockResolvedValueOnce(
|
|
82
|
+
new Response('Not found', { status: 404 })
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const response = await proxyBillingRequest(
|
|
86
|
+
'http://django',
|
|
87
|
+
'/x/',
|
|
88
|
+
undefined,
|
|
89
|
+
{ public: true, fromSnake: true }
|
|
90
|
+
);
|
|
91
|
+
expect(response.status).toBe(404);
|
|
92
|
+
});
|
|
93
|
+
});
|
package/src/server/proxy.ts
CHANGED
|
@@ -87,11 +87,31 @@ export function camelToSnake(obj: unknown): unknown {
|
|
|
87
87
|
return obj;
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
/**
|
|
91
|
+
* Recursively converts snake_case object keys to camelCase.
|
|
92
|
+
* Used for Django endpoints that bypass the camel-case middleware
|
|
93
|
+
* (e.g. public product endpoints) so consumers always receive camelCase.
|
|
94
|
+
*/
|
|
95
|
+
export function snakeToCamel(obj: unknown): unknown {
|
|
96
|
+
if (Array.isArray(obj)) return obj.map(snakeToCamel);
|
|
97
|
+
if (obj !== null && typeof obj === 'object') {
|
|
98
|
+
return Object.fromEntries(
|
|
99
|
+
Object.entries(obj as Record<string, unknown>).map(([k, v]) => [
|
|
100
|
+
k.replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase()),
|
|
101
|
+
snakeToCamel(v),
|
|
102
|
+
])
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
return obj;
|
|
106
|
+
}
|
|
107
|
+
|
|
90
108
|
export interface BillingProxyOptions {
|
|
91
109
|
method?: string;
|
|
92
110
|
body?: unknown;
|
|
93
111
|
/** If true, apply camelToSnake transform to the response body */
|
|
94
112
|
normalize?: boolean;
|
|
113
|
+
/** If true, apply snakeToCamel transform to the response body */
|
|
114
|
+
fromSnake?: boolean;
|
|
95
115
|
/** If true, skip Authorization header even when accessToken is provided */
|
|
96
116
|
public?: boolean;
|
|
97
117
|
}
|
|
@@ -110,7 +130,7 @@ export async function proxyBillingRequest(
|
|
|
110
130
|
accessToken: string | undefined,
|
|
111
131
|
options: BillingProxyOptions = {}
|
|
112
132
|
): Promise<NextResponse> {
|
|
113
|
-
const { method = 'GET', body, normalize = false } = options;
|
|
133
|
+
const { method = 'GET', body, normalize = false, fromSnake = false } = options;
|
|
114
134
|
const url = `${apiBaseUrl}${path}`;
|
|
115
135
|
|
|
116
136
|
const headers: Record<string, string> = { 'Content-Type': 'application/json' };
|
|
@@ -135,7 +155,8 @@ export async function proxyBillingRequest(
|
|
|
135
155
|
}
|
|
136
156
|
|
|
137
157
|
const data = await response.json();
|
|
138
|
-
|
|
158
|
+
const transformed = normalize ? camelToSnake(data) : fromSnake ? snakeToCamel(data) : data;
|
|
159
|
+
return NextResponse.json(transformed);
|
|
139
160
|
} catch (error) {
|
|
140
161
|
return NextResponse.json(
|
|
141
162
|
{
|