react-redux-cache 0.21.0 → 0.22.1

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.
@@ -1,30 +1,7 @@
1
- import type {
2
- Cache,
3
- CacheOptions,
4
- CacheState,
5
- Dict,
6
- EntitiesMap,
7
- EntityChanges,
8
- Globals,
9
- Key,
10
- MutateOptions,
11
- MutationInfo,
12
- MutationResult,
13
- MutationState,
14
- NormalizedQueryResponse,
15
- OptionalPartial,
16
- QueryInfo,
17
- QueryOptions,
18
- QueryResult,
19
- QueryState,
20
- Store,
21
- Typenames,
22
- UseQueryOptions,
23
- } from './types'
24
- import {useMutation} from './useMutation'
25
- import {useQuery} from './useQuery'
26
- import {applyEntityChanges} from './utilsAndConstants'
27
-
1
+ import type { Cache, CacheOptions, CacheState, EntitiesMap, EntityChanges, Globals, Key, MutateOptions, MutationInfo, MutationResult, MutationState, NormalizedQueryResponse, OptionalPartial, QueryInfo, QueryOptions, QueryResult, QueryState, Store, Typenames, UseQueryOptions } from './types';
2
+ import { useMutation } from './useMutation';
3
+ import { useQuery } from './useQuery';
4
+ import { applyEntityChanges } from './utilsAndConstants';
28
5
  /**
29
6
  * Function to provide generic Typenames if normalization is needed - this is a Typescript limitation.
30
7
  * Returns object with createCache function with provided typenames.
@@ -32,696 +9,417 @@ import {applyEntityChanges} from './utilsAndConstants'
32
9
  * `const cache = withTypenames<MyTypenames>().createCache({...})`
33
10
  */
34
11
  export declare const withTypenames: <T extends Typenames = Typenames>() => {
35
- /** Creates reducer, actions and hooks for managing queries and mutations. */
36
- createCache: <N extends string, QP, QR, MP, MR>(
37
- partialCache: OptionalPartial<
38
- Omit<Cache<N, T, QP, QR, MP, MR>, 'globals'>,
39
- 'options' | 'queries' | 'mutations' | 'cacheStateSelector' | 'storeHooks'
40
- > & {
41
- globals?: OptionalPartial<Cache<N, T, QP, QR, MP, MR>['globals'], 'queries'>
42
- }
43
- ) => {
44
- /** Keeps all options, passed while creating the cache. */
45
- cache: Cache<N, T, QP, QR, MP, MR>
46
- /** Reducer of the cache, should be added to redux/zustand store. */
47
- reducer: (
48
- state: CacheState<T, QP, QR, MP, MR> | undefined,
49
- action:
50
- | {
51
- type: `@rrc/${N}/updateQueryStateAndEntities`
52
- queryKey: keyof QP & keyof QR
53
- queryCacheKey: Key
54
- state: Partial<QueryState<T, QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>> | undefined
55
- entityChanges: EntityChanges<T> | undefined
56
- }
57
- | {
58
- type: `@rrc/${N}/updateMutationStateAndEntities`
59
- mutationKey: keyof MP & keyof MR
60
- state: Partial<MutationState<T, MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>> | undefined
61
- entityChanges: EntityChanges<T> | undefined
62
- }
63
- | {
64
- type: `@rrc/${N}/mergeEntityChanges`
65
- changes: EntityChanges<T>
66
- }
67
- | {
68
- type: `@rrc/${N}/invalidateQuery`
12
+ /** Creates reducer, actions and hooks for managing queries and mutations. */
13
+ createCache: <N extends string, QP, QR, MP, MR>(partialCache: OptionalPartial<Omit<Cache<N, T, QP, QR, MP, MR>, "globals">, "options" | "queries" | "mutations" | "cacheStateSelector" | "storeHooks"> & {
14
+ globals?: OptionalPartial<Cache<N, T, QP, QR, MP, MR>["globals"], "queries">;
15
+ }) => {
16
+ /** Keeps all options, passed while creating the cache. */
17
+ cache: Cache<N, T, QP, QR, MP, MR>;
18
+ /** Reducer of the cache, should be added to redux/zustand store. */
19
+ reducer: (state: CacheState<T, QP, QR, MP, MR> | undefined, action: {
20
+ type: `@rrc/${N}/updateQueryStateAndEntities`;
21
+ queryKey: keyof QP & keyof QR;
22
+ queryCacheKey: Key;
23
+ state: Partial<QueryState<T, QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>> | undefined;
24
+ entityChanges: EntityChanges<T> | undefined;
25
+ } | {
26
+ type: `@rrc/${N}/updateMutationStateAndEntities`;
27
+ mutationKey: keyof MP & keyof MR;
28
+ state: Partial<MutationState<T, MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>> | undefined;
29
+ entityChanges: EntityChanges<T> | undefined;
30
+ } | {
31
+ type: `@rrc/${N}/mergeEntityChanges`;
32
+ changes: EntityChanges<T>;
33
+ } | {
34
+ type: `@rrc/${N}/invalidateQuery`;
69
35
  queries: {
70
- query: keyof QP & keyof QR
71
- cacheKey?: Key
72
- expiresAt?: number
73
- }[]
74
- }
75
- | {
76
- type: `@rrc/${N}/clearQueryState`
36
+ query: keyof QP & keyof QR;
37
+ cacheKey?: Key;
38
+ expiresAt?: number;
39
+ }[];
40
+ } | {
41
+ type: `@rrc/${N}/clearQueryState`;
77
42
  queries: {
78
- query: keyof QP & keyof QR
79
- cacheKey?: Key
80
- }[]
81
- }
82
- | {
83
- type: `@rrc/${N}/clearMutationState`
84
- mutationKeys: (keyof MP & keyof MR)[]
85
- }
86
- | {
87
- type: `@rrc/${N}/clearCache`
88
- stateToKeep: Partial<CacheState<T, QP, QR, MP, MR>> | undefined
89
- }
90
- ) => CacheState<T, QP, QR, MP, MR>
43
+ query: keyof QP & keyof QR;
44
+ cacheKey?: Key;
45
+ }[];
46
+ } | {
47
+ type: `@rrc/${N}/clearMutationState`;
48
+ mutationKeys: (keyof MP & keyof MR)[];
49
+ } | {
50
+ type: `@rrc/${N}/clearCache`;
51
+ stateToKeep: Partial<CacheState<T, QP, QR, MP, MR>> | undefined;
52
+ }) => CacheState<T, QP, QR, MP, MR>;
53
+ actions: {
54
+ /** Updates query state, and optionally merges entity changes in a single action. */
55
+ updateQueryStateAndEntities: {
56
+ <K extends keyof QP & keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<QueryState<T, QP[K], QR[K]>> | undefined, entityChanges?: EntityChanges<T> | undefined): {
57
+ type: `@rrc/${N}/updateQueryStateAndEntities`;
58
+ queryKey: K;
59
+ queryCacheKey: Key;
60
+ state: Partial<QueryState<T, QP[K], QR[K]>> | undefined;
61
+ entityChanges: EntityChanges<T> | undefined;
62
+ };
63
+ type: `@rrc/${N}/updateQueryStateAndEntities`;
64
+ };
65
+ /** Updates mutation state, and optionally merges entity changes in a single action. */
66
+ updateMutationStateAndEntities: {
67
+ <K extends keyof MP & keyof MR>(mutationKey: K, state?: Partial<MutationState<T, MP[K], MR[K]>> | undefined, entityChanges?: EntityChanges<T> | undefined): {
68
+ type: `@rrc/${N}/updateMutationStateAndEntities`;
69
+ mutationKey: K;
70
+ state: Partial<MutationState<T, MP[K], MR[K]>> | undefined;
71
+ entityChanges: EntityChanges<T> | undefined;
72
+ };
73
+ type: `@rrc/${N}/updateMutationStateAndEntities`;
74
+ };
75
+ /** Merges EntityChanges to the state. */
76
+ mergeEntityChanges: {
77
+ (changes: EntityChanges<T>): {
78
+ type: `@rrc/${N}/mergeEntityChanges`;
79
+ changes: EntityChanges<T>;
80
+ };
81
+ type: `@rrc/${N}/mergeEntityChanges`;
82
+ };
83
+ /** Sets expiresAt to Date.now(). */
84
+ invalidateQuery: {
85
+ <K extends keyof QP & keyof QR>(queries: {
86
+ query: K;
87
+ cacheKey?: Key;
88
+ expiresAt?: number;
89
+ }[]): {
90
+ type: `@rrc/${N}/invalidateQuery`;
91
+ queries: {
92
+ query: K;
93
+ cacheKey?: Key;
94
+ expiresAt?: number;
95
+ }[];
96
+ };
97
+ type: `@rrc/${N}/invalidateQuery`;
98
+ };
99
+ /** Clears states for provided query keys and cache keys.
100
+ * If cache key for query key is not provided, the whole state for query key is cleared. */
101
+ clearQueryState: {
102
+ <K extends keyof QP & keyof QR>(queries: {
103
+ query: K;
104
+ cacheKey?: Key;
105
+ }[]): {
106
+ type: `@rrc/${N}/clearQueryState`;
107
+ queries: {
108
+ query: K;
109
+ cacheKey?: Key;
110
+ }[];
111
+ };
112
+ type: `@rrc/${N}/clearQueryState`;
113
+ };
114
+ /** Clears states for provided mutation keys. */
115
+ clearMutationState: {
116
+ <K extends keyof MP & keyof MR>(mutationKeys: K[]): {
117
+ type: `@rrc/${N}/clearMutationState`;
118
+ mutationKeys: K[];
119
+ };
120
+ type: `@rrc/${N}/clearMutationState`;
121
+ };
122
+ /** Replaces cache state with initial, optionally merging with provided state. Doesn't cancel running fetches and should be used with caution. */
123
+ clearCache: {
124
+ (stateToKeep?: Partial<CacheState<T, QP, QR, MP, MR>> | undefined): {
125
+ type: `@rrc/${N}/clearCache`;
126
+ stateToKeep: Partial<CacheState<T, QP, QR, MP, MR>> | undefined;
127
+ };
128
+ type: `@rrc/${N}/clearCache`;
129
+ };
130
+ };
131
+ selectors: {
132
+ /** This is a cacheStateSelector from createCache options, or default one if was not provided. */
133
+ selectCacheState: (state: any) => CacheState<T, QP, QR, MP, MR>;
134
+ /** Selects query state. */
135
+ selectQueryState: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => QueryState<T, QK extends keyof QP & keyof QR ? QP[QK] : never, QK extends keyof QP & keyof QR ? QR[QK] : never>;
136
+ /** Selects query latest result. */
137
+ selectQueryResult: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => (QK extends keyof QP & keyof QR ? QR[QK] : never) | undefined;
138
+ /** Selects query loading state. */
139
+ selectQueryLoading: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => false | Promise<NormalizedQueryResponse<T, QK extends keyof QP & keyof QR ? QR[QK] : never>>;
140
+ /** Selects query latest error. */
141
+ selectQueryError: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => Error | undefined;
142
+ /** Selects query latest params. */
143
+ selectQueryParams: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => (QK extends keyof QP & keyof QR ? QP[QK] : never) | undefined;
144
+ /** Selects query latest expiresAt. */
145
+ selectQueryExpiresAt: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => number | undefined;
146
+ /** Selects mutation state. */
147
+ selectMutationState: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => MutationState<T, MK extends keyof MP & keyof MR ? MP[MK] : never, MK extends keyof MP & keyof MR ? MR[MK] : never>;
148
+ /** Selects mutation latest result. */
149
+ selectMutationResult: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => (MK extends keyof MP & keyof MR ? MR[MK] : never) | undefined;
150
+ /** Selects mutation loading state. */
151
+ selectMutationLoading: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => false | Promise<NormalizedQueryResponse<T, MK extends keyof MP & keyof MR ? MR[MK] : never>>;
152
+ /** Selects mutation latest error. */
153
+ selectMutationError: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => Error | undefined;
154
+ /** Selects mutation latest params. */
155
+ selectMutationParams: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => (MK extends keyof MP & keyof MR ? MP[MK] : never) | undefined;
156
+ /** Selects entity by id and typename. */
157
+ selectEntityById: <TN extends keyof T>(state: unknown, id: Key | null | undefined, typename: TN) => T[TN] | undefined;
158
+ /** Selects all entities. */
159
+ selectEntities: (state: unknown) => EntitiesMap<T> & import("./types").Mutable;
160
+ /** Selects all entities of provided typename. */
161
+ selectEntitiesByTypename: <TN extends keyof T>(state: unknown, typename: TN) => (EntitiesMap<T> & import("./types").Mutable)[TN];
162
+ };
163
+ hooks: {
164
+ /** Returns memoized object with query and mutate functions. Memoization dependency is the store. */
165
+ useClient: () => {
166
+ /**
167
+ * Performs a query using provided options. Deduplicates calls with the same cache key. Always returns current cached result, even when query is cancelled or finished with error.
168
+ * @param onlyIfExpired When true, cancels fetch if result is not yet expired.
169
+ * @param skipFetch Fetch is cancelled and current cached result is returned.
170
+ */
171
+ 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>>;
172
+ /**
173
+ * Performs a mutation, aborting previous one with the same mutation key. Returns result only if finished succesfully.
174
+ */
175
+ 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>>;
176
+ };
177
+ /** Fetches query when params change and subscribes to query state changes (subscription depends on `selectorComparer`). */
178
+ useQuery: <QK extends keyof (QP & QR)>(options: Parameters<typeof useQuery<N, T, QP, QR, MP, MR, QK>>[3]) => readonly [Omit<QueryState<T, 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>>];
179
+ /** Subscribes to provided mutation state and provides mutate function. */
180
+ 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>>, MutationState<T, MK extends keyof MP & keyof MR ? MP[MK] : never, MK extends keyof MP & keyof MR ? MP[MK] : never>, () => boolean];
181
+ /** useSelector + selectEntityById. */
182
+ useSelectEntityById: <TN extends keyof T>(id: Key | null | undefined, typename: TN) => T[TN] | undefined;
183
+ /**
184
+ * useSelector + selectEntitiesByTypename. Also subscribes to collection's change key if `mutableCollections` enabled.
185
+ * @warning Subscribing to collections should be avoided.
186
+ * */
187
+ useEntitiesByTypename: <TN extends keyof T>(typename: TN) => (EntitiesMap<T> & import("./types").Mutable)[TN];
188
+ };
189
+ utils: {
190
+ /** Creates client by providing the store. Can be used when the store is a singleton - to not use a useClient hook for getting the client, but import it directly. */
191
+ createClient: (store: Store) => {
192
+ /**
193
+ * Performs a query using provided options. Deduplicates calls with the same cache key. Always returns current cached result, even when query is cancelled or finished with error.
194
+ * @param onlyIfExpired When true, cancels fetch if result is not yet expired.
195
+ * @param skipFetch Fetch is cancelled and current cached result is returned.
196
+ */
197
+ 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>>;
198
+ /**
199
+ * Performs a mutation, aborting previous one with the same mutation key. Returns result only if finished succesfully.
200
+ */
201
+ 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>>;
202
+ };
203
+ /** Generates the initial state by calling a reducer. Not needed for redux — it already generates it the same way when creating the store. */
204
+ getInitialState: () => CacheState<T, QP, QR, MP, MR>;
205
+ /**
206
+ * Apply changes to the entities map.
207
+ * Returns `undefined` if nothing to change, otherwise new `EntitiesMap<T>` with applied changes.
208
+ * Uses deep comparison if `deepComparisonEnabled` option is `true`.
209
+ * Performs additional checks for intersections if `additionalValidation` option is `true`, and prints warnings if finds any issues.
210
+ */
211
+ applyEntityChanges: (entities: Parameters<typeof applyEntityChanges<T>>[0], changes: Parameters<typeof applyEntityChanges<T>>[1]) => EntitiesMap<T> | undefined;
212
+ };
213
+ };
214
+ };
215
+ /** Creates reducer, actions and hooks for managing queries and mutations. */
216
+ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCache: Partial<{
217
+ queries: Partial<{ [QK in keyof (QP & QR)]: QK extends keyof QP & keyof QR ? QueryInfo<N, Typenames, QP[QK], QR[QK], QP, QR, MP, MR> : never; }>;
218
+ mutations: Partial<{ [MK in keyof (MP & MR)]: MK extends keyof MP & keyof MR ? MutationInfo<N, Typenames, MP[MK], MR[MK], QP, QR, MP, MR> : never; }>;
219
+ options: Partial<CacheOptions>;
220
+ storeHooks: Partial<{
221
+ useStore: () => Store;
222
+ useSelector: <R>(selector: (state: unknown) => R, comparer?: (x: R, y: R) => boolean) => R;
223
+ }>;
224
+ cacheStateSelector: Partial<(state: any) => CacheState<Typenames, QP, QR, MP, MR>>;
225
+ }> & Omit<Omit<Cache<N, Typenames, QP, QR, MP, MR>, "globals">, "queries" | "mutations" | "options" | "storeHooks" | "cacheStateSelector"> & {
226
+ globals?: OptionalPartial<Globals<N, Typenames, QP, QR, MP, MR>, "queries"> | undefined;
227
+ }) => {
228
+ /** Keeps all options, passed while creating the cache. */
229
+ cache: Cache<N, Typenames, QP, QR, MP, MR>;
230
+ /** Reducer of the cache, should be added to redux/zustand store. */
231
+ reducer: (state: CacheState<Typenames, QP, QR, MP, MR> | undefined, action: {
232
+ type: `@rrc/${N}/updateQueryStateAndEntities`;
233
+ queryKey: keyof QP & keyof QR;
234
+ queryCacheKey: Key;
235
+ state: Partial<QueryState<Typenames, QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>> | undefined;
236
+ entityChanges: EntityChanges<Typenames> | undefined;
237
+ } | {
238
+ type: `@rrc/${N}/updateMutationStateAndEntities`;
239
+ mutationKey: keyof MP & keyof MR;
240
+ state: Partial<MutationState<Typenames, MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>> | undefined;
241
+ entityChanges: EntityChanges<Typenames> | undefined;
242
+ } | {
243
+ type: `@rrc/${N}/mergeEntityChanges`;
244
+ changes: EntityChanges<Typenames>;
245
+ } | {
246
+ type: `@rrc/${N}/invalidateQuery`;
247
+ queries: {
248
+ query: keyof QP & keyof QR;
249
+ cacheKey?: Key;
250
+ expiresAt?: number;
251
+ }[];
252
+ } | {
253
+ type: `@rrc/${N}/clearQueryState`;
254
+ queries: {
255
+ query: keyof QP & keyof QR;
256
+ cacheKey?: Key;
257
+ }[];
258
+ } | {
259
+ type: `@rrc/${N}/clearMutationState`;
260
+ mutationKeys: (keyof MP & keyof MR)[];
261
+ } | {
262
+ type: `@rrc/${N}/clearCache`;
263
+ stateToKeep: Partial<CacheState<Typenames, QP, QR, MP, MR>> | undefined;
264
+ }) => CacheState<Typenames, QP, QR, MP, MR>;
91
265
  actions: {
92
- /** Updates query state, and optionally merges entity changes in a single action. */
93
- updateQueryStateAndEntities: {
94
- <K extends keyof QP & keyof QR>(
95
- queryKey: K,
96
- queryCacheKey: Key,
97
- state?: Partial<QueryState<T, QP[K], QR[K]>> | undefined,
98
- entityChanges?: EntityChanges<T> | undefined
99
- ): {
100
- type: `@rrc/${N}/updateQueryStateAndEntities`
101
- queryKey: K
102
- queryCacheKey: Key
103
- state: Partial<QueryState<T, QP[K], QR[K]>> | undefined
104
- entityChanges: EntityChanges<T> | undefined
105
- }
106
- type: `@rrc/${N}/updateQueryStateAndEntities`
107
- }
108
- /** Updates mutation state, and optionally merges entity changes in a single action. */
109
- updateMutationStateAndEntities: {
110
- <K extends keyof MP & keyof MR>(
111
- mutationKey: K,
112
- state?: Partial<MutationState<T, MP[K], MR[K]>> | undefined,
113
- entityChanges?: EntityChanges<T> | undefined
114
- ): {
115
- type: `@rrc/${N}/updateMutationStateAndEntities`
116
- mutationKey: K
117
- state: Partial<MutationState<T, MP[K], MR[K]>> | undefined
118
- entityChanges: EntityChanges<T> | undefined
119
- }
120
- type: `@rrc/${N}/updateMutationStateAndEntities`
121
- }
122
- /** Merges EntityChanges to the state. */
123
- mergeEntityChanges: {
124
- (changes: EntityChanges<T>): {
125
- type: `@rrc/${N}/mergeEntityChanges`
126
- changes: EntityChanges<T>
127
- }
128
- type: `@rrc/${N}/mergeEntityChanges`
129
- }
130
- /** Invalidates query states. */
131
- invalidateQuery: {
132
- <K extends keyof QP & keyof QR>(
133
- queries: {
134
- query: K
135
- cacheKey?: Key
136
- expiresAt?: number
137
- }[]
138
- ): {
139
- type: `@rrc/${N}/invalidateQuery`
140
- queries: {
141
- query: K
142
- cacheKey?: Key
143
- expiresAt?: number
144
- }[]
145
- }
146
- type: `@rrc/${N}/invalidateQuery`
147
- }
148
- /** Clears states for provided query keys and cache keys.
149
- * If cache key for query key is not provided, the whole state for query key is cleared. */
150
- clearQueryState: {
151
- <K extends keyof QP & keyof QR>(
152
- queries: {
153
- query: K
154
- cacheKey?: Key
155
- }[]
156
- ): {
157
- type: `@rrc/${N}/clearQueryState`
158
- queries: {
159
- query: K
160
- cacheKey?: Key
161
- }[]
162
- }
163
- type: `@rrc/${N}/clearQueryState`
164
- }
165
- /** Clears states for provided mutation keys. */
166
- clearMutationState: {
167
- <K extends keyof MP & keyof MR>(mutationKeys: K[]): {
168
- type: `@rrc/${N}/clearMutationState`
169
- mutationKeys: K[]
170
- }
171
- type: `@rrc/${N}/clearMutationState`
172
- }
173
- /** Replaces cache state with initial, optionally merging with provided state. Doesn't cancel running fetches and should be used with caution. */
174
- clearCache: {
175
- (stateToKeep?: Partial<CacheState<T, QP, QR, MP, MR>> | undefined): {
176
- type: `@rrc/${N}/clearCache`
177
- stateToKeep: Partial<CacheState<T, QP, QR, MP, MR>> | undefined
178
- }
179
- type: `@rrc/${N}/clearCache`
180
- }
181
- }
266
+ /** Updates query state, and optionally merges entity changes in a single action. */
267
+ updateQueryStateAndEntities: {
268
+ <K extends keyof QP & keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<QueryState<Typenames, QP[K], QR[K]>> | undefined, entityChanges?: EntityChanges<Typenames> | undefined): {
269
+ type: `@rrc/${N}/updateQueryStateAndEntities`;
270
+ queryKey: K;
271
+ queryCacheKey: Key;
272
+ state: Partial<QueryState<Typenames, QP[K], QR[K]>> | undefined;
273
+ entityChanges: EntityChanges<Typenames> | undefined;
274
+ };
275
+ type: `@rrc/${N}/updateQueryStateAndEntities`;
276
+ };
277
+ /** Updates mutation state, and optionally merges entity changes in a single action. */
278
+ updateMutationStateAndEntities: {
279
+ <K extends keyof MP & keyof MR>(mutationKey: K, state?: Partial<MutationState<Typenames, MP[K], MR[K]>> | undefined, entityChanges?: EntityChanges<Typenames> | undefined): {
280
+ type: `@rrc/${N}/updateMutationStateAndEntities`;
281
+ mutationKey: K;
282
+ state: Partial<MutationState<Typenames, MP[K], MR[K]>> | undefined;
283
+ entityChanges: EntityChanges<Typenames> | undefined;
284
+ };
285
+ type: `@rrc/${N}/updateMutationStateAndEntities`;
286
+ };
287
+ /** Merges EntityChanges to the state. */
288
+ mergeEntityChanges: {
289
+ (changes: EntityChanges<Typenames>): {
290
+ type: `@rrc/${N}/mergeEntityChanges`;
291
+ changes: EntityChanges<Typenames>;
292
+ };
293
+ type: `@rrc/${N}/mergeEntityChanges`;
294
+ };
295
+ /** Sets expiresAt to Date.now(). */
296
+ invalidateQuery: {
297
+ <K extends keyof QP & keyof QR>(queries: {
298
+ query: K;
299
+ cacheKey?: Key;
300
+ expiresAt?: number;
301
+ }[]): {
302
+ type: `@rrc/${N}/invalidateQuery`;
303
+ queries: {
304
+ query: K;
305
+ cacheKey?: Key;
306
+ expiresAt?: number;
307
+ }[];
308
+ };
309
+ type: `@rrc/${N}/invalidateQuery`;
310
+ };
311
+ /** Clears states for provided query keys and cache keys.
312
+ * If cache key for query key is not provided, the whole state for query key is cleared. */
313
+ clearQueryState: {
314
+ <K extends keyof QP & keyof QR>(queries: {
315
+ query: K;
316
+ cacheKey?: Key;
317
+ }[]): {
318
+ type: `@rrc/${N}/clearQueryState`;
319
+ queries: {
320
+ query: K;
321
+ cacheKey?: Key;
322
+ }[];
323
+ };
324
+ type: `@rrc/${N}/clearQueryState`;
325
+ };
326
+ /** Clears states for provided mutation keys. */
327
+ clearMutationState: {
328
+ <K extends keyof MP & keyof MR>(mutationKeys: K[]): {
329
+ type: `@rrc/${N}/clearMutationState`;
330
+ mutationKeys: K[];
331
+ };
332
+ type: `@rrc/${N}/clearMutationState`;
333
+ };
334
+ /** Replaces cache state with initial, optionally merging with provided state. Doesn't cancel running fetches and should be used with caution. */
335
+ clearCache: {
336
+ (stateToKeep?: Partial<CacheState<Typenames, QP, QR, MP, MR>> | undefined): {
337
+ type: `@rrc/${N}/clearCache`;
338
+ stateToKeep: Partial<CacheState<Typenames, QP, QR, MP, MR>> | undefined;
339
+ };
340
+ type: `@rrc/${N}/clearCache`;
341
+ };
342
+ };
182
343
  selectors: {
183
- /** This is a cacheStateSelector from createCache options, or default one if was not provided. */
184
- selectCacheState: (state: any) => CacheState<T, QP, QR, MP, MR>
185
- /** Selects query state. */
186
- selectQueryState: <QK extends keyof QP | keyof QR>(
187
- state: unknown,
188
- query: QK,
189
- cacheKey: Key
190
- ) => QueryState<
191
- T,
192
- QK extends keyof QP & keyof QR ? QP[QK] : never,
193
- QK extends keyof QP & keyof QR ? QR[QK] : never
194
- >
195
- /** Selects query latest result. */
196
- selectQueryResult: <QK extends keyof QP | keyof QR>(
197
- state: unknown,
198
- query: QK,
199
- cacheKey: Key
200
- ) => (QK extends keyof QP & keyof QR ? QR[QK] : never) | undefined
201
- /** Selects query loading state. */
202
- selectQueryLoading: <QK extends keyof QP | keyof QR>(
203
- state: unknown,
204
- query: QK,
205
- cacheKey: Key
206
- ) => false | Promise<NormalizedQueryResponse<T, QK extends keyof QP & keyof QR ? QR[QK] : never>>
207
- /** Selects query latest error. */
208
- selectQueryError: <QK extends keyof QP | keyof QR>(
209
- state: unknown,
210
- query: QK,
211
- cacheKey: Key
212
- ) => Error | undefined
213
- /** Selects query latest params. */
214
- selectQueryParams: <QK extends keyof QP | keyof QR>(
215
- state: unknown,
216
- query: QK,
217
- cacheKey: Key
218
- ) => (QK extends keyof QP & keyof QR ? QP[QK] : never) | undefined
219
- /** Selects query latest expiresAt. */
220
- selectQueryExpiresAt: <QK extends keyof QP | keyof QR>(
221
- state: unknown,
222
- query: QK,
223
- cacheKey: Key
224
- ) => number | undefined
225
- /** Selects mutation state. */
226
- selectMutationState: <MK extends keyof MP | keyof MR>(
227
- state: unknown,
228
- mutation: MK
229
- ) => MutationState<
230
- T,
231
- MK extends keyof MP & keyof MR ? MP[MK] : never,
232
- MK extends keyof MP & keyof MR ? MR[MK] : never
233
- >
234
- /** Selects mutation latest result. */
235
- selectMutationResult: <MK extends keyof MP | keyof MR>(
236
- state: unknown,
237
- mutation: MK
238
- ) => (MK extends keyof MP & keyof MR ? MR[MK] : never) | undefined
239
- /** Selects mutation loading state. */
240
- selectMutationLoading: <MK extends keyof MP | keyof MR>(
241
- state: unknown,
242
- mutation: MK
243
- ) => false | Promise<NormalizedQueryResponse<T, MK extends keyof MP & keyof MR ? MR[MK] : never>>
244
- /** Selects mutation latest error. */
245
- selectMutationError: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => Error | undefined
246
- /** Selects mutation latest params. */
247
- selectMutationParams: <MK extends keyof MP | keyof MR>(
248
- state: unknown,
249
- mutation: MK
250
- ) => (MK extends keyof MP & keyof MR ? MP[MK] : never) | undefined
251
- /** Selects entity by id and typename. */
252
- selectEntityById: <TN extends keyof T>(
253
- state: unknown,
254
- id: Key | null | undefined,
255
- typename: TN
256
- ) => T[TN] | undefined
257
- /** Selects all entities. */
258
- selectEntities: (state: unknown) => EntitiesMap<T>
259
- /** Selects all entities of provided typename. */
260
- selectEntitiesByTypename: <TN extends keyof T>(state: unknown, typename: TN) => EntitiesMap<T>[TN]
261
- }
344
+ /** This is a cacheStateSelector from createCache options, or default one if was not provided. */
345
+ selectCacheState: (state: any) => CacheState<Typenames, QP, QR, MP, MR>;
346
+ /** Selects query state. */
347
+ selectQueryState: <QK_1 extends keyof QP | keyof QR>(state: unknown, query: QK_1, cacheKey: Key) => QueryState<Typenames, QK_1 extends keyof QP & keyof QR ? QP[QK_1] : never, QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never>;
348
+ /** Selects query latest result. */
349
+ 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;
350
+ /** Selects query loading state. */
351
+ selectQueryLoading: <QK_1 extends keyof QP | keyof QR>(state: unknown, query: QK_1, cacheKey: Key) => false | Promise<NormalizedQueryResponse<Typenames, QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never>>;
352
+ /** Selects query latest error. */
353
+ selectQueryError: <QK_1 extends keyof QP | keyof QR>(state: unknown, query: QK_1, cacheKey: Key) => Error | undefined;
354
+ /** Selects query latest params. */
355
+ 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;
356
+ /** Selects query latest expiresAt. */
357
+ selectQueryExpiresAt: <QK_1 extends keyof QP | keyof QR>(state: unknown, query: QK_1, cacheKey: Key) => number | undefined;
358
+ /** Selects mutation state. */
359
+ selectMutationState: <MK_1 extends keyof MP | keyof MR>(state: unknown, mutation: MK_1) => MutationState<Typenames, MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never, MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>;
360
+ /** Selects mutation latest result. */
361
+ 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;
362
+ /** Selects mutation loading state. */
363
+ selectMutationLoading: <MK_1 extends keyof MP | keyof MR>(state: unknown, mutation: MK_1) => false | Promise<NormalizedQueryResponse<Typenames, MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>>;
364
+ /** Selects mutation latest error. */
365
+ selectMutationError: <MK_1 extends keyof MP | keyof MR>(state: unknown, mutation: MK_1) => Error | undefined;
366
+ /** Selects mutation latest params. */
367
+ 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;
368
+ /** Selects entity by id and typename. */
369
+ selectEntityById: <TN extends string>(state: unknown, id: Key | null | undefined, typename: TN) => object | undefined;
370
+ /** Selects all entities. */
371
+ selectEntities: (state: unknown) => EntitiesMap<Typenames> & import("./types").Mutable;
372
+ /** Selects all entities of provided typename. */
373
+ selectEntitiesByTypename: <TN extends string>(state: unknown, typename: TN) => (EntitiesMap<Typenames> & import("./types").Mutable)[TN];
374
+ };
262
375
  hooks: {
263
- /** Returns memoized object with query and mutate functions. Memoization dependency is the store. */
264
- useClient: () => {
265
- /**
266
- * Performs a query using provided options. Deduplicates calls with the same cache key. Always returns current cached result, even when query is cancelled or finished with error.
267
- * @param onlyIfExpired When true, cancels fetch if result is not yet expired.
268
- * @param skipFetch Fetch is cancelled and current cached result is returned.
269
- */
270
- query: <QK extends keyof (QP & QR)>(
271
- options: QueryOptions<N, T, QP, QR, QK, MP, MR>
272
- ) => Promise<QueryResult<QK extends keyof QP & keyof QR ? QR[QK] : never>>
376
+ /** Returns memoized object with query and mutate functions. Memoization dependency is the store. */
377
+ useClient: () => {
378
+ /**
379
+ * Performs a query using provided options. Deduplicates calls with the same cache key. Always returns current cached result, even when query is cancelled or finished with error.
380
+ * @param onlyIfExpired When true, cancels fetch if result is not yet expired.
381
+ * @param skipFetch Fetch is cancelled and current cached result is returned.
382
+ */
383
+ 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>>;
384
+ /**
385
+ * Performs a mutation, aborting previous one with the same mutation key. Returns result only if finished succesfully.
386
+ */
387
+ 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>>;
388
+ };
389
+ /** Fetches query when params change and subscribes to query state changes (subscription depends on `selectorComparer`). */
390
+ useQuery: <QK_1 extends keyof QP | keyof QR>(options: UseQueryOptions<N, Typenames, QK_1, QP, QR, MP, MR>) => readonly [Omit<QueryState<Typenames, 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>>];
391
+ /** Subscribes to provided mutation state and provides mutate function. */
392
+ 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>>, MutationState<Typenames, MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never, MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never>, () => boolean];
393
+ /** useSelector + selectEntityById. */
394
+ useSelectEntityById: <TN extends string>(id: Key | null | undefined, typename: TN) => object | undefined;
273
395
  /**
274
- * Performs a mutation, aborting previous one with the same mutation key. Returns result only if finished succesfully.
275
- */
276
- mutate: <MK extends keyof (MP & MR)>(
277
- options: MutateOptions<N, T, QP, QR, MP, MR, MK>
278
- ) => Promise<MutationResult<MK extends keyof MP & keyof MR ? MR[MK] : never>>
279
- }
280
- /** Fetches query when params change and subscribes to query state changes (subscription depends on `selectorComparer`). */
281
- useQuery: <QK extends keyof (QP & QR)>(
282
- options: Parameters<typeof useQuery<N, T, QP, QR, MP, MR, QK>>[3]
283
- ) => readonly [
284
- Omit<
285
- QueryState<
286
- T,
287
- QK extends keyof QP & keyof QR ? QP[QK] : never,
288
- QK extends keyof QP & keyof QR ? QR[QK] : never
289
- >,
290
- 'expiresAt'
291
- >,
292
- (
293
- options?:
294
- | Partial<Pick<QueryOptions<N, T, QP, QR, QK, MP, MR>, 'params' | 'onlyIfExpired'>>
295
- | undefined
296
- ) => Promise<
297
- QueryResult<
298
- QK extends infer T_1
299
- ? T_1 extends QK
300
- ? T_1 extends keyof QP & keyof QR
301
- ? QR[T_1]
302
- : never
303
- : never
304
- : never
305
- >
306
- >
307
- ]
308
- /** Subscribes to provided mutation state and provides mutate function. */
309
- useMutation: <MK extends keyof (MP & MR)>(
310
- options: Parameters<typeof useMutation<N, T, QP, QR, MP, MR, MK>>[3]
311
- ) => readonly [
312
- (
313
- params: MK extends keyof MP & keyof MR ? MP[MK] : never
314
- ) => Promise<
315
- MutationResult<
316
- MK extends infer T_1
317
- ? T_1 extends MK
318
- ? T_1 extends keyof MP & keyof MR
319
- ? MR[T_1]
320
- : never
321
- : never
322
- : never
323
- >
324
- >,
325
- MutationState<
326
- T,
327
- MK extends keyof MP & keyof MR ? MP[MK] : never,
328
- MK extends keyof MP & keyof MR ? MP[MK] : never
329
- >,
330
- () => boolean
331
- ]
332
- /** useSelector + selectEntityById. */
333
- useSelectEntityById: <TN extends keyof T>(id: Key | null | undefined, typename: TN) => T[TN] | undefined
334
- }
396
+ * useSelector + selectEntitiesByTypename. Also subscribes to collection's change key if `mutableCollections` enabled.
397
+ * @warning Subscribing to collections should be avoided.
398
+ * */
399
+ useEntitiesByTypename: <TN extends string>(typename: TN) => (EntitiesMap<Typenames> & import("./types").Mutable)[TN];
400
+ };
335
401
  utils: {
336
- /** Creates client by providing the store. Can be used when the store is a singleton - to not use a useClient hook for getting the client, but import it directly. */
337
- createClient: (store: Store) => {
338
- /**
339
- * Performs a query using provided options. Deduplicates calls with the same cache key. Always returns current cached result, even when query is cancelled or finished with error.
340
- * @param onlyIfExpired When true, cancels fetch if result is not yet expired.
341
- * @param skipFetch Fetch is cancelled and current cached result is returned.
342
- */
343
- query: <QK extends keyof (QP & QR)>(
344
- options: QueryOptions<N, T, QP, QR, QK, MP, MR>
345
- ) => Promise<QueryResult<QK extends keyof QP & keyof QR ? QR[QK] : never>>
402
+ /** Creates client by providing the store. Can be used when the store is a singleton - to not use a useClient hook for getting the client, but import it directly. */
403
+ createClient: (store: Store) => {
404
+ /**
405
+ * Performs a query using provided options. Deduplicates calls with the same cache key. Always returns current cached result, even when query is cancelled or finished with error.
406
+ * @param onlyIfExpired When true, cancels fetch if result is not yet expired.
407
+ * @param skipFetch Fetch is cancelled and current cached result is returned.
408
+ */
409
+ 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>>;
410
+ /**
411
+ * Performs a mutation, aborting previous one with the same mutation key. Returns result only if finished succesfully.
412
+ */
413
+ 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>>;
414
+ };
415
+ /** Generates the initial state by calling a reducer. Not needed for redux — it already generates it the same way when creating the store. */
416
+ getInitialState: () => CacheState<Typenames, QP, QR, MP, MR>;
346
417
  /**
347
- * Performs a mutation, aborting previous one with the same mutation key. Returns result only if finished succesfully.
418
+ * Apply changes to the entities map.
419
+ * Returns `undefined` if nothing to change, otherwise new `EntitiesMap<T>` with applied changes.
420
+ * Uses deep comparison if `deepComparisonEnabled` option is `true`.
421
+ * Performs additional checks for intersections if `additionalValidation` option is `true`, and prints warnings if finds any issues.
348
422
  */
349
- mutate: <MK extends keyof (MP & MR)>(
350
- options: MutateOptions<N, T, QP, QR, MP, MR, MK>
351
- ) => Promise<MutationResult<MK extends keyof MP & keyof MR ? MR[MK] : never>>
352
- }
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
- getInitialState: () => CacheState<T, QP, QR, MP, MR>
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
- */
361
- applyEntityChanges: (
362
- entities: Parameters<typeof applyEntityChanges<T>>[0],
363
- changes: Parameters<typeof applyEntityChanges<T>>[1]
364
- ) => EntitiesMap<T> | undefined
365
- }
366
- }
367
- }
368
-
369
- /** Creates reducer, actions and hooks for managing queries and mutations. */
370
- export declare const createCache: <N extends string, QP, QR, MP, MR>(
371
- partialCache: Partial<{
372
- queries: Partial<{
373
- [QK in keyof (QP & QR)]: QK extends keyof QP & keyof QR
374
- ? QueryInfo<N, Typenames, QP[QK], QR[QK], QP, QR, MP, MR>
375
- : never
376
- }>
377
- mutations: Partial<{
378
- [MK in keyof (MP & MR)]: MK extends keyof MP & keyof MR
379
- ? MutationInfo<N, Typenames, MP[MK], MR[MK], QP, QR, MP, MR>
380
- : never
381
- }>
382
- options: Partial<CacheOptions>
383
- storeHooks: Partial<{
384
- useStore: () => Store
385
- useSelector: <R>(selector: (state: unknown) => R, comparer?: (x: R, y: R) => boolean) => R
386
- }>
387
- cacheStateSelector: Partial<(state: any) => CacheState<Typenames, QP, QR, MP, MR>>
388
- }> &
389
- Omit<
390
- Omit<Cache<N, Typenames, QP, QR, MP, MR>, 'globals'>,
391
- 'queries' | 'mutations' | 'options' | 'storeHooks' | 'cacheStateSelector'
392
- > & {
393
- globals?: OptionalPartial<Globals<N, Typenames, QP, QR, MP, MR>, 'queries'> | undefined
394
- }
395
- ) => {
396
- /** Keeps all options, passed while creating the cache. */
397
- cache: Cache<N, Typenames, QP, QR, MP, MR>
398
- /** Reducer of the cache, should be added to redux/zustand store. */
399
- reducer: (
400
- state: CacheState<Typenames, QP, QR, MP, MR> | undefined,
401
- action:
402
- | {
403
- type: `@rrc/${N}/updateQueryStateAndEntities`
404
- queryKey: keyof QP & keyof QR
405
- queryCacheKey: Key
406
- state: Partial<QueryState<Typenames, QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>> | undefined
407
- entityChanges: EntityChanges<Typenames> | undefined
408
- }
409
- | {
410
- type: `@rrc/${N}/updateMutationStateAndEntities`
411
- mutationKey: keyof MP & keyof MR
412
- state:
413
- | Partial<MutationState<Typenames, MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>>
414
- | undefined
415
- entityChanges: EntityChanges<Typenames> | undefined
416
- }
417
- | {
418
- type: `@rrc/${N}/mergeEntityChanges`
419
- changes: EntityChanges<Typenames>
420
- }
421
- | {
422
- type: `@rrc/${N}/invalidateQuery`
423
- queries: {
424
- query: keyof QP & keyof QR
425
- cacheKey?: Key
426
- expiresAt?: number
427
- }[]
428
- }
429
- | {
430
- type: `@rrc/${N}/clearQueryState`
431
- queries: {
432
- query: keyof QP & keyof QR
433
- cacheKey?: Key
434
- }[]
435
- }
436
- | {
437
- type: `@rrc/${N}/clearMutationState`
438
- mutationKeys: (keyof MP & keyof MR)[]
439
- }
440
- | {
441
- type: `@rrc/${N}/clearCache`
442
- stateToKeep: Partial<CacheState<Typenames, QP, QR, MP, MR>> | undefined
443
- }
444
- ) => CacheState<Typenames, QP, QR, MP, MR>
445
- actions: {
446
- /** Updates query state, and optionally merges entity changes in a single action. */
447
- updateQueryStateAndEntities: {
448
- <K extends keyof QP & keyof QR>(
449
- queryKey: K,
450
- queryCacheKey: Key,
451
- state?: Partial<QueryState<Typenames, QP[K], QR[K]>> | undefined,
452
- entityChanges?: EntityChanges<Typenames> | undefined
453
- ): {
454
- type: `@rrc/${N}/updateQueryStateAndEntities`
455
- queryKey: K
456
- queryCacheKey: Key
457
- state: Partial<QueryState<Typenames, QP[K], QR[K]>> | undefined
458
- entityChanges: EntityChanges<Typenames> | undefined
459
- }
460
- type: `@rrc/${N}/updateQueryStateAndEntities`
461
- }
462
- /** Updates mutation state, and optionally merges entity changes in a single action. */
463
- updateMutationStateAndEntities: {
464
- <K extends keyof MP & keyof MR>(
465
- mutationKey: K,
466
- state?: Partial<MutationState<Typenames, MP[K], MR[K]>> | undefined,
467
- entityChanges?: EntityChanges<Typenames> | undefined
468
- ): {
469
- type: `@rrc/${N}/updateMutationStateAndEntities`
470
- mutationKey: K
471
- state: Partial<MutationState<Typenames, MP[K], MR[K]>> | undefined
472
- entityChanges: EntityChanges<Typenames> | undefined
473
- }
474
- type: `@rrc/${N}/updateMutationStateAndEntities`
475
- }
476
- /** Merges EntityChanges to the state. */
477
- mergeEntityChanges: {
478
- (changes: EntityChanges<Typenames>): {
479
- type: `@rrc/${N}/mergeEntityChanges`
480
- changes: EntityChanges<Typenames>
481
- }
482
- type: `@rrc/${N}/mergeEntityChanges`
483
- }
484
- /** Invalidates query states. */
485
- invalidateQuery: {
486
- <K extends keyof QP & keyof QR>(
487
- queries: {
488
- query: K
489
- cacheKey?: Key
490
- expiresAt?: number
491
- }[]
492
- ): {
493
- type: `@rrc/${N}/invalidateQuery`
494
- queries: {
495
- query: K
496
- cacheKey?: Key
497
- expiresAt?: number
498
- }[]
499
- }
500
- type: `@rrc/${N}/invalidateQuery`
501
- }
502
- /** Clears states for provided query keys and cache keys.
503
- * If cache key for query key is not provided, the whole state for query key is cleared. */
504
- clearQueryState: {
505
- <K extends keyof QP & keyof QR>(
506
- queries: {
507
- query: K
508
- cacheKey?: Key
509
- }[]
510
- ): {
511
- type: `@rrc/${N}/clearQueryState`
512
- queries: {
513
- query: K
514
- cacheKey?: Key
515
- }[]
516
- }
517
- type: `@rrc/${N}/clearQueryState`
518
- }
519
- /** Clears states for provided mutation keys. */
520
- clearMutationState: {
521
- <K extends keyof MP & keyof MR>(mutationKeys: K[]): {
522
- type: `@rrc/${N}/clearMutationState`
523
- mutationKeys: K[]
524
- }
525
- type: `@rrc/${N}/clearMutationState`
526
- }
527
- /** Replaces cache state with initial, optionally merging with provided state. Doesn't cancel running fetches and should be used with caution. */
528
- clearCache: {
529
- (stateToKeep?: Partial<CacheState<Typenames, QP, QR, MP, MR>> | undefined): {
530
- type: `@rrc/${N}/clearCache`
531
- stateToKeep: Partial<CacheState<Typenames, QP, QR, MP, MR>> | undefined
532
- }
533
- type: `@rrc/${N}/clearCache`
534
- }
535
- }
536
- selectors: {
537
- /** This is a cacheStateSelector from createCache options, or default one if was not provided. */
538
- selectCacheState: (state: any) => CacheState<Typenames, QP, QR, MP, MR>
539
- /** Selects query state. */
540
- selectQueryState: <QK_1 extends keyof QP | keyof QR>(
541
- state: unknown,
542
- query: QK_1,
543
- cacheKey: Key
544
- ) => QueryState<
545
- Typenames,
546
- QK_1 extends keyof QP & keyof QR ? QP[QK_1] : never,
547
- QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never
548
- >
549
- /** Selects query latest result. */
550
- selectQueryResult: <QK_1 extends keyof QP | keyof QR>(
551
- state: unknown,
552
- query: QK_1,
553
- cacheKey: Key
554
- ) => (QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never) | undefined
555
- /** Selects query loading state. */
556
- selectQueryLoading: <QK_1 extends keyof QP | keyof QR>(
557
- state: unknown,
558
- query: QK_1,
559
- cacheKey: Key
560
- ) =>
561
- | false
562
- | Promise<NormalizedQueryResponse<Typenames, QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never>>
563
- /** Selects query latest error. */
564
- selectQueryError: <QK_1 extends keyof QP | keyof QR>(
565
- state: unknown,
566
- query: QK_1,
567
- cacheKey: Key
568
- ) => Error | undefined
569
- /** Selects query latest params. */
570
- selectQueryParams: <QK_1 extends keyof QP | keyof QR>(
571
- state: unknown,
572
- query: QK_1,
573
- cacheKey: Key
574
- ) => (QK_1 extends keyof QP & keyof QR ? QP[QK_1] : never) | undefined
575
- /** Selects query latest expiresAt. */
576
- selectQueryExpiresAt: <QK_1 extends keyof QP | keyof QR>(
577
- state: unknown,
578
- query: QK_1,
579
- cacheKey: Key
580
- ) => number | undefined
581
- /** Selects mutation state. */
582
- selectMutationState: <MK_1 extends keyof MP | keyof MR>(
583
- state: unknown,
584
- mutation: MK_1
585
- ) => MutationState<
586
- Typenames,
587
- MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never,
588
- MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never
589
- >
590
- /** Selects mutation latest result. */
591
- selectMutationResult: <MK_1 extends keyof MP | keyof MR>(
592
- state: unknown,
593
- mutation: MK_1
594
- ) => (MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never) | undefined
595
- /** Selects mutation loading state. */
596
- selectMutationLoading: <MK_1 extends keyof MP | keyof MR>(
597
- state: unknown,
598
- mutation: MK_1
599
- ) =>
600
- | false
601
- | Promise<NormalizedQueryResponse<Typenames, MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>>
602
- /** Selects mutation latest error. */
603
- selectMutationError: <MK_1 extends keyof MP | keyof MR>(
604
- state: unknown,
605
- mutation: MK_1
606
- ) => Error | undefined
607
- /** Selects mutation latest params. */
608
- selectMutationParams: <MK_1 extends keyof MP | keyof MR>(
609
- state: unknown,
610
- mutation: MK_1
611
- ) => (MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never) | undefined
612
- /** Selects entity by id and typename. */
613
- selectEntityById: <TN extends string>(
614
- state: unknown,
615
- id: Key | null | undefined,
616
- typename: TN
617
- ) => object | undefined
618
- /** Selects all entities. */
619
- selectEntities: (state: unknown) => EntitiesMap<Typenames>
620
- /** Selects all entities of provided typename. */
621
- selectEntitiesByTypename: <TN extends string>(state: unknown, typename: TN) => Dict<object> | undefined
622
- }
623
- hooks: {
624
- /** Returns memoized object with query and mutate functions. Memoization dependency is the store. */
625
- useClient: () => {
626
- /**
627
- * Performs a query using provided options. Deduplicates calls with the same cache key. Always returns current cached result, even when query is cancelled or finished with error.
628
- * @param onlyIfExpired When true, cancels fetch if result is not yet expired.
629
- * @param skipFetch Fetch is cancelled and current cached result is returned.
630
- */
631
- query: <QK_1 extends keyof QP | keyof QR>(
632
- options: QueryOptions<N, Typenames, QP, QR, QK_1, MP, MR>
633
- ) => Promise<QueryResult<QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never>>
634
- /**
635
- * Performs a mutation, aborting previous one with the same mutation key. Returns result only if finished succesfully.
636
- */
637
- mutate: <MK_1 extends keyof MP | keyof MR>(
638
- options: MutateOptions<N, Typenames, QP, QR, MP, MR, MK_1>
639
- ) => Promise<MutationResult<MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>>
640
- }
641
- /** Fetches query when params change and subscribes to query state changes (subscription depends on `selectorComparer`). */
642
- useQuery: <QK_1 extends keyof QP | keyof QR>(
643
- options: UseQueryOptions<N, Typenames, QK_1, QP, QR, MP, MR>
644
- ) => readonly [
645
- Omit<
646
- QueryState<
647
- Typenames,
648
- QK_1 extends keyof QP & keyof QR ? QP[QK_1] : never,
649
- QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never
650
- >,
651
- 'expiresAt'
652
- >,
653
- (
654
- options?:
655
- | Partial<Pick<QueryOptions<N, Typenames, QP, QR, QK_1, MP, MR>, 'params' | 'onlyIfExpired'>>
656
- | undefined
657
- ) => Promise<
658
- QueryResult<
659
- QK_1 extends infer T
660
- ? T extends QK_1
661
- ? T extends keyof QP & keyof QR
662
- ? QR[T]
663
- : never
664
- : never
665
- : never
666
- >
667
- >
668
- ]
669
- /** Subscribes to provided mutation state and provides mutate function. */
670
- useMutation: <MK_1 extends keyof MP | keyof MR>(
671
- options: Omit<MutateOptions<N, Typenames, QP, QR, MP, MR, MK_1>, 'params'>
672
- ) => readonly [
673
- (
674
- params: MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never
675
- ) => Promise<
676
- MutationResult<
677
- MK_1 extends infer T
678
- ? T extends MK_1
679
- ? T extends keyof MP & keyof MR
680
- ? MR[T]
681
- : never
682
- : never
683
- : never
684
- >
685
- >,
686
- MutationState<
687
- Typenames,
688
- MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never,
689
- MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never
690
- >,
691
- () => boolean
692
- ]
693
- /** useSelector + selectEntityById. */
694
- useSelectEntityById: <TN extends string>(id: Key | null | undefined, typename: TN) => object | undefined
695
- }
696
- utils: {
697
- /** Creates client by providing the store. Can be used when the store is a singleton - to not use a useClient hook for getting the client, but import it directly. */
698
- createClient: (store: Store) => {
699
- /**
700
- * Performs a query using provided options. Deduplicates calls with the same cache key. Always returns current cached result, even when query is cancelled or finished with error.
701
- * @param onlyIfExpired When true, cancels fetch if result is not yet expired.
702
- * @param skipFetch Fetch is cancelled and current cached result is returned.
703
- */
704
- query: <QK_1 extends keyof QP | keyof QR>(
705
- options: QueryOptions<N, Typenames, QP, QR, QK_1, MP, MR>
706
- ) => Promise<QueryResult<QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never>>
707
- /**
708
- * Performs a mutation, aborting previous one with the same mutation key. Returns result only if finished succesfully.
709
- */
710
- mutate: <MK_1 extends keyof MP | keyof MR>(
711
- options: MutateOptions<N, Typenames, QP, QR, MP, MR, MK_1>
712
- ) => Promise<MutationResult<MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>>
713
- }
714
- /** Generates the initial state by calling a reducer. Not needed for redux — it already generates it the same way when creating the store. */
715
- getInitialState: () => CacheState<Typenames, QP, QR, MP, MR>
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
- */
722
- applyEntityChanges: (
723
- entities: EntitiesMap<Typenames>,
724
- changes: EntityChanges<Typenames>
725
- ) => EntitiesMap<Typenames> | undefined
726
- }
727
- }
423
+ applyEntityChanges: (entities: EntitiesMap<Typenames> & import("./types").Mutable, changes: EntityChanges<Typenames>) => EntitiesMap<Typenames> | undefined;
424
+ };
425
+ };