@sylphx/lens-react 1.0.3 → 1.0.4
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.ts +219 -8
- package/dist/index.js +26 -2744
- package/package.json +44 -45
- package/src/context.tsx +2 -2
- package/dist/context.d.ts +0 -52
- package/dist/context.d.ts.map +0 -1
- package/dist/hooks.d.ts +0 -190
- package/dist/hooks.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,220 @@
|
|
|
1
|
+
import { LensClient } from "@sylphx/lens-client";
|
|
2
|
+
import { ReactElement, ReactNode } from "react";
|
|
3
|
+
interface LensProviderProps {
|
|
4
|
+
/** Lens client instance */
|
|
5
|
+
client: LensClient<any, any>;
|
|
6
|
+
/** Children */
|
|
7
|
+
children: ReactNode;
|
|
8
|
+
}
|
|
1
9
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
* Provides Lens client to component tree
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* import { createClient, http } from '@sylphx/lens-client';
|
|
15
|
+
* import { LensProvider } from '@sylphx/lens-react';
|
|
16
|
+
* import type { AppRouter } from './server';
|
|
17
|
+
*
|
|
18
|
+
* const client = createClient<AppRouter>({
|
|
19
|
+
* transport: http({ url: '/api' }),
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* function App() {
|
|
23
|
+
* return (
|
|
24
|
+
* <LensProvider client={client}>
|
|
25
|
+
* <UserProfile />
|
|
26
|
+
* </LensProvider>
|
|
27
|
+
* );
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
declare function LensProvider({ client, children }: LensProviderProps): ReactElement;
|
|
32
|
+
/**
|
|
33
|
+
* Get Lens client from context
|
|
34
|
+
*
|
|
35
|
+
* @throws Error if used outside LensProvider
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```tsx
|
|
39
|
+
* function UserProfile({ userId }: { userId: string }) {
|
|
40
|
+
* const client = useLensClient<AppRouter>();
|
|
41
|
+
* const { data } = useQuery(client.user.get({ id: userId }));
|
|
42
|
+
* return <h1>{data?.name}</h1>;
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
declare function useLensClient<TRouter = any>(): LensClient<any, any> & TRouter;
|
|
47
|
+
import { MutationResult, QueryResult } from "@sylphx/lens-client";
|
|
48
|
+
/** Result of useQuery hook */
|
|
49
|
+
interface UseQueryResult<T> {
|
|
50
|
+
/** Query data (null if loading or error) */
|
|
51
|
+
data: T | null;
|
|
52
|
+
/** Loading state */
|
|
53
|
+
loading: boolean;
|
|
54
|
+
/** Error state */
|
|
55
|
+
error: Error | null;
|
|
56
|
+
/** Refetch the query */
|
|
57
|
+
refetch: () => void;
|
|
58
|
+
}
|
|
59
|
+
/** Result of useMutation hook */
|
|
60
|
+
interface UseMutationResult<
|
|
61
|
+
TInput,
|
|
62
|
+
TOutput
|
|
63
|
+
> {
|
|
64
|
+
/** Execute the mutation */
|
|
65
|
+
mutate: (input: TInput) => Promise<MutationResult<TOutput>>;
|
|
66
|
+
/** Mutation is in progress */
|
|
67
|
+
loading: boolean;
|
|
68
|
+
/** Mutation error */
|
|
69
|
+
error: Error | null;
|
|
70
|
+
/** Last mutation result */
|
|
71
|
+
data: TOutput | null;
|
|
72
|
+
/** Reset mutation state */
|
|
73
|
+
reset: () => void;
|
|
74
|
+
}
|
|
75
|
+
/** Options for useQuery */
|
|
76
|
+
interface UseQueryOptions {
|
|
77
|
+
/** Skip the query (don't execute) */
|
|
78
|
+
skip?: boolean;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Subscribe to a query with reactive updates
|
|
82
|
+
*
|
|
83
|
+
* @param query - QueryResult from client API call
|
|
84
|
+
* @param options - Query options
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```tsx
|
|
88
|
+
* // Basic usage
|
|
89
|
+
* function UserProfile({ userId }: { userId: string }) {
|
|
90
|
+
* const client = useLensClient();
|
|
91
|
+
* const { data: user, loading, error } = useQuery(client.user.get({ id: userId }));
|
|
92
|
+
*
|
|
93
|
+
* if (loading) return <Spinner />;
|
|
94
|
+
* if (error) return <Error message={error.message} />;
|
|
95
|
+
* if (!user) return <NotFound />;
|
|
96
|
+
*
|
|
97
|
+
* return <h1>{user.name}</h1>;
|
|
98
|
+
* }
|
|
99
|
+
*
|
|
100
|
+
* // With select (type-safe field selection)
|
|
101
|
+
* function UserName({ userId }: { userId: string }) {
|
|
102
|
+
* const client = useLensClient();
|
|
103
|
+
* const { data } = useQuery(
|
|
104
|
+
* client.user.get({ id: userId }).select({ name: true })
|
|
105
|
+
* );
|
|
106
|
+
* // data is { name: string } | null
|
|
107
|
+
* return <span>{data?.name}</span>;
|
|
108
|
+
* }
|
|
109
|
+
*
|
|
110
|
+
* // Skip query conditionally
|
|
111
|
+
* function ConditionalQuery({ shouldFetch }: { shouldFetch: boolean }) {
|
|
112
|
+
* const client = useLensClient();
|
|
113
|
+
* const { data } = useQuery(client.user.list(), { skip: !shouldFetch });
|
|
114
|
+
* }
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
declare function useQuery<T>(query: QueryResult<T>, options?: UseQueryOptions): UseQueryResult<T>;
|
|
118
|
+
/** Mutation function type */
|
|
119
|
+
type MutationFn<
|
|
120
|
+
TInput,
|
|
121
|
+
TOutput
|
|
122
|
+
> = (input: TInput) => Promise<MutationResult<TOutput>>;
|
|
123
|
+
/**
|
|
124
|
+
* Execute mutations with loading/error state
|
|
125
|
+
*
|
|
126
|
+
* @param mutationFn - Mutation function from client API
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* \`\`\`tsx
|
|
130
|
+
* function CreatePost() {
|
|
131
|
+
* const client = useLensClient();
|
|
132
|
+
* const { mutate, loading, error, data } = useMutation(client.post.create);
|
|
133
|
+
*
|
|
134
|
+
* const handleSubmit = async (formData: FormData) => {
|
|
135
|
+
* try {
|
|
136
|
+
* const result = await mutate({
|
|
137
|
+
* title: formData.get('title'),
|
|
138
|
+
* content: formData.get('content'),
|
|
139
|
+
* });
|
|
140
|
+
* console.log('Created:', result.data);
|
|
141
|
+
* } catch (err) {
|
|
142
|
+
* console.error('Failed:', err);
|
|
143
|
+
* }
|
|
144
|
+
* };
|
|
145
|
+
*
|
|
146
|
+
* return (
|
|
147
|
+
* <form onSubmit={handleSubmit}>
|
|
148
|
+
* <button type="submit" disabled={loading}>
|
|
149
|
+
* {loading ? 'Creating...' : 'Create'}
|
|
150
|
+
* </button>
|
|
151
|
+
* {error && <p className="error">{error.message}</p>}
|
|
152
|
+
* </form>
|
|
153
|
+
* );
|
|
154
|
+
* }
|
|
155
|
+
*
|
|
156
|
+
* // With optimistic updates
|
|
157
|
+
* function UpdatePost({ postId }: { postId: string }) {
|
|
158
|
+
* const client = useLensClient();
|
|
159
|
+
* const { mutate } = useMutation(client.post.update);
|
|
160
|
+
*
|
|
161
|
+
* const handleUpdate = async (title: string) => {
|
|
162
|
+
* const result = await mutate({ id: postId, title });
|
|
163
|
+
* // result.rollback?.() can undo optimistic update
|
|
164
|
+
* };
|
|
165
|
+
* }
|
|
166
|
+
* \`\`\`
|
|
167
|
+
*/
|
|
168
|
+
declare function useMutation<
|
|
169
|
+
TInput,
|
|
170
|
+
TOutput
|
|
171
|
+
>(mutationFn: MutationFn<TInput, TOutput>): UseMutationResult<TInput, TOutput>;
|
|
172
|
+
/** Result of useLazyQuery hook */
|
|
173
|
+
interface UseLazyQueryResult<T> {
|
|
174
|
+
/** Execute the query */
|
|
175
|
+
execute: () => Promise<T>;
|
|
176
|
+
/** Query data (null if not executed or error) */
|
|
177
|
+
data: T | null;
|
|
178
|
+
/** Loading state */
|
|
179
|
+
loading: boolean;
|
|
180
|
+
/** Error state */
|
|
181
|
+
error: Error | null;
|
|
182
|
+
/** Reset query state */
|
|
183
|
+
reset: () => void;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Execute a query on demand (not on mount)
|
|
187
|
+
*
|
|
188
|
+
* @param query - QueryResult from client API call
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```tsx
|
|
192
|
+
* function SearchUsers() {
|
|
193
|
+
* const client = useLensClient();
|
|
194
|
+
* const [searchTerm, setSearchTerm] = useState('');
|
|
195
|
+
* const { execute, data, loading } = useLazyQuery(
|
|
196
|
+
* client.user.search({ query: searchTerm })
|
|
197
|
+
* );
|
|
198
|
+
*
|
|
199
|
+
* const handleSearch = async () => {
|
|
200
|
+
* const users = await execute();
|
|
201
|
+
* console.log('Found:', users);
|
|
202
|
+
* };
|
|
203
|
+
*
|
|
204
|
+
* return (
|
|
205
|
+
* <div>
|
|
206
|
+
* <input
|
|
207
|
+
* value={searchTerm}
|
|
208
|
+
* onChange={e => setSearchTerm(e.target.value)}
|
|
209
|
+
* />
|
|
210
|
+
* <button onClick={handleSearch} disabled={loading}>
|
|
211
|
+
* Search
|
|
212
|
+
* </button>
|
|
213
|
+
* {data?.map(user => <UserCard key={user.id} user={user} />)}
|
|
214
|
+
* </div>
|
|
215
|
+
* );
|
|
216
|
+
* }
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
declare function useLazyQuery<T>(query: QueryResult<T>): UseLazyQueryResult<T>;
|
|
220
|
+
export { useQuery, useMutation, useLensClient, useLazyQuery, UseQueryResult, UseQueryOptions, UseMutationResult, UseLazyQueryResult, MutationFn, LensProviderProps, LensProvider };
|