@ventlio/tanstack-query 0.2.89 → 0.3.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/config/useQueryConfig.js +1 -27
- package/dist/config/useQueryConfig.js.map +1 -1
- package/dist/config/useQueryHeaders.js +15 -4
- package/dist/config/useQueryHeaders.js.map +1 -1
- package/dist/config/useReactNativeEnv.js +3 -3
- package/dist/index.mjs +64 -154
- package/dist/index.mjs.map +1 -1
- package/dist/model/useKeyTrackerModel.d.ts +1 -1
- package/dist/model/useKeyTrackerModel.js +1 -5
- package/dist/model/useKeyTrackerModel.js.map +1 -1
- package/dist/queries/useDeleteRequest.d.ts +4 -4
- package/dist/queries/useDeleteRequest.js +9 -24
- package/dist/queries/useDeleteRequest.js.map +1 -1
- package/dist/queries/useGetInfiniteRequest.d.ts +4 -4
- package/dist/queries/useGetInfiniteRequest.js +13 -24
- package/dist/queries/useGetInfiniteRequest.js.map +1 -1
- package/dist/queries/useGetRequest.d.ts +4 -4
- package/dist/queries/useGetRequest.js +12 -23
- package/dist/queries/useGetRequest.js.map +1 -1
- package/dist/queries/usePatchRequest.d.ts +8 -8
- package/dist/queries/usePatchRequest.js +5 -25
- package/dist/queries/usePatchRequest.js.map +1 -1
- package/dist/queries/usePostRequest.d.ts +4 -4
- package/dist/queries/usePostRequest.js +5 -20
- package/dist/queries/usePostRequest.js.map +1 -1
- package/dist/types/index.d.ts +4 -7
- package/package.json +1 -1
- package/src/config/useQueryConfig.ts +4 -41
- package/src/config/useQueryHeaders.ts +18 -6
- package/src/config/useReactNativeEnv.ts +3 -3
- package/src/model/useKeyTrackerModel.ts +1 -5
- package/src/queries/useDeleteRequest.ts +11 -25
- package/src/queries/useGetInfiniteRequest.ts +15 -25
- package/src/queries/useGetRequest.ts +15 -25
- package/src/queries/usePatchRequest.ts +8 -27
- package/src/queries/usePostRequest.ts +8 -22
- package/src/types/index.ts +4 -7
|
@@ -17,10 +17,10 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
17
17
|
}> | undefined;
|
|
18
18
|
keyTracker?: string | undefined;
|
|
19
19
|
} & DefaultRequestOptions) => {
|
|
20
|
-
isLoading: boolean | undefined;
|
|
21
20
|
data: undefined;
|
|
22
21
|
error: any;
|
|
23
22
|
isError: true;
|
|
23
|
+
isLoading: false;
|
|
24
24
|
isLoadingError: true;
|
|
25
25
|
isRefetchError: false;
|
|
26
26
|
isSuccess: false;
|
|
@@ -58,10 +58,10 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
58
58
|
pagination: Pagination;
|
|
59
59
|
}>> | undefined>;
|
|
60
60
|
} | {
|
|
61
|
-
isLoading: boolean | undefined;
|
|
62
61
|
data: undefined;
|
|
63
62
|
error: null;
|
|
64
63
|
isError: false;
|
|
64
|
+
isLoading: true;
|
|
65
65
|
isLoadingError: false;
|
|
66
66
|
isRefetchError: false;
|
|
67
67
|
isSuccess: false;
|
|
@@ -99,12 +99,12 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
99
99
|
pagination: Pagination;
|
|
100
100
|
}>> | undefined>;
|
|
101
101
|
} | {
|
|
102
|
-
isLoading: boolean | undefined;
|
|
103
102
|
data: InfiniteData<IRequestSuccess<TResponse & {
|
|
104
103
|
pagination: Pagination;
|
|
105
104
|
}>>;
|
|
106
105
|
error: any;
|
|
107
106
|
isError: true;
|
|
107
|
+
isLoading: false;
|
|
108
108
|
isLoadingError: false;
|
|
109
109
|
isRefetchError: true;
|
|
110
110
|
isSuccess: false;
|
|
@@ -142,12 +142,12 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
142
142
|
pagination: Pagination;
|
|
143
143
|
}>> | undefined>;
|
|
144
144
|
} | {
|
|
145
|
-
isLoading: boolean | undefined;
|
|
146
145
|
data: InfiniteData<IRequestSuccess<TResponse & {
|
|
147
146
|
pagination: Pagination;
|
|
148
147
|
}>>;
|
|
149
148
|
error: null;
|
|
150
149
|
isError: false;
|
|
150
|
+
isLoading: false;
|
|
151
151
|
isLoadingError: false;
|
|
152
152
|
isRefetchError: false;
|
|
153
153
|
isSuccess: true;
|
|
@@ -12,9 +12,8 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
12
12
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
13
13
|
const { getHeaders } = useQueryHeaders();
|
|
14
14
|
const [requestPath, setRequestPath] = useState(path);
|
|
15
|
-
const [queryConfig, setQueryConfig] = useState();
|
|
16
15
|
const [options, setOptions] = useState(queryOptions);
|
|
17
|
-
const { options: queryConfigOptions
|
|
16
|
+
const { options: queryConfigOptions } = useQueryConfig();
|
|
18
17
|
let queryClient = useQueryClient();
|
|
19
18
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
20
19
|
queryClient = useMemo(() => queryClient, []);
|
|
@@ -29,7 +28,7 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
29
28
|
timeout: TIMEOUT,
|
|
30
29
|
};
|
|
31
30
|
let shouldContinue = true;
|
|
32
|
-
if (queryConfigOptions
|
|
31
|
+
if (queryConfigOptions?.queryMiddleware) {
|
|
33
32
|
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
34
33
|
}
|
|
35
34
|
if (shouldContinue) {
|
|
@@ -61,7 +60,7 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
61
60
|
return pathname + '?' + queryParams.toString();
|
|
62
61
|
};
|
|
63
62
|
const query = useInfiniteQuery([requestPath, {}], ({ pageParam = requestPath, queryKey }) => new Promise((res, rej) => sendRequest(res, rej, queryKey, pageParam)), {
|
|
64
|
-
enabled: load
|
|
63
|
+
enabled: load,
|
|
65
64
|
getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
|
|
66
65
|
getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
|
|
67
66
|
...options,
|
|
@@ -72,15 +71,9 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
72
71
|
});
|
|
73
72
|
};
|
|
74
73
|
const get = async (link, fetchOptions) => {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return query.data;
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
setQueryConfig({ link, fetchOptions });
|
|
82
|
-
return undefined;
|
|
83
|
-
}
|
|
74
|
+
await setOptionsAsync(fetchOptions);
|
|
75
|
+
await updatedPathAsync(link);
|
|
76
|
+
return query.data;
|
|
84
77
|
};
|
|
85
78
|
const updatedPathAsync = async (link) => {
|
|
86
79
|
startTransition(() => {
|
|
@@ -89,21 +82,17 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
89
82
|
};
|
|
90
83
|
useEffect(() => {
|
|
91
84
|
if (keyTracker) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
get(queryConfig.link, queryConfig.fetchOptions);
|
|
99
|
-
setQueryConfig(undefined);
|
|
85
|
+
// set expiration time for the tracker
|
|
86
|
+
queryClient.setQueryDefaults([keyTracker], {
|
|
87
|
+
cacheTime: Infinity,
|
|
88
|
+
staleTime: Infinity,
|
|
89
|
+
});
|
|
90
|
+
queryClient.setQueryData([keyTracker], [requestPath, {}]);
|
|
100
91
|
}
|
|
101
|
-
|
|
102
|
-
}, [queryConfigOptions.pauseFutureQueries]);
|
|
92
|
+
}, [keyTracker, requestPath, queryClient, queryOptions?.staleTime]);
|
|
103
93
|
return {
|
|
104
94
|
get,
|
|
105
95
|
...query,
|
|
106
|
-
isLoading: query.isLoading || queryConfigOptions.pauseFutureQueries,
|
|
107
96
|
};
|
|
108
97
|
};
|
|
109
98
|
|
|
@@ -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,6 @@ 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
|
-
isLoading: boolean | undefined;
|
|
12
11
|
setRequestPath: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
13
12
|
nextPage: () => void;
|
|
14
13
|
prevPage: () => void;
|
|
@@ -19,6 +18,7 @@ export declare const useGetRequest: <TResponse extends Record<string, any>>({ pa
|
|
|
19
18
|
data: IRequestSuccess<TResponse>;
|
|
20
19
|
error: any;
|
|
21
20
|
isError: true;
|
|
21
|
+
isLoading: false;
|
|
22
22
|
isLoadingError: false;
|
|
23
23
|
isRefetchError: true;
|
|
24
24
|
isSuccess: false;
|
|
@@ -41,7 +41,6 @@ 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
|
-
isLoading: boolean | undefined;
|
|
45
44
|
setRequestPath: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
46
45
|
nextPage: () => void;
|
|
47
46
|
prevPage: () => void;
|
|
@@ -52,6 +51,7 @@ export declare const useGetRequest: <TResponse extends Record<string, any>>({ pa
|
|
|
52
51
|
data: IRequestSuccess<TResponse>;
|
|
53
52
|
error: null;
|
|
54
53
|
isError: false;
|
|
54
|
+
isLoading: false;
|
|
55
55
|
isLoadingError: false;
|
|
56
56
|
isRefetchError: false;
|
|
57
57
|
isSuccess: true;
|
|
@@ -74,7 +74,6 @@ 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
|
-
isLoading: boolean | undefined;
|
|
78
77
|
setRequestPath: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
79
78
|
nextPage: () => void;
|
|
80
79
|
prevPage: () => void;
|
|
@@ -85,6 +84,7 @@ export declare const useGetRequest: <TResponse extends Record<string, any>>({ pa
|
|
|
85
84
|
data: undefined;
|
|
86
85
|
error: any;
|
|
87
86
|
isError: true;
|
|
87
|
+
isLoading: false;
|
|
88
88
|
isLoadingError: true;
|
|
89
89
|
isRefetchError: false;
|
|
90
90
|
isSuccess: false;
|
|
@@ -107,7 +107,6 @@ 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
|
-
isLoading: boolean | undefined;
|
|
111
110
|
setRequestPath: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
112
111
|
nextPage: () => void;
|
|
113
112
|
prevPage: () => void;
|
|
@@ -118,6 +117,7 @@ export declare const useGetRequest: <TResponse extends Record<string, any>>({ pa
|
|
|
118
117
|
data: undefined;
|
|
119
118
|
error: null;
|
|
120
119
|
isError: false;
|
|
120
|
+
isLoading: true;
|
|
121
121
|
isLoadingError: false;
|
|
122
122
|
isRefetchError: false;
|
|
123
123
|
isSuccess: false;
|
|
@@ -14,8 +14,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
|
|
|
14
14
|
const [page, setPage] = useState(1);
|
|
15
15
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
16
16
|
const { getHeaders } = useQueryHeaders();
|
|
17
|
-
const { options: queryConfigOptions
|
|
18
|
-
const [queryConfig, setQueryConfig] = useState();
|
|
17
|
+
const { options: queryConfigOptions } = useQueryConfig();
|
|
19
18
|
let queryClient = useQueryClient();
|
|
20
19
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
21
20
|
queryClient = useMemo(() => queryClient, []);
|
|
@@ -32,7 +31,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
|
|
|
32
31
|
timeout: TIMEOUT,
|
|
33
32
|
};
|
|
34
33
|
let shouldContinue = true;
|
|
35
|
-
if (queryConfigOptions
|
|
34
|
+
if (queryConfigOptions?.queryMiddleware) {
|
|
36
35
|
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
37
36
|
}
|
|
38
37
|
if (shouldContinue) {
|
|
@@ -63,10 +62,14 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
|
|
|
63
62
|
}, [path]);
|
|
64
63
|
useEffect(() => {
|
|
65
64
|
if (keyTracker) {
|
|
66
|
-
|
|
65
|
+
// set expiration time for the tracker
|
|
66
|
+
queryClient.setQueryDefaults([keyTracker], {
|
|
67
|
+
cacheTime: Infinity,
|
|
68
|
+
staleTime: Infinity,
|
|
69
|
+
});
|
|
70
|
+
queryClient.setQueryData([keyTracker], [requestPath, {}]);
|
|
67
71
|
}
|
|
68
|
-
|
|
69
|
-
}, [keyTracker, requestPath]);
|
|
72
|
+
}, [keyTracker, requestPath, queryClient, queryOptions?.staleTime]);
|
|
70
73
|
const nextPage = () => {
|
|
71
74
|
if (query.data?.data.pagination) {
|
|
72
75
|
const pagination = query.data.data.pagination;
|
|
@@ -109,26 +112,12 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
|
|
|
109
112
|
});
|
|
110
113
|
};
|
|
111
114
|
const get = async (link, fetchOptions) => {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return query.data;
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
setQueryConfig({ link, fetchOptions });
|
|
119
|
-
return undefined;
|
|
120
|
-
}
|
|
115
|
+
await setOptionsAsync(fetchOptions);
|
|
116
|
+
await updatedPathAsync(link);
|
|
117
|
+
return query.data;
|
|
121
118
|
};
|
|
122
|
-
useEffect(() => {
|
|
123
|
-
if (!queryConfigOptions.pauseFutureQueries && queryConfig) {
|
|
124
|
-
get(queryConfig.link, queryConfig.fetchOptions);
|
|
125
|
-
setQueryConfig(undefined);
|
|
126
|
-
}
|
|
127
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
128
|
-
}, [queryConfigOptions.pauseFutureQueries]);
|
|
129
119
|
return {
|
|
130
120
|
...query,
|
|
131
|
-
isLoading: query.isLoading || queryConfigOptions.pauseFutureQueries,
|
|
132
121
|
setRequestPath,
|
|
133
122
|
nextPage,
|
|
134
123
|
prevPage,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useGetRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useGetRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -4,11 +4,11 @@ import type { DefaultRequestOptions } from './queries.interface';
|
|
|
4
4
|
export declare const usePatchRequest: <TResponse>({ path, baseUrl, headers }: {
|
|
5
5
|
path: string;
|
|
6
6
|
} & DefaultRequestOptions) => {
|
|
7
|
-
isLoading: boolean | undefined;
|
|
8
7
|
data: undefined;
|
|
9
8
|
error: null;
|
|
10
9
|
isError: false;
|
|
11
10
|
isIdle: true;
|
|
11
|
+
isLoading: false;
|
|
12
12
|
isSuccess: false;
|
|
13
13
|
status: "idle";
|
|
14
14
|
mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
@@ -19,14 +19,14 @@ export declare const usePatchRequest: <TResponse>({ path, baseUrl, headers }: {
|
|
|
19
19
|
isPaused: boolean;
|
|
20
20
|
variables: void | undefined;
|
|
21
21
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
22
|
-
patch: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse
|
|
22
|
+
patch: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
23
23
|
uploadProgressPercent: number;
|
|
24
24
|
} | {
|
|
25
|
-
isLoading: boolean | undefined;
|
|
26
25
|
data: undefined;
|
|
27
26
|
error: null;
|
|
28
27
|
isError: false;
|
|
29
28
|
isIdle: false;
|
|
29
|
+
isLoading: true;
|
|
30
30
|
isSuccess: false;
|
|
31
31
|
status: "loading";
|
|
32
32
|
mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
@@ -37,14 +37,14 @@ export declare const usePatchRequest: <TResponse>({ path, baseUrl, headers }: {
|
|
|
37
37
|
isPaused: boolean;
|
|
38
38
|
variables: void | undefined;
|
|
39
39
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
40
|
-
patch: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse
|
|
40
|
+
patch: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
41
41
|
uploadProgressPercent: number;
|
|
42
42
|
} | {
|
|
43
|
-
isLoading: boolean | undefined;
|
|
44
43
|
data: undefined;
|
|
45
44
|
error: IRequestError;
|
|
46
45
|
isError: true;
|
|
47
46
|
isIdle: false;
|
|
47
|
+
isLoading: false;
|
|
48
48
|
isSuccess: false;
|
|
49
49
|
status: "error";
|
|
50
50
|
mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
@@ -55,14 +55,14 @@ export declare const usePatchRequest: <TResponse>({ path, baseUrl, headers }: {
|
|
|
55
55
|
isPaused: boolean;
|
|
56
56
|
variables: void | undefined;
|
|
57
57
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
58
|
-
patch: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse
|
|
58
|
+
patch: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
59
59
|
uploadProgressPercent: number;
|
|
60
60
|
} | {
|
|
61
|
-
isLoading: boolean | undefined;
|
|
62
61
|
data: IRequestSuccess<TResponse>;
|
|
63
62
|
error: null;
|
|
64
63
|
isError: false;
|
|
65
64
|
isIdle: false;
|
|
65
|
+
isLoading: false;
|
|
66
66
|
isSuccess: true;
|
|
67
67
|
status: "success";
|
|
68
68
|
mutate: import("@tanstack/react-query").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
@@ -73,6 +73,6 @@ export declare const usePatchRequest: <TResponse>({ path, baseUrl, headers }: {
|
|
|
73
73
|
isPaused: boolean;
|
|
74
74
|
variables: void | undefined;
|
|
75
75
|
mutateAsync: import("@tanstack/react-query").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
76
|
-
patch: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse
|
|
76
|
+
patch: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
77
77
|
uploadProgressPercent: number;
|
|
78
78
|
};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { useMutation } from '@tanstack/react-query';
|
|
2
|
-
import { useState, useEffect } from 'react';
|
|
3
2
|
import 'url-search-params-polyfill';
|
|
4
3
|
import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
|
|
5
4
|
import { useQueryConfig } from '../config/useQueryConfig.js';
|
|
@@ -13,7 +12,6 @@ import { HttpMethod } from '../request/request.enum.js';
|
|
|
13
12
|
const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
14
13
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
15
14
|
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
16
|
-
const [mutationConfig, setMutationConfig] = useState();
|
|
17
15
|
const { getHeaders } = useQueryHeaders();
|
|
18
16
|
const config = useQueryConfig();
|
|
19
17
|
const sendRequest = async (res, rej, data) => {
|
|
@@ -29,7 +27,7 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
29
27
|
onUploadProgress,
|
|
30
28
|
};
|
|
31
29
|
let shouldContinue = true;
|
|
32
|
-
if (config.options
|
|
30
|
+
if (config.options?.mutationMiddleware) {
|
|
33
31
|
shouldContinue = await config.options.mutationMiddleware({
|
|
34
32
|
mutationKey: [path, { type: 'mutation' }],
|
|
35
33
|
...requestOptions,
|
|
@@ -39,14 +37,14 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
39
37
|
const patchResponse = await makeRequest(requestOptions);
|
|
40
38
|
if (patchResponse.status) {
|
|
41
39
|
// scroll to top after success
|
|
42
|
-
if (config.options
|
|
40
|
+
if (config.options?.context !== 'app') {
|
|
43
41
|
scrollToTop();
|
|
44
42
|
}
|
|
45
43
|
res(patchResponse);
|
|
46
44
|
}
|
|
47
45
|
else {
|
|
48
46
|
// scroll to top after error
|
|
49
|
-
if (config.options
|
|
47
|
+
if (config.options?.context !== 'app') {
|
|
50
48
|
scrollToTop();
|
|
51
49
|
}
|
|
52
50
|
rej(patchResponse);
|
|
@@ -61,27 +59,9 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
61
59
|
return sendRequest(res, rej, dataData);
|
|
62
60
|
}), { mutationKey: [path, { type: 'mutation' }] });
|
|
63
61
|
const patch = async (data, options) => {
|
|
64
|
-
|
|
65
|
-
return mutation.mutateAsync(data, options);
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
setMutationConfig({ data, options });
|
|
69
|
-
return undefined;
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
useEffect(() => {
|
|
73
|
-
if (!config.options.pauseFutureMutations && mutationConfig) {
|
|
74
|
-
patch(mutationConfig.data, mutationConfig.options);
|
|
75
|
-
setMutationConfig(undefined);
|
|
76
|
-
}
|
|
77
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
78
|
-
}, [config.options.pauseFutureMutations]);
|
|
79
|
-
return {
|
|
80
|
-
patch,
|
|
81
|
-
uploadProgressPercent,
|
|
82
|
-
...mutation,
|
|
83
|
-
isLoading: mutation.isLoading || config.options.pauseFutureMutations,
|
|
62
|
+
return mutation.mutateAsync(data, options);
|
|
84
63
|
};
|
|
64
|
+
return { patch, uploadProgressPercent, ...mutation };
|
|
85
65
|
};
|
|
86
66
|
|
|
87
67
|
export { usePatchRequest };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -39,7 +39,7 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, he
|
|
|
39
39
|
requestConfig?: Partial<Omit<IMakeRequest, "body">> | undefined;
|
|
40
40
|
} & {
|
|
41
41
|
requestConfig?: Partial<Omit<IMakeRequest, "body">> | undefined;
|
|
42
|
-
}) | undefined) => Promise<IRequestSuccess<TResponse
|
|
42
|
+
}) | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
43
43
|
uploadProgressPercent: number;
|
|
44
44
|
} | {
|
|
45
45
|
data: undefined;
|
|
@@ -75,7 +75,7 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, he
|
|
|
75
75
|
requestConfig?: Partial<Omit<IMakeRequest, "body">> | undefined;
|
|
76
76
|
} & {
|
|
77
77
|
requestConfig?: Partial<Omit<IMakeRequest, "body">> | undefined;
|
|
78
|
-
}) | undefined) => Promise<IRequestSuccess<TResponse
|
|
78
|
+
}) | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
79
79
|
uploadProgressPercent: number;
|
|
80
80
|
} | {
|
|
81
81
|
data: undefined;
|
|
@@ -111,7 +111,7 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, he
|
|
|
111
111
|
requestConfig?: Partial<Omit<IMakeRequest, "body">> | undefined;
|
|
112
112
|
} & {
|
|
113
113
|
requestConfig?: Partial<Omit<IMakeRequest, "body">> | undefined;
|
|
114
|
-
}) | undefined) => Promise<IRequestSuccess<TResponse
|
|
114
|
+
}) | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
115
115
|
uploadProgressPercent: number;
|
|
116
116
|
} | {
|
|
117
117
|
data: IRequestSuccess<TResponse>;
|
|
@@ -147,6 +147,6 @@ export declare const usePostRequest: <TResponse>({ path, isFormData, baseUrl, he
|
|
|
147
147
|
requestConfig?: Partial<Omit<IMakeRequest, "body">> | undefined;
|
|
148
148
|
} & {
|
|
149
149
|
requestConfig?: Partial<Omit<IMakeRequest, "body">> | undefined;
|
|
150
|
-
}) | undefined) => Promise<IRequestSuccess<TResponse
|
|
150
|
+
}) | undefined) => Promise<IRequestSuccess<TResponse>>;
|
|
151
151
|
uploadProgressPercent: number;
|
|
152
152
|
};
|
|
@@ -4,7 +4,6 @@ import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
|
|
|
4
4
|
import { useQueryConfig } from '../config/useQueryConfig.js';
|
|
5
5
|
import { useQueryHeaders } from '../config/useQueryHeaders.js';
|
|
6
6
|
import { useReactNativeEnv } from '../config/useReactNativeEnv.js';
|
|
7
|
-
import { useState, useEffect } from 'react';
|
|
8
7
|
import { scrollToTop } from '../helpers/scrollToTop.js';
|
|
9
8
|
import { useUploadProgress } from '../hooks/useUploadProgress.js';
|
|
10
9
|
import 'axios';
|
|
@@ -14,7 +13,6 @@ import { HttpMethod } from '../request/request.enum.js';
|
|
|
14
13
|
const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelectors, }) => {
|
|
15
14
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
16
15
|
const config = useQueryConfig();
|
|
17
|
-
const [mutationConfig, setMutationConfig] = useState();
|
|
18
16
|
const { getHeaders } = useQueryHeaders();
|
|
19
17
|
const { isApp } = useReactNativeEnv();
|
|
20
18
|
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
@@ -39,7 +37,7 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
39
37
|
...requestConfig,
|
|
40
38
|
};
|
|
41
39
|
let shouldContinue = true;
|
|
42
|
-
if (config.options
|
|
40
|
+
if (config.options?.mutationMiddleware) {
|
|
43
41
|
shouldContinue = await config.options.mutationMiddleware({
|
|
44
42
|
mutationKey: [path, { type: 'mutation' }],
|
|
45
43
|
...requestOptions,
|
|
@@ -49,14 +47,14 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
49
47
|
const postResponse = await makeRequest(requestOptions);
|
|
50
48
|
if (postResponse.status) {
|
|
51
49
|
// scroll to top after success
|
|
52
|
-
if (config.options
|
|
50
|
+
if (config.options?.context !== 'app') {
|
|
53
51
|
scrollToTop();
|
|
54
52
|
}
|
|
55
53
|
res(postResponse);
|
|
56
54
|
}
|
|
57
55
|
else {
|
|
58
56
|
// scroll to top after error
|
|
59
|
-
if (config.options
|
|
57
|
+
if (config.options?.context !== 'app') {
|
|
60
58
|
scrollToTop();
|
|
61
59
|
}
|
|
62
60
|
rej(postResponse);
|
|
@@ -71,22 +69,9 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
71
69
|
mutationKey: [path, { type: 'mutation' }],
|
|
72
70
|
});
|
|
73
71
|
const post = async (data, options) => {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return mutation.mutateAsync({ data, requestConfig }, otherOptions);
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
setMutationConfig({ data, options });
|
|
80
|
-
return undefined;
|
|
81
|
-
}
|
|
72
|
+
const { requestConfig, ...otherOptions } = options ?? {};
|
|
73
|
+
return mutation.mutateAsync({ data, requestConfig }, otherOptions);
|
|
82
74
|
};
|
|
83
|
-
useEffect(() => {
|
|
84
|
-
if (!config.options.pauseFutureMutations && mutationConfig) {
|
|
85
|
-
post(mutationConfig.data, mutationConfig.options);
|
|
86
|
-
setMutationConfig(undefined);
|
|
87
|
-
}
|
|
88
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
89
|
-
}, [config.options.pauseFutureMutations]);
|
|
90
75
|
return { post, uploadProgressPercent, ...mutation };
|
|
91
76
|
};
|
|
92
77
|
|
|
@@ -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
|
@@ -14,19 +14,16 @@ export interface BootstrapConfig {
|
|
|
14
14
|
queryMiddleware?: (queryRequestConfig?: IMakeRequest & {
|
|
15
15
|
queryKey: QueryKey;
|
|
16
16
|
}) => Promise<boolean>;
|
|
17
|
-
pauseFutureMutations?: boolean;
|
|
18
|
-
pauseFutureQueries?: boolean;
|
|
19
|
-
headers?: RawAxiosRequestHeaders;
|
|
20
17
|
}
|
|
21
18
|
export interface BootstrapModelConfig {
|
|
22
19
|
idColumn: string;
|
|
23
20
|
}
|
|
24
21
|
export type ContextType = 'app' | 'web' | 'electronjs';
|
|
25
22
|
export interface TanstackQueryConfig {
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
headers: RawAxiosRequestHeaders;
|
|
24
|
+
options?: BootstrapConfig;
|
|
28
25
|
}
|
|
29
26
|
export interface IUseQueryHeaders {
|
|
30
|
-
getHeaders: () =>
|
|
31
|
-
setQueryHeaders: (header:
|
|
27
|
+
getHeaders: () => TanstackQueryConfig['headers'];
|
|
28
|
+
setQueryHeaders: (header: TanstackQueryConfig['headers']) => void;
|
|
32
29
|
}
|
package/package.json
CHANGED
|
@@ -1,48 +1,11 @@
|
|
|
1
|
-
import type { MutationMeta, QueryMeta } from '@tanstack/react-query';
|
|
2
1
|
import { useQueryClient } from '@tanstack/react-query';
|
|
3
|
-
import type {
|
|
2
|
+
import type { TanstackQueryConfig } from '../types';
|
|
4
3
|
|
|
5
4
|
export const useQueryConfig = (): TanstackQueryConfig => {
|
|
6
5
|
const queryClient = useQueryClient();
|
|
7
6
|
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
let queryMeta: QueryMeta | undefined = queryClient.getQueryDefaults()?.meta ?? {};
|
|
7
|
+
const mutationMeta = (queryClient.getDefaultOptions().mutations?.meta ?? {}) as unknown as TanstackQueryConfig;
|
|
8
|
+
const queryMeta = (queryClient.getDefaultOptions().queries?.meta ?? {}) as unknown as TanstackQueryConfig;
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if (pauseFutureMutations) {
|
|
15
|
-
mutationMeta.pauseFutureMutations = pauseFutureMutations;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (mutationMiddleware) {
|
|
19
|
-
mutationMeta.mutationMiddleware = mutationMiddleware;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
mutationMeta = { meta: { ...mutationMeta, ...otherOptions } };
|
|
23
|
-
|
|
24
|
-
if (pauseFutureQueries) {
|
|
25
|
-
queryMeta.pauseFutureQueries = pauseFutureQueries;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (queryMiddleware) {
|
|
29
|
-
queryMeta.mutationMiddleware = queryMiddleware;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
queryMeta = { meta: { ...queryMeta, ...otherOptions } };
|
|
33
|
-
|
|
34
|
-
const defaultMutationOptions = queryClient.defaultMutationOptions();
|
|
35
|
-
const defaultQueryOptions = queryClient.defaultQueryOptions();
|
|
36
|
-
|
|
37
|
-
queryClient.setDefaultOptions({
|
|
38
|
-
queries: { ...defaultQueryOptions, meta: queryMeta },
|
|
39
|
-
mutations: { ...defaultMutationOptions, meta: mutationMeta },
|
|
40
|
-
});
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const mutationMeta = (queryClient.getDefaultOptions().mutations?.meta ?? {}) as unknown as BootstrapConfig;
|
|
44
|
-
const queryMeta = (queryClient.getDefaultOptions().queries?.meta ?? {}) as unknown as BootstrapConfig;
|
|
45
|
-
|
|
46
|
-
const options: BootstrapConfig = { ...queryMeta, ...mutationMeta };
|
|
47
|
-
return { options, setConfig };
|
|
10
|
+
return { ...queryMeta, ...mutationMeta };
|
|
48
11
|
};
|
|
@@ -1,15 +1,27 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
2
|
+
import type { IUseQueryHeaders, TanstackQueryConfig } from '../types';
|
|
2
3
|
import { useQueryConfig } from './useQueryConfig';
|
|
3
4
|
|
|
4
5
|
export const useQueryHeaders = (): IUseQueryHeaders => {
|
|
5
|
-
const
|
|
6
|
+
const queryClient = useQueryClient();
|
|
7
|
+
const { headers, options } = useQueryConfig();
|
|
6
8
|
|
|
7
|
-
const getHeaders = ():
|
|
8
|
-
return
|
|
9
|
+
const getHeaders = (): TanstackQueryConfig['headers'] => {
|
|
10
|
+
return headers;
|
|
9
11
|
};
|
|
10
12
|
|
|
11
|
-
const setQueryHeaders = (
|
|
12
|
-
|
|
13
|
+
const setQueryHeaders = (newHeaders: TanstackQueryConfig['headers']) => {
|
|
14
|
+
const defaultMeta = {
|
|
15
|
+
headers: { ...headers, ...newHeaders },
|
|
16
|
+
options,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
queryClient.setDefaultOptions({
|
|
20
|
+
queries: {
|
|
21
|
+
meta: defaultMeta,
|
|
22
|
+
},
|
|
23
|
+
mutations: { meta: defaultMeta },
|
|
24
|
+
});
|
|
13
25
|
};
|
|
14
26
|
|
|
15
27
|
return { setQueryHeaders, getHeaders };
|
|
@@ -3,9 +3,9 @@ import { useQueryConfig } from './useQueryConfig';
|
|
|
3
3
|
export const useReactNativeEnv = () => {
|
|
4
4
|
const config = useQueryConfig();
|
|
5
5
|
|
|
6
|
-
const appUrl: string | undefined = config.options
|
|
7
|
-
const appTimeout: number | undefined = config.options
|
|
8
|
-
const isApp = config.options
|
|
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';
|
|
9
9
|
|
|
10
10
|
return { appUrl, appTimeout, isApp };
|
|
11
11
|
};
|