@hjam91/billing 0.7.0 → 0.7.1
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 +12 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +14 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,6 +19,18 @@ npm install @hjam91/billing
|
|
|
19
19
|
|
|
20
20
|
`hono` and `react` are optional peer dependencies, needed only for `@hjam91/billing/hono` and `@hjam91/billing/react`.
|
|
21
21
|
|
|
22
|
+
## 0.7.1 fixes
|
|
23
|
+
|
|
24
|
+
`plans()` can include the service's current `trial_days` and `trial_mode`, so apps
|
|
25
|
+
can display trial terms from their billing settings. These fields are optional
|
|
26
|
+
for compatibility with older services.
|
|
27
|
+
|
|
28
|
+
- Cached entitlement reads respect `maxStaleSeconds`, including ordinary reads without `fresh`.
|
|
29
|
+
- Feature checks clear cached access after authentication failures or a kill-switch denial, so later reads cannot restore the rejected access.
|
|
30
|
+
- Malformed successful entitlement/status responses become `BillingError(502, 'invalid_response')` and never replace a valid cached entitlement. Entitlement checks retain the configured outage policy.
|
|
31
|
+
|
|
32
|
+
No API migration is required. The default fail-open and kill-switch policies are unchanged.
|
|
33
|
+
|
|
22
34
|
## Quick start (server)
|
|
23
35
|
|
|
24
36
|
The client holds your app's secret: **server-side only**.
|
package/dist/index.d.ts
CHANGED
|
@@ -318,6 +318,8 @@ export interface Billing {
|
|
|
318
318
|
plans(): Promise<{
|
|
319
319
|
plans: PublicPlan[];
|
|
320
320
|
default_plan: string | null;
|
|
321
|
+
trial_days?: number;
|
|
322
|
+
trial_mode?: 'explicit' | 'on_first_seen';
|
|
321
323
|
}>;
|
|
322
324
|
}
|
|
323
325
|
/** Options of changePlan / previewChange (since 0.7.0). */
|
package/dist/index.js
CHANGED
|
@@ -100,11 +100,19 @@ export function createBilling(opts) {
|
|
|
100
100
|
throw new BillingError(0, 'network_error', `Billing service unreachable: ${err?.message ?? err}`);
|
|
101
101
|
}
|
|
102
102
|
const data = (await res.json().catch(() => null));
|
|
103
|
-
if (!res.ok
|
|
103
|
+
if (!res.ok) {
|
|
104
104
|
throw new BillingError(res.status, data?.error ?? 'internal', data?.message ?? `Billing service answered HTTP ${res.status}`, (data ?? {}));
|
|
105
105
|
}
|
|
106
|
+
if (!data || typeof data !== 'object' || Array.isArray(data)) {
|
|
107
|
+
throw new BillingError(502, 'invalid_response', 'Billing service returned an invalid JSON object');
|
|
108
|
+
}
|
|
106
109
|
return data;
|
|
107
110
|
}
|
|
111
|
+
function validateEntitlement(value) {
|
|
112
|
+
if (!value || typeof value !== 'object' || Array.isArray(value) || typeof value.active !== 'boolean' || typeof value.reason !== 'string') {
|
|
113
|
+
throw new BillingError(502, 'invalid_response', 'Billing service returned an invalid entitlement');
|
|
114
|
+
}
|
|
115
|
+
}
|
|
108
116
|
const enc = encodeURIComponent;
|
|
109
117
|
/** One network fetch per account at a time; concurrent callers share it. */
|
|
110
118
|
function refresh(accountId) {
|
|
@@ -113,6 +121,7 @@ export function createBilling(opts) {
|
|
|
113
121
|
return existing;
|
|
114
122
|
const p = call('GET', `/v1/accounts/${enc(accountId)}/entitlement`)
|
|
115
123
|
.then(async (value) => {
|
|
124
|
+
validateEntitlement(value);
|
|
116
125
|
await cache.set(key(accountId), { value, fetchedAt: now() }).catch((e) => log.warn('[billing] cache write failed', e));
|
|
117
126
|
return value;
|
|
118
127
|
})
|
|
@@ -288,10 +297,12 @@ export function createBilling(opts) {
|
|
|
288
297
|
const code = String(err?.code ?? 'error');
|
|
289
298
|
const kill = killSignal(err);
|
|
290
299
|
if (kill && killDeny) {
|
|
300
|
+
await cache.delete(key(accountId)).catch(() => undefined);
|
|
291
301
|
reportFailure(accountId, err, `DENY feature ${featureId}`, 'deny');
|
|
292
302
|
return unverifiedCheck(accountId, featureId, false, 'error', code);
|
|
293
303
|
}
|
|
294
304
|
if (!kill && !isOutage(err) && !failOpenOnAuth) {
|
|
305
|
+
await cache.delete(key(accountId)).catch(() => undefined);
|
|
295
306
|
reportFailure(accountId, err, `DENY for feature ${featureId} (misconfigured: auth/app/feature error is not an outage)`, 'deny');
|
|
296
307
|
return unverifiedCheck(accountId, featureId, false, 'error', code);
|
|
297
308
|
}
|
|
@@ -351,7 +362,7 @@ export function createBilling(opts) {
|
|
|
351
362
|
log.warn('[billing] cache read failed', e);
|
|
352
363
|
return undefined;
|
|
353
364
|
});
|
|
354
|
-
if (entry && !callOpts.fresh) {
|
|
365
|
+
if (entry && !callOpts.fresh && now() - entry.fetchedAt < maxStaleMs) {
|
|
355
366
|
if (now() - entry.fetchedAt < ttlMs)
|
|
356
367
|
return { ...entry.value, source: 'cache' };
|
|
357
368
|
// Stale-while-revalidate: answer now from the last known value, refresh behind it.
|
|
@@ -538,6 +549,7 @@ export function createBilling(opts) {
|
|
|
538
549
|
},
|
|
539
550
|
async status(accountId, o = {}) {
|
|
540
551
|
const s = await call('GET', `/v1/accounts/${enc(accountId)}`, undefined, o.email ? { 'X-Billing-Email': o.email } : {});
|
|
552
|
+
validateEntitlement(s.entitlement);
|
|
541
553
|
await cache.set(key(accountId), { value: { account_id: accountId, ...s.entitlement }, fetchedAt: now() }).catch(() => undefined);
|
|
542
554
|
return s;
|
|
543
555
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hjam91/billing",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "Zero-dependency client for a billing-kit service: cached, fail-open entitlement and feature checks, usage tracking, Stripe Checkout / portal, signed webhook verification, a Hono gate and an optional React billing card.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"billing",
|