@hyper-fetch/react 9.6.2 → 9.8.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/__tests__/features/use-cache/use-cache.base.spec.ts +10 -0
- package/__tests__/features/use-fetch/use-fetch.payload.spec.ts +74 -0
- package/__tests__/features/use-infinite-fetch/use-infinite-fetch.key.spec.ts +39 -0
- package/__tests__/features/use-submit/use-submit.optimistic-helpers.spec.tsx +455 -0
- package/dist/hooks/use-cache/use-cache.hooks.d.ts.map +1 -1
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts.map +1 -1
- package/dist/hooks/use-infinite-fetch/use-infinite-fetch.utils.d.ts.map +1 -1
- package/dist/hooks/use-submit/use-submit.constants.d.ts +1 -1
- package/dist/hooks/use-submit/use-submit.constants.d.ts.map +1 -1
- package/dist/hooks/use-submit/use-submit.hooks.d.ts.map +1 -1
- package/dist/hooks/use-submit/use-submit.types.d.ts +6 -1
- package/dist/hooks/use-submit/use-submit.types.d.ts.map +1 -1
- package/dist/index.mjs +18 -5
- package/dist/index.mjs.map +1 -1
- package/dist/utils/key.utils.d.ts +7 -0
- package/dist/utils/key.utils.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/hooks/use-cache/use-cache.hooks.ts +2 -1
- package/src/hooks/use-fetch/use-fetch.hooks.ts +2 -1
- package/src/hooks/use-infinite-fetch/use-infinite-fetch.utils.ts +3 -2
- package/src/hooks/use-submit/use-submit.constants.ts +3 -1
- package/src/hooks/use-submit/use-submit.hooks.ts +5 -1
- package/src/hooks/use-submit/use-submit.types.ts +6 -0
- package/src/utils/key.utils.ts +8 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `JSON.stringify` replacer for the keys the hooks build from a request.
|
|
3
|
+
* Adapters with their own serializers accept `BigInt` payloads, which plain JSON can't encode.
|
|
4
|
+
* They are kept in the key and stay distinct from a string with the same digits.
|
|
5
|
+
*/
|
|
6
|
+
export declare const keyReplacer: (_key: string, value: unknown) => unknown;
|
|
7
|
+
//# sourceMappingURL=key.utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key.utils.d.ts","sourceRoot":"","sources":["../../src/utils/key.utils.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,eAAO,MAAM,WAAW,GAAI,MAAM,MAAM,EAAE,OAAO,OAAO,YAEvD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/react",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.8.0",
|
|
4
4
|
"description": "React hooks and utils for the hyper-fetch",
|
|
5
5
|
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
6
|
"homepage": "https://hyperfetch.bettertyped.com/",
|
|
@@ -7,6 +7,7 @@ import { useCacheDefaultOptions } from "hooks/use-cache";
|
|
|
7
7
|
import { useProvider } from "provider";
|
|
8
8
|
import { useRef } from "react";
|
|
9
9
|
import { createTrackedProxy } from "utils";
|
|
10
|
+
import { keyReplacer } from "utils/key.utils";
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* Options passed as `undefined` (e.g. forwarded optional props) fall back to the defaults instead of overriding them
|
|
@@ -33,7 +34,7 @@ export const useCache = <T extends RequestInstance, TSelected = ExtractResponseT
|
|
|
33
34
|
const { cache, loggerManager } = client;
|
|
34
35
|
const logger = useRef(loggerManager.initialize(client, "useCache")).current;
|
|
35
36
|
const [dispatcher] = getRequestDispatcher(request);
|
|
36
|
-
const updateKey = JSON.stringify(request.toJSON());
|
|
37
|
+
const updateKey = JSON.stringify(request.toJSON(), keyReplacer);
|
|
37
38
|
|
|
38
39
|
// Build the configuration options
|
|
39
40
|
const { config: globalConfig } = useProvider();
|
|
@@ -16,6 +16,7 @@ import { useFetchDefaultOptions } from "hooks/use-fetch";
|
|
|
16
16
|
import { useProvider } from "provider";
|
|
17
17
|
import { useCallback, useRef } from "react";
|
|
18
18
|
import { createTrackedProxy, getBounceData } from "utils";
|
|
19
|
+
import { keyReplacer } from "utils/key.utils";
|
|
19
20
|
import { isSameScope } from "utils/scope.utils";
|
|
20
21
|
|
|
21
22
|
type SuspenseEntry = { promise: Promise<void>; resolve: () => void; cleanup: () => void };
|
|
@@ -101,7 +102,7 @@ export const useFetch = <R extends RequestInstance, TSelected = ExtractResponseT
|
|
|
101
102
|
options,
|
|
102
103
|
);
|
|
103
104
|
|
|
104
|
-
const updateKey = JSON.stringify(request.toJSON());
|
|
105
|
+
const updateKey = JSON.stringify(request.toJSON(), keyReplacer);
|
|
105
106
|
const requestDebounce = useDebounce({ delay: bounceTime });
|
|
106
107
|
const requestThrottle = useThrottle({ interval: bounceTime, timeout: bounceTimeout });
|
|
107
108
|
const refreshDebounce = useDebounce({ delay: refreshTime });
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ClientInstance, RequestInstance } from "@hyper-fetch/core";
|
|
2
|
+
import { keyReplacer } from "utils/key.utils";
|
|
2
3
|
|
|
3
4
|
import type { InfiniteStore } from "./use-infinite-fetch.store";
|
|
4
5
|
import type { InfiniteSnapshot } from "./use-infinite-fetch.store.types";
|
|
@@ -8,8 +9,8 @@ import type { InfiniteSnapshot } from "./use-infinite-fetch.store.types";
|
|
|
8
9
|
* or URL param starts a new list without anything having to be listed by hand.
|
|
9
10
|
*/
|
|
10
11
|
export const getInfiniteKey = (request: RequestInstance, pagination: unknown, maxPages?: number) => {
|
|
11
|
-
return JSON.stringify([request.toJSON(), pagination, maxPages ?? null], (
|
|
12
|
-
return typeof value === "function" ? value.toString() : value;
|
|
12
|
+
return JSON.stringify([request.toJSON(), pagination, maxPages ?? null], (key, value) => {
|
|
13
|
+
return typeof value === "function" ? value.toString() : keyReplacer(key, value);
|
|
13
14
|
});
|
|
14
15
|
};
|
|
15
16
|
|
|
@@ -2,7 +2,9 @@ import type { RequestInstance, RequiredKeys } from "@hyper-fetch/core";
|
|
|
2
2
|
|
|
3
3
|
import type { UseSubmitOptionsType } from "./use-submit.types";
|
|
4
4
|
|
|
5
|
-
type DefaultOptionsType = RequiredKeys<
|
|
5
|
+
type DefaultOptionsType = RequiredKeys<
|
|
6
|
+
Omit<UseSubmitOptionsType<RequestInstance>, "initialResponse" | "select" | "optimistic">
|
|
7
|
+
> & {
|
|
6
8
|
initialResponse: null;
|
|
7
9
|
};
|
|
8
10
|
|
|
@@ -151,7 +151,10 @@ export const useSubmit = <RequestType extends RequestInstance, TSelected = Extra
|
|
|
151
151
|
return Promise.resolve(getNotSentResponse("Cannot submit request. Option 'disabled' is enabled"));
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
-
const
|
|
154
|
+
const { optimistic: submitOptimistic, ...cloneOptions } = submitOptions || {};
|
|
155
|
+
const requestClone = request.clone(cloneOptions as any) as unknown as RequestType;
|
|
156
|
+
// Read directly from options - functions are not part of the memoized options key
|
|
157
|
+
const optimistic = submitOptimistic ?? options?.optimistic;
|
|
155
158
|
|
|
156
159
|
const triggerRequest = () => {
|
|
157
160
|
addCacheDataListener(requestClone);
|
|
@@ -159,6 +162,7 @@ export const useSubmit = <RequestType extends RequestInstance, TSelected = Extra
|
|
|
159
162
|
const configuration: RequestSendOptionsType<RequestType> = {
|
|
160
163
|
dispatcherType: "submit",
|
|
161
164
|
...(submitOptions as RequestSendOptionsType<RequestType>),
|
|
165
|
+
optimistic,
|
|
162
166
|
onBeforeSent: (data) => {
|
|
163
167
|
// Deduplicated submit joins the request which is already in flight and shares its id.
|
|
164
168
|
// We keep a single set of listeners for it, so the callbacks fire once per response.
|
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
ExtractAdapterResolvedType,
|
|
3
3
|
ExtractResponseType,
|
|
4
4
|
NullableType,
|
|
5
|
+
OptimisticSendOptionType,
|
|
5
6
|
RequestInstance,
|
|
6
7
|
RequestSendType,
|
|
7
8
|
} from "@hyper-fetch/core";
|
|
@@ -47,6 +48,11 @@ export type UseSubmitOptionsType<T extends RequestInstance, TSelected = ExtractR
|
|
|
47
48
|
* Keep references of the unchanged parts of the selected data between updates (default true).
|
|
48
49
|
*/
|
|
49
50
|
structuralSharing?: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Optimistic update of every submit from this hook - `optimistic()` operations or a callback like the one of
|
|
53
|
+
* `setOptimistic`. Replaces the request's own optimistic callback; `submit({ optimistic })` replaces this one.
|
|
54
|
+
*/
|
|
55
|
+
optimistic?: OptimisticSendOptionType<T>;
|
|
50
56
|
} & (
|
|
51
57
|
| {
|
|
52
58
|
/**
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `JSON.stringify` replacer for the keys the hooks build from a request.
|
|
3
|
+
* Adapters with their own serializers accept `BigInt` payloads, which plain JSON can't encode.
|
|
4
|
+
* They are kept in the key and stay distinct from a string with the same digits.
|
|
5
|
+
*/
|
|
6
|
+
export const keyReplacer = (_key: string, value: unknown) => {
|
|
7
|
+
return typeof value === "bigint" ? { "~bigint": value.toString() } : value;
|
|
8
|
+
};
|