tanstack-cacher 1.4.0 → 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 +15 -7
- package/index.js +49 -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;
|
|
@@ -43,6 +44,7 @@ declare class QueryCacheManager<TData, TItem> {
|
|
|
43
44
|
refetch(key?: string | string[]): void;
|
|
44
45
|
hasQuery(key?: string): boolean;
|
|
45
46
|
removeQuery(key?: string): void;
|
|
47
|
+
getConfig(): typeof this.config;
|
|
46
48
|
createHandlers(): CacheHandlers<TItem>;
|
|
47
49
|
}
|
|
48
50
|
|
|
@@ -68,13 +70,20 @@ type CustomMutationOptions<TData, TError, TVariables> = UseMutationOptions<TData
|
|
|
68
70
|
successMessage?: string;
|
|
69
71
|
cacheActions?: CacheActions<TData>[] | CacheActions<TData>;
|
|
70
72
|
notificationConfig?: NotificationOptions;
|
|
71
|
-
getErrorMessage?: (error: TError) => string;
|
|
72
73
|
};
|
|
73
74
|
interface NotificationOptions {
|
|
74
75
|
duration?: number;
|
|
75
76
|
[key: string]: any;
|
|
76
77
|
}
|
|
77
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
|
+
|
|
78
87
|
declare const useCustomMutation: <TData, TError, TVariables = void>(options: CustomMutationOptions<TData, TError, TVariables>) => _tanstack_react_query.UseMutationResult<TData, TError, TVariables, unknown>;
|
|
79
88
|
|
|
80
89
|
declare const useQueryCacheManagers: <T extends Record<string, QueryCacheManager<any, any>>>(configs: { [K in keyof T]: {
|
|
@@ -82,16 +91,15 @@ declare const useQueryCacheManagers: <T extends Record<string, QueryCacheManager
|
|
|
82
91
|
options?: Partial<Omit<CacheConfig<any, any>, "queryClient">>;
|
|
83
92
|
}; }) => T;
|
|
84
93
|
|
|
85
|
-
interface
|
|
86
|
-
|
|
87
|
-
|
|
94
|
+
interface CacheProviderProps {
|
|
95
|
+
children: ReactNode;
|
|
96
|
+
config: CacheContextType;
|
|
88
97
|
}
|
|
89
|
-
|
|
90
|
-
declare const useNotificationContext: () => NotificationContextType;
|
|
98
|
+
declare const CacheProvider: ({ config, children }: CacheProviderProps) => react.JSX.Element;
|
|
91
99
|
|
|
92
100
|
type CacheOptions<TData = any, TItem = any> = Omit<CacheConfig<TData, TItem>, 'queryClient' | 'queryKey'>;
|
|
93
101
|
|
|
94
102
|
declare const resetCacheManager: (queryKey: QueryKey) => void;
|
|
95
103
|
declare const resetAllCacheManagers: () => void;
|
|
96
104
|
|
|
97
|
-
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) {
|
|
@@ -352,6 +368,9 @@ var QueryCacheManager = class {
|
|
|
352
368
|
const queryKey = key ?? this.config.queryKey;
|
|
353
369
|
this.config.queryClient.removeQueries({ queryKey: [queryKey] });
|
|
354
370
|
}
|
|
371
|
+
getConfig() {
|
|
372
|
+
return this.config;
|
|
373
|
+
}
|
|
355
374
|
/**
|
|
356
375
|
* Get handlers for use with mutations
|
|
357
376
|
*
|
|
@@ -385,18 +404,6 @@ var CacheManagerFactory = class {
|
|
|
385
404
|
}
|
|
386
405
|
};
|
|
387
406
|
var cacheManagerFactory = new CacheManagerFactory();
|
|
388
|
-
var NotificationContext = react.createContext(
|
|
389
|
-
void 0
|
|
390
|
-
);
|
|
391
|
-
|
|
392
|
-
// src/hooks/useNotificationContext.ts
|
|
393
|
-
var useNotificationContext = () => {
|
|
394
|
-
const context = react.useContext(NotificationContext);
|
|
395
|
-
if (!context) {
|
|
396
|
-
throw new Error("useNotificationContext must be used within an NotificationProvider");
|
|
397
|
-
}
|
|
398
|
-
return context;
|
|
399
|
-
};
|
|
400
407
|
|
|
401
408
|
// src/hooks/useCustomMutation.ts
|
|
402
409
|
var useCustomMutation = (options) => {
|
|
@@ -407,38 +414,37 @@ var useCustomMutation = (options) => {
|
|
|
407
414
|
notify = false,
|
|
408
415
|
notifyError = false,
|
|
409
416
|
notifySuccess = false,
|
|
410
|
-
|
|
411
|
-
|
|
417
|
+
successMessage = "\u018Fm\u0259liyyat u\u011Furla tamamland\u0131!",
|
|
418
|
+
errorMessage = "\u018Fm\u0259liyyat zaman\u0131 x\u0259ta ba\u015F verdi!",
|
|
412
419
|
notificationConfig = { duration: 2 },
|
|
413
|
-
getErrorMessage,
|
|
414
420
|
...rest
|
|
415
421
|
} = options;
|
|
416
|
-
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
|
+
};
|
|
417
433
|
return reactQuery.useMutation({
|
|
418
434
|
...rest,
|
|
419
|
-
onSuccess: (data, variables,
|
|
420
|
-
if (
|
|
421
|
-
|
|
422
|
-
}
|
|
423
|
-
onSuccess?.(data, variables, mResult, context);
|
|
424
|
-
if (Array.isArray(cacheActions)) {
|
|
425
|
-
cacheActions.forEach((item) => {
|
|
426
|
-
const { type, ...rest2 } = item;
|
|
427
|
-
const manager = cacheManagerFactory.create(rest2);
|
|
428
|
-
runCacheManagers(type, manager, data);
|
|
429
|
-
});
|
|
430
|
-
} else if (cacheActions) {
|
|
431
|
-
const { type, ...rest2 } = cacheActions;
|
|
432
|
-
const manager = cacheManagerFactory.create(rest2);
|
|
433
|
-
runCacheManagers(type, manager, data);
|
|
435
|
+
onSuccess: (data, variables, meta, context) => {
|
|
436
|
+
if (shouldNotifySuccess) {
|
|
437
|
+
cacherContext?.showSuccess?.(successMessage, notificationConfig);
|
|
434
438
|
}
|
|
439
|
+
onSuccess?.(data, variables, meta, context);
|
|
440
|
+
runCacheActions(data);
|
|
435
441
|
},
|
|
436
|
-
onError: (apiError, variables,
|
|
437
|
-
const message = getErrorMessage
|
|
438
|
-
if (
|
|
439
|
-
|
|
442
|
+
onError: (apiError, variables, meta, context) => {
|
|
443
|
+
const message = cacherContext?.getErrorMessage?.(apiError) ?? apiError?.error?.message ?? errorMessage;
|
|
444
|
+
if (shouldNotifyError) {
|
|
445
|
+
cacherContext?.showError?.(message, notificationConfig);
|
|
440
446
|
}
|
|
441
|
-
onError?.(apiError, variables,
|
|
447
|
+
onError?.(apiError, variables, meta, context);
|
|
442
448
|
}
|
|
443
449
|
});
|
|
444
450
|
};
|
|
@@ -456,6 +462,9 @@ var useQueryCacheManagers = (configs) => {
|
|
|
456
462
|
});
|
|
457
463
|
return managers;
|
|
458
464
|
};
|
|
465
|
+
var CacheProvider = ({ config, children }) => {
|
|
466
|
+
return /* @__PURE__ */ React__default.default.createElement(CacheContext.Provider, { value: config }, children);
|
|
467
|
+
};
|
|
459
468
|
|
|
460
469
|
// src/utils/cacheRegistry.ts
|
|
461
470
|
var cacheConfigRegistry = /* @__PURE__ */ new Map();
|
|
@@ -484,12 +493,13 @@ var resetAllCacheManagers = () => {
|
|
|
484
493
|
cacheRegistry.clear();
|
|
485
494
|
};
|
|
486
495
|
|
|
496
|
+
exports.CacheProvider = CacheProvider;
|
|
487
497
|
exports.QueryCacheManager = QueryCacheManager;
|
|
488
498
|
exports.cacheManagerFactory = cacheManagerFactory;
|
|
489
499
|
exports.resetAllCacheManagers = resetAllCacheManagers;
|
|
490
500
|
exports.resetCacheManager = resetCacheManager;
|
|
501
|
+
exports.useCacherContext = useCacherContext;
|
|
491
502
|
exports.useCustomMutation = useCustomMutation;
|
|
492
|
-
exports.useNotificationContext = useNotificationContext;
|
|
493
503
|
exports.useQueryCacheManagers = useQueryCacheManagers;
|
|
494
504
|
Object.keys(reactQuery).forEach(function (k) {
|
|
495
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
|
},
|