@ventlio/tanstack-query 0.2.87 → 0.3.0
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 +1 -1
- package/dist/config/useQueryConfig.js +27 -1
- package/dist/config/useQueryConfig.js.map +1 -1
- package/dist/config/useQueryHeaders.js +4 -15
- package/dist/config/useQueryHeaders.js.map +1 -1
- package/dist/config/useReactNativeEnv.js +3 -3
- package/dist/index.mjs +155 -65
- package/dist/index.mjs.map +1 -1
- package/dist/model/useKeyTrackerModel.d.ts +1 -1
- package/dist/model/useKeyTrackerModel.js +5 -1
- package/dist/model/useKeyTrackerModel.js.map +1 -1
- package/dist/queries/useDeleteRequest.d.ts +4 -4
- package/dist/queries/useDeleteRequest.js +24 -9
- package/dist/queries/useDeleteRequest.js.map +1 -1
- package/dist/queries/useGetInfiniteRequest.d.ts +4 -4
- package/dist/queries/useGetInfiniteRequest.js +24 -13
- package/dist/queries/useGetInfiniteRequest.js.map +1 -1
- package/dist/queries/useGetRequest.d.ts +4 -4
- package/dist/queries/useGetRequest.js +23 -12
- package/dist/queries/useGetRequest.js.map +1 -1
- package/dist/queries/usePatchRequest.d.ts +8 -8
- package/dist/queries/usePatchRequest.js +25 -5
- package/dist/queries/usePatchRequest.js.map +1 -1
- package/dist/queries/usePostRequest.d.ts +4 -4
- package/dist/queries/usePostRequest.js +20 -5
- package/dist/queries/usePostRequest.js.map +1 -1
- package/dist/types/index.d.ts +7 -4
- package/package.json +1 -1
- package/src/config/bootstrapQueryRequest.ts +1 -1
- package/src/config/useQueryConfig.ts +41 -4
- package/src/config/useQueryHeaders.ts +6 -18
- package/src/config/useReactNativeEnv.ts +3 -3
- package/src/model/useKeyTrackerModel.ts +5 -1
- package/src/queries/useDeleteRequest.ts +25 -11
- package/src/queries/useGetInfiniteRequest.ts +25 -15
- package/src/queries/useGetRequest.ts +25 -15
- package/src/queries/usePatchRequest.ts +27 -8
- package/src/queries/usePostRequest.ts +22 -8
- package/src/types/index.ts +7 -4
|
@@ -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
|
};
|
|
@@ -4,7 +4,11 @@ export const useKeyTrackerModel = <T>(keyTracker: string) => {
|
|
|
4
4
|
const queryClient = useQueryClient();
|
|
5
5
|
|
|
6
6
|
const getQueryKey = (innerKeyTracker?: string) => {
|
|
7
|
-
const
|
|
7
|
+
const meta = {
|
|
8
|
+
...queryClient.getDefaultOptions().mutations?.meta,
|
|
9
|
+
...queryClient.getDefaultOptions().queries?.meta,
|
|
10
|
+
};
|
|
11
|
+
const queryKey: any[] | undefined = meta[innerKeyTracker ?? keyTracker] as any[];
|
|
8
12
|
|
|
9
13
|
return queryKey;
|
|
10
14
|
};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { QueryKey, UseQueryOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useQuery } from '@tanstack/react-query';
|
|
3
|
-
import
|
|
4
|
-
import { useState } from 'react';
|
|
3
|
+
import { useEffect, useState } from 'react';
|
|
5
4
|
import { useEnvironmentVariables, useQueryConfig, useQueryHeaders } from '../config';
|
|
6
5
|
import type { IRequestError, IRequestSuccess } from '../request';
|
|
7
6
|
import { makeRequest } from '../request';
|
|
@@ -11,6 +10,7 @@ export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOption
|
|
|
11
10
|
const { baseUrl, headers } = deleteOptions ?? {};
|
|
12
11
|
const [requestPath, setRequestPath] = useState<string>('');
|
|
13
12
|
const [options, setOptions] = useState<any>();
|
|
13
|
+
const [destroyConfig, setDestroyConfig] = useState<{ link: string; internalDeleteOptions: any }>();
|
|
14
14
|
|
|
15
15
|
const { options: queryConfigOptions } = useQueryConfig();
|
|
16
16
|
|
|
@@ -20,7 +20,7 @@ export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOption
|
|
|
20
20
|
|
|
21
21
|
const sendRequest = async (res: (value: any) => void, rej: (reason?: any) => void, queryKey: QueryKey) => {
|
|
22
22
|
// get request headers
|
|
23
|
-
const globalHeaders
|
|
23
|
+
const globalHeaders = getHeaders();
|
|
24
24
|
|
|
25
25
|
const [url] = queryKey;
|
|
26
26
|
const requestUrl = (url ?? requestPath) as string;
|
|
@@ -34,7 +34,7 @@ export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOption
|
|
|
34
34
|
|
|
35
35
|
let shouldContinue = true;
|
|
36
36
|
|
|
37
|
-
if (queryConfigOptions
|
|
37
|
+
if (queryConfigOptions.queryMiddleware) {
|
|
38
38
|
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
39
39
|
}
|
|
40
40
|
|
|
@@ -75,15 +75,29 @@ export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOption
|
|
|
75
75
|
Array<any>
|
|
76
76
|
> & { cached?: boolean }
|
|
77
77
|
): Promise<IRequestSuccess<TResponse> | undefined> => {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
78
|
+
if (!queryConfigOptions.pauseFutureQueries) {
|
|
79
|
+
// set enabled to be true for every delete
|
|
80
|
+
internalDeleteOptions = internalDeleteOptions ?? {};
|
|
81
|
+
internalDeleteOptions.enabled = true;
|
|
81
82
|
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
await setOptionsAsync(internalDeleteOptions);
|
|
84
|
+
await updatedPathAsync(link);
|
|
84
85
|
|
|
85
|
-
|
|
86
|
+
return query.data;
|
|
87
|
+
} else {
|
|
88
|
+
// save delete config
|
|
89
|
+
setDestroyConfig({ link, internalDeleteOptions });
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
86
92
|
};
|
|
87
93
|
|
|
88
|
-
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
if (!queryConfigOptions.pauseFutureQueries && destroyConfig) {
|
|
96
|
+
destroy(destroyConfig.link, destroyConfig.internalDeleteOptions);
|
|
97
|
+
setDestroyConfig(undefined);
|
|
98
|
+
}
|
|
99
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
100
|
+
}, [queryConfigOptions.pauseFutureQueries]);
|
|
101
|
+
|
|
102
|
+
return { destroy, ...query, isLoading: query.isLoading || queryConfigOptions.pauseFutureQueries };
|
|
89
103
|
};
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { InfiniteData, QueryKey, UseQueryOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useInfiniteQuery, useQueryClient } from '@tanstack/react-query';
|
|
3
|
-
import type { RawAxiosRequestHeaders } from 'axios';
|
|
4
3
|
import { startTransition, useEffect, useMemo, useState } from 'react';
|
|
5
4
|
import { useEnvironmentVariables, useQueryConfig, useQueryHeaders } from '../config';
|
|
6
5
|
|
|
@@ -33,9 +32,10 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
33
32
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
34
33
|
const { getHeaders } = useQueryHeaders();
|
|
35
34
|
const [requestPath, setRequestPath] = useState<string>(path);
|
|
35
|
+
const [queryConfig, setQueryConfig] = useState<{ link: string; fetchOptions: any }>();
|
|
36
36
|
|
|
37
37
|
const [options, setOptions] = useState<any>(queryOptions);
|
|
38
|
-
const { options: queryConfigOptions } = useQueryConfig();
|
|
38
|
+
const { options: queryConfigOptions, setConfig } = useQueryConfig();
|
|
39
39
|
|
|
40
40
|
let queryClient = useQueryClient();
|
|
41
41
|
|
|
@@ -55,7 +55,7 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
55
55
|
) => {
|
|
56
56
|
if (load) {
|
|
57
57
|
// get request headers
|
|
58
|
-
const globalHeaders
|
|
58
|
+
const globalHeaders = getHeaders();
|
|
59
59
|
|
|
60
60
|
const requestOptions = {
|
|
61
61
|
path: pageParam ?? requestPath,
|
|
@@ -66,7 +66,7 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
66
66
|
|
|
67
67
|
let shouldContinue = true;
|
|
68
68
|
|
|
69
|
-
if (queryConfigOptions
|
|
69
|
+
if (queryConfigOptions.queryMiddleware) {
|
|
70
70
|
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
71
71
|
}
|
|
72
72
|
|
|
@@ -115,7 +115,7 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
115
115
|
sendRequest(res, rej, queryKey, pageParam)
|
|
116
116
|
),
|
|
117
117
|
{
|
|
118
|
-
enabled: load,
|
|
118
|
+
enabled: load || !queryConfigOptions.pauseFutureQueries,
|
|
119
119
|
getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
|
|
120
120
|
getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
|
|
121
121
|
...options,
|
|
@@ -146,10 +146,16 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
146
146
|
>
|
|
147
147
|
| undefined
|
|
148
148
|
> => {
|
|
149
|
-
|
|
150
|
-
|
|
149
|
+
if (!queryConfigOptions.pauseFutureQueries) {
|
|
150
|
+
await setOptionsAsync(fetchOptions);
|
|
151
|
+
await updatedPathAsync(link);
|
|
151
152
|
|
|
152
|
-
|
|
153
|
+
return query.data;
|
|
154
|
+
} else {
|
|
155
|
+
setQueryConfig({ link, fetchOptions });
|
|
156
|
+
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
153
159
|
};
|
|
154
160
|
|
|
155
161
|
const updatedPathAsync = async (link: string) => {
|
|
@@ -160,18 +166,22 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
160
166
|
|
|
161
167
|
useEffect(() => {
|
|
162
168
|
if (keyTracker) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
});
|
|
169
|
+
setConfig({ [keyTracker]: [requestPath, {}] });
|
|
170
|
+
}
|
|
171
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
172
|
+
}, [keyTracker, requestPath]);
|
|
168
173
|
|
|
169
|
-
|
|
174
|
+
useEffect(() => {
|
|
175
|
+
if (!queryConfigOptions.pauseFutureQueries && queryConfig) {
|
|
176
|
+
get(queryConfig.link, queryConfig.fetchOptions);
|
|
177
|
+
setQueryConfig(undefined);
|
|
170
178
|
}
|
|
171
|
-
|
|
179
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
180
|
+
}, [queryConfigOptions.pauseFutureQueries]);
|
|
172
181
|
|
|
173
182
|
return {
|
|
174
183
|
get,
|
|
175
184
|
...query,
|
|
185
|
+
isLoading: query.isLoading || queryConfigOptions.pauseFutureQueries,
|
|
176
186
|
};
|
|
177
187
|
};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { QueryKey, UseQueryOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
|
3
3
|
import { startTransition, useEffect, useMemo, useState } from 'react';
|
|
4
|
-
import type { RawAxiosRequestHeaders } from '../../node_modules/axios/index';
|
|
5
4
|
import { useEnvironmentVariables, useQueryConfig, useQueryHeaders } from '../config';
|
|
6
5
|
|
|
7
6
|
import type { IRequestError, IRequestSuccess } from '../request';
|
|
@@ -27,7 +26,8 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
|
|
|
27
26
|
|
|
28
27
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
29
28
|
const { getHeaders } = useQueryHeaders();
|
|
30
|
-
const { options: queryConfigOptions } = useQueryConfig();
|
|
29
|
+
const { options: queryConfigOptions, setConfig } = useQueryConfig();
|
|
30
|
+
const [queryConfig, setQueryConfig] = useState<{ link: string; fetchOptions: any }>();
|
|
31
31
|
|
|
32
32
|
let queryClient = useQueryClient();
|
|
33
33
|
|
|
@@ -43,7 +43,7 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
|
|
|
43
43
|
) => {
|
|
44
44
|
if (load) {
|
|
45
45
|
// get request headers
|
|
46
|
-
const globalHeaders
|
|
46
|
+
const globalHeaders = getHeaders();
|
|
47
47
|
|
|
48
48
|
const [url] = queryKey;
|
|
49
49
|
const requestUrl = (url ?? requestPath) as string;
|
|
@@ -57,7 +57,7 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
|
|
|
57
57
|
|
|
58
58
|
let shouldContinue = true;
|
|
59
59
|
|
|
60
|
-
if (queryConfigOptions
|
|
60
|
+
if (queryConfigOptions.queryMiddleware) {
|
|
61
61
|
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
62
62
|
}
|
|
63
63
|
|
|
@@ -95,15 +95,10 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
|
|
|
95
95
|
|
|
96
96
|
useEffect(() => {
|
|
97
97
|
if (keyTracker) {
|
|
98
|
-
|
|
99
|
-
queryClient.setQueryDefaults([keyTracker], {
|
|
100
|
-
cacheTime: Infinity,
|
|
101
|
-
staleTime: Infinity,
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
queryClient.setQueryData([keyTracker], [requestPath, {}]);
|
|
98
|
+
setConfig({ [keyTracker]: [requestPath, {}] });
|
|
105
99
|
}
|
|
106
|
-
|
|
100
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
101
|
+
}, [keyTracker, requestPath]);
|
|
107
102
|
|
|
108
103
|
const nextPage = () => {
|
|
109
104
|
if (query.data?.data.pagination) {
|
|
@@ -165,14 +160,29 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
|
|
|
165
160
|
Array<any>
|
|
166
161
|
>
|
|
167
162
|
): Promise<IRequestSuccess<TResponse> | undefined> => {
|
|
168
|
-
|
|
169
|
-
|
|
163
|
+
if (!queryConfigOptions.pauseFutureQueries) {
|
|
164
|
+
await setOptionsAsync(fetchOptions);
|
|
165
|
+
await updatedPathAsync(link);
|
|
166
|
+
|
|
167
|
+
return query.data;
|
|
168
|
+
} else {
|
|
169
|
+
setQueryConfig({ link, fetchOptions });
|
|
170
170
|
|
|
171
|
-
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
172
173
|
};
|
|
173
174
|
|
|
175
|
+
useEffect(() => {
|
|
176
|
+
if (!queryConfigOptions.pauseFutureQueries && queryConfig) {
|
|
177
|
+
get(queryConfig.link, queryConfig.fetchOptions);
|
|
178
|
+
setQueryConfig(undefined);
|
|
179
|
+
}
|
|
180
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
181
|
+
}, [queryConfigOptions.pauseFutureQueries]);
|
|
182
|
+
|
|
174
183
|
return {
|
|
175
184
|
...query,
|
|
185
|
+
isLoading: query.isLoading || queryConfigOptions.pauseFutureQueries,
|
|
176
186
|
setRequestPath,
|
|
177
187
|
nextPage,
|
|
178
188
|
prevPage,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { MutateOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useMutation } from '@tanstack/react-query';
|
|
3
|
-
import
|
|
3
|
+
import { useEffect, useState } from 'react';
|
|
4
4
|
import { useEnvironmentVariables, useQueryConfig, useQueryHeaders } from '../config';
|
|
5
5
|
import { scrollToTop } from '../helpers';
|
|
6
6
|
import { useUploadProgress } from '../hooks';
|
|
@@ -11,6 +11,7 @@ import type { DefaultRequestOptions } from './queries.interface';
|
|
|
11
11
|
export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: string } & DefaultRequestOptions) => {
|
|
12
12
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
13
13
|
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
14
|
+
const [mutationConfig, setMutationConfig] = useState<{ data: any; options: any }>();
|
|
14
15
|
|
|
15
16
|
const { getHeaders } = useQueryHeaders();
|
|
16
17
|
|
|
@@ -18,7 +19,7 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
|
|
|
18
19
|
|
|
19
20
|
const sendRequest = async (res: (value: any) => void, rej: (reason?: any) => void, data: any) => {
|
|
20
21
|
// get request headers
|
|
21
|
-
const globalHeaders
|
|
22
|
+
const globalHeaders = getHeaders();
|
|
22
23
|
|
|
23
24
|
const requestOptions = {
|
|
24
25
|
path: path,
|
|
@@ -32,7 +33,7 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
|
|
|
32
33
|
|
|
33
34
|
let shouldContinue = true;
|
|
34
35
|
|
|
35
|
-
if (config.options
|
|
36
|
+
if (config.options.mutationMiddleware) {
|
|
36
37
|
shouldContinue = await config.options.mutationMiddleware({
|
|
37
38
|
mutationKey: [path, { type: 'mutation' }],
|
|
38
39
|
...requestOptions,
|
|
@@ -43,13 +44,13 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
|
|
|
43
44
|
const patchResponse = await makeRequest<TResponse>(requestOptions);
|
|
44
45
|
if (patchResponse.status) {
|
|
45
46
|
// scroll to top after success
|
|
46
|
-
if (config.options
|
|
47
|
+
if (config.options.context !== 'app') {
|
|
47
48
|
scrollToTop();
|
|
48
49
|
}
|
|
49
50
|
res(patchResponse as IRequestSuccess<TResponse>);
|
|
50
51
|
} else {
|
|
51
52
|
// scroll to top after error
|
|
52
|
-
if (config.options
|
|
53
|
+
if (config.options.context !== 'app') {
|
|
53
54
|
scrollToTop();
|
|
54
55
|
}
|
|
55
56
|
rej(patchResponse);
|
|
@@ -71,9 +72,27 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
|
|
|
71
72
|
const patch = async (
|
|
72
73
|
data: any,
|
|
73
74
|
options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined
|
|
74
|
-
): Promise<IRequestSuccess<TResponse
|
|
75
|
-
|
|
75
|
+
): Promise<IRequestSuccess<TResponse> | undefined> => {
|
|
76
|
+
if (!config.options.pauseFutureMutations) {
|
|
77
|
+
return mutation.mutateAsync(data, options);
|
|
78
|
+
} else {
|
|
79
|
+
setMutationConfig({ data, options });
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
76
82
|
};
|
|
77
83
|
|
|
78
|
-
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
if (!config.options.pauseFutureMutations && mutationConfig) {
|
|
86
|
+
patch(mutationConfig.data, mutationConfig.options);
|
|
87
|
+
setMutationConfig(undefined);
|
|
88
|
+
}
|
|
89
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
90
|
+
}, [config.options.pauseFutureMutations]);
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
patch,
|
|
94
|
+
uploadProgressPercent,
|
|
95
|
+
...mutation,
|
|
96
|
+
isLoading: mutation.isLoading || config.options.pauseFutureMutations,
|
|
97
|
+
};
|
|
79
98
|
};
|
|
@@ -2,7 +2,7 @@ import type { MutateOptions } from '@tanstack/react-query';
|
|
|
2
2
|
import { useMutation } from '@tanstack/react-query';
|
|
3
3
|
import { useEnvironmentVariables, useQueryConfig, useQueryHeaders, useReactNativeEnv } from '../config';
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import { useEffect, useState } from 'react';
|
|
6
6
|
import { scrollToTop } from '../helpers';
|
|
7
7
|
import { useUploadProgress } from '../hooks';
|
|
8
8
|
import type { IMakeRequest, IRequestError, IRequestSuccess } from '../request';
|
|
@@ -23,6 +23,7 @@ export const usePostRequest = <TResponse>({
|
|
|
23
23
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
24
24
|
|
|
25
25
|
const config = useQueryConfig();
|
|
26
|
+
const [mutationConfig, setMutationConfig] = useState<{ data: any; options: any }>();
|
|
26
27
|
|
|
27
28
|
const { getHeaders } = useQueryHeaders();
|
|
28
29
|
const { isApp } = useReactNativeEnv();
|
|
@@ -34,7 +35,7 @@ export const usePostRequest = <TResponse>({
|
|
|
34
35
|
postData: { data: any; requestConfig?: Partial<IMakeRequest> }
|
|
35
36
|
) => {
|
|
36
37
|
// get request headers
|
|
37
|
-
const globalHeaders
|
|
38
|
+
const globalHeaders = getHeaders();
|
|
38
39
|
|
|
39
40
|
const { data, requestConfig } = postData;
|
|
40
41
|
|
|
@@ -58,7 +59,7 @@ export const usePostRequest = <TResponse>({
|
|
|
58
59
|
|
|
59
60
|
let shouldContinue = true;
|
|
60
61
|
|
|
61
|
-
if (config.options
|
|
62
|
+
if (config.options.mutationMiddleware) {
|
|
62
63
|
shouldContinue = await config.options.mutationMiddleware({
|
|
63
64
|
mutationKey: [path, { type: 'mutation' }],
|
|
64
65
|
...requestOptions,
|
|
@@ -71,13 +72,13 @@ export const usePostRequest = <TResponse>({
|
|
|
71
72
|
if (postResponse.status) {
|
|
72
73
|
// scroll to top after success
|
|
73
74
|
|
|
74
|
-
if (config.options
|
|
75
|
+
if (config.options.context !== 'app') {
|
|
75
76
|
scrollToTop();
|
|
76
77
|
}
|
|
77
78
|
res(postResponse as IRequestSuccess<TResponse>);
|
|
78
79
|
} else {
|
|
79
80
|
// scroll to top after error
|
|
80
|
-
if (config.options
|
|
81
|
+
if (config.options.context !== 'app') {
|
|
81
82
|
scrollToTop();
|
|
82
83
|
}
|
|
83
84
|
rej(postResponse);
|
|
@@ -108,10 +109,23 @@ export const usePostRequest = <TResponse>({
|
|
|
108
109
|
| { requestConfig?: Partial<Omit<IMakeRequest, 'body'>> }
|
|
109
110
|
| undefined
|
|
110
111
|
) & { requestConfig?: Partial<Omit<IMakeRequest, 'body'>> }
|
|
111
|
-
): Promise<IRequestSuccess<TResponse
|
|
112
|
-
|
|
113
|
-
|
|
112
|
+
): Promise<IRequestSuccess<TResponse> | undefined> => {
|
|
113
|
+
if (!config.options.pauseFutureMutations) {
|
|
114
|
+
const { requestConfig, ...otherOptions } = options ?? {};
|
|
115
|
+
return mutation.mutateAsync({ data, requestConfig }, otherOptions);
|
|
116
|
+
} else {
|
|
117
|
+
setMutationConfig({ data, options });
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
114
120
|
};
|
|
115
121
|
|
|
122
|
+
useEffect(() => {
|
|
123
|
+
if (!config.options.pauseFutureMutations && mutationConfig) {
|
|
124
|
+
post(mutationConfig.data, mutationConfig.options);
|
|
125
|
+
setMutationConfig(undefined);
|
|
126
|
+
}
|
|
127
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
128
|
+
}, [config.options.pauseFutureMutations]);
|
|
129
|
+
|
|
116
130
|
return { post, uploadProgressPercent, ...mutation };
|
|
117
131
|
};
|
package/src/types/index.ts
CHANGED
|
@@ -11,6 +11,9 @@ export interface BootstrapConfig {
|
|
|
11
11
|
modelConfig?: BootstrapModelConfig;
|
|
12
12
|
mutationMiddleware?: (mutateRequestConfig?: IMakeRequest & { mutationKey: QueryKey }) => Promise<boolean>;
|
|
13
13
|
queryMiddleware?: (queryRequestConfig?: IMakeRequest & { queryKey: QueryKey }) => Promise<boolean>;
|
|
14
|
+
pauseFutureMutations?: boolean;
|
|
15
|
+
pauseFutureQueries?: boolean;
|
|
16
|
+
headers?: RawAxiosRequestHeaders;
|
|
14
17
|
}
|
|
15
18
|
|
|
16
19
|
export interface BootstrapModelConfig {
|
|
@@ -19,11 +22,11 @@ export interface BootstrapModelConfig {
|
|
|
19
22
|
|
|
20
23
|
export type ContextType = 'app' | 'web' | 'electronjs';
|
|
21
24
|
export interface TanstackQueryConfig {
|
|
22
|
-
|
|
23
|
-
options
|
|
25
|
+
options: BootstrapConfig;
|
|
26
|
+
setConfig: (options: BootstrapConfig) => void;
|
|
24
27
|
}
|
|
25
28
|
|
|
26
29
|
export interface IUseQueryHeaders {
|
|
27
|
-
getHeaders: () =>
|
|
28
|
-
setQueryHeaders: (header:
|
|
30
|
+
getHeaders: () => BootstrapConfig['headers'];
|
|
31
|
+
setQueryHeaders: (header: BootstrapConfig['headers']) => void;
|
|
29
32
|
}
|