@ventlio/tanstack-query 0.2.86 → 0.2.89

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/dist/config/useQueryConfig.js +27 -1
  2. package/dist/config/useQueryConfig.js.map +1 -1
  3. package/dist/config/useQueryHeaders.js +4 -15
  4. package/dist/config/useQueryHeaders.js.map +1 -1
  5. package/dist/config/useReactNativeEnv.js +3 -3
  6. package/dist/index.mjs +261 -111
  7. package/dist/index.mjs.map +1 -1
  8. package/dist/model/useKeyTrackerModel.d.ts +1 -1
  9. package/dist/model/useKeyTrackerModel.js +5 -1
  10. package/dist/model/useKeyTrackerModel.js.map +1 -1
  11. package/dist/queries/useDeleteRequest.d.ts +4 -4
  12. package/dist/queries/useDeleteRequest.js +46 -19
  13. package/dist/queries/useDeleteRequest.js.map +1 -1
  14. package/dist/queries/useGetInfiniteRequest.d.ts +4 -4
  15. package/dist/queries/useGetInfiniteRequest.js +43 -20
  16. package/dist/queries/useGetInfiniteRequest.js.map +1 -1
  17. package/dist/queries/useGetRequest.d.ts +8 -8
  18. package/dist/queries/useGetRequest.js +48 -24
  19. package/dist/queries/useGetRequest.js.map +1 -1
  20. package/dist/queries/usePatchRequest.d.ts +8 -8
  21. package/dist/queries/usePatchRequest.js +48 -15
  22. package/dist/queries/usePatchRequest.js.map +1 -1
  23. package/dist/queries/usePostRequest.d.ts +4 -4
  24. package/dist/queries/usePostRequest.js +42 -14
  25. package/dist/queries/usePostRequest.js.map +1 -1
  26. package/dist/types/index.d.ts +15 -6
  27. package/package.json +1 -1
  28. package/src/config/useQueryConfig.ts +41 -4
  29. package/src/config/useQueryHeaders.ts +6 -18
  30. package/src/config/useReactNativeEnv.ts +3 -3
  31. package/src/model/useKeyTrackerModel.ts +5 -1
  32. package/src/queries/useDeleteRequest.ts +51 -23
  33. package/src/queries/useGetInfiniteRequest.ts +48 -24
  34. package/src/queries/useGetRequest.ts +53 -29
  35. package/src/queries/usePatchRequest.ts +52 -18
  36. package/src/queries/usePostRequest.ts +46 -17
  37. package/src/types/index.ts +11 -6
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, useMemo, useEffect, startTransition } from 'react';
5
+ import { useState, useEffect, useMemo, startTransition } from 'react';
6
6
  import axios from 'axios';
7
7
 
8
8
  const bootstrapQueryRequest = (queryClient, options) => {
@@ -24,16 +24,42 @@ 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
+ };
27
52
  const mutationMeta = (queryClient.getDefaultOptions().mutations?.meta ?? {});
28
53
  const queryMeta = (queryClient.getDefaultOptions().queries?.meta ?? {});
29
- return { ...queryMeta, ...mutationMeta };
54
+ const options = { ...queryMeta, ...mutationMeta };
55
+ return { options, setConfig };
30
56
  };
31
57
 
32
58
  const useReactNativeEnv = () => {
33
59
  const config = useQueryConfig();
34
- const appUrl = config.options?.environments?.appBaseUrl;
35
- const appTimeout = config.options?.environments?.appTimeout;
36
- const isApp = config.options?.context === 'app';
60
+ const appUrl = config.options.environments?.appBaseUrl;
61
+ const appTimeout = config.options.environments?.appTimeout;
62
+ const isApp = config.options.context === 'app';
37
63
  return { appUrl, appTimeout, isApp };
38
64
  };
39
65
 
@@ -48,22 +74,12 @@ const useEnvironmentVariables = () => {
48
74
  };
49
75
 
50
76
  const useQueryHeaders = () => {
51
- const queryClient = useQueryClient();
52
- const { headers, options } = useQueryConfig();
77
+ const { setConfig, ...config } = useQueryConfig();
53
78
  const getHeaders = () => {
54
- return headers;
79
+ return config.options?.headers;
55
80
  };
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
- });
81
+ const setQueryHeaders = (headers) => {
82
+ setConfig({ headers });
67
83
  };
68
84
  return { setQueryHeaders, getHeaders };
69
85
  };
@@ -87,7 +103,11 @@ function getDateInFuture(days) {
87
103
  const useKeyTrackerModel = (keyTracker) => {
88
104
  const queryClient = useQueryClient();
89
105
  const getQueryKey = (innerKeyTracker) => {
90
- const queryKey = queryClient.getQueryData([innerKeyTracker ?? keyTracker]);
106
+ const meta = {
107
+ ...queryClient.getDefaultOptions().mutations?.meta,
108
+ ...queryClient.getDefaultOptions().queries?.meta,
109
+ };
110
+ const queryKey = meta[innerKeyTracker ?? keyTracker];
91
111
  return queryKey;
92
112
  };
93
113
  const refetchQuery = async (innerKeyTracker) => {
@@ -393,69 +413,107 @@ function getAppFiles(body, fileSelectors = []) {
393
413
 
394
414
  const useDeleteRequest = (deleteOptions) => {
395
415
  const { baseUrl, headers } = deleteOptions ?? {};
396
- const [requestPath, updateDeletePath] = useState('');
416
+ const [requestPath, setRequestPath] = useState('');
397
417
  const [options, setOptions] = useState();
418
+ const [destroyConfig, setDestroyConfig] = useState();
419
+ const { options: queryConfigOptions } = useQueryConfig();
398
420
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
399
421
  const { getHeaders } = useQueryHeaders();
400
422
  const sendRequest = async (res, rej, queryKey) => {
401
423
  // get request headers
402
424
  const globalHeaders = getHeaders();
403
- const [url] = (queryKey ?? []);
404
- const postResponse = await makeRequest({
405
- path: url ?? requestPath,
425
+ const [url] = queryKey;
426
+ const requestUrl = (url ?? requestPath);
427
+ const requestOptions = {
428
+ path: requestUrl,
406
429
  headers: { ...globalHeaders, ...headers },
407
- method: HttpMethod.DELETE,
408
430
  baseURL: baseUrl ?? API_URL,
409
431
  timeout: TIMEOUT,
410
- });
411
- if (postResponse.status) {
412
- res(postResponse);
432
+ };
433
+ let shouldContinue = true;
434
+ if (queryConfigOptions.queryMiddleware) {
435
+ shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
436
+ }
437
+ if (shouldContinue) {
438
+ const postResponse = await makeRequest(requestOptions);
439
+ if (postResponse.status) {
440
+ res(postResponse);
441
+ }
442
+ else {
443
+ rej(postResponse);
444
+ }
413
445
  }
414
446
  else {
415
- rej(postResponse);
447
+ rej(null);
416
448
  }
417
449
  };
418
450
  const query = useQuery([requestPath, {}], ({ queryKey }) => new Promise((res, rej) => sendRequest(res, rej, queryKey)), { enabled: false, ...options });
419
451
  const updatedPathAsync = async (link) => {
420
- return updateDeletePath(link);
452
+ return setRequestPath(link);
421
453
  };
422
454
  const setOptionsAsync = async (fetchOptions) => {
423
455
  return setOptions(fetchOptions);
424
456
  };
425
457
  const destroy = async (link, internalDeleteOptions) => {
426
- // set enabled to be true for every delete
427
- internalDeleteOptions = internalDeleteOptions ?? {};
428
- internalDeleteOptions.enabled = true;
429
- await setOptionsAsync(internalDeleteOptions);
430
- await updatedPathAsync(link);
431
- return query.data;
432
- };
433
- return { destroy, ...query };
458
+ if (!queryConfigOptions.pauseFutureQueries) {
459
+ // set enabled to be true for every delete
460
+ internalDeleteOptions = internalDeleteOptions ?? {};
461
+ internalDeleteOptions.enabled = true;
462
+ await setOptionsAsync(internalDeleteOptions);
463
+ await updatedPathAsync(link);
464
+ return query.data;
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 };
434
480
  };
435
481
 
436
482
  const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl, headers, }) => {
437
483
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
438
484
  const { getHeaders } = useQueryHeaders();
439
- const [requestPath, updatePath] = useState(path);
485
+ const [requestPath, setRequestPath] = useState(path);
486
+ const [queryConfig, setQueryConfig] = useState();
440
487
  const [options, setOptions] = useState(queryOptions);
488
+ const { options: queryConfigOptions, setConfig } = useQueryConfig();
441
489
  let queryClient = useQueryClient();
442
490
  // eslint-disable-next-line react-hooks/exhaustive-deps
443
491
  queryClient = useMemo(() => queryClient, []);
444
- const sendRequest = async (res, rej, pageParam) => {
492
+ const sendRequest = async (res, rej, queryKey, pageParam) => {
445
493
  if (load) {
446
494
  // get request headers
447
495
  const globalHeaders = getHeaders();
448
- const getResponse = await makeRequest({
496
+ const requestOptions = {
449
497
  path: pageParam ?? requestPath,
450
498
  headers: { ...globalHeaders, ...headers },
451
499
  baseURL: baseUrl ?? API_URL,
452
500
  timeout: TIMEOUT,
453
- });
454
- if (getResponse.status) {
455
- res(getResponse);
501
+ };
502
+ let shouldContinue = true;
503
+ if (queryConfigOptions.queryMiddleware) {
504
+ shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
505
+ }
506
+ if (shouldContinue) {
507
+ const getResponse = await makeRequest(requestOptions);
508
+ if (getResponse.status) {
509
+ res(getResponse);
510
+ }
511
+ else {
512
+ rej(getResponse);
513
+ }
456
514
  }
457
515
  else {
458
- rej(getResponse);
516
+ rej(null);
459
517
  }
460
518
  }
461
519
  else {
@@ -473,8 +531,8 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
473
531
  queryParams.set('page', String(lastPageItem));
474
532
  return pathname + '?' + queryParams.toString();
475
533
  };
476
- const query = useInfiniteQuery([requestPath, {}], ({ pageParam = requestPath }) => new Promise((res, rej) => sendRequest(res, rej, pageParam)), {
477
- enabled: load,
534
+ const query = useInfiniteQuery([requestPath, {}], ({ pageParam = requestPath, queryKey }) => new Promise((res, rej) => sendRequest(res, rej, queryKey, pageParam)), {
535
+ enabled: load || !queryConfigOptions.pauseFutureQueries,
478
536
  getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
479
537
  getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
480
538
  ...options,
@@ -485,37 +543,49 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
485
543
  });
486
544
  };
487
545
  const get = async (link, fetchOptions) => {
488
- await setOptionsAsync(fetchOptions);
489
- await updatedPathAsync(link);
490
- return query.data;
546
+ if (!queryConfigOptions.pauseFutureQueries) {
547
+ await setOptionsAsync(fetchOptions);
548
+ await updatedPathAsync(link);
549
+ return query.data;
550
+ }
551
+ else {
552
+ setQueryConfig({ link, fetchOptions });
553
+ return undefined;
554
+ }
491
555
  };
492
556
  const updatedPathAsync = async (link) => {
493
557
  startTransition(() => {
494
- updatePath(link);
558
+ setRequestPath(link);
495
559
  });
496
560
  };
497
561
  useEffect(() => {
498
562
  if (keyTracker) {
499
- // set expiration time for the tracker
500
- queryClient.setQueryDefaults([keyTracker], {
501
- cacheTime: Infinity,
502
- staleTime: Infinity,
503
- });
504
- queryClient.setQueryData([keyTracker], [requestPath, {}]);
563
+ setConfig({ [keyTracker]: [requestPath, {}] });
505
564
  }
506
- }, [keyTracker, requestPath, queryClient, queryOptions?.staleTime]);
565
+ // eslint-disable-next-line react-hooks/exhaustive-deps
566
+ }, [keyTracker, requestPath]);
567
+ useEffect(() => {
568
+ if (!queryConfigOptions.pauseFutureQueries && queryConfig) {
569
+ get(queryConfig.link, queryConfig.fetchOptions);
570
+ setQueryConfig(undefined);
571
+ }
572
+ // eslint-disable-next-line react-hooks/exhaustive-deps
573
+ }, [queryConfigOptions.pauseFutureQueries]);
507
574
  return {
508
575
  get,
509
576
  ...query,
577
+ isLoading: query.isLoading || queryConfigOptions.pauseFutureQueries,
510
578
  };
511
579
  };
512
580
 
513
581
  const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl, headers, }) => {
514
- const [requestPath, updatePath] = useState(path);
582
+ const [requestPath, setRequestPath] = useState(path);
515
583
  const [options, setOptions] = useState(queryOptions);
516
584
  const [page, setPage] = useState(1);
517
585
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
518
586
  const { getHeaders } = useQueryHeaders();
587
+ const { options: queryConfigOptions, setConfig } = useQueryConfig();
588
+ const [queryConfig, setQueryConfig] = useState();
519
589
  let queryClient = useQueryClient();
520
590
  // eslint-disable-next-line react-hooks/exhaustive-deps
521
591
  queryClient = useMemo(() => queryClient, []);
@@ -523,18 +593,29 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
523
593
  if (load) {
524
594
  // get request headers
525
595
  const globalHeaders = getHeaders();
526
- const [url] = (queryKey ?? []);
527
- const getResponse = await makeRequest({
528
- path: url ?? requestPath,
596
+ const [url] = queryKey;
597
+ const requestUrl = (url ?? requestPath);
598
+ const requestOptions = {
599
+ path: requestUrl,
529
600
  headers: { ...globalHeaders, ...headers },
530
601
  baseURL: baseUrl ?? API_URL,
531
602
  timeout: TIMEOUT,
532
- });
533
- if (getResponse.status) {
534
- res(getResponse);
603
+ };
604
+ let shouldContinue = true;
605
+ if (queryConfigOptions.queryMiddleware) {
606
+ shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
607
+ }
608
+ if (shouldContinue) {
609
+ const getResponse = await makeRequest(requestOptions);
610
+ if (getResponse.status) {
611
+ res(getResponse);
612
+ }
613
+ else {
614
+ rej(getResponse);
615
+ }
535
616
  }
536
617
  else {
537
- rej(getResponse);
618
+ rej(null);
538
619
  }
539
620
  }
540
621
  else {
@@ -547,24 +628,20 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
547
628
  });
548
629
  useEffect(() => {
549
630
  if (path) {
550
- updatePath(path);
631
+ setRequestPath(path);
551
632
  }
552
633
  }, [path]);
553
634
  useEffect(() => {
554
635
  if (keyTracker) {
555
- // set expiration time for the tracker
556
- queryClient.setQueryDefaults([keyTracker], {
557
- cacheTime: Infinity,
558
- staleTime: Infinity,
559
- });
560
- queryClient.setQueryData([keyTracker], [requestPath, {}]);
636
+ setConfig({ [keyTracker]: [requestPath, {}] });
561
637
  }
562
- }, [keyTracker, requestPath, queryClient, queryOptions?.staleTime]);
638
+ // eslint-disable-next-line react-hooks/exhaustive-deps
639
+ }, [keyTracker, requestPath]);
563
640
  const nextPage = () => {
564
641
  if (query.data?.data.pagination) {
565
642
  const pagination = query.data.data.pagination;
566
643
  if (pagination.next_page !== pagination.current_page && pagination.next_page > pagination.current_page) {
567
- updatePath(constructPaginationLink(requestPath, pagination.next_page));
644
+ setRequestPath(constructPaginationLink(requestPath, pagination.next_page));
568
645
  }
569
646
  }
570
647
  };
@@ -572,7 +649,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
572
649
  if (query.data?.data.pagination) {
573
650
  const pagination = query.data.data.pagination;
574
651
  if (pagination.previous_page !== pagination.current_page && pagination.previous_page < pagination.current_page) {
575
- updatePath(constructPaginationLink(requestPath, pagination.previous_page));
652
+ setRequestPath(constructPaginationLink(requestPath, pagination.previous_page));
576
653
  }
577
654
  }
578
655
  };
@@ -589,11 +666,11 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
589
666
  return link;
590
667
  };
591
668
  const gotoPage = (pageNumber) => {
592
- updatePath(constructPaginationLink(requestPath, pageNumber));
669
+ setRequestPath(constructPaginationLink(requestPath, pageNumber));
593
670
  };
594
671
  const updatedPathAsync = async (link) => {
595
672
  startTransition(() => {
596
- updatePath(link);
673
+ setRequestPath(link);
597
674
  });
598
675
  };
599
676
  const setOptionsAsync = async (fetchOptions) => {
@@ -602,13 +679,27 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
602
679
  });
603
680
  };
604
681
  const get = async (link, fetchOptions) => {
605
- await setOptionsAsync(fetchOptions);
606
- await updatedPathAsync(link);
607
- return query.data;
682
+ if (!queryConfigOptions.pauseFutureQueries) {
683
+ await setOptionsAsync(fetchOptions);
684
+ await updatedPathAsync(link);
685
+ return query.data;
686
+ }
687
+ else {
688
+ setQueryConfig({ link, fetchOptions });
689
+ return undefined;
690
+ }
608
691
  };
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]);
609
699
  return {
610
700
  ...query,
611
- updatePath,
701
+ isLoading: query.isLoading || queryConfigOptions.pauseFutureQueries,
702
+ setRequestPath,
612
703
  nextPage,
613
704
  prevPage,
614
705
  get,
@@ -631,12 +722,13 @@ const useUploadProgress = () => {
631
722
  const usePatchRequest = ({ path, baseUrl, headers }) => {
632
723
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
633
724
  const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
725
+ const [mutationConfig, setMutationConfig] = useState();
634
726
  const { getHeaders } = useQueryHeaders();
635
727
  const config = useQueryConfig();
636
728
  const sendRequest = async (res, rej, data) => {
637
729
  // get request headers
638
730
  const globalHeaders = getHeaders();
639
- const patchResponse = await makeRequest({
731
+ const requestOptions = {
640
732
  path: path,
641
733
  body: data,
642
734
  method: HttpMethod.PATCH,
@@ -644,35 +736,67 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
644
736
  baseURL: baseUrl ?? API_URL,
645
737
  timeout: TIMEOUT,
646
738
  onUploadProgress,
647
- });
648
- if (patchResponse.status) {
649
- // scroll to top after success
650
- if (config.options?.context !== 'app') {
651
- scrollToTop();
739
+ };
740
+ let shouldContinue = true;
741
+ if (config.options.mutationMiddleware) {
742
+ shouldContinue = await config.options.mutationMiddleware({
743
+ mutationKey: [path, { type: 'mutation' }],
744
+ ...requestOptions,
745
+ });
746
+ }
747
+ if (shouldContinue) {
748
+ const patchResponse = await makeRequest(requestOptions);
749
+ if (patchResponse.status) {
750
+ // scroll to top after success
751
+ if (config.options.context !== 'app') {
752
+ scrollToTop();
753
+ }
754
+ res(patchResponse);
755
+ }
756
+ else {
757
+ // scroll to top after error
758
+ if (config.options.context !== 'app') {
759
+ scrollToTop();
760
+ }
761
+ rej(patchResponse);
652
762
  }
653
- res(patchResponse);
654
763
  }
655
764
  else {
656
- // scroll to top after error
657
- if (config.options?.context !== 'app') {
658
- scrollToTop();
659
- }
660
- rej(patchResponse);
765
+ rej(null);
661
766
  }
662
767
  };
663
768
  // register post mutation
664
769
  const mutation = useMutation((dataData) => new Promise((res, rej) => {
665
770
  return sendRequest(res, rej, dataData);
666
- }));
771
+ }), { mutationKey: [path, { type: 'mutation' }] });
667
772
  const patch = async (data, options) => {
668
- return mutation.mutateAsync(data, options);
773
+ if (!config.options.pauseFutureMutations) {
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,
669
793
  };
670
- return { patch, uploadProgressPercent, ...mutation };
671
794
  };
672
795
 
673
796
  const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelectors, }) => {
674
797
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
675
798
  const config = useQueryConfig();
799
+ const [mutationConfig, setMutationConfig] = useState();
676
800
  const { getHeaders } = useQueryHeaders();
677
801
  const { isApp } = useReactNativeEnv();
678
802
  const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
@@ -681,7 +805,7 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
681
805
  const globalHeaders = getHeaders();
682
806
  const { data, requestConfig } = postData;
683
807
  delete requestConfig?.body;
684
- const postResponse = await makeRequest({
808
+ const requestOptions = {
685
809
  path,
686
810
  body: data,
687
811
  method: HttpMethod.POST,
@@ -695,20 +819,33 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
695
819
  },
696
820
  onUploadProgress,
697
821
  ...requestConfig,
698
- });
699
- if (postResponse.status) {
700
- // scroll to top after success
701
- if (config.options?.context !== 'app') {
702
- scrollToTop();
822
+ };
823
+ let shouldContinue = true;
824
+ if (config.options.mutationMiddleware) {
825
+ shouldContinue = await config.options.mutationMiddleware({
826
+ mutationKey: [path, { type: 'mutation' }],
827
+ ...requestOptions,
828
+ });
829
+ }
830
+ if (shouldContinue) {
831
+ const postResponse = await makeRequest(requestOptions);
832
+ if (postResponse.status) {
833
+ // scroll to top after success
834
+ if (config.options.context !== 'app') {
835
+ scrollToTop();
836
+ }
837
+ res(postResponse);
838
+ }
839
+ else {
840
+ // scroll to top after error
841
+ if (config.options.context !== 'app') {
842
+ scrollToTop();
843
+ }
844
+ rej(postResponse);
703
845
  }
704
- res(postResponse);
705
846
  }
706
847
  else {
707
- // scroll to top after error
708
- if (config.options?.context !== 'app') {
709
- scrollToTop();
710
- }
711
- rej(postResponse);
848
+ rej(null);
712
849
  }
713
850
  };
714
851
  // register post mutation
@@ -716,9 +853,22 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
716
853
  mutationKey: [path, { type: 'mutation' }],
717
854
  });
718
855
  const post = async (data, options) => {
719
- const { requestConfig, ...otherOptions } = options ?? {};
720
- return mutation.mutateAsync({ data, requestConfig }, otherOptions);
856
+ if (!config.options.pauseFutureMutations) {
857
+ const { requestConfig, ...otherOptions } = options ?? {};
858
+ return mutation.mutateAsync({ data, requestConfig }, otherOptions);
859
+ }
860
+ else {
861
+ setMutationConfig({ data, options });
862
+ return undefined;
863
+ }
721
864
  };
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]);
722
872
  return { post, uploadProgressPercent, ...mutation };
723
873
  };
724
874
 
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,4 +1,4 @@
1
1
  export declare const useKeyTrackerModel: <T>(keyTracker: string) => {
2
2
  refetchQuery: (innerKeyTracker?: string) => Promise<void>;
3
- getQueryKey: (innerKeyTracker?: string) => any[] | undefined;
3
+ getQueryKey: (innerKeyTracker?: string) => any[];
4
4
  };
@@ -3,7 +3,11 @@ import { useQueryClient } from '@tanstack/react-query';
3
3
  const useKeyTrackerModel = (keyTracker) => {
4
4
  const queryClient = useQueryClient();
5
5
  const getQueryKey = (innerKeyTracker) => {
6
- const queryKey = queryClient.getQueryData([innerKeyTracker ?? keyTracker]);
6
+ const meta = {
7
+ ...queryClient.getDefaultOptions().mutations?.meta,
8
+ ...queryClient.getDefaultOptions().queries?.meta,
9
+ };
10
+ const queryKey = meta[innerKeyTracker ?? keyTracker];
7
11
  return queryKey;
8
12
  };
9
13
  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;
5
6
  data: IRequestSuccess<TResponse>;
6
7
  error: any;
7
8
  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;
34
35
  data: IRequestSuccess<TResponse>;
35
36
  error: null;
36
37
  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;
63
64
  data: undefined;
64
65
  error: any;
65
66
  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;
92
93
  data: undefined;
93
94
  error: null;
94
95
  isError: false;
95
- isLoading: true;
96
96
  isLoadingError: false;
97
97
  isRefetchError: false;
98
98
  isSuccess: false;