react-redux-cache 0.18.0 → 0.18.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.
@@ -175,6 +175,11 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
175
175
  useSelectEntityById: <TN extends keyof T>(id: Key | null | undefined, typename: TN) => T[TN] | undefined;
176
176
  };
177
177
  utils: {
178
+ /** Creates client by providing the store. Can be used when the store is a singleton - to not use a hook for getting the client, but import it directly. */
179
+ createClient: (store: Store) => {
180
+ query: <QK extends keyof (QP & QR)>(options: QueryOptions<N, T, QP, QR, QK, MP, MR>) => Promise<QueryResult<QK extends keyof QP & keyof QR ? QR[QK] : never>>;
181
+ mutate: <MK extends keyof (MP & MR)>(options: MutateOptions<N, T, QP, QR, MP, MR, MK>) => Promise<MutationResult<MK extends keyof MP & keyof MR ? MR[MK] : never>>;
182
+ };
178
183
  /** Generates the initial state by calling a reducer. Not needed for redux — it already generates it the same way when creating the store. */
179
184
  getInitialState: () => import("./types").CacheState<T, QP, QR, MP, MR>;
180
185
  /** Apply changes to the entities map.
@@ -359,6 +364,11 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCach
359
364
  useSelectEntityById: <TN extends string>(id: Key | null | undefined, typename: TN) => object | undefined;
360
365
  };
361
366
  utils: {
367
+ /** Creates client by providing the store. Can be used when the store is a singleton - to not use a hook for getting the client, but import it directly. */
368
+ createClient: (store: Store) => {
369
+ query: <QK_1 extends keyof QP | keyof QR>(options: QueryOptions<N, Typenames, QP, QR, QK_1, MP, MR>) => Promise<QueryResult<QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never>>;
370
+ mutate: <MK_1 extends keyof MP | keyof MR>(options: MutateOptions<N, Typenames, QP, QR, MP, MR, MK_1>) => Promise<MutationResult<MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>>;
371
+ };
362
372
  /** Generates the initial state by calling a reducer. Not needed for redux — it already generates it the same way when creating the store. */
363
373
  getInitialState: () => import("./types").CacheState<Typenames, QP, QR, MP, MR>;
364
374
  /** Apply changes to the entities map.
@@ -37,10 +37,8 @@ const withTypenames = () => {
37
37
  (_g = (_s = partialCache.globals.queries).fetchPolicy) !== null && _g !== void 0 ? _g : (_s.fetchPolicy = utilsAndConstants_1.FetchPolicy.NoCacheOrExpired);
38
38
  (_h = (_t = partialCache.globals.queries).skipFetch) !== null && _h !== void 0 ? _h : (_t.skipFetch = false);
39
39
  (_j = partialCache.storeHooks) !== null && _j !== void 0 ? _j : (partialCache.storeHooks = {
40
- // eslint-disable-next-line @typescript-eslint/no-var-requires
41
- useStore: require('react-redux').useStore,
42
- // eslint-disable-next-line @typescript-eslint/no-var-requires
43
- useSelector: require('react-redux').useSelector,
40
+ useStore: require.call(undefined, 'react-redux').useStore,
41
+ useSelector: require.call(undefined, 'react-redux').useSelector,
44
42
  });
45
43
  (_k = partialCache.cacheStateSelector) !== null && _k !== void 0 ? _k : (partialCache.cacheStateSelector = (state) => state[cache.name]);
46
44
  (_l = partialCache.mutations) !== null && _l !== void 0 ? _l : (partialCache.mutations = {});
@@ -60,6 +58,27 @@ const withTypenames = () => {
60
58
  const { updateQueryStateAndEntities, updateMutationStateAndEntities, mergeEntityChanges, invalidateQuery, clearQueryState, clearMutationState, clearCache, } = actions;
61
59
  // reducer
62
60
  const reducer = (0, createCacheReducer_1.createCacheReducer)(actions, Object.keys(cache.queries), cache.options);
61
+ // createClient
62
+ const createClient = (store) => {
63
+ const client = {
64
+ query: (options) => {
65
+ var _a;
66
+ const { query: queryKey, params } = options;
67
+ const getCacheKey = (_a = cache.queries[queryKey].getCacheKey) !== null && _a !== void 0 ? _a : (utilsAndConstants_1.defaultGetCacheKey);
68
+ // @ts-expect-error fix later
69
+ const cacheKey = getCacheKey(params);
70
+ return (0, query_1.query)('query', store, cache, actions, selectors, queryKey, cacheKey, params, options.secondsToLive, options.onlyIfExpired,
71
+ // @ts-expect-error fix later
72
+ options.mergeResults, options.onCompleted, options.onSuccess, options.onError);
73
+ },
74
+ mutate: (options) => {
75
+ return (0, mutate_1.mutate)('mutate', store, cache, actions, selectors, options.mutation, options.params, abortControllers,
76
+ // @ts-expect-error fix later
77
+ options.onCompleted, options.onSuccess, options.onError);
78
+ },
79
+ };
80
+ return client;
81
+ };
63
82
  return {
64
83
  /** Keeps all options, passed while creating the cache. */
65
84
  cache,
@@ -118,26 +137,7 @@ const withTypenames = () => {
118
137
  /** Returns client object with query and mutate functions. */
119
138
  useClient: () => {
120
139
  const store = cache.storeHooks.useStore();
121
- return (0, react_1.useMemo)(() => {
122
- const client = {
123
- query: (options) => {
124
- var _a;
125
- const { query: queryKey, params } = options;
126
- const getCacheKey = (_a = cache.queries[queryKey].getCacheKey) !== null && _a !== void 0 ? _a : (utilsAndConstants_1.defaultGetCacheKey);
127
- // @ts-expect-error fix later
128
- const cacheKey = getCacheKey(params);
129
- return (0, query_1.query)('query', store, cache, actions, selectors, queryKey, cacheKey, params, options.secondsToLive, options.onlyIfExpired,
130
- // @ts-expect-error fix later
131
- options.mergeResults, options.onCompleted, options.onSuccess, options.onError);
132
- },
133
- mutate: (options) => {
134
- return (0, mutate_1.mutate)('mutate', store, cache, actions, selectors, options.mutation, options.params, abortControllers,
135
- // @ts-expect-error fix later
136
- options.onCompleted, options.onSuccess, options.onError);
137
- },
138
- };
139
- return client;
140
- }, [store]);
140
+ return (0, react_1.useMemo)(() => createClient(store), [store]);
141
141
  },
142
142
  /** Fetches query when params change and subscribes to query state changes (except `expiresAt` field). */
143
143
  useQuery: (options) => (0, useQuery_1.useQuery)(cache, actions, selectors, options),
@@ -149,6 +149,8 @@ const withTypenames = () => {
149
149
  },
150
150
  },
151
151
  utils: {
152
+ /** Creates client by providing the store. Can be used when the store is a singleton - to not use a hook for getting the client, but import it directly. */
153
+ createClient,
152
154
  /** Generates the initial state by calling a reducer. Not needed for redux — it already generates it the same way when creating the store. */
153
155
  getInitialState: () => {
154
156
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
package/dist/query.js CHANGED
@@ -10,7 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.query = void 0;
13
- const cache_1 = require("./testing/redux/cache");
14
13
  const utilsAndConstants_1 = require("./utilsAndConstants");
15
14
  const query = (logTag, store, cache, actions, selectors, queryKey, cacheKey, params, secondsToLive, onlyIfExpired, mergeResults, onCompleted, onSuccess, onError) => __awaiter(void 0, void 0, void 0, function* () {
16
15
  var _a, _b, _c, _d;
@@ -19,11 +18,9 @@ const query = (logTag, store, cache, actions, selectors, queryKey, cacheKey, par
19
18
  if (onCompleted === void 0) { onCompleted = cache.queries[queryKey].onCompleted; }
20
19
  if (onSuccess === void 0) { onSuccess = cache.queries[queryKey].onSuccess; }
21
20
  if (onError === void 0) { onError = cache.queries[queryKey].onError; }
22
- const { selectQueryResult } = selectors;
21
+ const { selectQueryResult, selectQueryState } = selectors;
23
22
  const logsEnabled = cache.options.logsEnabled;
24
- const queryStateOnStart = (0, cache_1.selectQueryState)(store.getState(),
25
- // @ts-expect-error TODO fix types
26
- queryKey, cacheKey);
23
+ const queryStateOnStart = selectQueryState(store.getState(), queryKey, cacheKey);
27
24
  if (queryStateOnStart === null || queryStateOnStart === void 0 ? void 0 : queryStateOnStart.loading) {
28
25
  logsEnabled &&
29
26
  (0, utilsAndConstants_1.log)(`${logTag} fetch cancelled: already loading`, {
@@ -52,7 +49,6 @@ const query = (logTag, store, cache, actions, selectors, queryKey, cacheKey, par
52
49
  });
53
50
  return {
54
51
  cancelled: 'not-expired',
55
- // @ts-expect-error TODO fix types
56
52
  result: queryStateOnStart.result,
57
53
  };
58
54
  }
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.18.0",
5
+ "version": "0.18.2",
6
6
  "description": "Powerful data fetching and caching library for Redux and Zustand that supports normalization.",
7
7
  "main": "dist/index.js",
8
8
  "types": "dist/index.d.ts",