@ventlio/tanstack-query 0.2.77 → 0.2.79
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 +30 -9
- package/dist/index.mjs.map +1 -1
- package/dist/queries/useGetInfiniteRequest.d.ts +20 -7
- package/dist/queries/useGetInfiniteRequest.js +25 -7
- package/dist/queries/useGetInfiniteRequest.js.map +1 -1
- package/dist/queries/usePostRequest.d.ts +65 -17
- package/dist/queries/usePostRequest.js +6 -3
- package/dist/queries/usePostRequest.js.map +1 -1
- package/package.json +1 -1
- package/src/queries/useGetInfiniteRequest.ts +49 -8
- package/src/queries/usePostRequest.ts +29 -10
package/dist/index.mjs
CHANGED
|
@@ -436,6 +436,8 @@ const useDeleteRequest = (deleteOptions) => {
|
|
|
436
436
|
const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl, headers, }) => {
|
|
437
437
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
438
438
|
const { getHeaders } = useQueryHeaders();
|
|
439
|
+
const [requestPath, updatePath] = useState(path);
|
|
440
|
+
const [options, setOptions] = useState(queryOptions);
|
|
439
441
|
let queryClient = useQueryClient();
|
|
440
442
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
441
443
|
queryClient = useMemo(() => queryClient, []);
|
|
@@ -444,7 +446,7 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
444
446
|
// get request headers
|
|
445
447
|
const globalHeaders = getHeaders();
|
|
446
448
|
const getResponse = await makeRequest({
|
|
447
|
-
path: pageParam ??
|
|
449
|
+
path: pageParam ?? requestPath,
|
|
448
450
|
headers: { ...globalHeaders, ...headers },
|
|
449
451
|
baseURL: baseUrl ?? API_URL,
|
|
450
452
|
timeout: TIMEOUT,
|
|
@@ -465,18 +467,33 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
465
467
|
* This pagination implementation is currently tied to our use case
|
|
466
468
|
*/
|
|
467
469
|
const constructPaginationLink = (direction, lastPage) => {
|
|
468
|
-
const [pathname, queryString] =
|
|
470
|
+
const [pathname, queryString] = requestPath.split('?');
|
|
469
471
|
const queryParams = new URLSearchParams(queryString);
|
|
470
472
|
const lastPageItem = lastPage.data.pagination[direction];
|
|
471
473
|
queryParams.set('page', String(lastPageItem));
|
|
472
474
|
return pathname + '?' + queryParams.toString();
|
|
473
475
|
};
|
|
474
|
-
const query = useInfiniteQuery([
|
|
476
|
+
const query = useInfiniteQuery([requestPath, {}], ({ pageParam = requestPath }) => new Promise((res, rej) => sendRequest(res, rej, pageParam)), {
|
|
475
477
|
enabled: load,
|
|
476
478
|
getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
|
|
477
479
|
getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
|
|
478
|
-
...
|
|
480
|
+
...options,
|
|
479
481
|
});
|
|
482
|
+
const setOptionsAsync = async (fetchOptions) => {
|
|
483
|
+
startTransition(() => {
|
|
484
|
+
setOptions(fetchOptions);
|
|
485
|
+
});
|
|
486
|
+
};
|
|
487
|
+
const get = async (link, fetchOptions) => {
|
|
488
|
+
await setOptionsAsync(fetchOptions);
|
|
489
|
+
await updatedPathAsync(link);
|
|
490
|
+
return query.data;
|
|
491
|
+
};
|
|
492
|
+
const updatedPathAsync = async (link) => {
|
|
493
|
+
startTransition(() => {
|
|
494
|
+
updatePath(link);
|
|
495
|
+
});
|
|
496
|
+
};
|
|
480
497
|
useEffect(() => {
|
|
481
498
|
if (keyTracker) {
|
|
482
499
|
// set expiration time for the tracker
|
|
@@ -484,10 +501,11 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
484
501
|
cacheTime: Infinity,
|
|
485
502
|
staleTime: Infinity,
|
|
486
503
|
});
|
|
487
|
-
queryClient.setQueryData([keyTracker], [
|
|
504
|
+
queryClient.setQueryData([keyTracker], [requestPath, {}]);
|
|
488
505
|
}
|
|
489
|
-
}, [keyTracker,
|
|
506
|
+
}, [keyTracker, requestPath, queryClient, queryOptions?.staleTime]);
|
|
490
507
|
return {
|
|
508
|
+
get,
|
|
491
509
|
...query,
|
|
492
510
|
};
|
|
493
511
|
};
|
|
@@ -663,9 +681,11 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
663
681
|
// get request headers
|
|
664
682
|
const globalHeaders = getHeaders();
|
|
665
683
|
const config = queryClient.getQueryData(['config']);
|
|
684
|
+
const { data, requestConfig } = postData;
|
|
685
|
+
delete requestConfig?.body;
|
|
666
686
|
const postResponse = await makeRequest({
|
|
667
687
|
path,
|
|
668
|
-
body:
|
|
688
|
+
body: data,
|
|
669
689
|
method: HttpMethod.POST,
|
|
670
690
|
isFormData,
|
|
671
691
|
headers: { ...globalHeaders, ...headers },
|
|
@@ -676,6 +696,7 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
676
696
|
fileSelectors,
|
|
677
697
|
},
|
|
678
698
|
onUploadProgress,
|
|
699
|
+
...requestConfig,
|
|
679
700
|
});
|
|
680
701
|
if (postResponse.status) {
|
|
681
702
|
// scroll to top after success
|
|
@@ -694,8 +715,8 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
694
715
|
};
|
|
695
716
|
// register post mutation
|
|
696
717
|
const mutation = useMutation(async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)));
|
|
697
|
-
const post = async (data, options) => {
|
|
698
|
-
return mutation.mutateAsync(data, options);
|
|
718
|
+
const post = async (data, options, requestConfig) => {
|
|
719
|
+
return mutation.mutateAsync({ data, requestConfig }, options);
|
|
699
720
|
};
|
|
700
721
|
return { post, uploadProgressPercent, ...mutation };
|
|
701
722
|
};
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { InfiniteData, UseQueryOptions } from '@tanstack/react-query';
|
|
2
|
+
import type { IRequestError, IRequestSuccess } from '../request';
|
|
2
3
|
import type { DefaultRequestOptions, TanstackInfiniteQueryOption } from './queries.interface';
|
|
3
4
|
interface Pagination {
|
|
4
5
|
previous_page: number;
|
|
@@ -48,11 +49,14 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
48
49
|
isPreviousData: boolean;
|
|
49
50
|
isRefetching: boolean;
|
|
50
51
|
isStale: boolean;
|
|
51
|
-
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<
|
|
52
|
+
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<InfiniteData<IRequestSuccess<TResponse & {
|
|
52
53
|
pagination: Pagination;
|
|
53
54
|
}>>, any>>;
|
|
54
55
|
remove: () => void;
|
|
55
56
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
57
|
+
get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<InfiniteData<IRequestSuccess<TResponse & {
|
|
58
|
+
pagination: Pagination;
|
|
59
|
+
}>> | undefined>;
|
|
56
60
|
} | {
|
|
57
61
|
data: undefined;
|
|
58
62
|
error: null;
|
|
@@ -86,13 +90,16 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
86
90
|
isPreviousData: boolean;
|
|
87
91
|
isRefetching: boolean;
|
|
88
92
|
isStale: boolean;
|
|
89
|
-
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<
|
|
93
|
+
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<InfiniteData<IRequestSuccess<TResponse & {
|
|
90
94
|
pagination: Pagination;
|
|
91
95
|
}>>, any>>;
|
|
92
96
|
remove: () => void;
|
|
93
97
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
98
|
+
get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<InfiniteData<IRequestSuccess<TResponse & {
|
|
99
|
+
pagination: Pagination;
|
|
100
|
+
}>> | undefined>;
|
|
94
101
|
} | {
|
|
95
|
-
data:
|
|
102
|
+
data: InfiniteData<IRequestSuccess<TResponse & {
|
|
96
103
|
pagination: Pagination;
|
|
97
104
|
}>>;
|
|
98
105
|
error: any;
|
|
@@ -126,13 +133,16 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
126
133
|
isPreviousData: boolean;
|
|
127
134
|
isRefetching: boolean;
|
|
128
135
|
isStale: boolean;
|
|
129
|
-
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<
|
|
136
|
+
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<InfiniteData<IRequestSuccess<TResponse & {
|
|
130
137
|
pagination: Pagination;
|
|
131
138
|
}>>, any>>;
|
|
132
139
|
remove: () => void;
|
|
133
140
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
141
|
+
get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<InfiniteData<IRequestSuccess<TResponse & {
|
|
142
|
+
pagination: Pagination;
|
|
143
|
+
}>> | undefined>;
|
|
134
144
|
} | {
|
|
135
|
-
data:
|
|
145
|
+
data: InfiniteData<IRequestSuccess<TResponse & {
|
|
136
146
|
pagination: Pagination;
|
|
137
147
|
}>>;
|
|
138
148
|
error: null;
|
|
@@ -166,10 +176,13 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
166
176
|
isPreviousData: boolean;
|
|
167
177
|
isRefetching: boolean;
|
|
168
178
|
isStale: boolean;
|
|
169
|
-
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<
|
|
179
|
+
refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<InfiniteData<IRequestSuccess<TResponse & {
|
|
170
180
|
pagination: Pagination;
|
|
171
181
|
}>>, any>>;
|
|
172
182
|
remove: () => void;
|
|
173
183
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
184
|
+
get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<InfiniteData<IRequestSuccess<TResponse & {
|
|
185
|
+
pagination: Pagination;
|
|
186
|
+
}>> | undefined>;
|
|
174
187
|
};
|
|
175
188
|
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useQueryClient, useInfiniteQuery } from '@tanstack/react-query';
|
|
2
|
-
import { useMemo, useEffect } from 'react';
|
|
2
|
+
import { useState, useMemo, useEffect, startTransition } from 'react';
|
|
3
3
|
import 'url-search-params-polyfill';
|
|
4
4
|
import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
|
|
5
5
|
import { useQueryHeaders } from '../config/useQueryHeaders.js';
|
|
@@ -10,6 +10,8 @@ import '../request/request.enum.js';
|
|
|
10
10
|
const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl, headers, }) => {
|
|
11
11
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
12
12
|
const { getHeaders } = useQueryHeaders();
|
|
13
|
+
const [requestPath, updatePath] = useState(path);
|
|
14
|
+
const [options, setOptions] = useState(queryOptions);
|
|
13
15
|
let queryClient = useQueryClient();
|
|
14
16
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
15
17
|
queryClient = useMemo(() => queryClient, []);
|
|
@@ -18,7 +20,7 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
18
20
|
// get request headers
|
|
19
21
|
const globalHeaders = getHeaders();
|
|
20
22
|
const getResponse = await makeRequest({
|
|
21
|
-
path: pageParam ??
|
|
23
|
+
path: pageParam ?? requestPath,
|
|
22
24
|
headers: { ...globalHeaders, ...headers },
|
|
23
25
|
baseURL: baseUrl ?? API_URL,
|
|
24
26
|
timeout: TIMEOUT,
|
|
@@ -39,18 +41,33 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
39
41
|
* This pagination implementation is currently tied to our use case
|
|
40
42
|
*/
|
|
41
43
|
const constructPaginationLink = (direction, lastPage) => {
|
|
42
|
-
const [pathname, queryString] =
|
|
44
|
+
const [pathname, queryString] = requestPath.split('?');
|
|
43
45
|
const queryParams = new URLSearchParams(queryString);
|
|
44
46
|
const lastPageItem = lastPage.data.pagination[direction];
|
|
45
47
|
queryParams.set('page', String(lastPageItem));
|
|
46
48
|
return pathname + '?' + queryParams.toString();
|
|
47
49
|
};
|
|
48
|
-
const query = useInfiniteQuery([
|
|
50
|
+
const query = useInfiniteQuery([requestPath, {}], ({ pageParam = requestPath }) => new Promise((res, rej) => sendRequest(res, rej, pageParam)), {
|
|
49
51
|
enabled: load,
|
|
50
52
|
getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
|
|
51
53
|
getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
|
|
52
|
-
...
|
|
54
|
+
...options,
|
|
53
55
|
});
|
|
56
|
+
const setOptionsAsync = async (fetchOptions) => {
|
|
57
|
+
startTransition(() => {
|
|
58
|
+
setOptions(fetchOptions);
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
const get = async (link, fetchOptions) => {
|
|
62
|
+
await setOptionsAsync(fetchOptions);
|
|
63
|
+
await updatedPathAsync(link);
|
|
64
|
+
return query.data;
|
|
65
|
+
};
|
|
66
|
+
const updatedPathAsync = async (link) => {
|
|
67
|
+
startTransition(() => {
|
|
68
|
+
updatePath(link);
|
|
69
|
+
});
|
|
70
|
+
};
|
|
54
71
|
useEffect(() => {
|
|
55
72
|
if (keyTracker) {
|
|
56
73
|
// set expiration time for the tracker
|
|
@@ -58,10 +75,11 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
58
75
|
cacheTime: Infinity,
|
|
59
76
|
staleTime: Infinity,
|
|
60
77
|
});
|
|
61
|
-
queryClient.setQueryData([keyTracker], [
|
|
78
|
+
queryClient.setQueryData([keyTracker], [requestPath, {}]);
|
|
62
79
|
}
|
|
63
|
-
}, [keyTracker,
|
|
80
|
+
}, [keyTracker, requestPath, queryClient, queryOptions?.staleTime]);
|
|
64
81
|
return {
|
|
82
|
+
get,
|
|
65
83
|
...query,
|
|
66
84
|
};
|
|
67
85
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useGetInfiniteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useGetInfiniteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { MutateOptions } from '@tanstack/react-query';
|
|
2
|
-
import type { IRequestError, IRequestSuccess } from '../request';
|
|
2
|
+
import type { IMakeRequest, IRequestError, IRequestSuccess } from '../request';
|
|
3
3
|
import type { DefaultRequestOptions } from './queries.interface';
|
|
4
4
|
export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, headers, fileSelectors, }: {
|
|
5
5
|
path: string;
|
|
@@ -13,15 +13,27 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, he
|
|
|
13
13
|
isLoading: false;
|
|
14
14
|
isSuccess: false;
|
|
15
15
|
status: "idle";
|
|
16
|
-
mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError,
|
|
16
|
+
mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, {
|
|
17
|
+
data: any;
|
|
18
|
+
requestConfig?: Omit<IMakeRequest, "body"> | undefined;
|
|
19
|
+
}, unknown>;
|
|
17
20
|
reset: () => void;
|
|
18
21
|
context: unknown;
|
|
19
22
|
failureCount: number;
|
|
20
23
|
failureReason: IRequestError | null;
|
|
21
24
|
isPaused: boolean;
|
|
22
|
-
variables:
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
variables: {
|
|
26
|
+
data: any;
|
|
27
|
+
requestConfig?: Omit<IMakeRequest, "body"> | undefined;
|
|
28
|
+
} | undefined;
|
|
29
|
+
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, {
|
|
30
|
+
data: any;
|
|
31
|
+
requestConfig?: Omit<IMakeRequest, "body"> | undefined;
|
|
32
|
+
}, unknown>;
|
|
33
|
+
post: <T>(data: T, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, {
|
|
34
|
+
data: T;
|
|
35
|
+
requestConfig?: Omit<IMakeRequest, "body"> | undefined;
|
|
36
|
+
}, unknown> | undefined, requestConfig?: Omit<IMakeRequest, 'body'>) => Promise<IRequestSuccess<TResponse>>;
|
|
25
37
|
uploadProgressPercent: number;
|
|
26
38
|
} | {
|
|
27
39
|
data: undefined;
|
|
@@ -31,15 +43,27 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, he
|
|
|
31
43
|
isLoading: true;
|
|
32
44
|
isSuccess: false;
|
|
33
45
|
status: "loading";
|
|
34
|
-
mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError,
|
|
46
|
+
mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, {
|
|
47
|
+
data: any;
|
|
48
|
+
requestConfig?: Omit<IMakeRequest, "body"> | undefined;
|
|
49
|
+
}, unknown>;
|
|
35
50
|
reset: () => void;
|
|
36
51
|
context: unknown;
|
|
37
52
|
failureCount: number;
|
|
38
53
|
failureReason: IRequestError | null;
|
|
39
54
|
isPaused: boolean;
|
|
40
|
-
variables:
|
|
41
|
-
|
|
42
|
-
|
|
55
|
+
variables: {
|
|
56
|
+
data: any;
|
|
57
|
+
requestConfig?: Omit<IMakeRequest, "body"> | undefined;
|
|
58
|
+
} | undefined;
|
|
59
|
+
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, {
|
|
60
|
+
data: any;
|
|
61
|
+
requestConfig?: Omit<IMakeRequest, "body"> | undefined;
|
|
62
|
+
}, unknown>;
|
|
63
|
+
post: <T>(data: T, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, {
|
|
64
|
+
data: T;
|
|
65
|
+
requestConfig?: Omit<IMakeRequest, "body"> | undefined;
|
|
66
|
+
}, unknown> | undefined, requestConfig?: Omit<IMakeRequest, 'body'>) => Promise<IRequestSuccess<TResponse>>;
|
|
43
67
|
uploadProgressPercent: number;
|
|
44
68
|
} | {
|
|
45
69
|
data: undefined;
|
|
@@ -49,15 +73,27 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, he
|
|
|
49
73
|
isLoading: false;
|
|
50
74
|
isSuccess: false;
|
|
51
75
|
status: "error";
|
|
52
|
-
mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError,
|
|
76
|
+
mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, {
|
|
77
|
+
data: any;
|
|
78
|
+
requestConfig?: Omit<IMakeRequest, "body"> | undefined;
|
|
79
|
+
}, unknown>;
|
|
53
80
|
reset: () => void;
|
|
54
81
|
context: unknown;
|
|
55
82
|
failureCount: number;
|
|
56
83
|
failureReason: IRequestError | null;
|
|
57
84
|
isPaused: boolean;
|
|
58
|
-
variables:
|
|
59
|
-
|
|
60
|
-
|
|
85
|
+
variables: {
|
|
86
|
+
data: any;
|
|
87
|
+
requestConfig?: Omit<IMakeRequest, "body"> | undefined;
|
|
88
|
+
} | undefined;
|
|
89
|
+
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, {
|
|
90
|
+
data: any;
|
|
91
|
+
requestConfig?: Omit<IMakeRequest, "body"> | undefined;
|
|
92
|
+
}, unknown>;
|
|
93
|
+
post: <T>(data: T, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, {
|
|
94
|
+
data: T;
|
|
95
|
+
requestConfig?: Omit<IMakeRequest, "body"> | undefined;
|
|
96
|
+
}, unknown> | undefined, requestConfig?: Omit<IMakeRequest, 'body'>) => Promise<IRequestSuccess<TResponse>>;
|
|
61
97
|
uploadProgressPercent: number;
|
|
62
98
|
} | {
|
|
63
99
|
data: IRequestSuccess<TResponse>;
|
|
@@ -67,14 +103,26 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, he
|
|
|
67
103
|
isLoading: false;
|
|
68
104
|
isSuccess: true;
|
|
69
105
|
status: "success";
|
|
70
|
-
mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError,
|
|
106
|
+
mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, {
|
|
107
|
+
data: any;
|
|
108
|
+
requestConfig?: Omit<IMakeRequest, "body"> | undefined;
|
|
109
|
+
}, unknown>;
|
|
71
110
|
reset: () => void;
|
|
72
111
|
context: unknown;
|
|
73
112
|
failureCount: number;
|
|
74
113
|
failureReason: IRequestError | null;
|
|
75
114
|
isPaused: boolean;
|
|
76
|
-
variables:
|
|
77
|
-
|
|
78
|
-
|
|
115
|
+
variables: {
|
|
116
|
+
data: any;
|
|
117
|
+
requestConfig?: Omit<IMakeRequest, "body"> | undefined;
|
|
118
|
+
} | undefined;
|
|
119
|
+
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, {
|
|
120
|
+
data: any;
|
|
121
|
+
requestConfig?: Omit<IMakeRequest, "body"> | undefined;
|
|
122
|
+
}, unknown>;
|
|
123
|
+
post: <T>(data: T, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, {
|
|
124
|
+
data: T;
|
|
125
|
+
requestConfig?: Omit<IMakeRequest, "body"> | undefined;
|
|
126
|
+
}, unknown> | undefined, requestConfig?: Omit<IMakeRequest, 'body'>) => Promise<IRequestSuccess<TResponse>>;
|
|
79
127
|
uploadProgressPercent: number;
|
|
80
128
|
};
|
|
@@ -19,9 +19,11 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
19
19
|
// get request headers
|
|
20
20
|
const globalHeaders = getHeaders();
|
|
21
21
|
const config = queryClient.getQueryData(['config']);
|
|
22
|
+
const { data, requestConfig } = postData;
|
|
23
|
+
delete requestConfig?.body;
|
|
22
24
|
const postResponse = await makeRequest({
|
|
23
25
|
path,
|
|
24
|
-
body:
|
|
26
|
+
body: data,
|
|
25
27
|
method: HttpMethod.POST,
|
|
26
28
|
isFormData,
|
|
27
29
|
headers: { ...globalHeaders, ...headers },
|
|
@@ -32,6 +34,7 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
32
34
|
fileSelectors,
|
|
33
35
|
},
|
|
34
36
|
onUploadProgress,
|
|
37
|
+
...requestConfig,
|
|
35
38
|
});
|
|
36
39
|
if (postResponse.status) {
|
|
37
40
|
// scroll to top after success
|
|
@@ -50,8 +53,8 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
50
53
|
};
|
|
51
54
|
// register post mutation
|
|
52
55
|
const mutation = useMutation(async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)));
|
|
53
|
-
const post = async (data, options) => {
|
|
54
|
-
return mutation.mutateAsync(data, options);
|
|
56
|
+
const post = async (data, options, requestConfig) => {
|
|
57
|
+
return mutation.mutateAsync({ data, requestConfig }, options);
|
|
55
58
|
};
|
|
56
59
|
return { post, uploadProgressPercent, ...mutation };
|
|
57
60
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import type { InfiniteData, UseQueryOptions } from '@tanstack/react-query';
|
|
1
2
|
import { useInfiniteQuery, useQueryClient } from '@tanstack/react-query';
|
|
2
3
|
import type { RawAxiosRequestHeaders } from 'axios';
|
|
3
|
-
import { useEffect, useMemo } from 'react';
|
|
4
|
+
import { startTransition, useEffect, useMemo, useState } from 'react';
|
|
4
5
|
import { useEnvironmentVariables, useQueryHeaders } from '../config';
|
|
5
6
|
|
|
6
7
|
import type { IRequestError, IRequestSuccess } from '../request';
|
|
@@ -31,6 +32,9 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
31
32
|
} & DefaultRequestOptions) => {
|
|
32
33
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
33
34
|
const { getHeaders } = useQueryHeaders();
|
|
35
|
+
const [requestPath, updatePath] = useState<string>(path);
|
|
36
|
+
|
|
37
|
+
const [options, setOptions] = useState<any>(queryOptions);
|
|
34
38
|
|
|
35
39
|
let queryClient = useQueryClient();
|
|
36
40
|
|
|
@@ -52,7 +56,7 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
52
56
|
const globalHeaders: RawAxiosRequestHeaders = getHeaders();
|
|
53
57
|
|
|
54
58
|
const getResponse = await makeRequest<TResponse>({
|
|
55
|
-
path: pageParam ??
|
|
59
|
+
path: pageParam ?? requestPath,
|
|
56
60
|
headers: { ...globalHeaders, ...headers },
|
|
57
61
|
baseURL: baseUrl ?? API_URL,
|
|
58
62
|
timeout: TIMEOUT,
|
|
@@ -80,7 +84,7 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
80
84
|
}
|
|
81
85
|
>
|
|
82
86
|
) => {
|
|
83
|
-
const [pathname, queryString] =
|
|
87
|
+
const [pathname, queryString] = requestPath.split('?');
|
|
84
88
|
|
|
85
89
|
const queryParams = new URLSearchParams(queryString);
|
|
86
90
|
const lastPageItem = lastPage.data.pagination[direction];
|
|
@@ -91,8 +95,8 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
91
95
|
};
|
|
92
96
|
|
|
93
97
|
const query = useInfiniteQuery<any, any, IRequestSuccess<TResponse & { pagination: Pagination }>>(
|
|
94
|
-
[
|
|
95
|
-
({ pageParam =
|
|
98
|
+
[requestPath, {}],
|
|
99
|
+
({ pageParam = requestPath }) =>
|
|
96
100
|
new Promise<IRequestSuccess<TResponse & { pagination: Pagination }> | IRequestError>((res, rej) =>
|
|
97
101
|
sendRequest(res, rej, pageParam)
|
|
98
102
|
),
|
|
@@ -100,10 +104,46 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
100
104
|
enabled: load,
|
|
101
105
|
getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
|
|
102
106
|
getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
|
|
103
|
-
...
|
|
107
|
+
...options,
|
|
104
108
|
}
|
|
105
109
|
);
|
|
106
110
|
|
|
111
|
+
const setOptionsAsync = async (fetchOptions: any) => {
|
|
112
|
+
startTransition(() => {
|
|
113
|
+
setOptions(fetchOptions);
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const get = async (
|
|
118
|
+
link: string,
|
|
119
|
+
fetchOptions?: UseQueryOptions<
|
|
120
|
+
IRequestSuccess<TResponse | undefined>,
|
|
121
|
+
IRequestError,
|
|
122
|
+
IRequestSuccess<TResponse | undefined>,
|
|
123
|
+
Array<any>
|
|
124
|
+
>
|
|
125
|
+
): Promise<
|
|
126
|
+
| InfiniteData<
|
|
127
|
+
IRequestSuccess<
|
|
128
|
+
TResponse & {
|
|
129
|
+
pagination: Pagination;
|
|
130
|
+
}
|
|
131
|
+
>
|
|
132
|
+
>
|
|
133
|
+
| undefined
|
|
134
|
+
> => {
|
|
135
|
+
await setOptionsAsync(fetchOptions);
|
|
136
|
+
await updatedPathAsync(link);
|
|
137
|
+
|
|
138
|
+
return query.data;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const updatedPathAsync = async (link: string) => {
|
|
142
|
+
startTransition(() => {
|
|
143
|
+
updatePath(link);
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
|
|
107
147
|
useEffect(() => {
|
|
108
148
|
if (keyTracker) {
|
|
109
149
|
// set expiration time for the tracker
|
|
@@ -112,11 +152,12 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
112
152
|
staleTime: Infinity,
|
|
113
153
|
});
|
|
114
154
|
|
|
115
|
-
queryClient.setQueryData([keyTracker], [
|
|
155
|
+
queryClient.setQueryData([keyTracker], [requestPath, {}]);
|
|
116
156
|
}
|
|
117
|
-
}, [keyTracker,
|
|
157
|
+
}, [keyTracker, requestPath, queryClient, queryOptions?.staleTime]);
|
|
118
158
|
|
|
119
159
|
return {
|
|
160
|
+
get,
|
|
120
161
|
...query,
|
|
121
162
|
};
|
|
122
163
|
};
|
|
@@ -5,7 +5,7 @@ import { useEnvironmentVariables, useQueryHeaders, useReactNativeEnv } from '../
|
|
|
5
5
|
import type { RawAxiosRequestHeaders } from 'axios';
|
|
6
6
|
import { scrollToTop } from '../helpers';
|
|
7
7
|
import { useUploadProgress } from '../hooks';
|
|
8
|
-
import type { IRequestError, IRequestSuccess } from '../request';
|
|
8
|
+
import type { IMakeRequest, IRequestError, IRequestSuccess } from '../request';
|
|
9
9
|
import { HttpMethod, makeRequest } from '../request';
|
|
10
10
|
import type { TanstackQueryConfig } from '../types';
|
|
11
11
|
import type { DefaultRequestOptions } from './queries.interface';
|
|
@@ -27,14 +27,21 @@ export const usePostRequest = <TResponse>({
|
|
|
27
27
|
const { isApp } = useReactNativeEnv();
|
|
28
28
|
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
29
29
|
|
|
30
|
-
const sendRequest = async (
|
|
30
|
+
const sendRequest = async (
|
|
31
|
+
res: (value: any) => void,
|
|
32
|
+
rej: (reason?: any) => void,
|
|
33
|
+
postData: { data: any; requestConfig?: IMakeRequest }
|
|
34
|
+
) => {
|
|
31
35
|
// get request headers
|
|
32
36
|
const globalHeaders: RawAxiosRequestHeaders = getHeaders();
|
|
33
37
|
const config = queryClient.getQueryData<TanstackQueryConfig>(['config']);
|
|
38
|
+
const { data, requestConfig } = postData;
|
|
39
|
+
|
|
40
|
+
delete requestConfig?.body;
|
|
34
41
|
|
|
35
42
|
const postResponse = await makeRequest<TResponse>({
|
|
36
43
|
path,
|
|
37
|
-
body:
|
|
44
|
+
body: data,
|
|
38
45
|
method: HttpMethod.POST,
|
|
39
46
|
isFormData,
|
|
40
47
|
headers: { ...globalHeaders, ...headers },
|
|
@@ -45,6 +52,7 @@ export const usePostRequest = <TResponse>({
|
|
|
45
52
|
fileSelectors,
|
|
46
53
|
},
|
|
47
54
|
onUploadProgress,
|
|
55
|
+
...requestConfig,
|
|
48
56
|
});
|
|
49
57
|
|
|
50
58
|
if (postResponse.status) {
|
|
@@ -64,14 +72,25 @@ export const usePostRequest = <TResponse>({
|
|
|
64
72
|
};
|
|
65
73
|
|
|
66
74
|
// register post mutation
|
|
67
|
-
const mutation = useMutation<
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
75
|
+
const mutation = useMutation<
|
|
76
|
+
IRequestSuccess<TResponse>,
|
|
77
|
+
IRequestError,
|
|
78
|
+
{ data: any; requestConfig?: Omit<IMakeRequest, 'body'> }
|
|
79
|
+
>(async (postData) => new Promise<IRequestSuccess<TResponse>>((res, rej) => sendRequest(res, rej, postData)));
|
|
80
|
+
|
|
81
|
+
const post = async <T>(
|
|
82
|
+
data: T,
|
|
83
|
+
options?:
|
|
84
|
+
| MutateOptions<
|
|
85
|
+
IRequestSuccess<TResponse>,
|
|
86
|
+
IRequestError,
|
|
87
|
+
{ data: T; requestConfig?: Omit<IMakeRequest, 'body'> },
|
|
88
|
+
unknown
|
|
89
|
+
>
|
|
90
|
+
| undefined,
|
|
91
|
+
requestConfig?: Omit<IMakeRequest, 'body'>
|
|
73
92
|
): Promise<IRequestSuccess<TResponse>> => {
|
|
74
|
-
return mutation.mutateAsync(data, options);
|
|
93
|
+
return mutation.mutateAsync({ data, requestConfig }, options);
|
|
75
94
|
};
|
|
76
95
|
|
|
77
96
|
return { post, uploadProgressPercent, ...mutation };
|