react-redux-cache 0.8.2 → 0.10.0

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/README.md CHANGED
@@ -165,6 +165,7 @@ export const {
165
165
  query: getUser,
166
166
  // For each query `secondsToLive` option can be set, which is used to set expiration date of a cached result when query response is received.
167
167
  // After expiration query result is considered invalidated and will be refetched on the next useQuery mount.
168
+ // Can also be set globally in `globals`.
168
169
  secondsToLive: 5 * 60 // Here cached result is valid for 5 minutes.
169
170
  },
170
171
  },
@@ -289,7 +290,23 @@ export const updateBank = (bank) => {
289
290
  }
290
291
  }
291
292
  } satisfies Mutation<Partial<Bank>>
293
+ ```
294
+
295
+ If global error handling is needed for errors, not handled by query / mutation `onError` callback, global `onError` can be used:
292
296
 
297
+ ```typescript
298
+ export const cache = createCache({
299
+ name: 'cache',
300
+ globals: {
301
+ onError: (error, key) {
302
+ console.log('Not handled error', { error, key })
303
+ }
304
+ },
305
+ queries: {
306
+ getUsers: { query: getUsers },
307
+ },
308
+ ...
309
+ })
293
310
  ```
294
311
 
295
312
  #### Invalidation
@@ -11,7 +11,7 @@ import { applyEntityChanges } from './utilsAndConstants';
11
11
  * })
12
12
  */
13
13
  export declare const withTypenames: <T extends Typenames = Typenames>() => {
14
- createCache: <N extends string, QP, QR, MP, MR>(partialCache: OptionalPartial<Cache<N, T, QP, QR, MP, MR>, "queries" | "options" | "mutations" | "cacheStateSelector" | "defaults">) => {
14
+ createCache: <N extends string, QP, QR, MP, MR>(partialCache: OptionalPartial<Cache<N, T, QP, QR, MP, MR>, "queries" | "options" | "mutations" | "cacheStateSelector" | "globals">) => {
15
15
  /** Keeps all options, passed while creating the cache. */
16
16
  cache: Cache<N, T, QP, QR, MP, MR>;
17
17
  /** Reducer of the cache, should be added to redux store. */
@@ -172,7 +172,7 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
172
172
  /**
173
173
  * Creates reducer, actions and hooks for managing queries and mutations through redux cache.
174
174
  */
175
- export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCache: OptionalPartial<Cache<N, Typenames, QP, QR, MP, MR>, "queries" | "options" | "mutations" | "cacheStateSelector" | "defaults">) => {
175
+ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCache: OptionalPartial<Cache<N, Typenames, QP, QR, MP, MR>, "queries" | "options" | "mutations" | "cacheStateSelector" | "globals">) => {
176
176
  /** Keeps all options, passed while creating the cache. */
177
177
  cache: Cache<N, Typenames, QP, QR, MP, MR>;
178
178
  /** Reducer of the cache, should be added to redux store. */
@@ -30,12 +30,12 @@ const withTypenames = () => {
30
30
  // provide all optional fields
31
31
  (_a = partialCache.options) !== null && _a !== void 0 ? _a : (partialCache.options = {});
32
32
  (_b = (_k = partialCache.options).logsEnabled) !== null && _b !== void 0 ? _b : (_k.logsEnabled = false);
33
- (_c = (_l = partialCache.options).validateFunctionArguments) !== null && _c !== void 0 ? _c : (_l.validateFunctionArguments = utilsAndConstants_1.IS_DEV);
33
+ (_c = (_l = partialCache.options).additionalValidation) !== null && _c !== void 0 ? _c : (_l.additionalValidation = utilsAndConstants_1.IS_DEV);
34
34
  (_d = (_m = partialCache.options).deepComparisonEnabled) !== null && _d !== void 0 ? _d : (_m.deepComparisonEnabled = true);
35
35
  (_e = partialCache.queries) !== null && _e !== void 0 ? _e : (partialCache.queries = {});
36
36
  (_f = partialCache.mutations) !== null && _f !== void 0 ? _f : (partialCache.mutations = {});
37
- (_g = partialCache.defaults) !== null && _g !== void 0 ? _g : (partialCache.defaults = {});
38
- (_h = (_o = partialCache.defaults).cachePolicy) !== null && _h !== void 0 ? _h : (_o.cachePolicy = 'cache-first');
37
+ (_g = partialCache.globals) !== null && _g !== void 0 ? _g : (partialCache.globals = {});
38
+ (_h = (_o = partialCache.globals).cachePolicy) !== null && _h !== void 0 ? _h : (_o.cachePolicy = 'cache-first');
39
39
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
40
40
  (_j = partialCache.cacheStateSelector) !== null && _j !== void 0 ? _j : (partialCache.cacheStateSelector = (state) => state[cache.name]);
41
41
  // @ts-expect-error private field for testing
@@ -147,7 +147,9 @@ const withTypenames = () => {
147
147
  /** Fetches query when params change and subscribes to query state changes (except `expiresAt` field). */
148
148
  useQuery: (options) => (0, useQuery_1.useQuery)(cache, actions, options),
149
149
  /** Subscribes to provided mutation state and provides mutate function. */
150
- useMutation: (options) => (0, useMutation_1.useMutation)(cache, actions, options, abortControllers),
150
+ useMutation: (options
151
+ // @ts-expect-error cache type
152
+ ) => (0, useMutation_1.useMutation)(cache, actions, options, abortControllers),
151
153
  /** useSelector + selectEntityById. */
152
154
  useSelectEntityById: (id, typename) => {
153
155
  return (0, react_redux_1.useSelector)((state) => selectEntityById(state, id, typename));
package/dist/index.js CHANGED
@@ -24,22 +24,25 @@ Object.defineProperty(exports, "defaultGetCacheKey", { enumerable: true, get: fu
24
24
  Object.defineProperty(exports, "defaultQueryMutationState", { enumerable: true, get: function () { return utilsAndConstants_1.DEFAULT_QUERY_MUTATION_STATE; } });
25
25
  // Backlog
26
26
  // ! high (1.0.0)
27
- // rca -> vite
28
27
  // remove undefined optional fields & emtpy states. remove mutation state when it finished without errors
29
28
  // remove cachePolicy? make skip/enabled a function? skip -> enabled/shouldFetch?
30
29
  // generate full api docs
31
30
  // ! medium
31
+ // example rca -> vite
32
32
  // optimistic response
33
+ // reset [whole] cache to initial / to provided state
34
+ // globals for success, completions and loading states?
33
35
  // make query key / cache key difference more clear in the docs
34
36
  // check type of function arguments in dev
35
37
  // ! low
38
+ // local cache policy to keep in component state?
36
39
  // make error type generic
37
40
  // allow multiple mutation with same keys?
38
41
  // custom useStore & useSelector to support multiple stores?
39
42
  // easy access to all currently loading queries and mutations?
40
43
  // cancellation to queries
41
44
  // if mutation & query already loading - make options: last, throttle, debounce, parallel?
42
- // add refresh interval for queries that are mounted
45
+ // add refresh interval for queries that are mounted?
43
46
  // readonly types?
44
47
  // proper types, remove as, any, todo
45
48
  // add number of retries param?
package/dist/mutate.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { Store } from 'redux';
2
2
  import type { ActionMap } from './createActions';
3
3
  import type { Cache, Key, MutationResult, Typenames } from './types';
4
- export declare const mutate: <N extends string, T extends Typenames, QP, QR, MP, MR, MK extends keyof MP | keyof MR>(logTag: string, store: Store, cache: Cache<N, T, QP, QR, MP, MR>, { updateMutationStateAndEntities, }: Pick<ActionMap<N, T, unknown, unknown, MP, MR>, "updateMutationStateAndEntities">, mutationKey: MK, params: MK extends keyof MP & keyof MR ? MP[MK] : never, abortControllers: WeakMap<Store, Record<Key, AbortController>>, onCompleted?: ((response: import("./types").QueryResponse<T, MR[keyof MP & keyof MR & string]> | undefined, error: unknown, params: MP[keyof MP & keyof MR & string] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((response: import("./types").QueryResponse<T, MR[keyof MP & keyof MR & number]> | undefined, error: unknown, params: MP[keyof MP & keyof MR & number] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((response: import("./types").QueryResponse<T, MR[keyof MP & keyof MR & symbol]> | undefined, error: unknown, params: MP[keyof MP & keyof MR & symbol] | undefined, store: Store<any, import("redux").AnyAction>) => void) | undefined, onSuccess?: ((response: import("./types").QueryResponse<T, MR[keyof MP & keyof MR & string]>, params: MP[keyof MP & keyof MR & string] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((response: import("./types").QueryResponse<T, MR[keyof MP & keyof MR & number]>, params: MP[keyof MP & keyof MR & number] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((response: import("./types").QueryResponse<T, MR[keyof MP & keyof MR & symbol]>, params: MP[keyof MP & keyof MR & symbol] | undefined, store: Store<any, import("redux").AnyAction>) => void) | undefined, onError?: ((error: unknown, params: MP[keyof MP & keyof MR & string] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((error: unknown, params: MP[keyof MP & keyof MR & number] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((error: unknown, params: MP[keyof MP & keyof MR & symbol] | undefined, store: Store<any, import("redux").AnyAction>) => void) | undefined) => Promise<MutationResult<MK extends keyof MP & keyof MR ? MR[MK] : never>>;
4
+ export declare const mutate: <N extends string, T extends Typenames, QP, QR, MP, MR, MK extends keyof MP | keyof MR>(logTag: string, store: Store, cache: Cache<N, T, QP, QR, MP, MR>, { updateMutationStateAndEntities, }: Pick<ActionMap<N, T, unknown, unknown, MP, MR>, "updateMutationStateAndEntities">, mutationKey: MK, params: MK extends keyof MP & keyof MR ? MP[MK] : never, abortControllers: WeakMap<Store, Record<Key, AbortController>>, onCompleted?: ((response: import("./types").QueryResponse<T, MR[keyof MP & keyof MR & string]> | undefined, error: unknown, params: MP[keyof MP & keyof MR & string] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((response: import("./types").QueryResponse<T, MR[keyof MP & keyof MR & number]> | undefined, error: unknown, params: MP[keyof MP & keyof MR & number] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((response: import("./types").QueryResponse<T, MR[keyof MP & keyof MR & symbol]> | undefined, error: unknown, params: MP[keyof MP & keyof MR & symbol] | undefined, store: Store<any, import("redux").AnyAction>) => void) | undefined, onSuccess?: ((response: import("./types").QueryResponse<T, MR[keyof MP & keyof MR & string]>, params: MP[keyof MP & keyof MR & string] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((response: import("./types").QueryResponse<T, MR[keyof MP & keyof MR & number]>, params: MP[keyof MP & keyof MR & number] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((response: import("./types").QueryResponse<T, MR[keyof MP & keyof MR & symbol]>, params: MP[keyof MP & keyof MR & symbol] | undefined, store: Store<any, import("redux").AnyAction>) => void) | undefined, onError?: ((error: unknown, params: MP[keyof MP & keyof MR & string] | undefined, store: Store<any, import("redux").AnyAction>) => boolean | void | null | undefined) | ((error: unknown, params: MP[keyof MP & keyof MR & number] | undefined, store: Store<any, import("redux").AnyAction>) => boolean | void | null | undefined) | ((error: unknown, params: MP[keyof MP & keyof MR & symbol] | undefined, store: Store<any, import("redux").AnyAction>) => boolean | void | null | undefined) | undefined) => Promise<MutationResult<MK extends keyof MP & keyof MR ? MR[MK] : never>>;
package/dist/mutate.js CHANGED
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.mutate = void 0;
13
13
  const utilsAndConstants_1 = require("./utilsAndConstants");
14
14
  const mutate = (logTag, store, cache, { updateMutationStateAndEntities, }, mutationKey, params, abortControllers, onCompleted = cache.mutations[mutationKey].onCompleted, onSuccess = cache.mutations[mutationKey].onSuccess, onError = cache.mutations[mutationKey].onError) => __awaiter(void 0, void 0, void 0, function* () {
15
+ var _a, _b;
15
16
  let abortControllersOfStore = abortControllers.get(store);
16
17
  if (abortControllersOfStore === undefined) {
17
18
  abortControllersOfStore = {};
@@ -65,7 +66,10 @@ const mutate = (logTag, store, cache, { updateMutationStateAndEntities, }, mutat
65
66
  loading: false,
66
67
  }));
67
68
  // @ts-expect-error params
68
- onError === null || onError === void 0 ? void 0 : onError(error, params, store);
69
+ if (!(onError === null || onError === void 0 ? void 0 : onError(error, params, store))) {
70
+ // @ts-expect-error queryKey
71
+ (_b = (_a = cache.globals).onError) === null || _b === void 0 ? void 0 : _b.call(_a, error, mutationKey, params, store);
72
+ }
69
73
  // @ts-expect-error response
70
74
  onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(response, error, params, store);
71
75
  return { error };
package/dist/query.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { Store } from 'redux';
2
2
  import type { ActionMap } from './createActions';
3
3
  import type { Cache, Key, QueryResult, Typenames } from './types';
4
- export declare const query: <N extends string, T extends Typenames, QP, QR, MP, MR, QK extends keyof QP | keyof QR>(logTag: string, store: Store, cache: Cache<N, T, QP, QR, MP, MR>, { updateQueryStateAndEntities, }: Pick<ActionMap<N, T, QP, QR, unknown, unknown>, "updateQueryStateAndEntities">, queryKey: QK, cacheKey: Key, params: QK extends keyof QP & keyof QR ? QP[QK] : never, secondsToLive: number | undefined, onlyIfExpired: boolean | undefined, mergeResults?: ((oldResult: QR[keyof QP & keyof QR & string] | undefined, response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & string]>, params: QP[keyof QP & keyof QR & string] | undefined, store: Store<any, import("redux").AnyAction>) => QR[keyof QP & keyof QR & string]) | ((oldResult: QR[keyof QP & keyof QR & number] | undefined, response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & number]>, params: QP[keyof QP & keyof QR & number] | undefined, store: Store<any, import("redux").AnyAction>) => QR[keyof QP & keyof QR & number]) | ((oldResult: QR[keyof QP & keyof QR & symbol] | undefined, response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & symbol]>, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store<any, import("redux").AnyAction>) => QR[keyof QP & keyof QR & symbol]) | undefined, onCompleted?: ((response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & string]> | undefined, error: unknown, params: QP[keyof QP & keyof QR & string] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & number]> | undefined, error: unknown, params: QP[keyof QP & keyof QR & number] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & symbol]> | undefined, error: unknown, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store<any, import("redux").AnyAction>) => void) | undefined, onSuccess?: ((response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & string]>, params: QP[keyof QP & keyof QR & string] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & number]>, params: QP[keyof QP & keyof QR & number] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & symbol]>, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store<any, import("redux").AnyAction>) => void) | undefined, onError?: ((error: unknown, params: QP[keyof QP & keyof QR & string] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((error: unknown, params: QP[keyof QP & keyof QR & number] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((error: unknown, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store<any, import("redux").AnyAction>) => void) | undefined) => Promise<QueryResult<QK extends keyof QP & keyof QR ? QR[QK] : never>>;
4
+ export declare const query: <N extends string, T extends Typenames, QP, QR, MP, MR, QK extends keyof QP | keyof QR>(logTag: string, store: Store, cache: Cache<N, T, QP, QR, MP, MR>, { updateQueryStateAndEntities, }: Pick<ActionMap<N, T, QP, QR, unknown, unknown>, "updateQueryStateAndEntities">, queryKey: QK, cacheKey: Key, params: QK extends keyof QP & keyof QR ? QP[QK] : never, secondsToLive: number | undefined, onlyIfExpired: boolean | undefined, mergeResults?: ((oldResult: QR[keyof QP & keyof QR & string] | undefined, response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & string]>, params: QP[keyof QP & keyof QR & string] | undefined, store: Store<any, import("redux").AnyAction>) => QR[keyof QP & keyof QR & string]) | ((oldResult: QR[keyof QP & keyof QR & number] | undefined, response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & number]>, params: QP[keyof QP & keyof QR & number] | undefined, store: Store<any, import("redux").AnyAction>) => QR[keyof QP & keyof QR & number]) | ((oldResult: QR[keyof QP & keyof QR & symbol] | undefined, response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & symbol]>, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store<any, import("redux").AnyAction>) => QR[keyof QP & keyof QR & symbol]) | undefined, onCompleted?: ((response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & string]> | undefined, error: unknown, params: QP[keyof QP & keyof QR & string] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & number]> | undefined, error: unknown, params: QP[keyof QP & keyof QR & number] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & symbol]> | undefined, error: unknown, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store<any, import("redux").AnyAction>) => void) | undefined, onSuccess?: ((response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & string]>, params: QP[keyof QP & keyof QR & string] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & number]>, params: QP[keyof QP & keyof QR & number] | undefined, store: Store<any, import("redux").AnyAction>) => void) | ((response: import("./types").QueryResponse<T, QR[keyof QP & keyof QR & symbol]>, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store<any, import("redux").AnyAction>) => void) | undefined, onError?: ((error: unknown, params: QP[keyof QP & keyof QR & string] | undefined, store: Store<any, import("redux").AnyAction>) => boolean | void | null | undefined) | ((error: unknown, params: QP[keyof QP & keyof QR & number] | undefined, store: Store<any, import("redux").AnyAction>) => boolean | void | null | undefined) | ((error: unknown, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store<any, import("redux").AnyAction>) => boolean | void | null | undefined) | undefined) => Promise<QueryResult<QK extends keyof QP & keyof QR ? QR[QK] : never>>;
package/dist/query.js CHANGED
@@ -12,9 +12,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.query = void 0;
13
13
  const utilsAndConstants_1 = require("./utilsAndConstants");
14
14
  const query = (logTag, store, cache, _a, queryKey, cacheKey, params, secondsToLive, onlyIfExpired, mergeResults, onCompleted, onSuccess, onError) => __awaiter(void 0, void 0, void 0, function* () {
15
- var _b, _c, _d;
15
+ var _b, _c, _d, _e, _f;
16
16
  var { updateQueryStateAndEntities, } = _a;
17
- if (secondsToLive === void 0) { secondsToLive = (_b = cache.queries[queryKey].secondsToLive) !== null && _b !== void 0 ? _b : cache.defaults.secondsToLive; }
17
+ if (secondsToLive === void 0) { secondsToLive = (_b = cache.queries[queryKey].secondsToLive) !== null && _b !== void 0 ? _b : cache.globals.secondsToLive; }
18
18
  if (mergeResults === void 0) { mergeResults = cache.queries[queryKey].mergeResults; }
19
19
  if (onCompleted === void 0) { onCompleted = cache.queries[queryKey].onCompleted; }
20
20
  if (onSuccess === void 0) { onSuccess = cache.queries[queryKey].onSuccess; }
@@ -59,7 +59,10 @@ const query = (logTag, store, cache, _a, queryKey, cacheKey, params, secondsToLi
59
59
  loading: false,
60
60
  }));
61
61
  // @ts-expect-error params
62
- onError === null || onError === void 0 ? void 0 : onError(error, params, store);
62
+ if (!(onError === null || onError === void 0 ? void 0 : onError(error, params, store))) {
63
+ // @ts-expect-error queryKey
64
+ (_d = (_c = cache.globals).onError) === null || _d === void 0 ? void 0 : _d.call(_c, error, queryKey, params, store);
65
+ }
63
66
  // @ts-expect-error params
64
67
  onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(undefined, error, params, store);
65
68
  return { error };
@@ -67,11 +70,11 @@ const query = (logTag, store, cache, _a, queryKey, cacheKey, params, secondsToLi
67
70
  const newState = {
68
71
  error: undefined,
69
72
  loading: false,
70
- expiresAt: (_c = response.expiresAt) !== null && _c !== void 0 ? _c : (secondsToLive != null ? Date.now() + secondsToLive * 1000 : undefined),
73
+ expiresAt: (_e = response.expiresAt) !== null && _e !== void 0 ? _e : (secondsToLive != null ? Date.now() + secondsToLive * 1000 : undefined),
71
74
  result: mergeResults
72
75
  ? mergeResults(
73
76
  // @ts-expect-error fix later
74
- (_d = cacheStateSelector(store.getState()).queries[queryKey][cacheKey]) === null || _d === void 0 ? void 0 : _d.result, response, params, store)
77
+ (_f = cacheStateSelector(store.getState()).queries[queryKey][cacheKey]) === null || _f === void 0 ? void 0 : _f.result, response, params, store)
75
78
  : response.result,
76
79
  };
77
80
  store.dispatch(updateQueryStateAndEntities(queryKey, cacheKey, newState, response));
package/dist/types.d.ts CHANGED
@@ -27,14 +27,14 @@ export type Cache<N extends string, T extends Typenames, QP, QR, MP, MR> = {
27
27
  mutations: {
28
28
  [MK in keyof (MP & MR)]: MK extends keyof (MP | MR) ? MutationInfo<T, MP[MK], MR[MK]> : never;
29
29
  };
30
- /** Default options for queries.
31
- * @default { cachePolicy: 'cache-first', sedondsToLive: undefined } */
32
- defaults: Defaults;
30
+ /** Default options for queries and mutations.
31
+ * @default { cachePolicy: 'cache-first', sedondsToLive: undefined, onError: undefined } */
32
+ globals: Globals<QP, MP>;
33
33
  options: CacheOptions;
34
34
  /** Should return cache state from redux root state. Default implementation returns `state[name]`. */
35
35
  cacheStateSelector: (state: any) => ReduxCacheState<T, QP, QR, MP, MR>;
36
36
  };
37
- export type Defaults = {
37
+ export type Globals<QP, MP> = {
38
38
  /**
39
39
  * Cache policy.
40
40
  * @cache-first fetch only if cache either does not exist or is expired on useQuery mount.
@@ -44,15 +44,18 @@ export type Defaults = {
44
44
  cachePolicy: QueryCachePolicy;
45
45
  /** If set, this value updates expiresAt value of query state when query result is received. */
46
46
  secondsToLive?: number;
47
+ /** Handles errors, not handled by onError from queries and mutations. */
48
+ onError?: <K extends keyof QP | keyof MP>(error: unknown, key: K, params: QP[keyof QP] | MP[keyof MP], // TODO proper type
49
+ store: Store) => void;
47
50
  };
48
51
  export type CacheOptions = {
49
52
  /**
50
- * Enables validation of package function arguments. Recommened to enable in dev/testing mode.
53
+ * Enables additional validation with logging to console.warn. Recommened to enable in dev/testing mode.
51
54
  * @default true in dev mode.
52
55
  * */
53
- validateFunctionArguments: boolean;
56
+ additionalValidation: boolean;
54
57
  /**
55
- * Enables console logs.
58
+ * Enables debug logs.
56
59
  * @default false
57
60
  */
58
61
  logsEnabled: boolean;
@@ -78,7 +81,7 @@ export type Query<P, T extends Typenames = Typenames, R = unknown> = (
78
81
  params: P,
79
82
  /** Redux store */
80
83
  store: Store) => Promise<QueryResponse<T, R>>;
81
- export type QueryInfo<T extends Typenames, P, R> = Partial<Defaults> & {
84
+ export type QueryInfo<T extends Typenames, P, R> = Partial<Pick<Globals<unknown, unknown>, 'cachePolicy' | 'secondsToLive'>> & {
82
85
  query: Query<P, T, R>;
83
86
  /** Merges results before saving to the store. Default implementation is using the latest result. */
84
87
  mergeResults?: (oldResult: R | undefined, response: QueryResponse<T, R>, params: P | undefined, store: Store) => R;
@@ -92,8 +95,8 @@ export type QueryInfo<T extends Typenames, P, R> = Partial<Defaults> & {
92
95
  onCompleted?: (response: QueryResponse<T, R> | undefined, error: unknown | undefined, params: P | undefined, store: Store) => void;
93
96
  /** Called after fetch finished without error. */
94
97
  onSuccess?: (response: QueryResponse<T, R>, params: P | undefined, store: Store) => void;
95
- /** Called after fetch finished with error. */
96
- onError?: (error: unknown, params: P | undefined, store: Store) => void;
98
+ /** Called after fetch finished with error. Should return true if error was handled and does not require global onError handling. */
99
+ onError?: (error: unknown, params: P | undefined, store: Store) => boolean | void | null | undefined;
97
100
  };
98
101
  export type QueryState<P, R> = MutationState<P, R> & {
99
102
  expiresAt?: number;
package/dist/useQuery.js CHANGED
@@ -16,7 +16,7 @@ const query_1 = require("./query");
16
16
  const utilsAndConstants_1 = require("./utilsAndConstants");
17
17
  const useQuery = (cache, actions, options) => {
18
18
  var _a, _b, _c;
19
- const { query: queryKey, skip, params, secondsToLive, cachePolicy = (_a = cache.queries[queryKey].cachePolicy) !== null && _a !== void 0 ? _a : cache.defaults.cachePolicy, mergeResults, onCompleted, onSuccess, onError, } = options;
19
+ const { query: queryKey, skip, params, secondsToLive, cachePolicy = (_a = cache.queries[queryKey].cachePolicy) !== null && _a !== void 0 ? _a : cache.globals.cachePolicy, mergeResults, onCompleted, onSuccess, onError, } = options;
20
20
  const logsEnabled = cache.options.logsEnabled;
21
21
  const getCacheKey = (_b = cache.queries[queryKey].getCacheKey) !== null && _b !== void 0 ? _b : (utilsAndConstants_1.defaultGetCacheKey);
22
22
  const cacheStateSelector = cache.cacheStateSelector;
@@ -65,7 +65,7 @@ const applyEntityChanges = (entities, changes, options) => {
65
65
  continue;
66
66
  }
67
67
  // check for key intersection
68
- if (options.validateFunctionArguments) {
68
+ if (options.additionalValidation) {
69
69
  const mergeIds = entitiesToMerge && Object.keys(entitiesToMerge);
70
70
  const replaceIds = entitiesToReplace && Object.keys(entitiesToReplace);
71
71
  const idsSet = new Set(mergeIds);
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "react-redux-cache",
3
3
  "author": "Alexander Danilov",
4
4
  "license": "MIT",
5
- "version": "0.8.2",
5
+ "version": "0.10.0",
6
6
  "description": "Powerful data fetching and caching library that supports normalization, built on top of redux",
7
7
  "main": "dist/index.js",
8
8
  "types": "dist/index.d.ts",