@better-auth-ui/react 1.6.31 → 1.6.32

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.
@@ -0,0 +1,131 @@
1
+ import { oauthProviderQueryKeys } from "@better-auth-ui/core/plugins"
2
+ import {
3
+ type DataTag,
4
+ type QueryClient,
5
+ queryOptions,
6
+ skipToken,
7
+ useQuery
8
+ } from "@tanstack/react-query"
9
+ import type { BetterFetchError } from "better-auth/react"
10
+
11
+ import type { InferData, OAuthProviderAuthClient } from "../../lib/auth-client"
12
+
13
+ export type PublicOAuthClientData<
14
+ TAuthClient extends OAuthProviderAuthClient = OAuthProviderAuthClient
15
+ > = InferData<TAuthClient["oauth2"]["publicClient"]>
16
+
17
+ export type PublicOAuthClientParams<
18
+ TAuthClient extends OAuthProviderAuthClient = OAuthProviderAuthClient
19
+ > = Partial<
20
+ Omit<
21
+ NonNullable<Parameters<TAuthClient["oauth2"]["publicClient"]>[0]>,
22
+ "query"
23
+ >
24
+ >
25
+
26
+ export type PublicOAuthClientOptions<
27
+ TAuthClient extends OAuthProviderAuthClient = OAuthProviderAuthClient
28
+ > = Omit<
29
+ ReturnType<typeof publicOAuthClientOptions<TAuthClient>>,
30
+ "queryKey" | "queryFn"
31
+ >
32
+
33
+ /**
34
+ * Query options factory for an OAuth application's public metadata.
35
+ *
36
+ * @param authClient - The Better Auth client with the OAuth provider plugin.
37
+ * @param clientId - The OAuth client ID from the signed authorization request.
38
+ * @param params - Fetch options forwarded to `oauth2.publicClient`.
39
+ */
40
+ export function publicOAuthClientOptions<
41
+ TAuthClient extends OAuthProviderAuthClient
42
+ >(
43
+ authClient: TAuthClient,
44
+ clientId: string,
45
+ params?: PublicOAuthClientParams<TAuthClient>
46
+ ) {
47
+ type TData = PublicOAuthClientData<TAuthClient>
48
+ const queryKey = oauthProviderQueryKeys.publicClient(clientId)
49
+
50
+ const options = queryOptions<TData, BetterFetchError, TData, typeof queryKey>(
51
+ {
52
+ queryKey,
53
+ queryFn: ({ signal }) =>
54
+ authClient.oauth2.publicClient({
55
+ ...params,
56
+ query: { client_id: clientId },
57
+ fetchOptions: { ...params?.fetchOptions, signal, throw: true }
58
+ }) as Promise<TData>
59
+ }
60
+ )
61
+
62
+ return options as typeof options & {
63
+ queryKey: DataTag<typeof queryKey, TData, BetterFetchError>
64
+ }
65
+ }
66
+
67
+ export const ensurePublicOAuthClient = <
68
+ TAuthClient extends OAuthProviderAuthClient
69
+ >(
70
+ queryClient: QueryClient,
71
+ authClient: TAuthClient,
72
+ clientId: string,
73
+ params?: PublicOAuthClientParams<TAuthClient>
74
+ ) =>
75
+ queryClient.ensureQueryData(
76
+ publicOAuthClientOptions(authClient, clientId, params)
77
+ )
78
+
79
+ export const prefetchPublicOAuthClient = <
80
+ TAuthClient extends OAuthProviderAuthClient
81
+ >(
82
+ queryClient: QueryClient,
83
+ authClient: TAuthClient,
84
+ clientId: string,
85
+ params?: PublicOAuthClientParams<TAuthClient>
86
+ ) =>
87
+ queryClient.prefetchQuery(
88
+ publicOAuthClientOptions(authClient, clientId, params)
89
+ )
90
+
91
+ export const fetchPublicOAuthClient = <
92
+ TAuthClient extends OAuthProviderAuthClient
93
+ >(
94
+ queryClient: QueryClient,
95
+ authClient: TAuthClient,
96
+ clientId: string,
97
+ params?: PublicOAuthClientParams<TAuthClient>
98
+ ) =>
99
+ queryClient.fetchQuery(publicOAuthClientOptions(authClient, clientId, params))
100
+
101
+ export type UsePublicOAuthClientOptions<
102
+ TAuthClient extends OAuthProviderAuthClient = OAuthProviderAuthClient
103
+ > = PublicOAuthClientOptions<TAuthClient> & PublicOAuthClientParams<TAuthClient>
104
+
105
+ /**
106
+ * Subscribe to an OAuth application's public metadata.
107
+ *
108
+ * The query is disabled until a client ID is available.
109
+ */
110
+ export function usePublicOAuthClient<
111
+ TAuthClient extends OAuthProviderAuthClient
112
+ >(
113
+ authClient: TAuthClient,
114
+ clientId: string | undefined,
115
+ options: UsePublicOAuthClientOptions<TAuthClient> = {},
116
+ queryClient?: QueryClient
117
+ ) {
118
+ const { fetchOptions, ...queryOptionsRest } = options
119
+ const baseOptions = publicOAuthClientOptions(authClient, clientId ?? "", {
120
+ fetchOptions
121
+ } as PublicOAuthClientParams<TAuthClient>)
122
+
123
+ return useQuery(
124
+ {
125
+ ...queryOptionsRest,
126
+ ...baseOptions,
127
+ queryFn: clientId ? baseOptions.queryFn : skipToken
128
+ },
129
+ queryClient
130
+ )
131
+ }