react-redux-cache 0.19.4 → 0.19.6

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
@@ -23,7 +23,7 @@ Can be considered as `ApolloClient` for protocols other than `GraphQL`, but with
23
23
  |Not overengineered|Simplicity is the main goal.|
24
24
  |Performance|Every function is heavily optimized, Immer is not used ([RTK [Query] issue](https://github.com/reduxjs/redux-toolkit/issues/4793)).|
25
25
  |Reliability|High test coverage, zero issue policy.|
26
- |Lightweight|`npx minified-size dist/esm/*.js`<br/>minified: 18.3 kB<br/>gzipped: 7.91 kB<br/>brotlied: 7.02 kB|
26
+ |Lightweight|`npx minified-size dist/esm/*.js`<br/>minified: 18.2 kB<br/>gzipped: 7.91 kB<br/>brotlied: 7.03 kB|
27
27
 
28
28
  |Feature|Description|
29
29
  |--|--|
@@ -127,6 +127,8 @@ Can be considered as `ApolloClient` for protocols other than `GraphQL`, but with
127
127
  }
128
128
  ```
129
129
  </details>
130
+
131
+ [API Reference](DOCUMENTATION.md)
130
132
 
131
133
  ### Table of contents
132
134
 
@@ -3,7 +3,7 @@ Object.defineProperty(exports, '__esModule', {value: true})
3
3
  exports.createCache = exports.withTypenames = void 0
4
4
  const react_1 = require('react')
5
5
  const createActions_1 = require('./createActions')
6
- const createCacheReducer_1 = require('./createCacheReducer')
6
+ const createReducer_1 = require('./createReducer')
7
7
  const createSelectors_1 = require('./createSelectors')
8
8
  const mutate_1 = require('./mutate')
9
9
  const query_1 = require('./query')
@@ -100,11 +100,7 @@ const withTypenames = () => {
100
100
  clearMutationState,
101
101
  clearCache,
102
102
  } = actions
103
- const reducer = (0, createCacheReducer_1.createCacheReducer)(
104
- actions,
105
- Object.keys(cache.queries),
106
- cache.options
107
- )
103
+ const reducer = (0, createReducer_1.createReducer)(actions, Object.keys(cache.queries), cache.options)
108
104
  const createClient = (store) => {
109
105
  const client = {
110
106
  query: (options) => {
@@ -126,6 +122,7 @@ const withTypenames = () => {
126
122
  params,
127
123
  options.secondsToLive,
128
124
  options.onlyIfExpired,
125
+ options.skipFetch,
129
126
  options.mergeResults,
130
127
  options.onCompleted,
131
128
  options.onSuccess,
@@ -18,12 +18,12 @@ var __rest =
18
18
  return t
19
19
  }
20
20
  Object.defineProperty(exports, '__esModule', {value: true})
21
- exports.createCacheReducer = void 0
21
+ exports.createReducer = void 0
22
22
  const utilsAndConstants_1 = require('./utilsAndConstants')
23
23
 
24
24
  const optionalQueryKeys = ['error', 'expiresAt', 'result', 'params', 'loading']
25
25
  const optionalMutationKeys = ['error', 'result', 'params', 'loading']
26
- const createCacheReducer = (actions, queryKeys, cacheOptions) => {
26
+ const createReducer = (actions, queryKeys, cacheOptions) => {
27
27
  const initialState = Object.freeze({
28
28
  entities: Object.freeze({}),
29
29
  queries: Object.freeze(
@@ -282,4 +282,4 @@ const createCacheReducer = (actions, queryKeys, cacheOptions) => {
282
282
  return state
283
283
  }
284
284
  }
285
- exports.createCacheReducer = createCacheReducer
285
+ exports.createReducer = createReducer
package/dist/cjs/query.js CHANGED
@@ -45,6 +45,7 @@ const query = (
45
45
  params,
46
46
  secondsToLive,
47
47
  onlyIfExpired,
48
+ skipFetch,
48
49
  mergeResults,
49
50
  onCompleted,
50
51
  onSuccess,
@@ -73,6 +74,11 @@ const query = (
73
74
  const {selectQueryResult, selectQueryState} = selectors
74
75
  const logsEnabled = cache.options.logsEnabled
75
76
  const queryStateOnStart = selectQueryState(store.getState(), queryKey, cacheKey)
77
+ if (skipFetch) {
78
+ return {
79
+ result: queryStateOnStart.result,
80
+ }
81
+ }
76
82
  if (queryStateOnStart === null || queryStateOnStart === void 0 ? void 0 : queryStateOnStart.loading) {
77
83
  logsEnabled &&
78
84
  (0, utilsAndConstants_1.log)(`${logTag} fetch cancelled: already loading`, {
@@ -38,18 +38,13 @@ const utilsAndConstants_1 = require('./utilsAndConstants')
38
38
 
39
39
  const useMutation = (cache, actions, selectors, options, abortControllers) => {
40
40
  var _a
41
+ const {selectMutationState} = selectors
42
+ const {updateMutationStateAndEntities} = actions
41
43
  const {mutation: mutationKey, onCompleted, onSuccess, onError} = options
42
44
  const store = cache.storeHooks.useStore()
43
45
  const [mutationStateSelector, mutate, abort] = (0, react_1.useMemo)(() => {
44
46
  return [
45
- (state) => {
46
- cache.options.logsEnabled &&
47
- (0, utilsAndConstants_1.log)('mutationStateSelector', {
48
- state,
49
- cacheState: cache.cacheStateSelector(state),
50
- })
51
- return cache.cacheStateSelector(state).mutations[mutationKey]
52
- },
47
+ (state) => selectMutationState(state, mutationKey),
53
48
  (params) =>
54
49
  __awaiter(void 0, void 0, void 0, function* () {
55
50
  return yield (0,
@@ -63,7 +58,7 @@ const useMutation = (cache, actions, selectors, options, abortControllers) => {
63
58
  return false
64
59
  }
65
60
  abortController.abort()
66
- store.dispatch(actions.updateMutationStateAndEntities(mutationKey, {loading: undefined}))
61
+ store.dispatch(updateMutationStateAndEntities(mutationKey, {loading: undefined}))
67
62
  return true
68
63
  },
69
64
  ]
@@ -75,7 +75,7 @@ const useQuery = (cache, actions, selectors, options) => {
75
75
  __awaiter(void 0, void 0, void 0, function* () {
76
76
  const paramsPassed = options && 'params' in options
77
77
  return yield (0,
78
- query_1.query)('useQuery.fetch', store, cache, actions, selectors, queryKey, paramsPassed ? getCacheKey(options.params) : cacheKey, paramsPassed ? options.params : params, secondsToLive, options === null || options === void 0 ? void 0 : options.onlyIfExpired, mergeResults, onCompleted, onSuccess, onError)
78
+ query_1.query)('useQuery.fetch', store, cache, actions, selectors, queryKey, paramsPassed ? getCacheKey(options.params) : cacheKey, paramsPassed ? options.params : params, secondsToLive, options === null || options === void 0 ? void 0 : options.onlyIfExpired, false, mergeResults, onCompleted, onSuccess, onError)
79
79
  }),
80
80
  [store, queryKey, cacheKey]
81
81
  )
@@ -1,9 +1,9 @@
1
1
  'use strict'
2
2
  Object.defineProperty(exports, '__esModule', {value: true})
3
- exports.createStateComparer =
3
+ exports.FetchPolicy =
4
+ exports.createStateComparer =
4
5
  exports.isEmptyObject =
5
6
  exports.applyEntityChanges =
6
- exports.FetchPolicy =
7
7
  exports.log =
8
8
  exports.defaultGetCacheKey =
9
9
  exports.NOOP =
@@ -49,12 +49,6 @@ const log = (tag, data) => {
49
49
  console.debug(`@${exports.PACKAGE_SHORT_NAME} [${tag}]`, data)
50
50
  }
51
51
  exports.log = log
52
- exports.FetchPolicy = {
53
- NoCacheOrExpired: (expired, _params, state) => {
54
- return expired || state.result === undefined
55
- },
56
- Always: () => true,
57
- }
58
52
  const applyEntityChanges = (entities, changes, options) => {
59
53
  var _a, _b, _c, _d
60
54
  if (changes.merge && changes.entities) {
@@ -187,3 +181,9 @@ const createStateComparer = (fields) => {
187
181
  }
188
182
  }
189
183
  exports.createStateComparer = createStateComparer
184
+ exports.FetchPolicy = {
185
+ NoCacheOrExpired: (expired, _params, state) => {
186
+ return expired || state.result === undefined
187
+ },
188
+ Always: () => true,
189
+ }
@@ -1,7 +1,7 @@
1
1
  import {useMemo} from 'react'
2
2
 
3
3
  import {createActions} from './createActions'
4
- import {createCacheReducer} from './createCacheReducer'
4
+ import {createReducer} from './createReducer'
5
5
  import {createSelectors} from './createSelectors'
6
6
  import {mutate as mutateImpl} from './mutate'
7
7
  import {query as queryImpl} from './query'
@@ -103,7 +103,7 @@ export const withTypenames = () => {
103
103
  clearMutationState,
104
104
  clearCache,
105
105
  } = actions
106
- const reducer = createCacheReducer(actions, Object.keys(cache.queries), cache.options)
106
+ const reducer = createReducer(actions, Object.keys(cache.queries), cache.options)
107
107
  const createClient = (store) => {
108
108
  const client = {
109
109
  query: (options) => {
@@ -123,6 +123,7 @@ export const withTypenames = () => {
123
123
  params,
124
124
  options.secondsToLive,
125
125
  options.onlyIfExpired,
126
+ options.skipFetch,
126
127
  options.mergeResults,
127
128
  options.onCompleted,
128
129
  options.onSuccess,
@@ -200,4 +201,5 @@ export const withTypenames = () => {
200
201
  },
201
202
  }
202
203
  }
204
+
203
205
  export const createCache = withTypenames().createCache
@@ -20,7 +20,8 @@ import {applyEntityChanges, EMPTY_OBJECT, isEmptyObject, log, optionalUtils} fro
20
20
 
21
21
  const optionalQueryKeys = ['error', 'expiresAt', 'result', 'params', 'loading']
22
22
  const optionalMutationKeys = ['error', 'result', 'params', 'loading']
23
- export const createCacheReducer = (actions, queryKeys, cacheOptions) => {
23
+
24
+ export const createReducer = (actions, queryKeys, cacheOptions) => {
24
25
  const initialState = Object.freeze({
25
26
  entities: Object.freeze({}),
26
27
  queries: Object.freeze(
package/dist/esm/index.js CHANGED
@@ -1,3 +1,5 @@
1
1
  export {createCache, withTypenames} from './createCache'
2
+
2
3
  export * from './types'
4
+
3
5
  export {createStateComparer, defaultGetCacheKey, FetchPolicy, isEmptyObject} from './utilsAndConstants'
@@ -154,4 +154,5 @@ export const mutate = (
154
154
  throw new Error(`${logTag}: both error and response are not defined`)
155
155
  }
156
156
  )
157
+
157
158
  const ABORTED_RESULT = Object.freeze({aborted: true})
package/dist/esm/query.js CHANGED
@@ -42,6 +42,7 @@ export const query = (
42
42
  params,
43
43
  secondsToLive,
44
44
  onlyIfExpired,
45
+ skipFetch,
45
46
  mergeResults,
46
47
  onCompleted,
47
48
  onSuccess,
@@ -70,6 +71,11 @@ export const query = (
70
71
  const {selectQueryResult, selectQueryState} = selectors
71
72
  const logsEnabled = cache.options.logsEnabled
72
73
  const queryStateOnStart = selectQueryState(store.getState(), queryKey, cacheKey)
74
+ if (skipFetch) {
75
+ return {
76
+ result: queryStateOnStart.result,
77
+ }
78
+ }
73
79
  if (queryStateOnStart === null || queryStateOnStart === void 0 ? void 0 : queryStateOnStart.loading) {
74
80
  logsEnabled &&
75
81
  log(`${logTag} fetch cancelled: already loading`, {
@@ -166,4 +172,5 @@ export const query = (
166
172
  result: newState === null || newState === void 0 ? void 0 : newState.result,
167
173
  }
168
174
  })
175
+
169
176
  const catchAndReturn = (x) => x
@@ -36,18 +36,13 @@ import {EMPTY_OBJECT, log} from './utilsAndConstants'
36
36
 
37
37
  export const useMutation = (cache, actions, selectors, options, abortControllers) => {
38
38
  var _a
39
+ const {selectMutationState} = selectors
40
+ const {updateMutationStateAndEntities} = actions
39
41
  const {mutation: mutationKey, onCompleted, onSuccess, onError} = options
40
42
  const store = cache.storeHooks.useStore()
41
43
  const [mutationStateSelector, mutate, abort] = useMemo(() => {
42
44
  return [
43
- (state) => {
44
- cache.options.logsEnabled &&
45
- log('mutationStateSelector', {
46
- state,
47
- cacheState: cache.cacheStateSelector(state),
48
- })
49
- return cache.cacheStateSelector(state).mutations[mutationKey]
50
- },
45
+ (state) => selectMutationState(state, mutationKey),
51
46
  (params) =>
52
47
  __awaiter(void 0, void 0, void 0, function* () {
53
48
  return yield mutateImpl(
@@ -72,7 +67,7 @@ export const useMutation = (cache, actions, selectors, options, abortControllers
72
67
  return false
73
68
  }
74
69
  abortController.abort()
75
- store.dispatch(actions.updateMutationStateAndEntities(mutationKey, {loading: undefined}))
70
+ store.dispatch(updateMutationStateAndEntities(mutationKey, {loading: undefined}))
76
71
  return true
77
72
  },
78
73
  ]
@@ -82,6 +82,7 @@ export const useQuery = (cache, actions, selectors, options) => {
82
82
  paramsPassed ? options.params : params,
83
83
  secondsToLive,
84
84
  options === null || options === void 0 ? void 0 : options.onlyIfExpired,
85
+ false,
85
86
  mergeResults,
86
87
  onCompleted,
87
88
  onSuccess,
@@ -122,4 +123,5 @@ export const useQuery = (cache, actions, selectors, options) => {
122
123
  })
123
124
  return [queryState, performFetch]
124
125
  }
126
+
125
127
  const defaultStateComparer = createStateComparer(['result', 'loading', 'params', 'error'])
@@ -1,12 +1,15 @@
1
1
  export const PACKAGE_SHORT_NAME = 'rrc'
2
+
2
3
  export const optionalUtils = {
3
4
  deepEqual: undefined,
4
5
  }
6
+
5
7
  try {
6
8
  optionalUtils.deepEqual = require('fast-deep-equal/es6')
7
9
  } catch (_a) {
8
10
  console.debug(PACKAGE_SHORT_NAME + ': fast-deep-equal optional dependency was not installed')
9
11
  }
12
+
10
13
  export const IS_DEV = (() => {
11
14
  try {
12
15
  return __DEV__
@@ -14,9 +17,13 @@ export const IS_DEV = (() => {
14
17
  return process.env.NODE_ENV === 'development'
15
18
  }
16
19
  })()
20
+
17
21
  export const EMPTY_OBJECT = Object.freeze({})
22
+
18
23
  export const EMPTY_ARRAY = Object.freeze([])
24
+
19
25
  export const NOOP = () => {}
26
+
20
27
  export const defaultGetCacheKey = (params) => {
21
28
  switch (typeof params) {
22
29
  case 'string':
@@ -28,15 +35,11 @@ export const defaultGetCacheKey = (params) => {
28
35
  return String(params)
29
36
  }
30
37
  }
38
+
31
39
  export const log = (tag, data) => {
32
40
  console.debug(`@${PACKAGE_SHORT_NAME} [${tag}]`, data)
33
41
  }
34
- export const FetchPolicy = {
35
- NoCacheOrExpired: (expired, _params, state) => {
36
- return expired || state.result === undefined
37
- },
38
- Always: () => true,
39
- }
42
+
40
43
  export const applyEntityChanges = (entities, changes, options) => {
41
44
  var _a, _b, _c, _d
42
45
  if (changes.merge && changes.entities) {
@@ -143,12 +146,14 @@ export const applyEntityChanges = (entities, changes, options) => {
143
146
  })
144
147
  return result
145
148
  }
149
+
146
150
  export const isEmptyObject = (o) => {
147
151
  for (const _ in o) {
148
152
  return false
149
153
  }
150
154
  return true
151
155
  }
156
+
152
157
  export const createStateComparer = (fields) => {
153
158
  return (x, y) => {
154
159
  if (x === y) {
@@ -166,3 +171,10 @@ export const createStateComparer = (fields) => {
166
171
  return true
167
172
  }
168
173
  }
174
+
175
+ export const FetchPolicy = {
176
+ NoCacheOrExpired: (expired, _params, state) => {
177
+ return expired || state.result === undefined
178
+ },
179
+ Always: () => true,
180
+ }
@@ -9,6 +9,7 @@ export type Actions<
9
9
  MP = unknown,
10
10
  MR = unknown
11
11
  > = ReturnType<typeof createActions<N, T, QP, QR, MP, MR>>
12
+
12
13
  export declare const createActions: <N extends string, T extends Typenames, QP, QR, MP, MR>(
13
14
  name: N
14
15
  ) => {
@@ -1,28 +1,38 @@
1
1
  import type {
2
2
  Cache,
3
3
  CacheOptions,
4
+ CacheState,
5
+ Dict,
6
+ EntitiesMap,
7
+ EntityChanges,
4
8
  Globals,
5
9
  Key,
6
10
  MutateOptions,
11
+ MutationInfo,
7
12
  MutationResult,
13
+ MutationState,
14
+ NormalizedQueryResponse,
8
15
  OptionalPartial,
16
+ QueryInfo,
9
17
  QueryOptions,
10
18
  QueryResult,
19
+ QueryState,
11
20
  Store,
12
21
  Typenames,
22
+ UseQueryOptions,
13
23
  } from './types'
14
24
  import {useMutation} from './useMutation'
15
25
  import {useQuery} from './useQuery'
16
26
  import {applyEntityChanges} from './utilsAndConstants'
27
+
17
28
  /**
18
29
  * Function to provide generic Typenames if normalization is needed - this is a Typescript limitation.
19
30
  * Returns object with createCache function with provided typenames.
20
31
  * @example
21
- * const cache = withTypenames<MyTypenames>().createCache({
22
- * ...
23
- * })
32
+ * `const cache = withTypenames<MyTypenames>().createCache({...})`
24
33
  */
25
34
  export declare const withTypenames: <T extends Typenames = Typenames>() => {
35
+ /** Creates reducer, actions and hooks for managing queries and mutations through redux cache. */
26
36
  createCache: <N extends string, QP, QR, MP, MR>(
27
37
  partialCache: OptionalPartial<
28
38
  Omit<Cache<N, T, QP, QR, MP, MR>, 'globals'>,
@@ -35,28 +45,24 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
35
45
  cache: Cache<N, T, QP, QR, MP, MR>
36
46
  /** Reducer of the cache, should be added to redux store. */
37
47
  reducer: (
38
- state: import('./types').CacheState<T, QP, QR, MP, MR> | undefined,
48
+ state: CacheState<T, QP, QR, MP, MR> | undefined,
39
49
  action:
40
50
  | {
41
51
  type: `@rrc/${N}/updateQueryStateAndEntities`
42
52
  queryKey: keyof QP & keyof QR
43
53
  queryCacheKey: Key
44
- state:
45
- | Partial<import('./types').QueryState<T, QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>>
46
- | undefined
47
- entityChanges: import('./types').EntityChanges<T> | undefined
54
+ state: Partial<QueryState<T, QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>> | undefined
55
+ entityChanges: EntityChanges<T> | undefined
48
56
  }
49
57
  | {
50
58
  type: `@rrc/${N}/updateMutationStateAndEntities`
51
59
  mutationKey: keyof MP & keyof MR
52
- state:
53
- | Partial<import('./types').MutationState<T, MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>>
54
- | undefined
55
- entityChanges: import('./types').EntityChanges<T> | undefined
60
+ state: Partial<MutationState<T, MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>> | undefined
61
+ entityChanges: EntityChanges<T> | undefined
56
62
  }
57
63
  | {
58
64
  type: `@rrc/${N}/mergeEntityChanges`
59
- changes: import('./types').EntityChanges<T>
65
+ changes: EntityChanges<T>
60
66
  }
61
67
  | {
62
68
  type: `@rrc/${N}/invalidateQuery`
@@ -79,23 +85,23 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
79
85
  }
80
86
  | {
81
87
  type: `@rrc/${N}/clearCache`
82
- stateToKeep: Partial<import('./types').CacheState<T, QP, QR, MP, MR>> | undefined
88
+ stateToKeep: Partial<CacheState<T, QP, QR, MP, MR>> | undefined
83
89
  }
84
- ) => import('./types').CacheState<T, QP, QR, MP, MR>
90
+ ) => CacheState<T, QP, QR, MP, MR>
85
91
  actions: {
86
92
  /** Updates query state, and optionally merges entity changes in a single action. */
87
93
  updateQueryStateAndEntities: {
88
94
  <K extends keyof QP & keyof QR>(
89
95
  queryKey: K,
90
96
  queryCacheKey: Key,
91
- state?: Partial<import('./types').QueryState<T, QP[K], QR[K]>> | undefined,
92
- entityChanges?: import('./types').EntityChanges<T> | undefined
97
+ state?: Partial<QueryState<T, QP[K], QR[K]>> | undefined,
98
+ entityChanges?: EntityChanges<T> | undefined
93
99
  ): {
94
100
  type: `@rrc/${N}/updateQueryStateAndEntities`
95
101
  queryKey: K
96
102
  queryCacheKey: Key
97
- state: Partial<import('./types').QueryState<T, QP[K], QR[K]>> | undefined
98
- entityChanges: import('./types').EntityChanges<T> | undefined
103
+ state: Partial<QueryState<T, QP[K], QR[K]>> | undefined
104
+ entityChanges: EntityChanges<T> | undefined
99
105
  }
100
106
  type: `@rrc/${N}/updateQueryStateAndEntities`
101
107
  }
@@ -103,21 +109,21 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
103
109
  updateMutationStateAndEntities: {
104
110
  <K extends keyof MP & keyof MR>(
105
111
  mutationKey: K,
106
- state?: Partial<import('./types').MutationState<T, MP[K], MR[K]>> | undefined,
107
- entityChanges?: import('./types').EntityChanges<T> | undefined
112
+ state?: Partial<MutationState<T, MP[K], MR[K]>> | undefined,
113
+ entityChanges?: EntityChanges<T> | undefined
108
114
  ): {
109
115
  type: `@rrc/${N}/updateMutationStateAndEntities`
110
116
  mutationKey: K
111
- state: Partial<import('./types').MutationState<T, MP[K], MR[K]>> | undefined
112
- entityChanges: import('./types').EntityChanges<T> | undefined
117
+ state: Partial<MutationState<T, MP[K], MR[K]>> | undefined
118
+ entityChanges: EntityChanges<T> | undefined
113
119
  }
114
120
  type: `@rrc/${N}/updateMutationStateAndEntities`
115
121
  }
116
122
  /** Merges EntityChanges to the state. */
117
123
  mergeEntityChanges: {
118
- (changes: import('./types').EntityChanges<T>): {
124
+ (changes: EntityChanges<T>): {
119
125
  type: `@rrc/${N}/mergeEntityChanges`
120
- changes: import('./types').EntityChanges<T>
126
+ changes: EntityChanges<T>
121
127
  }
122
128
  type: `@rrc/${N}/mergeEntityChanges`
123
129
  }
@@ -166,22 +172,22 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
166
172
  }
167
173
  /** Replaces cache state with initial, optionally merging with provided state. Doesn't cancel running fetches and shoult be used with caution. */
168
174
  clearCache: {
169
- (stateToKeep?: Partial<import('./types').CacheState<T, QP, QR, MP, MR>> | undefined): {
175
+ (stateToKeep?: Partial<CacheState<T, QP, QR, MP, MR>> | undefined): {
170
176
  type: `@rrc/${N}/clearCache`
171
- stateToKeep: Partial<import('./types').CacheState<T, QP, QR, MP, MR>> | undefined
177
+ stateToKeep: Partial<CacheState<T, QP, QR, MP, MR>> | undefined
172
178
  }
173
179
  type: `@rrc/${N}/clearCache`
174
180
  }
175
181
  }
176
182
  selectors: {
177
183
  /** This is a cacheStateSelector from createCache options, or default one if was not provided. */
178
- selectCacheState: (state: any) => import('./types').CacheState<T, QP, QR, MP, MR>
184
+ selectCacheState: (state: any) => CacheState<T, QP, QR, MP, MR>
179
185
  /** Selects query state. */
180
186
  selectQueryState: <QK extends keyof QP | keyof QR>(
181
187
  state: unknown,
182
188
  query: QK,
183
189
  cacheKey: Key
184
- ) => import('./types').QueryState<
190
+ ) => QueryState<
185
191
  T,
186
192
  QK extends keyof QP & keyof QR ? QP[QK] : never,
187
193
  QK extends keyof QP & keyof QR ? QR[QK] : never
@@ -197,11 +203,7 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
197
203
  state: unknown,
198
204
  query: QK,
199
205
  cacheKey: Key
200
- ) =>
201
- | false
202
- | Promise<
203
- import('./types').NormalizedQueryResponse<T, QK extends keyof QP & keyof QR ? QR[QK] : never>
204
- >
206
+ ) => false | Promise<NormalizedQueryResponse<T, QK extends keyof QP & keyof QR ? QR[QK] : never>>
205
207
  /** Selects query latest error. */
206
208
  selectQueryError: <QK extends keyof QP | keyof QR>(
207
209
  state: unknown,
@@ -224,7 +226,7 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
224
226
  selectMutationState: <MK extends keyof MP | keyof MR>(
225
227
  state: unknown,
226
228
  mutation: MK
227
- ) => import('./types').MutationState<
229
+ ) => MutationState<
228
230
  T,
229
231
  MK extends keyof MP & keyof MR ? MP[MK] : never,
230
232
  MK extends keyof MP & keyof MR ? MR[MK] : never
@@ -238,11 +240,7 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
238
240
  selectMutationLoading: <MK extends keyof MP | keyof MR>(
239
241
  state: unknown,
240
242
  mutation: MK
241
- ) =>
242
- | false
243
- | Promise<
244
- import('./types').NormalizedQueryResponse<T, MK extends keyof MP & keyof MR ? MR[MK] : never>
245
- >
243
+ ) => false | Promise<NormalizedQueryResponse<T, MK extends keyof MP & keyof MR ? MR[MK] : never>>
246
244
  /** Selects mutation latest error. */
247
245
  selectMutationError: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => Error | undefined
248
246
  /** Selects mutation latest params. */
@@ -257,15 +255,12 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
257
255
  typename: TN
258
256
  ) => T[TN] | undefined
259
257
  /** Selects all entities. */
260
- selectEntities: (state: unknown) => import('./types').EntitiesMap<T>
258
+ selectEntities: (state: unknown) => EntitiesMap<T>
261
259
  /** Selects all entities of provided typename. */
262
- selectEntitiesByTypename: <TN extends keyof T>(
263
- state: unknown,
264
- typename: TN
265
- ) => import('./types').EntitiesMap<T>[TN]
260
+ selectEntitiesByTypename: <TN extends keyof T>(state: unknown, typename: TN) => EntitiesMap<T>[TN]
266
261
  }
267
262
  hooks: {
268
- /** Returns client object with query and mutate functions. */
263
+ /** Returns memoized object with query and mutate functions. Memoization dependency is the store. */
269
264
  useClient: () => {
270
265
  query: <QK extends keyof (QP & QR)>(
271
266
  options: QueryOptions<N, T, QP, QR, QK, MP, MR>
@@ -274,12 +269,12 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
274
269
  options: MutateOptions<N, T, QP, QR, MP, MR, MK>
275
270
  ) => Promise<MutationResult<MK extends keyof MP & keyof MR ? MR[MK] : never>>
276
271
  }
277
- /** Fetches query when params change and subscribes to query state changes (except `expiresAt` field). */
272
+ /** Fetches query when params change and subscribes to query state changes (subscription depends on `selectorComparer`). */
278
273
  useQuery: <QK extends keyof (QP & QR)>(
279
274
  options: Parameters<typeof useQuery<N, T, QP, QR, MP, MR, QK>>[3]
280
275
  ) => readonly [
281
276
  Omit<
282
- import('./types').QueryState<
277
+ QueryState<
283
278
  T,
284
279
  QK extends keyof QP & keyof QR ? QP[QK] : never,
285
280
  QK extends keyof QP & keyof QR ? QR[QK] : never
@@ -319,7 +314,7 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
319
314
  : never
320
315
  >
321
316
  >,
322
- import('./types').MutationState<
317
+ MutationState<
323
318
  T,
324
319
  MK extends keyof MP & keyof MR ? MP[MK] : never,
325
320
  MK extends keyof MP & keyof MR ? MP[MK] : never
@@ -340,16 +335,17 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
340
335
  ) => Promise<MutationResult<MK extends keyof MP & keyof MR ? MR[MK] : never>>
341
336
  }
342
337
  /** Generates the initial state by calling a reducer. Not needed for redux — it already generates it the same way when creating the store. */
343
- getInitialState: () => import('./types').CacheState<T, QP, QR, MP, MR>
338
+ getInitialState: () => CacheState<T, QP, QR, MP, MR>
344
339
  /** Apply changes to the entities map.
345
340
  * @returns `undefined` if nothing to change, otherwise new `EntitiesMap<T>` with applied changes. */
346
341
  applyEntityChanges: (
347
342
  entities: Parameters<typeof applyEntityChanges<T>>[0],
348
343
  changes: Parameters<typeof applyEntityChanges<T>>[1]
349
- ) => import('./types').EntitiesMap<T> | undefined
344
+ ) => EntitiesMap<T> | undefined
350
345
  }
351
346
  }
352
347
  }
348
+
353
349
  /**
354
350
  * Creates reducer, actions and hooks for managing queries and mutations through redux cache.
355
351
  */
@@ -357,12 +353,12 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
357
353
  partialCache: Partial<{
358
354
  queries: Partial<{
359
355
  [QK in keyof (QP & QR)]: QK extends keyof QP & keyof QR
360
- ? import('./types').QueryInfo<N, Typenames, QP[QK], QR[QK], QP, QR, MP, MR>
356
+ ? QueryInfo<N, Typenames, QP[QK], QR[QK], QP, QR, MP, MR>
361
357
  : never
362
358
  }>
363
359
  mutations: Partial<{
364
360
  [MK in keyof (MP & MR)]: MK extends keyof MP & keyof MR
365
- ? import('./types').MutationInfo<N, Typenames, MP[MK], MR[MK], QP, QR, MP, MR>
361
+ ? MutationInfo<N, Typenames, MP[MK], MR[MK], QP, QR, MP, MR>
366
362
  : never
367
363
  }>
368
364
  options: Partial<CacheOptions>
@@ -370,7 +366,7 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
370
366
  useStore: () => Store
371
367
  useSelector: <R>(selector: (state: unknown) => R, comparer?: (x: R, y: R) => boolean) => R
372
368
  }>
373
- cacheStateSelector: Partial<(state: any) => import('./types').CacheState<Typenames, QP, QR, MP, MR>>
369
+ cacheStateSelector: Partial<(state: any) => CacheState<Typenames, QP, QR, MP, MR>>
374
370
  }> &
375
371
  Omit<
376
372
  Omit<Cache<N, Typenames, QP, QR, MP, MR>, 'globals'>,
@@ -383,32 +379,26 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
383
379
  cache: Cache<N, Typenames, QP, QR, MP, MR>
384
380
  /** Reducer of the cache, should be added to redux store. */
385
381
  reducer: (
386
- state: import('./types').CacheState<Typenames, QP, QR, MP, MR> | undefined,
382
+ state: CacheState<Typenames, QP, QR, MP, MR> | undefined,
387
383
  action:
388
384
  | {
389
385
  type: `@rrc/${N}/updateQueryStateAndEntities`
390
386
  queryKey: keyof QP & keyof QR
391
387
  queryCacheKey: Key
392
- state:
393
- | Partial<
394
- import('./types').QueryState<Typenames, QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>
395
- >
396
- | undefined
397
- entityChanges: import('./types').EntityChanges<Typenames> | undefined
388
+ state: Partial<QueryState<Typenames, QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>> | undefined
389
+ entityChanges: EntityChanges<Typenames> | undefined
398
390
  }
399
391
  | {
400
392
  type: `@rrc/${N}/updateMutationStateAndEntities`
401
393
  mutationKey: keyof MP & keyof MR
402
394
  state:
403
- | Partial<
404
- import('./types').MutationState<Typenames, MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>
405
- >
395
+ | Partial<MutationState<Typenames, MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>>
406
396
  | undefined
407
- entityChanges: import('./types').EntityChanges<Typenames> | undefined
397
+ entityChanges: EntityChanges<Typenames> | undefined
408
398
  }
409
399
  | {
410
400
  type: `@rrc/${N}/mergeEntityChanges`
411
- changes: import('./types').EntityChanges<Typenames>
401
+ changes: EntityChanges<Typenames>
412
402
  }
413
403
  | {
414
404
  type: `@rrc/${N}/invalidateQuery`
@@ -431,23 +421,23 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
431
421
  }
432
422
  | {
433
423
  type: `@rrc/${N}/clearCache`
434
- stateToKeep: Partial<import('./types').CacheState<Typenames, QP, QR, MP, MR>> | undefined
424
+ stateToKeep: Partial<CacheState<Typenames, QP, QR, MP, MR>> | undefined
435
425
  }
436
- ) => import('./types').CacheState<Typenames, QP, QR, MP, MR>
426
+ ) => CacheState<Typenames, QP, QR, MP, MR>
437
427
  actions: {
438
428
  /** Updates query state, and optionally merges entity changes in a single action. */
439
429
  updateQueryStateAndEntities: {
440
430
  <K extends keyof QP & keyof QR>(
441
431
  queryKey: K,
442
432
  queryCacheKey: Key,
443
- state?: Partial<import('./types').QueryState<Typenames, QP[K], QR[K]>> | undefined,
444
- entityChanges?: import('./types').EntityChanges<Typenames> | undefined
433
+ state?: Partial<QueryState<Typenames, QP[K], QR[K]>> | undefined,
434
+ entityChanges?: EntityChanges<Typenames> | undefined
445
435
  ): {
446
436
  type: `@rrc/${N}/updateQueryStateAndEntities`
447
437
  queryKey: K
448
438
  queryCacheKey: Key
449
- state: Partial<import('./types').QueryState<Typenames, QP[K], QR[K]>> | undefined
450
- entityChanges: import('./types').EntityChanges<Typenames> | undefined
439
+ state: Partial<QueryState<Typenames, QP[K], QR[K]>> | undefined
440
+ entityChanges: EntityChanges<Typenames> | undefined
451
441
  }
452
442
  type: `@rrc/${N}/updateQueryStateAndEntities`
453
443
  }
@@ -455,21 +445,21 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
455
445
  updateMutationStateAndEntities: {
456
446
  <K extends keyof MP & keyof MR>(
457
447
  mutationKey: K,
458
- state?: Partial<import('./types').MutationState<Typenames, MP[K], MR[K]>> | undefined,
459
- entityChanges?: import('./types').EntityChanges<Typenames> | undefined
448
+ state?: Partial<MutationState<Typenames, MP[K], MR[K]>> | undefined,
449
+ entityChanges?: EntityChanges<Typenames> | undefined
460
450
  ): {
461
451
  type: `@rrc/${N}/updateMutationStateAndEntities`
462
452
  mutationKey: K
463
- state: Partial<import('./types').MutationState<Typenames, MP[K], MR[K]>> | undefined
464
- entityChanges: import('./types').EntityChanges<Typenames> | undefined
453
+ state: Partial<MutationState<Typenames, MP[K], MR[K]>> | undefined
454
+ entityChanges: EntityChanges<Typenames> | undefined
465
455
  }
466
456
  type: `@rrc/${N}/updateMutationStateAndEntities`
467
457
  }
468
458
  /** Merges EntityChanges to the state. */
469
459
  mergeEntityChanges: {
470
- (changes: import('./types').EntityChanges<Typenames>): {
460
+ (changes: EntityChanges<Typenames>): {
471
461
  type: `@rrc/${N}/mergeEntityChanges`
472
- changes: import('./types').EntityChanges<Typenames>
462
+ changes: EntityChanges<Typenames>
473
463
  }
474
464
  type: `@rrc/${N}/mergeEntityChanges`
475
465
  }
@@ -518,22 +508,22 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
518
508
  }
519
509
  /** Replaces cache state with initial, optionally merging with provided state. Doesn't cancel running fetches and shoult be used with caution. */
520
510
  clearCache: {
521
- (stateToKeep?: Partial<import('./types').CacheState<Typenames, QP, QR, MP, MR>> | undefined): {
511
+ (stateToKeep?: Partial<CacheState<Typenames, QP, QR, MP, MR>> | undefined): {
522
512
  type: `@rrc/${N}/clearCache`
523
- stateToKeep: Partial<import('./types').CacheState<Typenames, QP, QR, MP, MR>> | undefined
513
+ stateToKeep: Partial<CacheState<Typenames, QP, QR, MP, MR>> | undefined
524
514
  }
525
515
  type: `@rrc/${N}/clearCache`
526
516
  }
527
517
  }
528
518
  selectors: {
529
519
  /** This is a cacheStateSelector from createCache options, or default one if was not provided. */
530
- selectCacheState: (state: any) => import('./types').CacheState<Typenames, QP, QR, MP, MR>
520
+ selectCacheState: (state: any) => CacheState<Typenames, QP, QR, MP, MR>
531
521
  /** Selects query state. */
532
522
  selectQueryState: <QK_1 extends keyof QP | keyof QR>(
533
523
  state: unknown,
534
524
  query: QK_1,
535
525
  cacheKey: Key
536
- ) => import('./types').QueryState<
526
+ ) => QueryState<
537
527
  Typenames,
538
528
  QK_1 extends keyof QP & keyof QR ? QP[QK_1] : never,
539
529
  QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never
@@ -551,12 +541,7 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
551
541
  cacheKey: Key
552
542
  ) =>
553
543
  | false
554
- | Promise<
555
- import('./types').NormalizedQueryResponse<
556
- Typenames,
557
- QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never
558
- >
559
- >
544
+ | Promise<NormalizedQueryResponse<Typenames, QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never>>
560
545
  /** Selects query latest error. */
561
546
  selectQueryError: <QK_1 extends keyof QP | keyof QR>(
562
547
  state: unknown,
@@ -579,7 +564,7 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
579
564
  selectMutationState: <MK_1 extends keyof MP | keyof MR>(
580
565
  state: unknown,
581
566
  mutation: MK_1
582
- ) => import('./types').MutationState<
567
+ ) => MutationState<
583
568
  Typenames,
584
569
  MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never,
585
570
  MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never
@@ -595,12 +580,7 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
595
580
  mutation: MK_1
596
581
  ) =>
597
582
  | false
598
- | Promise<
599
- import('./types').NormalizedQueryResponse<
600
- Typenames,
601
- MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never
602
- >
603
- >
583
+ | Promise<NormalizedQueryResponse<Typenames, MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>>
604
584
  /** Selects mutation latest error. */
605
585
  selectMutationError: <MK_1 extends keyof MP | keyof MR>(
606
586
  state: unknown,
@@ -618,15 +598,12 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
618
598
  typename: TN
619
599
  ) => object | undefined
620
600
  /** Selects all entities. */
621
- selectEntities: (state: unknown) => import('./types').EntitiesMap<Typenames>
601
+ selectEntities: (state: unknown) => EntitiesMap<Typenames>
622
602
  /** Selects all entities of provided typename. */
623
- selectEntitiesByTypename: <TN extends string>(
624
- state: unknown,
625
- typename: TN
626
- ) => import('./types').Dict<object> | undefined
603
+ selectEntitiesByTypename: <TN extends string>(state: unknown, typename: TN) => Dict<object> | undefined
627
604
  }
628
605
  hooks: {
629
- /** Returns client object with query and mutate functions. */
606
+ /** Returns memoized object with query and mutate functions. Memoization dependency is the store. */
630
607
  useClient: () => {
631
608
  query: <QK_1 extends keyof QP | keyof QR>(
632
609
  options: QueryOptions<N, Typenames, QP, QR, QK_1, MP, MR>
@@ -635,12 +612,12 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
635
612
  options: MutateOptions<N, Typenames, QP, QR, MP, MR, MK_1>
636
613
  ) => Promise<MutationResult<MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>>
637
614
  }
638
- /** Fetches query when params change and subscribes to query state changes (except `expiresAt` field). */
615
+ /** Fetches query when params change and subscribes to query state changes (subscription depends on `selectorComparer`). */
639
616
  useQuery: <QK_1 extends keyof QP | keyof QR>(
640
- options: import('./types').UseQueryOptions<N, Typenames, QK_1, QP, QR, MP, MR>
617
+ options: UseQueryOptions<N, Typenames, QK_1, QP, QR, MP, MR>
641
618
  ) => readonly [
642
619
  Omit<
643
- import('./types').QueryState<
620
+ QueryState<
644
621
  Typenames,
645
622
  QK_1 extends keyof QP & keyof QR ? QP[QK_1] : never,
646
623
  QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never
@@ -680,7 +657,7 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
680
657
  : never
681
658
  >
682
659
  >,
683
- import('./types').MutationState<
660
+ MutationState<
684
661
  Typenames,
685
662
  MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never,
686
663
  MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never
@@ -701,12 +678,12 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
701
678
  ) => Promise<MutationResult<MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>>
702
679
  }
703
680
  /** Generates the initial state by calling a reducer. Not needed for redux — it already generates it the same way when creating the store. */
704
- getInitialState: () => import('./types').CacheState<Typenames, QP, QR, MP, MR>
681
+ getInitialState: () => CacheState<Typenames, QP, QR, MP, MR>
705
682
  /** Apply changes to the entities map.
706
683
  * @returns `undefined` if nothing to change, otherwise new `EntitiesMap<T>` with applied changes. */
707
684
  applyEntityChanges: (
708
- entities: import('./types').EntitiesMap<Typenames>,
709
- changes: import('./types').EntityChanges<Typenames>
710
- ) => import('./types').EntitiesMap<Typenames> | undefined
685
+ entities: EntitiesMap<Typenames>,
686
+ changes: EntityChanges<Typenames>
687
+ ) => EntitiesMap<Typenames> | undefined
711
688
  }
712
689
  }
@@ -1,7 +1,7 @@
1
1
  import type {Actions} from './createActions'
2
2
  import type {CacheOptions, CacheState, Typenames} from './types'
3
3
 
4
- export declare const createCacheReducer: <N extends string, T extends Typenames, QP, QR, MP, MR>(
4
+ export declare const createReducer: <N extends string, T extends Typenames, QP, QR, MP, MR>(
5
5
  actions: Actions<N, T, QP, QR, MP, MR>,
6
6
  queryKeys: (keyof (QP | QR))[],
7
7
  cacheOptions: CacheOptions
@@ -8,6 +8,7 @@ export type Selectors<
8
8
  MP = unknown,
9
9
  MR = unknown
10
10
  > = ReturnType<typeof createSelectors<N, T, QP, QR, MP, MR>>
11
+
11
12
  export declare const createSelectors: <N extends string, T extends Typenames, QP, QR, MP, MR>(
12
13
  cache: Cache<N, T, QP, QR, MP, MR>
13
14
  ) => {
@@ -1,3 +1,5 @@
1
1
  export {createCache, withTypenames} from './createCache'
2
+
2
3
  export * from './types'
4
+
3
5
  export {createStateComparer, defaultGetCacheKey, FetchPolicy, isEmptyObject} from './utilsAndConstants'
@@ -13,7 +13,7 @@ export declare const mutate: <
13
13
  >(
14
14
  logTag: string,
15
15
  store: Store,
16
- cache: Cache<N, T, QP, QR, MP, MR>,
16
+ cache: Pick<Cache<N, T, QP, QR, MP, MR>, 'options' | 'globals' | 'mutations'>,
17
17
  actions: Actions<N, T, QP, QR, MP, MR>,
18
18
  selectors: Selectors<N, T, QP, QR, MP, MR>,
19
19
  mutationKey: MK,
@@ -13,7 +13,7 @@ export declare const query: <
13
13
  >(
14
14
  logTag: string,
15
15
  store: Store,
16
- cache: Cache<N, T, QP, QR, MP, MR>,
16
+ cache: Pick<Cache<N, T, QP, QR, MP, MR>, 'options' | 'globals' | 'queries'>,
17
17
  actions: Actions<N, T, QP, QR, MP, MR>,
18
18
  selectors: Selectors<N, T, QP, QR, MP, MR>,
19
19
  queryKey: QK,
@@ -21,6 +21,7 @@ export declare const query: <
21
21
  params: QK extends keyof (QP | QR) ? QP[QK] : never,
22
22
  secondsToLive: number | undefined,
23
23
  onlyIfExpired: boolean | undefined,
24
+ skipFetch: boolean | undefined,
24
25
  mergeResults?:
25
26
  | ((
26
27
  oldResult: QR[keyof QP & keyof QR & string] | undefined,
@@ -2,11 +2,14 @@ import type {Actions} from './createActions'
2
2
  import type {Selectors} from './createSelectors'
3
3
 
4
4
  export type Key = string | number | symbol
5
+
5
6
  export type Dict<T> = Record<Key, T>
7
+
6
8
  export type OptionalPartial<T, K extends keyof T> = Partial<{
7
9
  [A in K]: Partial<T[A]>
8
10
  }> &
9
11
  Omit<T, K>
12
+
10
13
  /** Entity changes to be merged to the state. */
11
14
  export type EntityChanges<T extends Typenames> = {
12
15
  /** Entities that will be merged with existing. */
@@ -18,12 +21,15 @@ export type EntityChanges<T extends Typenames> = {
18
21
  /** Alias for `merge` to support normalizr. */
19
22
  entities?: EntityChanges<T>['merge']
20
23
  }
24
+
21
25
  export type Store<S = unknown> = {
22
26
  dispatch: (action: ReturnType<Actions[keyof Actions]>) => unknown
23
27
  getState: () => S
24
28
  }
29
+
25
30
  /** Record of typename and its corresponding entity type */
26
31
  export type Typenames = Record<string, object>
32
+
27
33
  export type Cache<N extends string, T extends Typenames, QP, QR, MP, MR> = {
28
34
  /** Used as prefix for actions and in default cacheStateSelector for selecting cache state from store root state. */
29
35
  name: N
@@ -51,10 +57,12 @@ export type Cache<N extends string, T extends Typenames, QP, QR, MP, MR> = {
51
57
  : never
52
58
  }
53
59
  }
60
+
54
61
  export type QueryStateComparer<T extends Typenames, P, R> = (
55
62
  x: QueryState<T, P, R> | undefined,
56
63
  y: QueryState<T, P, R> | undefined
57
64
  ) => boolean
65
+
58
66
  export type Globals<N extends string, T extends Typenames, QP, QR, MP, MR> = {
59
67
  /** Handles errors, not handled by onError from queries and mutations. @Default undefined. */
60
68
  onError?: (
@@ -85,6 +93,7 @@ export type Globals<N extends string, T extends Typenames, QP, QR, MP, MR> = {
85
93
  selectorComparer?: QueryStateComparer<T, unknown, unknown> | (keyof QueryState<T, unknown, unknown>)[]
86
94
  }
87
95
  }
96
+
88
97
  export type CacheOptions = {
89
98
  /** Enables additional validation with logging to console.warn. Recommened to enable in dev/testing mode. @Default true in dev mode. */
90
99
  additionalValidation: boolean
@@ -98,15 +107,19 @@ export type CacheOptions = {
98
107
  */
99
108
  deepComparisonEnabled: boolean
100
109
  }
110
+
101
111
  export type PartialEntitiesMap<T extends Typenames> = {
102
112
  [K in keyof T]?: Dict<Partial<T[K]>>
103
113
  }
114
+
104
115
  export type EntitiesMap<T extends Typenames> = {
105
116
  [K in keyof T]?: Dict<T[K]>
106
117
  }
118
+
107
119
  export type EntityIds<T extends Typenames> = {
108
120
  [K in keyof T]?: Key[]
109
121
  }
122
+
110
123
  export type CacheState<T extends Typenames, QP, QR, MP, MR> = {
111
124
  entities: EntitiesMap<T>
112
125
  queries: {
@@ -116,6 +129,7 @@ export type CacheState<T extends Typenames, QP, QR, MP, MR> = {
116
129
  [MK in keyof (MP | MR)]: MutationState<T, MP[MK], MR[MK]>
117
130
  }
118
131
  }
132
+
119
133
  export type QueryInfo<
120
134
  N extends string,
121
135
  T extends Typenames = Typenames,
@@ -180,15 +194,18 @@ export type QueryInfo<
180
194
  /** Either comparer function, or array of keys to subscribe by useQuery's useSelector. Default compares params, result, loading, error. */
181
195
  selectorComparer?: QueryStateComparer<T, P, R> | (keyof QueryState<T, P, R>)[]
182
196
  }
197
+
183
198
  export type Query<P = unknown, R = unknown> = (
184
199
  /** Query parameters */
185
200
  params: P,
186
201
  /** Store */
187
202
  store: Store
188
203
  ) => Promise<QueryResponse<R>>
204
+
189
205
  export type NormalizedQuery<T extends Typenames = Typenames, P = unknown, R = unknown> = (
190
206
  ...args: Parameters<Query<P, R>>
191
207
  ) => Promise<NormalizedQueryResponse<T, R>>
208
+
192
209
  export type QueryState<T extends Typenames, P, R> = MutationState<T, P, R> & {
193
210
  /**
194
211
  * Timestamp in milliseconds, after which state is considered expired.
@@ -197,6 +214,7 @@ export type QueryState<T extends Typenames, P, R> = MutationState<T, P, R> & {
197
214
  * */
198
215
  expiresAt?: number
199
216
  }
217
+
200
218
  export type UseQueryOptions<
201
219
  N extends string,
202
220
  T extends Typenames,
@@ -228,6 +246,7 @@ export type UseQueryOptions<
228
246
  | 'onSuccess'
229
247
  | 'onError'
230
248
  >
249
+
231
250
  export type QueryOptions<
232
251
  N extends string,
233
252
  T extends Typenames,
@@ -236,23 +255,37 @@ export type QueryOptions<
236
255
  QK extends keyof (QP & QR),
237
256
  MP,
238
257
  MR
239
- > = Omit<UseQueryOptions<N, T, QK, QP, QR, MP, MR>, 'skipFetch'> & {
258
+ > = Pick<
259
+ UseQueryOptions<N, T, QK, QP, QR, MP, MR>,
260
+ | 'query'
261
+ | 'params'
262
+ | 'skipFetch'
263
+ | 'secondsToLive'
264
+ | 'mergeResults'
265
+ | 'onCompleted'
266
+ | 'onSuccess'
267
+ | 'onError'
268
+ > & {
240
269
  /** If set to true, query will run only if it is expired or result not yet cached. */
241
270
  onlyIfExpired?: boolean
242
271
  }
272
+
243
273
  export type QueryResponse<R = unknown> = {
244
274
  result: R
245
275
  /** If defined, overrides this value for query state, ignoring `secondsToLive`. */
246
276
  expiresAt?: number
247
277
  }
278
+
248
279
  export type NormalizedQueryResponse<T extends Typenames = Typenames, R = unknown> = EntityChanges<T> &
249
280
  QueryResponse<R>
281
+
250
282
  export type QueryResult<R = unknown> = {
251
283
  error?: unknown
252
284
  /** Fetch cancelled reason. */
253
285
  cancelled?: 'loading' | 'not-expired'
254
286
  result?: R
255
287
  }
288
+
256
289
  export type MutationInfo<
257
290
  N extends string,
258
291
  T extends Typenames = Typenames,
@@ -265,6 +298,7 @@ export type MutationInfo<
265
298
  > = Pick<QueryInfo<N, T, P, R, QP, QR, MP, MR>, 'onCompleted' | 'onSuccess' | 'onError'> & {
266
299
  mutation: NormalizedMutation<T, P, R>
267
300
  }
301
+
268
302
  export type Mutation<P = unknown, R = unknown> = (
269
303
  /** Mutation parameters */
270
304
  params: P,
@@ -273,9 +307,11 @@ export type Mutation<P = unknown, R = unknown> = (
273
307
  /** Signal is aborted for current mutation when the same mutation was called once again. */
274
308
  abortSignal: AbortSignal
275
309
  ) => Promise<MutationResponse<R>>
310
+
276
311
  export type NormalizedMutation<T extends Typenames = Typenames, P = unknown, R = unknown> = (
277
312
  ...args: Parameters<Mutation<P, R>>
278
313
  ) => Promise<NormalizedMutationResponse<T, R>>
314
+
279
315
  export type MutateOptions<
280
316
  N extends string,
281
317
  T extends Typenames,
@@ -300,16 +336,20 @@ export type MutateOptions<
300
336
  mutation: MK
301
337
  params: MK extends keyof (MP | MR) ? MP[MK] : never
302
338
  }
339
+
303
340
  export type MutationResponse<R = unknown> = {
304
341
  result?: R
305
342
  }
343
+
306
344
  export type NormalizedMutationResponse<T extends Typenames = Typenames, R = unknown> = EntityChanges<T> &
307
345
  MutationResponse<R>
346
+
308
347
  export type MutationResult<R = unknown> = {
309
348
  error?: unknown
310
349
  aborted?: true
311
350
  result?: R
312
351
  }
352
+
313
353
  export type MutationState<T extends Typenames, P, R> = {
314
354
  /** Set when fetch is currently in progress. */
315
355
  loading?: Promise<NormalizedQueryResponse<T, R>>
@@ -11,7 +11,7 @@ export declare const useMutation: <
11
11
  MR,
12
12
  MK extends keyof (MP & MR)
13
13
  >(
14
- cache: Cache<N, T, QP, QR, MP, MR>,
14
+ cache: Pick<Cache<N, T, QP, QR, MP, MR>, 'options' | 'globals' | 'mutations' | 'storeHooks'>,
15
15
  actions: Actions<N, T, QP, QR, MP, MR>,
16
16
  selectors: Selectors<N, T, QP, QR, MP, MR>,
17
17
  options: Omit<MutateOptions<N, T, QP, QR, MP, MR, MK>, 'params'>,
@@ -11,7 +11,7 @@ export declare const useQuery: <
11
11
  MR,
12
12
  QK extends keyof (QP & QR)
13
13
  >(
14
- cache: Cache<N, T, QP, QR, MP, MR>,
14
+ cache: Pick<Cache<N, T, QP, QR, MP, MR>, 'options' | 'globals' | 'queries' | 'storeHooks'>,
15
15
  actions: Actions<N, T, QP, QR, MP, MR>,
16
16
  selectors: Selectors<N, T, QP, QR, MP, MR>,
17
17
  options: UseQueryOptions<N, T, QK, QP, QR, MP, MR>
@@ -9,31 +9,45 @@ import type {
9
9
  } from './types'
10
10
 
11
11
  export declare const PACKAGE_SHORT_NAME = 'rrc'
12
+
12
13
  export declare const optionalUtils: {
13
14
  deepEqual?: (a: any, b: any) => boolean
14
15
  }
16
+
15
17
  export declare const IS_DEV: boolean
18
+
16
19
  export declare const EMPTY_OBJECT: Readonly<{}>
20
+
17
21
  export declare const EMPTY_ARRAY: readonly never[]
22
+
18
23
  export declare const NOOP: () => void
24
+
25
+ /** Default getCacheKey implementation. */
19
26
  export declare const defaultGetCacheKey: <P = unknown>(params: P) => Key
27
+
20
28
  export declare const log: (tag: string, data?: unknown) => void
21
- export declare const FetchPolicy: {
22
- /** Only if cache does not exist (result is undefined) or expired. */
23
- NoCacheOrExpired: <T extends Typenames = Typenames, P = unknown, R = unknown>(
24
- expired: boolean,
25
- _params: P,
26
- state: QueryState<T, P, R>
27
- ) => boolean
28
- /** Every fetch trigger. */
29
- Always: () => boolean
30
- }
29
+
31
30
  export declare const applyEntityChanges: <T extends Typenames>(
32
31
  entities: EntitiesMap<T>,
33
32
  changes: EntityChanges<T>,
34
33
  options: CacheOptions
35
34
  ) => EntitiesMap<T> | undefined
35
+
36
+ /** Returns true if object has no keys. */
36
37
  export declare const isEmptyObject: (o: object) => boolean
38
+
39
+ /** Returns query state comparer that compares only provided fields. Used in implementation of `selectorComparer` option. */
37
40
  export declare const createStateComparer: <T extends Typenames = Typenames, Q = unknown, P = unknown>(
38
41
  fields: (keyof QueryState<T, Q, P>)[]
39
42
  ) => QueryStateComparer<T, Q, P>
43
+
44
+ export declare const FetchPolicy: {
45
+ /** Only if cache does not exist (result is undefined) or expired. @Default */
46
+ NoCacheOrExpired: <T extends Typenames = Typenames, P = unknown, R = unknown>(
47
+ expired: boolean,
48
+ _params: P,
49
+ state: QueryState<T, P, R>
50
+ ) => boolean
51
+ /** Every fetch trigger. */
52
+ Always: () => boolean
53
+ }
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.19.4",
5
+ "version": "0.19.6",
6
6
  "description": "Powerful data fetching and caching library for Redux and Zustand that supports normalization.",
7
7
  "main": "./dist/cjs/index.js",
8
8
  "module": "./dist/esm/index.js",
@@ -23,14 +23,15 @@
23
23
  "build-cjs": "tsc -p tsconfig.cjs.json && rm -rf dist/cjs/testing && rm -rf dist/cjs/__tests__",
24
24
  "build-esm": "tsc -p tsconfig.esm.json > /dev/null ; rm -rf dist/esm/testing && rm -rf dist/esm/__tests__",
25
25
  "build-types": "tsc -p tsconfig.types.json > /dev/null ; rm -rf dist/types/testing && rm -rf dist/types/__tests__",
26
- "build": "yarn clean && yarn lint && yarn build-cjs && yarn build-esm && yarn build-types && yarn size && yarn lint-fix-dist",
26
+ "build": "yarn clean && yarn lint && yarn build-cjs && yarn build-esm && yarn build-types && yarn size && yarn lint-fix-dist && yarn generate-docs",
27
27
  "size": "npx minified-size dist/esm/*.js | grep 'total:' | sed -E 's/.*total: [^,]*, ([^,]*), ([^,]*), ([^,]*).*/<br\\/>minified: \\1<br\\/>gzipped: \\2<br\\/>brotlied: \\3/'",
28
28
  "test": "node node_modules/jest/bin/jest.js",
29
29
  "preminor-rc": "yarn build && npm version preminor --preid rc",
30
30
  "prepatch-rc": "yarn build && npm version prepatch --preid rc",
31
31
  "publish-rc": "npm publish --tag rc",
32
32
  "remove-rc": "npm version <same-version-without-rc>",
33
- "prepublishOnly": "yarn build && yarn test"
33
+ "prepublishOnly": "yarn build && yarn test",
34
+ "generate-docs": "node --experimental-strip-types scripts/generate-docs.ts"
34
35
  },
35
36
  "peerDependencies": {
36
37
  "fast-deep-equal": "*",