react-redux-cache 0.17.0 → 0.18.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.
- package/README.md +4 -4
- package/dist/createActions.d.ts +4 -4
- package/dist/createCache.d.ts +24 -24
- package/dist/createCacheReducer.js +13 -5
- package/dist/createSelectors.d.ts +4 -4
- package/dist/index.js +5 -1
- package/dist/mutate.js +12 -13
- package/dist/query.js +28 -16
- package/dist/types.d.ts +10 -9
- package/dist/useMutation.d.ts +1 -1
- package/dist/useMutation.js +1 -3
- package/dist/useQuery.d.ts +2 -2
- package/dist/useQuery.js +2 -3
- package/dist/utilsAndConstants.d.ts +2 -1
- package/dist/utilsAndConstants.js +5 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -44,7 +44,7 @@ Examples of states, generated by cache reducer from `/example` project:
|
|
|
44
44
|
// each query has its own map of query states, stored by cache key, which is generated from query params
|
|
45
45
|
getUser: {
|
|
46
46
|
"2": {result: 2, params: 2, expiresAt: 1727217298025},
|
|
47
|
-
"3": {loading:
|
|
47
|
+
"3": {loading: Promise<...>, params: 3}
|
|
48
48
|
},
|
|
49
49
|
getUsers: {
|
|
50
50
|
// example of paginated state under custom cache key
|
|
@@ -82,7 +82,7 @@ Examples of states, generated by cache reducer from `/example` project:
|
|
|
82
82
|
params: 2,
|
|
83
83
|
expiresAt: 1727217298025
|
|
84
84
|
},
|
|
85
|
-
"3": {loading:
|
|
85
|
+
"3": {loading: Promise<...>, params: 3}
|
|
86
86
|
},
|
|
87
87
|
getUsers: {
|
|
88
88
|
// example of paginated state under custom cache key
|
|
@@ -223,7 +223,7 @@ Zustand:
|
|
|
223
223
|
type State = {[cache.name]: ReturnType<typeof reducer>}
|
|
224
224
|
type Actions = {dispatch: (action: Parameters<typeof reducer>[1]) => void}
|
|
225
225
|
|
|
226
|
-
const initialState = getInitialState()
|
|
226
|
+
const initialState = {[cache.name]: getInitialState()}
|
|
227
227
|
|
|
228
228
|
export const useStore = create<State & Actions>((set, get) => ({
|
|
229
229
|
...initialState,
|
|
@@ -304,7 +304,7 @@ export const UserScreen = () => {
|
|
|
304
304
|
|
|
305
305
|
#### Error handling
|
|
306
306
|
|
|
307
|
-
Queries and mutations are wrapped in try/catch, so any error will lead to cancelling of any updates to the state except
|
|
307
|
+
Queries and mutations are wrapped in try/catch, so any error will lead to cancelling of any updates to the state except loading state and the caught error. If you still want to make some state updates, or just want to use thrown errors only for unexpected cases, consider returning expected errors as a part of the result:
|
|
308
308
|
|
|
309
309
|
```typescript
|
|
310
310
|
export const updateBank = (async (bank) => {
|
package/dist/createActions.d.ts
CHANGED
|
@@ -3,20 +3,20 @@ import { CacheState } from './types';
|
|
|
3
3
|
export type Actions<N extends string = string, T extends Typenames = Typenames, QP = unknown, QR = unknown, MP = unknown, MR = unknown> = ReturnType<typeof createActions<N, T, QP, QR, MP, MR>>;
|
|
4
4
|
export declare const createActions: <N extends string, T extends Typenames, QP, QR, MP, MR>(name: N) => {
|
|
5
5
|
updateQueryStateAndEntities: {
|
|
6
|
-
<K extends keyof (QP | QR)>(queryKey: K, queryCacheKey: Key, state?: Partial<QueryState<QP[K], QR[K]>>, entityChanges?: EntityChanges<T>): {
|
|
6
|
+
<K extends keyof (QP | QR)>(queryKey: K, queryCacheKey: Key, state?: Partial<QueryState<T, QP[K], QR[K]>>, entityChanges?: EntityChanges<T>): {
|
|
7
7
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
8
8
|
queryKey: K;
|
|
9
9
|
queryCacheKey: Key;
|
|
10
|
-
state: Partial<QueryState<QP[K], QR[K]>> | undefined;
|
|
10
|
+
state: Partial<QueryState<T, QP[K], QR[K]>> | undefined;
|
|
11
11
|
entityChanges: EntityChanges<T> | undefined;
|
|
12
12
|
};
|
|
13
13
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
14
14
|
};
|
|
15
15
|
updateMutationStateAndEntities: {
|
|
16
|
-
<K extends keyof (MP | MR)>(mutationKey: K, state?: Partial<MutationState<MP[K], MR[K]>>, entityChanges?: EntityChanges<T>): {
|
|
16
|
+
<K extends keyof (MP | MR)>(mutationKey: K, state?: Partial<MutationState<T, MP[K], MR[K]>>, entityChanges?: EntityChanges<T>): {
|
|
17
17
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
18
18
|
mutationKey: K;
|
|
19
|
-
state: Partial<MutationState<MP[K], MR[K]>> | undefined;
|
|
19
|
+
state: Partial<MutationState<T, MP[K], MR[K]>> | undefined;
|
|
20
20
|
entityChanges: EntityChanges<T> | undefined;
|
|
21
21
|
};
|
|
22
22
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
package/dist/createCache.d.ts
CHANGED
|
@@ -21,12 +21,12 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
21
21
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
22
22
|
queryKey: keyof QP & keyof QR;
|
|
23
23
|
queryCacheKey: Key;
|
|
24
|
-
state: Partial<import("./types").QueryState<QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>> | undefined;
|
|
24
|
+
state: Partial<import("./types").QueryState<T, QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>> | undefined;
|
|
25
25
|
entityChanges: import("./types").EntityChanges<T> | undefined;
|
|
26
26
|
} | {
|
|
27
27
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
28
28
|
mutationKey: keyof MP & keyof MR;
|
|
29
|
-
state: Partial<import("./types").MutationState<MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>> | undefined;
|
|
29
|
+
state: Partial<import("./types").MutationState<T, MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>> | undefined;
|
|
30
30
|
entityChanges: import("./types").EntityChanges<T> | undefined;
|
|
31
31
|
} | {
|
|
32
32
|
type: `@rrc/${N}/mergeEntityChanges`;
|
|
@@ -54,21 +54,21 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
54
54
|
actions: {
|
|
55
55
|
/** Updates query state, and optionally merges entity changes in a single action. */
|
|
56
56
|
updateQueryStateAndEntities: {
|
|
57
|
-
<K extends keyof QP & keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<import("./types").QueryState<QP[K], QR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<T> | undefined): {
|
|
57
|
+
<K extends keyof QP & keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<import("./types").QueryState<T, QP[K], QR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<T> | undefined): {
|
|
58
58
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
59
59
|
queryKey: K;
|
|
60
60
|
queryCacheKey: Key;
|
|
61
|
-
state: Partial<import("./types").QueryState<QP[K], QR[K]>> | undefined;
|
|
61
|
+
state: Partial<import("./types").QueryState<T, QP[K], QR[K]>> | undefined;
|
|
62
62
|
entityChanges: import("./types").EntityChanges<T> | undefined;
|
|
63
63
|
};
|
|
64
64
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
65
65
|
};
|
|
66
66
|
/** Updates mutation state, and optionally merges entity changes in a single action. */
|
|
67
67
|
updateMutationStateAndEntities: {
|
|
68
|
-
<K extends keyof MP & keyof MR>(mutationKey: K, state?: Partial<import("./types").MutationState<MP[K], MR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<T> | undefined): {
|
|
68
|
+
<K extends keyof MP & keyof MR>(mutationKey: K, state?: Partial<import("./types").MutationState<T, MP[K], MR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<T> | undefined): {
|
|
69
69
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
70
70
|
mutationKey: K;
|
|
71
|
-
state: Partial<import("./types").MutationState<MP[K], MR[K]>> | undefined;
|
|
71
|
+
state: Partial<import("./types").MutationState<T, MP[K], MR[K]>> | undefined;
|
|
72
72
|
entityChanges: import("./types").EntityChanges<T> | undefined;
|
|
73
73
|
};
|
|
74
74
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
@@ -133,11 +133,11 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
133
133
|
/** This is a cacheStateSelector from createCache options, or default one if was not provided. */
|
|
134
134
|
selectCacheState: (state: any) => import("./types").CacheState<T, QP, QR, MP, MR>;
|
|
135
135
|
/** Selects query state. */
|
|
136
|
-
selectQueryState: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => import("./types").QueryState<QK extends keyof QP & keyof QR ? QP[QK] : never, QK extends keyof QP & keyof QR ? QR[QK] : never>;
|
|
136
|
+
selectQueryState: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => import("./types").QueryState<T, QK extends keyof QP & keyof QR ? QP[QK] : never, QK extends keyof QP & keyof QR ? QR[QK] : never>;
|
|
137
137
|
/** Selects query latest result. */
|
|
138
138
|
selectQueryResult: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => (QK extends keyof QP & keyof QR ? QR[QK] : never) | undefined;
|
|
139
139
|
/** Selects query loading state. */
|
|
140
|
-
selectQueryLoading: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) =>
|
|
140
|
+
selectQueryLoading: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => false | Promise<import("./types").NormalizedQueryResponse<T, QK extends keyof QP & keyof QR ? QR[QK] : never>>;
|
|
141
141
|
/** Selects query latest error. */
|
|
142
142
|
selectQueryError: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => Error | undefined;
|
|
143
143
|
/** Selects query latest params. */
|
|
@@ -145,11 +145,11 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
145
145
|
/** Selects query latest expiresAt. */
|
|
146
146
|
selectQueryExpiresAt: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => number | undefined;
|
|
147
147
|
/** Selects mutation state. */
|
|
148
|
-
selectMutationState: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => import("./types").MutationState<MK extends keyof MP & keyof MR ? MP[MK] : never, MK extends keyof MP & keyof MR ? MR[MK] : never>;
|
|
148
|
+
selectMutationState: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => import("./types").MutationState<T, MK extends keyof MP & keyof MR ? MP[MK] : never, MK extends keyof MP & keyof MR ? MR[MK] : never>;
|
|
149
149
|
/** Selects mutation latest result. */
|
|
150
150
|
selectMutationResult: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => (MK extends keyof MP & keyof MR ? MR[MK] : never) | undefined;
|
|
151
151
|
/** Selects mutation loading state. */
|
|
152
|
-
selectMutationLoading: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) =>
|
|
152
|
+
selectMutationLoading: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => false | Promise<import("./types").NormalizedQueryResponse<T, MK extends keyof MP & keyof MR ? MR[MK] : never>>;
|
|
153
153
|
/** Selects mutation latest error. */
|
|
154
154
|
selectMutationError: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => Error | undefined;
|
|
155
155
|
/** Selects mutation latest params. */
|
|
@@ -168,9 +168,9 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
168
168
|
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>>;
|
|
169
169
|
};
|
|
170
170
|
/** Fetches query when params change and subscribes to query state changes (except `expiresAt` field). */
|
|
171
|
-
useQuery: <QK extends keyof (QP & QR)>(options: Parameters<typeof useQuery<N, T, QP, QR, MP, MR, QK>>[3]) => readonly [Omit<import("./types").QueryState<QK extends keyof QP & keyof QR ? QP[QK] : never, QK extends keyof QP & keyof QR ? QR[QK] : never>, "expiresAt">, (options?: Partial<Pick<QueryOptions<N, T, QP, QR, QK, MP, MR>, "params" | "onlyIfExpired">> | undefined) => Promise<QueryResult<QK extends infer T_1 ? T_1 extends QK ? T_1 extends keyof QP & keyof QR ? QR[T_1] : never : never : never>>];
|
|
171
|
+
useQuery: <QK extends keyof (QP & QR)>(options: Parameters<typeof useQuery<N, T, QP, QR, MP, MR, QK>>[3]) => readonly [Omit<import("./types").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>>];
|
|
172
172
|
/** Subscribes to provided mutation state and provides mutate function. */
|
|
173
|
-
useMutation: <MK extends keyof (MP & MR)>(options: Parameters<typeof useMutation<N, T, QP, QR, MP, MR, MK>>[3]) => readonly [(params: MK extends keyof MP & keyof MR ? MP[MK] : never) => Promise<MutationResult<MK extends infer T_1 ? T_1 extends MK ? T_1 extends keyof MP & keyof MR ? MR[T_1] : never : never : never>>, import("./types").MutationState<MK extends keyof MP & keyof MR ? MP[MK] : never, MK extends keyof MP & keyof MR ? MP[MK] : never>, () => boolean];
|
|
173
|
+
useMutation: <MK extends keyof (MP & MR)>(options: Parameters<typeof useMutation<N, T, QP, QR, MP, MR, MK>>[3]) => readonly [(params: MK extends keyof MP & keyof MR ? MP[MK] : never) => Promise<MutationResult<MK extends infer T_1 ? T_1 extends MK ? T_1 extends keyof MP & keyof MR ? MR[T_1] : never : never : never>>, import("./types").MutationState<T, MK extends keyof MP & keyof MR ? MP[MK] : never, MK extends keyof MP & keyof MR ? MP[MK] : never>, () => boolean];
|
|
174
174
|
/** useSelector + selectEntityById. */
|
|
175
175
|
useSelectEntityById: <TN extends keyof T>(id: Key | null | undefined, typename: TN) => T[TN] | undefined;
|
|
176
176
|
};
|
|
@@ -205,12 +205,12 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCach
|
|
|
205
205
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
206
206
|
queryKey: keyof QP & keyof QR;
|
|
207
207
|
queryCacheKey: Key;
|
|
208
|
-
state: Partial<import("./types").QueryState<QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>> | undefined;
|
|
208
|
+
state: Partial<import("./types").QueryState<Typenames, QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>> | undefined;
|
|
209
209
|
entityChanges: import("./types").EntityChanges<Typenames> | undefined;
|
|
210
210
|
} | {
|
|
211
211
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
212
212
|
mutationKey: keyof MP & keyof MR;
|
|
213
|
-
state: Partial<import("./types").MutationState<MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>> | undefined;
|
|
213
|
+
state: Partial<import("./types").MutationState<Typenames, MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>> | undefined;
|
|
214
214
|
entityChanges: import("./types").EntityChanges<Typenames> | undefined;
|
|
215
215
|
} | {
|
|
216
216
|
type: `@rrc/${N}/mergeEntityChanges`;
|
|
@@ -238,21 +238,21 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCach
|
|
|
238
238
|
actions: {
|
|
239
239
|
/** Updates query state, and optionally merges entity changes in a single action. */
|
|
240
240
|
updateQueryStateAndEntities: {
|
|
241
|
-
<K extends keyof QP & keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<import("./types").QueryState<QP[K], QR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<Typenames> | undefined): {
|
|
241
|
+
<K extends keyof QP & keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<import("./types").QueryState<Typenames, QP[K], QR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<Typenames> | undefined): {
|
|
242
242
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
243
243
|
queryKey: K;
|
|
244
244
|
queryCacheKey: Key;
|
|
245
|
-
state: Partial<import("./types").QueryState<QP[K], QR[K]>> | undefined;
|
|
245
|
+
state: Partial<import("./types").QueryState<Typenames, QP[K], QR[K]>> | undefined;
|
|
246
246
|
entityChanges: import("./types").EntityChanges<Typenames> | undefined;
|
|
247
247
|
};
|
|
248
248
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
249
249
|
};
|
|
250
250
|
/** Updates mutation state, and optionally merges entity changes in a single action. */
|
|
251
251
|
updateMutationStateAndEntities: {
|
|
252
|
-
<K extends keyof MP & keyof MR>(mutationKey: K, state?: Partial<import("./types").MutationState<MP[K], MR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<Typenames> | undefined): {
|
|
252
|
+
<K extends keyof MP & keyof MR>(mutationKey: K, state?: Partial<import("./types").MutationState<Typenames, MP[K], MR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<Typenames> | undefined): {
|
|
253
253
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
254
254
|
mutationKey: K;
|
|
255
|
-
state: Partial<import("./types").MutationState<MP[K], MR[K]>> | undefined;
|
|
255
|
+
state: Partial<import("./types").MutationState<Typenames, MP[K], MR[K]>> | undefined;
|
|
256
256
|
entityChanges: import("./types").EntityChanges<Typenames> | undefined;
|
|
257
257
|
};
|
|
258
258
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
@@ -317,11 +317,11 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCach
|
|
|
317
317
|
/** This is a cacheStateSelector from createCache options, or default one if was not provided. */
|
|
318
318
|
selectCacheState: (state: any) => import("./types").CacheState<Typenames, QP, QR, MP, MR>;
|
|
319
319
|
/** Selects query state. */
|
|
320
|
-
selectQueryState: <QK_1 extends keyof QP | keyof QR>(state: unknown, query: QK_1, cacheKey: Key) => import("./types").QueryState<QK_1 extends keyof QP & keyof QR ? QP[QK_1] : never, QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never>;
|
|
320
|
+
selectQueryState: <QK_1 extends keyof QP | keyof QR>(state: unknown, query: QK_1, cacheKey: Key) => import("./types").QueryState<Typenames, QK_1 extends keyof QP & keyof QR ? QP[QK_1] : never, QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never>;
|
|
321
321
|
/** Selects query latest result. */
|
|
322
322
|
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;
|
|
323
323
|
/** Selects query loading state. */
|
|
324
|
-
selectQueryLoading: <QK_1 extends keyof QP | keyof QR>(state: unknown, query: QK_1, cacheKey: Key) =>
|
|
324
|
+
selectQueryLoading: <QK_1 extends keyof QP | keyof QR>(state: unknown, query: QK_1, cacheKey: Key) => false | Promise<import("./types").NormalizedQueryResponse<Typenames, QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never>>;
|
|
325
325
|
/** Selects query latest error. */
|
|
326
326
|
selectQueryError: <QK_1 extends keyof QP | keyof QR>(state: unknown, query: QK_1, cacheKey: Key) => Error | undefined;
|
|
327
327
|
/** Selects query latest params. */
|
|
@@ -329,11 +329,11 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCach
|
|
|
329
329
|
/** Selects query latest expiresAt. */
|
|
330
330
|
selectQueryExpiresAt: <QK_1 extends keyof QP | keyof QR>(state: unknown, query: QK_1, cacheKey: Key) => number | undefined;
|
|
331
331
|
/** Selects mutation state. */
|
|
332
|
-
selectMutationState: <MK_1 extends keyof MP | keyof MR>(state: unknown, mutation: MK_1) => import("./types").MutationState<MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never, MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>;
|
|
332
|
+
selectMutationState: <MK_1 extends keyof MP | keyof MR>(state: unknown, mutation: MK_1) => import("./types").MutationState<Typenames, MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never, MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>;
|
|
333
333
|
/** Selects mutation latest result. */
|
|
334
334
|
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;
|
|
335
335
|
/** Selects mutation loading state. */
|
|
336
|
-
selectMutationLoading: <MK_1 extends keyof MP | keyof MR>(state: unknown, mutation: MK_1) =>
|
|
336
|
+
selectMutationLoading: <MK_1 extends keyof MP | keyof MR>(state: unknown, mutation: MK_1) => false | Promise<import("./types").NormalizedQueryResponse<Typenames, MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>>;
|
|
337
337
|
/** Selects mutation latest error. */
|
|
338
338
|
selectMutationError: <MK_1 extends keyof MP | keyof MR>(state: unknown, mutation: MK_1) => Error | undefined;
|
|
339
339
|
/** Selects mutation latest params. */
|
|
@@ -352,9 +352,9 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCach
|
|
|
352
352
|
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>>;
|
|
353
353
|
};
|
|
354
354
|
/** Fetches query when params change and subscribes to query state changes (except `expiresAt` field). */
|
|
355
|
-
useQuery: <QK_1 extends keyof QP | keyof QR>(options: import("./types").UseQueryOptions<N, Typenames, QK_1, QP, QR, MP, MR>) => readonly [Omit<import("./types").QueryState<QK_1 extends keyof QP & keyof QR ? QP[QK_1] : never, QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never>, "expiresAt">, (options?: Partial<Pick<QueryOptions<N, Typenames, QP, QR, QK_1, MP, MR>, "params" | "onlyIfExpired">> | undefined) => Promise<QueryResult<QK_1 extends infer T ? T extends QK_1 ? T extends keyof QP & keyof QR ? QR[T] : never : never : never>>];
|
|
355
|
+
useQuery: <QK_1 extends keyof QP | keyof QR>(options: import("./types").UseQueryOptions<N, Typenames, QK_1, QP, QR, MP, MR>) => readonly [Omit<import("./types").QueryState<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>>];
|
|
356
356
|
/** Subscribes to provided mutation state and provides mutate function. */
|
|
357
|
-
useMutation: <MK_1 extends keyof MP | keyof MR>(options: Omit<MutateOptions<N, Typenames, QP, QR, MP, MR, MK_1>, "params">) => readonly [(params: MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never) => Promise<MutationResult<MK_1 extends infer T ? T extends MK_1 ? T extends keyof MP & keyof MR ? MR[T] : never : never : never>>, import("./types").MutationState<MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never, MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never>, () => boolean];
|
|
357
|
+
useMutation: <MK_1 extends keyof MP | keyof MR>(options: Omit<MutateOptions<N, Typenames, QP, QR, MP, MR, MK_1>, "params">) => readonly [(params: MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never) => Promise<MutationResult<MK_1 extends infer T ? T extends MK_1 ? T extends keyof MP & keyof MR ? MR[T] : never : never : never>>, import("./types").MutationState<Typenames, MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never, MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never>, () => boolean];
|
|
358
358
|
/** useSelector + selectEntityById. */
|
|
359
359
|
useSelectEntityById: <TN extends string>(id: Key | null | undefined, typename: TN) => object | undefined;
|
|
360
360
|
};
|
|
@@ -13,8 +13,19 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
13
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
14
|
exports.createCacheReducer = void 0;
|
|
15
15
|
const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
16
|
-
const optionalQueryKeys = [
|
|
17
|
-
|
|
16
|
+
const optionalQueryKeys = [
|
|
17
|
+
'error',
|
|
18
|
+
'expiresAt',
|
|
19
|
+
'result',
|
|
20
|
+
'params',
|
|
21
|
+
'loading',
|
|
22
|
+
];
|
|
23
|
+
const optionalMutationKeys = [
|
|
24
|
+
'error',
|
|
25
|
+
'result',
|
|
26
|
+
'params',
|
|
27
|
+
'loading',
|
|
28
|
+
];
|
|
18
29
|
const createCacheReducer = (actions, queryKeys, cacheOptions) => {
|
|
19
30
|
const initialState = Object.freeze({
|
|
20
31
|
entities: Object.freeze({}),
|
|
@@ -82,9 +93,6 @@ const createCacheReducer = (actions, queryKeys, cacheOptions) => {
|
|
|
82
93
|
delete newMutationState[key];
|
|
83
94
|
}
|
|
84
95
|
}
|
|
85
|
-
if ('loading' in newMutationState && !newMutationState.loading) {
|
|
86
|
-
delete newMutationState.loading;
|
|
87
|
-
}
|
|
88
96
|
// skip if new state deep equals to the old state
|
|
89
97
|
if (deepEqual === null || deepEqual === void 0 ? void 0 : deepEqual(oldMutationState !== null && oldMutationState !== void 0 ? oldMutationState : utilsAndConstants_1.EMPTY_OBJECT, newMutationState)) {
|
|
90
98
|
newMutationState = undefined;
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { Cache, Key, MutationState, QueryState, Typenames } from './types';
|
|
2
2
|
export type Selectors<N extends string = string, T extends Typenames = Typenames, QP = unknown, QR = unknown, MP = unknown, MR = unknown> = ReturnType<typeof createSelectors<N, T, QP, QR, MP, MR>>;
|
|
3
3
|
export declare const createSelectors: <N extends string, T extends Typenames, QP, QR, MP, MR>(cache: Cache<N, T, QP, QR, MP, MR>) => {
|
|
4
|
-
selectQueryState: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => QueryState<QK extends keyof (QP | QR) ? QP[QK] : never, QK extends keyof (QP | QR) ? QR[QK] : never>;
|
|
4
|
+
selectQueryState: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => QueryState<T, QK extends keyof (QP | QR) ? QP[QK] : never, QK extends keyof (QP | QR) ? QR[QK] : never>;
|
|
5
5
|
selectQueryResult: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => (QK extends keyof QP & keyof QR ? QR[QK] : never) | undefined;
|
|
6
|
-
selectQueryLoading: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) =>
|
|
6
|
+
selectQueryLoading: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => false | Promise<import("./types").NormalizedQueryResponse<T, QK extends keyof QP & keyof QR ? QR[QK] : never>>;
|
|
7
7
|
selectQueryError: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => Error | undefined;
|
|
8
8
|
selectQueryParams: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => (QK extends keyof QP & keyof QR ? QP[QK] : never) | undefined;
|
|
9
9
|
selectQueryExpiresAt: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => number | undefined;
|
|
10
|
-
selectMutationState: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => MutationState<MK extends keyof (MP | MR) ? MP[MK] : never, MK extends keyof (MP | MR) ? MR[MK] : never>;
|
|
10
|
+
selectMutationState: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => MutationState<T, MK extends keyof (MP | MR) ? MP[MK] : never, MK extends keyof (MP | MR) ? MR[MK] : never>;
|
|
11
11
|
selectMutationResult: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => (MK extends keyof MP & keyof MR ? MR[MK] : never) | undefined;
|
|
12
|
-
selectMutationLoading: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) =>
|
|
12
|
+
selectMutationLoading: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => false | Promise<import("./types").NormalizedQueryResponse<T, MK extends keyof MP & keyof MR ? MR[MK] : never>>;
|
|
13
13
|
selectMutationError: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => Error | undefined;
|
|
14
14
|
selectMutationParams: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => (MK extends keyof MP & keyof MR ? MP[MK] : never) | undefined;
|
|
15
15
|
selectEntityById: <TN extends keyof T>(state: unknown, id: Key | null | undefined, typename: TN) => T[TN] | undefined;
|
package/dist/index.js
CHANGED
|
@@ -26,9 +26,13 @@ Object.defineProperty(exports, "defaultGetCacheKey", { enumerable: true, get: fu
|
|
|
26
26
|
Object.defineProperty(exports, "FetchPolicy", { enumerable: true, get: function () { return utilsAndConstants_1.FetchPolicy; } });
|
|
27
27
|
Object.defineProperty(exports, "isEmptyObject", { enumerable: true, get: function () { return utilsAndConstants_1.isEmptyObject; } });
|
|
28
28
|
// Backlog
|
|
29
|
+
// highest
|
|
30
|
+
// generate full api docs
|
|
31
|
+
// cancel all queries / mutations
|
|
32
|
+
// selectors optional cache key
|
|
29
33
|
// ! high (1.0.0-rc.0)
|
|
30
34
|
// optimistic response
|
|
31
|
-
//
|
|
35
|
+
// features list
|
|
32
36
|
// ! medium
|
|
33
37
|
// onCancel & onAbort
|
|
34
38
|
// remove empty entities and queries from state
|
package/dist/mutate.js
CHANGED
|
@@ -30,23 +30,22 @@ const mutate = (logTag_1, store_1, cache_1, actions_1, selectors_1, mutationKey_
|
|
|
30
30
|
if (abortController !== undefined) {
|
|
31
31
|
abortController.abort();
|
|
32
32
|
}
|
|
33
|
-
else {
|
|
34
|
-
store.dispatch(updateMutationStateAndEntities(mutationKey, {
|
|
35
|
-
loading: true,
|
|
36
|
-
params,
|
|
37
|
-
result: undefined,
|
|
38
|
-
}));
|
|
39
|
-
}
|
|
40
33
|
}
|
|
41
34
|
const abortController = new AbortController();
|
|
42
35
|
abortControllersOfStore[mutationKey] = abortController;
|
|
36
|
+
const mutatePromise = cache.mutations[mutationKey].mutation(
|
|
37
|
+
// @ts-expect-error fix later
|
|
38
|
+
params, store, abortController.signal);
|
|
39
|
+
store.dispatch(updateMutationStateAndEntities(mutationKey, {
|
|
40
|
+
// @ts-expect-error TODO fix types
|
|
41
|
+
loading: mutatePromise,
|
|
42
|
+
params,
|
|
43
|
+
result: undefined,
|
|
44
|
+
}));
|
|
43
45
|
let response;
|
|
44
46
|
let error;
|
|
45
|
-
const fetchFn = cache.mutations[mutationKey].mutation;
|
|
46
47
|
try {
|
|
47
|
-
response = yield
|
|
48
|
-
// @ts-expect-error fix later
|
|
49
|
-
params, store, abortController.signal);
|
|
48
|
+
response = yield mutatePromise;
|
|
50
49
|
}
|
|
51
50
|
catch (e) {
|
|
52
51
|
error = e;
|
|
@@ -64,7 +63,7 @@ const mutate = (logTag_1, store_1, cache_1, actions_1, selectors_1, mutationKey_
|
|
|
64
63
|
if (error) {
|
|
65
64
|
store.dispatch(updateMutationStateAndEntities(mutationKey, {
|
|
66
65
|
error: error,
|
|
67
|
-
loading:
|
|
66
|
+
loading: undefined,
|
|
68
67
|
}));
|
|
69
68
|
// @ts-expect-error params
|
|
70
69
|
if (!(onError === null || onError === void 0 ? void 0 : onError(error, params, store, actions, selectors))) {
|
|
@@ -80,7 +79,7 @@ const mutate = (logTag_1, store_1, cache_1, actions_1, selectors_1, mutationKey_
|
|
|
80
79
|
if (response) {
|
|
81
80
|
const newState = {
|
|
82
81
|
error: undefined,
|
|
83
|
-
loading:
|
|
82
|
+
loading: undefined,
|
|
84
83
|
result: response.result,
|
|
85
84
|
};
|
|
86
85
|
store.dispatch(updateMutationStateAndEntities(mutationKey, newState, response));
|
package/dist/query.js
CHANGED
|
@@ -12,51 +12,63 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.query = void 0;
|
|
13
13
|
const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
14
14
|
const query = (logTag, store, cache, actions, selectors, queryKey, cacheKey, params, secondsToLive, onlyIfExpired, mergeResults, onCompleted, onSuccess, onError) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
-
var _a, _b, _c, _d
|
|
15
|
+
var _a, _b, _c, _d;
|
|
16
16
|
if (secondsToLive === void 0) { secondsToLive = (_a = cache.queries[queryKey].secondsToLive) !== null && _a !== void 0 ? _a : cache.globals.queries.secondsToLive; }
|
|
17
17
|
if (mergeResults === void 0) { mergeResults = cache.queries[queryKey].mergeResults; }
|
|
18
18
|
if (onCompleted === void 0) { onCompleted = cache.queries[queryKey].onCompleted; }
|
|
19
19
|
if (onSuccess === void 0) { onSuccess = cache.queries[queryKey].onSuccess; }
|
|
20
20
|
if (onError === void 0) { onError = cache.queries[queryKey].onError; }
|
|
21
|
+
const { selectQueryResult, selectQueryState } = selectors;
|
|
21
22
|
const logsEnabled = cache.options.logsEnabled;
|
|
22
|
-
const
|
|
23
|
-
const queryStateOnStart = cacheStateSelector(store.getState()).queries[queryKey][cacheKey];
|
|
23
|
+
const queryStateOnStart = selectQueryState(store.getState(), queryKey, cacheKey);
|
|
24
24
|
if (queryStateOnStart === null || queryStateOnStart === void 0 ? void 0 : queryStateOnStart.loading) {
|
|
25
25
|
logsEnabled &&
|
|
26
|
-
(0, utilsAndConstants_1.log)(`${logTag} cancelled: already loading`, {
|
|
26
|
+
(0, utilsAndConstants_1.log)(`${logTag} fetch cancelled: already loading`, {
|
|
27
27
|
queryStateOnStart,
|
|
28
28
|
params,
|
|
29
29
|
cacheKey,
|
|
30
30
|
});
|
|
31
|
-
|
|
31
|
+
const error = yield queryStateOnStart.loading.then(utilsAndConstants_1.NOOP).catch(catchAndReturn);
|
|
32
|
+
return error
|
|
33
|
+
? {
|
|
34
|
+
cancelled: 'loading',
|
|
35
|
+
error,
|
|
36
|
+
}
|
|
37
|
+
: {
|
|
38
|
+
cancelled: 'loading',
|
|
39
|
+
result: selectQueryResult(store.getState(), queryKey, cacheKey),
|
|
40
|
+
};
|
|
32
41
|
}
|
|
33
42
|
if (onlyIfExpired && (queryStateOnStart === null || queryStateOnStart === void 0 ? void 0 : queryStateOnStart.expiresAt) != null && queryStateOnStart.expiresAt > Date.now()) {
|
|
34
43
|
logsEnabled &&
|
|
35
|
-
(0, utilsAndConstants_1.log)(`${logTag} cancelled: not expired yet`, {
|
|
44
|
+
(0, utilsAndConstants_1.log)(`${logTag} fetch cancelled: not expired yet`, {
|
|
36
45
|
queryStateOnStart,
|
|
37
46
|
params,
|
|
38
47
|
cacheKey,
|
|
39
48
|
onlyIfExpired,
|
|
40
49
|
});
|
|
41
|
-
return
|
|
50
|
+
return {
|
|
51
|
+
cancelled: 'not-expired',
|
|
52
|
+
result: queryStateOnStart.result,
|
|
53
|
+
};
|
|
42
54
|
}
|
|
43
55
|
const { updateQueryStateAndEntities } = actions;
|
|
56
|
+
const fetchPromise = cache.queries[queryKey].query(
|
|
57
|
+
// @ts-expect-error fix later
|
|
58
|
+
params, store);
|
|
44
59
|
store.dispatch(updateQueryStateAndEntities(queryKey, cacheKey, {
|
|
45
|
-
loading:
|
|
60
|
+
loading: fetchPromise,
|
|
46
61
|
params,
|
|
47
62
|
}));
|
|
48
63
|
logsEnabled && (0, utilsAndConstants_1.log)(`${logTag} started`, { queryKey, params, cacheKey, queryStateOnStart, onlyIfExpired });
|
|
49
64
|
let response;
|
|
50
|
-
const fetchFn = cache.queries[queryKey].query;
|
|
51
65
|
try {
|
|
52
|
-
response = yield
|
|
53
|
-
// @ts-expect-error fix later
|
|
54
|
-
params, store);
|
|
66
|
+
response = yield fetchPromise;
|
|
55
67
|
}
|
|
56
68
|
catch (error) {
|
|
57
69
|
store.dispatch(updateQueryStateAndEntities(queryKey, cacheKey, {
|
|
58
70
|
error: error,
|
|
59
|
-
loading:
|
|
71
|
+
loading: undefined,
|
|
60
72
|
}));
|
|
61
73
|
// @ts-expect-error params
|
|
62
74
|
if (!(onError === null || onError === void 0 ? void 0 : onError(error, params, store))) {
|
|
@@ -71,12 +83,12 @@ const query = (logTag, store, cache, actions, selectors, queryKey, cacheKey, par
|
|
|
71
83
|
}
|
|
72
84
|
const newState = {
|
|
73
85
|
error: undefined,
|
|
74
|
-
loading:
|
|
86
|
+
loading: undefined,
|
|
75
87
|
expiresAt: (_d = response.expiresAt) !== null && _d !== void 0 ? _d : (secondsToLive != null ? Date.now() + secondsToLive * 1000 : undefined),
|
|
76
88
|
result: mergeResults
|
|
77
89
|
? mergeResults(
|
|
78
90
|
// @ts-expect-error fix later
|
|
79
|
-
(
|
|
91
|
+
selectQueryResult(store.getState(), queryKey, cacheKey), response, params, store, actions, selectors)
|
|
80
92
|
: response.result,
|
|
81
93
|
};
|
|
82
94
|
store.dispatch(updateQueryStateAndEntities(queryKey, cacheKey, newState, response));
|
|
@@ -92,4 +104,4 @@ const query = (logTag, store, cache, actions, selectors, queryKey, cacheKey, par
|
|
|
92
104
|
};
|
|
93
105
|
});
|
|
94
106
|
exports.query = query;
|
|
95
|
-
const
|
|
107
|
+
const catchAndReturn = (x) => x;
|
package/dist/types.d.ts
CHANGED
|
@@ -53,7 +53,7 @@ export type Globals<N extends string, T extends Typenames, QP, QR, MP, MR> = {
|
|
|
53
53
|
/** Determines when useQuery fetch triggers should start fetching. Fetch is performed if function returns true.
|
|
54
54
|
* Fetch triggers are: 1) mount 2) cache key change 3) skipFetch value change to false.
|
|
55
55
|
* @Default FetchPolicy.NoCacheOrExpired */
|
|
56
|
-
fetchPolicy: (expired: boolean, params: unknown, state: QueryState<unknown, unknown>, store: Store, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean;
|
|
56
|
+
fetchPolicy: (expired: boolean, params: unknown, state: QueryState<T, unknown, unknown>, store: Store, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean;
|
|
57
57
|
/** Disables any fetches when set to true. Triggers fetch when changed to false. @Default false */
|
|
58
58
|
skipFetch: boolean;
|
|
59
59
|
/** If set, this value updates expiresAt value of query state when query result is received. @Default undefined */
|
|
@@ -85,10 +85,10 @@ export type EntityIds<T extends Typenames> = {
|
|
|
85
85
|
export type CacheState<T extends Typenames, QP, QR, MP, MR> = {
|
|
86
86
|
entities: EntitiesMap<T>;
|
|
87
87
|
queries: {
|
|
88
|
-
[QK in keyof (QP | QR)]: Dict<QueryState<QP[QK], QR[QK]> | undefined>;
|
|
88
|
+
[QK in keyof (QP | QR)]: Dict<QueryState<T, QP[QK], QR[QK]> | undefined>;
|
|
89
89
|
};
|
|
90
90
|
mutations: {
|
|
91
|
-
[MK in keyof (MP | MR)]: MutationState<MP[MK], MR[MK]>;
|
|
91
|
+
[MK in keyof (MP | MR)]: MutationState<T, MP[MK], MR[MK]>;
|
|
92
92
|
};
|
|
93
93
|
};
|
|
94
94
|
export type QueryInfo<N extends string, T extends Typenames = Typenames, P = unknown, R = unknown, QP = unknown, QR = unknown, MP = unknown, MR = unknown> = Partial<Pick<Globals<N, T, QP, QR, MP, MR>['queries'], 'skipFetch' | 'secondsToLive'>> & {
|
|
@@ -96,7 +96,7 @@ export type QueryInfo<N extends string, T extends Typenames = Typenames, P = unk
|
|
|
96
96
|
/** Determines when useQuery fetch triggers should start fetching. Fetch is performed if function returns true.
|
|
97
97
|
* Fetch triggers are: 1) mount 2) cache key change 3) skipFetch value change to false.
|
|
98
98
|
* @Default FetchPolicy.NoCacheOrExpired */
|
|
99
|
-
fetchPolicy?: (expired: boolean, params: P, queryState: QueryState<P, R>, store: Store, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean;
|
|
99
|
+
fetchPolicy?: (expired: boolean, params: P, queryState: QueryState<T, P, R>, store: Store, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean;
|
|
100
100
|
/** Merges results before saving to the store. Default implementation is using the latest result. */
|
|
101
101
|
mergeResults?: (oldResult: R | undefined, response: NormalizedQueryResponse<T, R>, params: P | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => R;
|
|
102
102
|
/**
|
|
@@ -118,7 +118,7 @@ params: P,
|
|
|
118
118
|
/** Store */
|
|
119
119
|
store: Store) => Promise<QueryResponse<R>>;
|
|
120
120
|
export type NormalizedQuery<T extends Typenames = Typenames, P = unknown, R = unknown> = (...args: Parameters<Query<P, R>>) => Promise<NormalizedQueryResponse<T, R>>;
|
|
121
|
-
export type QueryState<P, R> = MutationState<P, R> & {
|
|
121
|
+
export type QueryState<T extends Typenames, P, R> = MutationState<T, P, R> & {
|
|
122
122
|
/**
|
|
123
123
|
* Timestamp in milliseconds, after which state is considered expired.
|
|
124
124
|
* Hooks may refetch the query again when component mounts, cache key or skip option change, depending on the fetch policy.
|
|
@@ -142,7 +142,8 @@ export type QueryResponse<R = unknown> = {
|
|
|
142
142
|
export type NormalizedQueryResponse<T extends Typenames = Typenames, R = unknown> = EntityChanges<T> & QueryResponse<R>;
|
|
143
143
|
export type QueryResult<R = unknown> = {
|
|
144
144
|
error?: unknown;
|
|
145
|
-
cancelled
|
|
145
|
+
/** Fetch cancelled reason. */
|
|
146
|
+
cancelled?: 'loading' | 'not-expired';
|
|
146
147
|
result?: R;
|
|
147
148
|
};
|
|
148
149
|
export type MutationInfo<N extends string, T extends Typenames = Typenames, P = unknown, R = unknown, QP = unknown, QR = unknown, MP = unknown, MR = unknown> = Pick<QueryInfo<N, T, P, R, QP, QR, MP, MR>, 'onCompleted' | 'onSuccess' | 'onError'> & {
|
|
@@ -169,9 +170,9 @@ export type MutationResult<R = unknown> = {
|
|
|
169
170
|
aborted?: true;
|
|
170
171
|
result?: R;
|
|
171
172
|
};
|
|
172
|
-
export type MutationState<P, R> = {
|
|
173
|
-
/**
|
|
174
|
-
loading?:
|
|
173
|
+
export type MutationState<T extends Typenames, P, R> = {
|
|
174
|
+
/** Set when fetch is currently in progress. */
|
|
175
|
+
loading?: Promise<NormalizedQueryResponse<T, R>>;
|
|
175
176
|
/** Result of the latest successfull response. */
|
|
176
177
|
result?: R;
|
|
177
178
|
/** Error of the latest response. */
|
package/dist/useMutation.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Actions } from './createActions';
|
|
2
2
|
import { Selectors } from './createSelectors';
|
|
3
3
|
import { Cache, Key, MutateOptions, MutationState, Store, Typenames } from './types';
|
|
4
|
-
export declare const useMutation: <N extends string, T extends Typenames, QP, QR, MP, MR, MK extends keyof (MP & MR)>(cache: Cache<N, T, QP, QR, MP, MR>, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>, options: Omit<MutateOptions<N, T, QP, QR, MP, MR, MK>, "params">, abortControllers: WeakMap<Store, Record<Key, AbortController>>) => readonly [(params: MK extends keyof MP & keyof MR ? MP[MK] : never) => Promise<import("./types").MutationResult<MK extends infer T_1 ? T_1 extends MK ? T_1 extends keyof MP & keyof MR ? MR[T_1] : never : never : never>>, MutationState<MK extends keyof MP & keyof MR ? MP[MK] : never, MK extends keyof MP & keyof MR ? MP[MK] : never>, () => boolean];
|
|
4
|
+
export declare const useMutation: <N extends string, T extends Typenames, QP, QR, MP, MR, MK extends keyof (MP & MR)>(cache: Cache<N, T, QP, QR, MP, MR>, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>, options: Omit<MutateOptions<N, T, QP, QR, MP, MR, MK>, "params">, abortControllers: WeakMap<Store, Record<Key, AbortController>>) => readonly [(params: MK extends keyof MP & keyof MR ? MP[MK] : never) => Promise<import("./types").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];
|
package/dist/useMutation.js
CHANGED
|
@@ -43,9 +43,7 @@ const useMutation = (cache, actions, selectors, options, abortControllers) => {
|
|
|
43
43
|
return false;
|
|
44
44
|
}
|
|
45
45
|
abortController.abort();
|
|
46
|
-
store.dispatch(actions.updateMutationStateAndEntities(mutationKey, {
|
|
47
|
-
loading: false,
|
|
48
|
-
}));
|
|
46
|
+
store.dispatch(actions.updateMutationStateAndEntities(mutationKey, { loading: undefined }));
|
|
49
47
|
return true;
|
|
50
48
|
},
|
|
51
49
|
];
|
package/dist/useQuery.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Actions } from './createActions';
|
|
2
2
|
import { Selectors } from './createSelectors';
|
|
3
3
|
import { Cache, QueryOptions, QueryState, Typenames, UseQueryOptions } from './types';
|
|
4
|
-
export declare const useQuery: <N extends string, T extends Typenames, QP, QR, MP, MR, QK extends keyof (QP & QR)>(cache: Cache<N, T, QP, QR, MP, MR>, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>, options: UseQueryOptions<N, T, QK, QP, QR, MP, MR>) => readonly [Omit<QueryState<QK extends keyof QP & keyof QR ? QP[QK] : never, QK extends keyof QP & keyof QR ? QR[QK] : never>, "expiresAt">, (options?: Partial<Pick<QueryOptions<N, T, QP, QR, QK, MP, MR>, "params" | "onlyIfExpired">>) => Promise<import("./types").QueryResult<QK extends infer T_1 ? T_1 extends QK ? T_1 extends keyof QP & keyof QR ? QR[T_1] : never : never : never>>];
|
|
4
|
+
export declare const useQuery: <N extends string, T extends Typenames, QP, QR, MP, MR, QK extends keyof (QP & QR)>(cache: Cache<N, T, QP, QR, MP, MR>, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>, options: UseQueryOptions<N, T, QK, QP, QR, MP, MR>) => 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">>) => Promise<import("./types").QueryResult<QK extends infer T_1 ? T_1 extends QK ? T_1 extends keyof QP & keyof QR ? QR[T_1] : never : never : never>>];
|
|
5
5
|
/** Omit `expiresAt` from comparison */
|
|
6
|
-
export declare const useQuerySelectorStateComparer: <P, R>(state1: QueryState<P, R> | undefined, state2: QueryState<P, R> | undefined) => boolean;
|
|
6
|
+
export declare const useQuerySelectorStateComparer: <T extends Typenames, P, R>(state1: QueryState<T, P, R> | undefined, state2: QueryState<T, P, R> | undefined) => boolean;
|
package/dist/useQuery.js
CHANGED
|
@@ -16,9 +16,9 @@ const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
|
16
16
|
const useQuery = (cache, actions, selectors, options) => {
|
|
17
17
|
var _a, _b, _c;
|
|
18
18
|
const { query: queryKey, skipFetch = false, params, secondsToLive, fetchPolicy = (_a = cache.queries[queryKey].fetchPolicy) !== null && _a !== void 0 ? _a : cache.globals.queries.fetchPolicy, mergeResults, onCompleted, onSuccess, onError, } = options;
|
|
19
|
+
const { selectQueryState } = selectors;
|
|
19
20
|
const logsEnabled = cache.options.logsEnabled;
|
|
20
21
|
const getCacheKey = (_b = cache.queries[queryKey].getCacheKey) !== null && _b !== void 0 ? _b : (utilsAndConstants_1.defaultGetCacheKey);
|
|
21
|
-
const cacheStateSelector = cache.cacheStateSelector;
|
|
22
22
|
const store = cache.storeHooks.useStore();
|
|
23
23
|
// @ts-expect-error fix types later
|
|
24
24
|
const cacheKey = getCacheKey(params);
|
|
@@ -36,8 +36,7 @@ const useQuery = (cache, actions, selectors, options) => {
|
|
|
36
36
|
[store, queryKey, cacheKey]);
|
|
37
37
|
/** Query state */
|
|
38
38
|
const queryState = (_c = cache.storeHooks.useSelector((state) => {
|
|
39
|
-
|
|
40
|
-
return queryState; // TODO proper type
|
|
39
|
+
return selectQueryState(state, queryKey, cacheKey); // TODO proper type
|
|
41
40
|
}, (exports.useQuerySelectorStateComparer))) !== null && _c !== void 0 ? _c : utilsAndConstants_1.EMPTY_OBJECT;
|
|
42
41
|
(0, react_1.useEffect)(() => {
|
|
43
42
|
if (skipFetch) {
|
|
@@ -6,11 +6,12 @@ export declare const optionalUtils: {
|
|
|
6
6
|
export declare const IS_DEV: boolean;
|
|
7
7
|
export declare const EMPTY_OBJECT: Readonly<{}>;
|
|
8
8
|
export declare const EMPTY_ARRAY: readonly never[];
|
|
9
|
+
export declare const NOOP: () => void;
|
|
9
10
|
export declare const defaultGetCacheKey: <P = unknown>(params: P) => Key;
|
|
10
11
|
export declare const log: (tag: string, data?: unknown) => void;
|
|
11
12
|
export declare const FetchPolicy: {
|
|
12
13
|
/** Only if cache does not exist (result is undefined) or expired. */
|
|
13
|
-
NoCacheOrExpired: (expired: boolean,
|
|
14
|
+
NoCacheOrExpired: <T extends Typenames = Typenames, P = unknown, R = unknown>(expired: boolean, _params: P, state: QueryState<T, P, R>) => boolean;
|
|
14
15
|
/** Every fetch trigger. */
|
|
15
16
|
Always: () => boolean;
|
|
16
17
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isEmptyObject = exports.applyEntityChanges = exports.FetchPolicy = exports.log = exports.defaultGetCacheKey = exports.EMPTY_ARRAY = exports.EMPTY_OBJECT = exports.IS_DEV = exports.optionalUtils = exports.PACKAGE_SHORT_NAME = void 0;
|
|
3
|
+
exports.isEmptyObject = exports.applyEntityChanges = exports.FetchPolicy = exports.log = exports.defaultGetCacheKey = exports.NOOP = exports.EMPTY_ARRAY = exports.EMPTY_OBJECT = exports.IS_DEV = exports.optionalUtils = exports.PACKAGE_SHORT_NAME = void 0;
|
|
4
4
|
exports.PACKAGE_SHORT_NAME = 'rrc';
|
|
5
5
|
exports.optionalUtils = {
|
|
6
6
|
deepEqual: undefined,
|
|
@@ -22,6 +22,9 @@ exports.IS_DEV = (() => {
|
|
|
22
22
|
})();
|
|
23
23
|
exports.EMPTY_OBJECT = Object.freeze({});
|
|
24
24
|
exports.EMPTY_ARRAY = Object.freeze([]);
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
26
|
+
const NOOP = () => { };
|
|
27
|
+
exports.NOOP = NOOP;
|
|
25
28
|
const defaultGetCacheKey = (params) => {
|
|
26
29
|
switch (typeof params) {
|
|
27
30
|
case 'string':
|
|
@@ -40,7 +43,7 @@ const log = (tag, data) => {
|
|
|
40
43
|
exports.log = log;
|
|
41
44
|
exports.FetchPolicy = {
|
|
42
45
|
/** Only if cache does not exist (result is undefined) or expired. */
|
|
43
|
-
NoCacheOrExpired: (expired,
|
|
46
|
+
NoCacheOrExpired: (expired, _params, state) => {
|
|
44
47
|
return expired || state.result === undefined;
|
|
45
48
|
},
|
|
46
49
|
/** Every fetch trigger. */
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "react-redux-cache",
|
|
3
3
|
"author": "Alexander Danilov",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.18.1",
|
|
6
6
|
"description": "Powerful data fetching and caching library for Redux and Zustand that supports normalization.",
|
|
7
7
|
"main": "dist/index.js",
|
|
8
8
|
"types": "dist/index.d.ts",
|