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

stacklok / codegate-ui / 13158206369

05 Feb 2025 01:15PM UTC coverage: 69.865% (+0.04%) from 69.83%
13158206369

Pull #257

github

web-flow
Merge 2dc14634c into 22c47aed9
Pull Request #257: feat: react-query-utils

379 of 604 branches covered (62.75%)

Branch coverage included in aggregate %.

3 of 6 new or added lines in 2 files covered. (50.0%)

815 of 1105 relevant lines covered (73.76%)

76.99 hits per line

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

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

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

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

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

26
/**
27
 * Takes a queryClient, and an array of queryKeyFns, and invalidates all queries
28
 * in the cache with matching query keys.
29
 */
30
export function invalidateQueries(
31
  queryClient: QueryClient,
32
  queryKeyFns: QueryKeyFn[],
33
) {
34
  return queryClient.invalidateQueries({
1✔
35
    refetchType: "all",
36
    predicate: (
37
      query: Query<unknown, Error, unknown, OpenApiTsReactQueryKey>,
38
    ) => {
NEW
39
      return queryKeyFns.some(
×
NEW
40
        (fn) => query.queryKey[0]._id === getQueryKeyFnId(fn),
×
41
      );
42
    },
43
  });
44
}
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