@ventlio/tanstack-query 0.5.1 → 0.5.3
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/index.mjs +30 -25
- package/dist/index.mjs.map +1 -1
- package/dist/model/useQueryModel.js +1 -1
- package/dist/model/useRefetchQuery.d.ts +1 -1
- package/dist/queries/queries.interface.d.ts +1 -1
- package/dist/queries/useDeleteRequest.d.ts +4 -4
- package/dist/queries/useDeleteRequest.js +3 -1
- package/dist/queries/useDeleteRequest.js.map +1 -1
- package/dist/queries/useGetInfiniteRequest.d.ts +31 -25
- package/dist/queries/useGetRequest.d.ts +4 -4
- package/dist/queries/usePatchRequest.js +1 -1
- package/dist/queries/usePostRequest.js +1 -1
- package/dist/queries/usePutRequest.d.ts +86 -0
- package/dist/request/make-request.js +25 -22
- package/dist/request/make-request.js.map +1 -1
- package/package.json +2 -2
- package/src/model/useQueryModel.ts +1 -1
- package/src/queries/queries.interface.ts +7 -5
- package/src/queries/useDeleteRequest.ts +3 -1
- package/src/queries/useGetInfiniteRequest.ts +8 -6
- package/src/queries/usePatchRequest.ts +1 -1
- package/src/queries/usePostRequest.ts +1 -1
- package/src/queries/usePutRequest.ts +95 -0
- package/src/request/make-request.ts +30 -19
package/dist/index.mjs
CHANGED
|
@@ -180,7 +180,7 @@ const useQueryModel = (keyTracker) => {
|
|
|
180
180
|
const data = get();
|
|
181
181
|
newData = lodashSet(data, path, newData);
|
|
182
182
|
}
|
|
183
|
-
return queryClient.setQueryData(queryKey, newData);
|
|
183
|
+
return queryClient.setQueryData(queryKey, () => newData);
|
|
184
184
|
};
|
|
185
185
|
const getModelConfig = () => {
|
|
186
186
|
const { options } = config;
|
|
@@ -344,27 +344,7 @@ async function makeRequest({ body = {}, method = HttpMethod.GET, path, isFormDat
|
|
|
344
344
|
// configure body
|
|
345
345
|
body = (isFormData ? axios.toFormData(body) : body);
|
|
346
346
|
// configure request header1
|
|
347
|
-
|
|
348
|
-
headers['Content-Type'] = ContentType.APPLICATION_JSON;
|
|
349
|
-
}
|
|
350
|
-
else if (isApp) {
|
|
351
|
-
headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
|
|
352
|
-
// add the app files
|
|
353
|
-
for (const fileKey in appFiles) {
|
|
354
|
-
const currentFile = appFiles[fileKey];
|
|
355
|
-
if (Array.isArray(currentFile)) {
|
|
356
|
-
for (const innerFile of currentFile) {
|
|
357
|
-
body.append(fileKey, innerFile);
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
else {
|
|
361
|
-
body.append(fileKey, currentFile);
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
else {
|
|
366
|
-
delete headers['Content-Type'];
|
|
367
|
-
}
|
|
347
|
+
configureRequestHeader(isFormData, headers, isApp, appFiles, body);
|
|
368
348
|
try {
|
|
369
349
|
const axiosRequest = axiosInstance({ baseURL, headers, timeout });
|
|
370
350
|
const axiosRequestConfig = {
|
|
@@ -401,6 +381,29 @@ async function makeRequest({ body = {}, method = HttpMethod.GET, path, isFormDat
|
|
|
401
381
|
});
|
|
402
382
|
}
|
|
403
383
|
}
|
|
384
|
+
const configureRequestHeader = (isFormData, headers, isApp, appFiles, body) => {
|
|
385
|
+
if (!isFormData) {
|
|
386
|
+
headers['Content-Type'] = ContentType.APPLICATION_JSON;
|
|
387
|
+
}
|
|
388
|
+
else if (isApp) {
|
|
389
|
+
headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
|
|
390
|
+
// add the app files
|
|
391
|
+
for (const fileKey in appFiles) {
|
|
392
|
+
const currentFile = appFiles[fileKey];
|
|
393
|
+
if (Array.isArray(currentFile)) {
|
|
394
|
+
for (const innerFile of currentFile) {
|
|
395
|
+
body.append(fileKey, innerFile);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
body.append(fileKey, currentFile);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
delete headers['Content-Type'];
|
|
405
|
+
}
|
|
406
|
+
};
|
|
404
407
|
function getAppFiles(body, fileSelectors = []) {
|
|
405
408
|
const files = {};
|
|
406
409
|
if (body) {
|
|
@@ -466,7 +469,9 @@ const useDeleteRequest = (deleteOptions) => {
|
|
|
466
469
|
const destroy = async (link, internalDeleteOptions) => {
|
|
467
470
|
if (!isFutureQueriesPaused) {
|
|
468
471
|
// set enabled to be true for every delete
|
|
469
|
-
internalDeleteOptions = internalDeleteOptions
|
|
472
|
+
internalDeleteOptions = internalDeleteOptions
|
|
473
|
+
? { ...internalDeleteOptions, queryKey: [link, {}], enabled: true }
|
|
474
|
+
: { queryKey: [link, {}], enabled: true };
|
|
470
475
|
await setOptionsAsync(internalDeleteOptions);
|
|
471
476
|
await updatedPathAsync(link);
|
|
472
477
|
return query.data;
|
|
@@ -773,10 +778,10 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
773
778
|
};
|
|
774
779
|
// register post mutation
|
|
775
780
|
const mutation = useMutation({
|
|
781
|
+
mutationKey: [path, { type: 'mutation' }],
|
|
776
782
|
mutationFn: (dataData) => new Promise((res, rej) => {
|
|
777
783
|
return sendRequest(res, rej, dataData);
|
|
778
784
|
}),
|
|
779
|
-
mutationKey: [path, { type: 'mutation' }],
|
|
780
785
|
});
|
|
781
786
|
const patch = async (data, options) => {
|
|
782
787
|
if (!isFutureMutationsPaused) {
|
|
@@ -854,8 +859,8 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
854
859
|
};
|
|
855
860
|
// register post mutation
|
|
856
861
|
const mutation = useMutation({
|
|
857
|
-
mutationFn: async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)),
|
|
858
862
|
mutationKey: [path, { type: 'mutation' }],
|
|
863
|
+
mutationFn: async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)),
|
|
859
864
|
});
|
|
860
865
|
const post = async (data, options) => {
|
|
861
866
|
if (!isFutureMutationsPaused) {
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -64,7 +64,7 @@ const useQueryModel = (keyTracker) => {
|
|
|
64
64
|
const data = get();
|
|
65
65
|
newData = lodashSet(data, path, newData);
|
|
66
66
|
}
|
|
67
|
-
return queryClient.setQueryData(queryKey, newData);
|
|
67
|
+
return queryClient.setQueryData(queryKey, () => newData);
|
|
68
68
|
};
|
|
69
69
|
const getModelConfig = () => {
|
|
70
70
|
const { options } = config;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export declare const useRefetchQuery: (queryKey: any[]) => Promise<{
|
|
2
|
-
refetchQuery: <T>(innerQueryKey?: any[]) => Promise<[import("@tanstack/query-core/build/legacy/
|
|
2
|
+
refetchQuery: <T>(innerQueryKey?: any[]) => Promise<[import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").z, T | undefined][]>;
|
|
3
3
|
}>;
|
|
@@ -10,7 +10,7 @@ export interface IPagination {
|
|
|
10
10
|
total: number;
|
|
11
11
|
}
|
|
12
12
|
export type TanstackQueryOption<TResponse> = UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, Array<any>>;
|
|
13
|
-
export type TanstackInfiniteQueryOption<TResponse> = UseInfiniteQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, Array<any
|
|
13
|
+
export type TanstackInfiniteQueryOption<TResponse> = Partial<UseInfiniteQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, Array<any>>>;
|
|
14
14
|
export interface DefaultRequestOptions {
|
|
15
15
|
baseUrl?: string;
|
|
16
16
|
headers?: RawAxiosRequestHeaders;
|
|
@@ -24,8 +24,8 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
|
|
|
24
24
|
isPlaceholderData: boolean;
|
|
25
25
|
isRefetching: boolean;
|
|
26
26
|
isStale: boolean;
|
|
27
|
-
refetch: (options?: import("@tanstack/query-core/build/legacy/
|
|
28
|
-
fetchStatus: import("@tanstack/query-core/build/legacy/
|
|
27
|
+
refetch: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ac | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").as<IRequestSuccess<TResponse>, any>>;
|
|
28
|
+
fetchStatus: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ak;
|
|
29
29
|
destroy: (link: string, internalDeleteOptions?: (UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> & {
|
|
30
30
|
cached?: boolean | undefined;
|
|
31
31
|
}) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
@@ -52,8 +52,8 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
|
|
|
52
52
|
isPlaceholderData: boolean;
|
|
53
53
|
isRefetching: boolean;
|
|
54
54
|
isStale: boolean;
|
|
55
|
-
refetch: (options?: import("@tanstack/query-core/build/legacy/
|
|
56
|
-
fetchStatus: import("@tanstack/query-core/build/legacy/
|
|
55
|
+
refetch: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ac | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").as<IRequestSuccess<TResponse>, any>>;
|
|
56
|
+
fetchStatus: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ak;
|
|
57
57
|
destroy: (link: string, internalDeleteOptions?: (UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> & {
|
|
58
58
|
cached?: boolean | undefined;
|
|
59
59
|
}) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
@@ -60,7 +60,9 @@ const useDeleteRequest = (deleteOptions) => {
|
|
|
60
60
|
const destroy = async (link, internalDeleteOptions) => {
|
|
61
61
|
if (!isFutureQueriesPaused) {
|
|
62
62
|
// set enabled to be true for every delete
|
|
63
|
-
internalDeleteOptions = internalDeleteOptions
|
|
63
|
+
internalDeleteOptions = internalDeleteOptions
|
|
64
|
+
? { ...internalDeleteOptions, queryKey: [link, {}], enabled: true }
|
|
65
|
+
: { queryKey: [link, {}], enabled: true };
|
|
64
66
|
await setOptionsAsync(internalDeleteOptions);
|
|
65
67
|
await updatedPathAsync(link);
|
|
66
68
|
return query.data;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { UseQueryOptions } from '@tanstack/react-query';
|
|
1
|
+
import type { InfiniteData, QueryKey, UseQueryOptions } from '@tanstack/react-query';
|
|
2
2
|
import type { IRequestError, IRequestSuccess } from '../request';
|
|
3
3
|
import type { DefaultRequestOptions, TanstackInfiniteQueryOption } from './queries.interface';
|
|
4
4
|
interface Pagination {
|
|
@@ -12,28 +12,32 @@ interface Pagination {
|
|
|
12
12
|
export declare const useGetInfiniteRequest: <TResponse extends Record<string, any>>({ path, load, queryOptions, keyTracker, baseUrl, headers, }: {
|
|
13
13
|
path: string;
|
|
14
14
|
load?: boolean | undefined;
|
|
15
|
-
queryOptions?:
|
|
15
|
+
queryOptions?: Partial<import("@tanstack/react-query/build/legacy/types").UseInfiniteQueryOptions<IRequestSuccess<(TResponse & {
|
|
16
16
|
pagination: Pagination;
|
|
17
|
-
}
|
|
17
|
+
}) | undefined>, IRequestError, IRequestSuccess<(TResponse & {
|
|
18
|
+
pagination: Pagination;
|
|
19
|
+
}) | undefined>, any[], QueryKey, unknown>> | undefined;
|
|
18
20
|
keyTracker?: string | undefined;
|
|
19
21
|
} & DefaultRequestOptions) => {
|
|
20
22
|
isLoading: boolean;
|
|
21
|
-
data: IRequestSuccess<TResponse & {
|
|
23
|
+
data: InfiniteData<IRequestSuccess<TResponse & {
|
|
22
24
|
pagination: Pagination;
|
|
23
|
-
}>;
|
|
25
|
+
}>, unknown>;
|
|
24
26
|
error: any;
|
|
25
27
|
isError: true;
|
|
26
28
|
isPending: false;
|
|
27
29
|
isLoadingError: false;
|
|
28
30
|
isRefetchError: true;
|
|
31
|
+
isFetchNextPageError: false;
|
|
32
|
+
isFetchPreviousPageError: false;
|
|
29
33
|
isSuccess: false;
|
|
30
34
|
status: "error";
|
|
31
|
-
fetchNextPage: (options?: import("@tanstack/query-core/build/legacy/
|
|
35
|
+
fetchNextPage: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ah | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").aA<InfiniteData<IRequestSuccess<TResponse & {
|
|
32
36
|
pagination: Pagination;
|
|
33
|
-
}>, any>>;
|
|
34
|
-
fetchPreviousPage: (options?: import("@tanstack/query-core/build/legacy/
|
|
37
|
+
}>, unknown>, any>>;
|
|
38
|
+
fetchPreviousPage: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ai | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").aA<InfiniteData<IRequestSuccess<TResponse & {
|
|
35
39
|
pagination: Pagination;
|
|
36
|
-
}>, any>>;
|
|
40
|
+
}>, unknown>, any>>;
|
|
37
41
|
hasNextPage: boolean;
|
|
38
42
|
hasPreviousPage: boolean;
|
|
39
43
|
isFetchingNextPage: boolean;
|
|
@@ -51,31 +55,33 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
51
55
|
isPlaceholderData: boolean;
|
|
52
56
|
isRefetching: boolean;
|
|
53
57
|
isStale: boolean;
|
|
54
|
-
refetch: (options?: import("@tanstack/query-core/build/legacy/
|
|
58
|
+
refetch: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ac | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").as<InfiniteData<IRequestSuccess<TResponse & {
|
|
55
59
|
pagination: Pagination;
|
|
56
|
-
}>, any>>;
|
|
57
|
-
fetchStatus: import("@tanstack/query-core/build/legacy/
|
|
58
|
-
get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse & {
|
|
60
|
+
}>, unknown>, any>>;
|
|
61
|
+
fetchStatus: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ak;
|
|
62
|
+
get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<InfiniteData<IRequestSuccess<TResponse & {
|
|
59
63
|
pagination: Pagination;
|
|
60
|
-
}> | undefined>;
|
|
64
|
+
}>, unknown> | undefined>;
|
|
61
65
|
} | {
|
|
62
66
|
isLoading: boolean;
|
|
63
|
-
data: IRequestSuccess<TResponse & {
|
|
67
|
+
data: InfiniteData<IRequestSuccess<TResponse & {
|
|
64
68
|
pagination: Pagination;
|
|
65
|
-
}>;
|
|
69
|
+
}>, unknown>;
|
|
66
70
|
error: null;
|
|
67
71
|
isError: false;
|
|
68
72
|
isPending: false;
|
|
69
73
|
isLoadingError: false;
|
|
70
74
|
isRefetchError: false;
|
|
75
|
+
isFetchNextPageError: false;
|
|
76
|
+
isFetchPreviousPageError: false;
|
|
71
77
|
isSuccess: true;
|
|
72
78
|
status: "success";
|
|
73
|
-
fetchNextPage: (options?: import("@tanstack/query-core/build/legacy/
|
|
79
|
+
fetchNextPage: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ah | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").aA<InfiniteData<IRequestSuccess<TResponse & {
|
|
74
80
|
pagination: Pagination;
|
|
75
|
-
}>, any>>;
|
|
76
|
-
fetchPreviousPage: (options?: import("@tanstack/query-core/build/legacy/
|
|
81
|
+
}>, unknown>, any>>;
|
|
82
|
+
fetchPreviousPage: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ai | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").aA<InfiniteData<IRequestSuccess<TResponse & {
|
|
77
83
|
pagination: Pagination;
|
|
78
|
-
}>, any>>;
|
|
84
|
+
}>, unknown>, any>>;
|
|
79
85
|
hasNextPage: boolean;
|
|
80
86
|
hasPreviousPage: boolean;
|
|
81
87
|
isFetchingNextPage: boolean;
|
|
@@ -93,12 +99,12 @@ export declare const useGetInfiniteRequest: <TResponse extends Record<string, an
|
|
|
93
99
|
isPlaceholderData: boolean;
|
|
94
100
|
isRefetching: boolean;
|
|
95
101
|
isStale: boolean;
|
|
96
|
-
refetch: (options?: import("@tanstack/query-core/build/legacy/
|
|
102
|
+
refetch: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ac | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").as<InfiniteData<IRequestSuccess<TResponse & {
|
|
97
103
|
pagination: Pagination;
|
|
98
|
-
}>, any>>;
|
|
99
|
-
fetchStatus: import("@tanstack/query-core/build/legacy/
|
|
100
|
-
get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse & {
|
|
104
|
+
}>, unknown>, any>>;
|
|
105
|
+
fetchStatus: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ak;
|
|
106
|
+
get: (link: string, fetchOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<InfiniteData<IRequestSuccess<TResponse & {
|
|
101
107
|
pagination: Pagination;
|
|
102
|
-
}> | undefined>;
|
|
108
|
+
}>, unknown> | undefined>;
|
|
103
109
|
};
|
|
104
110
|
export {};
|
|
@@ -37,8 +37,8 @@ export declare const useGetRequest: <TResponse extends Record<string, any>>({ pa
|
|
|
37
37
|
isPlaceholderData: boolean;
|
|
38
38
|
isRefetching: boolean;
|
|
39
39
|
isStale: boolean;
|
|
40
|
-
refetch: (options?: import("@tanstack/query-core/build/legacy/
|
|
41
|
-
fetchStatus: import("@tanstack/query-core/build/legacy/
|
|
40
|
+
refetch: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ac | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").as<IRequestSuccess<TResponse>, any>>;
|
|
41
|
+
fetchStatus: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ak;
|
|
42
42
|
} | {
|
|
43
43
|
isLoading: boolean;
|
|
44
44
|
setRequestPath: import("react").Dispatch<import("react").SetStateAction<string>>;
|
|
@@ -69,6 +69,6 @@ export declare const useGetRequest: <TResponse extends Record<string, any>>({ pa
|
|
|
69
69
|
isPlaceholderData: boolean;
|
|
70
70
|
isRefetching: boolean;
|
|
71
71
|
isStale: boolean;
|
|
72
|
-
refetch: (options?: import("@tanstack/query-core/build/legacy/
|
|
73
|
-
fetchStatus: import("@tanstack/query-core/build/legacy/
|
|
72
|
+
refetch: (options?: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ac | undefined) => Promise<import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").as<IRequestSuccess<TResponse>, any>>;
|
|
73
|
+
fetchStatus: import("@tanstack/query-core/build/legacy/hydration-BZ2M_xzi").ak;
|
|
74
74
|
};
|
|
@@ -59,10 +59,10 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
59
59
|
};
|
|
60
60
|
// register post mutation
|
|
61
61
|
const mutation = useMutation({
|
|
62
|
+
mutationKey: [path, { type: 'mutation' }],
|
|
62
63
|
mutationFn: (dataData) => new Promise((res, rej) => {
|
|
63
64
|
return sendRequest(res, rej, dataData);
|
|
64
65
|
}),
|
|
65
|
-
mutationKey: [path, { type: 'mutation' }],
|
|
66
66
|
});
|
|
67
67
|
const patch = async (data, options) => {
|
|
68
68
|
if (!isFutureMutationsPaused) {
|
|
@@ -69,8 +69,8 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
69
69
|
};
|
|
70
70
|
// register post mutation
|
|
71
71
|
const mutation = useMutation({
|
|
72
|
-
mutationFn: async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)),
|
|
73
72
|
mutationKey: [path, { type: 'mutation' }],
|
|
73
|
+
mutationFn: async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)),
|
|
74
74
|
});
|
|
75
75
|
const post = async (data, options) => {
|
|
76
76
|
if (!isFutureMutationsPaused) {
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { MutateOptions } from '@tanstack/react-query';
|
|
2
|
+
import type { IRequestError, IRequestSuccess } from '../request/request.interface';
|
|
3
|
+
import type { DefaultRequestOptions } from './queries.interface';
|
|
4
|
+
export declare const usePutRequest: <TResponse>({ path, baseUrl, headers }: {
|
|
5
|
+
path: string;
|
|
6
|
+
} & DefaultRequestOptions) => {
|
|
7
|
+
isLoading: boolean;
|
|
8
|
+
data: undefined;
|
|
9
|
+
variables: undefined;
|
|
10
|
+
error: null;
|
|
11
|
+
isError: false;
|
|
12
|
+
isIdle: true;
|
|
13
|
+
isPending: false;
|
|
14
|
+
isSuccess: false;
|
|
15
|
+
status: "idle";
|
|
16
|
+
mutate: import("@tanstack/react-query/build/legacy/types").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
17
|
+
reset: () => void;
|
|
18
|
+
context: unknown;
|
|
19
|
+
failureCount: number;
|
|
20
|
+
failureReason: IRequestError | null;
|
|
21
|
+
isPaused: boolean;
|
|
22
|
+
submittedAt: number;
|
|
23
|
+
mutateAsync: import("@tanstack/react-query/build/legacy/types").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
24
|
+
put: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
25
|
+
uploadProgressPercent: number;
|
|
26
|
+
} | {
|
|
27
|
+
isLoading: boolean;
|
|
28
|
+
data: undefined;
|
|
29
|
+
variables: void;
|
|
30
|
+
error: null;
|
|
31
|
+
isError: false;
|
|
32
|
+
isIdle: false;
|
|
33
|
+
isPending: true;
|
|
34
|
+
isSuccess: false;
|
|
35
|
+
status: "pending";
|
|
36
|
+
mutate: import("@tanstack/react-query/build/legacy/types").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
37
|
+
reset: () => void;
|
|
38
|
+
context: unknown;
|
|
39
|
+
failureCount: number;
|
|
40
|
+
failureReason: IRequestError | null;
|
|
41
|
+
isPaused: boolean;
|
|
42
|
+
submittedAt: number;
|
|
43
|
+
mutateAsync: import("@tanstack/react-query/build/legacy/types").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
44
|
+
put: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
45
|
+
uploadProgressPercent: number;
|
|
46
|
+
} | {
|
|
47
|
+
isLoading: boolean;
|
|
48
|
+
data: undefined;
|
|
49
|
+
error: IRequestError;
|
|
50
|
+
variables: void;
|
|
51
|
+
isError: true;
|
|
52
|
+
isIdle: false;
|
|
53
|
+
isPending: false;
|
|
54
|
+
isSuccess: false;
|
|
55
|
+
status: "error";
|
|
56
|
+
mutate: import("@tanstack/react-query/build/legacy/types").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
57
|
+
reset: () => void;
|
|
58
|
+
context: unknown;
|
|
59
|
+
failureCount: number;
|
|
60
|
+
failureReason: IRequestError | null;
|
|
61
|
+
isPaused: boolean;
|
|
62
|
+
submittedAt: number;
|
|
63
|
+
mutateAsync: import("@tanstack/react-query/build/legacy/types").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
64
|
+
put: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
65
|
+
uploadProgressPercent: number;
|
|
66
|
+
} | {
|
|
67
|
+
isLoading: boolean;
|
|
68
|
+
data: IRequestSuccess<TResponse>;
|
|
69
|
+
error: null;
|
|
70
|
+
variables: void;
|
|
71
|
+
isError: false;
|
|
72
|
+
isIdle: false;
|
|
73
|
+
isPending: false;
|
|
74
|
+
isSuccess: true;
|
|
75
|
+
status: "success";
|
|
76
|
+
mutate: import("@tanstack/react-query/build/legacy/types").UseMutateFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
77
|
+
reset: () => void;
|
|
78
|
+
context: unknown;
|
|
79
|
+
failureCount: number;
|
|
80
|
+
failureReason: IRequestError | null;
|
|
81
|
+
isPaused: boolean;
|
|
82
|
+
submittedAt: number;
|
|
83
|
+
mutateAsync: import("@tanstack/react-query/build/legacy/types").UseMutateAsyncFunction<IRequestSuccess<TResponse>, IRequestError, void, unknown>;
|
|
84
|
+
put: (data: any, options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
85
|
+
uploadProgressPercent: number;
|
|
86
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
2
|
import { axiosInstance } from './axios-instance.js';
|
|
3
|
-
import {
|
|
3
|
+
import { HttpMethod, ContentType } from './request.enum.js';
|
|
4
4
|
import { errorTransformer, successTransformer } from './transformer.js';
|
|
5
5
|
|
|
6
6
|
async function makeRequest({ body = {}, method = HttpMethod.GET, path, isFormData, headers = {}, baseURL, timeout, appFileConfig, onUploadProgress, }) {
|
|
@@ -11,27 +11,7 @@ async function makeRequest({ body = {}, method = HttpMethod.GET, path, isFormDat
|
|
|
11
11
|
// configure body
|
|
12
12
|
body = (isFormData ? axios.toFormData(body) : body);
|
|
13
13
|
// configure request header1
|
|
14
|
-
|
|
15
|
-
headers['Content-Type'] = ContentType.APPLICATION_JSON;
|
|
16
|
-
}
|
|
17
|
-
else if (isApp) {
|
|
18
|
-
headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
|
|
19
|
-
// add the app files
|
|
20
|
-
for (const fileKey in appFiles) {
|
|
21
|
-
const currentFile = appFiles[fileKey];
|
|
22
|
-
if (Array.isArray(currentFile)) {
|
|
23
|
-
for (const innerFile of currentFile) {
|
|
24
|
-
body.append(fileKey, innerFile);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
body.append(fileKey, currentFile);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
delete headers['Content-Type'];
|
|
34
|
-
}
|
|
14
|
+
configureRequestHeader(isFormData, headers, isApp, appFiles, body);
|
|
35
15
|
try {
|
|
36
16
|
const axiosRequest = axiosInstance({ baseURL, headers, timeout });
|
|
37
17
|
const axiosRequestConfig = {
|
|
@@ -68,6 +48,29 @@ async function makeRequest({ body = {}, method = HttpMethod.GET, path, isFormDat
|
|
|
68
48
|
});
|
|
69
49
|
}
|
|
70
50
|
}
|
|
51
|
+
const configureRequestHeader = (isFormData, headers, isApp, appFiles, body) => {
|
|
52
|
+
if (!isFormData) {
|
|
53
|
+
headers['Content-Type'] = ContentType.APPLICATION_JSON;
|
|
54
|
+
}
|
|
55
|
+
else if (isApp) {
|
|
56
|
+
headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
|
|
57
|
+
// add the app files
|
|
58
|
+
for (const fileKey in appFiles) {
|
|
59
|
+
const currentFile = appFiles[fileKey];
|
|
60
|
+
if (Array.isArray(currentFile)) {
|
|
61
|
+
for (const innerFile of currentFile) {
|
|
62
|
+
body.append(fileKey, innerFile);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
body.append(fileKey, currentFile);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
delete headers['Content-Type'];
|
|
72
|
+
}
|
|
73
|
+
};
|
|
71
74
|
function getAppFiles(body, fileSelectors = []) {
|
|
72
75
|
const files = {};
|
|
73
76
|
if (body) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"make-request.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"make-request.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ventlio/tanstack-query",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"contributors": [
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"@rollup/plugin-json": "^6.0.0",
|
|
48
48
|
"@rollup/plugin-node-resolve": "^15.0.1",
|
|
49
49
|
"@rollup/plugin-typescript": "^11.0.0",
|
|
50
|
-
"@tanstack/react-query": "
|
|
50
|
+
"@tanstack/react-query": "5.45.1",
|
|
51
51
|
"@testing-library/react": "^13.1",
|
|
52
52
|
"@testing-library/react-hooks": "^8.0.1",
|
|
53
53
|
"@types/axios": "^0.14.0",
|
|
@@ -75,7 +75,7 @@ export const useQueryModel = <T>(keyTracker: string): QueryModelBuilder<T> => {
|
|
|
75
75
|
newData = lodashSet(data, path, newData);
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
return queryClient.setQueryData(queryKey, newData as
|
|
78
|
+
return queryClient.setQueryData(queryKey, () => newData as any) as DataType;
|
|
79
79
|
};
|
|
80
80
|
|
|
81
81
|
const getModelConfig = () => {
|
|
@@ -18,11 +18,13 @@ export type TanstackQueryOption<TResponse> = UseQueryOptions<
|
|
|
18
18
|
Array<any>
|
|
19
19
|
>;
|
|
20
20
|
|
|
21
|
-
export type TanstackInfiniteQueryOption<TResponse> =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
export type TanstackInfiniteQueryOption<TResponse> = Partial<
|
|
22
|
+
UseInfiniteQueryOptions<
|
|
23
|
+
IRequestSuccess<TResponse | undefined>,
|
|
24
|
+
IRequestError,
|
|
25
|
+
IRequestSuccess<TResponse | undefined>,
|
|
26
|
+
Array<any>
|
|
27
|
+
>
|
|
26
28
|
>;
|
|
27
29
|
|
|
28
30
|
export interface DefaultRequestOptions {
|
|
@@ -79,7 +79,9 @@ export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOption
|
|
|
79
79
|
): Promise<IRequestSuccess<TResponse> | undefined> => {
|
|
80
80
|
if (!isFutureQueriesPaused) {
|
|
81
81
|
// set enabled to be true for every delete
|
|
82
|
-
internalDeleteOptions = internalDeleteOptions
|
|
82
|
+
internalDeleteOptions = internalDeleteOptions
|
|
83
|
+
? { ...internalDeleteOptions, queryKey: [link, {}], enabled: true }
|
|
84
|
+
: { queryKey: [link, {}], enabled: true };
|
|
83
85
|
|
|
84
86
|
await setOptionsAsync(internalDeleteOptions);
|
|
85
87
|
await updatedPathAsync(link);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { QueryKey, UseQueryOptions } from '@tanstack/react-query';
|
|
1
|
+
import type { InfiniteData, QueryKey, UseQueryOptions } from '@tanstack/react-query';
|
|
2
2
|
import { useInfiniteQuery, useQueryClient } from '@tanstack/react-query';
|
|
3
3
|
import { startTransition, useEffect, useMemo, useState } from 'react';
|
|
4
4
|
import { useEnvironmentVariables, useQueryConfig } from '../config';
|
|
@@ -110,7 +110,7 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
110
110
|
return pathname + '?' + queryParams.toString();
|
|
111
111
|
};
|
|
112
112
|
|
|
113
|
-
const query = useInfiniteQuery<any, any, IRequestSuccess<TResponse & { pagination: Pagination }
|
|
113
|
+
const query = useInfiniteQuery<any, any, InfiniteData<IRequestSuccess<TResponse & { pagination: Pagination }>>>({
|
|
114
114
|
queryKey: [requestPath, {}],
|
|
115
115
|
queryFn: ({ pageParam = requestPath, queryKey }) =>
|
|
116
116
|
new Promise<IRequestSuccess<TResponse & { pagination: Pagination }> | IRequestError>((res, rej) =>
|
|
@@ -137,10 +137,12 @@ export const useGetInfiniteRequest = <TResponse extends Record<string, any>>({
|
|
|
137
137
|
Array<any>
|
|
138
138
|
>
|
|
139
139
|
): Promise<
|
|
140
|
-
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
140
|
+
| InfiniteData<
|
|
141
|
+
IRequestSuccess<
|
|
142
|
+
TResponse & {
|
|
143
|
+
pagination: Pagination;
|
|
144
|
+
}
|
|
145
|
+
>
|
|
144
146
|
>
|
|
145
147
|
| undefined
|
|
146
148
|
> => {
|
|
@@ -64,11 +64,11 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
|
|
|
64
64
|
|
|
65
65
|
// register post mutation
|
|
66
66
|
const mutation = useMutation<IRequestSuccess<TResponse>, IRequestError>({
|
|
67
|
+
mutationKey: [path, { type: 'mutation' }],
|
|
67
68
|
mutationFn: (dataData: any) =>
|
|
68
69
|
new Promise<IRequestSuccess<TResponse>>((res, rej) => {
|
|
69
70
|
return sendRequest(res, rej, dataData);
|
|
70
71
|
}),
|
|
71
|
-
mutationKey: [path, { type: 'mutation' }],
|
|
72
72
|
});
|
|
73
73
|
|
|
74
74
|
const patch = async (
|
|
@@ -96,9 +96,9 @@ export const usePostRequest = <TResponse>({
|
|
|
96
96
|
IRequestError,
|
|
97
97
|
{ data: any; requestConfig?: Partial<Omit<IMakeRequest, 'body'>> }
|
|
98
98
|
>({
|
|
99
|
+
mutationKey: [path, { type: 'mutation' }],
|
|
99
100
|
mutationFn: async (postData) =>
|
|
100
101
|
new Promise<IRequestSuccess<TResponse>>((res, rej) => sendRequest(res, rej, postData)),
|
|
101
|
-
mutationKey: [path, { type: 'mutation' }],
|
|
102
102
|
});
|
|
103
103
|
|
|
104
104
|
const post = async <T>(
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { MutateOptions } from '@tanstack/react-query';
|
|
2
|
+
import { useMutation } from '@tanstack/react-query';
|
|
3
|
+
import { useEffect, useState } from 'react';
|
|
4
|
+
import { useEnvironmentVariables, useQueryConfig } from '../config';
|
|
5
|
+
import { scrollToTop } from '../helpers';
|
|
6
|
+
import { useUploadProgress } from '../hooks';
|
|
7
|
+
import { HttpMethod, makeRequest } from '../request';
|
|
8
|
+
import type { IRequestError, IRequestSuccess } from '../request/request.interface';
|
|
9
|
+
import { useHeaderStore, usePauseFutureRequests } from '../stores';
|
|
10
|
+
import type { DefaultRequestOptions } from './queries.interface';
|
|
11
|
+
|
|
12
|
+
export const usePutRequest = <TResponse>({ path, baseUrl, headers }: { path: string } & DefaultRequestOptions) => {
|
|
13
|
+
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
14
|
+
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
15
|
+
const globalHeaders = useHeaderStore((state) => state.headers);
|
|
16
|
+
|
|
17
|
+
const [requestPayload, setRequestPayload] = useState<Record<any, any>>();
|
|
18
|
+
|
|
19
|
+
const isFutureMutationsPaused = usePauseFutureRequests((state) => state.isFutureMutationsPaused);
|
|
20
|
+
|
|
21
|
+
const config = useQueryConfig();
|
|
22
|
+
|
|
23
|
+
const sendRequest = async (res: (value: any) => void, rej: (reason?: any) => void, data: any) => {
|
|
24
|
+
// get request headers
|
|
25
|
+
|
|
26
|
+
const requestOptions = {
|
|
27
|
+
path: path,
|
|
28
|
+
body: data,
|
|
29
|
+
method: HttpMethod.PUT,
|
|
30
|
+
headers: { ...globalHeaders, ...headers },
|
|
31
|
+
baseURL: baseUrl ?? API_URL,
|
|
32
|
+
timeout: TIMEOUT,
|
|
33
|
+
onUploadProgress,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
let shouldContinue = true;
|
|
37
|
+
|
|
38
|
+
if (config.options?.mutationMiddleware) {
|
|
39
|
+
shouldContinue = await config.options.mutationMiddleware({
|
|
40
|
+
mutationKey: [path, { type: 'mutation' }],
|
|
41
|
+
...requestOptions,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (shouldContinue) {
|
|
46
|
+
const putResponse = await makeRequest<TResponse>(requestOptions);
|
|
47
|
+
if (putResponse.status) {
|
|
48
|
+
// scroll to top after success
|
|
49
|
+
if (config.options?.context !== 'app') {
|
|
50
|
+
scrollToTop();
|
|
51
|
+
}
|
|
52
|
+
res(putResponse as IRequestSuccess<TResponse>);
|
|
53
|
+
} else {
|
|
54
|
+
// scroll to top after error
|
|
55
|
+
if (config.options?.context !== 'app') {
|
|
56
|
+
scrollToTop();
|
|
57
|
+
}
|
|
58
|
+
rej(putResponse);
|
|
59
|
+
}
|
|
60
|
+
} else {
|
|
61
|
+
rej(null);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// register post mutation
|
|
66
|
+
const mutation = useMutation<IRequestSuccess<TResponse>, IRequestError>({
|
|
67
|
+
mutationKey: [path, { type: 'mutation' }],
|
|
68
|
+
mutationFn: (dataData: any) =>
|
|
69
|
+
new Promise<IRequestSuccess<TResponse>>((res, rej) => {
|
|
70
|
+
return sendRequest(res, rej, dataData);
|
|
71
|
+
}),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const put = async (
|
|
75
|
+
data: any,
|
|
76
|
+
options?: MutateOptions<IRequestSuccess<TResponse>, IRequestError, void, unknown> | undefined
|
|
77
|
+
): Promise<IRequestSuccess<TResponse> | undefined> => {
|
|
78
|
+
if (!isFutureMutationsPaused) {
|
|
79
|
+
return mutation.mutateAsync(data, options);
|
|
80
|
+
} else {
|
|
81
|
+
setRequestPayload({ data, options });
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
if (!isFutureMutationsPaused && requestPayload) {
|
|
88
|
+
put(requestPayload.data, requestPayload.options);
|
|
89
|
+
setRequestPayload(undefined);
|
|
90
|
+
}
|
|
91
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
92
|
+
}, [isFutureMutationsPaused]);
|
|
93
|
+
|
|
94
|
+
return { put, uploadProgressPercent, ...mutation, isLoading: mutation.isPending || isFutureMutationsPaused };
|
|
95
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AxiosRequestConfig } from 'axios';
|
|
1
|
+
import type { AxiosRequestConfig, RawAxiosRequestHeaders } from 'axios';
|
|
2
2
|
import axios from 'axios';
|
|
3
3
|
import { axiosInstance } from './axios-instance';
|
|
4
4
|
|
|
@@ -26,24 +26,7 @@ export async function makeRequest<TResponse>({
|
|
|
26
26
|
body = (isFormData ? axios.toFormData(body as FormData) : body) as FormData;
|
|
27
27
|
|
|
28
28
|
// configure request header1
|
|
29
|
-
|
|
30
|
-
headers['Content-Type'] = ContentType.APPLICATION_JSON;
|
|
31
|
-
} else if (isApp) {
|
|
32
|
-
headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
|
|
33
|
-
// add the app files
|
|
34
|
-
for (const fileKey in appFiles) {
|
|
35
|
-
const currentFile = appFiles[fileKey];
|
|
36
|
-
if (Array.isArray(currentFile)) {
|
|
37
|
-
for (const innerFile of currentFile) {
|
|
38
|
-
body.append(fileKey, innerFile);
|
|
39
|
-
}
|
|
40
|
-
} else {
|
|
41
|
-
body.append(fileKey, currentFile);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
} else {
|
|
45
|
-
delete headers['Content-Type'];
|
|
46
|
-
}
|
|
29
|
+
configureRequestHeader(isFormData, headers, isApp, appFiles, body);
|
|
47
30
|
|
|
48
31
|
try {
|
|
49
32
|
const axiosRequest = axiosInstance({ baseURL, headers, timeout });
|
|
@@ -87,6 +70,34 @@ export async function makeRequest<TResponse>({
|
|
|
87
70
|
});
|
|
88
71
|
}
|
|
89
72
|
}
|
|
73
|
+
|
|
74
|
+
const configureRequestHeader = (
|
|
75
|
+
isFormData: boolean | undefined,
|
|
76
|
+
headers: RawAxiosRequestHeaders,
|
|
77
|
+
isApp: boolean | undefined,
|
|
78
|
+
appFiles: Record<string, string>,
|
|
79
|
+
body: Record<string, any>
|
|
80
|
+
) => {
|
|
81
|
+
if (!isFormData) {
|
|
82
|
+
headers['Content-Type'] = ContentType.APPLICATION_JSON;
|
|
83
|
+
} else if (isApp) {
|
|
84
|
+
headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
|
|
85
|
+
// add the app files
|
|
86
|
+
for (const fileKey in appFiles) {
|
|
87
|
+
const currentFile = appFiles[fileKey];
|
|
88
|
+
if (Array.isArray(currentFile)) {
|
|
89
|
+
for (const innerFile of currentFile) {
|
|
90
|
+
body.append(fileKey, innerFile);
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
body.append(fileKey, currentFile);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
delete headers['Content-Type'];
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
90
101
|
function getAppFiles(body: any, fileSelectors: string[] = []) {
|
|
91
102
|
const files: Record<string, string> = {};
|
|
92
103
|
|