tanstack-cacher 1.4.1 → 1.4.3

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.
Files changed (3) hide show
  1. package/index.d.ts +17 -7
  2. package/index.js +61 -40
  3. 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;
@@ -49,8 +50,11 @@ declare class QueryCacheManager<TData, TItem> {
49
50
 
50
51
  type CacheManagerConstructor = new <TData, TItem>(config: CacheConfig<TData, TItem>) => QueryCacheManager<TData, TItem>;
51
52
  declare class CacheManagerFactory {
53
+ private queryClient;
52
54
  private managerClass;
53
55
  setManagerClass(managerClass: CacheManagerConstructor): void;
56
+ setQueryClient(client: QueryClient): void;
57
+ getQueryClient(): QueryClient | null;
54
58
  resetManagerClass(): void;
55
59
  create<TData, TItem>(config: CacheConfig<TData, TItem>): QueryCacheManager<TData, TItem>;
56
60
  getManagerClass(): CacheManagerConstructor;
@@ -69,13 +73,20 @@ type CustomMutationOptions<TData, TError, TVariables> = UseMutationOptions<TData
69
73
  successMessage?: string;
70
74
  cacheActions?: CacheActions<TData>[] | CacheActions<TData>;
71
75
  notificationConfig?: NotificationOptions;
72
- getErrorMessage?: (error: TError) => string;
73
76
  };
74
77
  interface NotificationOptions {
75
78
  duration?: number;
76
79
  [key: string]: any;
77
80
  }
78
81
 
82
+ interface CacheContextType {
83
+ showError: (message: string, options?: NotificationOptions) => void;
84
+ showSuccess: (message: string, options?: NotificationOptions) => void;
85
+ getErrorMessage: (error: any) => void;
86
+ }
87
+
88
+ declare const useCacherContext: () => CacheContextType;
89
+
79
90
  declare const useCustomMutation: <TData, TError, TVariables = void>(options: CustomMutationOptions<TData, TError, TVariables>) => _tanstack_react_query.UseMutationResult<TData, TError, TVariables, unknown>;
80
91
 
81
92
  declare const useQueryCacheManagers: <T extends Record<string, QueryCacheManager<any, any>>>(configs: { [K in keyof T]: {
@@ -83,16 +94,15 @@ declare const useQueryCacheManagers: <T extends Record<string, QueryCacheManager
83
94
  options?: Partial<Omit<CacheConfig<any, any>, "queryClient">>;
84
95
  }; }) => T;
85
96
 
86
- interface NotificationContextType {
87
- showError: (message: string, options?: NotificationOptions) => void;
88
- showSuccess: (message: string, options?: NotificationOptions) => void;
97
+ interface CacheProviderProps {
98
+ children: ReactNode;
99
+ config: CacheContextType;
89
100
  }
90
-
91
- declare const useNotificationContext: () => NotificationContextType;
101
+ declare const CacheProvider: ({ config, children }: CacheProviderProps) => react.JSX.Element;
92
102
 
93
103
  type CacheOptions<TData = any, TItem = any> = Omit<CacheConfig<TData, TItem>, 'queryClient' | 'queryKey'>;
94
104
 
95
105
  declare const resetCacheManager: (queryKey: QueryKey) => void;
96
106
  declare const resetAllCacheManagers: () => void;
97
107
 
98
- export { type CacheConfig, type CacheHandlers, type CacheManagerConstructor, type CacheOptions, type CustomMutationOptions, type InsertPosition, type PaginationConfig, QueryCacheManager, cacheManagerFactory, resetAllCacheManagers, resetCacheManager, useCustomMutation, useNotificationContext, useQueryCacheManagers };
108
+ 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
- // src/hooks/useCustomMutation.ts
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) {
@@ -372,34 +388,32 @@ var QueryCacheManager = class {
372
388
  // src/managers/QueryCacheManager/CacheManagerFactory.ts
373
389
  var CacheManagerFactory = class {
374
390
  constructor() {
391
+ this.queryClient = null;
375
392
  this.managerClass = QueryCacheManager;
376
393
  }
377
394
  setManagerClass(managerClass) {
378
395
  this.managerClass = managerClass;
379
396
  }
397
+ setQueryClient(client) {
398
+ this.queryClient = client;
399
+ }
400
+ getQueryClient() {
401
+ return this.queryClient;
402
+ }
380
403
  resetManagerClass() {
381
404
  this.managerClass = QueryCacheManager;
382
405
  }
383
406
  create(config) {
384
- return new this.managerClass(config);
407
+ return new this.managerClass({
408
+ ...config,
409
+ queryClient: this.queryClient
410
+ });
385
411
  }
386
412
  getManagerClass() {
387
413
  return this.managerClass;
388
414
  }
389
415
  };
390
416
  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
417
 
404
418
  // src/hooks/useCustomMutation.ts
405
419
  var useCustomMutation = (options) => {
@@ -410,38 +424,37 @@ var useCustomMutation = (options) => {
410
424
  notify = false,
411
425
  notifyError = false,
412
426
  notifySuccess = false,
413
- errorMessage = "Operation failed!",
414
- successMessage = "Operation successfull!",
427
+ successMessage = "\u018Fm\u0259liyyat u\u011Furla tamamland\u0131!",
428
+ errorMessage = "\u018Fm\u0259liyyat zaman\u0131 x\u0259ta ba\u015F verdi!",
415
429
  notificationConfig = { duration: 2 },
416
- getErrorMessage,
417
430
  ...rest
418
431
  } = options;
419
- const notificationContext = useNotificationContext();
432
+ const cacherContext = useCacherContext();
433
+ const shouldNotifyError = notify || notifyError;
434
+ const shouldNotifySuccess = notify || notifySuccess;
435
+ const cacheActionsToRun = Array.isArray(cacheActions) ? cacheActions : cacheActions ? [cacheActions] : [];
436
+ const runCacheActions = (data) => {
437
+ cacheActionsToRun.forEach((action) => {
438
+ const { type, ...config } = action;
439
+ const manager = cacheManagerFactory.create(config);
440
+ runCacheManagers(type, manager, data);
441
+ });
442
+ };
420
443
  return reactQuery.useMutation({
421
444
  ...rest,
422
- onSuccess: (data, variables, mResult, context) => {
423
- if (notify || notifySuccess) {
424
- notificationContext?.showSuccess?.(successMessage, notificationConfig);
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);
445
+ onSuccess: (data, variables, meta, context) => {
446
+ if (shouldNotifySuccess) {
447
+ cacherContext?.showSuccess?.(successMessage, notificationConfig);
437
448
  }
449
+ onSuccess?.(data, variables, meta, context);
450
+ runCacheActions(data);
438
451
  },
439
- onError: (apiError, variables, mResult, context) => {
440
- const message = getErrorMessage ? getErrorMessage(apiError) : apiError?.error?.message ?? errorMessage;
441
- if (notify || notifyError) {
442
- notificationContext?.showError(message, notificationConfig);
452
+ onError: (apiError, variables, meta, context) => {
453
+ const message = cacherContext?.getErrorMessage?.(apiError) ?? apiError?.error?.message ?? errorMessage;
454
+ if (shouldNotifyError) {
455
+ cacherContext?.showError?.(message, notificationConfig);
443
456
  }
444
- onError?.(apiError, variables, mResult, context);
457
+ onError?.(apiError, variables, meta, context);
445
458
  }
446
459
  });
447
460
  };
@@ -459,6 +472,13 @@ var useQueryCacheManagers = (configs) => {
459
472
  });
460
473
  return managers;
461
474
  };
475
+ var CacheProvider = ({ config, children }) => {
476
+ const queryClient = reactQuery.useQueryClient();
477
+ React.useEffect(() => {
478
+ cacheManagerFactory.setQueryClient(queryClient);
479
+ }, [queryClient]);
480
+ return /* @__PURE__ */ React__default.default.createElement(CacheContext.Provider, { value: config }, children);
481
+ };
462
482
 
463
483
  // src/utils/cacheRegistry.ts
464
484
  var cacheConfigRegistry = /* @__PURE__ */ new Map();
@@ -487,12 +507,13 @@ var resetAllCacheManagers = () => {
487
507
  cacheRegistry.clear();
488
508
  };
489
509
 
510
+ exports.CacheProvider = CacheProvider;
490
511
  exports.QueryCacheManager = QueryCacheManager;
491
512
  exports.cacheManagerFactory = cacheManagerFactory;
492
513
  exports.resetAllCacheManagers = resetAllCacheManagers;
493
514
  exports.resetCacheManager = resetCacheManager;
515
+ exports.useCacherContext = useCacherContext;
494
516
  exports.useCustomMutation = useCustomMutation;
495
- exports.useNotificationContext = useNotificationContext;
496
517
  exports.useQueryCacheManagers = useQueryCacheManagers;
497
518
  Object.keys(reactQuery).forEach(function (k) {
498
519
  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.1",
3
+ "version": "1.4.3",
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
- "types": "./index.d.ts",
44
- "import": "./index.js"
48
+ "import": "./index.js",
49
+ "types": "./index.d.ts"
45
50
  },
46
51
  "./package.json": "./package.json"
47
52
  },