@ventlio/tanstack-query 0.2.65 → 0.2.69

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,8 +2,8 @@ import { useQueryClient } from '@tanstack/react-query';
2
2
 
3
3
  const useQueryConfig = () => {
4
4
  const queryClient = useQueryClient();
5
- const { headers = {} } = queryClient.getQueryData(['config']) ?? {};
6
- return { headers };
5
+ const { headers = {}, options = {} } = queryClient.getQueryData(['config']) ?? {};
6
+ return { headers, options };
7
7
  };
8
8
 
9
9
  export { useQueryConfig };
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { useQueryClient, useQuery, useMutation } from '@tanstack/react-query';
2
2
  import result from 'lodash.result';
3
- import set from 'lodash.set';
3
+ import lodashSet from 'lodash.set';
4
4
  import { useState, useMemo, useEffect, startTransition } from 'react';
5
5
  import axios from 'axios';
6
6
 
@@ -40,8 +40,8 @@ const useEnvironmentVariables = () => {
40
40
 
41
41
  const useQueryConfig = () => {
42
42
  const queryClient = useQueryClient();
43
- const { headers = {} } = queryClient.getQueryData(['config']) ?? {};
44
- return { headers };
43
+ const { headers = {}, options = {} } = queryClient.getQueryData(['config']) ?? {};
44
+ return { headers, options };
45
45
  };
46
46
 
47
47
  const scrollToTop = () => {
@@ -117,7 +117,7 @@ const useQueryModel = (keyTracker, exact = true) => {
117
117
  }
118
118
  else {
119
119
  const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
120
- queryClient.setQueryData(queryKey, set(queryData, path, records));
120
+ queryClient.setQueryData(queryKey, lodashSet(queryData, path, records));
121
121
  }
122
122
  return data;
123
123
  };
@@ -143,6 +143,21 @@ const useQueryModel = (keyTracker, exact = true) => {
143
143
  const data = findAll(path) ?? [];
144
144
  return data.find((record) => record[modelConfig.idColumn] === id);
145
145
  };
146
+ const get = (path) => {
147
+ let data = queryClient.getQueryData(queryKey, { exact });
148
+ if (path) {
149
+ data = result(data, path);
150
+ }
151
+ return data;
152
+ };
153
+ const set = (newData, path) => {
154
+ if (path) {
155
+ const data = get();
156
+ newData = lodashSet(data, path, newData);
157
+ return queryClient.setQueryData(queryKey, newData);
158
+ }
159
+ return queryClient.setQueryData(queryKey, newData);
160
+ };
146
161
  const getModelConfig = () => {
147
162
  const { options } = queryClient.getQueryData(['config']) ?? {};
148
163
  const { modelConfig } = options ?? {};
@@ -169,7 +184,7 @@ const useQueryModel = (keyTracker, exact = true) => {
169
184
  }
170
185
  else {
171
186
  const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
172
- queryClient.setQueryData(queryKey, set(queryData, path, newData));
187
+ queryClient.setQueryData(queryKey, lodashSet(queryData, path, newData));
173
188
  }
174
189
  return updatedRecord;
175
190
  };
@@ -194,18 +209,18 @@ const useQueryModel = (keyTracker, exact = true) => {
194
209
  }
195
210
  else {
196
211
  const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
197
- queryClient.setQueryData(queryKey, set(queryData, path, newData));
212
+ queryClient.setQueryData(queryKey, lodashSet(queryData, path, newData));
198
213
  }
199
214
  return updated;
200
215
  };
201
- return { find, findAll, findMany, remove, update, add };
216
+ return { find, findAll, findMany, remove, update, add, get, set };
202
217
  };
203
218
 
204
219
  const useRefetchQuery = async (queryKey) => {
205
220
  const queryClient = useQueryClient();
206
221
  const refetchQuery = async (innerQueryKey) => {
207
- await queryClient.refetchQueries({
208
- queryKey,
222
+ await queryClient.invalidateQueries({
223
+ queryKey: innerQueryKey ?? queryKey,
209
224
  exact: true,
210
225
  }, { throwOnError: true, cancelRefetch: true });
211
226
  return queryClient.getQueriesData(innerQueryKey ?? queryKey);
@@ -409,8 +424,8 @@ const useDeleteRequest = (deleteOptions) => {
409
424
  // set enabled to be true for every delete
410
425
  internalDeleteOptions = internalDeleteOptions ?? {};
411
426
  internalDeleteOptions.enabled = true;
412
- await updatedPathAsync(link);
413
427
  await setOptionsAsync(internalDeleteOptions);
428
+ await updatedPathAsync(link);
414
429
  return query.data;
415
430
  };
416
431
  return { destroy, ...query };
@@ -426,19 +441,24 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
426
441
  // eslint-disable-next-line react-hooks/exhaustive-deps
427
442
  queryClient = useMemo(() => queryClient, []);
428
443
  const sendRequest = async (res, rej) => {
429
- // get request headers
430
- const globalHeaders = getHeaders();
431
- const getResponse = await makeRequest({
432
- path: requestPath,
433
- headers: { ...globalHeaders, ...headers },
434
- baseURL: baseUrl ?? API_URL,
435
- timeout: TIMEOUT,
436
- });
437
- if (getResponse.status) {
438
- res(getResponse);
444
+ if (load) {
445
+ // get request headers
446
+ const globalHeaders = getHeaders();
447
+ const getResponse = await makeRequest({
448
+ path: requestPath,
449
+ headers: { ...globalHeaders, ...headers },
450
+ baseURL: baseUrl ?? API_URL,
451
+ timeout: TIMEOUT,
452
+ });
453
+ if (getResponse.status) {
454
+ res(getResponse);
455
+ }
456
+ else {
457
+ rej(getResponse);
458
+ }
439
459
  }
440
460
  else {
441
- rej(getResponse);
461
+ res(null);
442
462
  }
443
463
  };
444
464
  const query = useQuery([requestPath, {}], () => new Promise((res, rej) => sendRequest(res, rej)), {
@@ -521,6 +541,8 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
521
541
  const usePatchRequest = ({ path, baseUrl, headers }) => {
522
542
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
523
543
  const { getHeaders } = useQueryHeaders();
544
+ const queryClient = useQueryClient();
545
+ const config = queryClient.getQueryData(['config']);
524
546
  const sendRequest = async (res, rej, data) => {
525
547
  // get request headers
526
548
  const globalHeaders = getHeaders();
@@ -534,12 +556,16 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
534
556
  });
535
557
  if (patchResponse.status) {
536
558
  // scroll to top after success
537
- scrollToTop();
559
+ if (config?.options?.context !== 'app') {
560
+ scrollToTop();
561
+ }
538
562
  res(patchResponse);
539
563
  }
540
564
  else {
541
565
  // scroll to top after error
542
- scrollToTop();
566
+ if (config?.options?.context !== 'app') {
567
+ scrollToTop();
568
+ }
543
569
  rej(patchResponse);
544
570
  }
545
571
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -5,5 +5,7 @@ export interface QueryModelBuilder<T> {
5
5
  find: (id: number | string, path?: string) => T | undefined;
6
6
  update: (id: number | string, data: Partial<T>, path?: string) => T | undefined;
7
7
  remove: (id: number, path?: string) => boolean;
8
+ get: (path?: string) => T | undefined;
9
+ set: (data: Partial<T>, path?: string) => T | undefined;
8
10
  }
9
11
  export type QueryModelAddPosition = 'start' | 'end';
@@ -1,6 +1,6 @@
1
1
  import { useQueryClient } from '@tanstack/react-query';
2
2
  import result from 'lodash.result';
3
- import set from 'lodash.set';
3
+ import lodashSet from 'lodash.set';
4
4
  import { useKeyTrackerModel } from './useKeyTrackerModel.js';
5
5
 
6
6
  const useQueryModel = (keyTracker, exact = true) => {
@@ -21,7 +21,7 @@ const useQueryModel = (keyTracker, exact = true) => {
21
21
  }
22
22
  else {
23
23
  const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
24
- queryClient.setQueryData(queryKey, set(queryData, path, records));
24
+ queryClient.setQueryData(queryKey, lodashSet(queryData, path, records));
25
25
  }
26
26
  return data;
27
27
  };
@@ -47,6 +47,21 @@ const useQueryModel = (keyTracker, exact = true) => {
47
47
  const data = findAll(path) ?? [];
48
48
  return data.find((record) => record[modelConfig.idColumn] === id);
49
49
  };
50
+ const get = (path) => {
51
+ let data = queryClient.getQueryData(queryKey, { exact });
52
+ if (path) {
53
+ data = result(data, path);
54
+ }
55
+ return data;
56
+ };
57
+ const set = (newData, path) => {
58
+ if (path) {
59
+ const data = get();
60
+ newData = lodashSet(data, path, newData);
61
+ return queryClient.setQueryData(queryKey, newData);
62
+ }
63
+ return queryClient.setQueryData(queryKey, newData);
64
+ };
50
65
  const getModelConfig = () => {
51
66
  const { options } = queryClient.getQueryData(['config']) ?? {};
52
67
  const { modelConfig } = options ?? {};
@@ -73,7 +88,7 @@ const useQueryModel = (keyTracker, exact = true) => {
73
88
  }
74
89
  else {
75
90
  const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
76
- queryClient.setQueryData(queryKey, set(queryData, path, newData));
91
+ queryClient.setQueryData(queryKey, lodashSet(queryData, path, newData));
77
92
  }
78
93
  return updatedRecord;
79
94
  };
@@ -98,11 +113,11 @@ const useQueryModel = (keyTracker, exact = true) => {
98
113
  }
99
114
  else {
100
115
  const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
101
- queryClient.setQueryData(queryKey, set(queryData, path, newData));
116
+ queryClient.setQueryData(queryKey, lodashSet(queryData, path, newData));
102
117
  }
103
118
  return updated;
104
119
  };
105
- return { find, findAll, findMany, remove, update, add };
120
+ return { find, findAll, findMany, remove, update, add, get, set };
106
121
  };
107
122
 
108
123
  export { useQueryModel };
@@ -1 +1 @@
1
- {"version":3,"file":"useQueryModel.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"useQueryModel.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -3,8 +3,8 @@ import { useQueryClient } from '@tanstack/react-query';
3
3
  const useRefetchQuery = async (queryKey) => {
4
4
  const queryClient = useQueryClient();
5
5
  const refetchQuery = async (innerQueryKey) => {
6
- await queryClient.refetchQueries({
7
- queryKey,
6
+ await queryClient.invalidateQueries({
7
+ queryKey: innerQueryKey ?? queryKey,
8
8
  exact: true,
9
9
  }, { throwOnError: true, cancelRefetch: true });
10
10
  return queryClient.getQueriesData(innerQueryKey ?? queryKey);
@@ -27,7 +27,9 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
27
27
  refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<IRequestSuccess<TResponse>, any>>;
28
28
  remove: () => void;
29
29
  fetchStatus: import("@tanstack/react-query").FetchStatus;
30
- destroy: (link: string, internalDeleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
30
+ destroy: (link: string, internalDeleteOptions?: (UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> & {
31
+ cached?: boolean | undefined;
32
+ }) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
31
33
  } | {
32
34
  data: IRequestSuccess<TResponse>;
33
35
  error: null;
@@ -54,7 +56,9 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
54
56
  refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<IRequestSuccess<TResponse>, any>>;
55
57
  remove: () => void;
56
58
  fetchStatus: import("@tanstack/react-query").FetchStatus;
57
- destroy: (link: string, internalDeleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
59
+ destroy: (link: string, internalDeleteOptions?: (UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> & {
60
+ cached?: boolean | undefined;
61
+ }) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
58
62
  } | {
59
63
  data: undefined;
60
64
  error: any;
@@ -81,7 +85,9 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
81
85
  refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<IRequestSuccess<TResponse>, any>>;
82
86
  remove: () => void;
83
87
  fetchStatus: import("@tanstack/react-query").FetchStatus;
84
- destroy: (link: string, internalDeleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
88
+ destroy: (link: string, internalDeleteOptions?: (UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> & {
89
+ cached?: boolean | undefined;
90
+ }) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
85
91
  } | {
86
92
  data: undefined;
87
93
  error: null;
@@ -108,5 +114,7 @@ export declare const useDeleteRequest: <TResponse>(deleteOptions?: DefaultReques
108
114
  refetch: <TPageData>(options?: (import("@tanstack/react-query").RefetchOptions & import("@tanstack/react-query").RefetchQueryFilters<TPageData>) | undefined) => Promise<import("@tanstack/react-query").QueryObserverResult<IRequestSuccess<TResponse>, any>>;
109
115
  remove: () => void;
110
116
  fetchStatus: import("@tanstack/react-query").FetchStatus;
111
- destroy: (link: string, internalDeleteOptions?: UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
117
+ destroy: (link: string, internalDeleteOptions?: (UseQueryOptions<IRequestSuccess<TResponse | undefined>, IRequestError, IRequestSuccess<TResponse | undefined>, any[]> & {
118
+ cached?: boolean | undefined;
119
+ }) | undefined) => Promise<IRequestSuccess<TResponse> | undefined>;
112
120
  };
@@ -40,8 +40,8 @@ const useDeleteRequest = (deleteOptions) => {
40
40
  // set enabled to be true for every delete
41
41
  internalDeleteOptions = internalDeleteOptions ?? {};
42
42
  internalDeleteOptions.enabled = true;
43
- await updatedPathAsync(link);
44
43
  await setOptionsAsync(internalDeleteOptions);
44
+ await updatedPathAsync(link);
45
45
  return query.data;
46
46
  };
47
47
  return { destroy, ...query };
@@ -16,19 +16,24 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
16
16
  // eslint-disable-next-line react-hooks/exhaustive-deps
17
17
  queryClient = useMemo(() => queryClient, []);
18
18
  const sendRequest = async (res, rej) => {
19
- // get request headers
20
- const globalHeaders = getHeaders();
21
- const getResponse = await makeRequest({
22
- path: requestPath,
23
- headers: { ...globalHeaders, ...headers },
24
- baseURL: baseUrl ?? API_URL,
25
- timeout: TIMEOUT,
26
- });
27
- if (getResponse.status) {
28
- res(getResponse);
19
+ if (load) {
20
+ // get request headers
21
+ const globalHeaders = getHeaders();
22
+ const getResponse = await makeRequest({
23
+ path: requestPath,
24
+ headers: { ...globalHeaders, ...headers },
25
+ baseURL: baseUrl ?? API_URL,
26
+ timeout: TIMEOUT,
27
+ });
28
+ if (getResponse.status) {
29
+ res(getResponse);
30
+ }
31
+ else {
32
+ rej(getResponse);
33
+ }
29
34
  }
30
35
  else {
31
- rej(getResponse);
36
+ res(null);
32
37
  }
33
38
  };
34
39
  const query = useQuery([requestPath, {}], () => new Promise((res, rej) => sendRequest(res, rej)), {
@@ -1 +1 @@
1
- {"version":3,"file":"useGetRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"useGetRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,4 +1,4 @@
1
- import { useMutation } from '@tanstack/react-query';
1
+ import { useQueryClient, useMutation } from '@tanstack/react-query';
2
2
  import { useEnvironmentVariables } from '../config/useEnvironmentVariables.js';
3
3
  import { useQueryHeaders } from '../config/useQueryHeaders.js';
4
4
  import { scrollToTop } from '../helpers/scrollToTop.js';
@@ -9,6 +9,8 @@ import { HttpMethod } from '../request/request.enum.js';
9
9
  const usePatchRequest = ({ path, baseUrl, headers }) => {
10
10
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
11
11
  const { getHeaders } = useQueryHeaders();
12
+ const queryClient = useQueryClient();
13
+ const config = queryClient.getQueryData(['config']);
12
14
  const sendRequest = async (res, rej, data) => {
13
15
  // get request headers
14
16
  const globalHeaders = getHeaders();
@@ -22,12 +24,16 @@ const usePatchRequest = ({ path, baseUrl, headers }) => {
22
24
  });
23
25
  if (patchResponse.status) {
24
26
  // scroll to top after success
25
- scrollToTop();
27
+ if (config?.options?.context !== 'app') {
28
+ scrollToTop();
29
+ }
26
30
  res(patchResponse);
27
31
  }
28
32
  else {
29
33
  // scroll to top after error
30
- scrollToTop();
34
+ if (config?.options?.context !== 'app') {
35
+ scrollToTop();
36
+ }
31
37
  rej(patchResponse);
32
38
  }
33
39
  };
@@ -1 +1 @@
1
- {"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"usePatchRequest.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ventlio/tanstack-query",
3
- "version": "0.2.65",
3
+ "version": "0.2.69",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "contributors": [
@@ -4,7 +4,7 @@ import type { TanstackQueryConfig } from '../types';
4
4
  export const useQueryConfig = (): TanstackQueryConfig => {
5
5
  const queryClient = useQueryClient();
6
6
 
7
- const { headers = {} } = queryClient.getQueryData<TanstackQueryConfig>(['config']) ?? {};
7
+ const { headers = {}, options = {} } = queryClient.getQueryData<TanstackQueryConfig>(['config']) ?? {};
8
8
 
9
- return { headers };
9
+ return { headers, options };
10
10
  };
@@ -5,6 +5,8 @@ export interface QueryModelBuilder<T> {
5
5
  find: (id: number | string, path?: string) => T | undefined;
6
6
  update: (id: number | string, data: Partial<T>, path?: string) => T | undefined;
7
7
  remove: (id: number, path?: string) => boolean;
8
+ get: (path?: string) => T | undefined;
9
+ set: (data: Partial<T>, path?: string) => T | undefined;
8
10
  }
9
11
 
10
12
  export type QueryModelAddPosition = 'start' | 'end';
@@ -1,6 +1,6 @@
1
1
  import { useQueryClient } from '@tanstack/react-query';
2
2
  import result from 'lodash.result';
3
- import set from 'lodash.set';
3
+ import { default as lodashSet } from 'lodash.set';
4
4
  import type { TanstackQueryConfig } from '../types';
5
5
  import type { QueryModelAddPosition, QueryModelBuilder } from './model.interface';
6
6
  import { useKeyTrackerModel } from './useKeyTrackerModel';
@@ -24,7 +24,7 @@ export const useQueryModel = <T>(keyTracker: string, exact: boolean = true): Que
24
24
  queryClient.setQueryData(queryKey, records);
25
25
  } else {
26
26
  const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
27
- queryClient.setQueryData(queryKey, set(queryData, path, records));
27
+ queryClient.setQueryData(queryKey, lodashSet(queryData, path, records));
28
28
  }
29
29
 
30
30
  return data;
@@ -60,6 +60,24 @@ export const useQueryModel = <T>(keyTracker: string, exact: boolean = true): Que
60
60
  return data.find((record) => (record as Record<string, any>)[modelConfig.idColumn] === id);
61
61
  };
62
62
 
63
+ const get = (path?: string): T | undefined => {
64
+ let data = queryClient.getQueryData(queryKey, { exact });
65
+ if (path) {
66
+ data = result<T>(data, path);
67
+ }
68
+ return data as T;
69
+ };
70
+
71
+ const set = (newData: any, path?: string): T | undefined => {
72
+ if (path) {
73
+ const data = get() as any;
74
+ newData = lodashSet(data, path, newData);
75
+
76
+ return queryClient.setQueryData(queryKey, newData) as T;
77
+ }
78
+ return queryClient.setQueryData(queryKey, newData) as T;
79
+ };
80
+
63
81
  const getModelConfig = () => {
64
82
  const { options } = queryClient.getQueryData<TanstackQueryConfig>(['config']) ?? {};
65
83
  const { modelConfig } = options ?? {};
@@ -79,7 +97,6 @@ export const useQueryModel = <T>(keyTracker: string, exact: boolean = true): Que
79
97
  let updatedRecord: T | undefined = undefined;
80
98
  const newData = oldData.map((record) => {
81
99
  let dataRecord = record as Record<string, any>;
82
-
83
100
  if (dataRecord[idColumn] === id) {
84
101
  dataRecord = { ...dataRecord, ...data };
85
102
  updatedRecord = dataRecord as T;
@@ -92,7 +109,7 @@ export const useQueryModel = <T>(keyTracker: string, exact: boolean = true): Que
92
109
  queryClient.setQueryData(queryKey, newData);
93
110
  } else {
94
111
  const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
95
- queryClient.setQueryData(queryKey, set(queryData, path, newData));
112
+ queryClient.setQueryData(queryKey, lodashSet(queryData, path, newData));
96
113
  }
97
114
  return updatedRecord;
98
115
  };
@@ -119,10 +136,10 @@ export const useQueryModel = <T>(keyTracker: string, exact: boolean = true): Que
119
136
  queryClient.setQueryData(queryKey, newData);
120
137
  } else {
121
138
  const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
122
- queryClient.setQueryData(queryKey, set(queryData, path, newData));
139
+ queryClient.setQueryData(queryKey, lodashSet(queryData, path, newData));
123
140
  }
124
141
  return updated;
125
142
  };
126
143
 
127
- return { find, findAll, findMany, remove, update, add };
144
+ return { find, findAll, findMany, remove, update, add, get, set };
128
145
  };
@@ -4,9 +4,9 @@ export const useRefetchQuery = async (queryKey: any[]) => {
4
4
  const queryClient = useQueryClient();
5
5
 
6
6
  const refetchQuery = async <T>(innerQueryKey?: any[]) => {
7
- await queryClient.refetchQueries(
7
+ await queryClient.invalidateQueries(
8
8
  {
9
- queryKey,
9
+ queryKey: innerQueryKey ?? queryKey,
10
10
  exact: true,
11
11
  },
12
12
  { throwOnError: true, cancelRefetch: true }
@@ -56,14 +56,14 @@ export const useDeleteRequest = <TResponse>(deleteOptions?: DefaultRequestOption
56
56
  IRequestError,
57
57
  IRequestSuccess<TResponse | undefined>,
58
58
  Array<any>
59
- >
59
+ > & { cached?: boolean }
60
60
  ): Promise<IRequestSuccess<TResponse> | undefined> => {
61
61
  // set enabled to be true for every delete
62
62
  internalDeleteOptions = internalDeleteOptions ?? {};
63
63
  internalDeleteOptions.enabled = true;
64
64
 
65
- await updatedPathAsync(link);
66
65
  await setOptionsAsync(internalDeleteOptions);
66
+ await updatedPathAsync(link);
67
67
 
68
68
  return query.data;
69
69
  };
@@ -39,20 +39,24 @@ export const useGetRequest = <TResponse extends Record<string, any>>({
39
39
  ) => void,
40
40
  rej: (reason?: any) => void
41
41
  ) => {
42
- // get request headers
43
- const globalHeaders: RawAxiosRequestHeaders = getHeaders();
44
-
45
- const getResponse = await makeRequest<TResponse>({
46
- path: requestPath,
47
- headers: { ...globalHeaders, ...headers },
48
- baseURL: baseUrl ?? API_URL,
49
- timeout: TIMEOUT,
50
- });
42
+ if (load) {
43
+ // get request headers
44
+ const globalHeaders: RawAxiosRequestHeaders = getHeaders();
45
+
46
+ const getResponse = await makeRequest<TResponse>({
47
+ path: requestPath,
48
+ headers: { ...globalHeaders, ...headers },
49
+ baseURL: baseUrl ?? API_URL,
50
+ timeout: TIMEOUT,
51
+ });
51
52
 
52
- if (getResponse.status) {
53
- res(getResponse as IRequestSuccess<TResponse>);
53
+ if (getResponse.status) {
54
+ res(getResponse as IRequestSuccess<TResponse>);
55
+ } else {
56
+ rej(getResponse);
57
+ }
54
58
  } else {
55
- rej(getResponse);
59
+ res(null as any);
56
60
  }
57
61
  };
58
62
 
@@ -1,16 +1,20 @@
1
1
  import type { MutateOptions } from '@tanstack/react-query';
2
- import { useMutation } from '@tanstack/react-query';
2
+ import { useMutation, useQueryClient } from '@tanstack/react-query';
3
3
  import type { RawAxiosRequestHeaders } from 'axios';
4
4
  import { useEnvironmentVariables, useQueryHeaders } from '../config';
5
5
  import { scrollToTop } from '../helpers';
6
6
  import { HttpMethod, makeRequest } from '../request';
7
7
  import type { IRequestError, IRequestSuccess } from '../request/request.interface';
8
+ import type { TanstackQueryConfig } from '../types';
8
9
  import type { DefaultRequestOptions } from './queries.interface';
9
10
 
10
11
  export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: string } & DefaultRequestOptions) => {
11
12
  const { API_URL, TIMEOUT } = useEnvironmentVariables();
12
13
 
13
14
  const { getHeaders } = useQueryHeaders();
15
+ const queryClient = useQueryClient();
16
+
17
+ const config = queryClient.getQueryData<TanstackQueryConfig>(['config']);
14
18
 
15
19
  const sendRequest = async (res: (value: any) => void, rej: (reason?: any) => void, data: any) => {
16
20
  // get request headers
@@ -27,11 +31,15 @@ export const usePatchRequest = <TResponse>({ path, baseUrl, headers }: { path: s
27
31
 
28
32
  if (patchResponse.status) {
29
33
  // scroll to top after success
30
- scrollToTop();
34
+ if (config?.options?.context !== 'app') {
35
+ scrollToTop();
36
+ }
31
37
  res(patchResponse as IRequestSuccess<TResponse>);
32
38
  } else {
33
39
  // scroll to top after error
34
- scrollToTop();
40
+ if (config?.options?.context !== 'app') {
41
+ scrollToTop();
42
+ }
35
43
  rej(patchResponse);
36
44
  }
37
45
  };