@ventlio/tanstack-query 0.2.85 → 0.2.86

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.
@@ -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
  };
@@ -631,8 +632,7 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
631
632
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
632
633
  const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
633
634
  const { getHeaders } = useQueryHeaders();
634
- const queryClient = useQueryClient();
635
- const config = queryClient.getQueryData(['config']);
635
+ const config = useQueryConfig();
636
636
  const sendRequest = async (res, rej, data) => {
637
637
  // get request headers
638
638
  const globalHeaders = getHeaders();
@@ -647,14 +647,14 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
647
647
  });
648
648
  if (patchResponse.status) {
649
649
  // scroll to top after success
650
- if (config?.options?.context !== 'app') {
650
+ if (config.options?.context !== 'app') {
651
651
  scrollToTop();
652
652
  }
653
653
  res(patchResponse);
654
654
  }
655
655
  else {
656
656
  // scroll to top after error
657
- if (config?.options?.context !== 'app') {
657
+ if (config.options?.context !== 'app') {
658
658
  scrollToTop();
659
659
  }
660
660
  rej(patchResponse);
@@ -672,14 +672,13 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
672
672
 
673
673
  const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelectors, }) => {
674
674
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
675
- const queryClient = useQueryClient();
675
+ const config = useQueryConfig();
676
676
  const { getHeaders } = useQueryHeaders();
677
677
  const { isApp } = useReactNativeEnv();
678
678
  const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
679
679
  const sendRequest = async (res, rej, postData) => {
680
680
  // get request headers
681
681
  const globalHeaders = getHeaders();
682
- const config = queryClient.getQueryData(['config']);
683
682
  const { data, requestConfig } = postData;
684
683
  delete requestConfig?.body;
685
684
  const postResponse = await makeRequest({
@@ -699,21 +698,23 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
699
698
  });
700
699
  if (postResponse.status) {
701
700
  // scroll to top after success
702
- if (config?.options?.context !== 'app') {
701
+ if (config.options?.context !== 'app') {
703
702
  scrollToTop();
704
703
  }
705
704
  res(postResponse);
706
705
  }
707
706
  else {
708
707
  // scroll to top after error
709
- if (config?.options?.context !== 'app') {
708
+ if (config.options?.context !== 'app') {
710
709
  scrollToTop();
711
710
  }
712
711
  rej(postResponse);
713
712
  }
714
713
  };
715
714
  // register post mutation
716
- const mutation = useMutation(async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)));
715
+ const mutation = useMutation(async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)), {
716
+ mutationKey: [path, { type: 'mutation' }],
717
+ });
717
718
  const post = async (data, options) => {
718
719
  const { requestConfig, ...otherOptions } = options ?? {};
719
720
  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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,6 +1,7 @@
1
- import { useQueryClient, useMutation } from '@tanstack/react-query';
1
+ import { useMutation } from '@tanstack/react-query';
2
2
  import 'url-search-params-polyfill';
3
3
  import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
4
+ import { useQueryConfig } from '../config/useQueryConfig.js';
4
5
  import { useQueryHeaders } from '../config/useQueryHeaders.js';
5
6
  import { scrollToTop } from '../helpers/scrollToTop.js';
6
7
  import { useUploadProgress } from '../hooks/useUploadProgress.js';
@@ -12,8 +13,7 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
12
13
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
13
14
  const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
14
15
  const { getHeaders } = useQueryHeaders();
15
- const queryClient = useQueryClient();
16
- const config = queryClient.getQueryData(['config']);
16
+ const config = useQueryConfig();
17
17
  const sendRequest = async (res, rej, data) => {
18
18
  // get request headers
19
19
  const globalHeaders = getHeaders();
@@ -28,14 +28,14 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
28
28
  });
29
29
  if (patchResponse.status) {
30
30
  // scroll to top after success
31
- if (config?.options?.context !== 'app') {
31
+ if (config.options?.context !== 'app') {
32
32
  scrollToTop();
33
33
  }
34
34
  res(patchResponse);
35
35
  }
36
36
  else {
37
37
  // scroll to top after error
38
- if (config?.options?.context !== 'app') {
38
+ if (config.options?.context !== 'app') {
39
39
  scrollToTop();
40
40
  }
41
41
  rej(patchResponse);
@@ -1,6 +1,7 @@
1
- import { useQueryClient, useMutation } from '@tanstack/react-query';
1
+ import { useMutation } from '@tanstack/react-query';
2
2
  import 'url-search-params-polyfill';
3
3
  import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
4
+ import { useQueryConfig } from '../config/useQueryConfig.js';
4
5
  import { useQueryHeaders } from '../config/useQueryHeaders.js';
5
6
  import { useReactNativeEnv } from '../config/useReactNativeEnv.js';
6
7
  import { scrollToTop } from '../helpers/scrollToTop.js';
@@ -11,14 +12,13 @@ import { HttpMethod } from '../request/request.enum.js';
11
12
 
12
13
  const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelectors, }) => {
13
14
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
14
- const queryClient = useQueryClient();
15
+ const config = useQueryConfig();
15
16
  const { getHeaders } = useQueryHeaders();
16
17
  const { isApp } = useReactNativeEnv();
17
18
  const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
18
19
  const sendRequest = async (res, rej, postData) => {
19
20
  // get request headers
20
21
  const globalHeaders = getHeaders();
21
- const config = queryClient.getQueryData(['config']);
22
22
  const { data, requestConfig } = postData;
23
23
  delete requestConfig?.body;
24
24
  const postResponse = await makeRequest({
@@ -38,21 +38,23 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
38
38
  });
39
39
  if (postResponse.status) {
40
40
  // scroll to top after success
41
- if (config?.options?.context !== 'app') {
41
+ if (config.options?.context !== 'app') {
42
42
  scrollToTop();
43
43
  }
44
44
  res(postResponse);
45
45
  }
46
46
  else {
47
47
  // scroll to top after error
48
- if (config?.options?.context !== 'app') {
48
+ if (config.options?.context !== 'app') {
49
49
  scrollToTop();
50
50
  }
51
51
  rej(postResponse);
52
52
  }
53
53
  };
54
54
  // register post mutation
55
- const mutation = useMutation(async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)));
55
+ const mutation = useMutation(async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)), {
56
+ mutationKey: [path, { type: 'mutation' }],
57
+ });
56
58
  const post = async (data, options) => {
57
59
  const { requestConfig, ...otherOptions } = options ?? {};
58
60
  return mutation.mutateAsync({ data, requestConfig }, otherOptions);
@@ -1 +1 @@
1
- {"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"usePostRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -6,6 +6,8 @@ export interface BootstrapConfig {
6
6
  };
7
7
  context?: ContextType;
8
8
  modelConfig?: BootstrapModelConfig;
9
+ mutationMiddleware?: (mutateRequest?: any, mutateResponse?: any) => Promise<boolean>;
10
+ queryMiddleware?: (queryRequest?: any, queryResponse?: any) => Promise<boolean>;
9
11
  }
10
12
  export interface BootstrapModelConfig {
11
13
  idColumn: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ventlio/tanstack-query",
3
- "version": "0.2.85",
3
+ "version": "0.2.86",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "contributors": [
@@ -1,19 +1,22 @@
1
1
  import type { QueryClient } from '@tanstack/react-query';
2
2
  import 'url-search-params-polyfill';
3
- import type { BootstrapConfig, TanstackQueryConfig } from '../types';
3
+ import type { BootstrapConfig } from '../types';
4
4
 
5
5
  export const bootstrapQueryRequest = (queryClient: QueryClient, options?: BootstrapConfig): void => {
6
6
  // make query config doesn't expire
7
- queryClient.setQueryDefaults(['config'], {
8
- staleTime: Infinity,
9
- cacheTime: Infinity,
10
- });
11
7
 
12
8
  // set default query config
13
- queryClient.setQueryData<TanstackQueryConfig>(['config'], {
9
+ const defaultMeta = {
14
10
  headers: {
15
11
  Authorization: ``,
16
12
  },
17
13
  options,
14
+ };
15
+
16
+ queryClient.setDefaultOptions({
17
+ queries: {
18
+ meta: defaultMeta,
19
+ },
20
+ mutations: { meta: defaultMeta },
18
21
  });
19
22
  };
@@ -4,7 +4,8 @@ import type { TanstackQueryConfig } from '../types';
4
4
  export const useQueryConfig = (): TanstackQueryConfig => {
5
5
  const queryClient = useQueryClient();
6
6
 
7
- const { headers = {}, options = {} } = queryClient.getQueryData<TanstackQueryConfig>(['config']) ?? {};
7
+ const mutationMeta = (queryClient.getDefaultOptions().mutations?.meta ?? {}) as unknown as TanstackQueryConfig;
8
+ const queryMeta = (queryClient.getDefaultOptions().queries?.meta ?? {}) as unknown as TanstackQueryConfig;
8
9
 
9
- return { headers, options };
10
+ return { ...queryMeta, ...mutationMeta };
10
11
  };
@@ -1,33 +1,27 @@
1
1
  import { useQueryClient } from '@tanstack/react-query';
2
- import { getDateInFuture } from '../helpers';
3
2
  import type { IUseQueryHeaders, TanstackQueryConfig } from '../types';
3
+ import { useQueryConfig } from './useQueryConfig';
4
4
 
5
5
  export const useQueryHeaders = (): IUseQueryHeaders => {
6
6
  const queryClient = useQueryClient();
7
+ const { headers, options } = useQueryConfig();
7
8
 
8
9
  const getHeaders = (): TanstackQueryConfig['headers'] => {
9
- const config = queryClient.getQueryData(['config']) as TanstackQueryConfig;
10
- return config.headers;
10
+ return headers;
11
11
  };
12
12
 
13
13
  const setQueryHeaders = (newHeaders: TanstackQueryConfig['headers']) => {
14
- // make sure the config does not expire
15
- queryClient.setQueryDefaults(['config'], {
16
- staleTime: Infinity,
17
- cacheTime: Infinity,
18
- });
14
+ const defaultMeta = {
15
+ headers: { ...headers, ...newHeaders },
16
+ options,
17
+ };
19
18
 
20
- // set the config
21
- queryClient.setQueryData<TanstackQueryConfig>(
22
- ['config'],
23
- (config): TanstackQueryConfig => {
24
- const newConfig = { ...config, headers: newHeaders };
25
- return newConfig;
19
+ queryClient.setDefaultOptions({
20
+ queries: {
21
+ meta: defaultMeta,
26
22
  },
27
- {
28
- updatedAt: getDateInFuture(2),
29
- }
30
- );
23
+ mutations: { meta: defaultMeta },
24
+ });
31
25
  };
32
26
 
33
27
  return { setQueryHeaders, getHeaders };
@@ -1,13 +1,11 @@
1
- import { useQueryClient } from '@tanstack/react-query';
2
- import type { TanstackQueryConfig } from '../types';
1
+ import { useQueryConfig } from './useQueryConfig';
3
2
 
4
3
  export const useReactNativeEnv = () => {
5
- const queryClient = useQueryClient();
6
- const config = queryClient.getQueryData<TanstackQueryConfig>(['config']);
4
+ const config = useQueryConfig();
7
5
 
8
- const appUrl: string | undefined = config?.options?.environments?.appBaseUrl;
9
- const appTimeout: number | undefined = config?.options?.environments?.appTimeout;
10
- const isApp = config?.options?.context === 'app';
6
+ const appUrl: string | undefined = config.options?.environments?.appBaseUrl;
7
+ const appTimeout: number | undefined = config.options?.environments?.appTimeout;
8
+ const isApp = config.options?.context === 'app';
11
9
 
12
10
  return { appUrl, appTimeout, isApp };
13
11
  };
@@ -1,7 +1,7 @@
1
1
  import { useQueryClient } from '@tanstack/react-query';
2
2
  import result from 'lodash.result';
3
3
  import { default as lodashSet } from 'lodash.set';
4
- import type { TanstackQueryConfig } from '../types';
4
+ import { useQueryConfig } from '../config';
5
5
  import type { QueryModelAddPosition, QueryModelBuilder } from './model.interface';
6
6
  import { useKeyTrackerModel } from './useKeyTrackerModel';
7
7
 
@@ -9,6 +9,7 @@ export const useQueryModel = <T>(keyTracker: string, exact: boolean = true): Que
9
9
  const queryClient = useQueryClient();
10
10
  const { getQueryKey } = useKeyTrackerModel(keyTracker);
11
11
  const queryKey = getQueryKey() as any[];
12
+ const config = useQueryConfig();
12
13
 
13
14
  const add = (data: T, position?: QueryModelAddPosition, path?: string): T | undefined => {
14
15
  let records = (findAll(path) ?? []) as T[];
@@ -78,7 +79,7 @@ export const useQueryModel = <T>(keyTracker: string, exact: boolean = true): Que
78
79
  };
79
80
 
80
81
  const getModelConfig = () => {
81
- const { options } = queryClient.getQueryData<TanstackQueryConfig>(['config']) ?? {};
82
+ const { options } = config;
82
83
  const { modelConfig } = options ?? {};
83
84
 
84
85
  return modelConfig;
@@ -1,12 +1,11 @@
1
1
  import type { MutateOptions } from '@tanstack/react-query';
2
- import { useMutation, useQueryClient } from '@tanstack/react-query';
2
+ import { useMutation } from '@tanstack/react-query';
3
3
  import type { RawAxiosRequestHeaders } from 'axios';
4
- import { useEnvironmentVariables, useQueryHeaders } from '../config';
4
+ import { useEnvironmentVariables, useQueryConfig, useQueryHeaders } from '../config';
5
5
  import { scrollToTop } from '../helpers';
6
6
  import { useUploadProgress } from '../hooks';
7
7
  import { HttpMethod, makeRequest } from '../request';
8
8
  import type { IRequestError, IRequestSuccess } from '../request/request.interface';
9
- import type { TanstackQueryConfig } from '../types';
10
9
  import type { DefaultRequestOptions } from './queries.interface';
11
10
 
12
11
  export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: string } & DefaultRequestOptions) => {
@@ -14,9 +13,8 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
14
13
  const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
15
14
 
16
15
  const { getHeaders } = useQueryHeaders();
17
- const queryClient = useQueryClient();
18
16
 
19
- const config = queryClient.getQueryData<TanstackQueryConfig>(['config']);
17
+ const config = useQueryConfig();
20
18
 
21
19
  const sendRequest = async (res: (value: any) => void, rej: (reason?: any) => void, data: any) => {
22
20
  // get request headers
@@ -34,13 +32,13 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
34
32
 
35
33
  if (patchResponse.status) {
36
34
  // scroll to top after success
37
- if (config?.options?.context !== 'app') {
35
+ if (config.options?.context !== 'app') {
38
36
  scrollToTop();
39
37
  }
40
38
  res(patchResponse as IRequestSuccess<TResponse>);
41
39
  } else {
42
40
  // scroll to top after error
43
- if (config?.options?.context !== 'app') {
41
+ if (config.options?.context !== 'app') {
44
42
  scrollToTop();
45
43
  }
46
44
  rej(patchResponse);
@@ -1,13 +1,12 @@
1
1
  import type { MutateOptions } from '@tanstack/react-query';
2
- import { useMutation, useQueryClient } from '@tanstack/react-query';
3
- import { useEnvironmentVariables, useQueryHeaders, useReactNativeEnv } from '../config';
2
+ import { useMutation } from '@tanstack/react-query';
3
+ import { useEnvironmentVariables, useQueryConfig, useQueryHeaders, useReactNativeEnv } from '../config';
4
4
 
5
5
  import type { RawAxiosRequestHeaders } from 'axios';
6
6
  import { scrollToTop } from '../helpers';
7
7
  import { useUploadProgress } from '../hooks';
8
8
  import type { IMakeRequest, IRequestError, IRequestSuccess } from '../request';
9
9
  import { HttpMethod, makeRequest } from '../request';
10
- import type { TanstackQueryConfig } from '../types';
11
10
  import type { DefaultRequestOptions } from './queries.interface';
12
11
 
13
12
  export const usePostRequest = <TResponse>({
@@ -22,7 +21,9 @@ export const usePostRequest = <TResponse>({
22
21
  fileSelectors?: string[];
23
22
  } & DefaultRequestOptions) => {
24
23
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
25
- const queryClient = useQueryClient();
24
+
25
+ const config = useQueryConfig();
26
+
26
27
  const { getHeaders } = useQueryHeaders();
27
28
  const { isApp } = useReactNativeEnv();
28
29
  const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
@@ -34,7 +35,7 @@ export const usePostRequest = <TResponse>({
34
35
  ) => {
35
36
  // get request headers
36
37
  const globalHeaders: RawAxiosRequestHeaders = getHeaders();
37
- const config = queryClient.getQueryData<TanstackQueryConfig>(['config']);
38
+
38
39
  const { data, requestConfig } = postData;
39
40
 
40
41
  delete requestConfig?.body;
@@ -58,13 +59,13 @@ export const usePostRequest = <TResponse>({
58
59
  if (postResponse.status) {
59
60
  // scroll to top after success
60
61
 
61
- if (config?.options?.context !== 'app') {
62
+ if (config.options?.context !== 'app') {
62
63
  scrollToTop();
63
64
  }
64
65
  res(postResponse as IRequestSuccess<TResponse>);
65
66
  } else {
66
67
  // scroll to top after error
67
- if (config?.options?.context !== 'app') {
68
+ if (config.options?.context !== 'app') {
68
69
  scrollToTop();
69
70
  }
70
71
  rej(postResponse);
@@ -76,7 +77,9 @@ export const usePostRequest = <TResponse>({
76
77
  IRequestSuccess<TResponse>,
77
78
  IRequestError,
78
79
  { data: any; requestConfig?: Partial<Omit<IMakeRequest, 'body'>> }
79
- >(async (postData) => new Promise<IRequestSuccess<TResponse>>((res, rej) => sendRequest(res, rej, postData)));
80
+ >(async (postData) => new Promise<IRequestSuccess<TResponse>>((res, rej) => sendRequest(res, rej, postData)), {
81
+ mutationKey: [path, { type: 'mutation' }],
82
+ });
80
83
 
81
84
  const post = async <T>(
82
85
  data?: T,
@@ -7,6 +7,8 @@ export interface BootstrapConfig {
7
7
  };
8
8
  context?: ContextType;
9
9
  modelConfig?: BootstrapModelConfig;
10
+ mutationMiddleware?: (mutateRequest?: any, mutateResponse?: any) => Promise<boolean>;
11
+ queryMiddleware?: (queryRequest?: any, queryResponse?: any) => Promise<boolean>;
10
12
  }
11
13
 
12
14
  export interface BootstrapModelConfig {