All files / src/config queryKeys.ts

65% Statements 13/20
100% Branches 0/0
61.11% Functions 11/18
68.42% Lines 13/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85                                        63x   329x 13x         327x 463x 463x     583x 203x       12x 8x 10x 10x                                         63x                                            
// src/config/queryKeys.ts
/**
 * Centralized query keys for TanStack Query.
 *
 * This file provides a single source of truth for all query keys used
 * throughout the application. Using these factory functions ensures
 * consistent key naming and proper cache invalidation.
 *
 * @example
 * ```tsx
 * // In a query hook
 * useQuery({
 *   queryKey: queryKeys.flyers(10, 0),
 *   queryFn: fetchFlyers,
 * });
 *
 * // For cache invalidation
 * queryClient.invalidateQueries({ queryKey: queryKeys.watchedItems() });
 * ```
 */
export const queryKeys = {
  // User Features
  flyers: (limit: number, offset: number) => ['flyers', { limit, offset }] as const,
  flyerItems: (flyerId: number) => ['flyer-items', flyerId] as const,
  flyerItemsBatch: (flyerIds: number[]) =>
    ['flyer-items-batch', flyerIds.sort().join(',')] as const,
  flyerItemsCount: (flyerIds: number[]) =>
    ['flyer-items-count', flyerIds.sort().join(',')] as const,
  masterItems: () => ['master-items'] as const,
  watchedItems: () => ['watched-items'] as const,
  shoppingLists: () => ['shopping-lists'] as const,
 
  // Auth & Profile
  authProfile: () => ['auth-profile'] as const,
  userAddress: (addressId: number | null) => ['user-address', addressId] as const,
  userProfileData: () => ['user-profile-data'] as const,
 
  // Admin Features
  activityLog: (limit: number, offset: number) => ['activity-log', { limit, offset }] as const,
  applicationStats: () => ['application-stats'] as const,
  suggestedCorrections: () => ['suggested-corrections'] as const,
  categories: () => ['categories'] as const,
 
  // Analytics
  bestSalePrices: () => ['best-sale-prices'] as const,
  priceHistory: (masterItemIds: number[]) =>
    ['price-history', [...masterItemIds].sort((a, b) => a - b).join(',')] as const,
  leaderboard: (limit: number) => ['leaderboard', limit] as const,
} as const;
 
/**
 * Base keys for partial matching in cache invalidation.
 *
 * Use these when you need to invalidate all queries of a certain type
 * regardless of their parameters.
 *
 * @example
 * ```tsx
 * // Invalidate all flyer-related queries
 * queryClient.invalidateQueries({ queryKey: queryKeyBases.flyers });
 * ```
 */
export const queryKeyBases = {
  flyers: ['flyers'] as const,
  flyerItems: ['flyer-items'] as const,
  flyerItemsBatch: ['flyer-items-batch'] as const,
  flyerItemsCount: ['flyer-items-count'] as const,
  masterItems: ['master-items'] as const,
  watchedItems: ['watched-items'] as const,
  shoppingLists: ['shopping-lists'] as const,
  authProfile: ['auth-profile'] as const,
  userAddress: ['user-address'] as const,
  userProfileData: ['user-profile-data'] as const,
  activityLog: ['activity-log'] as const,
  applicationStats: ['application-stats'] as const,
  suggestedCorrections: ['suggested-corrections'] as const,
  categories: ['categories'] as const,
  bestSalePrices: ['best-sale-prices'] as const,
  priceHistory: ['price-history'] as const,
  leaderboard: ['leaderboard'] as const,
} as const;
 
export type QueryKeys = typeof queryKeys;
export type QueryKeyBases = typeof queryKeyBases;