react-redux-cache 0.19.5 → 0.20.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 +3 -1
- package/dist/cjs/createCache.js +5 -8
- package/dist/cjs/{createCacheReducer.js → createReducer.js} +4 -4
- package/dist/cjs/index.js +8 -1
- package/dist/cjs/mutate.js +2 -2
- package/dist/cjs/query.js +11 -24
- package/dist/cjs/useMutation.js +5 -14
- package/dist/cjs/useQuery.js +3 -8
- package/dist/cjs/utilsAndConstants.js +32 -29
- package/dist/esm/createCache.js +7 -4
- package/dist/esm/{createCacheReducer.js → createReducer.js} +4 -3
- package/dist/esm/index.js +3 -1
- package/dist/esm/mutate.js +4 -11
- package/dist/esm/query.js +14 -29
- package/dist/esm/useMutation.js +6 -15
- package/dist/esm/useQuery.js +5 -9
- package/dist/esm/utilsAndConstants.js +35 -24
- package/dist/types/createActions.d.ts +1 -0
- package/dist/types/createCache.d.ts +120 -111
- package/dist/types/{createCacheReducer.d.ts → createReducer.d.ts} +1 -1
- package/dist/types/createSelectors.d.ts +1 -0
- package/dist/types/index.d.ts +3 -1
- package/dist/types/mutate.d.ts +1 -1
- package/dist/types/query.d.ts +1 -1
- package/dist/types/types.d.ts +31 -0
- package/dist/types/useMutation.d.ts +1 -1
- package/dist/types/useQuery.d.ts +1 -1
- package/dist/types/utilsAndConstants.d.ts +29 -12
- package/package.json +4 -3
package/dist/types/types.d.ts
CHANGED
|
@@ -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,
|
|
@@ -250,19 +269,24 @@ export type QueryOptions<
|
|
|
250
269
|
/** If set to true, query will run only if it is expired or result not yet cached. */
|
|
251
270
|
onlyIfExpired?: boolean
|
|
252
271
|
}
|
|
272
|
+
|
|
253
273
|
export type QueryResponse<R = unknown> = {
|
|
254
274
|
result: R
|
|
255
275
|
/** If defined, overrides this value for query state, ignoring `secondsToLive`. */
|
|
256
276
|
expiresAt?: number
|
|
257
277
|
}
|
|
278
|
+
|
|
258
279
|
export type NormalizedQueryResponse<T extends Typenames = Typenames, R = unknown> = EntityChanges<T> &
|
|
259
280
|
QueryResponse<R>
|
|
281
|
+
|
|
282
|
+
/** Current result is always returned, even if cancelled or finished with error. */
|
|
260
283
|
export type QueryResult<R = unknown> = {
|
|
261
284
|
error?: unknown
|
|
262
285
|
/** Fetch cancelled reason. */
|
|
263
286
|
cancelled?: 'loading' | 'not-expired'
|
|
264
287
|
result?: R
|
|
265
288
|
}
|
|
289
|
+
|
|
266
290
|
export type MutationInfo<
|
|
267
291
|
N extends string,
|
|
268
292
|
T extends Typenames = Typenames,
|
|
@@ -275,6 +299,7 @@ export type MutationInfo<
|
|
|
275
299
|
> = Pick<QueryInfo<N, T, P, R, QP, QR, MP, MR>, 'onCompleted' | 'onSuccess' | 'onError'> & {
|
|
276
300
|
mutation: NormalizedMutation<T, P, R>
|
|
277
301
|
}
|
|
302
|
+
|
|
278
303
|
export type Mutation<P = unknown, R = unknown> = (
|
|
279
304
|
/** Mutation parameters */
|
|
280
305
|
params: P,
|
|
@@ -283,9 +308,11 @@ export type Mutation<P = unknown, R = unknown> = (
|
|
|
283
308
|
/** Signal is aborted for current mutation when the same mutation was called once again. */
|
|
284
309
|
abortSignal: AbortSignal
|
|
285
310
|
) => Promise<MutationResponse<R>>
|
|
311
|
+
|
|
286
312
|
export type NormalizedMutation<T extends Typenames = Typenames, P = unknown, R = unknown> = (
|
|
287
313
|
...args: Parameters<Mutation<P, R>>
|
|
288
314
|
) => Promise<NormalizedMutationResponse<T, R>>
|
|
315
|
+
|
|
289
316
|
export type MutateOptions<
|
|
290
317
|
N extends string,
|
|
291
318
|
T extends Typenames,
|
|
@@ -310,16 +337,20 @@ export type MutateOptions<
|
|
|
310
337
|
mutation: MK
|
|
311
338
|
params: MK extends keyof (MP | MR) ? MP[MK] : never
|
|
312
339
|
}
|
|
340
|
+
|
|
313
341
|
export type MutationResponse<R = unknown> = {
|
|
314
342
|
result?: R
|
|
315
343
|
}
|
|
344
|
+
|
|
316
345
|
export type NormalizedMutationResponse<T extends Typenames = Typenames, R = unknown> = EntityChanges<T> &
|
|
317
346
|
MutationResponse<R>
|
|
347
|
+
|
|
318
348
|
export type MutationResult<R = unknown> = {
|
|
319
349
|
error?: unknown
|
|
320
350
|
aborted?: true
|
|
321
351
|
result?: R
|
|
322
352
|
}
|
|
353
|
+
|
|
323
354
|
export type MutationState<T extends Typenames, P, R> = {
|
|
324
355
|
/** Set when fetch is currently in progress. */
|
|
325
356
|
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'>,
|
package/dist/types/useQuery.d.ts
CHANGED
|
@@ -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,48 @@ 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
|
+
|
|
17
|
+
export declare const logDebug: (tag: string, data?: unknown) => void
|
|
18
|
+
|
|
19
|
+
export declare const logWarn: (tag: string, data?: unknown) => void
|
|
20
|
+
|
|
15
21
|
export declare const IS_DEV: boolean
|
|
22
|
+
|
|
16
23
|
export declare const EMPTY_OBJECT: Readonly<{}>
|
|
24
|
+
|
|
17
25
|
export declare const EMPTY_ARRAY: readonly never[]
|
|
18
|
-
|
|
26
|
+
|
|
27
|
+
/** Empty function. */
|
|
28
|
+
export declare const noop: () => void
|
|
29
|
+
|
|
30
|
+
/** Default getCacheKey implementation. */
|
|
19
31
|
export declare const defaultGetCacheKey: <P = unknown>(params: P) => Key
|
|
20
|
-
|
|
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
|
-
}
|
|
32
|
+
|
|
31
33
|
export declare const applyEntityChanges: <T extends Typenames>(
|
|
32
34
|
entities: EntitiesMap<T>,
|
|
33
35
|
changes: EntityChanges<T>,
|
|
34
36
|
options: CacheOptions
|
|
35
37
|
) => EntitiesMap<T> | undefined
|
|
38
|
+
|
|
39
|
+
/** Returns true if object has no keys. */
|
|
36
40
|
export declare const isEmptyObject: (o: object) => boolean
|
|
41
|
+
|
|
42
|
+
/** Returns query state comparer that compares only provided fields. Used in implementation of `selectorComparer` option. */
|
|
37
43
|
export declare const createStateComparer: <T extends Typenames = Typenames, Q = unknown, P = unknown>(
|
|
38
44
|
fields: (keyof QueryState<T, Q, P>)[]
|
|
39
45
|
) => QueryStateComparer<T, Q, P>
|
|
46
|
+
|
|
47
|
+
export declare const FetchPolicy: {
|
|
48
|
+
/** Only if cache does not exist (result is undefined) or expired. @Default */
|
|
49
|
+
NoCacheOrExpired: <T extends Typenames = Typenames, P = unknown, R = unknown>(
|
|
50
|
+
expired: boolean,
|
|
51
|
+
_params: P,
|
|
52
|
+
state: QueryState<T, P, R>
|
|
53
|
+
) => boolean
|
|
54
|
+
/** Every fetch trigger. */
|
|
55
|
+
Always: () => boolean
|
|
56
|
+
}
|
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.20.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",
|
|
@@ -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": "*",
|