tanstack-cacher 1.4.1 → 1.4.2
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/index.d.ts +14 -7
- package/index.js +46 -39
- package/package.json +8 -3
package/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as _tanstack_react_query from '@tanstack/react-query';
|
|
2
2
|
import { QueryClient, QueryKey, UseMutationOptions } from '@tanstack/react-query';
|
|
3
3
|
export * from '@tanstack/react-query';
|
|
4
|
+
import react, { ReactNode } from 'react';
|
|
4
5
|
|
|
5
6
|
interface PaginationConfig {
|
|
6
7
|
totalElementsPath?: string;
|
|
@@ -69,13 +70,20 @@ type CustomMutationOptions<TData, TError, TVariables> = UseMutationOptions<TData
|
|
|
69
70
|
successMessage?: string;
|
|
70
71
|
cacheActions?: CacheActions<TData>[] | CacheActions<TData>;
|
|
71
72
|
notificationConfig?: NotificationOptions;
|
|
72
|
-
getErrorMessage?: (error: TError) => string;
|
|
73
73
|
};
|
|
74
74
|
interface NotificationOptions {
|
|
75
75
|
duration?: number;
|
|
76
76
|
[key: string]: any;
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
+
interface CacheContextType {
|
|
80
|
+
showError: (message: string, options?: NotificationOptions) => void;
|
|
81
|
+
showSuccess: (message: string, options?: NotificationOptions) => void;
|
|
82
|
+
getErrorMessage: (error: any) => void;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
declare const useCacherContext: () => CacheContextType;
|
|
86
|
+
|
|
79
87
|
declare const useCustomMutation: <TData, TError, TVariables = void>(options: CustomMutationOptions<TData, TError, TVariables>) => _tanstack_react_query.UseMutationResult<TData, TError, TVariables, unknown>;
|
|
80
88
|
|
|
81
89
|
declare const useQueryCacheManagers: <T extends Record<string, QueryCacheManager<any, any>>>(configs: { [K in keyof T]: {
|
|
@@ -83,16 +91,15 @@ declare const useQueryCacheManagers: <T extends Record<string, QueryCacheManager
|
|
|
83
91
|
options?: Partial<Omit<CacheConfig<any, any>, "queryClient">>;
|
|
84
92
|
}; }) => T;
|
|
85
93
|
|
|
86
|
-
interface
|
|
87
|
-
|
|
88
|
-
|
|
94
|
+
interface CacheProviderProps {
|
|
95
|
+
children: ReactNode;
|
|
96
|
+
config: CacheContextType;
|
|
89
97
|
}
|
|
90
|
-
|
|
91
|
-
declare const useNotificationContext: () => NotificationContextType;
|
|
98
|
+
declare const CacheProvider: ({ config, children }: CacheProviderProps) => react.JSX.Element;
|
|
92
99
|
|
|
93
100
|
type CacheOptions<TData = any, TItem = any> = Omit<CacheConfig<TData, TItem>, 'queryClient' | 'queryKey'>;
|
|
94
101
|
|
|
95
102
|
declare const resetCacheManager: (queryKey: QueryKey) => void;
|
|
96
103
|
declare const resetAllCacheManagers: () => void;
|
|
97
104
|
|
|
98
|
-
export { type CacheConfig, type CacheHandlers, type CacheManagerConstructor, type CacheOptions, type CustomMutationOptions, type InsertPosition, type PaginationConfig, QueryCacheManager, cacheManagerFactory, resetAllCacheManagers, resetCacheManager,
|
|
105
|
+
export { type CacheConfig, type CacheHandlers, type CacheManagerConstructor, type CacheOptions, CacheProvider, type CustomMutationOptions, type InsertPosition, type PaginationConfig, QueryCacheManager, cacheManagerFactory, resetAllCacheManagers, resetCacheManager, useCacherContext, useCustomMutation, useQueryCacheManagers };
|
package/index.js
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var React = require('react');
|
|
3
4
|
var reactQuery = require('@tanstack/react-query');
|
|
4
|
-
var react = require('react');
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
|
+
|
|
8
|
+
var React__default = /*#__PURE__*/_interopDefault(React);
|
|
9
|
+
|
|
10
|
+
// src/hooks/useCacherContext.ts
|
|
11
|
+
var CacheContext = React.createContext(void 0);
|
|
12
|
+
|
|
13
|
+
// src/hooks/useCacherContext.ts
|
|
14
|
+
var useCacherContext = () => {
|
|
15
|
+
const context = React.useContext(CacheContext);
|
|
16
|
+
if (!context) {
|
|
17
|
+
console.warn(
|
|
18
|
+
"useCacherContext was called outside of <NotificationProvider />. Wrap your app with <NotificationProvider /> to enable notifications."
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
return context;
|
|
22
|
+
};
|
|
7
23
|
|
|
8
24
|
// src/managers/QueryCacheManager/QueryCache.utils.ts
|
|
9
25
|
function getAtPath(obj, path, defaultValue) {
|
|
@@ -388,18 +404,6 @@ var CacheManagerFactory = class {
|
|
|
388
404
|
}
|
|
389
405
|
};
|
|
390
406
|
var cacheManagerFactory = new CacheManagerFactory();
|
|
391
|
-
var NotificationContext = react.createContext(
|
|
392
|
-
void 0
|
|
393
|
-
);
|
|
394
|
-
|
|
395
|
-
// src/hooks/useNotificationContext.ts
|
|
396
|
-
var useNotificationContext = () => {
|
|
397
|
-
const context = react.useContext(NotificationContext);
|
|
398
|
-
if (!context) {
|
|
399
|
-
throw new Error("useNotificationContext must be used within an NotificationProvider");
|
|
400
|
-
}
|
|
401
|
-
return context;
|
|
402
|
-
};
|
|
403
407
|
|
|
404
408
|
// src/hooks/useCustomMutation.ts
|
|
405
409
|
var useCustomMutation = (options) => {
|
|
@@ -410,38 +414,37 @@ var useCustomMutation = (options) => {
|
|
|
410
414
|
notify = false,
|
|
411
415
|
notifyError = false,
|
|
412
416
|
notifySuccess = false,
|
|
413
|
-
|
|
414
|
-
|
|
417
|
+
successMessage = "\u018Fm\u0259liyyat u\u011Furla tamamland\u0131!",
|
|
418
|
+
errorMessage = "\u018Fm\u0259liyyat zaman\u0131 x\u0259ta ba\u015F verdi!",
|
|
415
419
|
notificationConfig = { duration: 2 },
|
|
416
|
-
getErrorMessage,
|
|
417
420
|
...rest
|
|
418
421
|
} = options;
|
|
419
|
-
const
|
|
422
|
+
const cacherContext = useCacherContext();
|
|
423
|
+
const shouldNotifyError = notify || notifyError;
|
|
424
|
+
const shouldNotifySuccess = notify || notifySuccess;
|
|
425
|
+
const cacheActionsToRun = Array.isArray(cacheActions) ? cacheActions : cacheActions ? [cacheActions] : [];
|
|
426
|
+
const runCacheActions = (data) => {
|
|
427
|
+
cacheActionsToRun.forEach((action) => {
|
|
428
|
+
const { type, ...config } = action;
|
|
429
|
+
const manager = cacheManagerFactory.create(config);
|
|
430
|
+
runCacheManagers(type, manager, data);
|
|
431
|
+
});
|
|
432
|
+
};
|
|
420
433
|
return reactQuery.useMutation({
|
|
421
434
|
...rest,
|
|
422
|
-
onSuccess: (data, variables,
|
|
423
|
-
if (
|
|
424
|
-
|
|
425
|
-
}
|
|
426
|
-
onSuccess?.(data, variables, mResult, context);
|
|
427
|
-
if (Array.isArray(cacheActions)) {
|
|
428
|
-
cacheActions.forEach((item) => {
|
|
429
|
-
const { type, ...rest2 } = item;
|
|
430
|
-
const manager = cacheManagerFactory.create(rest2);
|
|
431
|
-
runCacheManagers(type, manager, data);
|
|
432
|
-
});
|
|
433
|
-
} else if (cacheActions) {
|
|
434
|
-
const { type, ...rest2 } = cacheActions;
|
|
435
|
-
const manager = cacheManagerFactory.create(rest2);
|
|
436
|
-
runCacheManagers(type, manager, data);
|
|
435
|
+
onSuccess: (data, variables, meta, context) => {
|
|
436
|
+
if (shouldNotifySuccess) {
|
|
437
|
+
cacherContext?.showSuccess?.(successMessage, notificationConfig);
|
|
437
438
|
}
|
|
439
|
+
onSuccess?.(data, variables, meta, context);
|
|
440
|
+
runCacheActions(data);
|
|
438
441
|
},
|
|
439
|
-
onError: (apiError, variables,
|
|
440
|
-
const message = getErrorMessage
|
|
441
|
-
if (
|
|
442
|
-
|
|
442
|
+
onError: (apiError, variables, meta, context) => {
|
|
443
|
+
const message = cacherContext?.getErrorMessage?.(apiError) ?? apiError?.error?.message ?? errorMessage;
|
|
444
|
+
if (shouldNotifyError) {
|
|
445
|
+
cacherContext?.showError?.(message, notificationConfig);
|
|
443
446
|
}
|
|
444
|
-
onError?.(apiError, variables,
|
|
447
|
+
onError?.(apiError, variables, meta, context);
|
|
445
448
|
}
|
|
446
449
|
});
|
|
447
450
|
};
|
|
@@ -459,6 +462,9 @@ var useQueryCacheManagers = (configs) => {
|
|
|
459
462
|
});
|
|
460
463
|
return managers;
|
|
461
464
|
};
|
|
465
|
+
var CacheProvider = ({ config, children }) => {
|
|
466
|
+
return /* @__PURE__ */ React__default.default.createElement(CacheContext.Provider, { value: config }, children);
|
|
467
|
+
};
|
|
462
468
|
|
|
463
469
|
// src/utils/cacheRegistry.ts
|
|
464
470
|
var cacheConfigRegistry = /* @__PURE__ */ new Map();
|
|
@@ -487,12 +493,13 @@ var resetAllCacheManagers = () => {
|
|
|
487
493
|
cacheRegistry.clear();
|
|
488
494
|
};
|
|
489
495
|
|
|
496
|
+
exports.CacheProvider = CacheProvider;
|
|
490
497
|
exports.QueryCacheManager = QueryCacheManager;
|
|
491
498
|
exports.cacheManagerFactory = cacheManagerFactory;
|
|
492
499
|
exports.resetAllCacheManagers = resetAllCacheManagers;
|
|
493
500
|
exports.resetCacheManager = resetCacheManager;
|
|
501
|
+
exports.useCacherContext = useCacherContext;
|
|
494
502
|
exports.useCustomMutation = useCustomMutation;
|
|
495
|
-
exports.useNotificationContext = useNotificationContext;
|
|
496
503
|
exports.useQueryCacheManagers = useQueryCacheManagers;
|
|
497
504
|
Object.keys(reactQuery).forEach(function (k) {
|
|
498
505
|
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tanstack-cacher",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.2",
|
|
4
4
|
"description": "A lightweight cache management utility for TanStack Query that simplifies adding, updating, deleting, and synchronizing cached data",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -17,6 +17,11 @@
|
|
|
17
17
|
"name": "Hasanli Hajagha",
|
|
18
18
|
"email": "hacagahasanli@gmail.com",
|
|
19
19
|
"web": "http://github.com/hacagahasanli"
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"name": "Sahira Mammadova",
|
|
23
|
+
"email": "mammadovasahira@gmail.com",
|
|
24
|
+
"web": "https://github.com/SahiraMamedova"
|
|
20
25
|
}
|
|
21
26
|
],
|
|
22
27
|
"homepage": "https://github.com/hacagahasanli/tanstack-cacher#readme",
|
|
@@ -40,8 +45,8 @@
|
|
|
40
45
|
],
|
|
41
46
|
"exports": {
|
|
42
47
|
".": {
|
|
43
|
-
"
|
|
44
|
-
"
|
|
48
|
+
"import": "./index.js",
|
|
49
|
+
"types": "./index.d.ts"
|
|
45
50
|
},
|
|
46
51
|
"./package.json": "./package.json"
|
|
47
52
|
},
|