@codeleap/query 7.0.1 → 7.1.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.
- package/dist/factors/createQueryManager.d.ts +17 -14
- package/dist/factors/createQueryManager.d.ts.map +1 -1
- package/dist/factors/createQueryManager.js +15 -12
- package/dist/factors/createQueryManager.js.map +1 -1
- package/dist/lib/Mutations.js +32 -35
- package/dist/lib/Mutations.js.map +1 -1
- package/dist/lib/QueryClientEnhanced/index.js +41 -43
- package/dist/lib/QueryClientEnhanced/index.js.map +1 -1
- package/dist/lib/QueryKeys.js +70 -73
- package/dist/lib/QueryKeys.js.map +1 -1
- package/dist/lib/QueryManager.d.ts +24 -8
- package/dist/lib/QueryManager.d.ts.map +1 -1
- package/dist/lib/QueryManager.js +153 -97
- package/dist/lib/QueryManager.js.map +1 -1
- package/dist/lib/QueryOperations/index.js +29 -25
- package/dist/lib/QueryOperations/index.js.map +1 -1
- package/dist/types/core.d.ts +17 -3
- package/dist/types/core.d.ts.map +1 -1
- package/dist/types/list.d.ts +6 -4
- package/dist/types/list.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/factors/createQueryManager.ts +19 -16
- package/src/lib/QueryManager.ts +55 -15
- package/src/types/core.ts +22 -3
- package/src/types/list.ts +7 -5
package/dist/lib/QueryKeys.js
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import { useMemo } from 'react';
|
|
11
2
|
import { TypeGuards } from '@codeleap/types';
|
|
12
3
|
import deepEqual from 'fast-deep-equal';
|
|
@@ -16,6 +7,8 @@ import deepEqual from 'fast-deep-equal';
|
|
|
16
7
|
* @template F - The filter type used for list queries
|
|
17
8
|
*/
|
|
18
9
|
export class QueryKeys {
|
|
10
|
+
queryName;
|
|
11
|
+
queryClient;
|
|
19
12
|
/**
|
|
20
13
|
* Creates a new QueryKeys instance
|
|
21
14
|
* @param queryName - The name of the query used as base for all keys
|
|
@@ -48,7 +41,7 @@ export class QueryKeys {
|
|
|
48
41
|
listKeyWithFilters(filters) {
|
|
49
42
|
const hasValidFilters = !TypeGuards.isNil(filters) && (typeof filters !== 'object'
|
|
50
43
|
? Boolean(filters)
|
|
51
|
-
: Object.values(filters
|
|
44
|
+
: Object.values(filters ?? {}).some(value => value != null && (typeof value !== 'string' || value.trim() !== '')));
|
|
52
45
|
return hasValidFilters ? [...this.keys.list, filters] : this.keys.list;
|
|
53
46
|
}
|
|
54
47
|
/**
|
|
@@ -79,9 +72,8 @@ export class QueryKeys {
|
|
|
79
72
|
* @returns True if the query matches the query name
|
|
80
73
|
*/
|
|
81
74
|
predicateQueryKeyAll(queryName, query) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return queryKey === null || queryKey === void 0 ? void 0 : queryKey.includes(queryName);
|
|
75
|
+
const queryKey = query?.queryKey?.join('/');
|
|
76
|
+
return queryKey?.includes(queryName);
|
|
85
77
|
}
|
|
86
78
|
/**
|
|
87
79
|
* Predicate function to check if a query is a list query for this query name
|
|
@@ -92,15 +84,14 @@ export class QueryKeys {
|
|
|
92
84
|
* @returns True if the query is a list query and not in the ignore list
|
|
93
85
|
*/
|
|
94
86
|
predicateQueryKeyList(queryName, query, toIgnoreQueryKeys) {
|
|
95
|
-
|
|
96
|
-
const queryKey = (_a = query === null || query === void 0 ? void 0 : query.queryKey) === null || _a === void 0 ? void 0 : _a.join('/');
|
|
87
|
+
const queryKey = query?.queryKey?.join('/');
|
|
97
88
|
if (!TypeGuards.isNil(toIgnoreQueryKeys)) {
|
|
98
|
-
const ignoreQueryKeys = Array.isArray(toIgnoreQueryKeys
|
|
99
|
-
if (ignoreQueryKeys.some(key => deepEqual(query
|
|
89
|
+
const ignoreQueryKeys = Array.isArray(toIgnoreQueryKeys?.[0]) ? toIgnoreQueryKeys : [toIgnoreQueryKeys];
|
|
90
|
+
if (ignoreQueryKeys.some(key => deepEqual(query?.queryKey, key))) {
|
|
100
91
|
return false;
|
|
101
92
|
}
|
|
102
93
|
}
|
|
103
|
-
const isListQueryKey =
|
|
94
|
+
const isListQueryKey = queryKey?.includes(queryName) && queryKey?.includes('list');
|
|
104
95
|
return isListQueryKey;
|
|
105
96
|
}
|
|
106
97
|
/**
|
|
@@ -109,10 +100,11 @@ export class QueryKeys {
|
|
|
109
100
|
* @param options - Optional invalidation options
|
|
110
101
|
* @returns Promise that resolves when invalidation is complete
|
|
111
102
|
*/
|
|
112
|
-
invalidateAll(filters, options) {
|
|
113
|
-
return
|
|
114
|
-
|
|
115
|
-
|
|
103
|
+
async invalidateAll(filters, options) {
|
|
104
|
+
return this.queryClient.invalidateQueries({
|
|
105
|
+
...filters,
|
|
106
|
+
predicate: (query) => this.predicateQueryKeyAll(this.queryName, query),
|
|
107
|
+
}, options);
|
|
116
108
|
}
|
|
117
109
|
/**
|
|
118
110
|
* Invalidates list queries, optionally with specific filters
|
|
@@ -122,14 +114,15 @@ export class QueryKeys {
|
|
|
122
114
|
* @param options - Optional invalidation options
|
|
123
115
|
* @returns Promise that resolves when invalidation is complete
|
|
124
116
|
*/
|
|
125
|
-
invalidateList(listFilters, ignoreQueryKeys, filters, options) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
117
|
+
async invalidateList(listFilters, ignoreQueryKeys, filters, options) {
|
|
118
|
+
if (!!listFilters) {
|
|
119
|
+
const queryKey = this.listKeyWithFilters(listFilters);
|
|
120
|
+
return this.queryClient.invalidateQueries({ ...filters, queryKey }, options);
|
|
121
|
+
}
|
|
122
|
+
return this.queryClient.invalidateQueries({
|
|
123
|
+
...filters,
|
|
124
|
+
predicate: (query) => this.predicateQueryKeyList(this.queryName, query, ignoreQueryKeys),
|
|
125
|
+
}, options);
|
|
133
126
|
}
|
|
134
127
|
/**
|
|
135
128
|
* Invalidates a specific retrieve query by ID
|
|
@@ -138,11 +131,12 @@ export class QueryKeys {
|
|
|
138
131
|
* @param options - Optional invalidation options
|
|
139
132
|
* @returns Promise that resolves when invalidation is complete
|
|
140
133
|
*/
|
|
141
|
-
invalidateRetrieve(id, filters, options) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
134
|
+
async invalidateRetrieve(id, filters, options) {
|
|
135
|
+
const queryKey = this.keys.retrieve(id);
|
|
136
|
+
return this.queryClient.invalidateQueries({
|
|
137
|
+
...filters,
|
|
138
|
+
queryKey,
|
|
139
|
+
}, options);
|
|
146
140
|
}
|
|
147
141
|
/**
|
|
148
142
|
* Refetches all queries for this query name
|
|
@@ -150,10 +144,11 @@ export class QueryKeys {
|
|
|
150
144
|
* @param options - Optional refetch options
|
|
151
145
|
* @returns Promise that resolves when refetch is complete
|
|
152
146
|
*/
|
|
153
|
-
refetchAll(filters, options) {
|
|
154
|
-
return
|
|
155
|
-
|
|
156
|
-
|
|
147
|
+
async refetchAll(filters, options) {
|
|
148
|
+
return this.queryClient.refetchQueries({
|
|
149
|
+
...filters,
|
|
150
|
+
predicate: (query) => this.predicateQueryKeyAll(this.queryName, query),
|
|
151
|
+
}, options);
|
|
157
152
|
}
|
|
158
153
|
/**
|
|
159
154
|
* Refetches list queries, optionally with specific filters
|
|
@@ -163,14 +158,15 @@ export class QueryKeys {
|
|
|
163
158
|
* @param options - Optional refetch options
|
|
164
159
|
* @returns Promise that resolves when refetch is complete
|
|
165
160
|
*/
|
|
166
|
-
refetchList(listFilters, ignoreQueryKeys, filters, options) {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
161
|
+
async refetchList(listFilters, ignoreQueryKeys, filters, options) {
|
|
162
|
+
if (!!listFilters) {
|
|
163
|
+
const queryKey = this.listKeyWithFilters(listFilters);
|
|
164
|
+
return this.queryClient.refetchQueries({ ...filters, queryKey }, options);
|
|
165
|
+
}
|
|
166
|
+
return this.queryClient.refetchQueries({
|
|
167
|
+
...filters,
|
|
168
|
+
predicate: (query) => this.predicateQueryKeyList(this.queryName, query, ignoreQueryKeys),
|
|
169
|
+
}, options);
|
|
174
170
|
}
|
|
175
171
|
/**
|
|
176
172
|
* Refetches a specific retrieve query by ID
|
|
@@ -179,11 +175,12 @@ export class QueryKeys {
|
|
|
179
175
|
* @param options - Optional refetch options
|
|
180
176
|
* @returns Promise that resolves when refetch is complete
|
|
181
177
|
*/
|
|
182
|
-
refetchRetrieve(id, filters, options) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
178
|
+
async refetchRetrieve(id, filters, options) {
|
|
179
|
+
const queryKey = this.keys.retrieve(id);
|
|
180
|
+
return this.queryClient.refetchQueries({
|
|
181
|
+
...filters,
|
|
182
|
+
queryKey,
|
|
183
|
+
}, options);
|
|
187
184
|
}
|
|
188
185
|
/**
|
|
189
186
|
* Removes a specific retrieve query data from the cache
|
|
@@ -191,10 +188,11 @@ export class QueryKeys {
|
|
|
191
188
|
* @param filters - Optional filters to apply to the removal
|
|
192
189
|
* @returns Promise that resolves when removal is complete
|
|
193
190
|
*/
|
|
194
|
-
removeRetrieveQueryData(id, filters) {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
191
|
+
async removeRetrieveQueryData(id, filters) {
|
|
192
|
+
const queryKey = this.keys.retrieve(id);
|
|
193
|
+
return this.queryClient.removeQueries({
|
|
194
|
+
...filters,
|
|
195
|
+
queryKey,
|
|
198
196
|
});
|
|
199
197
|
}
|
|
200
198
|
/**
|
|
@@ -205,14 +203,15 @@ export class QueryKeys {
|
|
|
205
203
|
* @param options - Optional cancellation options
|
|
206
204
|
* @returns Promise that resolves when cancellation is complete
|
|
207
205
|
*/
|
|
208
|
-
cancelListQueries(listFilters, ignoreQueryKeys, filters, options) {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
206
|
+
async cancelListQueries(listFilters, ignoreQueryKeys, filters, options) {
|
|
207
|
+
if (!!listFilters) {
|
|
208
|
+
const queryKey = this.listKeyWithFilters(listFilters);
|
|
209
|
+
return this.queryClient.cancelQueries({ ...filters, queryKey }, options);
|
|
210
|
+
}
|
|
211
|
+
return this.queryClient.cancelQueries({
|
|
212
|
+
...filters,
|
|
213
|
+
predicate: (query) => this.predicateQueryKeyList(this.queryName, query, ignoreQueryKeys),
|
|
214
|
+
}, options);
|
|
216
215
|
}
|
|
217
216
|
/**
|
|
218
217
|
* Gets list data from the cache, including both items array and item map
|
|
@@ -220,12 +219,11 @@ export class QueryKeys {
|
|
|
220
219
|
* @returns Object containing items array and itemMap (keyed by ID)
|
|
221
220
|
*/
|
|
222
221
|
getListData(filters) {
|
|
223
|
-
var _a;
|
|
224
222
|
const queryKey = this.listKeyWithFilters(filters);
|
|
225
223
|
const data = this.queryClient.getQueryData(queryKey);
|
|
226
|
-
const items = (
|
|
224
|
+
const items = (data?.pages ?? []).flat();
|
|
227
225
|
const itemMap = items.reduce((acc, item) => {
|
|
228
|
-
acc[item
|
|
226
|
+
acc[item?.id] = item;
|
|
229
227
|
return acc;
|
|
230
228
|
}, {});
|
|
231
229
|
return {
|
|
@@ -249,29 +247,28 @@ export class QueryKeys {
|
|
|
249
247
|
* @returns Item | undefined
|
|
250
248
|
*/
|
|
251
249
|
getRetrieveData(id, options = {}) {
|
|
252
|
-
var _a;
|
|
253
250
|
const { onlyQueryData = false, deepSearch = false, } = options;
|
|
254
251
|
if (TypeGuards.isNil(id))
|
|
255
252
|
return undefined;
|
|
256
253
|
const queryKey = this.keys.retrieve(id);
|
|
257
254
|
const queryData = this.queryClient.getQueryData(queryKey);
|
|
258
|
-
if (queryData
|
|
255
|
+
if (queryData?.id)
|
|
259
256
|
return queryData;
|
|
260
257
|
if (onlyQueryData)
|
|
261
258
|
return undefined;
|
|
262
259
|
if (!deepSearch) {
|
|
263
260
|
const { itemMap } = this.getListData();
|
|
264
|
-
return itemMap
|
|
261
|
+
return itemMap?.[id];
|
|
265
262
|
}
|
|
266
263
|
const queries = this.getAllListQueries();
|
|
267
264
|
for (const query of queries) {
|
|
268
|
-
const pages =
|
|
269
|
-
if (!
|
|
265
|
+
const pages = query.state.data?.pages;
|
|
266
|
+
if (!pages?.length)
|
|
270
267
|
continue;
|
|
271
268
|
const item = pages
|
|
272
269
|
.filter(Boolean)
|
|
273
270
|
.flatMap(page => Array.isArray(page) ? page : [])
|
|
274
|
-
.find(item =>
|
|
271
|
+
.find(item => item?.id === id);
|
|
275
272
|
if (item)
|
|
276
273
|
return item;
|
|
277
274
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"QueryKeys.js","sourceRoot":"","sources":["../../src/lib/QueryKeys.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"QueryKeys.js","sourceRoot":"","sources":["../../src/lib/QueryKeys.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAA;AAC/B,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAE5C,OAAO,SAAS,MAAM,iBAAiB,CAAA;AAEvC;;;;GAIG;AACH,MAAM,OAAO,SAAS;IAOV;IACA;IAPV;;;;OAIG;IACH,YACU,SAAiB,EACjB,WAAwB;QADxB,cAAS,GAAT,SAAS,CAAQ;QACjB,gBAAW,GAAX,WAAW,CAAa;IAC9B,CAAC;IA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E,CAAC,CAAA;QAEF,OAAO,OAAiG,CAAA;IAC1G,CAAC;IAED;;;;OAIG;IACH,YAAY,CAAC,WAAe;QAC1B,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,CAAC,aAAa,EAAE,CAAC,IAAI,CAAC;YAClD,QAAQ,EAAE,IAAI,CAAC,kBAAkB,CAAC,WAAW,CAAC;SAC/C,CAAC,CAAA;QAEF,OAAO,KAA6F,CAAA;IACtG,CAAC;CACF;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAyB,IAAY,EAAE,WAAwB,EAAE,EAAE;IAChG,OAAO,IAAI,SAAS,CAAO,IAAI,EAAE,WAAW,CAAC,CAAA;AAC/C,CAAC,CAAA"}
|
|
@@ -1,27 +1,29 @@
|
|
|
1
1
|
import { FetchInfiniteQueryOptions, FetchQueryOptions, QueryClient, QueryKey } from '@tanstack/react-query';
|
|
2
2
|
import { QueryKeys } from './QueryKeys';
|
|
3
3
|
import { Mutations } from './Mutations';
|
|
4
|
-
import { CreateMutationCtx, CreateMutationOptions, ListPaginationResponse, ListQueryOptions, QueryItem, QueryManagerOptions, RetrieveQueryOptions, UpdateMutationCtx, UpdateMutationOptions, DeleteMutationCtx, DeleteMutationOptions } from '../types';
|
|
4
|
+
import { CreateMutationCtx, CreateMutationOptions, ListPaginationResponse, ListQueryOptions, ListResponseMeta, QueryItem, QueryManagerOptions, RetrieveQueryOptions, UpdateMutationCtx, UpdateMutationOptions, DeleteMutationCtx, DeleteMutationOptions } from '../types';
|
|
5
5
|
/**
|
|
6
6
|
* Comprehensive query manager class that provides hooks and utilities for managing CRUD operations with React Query
|
|
7
7
|
* @template T - The query item type that extends QueryItem
|
|
8
8
|
* @template F - The filter type used for list queries
|
|
9
|
+
* @template M - Optional metadata shape returned alongside list items (e.g. `{ count: number }`). Defaults to `Record<string, unknown>`.
|
|
9
10
|
*
|
|
10
11
|
* @description
|
|
11
12
|
* QueryManager provides a complete solution for managing list and individual item queries with:
|
|
12
13
|
* - Infinite scroll pagination for lists
|
|
14
|
+
* - Support for list response metadata (e.g. total count) via envelope responses
|
|
13
15
|
* - Optimistic updates for create, update, and delete operations
|
|
14
16
|
* - Automatic cache synchronization between list and individual queries
|
|
15
17
|
* - Built-in error handling and rollback mechanisms
|
|
16
18
|
*/
|
|
17
|
-
export declare class QueryManager<T extends QueryItem, F> {
|
|
19
|
+
export declare class QueryManager<T extends QueryItem, F, M extends ListResponseMeta = ListResponseMeta> {
|
|
18
20
|
private options;
|
|
19
21
|
queryClient: QueryClient;
|
|
20
22
|
/**
|
|
21
23
|
* Creates a new QueryManager instance
|
|
22
24
|
* @param options - Configuration options for the query manager
|
|
23
25
|
*/
|
|
24
|
-
constructor(options: QueryManagerOptions<T, F>);
|
|
26
|
+
constructor(options: QueryManagerOptions<T, F, M>);
|
|
25
27
|
/**
|
|
26
28
|
* Gets the name of this query manager
|
|
27
29
|
* @returns The query name used for identification
|
|
@@ -32,7 +34,7 @@ export declare class QueryManager<T extends QueryItem, F> {
|
|
|
32
34
|
* @returns Object containing all configured function handlers
|
|
33
35
|
*/
|
|
34
36
|
get functions(): {
|
|
35
|
-
list: ((limit: number, offset: number, filters: F) => Promise<
|
|
37
|
+
list: ((limit: number, offset: number, filters: F) => Promise<import("..").ListFnResponse<T, M>>) | undefined;
|
|
36
38
|
retrieve: ((id: T["id"]) => Promise<T>) | undefined;
|
|
37
39
|
create: ((data: Partial<T>) => Promise<T>) | undefined;
|
|
38
40
|
update: ((data: Partial<T>) => Promise<T>) | undefined;
|
|
@@ -45,20 +47,33 @@ export declare class QueryManager<T extends QueryItem, F> {
|
|
|
45
47
|
/**
|
|
46
48
|
* React hook for infinite scroll list queries with pagination
|
|
47
49
|
* @param options - Configuration options for the list query
|
|
48
|
-
* @returns Object containing items array, query key, and query object
|
|
50
|
+
* @returns Object containing items array, response metadata, query key, and query object
|
|
51
|
+
*
|
|
52
|
+
* @description
|
|
53
|
+
* When `listFn` returns a `ListPaginationEnvelope` (an object with `listItems` and extra fields),
|
|
54
|
+
* the extra fields are exposed as typed `meta` in the return value. When `listFn` returns a plain
|
|
55
|
+
* array, `meta` is `null`. Meta is stored in the query cache under a companion key, so it
|
|
56
|
+
* survives component remounts and is available even when React Query serves from cache.
|
|
49
57
|
*
|
|
50
58
|
* @example
|
|
51
59
|
* ```typescript
|
|
60
|
+
* // Plain array response (backward compatible)
|
|
52
61
|
* const { items, query } = queryManager.useList({
|
|
53
62
|
* filters: { status: 'active' },
|
|
54
63
|
* limit: 20
|
|
55
64
|
* })
|
|
65
|
+
*
|
|
66
|
+
* // Envelope response with metadata
|
|
67
|
+
* const { items, meta } = queryManager.useList({ filters })
|
|
68
|
+
* console.log(meta?.count) // total item count from the API
|
|
56
69
|
* ```
|
|
57
70
|
*/
|
|
58
|
-
useList(options?: ListQueryOptions<T, F>): {
|
|
71
|
+
useList(options?: ListQueryOptions<T, F, M>): {
|
|
59
72
|
items: T[];
|
|
73
|
+
meta: M | null;
|
|
60
74
|
queryKey: readonly unknown[];
|
|
61
|
-
query: import("@tanstack/react-query").UseInfiniteQueryResult<import("..").ListSelector<T>, Error>;
|
|
75
|
+
query: import("@tanstack/react-query").UseInfiniteQueryResult<import("..").ListSelector<T, M>, Error>;
|
|
76
|
+
listLimit: number;
|
|
62
77
|
};
|
|
63
78
|
/**
|
|
64
79
|
* React hook for retrieving a single item by ID
|
|
@@ -183,6 +198,8 @@ export declare class QueryManager<T extends QueryItem, F> {
|
|
|
183
198
|
*
|
|
184
199
|
* @description
|
|
185
200
|
* Use this method to preload paginated list data that users are likely to need soon.
|
|
201
|
+
* Handles both plain array and envelope responses from `listFn`, but only caches the
|
|
202
|
+
* items — metadata is discarded during prefetch and populated on the first client-side fetch.
|
|
186
203
|
*
|
|
187
204
|
* @example
|
|
188
205
|
* ```typescript
|
|
@@ -192,7 +209,6 @@ export declare class QueryManager<T extends QueryItem, F> {
|
|
|
192
209
|
* { staleTime: 5 * 60 * 1000 }
|
|
193
210
|
* )
|
|
194
211
|
* }
|
|
195
|
-
*
|
|
196
212
|
* ```
|
|
197
213
|
*/
|
|
198
214
|
prefetchList(filters?: F, options?: Omit<FetchInfiniteQueryOptions<ListPaginationResponse<T>, Error, ListPaginationResponse<T>, QueryKey, number>, 'queryKey' | 'queryFn' | 'initialPageParam'> & {
|
|
@@ -1 +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;
|
|
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,EAAE,gBAAgB,EAAa,SAAS,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAA;AAKpR;;;;;;;;;;;;;GAaG;AACH,qBAAa,YAAY,CAAC,C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