@ventlio/tanstack-query 0.2.85 → 0.2.87

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 (36) hide show
  1. package/dist/config/bootstrapQueryRequest.js +7 -5
  2. package/dist/config/bootstrapQueryRequest.js.map +1 -1
  3. package/dist/config/useQueryConfig.js +3 -2
  4. package/dist/config/useQueryConfig.js.map +1 -1
  5. package/dist/config/useQueryHeaders.js +12 -14
  6. package/dist/config/useQueryHeaders.js.map +1 -1
  7. package/dist/config/useReactNativeEnv.js +5 -6
  8. package/dist/config/useReactNativeEnv.js.map +1 -1
  9. package/dist/index.mjs +161 -100
  10. package/dist/index.mjs.map +1 -1
  11. package/dist/model/useQueryModel.js +4 -1
  12. package/dist/model/useQueryModel.js.map +1 -1
  13. package/dist/queries/useDeleteRequest.js +23 -11
  14. package/dist/queries/useDeleteRequest.js.map +1 -1
  15. package/dist/queries/useGetInfiniteRequest.js +21 -9
  16. package/dist/queries/useGetInfiniteRequest.js.map +1 -1
  17. package/dist/queries/useGetRequest.d.ts +4 -4
  18. package/dist/queries/useGetRequest.js +27 -14
  19. package/dist/queries/useGetRequest.js.map +1 -1
  20. package/dist/queries/usePatchRequest.js +29 -16
  21. package/dist/queries/usePatchRequest.js.map +1 -1
  22. package/dist/queries/usePostRequest.js +31 -16
  23. package/dist/queries/usePostRequest.js.map +1 -1
  24. package/dist/types/index.d.ts +8 -0
  25. package/package.json +1 -1
  26. package/src/config/bootstrapQueryRequest.ts +9 -6
  27. package/src/config/useQueryConfig.ts +3 -2
  28. package/src/config/useQueryHeaders.ts +12 -18
  29. package/src/config/useReactNativeEnv.ts +5 -7
  30. package/src/model/useQueryModel.ts +3 -2
  31. package/src/queries/useDeleteRequest.ts +27 -13
  32. package/src/queries/useGetInfiniteRequest.ts +25 -11
  33. package/src/queries/useGetRequest.ts +30 -16
  34. package/src/queries/usePatchRequest.ts +31 -18
  35. package/src/queries/usePostRequest.ts +36 -18
  36. package/src/types/index.ts +4 -0
@@ -2,16 +2,18 @@ import 'url-search-params-polyfill';
2
2
 
3
3
  const bootstrapQueryRequest = (queryClient, options) => {
4
4
  // make query config doesn't expire
5
- queryClient.setQueryDefaults(['config'], {
6
- staleTime: Infinity,
7
- cacheTime: Infinity,
8
- });
9
5
  // set default query config
10
- queryClient.setQueryData(['config'], {
6
+ const defaultMeta = {
11
7
  headers: {
12
8
  Authorization: ``,
13
9
  },
14
10
  options,
11
+ };
12
+ queryClient.setDefaultOptions({
13
+ queries: {
14
+ meta: defaultMeta,
15
+ },
16
+ mutations: { meta: defaultMeta },
15
17
  });
16
18
  };
17
19
 
@@ -1 +1 @@
1
- {"version":3,"file":"bootstrapQueryRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"bootstrapQueryRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;"}
@@ -2,8 +2,9 @@ import { useQueryClient } from '@tanstack/react-query';
2
2
 
3
3
  const useQueryConfig = () => {
4
4
  const queryClient = useQueryClient();
5
- const { headers = {}, options = {} } = queryClient.getQueryData(['config']) ?? {};
6
- return { headers, options };
5
+ const mutationMeta = (queryClient.getDefaultOptions().mutations?.meta ?? {});
6
+ const queryMeta = (queryClient.getDefaultOptions().queries?.meta ?? {});
7
+ return { ...queryMeta, ...mutationMeta };
7
8
  };
8
9
 
9
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,24 +1,22 @@
1
1
  import { useQueryClient } from '@tanstack/react-query';
2
- import { getDateInFuture } from '../helpers/timeFuncs.js';
2
+ import { useQueryConfig } from './useQueryConfig.js';
3
3
 
4
4
  const useQueryHeaders = () => {
5
5
  const queryClient = useQueryClient();
6
+ const { headers, options } = useQueryConfig();
6
7
  const getHeaders = () => {
7
- const config = queryClient.getQueryData(['config']);
8
- return config.headers;
8
+ return headers;
9
9
  };
10
10
  const setQueryHeaders = (newHeaders) => {
11
- // make sure the config does not expire
12
- queryClient.setQueryDefaults(['config'], {
13
- staleTime: Infinity,
14
- cacheTime: Infinity,
15
- });
16
- // set the config
17
- queryClient.setQueryData(['config'], (config) => {
18
- const newConfig = { ...config, headers: newHeaders };
19
- return newConfig;
20
- }, {
21
- updatedAt: getDateInFuture(2),
11
+ const defaultMeta = {
12
+ headers: { ...headers, ...newHeaders },
13
+ options,
14
+ };
15
+ queryClient.setDefaultOptions({
16
+ queries: {
17
+ meta: defaultMeta,
18
+ },
19
+ mutations: { meta: defaultMeta },
22
20
  });
23
21
  };
24
22
  return { setQueryHeaders, getHeaders };
@@ -1 +1 @@
1
- {"version":3,"file":"useQueryHeaders.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"useQueryHeaders.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,11 +1,10 @@
1
- import { useQueryClient } from '@tanstack/react-query';
1
+ import { useQueryConfig } from './useQueryConfig.js';
2
2
 
3
3
  const useReactNativeEnv = () => {
4
- const queryClient = useQueryClient();
5
- const config = queryClient.getQueryData(['config']);
6
- const appUrl = config?.options?.environments?.appBaseUrl;
7
- const appTimeout = config?.options?.environments?.appTimeout;
8
- const isApp = config?.options?.context === 'app';
4
+ const config = useQueryConfig();
5
+ const appUrl = config.options?.environments?.appBaseUrl;
6
+ const appTimeout = config.options?.environments?.appTimeout;
7
+ const isApp = config.options?.context === 'app';
9
8
  return { appUrl, appTimeout, isApp };
10
9
  };
11
10
 
@@ -1 +1 @@
1
- {"version":3,"file":"useReactNativeEnv.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;"}
1
+ {"version":3,"file":"useReactNativeEnv.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;"}
package/dist/index.mjs CHANGED
@@ -7,25 +7,33 @@ import axios from 'axios';
7
7
 
8
8
  const bootstrapQueryRequest = (queryClient, options) => {
9
9
  // make query config doesn't expire
10
- queryClient.setQueryDefaults(['config'], {
11
- staleTime: Infinity,
12
- cacheTime: Infinity,
13
- });
14
10
  // set default query config
15
- queryClient.setQueryData(['config'], {
11
+ const defaultMeta = {
16
12
  headers: {
17
13
  Authorization: ``,
18
14
  },
19
15
  options,
16
+ };
17
+ queryClient.setDefaultOptions({
18
+ queries: {
19
+ meta: defaultMeta,
20
+ },
21
+ mutations: { meta: defaultMeta },
20
22
  });
21
23
  };
22
24
 
23
- const useReactNativeEnv = () => {
25
+ const useQueryConfig = () => {
24
26
  const queryClient = useQueryClient();
25
- const config = queryClient.getQueryData(['config']);
26
- const appUrl = config?.options?.environments?.appBaseUrl;
27
- const appTimeout = config?.options?.environments?.appTimeout;
28
- const isApp = config?.options?.context === 'app';
27
+ const mutationMeta = (queryClient.getDefaultOptions().mutations?.meta ?? {});
28
+ const queryMeta = (queryClient.getDefaultOptions().queries?.meta ?? {});
29
+ return { ...queryMeta, ...mutationMeta };
30
+ };
31
+
32
+ const useReactNativeEnv = () => {
33
+ const config = useQueryConfig();
34
+ const appUrl = config.options?.environments?.appBaseUrl;
35
+ const appTimeout = config.options?.environments?.appTimeout;
36
+ const isApp = config.options?.context === 'app';
29
37
  return { appUrl, appTimeout, isApp };
30
38
  };
31
39
 
@@ -39,10 +47,25 @@ const useEnvironmentVariables = () => {
39
47
  };
40
48
  };
41
49
 
42
- const useQueryConfig = () => {
50
+ const useQueryHeaders = () => {
43
51
  const queryClient = useQueryClient();
44
- const { headers = {}, options = {} } = queryClient.getQueryData(['config']) ?? {};
45
- return { headers, options };
52
+ const { headers, options } = useQueryConfig();
53
+ const getHeaders = () => {
54
+ return headers;
55
+ };
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
+ });
67
+ };
68
+ return { setQueryHeaders, getHeaders };
46
69
  };
47
70
 
48
71
  const scrollToTop = () => {
@@ -61,29 +84,6 @@ function getDateInFuture(days) {
61
84
  return date.getTime();
62
85
  }
63
86
 
64
- const useQueryHeaders = () => {
65
- const queryClient = useQueryClient();
66
- const getHeaders = () => {
67
- const config = queryClient.getQueryData(['config']);
68
- return config.headers;
69
- };
70
- const setQueryHeaders = (newHeaders) => {
71
- // make sure the config does not expire
72
- queryClient.setQueryDefaults(['config'], {
73
- staleTime: Infinity,
74
- cacheTime: Infinity,
75
- });
76
- // set the config
77
- queryClient.setQueryData(['config'], (config) => {
78
- const newConfig = { ...config, headers: newHeaders };
79
- return newConfig;
80
- }, {
81
- updatedAt: getDateInFuture(2),
82
- });
83
- };
84
- return { setQueryHeaders, getHeaders };
85
- };
86
-
87
87
  const useKeyTrackerModel = (keyTracker) => {
88
88
  const queryClient = useQueryClient();
89
89
  const getQueryKey = (innerKeyTracker) => {
@@ -104,6 +104,7 @@ const useQueryModel = (keyTracker, exact = true) => {
104
104
  const queryClient = useQueryClient();
105
105
  const { getQueryKey } = useKeyTrackerModel(keyTracker);
106
106
  const queryKey = getQueryKey();
107
+ const config = useQueryConfig();
107
108
  const add = (data, position, path) => {
108
109
  let records = (findAll(path) ?? []);
109
110
  if (!position || position === 'end') {
@@ -159,7 +160,7 @@ const useQueryModel = (keyTracker, exact = true) => {
159
160
  return queryClient.setQueryData(queryKey, newData);
160
161
  };
161
162
  const getModelConfig = () => {
162
- const { options } = queryClient.getQueryData(['config']) ?? {};
163
+ const { options } = config;
163
164
  const { modelConfig } = options ?? {};
164
165
  return modelConfig;
165
166
  };
@@ -392,31 +393,42 @@ function getAppFiles(body, fileSelectors = []) {
392
393
 
393
394
  const useDeleteRequest = (deleteOptions) => {
394
395
  const { baseUrl, headers } = deleteOptions ?? {};
395
- const [requestPath, updateDeletePath] = useState('');
396
+ const [requestPath, setRequestPath] = useState('');
396
397
  const [options, setOptions] = useState();
398
+ const { options: queryConfigOptions } = useQueryConfig();
397
399
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
398
400
  const { getHeaders } = useQueryHeaders();
399
401
  const sendRequest = async (res, rej, queryKey) => {
400
402
  // get request headers
401
403
  const globalHeaders = getHeaders();
402
- const [url] = (queryKey ?? []);
403
- const postResponse = await makeRequest({
404
- path: url ?? requestPath,
404
+ const [url] = queryKey;
405
+ const requestUrl = (url ?? requestPath);
406
+ const requestOptions = {
407
+ path: requestUrl,
405
408
  headers: { ...globalHeaders, ...headers },
406
- method: HttpMethod.DELETE,
407
409
  baseURL: baseUrl ?? API_URL,
408
410
  timeout: TIMEOUT,
409
- });
410
- if (postResponse.status) {
411
- res(postResponse);
411
+ };
412
+ let shouldContinue = true;
413
+ if (queryConfigOptions?.queryMiddleware) {
414
+ shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
415
+ }
416
+ if (shouldContinue) {
417
+ const postResponse = await makeRequest(requestOptions);
418
+ if (postResponse.status) {
419
+ res(postResponse);
420
+ }
421
+ else {
422
+ rej(postResponse);
423
+ }
412
424
  }
413
425
  else {
414
- rej(postResponse);
426
+ rej(null);
415
427
  }
416
428
  };
417
429
  const query = useQuery([requestPath, {}], ({ queryKey }) => new Promise((res, rej) => sendRequest(res, rej, queryKey)), { enabled: false, ...options });
418
430
  const updatedPathAsync = async (link) => {
419
- return updateDeletePath(link);
431
+ return setRequestPath(link);
420
432
  };
421
433
  const setOptionsAsync = async (fetchOptions) => {
422
434
  return setOptions(fetchOptions);
@@ -435,26 +447,37 @@ const useDeleteRequest = (deleteOptions) => {
435
447
  const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl, headers, }) => {
436
448
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
437
449
  const { getHeaders } = useQueryHeaders();
438
- const [requestPath, updatePath] = useState(path);
450
+ const [requestPath, setRequestPath] = useState(path);
439
451
  const [options, setOptions] = useState(queryOptions);
452
+ const { options: queryConfigOptions } = useQueryConfig();
440
453
  let queryClient = useQueryClient();
441
454
  // eslint-disable-next-line react-hooks/exhaustive-deps
442
455
  queryClient = useMemo(() => queryClient, []);
443
- const sendRequest = async (res, rej, pageParam) => {
456
+ const sendRequest = async (res, rej, queryKey, pageParam) => {
444
457
  if (load) {
445
458
  // get request headers
446
459
  const globalHeaders = getHeaders();
447
- const getResponse = await makeRequest({
460
+ const requestOptions = {
448
461
  path: pageParam ?? requestPath,
449
462
  headers: { ...globalHeaders, ...headers },
450
463
  baseURL: baseUrl ?? API_URL,
451
464
  timeout: TIMEOUT,
452
- });
453
- if (getResponse.status) {
454
- res(getResponse);
465
+ };
466
+ let shouldContinue = true;
467
+ if (queryConfigOptions?.queryMiddleware) {
468
+ shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
469
+ }
470
+ if (shouldContinue) {
471
+ const getResponse = await makeRequest(requestOptions);
472
+ if (getResponse.status) {
473
+ res(getResponse);
474
+ }
475
+ else {
476
+ rej(getResponse);
477
+ }
455
478
  }
456
479
  else {
457
- rej(getResponse);
480
+ rej(null);
458
481
  }
459
482
  }
460
483
  else {
@@ -472,7 +495,7 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
472
495
  queryParams.set('page', String(lastPageItem));
473
496
  return pathname + '?' + queryParams.toString();
474
497
  };
475
- const query = useInfiniteQuery([requestPath, {}], ({ pageParam = requestPath }) => new Promise((res, rej) => sendRequest(res, rej, pageParam)), {
498
+ const query = useInfiniteQuery([requestPath, {}], ({ pageParam = requestPath, queryKey }) => new Promise((res, rej) => sendRequest(res, rej, queryKey, pageParam)), {
476
499
  enabled: load,
477
500
  getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
478
501
  getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
@@ -490,7 +513,7 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
490
513
  };
491
514
  const updatedPathAsync = async (link) => {
492
515
  startTransition(() => {
493
- updatePath(link);
516
+ setRequestPath(link);
494
517
  });
495
518
  };
496
519
  useEffect(() => {
@@ -510,11 +533,12 @@ const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, b
510
533
  };
511
534
 
512
535
  const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl, headers, }) => {
513
- const [requestPath, updatePath] = useState(path);
536
+ const [requestPath, setRequestPath] = useState(path);
514
537
  const [options, setOptions] = useState(queryOptions);
515
538
  const [page, setPage] = useState(1);
516
539
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
517
540
  const { getHeaders } = useQueryHeaders();
541
+ const { options: queryConfigOptions } = useQueryConfig();
518
542
  let queryClient = useQueryClient();
519
543
  // eslint-disable-next-line react-hooks/exhaustive-deps
520
544
  queryClient = useMemo(() => queryClient, []);
@@ -522,18 +546,29 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
522
546
  if (load) {
523
547
  // get request headers
524
548
  const globalHeaders = getHeaders();
525
- const [url] = (queryKey ?? []);
526
- const getResponse = await makeRequest({
527
- path: url ?? requestPath,
549
+ const [url] = queryKey;
550
+ const requestUrl = (url ?? requestPath);
551
+ const requestOptions = {
552
+ path: requestUrl,
528
553
  headers: { ...globalHeaders, ...headers },
529
554
  baseURL: baseUrl ?? API_URL,
530
555
  timeout: TIMEOUT,
531
- });
532
- if (getResponse.status) {
533
- res(getResponse);
556
+ };
557
+ let shouldContinue = true;
558
+ if (queryConfigOptions?.queryMiddleware) {
559
+ shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
560
+ }
561
+ if (shouldContinue) {
562
+ const getResponse = await makeRequest(requestOptions);
563
+ if (getResponse.status) {
564
+ res(getResponse);
565
+ }
566
+ else {
567
+ rej(getResponse);
568
+ }
534
569
  }
535
570
  else {
536
- rej(getResponse);
571
+ rej(null);
537
572
  }
538
573
  }
539
574
  else {
@@ -546,7 +581,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
546
581
  });
547
582
  useEffect(() => {
548
583
  if (path) {
549
- updatePath(path);
584
+ setRequestPath(path);
550
585
  }
551
586
  }, [path]);
552
587
  useEffect(() => {
@@ -563,7 +598,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
563
598
  if (query.data?.data.pagination) {
564
599
  const pagination = query.data.data.pagination;
565
600
  if (pagination.next_page !== pagination.current_page && pagination.next_page > pagination.current_page) {
566
- updatePath(constructPaginationLink(requestPath, pagination.next_page));
601
+ setRequestPath(constructPaginationLink(requestPath, pagination.next_page));
567
602
  }
568
603
  }
569
604
  };
@@ -571,7 +606,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
571
606
  if (query.data?.data.pagination) {
572
607
  const pagination = query.data.data.pagination;
573
608
  if (pagination.previous_page !== pagination.current_page && pagination.previous_page < pagination.current_page) {
574
- updatePath(constructPaginationLink(requestPath, pagination.previous_page));
609
+ setRequestPath(constructPaginationLink(requestPath, pagination.previous_page));
575
610
  }
576
611
  }
577
612
  };
@@ -588,11 +623,11 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
588
623
  return link;
589
624
  };
590
625
  const gotoPage = (pageNumber) => {
591
- updatePath(constructPaginationLink(requestPath, pageNumber));
626
+ setRequestPath(constructPaginationLink(requestPath, pageNumber));
592
627
  };
593
628
  const updatedPathAsync = async (link) => {
594
629
  startTransition(() => {
595
- updatePath(link);
630
+ setRequestPath(link);
596
631
  });
597
632
  };
598
633
  const setOptionsAsync = async (fetchOptions) => {
@@ -607,7 +642,7 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
607
642
  };
608
643
  return {
609
644
  ...query,
610
- updatePath,
645
+ setRequestPath,
611
646
  nextPage,
612
647
  prevPage,
613
648
  get,
@@ -631,12 +666,11 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
631
666
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
632
667
  const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
633
668
  const { getHeaders } = useQueryHeaders();
634
- const queryClient = useQueryClient();
635
- const config = queryClient.getQueryData(['config']);
669
+ const config = useQueryConfig();
636
670
  const sendRequest = async (res, rej, data) => {
637
671
  // get request headers
638
672
  const globalHeaders = getHeaders();
639
- const patchResponse = await makeRequest({
673
+ const requestOptions = {
640
674
  path: path,
641
675
  body: data,
642
676
  method: HttpMethod.PATCH,
@@ -644,26 +678,39 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
644
678
  baseURL: baseUrl ?? API_URL,
645
679
  timeout: TIMEOUT,
646
680
  onUploadProgress,
647
- });
648
- if (patchResponse.status) {
649
- // scroll to top after success
650
- if (config?.options?.context !== 'app') {
651
- scrollToTop();
681
+ };
682
+ let shouldContinue = true;
683
+ if (config.options?.mutationMiddleware) {
684
+ shouldContinue = await config.options.mutationMiddleware({
685
+ mutationKey: [path, { type: 'mutation' }],
686
+ ...requestOptions,
687
+ });
688
+ }
689
+ if (shouldContinue) {
690
+ const patchResponse = await makeRequest(requestOptions);
691
+ if (patchResponse.status) {
692
+ // scroll to top after success
693
+ if (config.options?.context !== 'app') {
694
+ scrollToTop();
695
+ }
696
+ res(patchResponse);
697
+ }
698
+ else {
699
+ // scroll to top after error
700
+ if (config.options?.context !== 'app') {
701
+ scrollToTop();
702
+ }
703
+ rej(patchResponse);
652
704
  }
653
- res(patchResponse);
654
705
  }
655
706
  else {
656
- // scroll to top after error
657
- if (config?.options?.context !== 'app') {
658
- scrollToTop();
659
- }
660
- rej(patchResponse);
707
+ rej(null);
661
708
  }
662
709
  };
663
710
  // register post mutation
664
711
  const mutation = useMutation((dataData) => new Promise((res, rej) => {
665
712
  return sendRequest(res, rej, dataData);
666
- }));
713
+ }), { mutationKey: [path, { type: 'mutation' }] });
667
714
  const patch = async (data, options) => {
668
715
  return mutation.mutateAsync(data, options);
669
716
  };
@@ -672,17 +719,16 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
672
719
 
673
720
  const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelectors, }) => {
674
721
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
675
- const queryClient = useQueryClient();
722
+ const config = useQueryConfig();
676
723
  const { getHeaders } = useQueryHeaders();
677
724
  const { isApp } = useReactNativeEnv();
678
725
  const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
679
726
  const sendRequest = async (res, rej, postData) => {
680
727
  // get request headers
681
728
  const globalHeaders = getHeaders();
682
- const config = queryClient.getQueryData(['config']);
683
729
  const { data, requestConfig } = postData;
684
730
  delete requestConfig?.body;
685
- const postResponse = await makeRequest({
731
+ const requestOptions = {
686
732
  path,
687
733
  body: data,
688
734
  method: HttpMethod.POST,
@@ -696,24 +742,39 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
696
742
  },
697
743
  onUploadProgress,
698
744
  ...requestConfig,
699
- });
700
- if (postResponse.status) {
701
- // scroll to top after success
702
- if (config?.options?.context !== 'app') {
703
- scrollToTop();
745
+ };
746
+ let shouldContinue = true;
747
+ if (config.options?.mutationMiddleware) {
748
+ shouldContinue = await config.options.mutationMiddleware({
749
+ mutationKey: [path, { type: 'mutation' }],
750
+ ...requestOptions,
751
+ });
752
+ }
753
+ if (shouldContinue) {
754
+ const postResponse = await makeRequest(requestOptions);
755
+ if (postResponse.status) {
756
+ // scroll to top after success
757
+ if (config.options?.context !== 'app') {
758
+ scrollToTop();
759
+ }
760
+ res(postResponse);
761
+ }
762
+ else {
763
+ // scroll to top after error
764
+ if (config.options?.context !== 'app') {
765
+ scrollToTop();
766
+ }
767
+ rej(postResponse);
704
768
  }
705
- res(postResponse);
706
769
  }
707
770
  else {
708
- // scroll to top after error
709
- if (config?.options?.context !== 'app') {
710
- scrollToTop();
711
- }
712
- rej(postResponse);
771
+ rej(null);
713
772
  }
714
773
  };
715
774
  // register post mutation
716
- const mutation = useMutation(async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)));
775
+ const mutation = useMutation(async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)), {
776
+ mutationKey: [path, { type: 'mutation' }],
777
+ });
717
778
  const post = async (data, options) => {
718
779
  const { requestConfig, ...otherOptions } = options ?? {};
719
780
  return mutation.mutateAsync({ data, requestConfig }, otherOptions);
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,12 +1,15 @@
1
1
  import { useQueryClient } from '@tanstack/react-query';
2
2
  import result from 'lodash.result';
3
3
  import lodashSet from 'lodash.set';
4
+ import 'url-search-params-polyfill';
5
+ import { useQueryConfig } from '../config/useQueryConfig.js';
4
6
  import { useKeyTrackerModel } from './useKeyTrackerModel.js';
5
7
 
6
8
  const useQueryModel = (keyTracker, exact = true) => {
7
9
  const queryClient = useQueryClient();
8
10
  const { getQueryKey } = useKeyTrackerModel(keyTracker);
9
11
  const queryKey = getQueryKey();
12
+ const config = useQueryConfig();
10
13
  const add = (data, position, path) => {
11
14
  let records = (findAll(path) ?? []);
12
15
  if (!position || position === 'end') {
@@ -62,7 +65,7 @@ const useQueryModel = (keyTracker, exact = true) => {
62
65
  return queryClient.setQueryData(queryKey, newData);
63
66
  };
64
67
  const getModelConfig = () => {
65
- const { options } = queryClient.getQueryData(['config']) ?? {};
68
+ const { options } = config;
66
69
  const { modelConfig } = options ?? {};
67
70
  return modelConfig;
68
71
  };
@@ -1 +1 @@
1
- {"version":3,"file":"useQueryModel.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"useQueryModel.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}