-
Notifications
You must be signed in to change notification settings - Fork 2
/
ReactQueryManager.ts
152 lines (130 loc) · 4.01 KB
/
ReactQueryManager.ts
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import {
QueryFunctionContext,
useInfiniteQuery,
useMutation,
useQuery,
useQueryClient,
} from "@tanstack/react-query";
import { ServerStateManager, ApiServiceInterface } from "../interface";
import type { ResponseType, ErrorType } from "../type/httpTypes";
import type {
QueryKeyT,
MutationProps,
QueryProps,
GetInfinitePagesInterface,
InfinitePagesProps,
} from "../type/ReactQueryManager";
export class ReactQueryManager implements ServerStateManager {
private apiService: ApiServiceInterface;
constructor(apiService: ApiServiceInterface) {
this.apiService = apiService;
}
private async minimumLoadingTime<T>(
dataPromise: Promise<T>,
minimumTime = 400,
): Promise<T> {
const timerPromise = new Promise<void>(resolve =>
setTimeout(resolve, minimumTime),
);
await Promise.all([dataPromise, timerPromise]);
return dataPromise;
}
private fetcher<T>({
queryKey,
pageParam,
}: QueryFunctionContext<QueryKeyT>): Promise<T> {
const [url, params] = queryKey;
return this.apiService.get<T>(url, {
params: { ...params, page: pageParam },
});
}
fetch<T>(props: QueryProps<T>) {
const { url, params, config } = props;
return useQuery<T, Error, T, QueryKeyT>({
queryKey: [url!, params],
queryFn: (context: QueryFunctionContext<QueryKeyT>) => {
const dataPromise = this.fetcher<T>(context);
return this.minimumLoadingTime(dataPromise);
},
enabled: !!url,
...config,
});
}
preFetch<T>(props: Omit<QueryProps<T>, "config">) {
const { url, params } = props;
const queryClient = useQueryClient();
return () => {
if (!url) {
return;
}
queryClient.prefetchQuery<T, Error, T, QueryKeyT>({
queryKey: [url!, params],
queryFn: (context: QueryFunctionContext<QueryKeyT>) =>
this.fetcher(context),
});
};
}
loadMore<T>(props: InfinitePagesProps<T>) {
const { url, params, config } = props;
return useInfiniteQuery<
GetInfinitePagesInterface<T>,
ErrorType,
GetInfinitePagesInterface<T>,
QueryKeyT
>({
queryKey: [url!, params],
queryFn: (context: QueryFunctionContext<QueryKeyT>) => {
context.pageParam = context.pageParam || 1;
return this.fetcher(context);
},
getPreviousPageParam: firstPage => firstPage.previousId ?? false,
getNextPageParam: lastPage => lastPage.nextId ?? false,
...config,
});
}
private genericMutation<T, S = unknown>(props: MutationProps<T, S>) {
const { func, url, params, updater, config } = props;
const queryClient = useQueryClient();
return useMutation<ResponseType<S>, ErrorType, T | S>({
mutationFn: func,
onMutate: async data => {
await queryClient.cancelQueries([url!, params]);
const previousData = queryClient.getQueryData([url!, params]);
queryClient.setQueryData<T>([url!, params], oldData => {
if (oldData === undefined) return data as T;
return updater ? updater(oldData!, data as S) : (data as T);
});
return previousData;
},
onError: (_err, _, context) => {
queryClient.setQueryData([url!, params], context);
},
onSettled: () => {
queryClient.invalidateQueries([url!, params]);
},
...config,
});
}
post<T, S>(props: MutationProps<T, S>) {
const { url, params } = props;
return this.genericMutation<T, S>({
func: data => this.apiService.post<T, S>(url, data, params),
...props,
});
}
update<T, S>(props: MutationProps<T, S>) {
const { url, params } = props;
return this.genericMutation<T, S>({
func: data => this.apiService.patch<T, S>(url, data, params),
...props,
});
}
delete<T>(props: MutationProps<T>) {
const { url, params } = props;
return this.genericMutation<T>({
func: () => this.apiService.delete<T>(url, params),
...props,
});
}
}