react-redux-cache 0.12.0-rc.0 → 0.13.0-rc.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 +28 -13
- package/dist/createActions.d.ts +3 -10
- package/dist/createActions.js +0 -7
- package/dist/createCache.d.ts +80 -61
- package/dist/createCache.js +47 -68
- package/dist/createCacheReducer.d.ts +2 -2
- package/dist/createSelectors.d.ts +18 -0
- package/dist/createSelectors.js +61 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +10 -4
- package/dist/mutate.d.ts +3 -2
- package/dist/mutate.js +14 -9
- package/dist/query.d.ts +3 -2
- package/dist/query.js +14 -10
- package/dist/types.d.ts +42 -45
- package/dist/useMutation.d.ts +3 -2
- package/dist/useMutation.js +2 -2
- package/dist/useQuery.d.ts +5 -2
- package/dist/useQuery.js +21 -20
- package/dist/utilsAndConstants.d.ts +7 -1
- package/dist/utilsAndConstants.js +9 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -119,7 +119,7 @@ Examples of states, generated by cache reducer from `/example` project:
|
|
|
119
119
|
- [Advanced](https://github.com/gentlee/react-redux-cache#advanced)
|
|
120
120
|
- [Error handling](https://github.com/gentlee/react-redux-cache#error-handling)
|
|
121
121
|
- [Invalidation](https://github.com/gentlee/react-redux-cache#invalidation)
|
|
122
|
-
- [Extended
|
|
122
|
+
- [Extended fetch policy](https://github.com/gentlee/react-redux-cache#extended-cache-policy)
|
|
123
123
|
- [Infinite scroll pagination](https://github.com/gentlee/react-redux-cache#infinite-scroll-pagination)
|
|
124
124
|
- [redux-persist](https://github.com/gentlee/react-redux-cache#redux-persist)
|
|
125
125
|
- [FAQ](https://github.com/gentlee/react-redux-cache#faq)
|
|
@@ -244,7 +244,7 @@ Please check `example/` folder (`npm run example` to run).
|
|
|
244
244
|
export const UserScreen = () => {
|
|
245
245
|
const {id} = useParams()
|
|
246
246
|
|
|
247
|
-
// useQuery connects to redux state and if user with that id is already cached, fetch won't happen (with default
|
|
247
|
+
// useQuery connects to redux state and if user with that id is already cached, fetch won't happen (with default FetchPolicy.NoCacheOrExpired).
|
|
248
248
|
// Infers all types from created cache, telling here that params and result are of type `number`.
|
|
249
249
|
const [{result: userId, loading, error}] = useQuery({
|
|
250
250
|
query: 'getUser',
|
|
@@ -306,7 +306,7 @@ export const cache = createCache({
|
|
|
306
306
|
|
|
307
307
|
#### Invalidation
|
|
308
308
|
|
|
309
|
-
`
|
|
309
|
+
`FetchPolicy.NoCacheOrExpired` (default) skips fetching on fetch triggers if result is already cached, but we can invalidate cached query results using `invalidateQuery` action to make it run again on a next mount.
|
|
310
310
|
|
|
311
311
|
```typescript
|
|
312
312
|
|
|
@@ -315,18 +315,20 @@ export const cache = createCache({
|
|
|
315
315
|
mutations: {
|
|
316
316
|
updateUser: {
|
|
317
317
|
mutation: updateUser,
|
|
318
|
-
onSuccess(_, __, {dispatch}) {
|
|
319
|
-
//
|
|
320
|
-
dispatch(
|
|
318
|
+
onSuccess(_, __, {dispatch}, {invalidateQuery}) {
|
|
319
|
+
// Invalidate getUsers after a single user update (can be done better by updating getUsers state with updateQueryStateAndEntities)
|
|
320
|
+
dispatch(invalidateQuery([{query: 'getUsers'}]))
|
|
321
321
|
},
|
|
322
322
|
},
|
|
323
323
|
},
|
|
324
324
|
})
|
|
325
325
|
```
|
|
326
326
|
|
|
327
|
-
#### Extended
|
|
327
|
+
#### Extended & custom fetch policy
|
|
328
328
|
|
|
329
|
-
|
|
329
|
+
Fetch policy determines if `useQuery` fetch triggers should start fetching. They are: 1) component mount 2) cache key change (=params by default) 3) `skipFetch` change to false.
|
|
330
|
+
|
|
331
|
+
`FetchPolicy.NoCacheOrExpired` (default) skips fetching if result is already cached, but sometimes it can't determine that we already have result in some other's query result or in normalized entities cache. In that case we can use `skipFetch` parameter of a query:
|
|
330
332
|
|
|
331
333
|
```typescript
|
|
332
334
|
export const UserScreen = () => {
|
|
@@ -337,20 +339,33 @@ export const UserScreen = () => {
|
|
|
337
339
|
const [{loading, error}] = useQuery({
|
|
338
340
|
query: 'getUser',
|
|
339
341
|
params: userId,
|
|
340
|
-
|
|
342
|
+
skipFetch: !!user // Disable fetches if we already have user cached by some other query, e.g. getUsers
|
|
341
343
|
})
|
|
342
344
|
|
|
343
345
|
...
|
|
344
346
|
}
|
|
345
347
|
```
|
|
346
348
|
|
|
347
|
-
|
|
349
|
+
But if more control is needed, e.g. checking if entity is full, custom fetch policy can be provided:
|
|
348
350
|
|
|
349
351
|
```typescript
|
|
350
|
-
|
|
352
|
+
...
|
|
353
|
+
getFullUser: {
|
|
354
|
+
query: getUser,
|
|
355
|
+
fetchPolicy(expired, id, _, {getState}, {selectEntityById}) {
|
|
356
|
+
if (expired) {
|
|
357
|
+
return true // fetch if expired
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// fetch if user is not full
|
|
361
|
+
const user = selectEntityById(getState(), id, 'users')
|
|
362
|
+
return !user || !('name' in user) || !('bankId' in user)
|
|
363
|
+
},
|
|
364
|
+
},
|
|
365
|
+
...
|
|
351
366
|
```
|
|
352
367
|
|
|
353
|
-
|
|
368
|
+
One more approach is to set `skipFetch: true` by default and manually run `fetch`. `onlyIfExpired` option can be also used:
|
|
354
369
|
|
|
355
370
|
```typescript
|
|
356
371
|
export const UserScreen = () => {
|
|
@@ -359,7 +374,7 @@ export const UserScreen = () => {
|
|
|
359
374
|
const [{result, loading, error}, fetchUser] = useQuery({
|
|
360
375
|
query: 'getUser',
|
|
361
376
|
params: userId,
|
|
362
|
-
|
|
377
|
+
skipFetch: true
|
|
363
378
|
})
|
|
364
379
|
|
|
365
380
|
useEffect(() => {
|
package/dist/createActions.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { EntityChanges, Key, MutationState, QueryState, Typenames } from './types';
|
|
2
|
-
export type
|
|
2
|
+
export type Actions<N extends string = string, T extends Typenames = Typenames, QP = unknown, QR = unknown, MP = unknown, MR = unknown> = ReturnType<typeof createActions<N, T, QP, QR, MP, MR>>;
|
|
3
3
|
export declare const createActions: <N extends string, T extends Typenames, QP, QR, MP, MR>(name: N) => {
|
|
4
|
-
/** Updates query state, and optionally merges entity changes in a single action. */
|
|
5
4
|
updateQueryStateAndEntities: {
|
|
6
5
|
<K extends keyof (QP | QR)>(queryKey: K, queryCacheKey: Key, state?: Partial<QueryState<QP[K], QR[K]>>, entityChanges?: EntityChanges<T>): {
|
|
7
6
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
@@ -12,7 +11,6 @@ export declare const createActions: <N extends string, T extends Typenames, QP,
|
|
|
12
11
|
};
|
|
13
12
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
14
13
|
};
|
|
15
|
-
/** Updates mutation state, and optionally merges entity changes in a single action. */
|
|
16
14
|
updateMutationStateAndEntities: {
|
|
17
15
|
<K extends keyof (MP | MR)>(mutationKey: K, state?: Partial<MutationState<MP[K], MR[K]>>, entityChanges?: EntityChanges<T>): {
|
|
18
16
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
@@ -22,7 +20,6 @@ export declare const createActions: <N extends string, T extends Typenames, QP,
|
|
|
22
20
|
};
|
|
23
21
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
24
22
|
};
|
|
25
|
-
/** Merge EntityChanges to the state. */
|
|
26
23
|
mergeEntityChanges: {
|
|
27
24
|
(changes: EntityChanges<T>): {
|
|
28
25
|
type: `@rrc/${N}/mergeEntityChanges`;
|
|
@@ -30,14 +27,13 @@ export declare const createActions: <N extends string, T extends Typenames, QP,
|
|
|
30
27
|
};
|
|
31
28
|
type: `@rrc/${N}/mergeEntityChanges`;
|
|
32
29
|
};
|
|
33
|
-
/** Invalidates query states. */
|
|
34
30
|
invalidateQuery: {
|
|
35
31
|
<K extends keyof (QP | QR)>(queries: {
|
|
36
32
|
/** Query key */
|
|
37
33
|
query: K;
|
|
38
34
|
/** Query cache key */
|
|
39
35
|
cacheKey?: Key;
|
|
40
|
-
/** Unix timestamp at which query expires. Is set to the query state. @
|
|
36
|
+
/** Unix timestamp at which query expires. Is set to the query state. @Default Date.now() */
|
|
41
37
|
expiresAt?: number;
|
|
42
38
|
}[]): {
|
|
43
39
|
type: `@rrc/${N}/invalidateQuery`;
|
|
@@ -46,14 +42,12 @@ export declare const createActions: <N extends string, T extends Typenames, QP,
|
|
|
46
42
|
query: K;
|
|
47
43
|
/** Query cache key */
|
|
48
44
|
cacheKey?: Key;
|
|
49
|
-
/** Unix timestamp at which query expires. Is set to the query state. @
|
|
45
|
+
/** Unix timestamp at which query expires. Is set to the query state. @Default Date.now() */
|
|
50
46
|
expiresAt?: number;
|
|
51
47
|
}[];
|
|
52
48
|
};
|
|
53
49
|
type: `@rrc/${N}/invalidateQuery`;
|
|
54
50
|
};
|
|
55
|
-
/** Clear states for provided query keys and cache keys.
|
|
56
|
-
* If cache key for query key is not provided, the whole state for query key is cleared. */
|
|
57
51
|
clearQueryState: {
|
|
58
52
|
<K extends keyof (QP | QR)>(queries: {
|
|
59
53
|
/** Query key */
|
|
@@ -71,7 +65,6 @@ export declare const createActions: <N extends string, T extends Typenames, QP,
|
|
|
71
65
|
};
|
|
72
66
|
type: `@rrc/${N}/clearQueryState`;
|
|
73
67
|
};
|
|
74
|
-
/** Clear states for provided mutation keys. */
|
|
75
68
|
clearMutationState: {
|
|
76
69
|
<K extends keyof (MP | MR)>(mutationKeys: K[]): {
|
|
77
70
|
type: `@rrc/${N}/clearMutationState`;
|
package/dist/createActions.js
CHANGED
|
@@ -46,18 +46,11 @@ const createActions = (name) => {
|
|
|
46
46
|
});
|
|
47
47
|
clearMutationState.type = clearMutationStateType;
|
|
48
48
|
return {
|
|
49
|
-
/** Updates query state, and optionally merges entity changes in a single action. */
|
|
50
49
|
updateQueryStateAndEntities,
|
|
51
|
-
/** Updates mutation state, and optionally merges entity changes in a single action. */
|
|
52
50
|
updateMutationStateAndEntities,
|
|
53
|
-
/** Merge EntityChanges to the state. */
|
|
54
51
|
mergeEntityChanges,
|
|
55
|
-
/** Invalidates query states. */
|
|
56
52
|
invalidateQuery,
|
|
57
|
-
/** Clear states for provided query keys and cache keys.
|
|
58
|
-
* If cache key for query key is not provided, the whole state for query key is cleared. */
|
|
59
53
|
clearQueryState,
|
|
60
|
-
/** Clear states for provided mutation keys. */
|
|
61
54
|
clearMutationState,
|
|
62
55
|
};
|
|
63
56
|
};
|
package/dist/createCache.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Cache, Key, MutateOptions, MutationResult,
|
|
1
|
+
import type { Cache, CacheOptions, Globals, Key, MutateOptions, MutationResult, OptionalPartial, QueryOptions, QueryResult, Typenames } from './types';
|
|
2
2
|
import { useMutation } from './useMutation';
|
|
3
3
|
import { useQuery } from './useQuery';
|
|
4
4
|
import { applyEntityChanges } from './utilsAndConstants';
|
|
@@ -11,24 +11,26 @@ 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>, "options" | "queries" | "mutations" | "cacheStateSelector"
|
|
14
|
+
createCache: <N extends string, QP, QR, MP, MR>(partialCache: OptionalPartial<Omit<Cache<N, T, QP, QR, MP, MR>, "globals">, "options" | "queries" | "mutations" | "cacheStateSelector"> & {
|
|
15
|
+
globals?: OptionalPartial<Cache<N, T, QP, QR, MP, MR>["globals"], "queries">;
|
|
16
|
+
}) => {
|
|
15
17
|
/** Keeps all options, passed while creating the cache. */
|
|
16
18
|
cache: Cache<N, T, QP, QR, MP, MR>;
|
|
17
19
|
/** Reducer of the cache, should be added to redux store. */
|
|
18
20
|
reducer: (state: {
|
|
19
21
|
entities: import("./types").EntitiesMap<T>;
|
|
20
|
-
queries: { [QK in keyof (QP | QR)]: import("./types").Dict<QueryState<QP[QK], QR[QK]> | undefined>; };
|
|
21
|
-
mutations: { [MK in keyof (MP | MR)]: MutationState<MP[MK], MR[MK]>; };
|
|
22
|
+
queries: { [QK in keyof (QP | QR)]: import("./types").Dict<import("./types").QueryState<QP[QK], QR[QK]> | undefined>; };
|
|
23
|
+
mutations: { [MK in keyof (MP | MR)]: import("./types").MutationState<MP[MK], MR[MK]>; };
|
|
22
24
|
} | undefined, action: {
|
|
23
25
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
24
26
|
queryKey: keyof QP & keyof QR;
|
|
25
27
|
queryCacheKey: Key;
|
|
26
|
-
state: Partial<QueryState<QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>> | undefined;
|
|
28
|
+
state: Partial<import("./types").QueryState<QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>> | undefined;
|
|
27
29
|
entityChanges: import("./types").EntityChanges<T> | undefined;
|
|
28
30
|
} | {
|
|
29
31
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
30
32
|
mutationKey: keyof MP & keyof MR;
|
|
31
|
-
state: Partial<MutationState<MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>> | undefined;
|
|
33
|
+
state: Partial<import("./types").MutationState<MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>> | undefined;
|
|
32
34
|
entityChanges: import("./types").EntityChanges<T> | undefined;
|
|
33
35
|
} | {
|
|
34
36
|
type: `@rrc/${N}/mergeEntityChanges`;
|
|
@@ -51,29 +53,32 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
51
53
|
mutationKeys: (keyof MP & keyof MR)[];
|
|
52
54
|
}) => {
|
|
53
55
|
entities: import("./types").EntitiesMap<T>;
|
|
54
|
-
queries: { [QK in keyof (QP | QR)]: import("./types").Dict<QueryState<QP[QK], QR[QK]> | undefined>; };
|
|
55
|
-
mutations: { [MK in keyof (MP | MR)]: MutationState<MP[MK], MR[MK]>; };
|
|
56
|
+
queries: { [QK in keyof (QP | QR)]: import("./types").Dict<import("./types").QueryState<QP[QK], QR[QK]> | undefined>; };
|
|
57
|
+
mutations: { [MK in keyof (MP | MR)]: import("./types").MutationState<MP[MK], MR[MK]>; };
|
|
56
58
|
};
|
|
57
59
|
actions: {
|
|
60
|
+
/** Updates query state, and optionally merges entity changes in a single action. */
|
|
58
61
|
updateQueryStateAndEntities: {
|
|
59
|
-
<K extends keyof QP & keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<QueryState<QP[K], QR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<T> | undefined): {
|
|
62
|
+
<K extends keyof QP & keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<import("./types").QueryState<QP[K], QR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<T> | undefined): {
|
|
60
63
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
61
64
|
queryKey: K;
|
|
62
65
|
queryCacheKey: Key;
|
|
63
|
-
state: Partial<QueryState<QP[K], QR[K]>> | undefined;
|
|
66
|
+
state: Partial<import("./types").QueryState<QP[K], QR[K]>> | undefined;
|
|
64
67
|
entityChanges: import("./types").EntityChanges<T> | undefined;
|
|
65
68
|
};
|
|
66
69
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
67
70
|
};
|
|
71
|
+
/** Updates mutation state, and optionally merges entity changes in a single action. */
|
|
68
72
|
updateMutationStateAndEntities: {
|
|
69
|
-
<K extends keyof MP & keyof MR>(mutationKey: K, state?: Partial<MutationState<MP[K], MR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<T> | undefined): {
|
|
73
|
+
<K extends keyof MP & keyof MR>(mutationKey: K, state?: Partial<import("./types").MutationState<MP[K], MR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<T> | undefined): {
|
|
70
74
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
71
75
|
mutationKey: K;
|
|
72
|
-
state: Partial<MutationState<MP[K], MR[K]>> | undefined;
|
|
76
|
+
state: Partial<import("./types").MutationState<MP[K], MR[K]>> | undefined;
|
|
73
77
|
entityChanges: import("./types").EntityChanges<T> | undefined;
|
|
74
78
|
};
|
|
75
79
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
76
80
|
};
|
|
81
|
+
/** Merge EntityChanges to the state. */
|
|
77
82
|
mergeEntityChanges: {
|
|
78
83
|
(changes: import("./types").EntityChanges<T>): {
|
|
79
84
|
type: `@rrc/${N}/mergeEntityChanges`;
|
|
@@ -81,6 +86,7 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
81
86
|
};
|
|
82
87
|
type: `@rrc/${N}/mergeEntityChanges`;
|
|
83
88
|
};
|
|
89
|
+
/** Invalidates query states. */
|
|
84
90
|
invalidateQuery: {
|
|
85
91
|
<K extends keyof QP & keyof QR>(queries: {
|
|
86
92
|
query: K;
|
|
@@ -96,6 +102,8 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
96
102
|
};
|
|
97
103
|
type: `@rrc/${N}/invalidateQuery`;
|
|
98
104
|
};
|
|
105
|
+
/** Clear states for provided query keys and cache keys.
|
|
106
|
+
* If cache key for query key is not provided, the whole state for query key is cleared. */
|
|
99
107
|
clearQueryState: {
|
|
100
108
|
<K extends keyof QP & keyof QR>(queries: {
|
|
101
109
|
query: K;
|
|
@@ -109,6 +117,7 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
109
117
|
};
|
|
110
118
|
type: `@rrc/${N}/clearQueryState`;
|
|
111
119
|
};
|
|
120
|
+
/** Clear states for provided mutation keys. */
|
|
112
121
|
clearMutationState: {
|
|
113
122
|
<K extends keyof MP & keyof MR>(mutationKeys: K[]): {
|
|
114
123
|
type: `@rrc/${N}/clearMutationState`;
|
|
@@ -119,27 +128,27 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
119
128
|
};
|
|
120
129
|
selectors: {
|
|
121
130
|
/** Selects query state. */
|
|
122
|
-
selectQueryState: <QK extends keyof
|
|
131
|
+
selectQueryState: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => import("./types").QueryState<QK extends keyof QP & keyof QR ? QP[QK] : never, QK extends keyof QP & keyof QR ? QR[QK] : never>;
|
|
123
132
|
/** Selects query latest result. */
|
|
124
|
-
selectQueryResult: <QK extends keyof
|
|
133
|
+
selectQueryResult: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => (QK extends keyof QP & keyof QR ? QR[QK] : never) | undefined;
|
|
125
134
|
/** Selects query loading state. */
|
|
126
|
-
selectQueryLoading: <QK extends keyof
|
|
135
|
+
selectQueryLoading: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => boolean;
|
|
127
136
|
/** Selects query latest error. */
|
|
128
|
-
selectQueryError: <QK extends keyof
|
|
137
|
+
selectQueryError: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => Error | undefined;
|
|
129
138
|
/** Selects query latest params. */
|
|
130
|
-
selectQueryParams: <QK extends keyof
|
|
139
|
+
selectQueryParams: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => (QK extends keyof QP & keyof QR ? QP[QK] : never) | undefined;
|
|
131
140
|
/** Selects query latest expiresAt. */
|
|
132
|
-
selectQueryExpiresAt: <QK extends keyof
|
|
141
|
+
selectQueryExpiresAt: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => number | undefined;
|
|
133
142
|
/** Selects mutation state. */
|
|
134
|
-
selectMutationState: <MK extends keyof
|
|
143
|
+
selectMutationState: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => import("./types").MutationState<MK extends keyof MP & keyof MR ? MP[MK] : never, MK extends keyof MP & keyof MR ? MR[MK] : never>;
|
|
135
144
|
/** Selects mutation latest result. */
|
|
136
|
-
selectMutationResult: <MK extends keyof
|
|
145
|
+
selectMutationResult: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => (MK extends keyof MP & keyof MR ? MR[MK] : never) | undefined;
|
|
137
146
|
/** Selects mutation loading state. */
|
|
138
|
-
selectMutationLoading: <MK extends keyof
|
|
147
|
+
selectMutationLoading: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => boolean;
|
|
139
148
|
/** Selects mutation latest error. */
|
|
140
|
-
selectMutationError: <MK extends keyof
|
|
149
|
+
selectMutationError: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => Error | undefined;
|
|
141
150
|
/** Selects mutation latest params. */
|
|
142
|
-
selectMutationParams: <MK extends keyof
|
|
151
|
+
selectMutationParams: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => (MK extends keyof MP & keyof MR ? MP[MK] : never) | undefined;
|
|
143
152
|
/** Selects entity by id and typename. */
|
|
144
153
|
selectEntityById: <TN extends keyof T>(state: unknown, id: Key | null | undefined, typename: TN) => T[TN] | undefined;
|
|
145
154
|
/** Selects all entities. */
|
|
@@ -150,21 +159,19 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
150
159
|
hooks: {
|
|
151
160
|
/** Returns client object with query and mutate functions. */
|
|
152
161
|
useClient: () => {
|
|
153
|
-
query: <QK extends keyof (QP & QR)>(options: QueryOptions<T, QP, QR, QK>) => Promise<QueryResult<QK extends keyof QP & keyof QR ? QR[QK] : never>>;
|
|
154
|
-
mutate: <MK extends keyof (MP & MR)>(options: MutateOptions<T, MP, MR, MK>) => Promise<MutationResult<MK extends keyof MP & keyof MR ? MR[MK] : never>>;
|
|
162
|
+
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>>;
|
|
163
|
+
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>>;
|
|
155
164
|
};
|
|
156
165
|
/** Fetches query when params change and subscribes to query state changes (except `expiresAt` field). */
|
|
157
|
-
useQuery: <QK extends keyof (QP & QR)>(options: Parameters<typeof useQuery<N, T, QP, QR, MP, MR, QK>>[
|
|
166
|
+
useQuery: <QK extends keyof (QP & QR)>(options: Parameters<typeof useQuery<N, T, QP, QR, MP, MR, QK>>[3]) => readonly [Omit<import("./types").QueryState<QK extends keyof QP & keyof QR ? QP[QK] : never, QK extends keyof QP & keyof QR ? QR[QK] : never>, "expiresAt">, (options?: Partial<Pick<QueryOptions<N, T, QP, QR, QK, MP, MR>, "params" | "onlyIfExpired">> | undefined) => Promise<QueryResult<QK extends infer T_1 ? T_1 extends QK ? T_1 extends keyof QP & keyof QR ? QR[T_1] : never : never : never>>];
|
|
158
167
|
/** Subscribes to provided mutation state and provides mutate function. */
|
|
159
|
-
useMutation: <MK extends keyof (MP & MR)>(options: Parameters<typeof useMutation<N, T, MP, MR, MK>>[
|
|
168
|
+
useMutation: <MK extends keyof (MP & MR)>(options: Parameters<typeof useMutation<N, T, QP, QR, MP, MR, MK>>[3]) => readonly [(params: MK extends keyof MP & keyof MR ? MP[MK] : never) => Promise<MutationResult<MK extends infer T_1 ? T_1 extends MK ? T_1 extends keyof MP & keyof MR ? MR[T_1] : never : never : never>>, import("./types").MutationState<MK extends keyof MP & keyof MR ? MP[MK] : never, MK extends keyof MP & keyof MR ? MP[MK] : never>, () => boolean];
|
|
160
169
|
/** useSelector + selectEntityById. */
|
|
161
170
|
useSelectEntityById: <TN extends keyof T>(id: Key | null | undefined, typename: TN) => T[TN] | undefined;
|
|
162
171
|
};
|
|
163
172
|
utils: {
|
|
164
|
-
/**
|
|
165
|
-
*
|
|
166
|
-
* @return `undefined` if nothing to change, otherwise new entities map with applied changes.
|
|
167
|
-
*/
|
|
173
|
+
/** Apply changes to the entities map.
|
|
174
|
+
* @returns `undefined` if nothing to change, otherwise new `EntitiesMap<T>` with applied changes. */
|
|
168
175
|
applyEntityChanges: (entities: Parameters<typeof applyEntityChanges<T>>[0], changes: Parameters<typeof applyEntityChanges<T>>[1]) => import("./types").EntitiesMap<T> | undefined;
|
|
169
176
|
};
|
|
170
177
|
};
|
|
@@ -172,24 +179,31 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
172
179
|
/**
|
|
173
180
|
* Creates reducer, actions and hooks for managing queries and mutations through redux cache.
|
|
174
181
|
*/
|
|
175
|
-
export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCache:
|
|
182
|
+
export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCache: Partial<{
|
|
183
|
+
queries: Partial<{ [QK in keyof (QP & QR)]: QK extends keyof QP & keyof QR ? import("./types").QueryInfo<N, Typenames, QP[QK], QR[QK], QP, QR, MP, MR> : never; }>;
|
|
184
|
+
mutations: Partial<{ [MK in keyof (MP & MR)]: MK extends keyof MP & keyof MR ? import("./types").MutationInfo<N, Typenames, MP[MK], MR[MK], QP, QR, MP, MR> : never; }>;
|
|
185
|
+
options: Partial<CacheOptions>;
|
|
186
|
+
cacheStateSelector: Partial<(state: any) => import("./createCacheReducer").ReduxCacheState<Typenames, QP, QR, MP, MR>>;
|
|
187
|
+
}> & Omit<Omit<Cache<N, Typenames, QP, QR, MP, MR>, "globals">, "queries" | "mutations" | "options" | "cacheStateSelector"> & {
|
|
188
|
+
globals?: OptionalPartial<Globals<N, Typenames, QP, QR, MP, MR>, "queries"> | undefined;
|
|
189
|
+
}) => {
|
|
176
190
|
/** Keeps all options, passed while creating the cache. */
|
|
177
191
|
cache: Cache<N, Typenames, QP, QR, MP, MR>;
|
|
178
192
|
/** Reducer of the cache, should be added to redux store. */
|
|
179
193
|
reducer: (state: {
|
|
180
194
|
entities: import("./types").EntitiesMap<Typenames>;
|
|
181
|
-
queries: { [
|
|
182
|
-
mutations: { [
|
|
195
|
+
queries: { [QK_1 in keyof (QP | QR)]: import("./types").Dict<import("./types").QueryState<QP[QK_1], QR[QK_1]> | undefined>; };
|
|
196
|
+
mutations: { [MK_1 in keyof (MP | MR)]: import("./types").MutationState<MP[MK_1], MR[MK_1]>; };
|
|
183
197
|
} | undefined, action: {
|
|
184
198
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
185
199
|
queryKey: keyof QP & keyof QR;
|
|
186
200
|
queryCacheKey: Key;
|
|
187
|
-
state: Partial<QueryState<QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>> | undefined;
|
|
201
|
+
state: Partial<import("./types").QueryState<QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>> | undefined;
|
|
188
202
|
entityChanges: import("./types").EntityChanges<Typenames> | undefined;
|
|
189
203
|
} | {
|
|
190
204
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
191
205
|
mutationKey: keyof MP & keyof MR;
|
|
192
|
-
state: Partial<MutationState<MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>> | undefined;
|
|
206
|
+
state: Partial<import("./types").MutationState<MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>> | undefined;
|
|
193
207
|
entityChanges: import("./types").EntityChanges<Typenames> | undefined;
|
|
194
208
|
} | {
|
|
195
209
|
type: `@rrc/${N}/mergeEntityChanges`;
|
|
@@ -212,29 +226,32 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCach
|
|
|
212
226
|
mutationKeys: (keyof MP & keyof MR)[];
|
|
213
227
|
}) => {
|
|
214
228
|
entities: import("./types").EntitiesMap<Typenames>;
|
|
215
|
-
queries: { [
|
|
216
|
-
mutations: { [
|
|
229
|
+
queries: { [QK_1 in keyof (QP | QR)]: import("./types").Dict<import("./types").QueryState<QP[QK_1], QR[QK_1]> | undefined>; };
|
|
230
|
+
mutations: { [MK_1 in keyof (MP | MR)]: import("./types").MutationState<MP[MK_1], MR[MK_1]>; };
|
|
217
231
|
};
|
|
218
232
|
actions: {
|
|
233
|
+
/** Updates query state, and optionally merges entity changes in a single action. */
|
|
219
234
|
updateQueryStateAndEntities: {
|
|
220
|
-
<K extends keyof QP & keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<QueryState<QP[K], QR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<Typenames> | undefined): {
|
|
235
|
+
<K extends keyof QP & keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<import("./types").QueryState<QP[K], QR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<Typenames> | undefined): {
|
|
221
236
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
222
237
|
queryKey: K;
|
|
223
238
|
queryCacheKey: Key;
|
|
224
|
-
state: Partial<QueryState<QP[K], QR[K]>> | undefined;
|
|
239
|
+
state: Partial<import("./types").QueryState<QP[K], QR[K]>> | undefined;
|
|
225
240
|
entityChanges: import("./types").EntityChanges<Typenames> | undefined;
|
|
226
241
|
};
|
|
227
242
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
228
243
|
};
|
|
244
|
+
/** Updates mutation state, and optionally merges entity changes in a single action. */
|
|
229
245
|
updateMutationStateAndEntities: {
|
|
230
|
-
<K extends keyof MP & keyof MR>(mutationKey: K, state?: Partial<MutationState<MP[K], MR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<Typenames> | undefined): {
|
|
246
|
+
<K extends keyof MP & keyof MR>(mutationKey: K, state?: Partial<import("./types").MutationState<MP[K], MR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<Typenames> | undefined): {
|
|
231
247
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
232
248
|
mutationKey: K;
|
|
233
|
-
state: Partial<MutationState<MP[K], MR[K]>> | undefined;
|
|
249
|
+
state: Partial<import("./types").MutationState<MP[K], MR[K]>> | undefined;
|
|
234
250
|
entityChanges: import("./types").EntityChanges<Typenames> | undefined;
|
|
235
251
|
};
|
|
236
252
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
237
253
|
};
|
|
254
|
+
/** Merge EntityChanges to the state. */
|
|
238
255
|
mergeEntityChanges: {
|
|
239
256
|
(changes: import("./types").EntityChanges<Typenames>): {
|
|
240
257
|
type: `@rrc/${N}/mergeEntityChanges`;
|
|
@@ -242,6 +259,7 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCach
|
|
|
242
259
|
};
|
|
243
260
|
type: `@rrc/${N}/mergeEntityChanges`;
|
|
244
261
|
};
|
|
262
|
+
/** Invalidates query states. */
|
|
245
263
|
invalidateQuery: {
|
|
246
264
|
<K extends keyof QP & keyof QR>(queries: {
|
|
247
265
|
query: K;
|
|
@@ -257,6 +275,8 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCach
|
|
|
257
275
|
};
|
|
258
276
|
type: `@rrc/${N}/invalidateQuery`;
|
|
259
277
|
};
|
|
278
|
+
/** Clear states for provided query keys and cache keys.
|
|
279
|
+
* If cache key for query key is not provided, the whole state for query key is cleared. */
|
|
260
280
|
clearQueryState: {
|
|
261
281
|
<K extends keyof QP & keyof QR>(queries: {
|
|
262
282
|
query: K;
|
|
@@ -270,6 +290,7 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCach
|
|
|
270
290
|
};
|
|
271
291
|
type: `@rrc/${N}/clearQueryState`;
|
|
272
292
|
};
|
|
293
|
+
/** Clear states for provided mutation keys. */
|
|
273
294
|
clearMutationState: {
|
|
274
295
|
<K extends keyof MP & keyof MR>(mutationKeys: K[]): {
|
|
275
296
|
type: `@rrc/${N}/clearMutationState`;
|
|
@@ -280,27 +301,27 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCach
|
|
|
280
301
|
};
|
|
281
302
|
selectors: {
|
|
282
303
|
/** Selects query state. */
|
|
283
|
-
selectQueryState: <
|
|
304
|
+
selectQueryState: <QK_1 extends keyof QP | keyof QR>(state: unknown, query: QK_1, cacheKey: Key) => import("./types").QueryState<QK_1 extends keyof QP & keyof QR ? QP[QK_1] : never, QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never>;
|
|
284
305
|
/** Selects query latest result. */
|
|
285
|
-
selectQueryResult: <
|
|
306
|
+
selectQueryResult: <QK_1 extends keyof QP | keyof QR>(state: unknown, query: QK_1, cacheKey: Key) => (QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never) | undefined;
|
|
286
307
|
/** Selects query loading state. */
|
|
287
|
-
selectQueryLoading: <
|
|
308
|
+
selectQueryLoading: <QK_1 extends keyof QP | keyof QR>(state: unknown, query: QK_1, cacheKey: Key) => boolean;
|
|
288
309
|
/** Selects query latest error. */
|
|
289
|
-
selectQueryError: <
|
|
310
|
+
selectQueryError: <QK_1 extends keyof QP | keyof QR>(state: unknown, query: QK_1, cacheKey: Key) => Error | undefined;
|
|
290
311
|
/** Selects query latest params. */
|
|
291
|
-
selectQueryParams: <
|
|
312
|
+
selectQueryParams: <QK_1 extends keyof QP | keyof QR>(state: unknown, query: QK_1, cacheKey: Key) => (QK_1 extends keyof QP & keyof QR ? QP[QK_1] : never) | undefined;
|
|
292
313
|
/** Selects query latest expiresAt. */
|
|
293
|
-
selectQueryExpiresAt: <
|
|
314
|
+
selectQueryExpiresAt: <QK_1 extends keyof QP | keyof QR>(state: unknown, query: QK_1, cacheKey: Key) => number | undefined;
|
|
294
315
|
/** Selects mutation state. */
|
|
295
|
-
selectMutationState: <
|
|
316
|
+
selectMutationState: <MK_1 extends keyof MP | keyof MR>(state: unknown, mutation: MK_1) => import("./types").MutationState<MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never, MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>;
|
|
296
317
|
/** Selects mutation latest result. */
|
|
297
|
-
selectMutationResult: <
|
|
318
|
+
selectMutationResult: <MK_1 extends keyof MP | keyof MR>(state: unknown, mutation: MK_1) => (MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never) | undefined;
|
|
298
319
|
/** Selects mutation loading state. */
|
|
299
|
-
selectMutationLoading: <
|
|
320
|
+
selectMutationLoading: <MK_1 extends keyof MP | keyof MR>(state: unknown, mutation: MK_1) => boolean;
|
|
300
321
|
/** Selects mutation latest error. */
|
|
301
|
-
selectMutationError: <
|
|
322
|
+
selectMutationError: <MK_1 extends keyof MP | keyof MR>(state: unknown, mutation: MK_1) => Error | undefined;
|
|
302
323
|
/** Selects mutation latest params. */
|
|
303
|
-
selectMutationParams: <
|
|
324
|
+
selectMutationParams: <MK_1 extends keyof MP | keyof MR>(state: unknown, mutation: MK_1) => (MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never) | undefined;
|
|
304
325
|
/** Selects entity by id and typename. */
|
|
305
326
|
selectEntityById: <TN extends string>(state: unknown, id: Key | null | undefined, typename: TN) => object | undefined;
|
|
306
327
|
/** Selects all entities. */
|
|
@@ -311,21 +332,19 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCach
|
|
|
311
332
|
hooks: {
|
|
312
333
|
/** Returns client object with query and mutate functions. */
|
|
313
334
|
useClient: () => {
|
|
314
|
-
query: <
|
|
315
|
-
mutate: <
|
|
335
|
+
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>>;
|
|
336
|
+
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>>;
|
|
316
337
|
};
|
|
317
338
|
/** Fetches query when params change and subscribes to query state changes (except `expiresAt` field). */
|
|
318
|
-
useQuery: <
|
|
339
|
+
useQuery: <QK_1 extends keyof QP | keyof QR>(options: import("./types").UseQueryOptions<N, Typenames, QK_1, QP, QR, MP, MR>) => readonly [Omit<import("./types").QueryState<QK_1 extends keyof QP & keyof QR ? QP[QK_1] : never, QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never>, "expiresAt">, (options?: Partial<Pick<QueryOptions<N, Typenames, QP, QR, QK_1, MP, MR>, "params" | "onlyIfExpired">> | undefined) => Promise<QueryResult<QK_1 extends infer T ? T extends QK_1 ? T extends keyof QP & keyof QR ? QR[T] : never : never : never>>];
|
|
319
340
|
/** Subscribes to provided mutation state and provides mutate function. */
|
|
320
|
-
useMutation: <
|
|
341
|
+
useMutation: <MK_1 extends keyof MP | keyof MR>(options: Omit<MutateOptions<N, Typenames, QP, QR, MP, MR, MK_1>, "params">) => readonly [(params: MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never) => Promise<MutationResult<MK_1 extends infer T ? T extends MK_1 ? T extends keyof MP & keyof MR ? MR[T] : never : never : never>>, import("./types").MutationState<MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never, MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never>, () => boolean];
|
|
321
342
|
/** useSelector + selectEntityById. */
|
|
322
343
|
useSelectEntityById: <TN extends string>(id: Key | null | undefined, typename: TN) => object | undefined;
|
|
323
344
|
};
|
|
324
345
|
utils: {
|
|
325
|
-
/**
|
|
326
|
-
*
|
|
327
|
-
* @return `undefined` if nothing to change, otherwise new entities map with applied changes.
|
|
328
|
-
*/
|
|
346
|
+
/** Apply changes to the entities map.
|
|
347
|
+
* @returns `undefined` if nothing to change, otherwise new `EntitiesMap<T>` with applied changes. */
|
|
329
348
|
applyEntityChanges: (entities: import("./types").EntitiesMap<Typenames>, changes: import("./types").EntityChanges<Typenames>) => import("./types").EntitiesMap<Typenames> | undefined;
|
|
330
349
|
};
|
|
331
350
|
};
|