react-redux-cache 0.12.0 → 0.13.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 +30 -15
- package/dist/createActions.d.ts +3 -10
- package/dist/createActions.js +0 -7
- package/dist/createCache.d.ts +80 -61
- package/dist/createCache.js +47 -68
- package/dist/createCacheReducer.d.ts +2 -2
- package/dist/createSelectors.d.ts +18 -0
- package/dist/createSelectors.js +61 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +10 -4
- package/dist/mutate.d.ts +3 -2
- package/dist/mutate.js +14 -9
- package/dist/query.d.ts +3 -2
- package/dist/query.js +14 -10
- package/dist/types.d.ts +42 -45
- package/dist/useMutation.d.ts +3 -2
- package/dist/useMutation.js +2 -2
- package/dist/useQuery.d.ts +5 -2
- package/dist/useQuery.js +21 -20
- package/dist/utilsAndConstants.d.ts +7 -1
- package/dist/utilsAndConstants.js +9 -1
- package/package.json +2 -2
package/dist/createCache.js
CHANGED
|
@@ -5,6 +5,7 @@ const react_1 = require("react");
|
|
|
5
5
|
const react_redux_1 = require("react-redux");
|
|
6
6
|
const createActions_1 = require("./createActions");
|
|
7
7
|
const createCacheReducer_1 = require("./createCacheReducer");
|
|
8
|
+
const createSelectors_1 = require("./createSelectors");
|
|
8
9
|
const mutate_1 = require("./mutate");
|
|
9
10
|
const query_1 = require("./query");
|
|
10
11
|
const useMutation_1 = require("./useMutation");
|
|
@@ -24,20 +25,22 @@ const withTypenames = () => {
|
|
|
24
25
|
*/
|
|
25
26
|
return {
|
|
26
27
|
createCache: (partialCache) => {
|
|
27
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
28
|
-
var
|
|
28
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
29
|
+
var _m, _o, _p, _q, _r, _s;
|
|
29
30
|
const abortControllers = new WeakMap();
|
|
30
31
|
// provide all optional fields
|
|
31
32
|
(_a = partialCache.options) !== null && _a !== void 0 ? _a : (partialCache.options = {});
|
|
32
|
-
(_b = (
|
|
33
|
-
(_c = (
|
|
34
|
-
(_d = (
|
|
33
|
+
(_b = (_m = partialCache.options).logsEnabled) !== null && _b !== void 0 ? _b : (_m.logsEnabled = false);
|
|
34
|
+
(_c = (_o = partialCache.options).additionalValidation) !== null && _c !== void 0 ? _c : (_o.additionalValidation = utilsAndConstants_1.IS_DEV);
|
|
35
|
+
(_d = (_p = partialCache.options).deepComparisonEnabled) !== null && _d !== void 0 ? _d : (_p.deepComparisonEnabled = true);
|
|
35
36
|
(_e = partialCache.queries) !== null && _e !== void 0 ? _e : (partialCache.queries = {});
|
|
36
37
|
(_f = partialCache.mutations) !== null && _f !== void 0 ? _f : (partialCache.mutations = {});
|
|
37
38
|
(_g = partialCache.globals) !== null && _g !== void 0 ? _g : (partialCache.globals = {});
|
|
38
|
-
(_h = (
|
|
39
|
+
(_h = (_q = partialCache.globals).queries) !== null && _h !== void 0 ? _h : (_q.queries = {});
|
|
40
|
+
(_j = (_r = partialCache.globals.queries).fetchPolicy) !== null && _j !== void 0 ? _j : (_r.fetchPolicy = utilsAndConstants_1.FetchPolicy.NoCacheOrExpired);
|
|
41
|
+
(_k = (_s = partialCache.globals.queries).skipFetch) !== null && _k !== void 0 ? _k : (_s.skipFetch = false);
|
|
39
42
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
40
|
-
(
|
|
43
|
+
(_l = partialCache.cacheStateSelector) !== null && _l !== void 0 ? _l : (partialCache.cacheStateSelector = (state) => state[cache.name]);
|
|
41
44
|
// @ts-expect-error private field for testing
|
|
42
45
|
partialCache.abortControllers = abortControllers;
|
|
43
46
|
const cache = partialCache;
|
|
@@ -45,81 +48,61 @@ const withTypenames = () => {
|
|
|
45
48
|
if (cache.options.deepComparisonEnabled && !utilsAndConstants_1.optionalUtils.deepEqual) {
|
|
46
49
|
console.warn('react-redux-cache: optional dependency for fast-deep-equal was not provided, while deepComparisonEnabled option is true');
|
|
47
50
|
}
|
|
48
|
-
//
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
};
|
|
53
|
-
const selectQueryState = (state, query, cacheKey) => {
|
|
54
|
-
var _a;
|
|
55
|
-
// @ts-expect-error fix later
|
|
56
|
-
return (_a = cache.cacheStateSelector(state).queries[query][cacheKey]) !== null && _a !== void 0 ? _a : utilsAndConstants_1.EMPTY_OBJECT;
|
|
57
|
-
};
|
|
58
|
-
const selectMutationState = (state, mutation) => {
|
|
59
|
-
var _a;
|
|
60
|
-
// @ts-expect-error fix later
|
|
61
|
-
return (_a = cache.cacheStateSelector(state).mutations[mutation]) !== null && _a !== void 0 ? _a : utilsAndConstants_1.EMPTY_OBJECT;
|
|
62
|
-
};
|
|
51
|
+
// selectors
|
|
52
|
+
const selectors = (0, createSelectors_1.createSelectors)(cache);
|
|
53
|
+
const { selectQueryState, selectQueryResult, selectQueryLoading, selectQueryError, selectQueryParams, selectQueryExpiresAt, selectMutationState, selectMutationResult, selectMutationLoading, selectMutationError, selectMutationParams, selectEntityById, selectEntities, selectEntitiesByTypename, } = selectors;
|
|
54
|
+
// actions
|
|
63
55
|
const actions = (0, createActions_1.createActions)(cache.name);
|
|
56
|
+
const { updateQueryStateAndEntities, updateMutationStateAndEntities, mergeEntityChanges, invalidateQuery, clearQueryState, clearMutationState, } = actions;
|
|
64
57
|
return {
|
|
65
58
|
/** Keeps all options, passed while creating the cache. */
|
|
66
59
|
cache,
|
|
67
60
|
/** Reducer of the cache, should be added to redux store. */
|
|
68
61
|
reducer: (0, createCacheReducer_1.createCacheReducer)(actions, Object.keys(cache.queries), cache.options),
|
|
69
|
-
actions
|
|
62
|
+
actions: {
|
|
63
|
+
/** Updates query state, and optionally merges entity changes in a single action. */
|
|
64
|
+
updateQueryStateAndEntities,
|
|
65
|
+
/** Updates mutation state, and optionally merges entity changes in a single action. */
|
|
66
|
+
updateMutationStateAndEntities,
|
|
67
|
+
/** Merge EntityChanges to the state. */
|
|
68
|
+
mergeEntityChanges,
|
|
69
|
+
/** Invalidates query states. */
|
|
70
|
+
invalidateQuery,
|
|
71
|
+
/** Clear states for provided query keys and cache keys.
|
|
72
|
+
* If cache key for query key is not provided, the whole state for query key is cleared. */
|
|
73
|
+
clearQueryState,
|
|
74
|
+
/** Clear states for provided mutation keys. */
|
|
75
|
+
clearMutationState,
|
|
76
|
+
},
|
|
70
77
|
selectors: {
|
|
71
78
|
/** Selects query state. */
|
|
72
79
|
selectQueryState,
|
|
73
80
|
/** Selects query latest result. */
|
|
74
|
-
selectQueryResult
|
|
75
|
-
return selectQueryState(state, query, cacheKey).result;
|
|
76
|
-
},
|
|
81
|
+
selectQueryResult,
|
|
77
82
|
/** Selects query loading state. */
|
|
78
|
-
selectQueryLoading
|
|
79
|
-
var _a;
|
|
80
|
-
return (_a = selectQueryState(state, query, cacheKey).loading) !== null && _a !== void 0 ? _a : false;
|
|
81
|
-
},
|
|
83
|
+
selectQueryLoading,
|
|
82
84
|
/** Selects query latest error. */
|
|
83
|
-
selectQueryError
|
|
84
|
-
return selectQueryState(state, query, cacheKey).error;
|
|
85
|
-
},
|
|
85
|
+
selectQueryError,
|
|
86
86
|
/** Selects query latest params. */
|
|
87
|
-
selectQueryParams
|
|
88
|
-
return selectQueryState(state, query, cacheKey).params;
|
|
89
|
-
},
|
|
87
|
+
selectQueryParams,
|
|
90
88
|
/** Selects query latest expiresAt. */
|
|
91
|
-
selectQueryExpiresAt
|
|
92
|
-
return selectQueryState(state, query, cacheKey).expiresAt;
|
|
93
|
-
},
|
|
89
|
+
selectQueryExpiresAt,
|
|
94
90
|
/** Selects mutation state. */
|
|
95
91
|
selectMutationState,
|
|
96
92
|
/** Selects mutation latest result. */
|
|
97
|
-
selectMutationResult
|
|
98
|
-
return selectMutationState(state, mutation).result;
|
|
99
|
-
},
|
|
93
|
+
selectMutationResult,
|
|
100
94
|
/** Selects mutation loading state. */
|
|
101
|
-
selectMutationLoading
|
|
102
|
-
var _a;
|
|
103
|
-
return (_a = selectMutationState(state, mutation).loading) !== null && _a !== void 0 ? _a : false;
|
|
104
|
-
},
|
|
95
|
+
selectMutationLoading,
|
|
105
96
|
/** Selects mutation latest error. */
|
|
106
|
-
selectMutationError
|
|
107
|
-
return selectMutationState(state, mutation).error;
|
|
108
|
-
},
|
|
97
|
+
selectMutationError,
|
|
109
98
|
/** Selects mutation latest params. */
|
|
110
|
-
selectMutationParams
|
|
111
|
-
return selectMutationState(state, mutation).params;
|
|
112
|
-
},
|
|
99
|
+
selectMutationParams,
|
|
113
100
|
/** Selects entity by id and typename. */
|
|
114
101
|
selectEntityById,
|
|
115
102
|
/** Selects all entities. */
|
|
116
|
-
selectEntities
|
|
117
|
-
return cache.cacheStateSelector(state).entities;
|
|
118
|
-
},
|
|
103
|
+
selectEntities,
|
|
119
104
|
/** Selects all entities of provided typename. */
|
|
120
|
-
selectEntitiesByTypename
|
|
121
|
-
return cache.cacheStateSelector(state).entities[typename];
|
|
122
|
-
},
|
|
105
|
+
selectEntitiesByTypename,
|
|
123
106
|
},
|
|
124
107
|
hooks: {
|
|
125
108
|
/** Returns client object with query and mutate functions. */
|
|
@@ -133,12 +116,12 @@ const withTypenames = () => {
|
|
|
133
116
|
const getCacheKey = (_a = cache.queries[queryKey].getCacheKey) !== null && _a !== void 0 ? _a : (utilsAndConstants_1.defaultGetCacheKey);
|
|
134
117
|
// @ts-expect-error fix later
|
|
135
118
|
const cacheKey = getCacheKey(params);
|
|
136
|
-
return (0, query_1.query)('query', store, cache, actions, queryKey, cacheKey, params, options.secondsToLive, options.onlyIfExpired,
|
|
119
|
+
return (0, query_1.query)('query', store, cache, actions, selectors, queryKey, cacheKey, params, options.secondsToLive, options.onlyIfExpired,
|
|
137
120
|
// @ts-expect-error fix later
|
|
138
121
|
options.mergeResults, options.onCompleted, options.onSuccess, options.onError);
|
|
139
122
|
},
|
|
140
123
|
mutate: (options) => {
|
|
141
|
-
return (0, mutate_1.mutate)('mutate', store, cache, actions, options.mutation, options.params, abortControllers,
|
|
124
|
+
return (0, mutate_1.mutate)('mutate', store, cache, actions, selectors, options.mutation, options.params, abortControllers,
|
|
142
125
|
// @ts-expect-error fix later
|
|
143
126
|
options.onCompleted, options.onSuccess, options.onError);
|
|
144
127
|
},
|
|
@@ -147,21 +130,17 @@ const withTypenames = () => {
|
|
|
147
130
|
}, [store]);
|
|
148
131
|
},
|
|
149
132
|
/** Fetches query when params change and subscribes to query state changes (except `expiresAt` field). */
|
|
150
|
-
useQuery: (options) => (0, useQuery_1.useQuery)(cache, actions, options),
|
|
133
|
+
useQuery: (options) => (0, useQuery_1.useQuery)(cache, actions, selectors, options),
|
|
151
134
|
/** Subscribes to provided mutation state and provides mutate function. */
|
|
152
|
-
useMutation: (options
|
|
153
|
-
// @ts-expect-error cache type
|
|
154
|
-
) => (0, useMutation_1.useMutation)(cache, actions, options, abortControllers),
|
|
135
|
+
useMutation: (options) => (0, useMutation_1.useMutation)(cache, actions, selectors, options, abortControllers),
|
|
155
136
|
/** useSelector + selectEntityById. */
|
|
156
137
|
useSelectEntityById: (id, typename) => {
|
|
157
138
|
return (0, react_redux_1.useSelector)((state) => selectEntityById(state, id, typename));
|
|
158
139
|
},
|
|
159
140
|
},
|
|
160
141
|
utils: {
|
|
161
|
-
/**
|
|
162
|
-
*
|
|
163
|
-
* @return `undefined` if nothing to change, otherwise new entities map with applied changes.
|
|
164
|
-
*/
|
|
142
|
+
/** Apply changes to the entities map.
|
|
143
|
+
* @returns `undefined` if nothing to change, otherwise new `EntitiesMap<T>` with applied changes. */
|
|
165
144
|
applyEntityChanges: (entities, changes) => {
|
|
166
145
|
return (0, utilsAndConstants_1.applyEntityChanges)(entities, changes, cache.options);
|
|
167
146
|
},
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Actions } 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:
|
|
4
|
+
export declare const createCacheReducer: <N extends string, T extends Typenames, QP, QR, MP, MR>(actions: Actions<N, T, QP, QR, MP, MR>, queryKeys: (keyof (QP | QR))[], cacheOptions: CacheOptions) => (state: {
|
|
5
5
|
entities: EntitiesMap<T>;
|
|
6
6
|
queries: { [QK in keyof (QP | QR)]: Dict<QueryState<QP[QK], QR[QK]> | undefined>; };
|
|
7
7
|
mutations: { [MK in keyof (MP | MR)]: MutationState<MP[MK], MR[MK]>; };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Cache, Key, MutationState, QueryState, Typenames } from './types';
|
|
2
|
+
export type Selectors<N extends string = string, T extends Typenames = Typenames, QP = unknown, QR = unknown, MP = unknown, MR = unknown> = ReturnType<typeof createSelectors<N, T, QP, QR, MP, MR>>;
|
|
3
|
+
export declare const createSelectors: <N extends string, T extends Typenames, QP, QR, MP, MR>(cache: Cache<N, T, QP, QR, MP, MR>) => {
|
|
4
|
+
selectQueryState: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => QueryState<QK extends keyof (QP | QR) ? QP[QK] : never, QK extends keyof (QP | QR) ? QR[QK] : never>;
|
|
5
|
+
selectQueryResult: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => (QK extends keyof QP & keyof QR ? QR[QK] : never) | undefined;
|
|
6
|
+
selectQueryLoading: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => boolean;
|
|
7
|
+
selectQueryError: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => Error | undefined;
|
|
8
|
+
selectQueryParams: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => (QK extends keyof QP & keyof QR ? QP[QK] : never) | undefined;
|
|
9
|
+
selectQueryExpiresAt: <QK extends keyof (QP & QR)>(state: unknown, query: QK, cacheKey: Key) => number | undefined;
|
|
10
|
+
selectMutationState: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => MutationState<MK extends keyof (MP | MR) ? MP[MK] : never, MK extends keyof (MP | MR) ? MR[MK] : never>;
|
|
11
|
+
selectMutationResult: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => (MK extends keyof MP & keyof MR ? MR[MK] : never) | undefined;
|
|
12
|
+
selectMutationLoading: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => boolean;
|
|
13
|
+
selectMutationError: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => Error | undefined;
|
|
14
|
+
selectMutationParams: <MK extends keyof (MP & MR)>(state: unknown, mutation: MK) => (MK extends keyof MP & keyof MR ? MP[MK] : never) | undefined;
|
|
15
|
+
selectEntityById: <TN extends keyof T>(state: unknown, id: Key | null | undefined, typename: TN) => T[TN] | undefined;
|
|
16
|
+
selectEntities: (state: unknown) => import("./types").EntitiesMap<T>;
|
|
17
|
+
selectEntitiesByTypename: <TN extends keyof T>(state: unknown, typename: TN) => import("./types").EntitiesMap<T>[TN];
|
|
18
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSelectors = void 0;
|
|
4
|
+
const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
5
|
+
const createSelectors = (cache) => {
|
|
6
|
+
const selectEntityById = (state, id, typename) => {
|
|
7
|
+
var _a;
|
|
8
|
+
return id == null ? undefined : (_a = cache.cacheStateSelector(state).entities[typename]) === null || _a === void 0 ? void 0 : _a[id];
|
|
9
|
+
};
|
|
10
|
+
const selectQueryState = (state, query, cacheKey) => {
|
|
11
|
+
var _a;
|
|
12
|
+
// @ts-expect-error fix later
|
|
13
|
+
return (_a = cache.cacheStateSelector(state).queries[query][cacheKey]) !== null && _a !== void 0 ? _a : utilsAndConstants_1.EMPTY_OBJECT;
|
|
14
|
+
};
|
|
15
|
+
const selectMutationState = (state, mutation) => {
|
|
16
|
+
var _a;
|
|
17
|
+
// @ts-expect-error fix later
|
|
18
|
+
return (_a = cache.cacheStateSelector(state).mutations[mutation]) !== null && _a !== void 0 ? _a : utilsAndConstants_1.EMPTY_OBJECT;
|
|
19
|
+
};
|
|
20
|
+
return {
|
|
21
|
+
selectQueryState,
|
|
22
|
+
selectQueryResult: (state, query, cacheKey) => {
|
|
23
|
+
return selectQueryState(state, query, cacheKey).result;
|
|
24
|
+
},
|
|
25
|
+
selectQueryLoading: (state, query, cacheKey) => {
|
|
26
|
+
var _a;
|
|
27
|
+
return (_a = selectQueryState(state, query, cacheKey).loading) !== null && _a !== void 0 ? _a : false;
|
|
28
|
+
},
|
|
29
|
+
selectQueryError: (state, query, cacheKey) => {
|
|
30
|
+
return selectQueryState(state, query, cacheKey).error;
|
|
31
|
+
},
|
|
32
|
+
selectQueryParams: (state, query, cacheKey) => {
|
|
33
|
+
return selectQueryState(state, query, cacheKey).params;
|
|
34
|
+
},
|
|
35
|
+
selectQueryExpiresAt: (state, query, cacheKey) => {
|
|
36
|
+
return selectQueryState(state, query, cacheKey).expiresAt;
|
|
37
|
+
},
|
|
38
|
+
selectMutationState,
|
|
39
|
+
selectMutationResult: (state, mutation) => {
|
|
40
|
+
return selectMutationState(state, mutation).result;
|
|
41
|
+
},
|
|
42
|
+
selectMutationLoading: (state, mutation) => {
|
|
43
|
+
var _a;
|
|
44
|
+
return (_a = selectMutationState(state, mutation).loading) !== null && _a !== void 0 ? _a : false;
|
|
45
|
+
},
|
|
46
|
+
selectMutationError: (state, mutation) => {
|
|
47
|
+
return selectMutationState(state, mutation).error;
|
|
48
|
+
},
|
|
49
|
+
selectMutationParams: (state, mutation) => {
|
|
50
|
+
return selectMutationState(state, mutation).params;
|
|
51
|
+
},
|
|
52
|
+
selectEntityById,
|
|
53
|
+
selectEntities: (state) => {
|
|
54
|
+
return cache.cacheStateSelector(state).entities;
|
|
55
|
+
},
|
|
56
|
+
selectEntitiesByTypename: (state, typename) => {
|
|
57
|
+
return cache.cacheStateSelector(state).entities[typename];
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
exports.createSelectors = createSelectors;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { createCache, withTypenames } from './createCache';
|
|
2
|
-
export type { ReduxCacheState } from './createCacheReducer';
|
|
3
2
|
export * from './types';
|
|
4
|
-
export {
|
|
3
|
+
export { useQuerySelectorStateComparer } from './useQuery';
|
|
4
|
+
export { defaultGetCacheKey, FetchPolicy, isEmptyObject } from './utilsAndConstants';
|
package/dist/index.js
CHANGED
|
@@ -14,25 +14,31 @@ 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.defaultGetCacheKey = exports.withTypenames = exports.createCache = void 0;
|
|
17
|
+
exports.isEmptyObject = exports.FetchPolicy = exports.defaultGetCacheKey = exports.useQuerySelectorStateComparer = 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
|
+
var useQuery_1 = require("./useQuery");
|
|
23
|
+
Object.defineProperty(exports, "useQuerySelectorStateComparer", { enumerable: true, get: function () { return useQuery_1.useQuerySelectorStateComparer; } });
|
|
22
24
|
var utilsAndConstants_1 = require("./utilsAndConstants");
|
|
23
25
|
Object.defineProperty(exports, "defaultGetCacheKey", { enumerable: true, get: function () { return utilsAndConstants_1.defaultGetCacheKey; } });
|
|
26
|
+
Object.defineProperty(exports, "FetchPolicy", { enumerable: true, get: function () { return utilsAndConstants_1.FetchPolicy; } });
|
|
27
|
+
Object.defineProperty(exports, "isEmptyObject", { enumerable: true, get: function () { return utilsAndConstants_1.isEmptyObject; } });
|
|
24
28
|
// Backlog
|
|
25
|
-
// ! high (1.0.0)
|
|
26
|
-
// remove cachePolicy? make skip/enabled a function? skip -> enabled/shouldFetch?
|
|
29
|
+
// ! high (1.0.0-rc.0)
|
|
27
30
|
// generate full api docs
|
|
28
31
|
// ! medium
|
|
29
32
|
// optimistic response
|
|
33
|
+
// onCancel & onAbort
|
|
34
|
+
// remove empty entities and queries from state
|
|
30
35
|
// reset [whole] cache to initial / to provided state
|
|
31
36
|
// globals for success, completions and loading states?
|
|
32
37
|
// make query key / cache key difference more clear in the docs
|
|
33
38
|
// check type of function arguments in dev
|
|
39
|
+
// make skipFetch a function?
|
|
40
|
+
// example -> playground with changable options
|
|
34
41
|
// ! low
|
|
35
|
-
// local cache policy to keep in component state?
|
|
36
42
|
// make error type generic
|
|
37
43
|
// allow multiple mutation with same keys?
|
|
38
44
|
// custom useStore & useSelector to support multiple stores?
|
package/dist/mutate.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Store } from 'redux';
|
|
2
|
-
import type {
|
|
2
|
+
import type { Actions } from './createActions';
|
|
3
|
+
import { Selectors } from './createSelectors';
|
|
3
4
|
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 & MR)>(logTag: string, store: Store, cache: Cache<N, T, QP, QR, MP, MR>,
|
|
5
|
+
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>, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>, 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, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => 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, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => 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, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | undefined, onSuccess?: ((response: import("./types").NormalizedQueryResponse<T, MR[keyof MP & keyof MR & string]>, params: MP[keyof MP & keyof MR & string] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | ((response: import("./types").NormalizedQueryResponse<T, MR[keyof MP & keyof MR & number]>, params: MP[keyof MP & keyof MR & number] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | ((response: import("./types").NormalizedQueryResponse<T, MR[keyof MP & keyof MR & symbol]>, params: MP[keyof MP & keyof MR & symbol] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | undefined, onError?: ((error: unknown, params: MP[keyof MP & keyof MR & string] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean | void | null | undefined) | ((error: unknown, params: MP[keyof MP & keyof MR & number] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean | void | null | undefined) | ((error: unknown, params: MP[keyof MP & keyof MR & symbol] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean | void | null | undefined) | undefined) => Promise<MutationResult<MK extends keyof (MP | MR) ? MR[MK] : never>>;
|
package/dist/mutate.js
CHANGED
|
@@ -11,8 +11,9 @@ 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_1, store_1, cache_1,
|
|
15
|
-
var
|
|
14
|
+
const mutate = (logTag_1, store_1, cache_1, actions_1, selectors_1, mutationKey_1, params_1, abortControllers_1, ...args_1) => __awaiter(void 0, [logTag_1, store_1, cache_1, actions_1, selectors_1, mutationKey_1, params_1, abortControllers_1, ...args_1], void 0, function* (logTag, store, cache, actions, selectors, mutationKey, params, abortControllers, onCompleted = cache.mutations[mutationKey].onCompleted, onSuccess = cache.mutations[mutationKey].onSuccess, onError = cache.mutations[mutationKey].onError) {
|
|
15
|
+
var _a, _b;
|
|
16
|
+
const { updateMutationStateAndEntities } = actions;
|
|
16
17
|
let abortControllersOfStore = abortControllers.get(store);
|
|
17
18
|
if (abortControllersOfStore === undefined) {
|
|
18
19
|
abortControllersOfStore = {};
|
|
@@ -66,12 +67,14 @@ const mutate = (logTag_1, store_1, cache_1, _a, mutationKey_1, params_1, abortCo
|
|
|
66
67
|
loading: false,
|
|
67
68
|
}));
|
|
68
69
|
// @ts-expect-error params
|
|
69
|
-
if (!(onError === null || onError === void 0 ? void 0 : onError(error, params, store))) {
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
if (!(onError === null || onError === void 0 ? void 0 : onError(error, params, store, actions, selectors))) {
|
|
71
|
+
(_b = (_a = cache.globals).onError) === null || _b === void 0 ? void 0 : _b.call(_a, error,
|
|
72
|
+
// @ts-expect-error mutationKey
|
|
73
|
+
mutationKey, params, store, actions, selectors);
|
|
72
74
|
}
|
|
75
|
+
onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(
|
|
73
76
|
// @ts-expect-error response
|
|
74
|
-
|
|
77
|
+
response, error, params, store, actions, selectors);
|
|
75
78
|
return { error };
|
|
76
79
|
}
|
|
77
80
|
if (response) {
|
|
@@ -81,10 +84,12 @@ const mutate = (logTag_1, store_1, cache_1, _a, mutationKey_1, params_1, abortCo
|
|
|
81
84
|
result: response.result,
|
|
82
85
|
};
|
|
83
86
|
store.dispatch(updateMutationStateAndEntities(mutationKey, newState, response));
|
|
84
|
-
|
|
85
|
-
|
|
87
|
+
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(
|
|
88
|
+
// @ts-expect-error response
|
|
89
|
+
response, params, store, actions, selectors);
|
|
90
|
+
onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(
|
|
86
91
|
// @ts-expect-error response
|
|
87
|
-
|
|
92
|
+
response, error, params, store, actions, selectors);
|
|
88
93
|
// @ts-expect-error fix later
|
|
89
94
|
return { result: response.result };
|
|
90
95
|
}
|
package/dist/query.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Store } from 'redux';
|
|
2
|
-
import type {
|
|
2
|
+
import type { Actions } from './createActions';
|
|
3
|
+
import { Selectors } from './createSelectors';
|
|
3
4
|
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 & QR)>(logTag: string, store: Store, cache: Cache<N, T, QP, QR, MP, MR>,
|
|
5
|
+
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>, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>, 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, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => 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, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => 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, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => 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, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => 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, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => 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, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | undefined, onSuccess?: ((response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & string]>, params: QP[keyof QP & keyof QR & string] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | ((response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & number]>, params: QP[keyof QP & keyof QR & number] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | ((response: import("./types").NormalizedQueryResponse<T, QR[keyof QP & keyof QR & symbol]>, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => void) | undefined, onError?: ((error: unknown, params: QP[keyof QP & keyof QR & string] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean | void | null | undefined) | ((error: unknown, params: QP[keyof QP & keyof QR & number] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean | void | null | undefined) | ((error: unknown, params: QP[keyof QP & keyof QR & symbol] | undefined, store: Store, actions: Actions<N, T, QP, QR, MP, MR>, selectors: Selectors<N, T, QP, QR, MP, MR>) => boolean | void | null | undefined) | undefined) => Promise<QueryResult<QK extends keyof (QP | QR) ? QR[QK] : never>>;
|
package/dist/query.js
CHANGED
|
@@ -11,10 +11,9 @@ 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,
|
|
15
|
-
var _b, _c, _d, _e
|
|
16
|
-
|
|
17
|
-
if (secondsToLive === void 0) { secondsToLive = (_b = cache.queries[queryKey].secondsToLive) !== null && _b !== void 0 ? _b : cache.globals.secondsToLive; }
|
|
14
|
+
const query = (logTag, store, cache, actions, selectors, queryKey, cacheKey, params, secondsToLive, onlyIfExpired, mergeResults, onCompleted, onSuccess, onError) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
+
var _a, _b, _c, _d, _e;
|
|
16
|
+
if (secondsToLive === void 0) { secondsToLive = (_a = cache.queries[queryKey].secondsToLive) !== null && _a !== void 0 ? _a : cache.globals.queries.secondsToLive; }
|
|
18
17
|
if (mergeResults === void 0) { mergeResults = cache.queries[queryKey].mergeResults; }
|
|
19
18
|
if (onCompleted === void 0) { onCompleted = cache.queries[queryKey].onCompleted; }
|
|
20
19
|
if (onSuccess === void 0) { onSuccess = cache.queries[queryKey].onSuccess; }
|
|
@@ -41,6 +40,7 @@ const query = (logTag, store, cache, _a, queryKey, cacheKey, params, secondsToLi
|
|
|
41
40
|
});
|
|
42
41
|
return CANCELLED_RESULT;
|
|
43
42
|
}
|
|
43
|
+
const { updateQueryStateAndEntities } = actions;
|
|
44
44
|
store.dispatch(updateQueryStateAndEntities(queryKey, cacheKey, {
|
|
45
45
|
loading: true,
|
|
46
46
|
params,
|
|
@@ -60,28 +60,32 @@ const query = (logTag, store, cache, _a, queryKey, cacheKey, params, secondsToLi
|
|
|
60
60
|
}));
|
|
61
61
|
// @ts-expect-error params
|
|
62
62
|
if (!(onError === null || onError === void 0 ? void 0 : onError(error, params, store))) {
|
|
63
|
+
(_c = (_b = cache.globals).onError) === null || _c === void 0 ? void 0 : _c.call(_b, error,
|
|
63
64
|
// @ts-expect-error queryKey
|
|
64
|
-
|
|
65
|
+
queryKey, params, store, actions, selectors);
|
|
65
66
|
}
|
|
67
|
+
onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(undefined, error,
|
|
66
68
|
// @ts-expect-error params
|
|
67
|
-
|
|
69
|
+
params, store, actions, selectors);
|
|
68
70
|
return { error };
|
|
69
71
|
}
|
|
70
72
|
const newState = {
|
|
71
73
|
error: undefined,
|
|
72
74
|
loading: false,
|
|
73
|
-
expiresAt: (
|
|
75
|
+
expiresAt: (_d = response.expiresAt) !== null && _d !== void 0 ? _d : (secondsToLive != null ? Date.now() + secondsToLive * 1000 : undefined),
|
|
74
76
|
result: mergeResults
|
|
75
77
|
? mergeResults(
|
|
76
78
|
// @ts-expect-error fix later
|
|
77
|
-
(
|
|
79
|
+
(_e = cacheStateSelector(store.getState()).queries[queryKey][cacheKey]) === null || _e === void 0 ? void 0 : _e.result, response, params, store, actions, selectors)
|
|
78
80
|
: response.result,
|
|
79
81
|
};
|
|
80
82
|
store.dispatch(updateQueryStateAndEntities(queryKey, cacheKey, newState, response));
|
|
83
|
+
onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(
|
|
81
84
|
// @ts-expect-error response
|
|
82
|
-
|
|
85
|
+
response, params, store, actions, selectors);
|
|
86
|
+
onCompleted === null || onCompleted === void 0 ? void 0 : onCompleted(
|
|
83
87
|
// @ts-expect-error response
|
|
84
|
-
|
|
88
|
+
response, undefined, params, store, actions, selectors);
|
|
85
89
|
return {
|
|
86
90
|
// @ts-expect-error fix types
|
|
87
91
|
result: newState === null || newState === void 0 ? void 0 : newState.result,
|