react-redux-cache 0.20.0 → 0.21.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 +2 -1
- package/dist/types/createCache.d.ts +18 -12
- package/dist/types/types.d.ts +9 -5
- package/dist/types/utilsAndConstants.d.ts +4 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -34,6 +34,7 @@ Can be considered as `ApolloClient` for protocols other than `GraphQL`, but with
|
|
|
34
34
|
|Error handling|No need to use try / catch, errors are returned by fuctions and / or can be handled globally from single place.|
|
|
35
35
|
|Fetch policies|Decide if data is full enough or need to be fetched.|
|
|
36
36
|
|Normalization|Consistent state accross the app - better UX, minimum loading states and lower traffic consumption.|
|
|
37
|
+
|Minimal state|Default values such as `undefined` or default query states are removed from the state tree.|
|
|
37
38
|
|
|
38
39
|
#### Examples of states, generated by cache reducer from `/example` project:
|
|
39
40
|
<details>
|
|
@@ -552,7 +553,7 @@ export const defaultGetCacheKey = <P = unknown>(params: P): Key => {
|
|
|
552
553
|
}
|
|
553
554
|
```
|
|
554
555
|
|
|
555
|
-
It is recommended to override it when default implementation is not optimal or when keys in params object can be sorted in random order.
|
|
556
|
+
It is recommended to override it when default implementation is not optimal or when keys in params object can be sorted in random order. In second case you can also consider using array to pass params.
|
|
556
557
|
|
|
557
558
|
As example, can be overridden when implementing pagination.
|
|
558
559
|
|
|
@@ -32,7 +32,7 @@ import {applyEntityChanges} from './utilsAndConstants'
|
|
|
32
32
|
* `const cache = withTypenames<MyTypenames>().createCache({...})`
|
|
33
33
|
*/
|
|
34
34
|
export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
35
|
-
/** Creates reducer, actions and hooks for managing queries and mutations
|
|
35
|
+
/** Creates reducer, actions and hooks for managing queries and mutations. */
|
|
36
36
|
createCache: <N extends string, QP, QR, MP, MR>(
|
|
37
37
|
partialCache: OptionalPartial<
|
|
38
38
|
Omit<Cache<N, T, QP, QR, MP, MR>, 'globals'>,
|
|
@@ -43,7 +43,7 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
43
43
|
) => {
|
|
44
44
|
/** Keeps all options, passed while creating the cache. */
|
|
45
45
|
cache: Cache<N, T, QP, QR, MP, MR>
|
|
46
|
-
/** Reducer of the cache, should be added to redux store. */
|
|
46
|
+
/** Reducer of the cache, should be added to redux/zustand store. */
|
|
47
47
|
reducer: (
|
|
48
48
|
state: CacheState<T, QP, QR, MP, MR> | undefined,
|
|
49
49
|
action:
|
|
@@ -170,7 +170,7 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
170
170
|
}
|
|
171
171
|
type: `@rrc/${N}/clearMutationState`
|
|
172
172
|
}
|
|
173
|
-
/** Replaces cache state with initial, optionally merging with provided state. Doesn't cancel running fetches and
|
|
173
|
+
/** Replaces cache state with initial, optionally merging with provided state. Doesn't cancel running fetches and should be used with caution. */
|
|
174
174
|
clearCache: {
|
|
175
175
|
(stateToKeep?: Partial<CacheState<T, QP, QR, MP, MR>> | undefined): {
|
|
176
176
|
type: `@rrc/${N}/clearCache`
|
|
@@ -352,8 +352,12 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
352
352
|
}
|
|
353
353
|
/** Generates the initial state by calling a reducer. Not needed for redux — it already generates it the same way when creating the store. */
|
|
354
354
|
getInitialState: () => CacheState<T, QP, QR, MP, MR>
|
|
355
|
-
/**
|
|
356
|
-
*
|
|
355
|
+
/**
|
|
356
|
+
* Apply changes to the entities map.
|
|
357
|
+
* Returns `undefined` if nothing to change, otherwise new `EntitiesMap<T>` with applied changes.
|
|
358
|
+
* Uses deep comparison if `deepComparisonEnabled` option is `true`.
|
|
359
|
+
* Performs additional checks for intersections if `additionalValidation` option is `true`, and prints warnings if finds any issues.
|
|
360
|
+
*/
|
|
357
361
|
applyEntityChanges: (
|
|
358
362
|
entities: Parameters<typeof applyEntityChanges<T>>[0],
|
|
359
363
|
changes: Parameters<typeof applyEntityChanges<T>>[1]
|
|
@@ -362,9 +366,7 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
362
366
|
}
|
|
363
367
|
}
|
|
364
368
|
|
|
365
|
-
/**
|
|
366
|
-
* Creates reducer, actions and hooks for managing queries and mutations through redux cache.
|
|
367
|
-
*/
|
|
369
|
+
/** Creates reducer, actions and hooks for managing queries and mutations. */
|
|
368
370
|
export declare const createCache: <N extends string, QP, QR, MP, MR>(
|
|
369
371
|
partialCache: Partial<{
|
|
370
372
|
queries: Partial<{
|
|
@@ -393,7 +395,7 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
|
|
|
393
395
|
) => {
|
|
394
396
|
/** Keeps all options, passed while creating the cache. */
|
|
395
397
|
cache: Cache<N, Typenames, QP, QR, MP, MR>
|
|
396
|
-
/** Reducer of the cache, should be added to redux store. */
|
|
398
|
+
/** Reducer of the cache, should be added to redux/zustand store. */
|
|
397
399
|
reducer: (
|
|
398
400
|
state: CacheState<Typenames, QP, QR, MP, MR> | undefined,
|
|
399
401
|
action:
|
|
@@ -522,7 +524,7 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
|
|
|
522
524
|
}
|
|
523
525
|
type: `@rrc/${N}/clearMutationState`
|
|
524
526
|
}
|
|
525
|
-
/** Replaces cache state with initial, optionally merging with provided state. Doesn't cancel running fetches and
|
|
527
|
+
/** Replaces cache state with initial, optionally merging with provided state. Doesn't cancel running fetches and should be used with caution. */
|
|
526
528
|
clearCache: {
|
|
527
529
|
(stateToKeep?: Partial<CacheState<Typenames, QP, QR, MP, MR>> | undefined): {
|
|
528
530
|
type: `@rrc/${N}/clearCache`
|
|
@@ -711,8 +713,12 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
|
|
|
711
713
|
}
|
|
712
714
|
/** Generates the initial state by calling a reducer. Not needed for redux — it already generates it the same way when creating the store. */
|
|
713
715
|
getInitialState: () => CacheState<Typenames, QP, QR, MP, MR>
|
|
714
|
-
/**
|
|
715
|
-
*
|
|
716
|
+
/**
|
|
717
|
+
* Apply changes to the entities map.
|
|
718
|
+
* Returns `undefined` if nothing to change, otherwise new `EntitiesMap<T>` with applied changes.
|
|
719
|
+
* Uses deep comparison if `deepComparisonEnabled` option is `true`.
|
|
720
|
+
* Performs additional checks for intersections if `additionalValidation` option is `true`, and prints warnings if finds any issues.
|
|
721
|
+
*/
|
|
716
722
|
applyEntityChanges: (
|
|
717
723
|
entities: EntitiesMap<Typenames>,
|
|
718
724
|
changes: EntityChanges<Typenames>
|
package/dist/types/types.d.ts
CHANGED
|
@@ -95,7 +95,7 @@ export type Globals<N extends string, T extends Typenames, QP, QR, MP, MR> = {
|
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
export type CacheOptions = {
|
|
98
|
-
/** Enables additional validation with logging to console.warn.
|
|
98
|
+
/** Enables additional validation with logging to console.warn. Recommended to enable in dev/testing mode. @Default true in dev mode. */
|
|
99
99
|
additionalValidation: boolean
|
|
100
100
|
/** Enables debug logs. @Default false */
|
|
101
101
|
logsEnabled: boolean
|
|
@@ -165,7 +165,7 @@ export type QueryInfo<
|
|
|
165
165
|
* Default implementation uses `String()` or `JSON.stringify` depending on type.
|
|
166
166
|
* It is recommended to override it when default implementation is not optimal or when keys in params object can be sorted in random order etc.
|
|
167
167
|
*/
|
|
168
|
-
getCacheKey?: (params
|
|
168
|
+
getCacheKey?: (params: P) => Key
|
|
169
169
|
/** Called after fetch completed either successfully or not. */
|
|
170
170
|
onCompleted?: (
|
|
171
171
|
response: NormalizedQueryResponse<T, R> | undefined,
|
|
@@ -272,17 +272,21 @@ export type QueryOptions<
|
|
|
272
272
|
|
|
273
273
|
export type QueryResponse<R = unknown> = {
|
|
274
274
|
result: R
|
|
275
|
-
/** If defined, overrides this value
|
|
275
|
+
/** If defined, overrides this value in the query state, ignoring `secondsToLive` option. */
|
|
276
276
|
expiresAt?: number
|
|
277
277
|
}
|
|
278
278
|
|
|
279
279
|
export type NormalizedQueryResponse<T extends Typenames = Typenames, R = unknown> = EntityChanges<T> &
|
|
280
280
|
QueryResponse<R>
|
|
281
281
|
|
|
282
|
-
/**
|
|
282
|
+
/** Result is always returned, even if cancelled or finished with error. */
|
|
283
283
|
export type QueryResult<R = unknown> = {
|
|
284
284
|
error?: unknown
|
|
285
|
-
/**
|
|
285
|
+
/**
|
|
286
|
+
* Fetch cancelled reason.
|
|
287
|
+
* @value loading - already loading. Result of current fetch is returned.
|
|
288
|
+
* @value not-expired - not expired yet. Current state result is returned.
|
|
289
|
+
*/
|
|
286
290
|
cancelled?: 'loading' | 'not-expired'
|
|
287
291
|
result?: R
|
|
288
292
|
}
|
|
@@ -45,7 +45,10 @@ export declare const createStateComparer: <T extends Typenames = Typenames, Q =
|
|
|
45
45
|
) => QueryStateComparer<T, Q, P>
|
|
46
46
|
|
|
47
47
|
export declare const FetchPolicy: {
|
|
48
|
-
/**
|
|
48
|
+
/**
|
|
49
|
+
* Only if cache does not exist (result is undefined) or expired. Default.
|
|
50
|
+
* @param expired `true` when `expiresAt` is defined and lower than `Date.now()`
|
|
51
|
+
*/
|
|
49
52
|
NoCacheOrExpired: <T extends Typenames = Typenames, P = unknown, R = unknown>(
|
|
50
53
|
expired: boolean,
|
|
51
54
|
_params: P,
|
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.
|
|
5
|
+
"version": "0.21.0",
|
|
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",
|