@ventlio/tanstack-query 0.2.75 → 0.2.77
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/hooks/index.d.ts +1 -0
- package/dist/hooks/useUploadProgress.d.ts +5 -0
- package/dist/hooks/useUploadProgress.js +14 -0
- package/dist/hooks/useUploadProgress.js.map +1 -0
- package/dist/index.mjs +28 -13
- package/dist/index.mjs.map +1 -1
- package/dist/queries/useGetInfiniteRequest.js +10 -10
- package/dist/queries/usePatchRequest.d.ts +4 -0
- package/dist/queries/usePatchRequest.js +4 -1
- package/dist/queries/usePatchRequest.js.map +1 -1
- package/dist/queries/usePostRequest.d.ts +4 -0
- package/dist/queries/usePostRequest.js +4 -1
- package/dist/queries/usePostRequest.js.map +1 -1
- package/dist/request/make-request.d.ts +1 -1
- package/dist/request/make-request.js +2 -1
- package/dist/request/make-request.js.map +1 -1
- package/dist/request/request.interface.d.ts +2 -1
- package/package.json +1 -1
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useUploadProgress.ts +15 -0
- package/src/queries/useGetInfiniteRequest.ts +19 -20
- package/src/queries/usePatchRequest.ts +4 -1
- package/src/queries/usePostRequest.ts +5 -1
- package/src/request/make-request.ts +2 -0
- package/src/request/request.interface.ts +3 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useUploadProgress';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
|
|
3
|
+
const useUploadProgress = () => {
|
|
4
|
+
const [uploadProgressPercent, setUploadProgressPercent] = useState(0);
|
|
5
|
+
const onUploadProgress = (progressEvent) => {
|
|
6
|
+
const { loaded, total } = progressEvent;
|
|
7
|
+
const percentage = Math.round((loaded / total) * 100);
|
|
8
|
+
setUploadProgressPercent(percentage);
|
|
9
|
+
};
|
|
10
|
+
return { onUploadProgress, uploadProgressPercent };
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { useUploadProgress };
|
|
14
|
+
//# sourceMappingURL=useUploadProgress.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useUploadProgress.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;"}
|
package/dist/index.mjs
CHANGED
|
@@ -313,7 +313,7 @@ const successTransformer = (data) => {
|
|
|
313
313
|
};
|
|
314
314
|
};
|
|
315
315
|
|
|
316
|
-
async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, headers = {}, baseURL, timeout, appFileConfig, }) {
|
|
316
|
+
async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, headers = {}, baseURL, timeout, appFileConfig, onUploadProgress, }) {
|
|
317
317
|
// check if file is included in mobile app environment and extract all file input to avoid
|
|
318
318
|
// it being formatted to object using axios formData builder
|
|
319
319
|
const isApp = appFileConfig?.isApp;
|
|
@@ -351,6 +351,7 @@ async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, he
|
|
|
351
351
|
url: path,
|
|
352
352
|
method,
|
|
353
353
|
data: body,
|
|
354
|
+
onUploadProgress,
|
|
354
355
|
});
|
|
355
356
|
// get response json
|
|
356
357
|
const jsonResp = await resp.data;
|
|
@@ -459,23 +460,23 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
459
460
|
res(null);
|
|
460
461
|
}
|
|
461
462
|
};
|
|
462
|
-
const query = useInfiniteQuery([path, {}], ({ pageParam = path }) => new Promise((res, rej) => sendRequest(res, rej, pageParam)), {
|
|
463
|
-
enabled: load,
|
|
464
|
-
getNextPageParam: (lastPage, pages) => constructPaginationLink('next_page', lastPage, pages),
|
|
465
|
-
getPreviousPageParam: (lastPage, pages) => constructPaginationLink('previous_page', lastPage, pages),
|
|
466
|
-
...queryOptions,
|
|
467
|
-
});
|
|
468
463
|
/**
|
|
469
464
|
*
|
|
470
465
|
* This pagination implementation is currently tied to our use case
|
|
471
466
|
*/
|
|
472
|
-
const constructPaginationLink = (direction, lastPage
|
|
473
|
-
const [pathname, queryString] =
|
|
467
|
+
const constructPaginationLink = (direction, lastPage) => {
|
|
468
|
+
const [pathname, queryString] = path.split('?');
|
|
474
469
|
const queryParams = new URLSearchParams(queryString);
|
|
475
|
-
const lastPageItem =
|
|
476
|
-
queryParams.set('page', String(lastPageItem
|
|
470
|
+
const lastPageItem = lastPage.data.pagination[direction];
|
|
471
|
+
queryParams.set('page', String(lastPageItem));
|
|
477
472
|
return pathname + '?' + queryParams.toString();
|
|
478
473
|
};
|
|
474
|
+
const query = useInfiniteQuery([path, {}], ({ pageParam = path }) => new Promise((res, rej) => sendRequest(res, rej, pageParam)), {
|
|
475
|
+
enabled: load,
|
|
476
|
+
getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
|
|
477
|
+
getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
|
|
478
|
+
...queryOptions,
|
|
479
|
+
});
|
|
479
480
|
useEffect(() => {
|
|
480
481
|
if (keyTracker) {
|
|
481
482
|
// set expiration time for the tracker
|
|
@@ -599,8 +600,19 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
|
|
|
599
600
|
};
|
|
600
601
|
};
|
|
601
602
|
|
|
603
|
+
const useUploadProgress = () => {
|
|
604
|
+
const [uploadProgressPercent, setUploadProgressPercent] = useState(0);
|
|
605
|
+
const onUploadProgress = (progressEvent) => {
|
|
606
|
+
const { loaded, total } = progressEvent;
|
|
607
|
+
const percentage = Math.round((loaded / total) * 100);
|
|
608
|
+
setUploadProgressPercent(percentage);
|
|
609
|
+
};
|
|
610
|
+
return { onUploadProgress, uploadProgressPercent };
|
|
611
|
+
};
|
|
612
|
+
|
|
602
613
|
const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
603
614
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
615
|
+
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
604
616
|
const { getHeaders } = useQueryHeaders();
|
|
605
617
|
const queryClient = useQueryClient();
|
|
606
618
|
const config = queryClient.getQueryData(['config']);
|
|
@@ -614,6 +626,7 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
614
626
|
headers: { ...globalHeaders, ...headers },
|
|
615
627
|
baseURL: baseUrl ?? API_URL,
|
|
616
628
|
timeout: TIMEOUT,
|
|
629
|
+
onUploadProgress,
|
|
617
630
|
});
|
|
618
631
|
if (patchResponse.status) {
|
|
619
632
|
// scroll to top after success
|
|
@@ -637,7 +650,7 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
637
650
|
const patch = async (data, options) => {
|
|
638
651
|
return mutation.mutateAsync(data, options);
|
|
639
652
|
};
|
|
640
|
-
return { patch, ...mutation };
|
|
653
|
+
return { patch, uploadProgressPercent, ...mutation };
|
|
641
654
|
};
|
|
642
655
|
|
|
643
656
|
const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelectors, }) => {
|
|
@@ -645,6 +658,7 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
645
658
|
const queryClient = useQueryClient();
|
|
646
659
|
const { getHeaders } = useQueryHeaders();
|
|
647
660
|
const { isApp } = useReactNativeEnv();
|
|
661
|
+
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
648
662
|
const sendRequest = async (res, rej, postData) => {
|
|
649
663
|
// get request headers
|
|
650
664
|
const globalHeaders = getHeaders();
|
|
@@ -661,6 +675,7 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
661
675
|
isApp,
|
|
662
676
|
fileSelectors,
|
|
663
677
|
},
|
|
678
|
+
onUploadProgress,
|
|
664
679
|
});
|
|
665
680
|
if (postResponse.status) {
|
|
666
681
|
// scroll to top after success
|
|
@@ -682,7 +697,7 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
682
697
|
const post = async (data, options) => {
|
|
683
698
|
return mutation.mutateAsync(data, options);
|
|
684
699
|
};
|
|
685
|
-
return { post, ...mutation };
|
|
700
|
+
return { post, uploadProgressPercent, ...mutation };
|
|
686
701
|
};
|
|
687
702
|
|
|
688
703
|
export { ContentType, HttpMethod, axiosInstance, bootstrapQueryRequest, buildFormData, errorTransformer, getDateInFuture, makeRequest, scrollToTop, successTransformer, useDeleteRequest, useEnvironmentVariables, useGetInfiniteRequest, useGetRequest, useKeyTrackerModel, usePatchRequest, usePostRequest, useQueryConfig, useQueryHeaders, useQueryModel, useReactNativeEnv, useRefetchQuery };
|
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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -34,23 +34,23 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
34
34
|
res(null);
|
|
35
35
|
}
|
|
36
36
|
};
|
|
37
|
-
const query = useInfiniteQuery([path, {}], ({ pageParam = path }) => new Promise((res, rej) => sendRequest(res, rej, pageParam)), {
|
|
38
|
-
enabled: load,
|
|
39
|
-
getNextPageParam: (lastPage, pages) => constructPaginationLink('next_page', lastPage, pages),
|
|
40
|
-
getPreviousPageParam: (lastPage, pages) => constructPaginationLink('previous_page', lastPage, pages),
|
|
41
|
-
...queryOptions,
|
|
42
|
-
});
|
|
43
37
|
/**
|
|
44
38
|
*
|
|
45
39
|
* This pagination implementation is currently tied to our use case
|
|
46
40
|
*/
|
|
47
|
-
const constructPaginationLink = (direction, lastPage
|
|
48
|
-
const [pathname, queryString] =
|
|
41
|
+
const constructPaginationLink = (direction, lastPage) => {
|
|
42
|
+
const [pathname, queryString] = path.split('?');
|
|
49
43
|
const queryParams = new URLSearchParams(queryString);
|
|
50
|
-
const lastPageItem =
|
|
51
|
-
queryParams.set('page', String(lastPageItem
|
|
44
|
+
const lastPageItem = lastPage.data.pagination[direction];
|
|
45
|
+
queryParams.set('page', String(lastPageItem));
|
|
52
46
|
return pathname + '?' + queryParams.toString();
|
|
53
47
|
};
|
|
48
|
+
const query = useInfiniteQuery([path, {}], ({ pageParam = path }) => new Promise((res, rej) => sendRequest(res, rej, pageParam)), {
|
|
49
|
+
enabled: load,
|
|
50
|
+
getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
|
|
51
|
+
getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
|
|
52
|
+
...queryOptions,
|
|
53
|
+
});
|
|
54
54
|
useEffect(() => {
|
|
55
55
|
if (keyTracker) {
|
|
56
56
|
// set expiration time for the tracker
|
|
@@ -20,6 +20,7 @@ export declare const usePatchRequest: <TResponse>({ path, baseUrl, headers }: {
|
|
|
20
20
|
variables: void | undefined;
|
|
21
21
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
22
22
|
patch: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
23
|
+
uploadProgressPercent: number;
|
|
23
24
|
} | {
|
|
24
25
|
data: undefined;
|
|
25
26
|
error: null;
|
|
@@ -37,6 +38,7 @@ export declare const usePatchRequest: <TResponse>({ path, baseUrl, headers }: {
|
|
|
37
38
|
variables: void | undefined;
|
|
38
39
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
39
40
|
patch: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
41
|
+
uploadProgressPercent: number;
|
|
40
42
|
} | {
|
|
41
43
|
data: undefined;
|
|
42
44
|
error: IRequestError;
|
|
@@ -54,6 +56,7 @@ export declare const usePatchRequest: <TResponse>({ path, baseUrl, headers }: {
|
|
|
54
56
|
variables: void | undefined;
|
|
55
57
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
56
58
|
patch: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
59
|
+
uploadProgressPercent: number;
|
|
57
60
|
} | {
|
|
58
61
|
data: IRequestSuccess<TResponse>;
|
|
59
62
|
error: null;
|
|
@@ -71,4 +74,5 @@ export declare const usePatchRequest: <TResponse>({ path, baseUrl, headers }: {
|
|
|
71
74
|
variables: void | undefined;
|
|
72
75
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
73
76
|
patch: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
77
|
+
uploadProgressPercent: number;
|
|
74
78
|
};
|
|
@@ -3,12 +3,14 @@ import 'url-search-params-polyfill';
|
|
|
3
3
|
import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
|
|
4
4
|
import { useQueryHeaders } from '../config/useQueryHeaders.js';
|
|
5
5
|
import { scrollToTop } from '../helpers/scrollToTop.js';
|
|
6
|
+
import { useUploadProgress } from '../hooks/useUploadProgress.js';
|
|
6
7
|
import 'axios';
|
|
7
8
|
import { makeRequest } from '../request/make-request.js';
|
|
8
9
|
import { HttpMethod } from '../request/request.enum.js';
|
|
9
10
|
|
|
10
11
|
const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
11
12
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
13
|
+
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
12
14
|
const { getHeaders } = useQueryHeaders();
|
|
13
15
|
const queryClient = useQueryClient();
|
|
14
16
|
const config = queryClient.getQueryData(['config']);
|
|
@@ -22,6 +24,7 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
22
24
|
headers: { ...globalHeaders, ...headers },
|
|
23
25
|
baseURL: baseUrl ?? API_URL,
|
|
24
26
|
timeout: TIMEOUT,
|
|
27
|
+
onUploadProgress,
|
|
25
28
|
});
|
|
26
29
|
if (patchResponse.status) {
|
|
27
30
|
// scroll to top after success
|
|
@@ -45,7 +48,7 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
45
48
|
const patch = async (data, options) => {
|
|
46
49
|
return mutation.mutateAsync(data, options);
|
|
47
50
|
};
|
|
48
|
-
return { patch, ...mutation };
|
|
51
|
+
return { patch, uploadProgressPercent, ...mutation };
|
|
49
52
|
};
|
|
50
53
|
|
|
51
54
|
export { usePatchRequest };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -22,6 +22,7 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, he
|
|
|
22
22
|
variables: void | undefined;
|
|
23
23
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
24
24
|
post: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
25
|
+
uploadProgressPercent: number;
|
|
25
26
|
} | {
|
|
26
27
|
data: undefined;
|
|
27
28
|
error: null;
|
|
@@ -39,6 +40,7 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, he
|
|
|
39
40
|
variables: void | undefined;
|
|
40
41
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
41
42
|
post: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
43
|
+
uploadProgressPercent: number;
|
|
42
44
|
} | {
|
|
43
45
|
data: undefined;
|
|
44
46
|
error: IRequestError;
|
|
@@ -56,6 +58,7 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, he
|
|
|
56
58
|
variables: void | undefined;
|
|
57
59
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
58
60
|
post: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
61
|
+
uploadProgressPercent: number;
|
|
59
62
|
} | {
|
|
60
63
|
data: IRequestSuccess<TResponse>;
|
|
61
64
|
error: null;
|
|
@@ -73,4 +76,5 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, he
|
|
|
73
76
|
variables: void | undefined;
|
|
74
77
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
75
78
|
post: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
79
|
+
uploadProgressPercent: number;
|
|
76
80
|
};
|
|
@@ -4,6 +4,7 @@ import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
|
|
|
4
4
|
import { useQueryHeaders } from '../config/useQueryHeaders.js';
|
|
5
5
|
import { useReactNativeEnv } from '../config/useReactNativeEnv.js';
|
|
6
6
|
import { scrollToTop } from '../helpers/scrollToTop.js';
|
|
7
|
+
import { useUploadProgress } from '../hooks/useUploadProgress.js';
|
|
7
8
|
import 'axios';
|
|
8
9
|
import { makeRequest } from '../request/make-request.js';
|
|
9
10
|
import { HttpMethod } from '../request/request.enum.js';
|
|
@@ -13,6 +14,7 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
13
14
|
const queryClient = useQueryClient();
|
|
14
15
|
const { getHeaders } = useQueryHeaders();
|
|
15
16
|
const { isApp } = useReactNativeEnv();
|
|
17
|
+
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
16
18
|
const sendRequest = async (res, rej, postData) => {
|
|
17
19
|
// get request headers
|
|
18
20
|
const globalHeaders = getHeaders();
|
|
@@ -29,6 +31,7 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
29
31
|
isApp,
|
|
30
32
|
fileSelectors,
|
|
31
33
|
},
|
|
34
|
+
onUploadProgress,
|
|
32
35
|
});
|
|
33
36
|
if (postResponse.status) {
|
|
34
37
|
// scroll to top after success
|
|
@@ -50,7 +53,7 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
50
53
|
const post = async (data, options) => {
|
|
51
54
|
return mutation.mutateAsync(data, options);
|
|
52
55
|
};
|
|
53
|
-
return { post, ...mutation };
|
|
56
|
+
return { post, uploadProgressPercent, ...mutation };
|
|
54
57
|
};
|
|
55
58
|
|
|
56
59
|
export { usePostRequest };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { IMakeRequest } from './request.interface';
|
|
2
|
-
export declare function makeRequest<TResponse>({ body, method, path, isFormData, headers, baseURL, timeout, appFileConfig, }: IMakeRequest): Promise<import("./request.interface").IRequestError>;
|
|
2
|
+
export declare function makeRequest<TResponse>({ body, method, path, isFormData, headers, baseURL, timeout, appFileConfig, onUploadProgress, }: IMakeRequest): Promise<import("./request.interface").IRequestError>;
|
|
@@ -3,7 +3,7 @@ import { axiosInstance } from './axios-instance.js';
|
|
|
3
3
|
import { ContentType, HttpMethod } from './request.enum.js';
|
|
4
4
|
import { errorTransformer, successTransformer } from './transformer.js';
|
|
5
5
|
|
|
6
|
-
async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, headers = {}, baseURL, timeout, appFileConfig, }) {
|
|
6
|
+
async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, headers = {}, baseURL, timeout, appFileConfig, onUploadProgress, }) {
|
|
7
7
|
// check if file is included in mobile app environment and extract all file input to avoid
|
|
8
8
|
// it being formatted to object using axios formData builder
|
|
9
9
|
const isApp = appFileConfig?.isApp;
|
|
@@ -41,6 +41,7 @@ async function makeRequest({ body, method = HttpMethod.GET, path, isFormData, he
|
|
|
41
41
|
url: path,
|
|
42
42
|
method,
|
|
43
43
|
data: body,
|
|
44
|
+
onUploadProgress,
|
|
44
45
|
});
|
|
45
46
|
// get response json
|
|
46
47
|
const jsonResp = await resp.data;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"make-request.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"make-request.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RawAxiosRequestHeaders } from 'axios';
|
|
1
|
+
import type { AxiosProgressEvent, RawAxiosRequestHeaders } from 'axios';
|
|
2
2
|
import type { HttpMethod } from './request.enum';
|
|
3
3
|
export interface IMakeRequest {
|
|
4
4
|
baseURL: string;
|
|
@@ -9,6 +9,7 @@ export interface IMakeRequest {
|
|
|
9
9
|
isFormData?: boolean;
|
|
10
10
|
headers: RawAxiosRequestHeaders;
|
|
11
11
|
appFileConfig?: AppFileConfig;
|
|
12
|
+
onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
|
|
12
13
|
}
|
|
13
14
|
export interface AppFileConfig {
|
|
14
15
|
fileSelectors?: string[];
|
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useUploadProgress';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { AxiosProgressEvent } from 'axios';
|
|
2
|
+
import { useState } from 'react';
|
|
3
|
+
|
|
4
|
+
export const useUploadProgress = () => {
|
|
5
|
+
const [uploadProgressPercent, setUploadProgressPercent] = useState<number>(0);
|
|
6
|
+
|
|
7
|
+
const onUploadProgress = (progressEvent: AxiosProgressEvent) => {
|
|
8
|
+
const { loaded, total } = progressEvent;
|
|
9
|
+
const percentage = Math.round((loaded / (total as number)) * 100);
|
|
10
|
+
|
|
11
|
+
setUploadProgressPercent(percentage);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
return { onUploadProgress, uploadProgressPercent };
|
|
15
|
+
};
|
|
@@ -68,43 +68,42 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
68
68
|
}
|
|
69
69
|
};
|
|
70
70
|
|
|
71
|
-
const query = useInfiniteQuery<any, any, IRequestSuccess<TResponse & { pagination: Pagination }>>(
|
|
72
|
-
[path, {}],
|
|
73
|
-
({ pageParam = path }) =>
|
|
74
|
-
new Promise<IRequestSuccess<TResponse & { pagination: Pagination }> | IRequestError>((res, rej) =>
|
|
75
|
-
sendRequest(res, rej, pageParam)
|
|
76
|
-
),
|
|
77
|
-
{
|
|
78
|
-
enabled: load,
|
|
79
|
-
getNextPageParam: (lastPage, pages) => constructPaginationLink('next_page', lastPage, pages),
|
|
80
|
-
getPreviousPageParam: (lastPage, pages) => constructPaginationLink('previous_page', lastPage, pages),
|
|
81
|
-
...(queryOptions as any),
|
|
82
|
-
}
|
|
83
|
-
);
|
|
84
|
-
|
|
85
71
|
/**
|
|
86
72
|
*
|
|
87
73
|
* This pagination implementation is currently tied to our use case
|
|
88
74
|
*/
|
|
89
75
|
const constructPaginationLink = (
|
|
90
76
|
direction: 'next_page' | 'previous_page',
|
|
91
|
-
lastPage:
|
|
92
|
-
pages: IRequestSuccess<
|
|
77
|
+
lastPage: IRequestSuccess<
|
|
93
78
|
TResponse & {
|
|
94
79
|
pagination: Pagination;
|
|
95
80
|
}
|
|
96
|
-
>
|
|
81
|
+
>
|
|
97
82
|
) => {
|
|
98
|
-
const [pathname, queryString] =
|
|
83
|
+
const [pathname, queryString] = path.split('?');
|
|
99
84
|
|
|
100
85
|
const queryParams = new URLSearchParams(queryString);
|
|
101
|
-
const lastPageItem =
|
|
86
|
+
const lastPageItem = lastPage.data.pagination[direction];
|
|
102
87
|
|
|
103
|
-
queryParams.set('page', String(lastPageItem
|
|
88
|
+
queryParams.set('page', String(lastPageItem));
|
|
104
89
|
|
|
105
90
|
return pathname + '?' + queryParams.toString();
|
|
106
91
|
};
|
|
107
92
|
|
|
93
|
+
const query = useInfiniteQuery<any, any, IRequestSuccess<TResponse & { pagination: Pagination }>>(
|
|
94
|
+
[path, {}],
|
|
95
|
+
({ pageParam = path }) =>
|
|
96
|
+
new Promise<IRequestSuccess<TResponse & { pagination: Pagination }> | IRequestError>((res, rej) =>
|
|
97
|
+
sendRequest(res, rej, pageParam)
|
|
98
|
+
),
|
|
99
|
+
{
|
|
100
|
+
enabled: load,
|
|
101
|
+
getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
|
|
102
|
+
getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
|
|
103
|
+
...(queryOptions as any),
|
|
104
|
+
}
|
|
105
|
+
);
|
|
106
|
+
|
|
108
107
|
useEffect(() => {
|
|
109
108
|
if (keyTracker) {
|
|
110
109
|
// set expiration time for the tracker
|
|
@@ -3,6 +3,7 @@ import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
3
3
|
import type { RawAxiosRequestHeaders } from 'axios';
|
|
4
4
|
import { useEnvironmentVariables, useQueryHeaders } from '../config';
|
|
5
5
|
import { scrollToTop } from '../helpers';
|
|
6
|
+
import { useUploadProgress } from '../hooks';
|
|
6
7
|
import { HttpMethod, makeRequest } from '../request';
|
|
7
8
|
import type { IRequestError, IRequestSuccess } from '../request/request.interface';
|
|
8
9
|
import type { TanstackQueryConfig } from '../types';
|
|
@@ -10,6 +11,7 @@ import type { DefaultRequestOptions } from './queries.interface';
|
|
|
10
11
|
|
|
11
12
|
export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: string } & DefaultRequestOptions) => {
|
|
12
13
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
14
|
+
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
13
15
|
|
|
14
16
|
const { getHeaders } = useQueryHeaders();
|
|
15
17
|
const queryClient = useQueryClient();
|
|
@@ -27,6 +29,7 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
|
|
|
27
29
|
headers: { ...globalHeaders, ...headers },
|
|
28
30
|
baseURL: baseUrl ?? API_URL,
|
|
29
31
|
timeout: TIMEOUT,
|
|
32
|
+
onUploadProgress,
|
|
30
33
|
});
|
|
31
34
|
|
|
32
35
|
if (patchResponse.status) {
|
|
@@ -59,5 +62,5 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
|
|
|
59
62
|
return mutation.mutateAsync(data, options);
|
|
60
63
|
};
|
|
61
64
|
|
|
62
|
-
return { patch, ...mutation };
|
|
65
|
+
return { patch, uploadProgressPercent, ...mutation };
|
|
63
66
|
};
|
|
@@ -4,6 +4,7 @@ import { useEnvironmentVariables, useQueryHeaders, useReactNativeEnv } from '../
|
|
|
4
4
|
|
|
5
5
|
import type { RawAxiosRequestHeaders } from 'axios';
|
|
6
6
|
import { scrollToTop } from '../helpers';
|
|
7
|
+
import { useUploadProgress } from '../hooks';
|
|
7
8
|
import type { IRequestError, IRequestSuccess } from '../request';
|
|
8
9
|
import { HttpMethod, makeRequest } from '../request';
|
|
9
10
|
import type { TanstackQueryConfig } from '../types';
|
|
@@ -24,6 +25,8 @@ export const usePostRequest = <TResponse>({
|
|
|
24
25
|
const queryClient = useQueryClient();
|
|
25
26
|
const { getHeaders } = useQueryHeaders();
|
|
26
27
|
const { isApp } = useReactNativeEnv();
|
|
28
|
+
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
29
|
+
|
|
27
30
|
const sendRequest = async (res: (value: any) => void, rej: (reason?: any) => void, postData: any) => {
|
|
28
31
|
// get request headers
|
|
29
32
|
const globalHeaders: RawAxiosRequestHeaders = getHeaders();
|
|
@@ -41,6 +44,7 @@ export const usePostRequest = <TResponse>({
|
|
|
41
44
|
isApp,
|
|
42
45
|
fileSelectors,
|
|
43
46
|
},
|
|
47
|
+
onUploadProgress,
|
|
44
48
|
});
|
|
45
49
|
|
|
46
50
|
if (postResponse.status) {
|
|
@@ -70,5 +74,5 @@ export const usePostRequest = <TResponse>({
|
|
|
70
74
|
return mutation.mutateAsync(data, options);
|
|
71
75
|
};
|
|
72
76
|
|
|
73
|
-
return { post, ...mutation };
|
|
77
|
+
return { post, uploadProgressPercent, ...mutation };
|
|
74
78
|
};
|
|
@@ -14,6 +14,7 @@ export async function makeRequest<TResponse>({
|
|
|
14
14
|
baseURL,
|
|
15
15
|
timeout,
|
|
16
16
|
appFileConfig,
|
|
17
|
+
onUploadProgress,
|
|
17
18
|
}: IMakeRequest) {
|
|
18
19
|
// check if file is included in mobile app environment and extract all file input to avoid
|
|
19
20
|
// it being formatted to object using axios formData builder
|
|
@@ -53,6 +54,7 @@ export async function makeRequest<TResponse>({
|
|
|
53
54
|
url: path,
|
|
54
55
|
method,
|
|
55
56
|
data: body,
|
|
57
|
+
onUploadProgress,
|
|
56
58
|
});
|
|
57
59
|
|
|
58
60
|
// get response json
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RawAxiosRequestHeaders } from 'axios';
|
|
1
|
+
import type { AxiosProgressEvent, RawAxiosRequestHeaders } from 'axios';
|
|
2
2
|
import type { HttpMethod } from './request.enum';
|
|
3
3
|
|
|
4
4
|
export interface IMakeRequest {
|
|
@@ -10,12 +10,14 @@ export interface IMakeRequest {
|
|
|
10
10
|
isFormData?: boolean;
|
|
11
11
|
headers: RawAxiosRequestHeaders;
|
|
12
12
|
appFileConfig?: AppFileConfig;
|
|
13
|
+
onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
export interface AppFileConfig {
|
|
16
17
|
fileSelectors?: string[];
|
|
17
18
|
isApp: boolean;
|
|
18
19
|
}
|
|
20
|
+
|
|
19
21
|
export interface AxiosInstanceOption {
|
|
20
22
|
bearerToken?: string;
|
|
21
23
|
contentType?: string;
|