@fantastic.dev/better-stripe 0.88.1 → 0.89.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 (28) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +1 -1
  3. package/dist/client/billing/renewalAttach/internal/originalOwner.d.ts +27 -0
  4. package/dist/client/billing/renewalAttach/internal/originalOwner.d.ts.map +1 -0
  5. package/dist/client/billing/renewalAttach/internal/originalOwner.js +236 -0
  6. package/dist/client/billing/renewalAttach/internal/originalOwner.js.map +1 -0
  7. package/dist/client/billing/renewalAttach/internal/sourceBindings.d.ts +5 -0
  8. package/dist/client/billing/renewalAttach/internal/sourceBindings.d.ts.map +1 -0
  9. package/dist/client/billing/renewalAttach/internal/sourceBindings.js +9 -0
  10. package/dist/client/billing/renewalAttach/internal/sourceBindings.js.map +1 -0
  11. package/dist/client/billing/renewalAttach/internal/stockRawDelegate.d.ts +6 -0
  12. package/dist/client/billing/renewalAttach/internal/stockRawDelegate.d.ts.map +1 -0
  13. package/dist/client/billing/renewalAttach/internal/stockRawDelegate.js +35 -0
  14. package/dist/client/billing/renewalAttach/internal/stockRawDelegate.js.map +1 -0
  15. package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
  16. package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +32 -32
  17. package/dist/component/balanceFunding/internal/actions.d.ts +11 -11
  18. package/dist/component/financialObservations/settlementHelpers.d.ts +3 -3
  19. package/dist/component/usage/allowance.fixtures.d.ts +16 -16
  20. package/dist/component/usageCredits/accounting.fixtures.d.ts +16 -16
  21. package/docs/renewal-context-reads.md +15 -0
  22. package/package.json +1 -1
  23. package/src/client/billing/renewalAttach/internal/originalOwner.test.ts +258 -0
  24. package/src/client/billing/renewalAttach/internal/originalOwner.ts +280 -0
  25. package/src/client/billing/renewalAttach/internal/sourceBindings.test.ts +13 -0
  26. package/src/client/billing/renewalAttach/internal/sourceBindings.ts +8 -0
  27. package/src/client/billing/renewalAttach/internal/stockRawDelegate.test.ts +156 -0
  28. package/src/client/billing/renewalAttach/internal/stockRawDelegate.ts +43 -0
@@ -0,0 +1,8 @@
1
+ /** Internal adoption defaults. No production Source capability is bound. */
2
+ export const COMPILED_ENTRIES: readonly never[] = Object.freeze([]);
3
+ export function loadOwnedOriginalSource(): unknown {
4
+ throw new Error("ORIGINAL_OWNER_UNBOUND");
5
+ }
6
+ export function loadFixedStockClient(): unknown {
7
+ throw new Error("STOCK_CLIENT_UNBOUND");
8
+ }
@@ -0,0 +1,156 @@
1
+ import Stripe from "stripe";
2
+ import { afterEach, expect, it, vi } from "vitest";
3
+
4
+ afterEach(() => {
5
+ vi.restoreAllMocks();
6
+ vi.doUnmock("./sourceBindings.js");
7
+ vi.doUnmock("./originalOwner.js");
8
+ vi.resetModules();
9
+ });
10
+
11
+ async function delegate(client: unknown, check = () => {}) {
12
+ vi.resetModules();
13
+ vi.doMock("./originalOwner.js", () => ({ OWNER: { check } }));
14
+ vi.doMock("./sourceBindings.js", () => ({
15
+ loadFixedStockClient: () => client,
16
+ }));
17
+ return (await import("./stockRawDelegate.js")).createSourceStockRawDelegate;
18
+ }
19
+
20
+ it("captures the inherited stock method and preserves receiver, args and exact promise", async () => {
21
+ const client = new Stripe("sk_test_INTERNAL_SYNTHETIC_ONLY");
22
+ const promise = Promise.resolve({ ordinary: true });
23
+ let receiver: unknown;
24
+ let args: unknown[] = [];
25
+ vi.spyOn(Stripe.prototype, "rawRequest").mockImplementation(function (
26
+ this: Stripe,
27
+ ...values
28
+ ) {
29
+ receiver = this;
30
+ args = values;
31
+ return promise as ReturnType<Stripe["rawRequest"]>;
32
+ });
33
+ const create = await delegate(client);
34
+ let raw: ReturnType<typeof create> | undefined;
35
+ try {
36
+ raw = create();
37
+ } catch {
38
+ /* Fixed STOP scaffold must fail the live assertion. */
39
+ }
40
+ expect(raw).toBeDefined();
41
+ if (!raw) throw new Error("missing live delegate");
42
+ expect(Object.isFrozen(raw)).toBe(true);
43
+ expect(Object.prototype.hasOwnProperty.call(raw, "rawRequest")).toBe(true);
44
+ const options = { maxNetworkRetries: 0, idempotencyKey: "retained" };
45
+ expect(
46
+ raw.rawRequest(
47
+ "POST",
48
+ "/v1/subscription_schedules",
49
+ { from_subscription: "sub_original" },
50
+ options,
51
+ ),
52
+ ).toBe(promise);
53
+ expect(receiver).toBe(client);
54
+ expect(args[3]).toBe(options);
55
+ expect(args).toEqual([
56
+ "POST",
57
+ "/v1/subscription_schedules",
58
+ { from_subscription: "sub_original" },
59
+ options,
60
+ ]);
61
+ });
62
+
63
+ it("forwards the identical synchronous stock throw without retry or wrapping", async () => {
64
+ const client = new Stripe("sk_test_INTERNAL_SYNTHETIC_ONLY"),
65
+ error = Error("SYNTHETIC");
66
+ let calls = 0;
67
+ vi.spyOn(Stripe.prototype, "rawRequest").mockImplementation(() => {
68
+ calls++;
69
+ throw error;
70
+ });
71
+ const create = await delegate(client);
72
+ const raw = create();
73
+ try {
74
+ void raw.rawRequest("GET", "/v1/customers/cus_original");
75
+ expect.fail("must throw");
76
+ } catch (caught) {
77
+ expect(caught).toBe(error);
78
+ }
79
+ expect(calls).toBe(1);
80
+ });
81
+
82
+ it("forwards a real stock SDK request to a closed synthetic HTTP delegate once", async () => {
83
+ const requests: unknown[][] = [];
84
+ const client = new Stripe("sk_test_INTERNAL_SYNTHETIC_ONLY", {
85
+ maxNetworkRetries: 0,
86
+ httpClient: Stripe.createFetchHttpClient(async (url, init) => {
87
+ requests.push([url, init?.method, init?.body]);
88
+ return new Response(
89
+ JSON.stringify({
90
+ id: "sub_sched_original",
91
+ object: "subscription_schedule",
92
+ }),
93
+ { status: 200, headers: { "content-type": "application/json" } },
94
+ );
95
+ }),
96
+ });
97
+ const create = await delegate(client),
98
+ raw = create();
99
+ const result = await raw.rawRequest(
100
+ "POST",
101
+ "/v1/subscription_schedules",
102
+ { from_subscription: "sub_original" },
103
+ { maxNetworkRetries: 0, idempotencyKey: "same-retained-key" },
104
+ );
105
+ expect(result.id).toBe("sub_sched_original");
106
+ expect(requests).toHaveLength(1);
107
+ expect(requests[0][1]).toBe("POST");
108
+ expect(requests[0][2]).toBe("from_subscription=sub_original");
109
+ });
110
+
111
+ it.each(["foreign", "own-method", "proxy"])(
112
+ "refuses %s clients before a raw request",
113
+ async (kind) => {
114
+ const client = new Stripe("sk_test_INTERNAL_SYNTHETIC_ONLY");
115
+ let traps = 0;
116
+ const supplied =
117
+ kind === "foreign"
118
+ ? {}
119
+ : kind === "proxy"
120
+ ? new Proxy(client, {
121
+ getPrototypeOf() {
122
+ traps++;
123
+ return Stripe.prototype;
124
+ },
125
+ })
126
+ : Object.defineProperty(client, "rawRequest", {
127
+ value() {
128
+ throw Error("MUST_NOT_CALL");
129
+ },
130
+ });
131
+ const create = await delegate(supplied);
132
+ expect(() => create()).toThrow("STOCK_SOURCE_DELEGATE_UNBOUND");
133
+ expect(traps).toBe(0);
134
+ },
135
+ );
136
+
137
+ it("checks the owner before loading or inspecting a client and rejects parameters", async () => {
138
+ let gets = 0;
139
+ const client = new Proxy(
140
+ {},
141
+ {
142
+ getPrototypeOf() {
143
+ gets++;
144
+ return null;
145
+ },
146
+ },
147
+ );
148
+ const create = await delegate(client, () => {
149
+ throw Error("RETIRED");
150
+ });
151
+ expect(() => create()).toThrow("RETIRED");
152
+ expect(gets).toBe(0);
153
+ expect(() => Reflect.apply(create, undefined, [client])).toThrow(
154
+ "STOCK_SOURCE_DELEGATE_UNBOUND",
155
+ );
156
+ });
@@ -0,0 +1,43 @@
1
+ import Stripe from "stripe";
2
+ import { types } from "node:util";
3
+ import { OWNER } from "./originalOwner.js";
4
+ import { loadFixedStockClient } from "./sourceBindings.js";
5
+
6
+ function stop(): never {
7
+ throw new Error("STOCK_SOURCE_DELEGATE_UNBOUND");
8
+ }
9
+ const apply = Reflect.apply;
10
+ /** Internal fixed Source facade, not a public client or qualification selector. */
11
+ export function createSourceStockRawDelegate(): Readonly<{
12
+ rawRequest: Stripe["rawRequest"];
13
+ }> {
14
+ if (arguments.length !== 0) stop();
15
+ OWNER.check();
16
+ const client = loadFixedStockClient();
17
+ OWNER.check();
18
+ if (
19
+ !client ||
20
+ typeof client !== "object" ||
21
+ types.isProxy(client) ||
22
+ Object.getPrototypeOf(client) !== Stripe.prototype ||
23
+ Object.prototype.hasOwnProperty.call(client, "rawRequest")
24
+ )
25
+ stop();
26
+ const descriptor = Object.getOwnPropertyDescriptor(
27
+ Stripe.prototype,
28
+ "rawRequest",
29
+ );
30
+ if (
31
+ !descriptor ||
32
+ !Object.prototype.hasOwnProperty.call(descriptor, "value") ||
33
+ typeof descriptor.value !== "function"
34
+ )
35
+ stop();
36
+ const method = descriptor.value as Stripe["rawRequest"];
37
+ OWNER.check();
38
+ return Object.freeze({
39
+ rawRequest(...args: Parameters<Stripe["rawRequest"]>) {
40
+ return apply(method, client, args) as ReturnType<Stripe["rawRequest"]>;
41
+ },
42
+ });
43
+ }