@codeleap/query 5.8.7 → 5.8.8
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/package.json +4 -4
- package/package.json.bak +1 -1
- package/src/lib/QueryKeys.ts +40 -8
- package/src/types/retrieve.ts +5 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codeleap/query",
|
|
3
|
-
"version": "5.8.
|
|
3
|
+
"version": "5.8.8",
|
|
4
4
|
"main": "src/index.ts",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"repository": {
|
|
@@ -9,15 +9,15 @@
|
|
|
9
9
|
"directory": "packages/query"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
|
-
"@codeleap/config": "5.8.
|
|
13
|
-
"@codeleap/types": "5.8.
|
|
12
|
+
"@codeleap/config": "5.8.8",
|
|
13
|
+
"@codeleap/types": "5.8.8",
|
|
14
14
|
"ts-node-dev": "1.1.8"
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
17
|
"build": "echo 'No build needed'"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"@codeleap/types": "5.8.
|
|
20
|
+
"@codeleap/types": "5.8.8",
|
|
21
21
|
"typescript": "5.5.2",
|
|
22
22
|
"@tanstack/react-query": "5.89.0"
|
|
23
23
|
},
|
package/package.json.bak
CHANGED
package/src/lib/QueryKeys.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { CancelOptions, InfiniteData, InvalidateOptions, InvalidateQueryFilters, Query, QueryFilters, QueryKey, RefetchOptions, RefetchQueryFilters } from '@tanstack/react-query'
|
|
2
2
|
import { useMemo } from 'react'
|
|
3
3
|
import { TypeGuards } from '@codeleap/types'
|
|
4
|
-
import { ListPaginationResponse, ListSelector, PageParam, QueryClient, QueryItem } from '../types'
|
|
4
|
+
import { ListPaginationResponse, ListSelector, PageParam, QueryClient, QueryItem, RetrieveDataOptions } from '../types'
|
|
5
5
|
import deepEqual from 'fast-deep-equal'
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -272,25 +272,57 @@ export class QueryKeys<T extends QueryItem, F> {
|
|
|
272
272
|
}
|
|
273
273
|
|
|
274
274
|
/**
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
275
|
+
* Retrieves item data from cache with intelligent fallback strategies
|
|
276
|
+
*
|
|
277
|
+
* @description Searches for an item using a multi-layered approach:
|
|
278
|
+
* 1. Direct cache lookup using retrieve query
|
|
279
|
+
* 2. Fallback to itemMap from list data (shallow search)
|
|
280
|
+
* 3. Deep search through all paginated list queries
|
|
281
|
+
*
|
|
282
|
+
* @param {QueryItem['id']} id - The unique identifier of the item to retrieve
|
|
283
|
+
* @param {RetrieveDataOptions} [options] - Configuration options for retrieval behavior
|
|
284
|
+
* @param {boolean} [options.onlyQueryData=false] - If true, only returns data from the specific retrieve query cache, ignoring list data fallbacks
|
|
285
|
+
* @param {boolean} [options.deepSearch=true] - If true, performs deep search through paginated queries when item not found in direct cache or itemMap
|
|
286
|
+
*
|
|
287
|
+
* @returns Item | undefined
|
|
279
288
|
*/
|
|
280
|
-
getRetrieveData(id: QueryItem['id'],
|
|
289
|
+
getRetrieveData(id: QueryItem['id'], options: RetrieveDataOptions = {}): T | undefined {
|
|
290
|
+
const {
|
|
291
|
+
onlyQueryData = false,
|
|
292
|
+
deepSearch = false,
|
|
293
|
+
} = options
|
|
294
|
+
|
|
281
295
|
if (TypeGuards.isNil(id)) return undefined
|
|
282
296
|
|
|
283
297
|
const queryKey = this.keys.retrieve(id)
|
|
284
298
|
|
|
285
299
|
const queryData = this.queryClient.getQueryData<T>(queryKey)
|
|
286
300
|
|
|
287
|
-
if (
|
|
301
|
+
if (queryData?.id) return queryData
|
|
302
|
+
|
|
303
|
+
if (onlyQueryData) return undefined
|
|
304
|
+
|
|
305
|
+
if (!deepSearch) {
|
|
288
306
|
const { itemMap } = this.getListData()
|
|
289
307
|
|
|
290
308
|
return itemMap?.[id]
|
|
291
309
|
}
|
|
292
310
|
|
|
293
|
-
|
|
311
|
+
const queries = this.getAllListQueries()
|
|
312
|
+
|
|
313
|
+
for (const query of queries) {
|
|
314
|
+
const pages = query.state.data?.pages
|
|
315
|
+
if (!pages?.length) continue
|
|
316
|
+
|
|
317
|
+
const item = pages
|
|
318
|
+
.filter(Boolean)
|
|
319
|
+
.flatMap(page => Array.isArray(page) ? page : [])
|
|
320
|
+
.find(item => item?.id === id)
|
|
321
|
+
|
|
322
|
+
if (item) return item
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return undefined
|
|
294
326
|
}
|
|
295
327
|
|
|
296
328
|
/**
|
package/src/types/retrieve.ts
CHANGED