@ventlio/tanstack-query 0.2.85 → 0.2.87
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/config/bootstrapQueryRequest.js +7 -5
- package/dist/config/bootstrapQueryRequest.js.map +1 -1
- package/dist/config/useQueryConfig.js +3 -2
- package/dist/config/useQueryConfig.js.map +1 -1
- package/dist/config/useQueryHeaders.js +12 -14
- package/dist/config/useQueryHeaders.js.map +1 -1
- package/dist/config/useReactNativeEnv.js +5 -6
- package/dist/config/useReactNativeEnv.js.map +1 -1
- package/dist/index.mjs +161 -100
- package/dist/index.mjs.map +1 -1
- package/dist/model/useQueryModel.js +4 -1
- package/dist/model/useQueryModel.js.map +1 -1
- package/dist/queries/useDeleteRequest.js +23 -11
- package/dist/queries/useDeleteRequest.js.map +1 -1
- package/dist/queries/useGetInfiniteRequest.js +21 -9
- package/dist/queries/useGetInfiniteRequest.js.map +1 -1
- package/dist/queries/useGetRequest.d.ts +4 -4
- package/dist/queries/useGetRequest.js +27 -14
- package/dist/queries/useGetRequest.js.map +1 -1
- package/dist/queries/usePatchRequest.js +29 -16
- package/dist/queries/usePatchRequest.js.map +1 -1
- package/dist/queries/usePostRequest.js +31 -16
- package/dist/queries/usePostRequest.js.map +1 -1
- package/dist/types/index.d.ts +8 -0
- package/package.json +1 -1
- package/src/config/bootstrapQueryRequest.ts +9 -6
- package/src/config/useQueryConfig.ts +3 -2
- package/src/config/useQueryHeaders.ts +12 -18
- package/src/config/useReactNativeEnv.ts +5 -7
- package/src/model/useQueryModel.ts +3 -2
- package/src/queries/useDeleteRequest.ts +27 -13
- package/src/queries/useGetInfiniteRequest.ts +25 -11
- package/src/queries/useGetRequest.ts +30 -16
- package/src/queries/usePatchRequest.ts +31 -18
- package/src/queries/usePostRequest.ts +36 -18
- package/src/types/index.ts +4 -0
|
@@ -2,38 +2,50 @@ import { useQuery } from '@tanstack/react-query';
|
|
|
2
2
|
import { useState } from 'react';
|
|
3
3
|
import 'url-search-params-polyfill';
|
|
4
4
|
import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
|
|
5
|
+
import { useQueryConfig } from '../config/useQueryConfig.js';
|
|
5
6
|
import { useQueryHeaders } from '../config/useQueryHeaders.js';
|
|
6
7
|
import 'axios';
|
|
7
8
|
import { makeRequest } from '../request/make-request.js';
|
|
8
|
-
import
|
|
9
|
+
import '../request/request.enum.js';
|
|
9
10
|
|
|
10
11
|
const useDeleteRequest = (deleteOptions) => {
|
|
11
12
|
const { baseUrl, headers } = deleteOptions ?? {};
|
|
12
|
-
const [requestPath,
|
|
13
|
+
const [requestPath, setRequestPath] = useState('');
|
|
13
14
|
const [options, setOptions] = useState();
|
|
15
|
+
const { options: queryConfigOptions } = useQueryConfig();
|
|
14
16
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
15
17
|
const { getHeaders } = useQueryHeaders();
|
|
16
18
|
const sendRequest = async (res, rej, queryKey) => {
|
|
17
19
|
// get request headers
|
|
18
20
|
const globalHeaders = getHeaders();
|
|
19
|
-
const [url] =
|
|
20
|
-
const
|
|
21
|
-
|
|
21
|
+
const [url] = queryKey;
|
|
22
|
+
const requestUrl = (url ?? requestPath);
|
|
23
|
+
const requestOptions = {
|
|
24
|
+
path: requestUrl,
|
|
22
25
|
headers: { ...globalHeaders, ...headers },
|
|
23
|
-
method: HttpMethod.DELETE,
|
|
24
26
|
baseURL: baseUrl ?? API_URL,
|
|
25
27
|
timeout: TIMEOUT,
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
};
|
|
29
|
+
let shouldContinue = true;
|
|
30
|
+
if (queryConfigOptions?.queryMiddleware) {
|
|
31
|
+
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
32
|
+
}
|
|
33
|
+
if (shouldContinue) {
|
|
34
|
+
const postResponse = await makeRequest(requestOptions);
|
|
35
|
+
if (postResponse.status) {
|
|
36
|
+
res(postResponse);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
rej(postResponse);
|
|
40
|
+
}
|
|
29
41
|
}
|
|
30
42
|
else {
|
|
31
|
-
rej(
|
|
43
|
+
rej(null);
|
|
32
44
|
}
|
|
33
45
|
};
|
|
34
46
|
const query = useQuery([requestPath, {}], ({ queryKey }) => new Promise((res, rej) => sendRequest(res, rej, queryKey)), { enabled: false, ...options });
|
|
35
47
|
const updatedPathAsync = async (link) => {
|
|
36
|
-
return
|
|
48
|
+
return setRequestPath(link);
|
|
37
49
|
};
|
|
38
50
|
const setOptionsAsync = async (fetchOptions) => {
|
|
39
51
|
return setOptions(fetchOptions);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -2,6 +2,7 @@ import { useQueryClient, useInfiniteQuery } from '@tanstack/react-query';
|
|
|
2
2
|
import { useState, useMemo, useEffect, startTransition } from 'react';
|
|
3
3
|
import 'url-search-params-polyfill';
|
|
4
4
|
import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
|
|
5
|
+
import { useQueryConfig } from '../config/useQueryConfig.js';
|
|
5
6
|
import { useQueryHeaders } from '../config/useQueryHeaders.js';
|
|
6
7
|
import 'axios';
|
|
7
8
|
import { makeRequest } from '../request/make-request.js';
|
|
@@ -10,26 +11,37 @@ import '../request/request.enum.js';
|
|
|
10
11
|
const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl, headers, }) => {
|
|
11
12
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
12
13
|
const { getHeaders } = useQueryHeaders();
|
|
13
|
-
const [requestPath,
|
|
14
|
+
const [requestPath, setRequestPath] = useState(path);
|
|
14
15
|
const [options, setOptions] = useState(queryOptions);
|
|
16
|
+
const { options: queryConfigOptions } = useQueryConfig();
|
|
15
17
|
let queryClient = useQueryClient();
|
|
16
18
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
17
19
|
queryClient = useMemo(() => queryClient, []);
|
|
18
|
-
const sendRequest = async (res, rej, pageParam) => {
|
|
20
|
+
const sendRequest = async (res, rej, queryKey, pageParam) => {
|
|
19
21
|
if (load) {
|
|
20
22
|
// get request headers
|
|
21
23
|
const globalHeaders = getHeaders();
|
|
22
|
-
const
|
|
24
|
+
const requestOptions = {
|
|
23
25
|
path: pageParam ?? requestPath,
|
|
24
26
|
headers: { ...globalHeaders, ...headers },
|
|
25
27
|
baseURL: baseUrl ?? API_URL,
|
|
26
28
|
timeout: TIMEOUT,
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
};
|
|
30
|
+
let shouldContinue = true;
|
|
31
|
+
if (queryConfigOptions?.queryMiddleware) {
|
|
32
|
+
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
33
|
+
}
|
|
34
|
+
if (shouldContinue) {
|
|
35
|
+
const getResponse = await makeRequest(requestOptions);
|
|
36
|
+
if (getResponse.status) {
|
|
37
|
+
res(getResponse);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
rej(getResponse);
|
|
41
|
+
}
|
|
30
42
|
}
|
|
31
43
|
else {
|
|
32
|
-
rej(
|
|
44
|
+
rej(null);
|
|
33
45
|
}
|
|
34
46
|
}
|
|
35
47
|
else {
|
|
@@ -47,7 +59,7 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
47
59
|
queryParams.set('page', String(lastPageItem));
|
|
48
60
|
return pathname + '?' + queryParams.toString();
|
|
49
61
|
};
|
|
50
|
-
const query = useInfiniteQuery([requestPath, {}], ({ pageParam = requestPath }) => new Promise((res, rej) => sendRequest(res, rej, pageParam)), {
|
|
62
|
+
const query = useInfiniteQuery([requestPath, {}], ({ pageParam = requestPath, queryKey }) => new Promise((res, rej) => sendRequest(res, rej, queryKey, pageParam)), {
|
|
51
63
|
enabled: load,
|
|
52
64
|
getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
|
|
53
65
|
getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
|
|
@@ -65,7 +77,7 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
65
77
|
};
|
|
66
78
|
const updatedPathAsync = async (link) => {
|
|
67
79
|
startTransition(() => {
|
|
68
|
-
|
|
80
|
+
setRequestPath(link);
|
|
69
81
|
});
|
|
70
82
|
};
|
|
71
83
|
useEffect(() => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useGetInfiniteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useGetInfiniteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -8,7 +8,7 @@ export declare const useGetRequest: <TResponse extends Record<string, any>>({ pa
|
|
|
8
8
|
queryOptions?: TanstackQueryOption<TResponse> | undefined;
|
|
9
9
|
keyTracker?: string | undefined;
|
|
10
10
|
} & DefaultRequestOptions) => {
|
|
11
|
-
|
|
11
|
+
setRequestPath: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
12
12
|
nextPage: () => void;
|
|
13
13
|
prevPage: () => void;
|
|
14
14
|
get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
@@ -41,7 +41,7 @@ export declare const useGetRequest: <TResponse extends Record<string, any>>({ pa
|
|
|
41
41
|
remove: () => void;
|
|
42
42
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
43
43
|
} | {
|
|
44
|
-
|
|
44
|
+
setRequestPath: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
45
45
|
nextPage: () => void;
|
|
46
46
|
prevPage: () => void;
|
|
47
47
|
get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
@@ -74,7 +74,7 @@ export declare const useGetRequest: <TResponse extends Record<string, any>>({ pa
|
|
|
74
74
|
remove: () => void;
|
|
75
75
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
76
76
|
} | {
|
|
77
|
-
|
|
77
|
+
setRequestPath: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
78
78
|
nextPage: () => void;
|
|
79
79
|
prevPage: () => void;
|
|
80
80
|
get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
@@ -107,7 +107,7 @@ export declare const useGetRequest: <TResponse extends Record<string, any>>({ pa
|
|
|
107
107
|
remove: () => void;
|
|
108
108
|
fetchStatus: import("@tanstack/react-query").FetchStatus;
|
|
109
109
|
} | {
|
|
110
|
-
|
|
110
|
+
setRequestPath: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
111
111
|
nextPage: () => void;
|
|
112
112
|
prevPage: () => void;
|
|
113
113
|
get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
@@ -2,17 +2,19 @@ import { useQueryClient, useQuery } from '@tanstack/react-query';
|
|
|
2
2
|
import { useState, useMemo, useEffect, startTransition } from 'react';
|
|
3
3
|
import 'url-search-params-polyfill';
|
|
4
4
|
import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
|
|
5
|
+
import { useQueryConfig } from '../config/useQueryConfig.js';
|
|
5
6
|
import { useQueryHeaders } from '../config/useQueryHeaders.js';
|
|
6
7
|
import 'axios';
|
|
7
8
|
import { makeRequest } from '../request/make-request.js';
|
|
8
9
|
import '../request/request.enum.js';
|
|
9
10
|
|
|
10
11
|
const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl, headers, }) => {
|
|
11
|
-
const [requestPath,
|
|
12
|
+
const [requestPath, setRequestPath] = useState(path);
|
|
12
13
|
const [options, setOptions] = useState(queryOptions);
|
|
13
14
|
const [page, setPage] = useState(1);
|
|
14
15
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
15
16
|
const { getHeaders } = useQueryHeaders();
|
|
17
|
+
const { options: queryConfigOptions } = useQueryConfig();
|
|
16
18
|
let queryClient = useQueryClient();
|
|
17
19
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
18
20
|
queryClient = useMemo(() => queryClient, []);
|
|
@@ -20,18 +22,29 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
|
|
|
20
22
|
if (load) {
|
|
21
23
|
// get request headers
|
|
22
24
|
const globalHeaders = getHeaders();
|
|
23
|
-
const [url] =
|
|
24
|
-
const
|
|
25
|
-
|
|
25
|
+
const [url] = queryKey;
|
|
26
|
+
const requestUrl = (url ?? requestPath);
|
|
27
|
+
const requestOptions = {
|
|
28
|
+
path: requestUrl,
|
|
26
29
|
headers: { ...globalHeaders, ...headers },
|
|
27
30
|
baseURL: baseUrl ?? API_URL,
|
|
28
31
|
timeout: TIMEOUT,
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
};
|
|
33
|
+
let shouldContinue = true;
|
|
34
|
+
if (queryConfigOptions?.queryMiddleware) {
|
|
35
|
+
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
36
|
+
}
|
|
37
|
+
if (shouldContinue) {
|
|
38
|
+
const getResponse = await makeRequest(requestOptions);
|
|
39
|
+
if (getResponse.status) {
|
|
40
|
+
res(getResponse);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
rej(getResponse);
|
|
44
|
+
}
|
|
32
45
|
}
|
|
33
46
|
else {
|
|
34
|
-
rej(
|
|
47
|
+
rej(null);
|
|
35
48
|
}
|
|
36
49
|
}
|
|
37
50
|
else {
|
|
@@ -44,7 +57,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
|
|
|
44
57
|
});
|
|
45
58
|
useEffect(() => {
|
|
46
59
|
if (path) {
|
|
47
|
-
|
|
60
|
+
setRequestPath(path);
|
|
48
61
|
}
|
|
49
62
|
}, [path]);
|
|
50
63
|
useEffect(() => {
|
|
@@ -61,7 +74,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
|
|
|
61
74
|
if (query.data?.data.pagination) {
|
|
62
75
|
const pagination = query.data.data.pagination;
|
|
63
76
|
if (pagination.next_page !== pagination.current_page && pagination.next_page > pagination.current_page) {
|
|
64
|
-
|
|
77
|
+
setRequestPath(constructPaginationLink(requestPath, pagination.next_page));
|
|
65
78
|
}
|
|
66
79
|
}
|
|
67
80
|
};
|
|
@@ -69,7 +82,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
|
|
|
69
82
|
if (query.data?.data.pagination) {
|
|
70
83
|
const pagination = query.data.data.pagination;
|
|
71
84
|
if (pagination.previous_page !== pagination.current_page && pagination.previous_page < pagination.current_page) {
|
|
72
|
-
|
|
85
|
+
setRequestPath(constructPaginationLink(requestPath, pagination.previous_page));
|
|
73
86
|
}
|
|
74
87
|
}
|
|
75
88
|
};
|
|
@@ -86,11 +99,11 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
|
|
|
86
99
|
return link;
|
|
87
100
|
};
|
|
88
101
|
const gotoPage = (pageNumber) => {
|
|
89
|
-
|
|
102
|
+
setRequestPath(constructPaginationLink(requestPath, pageNumber));
|
|
90
103
|
};
|
|
91
104
|
const updatedPathAsync = async (link) => {
|
|
92
105
|
startTransition(() => {
|
|
93
|
-
|
|
106
|
+
setRequestPath(link);
|
|
94
107
|
});
|
|
95
108
|
};
|
|
96
109
|
const setOptionsAsync = async (fetchOptions) => {
|
|
@@ -105,7 +118,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
|
|
|
105
118
|
};
|
|
106
119
|
return {
|
|
107
120
|
...query,
|
|
108
|
-
|
|
121
|
+
setRequestPath,
|
|
109
122
|
nextPage,
|
|
110
123
|
prevPage,
|
|
111
124
|
get,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useGetRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useGetRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useMutation } from '@tanstack/react-query';
|
|
2
2
|
import 'url-search-params-polyfill';
|
|
3
3
|
import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
|
|
4
|
+
import { useQueryConfig } from '../config/useQueryConfig.js';
|
|
4
5
|
import { useQueryHeaders } from '../config/useQueryHeaders.js';
|
|
5
6
|
import { scrollToTop } from '../helpers/scrollToTop.js';
|
|
6
7
|
import { useUploadProgress } from '../hooks/useUploadProgress.js';
|
|
@@ -12,12 +13,11 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
12
13
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
13
14
|
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
14
15
|
const { getHeaders } = useQueryHeaders();
|
|
15
|
-
const
|
|
16
|
-
const config = queryClient.getQueryData(['config']);
|
|
16
|
+
const config = useQueryConfig();
|
|
17
17
|
const sendRequest = async (res, rej, data) => {
|
|
18
18
|
// get request headers
|
|
19
19
|
const globalHeaders = getHeaders();
|
|
20
|
-
const
|
|
20
|
+
const requestOptions = {
|
|
21
21
|
path: path,
|
|
22
22
|
body: data,
|
|
23
23
|
method: HttpMethod.PATCH,
|
|
@@ -25,26 +25,39 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
25
25
|
baseURL: baseUrl ?? API_URL,
|
|
26
26
|
timeout: TIMEOUT,
|
|
27
27
|
onUploadProgress,
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
};
|
|
29
|
+
let shouldContinue = true;
|
|
30
|
+
if (config.options?.mutationMiddleware) {
|
|
31
|
+
shouldContinue = await config.options.mutationMiddleware({
|
|
32
|
+
mutationKey: [path, { type: 'mutation' }],
|
|
33
|
+
...requestOptions,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
if (shouldContinue) {
|
|
37
|
+
const patchResponse = await makeRequest(requestOptions);
|
|
38
|
+
if (patchResponse.status) {
|
|
39
|
+
// scroll to top after success
|
|
40
|
+
if (config.options?.context !== 'app') {
|
|
41
|
+
scrollToTop();
|
|
42
|
+
}
|
|
43
|
+
res(patchResponse);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
// scroll to top after error
|
|
47
|
+
if (config.options?.context !== 'app') {
|
|
48
|
+
scrollToTop();
|
|
49
|
+
}
|
|
50
|
+
rej(patchResponse);
|
|
33
51
|
}
|
|
34
|
-
res(patchResponse);
|
|
35
52
|
}
|
|
36
53
|
else {
|
|
37
|
-
|
|
38
|
-
if (config?.options?.context !== 'app') {
|
|
39
|
-
scrollToTop();
|
|
40
|
-
}
|
|
41
|
-
rej(patchResponse);
|
|
54
|
+
rej(null);
|
|
42
55
|
}
|
|
43
56
|
};
|
|
44
57
|
// register post mutation
|
|
45
58
|
const mutation = useMutation((dataData) => new Promise((res, rej) => {
|
|
46
59
|
return sendRequest(res, rej, dataData);
|
|
47
|
-
}));
|
|
60
|
+
}), { mutationKey: [path, { type: 'mutation' }] });
|
|
48
61
|
const patch = async (data, options) => {
|
|
49
62
|
return mutation.mutateAsync(data, options);
|
|
50
63
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useMutation } from '@tanstack/react-query';
|
|
2
2
|
import 'url-search-params-polyfill';
|
|
3
3
|
import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
|
|
4
|
+
import { useQueryConfig } from '../config/useQueryConfig.js';
|
|
4
5
|
import { useQueryHeaders } from '../config/useQueryHeaders.js';
|
|
5
6
|
import { useReactNativeEnv } from '../config/useReactNativeEnv.js';
|
|
6
7
|
import { scrollToTop } from '../helpers/scrollToTop.js';
|
|
@@ -11,17 +12,16 @@ import { HttpMethod } from '../request/request.enum.js';
|
|
|
11
12
|
|
|
12
13
|
const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelectors, }) => {
|
|
13
14
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
14
|
-
const
|
|
15
|
+
const config = useQueryConfig();
|
|
15
16
|
const { getHeaders } = useQueryHeaders();
|
|
16
17
|
const { isApp } = useReactNativeEnv();
|
|
17
18
|
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
18
19
|
const sendRequest = async (res, rej, postData) => {
|
|
19
20
|
// get request headers
|
|
20
21
|
const globalHeaders = getHeaders();
|
|
21
|
-
const config = queryClient.getQueryData(['config']);
|
|
22
22
|
const { data, requestConfig } = postData;
|
|
23
23
|
delete requestConfig?.body;
|
|
24
|
-
const
|
|
24
|
+
const requestOptions = {
|
|
25
25
|
path,
|
|
26
26
|
body: data,
|
|
27
27
|
method: HttpMethod.POST,
|
|
@@ -35,24 +35,39 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
35
35
|
},
|
|
36
36
|
onUploadProgress,
|
|
37
37
|
...requestConfig,
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
};
|
|
39
|
+
let shouldContinue = true;
|
|
40
|
+
if (config.options?.mutationMiddleware) {
|
|
41
|
+
shouldContinue = await config.options.mutationMiddleware({
|
|
42
|
+
mutationKey: [path, { type: 'mutation' }],
|
|
43
|
+
...requestOptions,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (shouldContinue) {
|
|
47
|
+
const postResponse = await makeRequest(requestOptions);
|
|
48
|
+
if (postResponse.status) {
|
|
49
|
+
// scroll to top after success
|
|
50
|
+
if (config.options?.context !== 'app') {
|
|
51
|
+
scrollToTop();
|
|
52
|
+
}
|
|
53
|
+
res(postResponse);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
// scroll to top after error
|
|
57
|
+
if (config.options?.context !== 'app') {
|
|
58
|
+
scrollToTop();
|
|
59
|
+
}
|
|
60
|
+
rej(postResponse);
|
|
43
61
|
}
|
|
44
|
-
res(postResponse);
|
|
45
62
|
}
|
|
46
63
|
else {
|
|
47
|
-
|
|
48
|
-
if (config?.options?.context !== 'app') {
|
|
49
|
-
scrollToTop();
|
|
50
|
-
}
|
|
51
|
-
rej(postResponse);
|
|
64
|
+
rej(null);
|
|
52
65
|
}
|
|
53
66
|
};
|
|
54
67
|
// register post mutation
|
|
55
|
-
const mutation = useMutation(async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData))
|
|
68
|
+
const mutation = useMutation(async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)), {
|
|
69
|
+
mutationKey: [path, { type: 'mutation' }],
|
|
70
|
+
});
|
|
56
71
|
const post = async (data, options) => {
|
|
57
72
|
const { requestConfig, ...otherOptions } = options ?? {};
|
|
58
73
|
return mutation.mutateAsync({ data, requestConfig }, otherOptions);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import type { QueryKey } from '@tanstack/react-query';
|
|
1
2
|
import type { RawAxiosRequestHeaders } from 'axios';
|
|
3
|
+
import type { IMakeRequest } from '../request';
|
|
2
4
|
export interface BootstrapConfig {
|
|
3
5
|
environments?: {
|
|
4
6
|
appBaseUrl: string;
|
|
@@ -6,6 +8,12 @@ export interface BootstrapConfig {
|
|
|
6
8
|
};
|
|
7
9
|
context?: ContextType;
|
|
8
10
|
modelConfig?: BootstrapModelConfig;
|
|
11
|
+
mutationMiddleware?: (mutateRequestConfig?: IMakeRequest & {
|
|
12
|
+
mutationKey: QueryKey;
|
|
13
|
+
}) => Promise<boolean>;
|
|
14
|
+
queryMiddleware?: (queryRequestConfig?: IMakeRequest & {
|
|
15
|
+
queryKey: QueryKey;
|
|
16
|
+
}) => Promise<boolean>;
|
|
9
17
|
}
|
|
10
18
|
export interface BootstrapModelConfig {
|
|
11
19
|
idColumn: string;
|
package/package.json
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
import type { QueryClient } from '@tanstack/react-query';
|
|
2
2
|
import 'url-search-params-polyfill';
|
|
3
|
-
import type { BootstrapConfig
|
|
3
|
+
import type { BootstrapConfig } from '../types';
|
|
4
4
|
|
|
5
5
|
export const bootstrapQueryRequest = (queryClient: QueryClient, options?: BootstrapConfig): void => {
|
|
6
6
|
// make query config doesn't expire
|
|
7
|
-
queryClient.setQueryDefaults(['config'], {
|
|
8
|
-
staleTime: Infinity,
|
|
9
|
-
cacheTime: Infinity,
|
|
10
|
-
});
|
|
11
7
|
|
|
12
8
|
// set default query config
|
|
13
|
-
|
|
9
|
+
const defaultMeta = {
|
|
14
10
|
headers: {
|
|
15
11
|
Authorization: ``,
|
|
16
12
|
},
|
|
17
13
|
options,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
queryClient.setDefaultOptions({
|
|
17
|
+
queries: {
|
|
18
|
+
meta: defaultMeta,
|
|
19
|
+
},
|
|
20
|
+
mutations: { meta: defaultMeta },
|
|
18
21
|
});
|
|
19
22
|
};
|
|
@@ -4,7 +4,8 @@ import type { TanstackQueryConfig } from '../types';
|
|
|
4
4
|
export const useQueryConfig = (): TanstackQueryConfig => {
|
|
5
5
|
const queryClient = useQueryClient();
|
|
6
6
|
|
|
7
|
-
const
|
|
7
|
+
const mutationMeta = (queryClient.getDefaultOptions().mutations?.meta ?? {}) as unknown as TanstackQueryConfig;
|
|
8
|
+
const queryMeta = (queryClient.getDefaultOptions().queries?.meta ?? {}) as unknown as TanstackQueryConfig;
|
|
8
9
|
|
|
9
|
-
return {
|
|
10
|
+
return { ...queryMeta, ...mutationMeta };
|
|
10
11
|
};
|
|
@@ -1,33 +1,27 @@
|
|
|
1
1
|
import { useQueryClient } from '@tanstack/react-query';
|
|
2
|
-
import { getDateInFuture } from '../helpers';
|
|
3
2
|
import type { IUseQueryHeaders, TanstackQueryConfig } from '../types';
|
|
3
|
+
import { useQueryConfig } from './useQueryConfig';
|
|
4
4
|
|
|
5
5
|
export const useQueryHeaders = (): IUseQueryHeaders => {
|
|
6
6
|
const queryClient = useQueryClient();
|
|
7
|
+
const { headers, options } = useQueryConfig();
|
|
7
8
|
|
|
8
9
|
const getHeaders = (): TanstackQueryConfig['headers'] => {
|
|
9
|
-
|
|
10
|
-
return config.headers;
|
|
10
|
+
return headers;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
const setQueryHeaders = (newHeaders: TanstackQueryConfig['headers']) => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
});
|
|
14
|
+
const defaultMeta = {
|
|
15
|
+
headers: { ...headers, ...newHeaders },
|
|
16
|
+
options,
|
|
17
|
+
};
|
|
19
18
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
(config): TanstackQueryConfig => {
|
|
24
|
-
const newConfig = { ...config, headers: newHeaders };
|
|
25
|
-
return newConfig;
|
|
19
|
+
queryClient.setDefaultOptions({
|
|
20
|
+
queries: {
|
|
21
|
+
meta: defaultMeta,
|
|
26
22
|
},
|
|
27
|
-
{
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
);
|
|
23
|
+
mutations: { meta: defaultMeta },
|
|
24
|
+
});
|
|
31
25
|
};
|
|
32
26
|
|
|
33
27
|
return { setQueryHeaders, getHeaders };
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { TanstackQueryConfig } from '../types';
|
|
1
|
+
import { useQueryConfig } from './useQueryConfig';
|
|
3
2
|
|
|
4
3
|
export const useReactNativeEnv = () => {
|
|
5
|
-
const
|
|
6
|
-
const config = queryClient.getQueryData<TanstackQueryConfig>(['config']);
|
|
4
|
+
const config = useQueryConfig();
|
|
7
5
|
|
|
8
|
-
const appUrl: string | undefined = config
|
|
9
|
-
const appTimeout: number | undefined = config
|
|
10
|
-
const isApp = config
|
|
6
|
+
const appUrl: string | undefined = config.options?.environments?.appBaseUrl;
|
|
7
|
+
const appTimeout: number | undefined = config.options?.environments?.appTimeout;
|
|
8
|
+
const isApp = config.options?.context === 'app';
|
|
11
9
|
|
|
12
10
|
return { appUrl, appTimeout, isApp };
|
|
13
11
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useQueryClient } from '@tanstack/react-query';
|
|
2
2
|
import result from 'lodash.result';
|
|
3
3
|
import { default as lodashSet } from 'lodash.set';
|
|
4
|
-
import
|
|
4
|
+
import { useQueryConfig } from '../config';
|
|
5
5
|
import type { QueryModelAddPosition, QueryModelBuilder } from './model.interface';
|
|
6
6
|
import { useKeyTrackerModel } from './useKeyTrackerModel';
|
|
7
7
|
|
|
@@ -9,6 +9,7 @@ export const useQueryModel = <T>(keyTracker: string, exact: boolean = true): Que
|
|
|
9
9
|
const queryClient = useQueryClient();
|
|
10
10
|
const { getQueryKey } = useKeyTrackerModel(keyTracker);
|
|
11
11
|
const queryKey = getQueryKey() as any[];
|
|
12
|
+
const config = useQueryConfig();
|
|
12
13
|
|
|
13
14
|
const add = (data: T, position?: QueryModelAddPosition, path?: string): T | undefined => {
|
|
14
15
|
let records = (findAll(path) ?? []) as T[];
|
|
@@ -78,7 +79,7 @@ export const useQueryModel = <T>(keyTracker: string, exact: boolean = true): Que
|
|
|
78
79
|
};
|
|
79
80
|
|
|
80
81
|
const getModelConfig = () => {
|
|
81
|
-
const { options } =
|
|
82
|
+
const { options } = config;
|
|
82
83
|
const { modelConfig } = options ?? {};
|
|
83
84
|
|
|
84
85
|
return modelConfig;
|