@fluxbase/sdk-react 0.0.1-rc.2

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,191 @@
1
+ import { useState, useEffect, useCallback } from 'react'
2
+ import { useFluxbaseClient } from './context'
3
+ import type { EnrichedUser, ListUsersOptions } from '@fluxbase/sdk'
4
+
5
+ export interface UseUsersOptions extends ListUsersOptions {
6
+ /**
7
+ * Whether to automatically fetch users on mount
8
+ * @default true
9
+ */
10
+ autoFetch?: boolean
11
+
12
+ /**
13
+ * Refetch interval in milliseconds (0 to disable)
14
+ * @default 0
15
+ */
16
+ refetchInterval?: number
17
+ }
18
+
19
+ export interface UseUsersReturn {
20
+ /**
21
+ * Array of users
22
+ */
23
+ users: EnrichedUser[]
24
+
25
+ /**
26
+ * Total number of users (for pagination)
27
+ */
28
+ total: number
29
+
30
+ /**
31
+ * Whether users are being fetched
32
+ */
33
+ isLoading: boolean
34
+
35
+ /**
36
+ * Any error that occurred
37
+ */
38
+ error: Error | null
39
+
40
+ /**
41
+ * Refetch users
42
+ */
43
+ refetch: () => Promise<void>
44
+
45
+ /**
46
+ * Invite a new user
47
+ */
48
+ inviteUser: (email: string, role: 'user' | 'admin') => Promise<void>
49
+
50
+ /**
51
+ * Update user role
52
+ */
53
+ updateUserRole: (userId: string, role: 'user' | 'admin') => Promise<void>
54
+
55
+ /**
56
+ * Delete a user
57
+ */
58
+ deleteUser: (userId: string) => Promise<void>
59
+
60
+ /**
61
+ * Reset user password
62
+ */
63
+ resetPassword: (userId: string) => Promise<{ message: string }>
64
+ }
65
+
66
+ /**
67
+ * Hook for managing users
68
+ *
69
+ * Provides user list with pagination, search, and management functions.
70
+ *
71
+ * @example
72
+ * ```tsx
73
+ * function UserList() {
74
+ * const { users, total, isLoading, refetch, inviteUser, deleteUser } = useUsers({
75
+ * limit: 20,
76
+ * search: searchTerm
77
+ * })
78
+ *
79
+ * return (
80
+ * <div>
81
+ * {isLoading ? <Spinner /> : (
82
+ * <ul>
83
+ * {users.map(user => (
84
+ * <li key={user.id}>
85
+ * {user.email} - {user.role}
86
+ * <button onClick={() => deleteUser(user.id)}>Delete</button>
87
+ * </li>
88
+ * ))}
89
+ * </ul>
90
+ * )}
91
+ * </div>
92
+ * )
93
+ * }
94
+ * ```
95
+ */
96
+ export function useUsers(options: UseUsersOptions = {}): UseUsersReturn {
97
+ const { autoFetch = true, refetchInterval = 0, ...listOptions } = options
98
+ const client = useFluxbaseClient()
99
+
100
+ const [users, setUsers] = useState<EnrichedUser[]>([])
101
+ const [total, setTotal] = useState(0)
102
+ const [isLoading, setIsLoading] = useState(autoFetch)
103
+ const [error, setError] = useState<Error | null>(null)
104
+
105
+ /**
106
+ * Fetch users from API
107
+ */
108
+ const fetchUsers = useCallback(async () => {
109
+ try {
110
+ setIsLoading(true)
111
+ setError(null)
112
+ const response = await client.admin.listUsers(listOptions)
113
+ setUsers(response.users)
114
+ setTotal(response.total)
115
+ } catch (err) {
116
+ setError(err as Error)
117
+ } finally {
118
+ setIsLoading(false)
119
+ }
120
+ }, [client, JSON.stringify(listOptions)])
121
+
122
+ /**
123
+ * Invite a new user
124
+ */
125
+ const inviteUser = useCallback(
126
+ async (email: string, role: 'user' | 'admin'): Promise<void> => {
127
+ await client.admin.inviteUser({ email, role })
128
+ await fetchUsers() // Refresh list
129
+ },
130
+ [client, fetchUsers]
131
+ )
132
+
133
+ /**
134
+ * Update user role
135
+ */
136
+ const updateUserRole = useCallback(
137
+ async (userId: string, role: 'user' | 'admin'): Promise<void> => {
138
+ await client.admin.updateUserRole(userId, role)
139
+ await fetchUsers() // Refresh list
140
+ },
141
+ [client, fetchUsers]
142
+ )
143
+
144
+ /**
145
+ * Delete a user
146
+ */
147
+ const deleteUser = useCallback(
148
+ async (userId: string): Promise<void> => {
149
+ await client.admin.deleteUser(userId)
150
+ await fetchUsers() // Refresh list
151
+ },
152
+ [client, fetchUsers]
153
+ )
154
+
155
+ /**
156
+ * Reset user password
157
+ */
158
+ const resetPassword = useCallback(
159
+ async (userId: string): Promise<{ message: string }> => {
160
+ return await client.admin.resetUserPassword(userId)
161
+ },
162
+ [client]
163
+ )
164
+
165
+ // Auto-fetch on mount
166
+ useEffect(() => {
167
+ if (autoFetch) {
168
+ fetchUsers()
169
+ }
170
+ }, [autoFetch, fetchUsers])
171
+
172
+ // Set up refetch interval
173
+ useEffect(() => {
174
+ if (refetchInterval > 0) {
175
+ const interval = setInterval(fetchUsers, refetchInterval)
176
+ return () => clearInterval(interval)
177
+ }
178
+ }, [refetchInterval, fetchUsers])
179
+
180
+ return {
181
+ users,
182
+ total,
183
+ isLoading,
184
+ error,
185
+ refetch: fetchUsers,
186
+ inviteUser,
187
+ updateUserRole,
188
+ deleteUser,
189
+ resetPassword
190
+ }
191
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "ESNext",
5
+ "lib": ["ES2020", "DOM", "DOM.Iterable"],
6
+ "jsx": "react-jsx",
7
+ "declaration": true,
8
+ "declarationMap": true,
9
+ "sourceMap": true,
10
+ "outDir": "./dist",
11
+ "rootDir": "./src",
12
+ "moduleResolution": "bundler",
13
+ "allowImportingTsExtensions": true,
14
+ "resolveJsonModule": true,
15
+ "isolatedModules": true,
16
+ "esModuleInterop": true,
17
+ "forceConsistentCasingInFileNames": true,
18
+ "strict": true,
19
+ "skipLibCheck": true,
20
+ "noEmit": true
21
+ },
22
+ "include": ["src/**/*"],
23
+ "exclude": ["node_modules", "dist"]
24
+ }
@@ -0,0 +1 @@
1
+ {"root":["./src/context.tsx","./src/index.ts","./src/use-admin-auth.ts","./src/use-admin-hooks.ts","./src/use-api-keys.ts","./src/use-auth.ts","./src/use-query.ts","./src/use-realtime.ts","./src/use-rpc.ts","./src/use-storage.ts","./src/use-users.ts"],"version":"5.9.3"}
package/tsup.config.ts ADDED
@@ -0,0 +1,11 @@
1
+ import { defineConfig } from 'tsup'
2
+
3
+ export default defineConfig({
4
+ entry: ['src/index.ts'],
5
+ format: ['esm', 'cjs'],
6
+ dts: true,
7
+ splitting: false,
8
+ sourcemap: true,
9
+ clean: true,
10
+ external: ['react', '@tanstack/react-query', '@fluxbase/sdk'],
11
+ })
package/typedoc.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "$schema": "https://typedoc.org/schema.json",
3
+ "entryPoints": [
4
+ "src/index.ts"
5
+ ],
6
+ "out": "../docs/static/api/sdk-react",
7
+ "name": "@fluxbase/sdk-react",
8
+ "readme": "README.md",
9
+ "plugin": [],
10
+ "excludePrivate": true,
11
+ "excludeProtected": false,
12
+ "excludeInternal": true,
13
+ "includeVersion": true,
14
+ "searchInComments": true,
15
+ "validation": {
16
+ "notExported": true,
17
+ "invalidLink": true,
18
+ "notDocumented": false
19
+ },
20
+ "categorizeByGroup": true,
21
+ "categoryOrder": [
22
+ "Context",
23
+ "Authentication Hooks",
24
+ "Database Hooks",
25
+ "Realtime Hooks",
26
+ "Storage Hooks",
27
+ "RPC Hooks",
28
+ "Types",
29
+ "*"
30
+ ],
31
+ "navigationLinks": {
32
+ "GitHub": "https://github.com/yourusername/fluxbase",
33
+ "Documentation": "https://fluxbase.eu"
34
+ }
35
+ }