@startsimpli/billing 0.4.20 → 0.4.21
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/package.json
CHANGED
|
@@ -181,4 +181,24 @@ describe("useSubscription", () => {
|
|
|
181
181
|
expect(result.current.subscription?.id).toBe("sub-2")
|
|
182
182
|
);
|
|
183
183
|
});
|
|
184
|
+
|
|
185
|
+
it("fetches again when refetchKey changes (the signed-in user arrives after the first fetch)", async () => {
|
|
186
|
+
// First call: the session isn't ready yet, so the API says 401. Second: signed in.
|
|
187
|
+
const mockFetch = vi.fn()
|
|
188
|
+
.mockResolvedValueOnce({ ok: false, status: 401, json: async () => ({ error: "Not authenticated" }) })
|
|
189
|
+
.mockResolvedValue({ ok: true, status: 200, json: async () => mockSubscription }) as unknown as typeof fetch;
|
|
190
|
+
const wrapper = createWrapper(mockFetch);
|
|
191
|
+
|
|
192
|
+
const { result, rerender } = renderHook(({ user }) => useSubscription(undefined, { refetchKey: user }), {
|
|
193
|
+
wrapper,
|
|
194
|
+
initialProps: { user: undefined as string | undefined },
|
|
195
|
+
});
|
|
196
|
+
await waitFor(() => expect(result.current.loading).toBe(false));
|
|
197
|
+
expect(result.current.subscription).toBeNull();
|
|
198
|
+
|
|
199
|
+
rerender({ user: "user-1" });
|
|
200
|
+
await waitFor(() => expect(result.current.subscription?.id).toBe("sub-123"));
|
|
201
|
+
expect(result.current.error).toBeNull();
|
|
202
|
+
});
|
|
184
203
|
});
|
|
204
|
+
|
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Pass the app's product slug so another app's plan is never returned
|
|
5
5
|
* (startsim-oyr3a.2).
|
|
6
|
+
*
|
|
7
|
+
* Pass the signed-in user's id as `refetchKey`. The first fetch can run before
|
|
8
|
+
* the session is ready (straight back from sign-in it answers 401), and a plan
|
|
9
|
+
* read once and never again showed paying users as Free until they reloaded.
|
|
6
10
|
*/
|
|
7
11
|
|
|
8
12
|
import { useCallback, useEffect, useState } from "react";
|
|
@@ -16,7 +20,13 @@ interface UseSubscriptionResult {
|
|
|
16
20
|
refetch: () => void;
|
|
17
21
|
}
|
|
18
22
|
|
|
19
|
-
export
|
|
23
|
+
export interface UseSubscriptionOptions {
|
|
24
|
+
/** Fetch again whenever this changes, e.g. the signed-in user's id. */
|
|
25
|
+
refetchKey?: unknown;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function useSubscription(productId?: string, options: UseSubscriptionOptions = {}): UseSubscriptionResult {
|
|
29
|
+
const { refetchKey } = options;
|
|
20
30
|
const { client } = useBillingContext();
|
|
21
31
|
const [subscription, setSubscription] = useState<SubscriptionInfo | null>(null);
|
|
22
32
|
const [loading, setLoading] = useState(true);
|
|
@@ -37,7 +47,7 @@ export function useSubscription(productId?: string): UseSubscriptionResult {
|
|
|
37
47
|
|
|
38
48
|
useEffect(() => {
|
|
39
49
|
fetchSubscription();
|
|
40
|
-
}, [fetchSubscription]);
|
|
50
|
+
}, [fetchSubscription, refetchKey]);
|
|
41
51
|
|
|
42
52
|
return { subscription, loading, error, refetch: fetchSubscription };
|
|
43
53
|
}
|