@sylphx/lens-react 1.0.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.
- package/dist/context.d.ts +52 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/hooks.d.ts +190 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2876 -0
- package/package.json +47 -0
- package/src/context.tsx +90 -0
- package/src/hooks.test.tsx +358 -0
- package/src/hooks.ts +392 -0
- package/src/index.ts +30 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @sylphx/lens-react - Context Provider
|
|
3
|
+
*
|
|
4
|
+
* Provides Lens client to React component tree.
|
|
5
|
+
*/
|
|
6
|
+
import type { LensClient } from "@sylphx/lens-client";
|
|
7
|
+
import { type ReactNode } from "react";
|
|
8
|
+
export interface LensProviderProps {
|
|
9
|
+
/** Lens client instance */
|
|
10
|
+
client: LensClient<any, any>;
|
|
11
|
+
/** Children */
|
|
12
|
+
children: ReactNode;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Provides Lens client to component tree
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* import { createClient, http } from '@sylphx/lens-client';
|
|
20
|
+
* import { LensProvider } from '@sylphx/lens-react';
|
|
21
|
+
* import type { AppRouter } from './server';
|
|
22
|
+
*
|
|
23
|
+
* const client = createClient<AppRouter>({
|
|
24
|
+
* transport: http({ url: '/api' }),
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* function App() {
|
|
28
|
+
* return (
|
|
29
|
+
* <LensProvider client={client}>
|
|
30
|
+
* <UserProfile />
|
|
31
|
+
* </LensProvider>
|
|
32
|
+
* );
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function LensProvider({ client, children }: LensProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
37
|
+
/**
|
|
38
|
+
* Get Lens client from context
|
|
39
|
+
*
|
|
40
|
+
* @throws Error if used outside LensProvider
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```tsx
|
|
44
|
+
* function UserProfile({ userId }: { userId: string }) {
|
|
45
|
+
* const client = useLensClient<AppRouter>();
|
|
46
|
+
* const { data } = useQuery(client.user.get({ id: userId }));
|
|
47
|
+
* return <h1>{data?.name}</h1>;
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function useLensClient<TRouter = any>(): LensClient<any, any> & TRouter;
|
|
52
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.tsx"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,KAAK,SAAS,EAA6B,MAAM,OAAO,CAAC;AAiBlE,MAAM,WAAW,iBAAiB;IACjC,2BAA2B;IAE3B,MAAM,EAAE,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAC7B,eAAe;IACf,QAAQ,EAAE,SAAS,CAAC;CACpB;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,YAAY,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,iBAAiB,2CAEnE;AAMD;;;;;;;;;;;;;GAaG;AAEH,wBAAgB,aAAa,CAAC,OAAO,GAAG,GAAG,KAAK,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,OAAO,CAY7E"}
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @sylphx/lens-react - Hooks
|
|
3
|
+
*
|
|
4
|
+
* React hooks for Lens queries and mutations.
|
|
5
|
+
* Works with QueryResult from @sylphx/lens-client.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* import { useLensClient, useQuery, useMutation } from '@sylphx/lens-react';
|
|
10
|
+
*
|
|
11
|
+
* function UserProfile({ userId }: { userId: string }) {
|
|
12
|
+
* const client = useLensClient();
|
|
13
|
+
* const { data: user, loading } = useQuery(client.user.get({ id: userId }));
|
|
14
|
+
* if (loading) return <Spinner />;
|
|
15
|
+
* return <h1>{user?.name}</h1>;
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* function CreatePost() {
|
|
19
|
+
* const client = useLensClient();
|
|
20
|
+
* const { mutate, loading } = useMutation(client.post.create);
|
|
21
|
+
* const handleCreate = () => mutate({ title: 'Hello' });
|
|
22
|
+
* return <button onClick={handleCreate} disabled={loading}>Create</button>;
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
import type { MutationResult, QueryResult } from "@sylphx/lens-client";
|
|
27
|
+
/** Result of useQuery hook */
|
|
28
|
+
export interface UseQueryResult<T> {
|
|
29
|
+
/** Query data (null if loading or error) */
|
|
30
|
+
data: T | null;
|
|
31
|
+
/** Loading state */
|
|
32
|
+
loading: boolean;
|
|
33
|
+
/** Error state */
|
|
34
|
+
error: Error | null;
|
|
35
|
+
/** Refetch the query */
|
|
36
|
+
refetch: () => void;
|
|
37
|
+
}
|
|
38
|
+
/** Result of useMutation hook */
|
|
39
|
+
export interface UseMutationResult<TInput, TOutput> {
|
|
40
|
+
/** Execute the mutation */
|
|
41
|
+
mutate: (input: TInput) => Promise<MutationResult<TOutput>>;
|
|
42
|
+
/** Mutation is in progress */
|
|
43
|
+
loading: boolean;
|
|
44
|
+
/** Mutation error */
|
|
45
|
+
error: Error | null;
|
|
46
|
+
/** Last mutation result */
|
|
47
|
+
data: TOutput | null;
|
|
48
|
+
/** Reset mutation state */
|
|
49
|
+
reset: () => void;
|
|
50
|
+
}
|
|
51
|
+
/** Options for useQuery */
|
|
52
|
+
export interface UseQueryOptions {
|
|
53
|
+
/** Skip the query (don't execute) */
|
|
54
|
+
skip?: boolean;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Subscribe to a query with reactive updates
|
|
58
|
+
*
|
|
59
|
+
* @param query - QueryResult from client API call
|
|
60
|
+
* @param options - Query options
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```tsx
|
|
64
|
+
* // Basic usage
|
|
65
|
+
* function UserProfile({ userId }: { userId: string }) {
|
|
66
|
+
* const client = useLensClient();
|
|
67
|
+
* const { data: user, loading, error } = useQuery(client.user.get({ id: userId }));
|
|
68
|
+
*
|
|
69
|
+
* if (loading) return <Spinner />;
|
|
70
|
+
* if (error) return <Error message={error.message} />;
|
|
71
|
+
* if (!user) return <NotFound />;
|
|
72
|
+
*
|
|
73
|
+
* return <h1>{user.name}</h1>;
|
|
74
|
+
* }
|
|
75
|
+
*
|
|
76
|
+
* // With select (type-safe field selection)
|
|
77
|
+
* function UserName({ userId }: { userId: string }) {
|
|
78
|
+
* const client = useLensClient();
|
|
79
|
+
* const { data } = useQuery(
|
|
80
|
+
* client.user.get({ id: userId }).select({ name: true })
|
|
81
|
+
* );
|
|
82
|
+
* // data is { name: string } | null
|
|
83
|
+
* return <span>{data?.name}</span>;
|
|
84
|
+
* }
|
|
85
|
+
*
|
|
86
|
+
* // Skip query conditionally
|
|
87
|
+
* function ConditionalQuery({ shouldFetch }: { shouldFetch: boolean }) {
|
|
88
|
+
* const client = useLensClient();
|
|
89
|
+
* const { data } = useQuery(client.user.list(), { skip: !shouldFetch });
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export declare function useQuery<T>(query: QueryResult<T>, options?: UseQueryOptions): UseQueryResult<T>;
|
|
94
|
+
/** Mutation function type */
|
|
95
|
+
export type MutationFn<TInput, TOutput> = (input: TInput) => Promise<MutationResult<TOutput>>;
|
|
96
|
+
/**
|
|
97
|
+
* Execute mutations with loading/error state
|
|
98
|
+
*
|
|
99
|
+
* @param mutationFn - Mutation function from client API
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```tsx
|
|
103
|
+
* function CreatePost() {
|
|
104
|
+
* const client = useLensClient();
|
|
105
|
+
* const { mutate, loading, error, data } = useMutation(client.post.create);
|
|
106
|
+
*
|
|
107
|
+
* const handleSubmit = async (formData: FormData) => {
|
|
108
|
+
* try {
|
|
109
|
+
* const result = await mutate({
|
|
110
|
+
* title: formData.get('title'),
|
|
111
|
+
* content: formData.get('content'),
|
|
112
|
+
* });
|
|
113
|
+
* console.log('Created:', result.data);
|
|
114
|
+
* } catch (err) {
|
|
115
|
+
* console.error('Failed:', err);
|
|
116
|
+
* }
|
|
117
|
+
* };
|
|
118
|
+
*
|
|
119
|
+
* return (
|
|
120
|
+
* <form onSubmit={handleSubmit}>
|
|
121
|
+
* <button type="submit" disabled={loading}>
|
|
122
|
+
* {loading ? 'Creating...' : 'Create'}
|
|
123
|
+
* </button>
|
|
124
|
+
* {error && <p className="error">{error.message}</p>}
|
|
125
|
+
* </form>
|
|
126
|
+
* );
|
|
127
|
+
* }
|
|
128
|
+
*
|
|
129
|
+
* // With optimistic updates
|
|
130
|
+
* function UpdatePost({ postId }: { postId: string }) {
|
|
131
|
+
* const client = useLensClient();
|
|
132
|
+
* const { mutate } = useMutation(client.post.update);
|
|
133
|
+
*
|
|
134
|
+
* const handleUpdate = async (title: string) => {
|
|
135
|
+
* const result = await mutate({ id: postId, title });
|
|
136
|
+
* // result.rollback?.() can undo optimistic update
|
|
137
|
+
* };
|
|
138
|
+
* }
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
export declare function useMutation<TInput, TOutput>(mutationFn: MutationFn<TInput, TOutput>): UseMutationResult<TInput, TOutput>;
|
|
142
|
+
/** Result of useLazyQuery hook */
|
|
143
|
+
export interface UseLazyQueryResult<T> {
|
|
144
|
+
/** Execute the query */
|
|
145
|
+
execute: () => Promise<T>;
|
|
146
|
+
/** Query data (null if not executed or error) */
|
|
147
|
+
data: T | null;
|
|
148
|
+
/** Loading state */
|
|
149
|
+
loading: boolean;
|
|
150
|
+
/** Error state */
|
|
151
|
+
error: Error | null;
|
|
152
|
+
/** Reset query state */
|
|
153
|
+
reset: () => void;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Execute a query on demand (not on mount)
|
|
157
|
+
*
|
|
158
|
+
* @param query - QueryResult from client API call
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```tsx
|
|
162
|
+
* function SearchUsers() {
|
|
163
|
+
* const client = useLensClient();
|
|
164
|
+
* const [searchTerm, setSearchTerm] = useState('');
|
|
165
|
+
* const { execute, data, loading } = useLazyQuery(
|
|
166
|
+
* client.user.search({ query: searchTerm })
|
|
167
|
+
* );
|
|
168
|
+
*
|
|
169
|
+
* const handleSearch = async () => {
|
|
170
|
+
* const users = await execute();
|
|
171
|
+
* console.log('Found:', users);
|
|
172
|
+
* };
|
|
173
|
+
*
|
|
174
|
+
* return (
|
|
175
|
+
* <div>
|
|
176
|
+
* <input
|
|
177
|
+
* value={searchTerm}
|
|
178
|
+
* onChange={e => setSearchTerm(e.target.value)}
|
|
179
|
+
* />
|
|
180
|
+
* <button onClick={handleSearch} disabled={loading}>
|
|
181
|
+
* Search
|
|
182
|
+
* </button>
|
|
183
|
+
* {data?.map(user => <UserCard key={user.id} user={user} />)}
|
|
184
|
+
* </div>
|
|
185
|
+
* );
|
|
186
|
+
* }
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
export declare function useLazyQuery<T>(query: QueryResult<T>): UseLazyQueryResult<T>;
|
|
190
|
+
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,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BAAgB,WAAW,CAAC,MAAM,EAAE,OAAO,EAC1C,UAAU,EAAE,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC,GACrC,iBAAiB,CAAC,MAAM,EAAE,OAAO,CAAC,CAoDpC;AAMD,kCAAkC;AAClC,MAAM,WAAW,kBAAkB,CAAC,CAAC;IACpC,wBAAwB;IACxB,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC;IAC1B,iDAAiD;IACjD,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;IACf,oBAAoB;IACpB,OAAO,EAAE,OAAO,CAAC;IACjB,kBAAkB;IAClB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,wBAAwB;IACxB,KAAK,EAAE,MAAM,IAAI,CAAC;CAClB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,kBAAkB,CAAC,CAAC,CAAC,CAiD5E"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @sylphx/lens-react
|
|
3
|
+
*
|
|
4
|
+
* React bindings for Lens API framework.
|
|
5
|
+
* Hooks and context provider for reactive data access.
|
|
6
|
+
*/
|
|
7
|
+
export { LensProvider, useLensClient, type LensProviderProps } from "./context";
|
|
8
|
+
export { useQuery, useLazyQuery, useMutation, type UseQueryResult, type UseLazyQueryResult, type UseMutationResult, type UseQueryOptions, type MutationFn, } from "./hooks";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,KAAK,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAMhF,OAAO,EAEN,QAAQ,EACR,YAAY,EAEZ,WAAW,EAEX,KAAK,cAAc,EACnB,KAAK,kBAAkB,EACvB,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,KAAK,UAAU,GACf,MAAM,SAAS,CAAC"}
|