@fantastic.dev/better-stripe 0.50.3 → 0.52.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.
Files changed (88) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +5 -1
  3. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +166 -32
  4. package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
  5. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +83 -16
  6. package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
  7. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +134 -0
  8. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
  9. package/dist/client/connect/payoutSyncTestHelpers.d.ts +67 -0
  10. package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
  11. package/dist/client/index.d.ts +3 -0
  12. package/dist/client/index.d.ts.map +1 -1
  13. package/dist/client/index.js +4 -0
  14. package/dist/client/index.js.map +1 -1
  15. package/dist/client/usage/boundaries.d.ts +132 -0
  16. package/dist/client/usage/boundaries.d.ts.map +1 -0
  17. package/dist/client/usage/boundaries.js +43 -0
  18. package/dist/client/usage/boundaries.js.map +1 -0
  19. package/dist/client/usage/client.d.ts +37 -0
  20. package/dist/client/usage/client.d.ts.map +1 -0
  21. package/dist/client/usage/client.js +59 -0
  22. package/dist/client/usage/client.js.map +1 -0
  23. package/dist/client/usage/contracts.d.ts +59 -0
  24. package/dist/client/usage/contracts.d.ts.map +1 -0
  25. package/dist/client/usage/contracts.js +63 -0
  26. package/dist/client/usage/contracts.js.map +1 -0
  27. package/dist/client/usage/index.d.ts +7 -0
  28. package/dist/client/usage/index.d.ts.map +1 -0
  29. package/dist/client/usage/index.js +4 -0
  30. package/dist/client/usage/index.js.map +1 -0
  31. package/dist/client/usage/records.d.ts +50 -0
  32. package/dist/client/usage/records.d.ts.map +1 -0
  33. package/dist/client/usage/records.js +54 -0
  34. package/dist/client/usage/records.js.map +1 -0
  35. package/dist/component/_generated/api.d.ts +6 -0
  36. package/dist/component/_generated/api.d.ts.map +1 -1
  37. package/dist/component/_generated/api.js.map +1 -1
  38. package/dist/component/_generated/component.d.ts +79 -0
  39. package/dist/component/_generated/component.d.ts.map +1 -1
  40. package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
  41. package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
  42. package/dist/component/earnings/triggers.d.ts +178 -16
  43. package/dist/component/earnings/triggers.d.ts.map +1 -1
  44. package/dist/component/payoutRelease/queries.d.ts +2 -2
  45. package/dist/component/payoutRelease/queries.d.ts.map +1 -1
  46. package/dist/component/schema.d.ts +67 -0
  47. package/dist/component/schema.js +5 -0
  48. package/dist/component/schema.js.map +1 -1
  49. package/dist/component/usage/helpers.d.ts +13 -0
  50. package/dist/component/usage/helpers.d.ts.map +1 -0
  51. package/dist/component/usage/helpers.js +34 -0
  52. package/dist/component/usage/helpers.js.map +1 -0
  53. package/dist/component/usage/mutations.d.ts +14 -0
  54. package/dist/component/usage/mutations.d.ts.map +1 -0
  55. package/dist/component/usage/mutations.js +124 -0
  56. package/dist/component/usage/mutations.js.map +1 -0
  57. package/dist/component/usage/queries.d.ts +62 -0
  58. package/dist/component/usage/queries.d.ts.map +1 -0
  59. package/dist/component/usage/queries.js +65 -0
  60. package/dist/component/usage/queries.js.map +1 -0
  61. package/dist/component/usage/schema.d.ts +110 -0
  62. package/dist/component/usage/schema.d.ts.map +1 -0
  63. package/dist/component/usage/schema.js +54 -0
  64. package/dist/component/usage/schema.js.map +1 -0
  65. package/docs/README.md +7 -0
  66. package/docs/development.md +26 -8
  67. package/docs/metered-usage.md +289 -37
  68. package/docs/metering-setup.md +184 -0
  69. package/docs/usage-ledger.md +62 -0
  70. package/package.json +8 -2
  71. package/src/client/index.ts +5 -0
  72. package/src/client/transferContinuation.test.ts +2 -6
  73. package/src/client/usage/boundaries.test.ts +136 -0
  74. package/src/client/usage/boundaries.ts +182 -0
  75. package/src/client/usage/client.ts +97 -0
  76. package/src/client/usage/contracts.test.ts +145 -0
  77. package/src/client/usage/contracts.ts +79 -0
  78. package/src/client/usage/index.ts +34 -0
  79. package/src/client/usage/ledger-client.test.ts +74 -0
  80. package/src/client/usage/records.ts +76 -0
  81. package/src/component/_generated/api.ts +6 -0
  82. package/src/component/_generated/component.ts +106 -0
  83. package/src/component/schema.ts +10 -0
  84. package/src/component/usage/helpers.ts +33 -0
  85. package/src/component/usage/ledger.test.ts +310 -0
  86. package/src/component/usage/mutations.ts +139 -0
  87. package/src/component/usage/queries.ts +69 -0
  88. package/src/component/usage/schema.ts +65 -0
@@ -0,0 +1,124 @@
1
+ import { v } from "convex/values";
2
+ import { mutation } from "../_generated/server.js";
3
+ import { prepareUsageRecord, } from "../../client/usage/records.js";
4
+ import { defineUsagePlan } from "../../client/usage/contracts.js";
5
+ import { assertUsageReplay } from "../../client/usage/boundaries.js";
6
+ import { scopeKey, subscriptionSnapshot } from "./helpers.js";
7
+ /** Trusted host mutation. Authenticate, authorize scope and verify native binding first. */
8
+ export const record = mutation({
9
+ args: {
10
+ inputJson: v.string(),
11
+ subscriptionJson: v.string(),
12
+ dispatch: v.optional(v.string()),
13
+ reservationId: v.optional(v.id("usageReservations")),
14
+ quota: v.optional(v.number()),
15
+ },
16
+ returns: v.object({
17
+ eventId: v.id("usageEvents"),
18
+ identity: v.string(),
19
+ acceptedAt: v.number(),
20
+ delivery: v.literal("queued"),
21
+ }),
22
+ handler: async (ctx, args) => {
23
+ if (new TextEncoder().encode(args.inputJson).length > 8192)
24
+ throw new Error("Usage payload exceeds size limit");
25
+ const event = prepareUsageRecord(JSON.parse(args.inputJson));
26
+ const key = scopeKey(event.scope);
27
+ const subscription = subscriptionSnapshot(args.subscriptionJson);
28
+ const subscriptionJson = JSON.stringify(subscription);
29
+ const existing = await ctx.db
30
+ .query("usageEvents")
31
+ .withIndex("by_identity", (q) => q.eq("identity", event.identity))
32
+ .unique();
33
+ if (existing) {
34
+ if (args.reservationId !== undefined &&
35
+ args.reservationId !== existing.reservationId)
36
+ throw new Error("Conflicting usage reservation replay");
37
+ assertUsageReplay(JSON.parse(existing.recordJson), event);
38
+ if (existing.subscriptionJson !== subscriptionJson)
39
+ throw new Error("Conflicting usage replay");
40
+ const outbox = await ctx.db
41
+ .query("usageOutbox")
42
+ .withIndex("by_event", (q) => q.eq("eventId", existing._id))
43
+ .unique();
44
+ if (!outbox)
45
+ throw new Error("Usage outbox invariant violated");
46
+ // Acceptance is an immutable receipt; subsequent delivery is read separately.
47
+ return {
48
+ eventId: existing._id,
49
+ identity: existing.identity,
50
+ acceptedAt: existing.acceptedAt,
51
+ delivery: "queued",
52
+ };
53
+ }
54
+ const now = Math.floor(Date.now() / 1000);
55
+ if (event.occurredAt < now - 35 * 86400 || event.occurredAt > now + 300)
56
+ throw new Error("Usage occurrence is outside the native delivery window");
57
+ if (event.binding.stripePriceId !== subscription.stripePriceId ||
58
+ event.occurredAt < subscription.period.start ||
59
+ event.occurredAt >= subscription.period.end)
60
+ throw new Error("Usage does not match its subscription period/price");
61
+ const quota = args.quota ?? 100000;
62
+ if (!Number.isInteger(quota) || quota < 1 || quota > 100000)
63
+ throw new Error("Invalid usage scope quota");
64
+ const counter = await ctx.db
65
+ .query("usageScopeCounters")
66
+ .withIndex("by_scope", (q) => q.eq("scopeKey", key))
67
+ .unique();
68
+ if ((counter?.count ?? 0) >= quota)
69
+ throw new Error("Usage scope quota exhausted");
70
+ const reservation = args.reservationId
71
+ ? await ctx.db.get("usageReservations", args.reservationId)
72
+ : null;
73
+ if (args.reservationId !== undefined && !reservation)
74
+ throw new Error("Usage reservation is unavailable");
75
+ if (event.binding.plan.model === "prepaid" && !reservation)
76
+ throw new Error("A matching usage reservation is required");
77
+ if (reservation &&
78
+ (reservation.scopeKey !== key ||
79
+ reservation.sourceEventId !== event.sourceEventId ||
80
+ JSON.stringify(defineUsagePlan(JSON.parse(reservation.planJson))) !==
81
+ JSON.stringify(event.binding.plan) ||
82
+ reservation.scaledQuantity !== event.scaledQuantity ||
83
+ reservation.state !== "reserved"))
84
+ throw new Error("Usage reservation does not match event");
85
+ const eventId = await ctx.db.insert("usageEvents", {
86
+ reservationId: args.reservationId,
87
+ scopeKey: key,
88
+ identity: event.identity,
89
+ recordJson: JSON.stringify(event),
90
+ subscriptionJson,
91
+ occurredAt: event.occurredAt,
92
+ acceptedAt: now,
93
+ });
94
+ await ctx.db.insert("usageOutbox", {
95
+ eventId,
96
+ scopeKey: key,
97
+ state: "queued",
98
+ attempts: 0,
99
+ nextAttemptAt: now,
100
+ providerIdentifier: `bs_usage_${eventId}`,
101
+ dispatch: args.dispatch,
102
+ });
103
+ if (counter)
104
+ await ctx.db.patch("usageScopeCounters", counter._id, {
105
+ count: counter.count + 1,
106
+ });
107
+ else
108
+ await ctx.db.insert("usageScopeCounters", { scopeKey: key, count: 1 });
109
+ if (reservation)
110
+ await ctx.db.patch("usageReservations", reservation._id, {
111
+ state: "committed",
112
+ eventId,
113
+ });
114
+ if (args.dispatch)
115
+ await ctx.scheduler.runAfter(0, args.dispatch, { eventId });
116
+ return {
117
+ eventId,
118
+ identity: event.identity,
119
+ acceptedAt: now,
120
+ delivery: "queued",
121
+ };
122
+ },
123
+ });
124
+ //# sourceMappingURL=mutations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutations.js","sourceRoot":"","sources":["../../../src/component/usage/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,EACL,kBAAkB,GAEnB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AACrE,OAAO,EAAE,QAAQ,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAE9D,4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AC,CAAC;;YACA,MAAM,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,oBAAoB,EAAE,EAAE,QAAQ,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;QAC5E,IAAI,WAAW;YACb,MAAM,GAAG,CAAC,EAAE,CAAC,KAAK,CAAC,mBAAmB,EAAE,WAAW,CAAC,GAAG,EAAE;gBACvD,KAAK,EAAE,WAAW;gBAClB,OAAO;aACR,CAAC,CAAC;QACL,IAAI,IAAI,CAAC,QAAQ;YACf,MAAM,GAAG,CAAC,SAAS,CAAC,QAAQ,CAC1B,CAAC,EACD,IAAI,CAAC,QAAoC,EACzC,EAAE,OAAO,EAAE,CACZ,CAAC;QACJ,OAAO;YACL,OAAO;YACP,QAAQ,EAAE,KAAK,CAAC,QAAQ;YACxB,UAAU,EAAE,GAAG;YACf,QAAQ,EAAE,QAAiB;SAC5B,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
@@ -0,0 +1,62 @@
1
+ export declare const get: import("convex/server").RegisteredQuery<"public", {
2
+ eventId: import("convex/values").GenericId<"usageEvents">;
3
+ scope: {
4
+ customerAccount: string;
5
+ livemode: boolean;
6
+ meterId: string;
7
+ stripeAccountId: string;
8
+ };
9
+ }, Promise<{
10
+ event: {
11
+ _creationTime: number;
12
+ _id: import("convex/values").GenericId<"usageEvents">;
13
+ acceptedAt: number;
14
+ identity: string;
15
+ occurredAt: number;
16
+ recordJson: string;
17
+ reservationId?: import("convex/values").GenericId<"usageReservations"> | undefined;
18
+ scopeKey: string;
19
+ subscriptionJson: string;
20
+ };
21
+ delivery: "held" | "provider_accepted" | "queued" | "rejected" | "sending" | "unknown";
22
+ } | null>>;
23
+ export declare const read: import("convex/server").RegisteredQuery<"public", {
24
+ paginationOpts: {
25
+ id?: number;
26
+ endCursor?: string | null;
27
+ maximumRowsRead?: number;
28
+ maximumBytesRead?: number;
29
+ numItems: number;
30
+ cursor: string | null;
31
+ };
32
+ period: {
33
+ end: number;
34
+ start: number;
35
+ };
36
+ scope: {
37
+ customerAccount: string;
38
+ livemode: boolean;
39
+ meterId: string;
40
+ stripeAccountId: string;
41
+ };
42
+ }, Promise<{
43
+ isDone: boolean;
44
+ continueCursor: import("convex/server").Cursor;
45
+ splitCursor?: import("convex/server").Cursor | null;
46
+ pageStatus?: "SplitRecommended" | "SplitRequired" | null;
47
+ page: {
48
+ event: {
49
+ _creationTime: number;
50
+ _id: import("convex/values").GenericId<"usageEvents">;
51
+ acceptedAt: number;
52
+ identity: string;
53
+ occurredAt: number;
54
+ recordJson: string;
55
+ reservationId?: import("convex/values").GenericId<"usageReservations"> | undefined;
56
+ scopeKey: string;
57
+ subscriptionJson: string;
58
+ };
59
+ delivery: "held" | "provider_accepted" | "queued" | "rejected" | "sending" | "unknown";
60
+ }[];
61
+ }>>;
62
+ //# sourceMappingURL=queries.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queries.d.ts","sourceRoot":"","sources":["../../../src/component/usage/queries.ts"],"names":[],"mappings":"AAaA,eAAO,MAAM,GAAG;;;;;;;;;;;;;;;;;;;;;UAcd,CAAC;AACH,eAAO,MAAM,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCf,CAAC"}
@@ -0,0 +1,65 @@
1
+ import { v } from "convex/values";
2
+ import { paginationOptsValidator, paginationResultValidator, } from "convex/server";
3
+ import { paginator } from "convex-helpers/server/pagination";
4
+ import { query } from "../_generated/server.js";
5
+ import schema from "../schema.js";
6
+ import { eventDoc, scopeValidator, deliveryValidator } from "./schema.js";
7
+ import { scopeKey } from "./helpers.js";
8
+ import { validateUsagePageRequest } from "../../client/usage/boundaries.js";
9
+ const view = v.object({ event: eventDoc, delivery: deliveryValidator });
10
+ export const get = query({
11
+ args: { scope: scopeValidator, eventId: v.id("usageEvents") },
12
+ returns: v.union(view, v.null()),
13
+ handler: async (ctx, args) => {
14
+ const key = scopeKey(args.scope);
15
+ const event = await ctx.db.get("usageEvents", args.eventId);
16
+ if (!event || event.scopeKey !== key)
17
+ return null;
18
+ const outbox = await ctx.db
19
+ .query("usageOutbox")
20
+ .withIndex("by_event", (q) => q.eq("eventId", event._id))
21
+ .unique();
22
+ if (!outbox)
23
+ throw new Error("Usage outbox invariant violated");
24
+ return { event, delivery: outbox.state };
25
+ },
26
+ });
27
+ export const read = query({
28
+ args: {
29
+ scope: scopeValidator,
30
+ period: v.object({ start: v.number(), end: v.number() }),
31
+ paginationOpts: paginationOptsValidator,
32
+ },
33
+ returns: paginationResultValidator(view),
34
+ handler: async (ctx, args) => {
35
+ const key = scopeKey(args.scope);
36
+ const request = validateUsagePageRequest({
37
+ period: args.period,
38
+ limit: args.paginationOpts.numItems,
39
+ cursor: args.paginationOpts.cursor,
40
+ });
41
+ const result = await paginator(ctx.db, schema)
42
+ .query("usageEvents")
43
+ .withIndex("by_scope_time", (q) => q
44
+ .eq("scopeKey", key)
45
+ .gte("occurredAt", request.period.start)
46
+ .lt("occurredAt", request.period.end))
47
+ .order("desc")
48
+ .paginate({
49
+ ...args.paginationOpts,
50
+ maximumRowsRead: 200,
51
+ maximumBytesRead: 1000000,
52
+ });
53
+ const page = await Promise.all(result.page.map(async (event) => {
54
+ const outbox = await ctx.db
55
+ .query("usageOutbox")
56
+ .withIndex("by_event", (q) => q.eq("eventId", event._id))
57
+ .unique();
58
+ if (!outbox)
59
+ throw new Error("Usage outbox invariant violated");
60
+ return { event, delivery: outbox.state };
61
+ }));
62
+ return { ...result, page };
63
+ },
64
+ });
65
+ //# sourceMappingURL=queries.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queries.js","sourceRoot":"","sources":["../../../src/component/usage/queries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EACL,uBAAuB,EACvB,yBAAyB,GAC1B,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAC7D,OAAO,EAA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AAM,IAAI,GAAG,MAAM,OAAO,CAAC,GAAG,CAC5B,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;YAC9B,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,EAAE;iBACxB,KAAK,CAAC,aAAa,CAAC;iBACpB,SAAS,CAAC,UAAU,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;iBACxD,MAAM,EAAE,CAAC;YACZ,IAAI,CAAC,MAAM;gBAAE,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;YAChE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC;QAC3C,CAAC,CAAC,CACH,CAAC;QACF,OAAO,EAAE,GAAG,MAAM,EAAE,IAAI,EAAE,CAAC;IAC7B,CAAC;CACF,CAAC,CAAC"}
@@ -0,0 +1,110 @@
1
+ export declare const scopeValidator: import("convex/values").VObject<{
2
+ customerAccount: string;
3
+ livemode: boolean;
4
+ meterId: string;
5
+ stripeAccountId: string;
6
+ }, {
7
+ stripeAccountId: import("convex/values").VString<string, "required">;
8
+ customerAccount: import("convex/values").VString<string, "required">;
9
+ meterId: import("convex/values").VString<string, "required">;
10
+ livemode: import("convex/values").VBoolean<boolean, "required">;
11
+ }, "required", "customerAccount" | "livemode" | "meterId" | "stripeAccountId">;
12
+ export declare const deliveryValidator: import("convex/values").VUnion<"held" | "provider_accepted" | "queued" | "rejected" | "sending" | "unknown", [import("convex/values").VLiteral<"queued", "required">, import("convex/values").VLiteral<"sending", "required">, import("convex/values").VLiteral<"provider_accepted", "required">, import("convex/values").VLiteral<"rejected", "required">, import("convex/values").VLiteral<"unknown", "required">, import("convex/values").VLiteral<"held", "required">], "required", never>;
13
+ export declare const eventFields: {
14
+ reservationId: import("convex/values").VId<import("convex/values").GenericId<"usageReservations"> | undefined, "optional">;
15
+ scopeKey: import("convex/values").VString<string, "required">;
16
+ identity: import("convex/values").VString<string, "required">;
17
+ recordJson: import("convex/values").VString<string, "required">;
18
+ subscriptionJson: import("convex/values").VString<string, "required">;
19
+ occurredAt: import("convex/values").VFloat64<number, "required">;
20
+ acceptedAt: import("convex/values").VFloat64<number, "required">;
21
+ };
22
+ export declare const eventDoc: import("convex/values").VObject<{
23
+ _creationTime: number;
24
+ _id: import("convex/values").GenericId<"usageEvents">;
25
+ acceptedAt: number;
26
+ identity: string;
27
+ occurredAt: number;
28
+ recordJson: string;
29
+ reservationId?: import("convex/values").GenericId<"usageReservations"> | undefined;
30
+ scopeKey: string;
31
+ subscriptionJson: string;
32
+ }, {
33
+ reservationId: import("convex/values").VId<import("convex/values").GenericId<"usageReservations"> | undefined, "optional">;
34
+ scopeKey: import("convex/values").VString<string, "required">;
35
+ identity: import("convex/values").VString<string, "required">;
36
+ recordJson: import("convex/values").VString<string, "required">;
37
+ subscriptionJson: import("convex/values").VString<string, "required">;
38
+ occurredAt: import("convex/values").VFloat64<number, "required">;
39
+ acceptedAt: import("convex/values").VFloat64<number, "required">;
40
+ _id: import("convex/values").VId<import("convex/values").GenericId<"usageEvents">, "required">;
41
+ _creationTime: import("convex/values").VFloat64<number, "required">;
42
+ }, "required", "_creationTime" | "_id" | "acceptedAt" | "identity" | "occurredAt" | "recordJson" | "reservationId" | "scopeKey" | "subscriptionJson">;
43
+ export declare const usageEventsTable: import("convex/server").TableDefinition<import("convex/values").VObject<{
44
+ acceptedAt: number;
45
+ identity: string;
46
+ occurredAt: number;
47
+ recordJson: string;
48
+ reservationId?: import("convex/values").GenericId<"usageReservations"> | undefined;
49
+ scopeKey: string;
50
+ subscriptionJson: string;
51
+ }, {
52
+ reservationId: import("convex/values").VId<import("convex/values").GenericId<"usageReservations"> | undefined, "optional">;
53
+ scopeKey: import("convex/values").VString<string, "required">;
54
+ identity: import("convex/values").VString<string, "required">;
55
+ recordJson: import("convex/values").VString<string, "required">;
56
+ subscriptionJson: import("convex/values").VString<string, "required">;
57
+ occurredAt: import("convex/values").VFloat64<number, "required">;
58
+ acceptedAt: import("convex/values").VFloat64<number, "required">;
59
+ }, "required", "acceptedAt" | "identity" | "occurredAt" | "recordJson" | "reservationId" | "scopeKey" | "subscriptionJson">, {
60
+ by_identity: ["identity", "_creationTime"];
61
+ by_scope_time: ["scopeKey", "occurredAt", "_creationTime"];
62
+ }, {}, {}>;
63
+ export declare const usageOutboxTable: import("convex/server").TableDefinition<import("convex/values").VObject<{
64
+ attempts: number;
65
+ dispatch?: string | undefined;
66
+ eventId: import("convex/values").GenericId<"usageEvents">;
67
+ nextAttemptAt: number;
68
+ providerIdentifier: string;
69
+ scopeKey: string;
70
+ state: "held" | "provider_accepted" | "queued" | "rejected" | "sending" | "unknown";
71
+ }, {
72
+ eventId: import("convex/values").VId<import("convex/values").GenericId<"usageEvents">, "required">;
73
+ scopeKey: import("convex/values").VString<string, "required">;
74
+ state: import("convex/values").VUnion<"held" | "provider_accepted" | "queued" | "rejected" | "sending" | "unknown", [import("convex/values").VLiteral<"queued", "required">, import("convex/values").VLiteral<"sending", "required">, import("convex/values").VLiteral<"provider_accepted", "required">, import("convex/values").VLiteral<"rejected", "required">, import("convex/values").VLiteral<"unknown", "required">, import("convex/values").VLiteral<"held", "required">], "required", never>;
75
+ attempts: import("convex/values").VFloat64<number, "required">;
76
+ nextAttemptAt: import("convex/values").VFloat64<number, "required">;
77
+ providerIdentifier: import("convex/values").VString<string, "required">;
78
+ dispatch: import("convex/values").VString<string | undefined, "optional">;
79
+ }, "required", "attempts" | "dispatch" | "eventId" | "nextAttemptAt" | "providerIdentifier" | "scopeKey" | "state">, {
80
+ by_event: ["eventId", "_creationTime"];
81
+ by_scope_state: ["scopeKey", "state", "_creationTime"];
82
+ by_state_next: ["state", "nextAttemptAt", "_creationTime"];
83
+ }, {}, {}>;
84
+ export declare const usageScopeCountersTable: import("convex/server").TableDefinition<import("convex/values").VObject<{
85
+ count: number;
86
+ scopeKey: string;
87
+ }, {
88
+ scopeKey: import("convex/values").VString<string, "required">;
89
+ count: import("convex/values").VFloat64<number, "required">;
90
+ }, "required", "count" | "scopeKey">, {
91
+ by_scope: ["scopeKey", "_creationTime"];
92
+ }, {}, {}>;
93
+ export declare const usageReservationsTable: import("convex/server").TableDefinition<import("convex/values").VObject<{
94
+ eventId?: import("convex/values").GenericId<"usageEvents"> | undefined;
95
+ planJson: string;
96
+ scaledQuantity: string;
97
+ scopeKey: string;
98
+ sourceEventId: string;
99
+ state: "committed" | "released" | "reserved";
100
+ }, {
101
+ scopeKey: import("convex/values").VString<string, "required">;
102
+ sourceEventId: import("convex/values").VString<string, "required">;
103
+ planJson: import("convex/values").VString<string, "required">;
104
+ scaledQuantity: import("convex/values").VString<string, "required">;
105
+ state: import("convex/values").VUnion<"committed" | "released" | "reserved", [import("convex/values").VLiteral<"reserved", "required">, import("convex/values").VLiteral<"committed", "required">, import("convex/values").VLiteral<"released", "required">], "required", never>;
106
+ eventId: import("convex/values").VId<import("convex/values").GenericId<"usageEvents"> | undefined, "optional">;
107
+ }, "required", "eventId" | "planJson" | "scaledQuantity" | "scopeKey" | "sourceEventId" | "state">, {
108
+ by_scope_source: ["scopeKey", "sourceEventId", "_creationTime"];
109
+ }, {}, {}>;
110
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../../src/component/usage/schema.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,cAAc;;;;;;;;;;8EAKzB,CAAC;AACH,eAAO,MAAM,iBAAiB,geAO7B,CAAC;AACF,eAAO,MAAM,WAAW;IACtB,aAAa;IACb,QAAQ;IACR,QAAQ;IACR,UAAU;IACV,gBAAgB;IAChB,UAAU;IACV,UAAU;CACX,CAAC;AACF,eAAO,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;;qJAInB,CAAC;AACH,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;UAEwB,CAAC;AACtD,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;UAWoB,CAAC;AAClD,eAAO,MAAM,uBAAuB;;;;;;;;UAGF,CAAC;AAInC,eAAO,MAAM,sBAAsB;;;;;;;;;;;;;;;;UAWuB,CAAC"}
@@ -0,0 +1,54 @@
1
+ import { defineTable } from "convex/server";
2
+ import { v } from "convex/values";
3
+ export const scopeValidator = v.object({
4
+ stripeAccountId: v.string(),
5
+ customerAccount: v.string(),
6
+ meterId: v.string(),
7
+ livemode: v.boolean(),
8
+ });
9
+ export const deliveryValidator = v.union(v.literal("queued"), v.literal("sending"), v.literal("provider_accepted"), v.literal("rejected"), v.literal("unknown"), v.literal("held"));
10
+ export const eventFields = {
11
+ reservationId: v.optional(v.id("usageReservations")),
12
+ scopeKey: v.string(),
13
+ identity: v.string(),
14
+ recordJson: v.string(),
15
+ subscriptionJson: v.string(),
16
+ occurredAt: v.number(),
17
+ acceptedAt: v.number(),
18
+ };
19
+ export const eventDoc = v.object({
20
+ _id: v.id("usageEvents"),
21
+ _creationTime: v.number(),
22
+ ...eventFields,
23
+ });
24
+ export const usageEventsTable = defineTable(eventFields)
25
+ .index("by_identity", ["identity"])
26
+ .index("by_scope_time", ["scopeKey", "occurredAt"]);
27
+ export const usageOutboxTable = defineTable({
28
+ eventId: v.id("usageEvents"),
29
+ scopeKey: v.string(),
30
+ state: deliveryValidator,
31
+ attempts: v.number(),
32
+ nextAttemptAt: v.number(),
33
+ providerIdentifier: v.string(),
34
+ dispatch: v.optional(v.string()),
35
+ })
36
+ .index("by_event", ["eventId"])
37
+ .index("by_state_next", ["state", "nextAttemptAt"])
38
+ .index("by_scope_state", ["scopeKey", "state"]);
39
+ export const usageScopeCountersTable = defineTable({
40
+ scopeKey: v.string(),
41
+ count: v.number(),
42
+ }).index("by_scope", ["scopeKey"]);
43
+ // Only accounting APIs may create reservations. There is no public arbitrary
44
+ // reservation/grant insertion API. Acceptance commits a matching reservation
45
+ // in the same transaction as its immutable event and outbox intent.
46
+ export const usageReservationsTable = defineTable({
47
+ scopeKey: v.string(),
48
+ sourceEventId: v.string(),
49
+ planJson: v.string(),
50
+ scaledQuantity: v.string(),
51
+ state: v.union(v.literal("reserved"), v.literal("committed"), v.literal("released")),
52
+ eventId: v.optional(v.id("usageEvents")),
53
+ }).index("by_scope_source", ["scopeKey", "sourceEventId"]);
54
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../../src/component/usage/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC,MAAM,CAAC;IACrC,eAAe,EAAE,CAAC,CAAC,MAAM,EAAE;IAC3B,eAAe,EAAE,CAAC,CAAC,MAAM,EAAE;IAC3B,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE;IACnB,QAAQ,EAAE,CAAC,CAAC,OAAO,EAAE;CACtB,CAAC,CAAC;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,CAAC,KAAK,CACtC,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,EACnB,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,EACpB,CAAC,CAAC,OAAO,CAAC,mBAAmB,CAAC,EAC9B,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,EACrB,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,EACpB,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAClB,CAAC;AACF,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,aAAa,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,mBAAmB,CAAC,CAAC;IACpD,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;IACpB,QAAQ,EAAE,CAAC,CAAC,MAAM,EAA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CAAC,MAAM,EAAE;IACpB,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;CAClB,CAAC,CAAC,KAAK,CAAC,UAAU,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC;AACnC,6EAA6E;AAC7E,6EAA6E;AAC7E,oEAAoE;AACpE,MAAM,CAAC,MAAM,sBAAsB,GAAG,WAAW,CAAC;IAChD,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;IACpB,aAAa,EAAE,CAAC,CAAC,MAAM,EAAE;IACzB,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;IACpB,cAAc,EAAE,CAAC,CAAC,MAAM,EAAE;IAC1B,KAAK,EAAE,CAAC,CAAC,KAAK,CACZ,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,EACrB,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,EACtB,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC,CACtB;IACD,OAAO,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,aAAa,CAAC,CAAC;CACzC,CAAC,CAAC,KAAK,CAAC,iBAAiB,EAAE,CAAC,UAAU,EAAE,eAAe,CAAC,CAAC,CAAC"}
package/docs/README.md CHANGED
@@ -51,3 +51,10 @@ guide for your integration.
51
51
 
52
52
  The [example app setup guide](https://github.com/FantasticDevHQ/better-stripe/blob/main/example/README.md)
53
53
  covers sandbox configuration, checkout comparisons, and test flows.
54
+
55
+ ## Metered usage
56
+
57
+ Start with the [native Stripe setup guide](metering-setup.md), then read the
58
+ [shared contracts and sandbox evidence](metered-usage.md).
59
+
60
+ [Atomic usage ledger and scoped history](usage-ledger.md) describes acceptance and retention.
@@ -46,14 +46,32 @@ formatting, typechecks, codegen drift check, tests, and repository quality gates
46
46
  used by CI. Policy lives in `repo-gates.config.json`; measured baselines live in
47
47
  `gates/`. Fix failures without weakening the gates or raising existing budgets.
48
48
 
49
- Successful full coverage checks automatically raise the floors in
50
- `gates/coverage-budgets.json`. Run `pnpm run check:coverage` or
51
- `pnpm run check:all`, then commit any budget changes. Each metric retains the
52
- existing 0.5 percentage point margin below its measured baseline, and floors
53
- never decrease. Failed checks leave the budgets untouched. CI checks the same
54
- ratchet and fails if an improvement has not been committed. `pnpm test:coverage`
55
- only generates the report; filtered or skipped runs cannot update floors through
56
- the automatic ratchet.
49
+ Coverage checks enforce the committed floors in `gates/coverage-budgets.json`.
50
+ `pnpm run check:coverage` and `pnpm run check:all` never edit that file or fail
51
+ because coverage improved. Ordinary feature PRs should leave the budget file alone.
52
+
53
+ The [weekly coverage workflow](https://github.com/FantasticDevHQ/better-stripe/actions/workflows/coverage-ratchet.yml)
54
+ runs Mondays at 09:17 Asia/Bangkok, 02:17 UTC. It runs every repository gate on
55
+ `main`, then captures earned floor increases of at least **0.05 percentage points**
56
+ per metric. It keeps the existing 0.5-point margin below measured coverage and
57
+ never lowers a floor. Eligible metrics capture their full earned increase; this
58
+ is a minimum useful change, not a forced weekly increase. Smaller gains accumulate
59
+ until a later run. Failed checks and old reports cannot authorize any update.
60
+
61
+ The workflow maintains one ready-for-review PR on `codex/coverage-ratchet`, changing
62
+ only the coverage budget file. Its description records measured coverage, old and
63
+ proposed floors, and a link to the full gate run. It reuses the existing release
64
+ GitHub App configuration so the generated PR runs normal CI. Auto-merge matches
65
+ the exact generated head and waits for all required checks under the existing
66
+ branch protections, without bypass actors. The helper checks the app author,
67
+ branch, repository, changed files, policy metadata, and measured proposal before
68
+ enabling auto-merge. Do not add unrelated commits to this managed branch.
69
+
70
+ Use **Run workflow** on `main` for an additional full measurement between weekly
71
+ runs. Once merged, this workflow becomes available to the scheduler and manual
72
+ dispatch. Locally, `node scripts/check-coverage.mjs --ratchet` runs the same complete
73
+ gates before proposing a budget update. It accepts no test filters, partial mode,
74
+ or skipped execution. `pnpm test:coverage` generates a report only.
57
75
 
58
76
  Builds and typechecks use TypeScript 7 (`@typescript/native` aliases
59
77
  `typescript`). ESLint and Convex code generation still need the TypeScript 6