-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvalidators.ts
More file actions
32 lines (28 loc) · 930 Bytes
/
invalidators.ts
File metadata and controls
32 lines (28 loc) · 930 Bytes
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
import type { Query } from "@tanstack/react-query";
import { dequal } from "dequal";
import { createUrlWithParams } from "./createUrlWithParams";
export const invalidateByTags = (tags: readonly string[]) => (query: Query) =>
tags.every((tag) => query.queryKey.includes(tag));
type Service<
TArgs extends {
urlParams?: Record<string, string>;
},
TResult,
> = {
url: string;
call: (args: TArgs) => Promise<TResult>;
};
export const invalidateByUrlParams =
<TArgs extends { urlParams: Record<string, string> }, TResult>(
_: Service<TArgs, TResult>,
urlParams: TArgs["urlParams"],
) =>
(query: Query) =>
query.queryKey.some((qk) => dequal(qk, urlParams));
export const invalidateByUrl =
<TArgs extends { urlParams: Record<string, string> }, TResult>(
{ url }: Service<TArgs, TResult>,
urlParams: TArgs["urlParams"],
) =>
(query: Query) =>
query.queryKey[0] === createUrlWithParams(url, urlParams || {});