@fluxbase/sdk-react 0.0.1-rc.2 → 0.0.1-rc.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/dist/index.d.mts +25 -17
- package/dist/index.d.ts +25 -17
- package/dist/index.js +43 -14
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +43 -14
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/use-admin-auth.ts +62 -51
- package/src/use-auth.ts +5 -3
- package/src/use-realtime.ts +58 -44
- package/src/use-users.ts +11 -4
package/src/use-realtime.ts
CHANGED
|
@@ -2,99 +2,113 @@
|
|
|
2
2
|
* Realtime subscription hooks for Fluxbase SDK
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import { useEffect, useRef } from
|
|
6
|
-
import { useQueryClient } from
|
|
7
|
-
import { useFluxbaseClient } from
|
|
8
|
-
import type {
|
|
5
|
+
import { useEffect, useRef } from "react";
|
|
6
|
+
import { useQueryClient } from "@tanstack/react-query";
|
|
7
|
+
import { useFluxbaseClient } from "./context";
|
|
8
|
+
import type {
|
|
9
|
+
RealtimeCallback,
|
|
10
|
+
RealtimePostgresChangesPayload,
|
|
11
|
+
} from "@fluxbase/sdk";
|
|
9
12
|
|
|
10
13
|
export interface UseRealtimeOptions {
|
|
11
14
|
/**
|
|
12
15
|
* The channel name (e.g., 'table:public.products')
|
|
13
16
|
*/
|
|
14
|
-
channel: string
|
|
17
|
+
channel: string;
|
|
15
18
|
|
|
16
19
|
/**
|
|
17
20
|
* Event type to listen for ('INSERT', 'UPDATE', 'DELETE', or '*' for all)
|
|
18
21
|
*/
|
|
19
|
-
event?:
|
|
22
|
+
event?: "INSERT" | "UPDATE" | "DELETE" | "*";
|
|
20
23
|
|
|
21
24
|
/**
|
|
22
25
|
* Callback function when an event is received
|
|
23
26
|
*/
|
|
24
|
-
callback?: RealtimeCallback
|
|
27
|
+
callback?: RealtimeCallback;
|
|
25
28
|
|
|
26
29
|
/**
|
|
27
30
|
* Whether to automatically invalidate queries for the table
|
|
28
31
|
* Default: true
|
|
29
32
|
*/
|
|
30
|
-
autoInvalidate?: boolean
|
|
33
|
+
autoInvalidate?: boolean;
|
|
31
34
|
|
|
32
35
|
/**
|
|
33
36
|
* Custom query key to invalidate (if autoInvalidate is true)
|
|
34
37
|
* Default: ['fluxbase', 'table', tableName]
|
|
35
38
|
*/
|
|
36
|
-
invalidateKey?: unknown[]
|
|
39
|
+
invalidateKey?: unknown[];
|
|
37
40
|
|
|
38
41
|
/**
|
|
39
42
|
* Whether the subscription is enabled
|
|
40
43
|
* Default: true
|
|
41
44
|
*/
|
|
42
|
-
enabled?: boolean
|
|
45
|
+
enabled?: boolean;
|
|
43
46
|
}
|
|
44
47
|
|
|
45
48
|
/**
|
|
46
49
|
* Hook to subscribe to realtime changes for a channel
|
|
47
50
|
*/
|
|
48
51
|
export function useRealtime(options: UseRealtimeOptions) {
|
|
49
|
-
const client = useFluxbaseClient()
|
|
50
|
-
const queryClient = useQueryClient()
|
|
51
|
-
const channelRef = useRef<ReturnType<typeof client.realtime.channel> | null>(
|
|
52
|
+
const client = useFluxbaseClient();
|
|
53
|
+
const queryClient = useQueryClient();
|
|
54
|
+
const channelRef = useRef<ReturnType<typeof client.realtime.channel> | null>(
|
|
55
|
+
null,
|
|
56
|
+
);
|
|
52
57
|
|
|
53
58
|
const {
|
|
54
59
|
channel: channelName,
|
|
55
|
-
event =
|
|
60
|
+
event = "*",
|
|
56
61
|
callback,
|
|
57
62
|
autoInvalidate = true,
|
|
58
63
|
invalidateKey,
|
|
59
64
|
enabled = true,
|
|
60
|
-
} = options
|
|
65
|
+
} = options;
|
|
61
66
|
|
|
62
67
|
useEffect(() => {
|
|
63
68
|
if (!enabled) {
|
|
64
|
-
return
|
|
69
|
+
return;
|
|
65
70
|
}
|
|
66
71
|
|
|
67
72
|
// Create channel and subscribe
|
|
68
|
-
const channel = client.realtime.channel(channelName)
|
|
69
|
-
channelRef.current = channel
|
|
73
|
+
const channel = client.realtime.channel(channelName);
|
|
74
|
+
channelRef.current = channel;
|
|
70
75
|
|
|
71
|
-
const handleChange = (payload:
|
|
76
|
+
const handleChange = (payload: RealtimePostgresChangesPayload) => {
|
|
72
77
|
// Call user callback
|
|
73
78
|
if (callback) {
|
|
74
|
-
callback(payload)
|
|
79
|
+
callback(payload);
|
|
75
80
|
}
|
|
76
81
|
|
|
77
82
|
// Auto-invalidate queries if enabled
|
|
78
83
|
if (autoInvalidate) {
|
|
79
84
|
// Extract table name from channel (e.g., 'table:public.products' -> 'public.products')
|
|
80
|
-
const tableName = channelName.replace(/^table:/,
|
|
85
|
+
const tableName = channelName.replace(/^table:/, "");
|
|
81
86
|
|
|
82
|
-
const key = invalidateKey || [
|
|
83
|
-
queryClient.invalidateQueries({ queryKey: key })
|
|
87
|
+
const key = invalidateKey || ["fluxbase", "table", tableName];
|
|
88
|
+
queryClient.invalidateQueries({ queryKey: key });
|
|
84
89
|
}
|
|
85
|
-
}
|
|
90
|
+
};
|
|
86
91
|
|
|
87
|
-
channel.on(event, handleChange).subscribe()
|
|
92
|
+
channel.on(event, handleChange).subscribe();
|
|
88
93
|
|
|
89
94
|
return () => {
|
|
90
|
-
channel.unsubscribe()
|
|
91
|
-
channelRef.current = null
|
|
92
|
-
}
|
|
93
|
-
}, [
|
|
95
|
+
channel.unsubscribe();
|
|
96
|
+
channelRef.current = null;
|
|
97
|
+
};
|
|
98
|
+
}, [
|
|
99
|
+
client,
|
|
100
|
+
channelName,
|
|
101
|
+
event,
|
|
102
|
+
callback,
|
|
103
|
+
autoInvalidate,
|
|
104
|
+
invalidateKey,
|
|
105
|
+
queryClient,
|
|
106
|
+
enabled,
|
|
107
|
+
]);
|
|
94
108
|
|
|
95
109
|
return {
|
|
96
110
|
channel: channelRef.current,
|
|
97
|
-
}
|
|
111
|
+
};
|
|
98
112
|
}
|
|
99
113
|
|
|
100
114
|
/**
|
|
@@ -104,12 +118,12 @@ export function useRealtime(options: UseRealtimeOptions) {
|
|
|
104
118
|
*/
|
|
105
119
|
export function useTableSubscription(
|
|
106
120
|
table: string,
|
|
107
|
-
options?: Omit<UseRealtimeOptions,
|
|
121
|
+
options?: Omit<UseRealtimeOptions, "channel">,
|
|
108
122
|
) {
|
|
109
123
|
return useRealtime({
|
|
110
124
|
...options,
|
|
111
125
|
channel: `table:${table}`,
|
|
112
|
-
})
|
|
126
|
+
});
|
|
113
127
|
}
|
|
114
128
|
|
|
115
129
|
/**
|
|
@@ -117,15 +131,15 @@ export function useTableSubscription(
|
|
|
117
131
|
*/
|
|
118
132
|
export function useTableInserts(
|
|
119
133
|
table: string,
|
|
120
|
-
callback: (payload:
|
|
121
|
-
options?: Omit<UseRealtimeOptions,
|
|
134
|
+
callback: (payload: RealtimePostgresChangesPayload) => void,
|
|
135
|
+
options?: Omit<UseRealtimeOptions, "channel" | "event" | "callback">,
|
|
122
136
|
) {
|
|
123
137
|
return useRealtime({
|
|
124
138
|
...options,
|
|
125
139
|
channel: `table:${table}`,
|
|
126
|
-
event:
|
|
140
|
+
event: "INSERT",
|
|
127
141
|
callback,
|
|
128
|
-
})
|
|
142
|
+
});
|
|
129
143
|
}
|
|
130
144
|
|
|
131
145
|
/**
|
|
@@ -133,15 +147,15 @@ export function useTableInserts(
|
|
|
133
147
|
*/
|
|
134
148
|
export function useTableUpdates(
|
|
135
149
|
table: string,
|
|
136
|
-
callback: (payload:
|
|
137
|
-
options?: Omit<UseRealtimeOptions,
|
|
150
|
+
callback: (payload: RealtimePostgresChangesPayload) => void,
|
|
151
|
+
options?: Omit<UseRealtimeOptions, "channel" | "event" | "callback">,
|
|
138
152
|
) {
|
|
139
153
|
return useRealtime({
|
|
140
154
|
...options,
|
|
141
155
|
channel: `table:${table}`,
|
|
142
|
-
event:
|
|
156
|
+
event: "UPDATE",
|
|
143
157
|
callback,
|
|
144
|
-
})
|
|
158
|
+
});
|
|
145
159
|
}
|
|
146
160
|
|
|
147
161
|
/**
|
|
@@ -149,13 +163,13 @@ export function useTableUpdates(
|
|
|
149
163
|
*/
|
|
150
164
|
export function useTableDeletes(
|
|
151
165
|
table: string,
|
|
152
|
-
callback: (payload:
|
|
153
|
-
options?: Omit<UseRealtimeOptions,
|
|
166
|
+
callback: (payload: RealtimePostgresChangesPayload) => void,
|
|
167
|
+
options?: Omit<UseRealtimeOptions, "channel" | "event" | "callback">,
|
|
154
168
|
) {
|
|
155
169
|
return useRealtime({
|
|
156
170
|
...options,
|
|
157
171
|
channel: `table:${table}`,
|
|
158
|
-
event:
|
|
172
|
+
event: "DELETE",
|
|
159
173
|
callback,
|
|
160
|
-
})
|
|
174
|
+
});
|
|
161
175
|
}
|
package/src/use-users.ts
CHANGED
|
@@ -109,9 +109,12 @@ export function useUsers(options: UseUsersOptions = {}): UseUsersReturn {
|
|
|
109
109
|
try {
|
|
110
110
|
setIsLoading(true)
|
|
111
111
|
setError(null)
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
112
|
+
const { data, error: apiError } = await client.admin.listUsers(listOptions)
|
|
113
|
+
if (apiError) {
|
|
114
|
+
throw apiError
|
|
115
|
+
}
|
|
116
|
+
setUsers(data!.users)
|
|
117
|
+
setTotal(data!.total)
|
|
115
118
|
} catch (err) {
|
|
116
119
|
setError(err as Error)
|
|
117
120
|
} finally {
|
|
@@ -157,7 +160,11 @@ export function useUsers(options: UseUsersOptions = {}): UseUsersReturn {
|
|
|
157
160
|
*/
|
|
158
161
|
const resetPassword = useCallback(
|
|
159
162
|
async (userId: string): Promise<{ message: string }> => {
|
|
160
|
-
|
|
163
|
+
const { data, error } = await client.admin.resetUserPassword(userId)
|
|
164
|
+
if (error) {
|
|
165
|
+
throw error
|
|
166
|
+
}
|
|
167
|
+
return data!
|
|
161
168
|
},
|
|
162
169
|
[client]
|
|
163
170
|
)
|