@ventlio/tanstack-query 0.2.64 → 0.2.66

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.js CHANGED
@@ -5,7 +5,6 @@ export { useQueryHeaders } from './config/useQueryHeaders.js';
5
5
  export { useReactNativeEnv } from './config/useReactNativeEnv.js';
6
6
  export { scrollToTop } from './helpers/scrollToTop.js';
7
7
  export { getDateInFuture } from './helpers/timeFuncs.js';
8
- export { QueryModel } from './model/Model.js';
9
8
  export { useKeyTrackerModel } from './model/useKeyTrackerModel.js';
10
9
  export { useQueryModel } from './model/useQueryModel.js';
11
10
  export { useRefetchQuery } from './model/useRefetchQuery.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;"}
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 = () => {
@@ -83,94 +83,6 @@ const useQueryHeaders = () => {
83
83
  return { setQueryHeaders, getHeaders };
84
84
  };
85
85
 
86
- class QueryModel {
87
- queryKey;
88
- queryClient;
89
- exact;
90
- constructor(queryKey, queryClient, exact = true) {
91
- this.queryKey = queryKey;
92
- this.queryClient = queryClient;
93
- this.exact = exact;
94
- }
95
- findAll(path) {
96
- const data = this.queryClient.getQueryData(this.queryKey, { exact: this.exact });
97
- if (!data) {
98
- return [];
99
- }
100
- if (!path) {
101
- return Array.isArray(data) ? data : [data];
102
- }
103
- return result(data, path, []);
104
- }
105
- findMany(selector, path) {
106
- const data = this.findAll(path) ?? [];
107
- return data.filter(selector);
108
- }
109
- find(id, path) {
110
- const modelConfig = this.getModelConfig();
111
- if (!modelConfig?.idColumn) {
112
- return undefined;
113
- }
114
- const data = this.findAll(path) ?? [];
115
- return data.find((record) => record[modelConfig.idColumn] === id);
116
- }
117
- update(id, data, path) {
118
- const oldData = this.findAll(path) ?? [];
119
- const modelConfig = this.getModelConfig();
120
- if (!modelConfig?.idColumn) {
121
- return undefined;
122
- }
123
- const idColumn = modelConfig.idColumn;
124
- let updatedRecord = undefined;
125
- const newData = oldData.map((record) => {
126
- let dataRecord = record;
127
- if (dataRecord[idColumn] === id) {
128
- dataRecord = { ...dataRecord, ...data };
129
- updatedRecord = dataRecord;
130
- }
131
- return dataRecord;
132
- });
133
- if (!path) {
134
- this.queryClient.setQueryData(this.queryKey, newData);
135
- }
136
- else {
137
- const queryData = this.queryClient.getQueryData(this.queryKey, { exact: this.exact }) ?? {};
138
- this.queryClient.setQueryData(this.queryKey, set(queryData, path, newData));
139
- }
140
- return updatedRecord;
141
- }
142
- remove(id, path) {
143
- const oldData = this.findAll(path) ?? [];
144
- const modelConfig = this.getModelConfig();
145
- if (!modelConfig?.idColumn) {
146
- return false;
147
- }
148
- const idColumn = modelConfig.idColumn;
149
- let updated = false;
150
- const newData = oldData.filter((record) => {
151
- const dataRecord = record;
152
- if (dataRecord[idColumn] === id) {
153
- updated = true;
154
- return false;
155
- }
156
- return true;
157
- });
158
- if (!path) {
159
- this.queryClient.setQueryData(this.queryKey, newData);
160
- }
161
- else {
162
- const queryData = this.queryClient.getQueryData(this.queryKey, { exact: this.exact }) ?? {};
163
- this.queryClient.setQueryData(this.queryKey, set(queryData, path, newData));
164
- }
165
- return updated;
166
- }
167
- getModelConfig() {
168
- const { options } = this.queryClient.getQueryData(['config']) ?? {};
169
- const { modelConfig } = options ?? {};
170
- return modelConfig;
171
- }
172
- }
173
-
174
86
  const useKeyTrackerModel = (keyTracker) => {
175
87
  const queryClient = useQueryClient();
176
88
  const getQueryKey = (innerKeyTracker) => {
@@ -191,6 +103,24 @@ const useQueryModel = (keyTracker, exact = true) => {
191
103
  const queryClient = useQueryClient();
192
104
  const { getQueryKey } = useKeyTrackerModel(keyTracker);
193
105
  const queryKey = getQueryKey();
106
+ const add = (data, position, path) => {
107
+ let records = findAll(path) ?? [];
108
+ if (!position || position === 'end') {
109
+ records = [...records, data];
110
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
111
+ }
112
+ else if (position === 'start') {
113
+ records = [data, ...records];
114
+ }
115
+ if (!path) {
116
+ queryClient.setQueryData(queryKey, records);
117
+ }
118
+ else {
119
+ const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
120
+ queryClient.setQueryData(queryKey, lodashSet(queryData, path, records));
121
+ }
122
+ return data;
123
+ };
194
124
  const findAll = (path) => {
195
125
  const data = queryClient.getQueryData(queryKey, { exact });
196
126
  if (!data) {
@@ -213,6 +143,21 @@ const useQueryModel = (keyTracker, exact = true) => {
213
143
  const data = findAll(path) ?? [];
214
144
  return data.find((record) => record[modelConfig.idColumn] === id);
215
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
+ };
216
161
  const getModelConfig = () => {
217
162
  const { options } = queryClient.getQueryData(['config']) ?? {};
218
163
  const { modelConfig } = options ?? {};
@@ -239,7 +184,7 @@ const useQueryModel = (keyTracker, exact = true) => {
239
184
  }
240
185
  else {
241
186
  const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
242
- queryClient.setQueryData(queryKey, set(queryData, path, newData));
187
+ queryClient.setQueryData(queryKey, lodashSet(queryData, path, newData));
243
188
  }
244
189
  return updatedRecord;
245
190
  };
@@ -264,18 +209,18 @@ const useQueryModel = (keyTracker, exact = true) => {
264
209
  }
265
210
  else {
266
211
  const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
267
- queryClient.setQueryData(queryKey, set(queryData, path, newData));
212
+ queryClient.setQueryData(queryKey, lodashSet(queryData, path, newData));
268
213
  }
269
214
  return updated;
270
215
  };
271
- return { find, findAll, findMany, remove, update };
216
+ return { find, findAll, findMany, remove, update, add, get, set };
272
217
  };
273
218
 
274
219
  const useRefetchQuery = async (queryKey) => {
275
220
  const queryClient = useQueryClient();
276
221
  const refetchQuery = async (innerQueryKey) => {
277
- await queryClient.refetchQueries({
278
- queryKey,
222
+ await queryClient.invalidateQueries({
223
+ queryKey: innerQueryKey ?? queryKey,
279
224
  exact: true,
280
225
  }, { throwOnError: true, cancelRefetch: true });
281
226
  return queryClient.getQueriesData(innerQueryKey ?? queryKey);
@@ -496,19 +441,24 @@ const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl,
496
441
  // eslint-disable-next-line react-hooks/exhaustive-deps
497
442
  queryClient = useMemo(() => queryClient, []);
498
443
  const sendRequest = async (res, rej) => {
499
- // get request headers
500
- const globalHeaders = getHeaders();
501
- const getResponse = await makeRequest({
502
- path: requestPath,
503
- headers: { ...globalHeaders, ...headers },
504
- baseURL: baseUrl ?? API_URL,
505
- timeout: TIMEOUT,
506
- });
507
- if (getResponse.status) {
508
- 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
+ }
509
459
  }
510
460
  else {
511
- rej(getResponse);
461
+ res(null);
512
462
  }
513
463
  };
514
464
  const query = useQuery([requestPath, {}], () => new Promise((res, rej) => sendRequest(res, rej)), {
@@ -668,5 +618,5 @@ const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelect
668
618
  return { post, ...mutation };
669
619
  };
670
620
 
671
- export { ContentType, HttpMethod, QueryModel, axiosInstance, bootstrapQueryRequest, buildFormData, errorTransformer, getDateInFuture, makeRequest, scrollToTop, successTransformer, useDeleteRequest, useEnvironmentVariables, useGetRequest, useKeyTrackerModel, usePatchRequest, usePostRequest, useQueryConfig, useQueryHeaders, useQueryModel, useReactNativeEnv, useRefetchQuery };
621
+ export { ContentType, HttpMethod, axiosInstance, bootstrapQueryRequest, buildFormData, errorTransformer, getDateInFuture, makeRequest, scrollToTop, successTransformer, useDeleteRequest, useEnvironmentVariables, useGetRequest, useKeyTrackerModel, usePatchRequest, usePostRequest, useQueryConfig, useQueryHeaders, useQueryModel, useReactNativeEnv, useRefetchQuery };
672
622
  //# sourceMappingURL=index.mjs.map
@@ -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,3 @@
1
- export * from './Model';
2
1
  export * from './model.interface';
3
2
  export * from './useKeyTrackerModel';
4
3
  export * from './useQueryModel';
@@ -1,7 +1,11 @@
1
1
  export interface QueryModelBuilder<T> {
2
+ add: (data: T, position?: QueryModelAddPosition, path?: string) => T | undefined;
2
3
  findAll: (path?: string) => T[] | undefined;
3
4
  findMany: (selector: (record: T) => boolean, path?: string) => T[];
4
5
  find: (id: number | string, path?: string) => T | undefined;
5
6
  update: (id: number | string, data: Partial<T>, path?: string) => T | undefined;
6
7
  remove: (id: number, path?: string) => boolean;
8
+ get: (path?: string) => T | undefined;
9
+ set: (data: Partial<T>, path?: string) => T | undefined;
7
10
  }
11
+ export type QueryModelAddPosition = 'start' | 'end';
@@ -1,12 +1,30 @@
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) => {
7
7
  const queryClient = useQueryClient();
8
8
  const { getQueryKey } = useKeyTrackerModel(keyTracker);
9
9
  const queryKey = getQueryKey();
10
+ const add = (data, position, path) => {
11
+ let records = findAll(path) ?? [];
12
+ if (!position || position === 'end') {
13
+ records = [...records, data];
14
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
15
+ }
16
+ else if (position === 'start') {
17
+ records = [data, ...records];
18
+ }
19
+ if (!path) {
20
+ queryClient.setQueryData(queryKey, records);
21
+ }
22
+ else {
23
+ const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
24
+ queryClient.setQueryData(queryKey, lodashSet(queryData, path, records));
25
+ }
26
+ return data;
27
+ };
10
28
  const findAll = (path) => {
11
29
  const data = queryClient.getQueryData(queryKey, { exact });
12
30
  if (!data) {
@@ -29,6 +47,21 @@ const useQueryModel = (keyTracker, exact = true) => {
29
47
  const data = findAll(path) ?? [];
30
48
  return data.find((record) => record[modelConfig.idColumn] === id);
31
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
+ };
32
65
  const getModelConfig = () => {
33
66
  const { options } = queryClient.getQueryData(['config']) ?? {};
34
67
  const { modelConfig } = options ?? {};
@@ -55,7 +88,7 @@ const useQueryModel = (keyTracker, exact = true) => {
55
88
  }
56
89
  else {
57
90
  const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
58
- queryClient.setQueryData(queryKey, set(queryData, path, newData));
91
+ queryClient.setQueryData(queryKey, lodashSet(queryData, path, newData));
59
92
  }
60
93
  return updatedRecord;
61
94
  };
@@ -80,11 +113,11 @@ const useQueryModel = (keyTracker, exact = true) => {
80
113
  }
81
114
  else {
82
115
  const queryData = queryClient.getQueryData(queryKey, { exact }) ?? {};
83
- queryClient.setQueryData(queryKey, set(queryData, path, newData));
116
+ queryClient.setQueryData(queryKey, lodashSet(queryData, path, newData));
84
117
  }
85
118
  return updated;
86
119
  };
87
- return { find, findAll, findMany, remove, update };
120
+ return { find, findAll, findMany, remove, update, add, get, set };
88
121
  };
89
122
 
90
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);
@@ -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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ventlio/tanstack-query",
3
- "version": "0.2.64",
3
+ "version": "0.2.66",
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
  };
@@ -1,4 +1,3 @@
1
- export * from './Model';
2
1
  export * from './model.interface';
3
2
  export * from './useKeyTrackerModel';
4
3
  export * from './useQueryModel';
@@ -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 }
@@ -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,14 +0,0 @@
1
- import type { QueryClient } from '@tanstack/react-query';
2
- import type { QueryModelBuilder } from './model.interface';
3
- export declare class QueryModel<T> implements QueryModelBuilder<T> {
4
- private readonly queryKey;
5
- private readonly queryClient;
6
- private readonly exact;
7
- constructor(queryKey: any[], queryClient: QueryClient, exact?: boolean);
8
- findAll(path?: string): T[] | undefined;
9
- findMany(selector: (record: T) => boolean, path?: string): T[];
10
- find(id: string | number, path?: string): T | undefined;
11
- update(id: string | number, data: Partial<T>, path?: string): T | undefined;
12
- remove(id: number | string, path?: string): boolean;
13
- private getModelConfig;
14
- }
@@ -1,93 +0,0 @@
1
- import result from 'lodash.result';
2
- import set from 'lodash.set';
3
-
4
- class QueryModel {
5
- queryKey;
6
- queryClient;
7
- exact;
8
- constructor(queryKey, queryClient, exact = true) {
9
- this.queryKey = queryKey;
10
- this.queryClient = queryClient;
11
- this.exact = exact;
12
- }
13
- findAll(path) {
14
- const data = this.queryClient.getQueryData(this.queryKey, { exact: this.exact });
15
- if (!data) {
16
- return [];
17
- }
18
- if (!path) {
19
- return Array.isArray(data) ? data : [data];
20
- }
21
- return result(data, path, []);
22
- }
23
- findMany(selector, path) {
24
- const data = this.findAll(path) ?? [];
25
- return data.filter(selector);
26
- }
27
- find(id, path) {
28
- const modelConfig = this.getModelConfig();
29
- if (!modelConfig?.idColumn) {
30
- return undefined;
31
- }
32
- const data = this.findAll(path) ?? [];
33
- return data.find((record) => record[modelConfig.idColumn] === id);
34
- }
35
- update(id, data, path) {
36
- const oldData = this.findAll(path) ?? [];
37
- const modelConfig = this.getModelConfig();
38
- if (!modelConfig?.idColumn) {
39
- return undefined;
40
- }
41
- const idColumn = modelConfig.idColumn;
42
- let updatedRecord = undefined;
43
- const newData = oldData.map((record) => {
44
- let dataRecord = record;
45
- if (dataRecord[idColumn] === id) {
46
- dataRecord = { ...dataRecord, ...data };
47
- updatedRecord = dataRecord;
48
- }
49
- return dataRecord;
50
- });
51
- if (!path) {
52
- this.queryClient.setQueryData(this.queryKey, newData);
53
- }
54
- else {
55
- const queryData = this.queryClient.getQueryData(this.queryKey, { exact: this.exact }) ?? {};
56
- this.queryClient.setQueryData(this.queryKey, set(queryData, path, newData));
57
- }
58
- return updatedRecord;
59
- }
60
- remove(id, path) {
61
- const oldData = this.findAll(path) ?? [];
62
- const modelConfig = this.getModelConfig();
63
- if (!modelConfig?.idColumn) {
64
- return false;
65
- }
66
- const idColumn = modelConfig.idColumn;
67
- let updated = false;
68
- const newData = oldData.filter((record) => {
69
- const dataRecord = record;
70
- if (dataRecord[idColumn] === id) {
71
- updated = true;
72
- return false;
73
- }
74
- return true;
75
- });
76
- if (!path) {
77
- this.queryClient.setQueryData(this.queryKey, newData);
78
- }
79
- else {
80
- const queryData = this.queryClient.getQueryData(this.queryKey, { exact: this.exact }) ?? {};
81
- this.queryClient.setQueryData(this.queryKey, set(queryData, path, newData));
82
- }
83
- return updated;
84
- }
85
- getModelConfig() {
86
- const { options } = this.queryClient.getQueryData(['config']) ?? {};
87
- const { modelConfig } = options ?? {};
88
- return modelConfig;
89
- }
90
- }
91
-
92
- export { QueryModel };
93
- //# sourceMappingURL=Model.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Model.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,107 +0,0 @@
1
- import type { QueryClient } from '@tanstack/react-query';
2
- import result from 'lodash.result';
3
- import set from 'lodash.set';
4
- import type { TanstackQueryConfig } from '../types';
5
- import type { QueryModelBuilder } from './model.interface';
6
-
7
- export class QueryModel<T> implements QueryModelBuilder<T> {
8
- constructor(
9
- private readonly queryKey: any[],
10
- private readonly queryClient: QueryClient,
11
- private readonly exact: boolean = true
12
- ) {}
13
-
14
- public findAll(path?: string): T[] | undefined {
15
- const data = this.queryClient.getQueryData(this.queryKey, { exact: this.exact });
16
-
17
- if (!data) {
18
- return [];
19
- }
20
-
21
- if (!path) {
22
- return Array.isArray(data) ? data : [data];
23
- }
24
-
25
- return result<T[]>(data, path, []);
26
- }
27
-
28
- public findMany(selector: (record: T) => boolean, path?: string): T[] {
29
- const data = this.findAll(path) ?? [];
30
- return data.filter(selector);
31
- }
32
-
33
- find(id: string | number, path?: string): T | undefined {
34
- const modelConfig = this.getModelConfig();
35
-
36
- if (!modelConfig?.idColumn) {
37
- return undefined;
38
- }
39
- const data = this.findAll(path) ?? [];
40
-
41
- return data.find((record) => (record as Record<string, any>)[modelConfig.idColumn] === id);
42
- }
43
-
44
- update(id: string | number, data: Partial<T>, path?: string): T | undefined {
45
- const oldData = this.findAll(path) ?? [];
46
- const modelConfig = this.getModelConfig();
47
-
48
- if (!modelConfig?.idColumn) {
49
- return undefined;
50
- }
51
- const idColumn = modelConfig.idColumn;
52
-
53
- let updatedRecord: T | undefined = undefined;
54
- const newData = oldData.map((record) => {
55
- let dataRecord = record as Record<string, any>;
56
-
57
- if (dataRecord[idColumn] === id) {
58
- dataRecord = { ...dataRecord, ...data };
59
- updatedRecord = dataRecord as T;
60
- }
61
-
62
- return dataRecord;
63
- });
64
-
65
- if (!path) {
66
- this.queryClient.setQueryData(this.queryKey, newData);
67
- } else {
68
- const queryData = this.queryClient.getQueryData(this.queryKey, { exact: this.exact }) ?? {};
69
- this.queryClient.setQueryData(this.queryKey, set(queryData, path, newData));
70
- }
71
- return updatedRecord;
72
- }
73
-
74
- remove(id: number | string, path?: string): boolean {
75
- const oldData = this.findAll(path) ?? [];
76
- const modelConfig = this.getModelConfig();
77
-
78
- if (!modelConfig?.idColumn) {
79
- return false;
80
- }
81
- const idColumn = modelConfig.idColumn;
82
- let updated = false;
83
- const newData = oldData.filter((record) => {
84
- const dataRecord = record as Record<string, any>;
85
- if (dataRecord[idColumn] === id) {
86
- updated = true;
87
- return false;
88
- }
89
- return true;
90
- });
91
-
92
- if (!path) {
93
- this.queryClient.setQueryData(this.queryKey, newData);
94
- } else {
95
- const queryData = this.queryClient.getQueryData(this.queryKey, { exact: this.exact }) ?? {};
96
- this.queryClient.setQueryData(this.queryKey, set(queryData, path, newData));
97
- }
98
- return updated;
99
- }
100
-
101
- private getModelConfig() {
102
- const { options } = this.queryClient.getQueryData<TanstackQueryConfig>(['config']) ?? {};
103
- const { modelConfig } = options ?? {};
104
-
105
- return modelConfig;
106
- }
107
- }