@codeleap/query 5.8.6 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeleap/query",
3
- "version": "5.8.6",
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.6",
13
- "@codeleap/types": "5.8.6",
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.6",
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codeleap/query",
3
- "version": "5.8.6",
3
+ "version": "5.8.8",
4
4
  "main": "src/index.ts",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -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
- * Gets retrieve data from cache, with fallback to list data
276
- * @param id - The ID of the item to retrieve
277
- * @param onlyQueryData - If true, only returns data from the specific retrieve query, not from list data
278
- * @returns The item data or undefined if not found
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'], onlyQueryData = false): T | undefined {
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 (!queryData?.id && !onlyQueryData) {
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
- return queryData
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
  /**
@@ -5,3 +5,8 @@ export type RetrieveQueryOptions<T extends QueryItem> = Omit<
5
5
  UndefinedInitialDataOptions<T, Error, T, QueryKey>,
6
6
  'queryKey' | 'queryFn'
7
7
  >
8
+
9
+ export type RetrieveDataOptions = {
10
+ onlyQueryData?: boolean
11
+ deepSearch?: boolean
12
+ }