@codeleap/query 6.3.0 → 6.8.0
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/factors/createQueryManager.d.ts +36 -0
- package/dist/factors/createQueryManager.d.ts.map +1 -0
- package/dist/factors/createQueryOperations.d.ts +35 -0
- package/dist/factors/createQueryOperations.d.ts.map +1 -0
- package/dist/factors/index.d.ts +3 -0
- package/dist/factors/index.d.ts.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/lib/Mutations.d.ts +103 -0
- package/dist/lib/Mutations.d.ts.map +1 -0
- package/dist/lib/QueryClientEnhanced/index.d.ts +41 -0
- package/dist/lib/QueryClientEnhanced/index.d.ts.map +1 -0
- package/dist/lib/QueryClientEnhanced/types.d.ts +51 -0
- package/dist/lib/QueryClientEnhanced/types.d.ts.map +1 -0
- package/dist/lib/QueryKeys.d.ts +173 -0
- package/dist/lib/QueryKeys.d.ts.map +1 -0
- package/dist/lib/QueryManager.d.ts +202 -0
- package/dist/lib/QueryManager.d.ts.map +1 -0
- package/dist/lib/QueryOperations/index.d.ts +228 -0
- package/dist/lib/QueryOperations/index.d.ts.map +1 -0
- package/dist/lib/QueryOperations/types.d.ts +42 -0
- package/dist/lib/QueryOperations/types.d.ts.map +1 -0
- package/dist/lib/index.d.ts +6 -0
- package/dist/lib/index.d.ts.map +1 -0
- package/dist/types/core.d.ts +29 -0
- package/dist/types/core.d.ts.map +1 -0
- package/dist/types/create.d.ts +21 -0
- package/dist/types/create.d.ts.map +1 -0
- package/dist/types/delete.d.ts +18 -0
- package/dist/types/delete.d.ts.map +1 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/list.d.ts +19 -0
- package/dist/types/list.d.ts.map +1 -0
- package/dist/types/retrieve.d.ts +17 -0
- package/dist/types/retrieve.d.ts.map +1 -0
- package/dist/types/update.d.ts +13 -0
- package/dist/types/update.d.ts.map +1 -0
- package/dist/types/utility.d.ts +23 -0
- package/dist/types/utility.d.ts.map +1 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/misc.d.ts +5 -0
- package/dist/utils/misc.d.ts.map +1 -0
- package/package.json +23 -8
- package/src/lib/Mutations.ts +30 -44
- package/src/lib/QueryClientEnhanced/index.ts +32 -4
- package/src/lib/QueryClientEnhanced/types.ts +25 -0
- package/src/lib/QueryKeys.ts +4 -2
- package/src/lib/QueryManager.ts +37 -33
- package/src/lib/QueryOperations/index.ts +11 -3
- package/src/lib/QueryOperations/types.ts +4 -3
- package/src/tests/setup.ts +14 -0
- package/src/types/core.ts +8 -1
- package/src/types/create.ts +10 -1
- package/src/types/delete.ts +8 -1
- package/src/types/list.ts +5 -0
- package/src/types/retrieve.ts +9 -0
- package/src/types/update.ts +3 -0
- package/src/types/utility.ts +5 -0
- package/src/utils/misc.ts +2 -0
- package/package.json.bak +0 -27
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { FetchInfiniteQueryOptions, FetchQueryOptions, QueryClient, QueryKey } from '@tanstack/react-query';
|
|
2
|
+
import { QueryKeys } from './QueryKeys';
|
|
3
|
+
import { Mutations } from './Mutations';
|
|
4
|
+
import { CreateMutationCtx, CreateMutationOptions, ListPaginationResponse, ListQueryOptions, QueryItem, QueryManagerOptions, RetrieveQueryOptions, UpdateMutationCtx, UpdateMutationOptions, DeleteMutationCtx, DeleteMutationOptions } from '../types';
|
|
5
|
+
/**
|
|
6
|
+
* Comprehensive query manager class that provides hooks and utilities for managing CRUD operations with React Query
|
|
7
|
+
* @template T - The query item type that extends QueryItem
|
|
8
|
+
* @template F - The filter type used for list queries
|
|
9
|
+
*
|
|
10
|
+
* @description
|
|
11
|
+
* QueryManager provides a complete solution for managing list and individual item queries with:
|
|
12
|
+
* - Infinite scroll pagination for lists
|
|
13
|
+
* - Optimistic updates for create, update, and delete operations
|
|
14
|
+
* - Automatic cache synchronization between list and individual queries
|
|
15
|
+
* - Built-in error handling and rollback mechanisms
|
|
16
|
+
*/
|
|
17
|
+
export declare class QueryManager<T extends QueryItem, F> {
|
|
18
|
+
private options;
|
|
19
|
+
queryClient: QueryClient;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new QueryManager instance
|
|
22
|
+
* @param options - Configuration options for the query manager
|
|
23
|
+
*/
|
|
24
|
+
constructor(options: QueryManagerOptions<T, F>);
|
|
25
|
+
/**
|
|
26
|
+
* Gets the name of this query manager
|
|
27
|
+
* @returns The query name used for identification
|
|
28
|
+
*/
|
|
29
|
+
get name(): string;
|
|
30
|
+
/**
|
|
31
|
+
* Gets the configured CRUD functions
|
|
32
|
+
* @returns Object containing all configured function handlers
|
|
33
|
+
*/
|
|
34
|
+
get functions(): {
|
|
35
|
+
list: ((limit: number, offset: number, filters: F) => Promise<ListPaginationResponse<T>>) | undefined;
|
|
36
|
+
retrieve: ((id: T["id"]) => Promise<T>) | undefined;
|
|
37
|
+
create: ((data: Partial<T>) => Promise<T>) | undefined;
|
|
38
|
+
update: ((data: Partial<T>) => Promise<T>) | undefined;
|
|
39
|
+
delete: ((id: T["id"]) => Promise<T["id"]>) | undefined;
|
|
40
|
+
};
|
|
41
|
+
/** QueryKeys instance for managing query keys and cache operations */
|
|
42
|
+
queryKeys: QueryKeys<T, F>;
|
|
43
|
+
/** Mutations instance for managing cache mutations */
|
|
44
|
+
mutations: Mutations<T, F>;
|
|
45
|
+
/**
|
|
46
|
+
* React hook for infinite scroll list queries with pagination
|
|
47
|
+
* @param options - Configuration options for the list query
|
|
48
|
+
* @returns Object containing items array, query key, and query object
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* const { items, query } = queryManager.useList({
|
|
53
|
+
* filters: { status: 'active' },
|
|
54
|
+
* limit: 20
|
|
55
|
+
* })
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
useList(options?: ListQueryOptions<T, F>): {
|
|
59
|
+
items: T[];
|
|
60
|
+
queryKey: readonly unknown[];
|
|
61
|
+
query: import("@tanstack/react-query").UseInfiniteQueryResult<import("..").ListSelector<T>, Error>;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* React hook for retrieving a single item by ID
|
|
65
|
+
* @param id - The ID of the item to retrieve
|
|
66
|
+
* @param options - Configuration options for the retrieve query
|
|
67
|
+
* @returns Object containing the item data, query key, and query object
|
|
68
|
+
*
|
|
69
|
+
* @description
|
|
70
|
+
* This hook automatically:
|
|
71
|
+
* - Uses list cache as initial data if available
|
|
72
|
+
* - Updates list cache when retrieve data changes
|
|
73
|
+
* - Synchronizes data between list and individual caches
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* const { item, query } = queryManager.useRetrieve('user-123', {
|
|
78
|
+
* enabled: !!userId
|
|
79
|
+
* })
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
useRetrieve(id: T['id'], options?: RetrieveQueryOptions<T>): {
|
|
83
|
+
item: import("@tanstack/query-core").NoInfer<T> | undefined;
|
|
84
|
+
queryKey: readonly unknown[];
|
|
85
|
+
query: import("@tanstack/react-query").UseQueryResult<import("@tanstack/query-core").NoInfer<T>, Error>;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* React hook for creating new items with optimistic updates
|
|
89
|
+
* @param options - Configuration options for the create mutation
|
|
90
|
+
* @returns React Query mutation object for create operations
|
|
91
|
+
*
|
|
92
|
+
* @description
|
|
93
|
+
* This hook supports optimistic updates by:
|
|
94
|
+
* - Immediately adding a temporary item to the cache
|
|
95
|
+
* - Rolling back on error by removing the temporary item
|
|
96
|
+
* - Replacing temporary item with real data on success
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* const createMutation = queryManager.useCreate({
|
|
101
|
+
* optimistic: true,
|
|
102
|
+
* appendTo: 'start',
|
|
103
|
+
* listFilters: { status: 'active' }
|
|
104
|
+
* })
|
|
105
|
+
*
|
|
106
|
+
* // Usage
|
|
107
|
+
* createMutation.mutate({ name: 'New User', email: 'user@example.com' })
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
useCreate(options?: CreateMutationOptions<T, F>): import("@tanstack/react-query").UseMutationResult<T, Error, Partial<T>, CreateMutationCtx | undefined>;
|
|
111
|
+
/**
|
|
112
|
+
* React hook for updating existing items with optimistic updates
|
|
113
|
+
* @param options - Configuration options for the update mutation
|
|
114
|
+
* @returns React Query mutation object for update operations
|
|
115
|
+
*
|
|
116
|
+
* @description
|
|
117
|
+
* This hook supports optimistic updates by:
|
|
118
|
+
* - Immediately updating the item in cache with new data
|
|
119
|
+
* - Rolling back to previous data on error
|
|
120
|
+
* - Confirming updates with server response on success
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* const updateMutation = queryManager.useUpdate({
|
|
125
|
+
* optimistic: true,
|
|
126
|
+
* onSuccess: (data) => console.log('Updated:', data)
|
|
127
|
+
* })
|
|
128
|
+
*
|
|
129
|
+
* // Usage
|
|
130
|
+
* updateMutation.mutate({ id: 'user-123', name: 'Updated Name' })
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
useUpdate(options?: UpdateMutationOptions<T, F>): import("@tanstack/react-query").UseMutationResult<T, Error, Partial<T>, UpdateMutationCtx<T> | undefined>;
|
|
134
|
+
/**
|
|
135
|
+
* React hook for deleting items with optimistic updates
|
|
136
|
+
* @param options - Configuration options for the delete mutation
|
|
137
|
+
* @returns React Query mutation object for delete operations
|
|
138
|
+
*
|
|
139
|
+
* @description
|
|
140
|
+
* This hook supports optimistic updates by:
|
|
141
|
+
* - Immediately removing the item from cache
|
|
142
|
+
* - Storing the removal positions for potential rollback
|
|
143
|
+
* - Restoring the item to original positions on error
|
|
144
|
+
* - Confirming deletion on success
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* const deleteMutation = queryManager.useDelete({
|
|
149
|
+
* optimistic: true,
|
|
150
|
+
* onSuccess: () => console.log('Item deleted successfully')
|
|
151
|
+
* })
|
|
152
|
+
*
|
|
153
|
+
* // Usage
|
|
154
|
+
* deleteMutation.mutate('user-123')
|
|
155
|
+
* ```
|
|
156
|
+
*/
|
|
157
|
+
useDelete(options?: DeleteMutationOptions<T, F>): import("@tanstack/react-query").UseMutationResult<unknown, Error, T["id"], DeleteMutationCtx<T> | undefined>;
|
|
158
|
+
/**
|
|
159
|
+
* Prefetches a single item by ID for improved performance
|
|
160
|
+
* @param id - The ID of the item to prefetch
|
|
161
|
+
* @param options - Prefetch options compatible with React Query
|
|
162
|
+
* @returns Promise that resolves when prefetch is complete
|
|
163
|
+
*
|
|
164
|
+
* @description
|
|
165
|
+
* Use this method to preload data that users are likely to need soon,
|
|
166
|
+
* such as when hovering over links or preparing for navigation.
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```typescript
|
|
170
|
+
* // Prefetch on hover
|
|
171
|
+
* const handleHover = (userId: string) => {
|
|
172
|
+
* queryManager.prefetchRetrieve(userId, { staleTime: 5 * 60 * 1000 })
|
|
173
|
+
* }
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
prefetchRetrieve(id: T['id'], options?: Omit<FetchQueryOptions<T, Error, T, QueryKey, never>, 'queryKey' | 'queryFn'>): Promise<void>;
|
|
177
|
+
/**
|
|
178
|
+
* Prefetches a paginated list with filters for improved performance
|
|
179
|
+
* @param filters - Filter parameters to apply to the list query
|
|
180
|
+
* @param options - Prefetch options compatible with React Query's infinite queries
|
|
181
|
+
* @param options.initialOffset - Starting offset for pagination (default: 0)
|
|
182
|
+
* @returns Promise that resolves when prefetch is complete
|
|
183
|
+
*
|
|
184
|
+
* @description
|
|
185
|
+
* Use this method to preload paginated list data that users are likely to need soon.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```typescript
|
|
189
|
+
* const handle = () => {
|
|
190
|
+
* queryManager.prefetchList(
|
|
191
|
+
* { category: 'electronics' },
|
|
192
|
+
* { staleTime: 5 * 60 * 1000 }
|
|
193
|
+
* )
|
|
194
|
+
* }
|
|
195
|
+
*
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
prefetchList(filters?: F, options?: Omit<FetchInfiniteQueryOptions<ListPaginationResponse<T>, Error, ListPaginationResponse<T>, QueryKey, number>, 'queryKey' | 'queryFn' | 'initialPageParam'> & {
|
|
199
|
+
initialOffset?: number;
|
|
200
|
+
}): Promise<void>;
|
|
201
|
+
}
|
|
202
|
+
//# sourceMappingURL=QueryManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"QueryManager.d.ts","sourceRoot":"","sources":["../../src/lib/QueryManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,iBAAiB,EAAyC,WAAW,EAAE,QAAQ,EAA2C,MAAM,uBAAuB,CAAA;AAE3L,OAAO,EAAmB,SAAS,EAAE,MAAM,aAAa,CAAA;AACxD,OAAO,EAAmB,SAAS,EAAE,MAAM,aAAa,CAAA;AACxD,OAAO,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,gBAAgB,EAAa,SAAS,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAA;AAKlQ;;;;;;;;;;;GAWG;AACH,qBAAa,YAAY,CAAC,CAAC,SAAS,SAAS,EAAE,CAAC;IAMlC,OAAO,CAAC,OAAO;IAL3B,WAAW,EAAE,WAAW,CAAA;IACxB;;;OAGG;gBACiB,OAAO,EAAE,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC;IAMtD;;;OAGG;IACH,IAAI,IAAI,WAEP;IAED;;;OAGG;IACH,IAAI,SAAS;;;;;;MAQZ;IAED,sEAAsE;IACtE,SAAS,EAA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|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { UseQueryOptions, UseMutationOptions, QueryKey, FetchQueryOptions } from '@tanstack/react-query';
|
|
2
|
+
import { QueryOperationsOptions, MutationFn, QueryFn, InferMutationParams, InferMutationReturn, InferQueryParams, InferQueryReturn } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Builder class for creating type-safe query and mutation operations
|
|
5
|
+
* @template TMutations - Record type containing all registered mutation functions
|
|
6
|
+
* @template TQueries - Record type containing all registered query functions
|
|
7
|
+
*
|
|
8
|
+
* @description
|
|
9
|
+
* QueryOperations provides a fluent interface for building collections of queries and mutations
|
|
10
|
+
* with full type safety. It acts as a centralized registry for all data operations and provides
|
|
11
|
+
* corresponding React hooks that are automatically typed based on the registered functions.
|
|
12
|
+
*
|
|
13
|
+
* Key features:
|
|
14
|
+
* - Fluent builder pattern for registering operations
|
|
15
|
+
* - Automatic type inference for parameters and return types
|
|
16
|
+
* - Type-safe React hooks generation
|
|
17
|
+
* - Immutable operation registration (returns new instances)
|
|
18
|
+
*/
|
|
19
|
+
export declare class QueryOperations<TMutations, TQueries> {
|
|
20
|
+
private _options;
|
|
21
|
+
private _mutations;
|
|
22
|
+
private _queries;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a new QueryOperations instance
|
|
25
|
+
* @param _options - Configuration options including QueryClient
|
|
26
|
+
* @param _mutations - Record of registered mutation functions (internal)
|
|
27
|
+
* @param _queries - Record of registered query functions (internal)
|
|
28
|
+
*/
|
|
29
|
+
constructor(_options: QueryOperationsOptions, _mutations?: TMutations, _queries?: TQueries);
|
|
30
|
+
/**
|
|
31
|
+
* Gets all registered mutation functions
|
|
32
|
+
* @returns Readonly record of mutation functions
|
|
33
|
+
*/
|
|
34
|
+
get mutations(): Readonly<TMutations>;
|
|
35
|
+
get queryClient(): import("@tanstack/query-core").QueryClient;
|
|
36
|
+
/**
|
|
37
|
+
* Gets all registered query functions
|
|
38
|
+
* @returns Readonly record of query functions
|
|
39
|
+
*/
|
|
40
|
+
get queries(): Readonly<TQueries>;
|
|
41
|
+
/**
|
|
42
|
+
* Registers a new mutation function
|
|
43
|
+
* @template K - The name/key for the mutation
|
|
44
|
+
* @template T - The input data type for the mutation
|
|
45
|
+
* @template R - The return data type for the mutation
|
|
46
|
+
* @param name - Unique name identifier for the mutation
|
|
47
|
+
* @param fn - The mutation function that performs the operation
|
|
48
|
+
* @returns New QueryOperations instance with the mutation added
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* const operations = createQueryOperations({ queryClient })
|
|
53
|
+
* .mutation('createUser', async (userData: CreateUserData) => {
|
|
54
|
+
* return api.post('/users', userData)
|
|
55
|
+
* })
|
|
56
|
+
* .mutation('updateUser', async (userData: UpdateUserData) => {
|
|
57
|
+
* return api.put(`/users/${userData.id}`, userData)
|
|
58
|
+
* })
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
mutation<K extends string, T = any, R = any>(name: K, fn: MutationFn<T, R>): QueryOperations<TMutations & Record<K, MutationFn<T, R>>, TQueries>;
|
|
62
|
+
/**
|
|
63
|
+
* Registers a new query function
|
|
64
|
+
* @template K - The name/key for the query
|
|
65
|
+
* @template T - The parameters type for the query
|
|
66
|
+
* @template R - The return data type for the query
|
|
67
|
+
* @param name - Unique name identifier for the query
|
|
68
|
+
* @param fn - The query function that fetches the data
|
|
69
|
+
* @returns New QueryOperations instance with the query added
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const operations = createQueryOperations({ queryClient })
|
|
74
|
+
* .query('getUser', async (userId: string) => {
|
|
75
|
+
* return api.get(`/users/${userId}`)
|
|
76
|
+
* })
|
|
77
|
+
* .query('getUsers', async (filters?: UserFilters) => {
|
|
78
|
+
* return api.get('/users', { params: filters })
|
|
79
|
+
* })
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
query<K extends string, T = any, R = any>(name: K, fn: QueryFn<T, R>): QueryOperations<TMutations, TQueries & Record<K, QueryFn<T, R>>>;
|
|
83
|
+
/**
|
|
84
|
+
* React hook for executing mutations with full type safety
|
|
85
|
+
* @template K - The mutation key type
|
|
86
|
+
* @param mutationKey - The name of the registered mutation to use
|
|
87
|
+
* @param options - React Query mutation options (excluding mutationFn and mutationKey)
|
|
88
|
+
* @returns React Query mutation object with inferred types
|
|
89
|
+
*
|
|
90
|
+
* @description
|
|
91
|
+
* This hook automatically provides type-safe parameters and return types based on the
|
|
92
|
+
* registered mutation function. It handles error cases and provides proper TypeScript
|
|
93
|
+
* inference for the mutation data and variables.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* const createUserMutation = operations.useMutation('createUser', {
|
|
98
|
+
* onSuccess: (user) => {
|
|
99
|
+
* // 'user' is automatically typed as the return type of createUser
|
|
100
|
+
* console.log('Created user:', user.id)
|
|
101
|
+
* }
|
|
102
|
+
* })
|
|
103
|
+
*
|
|
104
|
+
* // Usage - parameters are type-checked
|
|
105
|
+
* createUserMutation.mutate({ name: 'John', email: 'john@example.com' })
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
useMutation<K extends keyof TMutations>(mutationKey: K, options?: Omit<UseMutationOptions<InferMutationReturn<TMutations[K]>, Error, InferMutationParams<TMutations[K]>>, 'mutationFn'>): import("@tanstack/react-query").UseMutationResult<InferMutationReturn<TMutations[K]>, Error, InferMutationParams<TMutations[K]>, unknown>;
|
|
109
|
+
/**
|
|
110
|
+
* React hook for executing queries with full type safety
|
|
111
|
+
* @template K - The query key type
|
|
112
|
+
* @param queryKey - The name of the registered query to use
|
|
113
|
+
* @param params - Parameters to pass to the query function (optional if query doesn't require params)
|
|
114
|
+
* @param options - React Query options (excluding queryKey and queryFn)
|
|
115
|
+
* @returns React Query query object with inferred types
|
|
116
|
+
*
|
|
117
|
+
* @description
|
|
118
|
+
* This hook automatically provides type-safe parameters and return types based on the
|
|
119
|
+
* registered query function. It generates appropriate query keys and handles parameter
|
|
120
|
+
* validation.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* // Query with parameters
|
|
125
|
+
* const userQuery = operations.useQuery('getUser', 'user-123', {
|
|
126
|
+
* enabled: !!userId
|
|
127
|
+
* })
|
|
128
|
+
*
|
|
129
|
+
* // Query without parameters
|
|
130
|
+
* const usersQuery = operations.useQuery('getUsers', undefined, {
|
|
131
|
+
* refetchInterval: 30000
|
|
132
|
+
* })
|
|
133
|
+
*
|
|
134
|
+
* // Query with optional parameters
|
|
135
|
+
* const filteredUsersQuery = operations.useQuery('getUsers', { status: 'active' })
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
useQuery<K extends keyof TQueries, T = InferQueryReturn<TQueries[K]>>(queryKey: K, params?: InferQueryParams<TQueries[K]>, options?: Omit<Partial<UseQueryOptions<InferQueryReturn<TQueries[K]>, Error, T, QueryKey>>, 'queryFn'>): import("@tanstack/react-query").DefinedUseQueryResult<import("@tanstack/query-core").NoInfer<T>, Error>;
|
|
139
|
+
/**
|
|
140
|
+
* Generates a properly typed query key for React Query
|
|
141
|
+
* @template K - The query key type
|
|
142
|
+
* @param queryKey - The name of the query
|
|
143
|
+
* @param params - Optional parameters for the query
|
|
144
|
+
* @returns Query key array, with params included only when necessary
|
|
145
|
+
*
|
|
146
|
+
* @description
|
|
147
|
+
* This method creates React Query compatible keys that include parameters when present.
|
|
148
|
+
* The return type is conditionally typed based on whether the query requires parameters.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* // Returns ['getUser', 'user-123']
|
|
153
|
+
* const keyWithParams = operations.getQueryKey('getUser', 'user-123')
|
|
154
|
+
*
|
|
155
|
+
* // Returns ['getUsers']
|
|
156
|
+
* const keyWithoutParams = operations.getQueryKey('getUsers')
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
getQueryKey<K extends keyof TQueries>(queryKey: K, params?: InferQueryParams<TQueries[K]>): QueryKey;
|
|
160
|
+
/**
|
|
161
|
+
* Generates a mutation key for React Query
|
|
162
|
+
* @template K - The mutation key type
|
|
163
|
+
* @param mutationKey - The name of the mutation
|
|
164
|
+
* @returns Mutation key array containing only the mutation name
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```typescript
|
|
168
|
+
* // Returns ['createUser']
|
|
169
|
+
* const mutationKey = operations.getMutationKey('createUser')
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
getMutationKey<K extends keyof TMutations>(mutationKey: K): QueryKey;
|
|
173
|
+
/**
|
|
174
|
+
* Prefetches a query to populate the cache ahead of time
|
|
175
|
+
* @template K - The query key type
|
|
176
|
+
* @param queryKey - The name of the registered query to prefetch
|
|
177
|
+
* @param params - Parameters to pass to the query function (optional if query doesn't require params)
|
|
178
|
+
* @param options - React Query prefetch options
|
|
179
|
+
* @returns Promise that resolves when the prefetch is complete
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```typescript
|
|
183
|
+
* // Prefetch user data when hovering over a user link
|
|
184
|
+
* const handleUserHover = async (userId: string) => {
|
|
185
|
+
* await operations.prefetchQuery('getUser', userId, {
|
|
186
|
+
* staleTime: 5 * 60 * 1000 // 5 minutes
|
|
187
|
+
* })
|
|
188
|
+
* }
|
|
189
|
+
*
|
|
190
|
+
* // Prefetch data on route change
|
|
191
|
+
* useEffect(() => {
|
|
192
|
+
* operations.prefetchQuery('getUsers', { status: 'active' })
|
|
193
|
+
* }, [])
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
prefetchQuery<K extends keyof TQueries, T = InferQueryReturn<TQueries[K]>>(queryKey: K, params?: InferQueryParams<TQueries[K]>, options?: FetchQueryOptions<InferQueryReturn<TQueries[K]>, Error, T, QueryKey, never>): Promise<void>;
|
|
197
|
+
/**
|
|
198
|
+
* Retrieves cached query data if it exists
|
|
199
|
+
* @template K - The query key type
|
|
200
|
+
* @template T - The expected return type (defaults to inferred query return type)
|
|
201
|
+
* @param queryKey - The name of the registered query
|
|
202
|
+
* @param params - Parameters used when the query was cached (optional if query doesn't require params)
|
|
203
|
+
* @returns The cached data if it exists, undefined otherwise
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```typescript
|
|
207
|
+
* // Get cached user data
|
|
208
|
+
* const cachedUser = operations.getQueryData('getUser', 'user-123')
|
|
209
|
+
* if (cachedUser) {
|
|
210
|
+
* console.log('User already in cache:', cachedUser.name)
|
|
211
|
+
* }
|
|
212
|
+
*
|
|
213
|
+
* // Check if users list is cached before showing loading state
|
|
214
|
+
* const cachedUsers = operations.getQueryData('getUsers')
|
|
215
|
+
* const showSkeleton = !cachedUsers
|
|
216
|
+
*
|
|
217
|
+
* // Access cached data in event handlers
|
|
218
|
+
* const handleUserAction = () => {
|
|
219
|
+
* const currentUser = operations.getQueryData('getCurrentUser')
|
|
220
|
+
* if (currentUser?.role === 'admin') {
|
|
221
|
+
* // Perform admin action
|
|
222
|
+
* }
|
|
223
|
+
* }
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
getQueryData<K extends keyof TQueries, T = InferQueryReturn<TQueries[K]>>(queryKey: K, params?: InferQueryParams<TQueries[K]>, options?: FetchQueryOptions<InferQueryReturn<TQueries[K]>, Error, T, QueryKey, never>): Promise<T | undefined>;
|
|
227
|
+
}
|
|
228
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/QueryOperations/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,eAAe,EAAE,kBAAkB,EAAE,QAAQ,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAA;AAC/H,OAAO,EAAE,sBAAsB,EAAE,UAAU,EAAE,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,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AY,CAAC,CAAC,SAAS,MAAM,QAAQ,EAAE,CAAC,GAAG,gBAAgB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAC5E,QAAQ,EAAE,CAAC,EACX,MAAM,CAAC,EAAE,gBAAgB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EACtC,OAAO,CAAC,EAAE,iBAAiB,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,KAAK,CAAC;CAYxF"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { QueryClient } from '../../types';
|
|
2
|
+
import { QueryClientEnhanced } from '../QueryClientEnhanced';
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for QueryOperations
|
|
5
|
+
*/
|
|
6
|
+
export type QueryOperationsOptions = {
|
|
7
|
+
/** The React Query client instance */
|
|
8
|
+
queryClient: QueryClient | QueryClientEnhanced;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Generic mutation function type
|
|
12
|
+
* @template T - The input data type
|
|
13
|
+
* @template R - The return data type
|
|
14
|
+
*/
|
|
15
|
+
export type MutationFn<T = any, R = any> = (data: T) => Promise<R>;
|
|
16
|
+
/**
|
|
17
|
+
* Generic query function type
|
|
18
|
+
* @template T - The parameters type
|
|
19
|
+
* @template R - The return data type
|
|
20
|
+
*/
|
|
21
|
+
export type QueryFn<T = any, R = any> = (params?: T) => Promise<R>;
|
|
22
|
+
/**
|
|
23
|
+
* Utility type to infer mutation function parameters
|
|
24
|
+
* @template T - The mutation function type
|
|
25
|
+
*/
|
|
26
|
+
export type InferMutationParams<T> = T extends MutationFn<infer P, any> ? P : never;
|
|
27
|
+
/**
|
|
28
|
+
* Utility type to infer mutation function return type
|
|
29
|
+
* @template T - The mutation function type
|
|
30
|
+
*/
|
|
31
|
+
export type InferMutationReturn<T> = T extends MutationFn<any, infer R> ? R : never;
|
|
32
|
+
/**
|
|
33
|
+
* Utility type to infer query function parameters
|
|
34
|
+
* @template T - The query function type
|
|
35
|
+
*/
|
|
36
|
+
export type InferQueryParams<T> = T extends QueryFn<infer P, any> ? P : never;
|
|
37
|
+
/**
|
|
38
|
+
* Utility type to infer query function return type
|
|
39
|
+
* @template T - The query function type
|
|
40
|
+
*/
|
|
41
|
+
export type InferQueryReturn<T> = T extends QueryFn<any, infer R> ? R : never;
|
|
42
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/lib/QueryOperations/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACzC,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AAE5D;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC,sCAAsC;IACtC,WAAW,EAAE,WAAW,GAAG,mBAAmB,CAAA;CAC/C,CAAA;AAED;;;;GAIG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;AAElE;;;;GAIG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;AAElE;;;GAGG;AACH,MAAM,MAAM,mBAAmB,CAAC,CAAC,IAAI,CAAC,SAAS,UAAU,CAAC,MAAM,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;AAEnF;;;GAGG;AACH,MAAM,MAAM,mBAAmB,CAAC,CAAC,IAAI,CAAC,SAAS,UAAU,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;AAEnF;;;GAGG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,EAAE,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;AAE7E;;;GAGG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI,CAAC,SAAS,OAAO,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAA;AACrC,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA;AAC3B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,mBAAmB,CAAA"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { UseInfiniteQueryResult, useQueryClient } from '@tanstack/react-query';
|
|
2
|
+
import { QueryClientEnhanced } from '../lib';
|
|
3
|
+
/** Minimum shape required for any entity managed by QueryManager. */
|
|
4
|
+
export type QueryItem = {
|
|
5
|
+
id: string | number;
|
|
6
|
+
};
|
|
7
|
+
/** Cursor value used to request a specific page in an infinite list query. */
|
|
8
|
+
export type PageParam = number;
|
|
9
|
+
/** Raw page payload returned by `listFn` — a flat array of items for one page. */
|
|
10
|
+
export type ListPaginationResponse<T extends QueryItem> = T[];
|
|
11
|
+
/** Constructor options for `QueryManager`. All `*Fn` fields are optional; omitting one disables the corresponding operation and its generated hooks. */
|
|
12
|
+
export type QueryManagerOptions<T extends QueryItem, F> = {
|
|
13
|
+
name: string;
|
|
14
|
+
queryClient: ReturnType<typeof useQueryClient> | QueryClientEnhanced;
|
|
15
|
+
listFn?: (limit: number, offset: number, filters: F) => Promise<ListPaginationResponse<T>>;
|
|
16
|
+
/** Page size sent to `listFn`. Defaults to 10 when omitted. */
|
|
17
|
+
listLimit?: number;
|
|
18
|
+
/** Side-effect hook called on every render with the current infinite-list query result. Useful for syncing list state into external stores. */
|
|
19
|
+
useListEffect?: (listQuery: UseInfiniteQueryResult<{
|
|
20
|
+
pageParams: number[];
|
|
21
|
+
pages: ListPaginationResponse<T>[];
|
|
22
|
+
allItems: T[];
|
|
23
|
+
}, Error>) => void;
|
|
24
|
+
retrieveFn?: (id: T['id']) => Promise<T>;
|
|
25
|
+
createFn?: (data: Partial<T>) => Promise<T>;
|
|
26
|
+
updateFn?: (data: Partial<T>) => Promise<T>;
|
|
27
|
+
deleteFn?: (id: T['id']) => Promise<T['id']>;
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../../src/types/core.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAC9E,OAAO,EAAE,mBAAmB,EAAE,MAAM,QAAQ,CAAA;AAE5C,qEAAqE;AACrE,MAAM,MAAM,SAAS,GAAG;IACtB,EAAE,EAAE,MAAM,GAAG,MAAM,CAAA;CACpB,CAAA;AAED,8EAA8E;AAC9E,MAAM,MAAM,SAAS,GAAG,MAAM,CAAA;AAE9B,kFAAkF;AAClF,MAAM,MAAM,sBAAsB,CAAC,CAAC,SAAS,SAAS,IAAI,CAAC,EAAE,CAAA;AAE7D,wJAAwJ;AACxJ,MAAM,MAAM,mBAAmB,CAAC,CAAC,SAAS,SAAS,EAAE,CAAC,IAAI;IACxD,IAAI,EAAE,MAAM,CAAA;IAEZ,WAAW,EAAE,UAAU,CAAC,OAAO,cAAc,CAAC,GAAG,mBAAmB,CAAA;IAEpE,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,KAAK,OAAO,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC,CAAA;IAE1F,+DAA+D;IAC/D,SAAS,CAAC,EAAE,MAAM,CAAA;IAElB,+IAA+I;IAC/I,aAAa,CAAC,EAAE,CAAC,SAAS,EAAE,sBAAsB,CAAC;QACjD,UAAU,EAAE,MAAM,EAAE,CAAA;QACpB,KAAK,EAAE,sBAAsB,CAAC,CAAC,CAAC,EAAE,CAAA;QAClC,QAAQ,EAAE,CAAC,EAAE,CAAA;KACd,EAAE,KAAK,CAAC,KAAK,IAAI,CAAA;IAElB,UAAU,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;IAExC,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;IAE3C,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAA;IAE3C,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAA;CAC7C,CAAA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { UseMutationOptions } from '@tanstack/react-query';
|
|
2
|
+
import { QueryItem } from './core';
|
|
3
|
+
import { RemovedItemMap } from './utility';
|
|
4
|
+
/** Rollback context threaded through an optimistic create mutation. `tempId` identifies the placeholder item inserted before the server responds. */
|
|
5
|
+
export type CreateMutationCtx = {
|
|
6
|
+
tempId?: QueryItem['id'];
|
|
7
|
+
};
|
|
8
|
+
/** Options forwarded to React Query's `useMutation` for a create operation. `mutationKey` and `mutationFn` are managed internally. */
|
|
9
|
+
export type CreateMutationOptions<T extends QueryItem, F> = Omit<UseMutationOptions<T, Error, Partial<T>, CreateMutationCtx | undefined>, 'mutationKey' | 'mutationFn'> & {
|
|
10
|
+
/** Insert a placeholder item immediately and replace it on success; roll back on error. */
|
|
11
|
+
optimistic?: boolean;
|
|
12
|
+
/** List query filters active at the time of the create, used to target the correct cached list for the optimistic insert. */
|
|
13
|
+
listFilters?: F;
|
|
14
|
+
/**
|
|
15
|
+
* Where to insert the item in the cached list.
|
|
16
|
+
* - `'start'` / `'end'` — prepend or append to the flat item array.
|
|
17
|
+
* - `RemovedItemMap` — re-insert at the exact page/index coordinates recorded during a previous delete (used internally for rollback).
|
|
18
|
+
*/
|
|
19
|
+
appendTo?: RemovedItemMap | 'start' | 'end';
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=create.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../../src/types/create.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAE1C,qJAAqJ;AACrJ,MAAM,MAAM,iBAAiB,GAAG;IAC9B,MAAM,CAAC,EAAE,SAAS,CAAC,IAAI,CAAC,CAAA;CACzB,CAAA;AAED,sIAAsI;AACtI,MAAM,MAAM,qBAAqB,CAAC,CAAC,SAAS,SAAS,EAAE,CAAC,IAAI,IAAI,CAC9D,kBAAkB,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,iBAAiB,GAAC,SAAS,CAAC,EACrE,aAAa,GAAG,YAAY,CAC7B,GAAG;IACF,2FAA2F;IAC3F,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,6HAA6H;IAC7H,WAAW,CAAC,EAAE,CAAC,CAAA;IACf;;;;OAIG;IACH,QAAQ,CAAC,EAAE,cAAc,GAAG,OAAO,GAAG,KAAK,CAAA;CAC5C,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { UseMutationOptions } from '@tanstack/react-query';
|
|
2
|
+
import { QueryItem } from './core';
|
|
3
|
+
import { RemovedItemMap } from './utility';
|
|
4
|
+
/**
|
|
5
|
+
* Rollback context captured before an optimistic delete.
|
|
6
|
+
* `removedAt` records the exact page/index coordinates of the removed item so it can be re-inserted at the same position if the mutation fails.
|
|
7
|
+
* `null` means the item was not found in any cached page.
|
|
8
|
+
*/
|
|
9
|
+
export type DeleteMutationCtx<T> = {
|
|
10
|
+
previousItem: T | undefined;
|
|
11
|
+
removedAt: RemovedItemMap | null;
|
|
12
|
+
};
|
|
13
|
+
/** Options forwarded to React Query's `useMutation` for a delete operation. `mutationKey` and `mutationFn` are managed internally. */
|
|
14
|
+
export type DeleteMutationOptions<T extends QueryItem, F> = Omit<UseMutationOptions<unknown, Error, T['id'], DeleteMutationCtx<T>>, 'mutationKey' | 'mutationFn'> & {
|
|
15
|
+
/** Remove the item from every cached list page immediately and restore it on error. */
|
|
16
|
+
optimistic?: boolean;
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=delete.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delete.d.ts","sourceRoot":"","sources":["../../src/types/delete.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAClC,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAE1C;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI;IACjC,YAAY,EAAE,CAAC,GAAG,SAAS,CAAA;IAC3B,SAAS,EAAE,cAAc,GAAG,IAAI,CAAA;CACjC,CAAA;AAED,sIAAsI;AACtI,MAAM,MAAM,qBAAqB,CAAC,CAAC,SAAS,SAAS,EAAE,CAAC,IAAI,IAAI,CAC9D,kBAAkB,CAAC,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,iBAAiB,CAAC,CAAC,CAAC,CAAC,EACjE,aAAa,GAAG,YAAY,CAC7B,GAAG;IACF,uFAAuF;IACvF,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAA;AACtB,cAAc,QAAQ,CAAA;AACtB,cAAc,WAAW,CAAA;AACzB,cAAc,YAAY,CAAA;AAC1B,cAAc,UAAU,CAAA;AACxB,cAAc,UAAU,CAAA;AACxB,cAAc,UAAU,CAAA"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { QueryKey, UseInfiniteQueryOptions } from '@tanstack/react-query';
|
|
2
|
+
import { ListPaginationResponse, PageParam, QueryItem } from './core';
|
|
3
|
+
/**
|
|
4
|
+
* Normalised shape of the infinite-query cache entry.
|
|
5
|
+
* `allItems` is a convenience flat-merge of every page's items — use it for rendering; use `pages` when you need per-page boundaries.
|
|
6
|
+
*/
|
|
7
|
+
export type ListSelector<T extends QueryItem> = {
|
|
8
|
+
pageParams: PageParam[];
|
|
9
|
+
pages: ListPaginationResponse<T>[];
|
|
10
|
+
allItems: T[];
|
|
11
|
+
};
|
|
12
|
+
type InfiniteQueryOptions<T extends QueryItem> = UseInfiniteQueryOptions<ListPaginationResponse<T>, Error, ListSelector<T>, QueryKey, PageParam>;
|
|
13
|
+
/** Options accepted by the generated `useList` hook. Extends React Query's infinite-query options; `limit` and `filters` are forwarded to `listFn`. */
|
|
14
|
+
export type ListQueryOptions<T extends QueryItem, F> = Partial<InfiniteQueryOptions<T>> & {
|
|
15
|
+
limit?: number;
|
|
16
|
+
filters?: F;
|
|
17
|
+
};
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=list.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"list.d.ts","sourceRoot":"","sources":["../../src/types/list.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,uBAAuB,EAAE,MAAM,uBAAuB,CAAA;AACzE,OAAO,EAAE,sBAAsB,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAErE;;;GAGG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,SAAS,IAAI;IAC9C,UAAU,EAAE,SAAS,EAAE,CAAA;IACvB,KAAK,EAAE,sBAAsB,CAAC,CAAC,CAAC,EAAE,CAAA;IAClC,QAAQ,EAAE,CAAC,EAAE,CAAA;CACd,CAAA;AAED,KAAK,oBAAoB,CAAC,CAAC,SAAS,SAAS,IAAI,uBAAuB,CACtE,sBAAsB,CAAC,CAAC,CAAC,EACzB,KAAK,EACL,YAAY,CAAC,CAAC,CAAC,EACf,QAAQ,EACR,SAAS,CACV,CAAA;AAED,uJAAuJ;AACvJ,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,SAAS,EAAE,CAAC,IAAI,OAAO,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,GAAG;IACxF,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,EAAE,CAAC,CAAA;CACZ,CAAA"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { QueryKey, UndefinedInitialDataOptions } from '@tanstack/react-query';
|
|
2
|
+
import { QueryItem } from './core';
|
|
3
|
+
/** Pass-through options forwarded to React Query's `useQuery` for a retrieve query. `queryKey` and `queryFn` are managed internally and cannot be overridden. */
|
|
4
|
+
export type RetrieveQueryOptions<T extends QueryItem> = Omit<UndefinedInitialDataOptions<T, Error, T, QueryKey>, 'queryKey' | 'queryFn'>;
|
|
5
|
+
export type RetrieveDataOptions = {
|
|
6
|
+
/**
|
|
7
|
+
* When `true`, skips the network fetch and returns only what is already in the cache.
|
|
8
|
+
* When `false` (default), falls back to a network fetch if the cache is empty.
|
|
9
|
+
*/
|
|
10
|
+
onlyQueryData?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* When `true`, searches all cached list pages for the item before issuing a dedicated retrieve request.
|
|
13
|
+
* Avoids a redundant round-trip when the item was already loaded by a list query.
|
|
14
|
+
*/
|
|
15
|
+
deepSearch?: boolean;
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=retrieve.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retrieve.d.ts","sourceRoot":"","sources":["../../src/types/retrieve.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,2BAA2B,EAAE,MAAM,uBAAuB,CAAA;AAC7E,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAElC,iKAAiK;AACjK,MAAM,MAAM,oBAAoB,CAAC,CAAC,SAAS,SAAS,IAAI,IAAI,CAC1D,2BAA2B,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,CAAC,EAClD,UAAU,GAAG,SAAS,CACvB,CAAA;AAED,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;OAGG;IACH,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB;;;OAGG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;CACrB,CAAA"}
|