@clerk/shared 4.0.0-canary.v20260107202600 → 4.0.0-canary.v20260108154421
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/dist/runtime/alternativePhoneCode.d.mts +1 -1
- package/dist/runtime/alternativePhoneCode.d.ts +1 -1
- package/dist/runtime/apiUrlFromPublishableKey.d.mts +1 -1
- package/dist/runtime/apiUrlFromPublishableKey.d.ts +1 -1
- package/dist/runtime/authorization-errors.d.mts +1 -1
- package/dist/runtime/authorization-errors.d.ts +1 -1
- package/dist/runtime/authorization.d.mts +1 -1
- package/dist/runtime/authorization.d.ts +1 -1
- package/dist/runtime/clerkEventBus.d.mts +1 -1
- package/dist/runtime/clerkEventBus.d.ts +1 -1
- package/dist/runtime/color.d.mts +1 -1
- package/dist/runtime/color.d.ts +1 -1
- package/dist/runtime/color.d.ts.map +1 -1
- package/dist/runtime/deriveState.d.mts +1 -1
- package/dist/runtime/deriveState.d.ts +1 -1
- package/dist/runtime/{error-BXk0GDM3.d.mts → error-DNiurtIH.d.mts} +2 -2
- package/dist/runtime/{error-BXk0GDM3.d.mts.map → error-DNiurtIH.d.mts.map} +1 -1
- package/dist/runtime/{error-Bc2wkICP.d.ts → error-DW8FKFzD.d.ts} +2 -2
- package/dist/runtime/{error-Bc2wkICP.d.ts.map → error-DW8FKFzD.d.ts.map} +1 -1
- package/dist/runtime/error.d.mts +2 -2
- package/dist/runtime/error.d.ts +2 -2
- package/dist/runtime/globs.d.ts.map +1 -1
- package/dist/runtime/{index-C3yQerjv.d.mts → index-CwXrQ0D9.d.mts} +17 -1
- package/dist/runtime/index-CwXrQ0D9.d.mts.map +1 -0
- package/dist/runtime/{index-CuGwkvfN.d.ts → index-y60Yzgpv.d.ts} +17 -1
- package/dist/runtime/index-y60Yzgpv.d.ts.map +1 -0
- package/dist/runtime/internal/clerk-js/completeSignUpFlow.d.mts +1 -1
- package/dist/runtime/internal/clerk-js/completeSignUpFlow.d.ts +1 -1
- package/dist/runtime/internal/clerk-js/componentGuards.d.mts +1 -1
- package/dist/runtime/internal/clerk-js/componentGuards.d.ts +1 -1
- package/dist/runtime/internal/clerk-js/injectedWeb3EthProviders.d.mts +1 -1
- package/dist/runtime/internal/clerk-js/injectedWeb3EthProviders.d.ts +1 -1
- package/dist/runtime/internal/clerk-js/passkeys.d.mts +2 -2
- package/dist/runtime/internal/clerk-js/passkeys.d.ts +2 -2
- package/dist/runtime/internal/clerk-js/passwords/complexity.d.mts +1 -1
- package/dist/runtime/internal/clerk-js/passwords/complexity.d.ts +1 -1
- package/dist/runtime/internal/clerk-js/passwords/loadZxcvbn.d.mts +1 -1
- package/dist/runtime/internal/clerk-js/passwords/loadZxcvbn.d.ts +1 -1
- package/dist/runtime/internal/clerk-js/passwords/password.d.mts +1 -1
- package/dist/runtime/internal/clerk-js/passwords/password.d.ts +1 -1
- package/dist/runtime/internal/clerk-js/passwords/strength.d.mts +1 -1
- package/dist/runtime/internal/clerk-js/passwords/strength.d.ts +1 -1
- package/dist/runtime/internal/clerk-js/queryParams.d.mts +2 -2
- package/dist/runtime/internal/clerk-js/queryParams.d.ts +2 -2
- package/dist/runtime/internal/clerk-js/redirectUrls.d.mts +1 -1
- package/dist/runtime/internal/clerk-js/redirectUrls.d.ts +1 -1
- package/dist/runtime/internal/clerk-js/sessionTasks.d.mts +2 -2
- package/dist/runtime/internal/clerk-js/sessionTasks.d.ts +2 -2
- package/dist/runtime/internal/clerk-js/url.d.mts +2 -2
- package/dist/runtime/internal/clerk-js/url.d.ts +2 -2
- package/dist/runtime/internal/clerk-js/user.d.mts +1 -1
- package/dist/runtime/internal/clerk-js/user.d.ts +1 -1
- package/dist/runtime/internal/clerk-js/web3.d.mts +1 -1
- package/dist/runtime/internal/clerk-js/web3.d.ts +1 -1
- package/dist/runtime/jwtPayloadParser.d.mts +1 -1
- package/dist/runtime/jwtPayloadParser.d.ts +1 -1
- package/dist/runtime/keys.d.mts +1 -1
- package/dist/runtime/keys.d.ts +1 -1
- package/dist/runtime/loadClerkJsScript.d.mts +1 -1
- package/dist/runtime/loadClerkJsScript.d.ts +1 -1
- package/dist/runtime/loadClerkJsScript.js +2 -2
- package/dist/runtime/loadClerkJsScript.mjs +2 -2
- package/dist/runtime/oauth.d.mts +1 -1
- package/dist/runtime/oauth.d.ts +1 -1
- package/dist/runtime/organization.d.mts +1 -1
- package/dist/runtime/organization.d.ts +1 -1
- package/dist/runtime/pathMatcher.d.mts +1 -1
- package/dist/runtime/pathMatcher.d.ts +1 -1
- package/dist/runtime/poller.d.ts.map +1 -1
- package/dist/runtime/react/index.d.mts +14 -29
- package/dist/runtime/react/index.d.mts.map +1 -1
- package/dist/runtime/react/index.d.ts +14 -29
- package/dist/runtime/react/index.d.ts.map +1 -1
- package/dist/runtime/react/index.js +499 -309
- package/dist/runtime/react/index.js.map +1 -1
- package/dist/runtime/react/index.mjs +498 -304
- package/dist/runtime/react/index.mjs.map +1 -1
- package/dist/runtime/router.d.mts +1 -1
- package/dist/runtime/router.d.ts +1 -1
- package/dist/runtime/saml.d.mts +1 -1
- package/dist/runtime/saml.d.ts +1 -1
- package/dist/runtime/telemetry.d.mts +1 -1
- package/dist/runtime/telemetry.d.ts +1 -1
- package/dist/runtime/types/index.d.mts +1 -1
- package/dist/runtime/types/index.d.ts +1 -1
- package/dist/runtime/ui/index.d.mts +1 -1
- package/dist/runtime/ui/index.d.ts +1 -1
- package/dist/runtime/{url-CeTIrv79.d.mts → url-DBdjYUUx.d.mts} +2 -2
- package/dist/runtime/{url-CeTIrv79.d.mts.map → url-DBdjYUUx.d.mts.map} +1 -1
- package/dist/runtime/{url-B3azMvOn.d.ts → url-yHlyfWdB.d.ts} +2 -2
- package/dist/runtime/{url-B3azMvOn.d.ts.map → url-yHlyfWdB.d.ts.map} +1 -1
- package/dist/runtime/{versionSelector-BvO2gPHd.js → versionSelector-DAcqxcwB.js} +2 -2
- package/dist/runtime/{versionSelector-BvO2gPHd.js.map → versionSelector-DAcqxcwB.js.map} +1 -1
- package/dist/runtime/{versionSelector-B270CtHZ.mjs → versionSelector-DfdpF2HN.mjs} +2 -2
- package/dist/runtime/{versionSelector-B270CtHZ.mjs.map → versionSelector-DfdpF2HN.mjs.map} +1 -1
- package/dist/runtime/versionSelector.js +1 -1
- package/dist/runtime/versionSelector.mjs +1 -1
- package/dist/runtime/web3.d.mts +1 -1
- package/dist/runtime/web3.d.ts +1 -1
- package/dist/types/index.d.mts +16 -0
- package/dist/types/index.d.mts.map +1 -1
- package/dist/types/index.d.ts +16 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +3 -4
- package/dist/runtime/index-C3yQerjv.d.mts.map +0 -1
- package/dist/runtime/index-CuGwkvfN.d.ts.map +0 -1
|
@@ -10,11 +10,10 @@ import "../noop-Dzu7lrge.mjs";
|
|
|
10
10
|
import "../underscore-Cv_9NQ-X.mjs";
|
|
11
11
|
import { a as eventMethodCalled } from "../telemetry-DnVD78JJ.mjs";
|
|
12
12
|
import { t as createDeferredPromise } from "../createDeferredPromise-DjiBNELG.mjs";
|
|
13
|
+
import * as React$1 from "react";
|
|
13
14
|
import React, { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
14
|
-
import
|
|
15
|
-
import useSWRInfinite from "swr/infinite";
|
|
15
|
+
import { InfiniteQueryObserver, QueryObserver, noop, notifyManager } from "@tanstack/query-core";
|
|
16
16
|
import { dequal } from "dequal";
|
|
17
|
-
import useSWRMutation from "swr/mutation";
|
|
18
17
|
|
|
19
18
|
//#region src/react/hooks/createContextAndHook.ts
|
|
20
19
|
/**
|
|
@@ -54,15 +53,6 @@ const createContextAndHook = (displayName, options) => {
|
|
|
54
53
|
];
|
|
55
54
|
};
|
|
56
55
|
|
|
57
|
-
//#endregion
|
|
58
|
-
//#region src/react/providers/SWRConfigCompat.swr.tsx
|
|
59
|
-
/**
|
|
60
|
-
* @internal
|
|
61
|
-
*/
|
|
62
|
-
function SWRConfigCompat({ swrConfig, children }) {
|
|
63
|
-
return /* @__PURE__ */ React.createElement(SWRConfig, { value: swrConfig }, children);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
56
|
//#endregion
|
|
67
57
|
//#region src/react/contexts.tsx
|
|
68
58
|
const [ClerkInstanceContext, useClerkInstanceContext] = createContextAndHook("ClerkInstanceContext");
|
|
@@ -83,8 +73,8 @@ function useOptionsContext() {
|
|
|
83
73
|
return context;
|
|
84
74
|
}
|
|
85
75
|
const [OrganizationContextInternal, useOrganizationContext] = createContextAndHook("OrganizationContext");
|
|
86
|
-
const OrganizationProvider = ({ children, organization
|
|
87
|
-
return /* @__PURE__ */ React.createElement(
|
|
76
|
+
const OrganizationProvider = ({ children, organization }) => {
|
|
77
|
+
return /* @__PURE__ */ React.createElement(OrganizationContextInternal.Provider, { value: { value: { organization } } }, children);
|
|
88
78
|
};
|
|
89
79
|
/**
|
|
90
80
|
* @internal
|
|
@@ -170,83 +160,123 @@ function createCacheKeys(params) {
|
|
|
170
160
|
authenticated: params.authenticated
|
|
171
161
|
};
|
|
172
162
|
}
|
|
163
|
+
|
|
164
|
+
//#endregion
|
|
165
|
+
//#region src/react/clerk-rq/keep-previous-data.ts
|
|
173
166
|
/**
|
|
174
167
|
* @internal
|
|
175
168
|
*/
|
|
176
|
-
function
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
type: queryKey[0],
|
|
180
|
-
...queryKey[2],
|
|
181
|
-
...queryKey[3].args
|
|
169
|
+
function defineKeepPreviousDataFn(enabled) {
|
|
170
|
+
if (enabled) return function KeepPreviousDataFn(previousData) {
|
|
171
|
+
return previousData;
|
|
182
172
|
};
|
|
183
173
|
}
|
|
184
174
|
|
|
185
175
|
//#endregion
|
|
186
|
-
//#region src/react/
|
|
176
|
+
//#region src/react/clerk-rq/use-clerk-query-client.ts
|
|
187
177
|
/**
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
* @example
|
|
194
|
-
* ```typescript
|
|
195
|
-
* // Example 1: With user-provided options
|
|
196
|
-
* const userOptions = { initialPage: 2, pageSize: 20, infinite: true };
|
|
197
|
-
* const defaults = { initialPage: 1, pageSize: 10, infinite: false };
|
|
198
|
-
* useWithSafeValues(userOptions, defaults);
|
|
199
|
-
* // Returns { initialPage: 2, pageSize: 20, infinite: true }
|
|
200
|
-
*
|
|
201
|
-
* // Example 2: With boolean true (use defaults)
|
|
202
|
-
* const params = true;
|
|
203
|
-
* const defaults = { initialPage: 1, pageSize: 10, infinite: false };
|
|
204
|
-
* useWithSafeValues(params, defaults);
|
|
205
|
-
* // Returns { initialPage: 1, pageSize: 10, infinite: false }
|
|
178
|
+
* Creates a recursively self-referential Proxy that safely handles:
|
|
179
|
+
* - Arbitrary property access (e.g., obj.any.prop.path)
|
|
180
|
+
* - Function calls at any level (e.g., obj.a().b.c())
|
|
181
|
+
* - Construction (e.g., new obj.a.b())
|
|
206
182
|
*
|
|
207
|
-
*
|
|
208
|
-
* const params = undefined;
|
|
209
|
-
* const defaults = { initialPage: 1, pageSize: 10, infinite: false };
|
|
210
|
-
* useWithSafeValues(params, defaults);
|
|
211
|
-
* // Returns { initialPage: 1, pageSize: 10, infinite: false }
|
|
212
|
-
* ```
|
|
183
|
+
* Always returns itself to allow infinite chaining without throwing.
|
|
213
184
|
*/
|
|
214
|
-
|
|
215
|
-
const
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
185
|
+
function createRecursiveProxy(label) {
|
|
186
|
+
const callableTarget = function noop$1() {};
|
|
187
|
+
let self;
|
|
188
|
+
self = new Proxy(callableTarget, {
|
|
189
|
+
get(_target, prop) {
|
|
190
|
+
if (prop === "then") return;
|
|
191
|
+
if (prop === "toString") return () => `[${label}]`;
|
|
192
|
+
if (prop === Symbol.toPrimitive) return () => 0;
|
|
193
|
+
return self;
|
|
194
|
+
},
|
|
195
|
+
apply() {
|
|
196
|
+
return self;
|
|
197
|
+
},
|
|
198
|
+
construct() {
|
|
199
|
+
return self;
|
|
200
|
+
},
|
|
201
|
+
has() {
|
|
202
|
+
return false;
|
|
203
|
+
},
|
|
204
|
+
set() {
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
return self;
|
|
209
|
+
}
|
|
210
|
+
const mockQueryClient = createRecursiveProxy("ClerkMockQueryClient");
|
|
211
|
+
const useClerkQueryClient = () => {
|
|
212
|
+
const clerk = useClerkInstanceContext();
|
|
213
|
+
const queryClient = clerk.__internal_queryClient;
|
|
214
|
+
const [, setQueryClientLoaded] = useState(typeof queryClient === "object" && "__tag" in queryClient && queryClient.__tag === "clerk-rq-client");
|
|
215
|
+
useEffect(() => {
|
|
216
|
+
const _setQueryClientLoaded = () => setQueryClientLoaded(true);
|
|
217
|
+
clerk.on("queryClientStatus", _setQueryClientLoaded);
|
|
218
|
+
return () => {
|
|
219
|
+
clerk.off("queryClientStatus", _setQueryClientLoaded);
|
|
220
|
+
};
|
|
221
|
+
}, [clerk, setQueryClientLoaded]);
|
|
222
|
+
const isLoaded = typeof queryClient === "object" && "__tag" in queryClient && queryClient.__tag === "clerk-rq-client";
|
|
223
|
+
return [queryClient?.client || mockQueryClient, isLoaded];
|
|
225
224
|
};
|
|
225
|
+
|
|
226
|
+
//#endregion
|
|
227
|
+
//#region src/react/clerk-rq/useBaseQuery.ts
|
|
228
|
+
/**
|
|
229
|
+
* Stripped down version of useBaseQuery from @tanstack/query-core.
|
|
230
|
+
* This implementation allows for an observer to be created every time a query client changes.
|
|
231
|
+
*/
|
|
226
232
|
/**
|
|
227
|
-
*
|
|
228
|
-
* Useful for extracting unique parameters that should be passed to a request while excluding common cache keys.
|
|
233
|
+
* An alternative `useBaseQuery` implementation that allows for an observer to be created every time a query client changes.
|
|
229
234
|
*
|
|
230
235
|
* @internal
|
|
236
|
+
*/
|
|
237
|
+
function useBaseQuery(options, Observer) {
|
|
238
|
+
const [client, isQueryClientLoaded] = useClerkQueryClient();
|
|
239
|
+
const defaultedOptions = isQueryClientLoaded ? client.defaultQueryOptions(options) : options;
|
|
240
|
+
defaultedOptions._optimisticResults = "optimistic";
|
|
241
|
+
const observer = React$1.useMemo(() => {
|
|
242
|
+
return new Observer(client, defaultedOptions);
|
|
243
|
+
}, [client]);
|
|
244
|
+
const result = observer.getOptimisticResult(defaultedOptions);
|
|
245
|
+
const shouldSubscribe = options.subscribed !== false;
|
|
246
|
+
React$1.useSyncExternalStore(React$1.useCallback((onStoreChange) => {
|
|
247
|
+
const unsubscribe = shouldSubscribe ? observer.subscribe(notifyManager.batchCalls(onStoreChange)) : noop;
|
|
248
|
+
observer.updateResult();
|
|
249
|
+
return unsubscribe;
|
|
250
|
+
}, [observer, shouldSubscribe]), () => observer.getCurrentResult(), () => observer.getCurrentResult());
|
|
251
|
+
React$1.useEffect(() => {
|
|
252
|
+
observer.setOptions(defaultedOptions);
|
|
253
|
+
}, [defaultedOptions, observer]);
|
|
254
|
+
if (!isQueryClientLoaded) return {
|
|
255
|
+
data: void 0,
|
|
256
|
+
error: null,
|
|
257
|
+
isLoading: false,
|
|
258
|
+
isFetching: false,
|
|
259
|
+
status: "pending"
|
|
260
|
+
};
|
|
261
|
+
return !defaultedOptions.notifyOnChangeProps ? observer.trackResult(result) : result;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
//#endregion
|
|
265
|
+
//#region src/react/clerk-rq/useInfiniteQuery.ts
|
|
266
|
+
/**
|
|
267
|
+
*
|
|
268
|
+
*/
|
|
269
|
+
function useClerkInfiniteQuery(options) {
|
|
270
|
+
return useBaseQuery(options, InfiniteQueryObserver);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
//#endregion
|
|
274
|
+
//#region src/react/clerk-rq/useQuery.ts
|
|
275
|
+
/**
|
|
231
276
|
*
|
|
232
|
-
* @example
|
|
233
|
-
* ```typescript
|
|
234
|
-
* // Example 1: Basic usage
|
|
235
|
-
* const obj1 = { name: 'John', age: 30, city: 'NY' };
|
|
236
|
-
* const obj2 = { name: 'John', age: 30 };
|
|
237
|
-
* getDifferentKeys(obj1, obj2); // Returns { city: 'NY' }
|
|
238
|
-
*
|
|
239
|
-
* // Example 2: With cache keys
|
|
240
|
-
* const requestParams = { page: 1, limit: 10, userId: '123' };
|
|
241
|
-
* const cacheKeys = { userId: '123' };
|
|
242
|
-
* getDifferentKeys(requestParams, cacheKeys); // Returns { page: 1, limit: 10 }
|
|
243
|
-
* ```
|
|
244
277
|
*/
|
|
245
|
-
function
|
|
246
|
-
|
|
247
|
-
const differentKeysObject = {};
|
|
248
|
-
for (const key1 of Object.keys(obj1)) if (!keysSet.has(key1)) differentKeysObject[key1] = obj1[key1];
|
|
249
|
-
return differentKeysObject;
|
|
278
|
+
function useClerkQuery(options) {
|
|
279
|
+
return useBaseQuery(options, QueryObserver);
|
|
250
280
|
}
|
|
251
281
|
|
|
252
282
|
//#endregion
|
|
@@ -277,114 +307,249 @@ function usePreviousValue(value) {
|
|
|
277
307
|
}
|
|
278
308
|
|
|
279
309
|
//#endregion
|
|
280
|
-
//#region src/react/hooks/
|
|
281
|
-
const
|
|
282
|
-
dedupingInterval: 1e3 * 60,
|
|
283
|
-
focusThrottleInterval: 1e3 * 60 * 2
|
|
284
|
-
};
|
|
285
|
-
const cachingSWRInfiniteOptions = {
|
|
286
|
-
...cachingSWROptions,
|
|
287
|
-
revalidateFirstPage: false
|
|
288
|
-
};
|
|
310
|
+
//#region src/react/hooks/useClearQueriesOnSignOut.ts
|
|
311
|
+
const withInfiniteKey = (key) => [key, `${key}-inf`];
|
|
289
312
|
/**
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
* The hook can operate in two modes:
|
|
293
|
-
* - Traditional pagination: Fetches one page at a time with page navigation
|
|
294
|
-
* - Infinite loading: Accumulates data as more pages are loaded.
|
|
295
|
-
*
|
|
296
|
-
* Features:
|
|
297
|
-
* - Cache management with SWR
|
|
298
|
-
* - Loading and error states
|
|
299
|
-
* - Page navigation helpers
|
|
300
|
-
* - Data revalidation and updates
|
|
301
|
-
* - Support for keeping previous data while loading.
|
|
313
|
+
* Clears React Query caches associated with the given stable prefixes when
|
|
314
|
+
* the authenticated state transitions from signed-in to signed-out.
|
|
302
315
|
*
|
|
303
316
|
* @internal
|
|
304
317
|
*/
|
|
318
|
+
function useClearQueriesOnSignOut(options) {
|
|
319
|
+
const { isSignedOut, stableKeys, authenticated = true, onCleanup } = options;
|
|
320
|
+
const stableKeysRef = useRef(stableKeys);
|
|
321
|
+
const [queryClient] = useClerkQueryClient();
|
|
322
|
+
const previousIsSignedIn = usePreviousValue(!isSignedOut);
|
|
323
|
+
if (authenticated !== true) return;
|
|
324
|
+
useEffect(() => {
|
|
325
|
+
if (previousIsSignedIn && isSignedOut === true) {
|
|
326
|
+
queryClient.removeQueries({ predicate: (query) => {
|
|
327
|
+
const [cachedStableKey, queryAuthenticated] = query.queryKey;
|
|
328
|
+
return queryAuthenticated === true && typeof cachedStableKey === "string" && (Array.isArray(stableKeysRef.current) ? stableKeysRef.current.includes(cachedStableKey) : stableKeysRef.current === cachedStableKey);
|
|
329
|
+
} });
|
|
330
|
+
onCleanup?.();
|
|
331
|
+
}
|
|
332
|
+
}, [
|
|
333
|
+
isSignedOut,
|
|
334
|
+
previousIsSignedIn,
|
|
335
|
+
queryClient
|
|
336
|
+
]);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
//#endregion
|
|
340
|
+
//#region src/react/hooks/usePagesOrInfinite.shared.ts
|
|
341
|
+
/**
|
|
342
|
+
* A hook that safely merges user-provided pagination options with default values.
|
|
343
|
+
* It caches initial pagination values (page and size) until component unmount to prevent unwanted rerenders.
|
|
344
|
+
*
|
|
345
|
+
* @internal
|
|
346
|
+
*
|
|
347
|
+
* @example
|
|
348
|
+
* ```typescript
|
|
349
|
+
* // Example 1: With user-provided options
|
|
350
|
+
* const userOptions = { initialPage: 2, pageSize: 20, infinite: true };
|
|
351
|
+
* const defaults = { initialPage: 1, pageSize: 10, infinite: false };
|
|
352
|
+
* useWithSafeValues(userOptions, defaults);
|
|
353
|
+
* // Returns { initialPage: 2, pageSize: 20, infinite: true }
|
|
354
|
+
*
|
|
355
|
+
* // Example 2: With boolean true (use defaults)
|
|
356
|
+
* const params = true;
|
|
357
|
+
* const defaults = { initialPage: 1, pageSize: 10, infinite: false };
|
|
358
|
+
* useWithSafeValues(params, defaults);
|
|
359
|
+
* // Returns { initialPage: 1, pageSize: 10, infinite: false }
|
|
360
|
+
*
|
|
361
|
+
* // Example 3: With undefined options (fallback to defaults)
|
|
362
|
+
* const params = undefined;
|
|
363
|
+
* const defaults = { initialPage: 1, pageSize: 10, infinite: false };
|
|
364
|
+
* useWithSafeValues(params, defaults);
|
|
365
|
+
* // Returns { initialPage: 1, pageSize: 10, infinite: false }
|
|
366
|
+
* ```
|
|
367
|
+
*/
|
|
368
|
+
const useWithSafeValues = (params, defaultValues) => {
|
|
369
|
+
const shouldUseDefaults = typeof params === "boolean" && params;
|
|
370
|
+
const initialPageRef = useRef(shouldUseDefaults ? defaultValues.initialPage : params?.initialPage ?? defaultValues.initialPage);
|
|
371
|
+
const pageSizeRef = useRef(shouldUseDefaults ? defaultValues.pageSize : params?.pageSize ?? defaultValues.pageSize);
|
|
372
|
+
const newObj = {};
|
|
373
|
+
for (const key of Object.keys(defaultValues)) newObj[key] = shouldUseDefaults ? defaultValues[key] : params?.[key] ?? defaultValues[key];
|
|
374
|
+
return {
|
|
375
|
+
...newObj,
|
|
376
|
+
initialPage: initialPageRef.current,
|
|
377
|
+
pageSize: pageSizeRef.current
|
|
378
|
+
};
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
//#endregion
|
|
382
|
+
//#region src/react/hooks/usePagesOrInfinite.tsx
|
|
305
383
|
const usePagesOrInfinite = (params) => {
|
|
306
384
|
const { fetcher, config, keys } = params;
|
|
307
385
|
const [paginatedPage, setPaginatedPage] = useState(config.initialPage ?? 1);
|
|
308
386
|
const initialPageRef = useRef(config.initialPage ?? 1);
|
|
309
387
|
const pageSizeRef = useRef(config.pageSize ?? 10);
|
|
310
388
|
const enabled = config.enabled ?? true;
|
|
311
|
-
const
|
|
389
|
+
const isSignedIn = config.isSignedIn;
|
|
312
390
|
const triggerInfinite = config.infinite ?? false;
|
|
391
|
+
const cacheMode = config.__experimental_mode === "cache";
|
|
313
392
|
const keepPreviousData = config.keepPreviousData ?? false;
|
|
314
|
-
const
|
|
315
|
-
const
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
};
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
393
|
+
const [queryClient] = useClerkQueryClient();
|
|
394
|
+
const queriesEnabled = enabled && Boolean(fetcher) && !cacheMode && isSignedIn !== false;
|
|
395
|
+
const [forceUpdateCounter, setForceUpdateCounter] = useState(0);
|
|
396
|
+
const forceUpdate = useCallback((updater) => {
|
|
397
|
+
setForceUpdateCounter(updater);
|
|
398
|
+
}, []);
|
|
399
|
+
const pagesQueryKey = useMemo(() => {
|
|
400
|
+
const [stablePrefix, authenticated, tracked, untracked] = keys.queryKey;
|
|
401
|
+
return [
|
|
402
|
+
stablePrefix,
|
|
403
|
+
authenticated,
|
|
404
|
+
tracked,
|
|
405
|
+
{
|
|
406
|
+
...untracked,
|
|
407
|
+
args: {
|
|
408
|
+
...untracked.args,
|
|
409
|
+
initialPage: paginatedPage,
|
|
410
|
+
pageSize: pageSizeRef.current
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
];
|
|
414
|
+
}, [keys.queryKey, paginatedPage]);
|
|
415
|
+
const singlePageQuery = useClerkQuery({
|
|
416
|
+
queryKey: pagesQueryKey,
|
|
417
|
+
queryFn: ({ queryKey }) => {
|
|
418
|
+
const { args } = queryKey[3];
|
|
419
|
+
if (!fetcher) return;
|
|
420
|
+
return fetcher(args);
|
|
421
|
+
},
|
|
422
|
+
staleTime: 6e4,
|
|
423
|
+
enabled: queriesEnabled && !triggerInfinite,
|
|
424
|
+
placeholderData: defineKeepPreviousDataFn(keepPreviousData)
|
|
331
425
|
});
|
|
332
|
-
const
|
|
333
|
-
|
|
426
|
+
const infiniteQueryKey = useMemo(() => {
|
|
427
|
+
const [stablePrefix, authenticated, tracked, untracked] = keys.queryKey;
|
|
428
|
+
return [
|
|
429
|
+
stablePrefix + "-inf",
|
|
430
|
+
authenticated,
|
|
431
|
+
tracked,
|
|
432
|
+
untracked
|
|
433
|
+
];
|
|
434
|
+
}, [keys.queryKey]);
|
|
435
|
+
const infiniteQuery = useClerkInfiniteQuery({
|
|
436
|
+
queryKey: infiniteQueryKey,
|
|
437
|
+
initialPageParam: config.initialPage ?? 1,
|
|
438
|
+
getNextPageParam: (lastPage, allPages, lastPageParam) => {
|
|
439
|
+
const total = lastPage?.total_count ?? 0;
|
|
440
|
+
return (allPages.length + (config.initialPage ? config.initialPage - 1 : 0)) * (config.pageSize ?? 10) < total ? lastPageParam + 1 : void 0;
|
|
441
|
+
},
|
|
442
|
+
queryFn: ({ pageParam, queryKey }) => {
|
|
443
|
+
const { args } = queryKey[3];
|
|
444
|
+
if (!fetcher) return;
|
|
445
|
+
return fetcher({
|
|
446
|
+
...args,
|
|
447
|
+
initialPage: pageParam,
|
|
448
|
+
pageSize: pageSizeRef.current
|
|
449
|
+
});
|
|
450
|
+
},
|
|
451
|
+
staleTime: 6e4,
|
|
452
|
+
enabled: queriesEnabled && triggerInfinite
|
|
453
|
+
});
|
|
454
|
+
useClearQueriesOnSignOut({
|
|
455
|
+
isSignedOut: isSignedIn === false,
|
|
456
|
+
authenticated: keys.authenticated,
|
|
457
|
+
stableKeys: withInfiniteKey(keys.stableKey),
|
|
458
|
+
onCleanup: () => {
|
|
459
|
+
setPaginatedPage(initialPageRef.current);
|
|
460
|
+
Promise.resolve().then(() => forceUpdate((n) => n + 1));
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
const { data, count, page } = useMemo(() => {
|
|
464
|
+
if (triggerInfinite) {
|
|
465
|
+
const cachedData = queryClient.getQueryData(infiniteQueryKey);
|
|
466
|
+
const pages = queriesEnabled ? infiniteQuery.data?.pages ?? cachedData?.pages ?? [] : cachedData?.pages ?? [];
|
|
467
|
+
const validPages = Array.isArray(pages) ? pages.filter(Boolean) : [];
|
|
468
|
+
return {
|
|
469
|
+
data: validPages.map((a) => a?.data).flat().filter(Boolean) ?? [],
|
|
470
|
+
count: validPages[validPages.length - 1]?.total_count ?? 0,
|
|
471
|
+
page: validPages.length > 0 ? validPages.length : initialPageRef.current
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
const pageData = queriesEnabled ? singlePageQuery.data ?? queryClient.getQueryData(pagesQueryKey) : queryClient.getQueryData(pagesQueryKey);
|
|
334
475
|
return {
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
476
|
+
data: Array.isArray(pageData?.data) ? pageData.data : [],
|
|
477
|
+
count: typeof pageData?.total_count === "number" ? pageData.total_count : 0,
|
|
478
|
+
page: paginatedPage
|
|
338
479
|
};
|
|
339
|
-
}, (cacheKeyParams) => {
|
|
340
|
-
const requestParams = getDifferentKeys(cacheKeyParams, {
|
|
341
|
-
type: keys.queryKey[0],
|
|
342
|
-
...keys.queryKey[2]
|
|
343
|
-
});
|
|
344
|
-
return fetcher?.(requestParams);
|
|
345
|
-
}, cachingSWRInfiniteOptions);
|
|
346
|
-
const page = useMemo(() => {
|
|
347
|
-
if (triggerInfinite) return size;
|
|
348
|
-
return paginatedPage;
|
|
349
480
|
}, [
|
|
481
|
+
queriesEnabled,
|
|
482
|
+
forceUpdateCounter,
|
|
350
483
|
triggerInfinite,
|
|
351
|
-
|
|
484
|
+
infiniteQuery.data?.pages,
|
|
485
|
+
singlePageQuery.data,
|
|
486
|
+
queryClient,
|
|
487
|
+
infiniteQueryKey,
|
|
488
|
+
pagesQueryKey,
|
|
352
489
|
paginatedPage
|
|
353
490
|
]);
|
|
354
491
|
const fetchPage = useCallback((numberOrgFn) => {
|
|
355
492
|
if (triggerInfinite) {
|
|
356
|
-
|
|
493
|
+
const next = typeof numberOrgFn === "function" ? numberOrgFn(page) : numberOrgFn;
|
|
494
|
+
const targetCount = Math.max(0, next);
|
|
495
|
+
const cachedData = queryClient.getQueryData(infiniteQueryKey);
|
|
496
|
+
if (targetCount - (infiniteQuery.data?.pages ?? cachedData?.pages ?? []).length > 0) infiniteQuery.fetchNextPage({ cancelRefetch: false });
|
|
357
497
|
return;
|
|
358
498
|
}
|
|
359
499
|
return setPaginatedPage(numberOrgFn);
|
|
360
|
-
}, [setSize, triggerInfinite]);
|
|
361
|
-
const data = useMemo(() => {
|
|
362
|
-
if (triggerInfinite) return swrInfiniteData?.map((a) => a?.data).flat() ?? [];
|
|
363
|
-
return swrData?.data ?? [];
|
|
364
|
-
}, [
|
|
365
|
-
triggerInfinite,
|
|
366
|
-
swrData,
|
|
367
|
-
swrInfiniteData
|
|
368
|
-
]);
|
|
369
|
-
const count = useMemo(() => {
|
|
370
|
-
if (triggerInfinite) return swrInfiniteData?.[swrInfiniteData?.length - 1]?.total_count || 0;
|
|
371
|
-
return swrData?.total_count ?? 0;
|
|
372
500
|
}, [
|
|
501
|
+
infiniteQuery,
|
|
502
|
+
page,
|
|
373
503
|
triggerInfinite,
|
|
374
|
-
|
|
375
|
-
|
|
504
|
+
queryClient,
|
|
505
|
+
infiniteQueryKey
|
|
376
506
|
]);
|
|
377
|
-
const isLoading = triggerInfinite ?
|
|
378
|
-
const isFetching = triggerInfinite ?
|
|
379
|
-
const error = (triggerInfinite ?
|
|
507
|
+
const isLoading = triggerInfinite ? infiniteQuery.isLoading : singlePageQuery.isLoading;
|
|
508
|
+
const isFetching = triggerInfinite ? infiniteQuery.isFetching : singlePageQuery.isFetching;
|
|
509
|
+
const error = (triggerInfinite ? infiniteQuery.error : singlePageQuery.error) ?? null;
|
|
380
510
|
const isError = !!error;
|
|
381
511
|
const fetchNext = useCallback(() => {
|
|
382
|
-
|
|
383
|
-
|
|
512
|
+
if (triggerInfinite) {
|
|
513
|
+
infiniteQuery.fetchNextPage({ cancelRefetch: false });
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
setPaginatedPage((n) => Math.max(0, n + 1));
|
|
517
|
+
}, [infiniteQuery, triggerInfinite]);
|
|
384
518
|
const fetchPrevious = useCallback(() => {
|
|
385
|
-
|
|
386
|
-
|
|
519
|
+
if (triggerInfinite) return;
|
|
520
|
+
setPaginatedPage((n) => Math.max(0, n - 1));
|
|
521
|
+
}, [triggerInfinite]);
|
|
387
522
|
const offsetCount = (initialPageRef.current - 1) * pageSizeRef.current;
|
|
523
|
+
const pageCount = Math.ceil((count - offsetCount) / pageSizeRef.current);
|
|
524
|
+
const hasNextPage = triggerInfinite ? Boolean(infiniteQuery.hasNextPage) : count - offsetCount * pageSizeRef.current > page * pageSizeRef.current;
|
|
525
|
+
const hasPreviousPage = triggerInfinite ? Boolean(infiniteQuery.hasPreviousPage) : (page - 1) * pageSizeRef.current > offsetCount * pageSizeRef.current;
|
|
526
|
+
const setData = (value) => {
|
|
527
|
+
if (triggerInfinite) {
|
|
528
|
+
queryClient.setQueryData(infiniteQueryKey, (prevValue = {}) => {
|
|
529
|
+
const prevPages = Array.isArray(prevValue?.pages) ? prevValue.pages : [];
|
|
530
|
+
const nextPages = typeof value === "function" ? value(prevPages) : value;
|
|
531
|
+
return {
|
|
532
|
+
...prevValue,
|
|
533
|
+
pages: nextPages
|
|
534
|
+
};
|
|
535
|
+
});
|
|
536
|
+
forceUpdate((n) => n + 1);
|
|
537
|
+
return Promise.resolve();
|
|
538
|
+
}
|
|
539
|
+
queryClient.setQueryData(pagesQueryKey, (prevValue = {
|
|
540
|
+
data: [],
|
|
541
|
+
total_count: 0
|
|
542
|
+
}) => {
|
|
543
|
+
return typeof value === "function" ? value(prevValue) : value;
|
|
544
|
+
});
|
|
545
|
+
forceUpdate((n) => n + 1);
|
|
546
|
+
return Promise.resolve();
|
|
547
|
+
};
|
|
548
|
+
const revalidate = async () => {
|
|
549
|
+
await queryClient.invalidateQueries({ queryKey: keys.invalidationKey });
|
|
550
|
+
const [stablePrefix, ...rest] = keys.invalidationKey;
|
|
551
|
+
return queryClient.invalidateQueries({ queryKey: [stablePrefix + "-inf", ...rest] });
|
|
552
|
+
};
|
|
388
553
|
return {
|
|
389
554
|
data,
|
|
390
555
|
count,
|
|
@@ -393,19 +558,19 @@ const usePagesOrInfinite = (params) => {
|
|
|
393
558
|
isFetching,
|
|
394
559
|
isError,
|
|
395
560
|
page,
|
|
396
|
-
pageCount
|
|
561
|
+
pageCount,
|
|
397
562
|
fetchPage,
|
|
398
563
|
fetchNext,
|
|
399
564
|
fetchPrevious,
|
|
400
|
-
hasNextPage
|
|
401
|
-
hasPreviousPage
|
|
402
|
-
revalidate
|
|
403
|
-
setData
|
|
565
|
+
hasNextPage,
|
|
566
|
+
hasPreviousPage,
|
|
567
|
+
revalidate,
|
|
568
|
+
setData
|
|
404
569
|
};
|
|
405
570
|
};
|
|
406
571
|
|
|
407
572
|
//#endregion
|
|
408
|
-
//#region src/react/hooks/useAPIKeys.
|
|
573
|
+
//#region src/react/hooks/useAPIKeys.tsx
|
|
409
574
|
/**
|
|
410
575
|
* @internal
|
|
411
576
|
*
|
|
@@ -466,7 +631,7 @@ function useAPIKeys(params) {
|
|
|
466
631
|
...safeValues.query ? { query: safeValues.query } : {}
|
|
467
632
|
};
|
|
468
633
|
const isEnabled = (safeValues.enabled ?? true) && clerk.loaded;
|
|
469
|
-
|
|
634
|
+
return usePagesOrInfinite({
|
|
470
635
|
fetcher: clerk.apiKeys?.getAll ? (params$1) => clerk.apiKeys.getAll({
|
|
471
636
|
...params$1,
|
|
472
637
|
subject: safeValues.subject
|
|
@@ -486,17 +651,6 @@ function useAPIKeys(params) {
|
|
|
486
651
|
untracked: { args: hookParams }
|
|
487
652
|
})
|
|
488
653
|
});
|
|
489
|
-
const { mutate } = useSWRConfig();
|
|
490
|
-
const invalidateAll = useCallback(() => {
|
|
491
|
-
return mutate((key) => {
|
|
492
|
-
if (!key || typeof key !== "object") return false;
|
|
493
|
-
return "type" in key && key.type === "apiKeys" && "subject" in key && key.subject === safeValues.subject;
|
|
494
|
-
});
|
|
495
|
-
}, [mutate, safeValues.subject]);
|
|
496
|
-
return {
|
|
497
|
-
...result,
|
|
498
|
-
revalidate: invalidateAll
|
|
499
|
-
};
|
|
500
654
|
}
|
|
501
655
|
|
|
502
656
|
//#endregion
|
|
@@ -1137,7 +1291,7 @@ const useSafeLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffec
|
|
|
1137
1291
|
|
|
1138
1292
|
//#endregion
|
|
1139
1293
|
//#region src/react/hooks/useSession.ts
|
|
1140
|
-
const hookName$
|
|
1294
|
+
const hookName$2 = `useSession`;
|
|
1141
1295
|
/**
|
|
1142
1296
|
* The `useSession()` hook provides access to the current user's [`Session`](https://clerk.com/docs/reference/javascript/session) object, as well as helpers for setting the active session.
|
|
1143
1297
|
*
|
|
@@ -1187,10 +1341,10 @@ const hookName$3 = `useSession`;
|
|
|
1187
1341
|
* </Tabs>
|
|
1188
1342
|
*/
|
|
1189
1343
|
const useSession = () => {
|
|
1190
|
-
useAssertWrappedByClerkProvider(hookName$
|
|
1344
|
+
useAssertWrappedByClerkProvider(hookName$2);
|
|
1191
1345
|
const session = useSessionContext();
|
|
1192
1346
|
const clerk = useClerkInstanceContext();
|
|
1193
|
-
clerk.telemetry?.record(eventMethodCalled(hookName$
|
|
1347
|
+
clerk.telemetry?.record(eventMethodCalled(hookName$2));
|
|
1194
1348
|
if (session === void 0) return {
|
|
1195
1349
|
isLoaded: false,
|
|
1196
1350
|
isSignedIn: void 0,
|
|
@@ -1210,7 +1364,7 @@ const useSession = () => {
|
|
|
1210
1364
|
|
|
1211
1365
|
//#endregion
|
|
1212
1366
|
//#region src/react/hooks/useSessionList.ts
|
|
1213
|
-
const hookName$
|
|
1367
|
+
const hookName$1 = "useSessionList";
|
|
1214
1368
|
/**
|
|
1215
1369
|
* The `useSessionList()` hook returns an array of [`Session`](https://clerk.com/docs/reference/javascript/session) objects that have been registered on the client device.
|
|
1216
1370
|
*
|
|
@@ -1255,10 +1409,10 @@ const hookName$2 = "useSessionList";
|
|
|
1255
1409
|
* </Tabs>
|
|
1256
1410
|
*/
|
|
1257
1411
|
const useSessionList = () => {
|
|
1258
|
-
useAssertWrappedByClerkProvider(hookName$
|
|
1412
|
+
useAssertWrappedByClerkProvider(hookName$1);
|
|
1259
1413
|
const isomorphicClerk = useClerkInstanceContext();
|
|
1260
1414
|
const client = useClientContext();
|
|
1261
|
-
useClerkInstanceContext().telemetry?.record(eventMethodCalled(hookName$
|
|
1415
|
+
useClerkInstanceContext().telemetry?.record(eventMethodCalled(hookName$1));
|
|
1262
1416
|
if (!client) return {
|
|
1263
1417
|
isLoaded: false,
|
|
1264
1418
|
sessions: void 0,
|
|
@@ -1273,7 +1427,7 @@ const useSessionList = () => {
|
|
|
1273
1427
|
|
|
1274
1428
|
//#endregion
|
|
1275
1429
|
//#region src/react/hooks/useUser.ts
|
|
1276
|
-
const hookName
|
|
1430
|
+
const hookName = "useUser";
|
|
1277
1431
|
/**
|
|
1278
1432
|
* The `useUser()` hook provides access to the current user's [`User`](https://clerk.com/docs/reference/javascript/user) object, which contains all the data for a single user in your application and provides methods to manage their account. This hook also allows you to check if the user is signed in and if Clerk has loaded and initialized.
|
|
1279
1433
|
*
|
|
@@ -1405,9 +1559,9 @@ const hookName$1 = "useUser";
|
|
|
1405
1559
|
* </Tabs>
|
|
1406
1560
|
*/
|
|
1407
1561
|
function useUser() {
|
|
1408
|
-
useAssertWrappedByClerkProvider(hookName
|
|
1562
|
+
useAssertWrappedByClerkProvider(hookName);
|
|
1409
1563
|
const user = useUserContext();
|
|
1410
|
-
useClerkInstanceContext().telemetry?.record(eventMethodCalled(hookName
|
|
1564
|
+
useClerkInstanceContext().telemetry?.record(eventMethodCalled(hookName));
|
|
1411
1565
|
if (user === void 0) return {
|
|
1412
1566
|
isLoaded: false,
|
|
1413
1567
|
isSignedIn: void 0,
|
|
@@ -1610,11 +1764,11 @@ function useBillingHookEnabled(params) {
|
|
|
1610
1764
|
*
|
|
1611
1765
|
* @internal
|
|
1612
1766
|
*/
|
|
1613
|
-
function createBillingPaginatedHook({ hookName: hookName$
|
|
1767
|
+
function createBillingPaginatedHook({ hookName: hookName$3, resourceType, useFetcher, options }) {
|
|
1614
1768
|
return function useBillingHook(params) {
|
|
1615
1769
|
const { for: _for, enabled: externalEnabled,...paginationParams } = params || {};
|
|
1616
1770
|
const safeFor = _for || "user";
|
|
1617
|
-
useAssertWrappedByClerkProvider(hookName$
|
|
1771
|
+
useAssertWrappedByClerkProvider(hookName$3);
|
|
1618
1772
|
const fetchFn = useFetcher(safeFor);
|
|
1619
1773
|
const safeValues = useWithSafeValues(paginationParams, {
|
|
1620
1774
|
initialPage: 1,
|
|
@@ -1626,7 +1780,7 @@ function createBillingPaginatedHook({ hookName: hookName$4, resourceType, useFet
|
|
|
1626
1780
|
const clerk = useClerkInstanceContext();
|
|
1627
1781
|
const user = useUserContext();
|
|
1628
1782
|
const { organization } = useOrganizationContext();
|
|
1629
|
-
clerk.telemetry?.record(eventMethodCalled(hookName$
|
|
1783
|
+
clerk.telemetry?.record(eventMethodCalled(hookName$3));
|
|
1630
1784
|
const isForOrganization = safeFor === "organization";
|
|
1631
1785
|
const billingEnabled = useBillingHookEnabled({
|
|
1632
1786
|
for: safeFor,
|
|
@@ -1655,7 +1809,7 @@ function createBillingPaginatedHook({ hookName: hookName$4, resourceType, useFet
|
|
|
1655
1809
|
authenticated: !options?.unauthenticated,
|
|
1656
1810
|
tracked: options?.unauthenticated ? { for: safeFor } : {
|
|
1657
1811
|
userId: user?.id,
|
|
1658
|
-
...isForOrganization ? {
|
|
1812
|
+
...isForOrganization ? { orgId: organization?.id } : {}
|
|
1659
1813
|
},
|
|
1660
1814
|
untracked: { args: hookParams }
|
|
1661
1815
|
})
|
|
@@ -1749,44 +1903,53 @@ function useSubscriptionCacheKeys(params) {
|
|
|
1749
1903
|
}
|
|
1750
1904
|
|
|
1751
1905
|
//#endregion
|
|
1752
|
-
//#region src/react/hooks/useSubscription.
|
|
1753
|
-
const
|
|
1906
|
+
//#region src/react/hooks/useSubscription.tsx
|
|
1907
|
+
const HOOK_NAME = "useSubscription";
|
|
1754
1908
|
/**
|
|
1755
|
-
* This is the existing implementation of useSubscription using SWR.
|
|
1756
|
-
* It is kept here for backwards compatibility until our next major version.
|
|
1757
|
-
*
|
|
1758
1909
|
* @internal
|
|
1759
1910
|
*/
|
|
1760
1911
|
function useSubscription(params) {
|
|
1761
|
-
useAssertWrappedByClerkProvider(
|
|
1912
|
+
useAssertWrappedByClerkProvider(HOOK_NAME);
|
|
1762
1913
|
const clerk = useClerkInstanceContext();
|
|
1763
1914
|
const user = useUserContext();
|
|
1764
1915
|
const { organization } = useOrganizationContext();
|
|
1765
|
-
const
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1916
|
+
const billingEnabled = useBillingHookEnabled(params);
|
|
1917
|
+
const recordedRef = useRef(false);
|
|
1918
|
+
useEffect(() => {
|
|
1919
|
+
if (!recordedRef.current && clerk?.telemetry) {
|
|
1920
|
+
clerk.telemetry.record(eventMethodCalled(HOOK_NAME));
|
|
1921
|
+
recordedRef.current = true;
|
|
1922
|
+
}
|
|
1923
|
+
}, [clerk]);
|
|
1924
|
+
const keepPreviousData = params?.keepPreviousData ?? false;
|
|
1925
|
+
const [queryClient] = useClerkQueryClient();
|
|
1926
|
+
const { queryKey, invalidationKey, stableKey, authenticated } = useSubscriptionCacheKeys({
|
|
1770
1927
|
userId: user?.id,
|
|
1771
1928
|
orgId: organization?.id,
|
|
1772
1929
|
for: params?.for
|
|
1773
1930
|
});
|
|
1774
|
-
const
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1931
|
+
const queriesEnabled = Boolean(user?.id && billingEnabled);
|
|
1932
|
+
useClearQueriesOnSignOut({
|
|
1933
|
+
isSignedOut: user === null,
|
|
1934
|
+
authenticated,
|
|
1935
|
+
stableKeys: stableKey
|
|
1936
|
+
});
|
|
1937
|
+
const query = useClerkQuery({
|
|
1938
|
+
queryKey,
|
|
1939
|
+
queryFn: ({ queryKey: queryKey$1 }) => {
|
|
1940
|
+
const obj = queryKey$1[3];
|
|
1941
|
+
return clerk.billing.getSubscription(obj.args);
|
|
1942
|
+
},
|
|
1943
|
+
staleTime: 1e3 * 60,
|
|
1944
|
+
enabled: queriesEnabled,
|
|
1945
|
+
placeholderData: defineKeepPreviousDataFn(keepPreviousData && queriesEnabled)
|
|
1781
1946
|
});
|
|
1782
|
-
const revalidate = useCallback(() => {
|
|
1783
|
-
swr.mutate();
|
|
1784
|
-
}, [swr]);
|
|
1947
|
+
const revalidate = useCallback(() => queryClient.invalidateQueries({ queryKey: invalidationKey }), [queryClient, invalidationKey]);
|
|
1785
1948
|
return {
|
|
1786
|
-
data:
|
|
1787
|
-
error:
|
|
1788
|
-
isLoading:
|
|
1789
|
-
isFetching:
|
|
1949
|
+
data: query.data,
|
|
1950
|
+
error: query.error ?? void 0,
|
|
1951
|
+
isLoading: query.isLoading,
|
|
1952
|
+
isFetching: query.isFetching,
|
|
1790
1953
|
revalidate
|
|
1791
1954
|
};
|
|
1792
1955
|
}
|
|
@@ -1866,40 +2029,47 @@ function useStatementQueryCacheKeys(params) {
|
|
|
1866
2029
|
}
|
|
1867
2030
|
|
|
1868
2031
|
//#endregion
|
|
1869
|
-
//#region src/react/hooks/useStatementQuery.
|
|
2032
|
+
//#region src/react/hooks/useStatementQuery.tsx
|
|
1870
2033
|
/**
|
|
1871
|
-
* This is the existing implementation of useStatementQuery using SWR.
|
|
1872
|
-
* It is kept here for backwards compatibility until our next major version.
|
|
1873
|
-
*
|
|
1874
2034
|
* @internal
|
|
1875
2035
|
*/
|
|
1876
|
-
function
|
|
1877
|
-
const { statementId = null,
|
|
2036
|
+
function useStatementQuery(params = {}) {
|
|
2037
|
+
const { statementId = null, keepPreviousData = false, for: forType = "user" } = params;
|
|
1878
2038
|
const clerk = useClerkInstanceContext();
|
|
1879
2039
|
const user = useUserContext();
|
|
1880
2040
|
const { organization } = useOrganizationContext();
|
|
1881
2041
|
const organizationId = forType === "organization" ? organization?.id ?? null : null;
|
|
1882
|
-
const { queryKey } = useStatementQueryCacheKeys({
|
|
2042
|
+
const { queryKey, stableKey, authenticated } = useStatementQueryCacheKeys({
|
|
1883
2043
|
statementId,
|
|
1884
2044
|
userId: user?.id ?? null,
|
|
1885
2045
|
orgId: organizationId,
|
|
1886
2046
|
for: forType
|
|
1887
2047
|
});
|
|
1888
|
-
const
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
}
|
|
1895
|
-
|
|
1896
|
-
|
|
2048
|
+
const billingEnabled = useBillingHookEnabled(params);
|
|
2049
|
+
const queryEnabled = Boolean(statementId) && billingEnabled;
|
|
2050
|
+
useClearQueriesOnSignOut({
|
|
2051
|
+
isSignedOut: user === null,
|
|
2052
|
+
authenticated,
|
|
2053
|
+
stableKeys: stableKey
|
|
2054
|
+
});
|
|
2055
|
+
const query = useClerkQuery({
|
|
2056
|
+
queryKey,
|
|
2057
|
+
queryFn: () => {
|
|
2058
|
+
if (!statementId) throw new Error("statementId is required to fetch a statement");
|
|
2059
|
+
return clerk.billing.getStatement({
|
|
2060
|
+
id: statementId,
|
|
2061
|
+
orgId: organizationId ?? void 0
|
|
2062
|
+
});
|
|
2063
|
+
},
|
|
2064
|
+
enabled: queryEnabled,
|
|
2065
|
+
placeholderData: defineKeepPreviousDataFn(keepPreviousData),
|
|
2066
|
+
staleTime: 1e3 * 60
|
|
1897
2067
|
});
|
|
1898
2068
|
return {
|
|
1899
|
-
data:
|
|
1900
|
-
error:
|
|
1901
|
-
isLoading:
|
|
1902
|
-
isFetching:
|
|
2069
|
+
data: query.data,
|
|
2070
|
+
error: query.error ?? null,
|
|
2071
|
+
isLoading: query.isLoading,
|
|
2072
|
+
isFetching: query.isFetching
|
|
1903
2073
|
};
|
|
1904
2074
|
}
|
|
1905
2075
|
|
|
@@ -1918,31 +2088,32 @@ function usePlanDetailsQueryCacheKeys(params) {
|
|
|
1918
2088
|
}
|
|
1919
2089
|
|
|
1920
2090
|
//#endregion
|
|
1921
|
-
//#region src/react/hooks/usePlanDetailsQuery.
|
|
2091
|
+
//#region src/react/hooks/usePlanDetailsQuery.tsx
|
|
1922
2092
|
/**
|
|
1923
|
-
* This is the existing implementation of usePlanDetailsQuery using SWR.
|
|
1924
|
-
* It is kept here for backwards compatibility until our next major version.
|
|
1925
|
-
*
|
|
1926
2093
|
* @internal
|
|
1927
2094
|
*/
|
|
1928
|
-
function
|
|
1929
|
-
const { planId, initialPlan = null,
|
|
2095
|
+
function __internal_usePlanDetailsQuery(params = {}) {
|
|
2096
|
+
const { planId, initialPlan = null, keepPreviousData = true } = params;
|
|
1930
2097
|
const clerk = useClerkInstanceContext();
|
|
1931
2098
|
const targetPlanId = planId ?? initialPlan?.id ?? null;
|
|
1932
2099
|
const { queryKey } = usePlanDetailsQueryCacheKeys({ planId: targetPlanId });
|
|
1933
|
-
const
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
2100
|
+
const billingEnabled = useBillingHookEnabled({ authenticated: false });
|
|
2101
|
+
const query = useClerkQuery({
|
|
2102
|
+
queryKey,
|
|
2103
|
+
queryFn: () => {
|
|
2104
|
+
if (!targetPlanId) throw new Error("planId is required to fetch plan details");
|
|
2105
|
+
return clerk.billing.getPlan({ id: targetPlanId });
|
|
2106
|
+
},
|
|
2107
|
+
enabled: Boolean(targetPlanId) && billingEnabled,
|
|
2108
|
+
initialData: initialPlan ?? void 0,
|
|
2109
|
+
placeholderData: defineKeepPreviousDataFn(keepPreviousData),
|
|
2110
|
+
initialDataUpdatedAt: 0
|
|
1940
2111
|
});
|
|
1941
2112
|
return {
|
|
1942
|
-
data:
|
|
1943
|
-
error:
|
|
1944
|
-
isLoading:
|
|
1945
|
-
isFetching:
|
|
2113
|
+
data: query.data,
|
|
2114
|
+
error: query.error ?? null,
|
|
2115
|
+
isLoading: query.isLoading,
|
|
2116
|
+
isFetching: query.isFetching
|
|
1946
2117
|
};
|
|
1947
2118
|
}
|
|
1948
2119
|
|
|
@@ -1974,37 +2145,44 @@ function usePaymentAttemptQueryCacheKeys(params) {
|
|
|
1974
2145
|
}
|
|
1975
2146
|
|
|
1976
2147
|
//#endregion
|
|
1977
|
-
//#region src/react/hooks/usePaymentAttemptQuery.
|
|
2148
|
+
//#region src/react/hooks/usePaymentAttemptQuery.tsx
|
|
1978
2149
|
/**
|
|
1979
|
-
* This is the existing implementation of usePaymentAttemptQuery using SWR.
|
|
1980
|
-
* It is kept here for backwards compatibility until our next major version.
|
|
1981
|
-
*
|
|
1982
2150
|
* @internal
|
|
1983
2151
|
*/
|
|
1984
|
-
function
|
|
1985
|
-
const { paymentAttemptId,
|
|
2152
|
+
function usePaymentAttemptQuery(params) {
|
|
2153
|
+
const { paymentAttemptId, keepPreviousData = false, for: forType = "user" } = params;
|
|
1986
2154
|
const clerk = useClerkInstanceContext();
|
|
1987
2155
|
const user = useUserContext();
|
|
1988
2156
|
const { organization } = useOrganizationContext();
|
|
1989
2157
|
const organizationId = forType === "organization" ? organization?.id ?? null : null;
|
|
1990
|
-
const { queryKey } = usePaymentAttemptQueryCacheKeys({
|
|
2158
|
+
const { queryKey, stableKey, authenticated } = usePaymentAttemptQueryCacheKeys({
|
|
1991
2159
|
paymentAttemptId,
|
|
1992
2160
|
userId: user?.id ?? null,
|
|
1993
2161
|
orgId: organizationId,
|
|
1994
2162
|
for: forType
|
|
1995
2163
|
});
|
|
1996
|
-
const
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2164
|
+
const billingEnabled = useBillingHookEnabled(params);
|
|
2165
|
+
const queryEnabled = Boolean(paymentAttemptId) && billingEnabled;
|
|
2166
|
+
useClearQueriesOnSignOut({
|
|
2167
|
+
isSignedOut: user === null,
|
|
2168
|
+
authenticated,
|
|
2169
|
+
stableKeys: stableKey
|
|
2170
|
+
});
|
|
2171
|
+
const query = useClerkQuery({
|
|
2172
|
+
queryKey,
|
|
2173
|
+
queryFn: ({ queryKey: queryKey$1 }) => {
|
|
2174
|
+
const args = queryKey$1[3].args;
|
|
2175
|
+
return clerk.billing.getPaymentAttempt(args);
|
|
2176
|
+
},
|
|
2177
|
+
enabled: queryEnabled,
|
|
2178
|
+
placeholderData: defineKeepPreviousDataFn(keepPreviousData),
|
|
2179
|
+
staleTime: 1e3 * 60
|
|
2002
2180
|
});
|
|
2003
2181
|
return {
|
|
2004
|
-
data:
|
|
2005
|
-
error:
|
|
2006
|
-
isLoading:
|
|
2007
|
-
isFetching:
|
|
2182
|
+
data: query.data,
|
|
2183
|
+
error: query.error ?? null,
|
|
2184
|
+
isLoading: query.isLoading,
|
|
2185
|
+
isFetching: query.isFetching
|
|
2008
2186
|
};
|
|
2009
2187
|
}
|
|
2010
2188
|
|
|
@@ -2258,74 +2436,90 @@ const createElementComponent = (type, isServer) => {
|
|
|
2258
2436
|
const PaymentElement$1 = createElementComponent("payment", typeof window === "undefined");
|
|
2259
2437
|
|
|
2260
2438
|
//#endregion
|
|
2261
|
-
//#region src/react/billing/useInitializePaymentMethod.
|
|
2439
|
+
//#region src/react/billing/useInitializePaymentMethod.tsx
|
|
2262
2440
|
/**
|
|
2263
|
-
* This is the existing implementation of the payment method initializer using SWR.
|
|
2264
|
-
* It is kept here for backwards compatibility until our next major version.
|
|
2265
|
-
*
|
|
2266
2441
|
* @internal
|
|
2267
2442
|
*/
|
|
2268
2443
|
function useInitializePaymentMethod(options) {
|
|
2269
|
-
const { for: forType
|
|
2444
|
+
const { for: forType } = options ?? {};
|
|
2270
2445
|
const { organization } = useOrganizationContext();
|
|
2271
2446
|
const user = useUserContext();
|
|
2272
2447
|
const resource = forType === "organization" ? organization : user;
|
|
2273
|
-
const
|
|
2274
|
-
|
|
2275
|
-
resourceId: resource
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2448
|
+
const billingEnabled = useBillingHookEnabled(options);
|
|
2449
|
+
const queryKey = useMemo(() => {
|
|
2450
|
+
return ["billing-payment-method-initialize", { resourceId: resource?.id }];
|
|
2451
|
+
}, [resource?.id]);
|
|
2452
|
+
const query = useClerkQuery({
|
|
2453
|
+
queryKey,
|
|
2454
|
+
queryFn: async () => {
|
|
2455
|
+
if (!resource) return;
|
|
2456
|
+
return resource.initializePaymentMethod({ gateway: "stripe" });
|
|
2457
|
+
},
|
|
2458
|
+
enabled: Boolean(resource?.id) && billingEnabled,
|
|
2459
|
+
staleTime: 1e3 * 60,
|
|
2460
|
+
refetchOnWindowFocus: false,
|
|
2461
|
+
placeholderData: defineKeepPreviousDataFn(true)
|
|
2279
2462
|
});
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2463
|
+
const [queryClient] = useClerkQueryClient();
|
|
2464
|
+
const initializePaymentMethod = useCallback(async () => {
|
|
2465
|
+
if (!resource) return;
|
|
2466
|
+
const result = await resource.initializePaymentMethod({ gateway: "stripe" });
|
|
2467
|
+
queryClient.setQueryData(queryKey, result);
|
|
2468
|
+
return result;
|
|
2469
|
+
}, [
|
|
2470
|
+
queryClient,
|
|
2471
|
+
queryKey,
|
|
2472
|
+
resource
|
|
2473
|
+
]);
|
|
2284
2474
|
return {
|
|
2285
|
-
initializedPaymentMethod: data,
|
|
2286
|
-
initializePaymentMethod
|
|
2475
|
+
initializedPaymentMethod: query.data ?? void 0,
|
|
2476
|
+
initializePaymentMethod
|
|
2287
2477
|
};
|
|
2288
2478
|
}
|
|
2289
2479
|
|
|
2290
2480
|
//#endregion
|
|
2291
|
-
//#region src/react/billing/useStripeClerkLibs.
|
|
2481
|
+
//#region src/react/billing/useStripeClerkLibs.tsx
|
|
2292
2482
|
/**
|
|
2293
|
-
* This is the existing implementation of the Stripe libraries loader using SWR.
|
|
2294
|
-
* It is kept here for backwards compatibility until our next major version.
|
|
2295
|
-
*
|
|
2296
2483
|
* @internal
|
|
2297
2484
|
*/
|
|
2298
2485
|
function useStripeClerkLibs() {
|
|
2299
2486
|
const clerk = useClerk();
|
|
2300
|
-
return
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2487
|
+
return useClerkQuery({
|
|
2488
|
+
queryKey: ["clerk-stripe-sdk"],
|
|
2489
|
+
queryFn: async () => {
|
|
2490
|
+
return { loadStripe: await clerk.__internal_loadStripeJs() };
|
|
2491
|
+
},
|
|
2492
|
+
enabled: useBillingHookEnabled(),
|
|
2493
|
+
staleTime: Infinity,
|
|
2494
|
+
refetchOnWindowFocus: false,
|
|
2495
|
+
placeholderData: defineKeepPreviousDataFn(true)
|
|
2306
2496
|
}).data ?? null;
|
|
2307
2497
|
}
|
|
2308
2498
|
|
|
2309
2499
|
//#endregion
|
|
2310
|
-
//#region src/react/billing/useStripeLoader.
|
|
2500
|
+
//#region src/react/billing/useStripeLoader.tsx
|
|
2311
2501
|
/**
|
|
2312
|
-
* This is the existing implementation of the Stripe instance loader using SWR.
|
|
2313
|
-
* It is kept here for backwards compatibility until our next major version.
|
|
2314
|
-
*
|
|
2315
2502
|
* @internal
|
|
2316
2503
|
*/
|
|
2317
2504
|
function useStripeLoader(options) {
|
|
2318
2505
|
const { stripeClerkLibs, externalGatewayId, stripePublishableKey } = options;
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2506
|
+
const queryKey = useMemo(() => {
|
|
2507
|
+
return ["stripe-sdk", {
|
|
2508
|
+
externalGatewayId,
|
|
2509
|
+
stripePublishableKey
|
|
2510
|
+
}];
|
|
2511
|
+
}, [externalGatewayId, stripePublishableKey]);
|
|
2512
|
+
const billingEnabled = useBillingHookEnabled({ authenticated: true });
|
|
2513
|
+
return useClerkQuery({
|
|
2514
|
+
queryKey,
|
|
2515
|
+
queryFn: () => {
|
|
2516
|
+
if (!stripeClerkLibs || !externalGatewayId || !stripePublishableKey) return null;
|
|
2517
|
+
return stripeClerkLibs.loadStripe(stripePublishableKey, { stripeAccount: externalGatewayId });
|
|
2518
|
+
},
|
|
2519
|
+
enabled: Boolean(stripeClerkLibs && externalGatewayId && stripePublishableKey) && billingEnabled,
|
|
2520
|
+
staleTime: 1e3 * 60,
|
|
2521
|
+
refetchOnWindowFocus: false,
|
|
2522
|
+
placeholderData: defineKeepPreviousDataFn(true)
|
|
2329
2523
|
}).data;
|
|
2330
2524
|
}
|
|
2331
2525
|
|
|
@@ -2498,5 +2692,5 @@ const usePaymentElement = () => {
|
|
|
2498
2692
|
};
|
|
2499
2693
|
|
|
2500
2694
|
//#endregion
|
|
2501
|
-
export { ClerkInstanceContext, ClientContext, OptionsContext, OrganizationProvider, SessionContext, UserContext, __experimental_CheckoutProvider, PaymentElement as __experimental_PaymentElement, PaymentElementProvider as __experimental_PaymentElementProvider, useAPIKeys as __experimental_useAPIKeys, useCheckout as __experimental_useCheckout, usePaymentAttempts as __experimental_usePaymentAttempts, usePaymentElement as __experimental_usePaymentElement, usePaymentMethods as __experimental_usePaymentMethods, usePlans as __experimental_usePlans, useStatements as __experimental_useStatements, useSubscription as __experimental_useSubscription, __internal_usePaymentAttemptQuery,
|
|
2695
|
+
export { ClerkInstanceContext, ClientContext, OptionsContext, OrganizationProvider, SessionContext, UserContext, __experimental_CheckoutProvider, PaymentElement as __experimental_PaymentElement, PaymentElementProvider as __experimental_PaymentElementProvider, useAPIKeys as __experimental_useAPIKeys, useCheckout as __experimental_useCheckout, usePaymentAttempts as __experimental_usePaymentAttempts, usePaymentElement as __experimental_usePaymentElement, usePaymentMethods as __experimental_usePaymentMethods, usePlans as __experimental_usePlans, useStatements as __experimental_useStatements, useSubscription as __experimental_useSubscription, usePaymentAttemptQuery as __internal_usePaymentAttemptQuery, __internal_usePlanDetailsQuery, useStatementQuery as __internal_useStatementQuery, assertContextExists, createContextAndHook, isDeeplyEqual, useAssertWrappedByClerkProvider, useAttemptToEnableOrganizations, useClerk, useClerkInstanceContext, useClientContext, useDeepEqualMemo, useOptionsContext, useOrganization, useOrganizationContext, useOrganizationList, useReverification, useSafeLayoutEffect, useSession, useSessionContext, useSessionList, useUser, useUserContext };
|
|
2502
2696
|
//# sourceMappingURL=index.mjs.map
|