@fantastic.dev/better-stripe 0.60.0 → 0.61.0
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/CHANGELOG.md +12 -0
- package/README.md +5 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +26 -6
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +13 -3
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +26 -6
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +13 -3
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/usage/creditCapacity.d.ts.map +1 -1
- package/dist/client/usage/creditCapacity.js +7 -1
- package/dist/client/usage/creditCapacity.js.map +1 -1
- package/dist/client/usage/creditClient.d.ts +3 -0
- package/dist/client/usage/creditClient.d.ts.map +1 -1
- package/dist/client/usage/creditDelivery.d.ts.map +1 -1
- package/dist/client/usage/creditDelivery.js +4 -1
- package/dist/client/usage/creditDelivery.js.map +1 -1
- package/dist/client/usage/creditLifecycle.d.ts.map +1 -1
- package/dist/client/usage/creditLifecycle.js +2 -1
- package/dist/client/usage/creditLifecycle.js.map +1 -1
- package/dist/client/usage/creditNative.d.ts +3 -0
- package/dist/client/usage/creditNative.d.ts.map +1 -1
- package/dist/client/usage/creditNative.js +3 -0
- package/dist/client/usage/creditNative.js.map +1 -1
- package/dist/client/usage/creditProvider.d.ts.map +1 -1
- package/dist/client/usage/creditProvider.js +2 -1
- package/dist/client/usage/creditProvider.js.map +1 -1
- package/dist/client/usage/creditRead.d.ts +1 -0
- package/dist/client/usage/creditRead.d.ts.map +1 -1
- package/dist/client/usage/creditReconciliation.d.ts.map +1 -1
- package/dist/client/usage/creditReconciliation.js +11 -3
- package/dist/client/usage/creditReconciliation.js.map +1 -1
- package/dist/client/usage/creditSettlement.fixtures.d.ts +1 -0
- package/dist/client/usage/creditSettlement.fixtures.d.ts.map +1 -1
- package/dist/client/usage/creditTransport.fixtures.d.ts +1 -0
- package/dist/client/usage/creditTransport.fixtures.d.ts.map +1 -1
- package/dist/client/usage/creditTransport.fixtures.js +2 -1
- package/dist/client/usage/creditTransport.fixtures.js.map +1 -1
- package/dist/client/usage/index.d.ts +2 -0
- package/dist/client/usage/index.d.ts.map +1 -1
- package/dist/client/usage/subscriptionCancellation.d.ts.map +1 -1
- package/dist/client/usage/subscriptionCancellation.js +3 -2
- package/dist/client/usage/subscriptionCancellation.js.map +1 -1
- package/dist/client/usage/subscriptionCheckout.d.ts.map +1 -1
- package/dist/client/usage/subscriptionCheckout.js +3 -2
- package/dist/client/usage/subscriptionCheckout.js.map +1 -1
- package/dist/client/usage/subscriptionLifecycle.d.ts +1 -0
- package/dist/client/usage/subscriptionLifecycle.d.ts.map +1 -1
- package/dist/client/usage/subscriptionLifecycle.js +3 -2
- package/dist/client/usage/subscriptionLifecycle.js.map +1 -1
- package/dist/client/usage/subscriptionProvider.d.ts +2 -0
- package/dist/client/usage/subscriptionProvider.d.ts.map +1 -1
- package/dist/client/usage/subscriptionProvider.js +9 -2
- package/dist/client/usage/subscriptionProvider.js.map +1 -1
- package/dist/component/_generated/component.d.ts +21 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/earnings/triggers.d.ts +16 -6
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/schema.d.ts +13 -3
- package/dist/component/usage/allowance.fixtures.d.ts +4212 -0
- package/dist/component/usage/allowance.fixtures.d.ts.map +1 -0
- package/dist/component/usage/allowance.fixtures.js +67 -0
- package/dist/component/usage/allowance.fixtures.js.map +1 -0
- package/dist/component/usageCredits/accounting.fixtures.d.ts +4235 -0
- package/dist/component/usageCredits/accounting.fixtures.d.ts.map +1 -0
- package/dist/component/usageCredits/accounting.fixtures.js +89 -0
- package/dist/component/usageCredits/accounting.fixtures.js.map +1 -0
- package/dist/component/usageCredits/helpers.d.ts +7 -0
- package/dist/component/usageCredits/helpers.d.ts.map +1 -1
- package/dist/component/usageCredits/helpers.js +5 -0
- package/dist/component/usageCredits/helpers.js.map +1 -1
- package/dist/component/usageCredits/mutations.d.ts +6 -0
- package/dist/component/usageCredits/mutations.d.ts.map +1 -1
- package/dist/component/usageCredits/mutations.js +70 -7
- package/dist/component/usageCredits/mutations.js.map +1 -1
- package/dist/component/usageCredits/purchase.d.ts +1 -0
- package/dist/component/usageCredits/purchase.d.ts.map +1 -1
- package/dist/component/usageCredits/queries.d.ts +10 -0
- package/dist/component/usageCredits/queries.d.ts.map +1 -1
- package/dist/component/usageCredits/schema.d.ts +24 -4
- package/dist/component/usageCredits/schema.d.ts.map +1 -1
- package/dist/component/usageCredits/schema.js +4 -0
- package/dist/component/usageCredits/schema.js.map +1 -1
- package/dist/component/usageSubscriptions/history.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/history.js +3 -1
- package/dist/component/usageSubscriptions/history.js.map +1 -1
- package/dist/component/usageSubscriptions/mutations.d.ts +5 -0
- package/dist/component/usageSubscriptions/mutations.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/mutations.js +19 -0
- package/dist/component/usageSubscriptions/mutations.js.map +1 -1
- package/dist/component/usageSubscriptions/queries.d.ts +2 -0
- package/dist/component/usageSubscriptions/queries.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/schema.d.ts +7 -2
- package/dist/component/usageSubscriptions/schema.d.ts.map +1 -1
- package/dist/component/usageSubscriptions/schema.js +1 -0
- package/dist/component/usageSubscriptions/schema.js.map +1 -1
- package/dist/react/index.d.ts +12 -0
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +7 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/workflows/Pagination.d.ts +1 -0
- package/dist/react/workflows/Pagination.d.ts.map +1 -1
- package/dist/react/workflows/Pagination.js +1 -0
- package/dist/react/workflows/Pagination.js.map +1 -1
- package/dist/react/workflows/Usage.fixtures.d.ts +17 -0
- package/dist/react/workflows/Usage.fixtures.d.ts.map +1 -0
- package/dist/react/workflows/Usage.fixtures.js +79 -0
- package/dist/react/workflows/Usage.fixtures.js.map +1 -0
- package/dist/react/workflows/Usage.types.d.ts +25 -0
- package/dist/react/workflows/Usage.types.d.ts.map +1 -0
- package/dist/react/workflows/Usage.types.js +11 -0
- package/dist/react/workflows/Usage.types.js.map +1 -0
- package/dist/react/workflows/UsageAllowance.d.ts +14 -0
- package/dist/react/workflows/UsageAllowance.d.ts.map +1 -0
- package/dist/react/workflows/UsageAllowance.js +21 -0
- package/dist/react/workflows/UsageAllowance.js.map +1 -0
- package/dist/react/workflows/UsageBilling.d.ts +20 -0
- package/dist/react/workflows/UsageBilling.d.ts.map +1 -0
- package/dist/react/workflows/UsageBilling.js +40 -0
- package/dist/react/workflows/UsageBilling.js.map +1 -0
- package/dist/react/workflows/UsageCredits.d.ts +14 -0
- package/dist/react/workflows/UsageCredits.d.ts.map +1 -0
- package/dist/react/workflows/UsageCredits.js +25 -0
- package/dist/react/workflows/UsageCredits.js.map +1 -0
- package/dist/react/workflows/UsageHistory.d.ts +40 -0
- package/dist/react/workflows/UsageHistory.d.ts.map +1 -0
- package/dist/react/workflows/UsageHistory.js +74 -0
- package/dist/react/workflows/UsageHistory.js.map +1 -0
- package/dist/react/workflows/UsageSummary.d.ts +19 -0
- package/dist/react/workflows/UsageSummary.d.ts.map +1 -0
- package/dist/react/workflows/UsageSummary.js +54 -0
- package/dist/react/workflows/UsageSummary.js.map +1 -0
- package/dist/react/workflows/usage-format.d.ts +11 -0
- package/dist/react/workflows/usage-format.d.ts.map +1 -0
- package/dist/react/workflows/usage-format.js +43 -0
- package/dist/react/workflows/usage-format.js.map +1 -0
- package/dist/react/workflows/usage-scope.d.ts +5 -0
- package/dist/react/workflows/usage-scope.d.ts.map +1 -0
- package/dist/react/workflows/usage-scope.js +26 -0
- package/dist/react/workflows/usage-scope.js.map +1 -0
- package/docs/README.md +4 -0
- package/docs/react-api.md +4 -0
- package/docs/usage-credits.md +6 -1
- package/docs/usage-react.md +182 -0
- package/docs/usage-security.md +130 -0
- package/package.json +3 -1
- package/src/client/usage/creditBackingIsolation.test.ts +110 -0
- package/src/client/usage/creditCapacity.test.ts +80 -1
- package/src/client/usage/creditCapacity.ts +10 -1
- package/src/client/usage/creditDelivery.ts +4 -0
- package/src/client/usage/creditLifecycle.ts +2 -0
- package/src/client/usage/creditNative.ts +5 -0
- package/src/client/usage/creditProofBounds.test.ts +187 -0
- package/src/client/usage/creditProvider.test.ts +74 -0
- package/src/client/usage/creditProvider.ts +2 -0
- package/src/client/usage/creditReconciliation.ts +18 -3
- package/src/client/usage/creditTransport.fixtures.ts +2 -1
- package/src/client/usage/index.ts +3 -0
- package/src/client/usage/subscriptionCancellation.ts +3 -2
- package/src/client/usage/subscriptionCheckout.ts +7 -2
- package/src/client/usage/subscriptionClient.test.ts +60 -1
- package/src/client/usage/subscriptionLifecycle.ts +3 -2
- package/src/client/usage/subscriptionProvider.ts +27 -3
- package/src/component/_generated/component.ts +40 -1
- package/src/component/usage/allowance.fixtures.ts +77 -0
- package/src/component/usage/allowance.test.ts +3 -66
- package/src/component/usage/allowanceBounds.test.ts +79 -0
- package/src/component/usage/historyIsolation.test.ts +124 -0
- package/src/component/usageCredits/accounting.fixtures.ts +96 -0
- package/src/component/usageCredits/accounting.test.ts +176 -82
- package/src/component/usageCredits/freshness.test.ts +33 -0
- package/src/component/usageCredits/helpers.ts +8 -0
- package/src/component/usageCredits/mutations.ts +83 -5
- package/src/component/usageCredits/quotas.test.ts +63 -0
- package/src/component/usageCredits/schema.ts +10 -0
- package/src/component/usageSubscriptions/history.test.ts +57 -0
- package/src/component/usageSubscriptions/history.ts +3 -1
- package/src/component/usageSubscriptions/mutations.ts +23 -0
- package/src/component/usageSubscriptions/schema.ts +1 -0
- package/src/react/index.ts +20 -0
- package/src/react/workflows/Pagination.tsx +2 -0
- package/src/react/workflows/Usage.fixtures.ts +88 -0
- package/src/react/workflows/Usage.test.tsx +373 -0
- package/src/react/workflows/Usage.types.ts +39 -0
- package/src/react/workflows/UsageAllowance.tsx +114 -0
- package/src/react/workflows/UsageBilling.tsx +90 -0
- package/src/react/workflows/UsageCredits.tsx +112 -0
- package/src/react/workflows/UsageHistory.test.tsx +206 -0
- package/src/react/workflows/UsageHistory.tsx +154 -0
- package/src/react/workflows/UsageSummary.tsx +204 -0
- package/src/react/workflows/usage-format.ts +48 -0
- package/src/react/workflows/usage-scope.ts +42 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { usageSummaryMatches } from "./usage-scope.js";
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
import type { UsageAvailability } from "../../client/usage/boundaries.js";
|
|
5
|
+
import type { UsageLocalSummary } from "../../client/usage/summary.js";
|
|
6
|
+
import type { UsageAggregateObservation } from "../../client/usage/observationTypes.js";
|
|
7
|
+
import type { UsageViewScope } from "./Usage.types.js";
|
|
8
|
+
import { formatUsageEstimate, formatUsageTime } from "./usage-format.js";
|
|
9
|
+
export type UsageSummaryProps = {
|
|
10
|
+
scope: UsageViewScope;
|
|
11
|
+
summary: UsageAvailability<UsageLocalSummary> | undefined;
|
|
12
|
+
provider?: UsageAggregateObservation;
|
|
13
|
+
className?: string;
|
|
14
|
+
children?: (state: {
|
|
15
|
+
summary: UsageSummaryProps["summary"];
|
|
16
|
+
provider: UsageSummaryProps["provider"];
|
|
17
|
+
isLoading: boolean;
|
|
18
|
+
isEmpty: boolean;
|
|
19
|
+
}) => ReactNode;
|
|
20
|
+
};
|
|
21
|
+
const models = {
|
|
22
|
+
pay_as_you_go: "Pay as you go",
|
|
23
|
+
included_overage: "Included monthly usage with overages",
|
|
24
|
+
prepaid: "Prepaid credits",
|
|
25
|
+
};
|
|
26
|
+
const deliveryLabels = {
|
|
27
|
+
queued: "Queued delivery",
|
|
28
|
+
sending: "Sending delivery",
|
|
29
|
+
provider_accepted: "Provider accepted",
|
|
30
|
+
rejected: "Rejected delivery",
|
|
31
|
+
unknown: "Unknown delivery",
|
|
32
|
+
held: "Held delivery",
|
|
33
|
+
canceled: "Canceled usage",
|
|
34
|
+
};
|
|
35
|
+
export function UsageSummary(props: UsageSummaryProps) {
|
|
36
|
+
const provider =
|
|
37
|
+
props.provider?.status === "available" &&
|
|
38
|
+
(props.provider.value.period.start !== props.scope.period.start ||
|
|
39
|
+
props.provider.value.period.end !== props.scope.period.end)
|
|
40
|
+
? {
|
|
41
|
+
status: "unavailable" as const,
|
|
42
|
+
reason: "observation_scope_mismatch",
|
|
43
|
+
retryable: true,
|
|
44
|
+
}
|
|
45
|
+
: props.provider;
|
|
46
|
+
const summary =
|
|
47
|
+
props.summary?.status === "available" &&
|
|
48
|
+
!usageSummaryMatches(props.scope, props.summary.value)
|
|
49
|
+
? {
|
|
50
|
+
status: "unavailable" as const,
|
|
51
|
+
reason: "observation_scope_mismatch",
|
|
52
|
+
retryable: true,
|
|
53
|
+
}
|
|
54
|
+
: props.summary;
|
|
55
|
+
const available = summary?.status === "available" ? summary.value : null;
|
|
56
|
+
if (props.children)
|
|
57
|
+
return (
|
|
58
|
+
<>
|
|
59
|
+
{props.children({
|
|
60
|
+
summary,
|
|
61
|
+
provider,
|
|
62
|
+
isLoading: summary === undefined,
|
|
63
|
+
isEmpty: !!available?.complete && available.eventCount === 0,
|
|
64
|
+
})}
|
|
65
|
+
</>
|
|
66
|
+
);
|
|
67
|
+
return (
|
|
68
|
+
<section
|
|
69
|
+
data-better-stripe="usage-summary"
|
|
70
|
+
className={props.className}
|
|
71
|
+
aria-label="Usage summary"
|
|
72
|
+
aria-busy={summary === undefined}
|
|
73
|
+
>
|
|
74
|
+
<h2>Usage by meter</h2>
|
|
75
|
+
{!summary ? (
|
|
76
|
+
<p role="status">Loading usage…</p>
|
|
77
|
+
) : summary.status !== "available" ? (
|
|
78
|
+
<p role="status">
|
|
79
|
+
Usage {summary.status}. Refresh when original usage evidence is
|
|
80
|
+
available.
|
|
81
|
+
</p>
|
|
82
|
+
) : (
|
|
83
|
+
<>
|
|
84
|
+
<p>
|
|
85
|
+
Period {formatUsageTime(summary.value.period.start)} to{" "}
|
|
86
|
+
{formatUsageTime(summary.value.period.end)}
|
|
87
|
+
</p>
|
|
88
|
+
{!summary.value.complete && (
|
|
89
|
+
<p role="status">Local history incomplete. Totals are partial.</p>
|
|
90
|
+
)}
|
|
91
|
+
{summary.value.complete && summary.value.eventCount === 0 && (
|
|
92
|
+
<p>No usage in this period.</p>
|
|
93
|
+
)}
|
|
94
|
+
{summary.value.groups.map((group) => (
|
|
95
|
+
<article
|
|
96
|
+
key={JSON.stringify([
|
|
97
|
+
group.binding.stripePriceId,
|
|
98
|
+
group.subscription.subscriptionItemId,
|
|
99
|
+
group.subscription.period,
|
|
100
|
+
])}
|
|
101
|
+
data-billing-model={group.binding.plan.model}
|
|
102
|
+
>
|
|
103
|
+
<h3>
|
|
104
|
+
{group.binding.plan.unit.name} ·{" "}
|
|
105
|
+
{models[group.binding.plan.model]}
|
|
106
|
+
</h3>
|
|
107
|
+
<p>
|
|
108
|
+
Plan {group.binding.plan.key}, version{" "}
|
|
109
|
+
{group.binding.plan.version}
|
|
110
|
+
</p>
|
|
111
|
+
<dl>
|
|
112
|
+
<dt>Accepted usage</dt>
|
|
113
|
+
<dd>
|
|
114
|
+
{group.acceptedQuantity} {group.binding.plan.unit.name}
|
|
115
|
+
</dd>
|
|
116
|
+
<dt>Billable after corrections</dt>
|
|
117
|
+
<dd>
|
|
118
|
+
{group.effectiveQuantity} {group.binding.plan.unit.name}
|
|
119
|
+
</dd>
|
|
120
|
+
<dt>Provider accepted</dt>
|
|
121
|
+
<dd>
|
|
122
|
+
{group.providerAcceptedQuantity}{" "}
|
|
123
|
+
{group.binding.plan.unit.name}
|
|
124
|
+
</dd>
|
|
125
|
+
{Object.entries(group.deliveryQuantities)
|
|
126
|
+
.filter(
|
|
127
|
+
([kind, quantity]) =>
|
|
128
|
+
kind !== "provider_accepted" &&
|
|
129
|
+
!/^0(?:\.0+)?$/.test(quantity),
|
|
130
|
+
)
|
|
131
|
+
.map(([kind, quantity]) => (
|
|
132
|
+
<div key={kind}>
|
|
133
|
+
<dt>
|
|
134
|
+
{deliveryLabels[kind as keyof typeof deliveryLabels]}
|
|
135
|
+
</dt>
|
|
136
|
+
<dd>
|
|
137
|
+
{quantity} {group.binding.plan.unit.name}
|
|
138
|
+
</dd>
|
|
139
|
+
</div>
|
|
140
|
+
))}
|
|
141
|
+
</dl>
|
|
142
|
+
{group.validationErrorEventCount > 0 && (
|
|
143
|
+
<p role="status">
|
|
144
|
+
Native validation rejected {group.validationErrorEventCount}{" "}
|
|
145
|
+
event(s).
|
|
146
|
+
</p>
|
|
147
|
+
)}
|
|
148
|
+
{group.estimate.status === "available" ? (
|
|
149
|
+
<p>
|
|
150
|
+
Estimated usage charge:{" "}
|
|
151
|
+
{formatUsageEstimate(
|
|
152
|
+
group.estimate.value.amountDecimal,
|
|
153
|
+
group.estimate.value.currency,
|
|
154
|
+
) ?? "Unavailable"}
|
|
155
|
+
</p>
|
|
156
|
+
) : (
|
|
157
|
+
<p>
|
|
158
|
+
Estimate {group.estimate.status}. Complete original usage and
|
|
159
|
+
pricing evidence is required.
|
|
160
|
+
</p>
|
|
161
|
+
)}
|
|
162
|
+
{group.included.status === "available" && (
|
|
163
|
+
<dl>
|
|
164
|
+
<dt>Included units</dt>
|
|
165
|
+
<dd>{group.included.value.included}</dd>
|
|
166
|
+
<dt>Remaining included units</dt>
|
|
167
|
+
<dd>{group.included.value.remaining}</dd>
|
|
168
|
+
<dt>Overage units</dt>
|
|
169
|
+
<dd>{group.included.value.overage}</dd>
|
|
170
|
+
</dl>
|
|
171
|
+
)}
|
|
172
|
+
</article>
|
|
173
|
+
))}
|
|
174
|
+
<p>
|
|
175
|
+
Estimate only. Finalized invoices and verified payments determine
|
|
176
|
+
billing.
|
|
177
|
+
</p>
|
|
178
|
+
</>
|
|
179
|
+
)}
|
|
180
|
+
{!provider || provider.status !== "available" ? (
|
|
181
|
+
<p role="status">
|
|
182
|
+
Native aggregation {provider?.status ?? "unavailable"}. Refresh after
|
|
183
|
+
provider ingestion.
|
|
184
|
+
</p>
|
|
185
|
+
) : (
|
|
186
|
+
<>
|
|
187
|
+
<p>Native aggregate: {provider.value.quantity}</p>
|
|
188
|
+
{provider.value.stale && (
|
|
189
|
+
<p role="status">Native aggregation stale.</p>
|
|
190
|
+
)}
|
|
191
|
+
{!provider.value.complete && (
|
|
192
|
+
<p role="status">
|
|
193
|
+
Native aggregation partial. More provider pages remain.
|
|
194
|
+
</p>
|
|
195
|
+
)}
|
|
196
|
+
<p>
|
|
197
|
+
Aggregation may be delayed. Provider acceptance does not prove
|
|
198
|
+
ingestion or payment.
|
|
199
|
+
</p>
|
|
200
|
+
</>
|
|
201
|
+
)}
|
|
202
|
+
</section>
|
|
203
|
+
);
|
|
204
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { getChargeCurrencyMetadata } from "../../client/charge-currency.js";
|
|
2
|
+
/** Native metered rates/estimates use minor currency decimals. Preserve fractional cents and large quantities. */
|
|
3
|
+
export function formatUsageEstimate(
|
|
4
|
+
amountDecimal: string,
|
|
5
|
+
currency: string,
|
|
6
|
+
): string | null {
|
|
7
|
+
const metadata = getChargeCurrencyMetadata(currency);
|
|
8
|
+
if (!metadata || !/^\d+(?:\.\d+)?$/.test(amountDecimal)) return null;
|
|
9
|
+
const [whole, fraction = ""] = amountDecimal.split(".");
|
|
10
|
+
const digits = (whole + fraction).replace(/^0+(?=\d)/, "");
|
|
11
|
+
const scale = metadata.apiDecimals + fraction.length;
|
|
12
|
+
const padded = digits.padStart(scale + 1, "0");
|
|
13
|
+
const major = scale ? padded.slice(0, -scale) : padded;
|
|
14
|
+
let minor = scale ? padded.slice(-scale).replace(/0+$/, "") : "";
|
|
15
|
+
if (metadata.displayDecimals === 0 && minor) return null;
|
|
16
|
+
minor = minor.padEnd(metadata.displayDecimals, "0");
|
|
17
|
+
return `${major}${minor ? "." + minor : ""} ${metadata.currency.toUpperCase()}`;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Normalize only the visual progress position; keep all visible quantities exact. */
|
|
21
|
+
export function allowanceProgress(value: {
|
|
22
|
+
consumedQuantity: string;
|
|
23
|
+
reservedQuantity: string;
|
|
24
|
+
includedQuantity: string;
|
|
25
|
+
}): number | null {
|
|
26
|
+
const quantities = [
|
|
27
|
+
value.consumedQuantity,
|
|
28
|
+
value.reservedQuantity,
|
|
29
|
+
value.includedQuantity,
|
|
30
|
+
];
|
|
31
|
+
if (quantities.some((quantity) => !/^\d{1,18}(?:\.\d{1,6})?$/.test(quantity)))
|
|
32
|
+
return null;
|
|
33
|
+
const [consumed, reserved, included] = quantities.map((quantity) => {
|
|
34
|
+
const [whole, fraction = ""] = quantity.split(".");
|
|
35
|
+
return BigInt(whole + fraction.padEnd(6, "0"));
|
|
36
|
+
});
|
|
37
|
+
if (included === 0n) return null;
|
|
38
|
+
const ratio = ((consumed + reserved) * 10000n) / included;
|
|
39
|
+
return Number(ratio > 10000n ? 10000n : ratio);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Numeric usage periods can exceed Date's supported display range. */
|
|
43
|
+
export function formatUsageTime(seconds: number): string {
|
|
44
|
+
const value = new Date(seconds * 1000);
|
|
45
|
+
return Number.isFinite(value.getTime())
|
|
46
|
+
? value.toISOString()
|
|
47
|
+
: "Time unavailable";
|
|
48
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { UsageBillingSnapshot, UsageViewScope } from "./Usage.types.js";
|
|
2
|
+
import type { UsageLocalSummary } from "../../client/usage/summary.js";
|
|
3
|
+
export function usageSummaryMatches(
|
|
4
|
+
scope: UsageViewScope,
|
|
5
|
+
value: UsageLocalSummary,
|
|
6
|
+
) {
|
|
7
|
+
return (
|
|
8
|
+
value.period.start === scope.period.start &&
|
|
9
|
+
value.period.end === scope.period.end &&
|
|
10
|
+
value.groups.every(
|
|
11
|
+
(group) =>
|
|
12
|
+
group.binding.plan.rate.currency.toLowerCase() ===
|
|
13
|
+
scope.currency.toLowerCase() &&
|
|
14
|
+
(group.estimate.status !== "available" ||
|
|
15
|
+
group.estimate.value.currency.toLowerCase() ===
|
|
16
|
+
scope.currency.toLowerCase()),
|
|
17
|
+
)
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
export function usageSnapshotMatches(
|
|
21
|
+
scope: UsageViewScope,
|
|
22
|
+
value: UsageBillingSnapshot,
|
|
23
|
+
) {
|
|
24
|
+
const periodMatches = (period: { start: number; end: number }) =>
|
|
25
|
+
period.start === scope.period.start && period.end === scope.period.end;
|
|
26
|
+
const currencyMatches = (currency: string) =>
|
|
27
|
+
currency.toLowerCase() === scope.currency.toLowerCase();
|
|
28
|
+
return (
|
|
29
|
+
(value.summary.status !== "available" ||
|
|
30
|
+
usageSummaryMatches(scope, value.summary.value)) &&
|
|
31
|
+
(value.provider?.status !== "available" ||
|
|
32
|
+
periodMatches(value.provider.value.period)) &&
|
|
33
|
+
(value.allowance?.status !== "available" ||
|
|
34
|
+
!value.allowance.value ||
|
|
35
|
+
(periodMatches(value.allowance.value.period) &&
|
|
36
|
+
currencyMatches(value.allowance.value.committedEstimate.currency) &&
|
|
37
|
+
currencyMatches(value.allowance.value.projectedEstimate.currency))) &&
|
|
38
|
+
(value.credits?.status !== "available" ||
|
|
39
|
+
currencyMatches(value.credits.value.currency)) &&
|
|
40
|
+
(!value.purchase || currencyMatches(value.purchase.currency))
|
|
41
|
+
);
|
|
42
|
+
}
|