@fantastic.dev/better-stripe 0.49.0 → 0.50.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/CHANGELOG.md +23 -0
- package/README.md +1 -1
- package/dist/client/billing/setup-intent-read.d.ts +22 -0
- package/dist/client/billing/setup-intent-read.d.ts.map +1 -0
- package/dist/client/billing/setup-intent-read.js +63 -0
- package/dist/client/billing/setup-intent-read.js.map +1 -0
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +32 -32
- package/dist/client/index.d.ts +4 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +5 -0
- package/dist/client/index.js.map +1 -1
- package/dist/component/balanceFunding/mutations.d.ts +16 -16
- package/dist/react/workflows/useWorkflowAction.d.ts.map +1 -1
- package/dist/react/workflows/useWorkflowAction.js +5 -3
- package/dist/react/workflows/useWorkflowAction.js.map +1 -1
- package/docs/react-workflows.md +15 -0
- package/docs/setup-intent-recovery.md +27 -0
- package/package.json +3 -2
- package/src/client/billing/setup-intent-read.test.ts +284 -0
- package/src/client/billing/setup-intent-read.ts +97 -0
- package/src/client/index.ts +18 -0
- package/src/react/workflows/useWorkflowAction.commit.test.tsx +194 -0
- package/src/react/workflows/useWorkflowAction.ssr.test.tsx +20 -0
- package/src/react/workflows/useWorkflowAction.ts +13 -3
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { StrictMode, useLayoutEffect } from "react";
|
|
3
|
+
import {
|
|
4
|
+
act,
|
|
5
|
+
cleanup,
|
|
6
|
+
fireEvent,
|
|
7
|
+
render,
|
|
8
|
+
renderHook,
|
|
9
|
+
screen,
|
|
10
|
+
} from "@testing-library/react";
|
|
11
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
12
|
+
import { useWorkflowAction } from "./useWorkflowAction.js";
|
|
13
|
+
|
|
14
|
+
afterEach(cleanup);
|
|
15
|
+
|
|
16
|
+
function ActionConsumer({
|
|
17
|
+
startsDuringCommit,
|
|
18
|
+
}: {
|
|
19
|
+
startsDuringCommit: boolean;
|
|
20
|
+
}) {
|
|
21
|
+
const observation = useWorkflowAction(async () => "observed", "original");
|
|
22
|
+
useLayoutEffect(() => {
|
|
23
|
+
if (startsDuringCommit) void observation.run();
|
|
24
|
+
}, [startsDuringCommit, observation.run]);
|
|
25
|
+
return (
|
|
26
|
+
<div>
|
|
27
|
+
<button
|
|
28
|
+
onClick={() => {
|
|
29
|
+
void observation.run();
|
|
30
|
+
}}
|
|
31
|
+
>
|
|
32
|
+
Observe
|
|
33
|
+
</button>
|
|
34
|
+
<p>
|
|
35
|
+
{observation.isPending ? "pending" : (observation.result ?? "idle")}
|
|
36
|
+
</p>
|
|
37
|
+
</div>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
it("completes an ordinary event-started workflow action", async () => {
|
|
42
|
+
render(<ActionConsumer startsDuringCommit={false} />);
|
|
43
|
+
fireEvent.click(screen.getByRole("button", { name: "Observe" }));
|
|
44
|
+
expect((await screen.findByText("observed")).textContent).toBe("observed");
|
|
45
|
+
expect(screen.queryByText("pending")).toBeNull();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("completes an action started by a consumer layout effect", async () => {
|
|
49
|
+
render(<ActionConsumer startsDuringCommit />);
|
|
50
|
+
expect((await screen.findByText("observed")).textContent).toBe("observed");
|
|
51
|
+
expect(screen.queryByText("pending")).toBeNull();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("completes the current commit-started action through StrictMode replay", async () => {
|
|
55
|
+
render(
|
|
56
|
+
<StrictMode>
|
|
57
|
+
<ActionConsumer startsDuringCommit />
|
|
58
|
+
</StrictMode>,
|
|
59
|
+
);
|
|
60
|
+
expect((await screen.findByText("observed")).textContent).toBe("observed");
|
|
61
|
+
expect(screen.queryByText("pending")).toBeNull();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("uses the current callback when a layout effect starts work after rerender", async () => {
|
|
65
|
+
const first = vi.fn(async () => "first");
|
|
66
|
+
const second = vi.fn(async () => "second");
|
|
67
|
+
const { result, rerender } = renderHook(
|
|
68
|
+
({ action }) => {
|
|
69
|
+
const observation = useWorkflowAction(action);
|
|
70
|
+
useLayoutEffect(() => {
|
|
71
|
+
void observation.run();
|
|
72
|
+
}, [action, observation.run]);
|
|
73
|
+
return observation;
|
|
74
|
+
},
|
|
75
|
+
{ initialProps: { action: first } },
|
|
76
|
+
);
|
|
77
|
+
await act(async () => {});
|
|
78
|
+
expect(result.current.result).toBe("first");
|
|
79
|
+
rerender({ action: second });
|
|
80
|
+
await act(async () => {});
|
|
81
|
+
expect(result.current.result).toBe("second");
|
|
82
|
+
expect(first).toHaveBeenCalledOnce();
|
|
83
|
+
expect(second).toHaveBeenCalledOnce();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("does not call success after a pending consumer unmounts", async () => {
|
|
87
|
+
let complete!: (value: string) => void;
|
|
88
|
+
const onSuccess = vi.fn();
|
|
89
|
+
const { result, unmount } = renderHook(() =>
|
|
90
|
+
useWorkflowAction(
|
|
91
|
+
() =>
|
|
92
|
+
new Promise<string>((resolve) => {
|
|
93
|
+
complete = resolve;
|
|
94
|
+
}),
|
|
95
|
+
"original",
|
|
96
|
+
onSuccess,
|
|
97
|
+
),
|
|
98
|
+
);
|
|
99
|
+
let pending!: Promise<string | undefined>;
|
|
100
|
+
act(() => {
|
|
101
|
+
pending = result.current.run();
|
|
102
|
+
});
|
|
103
|
+
unmount();
|
|
104
|
+
await act(async () => {
|
|
105
|
+
complete("discarded");
|
|
106
|
+
expect(await pending).toBeUndefined();
|
|
107
|
+
});
|
|
108
|
+
expect(onSuccess).not.toHaveBeenCalled();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
function Child({
|
|
112
|
+
run,
|
|
113
|
+
starts,
|
|
114
|
+
}: {
|
|
115
|
+
run: () => Promise<string | undefined>;
|
|
116
|
+
starts: boolean;
|
|
117
|
+
}) {
|
|
118
|
+
useLayoutEffect(() => {
|
|
119
|
+
if (starts) void run();
|
|
120
|
+
}, [run, starts]);
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
function Parent({ starts, label }: { starts: boolean; label: string }) {
|
|
124
|
+
const action = useWorkflowAction(async () => label, "original");
|
|
125
|
+
return (
|
|
126
|
+
<>
|
|
127
|
+
<Child run={action.run} starts={starts} />
|
|
128
|
+
<p>{action.isPending ? "pending" : (action.result ?? "idle")}</p>
|
|
129
|
+
</>
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
it("settles a parent-owned public workflow action started by its child layout effect", async () => {
|
|
134
|
+
render(<Parent starts label="observed" />);
|
|
135
|
+
expect(await screen.findByText("observed")).toBeTruthy();
|
|
136
|
+
expect(screen.queryByText("pending")).toBeNull();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("uses the current parent callback when child layout starts work after an update", async () => {
|
|
140
|
+
const view = render(<Parent starts={false} label="first" />);
|
|
141
|
+
expect(screen.getByText("idle")).toBeTruthy();
|
|
142
|
+
view.rerender(<Parent starts label="second" />);
|
|
143
|
+
expect(await screen.findByText("second")).toBeTruthy();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("dispatches and publishes once when a child starts its parent's action under StrictMode", async () => {
|
|
147
|
+
const action = vi.fn(async () => "current");
|
|
148
|
+
const onSuccess = vi.fn();
|
|
149
|
+
function StrictParent() {
|
|
150
|
+
const observation = useWorkflowAction(action, "original", onSuccess);
|
|
151
|
+
return (
|
|
152
|
+
<>
|
|
153
|
+
<Child run={observation.run} starts />
|
|
154
|
+
<p>
|
|
155
|
+
{observation.isPending ? "pending" : (observation.result ?? "idle")}
|
|
156
|
+
</p>
|
|
157
|
+
</>
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
render(
|
|
161
|
+
<StrictMode>
|
|
162
|
+
<StrictParent />
|
|
163
|
+
</StrictMode>,
|
|
164
|
+
);
|
|
165
|
+
expect((await screen.findByText("current")).textContent).toBe("current");
|
|
166
|
+
expect(action).toHaveBeenCalledOnce();
|
|
167
|
+
expect(onSuccess).toHaveBeenCalledExactlyOnceWith("current");
|
|
168
|
+
expect(screen.queryByText("pending")).toBeNull();
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("suppresses pending success and result after the scope changes", async () => {
|
|
172
|
+
let complete!: (value: string) => void;
|
|
173
|
+
const onSuccess = vi.fn();
|
|
174
|
+
const action = () =>
|
|
175
|
+
new Promise<string>((resolve) => {
|
|
176
|
+
complete = resolve;
|
|
177
|
+
});
|
|
178
|
+
const { result, rerender } = renderHook(
|
|
179
|
+
({ scope }) => useWorkflowAction(action, scope, onSuccess),
|
|
180
|
+
{ initialProps: { scope: "original" } },
|
|
181
|
+
);
|
|
182
|
+
let pending!: Promise<string | undefined>;
|
|
183
|
+
act(() => {
|
|
184
|
+
pending = result.current.run();
|
|
185
|
+
});
|
|
186
|
+
rerender({ scope: "replacement" });
|
|
187
|
+
await act(async () => {
|
|
188
|
+
complete("old");
|
|
189
|
+
expect(await pending).toBeUndefined();
|
|
190
|
+
});
|
|
191
|
+
expect(onSuccess).not.toHaveBeenCalled();
|
|
192
|
+
expect(result.current.result).toBeUndefined();
|
|
193
|
+
expect(result.current.isPending).toBe(false);
|
|
194
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
import { renderToString } from "react-dom/server";
|
|
3
|
+
import { expect, it, vi } from "vitest";
|
|
4
|
+
import { useWorkflowAction } from "./useWorkflowAction.js";
|
|
5
|
+
|
|
6
|
+
it("renders idle on the server without invoking actions or layout effects", () => {
|
|
7
|
+
const action = vi.fn(async () => "observed");
|
|
8
|
+
const warning = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
9
|
+
function Consumer() {
|
|
10
|
+
const observation = useWorkflowAction(action);
|
|
11
|
+
return <p>{observation.isPending ? "pending" : "idle"}</p>;
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
expect(renderToString(<Consumer />)).toBe("<p>idle</p>");
|
|
15
|
+
expect(action).not.toHaveBeenCalled();
|
|
16
|
+
expect(warning).not.toHaveBeenCalled();
|
|
17
|
+
} finally {
|
|
18
|
+
warning.mockRestore();
|
|
19
|
+
}
|
|
20
|
+
});
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
useCallback,
|
|
4
|
+
useEffect,
|
|
5
|
+
useInsertionEffect,
|
|
6
|
+
useRef,
|
|
7
|
+
useState,
|
|
8
|
+
} from "react";
|
|
9
|
+
// Ref-only synchronization must precede layout effects in descendants as well.
|
|
10
|
+
const useCommitEffect =
|
|
11
|
+
typeof window === "undefined" ? useEffect : useInsertionEffect;
|
|
12
|
+
|
|
3
13
|
export type WorkflowActionState<T, Args extends unknown[]> = {
|
|
4
14
|
isPending: boolean;
|
|
5
15
|
error: string | null;
|
|
@@ -26,11 +36,11 @@ export function useWorkflowAction<T, Args extends unknown[]>(
|
|
|
26
36
|
const success = useRef(onSuccess);
|
|
27
37
|
const lifecycle = useRef({ version: 0, mounted: false });
|
|
28
38
|
const lock = useRef<number | null>(null);
|
|
29
|
-
|
|
39
|
+
useCommitEffect(() => {
|
|
30
40
|
callback.current = action;
|
|
31
41
|
success.current = onSuccess;
|
|
32
42
|
}, [action, onSuccess]);
|
|
33
|
-
|
|
43
|
+
useCommitEffect(() => {
|
|
34
44
|
const version = ++lifecycle.current.version;
|
|
35
45
|
lifecycle.current.mounted = true;
|
|
36
46
|
lock.current = null;
|