@voltro/plugin-billing 0.84.0 → 0.86.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 +1215 -0
- package/README.md +7 -0
- package/THIRD-PARTY-NOTICES.md +118 -600
- package/dist/catalog.js +471 -0
- package/dist/dunning.js +209 -0
- package/dist/entitlement.js +41 -0
- package/dist/flushClaim.js +20 -0
- package/dist/index.d.ts +19 -24
- package/dist/index.js +158 -2776
- package/dist/metering.js +74 -0
- package/dist/plans.js +24 -0
- package/dist/providers.js +133 -0
- package/dist/provision.js +153 -0
- package/dist/rpc.d.ts +72 -73
- package/dist/service.js +615 -0
- package/dist/stores.js +425 -0
- package/dist/stripeClient.js +53 -0
- package/dist/stripeProvider.js +352 -0
- package/dist/stripeRest.js +52 -0
- package/dist/table.js +54 -0
- package/dist/types.d.ts +19 -24
- package/dist/types.js +1 -1
- package/dist/webhook.js +45 -0
- package/package.json +7 -7
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { BillingService as e, EntitlementExceeded as t, SubscriptionLocked as n } from "./types.js";
|
|
2
|
+
import { Effect as r } from "effect";
|
|
3
|
+
//#region src/entitlement.ts
|
|
4
|
+
var i = (e, t, n, r) => ({
|
|
5
|
+
allowed: !Number.isFinite(t) || r <= 0 || n + r <= t,
|
|
6
|
+
entitlement: e,
|
|
7
|
+
limit: t,
|
|
8
|
+
used: n,
|
|
9
|
+
cost: r
|
|
10
|
+
}), a = (e) => e.tenantId, o = (n, i, o) => r.gen(function* () {
|
|
11
|
+
let s = a(n.request.subject);
|
|
12
|
+
if (s === null) return o <= 0 ? void 0 : yield* r.fail(new t({
|
|
13
|
+
entitlement: i,
|
|
14
|
+
limit: 0,
|
|
15
|
+
used: 0,
|
|
16
|
+
cost: o
|
|
17
|
+
}));
|
|
18
|
+
yield* (yield* e).consumeEntitlement(s, i, o);
|
|
19
|
+
}), s = (t) => r.gen(function* () {
|
|
20
|
+
let i = a(t.request.subject);
|
|
21
|
+
if (i === null) return;
|
|
22
|
+
let o = yield* (yield* e).entitlementStatus(i);
|
|
23
|
+
if (!o.entitled) return yield* r.fail(new n({
|
|
24
|
+
tenantId: i,
|
|
25
|
+
status: o.status,
|
|
26
|
+
lockedSince: (o.lockedSince ?? /* @__PURE__ */ new Date()).toISOString(),
|
|
27
|
+
lockout: o.lockout
|
|
28
|
+
}));
|
|
29
|
+
}), c = (e, n) => (i, a) => {
|
|
30
|
+
let o = e[a.tag];
|
|
31
|
+
if (o === void 0) return i;
|
|
32
|
+
let s = o.cost ?? 1, c = a.subject.tenantId;
|
|
33
|
+
return c === null ? s <= 0 ? i : r.fail(new t({
|
|
34
|
+
entitlement: o.entitlement,
|
|
35
|
+
limit: 0,
|
|
36
|
+
used: 0,
|
|
37
|
+
cost: s
|
|
38
|
+
})) : r.flatMap(n.consumeEntitlement(c, o.entitlement, s), () => i);
|
|
39
|
+
};
|
|
40
|
+
//#endregion
|
|
41
|
+
export { c as buildEnforceInterceptor, i as evaluateEntitlement, s as requireEntitled, o as requireEntitlement };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { id as e, table as t, text as n, timestamp as r } from "@voltro/database";
|
|
2
|
+
//#region src/flushClaim.ts
|
|
3
|
+
var i = "_voltro_billing_flush_claims", a = t(i, {
|
|
4
|
+
id: e({ scheme: "numeric" }),
|
|
5
|
+
windowKey: n(),
|
|
6
|
+
claimedBy: n(),
|
|
7
|
+
claimedAt: r().default("now")
|
|
8
|
+
}).unique("billingFlushClaimWindow", ["windowKey"]), o = (e, t) => `billing-flush:${Math.floor(e / Math.max(1, t))}`, s = async (e, t, n, r) => {
|
|
9
|
+
try {
|
|
10
|
+
return (await e.insertIgnore(i, {
|
|
11
|
+
windowKey: t,
|
|
12
|
+
claimedBy: n,
|
|
13
|
+
claimedAt: new Date(r)
|
|
14
|
+
}, { conflictColumns: ["windowKey"] })).claimedBy === n;
|
|
15
|
+
} catch {
|
|
16
|
+
return !0;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
20
|
+
export { i as BILLING_FLUSH_CLAIMS_TABLE, a as billingFlushClaimsTable, s as claimFlushWindow, o as flushWindowKey };
|
package/dist/index.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ import { Subject } from '@voltro/protocol';
|
|
|
16
16
|
import { TableIndex } from '@voltro/database';
|
|
17
17
|
import { TableLike } from '@voltro/database';
|
|
18
18
|
import { VoltroPlugin } from '@voltro/protocol';
|
|
19
|
+
import { YieldableError } from 'effect/Cause';
|
|
19
20
|
|
|
20
21
|
export declare const assertBillingProductionReady: (input: {
|
|
21
22
|
readonly env: NodeJS.ProcessEnv;
|
|
@@ -93,15 +94,13 @@ export declare const billingEgressOf: (provider: Pick<BillingProvider, "name">)
|
|
|
93
94
|
export declare class BillingError extends BillingError_base {
|
|
94
95
|
}
|
|
95
96
|
|
|
96
|
-
declare const BillingError_base: Schema.
|
|
97
|
-
readonly _tag: Schema.tag<"BillingError">;
|
|
98
|
-
} & {
|
|
97
|
+
declare const BillingError_base: Schema.Class<BillingError, Schema.TaggedStruct<"BillingError", {
|
|
99
98
|
/** Where the failure originated — provider name or an internal stage. */
|
|
100
|
-
source:
|
|
101
|
-
message:
|
|
99
|
+
readonly source: Schema.String;
|
|
100
|
+
readonly message: Schema.String;
|
|
102
101
|
/** Retryable? The service retries `transient` failures on a Schedule. */
|
|
103
|
-
transient:
|
|
104
|
-
}>;
|
|
102
|
+
readonly transient: Schema.Boolean;
|
|
103
|
+
}>, YieldableError>;
|
|
105
104
|
|
|
106
105
|
/**
|
|
107
106
|
* The normalized event union a `BillingProvider.normalizeEvent` produces
|
|
@@ -420,7 +419,7 @@ export declare type BillingProviderName = 'stripe' | 'mock';
|
|
|
420
419
|
export declare class BillingService extends BillingService_base {
|
|
421
420
|
}
|
|
422
421
|
|
|
423
|
-
declare const BillingService_base: Context.
|
|
422
|
+
declare const BillingService_base: Context.ServiceClass<BillingService, "@voltro/plugin-billing/BillingService", BillingServiceShape>;
|
|
424
423
|
|
|
425
424
|
/** Wrap a built service in a `Layer` for the plugin's `services` field. */
|
|
426
425
|
export declare const billingServiceLayer: (service: BillingServiceShape) => Layer.Layer<BillingService, never, never>;
|
|
@@ -1012,18 +1011,16 @@ export declare interface EntitlementDecision {
|
|
|
1012
1011
|
export declare class EntitlementExceeded extends EntitlementExceeded_base {
|
|
1013
1012
|
}
|
|
1014
1013
|
|
|
1015
|
-
declare const EntitlementExceeded_base: Schema.
|
|
1016
|
-
readonly _tag: Schema.tag<"EntitlementExceeded">;
|
|
1017
|
-
} & {
|
|
1014
|
+
declare const EntitlementExceeded_base: Schema.Class<EntitlementExceeded, Schema.TaggedStruct<"EntitlementExceeded", {
|
|
1018
1015
|
/** The entitlement key that was exhausted (`'aiCalls'`). */
|
|
1019
|
-
entitlement:
|
|
1016
|
+
readonly entitlement: Schema.String;
|
|
1020
1017
|
/** The static limit for the caller's plan. */
|
|
1021
|
-
limit:
|
|
1018
|
+
readonly limit: Schema.Number;
|
|
1022
1019
|
/** How much was already used in the current window. */
|
|
1023
|
-
used:
|
|
1020
|
+
readonly used: Schema.Number;
|
|
1024
1021
|
/** The cost the rejected call would have charged. */
|
|
1025
|
-
cost:
|
|
1026
|
-
}>;
|
|
1022
|
+
readonly cost: Schema.Number;
|
|
1023
|
+
}>, YieldableError>;
|
|
1027
1024
|
|
|
1028
1025
|
/** An entitlement limit: a finite quota or the unbounded `'unlimited'`. */
|
|
1029
1026
|
export declare type EntitlementLimit = number | 'unlimited';
|
|
@@ -1839,17 +1836,15 @@ declare interface SubscriptionChangeResult {
|
|
|
1839
1836
|
export declare class SubscriptionLocked extends SubscriptionLocked_base {
|
|
1840
1837
|
}
|
|
1841
1838
|
|
|
1842
|
-
declare const SubscriptionLocked_base: Schema.
|
|
1843
|
-
readonly
|
|
1844
|
-
} & {
|
|
1845
|
-
tenantId: typeof Schema.String;
|
|
1839
|
+
declare const SubscriptionLocked_base: Schema.Class<SubscriptionLocked, Schema.TaggedStruct<"SubscriptionLocked", {
|
|
1840
|
+
readonly tenantId: Schema.String;
|
|
1846
1841
|
/** The subscription status at the time of the refusal (`'pastDue'`). */
|
|
1847
|
-
status:
|
|
1842
|
+
readonly status: Schema.String;
|
|
1848
1843
|
/** ISO timestamp at which the grace period expired. */
|
|
1849
|
-
lockedSince:
|
|
1844
|
+
readonly lockedSince: Schema.String;
|
|
1850
1845
|
/** `'hard'` (limits fell to the free plan) or `'soft'` (report only). */
|
|
1851
|
-
lockout:
|
|
1852
|
-
}>;
|
|
1846
|
+
readonly lockout: Schema.String;
|
|
1847
|
+
}>, YieldableError>;
|
|
1853
1848
|
|
|
1854
1849
|
/** Fields a mid-cycle / reconcile patch may set on the tenant's row. */
|
|
1855
1850
|
export declare interface SubscriptionPatch {
|