@ventlio/tanstack-query 0.3.9 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.mjs +51 -40
- package/dist/index.mjs.map +1 -1
- package/dist/model/useKeyTrackerModel.d.ts +1 -1
- package/dist/model/useQueryModel.d.ts +1 -1
- package/dist/model/useQueryModel.js +6 -6
- package/dist/model/useRefetchQuery.d.ts +1 -1
- package/dist/model/useRefetchQuery.js +1 -1
- package/dist/queries/useDeleteRequest.d.ts +6 -66
- package/dist/queries/useDeleteRequest.js +7 -3
- package/dist/queries/useDeleteRequest.js.map +1 -1
- package/dist/queries/useGetInfiniteRequest.d.ts +25 -109
- package/dist/queries/useGetInfiniteRequest.js +3 -2
- package/dist/queries/useGetInfiniteRequest.js.map +1 -1
- package/dist/queries/useGetRequest.d.ts +6 -74
- package/dist/queries/useGetRequest.js +3 -2
- package/dist/queries/useGetRequest.js.map +1 -1
- package/dist/queries/usePatchRequest.d.ts +21 -13
- package/dist/queries/usePatchRequest.js +7 -4
- package/dist/queries/usePatchRequest.js.map +1 -1
- package/dist/queries/usePostRequest.d.ts +30 -25
- package/dist/queries/usePostRequest.js +3 -2
- package/dist/queries/usePostRequest.js.map +1 -1
- package/dist/request/make-request.js +21 -20
- package/dist/request/make-request.js.map +1 -1
- package/package.json +2 -2
- package/src/model/useKeyTrackerModel.ts +2 -2
- package/src/model/useQueryModel.ts +7 -7
- package/src/model/useRefetchQuery.ts +1 -1
- package/src/queries/useDeleteRequest.ts +7 -7
- package/src/queries/useGetInfiniteRequest.ts +14 -19
- package/src/queries/useGetRequest.ts +6 -9
- package/src/queries/usePatchRequest.ts +5 -5
- package/src/queries/usePostRequest.ts +4 -2
- package/src/request/make-request.ts +22 -18
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { QueryKey, UseQueryOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useInfiniteQuery, useQueryClient } from '@tanstack/react-query';
|
|
3
3
|
import { startTransition, useEffect, useMemo, useState } from 'react';
|
|
4
4
|
import { useEnvironmentVariables, useQueryConfig } from '../config';
|
|
@@ -110,19 +110,17 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
110
110
|
return pathname + '?' + queryParams.toString();
|
|
111
111
|
};
|
|
112
112
|
|
|
113
|
-
const query = useInfiniteQuery<any, any, IRequestSuccess<TResponse & { pagination: Pagination }>>(
|
|
114
|
-
[requestPath, {}],
|
|
115
|
-
({ pageParam = requestPath, queryKey }) =>
|
|
113
|
+
const query = useInfiniteQuery<any, any, IRequestSuccess<TResponse & { pagination: Pagination }>>({
|
|
114
|
+
queryKey: [requestPath, {}],
|
|
115
|
+
queryFn: ({ pageParam = requestPath, queryKey }) =>
|
|
116
116
|
new Promise<IRequestSuccess<TResponse & { pagination: Pagination }> | IRequestError>((res, rej) =>
|
|
117
|
-
sendRequest(res, rej, queryKey, pageParam)
|
|
117
|
+
sendRequest(res, rej, queryKey, pageParam as string)
|
|
118
118
|
),
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
);
|
|
119
|
+
enabled: load && !isFutureQueriesPaused,
|
|
120
|
+
getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
|
|
121
|
+
getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
|
|
122
|
+
...options,
|
|
123
|
+
});
|
|
126
124
|
|
|
127
125
|
const setOptionsAsync = async (fetchOptions: any) => {
|
|
128
126
|
startTransition(() => {
|
|
@@ -139,12 +137,10 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
139
137
|
Array<any>
|
|
140
138
|
>
|
|
141
139
|
): Promise<
|
|
142
|
-
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
>
|
|
140
|
+
| IRequestSuccess<
|
|
141
|
+
TResponse & {
|
|
142
|
+
pagination: Pagination;
|
|
143
|
+
}
|
|
148
144
|
>
|
|
149
145
|
| undefined
|
|
150
146
|
> => {
|
|
@@ -177,7 +173,6 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
177
173
|
if (keyTracker) {
|
|
178
174
|
// set expiration time for the tracker
|
|
179
175
|
queryClient.setQueryDefaults([keyTracker], {
|
|
180
|
-
cacheTime: Infinity,
|
|
181
176
|
staleTime: Infinity,
|
|
182
177
|
});
|
|
183
178
|
|
|
@@ -78,15 +78,13 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
|
|
|
78
78
|
}
|
|
79
79
|
};
|
|
80
80
|
|
|
81
|
-
const query = useQuery<any, any, IRequestSuccess<TResponse>>(
|
|
82
|
-
[requestPath, {}],
|
|
83
|
-
({ queryKey }) =>
|
|
81
|
+
const query = useQuery<any, any, IRequestSuccess<TResponse>>({
|
|
82
|
+
queryKey: [requestPath, {}],
|
|
83
|
+
queryFn: ({ queryKey }) =>
|
|
84
84
|
new Promise<IRequestSuccess<TResponse> | IRequestError>((res, rej) => sendRequest(res, rej, queryKey)),
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
);
|
|
85
|
+
enabled: load && !isFutureQueriesPaused,
|
|
86
|
+
...options,
|
|
87
|
+
});
|
|
90
88
|
|
|
91
89
|
useEffect(() => {
|
|
92
90
|
if (path) {
|
|
@@ -98,7 +96,6 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
|
|
|
98
96
|
if (keyTracker) {
|
|
99
97
|
// set expiration time for the tracker
|
|
100
98
|
queryClient.setQueryDefaults([keyTracker], {
|
|
101
|
-
cacheTime: Infinity,
|
|
102
99
|
staleTime: Infinity,
|
|
103
100
|
});
|
|
104
101
|
|
|
@@ -63,13 +63,13 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
|
|
|
63
63
|
};
|
|
64
64
|
|
|
65
65
|
// register post mutation
|
|
66
|
-
const mutation = useMutation<IRequestSuccess<TResponse>, IRequestError>(
|
|
67
|
-
(dataData: any) =>
|
|
66
|
+
const mutation = useMutation<IRequestSuccess<TResponse>, IRequestError>({
|
|
67
|
+
mutationFn: (dataData: any) =>
|
|
68
68
|
new Promise<IRequestSuccess<TResponse>>((res, rej) => {
|
|
69
69
|
return sendRequest(res, rej, dataData);
|
|
70
70
|
}),
|
|
71
|
-
|
|
72
|
-
);
|
|
71
|
+
mutationKey: [path, { type: 'mutation' }],
|
|
72
|
+
});
|
|
73
73
|
|
|
74
74
|
const patch = async (
|
|
75
75
|
data: any,
|
|
@@ -91,5 +91,5 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
|
|
|
91
91
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
92
92
|
}, [isFutureMutationsPaused]);
|
|
93
93
|
|
|
94
|
-
return { patch, uploadProgressPercent, ...mutation, isLoading: mutation.
|
|
94
|
+
return { patch, uploadProgressPercent, ...mutation, isLoading: mutation.isPending || isFutureMutationsPaused };
|
|
95
95
|
};
|
|
@@ -95,7 +95,9 @@ export const usePostRequest = <TResponse>({
|
|
|
95
95
|
IRequestSuccess<TResponse>,
|
|
96
96
|
IRequestError,
|
|
97
97
|
{ data: any; requestConfig?: Partial<Omit<IMakeRequest, 'body'>> }
|
|
98
|
-
>(
|
|
98
|
+
>({
|
|
99
|
+
mutationFn: async (postData) =>
|
|
100
|
+
new Promise<IRequestSuccess<TResponse>>((res, rej) => sendRequest(res, rej, postData)),
|
|
99
101
|
mutationKey: [path, { type: 'mutation' }],
|
|
100
102
|
});
|
|
101
103
|
|
|
@@ -129,5 +131,5 @@ export const usePostRequest = <TResponse>({
|
|
|
129
131
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
130
132
|
}, [isFutureMutationsPaused]);
|
|
131
133
|
|
|
132
|
-
return { post, uploadProgressPercent, ...mutation, isLoading: mutation.
|
|
134
|
+
return { post, uploadProgressPercent, ...mutation, isLoading: mutation.isPending || isFutureMutationsPaused };
|
|
133
135
|
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { AxiosRequestConfig } from 'axios';
|
|
1
2
|
import axios from 'axios';
|
|
2
3
|
import { axiosInstance } from './axios-instance';
|
|
3
4
|
|
|
@@ -27,35 +28,38 @@ export async function makeRequest<TResponse>({
|
|
|
27
28
|
// configure request header1
|
|
28
29
|
if (!isFormData) {
|
|
29
30
|
headers['Content-Type'] = ContentType.APPLICATION_JSON;
|
|
30
|
-
} else {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
body.append(fileKey, innerFile);
|
|
39
|
-
}
|
|
40
|
-
} else {
|
|
41
|
-
body.append(fileKey, currentFile);
|
|
31
|
+
} else if (isApp) {
|
|
32
|
+
headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
|
|
33
|
+
// add the app files
|
|
34
|
+
for (const fileKey in appFiles) {
|
|
35
|
+
const currentFile = appFiles[fileKey];
|
|
36
|
+
if (Array.isArray(currentFile)) {
|
|
37
|
+
for (const innerFile of currentFile) {
|
|
38
|
+
body.append(fileKey, innerFile);
|
|
42
39
|
}
|
|
40
|
+
} else {
|
|
41
|
+
body.append(fileKey, currentFile);
|
|
43
42
|
}
|
|
44
|
-
} else {
|
|
45
|
-
delete headers['Content-Type'];
|
|
46
43
|
}
|
|
44
|
+
} else {
|
|
45
|
+
delete headers['Content-Type'];
|
|
47
46
|
}
|
|
48
47
|
|
|
49
48
|
try {
|
|
50
49
|
const axiosRequest = axiosInstance({ baseURL, headers, timeout });
|
|
51
50
|
|
|
52
|
-
|
|
53
|
-
const resp = await axiosRequest({
|
|
51
|
+
const axiosRequestConfig: AxiosRequestConfig<Record<string, any>> = {
|
|
54
52
|
url: path,
|
|
55
53
|
method,
|
|
56
|
-
data: body,
|
|
57
54
|
onUploadProgress,
|
|
58
|
-
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
if (Object.keys(body).length > 0) {
|
|
58
|
+
axiosRequestConfig.data = body;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// send request
|
|
62
|
+
const resp = await axiosRequest(axiosRequestConfig);
|
|
59
63
|
|
|
60
64
|
// get response json
|
|
61
65
|
const jsonResp: any = await resp.data;
|