@fluxbase/sdk-react 0.1.0-rc.1 → 2026.1.1-rc.1

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/src/use-rpc.ts DELETED
@@ -1,109 +0,0 @@
1
- /**
2
- * React hooks for RPC (Remote Procedure Calls)
3
- * Call PostgreSQL functions with React Query integration
4
- */
5
-
6
- import { useQuery, useMutation, useQueryClient, type UseQueryOptions, type UseMutationOptions } from '@tanstack/react-query'
7
- import { useFluxbaseClient } from './context'
8
- import type { PostgrestResponse } from '@fluxbase/sdk'
9
-
10
- /**
11
- * Hook to call a PostgreSQL function and cache the result
12
- *
13
- * @example
14
- * ```tsx
15
- * const { data, isLoading, error } = useRPC(
16
- * 'calculate_total',
17
- * { order_id: 123 },
18
- * { enabled: !!orderId }
19
- * )
20
- * ```
21
- */
22
- export function useRPC<TData = unknown, TParams extends Record<string, unknown> = Record<string, unknown>>(
23
- functionName: string,
24
- params?: TParams,
25
- options?: Omit<UseQueryOptions<TData, Error>, 'queryKey' | 'queryFn'>
26
- ) {
27
- const client = useFluxbaseClient()
28
-
29
- return useQuery<TData, Error>({
30
- queryKey: ['rpc', functionName, params],
31
- queryFn: async () => {
32
- const { data, error } = await client.rpc<TData>(functionName, params)
33
- if (error) {
34
- throw new Error(error.message)
35
- }
36
- return data as TData
37
- },
38
- ...options,
39
- })
40
- }
41
-
42
- /**
43
- * Hook to create a mutation for calling PostgreSQL functions
44
- * Useful for functions that modify data
45
- *
46
- * @example
47
- * ```tsx
48
- * const createOrder = useRPCMutation('create_order')
49
- *
50
- * const handleSubmit = async () => {
51
- * await createOrder.mutateAsync({
52
- * user_id: 123,
53
- * items: [{ product_id: 1, quantity: 2 }]
54
- * })
55
- * }
56
- * ```
57
- */
58
- export function useRPCMutation<TData = unknown, TParams extends Record<string, unknown> = Record<string, unknown>>(
59
- functionName: string,
60
- options?: Omit<UseMutationOptions<TData, Error, TParams>, 'mutationFn'>
61
- ) {
62
- const client = useFluxbaseClient()
63
-
64
- return useMutation<TData, Error, TParams>({
65
- mutationFn: async (params: TParams) => {
66
- const { data, error } = await client.rpc<TData>(functionName, params)
67
- if (error) {
68
- throw new Error(error.message)
69
- }
70
- return data as TData
71
- },
72
- ...options,
73
- })
74
- }
75
-
76
- /**
77
- * Hook to call multiple RPC functions in parallel
78
- *
79
- * @example
80
- * ```tsx
81
- * const { data, isLoading } = useRPCBatch([
82
- * { name: 'get_user_stats', params: { user_id: 123 } },
83
- * { name: 'get_recent_orders', params: { limit: 10 } },
84
- * ])
85
- * ```
86
- */
87
- export function useRPCBatch<TData = unknown>(
88
- calls: Array<{ name: string; params?: Record<string, unknown> }>,
89
- options?: Omit<UseQueryOptions<TData[], Error, TData[], readonly unknown[]>, 'queryKey' | 'queryFn'>
90
- ) {
91
- const client = useFluxbaseClient()
92
-
93
- return useQuery({
94
- queryKey: ['rpc-batch', calls] as const,
95
- queryFn: async () => {
96
- const results = await Promise.all(
97
- calls.map(async ({ name, params }) => {
98
- const { data, error } = await client.rpc<TData>(name, params)
99
- if (error) {
100
- throw new Error(`${name}: ${error.message}`)
101
- }
102
- return data
103
- })
104
- )
105
- return results as TData[]
106
- },
107
- ...options,
108
- })
109
- }