• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stacklok / codegate-ui / 13160116517

05 Feb 2025 02:54PM UTC coverage: 69.459% (-0.4%) from 69.865%
13160116517

Pull #265

github

web-flow
Merge a3f152627 into eb46c5b85
Pull Request #265: fix: implement default caching options for react-query

379 of 608 branches covered (62.34%)

Branch coverage included in aggregate %.

0 of 7 new or added lines in 2 files covered. (0.0%)

815 of 1111 relevant lines covered (73.36%)

76.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

14.29
/src/lib/react-query-utils.ts
1
import { OpenApiTsReactQueryKey } from "@/types/openapi-ts";
2
import { OptionsLegacyParser } from "@hey-api/client-fetch";
3
import {
4
  Query,
5
  QueryClient,
6
  QueryObserverOptions,
7
} from "@tanstack/react-query";
8

9
// NOTE: This is copy/pasted from @/api/generated/@tanstack/react-query.gen
10
type QueryKey<TOptions extends OptionsLegacyParser = OptionsLegacyParser> = [
11
  Pick<TOptions, "baseUrl" | "body" | "headers" | "path" | "query"> & {
12
    _id: string;
13
    _infinite?: boolean;
14
  },
15
];
16

17
// A generic type that describes the possible permutations of openapi-ts
18
// react-query queryKey functions. The use of `any` is required to make it
19
// generic enough for use with any openapi-ts queryKey fn. The return type
20
// constraint is sufficiently strict that it is still safe to use.
21
// eslint-disable-next-line @typescript-eslint/no-explicit-any
22
type QueryKeyFn = (options: any) => QueryKey[0][];
23

24
// NOTE: The type constraints on `queryKeyFn` are sufficiently strict that we
25
// can use type assertion for the return type with relative safety.
26
const getQueryKeyFnId = <T extends OptionsLegacyParser>(
2✔
27
  queryKeyFn: QueryKeyFn,
28
) => queryKeyFn({} as T)[0]?._id as string;
×
29

30
/**
31
 * Takes a queryClient, and an array of queryKeyFns, and invalidates all queries
32
 * in the cache with matching query keys.
33
 */
34
export function invalidateQueries(
35
  queryClient: QueryClient,
36
  queryKeyFns: QueryKeyFn[],
37
) {
38
  return queryClient.invalidateQueries({
1✔
39
    refetchType: "all",
40
    stale: true,
41
    predicate: (
42
      query: Query<unknown, Error, unknown, OpenApiTsReactQueryKey>,
43
    ) => {
44
      return queryKeyFns.some(
×
45
        (fn) => query.queryKey[0]._id === getQueryKeyFnId(fn),
×
46
      );
47
    },
48
  });
49
}
50

51
export function getQueryCacheConfig(
52
  lifetime: "dynamic" | "short" | "indefinite",
53
) {
NEW
54
  switch (lifetime) {
×
55
    case "dynamic":
NEW
56
      return {
×
57
        staleTime: 0,
58
      } as const satisfies Pick<QueryObserverOptions, "staleTime">;
59

60
    case "short":
NEW
61
      return {
×
62
        staleTime: 5 * 1_000,
63
      } as const satisfies Pick<QueryObserverOptions, "staleTime">;
64

65
    case "indefinite":
NEW
66
      return {
×
67
        staleTime: Infinity,
68
      } as const satisfies Pick<QueryObserverOptions, "staleTime">;
69

70
    default:
NEW
71
      return lifetime satisfies never;
×
72
  }
73
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc