@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 CHANGED
@@ -127,7 +127,7 @@ const CreateUserForm = () => {
127
127
  const form = new FormData(e.currentTarget);
128
128
  // Pass typed payload — TypeScript ensures the shape matches your request definition
129
129
  submit({
130
- data: { name: form.get("name") as string, email: form.get("email") as string },
130
+ payload: { name: form.get("name") as string, email: form.get("email") as string },
131
131
  });
132
132
  };
133
133
 
@@ -63,6 +63,16 @@ describe("useCache [ Base ]", () => {
63
63
  });
64
64
  });
65
65
  });
66
+ describe("given the payload holds a BigInt", () => {
67
+ it("should read the cache", async () => {
68
+ const bigintRequest = createRequest<{ test: number }, { total: bigint }>().setPayload({ total: 1n });
69
+ client.cache.set(bigintRequest, cacheData);
70
+
71
+ const response = renderUseCache(bigintRequest);
72
+
73
+ await testSuccessState(cacheData.data, response);
74
+ });
75
+ });
66
76
  describe("given cache is empty", () => {
67
77
  describe("when reading the state", () => {
68
78
  it("should return empty state", async () => {
@@ -0,0 +1,74 @@
1
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
2
+ import { waitFor } from "@testing-library/react";
3
+
4
+ import { client, createRequest, renderUseFetch } from "../../utils";
5
+
6
+ const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
7
+
8
+ describe("useFetch [ Payload ]", () => {
9
+ beforeAll(() => {
10
+ startServer();
11
+ });
12
+
13
+ afterEach(() => {
14
+ resetMocks();
15
+ vi.restoreAllMocks();
16
+ });
17
+
18
+ afterAll(() => {
19
+ stopServer();
20
+ });
21
+
22
+ beforeEach(() => {
23
+ client.clear();
24
+ });
25
+
26
+ // Adapters with their own serializers, like oRPC or tRPC with superjson, accept such payloads
27
+ describe("when the payload holds a BigInt", () => {
28
+ it("should fetch the data", async () => {
29
+ const request = createRequest<{ id: number }, { total: bigint }>().setPayload({ total: 1n });
30
+ mockRequest(request, { data: { id: 1 } });
31
+
32
+ const view = renderUseFetch(request);
33
+
34
+ await waitFor(() => expect(view.result.current.data).toStrictEqual({ id: 1 }));
35
+ });
36
+
37
+ it("should fetch again when the BigInt changes", async () => {
38
+ const request = createRequest<{ id: number }, { total: bigint }>().setPayload({ total: 1n });
39
+ mockRequest(request, { data: { id: 1 } });
40
+ const spy = vi.spyOn(client.fetchDispatcher, "add");
41
+
42
+ const view = renderUseFetch(request);
43
+ await waitFor(() => expect(view.result.current.data).toStrictEqual({ id: 1 }));
44
+ view.rerender({ request: request.setPayload({ total: 2n }) });
45
+
46
+ await waitFor(() => expect(spy).toHaveBeenCalledTimes(2));
47
+ expect(spy.mock.calls[1][0].payload).toStrictEqual({ total: 2n });
48
+ });
49
+
50
+ it("should not fetch again when the same BigInt is set again", async () => {
51
+ const request = createRequest<{ id: number }, { total: bigint }>().setPayload({ total: 1n });
52
+ mockRequest(request, { data: { id: 1 } });
53
+ const spy = vi.spyOn(client.fetchDispatcher, "add");
54
+
55
+ const view = renderUseFetch(request);
56
+ await waitFor(() => expect(view.result.current.data).toStrictEqual({ id: 1 }));
57
+ view.rerender({ request: request.setPayload({ total: 1n }) });
58
+
59
+ expect(spy).toHaveBeenCalledOnce();
60
+ });
61
+
62
+ it("should tell a BigInt apart from the same digits in a string", async () => {
63
+ const request = createRequest<{ id: number }, { total: bigint | string }>().setPayload({ total: 1n });
64
+ mockRequest(request, { data: { id: 1 } });
65
+ const spy = vi.spyOn(client.fetchDispatcher, "add");
66
+
67
+ const view = renderUseFetch(request);
68
+ await waitFor(() => expect(view.result.current.data).toStrictEqual({ id: 1 }));
69
+ view.rerender({ request: request.setPayload({ total: "1" }) });
70
+
71
+ await waitFor(() => expect(spy).toHaveBeenCalledTimes(2));
72
+ });
73
+ });
74
+ });
@@ -0,0 +1,39 @@
1
+ import { getInfiniteKey } from "hooks/use-infinite-fetch/use-infinite-fetch.utils";
2
+
3
+ import { createRequest } from "../../utils";
4
+
5
+ describe("useInfiniteFetch [ Key ]", () => {
6
+ const pagination = { initialPageParam: 0 };
7
+
8
+ describe("when the payload holds a BigInt", () => {
9
+ it("should build the key", () => {
10
+ const request = createRequest<unknown, { total: bigint }>().setPayload({ total: 1n });
11
+
12
+ expect(() => getInfiniteKey(request, pagination)).not.toThrow();
13
+ });
14
+
15
+ it("should change the key together with the BigInt", () => {
16
+ const request = createRequest<unknown, { total: bigint | string }>();
17
+
18
+ const one = getInfiniteKey(request.setPayload({ total: 1n }), pagination);
19
+ const two = getInfiniteKey(request.setPayload({ total: 2n }), pagination);
20
+ const digits = getInfiniteKey(request.setPayload({ total: "1" }), pagination);
21
+
22
+ expect(one).toBe(getInfiniteKey(request.setPayload({ total: 1n }), pagination));
23
+ expect(one).not.toBe(two);
24
+ expect(one).not.toBe(digits);
25
+ });
26
+ });
27
+
28
+ describe("when the pagination holds functions", () => {
29
+ it("should change the key together with the function source", () => {
30
+ const request = createRequest();
31
+
32
+ const first = getInfiniteKey(request, { getNextPageParam: () => 1 });
33
+ const second = getInfiniteKey(request, { getNextPageParam: () => 2 });
34
+
35
+ expect(first).toBe(getInfiniteKey(request, { getNextPageParam: () => 1 }));
36
+ expect(first).not.toBe(second);
37
+ });
38
+ });
39
+ });
@@ -1 +1 @@
1
- {"version":3,"file":"use-cache.hooks.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-cache/use-cache.hooks.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAG9E,OAAO,KAAK,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAqB/E,0FAA0F;AAC1F,eAAO,MAAM,QAAQ,GAAI,CAAC,SAAS,eAAe,EAAE,SAAS,GAAG,mBAAmB,CAAC,CAAC,CAAC,EACpF,SAAS,CAAC,EACV,UAAU,mBAAmB,CAAC,CAAC,EAAE,SAAS,CAAC,KAC1C,kBAAkB,CAAC,CAAC,EAAE,SAAS,CA8FjC,CAAC"}
1
+ {"version":3,"file":"use-cache.hooks.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-cache/use-cache.hooks.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAG9E,OAAO,KAAK,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAsB/E,0FAA0F;AAC1F,eAAO,MAAM,QAAQ,GAAI,CAAC,SAAS,eAAe,EAAE,SAAS,GAAG,mBAAmB,CAAC,CAAC,CAAC,EACpF,SAAS,CAAC,EACV,UAAU,mBAAmB,CAAC,CAAC,EAAE,SAAS,CAAC,KAC1C,kBAAkB,CAAC,CAAC,EAAE,SAAS,CA8FjC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"use-fetch.hooks.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-fetch/use-fetch.hooks.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAEV,eAAe,EAKf,mBAAmB,EACpB,MAAM,mBAAmB,CAAC;AAG3B,OAAO,KAAK,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAkD/E;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ,GAAI,CAAC,SAAS,eAAe,EAAE,SAAS,GAAG,mBAAmB,CAAC,CAAC,CAAC,EACpF,SAAS,CAAC,EACV,UAAU,mBAAmB,CAAC,CAAC,EAAE,SAAS,CAAC,KAC1C,kBAAkB,CAAC,CAAC,EAAE,SAAS,CA2cjC,CAAC"}
1
+ {"version":3,"file":"use-fetch.hooks.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-fetch/use-fetch.hooks.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAEV,eAAe,EAKf,mBAAmB,EACpB,MAAM,mBAAmB,CAAC;AAG3B,OAAO,KAAK,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAmD/E;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ,GAAI,CAAC,SAAS,eAAe,EAAE,SAAS,GAAG,mBAAmB,CAAC,CAAC,CAAC,EACpF,SAAS,CAAC,EACV,UAAU,mBAAmB,CAAC,CAAC,EAAE,SAAS,CAAC,KAC1C,kBAAkB,CAAC,CAAC,EAAE,SAAS,CA2cjC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"use-infinite-fetch.utils.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-infinite-fetch/use-infinite-fetch.utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEzE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AAEzE;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAI,SAAS,eAAe,EAAE,YAAY,OAAO,EAAE,WAAW,MAAM,WAI9F,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,OAAO,OAAO,EAAE,EAAE,UAAU,OAAO,cAcjE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,yBAAyB,GACpC,MAAM,MAAM,GAAG,UAAU,GAAG,OAAO,EACnC,UAAU,eAAe,EACzB,SAAS,eAAe,YAMzB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,GAAG,gBAAgB,EAAE,GAAG,gBAAgB,YAEtE,CAAC;AAIF;;GAEG;AACH,eAAO,MAAM,SAAS,GAAI,QAAQ,cAAc,+BAO/C,CAAC"}
1
+ {"version":3,"file":"use-infinite-fetch.utils.d.ts","sourceRoot":"","sources":["../../../src/hooks/use-infinite-fetch/use-infinite-fetch.utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAGzE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAChE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AAEzE;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAI,SAAS,eAAe,EAAE,YAAY,OAAO,EAAE,WAAW,MAAM,WAI9F,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,OAAO,OAAO,EAAE,EAAE,UAAU,OAAO,cAcjE,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,yBAAyB,GACpC,MAAM,MAAM,GAAG,UAAU,GAAG,OAAO,EACnC,UAAU,eAAe,EACzB,SAAS,eAAe,YAMzB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,GAAG,gBAAgB,EAAE,GAAG,gBAAgB,YAEtE,CAAC;AAIF;;GAEG;AACH,eAAO,MAAM,SAAS,GAAI,QAAQ,cAAc,+BAO/C,CAAC"}
package/dist/index.mjs CHANGED
@@ -1261,6 +1261,16 @@ var useProvider = () => {
1261
1261
  return useContext(ConfigContext);
1262
1262
  };
1263
1263
  //#endregion
1264
+ //#region src/utils/key.utils.ts
1265
+ /**
1266
+ * `JSON.stringify` replacer for the keys the hooks build from a request.
1267
+ * Adapters with their own serializers accept `BigInt` payloads, which plain JSON can't encode.
1268
+ * They are kept in the key and stay distinct from a string with the same digits.
1269
+ */
1270
+ var keyReplacer = (_key, value) => {
1271
+ return typeof value === "bigint" ? { "~bigint": value.toString() } : value;
1272
+ };
1273
+ //#endregion
1264
1274
  //#region src/hooks/use-fetch/use-fetch.hooks.ts
1265
1275
  /**
1266
1276
  * Suspended components lose their state, so the pending promises and the responses that are not stored in the cache
@@ -1306,7 +1316,7 @@ var mergeOptions$1 = (...sources) => {
1306
1316
  var useFetch = (request, options) => {
1307
1317
  const { config: globalConfig } = useProvider();
1308
1318
  const { suspense, dependencies, disabled, dependencyTracking, revalidate, initialResponse, keepPreviousData, refresh, refreshTime, refetchBlurred, refetchOnBlur, refetchOnFocus, refetchOnReconnect, bounce, bounceType, bounceTime, bounceTimeout, deepCompare, structuralSharing, throwOnError } = mergeOptions$1(useFetchDefaultOptions, globalConfig.useFetchConfig, options);
1309
- const updateKey = JSON.stringify(request.toJSON());
1319
+ const updateKey = JSON.stringify(request.toJSON(), keyReplacer);
1310
1320
  const requestDebounce = f({ delay: bounceTime });
1311
1321
  const requestThrottle = p({
1312
1322
  interval: bounceTime,
@@ -1753,8 +1763,8 @@ var getInfiniteKey = (request, pagination, maxPages) => {
1753
1763
  request.toJSON(),
1754
1764
  pagination,
1755
1765
  maxPages ?? null
1756
- ], (_key, value) => {
1757
- return typeof value === "function" ? value.toString() : value;
1766
+ ], (key, value) => {
1767
+ return typeof value === "function" ? value.toString() : keyReplacer(key, value);
1758
1768
  });
1759
1769
  };
1760
1770
  /**
@@ -3065,7 +3075,7 @@ var useCache = (request, options) => {
3065
3075
  const { cache, loggerManager } = client;
3066
3076
  const logger = useRef(loggerManager.initialize(client, "useCache")).current;
3067
3077
  const [dispatcher] = getRequestDispatcher(request);
3068
- const updateKey = JSON.stringify(request.toJSON());
3078
+ const updateKey = JSON.stringify(request.toJSON(), keyReplacer);
3069
3079
  const { config: globalConfig } = useProvider();
3070
3080
  const { dependencyTracking, initialResponse, deepCompare, structuralSharing } = mergeOptions(useCacheDefaultOptions, globalConfig.useCacheConfig, options);
3071
3081
  /**