@molopos/shared 2.0.8 → 2.0.10

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.
@@ -5,17 +5,21 @@ describe("Utils", () => {
5
5
  test("fromCents", () => {
6
6
  const result = (0, index_1.fromCents)(100);
7
7
  expect(result).toEqual(1);
8
+ expect(result).not.toBeNull();
8
9
  });
9
10
  test("toCents", () => {
10
11
  const result = (0, index_1.toCents)(1);
11
12
  expect(result).toEqual(100);
13
+ expect(result).not.toBeNull();
12
14
  });
13
15
  test("calculVat", () => {
14
16
  const result = (0, index_1.calculVat)({ value: 100, percentage: 20 });
15
17
  expect(result).toEqual(20);
18
+ expect(result).not.toBeNull();
16
19
  });
17
20
  test("calculDiscount", () => {
18
21
  const result = (0, index_1.calculDiscount)({ value: 100, percentage: 20 });
19
22
  expect(result).toEqual(20);
23
+ expect(result).not.toBeNull();
20
24
  });
21
25
  });
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@molopos/shared","publishConfig":{"access":"public"},"version":"2.0.8","description":"Shared between backend and frontend repos","license":"ISC","repository":{"type":"git","url":"https://github.com/unicubate/molopos-shared.git"},"dependencies":{"@tanstack/react-query":"^5.100.7","date-fns":"^4.1.0","luxon":"^3.7.2"}}
1
+ {"name":"@molopos/shared","publishConfig":{"access":"public"},"version":"2.0.10","description":"Shared between backend and frontend repos","license":"ISC","repository":{"type":"git","url":"https://github.com/unicubate/molopos-shared.git"},"dependencies":{"date-fns":"^4.1.0","luxon":"^3.7.2"}}
@@ -1,502 +0,0 @@
1
- import { MutationOptions, QueryFunction, QueryKey, QueryObserverOptions } from "@tanstack/react-query";
2
- import { SortOrderEnum } from "../enum";
3
- type IPaginationRes<T> = {
4
- total: number;
5
- per_page: number;
6
- current_page: number;
7
- previous_page: number;
8
- next_page: number;
9
- last_page: number;
10
- sort: SortOrderEnum;
11
- total_page: number;
12
- total_value: number;
13
- value: T[];
14
- };
15
- type TInfiniteQueryResult<T> = {
16
- data: IPaginationRes<T>;
17
- pages: {
18
- data: IPaginationRes<T>;
19
- }[];
20
- pageParams: number[];
21
- };
22
- /**
23
- * @description Use infinite query handlers
24
- * @param queryKey - Query key
25
- * @param initialPageParam - Initial page param
26
- * @param queryFn - Query function
27
- * @returns {data: T[], ...rest} - Data and rest of the query result
28
- *
29
- * @example
30
- * const { data, isLoading, isError, error } = useInfiniteQueryHandlers({
31
- * queryKey: ['queryKey'],
32
- * initialPageParam: 1,
33
- * queryFn: async () => {
34
- * return await axios.get('/api/v1/data');
35
- * },
36
- * });
37
- */
38
- export declare function useInfiniteQueryHandlers<T>({ queryFn, queryKey, initialPageParam, }: {
39
- queryKey: QueryKey;
40
- initialPageParam?: number;
41
- queryFn: QueryFunction;
42
- }): {
43
- data: {
44
- totalValue: number;
45
- nextPage: number;
46
- totalPage: number;
47
- valueCount: number;
48
- value: T[];
49
- };
50
- error: Error;
51
- isError: true;
52
- isPending: false;
53
- isLoading: false;
54
- isLoadingError: false;
55
- isRefetchError: true;
56
- isSuccess: false;
57
- isPlaceholderData: false;
58
- status: "error";
59
- fetchNextPage: (options?: import("@tanstack/react-query").FetchNextPageOptions) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
60
- fetchPreviousPage: (options?: import("@tanstack/react-query").FetchPreviousPageOptions) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
61
- hasNextPage: boolean;
62
- hasPreviousPage: boolean;
63
- isFetchNextPageError: boolean;
64
- isFetchingNextPage: boolean;
65
- isFetchPreviousPageError: boolean;
66
- isFetchingPreviousPage: boolean;
67
- dataUpdatedAt: number;
68
- errorUpdatedAt: number;
69
- failureCount: number;
70
- failureReason: Error;
71
- errorUpdateCount: number;
72
- isFetched: boolean;
73
- isFetchedAfterMount: boolean;
74
- isFetching: boolean;
75
- isInitialLoading: boolean;
76
- isPaused: boolean;
77
- isRefetching: boolean;
78
- isStale: boolean;
79
- isEnabled: boolean;
80
- refetch: (options?: import("@tanstack/react-query").RefetchOptions) => Promise<import("@tanstack/react-query").QueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
81
- fetchStatus: import("@tanstack/react-query").FetchStatus;
82
- promise: Promise<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>>;
83
- } | {
84
- data: {
85
- totalValue: number;
86
- nextPage: number;
87
- totalPage: number;
88
- valueCount: number;
89
- value: T[];
90
- };
91
- error: null;
92
- isError: false;
93
- isPending: false;
94
- isLoading: false;
95
- isLoadingError: false;
96
- isRefetchError: false;
97
- isFetchNextPageError: false;
98
- isFetchPreviousPageError: false;
99
- isSuccess: true;
100
- isPlaceholderData: false;
101
- status: "success";
102
- fetchNextPage: (options?: import("@tanstack/react-query").FetchNextPageOptions) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
103
- fetchPreviousPage: (options?: import("@tanstack/react-query").FetchPreviousPageOptions) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
104
- hasNextPage: boolean;
105
- hasPreviousPage: boolean;
106
- isFetchingNextPage: boolean;
107
- isFetchingPreviousPage: boolean;
108
- dataUpdatedAt: number;
109
- errorUpdatedAt: number;
110
- failureCount: number;
111
- failureReason: Error;
112
- errorUpdateCount: number;
113
- isFetched: boolean;
114
- isFetchedAfterMount: boolean;
115
- isFetching: boolean;
116
- isInitialLoading: boolean;
117
- isPaused: boolean;
118
- isRefetching: boolean;
119
- isStale: boolean;
120
- isEnabled: boolean;
121
- refetch: (options?: import("@tanstack/react-query").RefetchOptions) => Promise<import("@tanstack/react-query").QueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
122
- fetchStatus: import("@tanstack/react-query").FetchStatus;
123
- promise: Promise<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>>;
124
- } | {
125
- data: {
126
- totalValue: number;
127
- nextPage: number;
128
- totalPage: number;
129
- valueCount: number;
130
- value: T[];
131
- };
132
- error: Error;
133
- isError: true;
134
- isPending: false;
135
- isLoading: false;
136
- isLoadingError: true;
137
- isRefetchError: false;
138
- isFetchNextPageError: false;
139
- isFetchPreviousPageError: false;
140
- isSuccess: false;
141
- isPlaceholderData: false;
142
- status: "error";
143
- fetchNextPage: (options?: import("@tanstack/react-query").FetchNextPageOptions) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
144
- fetchPreviousPage: (options?: import("@tanstack/react-query").FetchPreviousPageOptions) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
145
- hasNextPage: boolean;
146
- hasPreviousPage: boolean;
147
- isFetchingNextPage: boolean;
148
- isFetchingPreviousPage: boolean;
149
- dataUpdatedAt: number;
150
- errorUpdatedAt: number;
151
- failureCount: number;
152
- failureReason: Error;
153
- errorUpdateCount: number;
154
- isFetched: boolean;
155
- isFetchedAfterMount: boolean;
156
- isFetching: boolean;
157
- isInitialLoading: boolean;
158
- isPaused: boolean;
159
- isRefetching: boolean;
160
- isStale: boolean;
161
- isEnabled: boolean;
162
- refetch: (options?: import("@tanstack/react-query").RefetchOptions) => Promise<import("@tanstack/react-query").QueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
163
- fetchStatus: import("@tanstack/react-query").FetchStatus;
164
- promise: Promise<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>>;
165
- } | {
166
- data: {
167
- totalValue: number;
168
- nextPage: number;
169
- totalPage: number;
170
- valueCount: number;
171
- value: T[];
172
- };
173
- error: null;
174
- isError: false;
175
- isPending: true;
176
- isLoading: true;
177
- isLoadingError: false;
178
- isRefetchError: false;
179
- isFetchNextPageError: false;
180
- isFetchPreviousPageError: false;
181
- isSuccess: false;
182
- isPlaceholderData: false;
183
- status: "pending";
184
- fetchNextPage: (options?: import("@tanstack/react-query").FetchNextPageOptions) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
185
- fetchPreviousPage: (options?: import("@tanstack/react-query").FetchPreviousPageOptions) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
186
- hasNextPage: boolean;
187
- hasPreviousPage: boolean;
188
- isFetchingNextPage: boolean;
189
- isFetchingPreviousPage: boolean;
190
- dataUpdatedAt: number;
191
- errorUpdatedAt: number;
192
- failureCount: number;
193
- failureReason: Error;
194
- errorUpdateCount: number;
195
- isFetched: boolean;
196
- isFetchedAfterMount: boolean;
197
- isFetching: boolean;
198
- isInitialLoading: boolean;
199
- isPaused: boolean;
200
- isRefetching: boolean;
201
- isStale: boolean;
202
- isEnabled: boolean;
203
- refetch: (options?: import("@tanstack/react-query").RefetchOptions) => Promise<import("@tanstack/react-query").QueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
204
- fetchStatus: import("@tanstack/react-query").FetchStatus;
205
- promise: Promise<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>>;
206
- } | {
207
- data: {
208
- totalValue: number;
209
- nextPage: number;
210
- totalPage: number;
211
- valueCount: number;
212
- value: T[];
213
- };
214
- error: null;
215
- isError: false;
216
- isPending: true;
217
- isLoadingError: false;
218
- isRefetchError: false;
219
- isFetchNextPageError: false;
220
- isFetchPreviousPageError: false;
221
- isSuccess: false;
222
- isPlaceholderData: false;
223
- status: "pending";
224
- fetchNextPage: (options?: import("@tanstack/react-query").FetchNextPageOptions) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
225
- fetchPreviousPage: (options?: import("@tanstack/react-query").FetchPreviousPageOptions) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
226
- hasNextPage: boolean;
227
- hasPreviousPage: boolean;
228
- isFetchingNextPage: boolean;
229
- isFetchingPreviousPage: boolean;
230
- dataUpdatedAt: number;
231
- errorUpdatedAt: number;
232
- failureCount: number;
233
- failureReason: Error;
234
- errorUpdateCount: number;
235
- isFetched: boolean;
236
- isFetchedAfterMount: boolean;
237
- isFetching: boolean;
238
- isLoading: boolean;
239
- isInitialLoading: boolean;
240
- isPaused: boolean;
241
- isRefetching: boolean;
242
- isStale: boolean;
243
- isEnabled: boolean;
244
- refetch: (options?: import("@tanstack/react-query").RefetchOptions) => Promise<import("@tanstack/react-query").QueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
245
- fetchStatus: import("@tanstack/react-query").FetchStatus;
246
- promise: Promise<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>>;
247
- } | {
248
- data: {
249
- totalValue: number;
250
- nextPage: number;
251
- totalPage: number;
252
- valueCount: number;
253
- value: T[];
254
- };
255
- isError: false;
256
- error: null;
257
- isPending: false;
258
- isLoading: false;
259
- isLoadingError: false;
260
- isRefetchError: false;
261
- isSuccess: true;
262
- isPlaceholderData: true;
263
- isFetchNextPageError: false;
264
- isFetchPreviousPageError: false;
265
- status: "success";
266
- fetchNextPage: (options?: import("@tanstack/react-query").FetchNextPageOptions) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
267
- fetchPreviousPage: (options?: import("@tanstack/react-query").FetchPreviousPageOptions) => Promise<import("@tanstack/react-query").InfiniteQueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
268
- hasNextPage: boolean;
269
- hasPreviousPage: boolean;
270
- isFetchingNextPage: boolean;
271
- isFetchingPreviousPage: boolean;
272
- dataUpdatedAt: number;
273
- errorUpdatedAt: number;
274
- failureCount: number;
275
- failureReason: Error;
276
- errorUpdateCount: number;
277
- isFetched: boolean;
278
- isFetchedAfterMount: boolean;
279
- isFetching: boolean;
280
- isInitialLoading: boolean;
281
- isPaused: boolean;
282
- isRefetching: boolean;
283
- isStale: boolean;
284
- isEnabled: boolean;
285
- refetch: (options?: import("@tanstack/react-query").RefetchOptions) => Promise<import("@tanstack/react-query").QueryObserverResult<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>, Error>>;
286
- fetchStatus: import("@tanstack/react-query").FetchStatus;
287
- promise: Promise<import("@tanstack/react-query").InfiniteData<TInfiniteQueryResult<T>, unknown>>;
288
- };
289
- /**
290
- * @description Use query handlers
291
- * @param queryKey - Query key
292
- * @param queryFn - Query function
293
- * @param options - Query options
294
- * @returns {data: T, ...rest} - Data and rest of the query result
295
- *
296
- * @example
297
- * const { data, isLoading, isError, error } = useQueryHandler({
298
- * queryKey: ['queryKey'],
299
- * queryFn: async () => {
300
- * return await axios.get('/api/v1/data');
301
- * },
302
- * });
303
- */
304
- export declare function useQueryHandler<T>({ queryFn, queryKey, ...options }: {
305
- queryKey: QueryKey;
306
- queryFn: QueryFunction;
307
- } & QueryObserverOptions): {
308
- data: T;
309
- error: Error;
310
- isError: true;
311
- isPending: false;
312
- isLoading: false;
313
- isLoadingError: false;
314
- isRefetchError: true;
315
- isSuccess: false;
316
- isPlaceholderData: false;
317
- status: "error";
318
- dataUpdatedAt: number;
319
- errorUpdatedAt: number;
320
- failureCount: number;
321
- failureReason: Error;
322
- errorUpdateCount: number;
323
- isFetched: boolean;
324
- isFetchedAfterMount: boolean;
325
- isFetching: boolean;
326
- isInitialLoading: boolean;
327
- isPaused: boolean;
328
- isRefetching: boolean;
329
- isStale: boolean;
330
- isEnabled: boolean;
331
- refetch: (options?: import("@tanstack/react-query").RefetchOptions) => Promise<import("@tanstack/react-query").QueryObserverResult<unknown, Error>>;
332
- fetchStatus: import("@tanstack/react-query").FetchStatus;
333
- promise: Promise<unknown>;
334
- } | {
335
- data: T;
336
- error: null;
337
- isError: false;
338
- isPending: false;
339
- isLoading: false;
340
- isLoadingError: false;
341
- isRefetchError: false;
342
- isSuccess: true;
343
- isPlaceholderData: false;
344
- status: "success";
345
- dataUpdatedAt: number;
346
- errorUpdatedAt: number;
347
- failureCount: number;
348
- failureReason: Error;
349
- errorUpdateCount: number;
350
- isFetched: boolean;
351
- isFetchedAfterMount: boolean;
352
- isFetching: boolean;
353
- isInitialLoading: boolean;
354
- isPaused: boolean;
355
- isRefetching: boolean;
356
- isStale: boolean;
357
- isEnabled: boolean;
358
- refetch: (options?: import("@tanstack/react-query").RefetchOptions) => Promise<import("@tanstack/react-query").QueryObserverResult<unknown, Error>>;
359
- fetchStatus: import("@tanstack/react-query").FetchStatus;
360
- promise: Promise<unknown>;
361
- } | {
362
- data: T;
363
- error: Error;
364
- isError: true;
365
- isPending: false;
366
- isLoading: false;
367
- isLoadingError: true;
368
- isRefetchError: false;
369
- isSuccess: false;
370
- isPlaceholderData: false;
371
- status: "error";
372
- dataUpdatedAt: number;
373
- errorUpdatedAt: number;
374
- failureCount: number;
375
- failureReason: Error;
376
- errorUpdateCount: number;
377
- isFetched: boolean;
378
- isFetchedAfterMount: boolean;
379
- isFetching: boolean;
380
- isInitialLoading: boolean;
381
- isPaused: boolean;
382
- isRefetching: boolean;
383
- isStale: boolean;
384
- isEnabled: boolean;
385
- refetch: (options?: import("@tanstack/react-query").RefetchOptions) => Promise<import("@tanstack/react-query").QueryObserverResult<unknown, Error>>;
386
- fetchStatus: import("@tanstack/react-query").FetchStatus;
387
- promise: Promise<unknown>;
388
- } | {
389
- data: T;
390
- error: null;
391
- isError: false;
392
- isPending: true;
393
- isLoading: true;
394
- isLoadingError: false;
395
- isRefetchError: false;
396
- isSuccess: false;
397
- isPlaceholderData: false;
398
- status: "pending";
399
- dataUpdatedAt: number;
400
- errorUpdatedAt: number;
401
- failureCount: number;
402
- failureReason: Error;
403
- errorUpdateCount: number;
404
- isFetched: boolean;
405
- isFetchedAfterMount: boolean;
406
- isFetching: boolean;
407
- isInitialLoading: boolean;
408
- isPaused: boolean;
409
- isRefetching: boolean;
410
- isStale: boolean;
411
- isEnabled: boolean;
412
- refetch: (options?: import("@tanstack/react-query").RefetchOptions) => Promise<import("@tanstack/react-query").QueryObserverResult<unknown, Error>>;
413
- fetchStatus: import("@tanstack/react-query").FetchStatus;
414
- promise: Promise<unknown>;
415
- } | {
416
- data: T;
417
- error: null;
418
- isError: false;
419
- isPending: true;
420
- isLoadingError: false;
421
- isRefetchError: false;
422
- isSuccess: false;
423
- isPlaceholderData: false;
424
- status: "pending";
425
- dataUpdatedAt: number;
426
- errorUpdatedAt: number;
427
- failureCount: number;
428
- failureReason: Error;
429
- errorUpdateCount: number;
430
- isFetched: boolean;
431
- isFetchedAfterMount: boolean;
432
- isFetching: boolean;
433
- isLoading: boolean;
434
- isInitialLoading: boolean;
435
- isPaused: boolean;
436
- isRefetching: boolean;
437
- isStale: boolean;
438
- isEnabled: boolean;
439
- refetch: (options?: import("@tanstack/react-query").RefetchOptions) => Promise<import("@tanstack/react-query").QueryObserverResult<unknown, Error>>;
440
- fetchStatus: import("@tanstack/react-query").FetchStatus;
441
- promise: Promise<unknown>;
442
- } | {
443
- data: T;
444
- isError: false;
445
- error: null;
446
- isPending: false;
447
- isLoading: false;
448
- isLoadingError: false;
449
- isRefetchError: false;
450
- isSuccess: true;
451
- isPlaceholderData: true;
452
- status: "success";
453
- dataUpdatedAt: number;
454
- errorUpdatedAt: number;
455
- failureCount: number;
456
- failureReason: Error;
457
- errorUpdateCount: number;
458
- isFetched: boolean;
459
- isFetchedAfterMount: boolean;
460
- isFetching: boolean;
461
- isInitialLoading: boolean;
462
- isPaused: boolean;
463
- isRefetching: boolean;
464
- isStale: boolean;
465
- isEnabled: boolean;
466
- refetch: (options?: import("@tanstack/react-query").RefetchOptions) => Promise<import("@tanstack/react-query").QueryObserverResult<unknown, Error>>;
467
- fetchStatus: import("@tanstack/react-query").FetchStatus;
468
- promise: Promise<unknown>;
469
- };
470
- /**
471
- * @description Use mutation handlers
472
- * @param queryKeys - Query keys
473
- * @param onSuccess - On success callback
474
- * @param onError - On error callback
475
- * @returns {handleError, handleSettled, handleSuccess} - Error, settled, and success handlers
476
- */
477
- type MutationHandlerOptions<TVariables> = {
478
- queryKeys: QueryKey;
479
- mutationFn: (variables: TVariables) => Promise<any>;
480
- onSuccess?: () => void;
481
- onError?: (error: any) => void;
482
- };
483
- /**
484
- * @description Generic reusable mutation hook
485
- * @param queryKeys - Query keys to invalidate on settled
486
- * @param axiosConfig - Function receiving variables and returning a makeAxios config
487
- * @param mutationFn - Custom async mutation function (use for complex logic)
488
- * @param onSuccess - On success callback
489
- * @param onError - On error callback
490
- * @returns useMutation result
491
- *
492
- * @example
493
- * // With mutationFn (for conditional logic)
494
- * const { mutate } = useMutationHandler<TVariables>({
495
- * queryKeys: ['queryKey'],
496
- * mutationFn: async (variables: TVariables) => {
497
- * return await axios.post('/api/v1/data', variables);
498
- * },
499
- * });
500
- */
501
- export declare function useMutationHandler<TVariables = void>({ mutationFn, onSuccess, onError, queryKeys, ...options }: MutationHandlerOptions<TVariables> & Omit<MutationOptions<any, any, TVariables>, "mutationFn">): import("@tanstack/react-query").UseMutationResult<any, any, TVariables, unknown>;
502
- export {};
@@ -1,153 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __rest = (this && this.__rest) || function (s, e) {
12
- var t = {};
13
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
- t[p] = s[p];
15
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
- t[p[i]] = s[p[i]];
19
- }
20
- return t;
21
- };
22
- Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.useInfiniteQueryHandlers = useInfiniteQueryHandlers;
24
- exports.useQueryHandler = useQueryHandler;
25
- exports.useMutationHandler = useMutationHandler;
26
- const react_query_1 = require("@tanstack/react-query");
27
- /**
28
- * @description Use infinite query handlers
29
- * @param queryKey - Query key
30
- * @param initialPageParam - Initial page param
31
- * @param queryFn - Query function
32
- * @returns {data: T[], ...rest} - Data and rest of the query result
33
- *
34
- * @example
35
- * const { data, isLoading, isError, error } = useInfiniteQueryHandlers({
36
- * queryKey: ['queryKey'],
37
- * initialPageParam: 1,
38
- * queryFn: async () => {
39
- * return await axios.get('/api/v1/data');
40
- * },
41
- * });
42
- */
43
- function useInfiniteQueryHandlers({ queryFn, queryKey, initialPageParam = 1, }) {
44
- var _a;
45
- const _b = (0, react_query_1.useInfiniteQuery)({
46
- queryKey: queryKey,
47
- initialPageParam: initialPageParam,
48
- getNextPageParam: (lastPage) => { var _a; return (_a = lastPage.data.next_page) !== null && _a !== void 0 ? _a : undefined; },
49
- getPreviousPageParam: (firstPage) => { var _a; return (_a = firstPage.data.previous_page) !== null && _a !== void 0 ? _a : undefined; },
50
- queryFn: queryFn,
51
- }), { data } = _b, rest = __rest(_b, ["data"]);
52
- const preValue = (_a = data === null || data === void 0 ? void 0 : data.pages[0]) === null || _a === void 0 ? void 0 : _a.data;
53
- return Object.assign(Object.assign({}, rest), { data: {
54
- totalValue: preValue === null || preValue === void 0 ? void 0 : preValue.total,
55
- nextPage: preValue === null || preValue === void 0 ? void 0 : preValue.next_page,
56
- totalPage: preValue === null || preValue === void 0 ? void 0 : preValue.total_page,
57
- valueCount: preValue === null || preValue === void 0 ? void 0 : preValue.total_value,
58
- value: data === null || data === void 0 ? void 0 : data.pages.flatMap((page) => { var _a; return (_a = page === null || page === void 0 ? void 0 : page.data) === null || _a === void 0 ? void 0 : _a.value; }),
59
- } });
60
- }
61
- /**
62
- * @description Use query handlers
63
- * @param queryKey - Query key
64
- * @param queryFn - Query function
65
- * @param options - Query options
66
- * @returns {data: T, ...rest} - Data and rest of the query result
67
- *
68
- * @example
69
- * const { data, isLoading, isError, error } = useQueryHandler({
70
- * queryKey: ['queryKey'],
71
- * queryFn: async () => {
72
- * return await axios.get('/api/v1/data');
73
- * },
74
- * });
75
- */
76
- function useQueryHandler(_a) {
77
- var { queryFn, queryKey } = _a, options = __rest(_a, ["queryFn", "queryKey"]);
78
- const _b = (0, react_query_1.useQuery)(Object.assign({ queryKey,
79
- queryFn }, options)), { data } = _b, rest = __rest(_b, ["data"]);
80
- return Object.assign(Object.assign({}, rest), { data: data === null || data === void 0 ? void 0 : data.data });
81
- }
82
- /**
83
- * @description Use mutation handlers
84
- * @param queryKeys - Query keys
85
- * @param onSuccess - On success callback
86
- * @param onError - On error callback
87
- * @returns {handleError, handleSettled, handleSuccess} - Error, settled, and success handlers
88
- *
89
- * @example
90
- * const { handleError, handleSettled, handleSuccess } = invalidateQueries({
91
- * queryKeys: ['queryKey'],
92
- * onSuccess: () => {
93
- * console.log('success');
94
- * },
95
- * onError: (error) => {
96
- * console.log(error);
97
- * },
98
- * });
99
- */
100
- function invalidateQueries({ queryKeys, onSuccess, onError, }) {
101
- const queryClient = (0, react_query_1.useQueryClient)();
102
- const handleError = (error) => __awaiter(this, void 0, void 0, function* () {
103
- if (onError) {
104
- onError(error);
105
- }
106
- });
107
- const handleSettled = () => __awaiter(this, void 0, void 0, function* () {
108
- // Invalidate all queries matching the prefixes
109
- for (const key of queryKeys) {
110
- yield queryClient.invalidateQueries({
111
- queryKey: [key],
112
- refetchType: "active", // Only refetch queries that are currently active
113
- });
114
- }
115
- });
116
- const handleSuccess = () => __awaiter(this, void 0, void 0, function* () {
117
- if (onSuccess) {
118
- onSuccess();
119
- }
120
- });
121
- return {
122
- handleError,
123
- handleSettled,
124
- handleSuccess,
125
- };
126
- }
127
- /**
128
- * @description Generic reusable mutation hook
129
- * @param queryKeys - Query keys to invalidate on settled
130
- * @param axiosConfig - Function receiving variables and returning a makeAxios config
131
- * @param mutationFn - Custom async mutation function (use for complex logic)
132
- * @param onSuccess - On success callback
133
- * @param onError - On error callback
134
- * @returns useMutation result
135
- *
136
- * @example
137
- * // With mutationFn (for conditional logic)
138
- * const { mutate } = useMutationHandler<TVariables>({
139
- * queryKeys: ['queryKey'],
140
- * mutationFn: async (variables: TVariables) => {
141
- * return await axios.post('/api/v1/data', variables);
142
- * },
143
- * });
144
- */
145
- function useMutationHandler(_a) {
146
- var { mutationFn, onSuccess, onError, queryKeys } = _a, options = __rest(_a, ["mutationFn", "onSuccess", "onError", "queryKeys"]);
147
- const { handleError, handleSettled, handleSuccess } = invalidateQueries({
148
- queryKeys,
149
- onSuccess,
150
- onError,
151
- });
152
- return (0, react_query_1.useMutation)(Object.assign(Object.assign({}, options), { mutationFn, mutationKey: queryKeys, onError: handleError, onSettled: handleSettled, onSuccess: handleSuccess }));
153
- }