react-redux-cache 0.4.1 → 0.4.3
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/dist/createActions.d.ts +8 -8
- package/dist/createCache.d.ts +37 -31
- package/dist/createCache.js +17 -3
- package/dist/index.js +2 -1
- package/dist/mutate.d.ts +1 -1
- package/dist/mutate.js +2 -1
- package/dist/query.d.ts +1 -1
- package/dist/query.js +2 -1
- package/dist/reducer.d.ts +12 -12
- package/dist/types.d.ts +12 -8
- package/dist/useMutation.d.ts +2 -2
- package/dist/useQuery.d.ts +1 -1
- package/package.json +1 -1
package/dist/createActions.d.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
import type { EntityChanges, Key, QueryMutationState, Typenames } from './types';
|
|
2
|
-
export type ActionMap<N extends string, T extends Typenames, QR, MR> = ReturnType<typeof createActions<N, T, QR, MR>>;
|
|
3
|
-
export declare const createActions: <N extends string, T extends Typenames, QR, MR>(name: N) => {
|
|
2
|
+
export type ActionMap<N extends string, T extends Typenames, QP, QR, MP, MR> = ReturnType<typeof createActions<N, T, QP, QR, MP, MR>>;
|
|
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 QR>(queryKey: K, queryCacheKey: Key, state?: Partial<QueryMutationState<QR[K]>> | undefined, entityChagnes?: EntityChanges<T> | undefined): {
|
|
6
|
+
<K extends keyof QP & keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<QueryMutationState<QP[K], QR[K]>> | undefined, entityChagnes?: EntityChanges<T> | undefined): {
|
|
7
7
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
8
8
|
queryKey: K;
|
|
9
9
|
queryCacheKey: Key;
|
|
10
|
-
state: Partial<QueryMutationState<QR[K]>> | undefined;
|
|
10
|
+
state: Partial<QueryMutationState<QP[K], QR[K]>> | undefined;
|
|
11
11
|
entityChagnes: EntityChanges<T> | undefined;
|
|
12
12
|
};
|
|
13
13
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
14
14
|
};
|
|
15
15
|
/** Updates mutation state, and optionally merges entity changes in a single action. */
|
|
16
16
|
updateMutationStateAndEntities: {
|
|
17
|
-
<K_1 extends keyof MR>(mutationKey: K_1, state?: Partial<QueryMutationState<MR[K_1]>> | undefined, entityChagnes?: EntityChanges<T> | undefined): {
|
|
17
|
+
<K_1 extends keyof MP & keyof MR>(mutationKey: K_1, state?: Partial<QueryMutationState<MP[K_1], MR[K_1]>> | undefined, entityChagnes?: EntityChanges<T> | undefined): {
|
|
18
18
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
19
19
|
mutationKey: K_1;
|
|
20
|
-
state: Partial<QueryMutationState<MR[K_1]>> | undefined;
|
|
20
|
+
state: Partial<QueryMutationState<MP[K_1], MR[K_1]>> | undefined;
|
|
21
21
|
entityChagnes: EntityChanges<T> | undefined;
|
|
22
22
|
};
|
|
23
23
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
@@ -33,7 +33,7 @@ export declare const createActions: <N extends string, T extends Typenames, QR,
|
|
|
33
33
|
/** Clear states for provided query keys and cache keys.
|
|
34
34
|
* If cache key for query key is not provided, the whole state for query key is cleared. */
|
|
35
35
|
clearQueryState: {
|
|
36
|
-
<K_2 extends keyof QR>(queryKeys: {
|
|
36
|
+
<K_2 extends keyof QP & keyof QR>(queryKeys: {
|
|
37
37
|
key: K_2;
|
|
38
38
|
cacheKey?: Key | undefined;
|
|
39
39
|
}[]): {
|
|
@@ -47,7 +47,7 @@ export declare const createActions: <N extends string, T extends Typenames, QR,
|
|
|
47
47
|
};
|
|
48
48
|
/** Clear states for provided mutation keys. */
|
|
49
49
|
clearMutationState: {
|
|
50
|
-
<K_3 extends keyof MR>(mutationKeys: K_3[]): {
|
|
50
|
+
<K_3 extends keyof MP & keyof MR>(mutationKeys: K_3[]): {
|
|
51
51
|
type: `@rrc/${N}/clearMutationState`;
|
|
52
52
|
mutationKeys: K_3[];
|
|
53
53
|
};
|
package/dist/createCache.d.ts
CHANGED
|
@@ -11,18 +11,18 @@ export declare const createCache: <N extends string, T extends Typenames, QP, QR
|
|
|
11
11
|
/** Reducer of the cache, should be added to redux store. */
|
|
12
12
|
reducer: (state: {
|
|
13
13
|
entities: import("./types").EntitiesMap<T>;
|
|
14
|
-
queries: { [QK in keyof
|
|
15
|
-
mutations: { [MK in keyof
|
|
14
|
+
queries: QP | QR extends infer T_1 ? { [QK in keyof T_1]: import("./types").Dict<QueryMutationState<QP[QK], QR[QK]>>; } : never;
|
|
15
|
+
mutations: MP | MR extends infer T_2 ? { [MK in keyof T_2]: QueryMutationState<MP[MK], MR[MK]>; } : never;
|
|
16
16
|
} | undefined, action: {
|
|
17
17
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
18
|
-
queryKey: keyof QR;
|
|
18
|
+
queryKey: keyof QP & keyof QR;
|
|
19
19
|
queryCacheKey: Key;
|
|
20
|
-
state: Partial<QueryMutationState<QR[keyof QR]>> | undefined;
|
|
20
|
+
state: Partial<QueryMutationState<QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>> | undefined;
|
|
21
21
|
entityChagnes: import("./types").EntityChanges<T> | undefined;
|
|
22
22
|
} | {
|
|
23
23
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
24
|
-
mutationKey: keyof MR;
|
|
25
|
-
state: Partial<QueryMutationState<MR[keyof MR]>> | undefined;
|
|
24
|
+
mutationKey: keyof MP & keyof MR;
|
|
25
|
+
state: Partial<QueryMutationState<MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>> | undefined;
|
|
26
26
|
entityChagnes: import("./types").EntityChanges<T> | undefined;
|
|
27
27
|
} | {
|
|
28
28
|
type: `@rrc/${N}/mergeEntityChanges`;
|
|
@@ -30,33 +30,33 @@ export declare const createCache: <N extends string, T extends Typenames, QP, QR
|
|
|
30
30
|
} | {
|
|
31
31
|
type: `@rrc/${N}/clearQueryState`;
|
|
32
32
|
queryKeys: {
|
|
33
|
-
key: keyof QR;
|
|
33
|
+
key: keyof QP & keyof QR;
|
|
34
34
|
cacheKey?: Key | undefined;
|
|
35
35
|
}[];
|
|
36
36
|
} | {
|
|
37
37
|
type: `@rrc/${N}/clearMutationState`;
|
|
38
|
-
mutationKeys: (keyof MR)[];
|
|
38
|
+
mutationKeys: (keyof MP & keyof MR)[];
|
|
39
39
|
}) => {
|
|
40
40
|
entities: import("./types").EntitiesMap<T>;
|
|
41
|
-
queries: { [QK in keyof
|
|
42
|
-
mutations: { [MK in keyof
|
|
41
|
+
queries: QP | QR extends infer T_3 ? { [QK in keyof T_3]: import("./types").Dict<QueryMutationState<QP[QK], QR[QK]>>; } : never;
|
|
42
|
+
mutations: MP | MR extends infer T_4 ? { [MK in keyof T_4]: QueryMutationState<MP[MK], MR[MK]>; } : never;
|
|
43
43
|
};
|
|
44
44
|
actions: {
|
|
45
45
|
updateQueryStateAndEntities: {
|
|
46
|
-
<K extends keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<QueryMutationState<QR[K]>> | undefined, entityChagnes?: import("./types").EntityChanges<T> | undefined): {
|
|
46
|
+
<K extends keyof QP & keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<QueryMutationState<QP[K], QR[K]>> | undefined, entityChagnes?: import("./types").EntityChanges<T> | undefined): {
|
|
47
47
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
48
48
|
queryKey: K;
|
|
49
49
|
queryCacheKey: Key;
|
|
50
|
-
state: Partial<QueryMutationState<QR[K]>> | undefined;
|
|
50
|
+
state: Partial<QueryMutationState<QP[K], QR[K]>> | undefined;
|
|
51
51
|
entityChagnes: import("./types").EntityChanges<T> | undefined;
|
|
52
52
|
};
|
|
53
53
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
54
54
|
};
|
|
55
55
|
updateMutationStateAndEntities: {
|
|
56
|
-
<K_1 extends keyof MR>(mutationKey: K_1, state?: Partial<QueryMutationState<MR[K_1]>> | undefined, entityChagnes?: import("./types").EntityChanges<T> | undefined): {
|
|
56
|
+
<K_1 extends keyof MP & keyof MR>(mutationKey: K_1, state?: Partial<QueryMutationState<MP[K_1], MR[K_1]>> | undefined, entityChagnes?: import("./types").EntityChanges<T> | undefined): {
|
|
57
57
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
58
58
|
mutationKey: K_1;
|
|
59
|
-
state: Partial<QueryMutationState<MR[K_1]>> | undefined;
|
|
59
|
+
state: Partial<QueryMutationState<MP[K_1], MR[K_1]>> | undefined;
|
|
60
60
|
entityChagnes: import("./types").EntityChanges<T> | undefined;
|
|
61
61
|
};
|
|
62
62
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
@@ -69,7 +69,7 @@ export declare const createCache: <N extends string, T extends Typenames, QP, QR
|
|
|
69
69
|
type: `@rrc/${N}/mergeEntityChanges`;
|
|
70
70
|
};
|
|
71
71
|
clearQueryState: {
|
|
72
|
-
<K_2 extends keyof QR>(queryKeys: {
|
|
72
|
+
<K_2 extends keyof QP & keyof QR>(queryKeys: {
|
|
73
73
|
key: K_2;
|
|
74
74
|
cacheKey?: Key | undefined;
|
|
75
75
|
}[]): {
|
|
@@ -82,30 +82,34 @@ export declare const createCache: <N extends string, T extends Typenames, QP, QR
|
|
|
82
82
|
type: `@rrc/${N}/clearQueryState`;
|
|
83
83
|
};
|
|
84
84
|
clearMutationState: {
|
|
85
|
-
<K_3 extends keyof MR>(mutationKeys: K_3[]): {
|
|
85
|
+
<K_3 extends keyof MP & keyof MR>(mutationKeys: K_3[]): {
|
|
86
86
|
type: `@rrc/${N}/clearMutationState`;
|
|
87
87
|
mutationKeys: K_3[];
|
|
88
88
|
};
|
|
89
89
|
type: `@rrc/${N}/clearMutationState`;
|
|
90
|
-
};
|
|
90
|
+
};
|
|
91
91
|
};
|
|
92
92
|
selectors: {
|
|
93
93
|
/** Selects query state. */
|
|
94
|
-
selectQueryState: <QK_1 extends keyof
|
|
94
|
+
selectQueryState: <QK_1 extends keyof QP | keyof QR>(state: unknown, query: QK_1, cacheKey: Key) => QueryMutationState<QK_1 extends keyof QP & keyof QR ? QP[QK_1] : never, QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never> | undefined;
|
|
95
95
|
/** Selects query latest result. */
|
|
96
|
-
selectQueryResult: <QK_2 extends keyof
|
|
96
|
+
selectQueryResult: <QK_2 extends keyof QP | keyof QR>(state: unknown, query: QK_2, cacheKey: Key) => (QK_2 extends keyof QP & keyof QR ? QR[QK_2] : never) | undefined;
|
|
97
97
|
/** Selects query loading state. */
|
|
98
|
-
selectQueryLoading: <QK_3 extends keyof
|
|
98
|
+
selectQueryLoading: <QK_3 extends keyof QP | keyof QR>(state: unknown, query: QK_3, cacheKey: Key) => boolean | undefined;
|
|
99
99
|
/** Selects query latest error. */
|
|
100
|
-
selectQueryError: <QK_4 extends keyof
|
|
100
|
+
selectQueryError: <QK_4 extends keyof QP | keyof QR>(state: unknown, query: QK_4, cacheKey: Key) => Error | undefined;
|
|
101
|
+
/** Selects query latest params. */
|
|
102
|
+
selectQueryParams: <QK_5 extends keyof QP | keyof QR>(state: unknown, query: QK_5, cacheKey: Key) => (QK_5 extends keyof QP & keyof QR ? QP[QK_5] : never) | undefined;
|
|
101
103
|
/** Selects mutation state. */
|
|
102
|
-
selectMutationState: <MK_1 extends keyof MP | keyof MR>(state: unknown, mutation: MK_1) => QueryMutationState<MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>;
|
|
104
|
+
selectMutationState: <MK_1 extends keyof MP | keyof MR>(state: unknown, mutation: MK_1) => QueryMutationState<MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never, MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>;
|
|
103
105
|
/** Selects mutation latest result. */
|
|
104
106
|
selectMutationResult: <MK_2 extends keyof MP | keyof MR>(state: unknown, mutation: MK_2) => (MK_2 extends keyof MP & keyof MR ? MR[MK_2] : never) | undefined;
|
|
105
107
|
/** Selects mutation loading state. */
|
|
106
108
|
selectMutationLoading: <MK_3 extends keyof MP | keyof MR>(state: unknown, mutation: MK_3) => boolean;
|
|
107
109
|
/** Selects mutation latest error. */
|
|
108
110
|
selectMutationError: <MK_4 extends keyof MP | keyof MR>(state: unknown, mutation: MK_4) => Error | undefined;
|
|
111
|
+
/** Selects mutation latest params. */
|
|
112
|
+
selectMutationParams: <MK_5 extends keyof MP | keyof MR>(state: unknown, mutation: MK_5) => MK_5 extends keyof MP & keyof MR ? MP[MK_5] : never;
|
|
109
113
|
/** Selects entity by id and typename. */
|
|
110
114
|
selectEntityById: <TN extends keyof T>(state: unknown, id: Key | null | undefined, typename: TN) => T[TN] | undefined;
|
|
111
115
|
/** Selects all entities. */
|
|
@@ -116,18 +120,20 @@ export declare const createCache: <N extends string, T extends Typenames, QP, QR
|
|
|
116
120
|
hooks: {
|
|
117
121
|
/** Returns client object with query and mutate functions. */
|
|
118
122
|
useClient: () => {
|
|
119
|
-
query: <
|
|
120
|
-
mutate: <
|
|
121
|
-
mutation:
|
|
122
|
-
params:
|
|
123
|
-
}) => Promise<MutationResult<
|
|
123
|
+
query: <QK_6 extends keyof QP | keyof QR>(options: Omit<QueryOptions<T, QP, QR, QK_6>, "cachePolicy">) => Promise<QueryResult<QK_6 extends keyof QP & keyof QR ? QR[QK_6] : never>>;
|
|
124
|
+
mutate: <MK_6 extends keyof MP | keyof MR>(options: {
|
|
125
|
+
mutation: MK_6;
|
|
126
|
+
params: MK_6 extends keyof MP & keyof MR ? MP[MK_6] : never;
|
|
127
|
+
}) => Promise<MutationResult<MK_6 extends keyof MP & keyof MR ? MR[MK_6] : never>>;
|
|
124
128
|
};
|
|
125
129
|
/** Fetches query when params change and subscribes to query state. */
|
|
126
|
-
useQuery: <
|
|
130
|
+
useQuery: <QK_7 extends keyof QP | keyof QR>(options: import("./types").UseQueryOptions<T, QP, QR, QK_7>) => readonly [QueryMutationState<QK_7 extends keyof QP & keyof QR ? QP[QK_7] : never, QK_7 extends keyof QP & keyof QR ? QR[QK_7] : never>, () => Promise<QueryResult<QK_7 extends infer T_5 ? T_5 extends QK_7 ? T_5 extends keyof QP & keyof QR ? QR[T_5] : never : never : never>>];
|
|
127
131
|
/** Subscribes to provided mutation state and provides mutate function. */
|
|
128
|
-
useMutation: <
|
|
129
|
-
mutation:
|
|
130
|
-
}) => readonly [(params:
|
|
132
|
+
useMutation: <MK_7 extends keyof MP | keyof MR>(options: {
|
|
133
|
+
mutation: MK_7;
|
|
134
|
+
}) => readonly [(params: MK_7 extends keyof MP & keyof MR ? MP[MK_7] : never) => Promise<MutationResult<MK_7 extends infer T_6 ? T_6 extends MK_7 ? T_6 extends keyof MP & keyof MR ? MR[T_6] : never : never : never>>, QueryMutationState<MK_7 extends keyof MP & keyof MR ? MP[MK_7] : never, MK_7 extends keyof MP & keyof MR ? MP[MK_7] : never>, () => boolean];
|
|
135
|
+
/** useSelector + selectEntityById. */
|
|
136
|
+
useSelectEntityById: <TN_2 extends keyof T>(id: Key | null | undefined, typename: TN_2) => T[TN_2] | undefined;
|
|
131
137
|
};
|
|
132
138
|
utils: {
|
|
133
139
|
/**
|
package/dist/createCache.js
CHANGED
|
@@ -29,6 +29,9 @@ const createCache = (partialCache) => {
|
|
|
29
29
|
partialCache.abortControllers = abortControllers;
|
|
30
30
|
const cache = partialCache;
|
|
31
31
|
// make selectors
|
|
32
|
+
const selectEntityById = (state, id, typename) => {
|
|
33
|
+
return id == null ? undefined : cache.cacheStateSelector(state).entities[typename][id];
|
|
34
|
+
};
|
|
32
35
|
const selectQueryState = (state, query, cacheKey) => {
|
|
33
36
|
// @ts-expect-error fix later
|
|
34
37
|
return cache.cacheStateSelector(state).queries[query][cacheKey];
|
|
@@ -62,6 +65,11 @@ const createCache = (partialCache) => {
|
|
|
62
65
|
var _a;
|
|
63
66
|
return (_a = selectQueryState(state, query, cacheKey)) === null || _a === void 0 ? void 0 : _a.error;
|
|
64
67
|
},
|
|
68
|
+
/** Selects query latest params. */
|
|
69
|
+
selectQueryParams: (state, query, cacheKey) => {
|
|
70
|
+
var _a;
|
|
71
|
+
return (_a = selectQueryState(state, query, cacheKey)) === null || _a === void 0 ? void 0 : _a.params;
|
|
72
|
+
},
|
|
65
73
|
/** Selects mutation state. */
|
|
66
74
|
selectMutationState,
|
|
67
75
|
/** Selects mutation latest result. */
|
|
@@ -76,10 +84,12 @@ const createCache = (partialCache) => {
|
|
|
76
84
|
selectMutationError: (state, mutation) => {
|
|
77
85
|
return selectMutationState(state, mutation).error;
|
|
78
86
|
},
|
|
79
|
-
/** Selects
|
|
80
|
-
|
|
81
|
-
return
|
|
87
|
+
/** Selects mutation latest params. */
|
|
88
|
+
selectMutationParams: (state, mutation) => {
|
|
89
|
+
return selectMutationState(state, mutation).params;
|
|
82
90
|
},
|
|
91
|
+
/** Selects entity by id and typename. */
|
|
92
|
+
selectEntityById,
|
|
83
93
|
/** Selects all entities. */
|
|
84
94
|
selectEntities: (state) => {
|
|
85
95
|
return cache.cacheStateSelector(state).entities;
|
|
@@ -114,6 +124,10 @@ const createCache = (partialCache) => {
|
|
|
114
124
|
useQuery: (options) => (0, useQuery_1.useQuery)(cache, actions, options),
|
|
115
125
|
/** Subscribes to provided mutation state and provides mutate function. */
|
|
116
126
|
useMutation: (options) => (0, useMutation_1.useMutation)(cache, actions, options, abortControllers),
|
|
127
|
+
/** useSelector + selectEntityById. */
|
|
128
|
+
useSelectEntityById: (id, typename) => {
|
|
129
|
+
return (0, react_redux_1.useSelector)((state) => selectEntityById(state, id, typename));
|
|
130
|
+
},
|
|
117
131
|
},
|
|
118
132
|
utils: {
|
|
119
133
|
/**
|
package/dist/index.js
CHANGED
|
@@ -23,7 +23,8 @@ Object.defineProperty(exports, "defaultGetCacheKey", { enumerable: true, get: fu
|
|
|
23
23
|
Object.defineProperty(exports, "defaultQueryMutationState", { enumerable: true, get: function () { return utilsAndConstants_1.DEFAULT_QUERY_MUTATION_STATE; } });
|
|
24
24
|
// Backlog
|
|
25
25
|
// ! high
|
|
26
|
-
//
|
|
26
|
+
// determine refresh vs load next page
|
|
27
|
+
// update readme with how to use mergeResults for invalidating / updating caches
|
|
27
28
|
// try use skip for refreshing strategy?
|
|
28
29
|
// optimistic response
|
|
29
30
|
// make query key / cache key difference more clear in the docs, and/or rename queryKey -> query
|
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 | keyof MR>(logTag: string, store: Store, cache: Cache<N, T, QP, QR, MP, MR>, { updateMutationStateAndEntities }: Pick<ActionMap<N, T,
|
|
4
|
+
export declare const mutate: <N extends string, T extends Typenames, QP, QR, MP, MR, MK extends keyof MP | keyof 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 & keyof MR ? MP[MK] : never, abortControllers: WeakMap<Store, Record<Key, AbortController>>) => Promise<MutationResult<MK extends keyof MP & keyof MR ? MR[MK] : never>>;
|
package/dist/mutate.js
CHANGED
|
@@ -11,7 +11,7 @@ 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) => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
|
+
const mutate = (logTag, store, cache, { updateMutationStateAndEntities, }, mutationKey, params, abortControllers) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
15
|
let abortControllersOfStore = abortControllers.get(store);
|
|
16
16
|
if (abortControllersOfStore === undefined) {
|
|
17
17
|
abortControllersOfStore = {};
|
|
@@ -31,6 +31,7 @@ const mutate = (logTag, store, cache, { updateMutationStateAndEntities }, mutati
|
|
|
31
31
|
else {
|
|
32
32
|
store.dispatch(updateMutationStateAndEntities(mutationKey, {
|
|
33
33
|
loading: true,
|
|
34
|
+
params,
|
|
34
35
|
result: undefined,
|
|
35
36
|
}));
|
|
36
37
|
}
|
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 | keyof QR>(logTag: string, store: Store, cache: Cache<N, T, QP, QR, MP, MR>, { updateQueryStateAndEntities }: Pick<ActionMap<N, T, QR,
|
|
4
|
+
export declare const query: <N extends string, T extends Typenames, QP, QR, MP, MR, QK extends keyof QP | keyof 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 & keyof QR ? QP[QK] : never) => Promise<QueryResult<QK extends keyof QP & keyof QR ? QR[QK] : never>>;
|
package/dist/query.js
CHANGED
|
@@ -11,7 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.query = void 0;
|
|
13
13
|
const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
14
|
-
const query = (logTag, store, cache, { updateQueryStateAndEntities }, queryKey, cacheKey, params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
|
+
const query = (logTag, store, cache, { updateQueryStateAndEntities, }, queryKey, cacheKey, params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
15
|
var _a;
|
|
16
16
|
const logsEnabled = cache.options.logsEnabled;
|
|
17
17
|
const cacheStateSelector = cache.cacheStateSelector;
|
|
@@ -29,6 +29,7 @@ const query = (logTag, store, cache, { updateQueryStateAndEntities }, queryKey,
|
|
|
29
29
|
}
|
|
30
30
|
store.dispatch(updateQueryStateAndEntities(queryKey, cacheKey, {
|
|
31
31
|
loading: true,
|
|
32
|
+
params,
|
|
32
33
|
}));
|
|
33
34
|
logsEnabled && (0, utilsAndConstants_1.log)(`${logTag} started`, { queryStateOnStart, params, cacheKey });
|
|
34
35
|
let response;
|
package/dist/reducer.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import type { ActionMap } from './createActions';
|
|
2
2
|
import type { CacheOptions, Dict, EntitiesMap, QueryMutationState, Typenames } from './types';
|
|
3
|
-
export type ReduxCacheState<T extends Typenames, QR, MR> = ReturnType<ReturnType<typeof createCacheReducer<string, T, QR, MR>>>;
|
|
4
|
-
export declare const createCacheReducer: <N extends string, T extends Typenames, QR, MR>(actions: ActionMap<N, T, QR, MR>, typenames: T, queryKeys: (keyof QR)[], cacheOptions: CacheOptions) => (state: {
|
|
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>, typenames: T, queryKeys: (keyof QP & keyof QR)[], cacheOptions: CacheOptions) => (state: {
|
|
5
5
|
entities: EntitiesMap<T>;
|
|
6
|
-
queries: { [QK in keyof
|
|
7
|
-
mutations: { [MK in keyof
|
|
6
|
+
queries: QP | QR extends infer T_1 ? { [QK in keyof T_1]: Dict<QueryMutationState<QP[QK], QR[QK]>>; } : never;
|
|
7
|
+
mutations: MP | MR extends infer T_2 ? { [MK in keyof T_2]: QueryMutationState<MP[MK], MR[MK]>; } : never;
|
|
8
8
|
} | undefined, action: {
|
|
9
9
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
10
|
-
queryKey: keyof QR;
|
|
10
|
+
queryKey: keyof QP & keyof QR;
|
|
11
11
|
queryCacheKey: import("./types").Key;
|
|
12
|
-
state: Partial<QueryMutationState<QR[keyof QR]>> | undefined;
|
|
12
|
+
state: Partial<QueryMutationState<QP[keyof QP & keyof QR], QR[keyof QP & keyof QR]>> | undefined;
|
|
13
13
|
entityChagnes: import("./types").EntityChanges<T> | undefined;
|
|
14
14
|
} | {
|
|
15
15
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
16
|
-
mutationKey: keyof MR;
|
|
17
|
-
state: Partial<QueryMutationState<MR[keyof MR]>> | undefined;
|
|
16
|
+
mutationKey: keyof MP & keyof MR;
|
|
17
|
+
state: Partial<QueryMutationState<MP[keyof MP & keyof MR], MR[keyof MP & keyof MR]>> | undefined;
|
|
18
18
|
entityChagnes: import("./types").EntityChanges<T> | undefined;
|
|
19
19
|
} | {
|
|
20
20
|
type: `@rrc/${N}/mergeEntityChanges`;
|
|
@@ -22,14 +22,14 @@ export declare const createCacheReducer: <N extends string, T extends Typenames,
|
|
|
22
22
|
} | {
|
|
23
23
|
type: `@rrc/${N}/clearQueryState`;
|
|
24
24
|
queryKeys: {
|
|
25
|
-
key: keyof QR;
|
|
25
|
+
key: keyof QP & keyof QR;
|
|
26
26
|
cacheKey?: import("./types").Key | undefined;
|
|
27
27
|
}[];
|
|
28
28
|
} | {
|
|
29
29
|
type: `@rrc/${N}/clearMutationState`;
|
|
30
|
-
mutationKeys: (keyof MR)[];
|
|
30
|
+
mutationKeys: (keyof MP & keyof MR)[];
|
|
31
31
|
}) => {
|
|
32
32
|
entities: EntitiesMap<T>;
|
|
33
|
-
queries: { [QK in keyof
|
|
34
|
-
mutations: { [MK in keyof
|
|
33
|
+
queries: QP | QR extends infer T_1 ? { [QK in keyof T_1]: Dict<QueryMutationState<QP[QK], QR[QK]>>; } : never;
|
|
34
|
+
mutations: MP | MR extends infer T_2 ? { [MK in keyof T_2]: QueryMutationState<MP[MK], MR[MK]>; } : never;
|
|
35
35
|
};
|
package/dist/types.d.ts
CHANGED
|
@@ -21,8 +21,10 @@ export type Typenames = Record<string, object>;
|
|
|
21
21
|
export type Cache<N extends string, T extends Typenames, QP, QR, MP, MR> = {
|
|
22
22
|
/** Used as prefix for actions and in default cacheStateSelector for selecting cache state from redux state. */
|
|
23
23
|
name: N;
|
|
24
|
-
/**
|
|
25
|
-
*
|
|
24
|
+
/**
|
|
25
|
+
* Mapping of all typenames to their entity types, which is needed for proper normalization. Can be empty If normalization not needed.
|
|
26
|
+
* @key Typename.
|
|
27
|
+
* @value Object with proper type of the typename. Empty objects with type casting can be used.
|
|
26
28
|
* @example
|
|
27
29
|
* typenames: {
|
|
28
30
|
users: {} as User, // here `users` entities will have type `User`
|
|
@@ -30,14 +32,14 @@ export type Cache<N extends string, T extends Typenames, QP, QR, MP, MR> = {
|
|
|
30
32
|
} */
|
|
31
33
|
typenames: T;
|
|
32
34
|
queries: {
|
|
33
|
-
[QK in keyof (QP & QR)]: QK extends keyof (QP | QR) ? QueryInfo<T, QP[QK], QR[QK], ReduxCacheState<T, QR, MR>> : never;
|
|
35
|
+
[QK in keyof (QP & QR)]: QK extends keyof (QP | QR) ? QueryInfo<T, QP[QK], QR[QK], ReduxCacheState<T, QP, QR, MP, MR>> : never;
|
|
34
36
|
};
|
|
35
37
|
mutations: {
|
|
36
38
|
[MK in keyof (MP & MR)]: MK extends keyof (MP | MR) ? MutationInfo<T, MP[MK], MR[MK]> : never;
|
|
37
39
|
};
|
|
38
40
|
options: CacheOptions;
|
|
39
41
|
/** Should return cache state from redux root state. Default implementation returns `state[name]`. */
|
|
40
|
-
cacheStateSelector: (state: any) => ReduxCacheState<T, QR, MR>;
|
|
42
|
+
cacheStateSelector: (state: any) => ReduxCacheState<T, QP, QR, MP, MR>;
|
|
41
43
|
};
|
|
42
44
|
export type CacheOptions = {
|
|
43
45
|
/**
|
|
@@ -83,11 +85,11 @@ export type QueryInfo<T extends Typenames, P, R, S> = {
|
|
|
83
85
|
* */
|
|
84
86
|
getCacheKey?: (params?: P) => Key;
|
|
85
87
|
};
|
|
86
|
-
export type UseQueryOptions<T extends Typenames, QP, QR,
|
|
88
|
+
export type UseQueryOptions<T extends Typenames, QP, QR, QK extends keyof (QP & QR)> = {
|
|
87
89
|
query: QK;
|
|
88
90
|
params: QK extends keyof (QP | QR) ? QP[QK] : never;
|
|
89
91
|
skip?: boolean;
|
|
90
|
-
} & Pick<
|
|
92
|
+
} & Pick<QueryInfo<T, unknown, unknown, unknown>, 'cachePolicy'>;
|
|
91
93
|
/**
|
|
92
94
|
* @param cache-first for each params key fetch is not called if cache exists.
|
|
93
95
|
* @param cache-and-fetch for each params key result is taken from cache and fetch is called.
|
|
@@ -102,7 +104,7 @@ export type QueryResult<R> = {
|
|
|
102
104
|
cancelled?: true;
|
|
103
105
|
result?: R;
|
|
104
106
|
};
|
|
105
|
-
export type QueryOptions<T extends Typenames, QP, QR,
|
|
107
|
+
export type QueryOptions<T extends Typenames, QP, QR, QK extends keyof (QP & QR)> = Omit<UseQueryOptions<T, QP, QR, QK>, 'skip'>;
|
|
106
108
|
export type Mutation<T extends Typenames, P, R> = (params: P,
|
|
107
109
|
/** Signal is aborted for current mutation when the same mutation was called once again. */
|
|
108
110
|
abortSignal: AbortSignal) => Promise<MutationResponse<T, R>>;
|
|
@@ -118,11 +120,13 @@ export type MutationResult<R> = {
|
|
|
118
120
|
aborted?: true;
|
|
119
121
|
result?: R;
|
|
120
122
|
};
|
|
121
|
-
export type QueryMutationState<R> = {
|
|
123
|
+
export type QueryMutationState<P, R> = {
|
|
122
124
|
/** `true` when query or mutation is currently in progress. */
|
|
123
125
|
loading: boolean;
|
|
124
126
|
/** Result of the latest successfull response. */
|
|
125
127
|
result?: R;
|
|
126
128
|
/** Error of the latest response. */
|
|
127
129
|
error?: Error;
|
|
130
|
+
/** Parameters of the latest request. */
|
|
131
|
+
params: P;
|
|
128
132
|
};
|
package/dist/useMutation.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Store } from 'redux';
|
|
2
2
|
import { ActionMap } from './createActions';
|
|
3
3
|
import { Cache, Key, QueryMutationState, Typenames } from './types';
|
|
4
|
-
export declare const useMutation: <N extends string, T extends Typenames, MP, MR, MK extends keyof MP | keyof MR>(cache: Cache<N, T, unknown, unknown, MP, MR>, actions: Pick<ActionMap<N, T, unknown, MR>, "updateMutationStateAndEntities">, options: {
|
|
4
|
+
export declare const useMutation: <N extends string, T extends Typenames, MP, MR, MK extends keyof MP | keyof MR>(cache: Cache<N, T, unknown, unknown, MP, MR>, actions: Pick<ActionMap<N, T, unknown, unknown, MP, MR>, "updateMutationStateAndEntities">, options: {
|
|
5
5
|
mutation: MK;
|
|
6
|
-
}, 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>>, QueryMutationState<MK extends keyof MP & keyof MR ? MP[MK] : never>, () => boolean];
|
|
6
|
+
}, 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>>, QueryMutationState<MK extends keyof MP & keyof MR ? MP[MK] : never, MK extends keyof MP & keyof MR ? MP[MK] : never>, () => boolean];
|
package/dist/useQuery.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { ActionMap } from './createActions';
|
|
2
2
|
import { Cache, QueryMutationState, Typenames, UseQueryOptions } from './types';
|
|
3
|
-
export declare const useQuery: <N extends string, T extends Typenames, QP, QR, MP, MR, QK extends keyof QP | keyof QR>(cache: Cache<N, T, QP, QR, MP, MR>, actions: Pick<ActionMap<N, T, QR, MR>, "updateQueryStateAndEntities">, options: UseQueryOptions<T, QP, QR,
|
|
3
|
+
export declare const useQuery: <N extends string, T extends Typenames, QP, QR, MP, MR, QK extends keyof QP | keyof 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 [QueryMutationState<QK extends keyof QP & keyof QR ? QP[QK] : never, QK extends keyof QP & keyof QR ? QR[QK] : never>, () => 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/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.4.
|
|
5
|
+
"version": "0.4.3",
|
|
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",
|