@ventlio/tanstack-query 0.5.3 → 0.5.5
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.
- package/dist/config/bootstrapQueryRequest.d.ts +4 -4
- package/dist/config/bootstrapQueryRequest.js +15 -15
- package/dist/config/config.interface.d.ts +4 -4
- package/dist/config/index.d.ts +6 -6
- package/dist/config/useEnvironmentVariables.d.ts +2 -2
- package/dist/config/useEnvironmentVariables.js +8 -8
- package/dist/config/useQueryConfig.d.ts +2 -2
- package/dist/config/useQueryConfig.js +5 -5
- package/dist/config/useQueryHeaders.d.ts +2 -2
- package/dist/config/useQueryHeaders.js +9 -9
- package/dist/config/useReactNativeEnv.d.ts +5 -5
- package/dist/config/useReactNativeEnv.js +6 -6
- package/dist/helpers/index.d.ts +2 -2
- package/dist/helpers/scrollToTop.d.ts +1 -1
- package/dist/helpers/scrollToTop.js +5 -5
- package/dist/helpers/timeFuncs.d.ts +1 -1
- package/dist/helpers/timeFuncs.js +7 -7
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/useUploadProgress.d.ts +5 -5
- package/dist/hooks/useUploadProgress.js +8 -8
- package/dist/index.d.ts +8 -8
- package/dist/index.mjs +907 -830
- package/dist/index.mjs.map +1 -1
- package/dist/model/index.d.ts +4 -4
- package/dist/model/model.interface.d.ts +11 -11
- package/dist/model/useKeyTrackerModel.d.ts +4 -4
- package/dist/model/useKeyTrackerModel.js +14 -14
- package/dist/model/useQueryModel.d.ts +2 -2
- package/dist/model/useQueryModel.js +115 -115
- package/dist/model/useRefetchQuery.d.ts +3 -3
- package/dist/model/useRefetchQuery.js +10 -10
- package/dist/queries/index.d.ts +6 -6
- package/dist/queries/queries.interface.d.ts +17 -17
- package/dist/queries/useDeleteRequest.d.ts +60 -60
- package/dist/queries/useDeleteRequest.js +86 -71
- package/dist/queries/useDeleteRequest.js.map +1 -1
- package/dist/queries/useGetInfiniteRequest.d.ts +108 -110
- package/dist/queries/useGetInfiniteRequest.js +117 -102
- package/dist/queries/useGetInfiniteRequest.js.map +1 -1
- package/dist/queries/useGetRequest.d.ts +73 -74
- package/dist/queries/useGetRequest.js +148 -133
- package/dist/queries/useGetRequest.js.map +1 -1
- package/dist/queries/usePatchRequest.d.ts +86 -86
- package/dist/queries/usePatchRequest.js +86 -70
- package/dist/queries/usePatchRequest.js.map +1 -1
- package/dist/queries/usePostRequest.d.ts +149 -157
- package/dist/queries/usePostRequest.js +94 -78
- package/dist/queries/usePostRequest.js.map +1 -1
- package/dist/queries/usePutRequest.d.ts +86 -86
- package/dist/request/axios-instance.d.ts +3 -3
- package/dist/request/axios-instance.js +6 -6
- package/dist/request/buildFormData.d.ts +1 -1
- package/dist/request/buildFormData.js +40 -40
- package/dist/request/index.d.ts +6 -6
- package/dist/request/make-request.d.ts +2 -2
- package/dist/request/make-request.js +80 -80
- package/dist/request/request.enum.d.ts +11 -11
- package/dist/request/request.enum.js +12 -12
- package/dist/request/request.interface.d.ts +48 -48
- package/dist/request/transformer.d.ts +7 -7
- package/dist/request/transformer.js +18 -18
- package/dist/stores/index.d.ts +2 -2
- package/dist/stores/useHeaderStore.d.ts +7 -7
- package/dist/stores/useHeaderStore.js +5 -5
- package/dist/stores/usePauseFutureRequests.d.ts +7 -7
- package/dist/stores/usePauseFutureRequests.js +13 -13
- package/dist/types/index.d.ts +34 -29
- package/package.json +2 -2
- package/src/queries/useDeleteRequest.ts +24 -5
- package/src/queries/useGetInfiniteRequest.ts +21 -2
- package/src/queries/useGetRequest.ts +26 -9
- package/src/queries/usePatchRequest.ts +21 -1
- package/src/queries/usePostRequest.ts +21 -1
- package/src/queries/usePutRequest.ts +21 -1
- package/src/request/make-request.ts +3 -3
- package/src/request/transformer.ts +1 -1
- package/src/types/index.ts +5 -1
package/dist/index.mjs
CHANGED
|
@@ -6,880 +6,957 @@ import result from 'lodash.result';
|
|
|
6
6
|
import lodashSet from 'lodash.set';
|
|
7
7
|
import axios from 'axios';
|
|
8
8
|
|
|
9
|
-
const bootstrapQueryRequest = (queryClient, options) => {
|
|
10
|
-
// make query config doesn't expire
|
|
11
|
-
// set default query config
|
|
12
|
-
const defaultMeta = {
|
|
13
|
-
headers: {
|
|
14
|
-
Authorization: ``,
|
|
15
|
-
},
|
|
16
|
-
options,
|
|
17
|
-
};
|
|
18
|
-
queryClient.setDefaultOptions({
|
|
19
|
-
queries: {
|
|
20
|
-
meta: defaultMeta,
|
|
21
|
-
},
|
|
22
|
-
mutations: { meta: defaultMeta },
|
|
23
|
-
});
|
|
9
|
+
const bootstrapQueryRequest = (queryClient, options) => {
|
|
10
|
+
// make query config doesn't expire
|
|
11
|
+
// set default query config
|
|
12
|
+
const defaultMeta = {
|
|
13
|
+
headers: {
|
|
14
|
+
Authorization: ``,
|
|
15
|
+
},
|
|
16
|
+
options,
|
|
17
|
+
};
|
|
18
|
+
queryClient.setDefaultOptions({
|
|
19
|
+
queries: {
|
|
20
|
+
meta: defaultMeta,
|
|
21
|
+
},
|
|
22
|
+
mutations: { meta: defaultMeta },
|
|
23
|
+
});
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
const useQueryConfig = () => {
|
|
27
|
-
const queryClient = useQueryClient();
|
|
28
|
-
const mutationMeta = (queryClient.getDefaultOptions().mutations?.meta ?? {});
|
|
29
|
-
const queryMeta = (queryClient.getDefaultOptions().queries?.meta ?? {});
|
|
30
|
-
return { ...queryMeta, ...mutationMeta };
|
|
26
|
+
const useQueryConfig = () => {
|
|
27
|
+
const queryClient = useQueryClient();
|
|
28
|
+
const mutationMeta = (queryClient.getDefaultOptions().mutations?.meta ?? {});
|
|
29
|
+
const queryMeta = (queryClient.getDefaultOptions().queries?.meta ?? {});
|
|
30
|
+
return { ...queryMeta, ...mutationMeta };
|
|
31
31
|
};
|
|
32
32
|
|
|
33
|
-
const useReactNativeEnv = () => {
|
|
34
|
-
const config = useQueryConfig();
|
|
35
|
-
const appUrl = config.options?.environments?.appBaseUrl;
|
|
36
|
-
const appTimeout = config.options?.environments?.appTimeout;
|
|
37
|
-
const isApp = config.options?.context === 'app';
|
|
38
|
-
return { appUrl, appTimeout, isApp };
|
|
33
|
+
const useReactNativeEnv = () => {
|
|
34
|
+
const config = useQueryConfig();
|
|
35
|
+
const appUrl = config.options?.environments?.appBaseUrl;
|
|
36
|
+
const appTimeout = config.options?.environments?.appTimeout;
|
|
37
|
+
const isApp = config.options?.context === 'app';
|
|
38
|
+
return { appUrl, appTimeout, isApp };
|
|
39
39
|
};
|
|
40
40
|
|
|
41
|
-
const useEnvironmentVariables = () => {
|
|
42
|
-
const { appTimeout, appUrl } = useReactNativeEnv();
|
|
43
|
-
const url = process.env.REACT_APP_API_URL ?? process.env.NEXT_PUBLIC_API_URL ?? appUrl;
|
|
44
|
-
const timeout = process.env.REACT_APP_API_TIMEOUT ?? process.env.NEXT_PUBLIC_API_TIMEOUT ?? appTimeout;
|
|
45
|
-
return {
|
|
46
|
-
API_URL: url,
|
|
47
|
-
TIMEOUT: Number(timeout),
|
|
48
|
-
};
|
|
41
|
+
const useEnvironmentVariables = () => {
|
|
42
|
+
const { appTimeout, appUrl } = useReactNativeEnv();
|
|
43
|
+
const url = process.env.REACT_APP_API_URL ?? process.env.NEXT_PUBLIC_API_URL ?? appUrl;
|
|
44
|
+
const timeout = process.env.REACT_APP_API_TIMEOUT ?? process.env.NEXT_PUBLIC_API_TIMEOUT ?? appTimeout;
|
|
45
|
+
return {
|
|
46
|
+
API_URL: url,
|
|
47
|
+
TIMEOUT: Number(timeout),
|
|
48
|
+
};
|
|
49
49
|
};
|
|
50
50
|
|
|
51
|
-
const useHeaderStore = create((set) => ({
|
|
52
|
-
headers: undefined,
|
|
53
|
-
setHeader(headers) {
|
|
54
|
-
set({ headers });
|
|
55
|
-
},
|
|
51
|
+
const useHeaderStore = create((set) => ({
|
|
52
|
+
headers: undefined,
|
|
53
|
+
setHeader(headers) {
|
|
54
|
+
set({ headers });
|
|
55
|
+
},
|
|
56
56
|
}));
|
|
57
57
|
|
|
58
|
-
const usePauseFutureRequests = create((set) => {
|
|
59
|
-
const pauseFutureQueries = (status) => {
|
|
60
|
-
return set({ isFutureQueriesPaused: status });
|
|
61
|
-
};
|
|
62
|
-
const pauseFutureMutation = (status) => {
|
|
63
|
-
return set({ isFutureQueriesPaused: status });
|
|
64
|
-
};
|
|
65
|
-
return {
|
|
66
|
-
isFutureMutationsPaused: false,
|
|
67
|
-
isFutureQueriesPaused: false,
|
|
68
|
-
pauseFutureQueries,
|
|
69
|
-
pauseFutureMutation,
|
|
70
|
-
};
|
|
58
|
+
const usePauseFutureRequests = create((set) => {
|
|
59
|
+
const pauseFutureQueries = (status) => {
|
|
60
|
+
return set({ isFutureQueriesPaused: status });
|
|
61
|
+
};
|
|
62
|
+
const pauseFutureMutation = (status) => {
|
|
63
|
+
return set({ isFutureQueriesPaused: status });
|
|
64
|
+
};
|
|
65
|
+
return {
|
|
66
|
+
isFutureMutationsPaused: false,
|
|
67
|
+
isFutureQueriesPaused: false,
|
|
68
|
+
pauseFutureQueries,
|
|
69
|
+
pauseFutureMutation,
|
|
70
|
+
};
|
|
71
71
|
});
|
|
72
72
|
|
|
73
|
-
const useQueryHeaders = () => {
|
|
74
|
-
const { headers, setHeader } = useHeaderStore();
|
|
75
|
-
const getHeaders = () => {
|
|
76
|
-
return headers;
|
|
77
|
-
};
|
|
78
|
-
const setQueryHeaders = (newHeaders) => {
|
|
79
|
-
setHeader(newHeaders);
|
|
80
|
-
};
|
|
81
|
-
return { setQueryHeaders, getHeaders };
|
|
73
|
+
const useQueryHeaders = () => {
|
|
74
|
+
const { headers, setHeader } = useHeaderStore();
|
|
75
|
+
const getHeaders = () => {
|
|
76
|
+
return headers;
|
|
77
|
+
};
|
|
78
|
+
const setQueryHeaders = (newHeaders) => {
|
|
79
|
+
setHeader(newHeaders);
|
|
80
|
+
};
|
|
81
|
+
return { setQueryHeaders, getHeaders };
|
|
82
82
|
};
|
|
83
83
|
|
|
84
|
-
const scrollToTop = () => {
|
|
85
|
-
window.scrollTo({
|
|
86
|
-
top: 0,
|
|
87
|
-
behavior: 'smooth',
|
|
88
|
-
});
|
|
84
|
+
const scrollToTop = () => {
|
|
85
|
+
window.scrollTo({
|
|
86
|
+
top: 0,
|
|
87
|
+
behavior: 'smooth',
|
|
88
|
+
});
|
|
89
89
|
};
|
|
90
90
|
|
|
91
|
-
function getDateInFuture(days) {
|
|
92
|
-
// Create a new Date object
|
|
93
|
-
const date = new Date();
|
|
94
|
-
// Add the specified number of days to the date
|
|
95
|
-
date.setDate(date.getDate() + days);
|
|
96
|
-
// Get the Unix timestamp of the date (in milliseconds)
|
|
97
|
-
return date.getTime();
|
|
91
|
+
function getDateInFuture(days) {
|
|
92
|
+
// Create a new Date object
|
|
93
|
+
const date = new Date();
|
|
94
|
+
// Add the specified number of days to the date
|
|
95
|
+
date.setDate(date.getDate() + days);
|
|
96
|
+
// Get the Unix timestamp of the date (in milliseconds)
|
|
97
|
+
return date.getTime();
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
const useUploadProgress = () => {
|
|
101
|
-
const [uploadProgressPercent, setUploadProgressPercent] = useState(0);
|
|
102
|
-
const onUploadProgress = (progressEvent) => {
|
|
103
|
-
const { loaded, total } = progressEvent;
|
|
104
|
-
const percentage = Math.round((loaded / total) * 100);
|
|
105
|
-
setUploadProgressPercent(percentage);
|
|
106
|
-
};
|
|
107
|
-
return { onUploadProgress, uploadProgressPercent };
|
|
100
|
+
const useUploadProgress = () => {
|
|
101
|
+
const [uploadProgressPercent, setUploadProgressPercent] = useState(0);
|
|
102
|
+
const onUploadProgress = (progressEvent) => {
|
|
103
|
+
const { loaded, total } = progressEvent;
|
|
104
|
+
const percentage = Math.round((loaded / total) * 100);
|
|
105
|
+
setUploadProgressPercent(percentage);
|
|
106
|
+
};
|
|
107
|
+
return { onUploadProgress, uploadProgressPercent };
|
|
108
108
|
};
|
|
109
109
|
|
|
110
|
-
const useKeyTrackerModel = (keyTracker) => {
|
|
111
|
-
const queryClient = useQueryClient();
|
|
112
|
-
const getQueryKey = (innerKeyTracker) => {
|
|
113
|
-
const queryKey = queryClient.getQueryData([innerKeyTracker ?? keyTracker]);
|
|
114
|
-
return queryKey;
|
|
115
|
-
};
|
|
116
|
-
const refetchQuery = async (innerKeyTracker) => {
|
|
117
|
-
const queryKey = getQueryKey(innerKeyTracker ?? keyTracker);
|
|
118
|
-
await queryClient.refetchQueries({
|
|
119
|
-
queryKey,
|
|
120
|
-
exact: true,
|
|
121
|
-
});
|
|
122
|
-
};
|
|
123
|
-
return { refetchQuery, getQueryKey };
|
|
110
|
+
const useKeyTrackerModel = (keyTracker) => {
|
|
111
|
+
const queryClient = useQueryClient();
|
|
112
|
+
const getQueryKey = (innerKeyTracker) => {
|
|
113
|
+
const queryKey = queryClient.getQueryData([innerKeyTracker ?? keyTracker]);
|
|
114
|
+
return queryKey;
|
|
115
|
+
};
|
|
116
|
+
const refetchQuery = async (innerKeyTracker) => {
|
|
117
|
+
const queryKey = getQueryKey(innerKeyTracker ?? keyTracker);
|
|
118
|
+
await queryClient.refetchQueries({
|
|
119
|
+
queryKey,
|
|
120
|
+
exact: true,
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
return { refetchQuery, getQueryKey };
|
|
124
124
|
};
|
|
125
125
|
|
|
126
|
-
const useQueryModel = (keyTracker) => {
|
|
127
|
-
const queryClient = useQueryClient();
|
|
128
|
-
const { getQueryKey } = useKeyTrackerModel(keyTracker);
|
|
129
|
-
const queryKey = getQueryKey();
|
|
130
|
-
const config = useQueryConfig();
|
|
131
|
-
const add = (data, position, path) => {
|
|
132
|
-
let records = (findAll(path) ?? []);
|
|
133
|
-
if (!position || position === 'end') {
|
|
134
|
-
records = [...records, data];
|
|
135
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
136
|
-
}
|
|
137
|
-
else if (position === 'start') {
|
|
138
|
-
records = [data, ...records];
|
|
139
|
-
}
|
|
140
|
-
if (!path) {
|
|
141
|
-
queryClient.setQueryData(queryKey, records);
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
const queryData = queryClient.getQueryData(queryKey) ?? {};
|
|
145
|
-
queryClient.setQueryData(queryKey, lodashSet(queryData, path, records));
|
|
146
|
-
}
|
|
147
|
-
return data;
|
|
148
|
-
};
|
|
149
|
-
const findAll = (path) => {
|
|
150
|
-
const data = queryClient.getQueryData(queryKey);
|
|
151
|
-
if (!data) {
|
|
152
|
-
return [];
|
|
153
|
-
}
|
|
154
|
-
if (!path) {
|
|
155
|
-
return Array.isArray(data) ? data : [data];
|
|
156
|
-
}
|
|
157
|
-
return result(data, path, []);
|
|
158
|
-
};
|
|
159
|
-
const findMany = (selector, path) => {
|
|
160
|
-
const data = (findAll(path) ?? []);
|
|
161
|
-
return data.filter(selector);
|
|
162
|
-
};
|
|
163
|
-
const find = (id, path) => {
|
|
164
|
-
const modelConfig = getModelConfig();
|
|
165
|
-
if (!modelConfig?.idColumn) {
|
|
166
|
-
return undefined;
|
|
167
|
-
}
|
|
168
|
-
const data = (findAll(path) ?? []);
|
|
169
|
-
return data.find((record) => record[modelConfig.idColumn] === id);
|
|
170
|
-
};
|
|
171
|
-
const get = (path) => {
|
|
172
|
-
let data = queryClient.getQueryData(queryKey);
|
|
173
|
-
if (path) {
|
|
174
|
-
data = result(data, path);
|
|
175
|
-
}
|
|
176
|
-
return data;
|
|
177
|
-
};
|
|
178
|
-
const set = (newData, path) => {
|
|
179
|
-
if (path) {
|
|
180
|
-
const data = get();
|
|
181
|
-
newData = lodashSet(data, path, newData);
|
|
182
|
-
}
|
|
183
|
-
return queryClient.setQueryData(queryKey, () => newData);
|
|
184
|
-
};
|
|
185
|
-
const getModelConfig = () => {
|
|
186
|
-
const { options } = config;
|
|
187
|
-
const { modelConfig } = options ?? {};
|
|
188
|
-
return modelConfig;
|
|
189
|
-
};
|
|
190
|
-
const update = (id, data, path) => {
|
|
191
|
-
const oldData = (findAll(path) ?? []);
|
|
192
|
-
const modelConfig = getModelConfig();
|
|
193
|
-
if (!modelConfig?.idColumn) {
|
|
194
|
-
return undefined;
|
|
195
|
-
}
|
|
196
|
-
const idColumn = modelConfig.idColumn;
|
|
197
|
-
let updatedRecord = undefined;
|
|
198
|
-
const newData = oldData.map((record) => {
|
|
199
|
-
let dataRecord = record;
|
|
200
|
-
if (dataRecord[idColumn] === id) {
|
|
201
|
-
dataRecord = { ...dataRecord, ...data };
|
|
202
|
-
updatedRecord = dataRecord;
|
|
203
|
-
}
|
|
204
|
-
return dataRecord;
|
|
205
|
-
});
|
|
206
|
-
if (!path) {
|
|
207
|
-
queryClient.setQueryData(queryKey, newData);
|
|
208
|
-
}
|
|
209
|
-
else {
|
|
210
|
-
const queryData = queryClient.getQueryData(queryKey) ?? {};
|
|
211
|
-
queryClient.setQueryData(queryKey, lodashSet(queryData, path, newData));
|
|
212
|
-
}
|
|
213
|
-
return updatedRecord;
|
|
214
|
-
};
|
|
215
|
-
const remove = (id, path) => {
|
|
216
|
-
const oldData = (findAll(path) ?? []);
|
|
217
|
-
const modelConfig = getModelConfig();
|
|
218
|
-
if (!modelConfig?.idColumn) {
|
|
219
|
-
return false;
|
|
220
|
-
}
|
|
221
|
-
const idColumn = modelConfig.idColumn;
|
|
222
|
-
let updated = false;
|
|
223
|
-
const newData = oldData.filter((record) => {
|
|
224
|
-
const dataRecord = record;
|
|
225
|
-
if (dataRecord[idColumn] === id) {
|
|
226
|
-
updated = true;
|
|
227
|
-
return false;
|
|
228
|
-
}
|
|
229
|
-
return true;
|
|
230
|
-
});
|
|
231
|
-
if (!path) {
|
|
232
|
-
queryClient.setQueryData(queryKey, newData);
|
|
233
|
-
}
|
|
234
|
-
else {
|
|
235
|
-
const queryData = queryClient.getQueryData(queryKey) ?? {};
|
|
236
|
-
queryClient.setQueryData(queryKey, lodashSet(queryData, path, newData));
|
|
237
|
-
}
|
|
238
|
-
return updated;
|
|
239
|
-
};
|
|
240
|
-
return { find, findAll, findMany, remove, update, add, get, set };
|
|
126
|
+
const useQueryModel = (keyTracker) => {
|
|
127
|
+
const queryClient = useQueryClient();
|
|
128
|
+
const { getQueryKey } = useKeyTrackerModel(keyTracker);
|
|
129
|
+
const queryKey = getQueryKey();
|
|
130
|
+
const config = useQueryConfig();
|
|
131
|
+
const add = (data, position, path) => {
|
|
132
|
+
let records = (findAll(path) ?? []);
|
|
133
|
+
if (!position || position === 'end') {
|
|
134
|
+
records = [...records, data];
|
|
135
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
136
|
+
}
|
|
137
|
+
else if (position === 'start') {
|
|
138
|
+
records = [data, ...records];
|
|
139
|
+
}
|
|
140
|
+
if (!path) {
|
|
141
|
+
queryClient.setQueryData(queryKey, records);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
const queryData = queryClient.getQueryData(queryKey) ?? {};
|
|
145
|
+
queryClient.setQueryData(queryKey, lodashSet(queryData, path, records));
|
|
146
|
+
}
|
|
147
|
+
return data;
|
|
148
|
+
};
|
|
149
|
+
const findAll = (path) => {
|
|
150
|
+
const data = queryClient.getQueryData(queryKey);
|
|
151
|
+
if (!data) {
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
if (!path) {
|
|
155
|
+
return Array.isArray(data) ? data : [data];
|
|
156
|
+
}
|
|
157
|
+
return result(data, path, []);
|
|
158
|
+
};
|
|
159
|
+
const findMany = (selector, path) => {
|
|
160
|
+
const data = (findAll(path) ?? []);
|
|
161
|
+
return data.filter(selector);
|
|
162
|
+
};
|
|
163
|
+
const find = (id, path) => {
|
|
164
|
+
const modelConfig = getModelConfig();
|
|
165
|
+
if (!modelConfig?.idColumn) {
|
|
166
|
+
return undefined;
|
|
167
|
+
}
|
|
168
|
+
const data = (findAll(path) ?? []);
|
|
169
|
+
return data.find((record) => record[modelConfig.idColumn] === id);
|
|
170
|
+
};
|
|
171
|
+
const get = (path) => {
|
|
172
|
+
let data = queryClient.getQueryData(queryKey);
|
|
173
|
+
if (path) {
|
|
174
|
+
data = result(data, path);
|
|
175
|
+
}
|
|
176
|
+
return data;
|
|
177
|
+
};
|
|
178
|
+
const set = (newData, path) => {
|
|
179
|
+
if (path) {
|
|
180
|
+
const data = get();
|
|
181
|
+
newData = lodashSet(data, path, newData);
|
|
182
|
+
}
|
|
183
|
+
return queryClient.setQueryData(queryKey, () => newData);
|
|
184
|
+
};
|
|
185
|
+
const getModelConfig = () => {
|
|
186
|
+
const { options } = config;
|
|
187
|
+
const { modelConfig } = options ?? {};
|
|
188
|
+
return modelConfig;
|
|
189
|
+
};
|
|
190
|
+
const update = (id, data, path) => {
|
|
191
|
+
const oldData = (findAll(path) ?? []);
|
|
192
|
+
const modelConfig = getModelConfig();
|
|
193
|
+
if (!modelConfig?.idColumn) {
|
|
194
|
+
return undefined;
|
|
195
|
+
}
|
|
196
|
+
const idColumn = modelConfig.idColumn;
|
|
197
|
+
let updatedRecord = undefined;
|
|
198
|
+
const newData = oldData.map((record) => {
|
|
199
|
+
let dataRecord = record;
|
|
200
|
+
if (dataRecord[idColumn] === id) {
|
|
201
|
+
dataRecord = { ...dataRecord, ...data };
|
|
202
|
+
updatedRecord = dataRecord;
|
|
203
|
+
}
|
|
204
|
+
return dataRecord;
|
|
205
|
+
});
|
|
206
|
+
if (!path) {
|
|
207
|
+
queryClient.setQueryData(queryKey, newData);
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
const queryData = queryClient.getQueryData(queryKey) ?? {};
|
|
211
|
+
queryClient.setQueryData(queryKey, lodashSet(queryData, path, newData));
|
|
212
|
+
}
|
|
213
|
+
return updatedRecord;
|
|
214
|
+
};
|
|
215
|
+
const remove = (id, path) => {
|
|
216
|
+
const oldData = (findAll(path) ?? []);
|
|
217
|
+
const modelConfig = getModelConfig();
|
|
218
|
+
if (!modelConfig?.idColumn) {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
const idColumn = modelConfig.idColumn;
|
|
222
|
+
let updated = false;
|
|
223
|
+
const newData = oldData.filter((record) => {
|
|
224
|
+
const dataRecord = record;
|
|
225
|
+
if (dataRecord[idColumn] === id) {
|
|
226
|
+
updated = true;
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
return true;
|
|
230
|
+
});
|
|
231
|
+
if (!path) {
|
|
232
|
+
queryClient.setQueryData(queryKey, newData);
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
const queryData = queryClient.getQueryData(queryKey) ?? {};
|
|
236
|
+
queryClient.setQueryData(queryKey, lodashSet(queryData, path, newData));
|
|
237
|
+
}
|
|
238
|
+
return updated;
|
|
239
|
+
};
|
|
240
|
+
return { find, findAll, findMany, remove, update, add, get, set };
|
|
241
241
|
};
|
|
242
242
|
|
|
243
|
-
const useRefetchQuery = async (queryKey) => {
|
|
244
|
-
const queryClient = useQueryClient();
|
|
245
|
-
const refetchQuery = async (innerQueryKey) => {
|
|
246
|
-
await queryClient.invalidateQueries({
|
|
247
|
-
queryKey: innerQueryKey ?? queryKey,
|
|
248
|
-
exact: true,
|
|
249
|
-
}, { throwOnError: true, cancelRefetch: true });
|
|
250
|
-
return queryClient.getQueriesData({ queryKey: innerQueryKey ?? queryKey });
|
|
251
|
-
};
|
|
252
|
-
return { refetchQuery };
|
|
243
|
+
const useRefetchQuery = async (queryKey) => {
|
|
244
|
+
const queryClient = useQueryClient();
|
|
245
|
+
const refetchQuery = async (innerQueryKey) => {
|
|
246
|
+
await queryClient.invalidateQueries({
|
|
247
|
+
queryKey: innerQueryKey ?? queryKey,
|
|
248
|
+
exact: true,
|
|
249
|
+
}, { throwOnError: true, cancelRefetch: true });
|
|
250
|
+
return queryClient.getQueriesData({ queryKey: innerQueryKey ?? queryKey });
|
|
251
|
+
};
|
|
252
|
+
return { refetchQuery };
|
|
253
253
|
};
|
|
254
254
|
|
|
255
|
-
const axiosInstance = ({ baseURL, timeout, headers }) => {
|
|
256
|
-
return axios.create({
|
|
257
|
-
baseURL,
|
|
258
|
-
timeout,
|
|
259
|
-
headers,
|
|
260
|
-
});
|
|
255
|
+
const axiosInstance = ({ baseURL, timeout, headers }) => {
|
|
256
|
+
return axios.create({
|
|
257
|
+
baseURL,
|
|
258
|
+
timeout,
|
|
259
|
+
headers,
|
|
260
|
+
});
|
|
261
261
|
};
|
|
262
262
|
|
|
263
|
-
const buildFormData = (body) => {
|
|
264
|
-
const formData = new FormData();
|
|
265
|
-
const handleArrayValue = (key, value) => {
|
|
266
|
-
for (const item of value) {
|
|
267
|
-
if (item instanceof File) {
|
|
268
|
-
formData.append(key, item);
|
|
269
|
-
}
|
|
270
|
-
else if (item instanceof Object) {
|
|
271
|
-
formData.append(key, JSON.stringify(item));
|
|
272
|
-
}
|
|
273
|
-
else {
|
|
274
|
-
formData.append(key, item);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
};
|
|
278
|
-
const handleObjectValue = (key, value) => {
|
|
279
|
-
if (value instanceof File) {
|
|
280
|
-
formData.append(key, value);
|
|
281
|
-
}
|
|
282
|
-
else {
|
|
283
|
-
formData.append(key, JSON.stringify(value));
|
|
284
|
-
}
|
|
285
|
-
};
|
|
286
|
-
const handlePrimitiveValue = (key, value) => {
|
|
287
|
-
formData.append(key, value);
|
|
288
|
-
};
|
|
289
|
-
const bodyKeys = Object.keys(body);
|
|
290
|
-
bodyKeys.forEach((key) => {
|
|
291
|
-
const inputValue = body[key];
|
|
292
|
-
if (Array.isArray(inputValue) && inputValue.length > 0) {
|
|
293
|
-
handleArrayValue(key, inputValue);
|
|
294
|
-
}
|
|
295
|
-
else if (inputValue instanceof Object) {
|
|
296
|
-
handleObjectValue(key, inputValue);
|
|
297
|
-
}
|
|
298
|
-
else {
|
|
299
|
-
handlePrimitiveValue(key, inputValue);
|
|
300
|
-
}
|
|
301
|
-
});
|
|
302
|
-
return formData;
|
|
263
|
+
const buildFormData = (body) => {
|
|
264
|
+
const formData = new FormData();
|
|
265
|
+
const handleArrayValue = (key, value) => {
|
|
266
|
+
for (const item of value) {
|
|
267
|
+
if (item instanceof File) {
|
|
268
|
+
formData.append(key, item);
|
|
269
|
+
}
|
|
270
|
+
else if (item instanceof Object) {
|
|
271
|
+
formData.append(key, JSON.stringify(item));
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
formData.append(key, item);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
const handleObjectValue = (key, value) => {
|
|
279
|
+
if (value instanceof File) {
|
|
280
|
+
formData.append(key, value);
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
formData.append(key, JSON.stringify(value));
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
const handlePrimitiveValue = (key, value) => {
|
|
287
|
+
formData.append(key, value);
|
|
288
|
+
};
|
|
289
|
+
const bodyKeys = Object.keys(body);
|
|
290
|
+
bodyKeys.forEach((key) => {
|
|
291
|
+
const inputValue = body[key];
|
|
292
|
+
if (Array.isArray(inputValue) && inputValue.length > 0) {
|
|
293
|
+
handleArrayValue(key, inputValue);
|
|
294
|
+
}
|
|
295
|
+
else if (inputValue instanceof Object) {
|
|
296
|
+
handleObjectValue(key, inputValue);
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
handlePrimitiveValue(key, inputValue);
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
return formData;
|
|
303
303
|
};
|
|
304
304
|
|
|
305
|
-
var HttpMethod;
|
|
306
|
-
(function (HttpMethod) {
|
|
307
|
-
HttpMethod["GET"] = "GET";
|
|
308
|
-
HttpMethod["POST"] = "POST";
|
|
309
|
-
HttpMethod["PUT"] = "PUT";
|
|
310
|
-
HttpMethod["PATCH"] = "PATCH";
|
|
311
|
-
HttpMethod["DELETE"] = "DELETE";
|
|
312
|
-
})(HttpMethod || (HttpMethod = {}));
|
|
313
|
-
var ContentType;
|
|
314
|
-
(function (ContentType) {
|
|
315
|
-
ContentType["APPLICATION_JSON"] = "application/json";
|
|
316
|
-
ContentType["MULTIPART_FORM_DATA"] = "multipart/form-data";
|
|
305
|
+
var HttpMethod;
|
|
306
|
+
(function (HttpMethod) {
|
|
307
|
+
HttpMethod["GET"] = "GET";
|
|
308
|
+
HttpMethod["POST"] = "POST";
|
|
309
|
+
HttpMethod["PUT"] = "PUT";
|
|
310
|
+
HttpMethod["PATCH"] = "PATCH";
|
|
311
|
+
HttpMethod["DELETE"] = "DELETE";
|
|
312
|
+
})(HttpMethod || (HttpMethod = {}));
|
|
313
|
+
var ContentType;
|
|
314
|
+
(function (ContentType) {
|
|
315
|
+
ContentType["APPLICATION_JSON"] = "application/json";
|
|
316
|
+
ContentType["MULTIPART_FORM_DATA"] = "multipart/form-data";
|
|
317
317
|
})(ContentType || (ContentType = {}));
|
|
318
318
|
|
|
319
|
-
//
|
|
320
|
-
const errorTransformer = (data) => {
|
|
321
|
-
return {
|
|
322
|
-
timeStamp: new Date(),
|
|
323
|
-
|
|
324
|
-
data
|
|
325
|
-
|
|
326
|
-
};
|
|
327
|
-
};
|
|
328
|
-
//
|
|
329
|
-
const successTransformer = (data) => {
|
|
330
|
-
return {
|
|
331
|
-
message: data.message ?? 'Request successful',
|
|
332
|
-
statusCode: data.statusCode,
|
|
333
|
-
timeStamp: new Date(),
|
|
334
|
-
status: true,
|
|
335
|
-
data: data.data,
|
|
336
|
-
};
|
|
319
|
+
//
|
|
320
|
+
const errorTransformer = (data) => {
|
|
321
|
+
return {
|
|
322
|
+
timeStamp: new Date(),
|
|
323
|
+
data: data.data,
|
|
324
|
+
...data,
|
|
325
|
+
status: false,
|
|
326
|
+
};
|
|
327
|
+
};
|
|
328
|
+
//
|
|
329
|
+
const successTransformer = (data) => {
|
|
330
|
+
return {
|
|
331
|
+
message: data.message ?? 'Request successful',
|
|
332
|
+
statusCode: data.statusCode,
|
|
333
|
+
timeStamp: new Date(),
|
|
334
|
+
status: true,
|
|
335
|
+
data: data.data,
|
|
336
|
+
};
|
|
337
337
|
};
|
|
338
338
|
|
|
339
|
-
async function makeRequest({ body = {}, method = HttpMethod.GET, path, isFormData, headers = {}, baseURL, timeout, appFileConfig, onUploadProgress, }) {
|
|
340
|
-
// check if file is included in mobile app environment and extract all file input to avoid
|
|
341
|
-
// it being formatted to object using axios formData builder
|
|
342
|
-
const isApp = appFileConfig?.isApp;
|
|
343
|
-
const appFiles = isApp ? getAppFiles(body, appFileConfig.fileSelectors) : {};
|
|
344
|
-
// configure body
|
|
345
|
-
body = (isFormData ? axios.toFormData(body) : body);
|
|
346
|
-
// configure request header1
|
|
347
|
-
configureRequestHeader(isFormData, headers, isApp, appFiles, body);
|
|
348
|
-
try {
|
|
349
|
-
const axiosRequest = axiosInstance({ baseURL, headers, timeout });
|
|
350
|
-
const axiosRequestConfig = {
|
|
351
|
-
url: path,
|
|
352
|
-
method,
|
|
353
|
-
onUploadProgress,
|
|
354
|
-
};
|
|
355
|
-
if (Object.keys(body).length > 0) {
|
|
356
|
-
axiosRequestConfig.data = body;
|
|
357
|
-
}
|
|
358
|
-
// send request
|
|
359
|
-
const resp = await axiosRequest(axiosRequestConfig);
|
|
360
|
-
// get response json
|
|
361
|
-
const jsonResp = await resp.data;
|
|
362
|
-
// get response code
|
|
363
|
-
const responseCode = resp.status;
|
|
364
|
-
if (responseCode > 299) {
|
|
365
|
-
// server returned an error
|
|
366
|
-
return errorTransformer({ ...jsonResp, statusCode: responseCode });
|
|
367
|
-
}
|
|
368
|
-
return successTransformer({
|
|
369
|
-
statusCode: responseCode,
|
|
370
|
-
...jsonResp,
|
|
371
|
-
status: resp.status,
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
catch (error) {
|
|
375
|
-
const errorData = error?.response?.data;
|
|
376
|
-
return errorTransformer({
|
|
377
|
-
statusCode: error.status,
|
|
378
|
-
message: error.message,
|
|
379
|
-
code: error.status || error.statusCode,
|
|
380
|
-
...errorData,
|
|
381
|
-
});
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
const configureRequestHeader = (isFormData, headers, isApp, appFiles, body) => {
|
|
385
|
-
if (!isFormData) {
|
|
386
|
-
headers['Content-Type'] = ContentType.APPLICATION_JSON;
|
|
387
|
-
}
|
|
388
|
-
else if (isApp) {
|
|
389
|
-
headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
|
|
390
|
-
// add the app files
|
|
391
|
-
for (const fileKey in appFiles) {
|
|
392
|
-
const currentFile = appFiles[fileKey];
|
|
393
|
-
if (Array.isArray(currentFile)) {
|
|
394
|
-
for (const innerFile of currentFile) {
|
|
395
|
-
body.append(fileKey, innerFile);
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
else {
|
|
399
|
-
body.append(fileKey, currentFile);
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
else {
|
|
404
|
-
delete headers['Content-Type'];
|
|
405
|
-
}
|
|
406
|
-
};
|
|
407
|
-
function getAppFiles(body, fileSelectors = []) {
|
|
408
|
-
const files = {};
|
|
409
|
-
if (body) {
|
|
410
|
-
if (fileSelectors.length > 0) {
|
|
411
|
-
//
|
|
412
|
-
for (const fileKey of fileSelectors) {
|
|
413
|
-
files[fileKey] = body[fileKey];
|
|
414
|
-
delete body[fileKey];
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
return files;
|
|
339
|
+
async function makeRequest({ body = {}, method = HttpMethod.GET, path, isFormData, headers = {}, baseURL, timeout, appFileConfig, onUploadProgress, }) {
|
|
340
|
+
// check if file is included in mobile app environment and extract all file input to avoid
|
|
341
|
+
// it being formatted to object using axios formData builder
|
|
342
|
+
const isApp = appFileConfig?.isApp;
|
|
343
|
+
const appFiles = isApp ? getAppFiles(body, appFileConfig.fileSelectors) : {};
|
|
344
|
+
// configure body
|
|
345
|
+
body = (isFormData ? axios.toFormData(body) : body);
|
|
346
|
+
// configure request header1
|
|
347
|
+
configureRequestHeader(isFormData, headers, isApp, appFiles, body);
|
|
348
|
+
try {
|
|
349
|
+
const axiosRequest = axiosInstance({ baseURL, headers, timeout });
|
|
350
|
+
const axiosRequestConfig = {
|
|
351
|
+
url: path,
|
|
352
|
+
method,
|
|
353
|
+
onUploadProgress,
|
|
354
|
+
};
|
|
355
|
+
if (Object.keys(body).length > 0 || (isFormData && !isApp && [...body.keys()].length > 0)) {
|
|
356
|
+
axiosRequestConfig.data = body;
|
|
357
|
+
}
|
|
358
|
+
// send request
|
|
359
|
+
const resp = await axiosRequest(axiosRequestConfig);
|
|
360
|
+
// get response json
|
|
361
|
+
const jsonResp = await resp.data;
|
|
362
|
+
// get response code
|
|
363
|
+
const responseCode = resp.status;
|
|
364
|
+
if (responseCode > 299) {
|
|
365
|
+
// server returned an error
|
|
366
|
+
return errorTransformer({ ...jsonResp, statusCode: responseCode });
|
|
367
|
+
}
|
|
368
|
+
return successTransformer({
|
|
369
|
+
statusCode: responseCode,
|
|
370
|
+
...jsonResp,
|
|
371
|
+
status: resp.status,
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
catch (error) {
|
|
375
|
+
const errorData = error?.response?.data;
|
|
376
|
+
return errorTransformer({
|
|
377
|
+
statusCode: error.status,
|
|
378
|
+
message: error.message,
|
|
379
|
+
code: error.status || error.statusCode,
|
|
380
|
+
...errorData,
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
const configureRequestHeader = (isFormData, headers, isApp, appFiles, body) => {
|
|
385
|
+
if (!isFormData) {
|
|
386
|
+
headers['Content-Type'] = ContentType.APPLICATION_JSON;
|
|
387
|
+
}
|
|
388
|
+
else if (isApp) {
|
|
389
|
+
headers['Content-Type'] = ContentType.MULTIPART_FORM_DATA;
|
|
390
|
+
// add the app files
|
|
391
|
+
for (const fileKey in appFiles) {
|
|
392
|
+
const currentFile = appFiles[fileKey];
|
|
393
|
+
if (Array.isArray(currentFile)) {
|
|
394
|
+
for (const innerFile of currentFile) {
|
|
395
|
+
body.append(fileKey, innerFile);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
body.append(fileKey, currentFile);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
delete headers['Content-Type'];
|
|
405
|
+
}
|
|
406
|
+
};
|
|
407
|
+
function getAppFiles(body, fileSelectors = []) {
|
|
408
|
+
const files = {};
|
|
409
|
+
if (body) {
|
|
410
|
+
if (fileSelectors.length > 0) {
|
|
411
|
+
//
|
|
412
|
+
for (const fileKey of fileSelectors) {
|
|
413
|
+
files[fileKey] = body[fileKey];
|
|
414
|
+
delete body[fileKey];
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return files;
|
|
419
419
|
}
|
|
420
420
|
|
|
421
|
-
const useDeleteRequest = (deleteOptions) => {
|
|
422
|
-
const { baseUrl, headers } = deleteOptions ?? {};
|
|
423
|
-
const [requestPath, setRequestPath] = useState('');
|
|
424
|
-
const [options, setOptions] = useState();
|
|
425
|
-
const { options: queryConfigOptions } = useQueryConfig();
|
|
426
|
-
const [requestPayload, setRequestPayload] = useState();
|
|
427
|
-
const isFutureQueriesPaused = usePauseFutureRequests((state) => state.isFutureQueriesPaused);
|
|
428
|
-
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
429
|
-
const globalHeaders = useHeaderStore((state) => state.headers);
|
|
430
|
-
const sendRequest = async (res, rej, queryKey) => {
|
|
431
|
-
const [url] = queryKey;
|
|
432
|
-
const requestUrl = (url ?? requestPath);
|
|
433
|
-
const requestOptions = {
|
|
434
|
-
path: requestUrl,
|
|
435
|
-
headers: { ...globalHeaders, ...headers },
|
|
436
|
-
baseURL: baseUrl ?? API_URL,
|
|
437
|
-
method: HttpMethod.DELETE,
|
|
438
|
-
timeout: TIMEOUT,
|
|
439
|
-
};
|
|
440
|
-
let shouldContinue = true;
|
|
441
|
-
if (queryConfigOptions?.queryMiddleware) {
|
|
442
|
-
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
443
|
-
}
|
|
444
|
-
if (shouldContinue) {
|
|
445
|
-
|
|
446
|
-
if (
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
};
|
|
484
|
-
|
|
485
|
-
if (!isFutureQueriesPaused
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
421
|
+
const useDeleteRequest = (deleteOptions) => {
|
|
422
|
+
const { baseUrl, headers } = deleteOptions ?? {};
|
|
423
|
+
const [requestPath, setRequestPath] = useState('');
|
|
424
|
+
const [options, setOptions] = useState();
|
|
425
|
+
const { options: queryConfigOptions } = useQueryConfig();
|
|
426
|
+
const [requestPayload, setRequestPayload] = useState();
|
|
427
|
+
const isFutureQueriesPaused = usePauseFutureRequests((state) => state.isFutureQueriesPaused);
|
|
428
|
+
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
429
|
+
const globalHeaders = useHeaderStore((state) => state.headers);
|
|
430
|
+
const sendRequest = async (res, rej, queryKey) => {
|
|
431
|
+
const [url] = queryKey;
|
|
432
|
+
const requestUrl = (url ?? requestPath);
|
|
433
|
+
const requestOptions = {
|
|
434
|
+
path: requestUrl,
|
|
435
|
+
headers: { ...globalHeaders, ...headers },
|
|
436
|
+
baseURL: baseUrl ?? API_URL,
|
|
437
|
+
method: HttpMethod.DELETE,
|
|
438
|
+
timeout: TIMEOUT,
|
|
439
|
+
};
|
|
440
|
+
let shouldContinue = true;
|
|
441
|
+
if (queryConfigOptions?.queryMiddleware) {
|
|
442
|
+
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
443
|
+
}
|
|
444
|
+
if (shouldContinue) {
|
|
445
|
+
let deleteResponse;
|
|
446
|
+
if (queryConfigOptions?.middleware) {
|
|
447
|
+
// perform global middleware
|
|
448
|
+
const middlewareResponse = await queryConfigOptions.middleware(async () => await makeRequest(requestOptions), {
|
|
449
|
+
path: requestUrl,
|
|
450
|
+
baseUrl: baseUrl ?? API_URL,
|
|
451
|
+
});
|
|
452
|
+
if (!middlewareResponse) {
|
|
453
|
+
rej();
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
deleteResponse = middlewareResponse;
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
deleteResponse = await makeRequest(requestOptions);
|
|
460
|
+
}
|
|
461
|
+
if (deleteResponse.status) {
|
|
462
|
+
res(deleteResponse);
|
|
463
|
+
}
|
|
464
|
+
else {
|
|
465
|
+
rej(deleteResponse);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
else {
|
|
469
|
+
rej(null);
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
const query = useQuery({
|
|
473
|
+
queryKey: [requestPath, {}],
|
|
474
|
+
queryFn: ({ queryKey }) => new Promise((res, rej) => sendRequest(res, rej, queryKey)),
|
|
475
|
+
enabled: false,
|
|
476
|
+
...options,
|
|
477
|
+
});
|
|
478
|
+
const updatedPathAsync = async (link) => {
|
|
479
|
+
return setRequestPath(link);
|
|
480
|
+
};
|
|
481
|
+
const setOptionsAsync = async (fetchOptions) => {
|
|
482
|
+
return setOptions(fetchOptions);
|
|
483
|
+
};
|
|
484
|
+
const destroy = async (link, internalDeleteOptions) => {
|
|
485
|
+
if (!isFutureQueriesPaused) {
|
|
486
|
+
// set enabled to be true for every delete
|
|
487
|
+
internalDeleteOptions = internalDeleteOptions
|
|
488
|
+
? { ...internalDeleteOptions, queryKey: [link, {}], enabled: true }
|
|
489
|
+
: { queryKey: [link, {}], enabled: true };
|
|
490
|
+
await setOptionsAsync(internalDeleteOptions);
|
|
491
|
+
await updatedPathAsync(link);
|
|
492
|
+
return query.data;
|
|
493
|
+
}
|
|
494
|
+
else {
|
|
495
|
+
setRequestPayload({ link, internalDeleteOptions });
|
|
496
|
+
return undefined;
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
useEffect(() => {
|
|
500
|
+
if (!isFutureQueriesPaused && requestPayload) {
|
|
501
|
+
destroy(requestPayload.link, requestPayload.internalDeleteOptions);
|
|
502
|
+
setRequestPayload(undefined);
|
|
503
|
+
}
|
|
504
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
505
|
+
}, [isFutureQueriesPaused]);
|
|
506
|
+
return { destroy, ...query, isLoading: query.isLoading || isFutureQueriesPaused };
|
|
492
507
|
};
|
|
493
508
|
|
|
494
|
-
const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl, headers, }) => {
|
|
495
|
-
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
496
|
-
const globalHeaders = useHeaderStore((state) => state.headers);
|
|
497
|
-
const [requestPath, setRequestPath] = useState(path);
|
|
498
|
-
const [options, setOptions] = useState(queryOptions);
|
|
499
|
-
const { options: queryConfigOptions } = useQueryConfig();
|
|
500
|
-
const [requestPayload, setRequestPayload] = useState();
|
|
501
|
-
const isFutureQueriesPaused = usePauseFutureRequests((state) => state.isFutureQueriesPaused);
|
|
502
|
-
let queryClient = useQueryClient();
|
|
503
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
504
|
-
queryClient = useMemo(() => queryClient, []);
|
|
505
|
-
const sendRequest = async (res, rej, queryKey, pageParam) => {
|
|
506
|
-
if (load) {
|
|
507
|
-
// get request headers
|
|
508
|
-
const requestOptions = {
|
|
509
|
-
path: pageParam ?? requestPath,
|
|
510
|
-
headers: { ...globalHeaders, ...headers },
|
|
511
|
-
baseURL: baseUrl ?? API_URL,
|
|
512
|
-
timeout: TIMEOUT,
|
|
513
|
-
};
|
|
514
|
-
let shouldContinue = true;
|
|
515
|
-
if (queryConfigOptions?.queryMiddleware) {
|
|
516
|
-
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
517
|
-
}
|
|
518
|
-
if (shouldContinue) {
|
|
519
|
-
|
|
520
|
-
if (
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
const
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
509
|
+
const useGetInfiniteRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl, headers, }) => {
|
|
510
|
+
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
511
|
+
const globalHeaders = useHeaderStore((state) => state.headers);
|
|
512
|
+
const [requestPath, setRequestPath] = useState(path);
|
|
513
|
+
const [options, setOptions] = useState(queryOptions);
|
|
514
|
+
const { options: queryConfigOptions } = useQueryConfig();
|
|
515
|
+
const [requestPayload, setRequestPayload] = useState();
|
|
516
|
+
const isFutureQueriesPaused = usePauseFutureRequests((state) => state.isFutureQueriesPaused);
|
|
517
|
+
let queryClient = useQueryClient();
|
|
518
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
519
|
+
queryClient = useMemo(() => queryClient, []);
|
|
520
|
+
const sendRequest = async (res, rej, queryKey, pageParam) => {
|
|
521
|
+
if (load) {
|
|
522
|
+
// get request headers
|
|
523
|
+
const requestOptions = {
|
|
524
|
+
path: pageParam ?? requestPath,
|
|
525
|
+
headers: { ...globalHeaders, ...headers },
|
|
526
|
+
baseURL: baseUrl ?? API_URL,
|
|
527
|
+
timeout: TIMEOUT,
|
|
528
|
+
};
|
|
529
|
+
let shouldContinue = true;
|
|
530
|
+
if (queryConfigOptions?.queryMiddleware) {
|
|
531
|
+
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
532
|
+
}
|
|
533
|
+
if (shouldContinue) {
|
|
534
|
+
let getResponse;
|
|
535
|
+
if (queryConfigOptions?.middleware) {
|
|
536
|
+
// perform global middleware
|
|
537
|
+
const middlewareResponse = await queryConfigOptions.middleware(async () => await makeRequest(requestOptions), {
|
|
538
|
+
path,
|
|
539
|
+
baseUrl: baseUrl ?? API_URL,
|
|
540
|
+
});
|
|
541
|
+
if (!middlewareResponse) {
|
|
542
|
+
rej();
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
getResponse = middlewareResponse;
|
|
546
|
+
}
|
|
547
|
+
else {
|
|
548
|
+
getResponse = await makeRequest(requestOptions);
|
|
549
|
+
}
|
|
550
|
+
if (getResponse.status) {
|
|
551
|
+
res(getResponse);
|
|
552
|
+
}
|
|
553
|
+
else {
|
|
554
|
+
rej(getResponse);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
else {
|
|
558
|
+
rej(null);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
else {
|
|
562
|
+
res(null);
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
/**
|
|
566
|
+
*
|
|
567
|
+
* This pagination implementation is currently tied to our use case
|
|
568
|
+
*/
|
|
569
|
+
const constructPaginationLink = (direction, lastPage) => {
|
|
570
|
+
const [pathname, queryString] = requestPath.split('?');
|
|
571
|
+
const queryParams = new URLSearchParams(queryString);
|
|
572
|
+
const lastPageItem = lastPage.data.pagination[direction];
|
|
573
|
+
queryParams.set('page', String(lastPageItem));
|
|
574
|
+
return pathname + '?' + queryParams.toString();
|
|
575
|
+
};
|
|
576
|
+
const query = useInfiniteQuery({
|
|
577
|
+
queryKey: [requestPath, {}],
|
|
578
|
+
queryFn: ({ pageParam = requestPath, queryKey }) => new Promise((res, rej) => sendRequest(res, rej, queryKey, pageParam)),
|
|
579
|
+
enabled: load && !isFutureQueriesPaused,
|
|
580
|
+
getNextPageParam: (lastPage) => constructPaginationLink('next_page', lastPage),
|
|
581
|
+
getPreviousPageParam: (lastPage) => constructPaginationLink('previous_page', lastPage),
|
|
582
|
+
...options,
|
|
583
|
+
});
|
|
584
|
+
const setOptionsAsync = async (fetchOptions) => {
|
|
585
|
+
startTransition(() => {
|
|
586
|
+
setOptions(fetchOptions);
|
|
587
|
+
});
|
|
588
|
+
};
|
|
589
|
+
const get = async (link, fetchOptions) => {
|
|
590
|
+
if (!isFutureQueriesPaused) {
|
|
591
|
+
await setOptionsAsync(fetchOptions);
|
|
592
|
+
await updatedPathAsync(link);
|
|
593
|
+
return query.data;
|
|
594
|
+
}
|
|
595
|
+
else {
|
|
596
|
+
setRequestPayload({ link, fetchOptions });
|
|
597
|
+
return undefined;
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
useEffect(() => {
|
|
601
|
+
if (!isFutureQueriesPaused && requestPayload) {
|
|
602
|
+
get(requestPayload.link, requestPayload.fetchOptions);
|
|
603
|
+
setRequestPayload(undefined);
|
|
604
|
+
}
|
|
605
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
606
|
+
}, [isFutureQueriesPaused]);
|
|
607
|
+
const updatedPathAsync = async (link) => {
|
|
608
|
+
startTransition(() => {
|
|
609
|
+
setRequestPath(link);
|
|
610
|
+
});
|
|
611
|
+
};
|
|
612
|
+
useEffect(() => {
|
|
613
|
+
if (keyTracker) {
|
|
614
|
+
// set expiration time for the tracker
|
|
615
|
+
queryClient.setQueryDefaults([keyTracker], {
|
|
616
|
+
staleTime: Infinity,
|
|
617
|
+
});
|
|
618
|
+
queryClient.setQueryData([keyTracker], [requestPath, {}]);
|
|
619
|
+
}
|
|
620
|
+
}, [keyTracker, requestPath, queryClient, queryOptions?.staleTime]);
|
|
621
|
+
return {
|
|
622
|
+
get,
|
|
623
|
+
...query,
|
|
624
|
+
isLoading: query.isLoading || isFutureQueriesPaused,
|
|
625
|
+
};
|
|
596
626
|
};
|
|
597
627
|
|
|
598
|
-
const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl, headers, }) => {
|
|
599
|
-
const [requestPath, setRequestPath] = useState(path);
|
|
600
|
-
const [options, setOptions] = useState(queryOptions);
|
|
601
|
-
const [page, setPage] = useState(1);
|
|
602
|
-
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
603
|
-
const globalHeaders = useHeaderStore((state) => state.headers);
|
|
604
|
-
const { options: queryConfigOptions } = useQueryConfig();
|
|
605
|
-
const [requestPayload, setRequestPayload] = useState();
|
|
606
|
-
const isFutureQueriesPaused = usePauseFutureRequests((state) => state.isFutureQueriesPaused);
|
|
607
|
-
let queryClient = useQueryClient();
|
|
608
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
609
|
-
queryClient = useMemo(() => queryClient, []);
|
|
610
|
-
const sendRequest = async (res, rej, queryKey) => {
|
|
611
|
-
if (load) {
|
|
612
|
-
const [url] = queryKey;
|
|
613
|
-
const requestUrl = (url ?? requestPath);
|
|
614
|
-
const requestOptions = {
|
|
615
|
-
path: requestUrl,
|
|
616
|
-
headers: { ...globalHeaders, ...headers },
|
|
617
|
-
baseURL: baseUrl ?? API_URL,
|
|
618
|
-
timeout: TIMEOUT,
|
|
619
|
-
};
|
|
620
|
-
let shouldContinue = true;
|
|
621
|
-
if (queryConfigOptions?.queryMiddleware) {
|
|
622
|
-
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
623
|
-
}
|
|
624
|
-
if (shouldContinue) {
|
|
625
|
-
|
|
626
|
-
if (
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
};
|
|
692
|
-
const
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
628
|
+
const useGetRequest = ({ path, load = false, queryOptions, keyTracker, baseUrl, headers, }) => {
|
|
629
|
+
const [requestPath, setRequestPath] = useState(path);
|
|
630
|
+
const [options, setOptions] = useState(queryOptions);
|
|
631
|
+
const [page, setPage] = useState(1);
|
|
632
|
+
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
633
|
+
const globalHeaders = useHeaderStore((state) => state.headers);
|
|
634
|
+
const { options: queryConfigOptions } = useQueryConfig();
|
|
635
|
+
const [requestPayload, setRequestPayload] = useState();
|
|
636
|
+
const isFutureQueriesPaused = usePauseFutureRequests((state) => state.isFutureQueriesPaused);
|
|
637
|
+
let queryClient = useQueryClient();
|
|
638
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
639
|
+
queryClient = useMemo(() => queryClient, []);
|
|
640
|
+
const sendRequest = async (res, rej, queryKey) => {
|
|
641
|
+
if (load) {
|
|
642
|
+
const [url] = queryKey;
|
|
643
|
+
const requestUrl = (url ?? requestPath);
|
|
644
|
+
const requestOptions = {
|
|
645
|
+
path: requestUrl,
|
|
646
|
+
headers: { ...globalHeaders, ...headers },
|
|
647
|
+
baseURL: baseUrl ?? API_URL,
|
|
648
|
+
timeout: TIMEOUT,
|
|
649
|
+
};
|
|
650
|
+
let shouldContinue = true;
|
|
651
|
+
if (queryConfigOptions?.queryMiddleware) {
|
|
652
|
+
shouldContinue = await queryConfigOptions.queryMiddleware({ queryKey, ...requestOptions });
|
|
653
|
+
}
|
|
654
|
+
if (shouldContinue) {
|
|
655
|
+
let getResponse;
|
|
656
|
+
if (queryConfigOptions?.middleware) {
|
|
657
|
+
// perform global middleware
|
|
658
|
+
const middlewareResponse = await queryConfigOptions.middleware(async () => await makeRequest(requestOptions), {
|
|
659
|
+
path,
|
|
660
|
+
baseUrl: baseUrl ?? API_URL,
|
|
661
|
+
});
|
|
662
|
+
if (!middlewareResponse) {
|
|
663
|
+
rej();
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
getResponse = middlewareResponse;
|
|
667
|
+
}
|
|
668
|
+
else {
|
|
669
|
+
getResponse = await makeRequest(requestOptions);
|
|
670
|
+
}
|
|
671
|
+
if (getResponse.status) {
|
|
672
|
+
res(getResponse);
|
|
673
|
+
}
|
|
674
|
+
else {
|
|
675
|
+
rej(getResponse);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
else {
|
|
679
|
+
rej(null);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
else {
|
|
683
|
+
res(null);
|
|
684
|
+
}
|
|
685
|
+
};
|
|
686
|
+
const query = useQuery({
|
|
687
|
+
queryKey: [requestPath, {}],
|
|
688
|
+
queryFn: ({ queryKey }) => new Promise((res, rej) => sendRequest(res, rej, queryKey)),
|
|
689
|
+
enabled: load && !isFutureQueriesPaused,
|
|
690
|
+
...options,
|
|
691
|
+
});
|
|
692
|
+
useEffect(() => {
|
|
693
|
+
if (path) {
|
|
694
|
+
setRequestPath(path);
|
|
695
|
+
}
|
|
696
|
+
}, [path]);
|
|
697
|
+
useEffect(() => {
|
|
698
|
+
if (keyTracker) {
|
|
699
|
+
// set expiration time for the tracker
|
|
700
|
+
queryClient.setQueryDefaults([keyTracker], {
|
|
701
|
+
staleTime: Infinity,
|
|
702
|
+
});
|
|
703
|
+
queryClient.setQueryData([keyTracker], [requestPath, {}]);
|
|
704
|
+
}
|
|
705
|
+
}, [keyTracker, requestPath, queryClient, queryOptions?.staleTime]);
|
|
706
|
+
const nextPage = () => {
|
|
707
|
+
if (query.data.data.pagination) {
|
|
708
|
+
const pagination = query.data.data.pagination;
|
|
709
|
+
if (pagination.next_page !== pagination.current_page && pagination.next_page > pagination.current_page) {
|
|
710
|
+
setRequestPath(constructPaginationLink(requestPath, pagination.next_page));
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
};
|
|
714
|
+
const prevPage = () => {
|
|
715
|
+
if (query.data.data.pagination) {
|
|
716
|
+
const pagination = query.data.data.pagination;
|
|
717
|
+
if (pagination.previous_page !== pagination.current_page && pagination.previous_page < pagination.current_page) {
|
|
718
|
+
setRequestPath(constructPaginationLink(requestPath, pagination.previous_page));
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
const constructPaginationLink = (link, pageNumber) => {
|
|
723
|
+
const [pathname, queryString] = link.split('?');
|
|
724
|
+
const queryParams = new URLSearchParams(queryString);
|
|
725
|
+
const oldPage = Number(queryParams.get('page'));
|
|
726
|
+
queryParams.set('page', pageNumber);
|
|
727
|
+
link = pathname + '?' + queryParams.toString();
|
|
728
|
+
// only update page when pagination number changed
|
|
729
|
+
if (oldPage !== pageNumber) {
|
|
730
|
+
setPage(pageNumber);
|
|
731
|
+
}
|
|
732
|
+
return link;
|
|
733
|
+
};
|
|
734
|
+
const gotoPage = (pageNumber) => {
|
|
735
|
+
setRequestPath(constructPaginationLink(requestPath, pageNumber));
|
|
736
|
+
};
|
|
737
|
+
const updatedPathAsync = async (link) => {
|
|
738
|
+
startTransition(() => {
|
|
739
|
+
setRequestPath(link);
|
|
740
|
+
});
|
|
741
|
+
};
|
|
742
|
+
const setOptionsAsync = async (fetchOptions) => {
|
|
743
|
+
startTransition(() => {
|
|
744
|
+
setOptions(fetchOptions);
|
|
745
|
+
});
|
|
746
|
+
};
|
|
747
|
+
const get = async (link, fetchOptions) => {
|
|
748
|
+
if (!isFutureQueriesPaused) {
|
|
749
|
+
await setOptionsAsync(fetchOptions);
|
|
750
|
+
await updatedPathAsync(link);
|
|
751
|
+
return query.data;
|
|
752
|
+
}
|
|
753
|
+
else {
|
|
754
|
+
setRequestPayload({ link, fetchOptions });
|
|
755
|
+
return undefined;
|
|
756
|
+
}
|
|
757
|
+
};
|
|
758
|
+
useEffect(() => {
|
|
759
|
+
if (!isFutureQueriesPaused && requestPayload) {
|
|
760
|
+
get(requestPayload.link, requestPayload.fetchOptions);
|
|
761
|
+
setRequestPayload(undefined);
|
|
762
|
+
}
|
|
763
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
764
|
+
}, [isFutureQueriesPaused]);
|
|
765
|
+
return {
|
|
766
|
+
...query,
|
|
767
|
+
isLoading: query.isLoading || isFutureQueriesPaused,
|
|
768
|
+
setRequestPath,
|
|
769
|
+
nextPage,
|
|
770
|
+
prevPage,
|
|
771
|
+
get,
|
|
772
|
+
gotoPage,
|
|
773
|
+
page,
|
|
774
|
+
queryKey: [requestPath, {}],
|
|
775
|
+
};
|
|
731
776
|
};
|
|
732
777
|
|
|
733
|
-
const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
734
|
-
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
735
|
-
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
736
|
-
const globalHeaders = useHeaderStore((state) => state.headers);
|
|
737
|
-
const [requestPayload, setRequestPayload] = useState();
|
|
738
|
-
const isFutureMutationsPaused = usePauseFutureRequests((state) => state.isFutureMutationsPaused);
|
|
739
|
-
const config = useQueryConfig();
|
|
740
|
-
const sendRequest = async (res, rej, data) => {
|
|
741
|
-
// get request headers
|
|
742
|
-
const requestOptions = {
|
|
743
|
-
path: path,
|
|
744
|
-
body: data,
|
|
745
|
-
method: HttpMethod.PATCH,
|
|
746
|
-
headers: { ...globalHeaders, ...headers },
|
|
747
|
-
baseURL: baseUrl ?? API_URL,
|
|
748
|
-
timeout: TIMEOUT,
|
|
749
|
-
onUploadProgress,
|
|
750
|
-
};
|
|
751
|
-
let shouldContinue = true;
|
|
752
|
-
if (config.options?.mutationMiddleware) {
|
|
753
|
-
shouldContinue = await config.options.mutationMiddleware({
|
|
754
|
-
mutationKey: [path, { type: 'mutation' }],
|
|
755
|
-
...requestOptions,
|
|
756
|
-
});
|
|
757
|
-
}
|
|
758
|
-
if (shouldContinue) {
|
|
759
|
-
|
|
760
|
-
if (
|
|
761
|
-
//
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
}
|
|
794
|
-
};
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
}
|
|
802
|
-
|
|
778
|
+
const usePatchRequest = ({ path, baseUrl, headers }) => {
|
|
779
|
+
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
780
|
+
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
781
|
+
const globalHeaders = useHeaderStore((state) => state.headers);
|
|
782
|
+
const [requestPayload, setRequestPayload] = useState();
|
|
783
|
+
const isFutureMutationsPaused = usePauseFutureRequests((state) => state.isFutureMutationsPaused);
|
|
784
|
+
const config = useQueryConfig();
|
|
785
|
+
const sendRequest = async (res, rej, data) => {
|
|
786
|
+
// get request headers
|
|
787
|
+
const requestOptions = {
|
|
788
|
+
path: path,
|
|
789
|
+
body: data,
|
|
790
|
+
method: HttpMethod.PATCH,
|
|
791
|
+
headers: { ...globalHeaders, ...headers },
|
|
792
|
+
baseURL: baseUrl ?? API_URL,
|
|
793
|
+
timeout: TIMEOUT,
|
|
794
|
+
onUploadProgress,
|
|
795
|
+
};
|
|
796
|
+
let shouldContinue = true;
|
|
797
|
+
if (config.options?.mutationMiddleware) {
|
|
798
|
+
shouldContinue = await config.options.mutationMiddleware({
|
|
799
|
+
mutationKey: [path, { type: 'mutation' }],
|
|
800
|
+
...requestOptions,
|
|
801
|
+
});
|
|
802
|
+
}
|
|
803
|
+
if (shouldContinue) {
|
|
804
|
+
let patchResponse;
|
|
805
|
+
if (config.options?.middleware) {
|
|
806
|
+
// perform global middleware
|
|
807
|
+
const middlewareResponse = await config.options.middleware(async () => await makeRequest(requestOptions), {
|
|
808
|
+
path,
|
|
809
|
+
baseUrl: baseUrl ?? API_URL,
|
|
810
|
+
body: data,
|
|
811
|
+
});
|
|
812
|
+
if (!middlewareResponse) {
|
|
813
|
+
rej();
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
patchResponse = middlewareResponse;
|
|
817
|
+
}
|
|
818
|
+
else {
|
|
819
|
+
patchResponse = await makeRequest(requestOptions);
|
|
820
|
+
}
|
|
821
|
+
if (patchResponse.status) {
|
|
822
|
+
// scroll to top after success
|
|
823
|
+
if (config.options?.context !== 'app') {
|
|
824
|
+
scrollToTop();
|
|
825
|
+
}
|
|
826
|
+
res(patchResponse);
|
|
827
|
+
}
|
|
828
|
+
else {
|
|
829
|
+
// scroll to top after error
|
|
830
|
+
if (config.options?.context !== 'app') {
|
|
831
|
+
scrollToTop();
|
|
832
|
+
}
|
|
833
|
+
rej(patchResponse);
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
else {
|
|
837
|
+
rej(null);
|
|
838
|
+
}
|
|
839
|
+
};
|
|
840
|
+
// register post mutation
|
|
841
|
+
const mutation = useMutation({
|
|
842
|
+
mutationKey: [path, { type: 'mutation' }],
|
|
843
|
+
mutationFn: (dataData) => new Promise((res, rej) => {
|
|
844
|
+
return sendRequest(res, rej, dataData);
|
|
845
|
+
}),
|
|
846
|
+
});
|
|
847
|
+
const patch = async (data, options) => {
|
|
848
|
+
if (!isFutureMutationsPaused) {
|
|
849
|
+
return mutation.mutateAsync(data, options);
|
|
850
|
+
}
|
|
851
|
+
else {
|
|
852
|
+
setRequestPayload({ data, options });
|
|
853
|
+
return undefined;
|
|
854
|
+
}
|
|
855
|
+
};
|
|
856
|
+
useEffect(() => {
|
|
857
|
+
if (!isFutureMutationsPaused && requestPayload) {
|
|
858
|
+
patch(requestPayload.data, requestPayload.options);
|
|
859
|
+
setRequestPayload(undefined);
|
|
860
|
+
}
|
|
861
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
862
|
+
}, [isFutureMutationsPaused]);
|
|
863
|
+
return { patch, uploadProgressPercent, ...mutation, isLoading: mutation.isPending || isFutureMutationsPaused };
|
|
803
864
|
};
|
|
804
865
|
|
|
805
|
-
const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelectors, }) => {
|
|
806
|
-
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
807
|
-
const config = useQueryConfig();
|
|
808
|
-
const globalHeaders = useHeaderStore((state) => state.headers);
|
|
809
|
-
const { isApp } = useReactNativeEnv();
|
|
810
|
-
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
811
|
-
const [requestPayload, setRequestPayload] = useState();
|
|
812
|
-
const isFutureMutationsPaused = usePauseFutureRequests((state) => state.isFutureMutationsPaused);
|
|
813
|
-
const sendRequest = async (res, rej, postData) => {
|
|
814
|
-
// get request headers
|
|
815
|
-
const { data, requestConfig } = postData;
|
|
816
|
-
delete requestConfig?.body;
|
|
817
|
-
const requestOptions = {
|
|
818
|
-
path,
|
|
819
|
-
body: data,
|
|
820
|
-
method: HttpMethod.POST,
|
|
821
|
-
isFormData,
|
|
822
|
-
headers: { ...globalHeaders, ...headers },
|
|
823
|
-
baseURL: baseUrl ?? API_URL,
|
|
824
|
-
timeout: TIMEOUT,
|
|
825
|
-
appFileConfig: {
|
|
826
|
-
isApp,
|
|
827
|
-
fileSelectors,
|
|
828
|
-
},
|
|
829
|
-
onUploadProgress,
|
|
830
|
-
...requestConfig,
|
|
831
|
-
};
|
|
832
|
-
let shouldContinue = true;
|
|
833
|
-
if (config.options?.mutationMiddleware) {
|
|
834
|
-
shouldContinue = await config.options.mutationMiddleware({
|
|
835
|
-
mutationKey: [path, { type: 'mutation' }],
|
|
836
|
-
...requestOptions,
|
|
837
|
-
});
|
|
838
|
-
}
|
|
839
|
-
if (shouldContinue) {
|
|
840
|
-
|
|
841
|
-
if (
|
|
842
|
-
//
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
866
|
+
const usePostRequest = ({ path, isFormData = false, baseUrl, headers, fileSelectors, }) => {
|
|
867
|
+
const { API_URL, TIMEOUT } = useEnvironmentVariables();
|
|
868
|
+
const config = useQueryConfig();
|
|
869
|
+
const globalHeaders = useHeaderStore((state) => state.headers);
|
|
870
|
+
const { isApp } = useReactNativeEnv();
|
|
871
|
+
const { uploadProgressPercent, onUploadProgress } = useUploadProgress();
|
|
872
|
+
const [requestPayload, setRequestPayload] = useState();
|
|
873
|
+
const isFutureMutationsPaused = usePauseFutureRequests((state) => state.isFutureMutationsPaused);
|
|
874
|
+
const sendRequest = async (res, rej, postData) => {
|
|
875
|
+
// get request headers
|
|
876
|
+
const { data, requestConfig } = postData;
|
|
877
|
+
delete requestConfig?.body;
|
|
878
|
+
const requestOptions = {
|
|
879
|
+
path,
|
|
880
|
+
body: data,
|
|
881
|
+
method: HttpMethod.POST,
|
|
882
|
+
isFormData,
|
|
883
|
+
headers: { ...globalHeaders, ...headers },
|
|
884
|
+
baseURL: baseUrl ?? API_URL,
|
|
885
|
+
timeout: TIMEOUT,
|
|
886
|
+
appFileConfig: {
|
|
887
|
+
isApp,
|
|
888
|
+
fileSelectors,
|
|
889
|
+
},
|
|
890
|
+
onUploadProgress,
|
|
891
|
+
...requestConfig,
|
|
892
|
+
};
|
|
893
|
+
let shouldContinue = true;
|
|
894
|
+
if (config.options?.mutationMiddleware) {
|
|
895
|
+
shouldContinue = await config.options.mutationMiddleware({
|
|
896
|
+
mutationKey: [path, { type: 'mutation' }],
|
|
897
|
+
...requestOptions,
|
|
898
|
+
});
|
|
899
|
+
}
|
|
900
|
+
if (shouldContinue) {
|
|
901
|
+
let postResponse;
|
|
902
|
+
if (config.options?.middleware) {
|
|
903
|
+
// perform global middleware
|
|
904
|
+
const middlewareResponse = await config.options.middleware(async () => await makeRequest(requestOptions), {
|
|
905
|
+
path,
|
|
906
|
+
baseUrl: baseUrl ?? API_URL,
|
|
907
|
+
body: data,
|
|
908
|
+
});
|
|
909
|
+
if (!middlewareResponse) {
|
|
910
|
+
rej();
|
|
911
|
+
return;
|
|
912
|
+
}
|
|
913
|
+
postResponse = middlewareResponse;
|
|
914
|
+
}
|
|
915
|
+
else {
|
|
916
|
+
postResponse = await makeRequest(requestOptions);
|
|
917
|
+
}
|
|
918
|
+
if (postResponse.status) {
|
|
919
|
+
// scroll to top after success
|
|
920
|
+
if (config.options?.context !== 'app') {
|
|
921
|
+
scrollToTop();
|
|
922
|
+
}
|
|
923
|
+
res(postResponse);
|
|
924
|
+
}
|
|
925
|
+
else {
|
|
926
|
+
// scroll to top after error
|
|
927
|
+
if (config.options?.context !== 'app') {
|
|
928
|
+
scrollToTop();
|
|
929
|
+
}
|
|
930
|
+
rej(postResponse);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
else {
|
|
934
|
+
rej(null);
|
|
935
|
+
}
|
|
936
|
+
};
|
|
937
|
+
// register post mutation
|
|
938
|
+
const mutation = useMutation({
|
|
939
|
+
mutationKey: [path, { type: 'mutation' }],
|
|
940
|
+
mutationFn: async (postData) => new Promise((res, rej) => sendRequest(res, rej, postData)),
|
|
941
|
+
});
|
|
942
|
+
const post = async (data, options) => {
|
|
943
|
+
if (!isFutureMutationsPaused) {
|
|
944
|
+
const { requestConfig, ...otherOptions } = options ?? {};
|
|
945
|
+
return mutation.mutateAsync({ data, requestConfig }, otherOptions);
|
|
946
|
+
}
|
|
947
|
+
else {
|
|
948
|
+
setRequestPayload({ data, options });
|
|
949
|
+
return undefined;
|
|
950
|
+
}
|
|
951
|
+
};
|
|
952
|
+
useEffect(() => {
|
|
953
|
+
if (!isFutureMutationsPaused && requestPayload) {
|
|
954
|
+
post(requestPayload.data, requestPayload.options);
|
|
955
|
+
setRequestPayload(undefined);
|
|
956
|
+
}
|
|
957
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
958
|
+
}, [isFutureMutationsPaused]);
|
|
959
|
+
return { post, uploadProgressPercent, ...mutation, isLoading: mutation.isPending || isFutureMutationsPaused };
|
|
883
960
|
};
|
|
884
961
|
|
|
885
962
|
export { ContentType, HttpMethod, axiosInstance, bootstrapQueryRequest, buildFormData, errorTransformer, getDateInFuture, makeRequest, scrollToTop, successTransformer, useDeleteRequest, useEnvironmentVariables, useGetInfiniteRequest, useGetRequest, useHeaderStore, useKeyTrackerModel, usePatchRequest, usePauseFutureRequests, usePostRequest, useQueryConfig, useQueryHeaders, useQueryModel, useReactNativeEnv, useRefetchQuery, useUploadProgress };
|