@ventlio/tanstack-query 0.2.86 → 0.2.89

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.
Files changed (37) hide show
  1. package/dist/config/useQueryConfig.js +27 -1
  2. package/dist/config/useQueryConfig.js.map +1 -1
  3. package/dist/config/useQueryHeaders.js +4 -15
  4. package/dist/config/useQueryHeaders.js.map +1 -1
  5. package/dist/config/useReactNativeEnv.js +3 -3
  6. package/dist/index.mjs +261 -111
  7. package/dist/index.mjs.map +1 -1
  8. package/dist/model/useKeyTrackerModel.d.ts +1 -1
  9. package/dist/model/useKeyTrackerModel.js +5 -1
  10. package/dist/model/useKeyTrackerModel.js.map +1 -1
  11. package/dist/queries/useDeleteRequest.d.ts +4 -4
  12. package/dist/queries/useDeleteRequest.js +46 -19
  13. package/dist/queries/useDeleteRequest.js.map +1 -1
  14. package/dist/queries/useGetInfiniteRequest.d.ts +4 -4
  15. package/dist/queries/useGetInfiniteRequest.js +43 -20
  16. package/dist/queries/useGetInfiniteRequest.js.map +1 -1
  17. package/dist/queries/useGetRequest.d.ts +8 -8
  18. package/dist/queries/useGetRequest.js +48 -24
  19. package/dist/queries/useGetRequest.js.map +1 -1
  20. package/dist/queries/usePatchRequest.d.ts +8 -8
  21. package/dist/queries/usePatchRequest.js +48 -15
  22. package/dist/queries/usePatchRequest.js.map +1 -1
  23. package/dist/queries/usePostRequest.d.ts +4 -4
  24. package/dist/queries/usePostRequest.js +42 -14
  25. package/dist/queries/usePostRequest.js.map +1 -1
  26. package/dist/types/index.d.ts +15 -6
  27. package/package.json +1 -1
  28. package/src/config/useQueryConfig.ts +41 -4
  29. package/src/config/useQueryHeaders.ts +6 -18
  30. package/src/config/useReactNativeEnv.ts +3 -3
  31. package/src/model/useKeyTrackerModel.ts +5 -1
  32. package/src/queries/useDeleteRequest.ts +51 -23
  33. package/src/queries/useGetInfiniteRequest.ts +48 -24
  34. package/src/queries/useGetRequest.ts +53 -29
  35. package/src/queries/usePatchRequest.ts +52 -18
  36. package/src/queries/usePostRequest.ts +46 -17
  37. package/src/types/index.ts +11 -6
@@ -1,52 +1,79 @@
1
1
  import { useQuery } from '@tanstack/react-query';
2
- import { useState } from 'react';
2
+ import { useState, useEffect } 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 { HttpMethod } from '../request/request.enum.js';
9
+ import '../request/request.enum.js';
9
10
 
10
11
  const useDeleteRequest = (deleteOptions) => {
11
12
  const { baseUrl, headers } = deleteOptions ?? {};
12
- const [requestPath, updateDeletePath] = useState('');
13
+ const [requestPath, setRequestPath] = useState('');
13
14
  const [options, setOptions] = useState();
15
+ const [destroyConfig, setDestroyConfig] = useState();
16
+ const { options: queryConfigOptions } = useQueryConfig();
14
17
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
15
18
  const { getHeaders } = useQueryHeaders();
16
19
  const sendRequest = async (res, rej, queryKey) => {
17
20
  // get request headers
18
21
  const globalHeaders = getHeaders();
19
- const [url] = (queryKey ?? []);
20
- const postResponse = await makeRequest({
21
- path: url ?? requestPath,
22
+ const [url] = queryKey;
23
+ const requestUrl = (url ?? requestPath);
24
+ const requestOptions = {
25
+ path: requestUrl,
22
26
  headers: { ...globalHeaders, ...headers },
23
- method: HttpMethod.DELETE,
24
27
  baseURL: baseUrl ?? API_URL,
25
28
  timeout: TIMEOUT,
26
- });
27
- if (postResponse.status) {
28
- res(postResponse);
29
+ };
30
+ let shouldContinue = true;
31
+ if (queryConfigOptions.queryMiddleware) {
32
+ shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
33
+ }
34
+ if (shouldContinue) {
35
+ const postResponse = await makeRequest(requestOptions);
36
+ if (postResponse.status) {
37
+ res(postResponse);
38
+ }
39
+ else {
40
+ rej(postResponse);
41
+ }
29
42
  }
30
43
  else {
31
- rej(postResponse);
44
+ rej(null);
32
45
  }
33
46
  };
34
47
  const query = useQuery([requestPath, {}], ({ queryKey }) => new Promise((res, rej) => sendRequest(res, rej, queryKey)), { enabled: false, ...options });
35
48
  const updatedPathAsync = async (link) => {
36
- return updateDeletePath(link);
49
+ return setRequestPath(link);
37
50
  };
38
51
  const setOptionsAsync = async (fetchOptions) => {
39
52
  return setOptions(fetchOptions);
40
53
  };
41
54
  const destroy = async (link, internalDeleteOptions) => {
42
- // set enabled to be true for every delete
43
- internalDeleteOptions = internalDeleteOptions ?? {};
44
- internalDeleteOptions.enabled = true;
45
- await setOptionsAsync(internalDeleteOptions);
46
- await updatedPathAsync(link);
47
- return query.data;
55
+ if (!queryConfigOptions.pauseFutureQueries) {
56
+ // set enabled to be true for every delete
57
+ internalDeleteOptions = internalDeleteOptions ?? {};
58
+ internalDeleteOptions.enabled = true;
59
+ await setOptionsAsync(internalDeleteOptions);
60
+ await updatedPathAsync(link);
61
+ return query.data;
62
+ }
63
+ else {
64
+ // save delete config
65
+ setDestroyConfig({ link, internalDeleteOptions });
66
+ return undefined;
67
+ }
48
68
  };
49
- return { destroy, ...query };
69
+ useEffect(() => {
70
+ if (!queryConfigOptions.pauseFutureQueries && destroyConfig) {
71
+ destroy(destroyConfig.link, destroyConfig.internalDeleteOptions);
72
+ setDestroyConfig(undefined);
73
+ }
74
+ // eslint-disable-next-line react-hooks/exhaustive-deps
75
+ }, [queryConfigOptions.pauseFutureQueries]);
76
+ return { destroy, ...query, isLoading: query.isLoading || queryConfigOptions.pauseFutureQueries };
50
77
  };
51
78
 
52
79
  export { useDeleteRequest };
@@ -1 +1 @@
1
- {"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -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;
20
21
  data: undefined;
21
22
  error: any;
22
23
  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;
61
62
  data: undefined;
62
63
  error: null;
63
64
  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;
102
103
  data: InfiniteData<IRequestSuccess<TResponse & {
103
104
  pagination: Pagination;
104
105
  }>>;
105
106
  error: any;
106
107
  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;
145
146
  data: InfiniteData<IRequestSuccess<TResponse & {
146
147
  pagination: Pagination;
147
148
  }>>;
148
149
  error: null;
149
150
  isError: false;
150
- isLoading: false;
151
151
  isLoadingError: false;
152
152
  isRefetchError: false;
153
153
  isSuccess: true;
@@ -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,38 @@ 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, updatePath] = useState(path);
14
+ const [requestPath, setRequestPath] = useState(path);
15
+ const [queryConfig, setQueryConfig] = useState();
14
16
  const [options, setOptions] = useState(queryOptions);
17
+ const { options: queryConfigOptions, setConfig } = useQueryConfig();
15
18
  let queryClient = useQueryClient();
16
19
  // eslint-disable-next-line react-hooks/exhaustive-deps
17
20
  queryClient = useMemo(() => queryClient, []);
18
- const sendRequest = async (res, rej, pageParam) => {
21
+ const sendRequest = async (res, rej, queryKey, pageParam) => {
19
22
  if (load) {
20
23
  // get request headers
21
24
  const globalHeaders = getHeaders();
22
- const getResponse = await makeRequest({
25
+ const requestOptions = {
23
26
  path: pageParam ?? requestPath,
24
27
  headers: { ...globalHeaders, ...headers },
25
28
  baseURL: baseUrl ?? API_URL,
26
29
  timeout: TIMEOUT,
27
- });
28
- if (getResponse.status) {
29
- res(getResponse);
30
+ };
31
+ let shouldContinue = true;
32
+ if (queryConfigOptions.queryMiddleware) {
33
+ shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
34
+ }
35
+ if (shouldContinue) {
36
+ const getResponse = await makeRequest(requestOptions);
37
+ if (getResponse.status) {
38
+ res(getResponse);
39
+ }
40
+ else {
41
+ rej(getResponse);
42
+ }
30
43
  }
31
44
  else {
32
- rej(getResponse);
45
+ rej(null);
33
46
  }
34
47
  }
35
48
  else {
@@ -47,8 +60,8 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
47
60
  queryParams.set('page', String(lastPageItem));
48
61
  return pathname + '?' + queryParams.toString();
49
62
  };
50
- const query = useInfiniteQuery([requestPath, {}], ({ pageParam = requestPath }) => new Promise((res, rej) => sendRequest(res, rej, pageParam)), {
51
- enabled: load,
63
+ const query = useInfiniteQuery([requestPath, {}], ({ pageParam = requestPath, queryKey }) => new Promise((res, rej) => sendRequest(res, rej, queryKey, pageParam)), {
64
+ enabled: load || !queryConfigOptions.pauseFutureQueries,
52
65
  getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
53
66
  getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
54
67
  ...options,
@@ -59,28 +72,38 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
59
72
  });
60
73
  };
61
74
  const get = async (link, fetchOptions) => {
62
- await setOptionsAsync(fetchOptions);
63
- await updatedPathAsync(link);
64
- return query.data;
75
+ if (!queryConfigOptions.pauseFutureQueries) {
76
+ await setOptionsAsync(fetchOptions);
77
+ await updatedPathAsync(link);
78
+ return query.data;
79
+ }
80
+ else {
81
+ setQueryConfig({ link, fetchOptions });
82
+ return undefined;
83
+ }
65
84
  };
66
85
  const updatedPathAsync = async (link) => {
67
86
  startTransition(() => {
68
- updatePath(link);
87
+ setRequestPath(link);
69
88
  });
70
89
  };
71
90
  useEffect(() => {
72
91
  if (keyTracker) {
73
- // set expiration time for the tracker
74
- queryClient.setQueryDefaults([keyTracker], {
75
- cacheTime: Infinity,
76
- staleTime: Infinity,
77
- });
78
- queryClient.setQueryData([keyTracker], [requestPath, {}]);
92
+ setConfig({ [keyTracker]: [requestPath, {}] });
93
+ }
94
+ // eslint-disable-next-line react-hooks/exhaustive-deps
95
+ }, [keyTracker, requestPath]);
96
+ useEffect(() => {
97
+ if (!queryConfigOptions.pauseFutureQueries && queryConfig) {
98
+ get(queryConfig.link, queryConfig.fetchOptions);
99
+ setQueryConfig(undefined);
79
100
  }
80
- }, [keyTracker, requestPath, queryClient, queryOptions?.staleTime]);
101
+ // eslint-disable-next-line react-hooks/exhaustive-deps
102
+ }, [queryConfigOptions.pauseFutureQueries]);
81
103
  return {
82
104
  get,
83
105
  ...query,
106
+ isLoading: query.isLoading || queryConfigOptions.pauseFutureQueries,
84
107
  };
85
108
  };
86
109
 
@@ -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,8 @@ 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
- updatePath: import("react").Dispatch<import("react").SetStateAction<string>>;
11
+ isLoading: boolean | undefined;
12
+ setRequestPath: import("react").Dispatch<import("react").SetStateAction<string>>;
12
13
  nextPage: () => void;
13
14
  prevPage: () => void;
14
15
  get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
@@ -18,7 +19,6 @@ export declare const useGetRequest: <TResponse extends Record<string, any>>({ pa
18
19
  data: IRequestSuccess<TResponse>;
19
20
  error: any;
20
21
  isError: true;
21
- isLoading: false;
22
22
  isLoadingError: false;
23
23
  isRefetchError: true;
24
24
  isSuccess: false;
@@ -41,7 +41,8 @@ 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
- updatePath: import("react").Dispatch<import("react").SetStateAction<string>>;
44
+ isLoading: boolean | undefined;
45
+ setRequestPath: import("react").Dispatch<import("react").SetStateAction<string>>;
45
46
  nextPage: () => void;
46
47
  prevPage: () => void;
47
48
  get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
@@ -51,7 +52,6 @@ export declare const useGetRequest: <TResponse extends Record<string, any>>({ pa
51
52
  data: IRequestSuccess<TResponse>;
52
53
  error: null;
53
54
  isError: false;
54
- isLoading: false;
55
55
  isLoadingError: false;
56
56
  isRefetchError: false;
57
57
  isSuccess: true;
@@ -74,7 +74,8 @@ 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
- updatePath: import("react").Dispatch<import("react").SetStateAction<string>>;
77
+ isLoading: boolean | undefined;
78
+ setRequestPath: import("react").Dispatch<import("react").SetStateAction<string>>;
78
79
  nextPage: () => void;
79
80
  prevPage: () => void;
80
81
  get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
@@ -84,7 +85,6 @@ export declare const useGetRequest: <TResponse extends Record<string, any>>({ pa
84
85
  data: undefined;
85
86
  error: any;
86
87
  isError: true;
87
- isLoading: false;
88
88
  isLoadingError: true;
89
89
  isRefetchError: false;
90
90
  isSuccess: false;
@@ -107,7 +107,8 @@ 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
- updatePath: import("react").Dispatch<import("react").SetStateAction<string>>;
110
+ isLoading: boolean | undefined;
111
+ setRequestPath: import("react").Dispatch<import("react").SetStateAction<string>>;
111
112
  nextPage: () => void;
112
113
  prevPage: () => void;
113
114
  get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
@@ -117,7 +118,6 @@ export declare const useGetRequest: <TResponse extends Record<string, any>>({ pa
117
118
  data: undefined;
118
119
  error: null;
119
120
  isError: false;
120
- isLoading: true;
121
121
  isLoadingError: false;
122
122
  isRefetchError: false;
123
123
  isSuccess: false;
@@ -2,17 +2,20 @@ 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, updatePath] = useState(path);
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, setConfig } = useQueryConfig();
18
+ const [queryConfig, setQueryConfig] = useState();
16
19
  let queryClient = useQueryClient();
17
20
  // eslint-disable-next-line react-hooks/exhaustive-deps
18
21
  queryClient = useMemo(() => queryClient, []);
@@ -20,18 +23,29 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
20
23
  if (load) {
21
24
  // get request headers
22
25
  const globalHeaders = getHeaders();
23
- const [url] = (queryKey ?? []);
24
- const getResponse = await makeRequest({
25
- path: url ?? requestPath,
26
+ const [url] = queryKey;
27
+ const requestUrl = (url ?? requestPath);
28
+ const requestOptions = {
29
+ path: requestUrl,
26
30
  headers: { ...globalHeaders, ...headers },
27
31
  baseURL: baseUrl ?? API_URL,
28
32
  timeout: TIMEOUT,
29
- });
30
- if (getResponse.status) {
31
- res(getResponse);
33
+ };
34
+ let shouldContinue = true;
35
+ if (queryConfigOptions.queryMiddleware) {
36
+ shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
37
+ }
38
+ if (shouldContinue) {
39
+ const getResponse = await makeRequest(requestOptions);
40
+ if (getResponse.status) {
41
+ res(getResponse);
42
+ }
43
+ else {
44
+ rej(getResponse);
45
+ }
32
46
  }
33
47
  else {
34
- rej(getResponse);
48
+ rej(null);
35
49
  }
36
50
  }
37
51
  else {
@@ -44,24 +58,20 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
44
58
  });
45
59
  useEffect(() => {
46
60
  if (path) {
47
- updatePath(path);
61
+ setRequestPath(path);
48
62
  }
49
63
  }, [path]);
50
64
  useEffect(() => {
51
65
  if (keyTracker) {
52
- // set expiration time for the tracker
53
- queryClient.setQueryDefaults([keyTracker], {
54
- cacheTime: Infinity,
55
- staleTime: Infinity,
56
- });
57
- queryClient.setQueryData([keyTracker], [requestPath, {}]);
66
+ setConfig({ [keyTracker]: [requestPath, {}] });
58
67
  }
59
- }, [keyTracker, requestPath, queryClient, queryOptions?.staleTime]);
68
+ // eslint-disable-next-line react-hooks/exhaustive-deps
69
+ }, [keyTracker, requestPath]);
60
70
  const nextPage = () => {
61
71
  if (query.data?.data.pagination) {
62
72
  const pagination = query.data.data.pagination;
63
73
  if (pagination.next_page !== pagination.current_page && pagination.next_page > pagination.current_page) {
64
- updatePath(constructPaginationLink(requestPath, pagination.next_page));
74
+ setRequestPath(constructPaginationLink(requestPath, pagination.next_page));
65
75
  }
66
76
  }
67
77
  };
@@ -69,7 +79,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
69
79
  if (query.data?.data.pagination) {
70
80
  const pagination = query.data.data.pagination;
71
81
  if (pagination.previous_page !== pagination.current_page && pagination.previous_page < pagination.current_page) {
72
- updatePath(constructPaginationLink(requestPath, pagination.previous_page));
82
+ setRequestPath(constructPaginationLink(requestPath, pagination.previous_page));
73
83
  }
74
84
  }
75
85
  };
@@ -86,11 +96,11 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
86
96
  return link;
87
97
  };
88
98
  const gotoPage = (pageNumber) => {
89
- updatePath(constructPaginationLink(requestPath, pageNumber));
99
+ setRequestPath(constructPaginationLink(requestPath, pageNumber));
90
100
  };
91
101
  const updatedPathAsync = async (link) => {
92
102
  startTransition(() => {
93
- updatePath(link);
103
+ setRequestPath(link);
94
104
  });
95
105
  };
96
106
  const setOptionsAsync = async (fetchOptions) => {
@@ -99,13 +109,27 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
99
109
  });
100
110
  };
101
111
  const get = async (link, fetchOptions) => {
102
- await setOptionsAsync(fetchOptions);
103
- await updatedPathAsync(link);
104
- return query.data;
112
+ if (!queryConfigOptions.pauseFutureQueries) {
113
+ await setOptionsAsync(fetchOptions);
114
+ await updatedPathAsync(link);
115
+ return query.data;
116
+ }
117
+ else {
118
+ setQueryConfig({ link, fetchOptions });
119
+ return undefined;
120
+ }
105
121
  };
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]);
106
129
  return {
107
130
  ...query,
108
- updatePath,
131
+ isLoading: query.isLoading || queryConfigOptions.pauseFutureQueries,
132
+ setRequestPath,
109
133
  nextPage,
110
134
  prevPage,
111
135
  get,
@@ -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;
7
8
  data: undefined;
8
9
  error: null;
9
10
  isError: false;
10
11
  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> | undefined>;
23
23
  uploadProgressPercent: number;
24
24
  } | {
25
+ isLoading: boolean | undefined;
25
26
  data: undefined;
26
27
  error: null;
27
28
  isError: false;
28
29
  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> | undefined>;
41
41
  uploadProgressPercent: number;
42
42
  } | {
43
+ isLoading: boolean | undefined;
43
44
  data: undefined;
44
45
  error: IRequestError;
45
46
  isError: true;
46
47
  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> | undefined>;
59
59
  uploadProgressPercent: number;
60
60
  } | {
61
+ isLoading: boolean | undefined;
61
62
  data: IRequestSuccess<TResponse>;
62
63
  error: null;
63
64
  isError: false;
64
65
  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> | undefined>;
77
77
  uploadProgressPercent: number;
78
78
  };
@@ -1,4 +1,5 @@
1
1
  import { useMutation } from '@tanstack/react-query';
2
+ import { useState, useEffect } from 'react';
2
3
  import 'url-search-params-polyfill';
3
4
  import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
4
5
  import { useQueryConfig } from '../config/useQueryConfig.js';
@@ -12,12 +13,13 @@ import { HttpMethod } from '../request/request.enum.js';
12
13
  const usePatchRequest = ({ path, baseUrl, headers }) => {
13
14
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
14
15
  const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
16
+ const [mutationConfig, setMutationConfig] = useState();
15
17
  const { getHeaders } = useQueryHeaders();
16
18
  const config = useQueryConfig();
17
19
  const sendRequest = async (res, rej, data) => {
18
20
  // get request headers
19
21
  const globalHeaders = getHeaders();
20
- const patchResponse = await makeRequest({
22
+ const requestOptions = {
21
23
  path: path,
22
24
  body: data,
23
25
  method: HttpMethod.PATCH,
@@ -25,30 +27,61 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
25
27
  baseURL: baseUrl ?? API_URL,
26
28
  timeout: TIMEOUT,
27
29
  onUploadProgress,
28
- });
29
- if (patchResponse.status) {
30
- // scroll to top after success
31
- if (config.options?.context !== 'app') {
32
- scrollToTop();
30
+ };
31
+ let shouldContinue = true;
32
+ if (config.options.mutationMiddleware) {
33
+ shouldContinue = await config.options.mutationMiddleware({
34
+ mutationKey: [path, { type: 'mutation' }],
35
+ ...requestOptions,
36
+ });
37
+ }
38
+ if (shouldContinue) {
39
+ const patchResponse = await makeRequest(requestOptions);
40
+ if (patchResponse.status) {
41
+ // scroll to top after success
42
+ if (config.options.context !== 'app') {
43
+ scrollToTop();
44
+ }
45
+ res(patchResponse);
46
+ }
47
+ else {
48
+ // scroll to top after error
49
+ if (config.options.context !== 'app') {
50
+ scrollToTop();
51
+ }
52
+ rej(patchResponse);
33
53
  }
34
- res(patchResponse);
35
54
  }
36
55
  else {
37
- // scroll to top after error
38
- if (config.options?.context !== 'app') {
39
- scrollToTop();
40
- }
41
- rej(patchResponse);
56
+ rej(null);
42
57
  }
43
58
  };
44
59
  // register post mutation
45
60
  const mutation = useMutation((dataData) => new Promise((res, rej) => {
46
61
  return sendRequest(res, rej, dataData);
47
- }));
62
+ }), { mutationKey: [path, { type: 'mutation' }] });
48
63
  const patch = async (data, options) => {
49
- return mutation.mutateAsync(data, options);
64
+ if (!config.options.pauseFutureMutations) {
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,
50
84
  };
51
- return { patch, uploadProgressPercent, ...mutation };
52
85
  };
53
86
 
54
87
  export { usePatchRequest };