react-redux-cache 0.22.1 → 0.22.2

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