@supatype/react 0.1.0-alpha.6
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/.turbo/turbo-build.log +4 -0
- package/.turbo/turbo-typecheck.log +4 -0
- package/dist/context.d.ts +31 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +35 -0
- package/dist/context.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/useAuth.d.ts +77 -0
- package/dist/useAuth.d.ts.map +1 -0
- package/dist/useAuth.js +45 -0
- package/dist/useAuth.js.map +1 -0
- package/dist/useFunction.d.ts +35 -0
- package/dist/useFunction.d.ts.map +1 -0
- package/dist/useFunction.js +38 -0
- package/dist/useFunction.js.map +1 -0
- package/dist/useLivePreview.d.ts +35 -0
- package/dist/useLivePreview.d.ts.map +1 -0
- package/dist/useLivePreview.js +46 -0
- package/dist/useLivePreview.js.map +1 -0
- package/dist/useMutation.d.ts +29 -0
- package/dist/useMutation.d.ts.map +1 -0
- package/dist/useMutation.js +59 -0
- package/dist/useMutation.js.map +1 -0
- package/dist/useQuery.d.ts +41 -0
- package/dist/useQuery.d.ts.map +1 -0
- package/dist/useQuery.js +68 -0
- package/dist/useQuery.js.map +1 -0
- package/dist/useSubscription.d.ts +32 -0
- package/dist/useSubscription.d.ts.map +1 -0
- package/dist/useSubscription.js +38 -0
- package/dist/useSubscription.js.map +1 -0
- package/package.json +31 -0
- package/src/context.tsx +44 -0
- package/src/index.ts +20 -0
- package/src/useAuth.ts +84 -0
- package/src/useFunction.ts +70 -0
- package/src/useLivePreview.ts +62 -0
- package/src/useMutation.ts +90 -0
- package/src/useQuery.ts +109 -0
- package/src/useSubscription.ts +70 -0
- package/tsconfig.json +14 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import type { SupatypeClient, AnyDatabase } from "@supatype/client";
|
|
3
|
+
export declare const SupatypeContext: React.Context<SupatypeClient<any> | null>;
|
|
4
|
+
export interface SupatypeProviderProps {
|
|
5
|
+
client: SupatypeClient<any>;
|
|
6
|
+
children: React.ReactNode;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Wrap your application with SupatypeProvider to make the client available
|
|
10
|
+
* via all Supatype hooks.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* const client = createClient({ url: '...', anonKey: '...' })
|
|
15
|
+
*
|
|
16
|
+
* function App() {
|
|
17
|
+
* return (
|
|
18
|
+
* <SupatypeProvider client={client}>
|
|
19
|
+
* <YourApp />
|
|
20
|
+
* </SupatypeProvider>
|
|
21
|
+
* )
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function SupatypeProvider({ client, children }: SupatypeProviderProps): React.ReactElement;
|
|
26
|
+
/**
|
|
27
|
+
* Access the Supatype client directly. Most callers should use the
|
|
28
|
+
* higher-level hooks (useAuth, useQuery, useMutation) instead.
|
|
29
|
+
*/
|
|
30
|
+
export declare function useSupatype<TDatabase extends AnyDatabase = AnyDatabase>(): SupatypeClient<TDatabase>;
|
|
31
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAGnE,eAAO,MAAM,eAAe,2CAAwD,CAAA;AAEpF,MAAM,WAAW,qBAAqB;IAEpC,MAAM,EAAE,cAAc,CAAC,GAAG,CAAC,CAAA;IAC3B,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;CAC1B;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,gBAAgB,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,qBAAqB,GAAG,KAAK,CAAC,YAAY,CAEhG;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,SAAS,SAAS,WAAW,GAAG,WAAW,KAAK,cAAc,CAAC,SAAS,CAAC,CAMpG"}
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3
|
+
export const SupatypeContext = React.createContext(null);
|
|
4
|
+
/**
|
|
5
|
+
* Wrap your application with SupatypeProvider to make the client available
|
|
6
|
+
* via all Supatype hooks.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```tsx
|
|
10
|
+
* const client = createClient({ url: '...', anonKey: '...' })
|
|
11
|
+
*
|
|
12
|
+
* function App() {
|
|
13
|
+
* return (
|
|
14
|
+
* <SupatypeProvider client={client}>
|
|
15
|
+
* <YourApp />
|
|
16
|
+
* </SupatypeProvider>
|
|
17
|
+
* )
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function SupatypeProvider({ client, children }) {
|
|
22
|
+
return React.createElement(SupatypeContext.Provider, { value: client }, children);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Access the Supatype client directly. Most callers should use the
|
|
26
|
+
* higher-level hooks (useAuth, useQuery, useMutation) instead.
|
|
27
|
+
*/
|
|
28
|
+
export function useSupatype() {
|
|
29
|
+
const client = React.useContext(SupatypeContext);
|
|
30
|
+
if (client === null) {
|
|
31
|
+
throw new Error("useSupatype must be used inside a <SupatypeProvider>");
|
|
32
|
+
}
|
|
33
|
+
return client;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAGzB,8DAA8D;AAC9D,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,CAAC,aAAa,CAA6B,IAAI,CAAC,CAAA;AAQpF;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,gBAAgB,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAyB;IAC1E,OAAO,KAAK,CAAC,aAAa,CAAC,eAAe,CAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,QAAQ,CAAC,CAAA;AACnF,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW;IACzB,MAAM,MAAM,GAAG,KAAK,CAAC,UAAU,CAAC,eAAe,CAAqC,CAAA;IACpF,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC,CAAA;IACzE,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { SupatypeProvider, SupatypeContext, useSupatype } from "./context.js";
|
|
2
|
+
export type { SupatypeProviderProps } from "./context.js";
|
|
3
|
+
export { useAuth } from "./useAuth.js";
|
|
4
|
+
export type { UseAuthReturn } from "./useAuth.js";
|
|
5
|
+
export { useQuery } from "./useQuery.js";
|
|
6
|
+
export type { UseQueryOptions, UseQueryResult } from "./useQuery.js";
|
|
7
|
+
export { useMutation } from "./useMutation.js";
|
|
8
|
+
export type { UseMutationResult, MutationOperation, MutationOptions } from "./useMutation.js";
|
|
9
|
+
export { useSubscription } from "./useSubscription.js";
|
|
10
|
+
export type { UseSubscriptionOptions, UseSubscriptionResult, SubscriptionStatus } from "./useSubscription.js";
|
|
11
|
+
export { useLivePreview } from "./useLivePreview.js";
|
|
12
|
+
export type { UseLivePreviewOptions, UseLivePreviewResult } from "./useLivePreview.js";
|
|
13
|
+
export { useFunction } from "./useFunction.js";
|
|
14
|
+
export type { UseFunctionResult } from "./useFunction.js";
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAC7E,YAAY,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAA;AAEzD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,YAAY,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAEjD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACxC,YAAY,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAEpE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,YAAY,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAE7F,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AACtD,YAAY,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AAE7G,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AACpD,YAAY,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAA;AAEtF,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,YAAY,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { SupatypeProvider, SupatypeContext, useSupatype } from "./context.js";
|
|
2
|
+
export { useAuth } from "./useAuth.js";
|
|
3
|
+
export { useQuery } from "./useQuery.js";
|
|
4
|
+
export { useMutation } from "./useMutation.js";
|
|
5
|
+
export { useSubscription } from "./useSubscription.js";
|
|
6
|
+
export { useLivePreview } from "./useLivePreview.js";
|
|
7
|
+
export { useFunction } from "./useFunction.js";
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAG7E,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAGtC,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAGxC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAG9C,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAGtD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAGpD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { Session, User, SupatypeError } from "@supatype/client";
|
|
2
|
+
export interface UseAuthReturn {
|
|
3
|
+
/** The currently authenticated user, or null if not signed in. */
|
|
4
|
+
user: User | null;
|
|
5
|
+
/** The current session (includes access token), or null. */
|
|
6
|
+
session: Session | null;
|
|
7
|
+
/** True while loading the initial auth state. */
|
|
8
|
+
loading: boolean;
|
|
9
|
+
/** Sign up with email and password. */
|
|
10
|
+
signUp(credentials: {
|
|
11
|
+
email: string;
|
|
12
|
+
password: string;
|
|
13
|
+
options?: {
|
|
14
|
+
data?: Record<string, unknown>;
|
|
15
|
+
} | undefined;
|
|
16
|
+
}): Promise<{
|
|
17
|
+
data: {
|
|
18
|
+
session: Session | null;
|
|
19
|
+
user: User | null;
|
|
20
|
+
};
|
|
21
|
+
error: SupatypeError | null;
|
|
22
|
+
}>;
|
|
23
|
+
/** Sign in with email and password. */
|
|
24
|
+
signIn(credentials: {
|
|
25
|
+
email: string;
|
|
26
|
+
password: string;
|
|
27
|
+
}): Promise<{
|
|
28
|
+
data: {
|
|
29
|
+
session: Session | null;
|
|
30
|
+
user: User | null;
|
|
31
|
+
};
|
|
32
|
+
error: SupatypeError | null;
|
|
33
|
+
}>;
|
|
34
|
+
/** Sign in via OAuth provider (returns redirect URL). */
|
|
35
|
+
signInWithOAuth(opts: {
|
|
36
|
+
provider: string;
|
|
37
|
+
options?: {
|
|
38
|
+
redirectTo?: string;
|
|
39
|
+
} | undefined;
|
|
40
|
+
}): Promise<{
|
|
41
|
+
data: {
|
|
42
|
+
url: string;
|
|
43
|
+
provider: string;
|
|
44
|
+
};
|
|
45
|
+
error: SupatypeError | null;
|
|
46
|
+
}>;
|
|
47
|
+
/** Sign in with a magic link (OTP) sent to the email. */
|
|
48
|
+
signInWithOtp(opts: {
|
|
49
|
+
email: string;
|
|
50
|
+
options?: {
|
|
51
|
+
emailRedirectTo?: string;
|
|
52
|
+
} | undefined;
|
|
53
|
+
}): Promise<{
|
|
54
|
+
data: {
|
|
55
|
+
messageId?: string | undefined;
|
|
56
|
+
};
|
|
57
|
+
error: SupatypeError | null;
|
|
58
|
+
}>;
|
|
59
|
+
/** Sign out the current user. */
|
|
60
|
+
signOut(): Promise<{
|
|
61
|
+
error: SupatypeError | null;
|
|
62
|
+
}>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Access auth state and methods. Must be used inside a <SupatypeProvider>.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```tsx
|
|
69
|
+
* function LoginButton() {
|
|
70
|
+
* const { user, signIn, signOut } = useAuth()
|
|
71
|
+
* if (user) return <button onClick={signOut}>Sign out</button>
|
|
72
|
+
* return <button onClick={() => signIn({ email, password })}>Sign in</button>
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export declare function useAuth(): UseAuthReturn;
|
|
77
|
+
//# sourceMappingURL=useAuth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAuth.d.ts","sourceR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E,GAAG,EAAE,MAAM,CAAC;YAAC,QAAQ,EAAE,MAAM,CAAA;SAAE,CAAC;QAAC,KAAK,EAAE,aAAa,GAAG,IAAI,CAAA;KAAE,CAAC,CAAA;IACrF,yDAAyD;IACzD,aAAa,CAAC,IAAI,EAAE;QAClB,KAAK,EAAE,MAAM,CAAA;QACb,OAAO,CAAC,EAAE;YAAE,eAAe,CAAC,EAAE,MAAM,CAAA;SAAE,GAAG,SAAS,CAAA;KACnD,GAAG,OAAO,CAAC;QAAE,IAAI,EAAE;YAAE,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;SAAE,CAAC;QAAC,KAAK,EAAE,aAAa,GAAG,IAAI,CAAA;KAAE,CAAC,CAAA;IACtF,iCAAiC;IACjC,OAAO,IAAI,OAAO,CAAC;QAAE,KAAK,EAAE,aAAa,GAAG,IAAI,CAAA;KAAE,CAAC,CAAA;CACpD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,OAAO,IAAI,aAAa,CAmCvC"}
|
package/dist/useAuth.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { useState, useEffect } from "react";
|
|
2
|
+
import { useSupatype } from "./context.js";
|
|
3
|
+
/**
|
|
4
|
+
* Access auth state and methods. Must be used inside a <SupatypeProvider>.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* function LoginButton() {
|
|
9
|
+
* const { user, signIn, signOut } = useAuth()
|
|
10
|
+
* if (user) return <button onClick={signOut}>Sign out</button>
|
|
11
|
+
* return <button onClick={() => signIn({ email, password })}>Sign in</button>
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function useAuth() {
|
|
16
|
+
const client = useSupatype();
|
|
17
|
+
const [user, setUser] = useState(null);
|
|
18
|
+
const [session, setSession] = useState(null);
|
|
19
|
+
const [loading, setLoading] = useState(true);
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
// Seed from current session
|
|
22
|
+
void client.auth.getSession().then(({ data }) => {
|
|
23
|
+
setSession(data.session);
|
|
24
|
+
setUser(data.session?.user ?? null);
|
|
25
|
+
setLoading(false);
|
|
26
|
+
});
|
|
27
|
+
// Subscribe to future changes
|
|
28
|
+
const { data: { subscription } } = client.auth.onAuthStateChange((_event, newSession) => {
|
|
29
|
+
setSession(newSession);
|
|
30
|
+
setUser(newSession?.user ?? null);
|
|
31
|
+
});
|
|
32
|
+
return () => subscription.unsubscribe();
|
|
33
|
+
}, [client]);
|
|
34
|
+
return {
|
|
35
|
+
user,
|
|
36
|
+
session,
|
|
37
|
+
loading,
|
|
38
|
+
signUp: (creds) => client.auth.signUp(creds),
|
|
39
|
+
signIn: (creds) => client.auth.signInWithPassword(creds),
|
|
40
|
+
signInWithOAuth: (opts) => client.auth.signInWithOAuth(opts),
|
|
41
|
+
signInWithOtp: (opts) => client.auth.signInWithOtp(opts),
|
|
42
|
+
signOut: () => client.auth.signOut(),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=useAuth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAuth.js","sourceRoot":"","sources":["../src/useAuth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAE3C,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAkC1C;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,OAAO;IACrB,MAAM,MAAM,GAAG,WAAW,EAAE,CAAA;IAC5B,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,CAAc,IAAI,CAAC,CAAA;IACnD,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAiB,IAAI,CAAC,CAAA;IAC5D,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAA;IAE5C,SAAS,CAAC,GAAG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 +1,35 @@
|
|
|
1
|
+
import type { AnyDatabase, SupatypeError } from "@supatype/client";
|
|
2
|
+
export interface FunctionCallOptions {
|
|
3
|
+
/** HTTP method. Default: POST */
|
|
4
|
+
method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | undefined;
|
|
5
|
+
/** Extra request headers */
|
|
6
|
+
headers?: Record<string, string> | undefined;
|
|
7
|
+
}
|
|
8
|
+
export interface UseFunctionResult<TResponse> {
|
|
9
|
+
/** Invoke the function with optional options */
|
|
10
|
+
invoke(body?: unknown, options?: FunctionCallOptions | undefined): Promise<{
|
|
11
|
+
data: TResponse | null;
|
|
12
|
+
error: SupatypeError | null;
|
|
13
|
+
}>;
|
|
14
|
+
/** Most recent response data */
|
|
15
|
+
data: TResponse | null;
|
|
16
|
+
/** Most recent error */
|
|
17
|
+
error: SupatypeError | null;
|
|
18
|
+
/** True while the function is executing */
|
|
19
|
+
loading: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Invoke a Supatype Edge Function.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```tsx
|
|
26
|
+
* const { invoke, data, loading, error } = useFunction<OrderResult>('process-order')
|
|
27
|
+
*
|
|
28
|
+
* const handleSubmit = async () => {
|
|
29
|
+
* const { data, error } = await invoke({ items: cart, address })
|
|
30
|
+
* if (data) router.push(`/orders/${data.orderId}`)
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function useFunction<TResponse = unknown, TDatabase extends AnyDatabase = AnyDatabase>(functionName: string): UseFunctionResult<TResponse>;
|
|
35
|
+
//# sourceMappingURL=useFunction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFunction.d.ts","sourceRoot":"","sources":["../src/useFunction.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAGlE,MAAM,WAAW,mBAAmB;IAClC,iCAAiC;IACjC,MAAM,CAAC,EAAE,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,OAAO,GAAG,QAAQ,GAAG,SAAS,CAAA;IAChE,4BAA4B;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,CAAA;CAC7C;AAED,MAAM,WAAW,iBAAiB,CAAC,SAAS;IAC1C,gDAAgD;IAChD,MAAM,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,SAAS,GAAG,OAAO,CAAC;QAAE,IAAI,EAAE,SAAS,GAAG,IAAI,CAAC;QAAC,KAAK,EAAE,aAAa,GAAG,IAAI,CAAA;KAAE,CAAC,CAAA;IACnI,gCAAgC;IAChC,IAAI,EAAE,SAAS,GAAG,IAAI,CAAA;IACtB,wBAAwB;IACxB,KAAK,EAAE,aAAa,GAAG,IAAI,CAAA;IAC3B,2CAA2C;IAC3C,OAAO,EAAE,OAAO,CAAA;CACjB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CACzB,SAAS,GAAG,OAAO,EACnB,SAAS,SAAS,WAAW,GAAG,WAAW,EAE3C,YAAY,EAAE,MAAM,GACnB,iBAAiB,CAAC,SAAS,CAAC,CA6B9B"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useState, useCallback } from "react";
|
|
2
|
+
import { useSupatype } from "./context.js";
|
|
3
|
+
/**
|
|
4
|
+
* Invoke a Supatype Edge Function.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* const { invoke, data, loading, error } = useFunction<OrderResult>('process-order')
|
|
9
|
+
*
|
|
10
|
+
* const handleSubmit = async () => {
|
|
11
|
+
* const { data, error } = await invoke({ items: cart, address })
|
|
12
|
+
* if (data) router.push(`/orders/${data.orderId}`)
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export function useFunction(functionName) {
|
|
17
|
+
const client = useSupatype();
|
|
18
|
+
const [data, setData] = useState(null);
|
|
19
|
+
const [error, setError] = useState(null);
|
|
20
|
+
const [loading, setLoading] = useState(false);
|
|
21
|
+
const invoke = useCallback(async (body, options) => {
|
|
22
|
+
setLoading(true);
|
|
23
|
+
setError(null);
|
|
24
|
+
const result = await client.functions.invoke(functionName, {
|
|
25
|
+
...options,
|
|
26
|
+
...(body !== undefined ? { body } : {}),
|
|
27
|
+
});
|
|
28
|
+
setLoading(false);
|
|
29
|
+
setData(result.data);
|
|
30
|
+
if (result.error !== null)
|
|
31
|
+
setError(result.error);
|
|
32
|
+
return result;
|
|
33
|
+
},
|
|
34
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
35
|
+
[client, functionName]);
|
|
36
|
+
return { invoke, data, error, loading };
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=useFunction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useFunction.js","sourceRoot":"","sources":["../src/useFunction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,OAAO,CAAA;AAE7C,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAoB1C;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,WAAW,CAIzB,YAAoB;IAEpB,MAAM,MAAM,GAAG,WAAW,EAAa,CAAA;IACvC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,CAAmB,IAAI,CAAC,CAAA;IACxD,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAuB,IAAI,CAAC,CAAA;IAC9D,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;IAE7C,MAAM,MAAM,GAAG,WAAW,CACxB,KAAK,EACH,IAAc,EACd,OAAyC,EACzC,EAAE;QACF,UAAU,CAAC,IAAI,CAAC,CAAA;QAChB,QAAQ,CAAC,IAAI,CAAC,CAAA;QAEd,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,SAAS,CAAC,MAAM,CAAY,YAAY,EAAE;YACpE,GAAG,OAAO;YACV,GAAG,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SACxC,CAAC,CAAA;QAEF,UAAU,CAAC,KAAK,CAAC,CAAA;QACjB,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QACpB,IAAI,MAAM,CAAC,KAAK,KAAK,IAAI;YAAE,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;QACjD,OAAO,MAAM,CAAA;IACf,CAAC;IACD,uDAAuD;IACvD,CAAC,MAAM,EAAE,YAAY,CAAC,CACvB,CAAA;IAED,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,CAAA;AACzC,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export interface UseLivePreviewOptions<TData> {
|
|
2
|
+
/** The initial/server data to display when not in preview mode. */
|
|
3
|
+
initialData: TData;
|
|
4
|
+
/** Optional model name filter — only accept preview data for this model. */
|
|
5
|
+
model?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface UseLivePreviewResult<TData> {
|
|
8
|
+
/** The current data — either live preview data or initial data. */
|
|
9
|
+
data: TData;
|
|
10
|
+
/** Whether the component is currently receiving live preview updates. */
|
|
11
|
+
isPreview: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Hook for receiving live preview updates from the Supatype admin panel.
|
|
15
|
+
*
|
|
16
|
+
* When the admin panel's edit view is open with Live Preview enabled,
|
|
17
|
+
* it sends `postMessage` events with form data as the editor types.
|
|
18
|
+
* This hook receives those events and returns the live data.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```tsx
|
|
22
|
+
* function BlogPost({ post }: { post: Post }) {
|
|
23
|
+
* const { data, isPreview } = useLivePreview({ initialData: post, model: "post" })
|
|
24
|
+
* return (
|
|
25
|
+
* <article>
|
|
26
|
+
* {isPreview && <div className="preview-badge">Preview</div>}
|
|
27
|
+
* <h1>{data.title}</h1>
|
|
28
|
+
* <div>{data.body}</div>
|
|
29
|
+
* </article>
|
|
30
|
+
* )
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function useLivePreview<TData>(opts: UseLivePreviewOptions<TData>): UseLivePreviewResult<TData>;
|
|
35
|
+
//# sourceMappingURL=useLivePreview.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useLivePreview.d.ts","sourceRoot":"","sources":["../src/useLivePreview.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,qBAAqB,CAAC,KAAK;IAC1C,mEAAmE;IACnE,WAAW,EAAE,KAAK,CAAA;IAClB,4EAA4E;IAC5E,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,oBAAoB,CAAC,KAAK;IACzC,mEAAmE;IACnE,IAAI,EAAE,KAAK,CAAA;IACX,yEAAyE;IACzE,SAAS,EAAE,OAAO,CAAA;CACnB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAClC,IAAI,EAAE,qBAAqB,CAAC,KAAK,CAAC,GACjC,oBAAoB,CAAC,KAAK,CAAC,CAsB7B"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { useState, useEffect, useRef } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Hook for receiving live preview updates from the Supatype admin panel.
|
|
4
|
+
*
|
|
5
|
+
* When the admin panel's edit view is open with Live Preview enabled,
|
|
6
|
+
* it sends `postMessage` events with form data as the editor types.
|
|
7
|
+
* This hook receives those events and returns the live data.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```tsx
|
|
11
|
+
* function BlogPost({ post }: { post: Post }) {
|
|
12
|
+
* const { data, isPreview } = useLivePreview({ initialData: post, model: "post" })
|
|
13
|
+
* return (
|
|
14
|
+
* <article>
|
|
15
|
+
* {isPreview && <div className="preview-badge">Preview</div>}
|
|
16
|
+
* <h1>{data.title}</h1>
|
|
17
|
+
* <div>{data.body}</div>
|
|
18
|
+
* </article>
|
|
19
|
+
* )
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function useLivePreview(opts) {
|
|
24
|
+
const [previewData, setPreviewData] = useState(null);
|
|
25
|
+
const initialRef = useRef(opts.initialData);
|
|
26
|
+
initialRef.current = opts.initialData;
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
const handler = (event) => {
|
|
29
|
+
if (typeof event.data !== "object" || event.data === null)
|
|
30
|
+
return;
|
|
31
|
+
const msg = event.data;
|
|
32
|
+
if (msg.type !== "supatype:live-preview")
|
|
33
|
+
return;
|
|
34
|
+
if (opts.model && msg.model !== opts.model)
|
|
35
|
+
return;
|
|
36
|
+
setPreviewData(msg.data);
|
|
37
|
+
};
|
|
38
|
+
window.addEventListener("message", handler);
|
|
39
|
+
return () => { window.removeEventListener("message", handler); };
|
|
40
|
+
}, [opts.model]);
|
|
41
|
+
return {
|
|
42
|
+
data: previewData ?? initialRef.current,
|
|
43
|
+
isPreview: previewData !== null,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=useLivePreview.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useLivePreview.js","sourceRoot":"","sources":["../src/useLivePreview.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAA;AAgBnD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,cAAc,CAC5B,IAAkC;IAElC,MAAM,CAAC,WAAW,EAAE,cAAc,CAAC,GAAG,QAAQ,CAAe,IAAI,CAAC,CAAA;IAClE,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAA;IAC3C,UAAU,CAAC,OAAO,GAAG,IAAI,CAAC,WAAW,CAAA;IAErC,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,OAAO,GAAG,CAAC,KAAmB,EAAE,EAAE;YACtC,IAAI,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,KAAK,IAAI;gBAAE,OAAM;YACjE,MAAM,GAAG,GAAG,KAAK,CAAC,IAAyD,CAAA;YAC3E,IAAI,GAAG,CAAC,IAAI,KAAK,uBAAuB;gBAAE,OAAM;YAChD,IAAI,IAAI,CAAC,KAAK,IAAI,GAAG,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK;gBAAE,OAAM;YAClD,cAAc,CAAC,GAAG,CAAC,IAAa,CAAC,CAAA;QACnC,CAAC,CAAA;QAED,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAA;QAC3C,OAAO,GAAG,EAAE,GAAG,MAAM,CAAC,mBAAmB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAA,CAAC,CAAC,CAAA;IACjE,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAA;IAEhB,OAAO;QACL,IAAI,EAAE,WAAW,IAAI,UAAU,CAAC,OAAO;QACvC,SAAS,EAAE,WAAW,KAAK,IAAI;KAChC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { AnyDatabase, SupatypeError } from "@supatype/client";
|
|
2
|
+
export type MutationOperation = "insert" | "update" | "delete" | "upsert";
|
|
3
|
+
export interface MutationOptions {
|
|
4
|
+
/** Simple equality filters for update/delete operations */
|
|
5
|
+
filter?: Record<string, unknown> | undefined;
|
|
6
|
+
}
|
|
7
|
+
export interface UseMutationResult<TRow> {
|
|
8
|
+
mutate(data?: Record<string, unknown> | Record<string, unknown>[] | undefined, options?: MutationOptions | undefined): Promise<{
|
|
9
|
+
data: TRow[] | null;
|
|
10
|
+
error: SupatypeError | null;
|
|
11
|
+
count: number | null;
|
|
12
|
+
}>;
|
|
13
|
+
loading: boolean;
|
|
14
|
+
error: SupatypeError | null;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Perform insert, update, delete, or upsert on a table.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* const { mutate: createPost, loading } = useMutation('posts', 'insert')
|
|
22
|
+
* await createPost({ title: 'Hello', status: 'draft' })
|
|
23
|
+
*
|
|
24
|
+
* const { mutate: deletePost } = useMutation('posts', 'delete')
|
|
25
|
+
* await deletePost(undefined, { filter: { id: postId } })
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function useMutation<TDatabase extends AnyDatabase = AnyDatabase, TTable extends keyof TDatabase["public"]["Tables"] & string = keyof TDatabase["public"]["Tables"] & string, TRow = TDatabase["public"]["Tables"][TTable]["Row"]>(table: TTable, operation: MutationOperation): UseMutationResult<TRow>;
|
|
29
|
+
//# sourceMappingURL=useMutation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useMutation.d.ts","sourceRoot":"","sources":["../src/useMutation.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAGlE,MAAM,MAAM,iBAAiB,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,CAAA;AAEzE,MAAM,WAAW,eAAe;IAC9B,2DAA2D;IAC3D,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAA;CAC7C;AAED,MAAM,WAAW,iBAAiB,CAAC,IAAI;IACrC,MAAM,CACJ,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,SAAS,EACtE,OAAO,CAAC,EAAE,eAAe,GAAG,SAAS,GACpC,OAAO,CAAC;QAAE,IAAI,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC;QAAC,KAAK,EAAE,aAAa,GAAG,IAAI,CAAC;QAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC,CAAA;IACtF,OAAO,EAAE,OAAO,CAAA;IAChB,KAAK,EAAE,aAAa,GAAG,IAAI,CAAA;CAC5B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CACzB,SAAS,SAAS,WAAW,GAAG,WAAW,EAC3C,MAAM,SAAS,MAAM,SAAS,CAAC,QAAQ,CAAC,CAAC,QAAQ,CAAC,GAAG,MAAM,GAAG,MAAM,SAAS,CAAC,QAAQ,CAAC,CAAC,QAAQ,CAAC,GAAG,MAAM,EAC1G,IAAI,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,EAEnD,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,iBAAiB,GAC3B,iBAAiB,CAAC,IAAI,CAAC,CAkDzB"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { useState, useCallback } from "react";
|
|
2
|
+
import { useSupatype } from "./context.js";
|
|
3
|
+
/**
|
|
4
|
+
* Perform insert, update, delete, or upsert on a table.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* const { mutate: createPost, loading } = useMutation('posts', 'insert')
|
|
9
|
+
* await createPost({ title: 'Hello', status: 'draft' })
|
|
10
|
+
*
|
|
11
|
+
* const { mutate: deletePost } = useMutation('posts', 'delete')
|
|
12
|
+
* await deletePost(undefined, { filter: { id: postId } })
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function useMutation(table, operation) {
|
|
16
|
+
const client = useSupatype();
|
|
17
|
+
const [loading, setLoading] = useState(false);
|
|
18
|
+
const [error, setError] = useState(null);
|
|
19
|
+
const mutate = useCallback(async (data, options) => {
|
|
20
|
+
setLoading(true);
|
|
21
|
+
setError(null);
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
23
|
+
const tableClient = client.from(table);
|
|
24
|
+
let result;
|
|
25
|
+
if (operation === "insert") {
|
|
26
|
+
result = await tableClient.insert(data);
|
|
27
|
+
}
|
|
28
|
+
else if (operation === "upsert") {
|
|
29
|
+
result = await tableClient.upsert(data);
|
|
30
|
+
}
|
|
31
|
+
else if (operation === "update") {
|
|
32
|
+
let q = tableClient.update(data);
|
|
33
|
+
if (options?.filter !== undefined) {
|
|
34
|
+
for (const [col, val] of Object.entries(options.filter)) {
|
|
35
|
+
q = q.eq(col, val);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
result = await q;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
// delete
|
|
42
|
+
let q = tableClient.delete();
|
|
43
|
+
if (options?.filter !== undefined) {
|
|
44
|
+
for (const [col, val] of Object.entries(options.filter)) {
|
|
45
|
+
q = q.eq(col, val);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
result = await q;
|
|
49
|
+
}
|
|
50
|
+
setLoading(false);
|
|
51
|
+
if (result.error !== null)
|
|
52
|
+
setError(result.error);
|
|
53
|
+
return result;
|
|
54
|
+
},
|
|
55
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
56
|
+
[client, table, operation]);
|
|
57
|
+
return { mutate, loading, error };
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=useMutation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useMutation.js","sourceRoot":"","sources":["../src/useMutation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,OAAO,CAAA;AAE7C,OAAO,EA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;QAClB,CAAC;aAAM,CAAC;YACN,SAAS;YACT,IAAI,CAAC,GAAG,WAAW,CAAC,MAAM,EAAE,CAAA;YAC5B,IAAI,OAAO,EAAE,MAAM,KAAK,SAAS,EAAE,CAAC;gBAClC,KAAK,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;oBACxD,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,GAAG,EAAE,GAAG,CAAC,CAAA;gBACpB,CAAC;YACH,CAAC;YACD,MAAM,GAAG,MAAM,CAAC,CAAA;QAClB,CAAC;QAED,UAAU,CAAC,KAAK,CAAC,CAAA;QACjB,IAAI,MAAM,CAAC,KAAK,KAAK,IAAI;YAAE,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;QACjD,OAAO,MAAM,CAAA;IACf,CAAC;IACD,uDAAuD;IACvD,CAAC,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,CAC3B,CAAA;IAED,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,CAAA;AACnC,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { AnyDatabase, SupatypeError } from "@supatype/client";
|
|
2
|
+
export interface UseQueryOptions {
|
|
3
|
+
/** PostgREST select expression (default: '*') */
|
|
4
|
+
select?: string | undefined;
|
|
5
|
+
/** Simple equality filters keyed by column name */
|
|
6
|
+
filter?: Record<string, unknown> | undefined;
|
|
7
|
+
/** Column to order by */
|
|
8
|
+
order?: {
|
|
9
|
+
column: string;
|
|
10
|
+
ascending?: boolean | undefined;
|
|
11
|
+
} | undefined;
|
|
12
|
+
/** Maximum number of rows to return */
|
|
13
|
+
limit?: number | undefined;
|
|
14
|
+
/** Row offset (for pagination) */
|
|
15
|
+
offset?: number | undefined;
|
|
16
|
+
/** Set to false to skip the query (useful for conditional fetching) */
|
|
17
|
+
enabled?: boolean | undefined;
|
|
18
|
+
/** Re-fetch interval in milliseconds */
|
|
19
|
+
refetchInterval?: number | undefined;
|
|
20
|
+
}
|
|
21
|
+
export interface UseQueryResult<TRow> {
|
|
22
|
+
data: TRow[] | null;
|
|
23
|
+
error: SupatypeError | null;
|
|
24
|
+
count: number | null;
|
|
25
|
+
loading: boolean;
|
|
26
|
+
refetch(): void;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Fetch rows from a table with automatic re-execution when options change.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```tsx
|
|
33
|
+
* const { data: posts, loading } = useQuery('posts', {
|
|
34
|
+
* filter: { status: 'published' },
|
|
35
|
+
* order: { column: 'created_at', ascending: false },
|
|
36
|
+
* limit: 10,
|
|
37
|
+
* })
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare function useQuery<TDatabase extends AnyDatabase = AnyDatabase, TTable extends keyof TDatabase["public"]["Tables"] & string = keyof TDatabase["public"]["Tables"] & string, TRow = TDatabase["public"]["Tables"][TTable]["Row"]>(table: TTable, options?: UseQueryOptions | undefined): UseQueryResult<TRow>;
|
|
41
|
+
//# sourceMappingURL=useQuery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../src/useQuery.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAGlE,MAAM,WAAW,eAAe;IAC9B,iDAAiD;IACjD,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC3B,mDAAmD;IACnD,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAA;IAC5C,yBAAyB;IACzB,KAAK,CAAC,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;KAAE,GAAG,SAAS,CAAA;IACvE,uCAAuC;IACvC,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,kCAAkC;IAClC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC3B,uEAAuE;IACvE,OAAO,CAAC,EAAE,OAAO,GAAG,SAAS,CAAA;IAC7B,wCAAwC;IACxC,eAAe,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CACrC;AAED,MAAM,WAAW,cAAc,CAAC,IAAI;IAClC,IAAI,EAAE,IAAI,EAAE,GAAG,IAAI,CAAA;IACnB,KAAK,EAAE,aAAa,GAAG,IAAI,CAAA;IAC3B,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;IACpB,OAAO,EAAE,OAAO,CAAA;IAChB,OAAO,IAAI,IAAI,CAAA;CAChB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,QAAQ,CACtB,SAAS,SAAS,WAAW,GAAG,WAAW,EAC3C,MAAM,SAAS,MAAM,SAAS,CAAC,QAAQ,CAAC,CAAC,QAAQ,CAAC,GAAG,MAAM,GAAG,MAAM,SAAS,CAAC,QAAQ,CAAC,CAAC,QAAQ,CAAC,GAAG,MAAM,EAC1G,IAAI,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,EAEnD,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,eAAe,GAAG,SAAS,GACpC,cAAc,CAAC,IAAI,CAAC,CA4DtB"}
|
package/dist/useQuery.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { useState, useEffect, useCallback, useRef } from "react";
|
|
2
|
+
import { useSupatype } from "./context.js";
|
|
3
|
+
/**
|
|
4
|
+
* Fetch rows from a table with automatic re-execution when options change.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* const { data: posts, loading } = useQuery('posts', {
|
|
9
|
+
* filter: { status: 'published' },
|
|
10
|
+
* order: { column: 'created_at', ascending: false },
|
|
11
|
+
* limit: 10,
|
|
12
|
+
* })
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export function useQuery(table, options) {
|
|
16
|
+
const client = useSupatype();
|
|
17
|
+
const [data, setData] = useState(null);
|
|
18
|
+
const [error, setError] = useState(null);
|
|
19
|
+
const [count, setCount] = useState(null);
|
|
20
|
+
const [loading, setLoading] = useState(true);
|
|
21
|
+
// Stable reference to options for the effect dep
|
|
22
|
+
const optionsJson = JSON.stringify(options);
|
|
23
|
+
const intervalRef = useRef(null);
|
|
24
|
+
const execute = useCallback(async () => {
|
|
25
|
+
if (options?.enabled === false) {
|
|
26
|
+
setLoading(false);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
setLoading(true);
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
31
|
+
let query = client.from(table).select(options?.select ?? "*");
|
|
32
|
+
if (options?.filter !== undefined) {
|
|
33
|
+
for (const [col, val] of Object.entries(options.filter)) {
|
|
34
|
+
query = query.eq(col, val);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (options?.order !== undefined) {
|
|
38
|
+
query = query.order(options.order.column, { ascending: options.order.ascending ?? true });
|
|
39
|
+
}
|
|
40
|
+
if (options?.limit !== undefined) {
|
|
41
|
+
query = query.limit(options.limit);
|
|
42
|
+
}
|
|
43
|
+
if (options?.offset !== undefined && options.limit !== undefined) {
|
|
44
|
+
query = query.range(options.offset, options.offset + options.limit - 1);
|
|
45
|
+
}
|
|
46
|
+
const result = (await query);
|
|
47
|
+
setData(result.data);
|
|
48
|
+
setError(result.error);
|
|
49
|
+
setCount(result.count);
|
|
50
|
+
setLoading(false);
|
|
51
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
52
|
+
}, [client, table, optionsJson]);
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
void execute();
|
|
55
|
+
if (options?.refetchInterval !== undefined) {
|
|
56
|
+
intervalRef.current = setInterval(() => void execute(), options.refetchInterval);
|
|
57
|
+
}
|
|
58
|
+
return () => {
|
|
59
|
+
if (intervalRef.current !== null) {
|
|
60
|
+
clearInterval(intervalRef.current);
|
|
61
|
+
intervalRef.current = null;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
65
|
+
}, [execute]);
|
|
66
|
+
return { data, error, count, loading, refetch: () => void execute() };
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=useQuery.js.map
|