@objectstack/client-react 1.0.4 → 1.0.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.
@@ -1,243 +0,0 @@
1
- "use strict";
2
- /**
3
- * Metadata Hooks
4
- *
5
- * React hooks for accessing ObjectStack metadata (schemas, views, fields)
6
- */
7
- Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.useObject = useObject;
9
- exports.useView = useView;
10
- exports.useFields = useFields;
11
- exports.useMetadata = useMetadata;
12
- const react_1 = require("react");
13
- const context_1 = require("./context");
14
- /**
15
- * Hook for fetching object schema/metadata
16
- *
17
- * @example
18
- * ```tsx
19
- * function ObjectSchemaViewer({ objectName }: { objectName: string }) {
20
- * const { data: schema, isLoading, error } = useObject(objectName);
21
- *
22
- * if (isLoading) return <div>Loading schema...</div>;
23
- * if (error) return <div>Error: {error.message}</div>;
24
- *
25
- * return (
26
- * <div>
27
- * <h2>{schema.label}</h2>
28
- * <p>Fields: {Object.keys(schema.fields).length}</p>
29
- * </div>
30
- * );
31
- * }
32
- * ```
33
- */
34
- function useObject(objectName, options = {}) {
35
- const client = (0, context_1.useClient)();
36
- const [data, setData] = (0, react_1.useState)(null);
37
- const [isLoading, setIsLoading] = (0, react_1.useState)(true);
38
- const [error, setError] = (0, react_1.useState)(null);
39
- const [etag, setEtag] = (0, react_1.useState)();
40
- const [fromCache, setFromCache] = (0, react_1.useState)(false);
41
- const { enabled = true, useCache = true, ifNoneMatch, ifModifiedSince, onSuccess, onError } = options;
42
- const fetchMetadata = (0, react_1.useCallback)(async () => {
43
- if (!enabled)
44
- return;
45
- try {
46
- setIsLoading(true);
47
- setError(null);
48
- setFromCache(false);
49
- if (useCache) {
50
- // Use cached metadata endpoint
51
- const result = await client.meta.getCached(objectName, {
52
- ifNoneMatch: ifNoneMatch || etag,
53
- ifModifiedSince
54
- });
55
- if (result.notModified) {
56
- setFromCache(true);
57
- }
58
- else {
59
- setData(result.data);
60
- if (result.etag) {
61
- setEtag(result.etag.value);
62
- }
63
- }
64
- onSuccess?.(result.data || data);
65
- }
66
- else {
67
- // Direct fetch without cache
68
- const result = await client.meta.getObject(objectName);
69
- setData(result);
70
- onSuccess?.(result);
71
- }
72
- }
73
- catch (err) {
74
- const error = err instanceof Error ? err : new Error('Failed to fetch object metadata');
75
- setError(error);
76
- onError?.(error);
77
- }
78
- finally {
79
- setIsLoading(false);
80
- }
81
- }, [client, objectName, enabled, useCache, ifNoneMatch, ifModifiedSince, etag, data, onSuccess, onError]);
82
- (0, react_1.useEffect)(() => {
83
- fetchMetadata();
84
- }, [fetchMetadata]);
85
- const refetch = (0, react_1.useCallback)(async () => {
86
- await fetchMetadata();
87
- }, [fetchMetadata]);
88
- return {
89
- data,
90
- isLoading,
91
- error,
92
- refetch,
93
- etag,
94
- fromCache
95
- };
96
- }
97
- /**
98
- * Hook for fetching view configuration
99
- *
100
- * @example
101
- * ```tsx
102
- * function ViewConfiguration({ objectName }: { objectName: string }) {
103
- * const { data: view, isLoading } = useView(objectName, 'list');
104
- *
105
- * if (isLoading) return <div>Loading view...</div>;
106
- *
107
- * return (
108
- * <div>
109
- * <h3>List View for {objectName}</h3>
110
- * <p>Columns: {view?.columns?.length}</p>
111
- * </div>
112
- * );
113
- * }
114
- * ```
115
- */
116
- function useView(objectName, viewType = 'list', options = {}) {
117
- const client = (0, context_1.useClient)();
118
- const [data, setData] = (0, react_1.useState)(null);
119
- const [isLoading, setIsLoading] = (0, react_1.useState)(true);
120
- const [error, setError] = (0, react_1.useState)(null);
121
- const { enabled = true, onSuccess, onError } = options;
122
- const fetchView = (0, react_1.useCallback)(async () => {
123
- if (!enabled)
124
- return;
125
- try {
126
- setIsLoading(true);
127
- setError(null);
128
- const result = await client.meta.getView(objectName, viewType);
129
- setData(result);
130
- onSuccess?.(result);
131
- }
132
- catch (err) {
133
- const error = err instanceof Error ? err : new Error('Failed to fetch view configuration');
134
- setError(error);
135
- onError?.(error);
136
- }
137
- finally {
138
- setIsLoading(false);
139
- }
140
- }, [client, objectName, viewType, enabled, onSuccess, onError]);
141
- (0, react_1.useEffect)(() => {
142
- fetchView();
143
- }, [fetchView]);
144
- const refetch = (0, react_1.useCallback)(async () => {
145
- await fetchView();
146
- }, [fetchView]);
147
- return {
148
- data,
149
- isLoading,
150
- error,
151
- refetch,
152
- fromCache: false
153
- };
154
- }
155
- /**
156
- * Hook for extracting fields from object schema
157
- *
158
- * @example
159
- * ```tsx
160
- * function FieldList({ objectName }: { objectName: string }) {
161
- * const { data: fields, isLoading } = useFields(objectName);
162
- *
163
- * if (isLoading) return <div>Loading fields...</div>;
164
- *
165
- * return (
166
- * <ul>
167
- * {fields?.map(field => (
168
- * <li key={field.name}>{field.label} ({field.type})</li>
169
- * ))}
170
- * </ul>
171
- * );
172
- * }
173
- * ```
174
- */
175
- function useFields(objectName, options = {}) {
176
- const objectResult = useObject(objectName, options);
177
- const fields = objectResult.data?.fields
178
- ? Object.entries(objectResult.data.fields).map(([name, field]) => ({
179
- name,
180
- ...field
181
- }))
182
- : null;
183
- return {
184
- ...objectResult,
185
- data: fields
186
- };
187
- }
188
- /**
189
- * Generic metadata hook for custom metadata queries
190
- *
191
- * @example
192
- * ```tsx
193
- * function CustomMetadata() {
194
- * const { data, isLoading } = useMetadata(async (client) => {
195
- * // Custom metadata fetching logic
196
- * const object = await client.meta.getObject('custom_object');
197
- * const view = await client.meta.getView('custom_object', 'list');
198
- * return { object, view };
199
- * });
200
- *
201
- * return <pre>{JSON.stringify(data, null, 2)}</pre>;
202
- * }
203
- * ```
204
- */
205
- function useMetadata(fetcher, options = {}) {
206
- const client = (0, context_1.useClient)();
207
- const [data, setData] = (0, react_1.useState)(null);
208
- const [isLoading, setIsLoading] = (0, react_1.useState)(true);
209
- const [error, setError] = (0, react_1.useState)(null);
210
- const { enabled = true, onSuccess, onError } = options;
211
- const fetchMetadata = (0, react_1.useCallback)(async () => {
212
- if (!enabled)
213
- return;
214
- try {
215
- setIsLoading(true);
216
- setError(null);
217
- const result = await fetcher(client);
218
- setData(result);
219
- onSuccess?.(result);
220
- }
221
- catch (err) {
222
- const error = err instanceof Error ? err : new Error('Failed to fetch metadata');
223
- setError(error);
224
- onError?.(error);
225
- }
226
- finally {
227
- setIsLoading(false);
228
- }
229
- }, [client, fetcher, enabled, onSuccess, onError]);
230
- (0, react_1.useEffect)(() => {
231
- fetchMetadata();
232
- }, [fetchMetadata]);
233
- const refetch = (0, react_1.useCallback)(async () => {
234
- await fetchMetadata();
235
- }, [fetchMetadata]);
236
- return {
237
- data,
238
- isLoading,
239
- error,
240
- refetch,
241
- fromCache: false
242
- };
243
- }