@ventlio/tanstack-query 0.3.0 → 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/bootstrapQueryRequest.js +1 -1
- 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 +65 -155
- 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/bootstrapQueryRequest.ts +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
|
@@ -2,35 +2,9 @@ import { useQueryClient } from '@tanstack/react-query';
|
|
|
2
2
|
|
|
3
3
|
const useQueryConfig = () => {
|
|
4
4
|
const queryClient = useQueryClient();
|
|
5
|
-
const setConfig = (options) => {
|
|
6
|
-
let mutationMeta = queryClient.getMutationDefaults()?.meta ?? {};
|
|
7
|
-
let queryMeta = queryClient.getQueryDefaults()?.meta ?? {};
|
|
8
|
-
const { pauseFutureMutations, pauseFutureQueries, mutationMiddleware, queryMiddleware, ...otherOptions } = options;
|
|
9
|
-
if (pauseFutureMutations) {
|
|
10
|
-
mutationMeta.pauseFutureMutations = pauseFutureMutations;
|
|
11
|
-
}
|
|
12
|
-
if (mutationMiddleware) {
|
|
13
|
-
mutationMeta.mutationMiddleware = mutationMiddleware;
|
|
14
|
-
}
|
|
15
|
-
mutationMeta = { meta: { ...mutationMeta, ...otherOptions } };
|
|
16
|
-
if (pauseFutureQueries) {
|
|
17
|
-
queryMeta.pauseFutureQueries = pauseFutureQueries;
|
|
18
|
-
}
|
|
19
|
-
if (queryMiddleware) {
|
|
20
|
-
queryMeta.mutationMiddleware = queryMiddleware;
|
|
21
|
-
}
|
|
22
|
-
queryMeta = { meta: { ...queryMeta, ...otherOptions } };
|
|
23
|
-
const defaultMutationOptions = queryClient.defaultMutationOptions();
|
|
24
|
-
const defaultQueryOptions = queryClient.defaultQueryOptions();
|
|
25
|
-
queryClient.setDefaultOptions({
|
|
26
|
-
queries: { ...defaultQueryOptions, meta: queryMeta },
|
|
27
|
-
mutations: { ...defaultMutationOptions, meta: mutationMeta },
|
|
28
|
-
});
|
|
29
|
-
};
|
|
30
5
|
const mutationMeta = (queryClient.getDefaultOptions().mutations?.meta ?? {});
|
|
31
6
|
const queryMeta = (queryClient.getDefaultOptions().queries?.meta ?? {});
|
|
32
|
-
|
|
33
|
-
return { options, setConfig };
|
|
7
|
+
return { ...queryMeta, ...mutationMeta };
|
|
34
8
|
};
|
|
35
9
|
|
|
36
10
|
export { useQueryConfig };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQueryConfig.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useQueryConfig.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;"}
|
|
@@ -1,12 +1,23 @@
|
|
|
1
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
1
2
|
import { useQueryConfig } from './useQueryConfig.js';
|
|
2
3
|
|
|
3
4
|
const useQueryHeaders = () => {
|
|
4
|
-
const
|
|
5
|
+
const queryClient = useQueryClient();
|
|
6
|
+
const { headers, options } = useQueryConfig();
|
|
5
7
|
const getHeaders = () => {
|
|
6
|
-
return
|
|
8
|
+
return headers;
|
|
7
9
|
};
|
|
8
|
-
const setQueryHeaders = (
|
|
9
|
-
|
|
10
|
+
const setQueryHeaders = (newHeaders) => {
|
|
11
|
+
const defaultMeta = {
|
|
12
|
+
headers: { ...headers, ...newHeaders },
|
|
13
|
+
options,
|
|
14
|
+
};
|
|
15
|
+
queryClient.setDefaultOptions({
|
|
16
|
+
queries: {
|
|
17
|
+
meta: defaultMeta,
|
|
18
|
+
},
|
|
19
|
+
mutations: { meta: defaultMeta },
|
|
20
|
+
});
|
|
10
21
|
};
|
|
11
22
|
return { setQueryHeaders, getHeaders };
|
|
12
23
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQueryHeaders.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useQueryHeaders.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -2,9 +2,9 @@ import { useQueryConfig } from './useQueryConfig.js';
|
|
|
2
2
|
|
|
3
3
|
const useReactNativeEnv = () => {
|
|
4
4
|
const config = useQueryConfig();
|
|
5
|
-
const appUrl = config.options
|
|
6
|
-
const appTimeout = config.options
|
|
7
|
-
const isApp = config.options
|
|
5
|
+
const appUrl = config.options?.environments?.appBaseUrl;
|
|
6
|
+
const appTimeout = config.options?.environments?.appTimeout;
|
|
7
|
+
const isApp = config.options?.context === 'app';
|
|
8
8
|
return { appUrl, appTimeout, isApp };
|
|
9
9
|
};
|
|
10
10
|
|
package/dist/index.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import 'url-search-params-polyfill';
|
|
|
2
2
|
import { useQueryClient, useQuery, useInfiniteQuery, useMutation } from '@tanstack/react-query';
|
|
3
3
|
import result from 'lodash.result';
|
|
4
4
|
import lodashSet from 'lodash.set';
|
|
5
|
-
import { useState,
|
|
5
|
+
import { useState, useMemo, useEffect, startTransition } from 'react';
|
|
6
6
|
import axios from 'axios';
|
|
7
7
|
|
|
8
8
|
const bootstrapQueryRequest = (queryClient, options) => {
|
|
@@ -12,7 +12,7 @@ const bootstrapQueryRequest = (queryClient, options) => {
|
|
|
12
12
|
headers: {
|
|
13
13
|
Authorization: ``,
|
|
14
14
|
},
|
|
15
|
-
|
|
15
|
+
options,
|
|
16
16
|
};
|
|
17
17
|
queryClient.setDefaultOptions({
|
|
18
18
|
queries: {
|
|
@@ -24,42 +24,16 @@ const bootstrapQueryRequest = (queryClient, options) => {
|
|
|
24
24
|
|
|
25
25
|
const useQueryConfig = () => {
|
|
26
26
|
const queryClient = useQueryClient();
|
|
27
|
-
const setConfig = (options) => {
|
|
28
|
-
let mutationMeta = queryClient.getMutationDefaults()?.meta ?? {};
|
|
29
|
-
let queryMeta = queryClient.getQueryDefaults()?.meta ?? {};
|
|
30
|
-
const { pauseFutureMutations, pauseFutureQueries, mutationMiddleware, queryMiddleware, ...otherOptions } = options;
|
|
31
|
-
if (pauseFutureMutations) {
|
|
32
|
-
mutationMeta.pauseFutureMutations = pauseFutureMutations;
|
|
33
|
-
}
|
|
34
|
-
if (mutationMiddleware) {
|
|
35
|
-
mutationMeta.mutationMiddleware = mutationMiddleware;
|
|
36
|
-
}
|
|
37
|
-
mutationMeta = { meta: { ...mutationMeta, ...otherOptions } };
|
|
38
|
-
if (pauseFutureQueries) {
|
|
39
|
-
queryMeta.pauseFutureQueries = pauseFutureQueries;
|
|
40
|
-
}
|
|
41
|
-
if (queryMiddleware) {
|
|
42
|
-
queryMeta.mutationMiddleware = queryMiddleware;
|
|
43
|
-
}
|
|
44
|
-
queryMeta = { meta: { ...queryMeta, ...otherOptions } };
|
|
45
|
-
const defaultMutationOptions = queryClient.defaultMutationOptions();
|
|
46
|
-
const defaultQueryOptions = queryClient.defaultQueryOptions();
|
|
47
|
-
queryClient.setDefaultOptions({
|
|
48
|
-
queries: { ...defaultQueryOptions, meta: queryMeta },
|
|
49
|
-
mutations: { ...defaultMutationOptions, meta: mutationMeta },
|
|
50
|
-
});
|
|
51
|
-
};
|
|
52
27
|
const mutationMeta = (queryClient.getDefaultOptions().mutations?.meta ?? {});
|
|
53
28
|
const queryMeta = (queryClient.getDefaultOptions().queries?.meta ?? {});
|
|
54
|
-
|
|
55
|
-
return { options, setConfig };
|
|
29
|
+
return { ...queryMeta, ...mutationMeta };
|
|
56
30
|
};
|
|
57
31
|
|
|
58
32
|
const useReactNativeEnv = () => {
|
|
59
33
|
const config = useQueryConfig();
|
|
60
|
-
const appUrl = config.options
|
|
61
|
-
const appTimeout = config.options
|
|
62
|
-
const isApp = config.options
|
|
34
|
+
const appUrl = config.options?.environments?.appBaseUrl;
|
|
35
|
+
const appTimeout = config.options?.environments?.appTimeout;
|
|
36
|
+
const isApp = config.options?.context === 'app';
|
|
63
37
|
return { appUrl, appTimeout, isApp };
|
|
64
38
|
};
|
|
65
39
|
|
|
@@ -74,12 +48,22 @@ const useEnvironmentVariables = () => {
|
|
|
74
48
|
};
|
|
75
49
|
|
|
76
50
|
const useQueryHeaders = () => {
|
|
77
|
-
const
|
|
51
|
+
const queryClient = useQueryClient();
|
|
52
|
+
const { headers, options } = useQueryConfig();
|
|
78
53
|
const getHeaders = () => {
|
|
79
|
-
return
|
|
54
|
+
return headers;
|
|
80
55
|
};
|
|
81
|
-
const setQueryHeaders = (
|
|
82
|
-
|
|
56
|
+
const setQueryHeaders = (newHeaders) => {
|
|
57
|
+
const defaultMeta = {
|
|
58
|
+
headers: { ...headers, ...newHeaders },
|
|
59
|
+
options,
|
|
60
|
+
};
|
|
61
|
+
queryClient.setDefaultOptions({
|
|
62
|
+
queries: {
|
|
63
|
+
meta: defaultMeta,
|
|
64
|
+
},
|
|
65
|
+
mutations: { meta: defaultMeta },
|
|
66
|
+
});
|
|
83
67
|
};
|
|
84
68
|
return { setQueryHeaders, getHeaders };
|
|
85
69
|
};
|
|
@@ -103,11 +87,7 @@ function getDateInFuture(days) {
|
|
|
103
87
|
const useKeyTrackerModel = (keyTracker) => {
|
|
104
88
|
const queryClient = useQueryClient();
|
|
105
89
|
const getQueryKey = (innerKeyTracker) => {
|
|
106
|
-
const
|
|
107
|
-
...queryClient.getDefaultOptions().mutations?.meta,
|
|
108
|
-
...queryClient.getDefaultOptions().queries?.meta,
|
|
109
|
-
};
|
|
110
|
-
const queryKey = meta[innerKeyTracker ?? keyTracker];
|
|
90
|
+
const queryKey = queryClient.getQueryData([innerKeyTracker ?? keyTracker]);
|
|
111
91
|
return queryKey;
|
|
112
92
|
};
|
|
113
93
|
const refetchQuery = async (innerKeyTracker) => {
|
|
@@ -415,7 +395,6 @@ const useDeleteRequest = (deleteOptions) => {
|
|
|
415
395
|
const { baseUrl, headers } = deleteOptions ?? {};
|
|
416
396
|
const [requestPath, setRequestPath] = useState('');
|
|
417
397
|
const [options, setOptions] = useState();
|
|
418
|
-
const [destroyConfig, setDestroyConfig] = useState();
|
|
419
398
|
const { options: queryConfigOptions } = useQueryConfig();
|
|
420
399
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
421
400
|
const { getHeaders } = useQueryHeaders();
|
|
@@ -431,7 +410,7 @@ const useDeleteRequest = (deleteOptions) => {
|
|
|
431
410
|
timeout: TIMEOUT,
|
|
432
411
|
};
|
|
433
412
|
let shouldContinue = true;
|
|
434
|
-
if (queryConfigOptions
|
|
413
|
+
if (queryConfigOptions?.queryMiddleware) {
|
|
435
414
|
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
436
415
|
}
|
|
437
416
|
if (shouldContinue) {
|
|
@@ -455,37 +434,22 @@ const useDeleteRequest = (deleteOptions) => {
|
|
|
455
434
|
return setOptions(fetchOptions);
|
|
456
435
|
};
|
|
457
436
|
const destroy = async (link, internalDeleteOptions) => {
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
else {
|
|
467
|
-
// save delete config
|
|
468
|
-
setDestroyConfig({ link, internalDeleteOptions });
|
|
469
|
-
return undefined;
|
|
470
|
-
}
|
|
471
|
-
};
|
|
472
|
-
useEffect(() => {
|
|
473
|
-
if (!queryConfigOptions.pauseFutureQueries && destroyConfig) {
|
|
474
|
-
destroy(destroyConfig.link, destroyConfig.internalDeleteOptions);
|
|
475
|
-
setDestroyConfig(undefined);
|
|
476
|
-
}
|
|
477
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
478
|
-
}, [queryConfigOptions.pauseFutureQueries]);
|
|
479
|
-
return { destroy, ...query, isLoading: query.isLoading || queryConfigOptions.pauseFutureQueries };
|
|
437
|
+
// set enabled to be true for every delete
|
|
438
|
+
internalDeleteOptions = internalDeleteOptions ?? {};
|
|
439
|
+
internalDeleteOptions.enabled = true;
|
|
440
|
+
await setOptionsAsync(internalDeleteOptions);
|
|
441
|
+
await updatedPathAsync(link);
|
|
442
|
+
return query.data;
|
|
443
|
+
};
|
|
444
|
+
return { destroy, ...query };
|
|
480
445
|
};
|
|
481
446
|
|
|
482
447
|
const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl, headers, }) => {
|
|
483
448
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
484
449
|
const { getHeaders } = useQueryHeaders();
|
|
485
450
|
const [requestPath, setRequestPath] = useState(path);
|
|
486
|
-
const [queryConfig, setQueryConfig] = useState();
|
|
487
451
|
const [options, setOptions] = useState(queryOptions);
|
|
488
|
-
const { options: queryConfigOptions
|
|
452
|
+
const { options: queryConfigOptions } = useQueryConfig();
|
|
489
453
|
let queryClient = useQueryClient();
|
|
490
454
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
491
455
|
queryClient = useMemo(() => queryClient, []);
|
|
@@ -500,7 +464,7 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
500
464
|
timeout: TIMEOUT,
|
|
501
465
|
};
|
|
502
466
|
let shouldContinue = true;
|
|
503
|
-
if (queryConfigOptions
|
|
467
|
+
if (queryConfigOptions?.queryMiddleware) {
|
|
504
468
|
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
505
469
|
}
|
|
506
470
|
if (shouldContinue) {
|
|
@@ -532,7 +496,7 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
532
496
|
return pathname + '?' + queryParams.toString();
|
|
533
497
|
};
|
|
534
498
|
const query = useInfiniteQuery([requestPath, {}], ({ pageParam = requestPath, queryKey }) => new Promise((res, rej) => sendRequest(res, rej, queryKey, pageParam)), {
|
|
535
|
-
enabled: load
|
|
499
|
+
enabled: load,
|
|
536
500
|
getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
|
|
537
501
|
getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
|
|
538
502
|
...options,
|
|
@@ -543,15 +507,9 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
543
507
|
});
|
|
544
508
|
};
|
|
545
509
|
const get = async (link, fetchOptions) => {
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
return query.data;
|
|
550
|
-
}
|
|
551
|
-
else {
|
|
552
|
-
setQueryConfig({ link, fetchOptions });
|
|
553
|
-
return undefined;
|
|
554
|
-
}
|
|
510
|
+
await setOptionsAsync(fetchOptions);
|
|
511
|
+
await updatedPathAsync(link);
|
|
512
|
+
return query.data;
|
|
555
513
|
};
|
|
556
514
|
const updatedPathAsync = async (link) => {
|
|
557
515
|
startTransition(() => {
|
|
@@ -560,21 +518,17 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
|
|
|
560
518
|
};
|
|
561
519
|
useEffect(() => {
|
|
562
520
|
if (keyTracker) {
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
get(queryConfig.link, queryConfig.fetchOptions);
|
|
570
|
-
setQueryConfig(undefined);
|
|
521
|
+
// set expiration time for the tracker
|
|
522
|
+
queryClient.setQueryDefaults([keyTracker], {
|
|
523
|
+
cacheTime: Infinity,
|
|
524
|
+
staleTime: Infinity,
|
|
525
|
+
});
|
|
526
|
+
queryClient.setQueryData([keyTracker], [requestPath, {}]);
|
|
571
527
|
}
|
|
572
|
-
|
|
573
|
-
}, [queryConfigOptions.pauseFutureQueries]);
|
|
528
|
+
}, [keyTracker, requestPath, queryClient, queryOptions?.staleTime]);
|
|
574
529
|
return {
|
|
575
530
|
get,
|
|
576
531
|
...query,
|
|
577
|
-
isLoading: query.isLoading || queryConfigOptions.pauseFutureQueries,
|
|
578
532
|
};
|
|
579
533
|
};
|
|
580
534
|
|
|
@@ -584,8 +538,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
|
|
|
584
538
|
const [page, setPage] = useState(1);
|
|
585
539
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
586
540
|
const { getHeaders } = useQueryHeaders();
|
|
587
|
-
const { options: queryConfigOptions
|
|
588
|
-
const [queryConfig, setQueryConfig] = useState();
|
|
541
|
+
const { options: queryConfigOptions } = useQueryConfig();
|
|
589
542
|
let queryClient = useQueryClient();
|
|
590
543
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
591
544
|
queryClient = useMemo(() => queryClient, []);
|
|
@@ -602,7 +555,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
|
|
|
602
555
|
timeout: TIMEOUT,
|
|
603
556
|
};
|
|
604
557
|
let shouldContinue = true;
|
|
605
|
-
if (queryConfigOptions
|
|
558
|
+
if (queryConfigOptions?.queryMiddleware) {
|
|
606
559
|
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
607
560
|
}
|
|
608
561
|
if (shouldContinue) {
|
|
@@ -633,10 +586,14 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
|
|
|
633
586
|
}, [path]);
|
|
634
587
|
useEffect(() => {
|
|
635
588
|
if (keyTracker) {
|
|
636
|
-
|
|
589
|
+
// set expiration time for the tracker
|
|
590
|
+
queryClient.setQueryDefaults([keyTracker], {
|
|
591
|
+
cacheTime: Infinity,
|
|
592
|
+
staleTime: Infinity,
|
|
593
|
+
});
|
|
594
|
+
queryClient.setQueryData([keyTracker], [requestPath, {}]);
|
|
637
595
|
}
|
|
638
|
-
|
|
639
|
-
}, [keyTracker, requestPath]);
|
|
596
|
+
}, [keyTracker, requestPath, queryClient, queryOptions?.staleTime]);
|
|
640
597
|
const nextPage = () => {
|
|
641
598
|
if (query.data?.data.pagination) {
|
|
642
599
|
const pagination = query.data.data.pagination;
|
|
@@ -679,26 +636,12 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
|
|
|
679
636
|
});
|
|
680
637
|
};
|
|
681
638
|
const get = async (link, fetchOptions) => {
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
return query.data;
|
|
686
|
-
}
|
|
687
|
-
else {
|
|
688
|
-
setQueryConfig({ link, fetchOptions });
|
|
689
|
-
return undefined;
|
|
690
|
-
}
|
|
639
|
+
await setOptionsAsync(fetchOptions);
|
|
640
|
+
await updatedPathAsync(link);
|
|
641
|
+
return query.data;
|
|
691
642
|
};
|
|
692
|
-
useEffect(() => {
|
|
693
|
-
if (!queryConfigOptions.pauseFutureQueries && queryConfig) {
|
|
694
|
-
get(queryConfig.link, queryConfig.fetchOptions);
|
|
695
|
-
setQueryConfig(undefined);
|
|
696
|
-
}
|
|
697
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
698
|
-
}, [queryConfigOptions.pauseFutureQueries]);
|
|
699
643
|
return {
|
|
700
644
|
...query,
|
|
701
|
-
isLoading: query.isLoading || queryConfigOptions.pauseFutureQueries,
|
|
702
645
|
setRequestPath,
|
|
703
646
|
nextPage,
|
|
704
647
|
prevPage,
|
|
@@ -722,7 +665,6 @@ const useUploadProgress = () => {
|
|
|
722
665
|
const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
723
666
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
724
667
|
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
725
|
-
const [mutationConfig, setMutationConfig] = useState();
|
|
726
668
|
const { getHeaders } = useQueryHeaders();
|
|
727
669
|
const config = useQueryConfig();
|
|
728
670
|
const sendRequest = async (res, rej, data) => {
|
|
@@ -738,7 +680,7 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
738
680
|
onUploadProgress,
|
|
739
681
|
};
|
|
740
682
|
let shouldContinue = true;
|
|
741
|
-
if (config.options
|
|
683
|
+
if (config.options?.mutationMiddleware) {
|
|
742
684
|
shouldContinue = await config.options.mutationMiddleware({
|
|
743
685
|
mutationKey: [path, { type: 'mutation' }],
|
|
744
686
|
...requestOptions,
|
|
@@ -748,14 +690,14 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
748
690
|
const patchResponse = await makeRequest(requestOptions);
|
|
749
691
|
if (patchResponse.status) {
|
|
750
692
|
// scroll to top after success
|
|
751
|
-
if (config.options
|
|
693
|
+
if (config.options?.context !== 'app') {
|
|
752
694
|
scrollToTop();
|
|
753
695
|
}
|
|
754
696
|
res(patchResponse);
|
|
755
697
|
}
|
|
756
698
|
else {
|
|
757
699
|
// scroll to top after error
|
|
758
|
-
if (config.options
|
|
700
|
+
if (config.options?.context !== 'app') {
|
|
759
701
|
scrollToTop();
|
|
760
702
|
}
|
|
761
703
|
rej(patchResponse);
|
|
@@ -770,33 +712,14 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
|
770
712
|
return sendRequest(res, rej, dataData);
|
|
771
713
|
}), { mutationKey: [path, { type: 'mutation' }] });
|
|
772
714
|
const patch = async (data, options) => {
|
|
773
|
-
|
|
774
|
-
return mutation.mutateAsync(data, options);
|
|
775
|
-
}
|
|
776
|
-
else {
|
|
777
|
-
setMutationConfig({ data, options });
|
|
778
|
-
return undefined;
|
|
779
|
-
}
|
|
780
|
-
};
|
|
781
|
-
useEffect(() => {
|
|
782
|
-
if (!config.options.pauseFutureMutations && mutationConfig) {
|
|
783
|
-
patch(mutationConfig.data, mutationConfig.options);
|
|
784
|
-
setMutationConfig(undefined);
|
|
785
|
-
}
|
|
786
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
787
|
-
}, [config.options.pauseFutureMutations]);
|
|
788
|
-
return {
|
|
789
|
-
patch,
|
|
790
|
-
uploadProgressPercent,
|
|
791
|
-
...mutation,
|
|
792
|
-
isLoading: mutation.isLoading || config.options.pauseFutureMutations,
|
|
715
|
+
return mutation.mutateAsync(data, options);
|
|
793
716
|
};
|
|
717
|
+
return { patch, uploadProgressPercent, ...mutation };
|
|
794
718
|
};
|
|
795
719
|
|
|
796
720
|
const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelectors, }) => {
|
|
797
721
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
798
722
|
const config = useQueryConfig();
|
|
799
|
-
const [mutationConfig, setMutationConfig] = useState();
|
|
800
723
|
const { getHeaders } = useQueryHeaders();
|
|
801
724
|
const { isApp } = useReactNativeEnv();
|
|
802
725
|
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
@@ -821,7 +744,7 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
821
744
|
...requestConfig,
|
|
822
745
|
};
|
|
823
746
|
let shouldContinue = true;
|
|
824
|
-
if (config.options
|
|
747
|
+
if (config.options?.mutationMiddleware) {
|
|
825
748
|
shouldContinue = await config.options.mutationMiddleware({
|
|
826
749
|
mutationKey: [path, { type: 'mutation' }],
|
|
827
750
|
...requestOptions,
|
|
@@ -831,14 +754,14 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
831
754
|
const postResponse = await makeRequest(requestOptions);
|
|
832
755
|
if (postResponse.status) {
|
|
833
756
|
// scroll to top after success
|
|
834
|
-
if (config.options
|
|
757
|
+
if (config.options?.context !== 'app') {
|
|
835
758
|
scrollToTop();
|
|
836
759
|
}
|
|
837
760
|
res(postResponse);
|
|
838
761
|
}
|
|
839
762
|
else {
|
|
840
763
|
// scroll to top after error
|
|
841
|
-
if (config.options
|
|
764
|
+
if (config.options?.context !== 'app') {
|
|
842
765
|
scrollToTop();
|
|
843
766
|
}
|
|
844
767
|
rej(postResponse);
|
|
@@ -853,22 +776,9 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
|
|
|
853
776
|
mutationKey: [path, { type: 'mutation' }],
|
|
854
777
|
});
|
|
855
778
|
const post = async (data, options) => {
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
return mutation.mutateAsync({ data, requestConfig }, otherOptions);
|
|
859
|
-
}
|
|
860
|
-
else {
|
|
861
|
-
setMutationConfig({ data, options });
|
|
862
|
-
return undefined;
|
|
863
|
-
}
|
|
779
|
+
const { requestConfig, ...otherOptions } = options ?? {};
|
|
780
|
+
return mutation.mutateAsync({ data, requestConfig }, otherOptions);
|
|
864
781
|
};
|
|
865
|
-
useEffect(() => {
|
|
866
|
-
if (!config.options.pauseFutureMutations && mutationConfig) {
|
|
867
|
-
post(mutationConfig.data, mutationConfig.options);
|
|
868
|
-
setMutationConfig(undefined);
|
|
869
|
-
}
|
|
870
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
871
|
-
}, [config.options.pauseFutureMutations]);
|
|
872
782
|
return { post, uploadProgressPercent, ...mutation };
|
|
873
783
|
};
|
|
874
784
|
|
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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -3,11 +3,7 @@ import { useQueryClient } from '@tanstack/react-query';
|
|
|
3
3
|
const useKeyTrackerModel = (keyTracker) => {
|
|
4
4
|
const queryClient = useQueryClient();
|
|
5
5
|
const getQueryKey = (innerKeyTracker) => {
|
|
6
|
-
const
|
|
7
|
-
...queryClient.getDefaultOptions().mutations?.meta,
|
|
8
|
-
...queryClient.getDefaultOptions().queries?.meta,
|
|
9
|
-
};
|
|
10
|
-
const queryKey = meta[innerKeyTracker ?? keyTracker];
|
|
6
|
+
const queryKey = queryClient.getQueryData([innerKeyTracker ?? keyTracker]);
|
|
11
7
|
return queryKey;
|
|
12
8
|
};
|
|
13
9
|
const refetchQuery = async (innerKeyTracker) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useKeyTrackerModel.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useKeyTrackerModel.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -2,10 +2,10 @@ import type { UseQueryOptions } from '@tanstack/react-query';
|
|
|
2
2
|
import type { IRequestError, IRequestSuccess } from '../request';
|
|
3
3
|
import type { DefaultRequestOptions } from './queries.interface';
|
|
4
4
|
export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultRequestOptions) => {
|
|
5
|
-
isLoading: boolean | undefined;
|
|
6
5
|
data: IRequestSuccess<TResponse>;
|
|
7
6
|
error: any;
|
|
8
7
|
isError: true;
|
|
8
|
+
isLoading: false;
|
|
9
9
|
isLoadingError: false;
|
|
10
10
|
isRefetchError: true;
|
|
11
11
|
isSuccess: false;
|
|
@@ -31,10 +31,10 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
|
|
|
31
31
|
cached?: boolean | undefined;
|
|
32
32
|
}) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
33
33
|
} | {
|
|
34
|
-
isLoading: boolean | undefined;
|
|
35
34
|
data: IRequestSuccess<TResponse>;
|
|
36
35
|
error: null;
|
|
37
36
|
isError: false;
|
|
37
|
+
isLoading: false;
|
|
38
38
|
isLoadingError: false;
|
|
39
39
|
isRefetchError: false;
|
|
40
40
|
isSuccess: true;
|
|
@@ -60,10 +60,10 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
|
|
|
60
60
|
cached?: boolean | undefined;
|
|
61
61
|
}) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
62
62
|
} | {
|
|
63
|
-
isLoading: boolean | undefined;
|
|
64
63
|
data: undefined;
|
|
65
64
|
error: any;
|
|
66
65
|
isError: true;
|
|
66
|
+
isLoading: false;
|
|
67
67
|
isLoadingError: true;
|
|
68
68
|
isRefetchError: false;
|
|
69
69
|
isSuccess: false;
|
|
@@ -89,10 +89,10 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
|
|
|
89
89
|
cached?: boolean | undefined;
|
|
90
90
|
}) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
|
|
91
91
|
} | {
|
|
92
|
-
isLoading: boolean | undefined;
|
|
93
92
|
data: undefined;
|
|
94
93
|
error: null;
|
|
95
94
|
isError: false;
|
|
95
|
+
isLoading: true;
|
|
96
96
|
isLoadingError: false;
|
|
97
97
|
isRefetchError: false;
|
|
98
98
|
isSuccess: false;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useQuery } from '@tanstack/react-query';
|
|
2
|
-
import { useState
|
|
2
|
+
import { useState } from 'react';
|
|
3
3
|
import 'url-search-params-polyfill';
|
|
4
4
|
import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
|
|
5
5
|
import { useQueryConfig } from '../config/useQueryConfig.js';
|
|
@@ -12,7 +12,6 @@ const useDeleteRequest = (deleteOptions) => {
|
|
|
12
12
|
const { baseUrl, headers } = deleteOptions ?? {};
|
|
13
13
|
const [requestPath, setRequestPath] = useState('');
|
|
14
14
|
const [options, setOptions] = useState();
|
|
15
|
-
const [destroyConfig, setDestroyConfig] = useState();
|
|
16
15
|
const { options: queryConfigOptions } = useQueryConfig();
|
|
17
16
|
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
18
17
|
const { getHeaders } = useQueryHeaders();
|
|
@@ -28,7 +27,7 @@ const useDeleteRequest = (deleteOptions) => {
|
|
|
28
27
|
timeout: TIMEOUT,
|
|
29
28
|
};
|
|
30
29
|
let shouldContinue = true;
|
|
31
|
-
if (queryConfigOptions
|
|
30
|
+
if (queryConfigOptions?.queryMiddleware) {
|
|
32
31
|
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
33
32
|
}
|
|
34
33
|
if (shouldContinue) {
|
|
@@ -52,28 +51,14 @@ const useDeleteRequest = (deleteOptions) => {
|
|
|
52
51
|
return setOptions(fetchOptions);
|
|
53
52
|
};
|
|
54
53
|
const destroy = async (link, internalDeleteOptions) => {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return query.data;
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
// save delete config
|
|
65
|
-
setDestroyConfig({ link, internalDeleteOptions });
|
|
66
|
-
return undefined;
|
|
67
|
-
}
|
|
54
|
+
// set enabled to be true for every delete
|
|
55
|
+
internalDeleteOptions = internalDeleteOptions ?? {};
|
|
56
|
+
internalDeleteOptions.enabled = true;
|
|
57
|
+
await setOptionsAsync(internalDeleteOptions);
|
|
58
|
+
await updatedPathAsync(link);
|
|
59
|
+
return query.data;
|
|
68
60
|
};
|
|
69
|
-
|
|
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 };
|
|
61
|
+
return { destroy, ...query };
|
|
77
62
|
};
|
|
78
63
|
|
|
79
64
|
export { useDeleteRequest };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useDeleteRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|