@hyper-fetch/react 9.7.0 → 9.8.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 +1 -1
- 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/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/index.mjs +14 -4
- 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/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.1",
|
|
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
|
|
|
@@ -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
|
+
};
|