react-redux-cache 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -7
- package/dist/createCache.d.ts +3 -1
- package/dist/index.js +1 -1
- package/dist/useQuery.d.ts +3 -1
- package/dist/useQuery.js +7 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -122,7 +122,7 @@ Examples of states, generated by cache reducer from `/example` project:
|
|
|
122
122
|
- [api.ts](https://github.com/gentlee/react-redux-cache#apits)
|
|
123
123
|
- [Usage](https://github.com/gentlee/react-redux-cache#usage)
|
|
124
124
|
- [Advanced](https://github.com/gentlee/react-redux-cache#advanced)
|
|
125
|
-
- [
|
|
125
|
+
- [Advanced cache policy](https://github.com/gentlee/react-redux-cache#advanced-cache-policy)
|
|
126
126
|
- [Infinite scroll pagination](https://github.com/gentlee/react-redux-cache#infinite-scroll-pagination)
|
|
127
127
|
- [redux-persist](https://github.com/gentlee/react-redux-cache#redux-persist)
|
|
128
128
|
- [FAQ](https://github.com/gentlee/react-redux-cache#faq)
|
|
@@ -248,7 +248,7 @@ export const UserScreen = () => {
|
|
|
248
248
|
|
|
249
249
|
### Advanced
|
|
250
250
|
|
|
251
|
-
#### Advanced cache policy
|
|
251
|
+
#### Advanced cache policy
|
|
252
252
|
|
|
253
253
|
`cache-first` cache policy skips fetching if result is already cached, but sometimes it can't determine that we already have result in some other's query result or in normalized entities cache. In that case we can use `skip` parameter of a query:
|
|
254
254
|
|
|
@@ -327,9 +327,7 @@ Here is an example of `getUsers` query configuration with pagination support. Yo
|
|
|
327
327
|
// Component
|
|
328
328
|
|
|
329
329
|
export const GetUsersScreen = () => {
|
|
330
|
-
const {
|
|
331
|
-
|
|
332
|
-
const [{result: usersResult, loading, error, params}, refetch] = useQuery({
|
|
330
|
+
const [{result: usersResult, loading, error, params}, fetchUsers] = useQuery({
|
|
333
331
|
query: 'getUsers',
|
|
334
332
|
params: 1 // page
|
|
335
333
|
})
|
|
@@ -339,7 +337,7 @@ export const GetUsersScreen = () => {
|
|
|
339
337
|
|
|
340
338
|
const onLoadNextPage = () => {
|
|
341
339
|
const lastLoadedPage = usersResult?.page ?? 0
|
|
342
|
-
|
|
340
|
+
fetchUsers({
|
|
343
341
|
query: 'getUsers',
|
|
344
342
|
params: lastLoadedPage + 1,
|
|
345
343
|
})
|
|
@@ -355,7 +353,7 @@ export const GetUsersScreen = () => {
|
|
|
355
353
|
<div>
|
|
356
354
|
{refreshing && <div className="spinner" />}
|
|
357
355
|
{usersResult?.items.map(renderUser)}
|
|
358
|
-
<button onClick={
|
|
356
|
+
<button onClick={() => fetchUsers()}>Refresh</button>
|
|
359
357
|
{loadingNextPage ? (
|
|
360
358
|
<div className="spinner" />
|
|
361
359
|
) : (
|
package/dist/createCache.d.ts
CHANGED
|
@@ -127,7 +127,9 @@ export declare const createCache: <N extends string, T extends Typenames, QP, QR
|
|
|
127
127
|
}) => Promise<MutationResult<MK_6 extends keyof MP & keyof MR ? MR[MK_6] : never>>;
|
|
128
128
|
};
|
|
129
129
|
/** Fetches query when params change and subscribes to query state. */
|
|
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>, (
|
|
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>, (options?: {
|
|
131
|
+
params: QK_7 extends keyof QP & keyof QR ? QP[QK_7] : never;
|
|
132
|
+
} | undefined) => 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>>];
|
|
131
133
|
/** Subscribes to provided mutation state and provides mutate function. */
|
|
132
134
|
useMutation: <MK_7 extends keyof MP | keyof MR>(options: {
|
|
133
135
|
mutation: MK_7;
|
package/dist/index.js
CHANGED
|
@@ -23,7 +23,7 @@ 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
|
+
// add full api docs
|
|
27
27
|
// update readme with how to use mergeResults for invalidating / updating caches?
|
|
28
28
|
// optimistic response
|
|
29
29
|
// make query key / cache key difference more clear in the docs, and/or rename queryKey -> query
|
package/dist/useQuery.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
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, 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>, (
|
|
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>, (options?: {
|
|
4
|
+
params: QK extends keyof QP & keyof QR ? QP[QK] : never;
|
|
5
|
+
} | undefined) => 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
|
@@ -23,10 +23,13 @@ const useQuery = (cache, actions, options) => {
|
|
|
23
23
|
const store = (0, react_redux_1.useStore)();
|
|
24
24
|
// @ts-expect-error fix types later
|
|
25
25
|
const cacheKey = getCacheKey(params);
|
|
26
|
-
const fetch = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
|
-
return yield (0, query_1.query)('useQuery.fetch', store, cache, actions, queryKey,
|
|
28
|
-
//
|
|
29
|
-
|
|
26
|
+
const fetch = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
|
+
return yield (0, query_1.query)('useQuery.fetch', store, cache, actions, queryKey,
|
|
28
|
+
// @ts-expect-error fix later
|
|
29
|
+
options ? getCacheKey(options.params) : cacheKey, options ? options.params : params);
|
|
30
|
+
}),
|
|
31
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
32
|
+
[store, queryKey, cacheKey]);
|
|
30
33
|
const queryState = (_c = (0, react_redux_1.useSelector)((state) => {
|
|
31
34
|
const queryState = cacheStateSelector(state).queries[queryKey][cacheKey];
|
|
32
35
|
return queryState; // TODO proper type
|
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.
|
|
5
|
+
"version": "0.5.1",
|
|
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",
|