react-redux-cache 0.10.0 → 0.12.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -12
- package/dist/createActions.d.ts +18 -18
- package/dist/createCache.d.ts +84 -84
- package/dist/createCache.js +6 -4
- package/dist/createCacheReducer.d.ts +6 -36
- package/dist/createCacheReducer.js +55 -21
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -4
- package/dist/mutate.d.ts +1 -1
- package/dist/mutate.js +3 -3
- package/dist/query.d.ts +1 -1
- package/dist/types.d.ts +25 -21
- package/dist/useMutation.d.ts +1 -1
- package/dist/useMutation.js +1 -1
- package/dist/useQuery.d.ts +1 -1
- package/dist/useQuery.js +1 -1
- package/dist/utilsAndConstants.d.ts +1 -3
- package/dist/utilsAndConstants.js +8 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -41,13 +41,12 @@ Examples of states, generated by cache reducer from `/example` project:
|
|
|
41
41
|
queries: {
|
|
42
42
|
// each query has its own map of query states, stored by cache key, which is generated from query params
|
|
43
43
|
getUser: {
|
|
44
|
-
"2": {
|
|
44
|
+
"2": {result: 2, params: 2, expiresAt: 1727217298025},
|
|
45
45
|
"3": {loading: true, params: 3}
|
|
46
46
|
},
|
|
47
47
|
getUsers: {
|
|
48
48
|
// example of paginated state under custom cache key
|
|
49
49
|
"all-pages": {
|
|
50
|
-
loading: false,
|
|
51
50
|
result: {items: [0,1,2], page: 1},
|
|
52
51
|
params: {page: 1}
|
|
53
52
|
}
|
|
@@ -56,7 +55,6 @@ Examples of states, generated by cache reducer from `/example` project:
|
|
|
56
55
|
mutations: {
|
|
57
56
|
// each mutation has its own state as well
|
|
58
57
|
updateUser: {
|
|
59
|
-
loading: false,
|
|
60
58
|
result: 1,
|
|
61
59
|
params: {id: 1, name: "User 1 *"}
|
|
62
60
|
}
|
|
@@ -78,7 +76,6 @@ Examples of states, generated by cache reducer from `/example` project:
|
|
|
78
76
|
// each query has its own map of query states, stored by cache key, which is generated from query params
|
|
79
77
|
getUser: {
|
|
80
78
|
"2": {
|
|
81
|
-
loading: false,
|
|
82
79
|
result: {id: 2, bank: {id: "2", name: "Bank 2"}, name: "User 2"},
|
|
83
80
|
params: 2,
|
|
84
81
|
expiresAt: 1727217298025
|
|
@@ -88,7 +85,6 @@ Examples of states, generated by cache reducer from `/example` project:
|
|
|
88
85
|
getUsers: {
|
|
89
86
|
// example of paginated state under custom cache key
|
|
90
87
|
"all-pages": {
|
|
91
|
-
loading: false,
|
|
92
88
|
result: {
|
|
93
89
|
items: [
|
|
94
90
|
{id: 0, bank: {id: "0", name: "Bank 0"}, name: "User 0 *"},
|
|
@@ -104,7 +100,6 @@ Examples of states, generated by cache reducer from `/example` project:
|
|
|
104
100
|
mutations: {
|
|
105
101
|
// each mutation has its own state as well
|
|
106
102
|
updateUser: {
|
|
107
|
-
loading: false,
|
|
108
103
|
result: {id: 1, bank: {id: "1", name: "Bank 1"}, name: "User 1 *"},
|
|
109
104
|
params: {id: 1, name: "User 1 *"}
|
|
110
105
|
}
|
|
@@ -212,7 +207,7 @@ Perfect implementation is when the backend already returns normalized data.
|
|
|
212
207
|
|
|
213
208
|
// Example of query with normalization (recommended)
|
|
214
209
|
|
|
215
|
-
export const getUser = async (id) => {
|
|
210
|
+
export const getUser = (async (id) => {
|
|
216
211
|
// Result can be get by any way - fetch, axios etc, even with database connection.
|
|
217
212
|
// There is no limitation here.
|
|
218
213
|
const response = await ...
|
|
@@ -220,24 +215,24 @@ export const getUser = async (id) => {
|
|
|
220
215
|
// In this example normalizr package is used, but it is not necessary.
|
|
221
216
|
return normalize(response, getUserSchema)
|
|
222
217
|
// satisfies keyword is used here for proper typing of params and returned value.
|
|
223
|
-
} satisfies
|
|
218
|
+
}) satisfies NormalizedQuery<CacheTypenames, number>
|
|
224
219
|
|
|
225
220
|
// Example of query without normalization (not recommended), with selecting access token from the store
|
|
226
221
|
|
|
227
|
-
export const getBank = (id, {getState}) => {
|
|
222
|
+
export const getBank = (async (id, {getState}) => {
|
|
228
223
|
const token = tokenSelector(getState())
|
|
229
224
|
const result: Bank = ...
|
|
230
225
|
return {result} // result is bank object, no entities passed
|
|
231
|
-
} satisfies Query<string>
|
|
226
|
+
}) satisfies Query<string>
|
|
232
227
|
|
|
233
228
|
// Example of mutation with normalization
|
|
234
229
|
|
|
235
|
-
export const removeUser = async (id, _, abortSignal) => {
|
|
230
|
+
export const removeUser = (async (id, _, abortSignal) => {
|
|
236
231
|
await ...
|
|
237
232
|
return {
|
|
238
233
|
remove: { users: [id] },
|
|
239
234
|
}
|
|
240
|
-
} satisfies
|
|
235
|
+
}) satisfies NormalizedQuery<CacheTypenames, number>
|
|
241
236
|
```
|
|
242
237
|
|
|
243
238
|
### Usage
|
package/dist/createActions.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export type ActionMap<N extends string, T extends Typenames, QP, QR, MP, MR> = R
|
|
|
3
3
|
export declare const createActions: <N extends string, T extends Typenames, QP, QR, MP, MR>(name: N) => {
|
|
4
4
|
/** Updates query state, and optionally merges entity changes in a single action. */
|
|
5
5
|
updateQueryStateAndEntities: {
|
|
6
|
-
<K extends keyof QP
|
|
6
|
+
<K extends keyof (QP | QR)>(queryKey: K, queryCacheKey: Key, state?: Partial<QueryState<QP[K], QR[K]>>, entityChanges?: EntityChanges<T>): {
|
|
7
7
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
8
8
|
queryKey: K;
|
|
9
9
|
queryCacheKey: Key;
|
|
@@ -14,10 +14,10 @@ export declare const createActions: <N extends string, T extends Typenames, QP,
|
|
|
14
14
|
};
|
|
15
15
|
/** Updates mutation state, and optionally merges entity changes in a single action. */
|
|
16
16
|
updateMutationStateAndEntities: {
|
|
17
|
-
<
|
|
17
|
+
<K extends keyof (MP | MR)>(mutationKey: K, state?: Partial<MutationState<MP[K], MR[K]>>, entityChanges?: EntityChanges<T>): {
|
|
18
18
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
19
|
-
mutationKey:
|
|
20
|
-
state: Partial<MutationState<MP[
|
|
19
|
+
mutationKey: K;
|
|
20
|
+
state: Partial<MutationState<MP[K], MR[K]>> | undefined;
|
|
21
21
|
entityChanges: EntityChanges<T> | undefined;
|
|
22
22
|
};
|
|
23
23
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
@@ -32,22 +32,22 @@ export declare const createActions: <N extends string, T extends Typenames, QP,
|
|
|
32
32
|
};
|
|
33
33
|
/** Invalidates query states. */
|
|
34
34
|
invalidateQuery: {
|
|
35
|
-
<
|
|
35
|
+
<K extends keyof (QP | QR)>(queries: {
|
|
36
36
|
/** Query key */
|
|
37
|
-
query:
|
|
37
|
+
query: K;
|
|
38
38
|
/** Query cache key */
|
|
39
|
-
cacheKey?: Key
|
|
39
|
+
cacheKey?: Key;
|
|
40
40
|
/** Unix timestamp at which query expires. Is set to the query state. @default Date.now() */
|
|
41
|
-
expiresAt?: number
|
|
41
|
+
expiresAt?: number;
|
|
42
42
|
}[]): {
|
|
43
43
|
type: `@rrc/${N}/invalidateQuery`;
|
|
44
44
|
queries: {
|
|
45
45
|
/** Query key */
|
|
46
|
-
query:
|
|
46
|
+
query: K;
|
|
47
47
|
/** Query cache key */
|
|
48
|
-
cacheKey?: Key
|
|
48
|
+
cacheKey?: Key;
|
|
49
49
|
/** Unix timestamp at which query expires. Is set to the query state. @default Date.now() */
|
|
50
|
-
expiresAt?: number
|
|
50
|
+
expiresAt?: number;
|
|
51
51
|
}[];
|
|
52
52
|
};
|
|
53
53
|
type: `@rrc/${N}/invalidateQuery`;
|
|
@@ -55,27 +55,27 @@ export declare const createActions: <N extends string, T extends Typenames, QP,
|
|
|
55
55
|
/** Clear states for provided query keys and cache keys.
|
|
56
56
|
* If cache key for query key is not provided, the whole state for query key is cleared. */
|
|
57
57
|
clearQueryState: {
|
|
58
|
-
<
|
|
58
|
+
<K extends keyof (QP | QR)>(queries: {
|
|
59
59
|
/** Query key */
|
|
60
|
-
query:
|
|
60
|
+
query: K;
|
|
61
61
|
/** Query cache key */
|
|
62
|
-
cacheKey?: Key
|
|
62
|
+
cacheKey?: Key;
|
|
63
63
|
}[]): {
|
|
64
64
|
type: `@rrc/${N}/clearQueryState`;
|
|
65
65
|
queries: {
|
|
66
66
|
/** Query key */
|
|
67
|
-
query:
|
|
67
|
+
query: K;
|
|
68
68
|
/** Query cache key */
|
|
69
|
-
cacheKey?: Key
|
|
69
|
+
cacheKey?: Key;
|
|
70
70
|
}[];
|
|
71
71
|
};
|
|
72
72
|
type: `@rrc/${N}/clearQueryState`;
|
|
73
73
|
};
|
|
74
74
|
/** Clear states for provided mutation keys. */
|
|
75
75
|
clearMutationState: {
|
|
76
|
-
<
|
|
76
|
+
<K extends keyof (MP | MR)>(mutationKeys: K[]): {
|
|
77
77
|
type: `@rrc/${N}/clearMutationState`;
|
|
78
|
-
mutationKeys:
|
|
78
|
+
mutationKeys: K[];
|
|
79
79
|
};
|
|
80
80
|
type: `@rrc/${N}/clearMutationState`;
|
|
81
81
|
};
|
package/dist/createCache.d.ts
CHANGED
|
@@ -11,14 +11,14 @@ import { applyEntityChanges } from './utilsAndConstants';
|
|
|
11
11
|
* })
|
|
12
12
|
*/
|
|
13
13
|
export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
14
|
-
createCache: <N extends string, QP, QR, MP, MR>(partialCache: OptionalPartial<Cache<N, T, QP, QR, MP, MR>, "
|
|
14
|
+
createCache: <N extends string, QP, QR, MP, MR>(partialCache: OptionalPartial<Cache<N, T, QP, QR, MP, MR>, "options" | "queries" | "mutations" | "cacheStateSelector" | "globals">) => {
|
|
15
15
|
/** Keeps all options, passed while creating the cache. */
|
|
16
16
|
cache: Cache<N, T, QP, QR, MP, MR>;
|
|
17
17
|
/** Reducer of the cache, should be added to redux store. */
|
|
18
18
|
reducer: (state: {
|
|
19
19
|
entities: import("./types").EntitiesMap<T>;
|
|
20
|
-
queries:
|
|
21
|
-
mutations:
|
|
20
|
+
queries: { [QK in keyof (QP | QR)]: import("./types").Dict<QueryState<QP[QK], QR[QK]> | undefined>; };
|
|
21
|
+
mutations: { [MK in keyof (MP | MR)]: MutationState<MP[MK], MR[MK]>; };
|
|
22
22
|
} | undefined, action: {
|
|
23
23
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
24
24
|
queryKey: keyof QP & keyof QR;
|
|
@@ -37,22 +37,22 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
37
37
|
type: `@rrc/${N}/invalidateQuery`;
|
|
38
38
|
queries: {
|
|
39
39
|
query: keyof QP & keyof QR;
|
|
40
|
-
cacheKey?: Key
|
|
41
|
-
expiresAt?: number
|
|
40
|
+
cacheKey?: Key;
|
|
41
|
+
expiresAt?: number;
|
|
42
42
|
}[];
|
|
43
43
|
} | {
|
|
44
44
|
type: `@rrc/${N}/clearQueryState`;
|
|
45
45
|
queries: {
|
|
46
46
|
query: keyof QP & keyof QR;
|
|
47
|
-
cacheKey?: Key
|
|
47
|
+
cacheKey?: Key;
|
|
48
48
|
}[];
|
|
49
49
|
} | {
|
|
50
50
|
type: `@rrc/${N}/clearMutationState`;
|
|
51
51
|
mutationKeys: (keyof MP & keyof MR)[];
|
|
52
52
|
}) => {
|
|
53
53
|
entities: import("./types").EntitiesMap<T>;
|
|
54
|
-
queries:
|
|
55
|
-
mutations:
|
|
54
|
+
queries: { [QK in keyof (QP | QR)]: import("./types").Dict<QueryState<QP[QK], QR[QK]> | undefined>; };
|
|
55
|
+
mutations: { [MK in keyof (MP | MR)]: MutationState<MP[MK], MR[MK]>; };
|
|
56
56
|
};
|
|
57
57
|
actions: {
|
|
58
58
|
updateQueryStateAndEntities: {
|
|
@@ -66,10 +66,10 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
66
66
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
67
67
|
};
|
|
68
68
|
updateMutationStateAndEntities: {
|
|
69
|
-
<
|
|
69
|
+
<K extends keyof MP & keyof MR>(mutationKey: K, state?: Partial<MutationState<MP[K], MR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<T> | undefined): {
|
|
70
70
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
71
|
-
mutationKey:
|
|
72
|
-
state: Partial<MutationState<MP[
|
|
71
|
+
mutationKey: K;
|
|
72
|
+
state: Partial<MutationState<MP[K], MR[K]>> | undefined;
|
|
73
73
|
entityChanges: import("./types").EntityChanges<T> | undefined;
|
|
74
74
|
};
|
|
75
75
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
@@ -82,90 +82,90 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
82
82
|
type: `@rrc/${N}/mergeEntityChanges`;
|
|
83
83
|
};
|
|
84
84
|
invalidateQuery: {
|
|
85
|
-
<
|
|
86
|
-
query:
|
|
87
|
-
cacheKey?: Key
|
|
88
|
-
expiresAt?: number
|
|
85
|
+
<K extends keyof QP & keyof QR>(queries: {
|
|
86
|
+
query: K;
|
|
87
|
+
cacheKey?: Key;
|
|
88
|
+
expiresAt?: number;
|
|
89
89
|
}[]): {
|
|
90
90
|
type: `@rrc/${N}/invalidateQuery`;
|
|
91
91
|
queries: {
|
|
92
|
-
query:
|
|
93
|
-
cacheKey?: Key
|
|
94
|
-
expiresAt?: number
|
|
92
|
+
query: K;
|
|
93
|
+
cacheKey?: Key;
|
|
94
|
+
expiresAt?: number;
|
|
95
95
|
}[];
|
|
96
96
|
};
|
|
97
97
|
type: `@rrc/${N}/invalidateQuery`;
|
|
98
98
|
};
|
|
99
99
|
clearQueryState: {
|
|
100
|
-
<
|
|
101
|
-
query:
|
|
102
|
-
cacheKey?: Key
|
|
100
|
+
<K extends keyof QP & keyof QR>(queries: {
|
|
101
|
+
query: K;
|
|
102
|
+
cacheKey?: Key;
|
|
103
103
|
}[]): {
|
|
104
104
|
type: `@rrc/${N}/clearQueryState`;
|
|
105
105
|
queries: {
|
|
106
|
-
query:
|
|
107
|
-
cacheKey?: Key
|
|
106
|
+
query: K;
|
|
107
|
+
cacheKey?: Key;
|
|
108
108
|
}[];
|
|
109
109
|
};
|
|
110
110
|
type: `@rrc/${N}/clearQueryState`;
|
|
111
111
|
};
|
|
112
112
|
clearMutationState: {
|
|
113
|
-
<
|
|
113
|
+
<K extends keyof MP & keyof MR>(mutationKeys: K[]): {
|
|
114
114
|
type: `@rrc/${N}/clearMutationState`;
|
|
115
|
-
mutationKeys:
|
|
115
|
+
mutationKeys: K[];
|
|
116
116
|
};
|
|
117
117
|
type: `@rrc/${N}/clearMutationState`;
|
|
118
118
|
};
|
|
119
119
|
};
|
|
120
120
|
selectors: {
|
|
121
121
|
/** Selects query state. */
|
|
122
|
-
selectQueryState: <
|
|
122
|
+
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>;
|
|
123
123
|
/** Selects query latest result. */
|
|
124
|
-
selectQueryResult: <
|
|
124
|
+
selectQueryResult: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => (QK extends keyof QP & keyof QR ? QR[QK] : never) | undefined;
|
|
125
125
|
/** Selects query loading state. */
|
|
126
|
-
selectQueryLoading: <
|
|
126
|
+
selectQueryLoading: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => boolean;
|
|
127
127
|
/** Selects query latest error. */
|
|
128
|
-
selectQueryError: <
|
|
128
|
+
selectQueryError: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => Error | undefined;
|
|
129
129
|
/** Selects query latest params. */
|
|
130
|
-
selectQueryParams: <
|
|
130
|
+
selectQueryParams: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => (QK extends keyof QP & keyof QR ? QP[QK] : never) | undefined;
|
|
131
131
|
/** Selects query latest expiresAt. */
|
|
132
|
-
selectQueryExpiresAt: <
|
|
132
|
+
selectQueryExpiresAt: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => number | undefined;
|
|
133
133
|
/** Selects mutation state. */
|
|
134
|
-
selectMutationState: <
|
|
134
|
+
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>;
|
|
135
135
|
/** Selects mutation latest result. */
|
|
136
|
-
selectMutationResult: <
|
|
136
|
+
selectMutationResult: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => (MK extends keyof MP & keyof MR ? MR[MK] : never) | undefined;
|
|
137
137
|
/** Selects mutation loading state. */
|
|
138
|
-
selectMutationLoading: <
|
|
138
|
+
selectMutationLoading: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => boolean;
|
|
139
139
|
/** Selects mutation latest error. */
|
|
140
|
-
selectMutationError: <
|
|
140
|
+
selectMutationError: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => Error | undefined;
|
|
141
141
|
/** Selects mutation latest params. */
|
|
142
|
-
selectMutationParams: <
|
|
142
|
+
selectMutationParams: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => (MK extends keyof MP & keyof MR ? MP[MK] : never) | undefined;
|
|
143
143
|
/** Selects entity by id and typename. */
|
|
144
144
|
selectEntityById: <TN extends keyof T>(state: unknown, id: Key | null | undefined, typename: TN) => T[TN] | undefined;
|
|
145
145
|
/** Selects all entities. */
|
|
146
146
|
selectEntities: (state: unknown) => import("./types").EntitiesMap<T>;
|
|
147
147
|
/** Selects all entities of provided typename. */
|
|
148
|
-
selectEntitiesByTypename: <
|
|
148
|
+
selectEntitiesByTypename: <TN extends keyof T>(state: unknown, typename: TN) => import("./types").EntitiesMap<T>[TN];
|
|
149
149
|
};
|
|
150
150
|
hooks: {
|
|
151
151
|
/** Returns client object with query and mutate functions. */
|
|
152
152
|
useClient: () => {
|
|
153
|
-
query: <
|
|
154
|
-
mutate: <
|
|
153
|
+
query: <QK extends keyof (QP & QR)>(options: QueryOptions<T, QP, QR, QK>) => Promise<QueryResult<QK extends keyof QP & keyof QR ? QR[QK] : never>>;
|
|
154
|
+
mutate: <MK extends keyof (MP & MR)>(options: MutateOptions<T, MP, MR, MK>) => Promise<MutationResult<MK extends keyof MP & keyof MR ? MR[MK] : never>>;
|
|
155
155
|
};
|
|
156
156
|
/** Fetches query when params change and subscribes to query state changes (except `expiresAt` field). */
|
|
157
|
-
useQuery: <
|
|
157
|
+
useQuery: <QK extends keyof (QP & QR)>(options: Parameters<typeof useQuery<N, T, QP, QR, MP, MR, QK>>[2]) => 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<T, QP, QR, QK>, "params" | "onlyIfExpired">> | undefined) => Promise<QueryResult<QK extends infer T_1 ? T_1 extends QK ? T_1 extends keyof QP & keyof QR ? QR[T_1] : never : never : never>>];
|
|
158
158
|
/** Subscribes to provided mutation state and provides mutate function. */
|
|
159
|
-
useMutation: <
|
|
159
|
+
useMutation: <MK extends keyof (MP & MR)>(options: Parameters<typeof useMutation<N, T, MP, MR, MK>>[2]) => 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<MK extends keyof MP & keyof MR ? MP[MK] : never, MK extends keyof MP & keyof MR ? MP[MK] : never>, () => boolean];
|
|
160
160
|
/** useSelector + selectEntityById. */
|
|
161
|
-
useSelectEntityById: <
|
|
161
|
+
useSelectEntityById: <TN extends keyof T>(id: Key | null | undefined, typename: TN) => T[TN] | undefined;
|
|
162
162
|
};
|
|
163
163
|
utils: {
|
|
164
164
|
/**
|
|
165
165
|
* Apply changes to the entities map.
|
|
166
166
|
* @return `undefined` if nothing to change, otherwise new entities map with applied changes.
|
|
167
167
|
*/
|
|
168
|
-
applyEntityChanges: (entities:
|
|
168
|
+
applyEntityChanges: (entities: Parameters<typeof applyEntityChanges<T>>[0], changes: Parameters<typeof applyEntityChanges<T>>[1]) => import("./types").EntitiesMap<T> | undefined;
|
|
169
169
|
};
|
|
170
170
|
};
|
|
171
171
|
};
|
|
@@ -178,8 +178,8 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCach
|
|
|
178
178
|
/** Reducer of the cache, should be added to redux store. */
|
|
179
179
|
reducer: (state: {
|
|
180
180
|
entities: import("./types").EntitiesMap<Typenames>;
|
|
181
|
-
queries:
|
|
182
|
-
mutations:
|
|
181
|
+
queries: { [QK in keyof (QP | QR)]: import("./types").Dict<QueryState<QP[QK], QR[QK]> | undefined>; };
|
|
182
|
+
mutations: { [MK in keyof (MP | MR)]: MutationState<MP[MK], MR[MK]>; };
|
|
183
183
|
} | undefined, action: {
|
|
184
184
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
185
185
|
queryKey: keyof QP & keyof QR;
|
|
@@ -198,22 +198,22 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCach
|
|
|
198
198
|
type: `@rrc/${N}/invalidateQuery`;
|
|
199
199
|
queries: {
|
|
200
200
|
query: keyof QP & keyof QR;
|
|
201
|
-
cacheKey?: Key
|
|
202
|
-
expiresAt?: number
|
|
201
|
+
cacheKey?: Key;
|
|
202
|
+
expiresAt?: number;
|
|
203
203
|
}[];
|
|
204
204
|
} | {
|
|
205
205
|
type: `@rrc/${N}/clearQueryState`;
|
|
206
206
|
queries: {
|
|
207
207
|
query: keyof QP & keyof QR;
|
|
208
|
-
cacheKey?: Key
|
|
208
|
+
cacheKey?: Key;
|
|
209
209
|
}[];
|
|
210
210
|
} | {
|
|
211
211
|
type: `@rrc/${N}/clearMutationState`;
|
|
212
212
|
mutationKeys: (keyof MP & keyof MR)[];
|
|
213
213
|
}) => {
|
|
214
214
|
entities: import("./types").EntitiesMap<Typenames>;
|
|
215
|
-
queries:
|
|
216
|
-
mutations:
|
|
215
|
+
queries: { [QK in keyof (QP | QR)]: import("./types").Dict<QueryState<QP[QK], QR[QK]> | undefined>; };
|
|
216
|
+
mutations: { [MK in keyof (MP | MR)]: MutationState<MP[MK], MR[MK]>; };
|
|
217
217
|
};
|
|
218
218
|
actions: {
|
|
219
219
|
updateQueryStateAndEntities: {
|
|
@@ -227,10 +227,10 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCach
|
|
|
227
227
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
228
228
|
};
|
|
229
229
|
updateMutationStateAndEntities: {
|
|
230
|
-
<
|
|
230
|
+
<K extends keyof MP & keyof MR>(mutationKey: K, state?: Partial<MutationState<MP[K], MR[K]>> | undefined, entityChanges?: import("./types").EntityChanges<Typenames> | undefined): {
|
|
231
231
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
232
|
-
mutationKey:
|
|
233
|
-
state: Partial<MutationState<MP[
|
|
232
|
+
mutationKey: K;
|
|
233
|
+
state: Partial<MutationState<MP[K], MR[K]>> | undefined;
|
|
234
234
|
entityChanges: import("./types").EntityChanges<Typenames> | undefined;
|
|
235
235
|
};
|
|
236
236
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
@@ -243,83 +243,83 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(partialCach
|
|
|
243
243
|
type: `@rrc/${N}/mergeEntityChanges`;
|
|
244
244
|
};
|
|
245
245
|
invalidateQuery: {
|
|
246
|
-
<
|
|
247
|
-
query:
|
|
248
|
-
cacheKey?: Key
|
|
249
|
-
expiresAt?: number
|
|
246
|
+
<K extends keyof QP & keyof QR>(queries: {
|
|
247
|
+
query: K;
|
|
248
|
+
cacheKey?: Key;
|
|
249
|
+
expiresAt?: number;
|
|
250
250
|
}[]): {
|
|
251
251
|
type: `@rrc/${N}/invalidateQuery`;
|
|
252
252
|
queries: {
|
|
253
|
-
query:
|
|
254
|
-
cacheKey?: Key
|
|
255
|
-
expiresAt?: number
|
|
253
|
+
query: K;
|
|
254
|
+
cacheKey?: Key;
|
|
255
|
+
expiresAt?: number;
|
|
256
256
|
}[];
|
|
257
257
|
};
|
|
258
258
|
type: `@rrc/${N}/invalidateQuery`;
|
|
259
259
|
};
|
|
260
260
|
clearQueryState: {
|
|
261
|
-
<
|
|
262
|
-
query:
|
|
263
|
-
cacheKey?: Key
|
|
261
|
+
<K extends keyof QP & keyof QR>(queries: {
|
|
262
|
+
query: K;
|
|
263
|
+
cacheKey?: Key;
|
|
264
264
|
}[]): {
|
|
265
265
|
type: `@rrc/${N}/clearQueryState`;
|
|
266
266
|
queries: {
|
|
267
|
-
query:
|
|
268
|
-
cacheKey?: Key
|
|
267
|
+
query: K;
|
|
268
|
+
cacheKey?: Key;
|
|
269
269
|
}[];
|
|
270
270
|
};
|
|
271
271
|
type: `@rrc/${N}/clearQueryState`;
|
|
272
272
|
};
|
|
273
273
|
clearMutationState: {
|
|
274
|
-
<
|
|
274
|
+
<K extends keyof MP & keyof MR>(mutationKeys: K[]): {
|
|
275
275
|
type: `@rrc/${N}/clearMutationState`;
|
|
276
|
-
mutationKeys:
|
|
276
|
+
mutationKeys: K[];
|
|
277
277
|
};
|
|
278
278
|
type: `@rrc/${N}/clearMutationState`;
|
|
279
279
|
};
|
|
280
280
|
};
|
|
281
281
|
selectors: {
|
|
282
282
|
/** Selects query state. */
|
|
283
|
-
selectQueryState: <
|
|
283
|
+
selectQueryState: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => QueryState<QK extends keyof QP & keyof QR ? QP[QK] : never, QK extends keyof QP & keyof QR ? QR[QK] : never>;
|
|
284
284
|
/** Selects query latest result. */
|
|
285
|
-
selectQueryResult: <
|
|
285
|
+
selectQueryResult: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => (QK extends keyof QP & keyof QR ? QR[QK] : never) | undefined;
|
|
286
286
|
/** Selects query loading state. */
|
|
287
|
-
selectQueryLoading: <
|
|
287
|
+
selectQueryLoading: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => boolean;
|
|
288
288
|
/** Selects query latest error. */
|
|
289
|
-
selectQueryError: <
|
|
289
|
+
selectQueryError: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => Error | undefined;
|
|
290
290
|
/** Selects query latest params. */
|
|
291
|
-
selectQueryParams: <
|
|
291
|
+
selectQueryParams: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => (QK extends keyof QP & keyof QR ? QP[QK] : never) | undefined;
|
|
292
292
|
/** Selects query latest expiresAt. */
|
|
293
|
-
selectQueryExpiresAt: <
|
|
293
|
+
selectQueryExpiresAt: <QK extends keyof QP | keyof QR>(state: unknown, query: QK, cacheKey: Key) => number | undefined;
|
|
294
294
|
/** Selects mutation state. */
|
|
295
|
-
selectMutationState: <
|
|
295
|
+
selectMutationState: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => MutationState<MK extends keyof MP & keyof MR ? MP[MK] : never, MK extends keyof MP & keyof MR ? MR[MK] : never>;
|
|
296
296
|
/** Selects mutation latest result. */
|
|
297
|
-
selectMutationResult: <
|
|
297
|
+
selectMutationResult: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => (MK extends keyof MP & keyof MR ? MR[MK] : never) | undefined;
|
|
298
298
|
/** Selects mutation loading state. */
|
|
299
|
-
selectMutationLoading: <
|
|
299
|
+
selectMutationLoading: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => boolean;
|
|
300
300
|
/** Selects mutation latest error. */
|
|
301
|
-
selectMutationError: <
|
|
301
|
+
selectMutationError: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => Error | undefined;
|
|
302
302
|
/** Selects mutation latest params. */
|
|
303
|
-
selectMutationParams: <
|
|
303
|
+
selectMutationParams: <MK extends keyof MP | keyof MR>(state: unknown, mutation: MK) => (MK extends keyof MP & keyof MR ? MP[MK] : never) | undefined;
|
|
304
304
|
/** Selects entity by id and typename. */
|
|
305
305
|
selectEntityById: <TN extends string>(state: unknown, id: Key | null | undefined, typename: TN) => object | undefined;
|
|
306
306
|
/** Selects all entities. */
|
|
307
307
|
selectEntities: (state: unknown) => import("./types").EntitiesMap<Typenames>;
|
|
308
308
|
/** Selects all entities of provided typename. */
|
|
309
|
-
selectEntitiesByTypename: <
|
|
309
|
+
selectEntitiesByTypename: <TN extends string>(state: unknown, typename: TN) => import("./types").Dict<object> | undefined;
|
|
310
310
|
};
|
|
311
311
|
hooks: {
|
|
312
312
|
/** Returns client object with query and mutate functions. */
|
|
313
313
|
useClient: () => {
|
|
314
|
-
query: <
|
|
315
|
-
mutate: <
|
|
314
|
+
query: <QK extends keyof QP | keyof QR>(options: QueryOptions<Typenames, QP, QR, QK>) => Promise<QueryResult<QK extends keyof QP & keyof QR ? QR[QK] : never>>;
|
|
315
|
+
mutate: <MK extends keyof MP | keyof MR>(options: MutateOptions<Typenames, MP, MR, MK>) => Promise<MutationResult<MK extends keyof MP & keyof MR ? MR[MK] : never>>;
|
|
316
316
|
};
|
|
317
317
|
/** Fetches query when params change and subscribes to query state changes (except `expiresAt` field). */
|
|
318
|
-
useQuery: <
|
|
318
|
+
useQuery: <QK extends keyof QP | keyof QR>(options: import("./types").UseQueryOptions<Typenames, QP, QR, QK>) => 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<Typenames, QP, QR, QK>, "params" | "onlyIfExpired">> | undefined) => Promise<QueryResult<QK extends infer T ? T extends QK ? T extends keyof QP & keyof QR ? QR[T] : never : never : never>>];
|
|
319
319
|
/** Subscribes to provided mutation state and provides mutate function. */
|
|
320
|
-
useMutation: <
|
|
320
|
+
useMutation: <MK extends keyof MP | keyof MR>(options: Omit<MutateOptions<Typenames, MP, MR, MK>, "params">) => readonly [(params: MK extends keyof MP & keyof MR ? MP[MK] : never) => Promise<MutationResult<MK extends infer T ? T extends MK ? T extends keyof MP & keyof MR ? MR[T] : never : never : never>>, MutationState<MK extends keyof MP & keyof MR ? MP[MK] : never, MK extends keyof MP & keyof MR ? MP[MK] : never>, () => boolean];
|
|
321
321
|
/** useSelector + selectEntityById. */
|
|
322
|
-
useSelectEntityById: <
|
|
322
|
+
useSelectEntityById: <TN extends string>(id: Key | null | undefined, typename: TN) => object | undefined;
|
|
323
323
|
};
|
|
324
324
|
utils: {
|
|
325
325
|
/**
|
package/dist/createCache.js
CHANGED
|
@@ -53,12 +53,12 @@ const withTypenames = () => {
|
|
|
53
53
|
const selectQueryState = (state, query, cacheKey) => {
|
|
54
54
|
var _a;
|
|
55
55
|
// @ts-expect-error fix later
|
|
56
|
-
return (_a = cache.cacheStateSelector(state).queries[query][cacheKey]) !== null && _a !== void 0 ? _a : utilsAndConstants_1.
|
|
56
|
+
return (_a = cache.cacheStateSelector(state).queries[query][cacheKey]) !== null && _a !== void 0 ? _a : utilsAndConstants_1.EMPTY_OBJECT;
|
|
57
57
|
};
|
|
58
58
|
const selectMutationState = (state, mutation) => {
|
|
59
59
|
var _a;
|
|
60
60
|
// @ts-expect-error fix later
|
|
61
|
-
return (_a = cache.cacheStateSelector(state).mutations[mutation]) !== null && _a !== void 0 ? _a : utilsAndConstants_1.
|
|
61
|
+
return (_a = cache.cacheStateSelector(state).mutations[mutation]) !== null && _a !== void 0 ? _a : utilsAndConstants_1.EMPTY_OBJECT;
|
|
62
62
|
};
|
|
63
63
|
const actions = (0, createActions_1.createActions)(cache.name);
|
|
64
64
|
return {
|
|
@@ -76,7 +76,8 @@ const withTypenames = () => {
|
|
|
76
76
|
},
|
|
77
77
|
/** Selects query loading state. */
|
|
78
78
|
selectQueryLoading: (state, query, cacheKey) => {
|
|
79
|
-
|
|
79
|
+
var _a;
|
|
80
|
+
return (_a = selectQueryState(state, query, cacheKey).loading) !== null && _a !== void 0 ? _a : false;
|
|
80
81
|
},
|
|
81
82
|
/** Selects query latest error. */
|
|
82
83
|
selectQueryError: (state, query, cacheKey) => {
|
|
@@ -98,7 +99,8 @@ const withTypenames = () => {
|
|
|
98
99
|
},
|
|
99
100
|
/** Selects mutation loading state. */
|
|
100
101
|
selectMutationLoading: (state, mutation) => {
|
|
101
|
-
|
|
102
|
+
var _a;
|
|
103
|
+
return (_a = selectMutationState(state, mutation).loading) !== null && _a !== void 0 ? _a : false;
|
|
102
104
|
},
|
|
103
105
|
/** Selects mutation latest error. */
|
|
104
106
|
selectMutationError: (state, mutation) => {
|
|
@@ -1,42 +1,12 @@
|
|
|
1
1
|
import type { ActionMap } from './createActions';
|
|
2
2
|
import type { CacheOptions, Dict, EntitiesMap, MutationState, QueryState, Typenames } from './types';
|
|
3
3
|
export type ReduxCacheState<T extends Typenames, QP, QR, MP, MR> = ReturnType<ReturnType<typeof createCacheReducer<string, T, QP, QR, MP, MR>>>;
|
|
4
|
-
export declare const createCacheReducer: <N extends string, T extends Typenames, QP, QR, MP, MR>(actions: ActionMap<N, T, QP, QR, MP, MR>, queryKeys: (keyof QP
|
|
4
|
+
export declare const createCacheReducer: <N extends string, T extends Typenames, QP, QR, MP, MR>(actions: ActionMap<N, T, QP, QR, MP, MR>, queryKeys: (keyof (QP | QR))[], cacheOptions: CacheOptions) => (state: {
|
|
5
5
|
entities: EntitiesMap<T>;
|
|
6
|
-
queries:
|
|
7
|
-
mutations:
|
|
8
|
-
} | undefined, action: {
|
|
9
|
-
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
10
|
-
queryKey: keyof QP & keyof QR;
|
|
11
|
-
queryCacheKey: import("./types").Key;
|
|
12
|
-
state: Partial<QueryState<QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>> | undefined;
|
|
13
|
-
entityChanges: import("./types").EntityChanges<T> | undefined;
|
|
14
|
-
} | {
|
|
15
|
-
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
16
|
-
mutationKey: keyof MP & keyof MR;
|
|
17
|
-
state: Partial<MutationState<MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>> | undefined;
|
|
18
|
-
entityChanges: import("./types").EntityChanges<T> | undefined;
|
|
19
|
-
} | {
|
|
20
|
-
type: `@rrc/${N}/mergeEntityChanges`;
|
|
21
|
-
changes: import("./types").EntityChanges<T>;
|
|
22
|
-
} | {
|
|
23
|
-
type: `@rrc/${N}/invalidateQuery`;
|
|
24
|
-
queries: {
|
|
25
|
-
query: keyof QP & keyof QR;
|
|
26
|
-
cacheKey?: import("./types").Key | undefined;
|
|
27
|
-
expiresAt?: number | undefined;
|
|
28
|
-
}[];
|
|
29
|
-
} | {
|
|
30
|
-
type: `@rrc/${N}/clearQueryState`;
|
|
31
|
-
queries: {
|
|
32
|
-
query: keyof QP & keyof QR;
|
|
33
|
-
cacheKey?: import("./types").Key | undefined;
|
|
34
|
-
}[];
|
|
35
|
-
} | {
|
|
36
|
-
type: `@rrc/${N}/clearMutationState`;
|
|
37
|
-
mutationKeys: (keyof MP & keyof MR)[];
|
|
38
|
-
}) => {
|
|
6
|
+
queries: { [QK in keyof (QP | QR)]: Dict<QueryState<QP[QK], QR[QK]> | undefined>; };
|
|
7
|
+
mutations: { [MK in keyof (MP | MR)]: MutationState<MP[MK], MR[MK]>; };
|
|
8
|
+
} | undefined, action: ReturnType<(typeof actions)[keyof typeof actions]>) => {
|
|
39
9
|
entities: EntitiesMap<T>;
|
|
40
|
-
queries:
|
|
41
|
-
mutations:
|
|
10
|
+
queries: { [QK in keyof (QP | QR)]: Dict<QueryState<QP[QK], QR[QK]> | undefined>; };
|
|
11
|
+
mutations: { [MK in keyof (MP | MR)]: MutationState<MP[MK], MR[MK]>; };
|
|
42
12
|
};
|
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
+
var t = {};
|
|
4
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
+
t[p] = s[p];
|
|
6
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
+
t[p[i]] = s[p[i]];
|
|
10
|
+
}
|
|
11
|
+
return t;
|
|
12
|
+
};
|
|
2
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
14
|
exports.createCacheReducer = void 0;
|
|
4
15
|
const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
@@ -23,22 +34,25 @@ const createCacheReducer = (actions, queryKeys, cacheOptions) => {
|
|
|
23
34
|
});
|
|
24
35
|
const deepEqual = cacheOptions.deepComparisonEnabled ? utilsAndConstants_1.optionalUtils.deepEqual : undefined;
|
|
25
36
|
return (state = initialState, action) => {
|
|
26
|
-
var _a, _b;
|
|
27
37
|
switch (action.type) {
|
|
28
38
|
case actions.updateQueryStateAndEntities.type: {
|
|
29
39
|
const { queryKey, queryCacheKey, state: queryState, entityChanges, } = action;
|
|
30
|
-
const oldQueryState =
|
|
40
|
+
const oldQueryState = state.queries[queryKey][queryCacheKey];
|
|
31
41
|
let newQueryState = queryState && Object.assign(Object.assign({}, oldQueryState), queryState);
|
|
32
|
-
// remove undefined optional fields
|
|
33
42
|
if (newQueryState) {
|
|
43
|
+
// remove undefined optional fields
|
|
34
44
|
for (const key of optionalQueryKeys) {
|
|
35
45
|
if (key in newQueryState && newQueryState[key] === undefined) {
|
|
36
46
|
delete newQueryState[key];
|
|
37
47
|
}
|
|
38
48
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
49
|
+
if ('loading' in newQueryState && !newQueryState.loading) {
|
|
50
|
+
delete newQueryState.loading;
|
|
51
|
+
}
|
|
52
|
+
// skip if new state deep equals to the old state
|
|
53
|
+
if (deepEqual === null || deepEqual === void 0 ? void 0 : deepEqual(oldQueryState !== null && oldQueryState !== void 0 ? oldQueryState : utilsAndConstants_1.EMPTY_OBJECT, newQueryState)) {
|
|
54
|
+
newQueryState = undefined;
|
|
55
|
+
}
|
|
42
56
|
}
|
|
43
57
|
const newEntities = entityChanges && (0, utilsAndConstants_1.applyEntityChanges)(state.entities, entityChanges, cacheOptions);
|
|
44
58
|
let newState;
|
|
@@ -47,25 +61,37 @@ const createCacheReducer = (actions, queryKeys, cacheOptions) => {
|
|
|
47
61
|
newState.entities = newEntities;
|
|
48
62
|
}
|
|
49
63
|
if (newQueryState) {
|
|
50
|
-
|
|
51
|
-
|
|
64
|
+
if (!(0, utilsAndConstants_1.isEmptyObject)(newQueryState)) {
|
|
65
|
+
newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state));
|
|
66
|
+
newState.queries = Object.assign(Object.assign({}, state.queries), { [queryKey]: Object.assign(Object.assign({}, state.queries[queryKey]), { [queryCacheKey]: newQueryState }) });
|
|
67
|
+
}
|
|
68
|
+
else if (oldQueryState !== undefined) {
|
|
69
|
+
// empty states are removed
|
|
70
|
+
const _a = state.queries[queryKey], _b = queryCacheKey, _ = _a[_b], withoutCacheKey = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]);
|
|
71
|
+
newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state));
|
|
72
|
+
newState.queries = Object.assign(Object.assign({}, state.queries), { [queryKey]: withoutCacheKey });
|
|
73
|
+
}
|
|
52
74
|
}
|
|
53
75
|
return newState !== null && newState !== void 0 ? newState : state;
|
|
54
76
|
}
|
|
55
77
|
case actions.updateMutationStateAndEntities.type: {
|
|
56
78
|
const { mutationKey, state: mutationState, entityChanges, } = action;
|
|
57
|
-
const oldMutationState =
|
|
79
|
+
const oldMutationState = state.mutations[mutationKey];
|
|
58
80
|
let newMutationState = mutationState && Object.assign(Object.assign({}, oldMutationState), mutationState);
|
|
59
|
-
// remove undefined optional fields
|
|
60
81
|
if (newMutationState) {
|
|
82
|
+
// remove optional fields with default values
|
|
61
83
|
for (const key of optionalMutationKeys) {
|
|
62
84
|
if (key in newMutationState && newMutationState[key] === undefined) {
|
|
63
85
|
delete newMutationState[key];
|
|
64
86
|
}
|
|
65
87
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
88
|
+
if ('loading' in newMutationState && !newMutationState.loading) {
|
|
89
|
+
delete newMutationState.loading;
|
|
90
|
+
}
|
|
91
|
+
// skip if new state deep equals to the old state
|
|
92
|
+
if (deepEqual === null || deepEqual === void 0 ? void 0 : deepEqual(oldMutationState !== null && oldMutationState !== void 0 ? oldMutationState : utilsAndConstants_1.EMPTY_OBJECT, newMutationState)) {
|
|
93
|
+
newMutationState = undefined;
|
|
94
|
+
}
|
|
69
95
|
}
|
|
70
96
|
const newEntities = entityChanges && (0, utilsAndConstants_1.applyEntityChanges)(state.entities, entityChanges, cacheOptions);
|
|
71
97
|
let newState;
|
|
@@ -74,8 +100,16 @@ const createCacheReducer = (actions, queryKeys, cacheOptions) => {
|
|
|
74
100
|
newState.entities = newEntities;
|
|
75
101
|
}
|
|
76
102
|
if (newMutationState) {
|
|
77
|
-
|
|
78
|
-
|
|
103
|
+
if (!(0, utilsAndConstants_1.isEmptyObject)(newMutationState)) {
|
|
104
|
+
newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state));
|
|
105
|
+
newState.mutations = Object.assign(Object.assign({}, state.mutations), { [mutationKey]: newMutationState });
|
|
106
|
+
}
|
|
107
|
+
else if (oldMutationState !== undefined) {
|
|
108
|
+
// empty states are removed
|
|
109
|
+
const _c = state.mutations, _d = mutationKey, _ = _c[_d], withoutMutationKey = __rest(_c, [typeof _d === "symbol" ? _d : _d + ""]);
|
|
110
|
+
newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state));
|
|
111
|
+
newState.mutations = withoutMutationKey;
|
|
112
|
+
}
|
|
79
113
|
}
|
|
80
114
|
return newState !== null && newState !== void 0 ? newState : state;
|
|
81
115
|
}
|
|
@@ -86,7 +120,7 @@ const createCacheReducer = (actions, queryKeys, cacheOptions) => {
|
|
|
86
120
|
}
|
|
87
121
|
case actions.invalidateQuery.type: {
|
|
88
122
|
const { queries: queriesToInvalidate } = action;
|
|
89
|
-
if (
|
|
123
|
+
if (queriesToInvalidate.length === 0) {
|
|
90
124
|
return state;
|
|
91
125
|
}
|
|
92
126
|
const now = Date.now();
|
|
@@ -125,13 +159,13 @@ const createCacheReducer = (actions, queryKeys, cacheOptions) => {
|
|
|
125
159
|
}
|
|
126
160
|
}
|
|
127
161
|
}
|
|
128
|
-
return
|
|
162
|
+
return newQueries === undefined
|
|
129
163
|
? state
|
|
130
164
|
: Object.assign(Object.assign({}, state), { queries: newQueries });
|
|
131
165
|
}
|
|
132
166
|
case actions.clearQueryState.type: {
|
|
133
167
|
const { queries: queriesToClear } = action;
|
|
134
|
-
if (
|
|
168
|
+
if (queriesToClear.length === 0) {
|
|
135
169
|
return state;
|
|
136
170
|
}
|
|
137
171
|
let newQueries = undefined;
|
|
@@ -151,13 +185,13 @@ const createCacheReducer = (actions, queryKeys, cacheOptions) => {
|
|
|
151
185
|
newQueries[queryKey] = utilsAndConstants_1.EMPTY_OBJECT;
|
|
152
186
|
}
|
|
153
187
|
}
|
|
154
|
-
return
|
|
188
|
+
return newQueries === undefined
|
|
155
189
|
? state
|
|
156
190
|
: Object.assign(Object.assign({}, state), { queries: newQueries });
|
|
157
191
|
}
|
|
158
192
|
case actions.clearMutationState.type: {
|
|
159
193
|
const { mutationKeys } = action;
|
|
160
|
-
if (
|
|
194
|
+
if (mutationKeys.length === 0) {
|
|
161
195
|
return state;
|
|
162
196
|
}
|
|
163
197
|
let newMutations = undefined;
|
|
@@ -167,7 +201,7 @@ const createCacheReducer = (actions, queryKeys, cacheOptions) => {
|
|
|
167
201
|
delete newMutations[mutation];
|
|
168
202
|
}
|
|
169
203
|
}
|
|
170
|
-
return
|
|
204
|
+
return newMutations === undefined
|
|
171
205
|
? state
|
|
172
206
|
: Object.assign(Object.assign({}, state), { mutations: newMutations });
|
|
173
207
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { createCache, withTypenames } from './createCache';
|
|
2
2
|
export type { ReduxCacheState } from './createCacheReducer';
|
|
3
3
|
export * from './types';
|
|
4
|
-
export { defaultGetCacheKey
|
|
4
|
+
export { defaultGetCacheKey } from './utilsAndConstants';
|
package/dist/index.js
CHANGED
|
@@ -14,21 +14,18 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
17
|
+
exports.defaultGetCacheKey = exports.withTypenames = exports.createCache = void 0;
|
|
18
18
|
var createCache_1 = require("./createCache");
|
|
19
19
|
Object.defineProperty(exports, "createCache", { enumerable: true, get: function () { return createCache_1.createCache; } });
|
|
20
20
|
Object.defineProperty(exports, "withTypenames", { enumerable: true, get: function () { return createCache_1.withTypenames; } });
|
|
21
21
|
__exportStar(require("./types"), exports);
|
|
22
22
|
var utilsAndConstants_1 = require("./utilsAndConstants");
|
|
23
23
|
Object.defineProperty(exports, "defaultGetCacheKey", { enumerable: true, get: function () { return utilsAndConstants_1.defaultGetCacheKey; } });
|
|
24
|
-
Object.defineProperty(exports, "defaultQueryMutationState", { enumerable: true, get: function () { return utilsAndConstants_1.DEFAULT_QUERY_MUTATION_STATE; } });
|
|
25
24
|
// Backlog
|
|
26
25
|
// ! high (1.0.0)
|
|
27
|
-
// remove undefined optional fields & emtpy states. remove mutation state when it finished without errors
|
|
28
26
|
// remove cachePolicy? make skip/enabled a function? skip -> enabled/shouldFetch?
|
|
29
27
|
// generate full api docs
|
|
30
28
|
// ! medium
|
|
31
|
-
// example rca -> vite
|
|
32
29
|
// optimistic response
|
|
33
30
|
// reset [whole] cache to initial / to provided state
|
|
34
31
|
// globals for success, completions and loading states?
|
package/dist/mutate.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Store } from 'redux';
|
|
2
2
|
import type { ActionMap } from './createActions';
|
|
3
3
|
import type { Cache, Key, MutationResult, Typenames } from './types';
|
|
4
|
-
export declare const mutate: <N extends string, T extends Typenames, QP, QR, MP, MR, MK extends keyof MP
|
|
4
|
+
export declare const mutate: <N extends string, T extends Typenames, QP, QR, MP, MR, MK extends keyof (MP & MR)>(logTag: string, store: Store, cache: Cache<N, T, QP, QR, MP, MR>, { updateMutationStateAndEntities, }: Pick<ActionMap<N, T, unknown, unknown, MP, MR>, "updateMutationStateAndEntities">, mutationKey: MK, params: MK extends keyof (MP | MR) ? MP[MK] : never, abortControllers: WeakMap<Store, Record<Key, AbortController>>, onCompleted?: ((response: import("./types").NormalizedQueryResponse<T, MR[keyof MP & keyof MR & string]> | undefined, error: unknown | undefined, params: MP[keyof MP & keyof MR & string] | undefined, store: Store) => void) | ((response: import("./types").NormalizedQueryResponse<T, MR[keyof MP & keyof MR & number]> | undefined, error: unknown | undefined, params: MP[keyof MP & keyof MR & number] | undefined, store: Store) => void) | ((response: import("./types").NormalizedQueryResponse<T, MR[keyof MP & keyof MR & symbol]> | undefined, error: unknown | undefined, params: MP[keyof MP & keyof MR & symbol] | undefined, store: Store) => void) | undefined, onSuccess?: ((response: import("./types").NormalizedQueryResponse<T, MR[keyof MP & keyof MR & string]>, params: MP[keyof MP & keyof MR & string] | undefined, store: Store) => void) | ((response: import("./types").NormalizedQueryResponse<T, MR[keyof MP & keyof MR & number]>, params: MP[keyof MP & keyof MR & number] | undefined, store: Store) => void) | ((response: import("./types").NormalizedQueryResponse<T, MR[keyof MP & keyof MR & symbol]>, params: MP[keyof MP & keyof MR & symbol] | undefined, store: Store) => void) | undefined, onError?: ((error: unknown, params: MP[keyof MP & keyof MR & string] | undefined, store: Store) => boolean | void | null | undefined) | ((error: unknown, params: MP[keyof MP & keyof MR & number] | undefined, store: Store) => boolean | void | null | undefined) | ((error: unknown, params: MP[keyof MP & keyof MR & symbol] | undefined, store: Store) => boolean | void | null | undefined) | undefined) => Promise<MutationResult<MK extends keyof (MP | MR) ? MR[MK] : never>>;
|
package/dist/mutate.js
CHANGED
|
@@ -11,8 +11,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.mutate = void 0;
|
|
13
13
|
const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
14
|
-
const mutate = (logTag, store, cache, { updateMutationStateAndEntities, }, mutationKey, params, abortControllers, onCompleted = cache.mutations[mutationKey].onCompleted, onSuccess = cache.mutations[mutationKey].onSuccess, onError = cache.mutations[mutationKey].onError)
|
|
15
|
-
var
|
|
14
|
+
const mutate = (logTag_1, store_1, cache_1, _a, mutationKey_1, params_1, abortControllers_1, ...args_1) => __awaiter(void 0, [logTag_1, store_1, cache_1, _a, mutationKey_1, params_1, abortControllers_1, ...args_1], void 0, function* (logTag, store, cache, { updateMutationStateAndEntities, }, mutationKey, params, abortControllers, onCompleted = cache.mutations[mutationKey].onCompleted, onSuccess = cache.mutations[mutationKey].onSuccess, onError = cache.mutations[mutationKey].onError) {
|
|
15
|
+
var _b, _c;
|
|
16
16
|
let abortControllersOfStore = abortControllers.get(store);
|
|
17
17
|
if (abortControllersOfStore === undefined) {
|
|
18
18
|
abortControllersOfStore = {};
|
|
@@ -68,7 +68,7 @@ const mutate = (logTag, store, cache, { updateMutationStateAndEntities, }, mutat
|
|
|
68
68
|
// @ts-expect-error params
|
|
69
69
|
if (!(onError === null || onError === void 0 ? void 0 : onError(error, params, store))) {
|
|
70
70
|
// @ts-expect-error queryKey
|
|
71
|
-
(
|
|
71
|
+
(_c = (_b = cache.globals).onError) === null || _c === void 0 ? void 0 : _c.call(_b, error, mutationKey, params, store);
|
|
72
72
|
}
|
|
73
73
|
// @ts-expect-error response
|
|
74
74
|
onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(response, error, params, store);
|
package/dist/query.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Store } from 'redux';
|
|
2
2
|
import type { ActionMap } from './createActions';
|
|
3
3
|
import type { Cache, Key, QueryResult, Typenames } from './types';
|
|
4
|
-
export declare const query: <N extends string, T extends Typenames, QP, QR, MP, MR, QK extends keyof QP
|
|
4
|
+
export declare const query: <N extends string, T extends Typenames, QP, QR, MP, MR, QK extends keyof (QP & QR)>(logTag: string, store: Store, cache: Cache<N, T, QP, QR, MP, MR>, { updateQueryStateAndEntities, }: Pick<ActionMap<N, T, QP, QR, unknown, unknown>, "updateQueryStateAndEntities">, queryKey: QK, cacheKey: Key, params: QK extends keyof (QP | QR) ? QP[QK] : never, secondsToLive: number | undefined, onlyIfExpired: boolean | undefined, mergeResults?: ((oldResult: QR[keyof QP & keyof QR & string] | undefined, response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & string]>, params: QP[keyof QP & keyof QR & string] | undefined, store: Store) => QR[keyof QP & keyof QR & string]) | ((oldResult: QR[keyof QP & keyof QR & number] | undefined, response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & number]>, params: QP[keyof QP & keyof QR & number] | undefined, store: Store) => QR[keyof QP & keyof QR & number]) | ((oldResult: QR[keyof QP & keyof QR & symbol] | undefined, response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & symbol]>, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store) => QR[keyof QP & keyof QR & symbol]) | undefined, onCompleted?: ((response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & string]> | undefined, error: unknown | undefined, params: QP[keyof QP & keyof QR & string] | undefined, store: Store) => void) | ((response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & number]> | undefined, error: unknown | undefined, params: QP[keyof QP & keyof QR & number] | undefined, store: Store) => void) | ((response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & symbol]> | undefined, error: unknown | undefined, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store) => void) | undefined, onSuccess?: ((response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & string]>, params: QP[keyof QP & keyof QR & string] | undefined, store: Store) => void) | ((response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & number]>, params: QP[keyof QP & keyof QR & number] | undefined, store: Store) => void) | ((response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & symbol]>, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store) => void) | undefined, onError?: ((error: unknown, params: QP[keyof QP & keyof QR & string] | undefined, store: Store) => boolean | void | null | undefined) | ((error: unknown, params: QP[keyof QP & keyof QR & number] | undefined, store: Store) => boolean | void | null | undefined) | ((error: unknown, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store) => boolean | void | null | undefined) | undefined) => Promise<QueryResult<QK extends keyof (QP | QR) ? QR[QK] : never>>;
|
package/dist/types.d.ts
CHANGED
|
@@ -76,15 +76,10 @@ export type EntitiesMap<T extends Typenames> = {
|
|
|
76
76
|
export type EntityIds<T extends Typenames> = {
|
|
77
77
|
[K in keyof T]?: Key[];
|
|
78
78
|
};
|
|
79
|
-
export type
|
|
80
|
-
|
|
81
|
-
params: P,
|
|
82
|
-
/** Redux store */
|
|
83
|
-
store: Store) => Promise<QueryResponse<T, R>>;
|
|
84
|
-
export type QueryInfo<T extends Typenames, P, R> = Partial<Pick<Globals<unknown, unknown>, 'cachePolicy' | 'secondsToLive'>> & {
|
|
85
|
-
query: Query<P, T, R>;
|
|
79
|
+
export type QueryInfo<T extends Typenames = Typenames, P = unknown, R = unknown> = Partial<Pick<Globals<unknown, unknown>, 'cachePolicy' | 'secondsToLive'>> & {
|
|
80
|
+
query: NormalizedQuery<T, P, R>;
|
|
86
81
|
/** Merges results before saving to the store. Default implementation is using the latest result. */
|
|
87
|
-
mergeResults?: (oldResult: R | undefined, response:
|
|
82
|
+
mergeResults?: (oldResult: R | undefined, response: NormalizedQueryResponse<T, R>, params: P | undefined, store: Store) => R;
|
|
88
83
|
/**
|
|
89
84
|
* Cache key is used for storing the query state and for performing a fetch when it changes. Queries with the same cache key share their state.
|
|
90
85
|
* Default implementation uses `JSON.stringify` or `String()` depending on type.
|
|
@@ -92,12 +87,18 @@ export type QueryInfo<T extends Typenames, P, R> = Partial<Pick<Globals<unknown,
|
|
|
92
87
|
* */
|
|
93
88
|
getCacheKey?: (params?: P) => Key;
|
|
94
89
|
/** Called after fetch finished either successfully or not. */
|
|
95
|
-
onCompleted?: (response:
|
|
90
|
+
onCompleted?: (response: NormalizedQueryResponse<T, R> | undefined, error: unknown | undefined, params: P | undefined, store: Store) => void;
|
|
96
91
|
/** Called after fetch finished without error. */
|
|
97
|
-
onSuccess?: (response:
|
|
92
|
+
onSuccess?: (response: NormalizedQueryResponse<T, R>, params: P | undefined, store: Store) => void;
|
|
98
93
|
/** Called after fetch finished with error. Should return true if error was handled and does not require global onError handling. */
|
|
99
94
|
onError?: (error: unknown, params: P | undefined, store: Store) => boolean | void | null | undefined;
|
|
100
95
|
};
|
|
96
|
+
export type Query<P = unknown, R = unknown> = (
|
|
97
|
+
/** Query parameters */
|
|
98
|
+
params: P,
|
|
99
|
+
/** Redux store */
|
|
100
|
+
store: Store) => Promise<QueryResponse<R>>;
|
|
101
|
+
export type NormalizedQuery<T extends Typenames = Typenames, P = unknown, R = unknown> = (...args: Parameters<Query<P, R>>) => Promise<NormalizedQueryResponse<T, R>>;
|
|
101
102
|
export type QueryState<P, R> = MutationState<P, R> & {
|
|
102
103
|
expiresAt?: number;
|
|
103
104
|
};
|
|
@@ -112,41 +113,44 @@ export type QueryOptions<T extends Typenames, QP, QR, QK extends keyof (QP & QR)
|
|
|
112
113
|
onlyIfExpired?: boolean;
|
|
113
114
|
};
|
|
114
115
|
export type QueryCachePolicy = 'cache-first' | 'cache-and-fetch';
|
|
115
|
-
export type QueryResponse<
|
|
116
|
+
export type QueryResponse<R = unknown> = {
|
|
116
117
|
result: R;
|
|
117
118
|
/** If defined, overrides this value for query state, ignoring `secondsToLive`. */
|
|
118
119
|
expiresAt?: number;
|
|
119
120
|
};
|
|
120
|
-
export type
|
|
121
|
+
export type NormalizedQueryResponse<T extends Typenames = Typenames, R = unknown> = EntityChanges<T> & QueryResponse<R>;
|
|
122
|
+
export type QueryResult<R = unknown> = {
|
|
121
123
|
error?: unknown;
|
|
122
124
|
cancelled?: true;
|
|
123
125
|
result?: R;
|
|
124
126
|
};
|
|
125
|
-
export type
|
|
127
|
+
export type MutationInfo<T extends Typenames = Typenames, P = unknown, R = unknown> = Pick<QueryInfo<T, P, R>, 'onCompleted' | 'onSuccess' | 'onError'> & {
|
|
128
|
+
mutation: NormalizedMutation<T, P, R>;
|
|
129
|
+
};
|
|
130
|
+
export type Mutation<P = unknown, R = unknown> = (
|
|
126
131
|
/** Mutation parameters */
|
|
127
132
|
params: P,
|
|
128
133
|
/** Redux store */
|
|
129
134
|
store: Store,
|
|
130
135
|
/** Signal is aborted for current mutation when the same mutation was called once again. */
|
|
131
|
-
abortSignal: AbortSignal) => Promise<MutationResponse<
|
|
132
|
-
export type
|
|
133
|
-
mutation: Mutation<P, T, R>;
|
|
134
|
-
};
|
|
136
|
+
abortSignal: AbortSignal) => Promise<MutationResponse<R>>;
|
|
137
|
+
export type NormalizedMutation<T extends Typenames = Typenames, P = unknown, R = unknown> = (...args: Parameters<Mutation<P, R>>) => Promise<NormalizedMutationResponse<T, R>>;
|
|
135
138
|
export type MutateOptions<T extends Typenames, MP, MR, MK extends keyof (MP & MR)> = Pick<MutationInfo<T, MK extends keyof (MP | MR) ? MP[MK] : never, MK extends keyof (MP | MR) ? MR[MK] : never>, 'onCompleted' | 'onSuccess' | 'onError'> & {
|
|
136
139
|
mutation: MK;
|
|
137
140
|
params: MK extends keyof (MP | MR) ? MP[MK] : never;
|
|
138
141
|
};
|
|
139
|
-
export type MutationResponse<
|
|
142
|
+
export type MutationResponse<R = unknown> = {
|
|
140
143
|
result?: R;
|
|
141
144
|
};
|
|
142
|
-
export type
|
|
145
|
+
export type NormalizedMutationResponse<T extends Typenames = Typenames, R = unknown> = EntityChanges<T> & MutationResponse<R>;
|
|
146
|
+
export type MutationResult<R = unknown> = {
|
|
143
147
|
error?: unknown;
|
|
144
148
|
aborted?: true;
|
|
145
149
|
result?: R;
|
|
146
150
|
};
|
|
147
151
|
export type MutationState<P, R> = {
|
|
148
|
-
/** `true` when
|
|
149
|
-
loading
|
|
152
|
+
/** `true` when fetch is currently in progress. */
|
|
153
|
+
loading?: boolean;
|
|
150
154
|
/** Result of the latest successfull response. */
|
|
151
155
|
result?: R;
|
|
152
156
|
/** Error of the latest response. */
|
package/dist/useMutation.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Store } from 'redux';
|
|
2
2
|
import { ActionMap } from './createActions';
|
|
3
3
|
import { Cache, Key, MutateOptions, MutationState, Typenames } from './types';
|
|
4
|
-
export declare const useMutation: <N extends string, T extends Typenames, MP, MR, MK extends keyof MP
|
|
4
|
+
export declare const useMutation: <N extends string, T extends Typenames, MP, MR, MK extends keyof (MP & MR)>(cache: Cache<N, T, unknown, unknown, MP, MR>, actions: Pick<ActionMap<N, T, unknown, unknown, MP, MR>, "updateMutationStateAndEntities">, options: Omit<MutateOptions<T, 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];
|
package/dist/useMutation.js
CHANGED
|
@@ -53,7 +53,7 @@ const useMutation = (cache, actions, options, abortControllers) => {
|
|
|
53
53
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
54
54
|
}, [mutationKey, store]);
|
|
55
55
|
// @ts-expect-error fix later
|
|
56
|
-
const mutationState = (_a = (0, react_redux_1.useSelector)(mutationStateSelector)) !== null && _a !== void 0 ? _a : utilsAndConstants_1.
|
|
56
|
+
const mutationState = (_a = (0, react_redux_1.useSelector)(mutationStateSelector)) !== null && _a !== void 0 ? _a : utilsAndConstants_1.EMPTY_OBJECT;
|
|
57
57
|
cache.options.logsEnabled &&
|
|
58
58
|
(0, utilsAndConstants_1.log)('useMutation', {
|
|
59
59
|
options,
|
package/dist/useQuery.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { ActionMap } from './createActions';
|
|
2
2
|
import { Cache, QueryOptions, QueryState, Typenames, UseQueryOptions } from './types';
|
|
3
|
-
export declare const useQuery: <N extends string, T extends Typenames, QP, QR, MP, MR, QK extends keyof QP
|
|
3
|
+
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: Pick<ActionMap<N, T, QP, QR, MP, MR>, "updateQueryStateAndEntities">, options: UseQueryOptions<T, QP, QR, QK>) => 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<T, QP, QR, QK>, "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>>];
|
package/dist/useQuery.js
CHANGED
|
@@ -39,7 +39,7 @@ const useQuery = (cache, actions, options) => {
|
|
|
39
39
|
const queryState = (_c = (0, react_redux_1.useSelector)((state) => {
|
|
40
40
|
const queryState = cacheStateSelector(state).queries[queryKey][cacheKey];
|
|
41
41
|
return queryState; // TODO proper type
|
|
42
|
-
}, (useQueryStateComparer))) !== null && _c !== void 0 ? _c : utilsAndConstants_1.
|
|
42
|
+
}, (useQueryStateComparer))) !== null && _c !== void 0 ? _c : utilsAndConstants_1.EMPTY_OBJECT;
|
|
43
43
|
(0, react_1.useEffect)(() => {
|
|
44
44
|
if (skip) {
|
|
45
45
|
logsEnabled && (0, utilsAndConstants_1.log)('useQuery.useEffect skip fetch', { skip, cacheKey });
|
|
@@ -4,11 +4,9 @@ export declare const optionalUtils: {
|
|
|
4
4
|
deepEqual?: (a: any, b: any) => boolean;
|
|
5
5
|
};
|
|
6
6
|
export declare const IS_DEV: boolean;
|
|
7
|
-
export declare const DEFAULT_QUERY_MUTATION_STATE: Readonly<{
|
|
8
|
-
loading: false;
|
|
9
|
-
}>;
|
|
10
7
|
export declare const EMPTY_OBJECT: Readonly<{}>;
|
|
11
8
|
export declare const EMPTY_ARRAY: readonly never[];
|
|
12
9
|
export declare const defaultGetCacheKey: <P = unknown>(params: P) => Key;
|
|
13
10
|
export declare const log: (tag: string, data?: unknown) => void;
|
|
14
11
|
export declare const applyEntityChanges: <T extends Typenames>(entities: EntitiesMap<T>, changes: EntityChanges<T>, options: CacheOptions) => EntitiesMap<T> | undefined;
|
|
12
|
+
export declare const isEmptyObject: (o: object) => boolean;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.applyEntityChanges = exports.log = exports.defaultGetCacheKey = exports.EMPTY_ARRAY = exports.EMPTY_OBJECT = exports.
|
|
3
|
+
exports.isEmptyObject = exports.applyEntityChanges = exports.log = exports.defaultGetCacheKey = 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,
|
|
@@ -20,7 +20,6 @@ exports.IS_DEV = (() => {
|
|
|
20
20
|
return process.env.NODE_ENV === 'development';
|
|
21
21
|
}
|
|
22
22
|
})();
|
|
23
|
-
exports.DEFAULT_QUERY_MUTATION_STATE = Object.freeze({ loading: false });
|
|
24
23
|
exports.EMPTY_OBJECT = Object.freeze({});
|
|
25
24
|
exports.EMPTY_ARRAY = Object.freeze([]);
|
|
26
25
|
const defaultGetCacheKey = (params) => {
|
|
@@ -124,3 +123,10 @@ const applyEntityChanges = (entities, changes, options) => {
|
|
|
124
123
|
return result;
|
|
125
124
|
};
|
|
126
125
|
exports.applyEntityChanges = applyEntityChanges;
|
|
126
|
+
const isEmptyObject = (o) => {
|
|
127
|
+
for (const _ in o) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
return true;
|
|
131
|
+
};
|
|
132
|
+
exports.isEmptyObject = isEmptyObject;
|
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.12.0-rc.0",
|
|
6
6
|
"description": "Powerful data fetching and caching library that supports normalization, built on top of redux",
|
|
7
7
|
"main": "dist/index.js",
|
|
8
8
|
"types": "dist/index.d.ts",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"redux": "4.2.1",
|
|
52
52
|
"redux-logger": "3.0.6",
|
|
53
53
|
"ts-jest": "29.1.0",
|
|
54
|
-
"typescript": "5.
|
|
54
|
+
"typescript": "5.6.3"
|
|
55
55
|
},
|
|
56
56
|
"files": [
|
|
57
57
|
"dist/*"
|