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