schemock 0.0.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.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +82 -0
  3. package/dist/adapters/index.d.mts +1364 -0
  4. package/dist/adapters/index.d.ts +1364 -0
  5. package/dist/adapters/index.js +36988 -0
  6. package/dist/adapters/index.js.map +1 -0
  7. package/dist/adapters/index.mjs +36972 -0
  8. package/dist/adapters/index.mjs.map +1 -0
  9. package/dist/cli/index.d.mts +831 -0
  10. package/dist/cli/index.d.ts +831 -0
  11. package/dist/cli/index.js +4425 -0
  12. package/dist/cli/index.js.map +1 -0
  13. package/dist/cli/index.mjs +4401 -0
  14. package/dist/cli/index.mjs.map +1 -0
  15. package/dist/cli.js +6776 -0
  16. package/dist/index.d.mts +8 -0
  17. package/dist/index.d.ts +8 -0
  18. package/dist/index.js +39439 -0
  19. package/dist/index.js.map +1 -0
  20. package/dist/index.mjs +39367 -0
  21. package/dist/index.mjs.map +1 -0
  22. package/dist/middleware/index.d.mts +688 -0
  23. package/dist/middleware/index.d.ts +688 -0
  24. package/dist/middleware/index.js +921 -0
  25. package/dist/middleware/index.js.map +1 -0
  26. package/dist/middleware/index.mjs +899 -0
  27. package/dist/middleware/index.mjs.map +1 -0
  28. package/dist/react/index.d.mts +316 -0
  29. package/dist/react/index.d.ts +316 -0
  30. package/dist/react/index.js +466 -0
  31. package/dist/react/index.js.map +1 -0
  32. package/dist/react/index.mjs +456 -0
  33. package/dist/react/index.mjs.map +1 -0
  34. package/dist/runtime/index.d.mts +814 -0
  35. package/dist/runtime/index.d.ts +814 -0
  36. package/dist/runtime/index.js +1270 -0
  37. package/dist/runtime/index.js.map +1 -0
  38. package/dist/runtime/index.mjs +1246 -0
  39. package/dist/runtime/index.mjs.map +1 -0
  40. package/dist/schema/index.d.mts +838 -0
  41. package/dist/schema/index.d.ts +838 -0
  42. package/dist/schema/index.js +696 -0
  43. package/dist/schema/index.js.map +1 -0
  44. package/dist/schema/index.mjs +681 -0
  45. package/dist/schema/index.mjs.map +1 -0
  46. package/dist/types-C1MiZh1d.d.ts +96 -0
  47. package/dist/types-C2bd2vgy.d.mts +773 -0
  48. package/dist/types-C2bd2vgy.d.ts +773 -0
  49. package/dist/types-C9VMgu3E.d.mts +289 -0
  50. package/dist/types-DV2DS7wj.d.mts +96 -0
  51. package/dist/types-c2AN3vky.d.ts +289 -0
  52. package/package.json +116 -0
@@ -0,0 +1,316 @@
1
+ import * as react from 'react';
2
+ import react__default, { ReactNode } from 'react';
3
+ import { QueryClient, UseQueryResult, UseMutationResult } from '@tanstack/react-query';
4
+ import { A as Adapter } from '../types-c2AN3vky.js';
5
+ import { M as Middleware } from '../types-C1MiZh1d.js';
6
+ import { o as EntitySchema, s as ViewSchema } from '../types-C2bd2vgy.js';
7
+
8
+ /**
9
+ * Value provided by the DataLayer context.
10
+ */
11
+ interface DataLayerContextValue {
12
+ /** The adapter to use for data operations */
13
+ adapter: Adapter;
14
+ /** React Query client instance */
15
+ queryClient: QueryClient;
16
+ /** Registered entity schemas */
17
+ schemas: Map<string, EntitySchema>;
18
+ /** Middleware chain */
19
+ middleware: Middleware[];
20
+ /** Get schema by entity name */
21
+ getSchema: (name: string) => EntitySchema | undefined;
22
+ }
23
+ /**
24
+ * React context for the data layer.
25
+ *
26
+ * Holds the adapter, query client, and schema registry.
27
+ */
28
+ declare const DataLayerContext: react.Context<DataLayerContextValue | null>;
29
+ /**
30
+ * Hook to access the DataLayer context.
31
+ *
32
+ * @returns The DataLayer context value
33
+ * @throws Error if used outside of DataLayerProvider
34
+ *
35
+ * @example
36
+ * ```typescript
37
+ * function MyComponent() {
38
+ * const { adapter, queryClient } = useDataLayerContext();
39
+ *
40
+ * const handleRefresh = () => {
41
+ * queryClient.invalidateQueries();
42
+ * };
43
+ *
44
+ * return <button onClick={handleRefresh}>Refresh</button>;
45
+ * }
46
+ * ```
47
+ */
48
+ declare function useDataLayerContext(): DataLayerContextValue;
49
+ /**
50
+ * Hook to get the current adapter.
51
+ *
52
+ * @returns The adapter instance
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * function MyComponent() {
57
+ * const adapter = useAdapter();
58
+ * // Use adapter directly for custom operations
59
+ * }
60
+ * ```
61
+ */
62
+ declare function useAdapter(): Adapter;
63
+ /**
64
+ * Hook to get the query client.
65
+ *
66
+ * @returns The QueryClient instance
67
+ */
68
+ declare function useQueryClientFromContext(): QueryClient;
69
+
70
+ /**
71
+ * DataLayerProvider - React context provider for data layer
72
+ *
73
+ * Wraps your application to provide adapter and query client
74
+ * to all data hooks.
75
+ *
76
+ * @module react/provider
77
+ * @category React
78
+ */
79
+
80
+ /**
81
+ * Props for the DataLayerProvider component.
82
+ */
83
+ interface DataLayerProviderProps {
84
+ /** The adapter to use for data operations */
85
+ adapter: Adapter;
86
+ /** Entity schemas to register */
87
+ schemas?: EntitySchema[];
88
+ /** Middleware chain to apply */
89
+ middleware?: Middleware[];
90
+ /** Custom QueryClient (optional, will create one if not provided) */
91
+ queryClient?: QueryClient;
92
+ /** Default query options */
93
+ defaultOptions?: {
94
+ /** Default stale time in ms */
95
+ staleTime?: number;
96
+ /** Default cache time in ms */
97
+ cacheTime?: number;
98
+ /** Default retry count */
99
+ retry?: number;
100
+ };
101
+ /** Child components */
102
+ children: ReactNode;
103
+ }
104
+ /**
105
+ * DataLayerProvider component.
106
+ *
107
+ * Provides the data layer context to all child components,
108
+ * enabling the use of useData, useMutate, and useView hooks.
109
+ *
110
+ * @example
111
+ * ```typescript
112
+ * import { DataLayerProvider } from 'schemock/react';
113
+ * import { createMockAdapter } from 'schemock/adapters';
114
+ *
115
+ * const adapter = createMockAdapter([userSchema, postSchema]);
116
+ *
117
+ * function App() {
118
+ * return (
119
+ * <DataLayerProvider adapter={adapter} schemas={[userSchema, postSchema]}>
120
+ * <MyApp />
121
+ * </DataLayerProvider>
122
+ * );
123
+ * }
124
+ * ```
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * // With custom query client and middleware
129
+ * const queryClient = new QueryClient({
130
+ * defaultOptions: { queries: { staleTime: 5000 } },
131
+ * });
132
+ *
133
+ * function App() {
134
+ * return (
135
+ * <DataLayerProvider
136
+ * adapter={adapter}
137
+ * schemas={schemas}
138
+ * queryClient={queryClient}
139
+ * middleware={[authMiddleware, loggerMiddleware]}
140
+ * >
141
+ * <MyApp />
142
+ * </DataLayerProvider>
143
+ * );
144
+ * }
145
+ * ```
146
+ */
147
+ declare const DataLayerProvider: react__default.FC<DataLayerProviderProps>;
148
+
149
+ /**
150
+ * React Hooks - Data fetching and mutation hooks
151
+ *
152
+ * Provides useData, useMutate, and useView hooks for
153
+ * React Query integration with Schemock.
154
+ *
155
+ * @module react/hooks
156
+ * @category React
157
+ */
158
+
159
+ /**
160
+ * Options for useData hook.
161
+ */
162
+ interface UseDataOptions<T> {
163
+ /** Fetch single entity by ID */
164
+ id?: string;
165
+ /** Relations to include */
166
+ include?: string[];
167
+ /** Filter conditions */
168
+ where?: Record<string, unknown>;
169
+ /** Number of items to fetch */
170
+ limit?: number;
171
+ /** Pagination offset */
172
+ offset?: number;
173
+ /** Ordering */
174
+ orderBy?: Record<string, 'asc' | 'desc'>;
175
+ /** Fields to select */
176
+ select?: string[];
177
+ /** Whether the query is enabled */
178
+ enabled?: boolean;
179
+ /** Stale time in ms */
180
+ staleTime?: number;
181
+ /** Placeholder data */
182
+ placeholderData?: T | T[];
183
+ }
184
+ /**
185
+ * useData hook for fetching entities.
186
+ *
187
+ * Fetches single or multiple entities based on options.
188
+ *
189
+ * @param entity - The entity schema to fetch
190
+ * @param options - Query options
191
+ * @returns React Query result
192
+ *
193
+ * @example
194
+ * ```typescript
195
+ * // Fetch single entity
196
+ * const { data: user } = useData(userSchema, { id: '123' });
197
+ *
198
+ * // Fetch multiple entities
199
+ * const { data: users } = useData(userSchema, {
200
+ * where: { role: 'admin' },
201
+ * limit: 10,
202
+ * orderBy: { createdAt: 'desc' },
203
+ * });
204
+ *
205
+ * // Fetch with relations
206
+ * const { data: user } = useData(userSchema, {
207
+ * id: '123',
208
+ * include: ['posts', 'comments'],
209
+ * });
210
+ * ```
211
+ */
212
+ declare function useData<T>(entity: EntitySchema<T>, options?: UseDataOptions<T>): UseQueryResult<T | T[]>;
213
+ /**
214
+ * Result from useMutate hook.
215
+ */
216
+ interface UseMutateResult<T> {
217
+ /** Create mutation */
218
+ create: UseMutationResult<T, Error, Partial<T>>;
219
+ /** Update mutation */
220
+ update: UseMutationResult<T, Error, {
221
+ id: string;
222
+ data: Partial<T>;
223
+ }>;
224
+ /** Delete mutation */
225
+ remove: UseMutationResult<void, Error, string>;
226
+ }
227
+ /**
228
+ * Options for useMutate hook.
229
+ */
230
+ interface UseMutateOptions {
231
+ /** Invalidate queries on success */
232
+ invalidateOnSuccess?: boolean;
233
+ /** Specific query keys to invalidate */
234
+ invalidateQueries?: unknown[][];
235
+ /** Optimistic update function */
236
+ onOptimisticUpdate?: () => void;
237
+ }
238
+ /**
239
+ * useMutate hook for CRUD mutations.
240
+ *
241
+ * Provides create, update, and delete mutations for an entity.
242
+ *
243
+ * @param entity - The entity schema
244
+ * @param options - Mutation options
245
+ * @returns Object with create, update, and remove mutations
246
+ *
247
+ * @example
248
+ * ```typescript
249
+ * const { create, update, remove } = useMutate(userSchema);
250
+ *
251
+ * // Create
252
+ * const handleCreate = async () => {
253
+ * await create.mutateAsync({ name: 'John', email: 'john@example.com' });
254
+ * };
255
+ *
256
+ * // Update
257
+ * const handleUpdate = async (id: string) => {
258
+ * await update.mutateAsync({ id, data: { name: 'Jane' } });
259
+ * };
260
+ *
261
+ * // Delete
262
+ * const handleDelete = async (id: string) => {
263
+ * await remove.mutateAsync(id);
264
+ * };
265
+ * ```
266
+ */
267
+ declare function useMutate<T>(entity: EntitySchema<T>, options?: UseMutateOptions): UseMutateResult<T>;
268
+ /**
269
+ * Options for useView hook.
270
+ */
271
+ interface UseViewOptions<T> {
272
+ /** URL parameters for the view */
273
+ params?: Record<string, string>;
274
+ /** Whether the query is enabled */
275
+ enabled?: boolean;
276
+ /** Stale time in ms */
277
+ staleTime?: number;
278
+ /** Placeholder data */
279
+ placeholderData?: T;
280
+ }
281
+ /**
282
+ * useView hook for fetching computed views.
283
+ *
284
+ * Fetches data from a view schema endpoint.
285
+ *
286
+ * @param view - The view schema
287
+ * @param options - View options
288
+ * @returns React Query result
289
+ *
290
+ * @example
291
+ * ```typescript
292
+ * const userFullView = defineView('user-full', ...);
293
+ *
294
+ * const { data } = useView(userFullView, { params: { id: '123' } });
295
+ * ```
296
+ */
297
+ declare function useView<T>(view: ViewSchema, options?: UseViewOptions<T>): UseQueryResult<T>;
298
+ /**
299
+ * Hook to prefetch data.
300
+ *
301
+ * @param entity - The entity schema
302
+ * @param options - Data options
303
+ *
304
+ * @example
305
+ * ```typescript
306
+ * const prefetchUser = usePrefetch(userSchema);
307
+ *
308
+ * // Prefetch on hover
309
+ * <div onMouseEnter={() => prefetchUser({ id: '123' })}>
310
+ * User Profile
311
+ * </div>
312
+ * ```
313
+ */
314
+ declare function usePrefetch<T>(entity: EntitySchema<T>): (options?: UseDataOptions<T>) => Promise<void>;
315
+
316
+ export { DataLayerContext, type DataLayerContextValue, DataLayerProvider, type DataLayerProviderProps, type UseDataOptions, type UseMutateOptions, type UseMutateResult, type UseViewOptions, useAdapter, useData, useDataLayerContext, useMutate, usePrefetch, useQueryClientFromContext, useView };