@hyper-fetch/react 9.4.2 → 9.5.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/hydrate/hydrate.spec.tsx +511 -0
- package/__tests__/features/hydrate/hydrate.ssr.spec.tsx +189 -0
- package/__tests__/features/use-fetch/use-fetch.concurrency.spec.ts +2 -1
- package/__tests__/features/use-fetch/use-fetch.offline.spec.ts +4 -2
- package/__tests__/features/use-fetch/use-fetch.scope.spec.tsx +211 -0
- package/__tests__/features/use-fetch/use-fetch.suspense.spec.tsx +223 -0
- package/__tests__/tsconfig.json +2 -0
- package/dist/helpers/use-request-events/use-request-events.hooks.d.ts.map +1 -1
- package/dist/helpers/use-tracked-state/use-tracked-state.hooks.d.ts.map +1 -1
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts +4 -1
- package/dist/helpers/use-tracked-state/use-tracked-state.utils.d.ts.map +1 -1
- package/dist/hooks/use-fetch/use-fetch.hooks.d.ts.map +1 -1
- package/dist/hydrate/hydrate.d.ts +16 -0
- package/dist/hydrate/hydrate.d.ts.map +1 -0
- package/dist/hydrate/hydrate.types.d.ts +52 -0
- package/dist/hydrate/hydrate.types.d.ts.map +1 -0
- package/dist/hydrate/hydrate.utils.d.ts +19 -0
- package/dist/hydrate/hydrate.utils.d.ts.map +1 -0
- package/dist/hydrate/index.d.ts +3 -0
- package/dist/hydrate/index.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +225 -28
- package/dist/index.mjs.map +1 -1
- package/dist/utils/scope.utils.d.ts +7 -0
- package/dist/utils/scope.utils.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/helpers/use-request-events/use-request-events.hooks.ts +15 -6
- package/src/helpers/use-tracked-state/use-tracked-state.hooks.ts +8 -3
- package/src/helpers/use-tracked-state/use-tracked-state.utils.ts +9 -1
- package/src/hooks/use-fetch/use-fetch.hooks.ts +78 -16
- package/src/hydrate/hydrate.tsx +74 -0
- package/src/hydrate/hydrate.types.ts +55 -0
- package/src/hydrate/hydrate.utils.ts +124 -0
- package/src/hydrate/index.ts +2 -0
- package/src/index.ts +1 -0
- package/src/utils/scope.utils.ts +8 -0
- package/tsconfig.json +2 -0
|
@@ -49,7 +49,8 @@ describe("useFetch [ Concurrency ]", () => {
|
|
|
49
49
|
expect(spyTwo).toHaveBeenCalledTimes(1);
|
|
50
50
|
});
|
|
51
51
|
it("should start in loading mode when request is already handled by the queue", async () => {
|
|
52
|
-
|
|
52
|
+
// The request must still be running after waitForRender - the default 20ms delay can elapse under load
|
|
53
|
+
mockRequest(request, { delay: 500 });
|
|
53
54
|
act(() => {
|
|
54
55
|
client.fetchDispatcher.add(request);
|
|
55
56
|
});
|
|
@@ -72,7 +72,8 @@ describe("useFetch [ Offline ]", () => {
|
|
|
72
72
|
});
|
|
73
73
|
describe("when request offline attribute is set to true", () => {
|
|
74
74
|
it("should not emit offline error until request is finished", async () => {
|
|
75
|
-
|
|
75
|
+
// The connection drops while the request is in flight - the default 20ms delay can elapse under load
|
|
76
|
+
const mock = mockRequest(request, { delay: 500 });
|
|
76
77
|
const response = renderUseFetch(request);
|
|
77
78
|
await waitForRender();
|
|
78
79
|
act(() => {
|
|
@@ -87,7 +88,8 @@ describe("useFetch [ Offline ]", () => {
|
|
|
87
88
|
});
|
|
88
89
|
describe("when request offline attribute is set to false", () => {
|
|
89
90
|
it("should emit offline error until request is finished", async () => {
|
|
90
|
-
|
|
91
|
+
// The connection drops while the request is in flight - the default 20ms delay can elapse under load
|
|
92
|
+
const mock = mockRequest(request, { status: 400, delay: 500 });
|
|
91
93
|
const response = renderUseFetch(request.setOffline(false));
|
|
92
94
|
await waitForRender();
|
|
93
95
|
act(() => {
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import type { RequestInstance } from "@hyper-fetch/core";
|
|
2
|
+
import { Client, Time, xhrExtra } from "@hyper-fetch/core";
|
|
3
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
4
|
+
import { act, renderHook, waitFor } from "@testing-library/react";
|
|
5
|
+
import { useFetch } from "hooks/use-fetch";
|
|
6
|
+
|
|
7
|
+
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
8
|
+
|
|
9
|
+
type User = { name: string };
|
|
10
|
+
|
|
11
|
+
// Typed loosely - the tests write raw cache values for requests of different shapes
|
|
12
|
+
const createResponse = (data: User): any => ({
|
|
13
|
+
data,
|
|
14
|
+
error: null,
|
|
15
|
+
status: 200,
|
|
16
|
+
success: true,
|
|
17
|
+
extra: xhrExtra,
|
|
18
|
+
requestTimestamp: Date.now(),
|
|
19
|
+
responseTimestamp: Date.now(),
|
|
20
|
+
retries: 0,
|
|
21
|
+
isCanceled: false,
|
|
22
|
+
isOffline: false,
|
|
23
|
+
willRetry: false,
|
|
24
|
+
addedTimestamp: Date.now(),
|
|
25
|
+
triggerTimestamp: Date.now(),
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe("useFetch [ Scope ]", () => {
|
|
29
|
+
let client: Client;
|
|
30
|
+
let request: ReturnType<typeof createUserRequest>;
|
|
31
|
+
|
|
32
|
+
const createUserRequest = (c: Client) =>
|
|
33
|
+
c.createRequest<{ response: User }>()({ endpoint: "/users/:userId" }).setStaleTime(Time.MIN);
|
|
34
|
+
|
|
35
|
+
beforeAll(() => startServer());
|
|
36
|
+
afterEach(() => resetMocks());
|
|
37
|
+
afterAll(() => stopServer());
|
|
38
|
+
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
client = new Client({ url: "http://localhost:3000" });
|
|
41
|
+
request = createUserRequest(client);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const renderScoped = (initial: RequestInstance) =>
|
|
45
|
+
renderHook(({ req }: { req: RequestInstance }) => useFetch(req, { revalidate: false, dependencyTracking: false }), {
|
|
46
|
+
initialProps: { req: initial },
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("should read the scoped cache on mount", () => {
|
|
50
|
+
const scoped = request.setParams({ userId: 1 }).setScope("a");
|
|
51
|
+
client.cache.set(scoped, createResponse({ name: "Scoped" }));
|
|
52
|
+
|
|
53
|
+
const view = renderScoped(scoped);
|
|
54
|
+
|
|
55
|
+
expect(view.result.current.data).toEqual({ name: "Scoped" });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("should read the scoped cache when the request changes to another key", () => {
|
|
59
|
+
const first = request.setParams({ userId: 1 }).setScope("a");
|
|
60
|
+
const second = request.setParams({ userId: 2 }).setScope("a");
|
|
61
|
+
client.cache.set(first, createResponse({ name: "First" }));
|
|
62
|
+
client.cache.set(second, createResponse({ name: "Second" }));
|
|
63
|
+
const spy = vi.spyOn(client.fetchDispatcher, "add");
|
|
64
|
+
|
|
65
|
+
const view = renderScoped(first);
|
|
66
|
+
view.rerender({ req: second });
|
|
67
|
+
|
|
68
|
+
expect(view.result.current.data).toEqual({ name: "Second" });
|
|
69
|
+
expect(spy).not.toHaveBeenCalled();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("should not receive cache updates of another scope with the same cache key", async () => {
|
|
73
|
+
const scopeA = request.setParams({ userId: 1 }).setScope("a");
|
|
74
|
+
const scopeB = request.setParams({ userId: 1 }).setScope("b");
|
|
75
|
+
client.cache.set(scopeA, createResponse({ name: "A" }));
|
|
76
|
+
client.cache.set(scopeB, createResponse({ name: "B" }));
|
|
77
|
+
|
|
78
|
+
const viewA = renderScoped(scopeA);
|
|
79
|
+
const viewB = renderScoped(scopeB);
|
|
80
|
+
|
|
81
|
+
act(() => {
|
|
82
|
+
client.cache.set(scopeA, createResponse({ name: "A updated" }));
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
await waitFor(() => expect(viewA.result.current.data).toEqual({ name: "A updated" }));
|
|
86
|
+
expect(viewB.result.current.data).toEqual({ name: "B" });
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("should not receive cache updates of the unscoped request with the same cache key", async () => {
|
|
90
|
+
const scoped = request.setParams({ userId: 1 }).setScope("a");
|
|
91
|
+
const unscoped = request.setParams({ userId: 1 });
|
|
92
|
+
client.cache.set(scoped, createResponse({ name: "Scoped" }));
|
|
93
|
+
|
|
94
|
+
const view = renderScoped(scoped);
|
|
95
|
+
|
|
96
|
+
act(() => {
|
|
97
|
+
client.cache.set(unscoped, createResponse({ name: "Unscoped" }));
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
expect(view.result.current.data).toEqual({ name: "Scoped" });
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("should store the fetched response under its scope only", async () => {
|
|
104
|
+
const scoped = request.setParams({ userId: 1 }).setScope("a");
|
|
105
|
+
const mock = mockRequest(scoped, { data: { name: "Fetched" } });
|
|
106
|
+
|
|
107
|
+
const view = renderHook(() => useFetch(scoped, { dependencyTracking: false }));
|
|
108
|
+
|
|
109
|
+
await waitFor(() => expect(view.result.current.data).toEqual(mock));
|
|
110
|
+
expect(client.cache.get(`a__${scoped.cacheKey}`)?.data).toEqual(mock);
|
|
111
|
+
expect(client.cache.get(scoped.cacheKey)).toBeUndefined();
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe("useFetch [ Scope - loading ]", () => {
|
|
116
|
+
const { startServer: start, stopServer: stop, resetMocks: reset, mockRequest: mock } = createHttpMockingServer();
|
|
117
|
+
|
|
118
|
+
beforeAll(() => start());
|
|
119
|
+
afterEach(() => reset());
|
|
120
|
+
afterAll(() => stop());
|
|
121
|
+
|
|
122
|
+
it("should show loading while a scoped request is refetched", async () => {
|
|
123
|
+
const client = new Client({ url: "http://loading.test" });
|
|
124
|
+
const scoped = client.createRequest<{ response: User }>()({ endpoint: "/users/1" }).setScope("a");
|
|
125
|
+
mock(scoped, { data: { name: "Fetched" }, delay: 50 });
|
|
126
|
+
client.cache.set(scoped, createResponse({ name: "Cached" }));
|
|
127
|
+
|
|
128
|
+
const view = renderHook(() => useFetch(scoped, { revalidate: false, dependencyTracking: false }));
|
|
129
|
+
expect(view.result.current.loading).toBe(false);
|
|
130
|
+
|
|
131
|
+
act(() => {
|
|
132
|
+
view.result.current.refetch();
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
await waitFor(() => expect(view.result.current.loading).toBe(true));
|
|
136
|
+
await waitFor(() => expect(view.result.current.data).toEqual({ name: "Fetched" }));
|
|
137
|
+
expect(view.result.current.loading).toBe(false);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("should not show loading when another scope with the same key is fetching", async () => {
|
|
141
|
+
const client = new Client({ url: "http://loading.test" });
|
|
142
|
+
const scopeA = client.createRequest<{ response: User }>()({ endpoint: "/users/1" }).setScope("a");
|
|
143
|
+
const scopeB = scopeA.setScope("b");
|
|
144
|
+
mock(scopeA, { data: { name: "Fetched" }, delay: 50 });
|
|
145
|
+
client.cache.set(scopeA, createResponse({ name: "A" }));
|
|
146
|
+
client.cache.set(scopeB, createResponse({ name: "B" }));
|
|
147
|
+
|
|
148
|
+
const viewA = renderHook(() => useFetch(scopeA, { revalidate: false, dependencyTracking: false }));
|
|
149
|
+
const viewB = renderHook(() => useFetch(scopeB, { revalidate: false, dependencyTracking: false }));
|
|
150
|
+
|
|
151
|
+
act(() => {
|
|
152
|
+
viewA.result.current.refetch();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
await waitFor(() => expect(viewA.result.current.loading).toBe(true));
|
|
156
|
+
expect(viewB.result.current.loading).toBe(false);
|
|
157
|
+
await waitFor(() => expect(viewA.result.current.loading).toBe(false));
|
|
158
|
+
expect(viewB.result.current.data).toEqual({ name: "B" });
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
describe("useFetch [ Scope - key changes and running requests ]", () => {
|
|
163
|
+
const { startServer: start, stopServer: stop, resetMocks: reset, mockRequest: mock } = createHttpMockingServer();
|
|
164
|
+
|
|
165
|
+
beforeAll(() => start());
|
|
166
|
+
afterEach(() => reset());
|
|
167
|
+
afterAll(() => stop());
|
|
168
|
+
|
|
169
|
+
it("should show the scoped cache of the new page when only query params change", () => {
|
|
170
|
+
const client = new Client({ url: "http://pages.test" });
|
|
171
|
+
const base = client
|
|
172
|
+
.createRequest<{ response: User; queryParams: { page: number } }>()({ endpoint: "/users" })
|
|
173
|
+
.setScope("a")
|
|
174
|
+
.setStaleTime(Time.MIN);
|
|
175
|
+
const page1 = base.setQueryParams({ page: 1 });
|
|
176
|
+
const page2 = base.setQueryParams({ page: 2 });
|
|
177
|
+
client.cache.set(page1, createResponse({ name: "Page 1" }));
|
|
178
|
+
client.cache.set(page2, createResponse({ name: "Page 2" }));
|
|
179
|
+
|
|
180
|
+
// "auto" keeps the previous data on query param changes, so only the cache read can switch the page
|
|
181
|
+
const view = renderHook(
|
|
182
|
+
({ req }) => useFetch(req, { revalidate: false, dependencyTracking: false, keepPreviousData: "auto" }),
|
|
183
|
+
{ initialProps: { req: page1 } },
|
|
184
|
+
);
|
|
185
|
+
view.rerender({ req: page2 });
|
|
186
|
+
|
|
187
|
+
expect(view.result.current.data).toEqual({ name: "Page 2" });
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("should keep loading while a scoped request is still running and the cache changes", async () => {
|
|
191
|
+
const client = new Client({ url: "http://running.test" });
|
|
192
|
+
const scoped = client.createRequest<{ response: User }>()({ endpoint: "/users/1" }).setScope("a");
|
|
193
|
+
mock(scoped, { data: { name: "Fetched" }, delay: 100 });
|
|
194
|
+
client.cache.set(scoped, createResponse({ name: "Cached" }));
|
|
195
|
+
|
|
196
|
+
const view = renderHook(() => useFetch(scoped, { revalidate: false, dependencyTracking: false }));
|
|
197
|
+
act(() => {
|
|
198
|
+
view.result.current.refetch();
|
|
199
|
+
});
|
|
200
|
+
await waitFor(() => expect(view.result.current.loading).toBe(true));
|
|
201
|
+
|
|
202
|
+
// e.g. an optimistic update while the request is in flight
|
|
203
|
+
act(() => {
|
|
204
|
+
client.cache.set(scoped, createResponse({ name: "Optimistic" }));
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
expect(view.result.current.data).toEqual({ name: "Optimistic" });
|
|
208
|
+
expect(view.result.current.loading).toBe(true);
|
|
209
|
+
await waitFor(() => expect(view.result.current.data).toEqual({ name: "Fetched" }));
|
|
210
|
+
});
|
|
211
|
+
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { Client } from "@hyper-fetch/core";
|
|
1
2
|
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
3
|
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
|
|
3
4
|
import { useFetch } from "hooks/use-fetch";
|
|
5
|
+
import { HttpResponse, delay, http } from "msw";
|
|
4
6
|
/// <reference types="vitest/globals" />
|
|
5
7
|
import type { ReactNode } from "react";
|
|
6
8
|
import { Suspense, StrictMode, Component, useState } from "react";
|
|
@@ -503,3 +505,224 @@ describe("useFetch [ Suspense ]", () => {
|
|
|
503
505
|
});
|
|
504
506
|
});
|
|
505
507
|
});
|
|
508
|
+
|
|
509
|
+
describe("useFetch [ Suspense - isolation ]", () => {
|
|
510
|
+
const { server, startServer: start, stopServer: stop, resetMocks: reset } = createHttpMockingServer();
|
|
511
|
+
|
|
512
|
+
beforeAll(() => start());
|
|
513
|
+
afterEach(() => reset());
|
|
514
|
+
afterAll(() => stop());
|
|
515
|
+
|
|
516
|
+
type Echo = { from: string };
|
|
517
|
+
|
|
518
|
+
// Echoes who asked: the host for clients, the x-scope header for scopes. The second one answers later.
|
|
519
|
+
const mockEcho = () => {
|
|
520
|
+
server.use(
|
|
521
|
+
http.get(/\/users\/1$/, async ({ request: req }) => {
|
|
522
|
+
const from = req.headers.get("x-scope") ?? new URL(req.url).host;
|
|
523
|
+
await delay(from === "b" || from.startsWith("b.") ? 300 : 10);
|
|
524
|
+
return HttpResponse.json({ from });
|
|
525
|
+
}),
|
|
526
|
+
);
|
|
527
|
+
};
|
|
528
|
+
|
|
529
|
+
const createUserRequest = (c: Client) => c.createRequest<{ response: Echo }>()({ endpoint: "/users/1" });
|
|
530
|
+
|
|
531
|
+
// Every value a view rendered - a view must never show data of another client or scope, even briefly
|
|
532
|
+
let rendered: Record<string, (string | undefined)[]> = {};
|
|
533
|
+
beforeEach(() => {
|
|
534
|
+
rendered = {};
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
const View = ({ request, testId }: { request: any; testId: string }) => {
|
|
538
|
+
const { data } = useFetch(request, { suspense: true, dependencyTracking: false });
|
|
539
|
+
const from = (data as Echo | null)?.from;
|
|
540
|
+
rendered[testId] = [...(rendered[testId] ?? []), from];
|
|
541
|
+
return <div data-testid={testId}>{from}</div>;
|
|
542
|
+
};
|
|
543
|
+
|
|
544
|
+
it("should not share suspense results between clients with the same cache key", async () => {
|
|
545
|
+
mockEcho();
|
|
546
|
+
const clientA = new Client({ url: "http://a.test" });
|
|
547
|
+
const clientB = new Client({ url: "http://b.test" });
|
|
548
|
+
const requestA = createUserRequest(clientA);
|
|
549
|
+
const requestB = createUserRequest(clientB);
|
|
550
|
+
expect(requestA.cacheKey).toBe(requestB.cacheKey);
|
|
551
|
+
|
|
552
|
+
render(
|
|
553
|
+
<>
|
|
554
|
+
<Suspense fallback={<div data-testid="fallback-b" />}>
|
|
555
|
+
<View request={requestB} testId="b" />
|
|
556
|
+
</Suspense>
|
|
557
|
+
<Suspense fallback={<div data-testid="fallback-a" />}>
|
|
558
|
+
<View request={requestA} testId="a" />
|
|
559
|
+
</Suspense>
|
|
560
|
+
</>,
|
|
561
|
+
);
|
|
562
|
+
|
|
563
|
+
// The fast client must not wait for the slow one
|
|
564
|
+
await waitFor(() => expect(screen.getByTestId("a")).toHaveTextContent("a.test"), { interval: 5 });
|
|
565
|
+
expect(screen.getByTestId("fallback-b")).toBeInTheDocument();
|
|
566
|
+
|
|
567
|
+
await waitFor(() => expect(screen.getByTestId("b")).toHaveTextContent("b.test"));
|
|
568
|
+
expect(rendered.a.every((value) => value === "a.test")).toBe(true);
|
|
569
|
+
expect(rendered.b.every((value) => value === "b.test")).toBe(true);
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
it("should not share suspense results between scopes with the same cache key", async () => {
|
|
573
|
+
mockEcho();
|
|
574
|
+
const testClient = new Client({ url: "http://scopes.test" });
|
|
575
|
+
const scopeA = createUserRequest(testClient).setScope("a").setHeaders({ "x-scope": "a" });
|
|
576
|
+
const scopeB = createUserRequest(testClient).setScope("b").setHeaders({ "x-scope": "b" });
|
|
577
|
+
|
|
578
|
+
render(
|
|
579
|
+
<Suspense fallback={<div data-testid="fallback" />}>
|
|
580
|
+
<View request={scopeB} testId="b" />
|
|
581
|
+
<View request={scopeA} testId="a" />
|
|
582
|
+
</Suspense>,
|
|
583
|
+
);
|
|
584
|
+
|
|
585
|
+
await waitFor(() => {
|
|
586
|
+
expect(screen.getByTestId("a")).toHaveTextContent(/^a$/);
|
|
587
|
+
expect(screen.getByTestId("b")).toHaveTextContent(/^b$/);
|
|
588
|
+
});
|
|
589
|
+
expect(rendered.a.every((value) => value === "a")).toBe(true);
|
|
590
|
+
expect(rendered.b.every((value) => value === "b")).toBe(true);
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
it("should send one request per scope", async () => {
|
|
594
|
+
mockEcho();
|
|
595
|
+
const testClient = new Client({ url: "http://scopes.test" });
|
|
596
|
+
const spy = vi.spyOn(testClient.fetchDispatcher, "add");
|
|
597
|
+
const scopeA = createUserRequest(testClient).setScope("a").setHeaders({ "x-scope": "a" });
|
|
598
|
+
const scopeB = createUserRequest(testClient).setScope("b").setHeaders({ "x-scope": "b" });
|
|
599
|
+
|
|
600
|
+
render(
|
|
601
|
+
<Suspense fallback={<div data-testid="fallback" />}>
|
|
602
|
+
<View request={scopeA} testId="a" />
|
|
603
|
+
<View request={scopeB} testId="b" />
|
|
604
|
+
</Suspense>,
|
|
605
|
+
);
|
|
606
|
+
|
|
607
|
+
await waitFor(() => expect(screen.getByTestId("b")).toHaveTextContent(/^b$/));
|
|
608
|
+
const scopes = spy.mock.calls.map(([added]) => added.scope);
|
|
609
|
+
expect(scopes.filter((scope) => scope === "a")).toHaveLength(1);
|
|
610
|
+
expect(scopes.filter((scope) => scope === "b")).toHaveLength(1);
|
|
611
|
+
});
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
describe("useFetch [ Suspense - requests ]", () => {
|
|
615
|
+
const { startServer: start, stopServer: stop, resetMocks: reset, mockRequest: mock } = createHttpMockingServer();
|
|
616
|
+
|
|
617
|
+
beforeAll(() => start());
|
|
618
|
+
afterEach(() => reset());
|
|
619
|
+
afterAll(() => stop());
|
|
620
|
+
|
|
621
|
+
const View = ({ request, options = {} }: { request: any; options?: Record<string, unknown> }) => {
|
|
622
|
+
const { data, error, loading } = useFetch(request, { suspense: true, dependencyTracking: false, ...options });
|
|
623
|
+
return (
|
|
624
|
+
<div data-testid="view" data-loading={String(loading)}>
|
|
625
|
+
{error ? "error" : JSON.stringify(data)}
|
|
626
|
+
</div>
|
|
627
|
+
);
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
it("should send a single request for a suspended component", async () => {
|
|
631
|
+
const testClient = new Client({ url: "http://single.test" });
|
|
632
|
+
const request = testClient.createRequest<{ response: any }>()({ endpoint: "/single" });
|
|
633
|
+
const data = mock(request, { data: { value: 1 } });
|
|
634
|
+
const spy = vi.spyOn(testClient.fetchDispatcher, "add");
|
|
635
|
+
|
|
636
|
+
render(
|
|
637
|
+
<Suspense fallback={<div data-testid="fallback" />}>
|
|
638
|
+
<View request={request} />
|
|
639
|
+
</Suspense>,
|
|
640
|
+
);
|
|
641
|
+
|
|
642
|
+
await waitFor(() => expect(screen.getByTestId("view")).toHaveTextContent(JSON.stringify(data)));
|
|
643
|
+
await act(async () => {
|
|
644
|
+
await new Promise((resolve) => {
|
|
645
|
+
setTimeout(resolve, 50);
|
|
646
|
+
});
|
|
647
|
+
});
|
|
648
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
649
|
+
expect(screen.getByTestId("view")).toHaveAttribute("data-loading", "false");
|
|
650
|
+
});
|
|
651
|
+
|
|
652
|
+
it("should settle loading after an error response without refetching", async () => {
|
|
653
|
+
const testClient = new Client({ url: "http://error.test" });
|
|
654
|
+
const request = testClient.createRequest<{ response: any }>()({ endpoint: "/error" });
|
|
655
|
+
mock(request, { status: 400 });
|
|
656
|
+
const spy = vi.spyOn(testClient.fetchDispatcher, "add");
|
|
657
|
+
|
|
658
|
+
render(
|
|
659
|
+
<Suspense fallback={<div data-testid="fallback" />}>
|
|
660
|
+
<View request={request} />
|
|
661
|
+
</Suspense>,
|
|
662
|
+
);
|
|
663
|
+
|
|
664
|
+
await waitFor(() => expect(screen.getByTestId("view")).toHaveTextContent("error"));
|
|
665
|
+
expect(screen.getByTestId("view")).toHaveAttribute("data-loading", "false");
|
|
666
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
it("should still revalidate on mount when the cached data is older than the suspense guard", async () => {
|
|
670
|
+
const testClient = new Client({ url: "http://stale.test" });
|
|
671
|
+
const request = testClient.createRequest<{ response: any }>()({ endpoint: "/stale" });
|
|
672
|
+
const fresh = mock(request, { data: { value: "fresh" } });
|
|
673
|
+
const old = Date.now() - 5000;
|
|
674
|
+
testClient.cache.set(request, {
|
|
675
|
+
data: { value: "cached" },
|
|
676
|
+
error: null,
|
|
677
|
+
status: 200,
|
|
678
|
+
success: true,
|
|
679
|
+
extra: null,
|
|
680
|
+
requestTimestamp: old,
|
|
681
|
+
responseTimestamp: old,
|
|
682
|
+
retries: 0,
|
|
683
|
+
isCanceled: false,
|
|
684
|
+
isOffline: false,
|
|
685
|
+
willRetry: false,
|
|
686
|
+
addedTimestamp: old,
|
|
687
|
+
triggerTimestamp: old,
|
|
688
|
+
} as any);
|
|
689
|
+
|
|
690
|
+
render(
|
|
691
|
+
<Suspense fallback={<div data-testid="fallback" />}>
|
|
692
|
+
<View request={request} />
|
|
693
|
+
</Suspense>,
|
|
694
|
+
);
|
|
695
|
+
|
|
696
|
+
expect(screen.getByTestId("view")).toHaveTextContent("cached");
|
|
697
|
+
await waitFor(() => expect(screen.getByTestId("view")).toHaveTextContent(JSON.stringify(fresh)));
|
|
698
|
+
});
|
|
699
|
+
});
|
|
700
|
+
|
|
701
|
+
describe("useFetch [ Suspense - offline ]", () => {
|
|
702
|
+
it("should not queue duplicates when a suspended component re-renders while offline", async () => {
|
|
703
|
+
const testClient = new Client({ url: "http://offline.test" });
|
|
704
|
+
testClient.appManager.setOnline(false);
|
|
705
|
+
const request = testClient.createRequest<{ response: any }>()({ endpoint: "/offline" });
|
|
706
|
+
const spy = vi.spyOn(testClient.fetchDispatcher, "add");
|
|
707
|
+
|
|
708
|
+
const View = () => {
|
|
709
|
+
const { data } = useFetch(request, { suspense: true, dependencyTracking: false });
|
|
710
|
+
return <div>{JSON.stringify(data)}</div>;
|
|
711
|
+
};
|
|
712
|
+
const App = ({ tick }: { tick: number }) => (
|
|
713
|
+
<div data-tick={tick}>
|
|
714
|
+
<Suspense fallback={<div data-testid="fallback" />}>
|
|
715
|
+
<View />
|
|
716
|
+
</Suspense>
|
|
717
|
+
</div>
|
|
718
|
+
);
|
|
719
|
+
|
|
720
|
+
const view = render(<App tick={0} />);
|
|
721
|
+
view.rerender(<App tick={1} />);
|
|
722
|
+
view.rerender(<App tick={2} />);
|
|
723
|
+
|
|
724
|
+
expect(screen.getByTestId("fallback")).toBeInTheDocument();
|
|
725
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
726
|
+
expect(testClient.fetchDispatcher.getQueue(request.queryKey).requests).toHaveLength(1);
|
|
727
|
+
});
|
|
728
|
+
});
|
package/__tests__/tsconfig.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-request-events.hooks.d.ts","sourceRoot":"","sources":["../../../src/helpers/use-request-events/use-request-events.hooks.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAGV,eAAe,EAQhB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAOV,0BAA0B,EAC1B,yBAAyB,EAE1B,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"use-request-events.hooks.d.ts","sourceRoot":"","sources":["../../../src/helpers/use-request-events/use-request-events.hooks.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAGV,eAAe,EAQhB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAOV,0BAA0B,EAC1B,yBAAyB,EAE1B,MAAM,SAAS,CAAC;AAIjB;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,GAAI,CAAC,SAAS,eAAe,EAAE,8EAOzD,yBAAyB,CAAC,CAAC,CAAC,KAAG,0BAA0B,CAAC,CAAC,CAuU7D,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-tracked-state.hooks.d.ts","sourceRoot":"","sources":["../../../src/helpers/use-tracked-state/use-tracked-state.hooks.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAGV,mBAAmB,EACnB,eAAe,EAIhB,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"use-tracked-state.hooks.d.ts","sourceRoot":"","sources":["../../../src/helpers/use-tracked-state/use-tracked-state.hooks.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAGV,mBAAmB,EACnB,eAAe,EAIhB,MAAM,mBAAmB,CAAC;AAM3B,OAAO,KAAK,EAGV,oBAAoB,EACpB,qBAAqB,EAEtB,MAAM,2BAA2B,CAAC;AASnC;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,GAAI,CAAC,SAAS,eAAe,EAAE,KAAK,GAAG,mBAAmB,CAAC,CAAC,CAAC,EAAE,+IAWxF,oBAAoB,CAAC,CAAC,EAAE,KAAK,CAAC,KAAG,qBAAqB,CAAC,CAAC,EAAE,KAAK,CAoXjE,CAAC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CacheValueType, NullableType, RequestInstance, ExtractResponseType, ExtractErrorType, ExtractAdapterResolvedType, Dispatcher, ExtractAdapterType, ResponseDetailsType } from '@hyper-fetch/core';
|
|
2
2
|
import { UseTrackedStateType } from '..';
|
|
3
|
+
import { HydratedEntry } from '../../hydrate/hydrate.types';
|
|
3
4
|
/**
|
|
4
5
|
* Determines whether state should be cleared when the cache key changes.
|
|
5
6
|
*
|
|
@@ -19,11 +20,13 @@ export declare const getIsInitiallyLoading: <T extends RequestInstance>({ queryK
|
|
|
19
20
|
revalidate?: boolean;
|
|
20
21
|
disabled?: boolean;
|
|
21
22
|
}) => boolean;
|
|
22
|
-
export declare const getInitialState: <T extends RequestInstance>({ initialResponse, dispatcher, request, disabled, revalidate, }: {
|
|
23
|
+
export declare const getInitialState: <T extends RequestInstance>({ initialResponse, dispatcher, request, disabled, revalidate, hydratedEntry, }: {
|
|
23
24
|
initialResponse: NullableType<Partial<ExtractAdapterResolvedType<T>>>;
|
|
24
25
|
dispatcher: Dispatcher<ExtractAdapterType<T>>;
|
|
25
26
|
request: T;
|
|
26
27
|
disabled?: boolean;
|
|
27
28
|
revalidate?: boolean;
|
|
29
|
+
/** Data from the closest `Hydrate` that was not written to the cache yet */
|
|
30
|
+
hydratedEntry?: NullableType<HydratedEntry>;
|
|
28
31
|
}) => UseTrackedStateType<T>;
|
|
29
32
|
//# sourceMappingURL=use-tracked-state.utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-tracked-state.utils.d.ts","sourceRoot":"","sources":["../../../src/helpers/use-tracked-state/use-tracked-state.utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,cAAc,EACd,YAAY,EACZ,eAAe,EACf,mBAAmB,EACnB,gBAAgB,EAChB,0BAA0B,EAC1B,UAAU,EACV,kBAAkB,EAElB,mBAAmB,EAGpB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"use-tracked-state.utils.d.ts","sourceRoot":"","sources":["../../../src/helpers/use-tracked-state/use-tracked-state.utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,cAAc,EACd,YAAY,EACZ,eAAe,EACf,mBAAmB,EACnB,gBAAgB,EAChB,0BAA0B,EAC1B,UAAU,EACV,kBAAkB,EAElB,mBAAmB,EAGpB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAEnD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAa3D;;;;;;GAMG;AACH,eAAO,MAAM,mBAAmB,GAC9B,MAAM,MAAM,GAAG,UAAU,GAAG,OAAO,EACnC,aAAa,MAAM,EACnB,aAAa,MAAM,KAClB,OAQF,CAAC;AAEF,eAAO,MAAM,eAAe,GAC1B,QAAQ,mBAAmB,CAAC,eAAe,CAAC,EAC5C,UAAU,OAAO,CAAC,mBAAmB,CAAC,KACrC,mBAYF,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAAI,WAAW,MAAM,EAAE,gBAAgB,YAAY,CAAC,IAAI,GAAG,MAAM,CAAC,YAG9F,CAAC;AAEF,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,eAAe,EACzD,SAAS,CAAC,EACV,iBAAiB,YAAY,CAAC,OAAO,CAAC,0BAA0B,CAAC,CAAC,CAAC,CAAC,CAAC,EACrE,WAAW,YAAY,CAAC,cAAc,CAAC,mBAAmB,CAAC,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAAE,kBAAkB,CAAC,CAAC,CAAC,CAAC,CAAC,KAC1G,cAAc,CAAC,mBAAmB,CAAC,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAAE,kBAAkB,CAAC,CAAC,CAAC,CAAC,GAAG,IA0BvF,CAAC;AAEF,eAAO,MAAM,YAAY,GAAI,YAAY,YAAY,CAAC,MAAM,GAAG,IAAI,CAAC,gBAEnE,CAAC;AAEF,eAAO,MAAM,qBAAqB,GAAI,CAAC,SAAS,eAAe,EAAE,2DAM9D;IACD,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,UAAU,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9C,QAAQ,EAAE,OAAO,CAAC;IAClB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,YASA,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,CAAC,SAAS,eAAe,EAAE,gFAOxD;IACD,eAAe,EAAE,YAAY,CAAC,OAAO,CAAC,0BAA0B,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IACtE,UAAU,EAAE,UAAU,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9C,OAAO,EAAE,CAAC,CAAC;IACX,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,4EAA4E;IAC5E,aAAa,CAAC,EAAE,YAAY,CAAC,aAAa,CAAC,CAAC;CAC7C,KAAG,mBAAmB,CAAC,CAAC,CAmDxB,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,
|
|
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;AA2B/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,CAiZjC,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { HydrateProps, HydrationContextType } from './hydrate.types';
|
|
2
|
+
/**
|
|
3
|
+
* Hydrates the client cache with data fetched on the server.
|
|
4
|
+
*
|
|
5
|
+
* - Hooks below it render with the hydrated data on the first render, also during server rendering.
|
|
6
|
+
* - On the client the data is written to the cache before the hooks mount, so fresh data is not refetched.
|
|
7
|
+
* - Newer cache data is never replaced by an older snapshot.
|
|
8
|
+
* - It can be nested and rendered conditionally; every `Hydrate` serves only its own client.
|
|
9
|
+
*/
|
|
10
|
+
export declare const Hydrate: ({ client, data, options, children }: HydrateProps) => import("react/jsx-runtime").JSX.Element;
|
|
11
|
+
/**
|
|
12
|
+
* Access to the closest hydration data
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export declare const useHydration: () => HydrationContextType | null;
|
|
16
|
+
//# sourceMappingURL=hydrate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hydrate.d.ts","sourceRoot":"","sources":["../../src/hydrate/hydrate.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAQ1E;;;;;;;GAOG;AACH,eAAO,MAAM,OAAO,GAAI,qCAAqC,YAAY,4CA+CxE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,YAAY,QAAO,oBAAoB,GAAG,IAEtD,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { CacheValueType, ClientInstance, EmptyTypes, HydrateDataType, HydrationOptions } from '@hyper-fetch/core';
|
|
2
|
+
import { default as React } from 'react';
|
|
3
|
+
/**
|
|
4
|
+
* Options applied to every hydrated item - the same shape as the second argument of `client.hydrate()`
|
|
5
|
+
*/
|
|
6
|
+
export type HydrateOptionsType = Partial<HydrationOptions> | ((item: HydrateDataType) => Partial<HydrationOptions>);
|
|
7
|
+
export type HydrateProps = {
|
|
8
|
+
/**
|
|
9
|
+
* Client which cache receives the hydrated data
|
|
10
|
+
*/
|
|
11
|
+
client: ClientInstance;
|
|
12
|
+
/**
|
|
13
|
+
* Dehydrated items, usually created on the server with `request.dehydrate()`.
|
|
14
|
+
* Empty values are ignored, so the list can be built conditionally.
|
|
15
|
+
*/
|
|
16
|
+
data?: (HydrateDataType | EmptyTypes)[] | EmptyTypes;
|
|
17
|
+
/**
|
|
18
|
+
* Options applied to every item, as an object or a function of the item
|
|
19
|
+
*/
|
|
20
|
+
options?: HydrateOptionsType;
|
|
21
|
+
/**
|
|
22
|
+
* Children to render
|
|
23
|
+
*/
|
|
24
|
+
children?: React.ReactNode;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Single hydrated item resolved to the value it will have in the cache
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
export type HydratedEntry = {
|
|
31
|
+
/** Original item - identity is used to track which items were already written to the cache */
|
|
32
|
+
item: HydrateDataType;
|
|
33
|
+
/** Cache storage key - cacheKey prefixed with the scope */
|
|
34
|
+
storageKey: string;
|
|
35
|
+
/** Value the cache will hold once the item is written */
|
|
36
|
+
value: CacheValueType;
|
|
37
|
+
/** When false, existing cache data is never replaced */
|
|
38
|
+
override: boolean;
|
|
39
|
+
/** Response timestamp used to decide which of the cached and hydrated data is newer */
|
|
40
|
+
timestamp: number;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* @internal
|
|
44
|
+
*/
|
|
45
|
+
export type HydrationContextType = {
|
|
46
|
+
/**
|
|
47
|
+
* Returns the hydrated entry for the client and storage key,
|
|
48
|
+
* as long as it was not written to the cache yet
|
|
49
|
+
*/
|
|
50
|
+
get: (client: ClientInstance, storageKey: string) => HydratedEntry | undefined;
|
|
51
|
+
};
|
|
52
|
+
//# sourceMappingURL=hydrate.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hydrate.types.d.ts","sourceRoot":"","sources":["../../src/hydrate/hydrate.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,UAAU,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACvH,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG,OAAO,CAAC,gBAAgB,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,eAAe,KAAK,OAAO,CAAC,gBAAgB,CAAC,CAAC,CAAC;AAEpH,MAAM,MAAM,YAAY,GAAG;IACzB;;OAEG;IACH,MAAM,EAAE,cAAc,CAAC;IACvB;;;OAGG;IACH,IAAI,CAAC,EAAE,CAAC,eAAe,GAAG,UAAU,CAAC,EAAE,GAAG,UAAU,CAAC;IACrD;;OAEG;IACH,OAAO,CAAC,EAAE,kBAAkB,CAAC;IAC7B;;OAEG;IACH,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;CAC5B,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,8FAA8F;IAC9F,IAAI,EAAE,eAAe,CAAC;IACtB,2DAA2D;IAC3D,UAAU,EAAE,MAAM,CAAC;IACnB,yDAAyD;IACzD,KAAK,EAAE,cAAc,CAAC;IACtB,wDAAwD;IACxD,QAAQ,EAAE,OAAO,CAAC;IAClB,uFAAuF;IACvF,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC;;;OAGG;IACH,GAAG,EAAE,CAAC,MAAM,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,KAAK,aAAa,GAAG,SAAS,CAAC;CAChF,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CacheValueType, ClientInstance, EmptyTypes, HydrateDataType, NullableType } from '@hyper-fetch/core';
|
|
2
|
+
import { HydratedEntry, HydrateOptionsType } from './hydrate.types';
|
|
3
|
+
export declare const isConsumed: (client: ClientInstance, item: HydrateDataType) => boolean;
|
|
4
|
+
export declare const markConsumed: (client: ClientInstance, item: HydrateDataType) => void;
|
|
5
|
+
/**
|
|
6
|
+
* Turns dehydrated items into the values they will have in the cache, keyed by storage key.
|
|
7
|
+
* The last item wins when the same key is passed twice.
|
|
8
|
+
*/
|
|
9
|
+
export declare const getHydratedEntries: (client: ClientInstance, data: NullableType<(HydrateDataType | EmptyTypes)[]>, options?: HydrateOptionsType) => Map<string, HydratedEntry>;
|
|
10
|
+
/**
|
|
11
|
+
* Decides whether the hydrated value should be used instead of the cached one.
|
|
12
|
+
* Newer data always wins; with `override: false` existing cache data is kept.
|
|
13
|
+
*/
|
|
14
|
+
export declare const shouldUseHydratedValue: (cacheData: NullableType<CacheValueType>, entry: HydratedEntry) => boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Picks the data a hook should start with: cached data, unless the hydrated data is newer
|
|
17
|
+
*/
|
|
18
|
+
export declare const pickInitialCacheData: <T extends CacheValueType>(cacheData: NullableType<T>, entry: NullableType<HydratedEntry>) => NullableType<T>;
|
|
19
|
+
//# sourceMappingURL=hydrate.utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hydrate.utils.d.ts","sourceRoot":"","sources":["../../src/hydrate/hydrate.utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EACd,UAAU,EACV,eAAe,EAEf,YAAY,EACb,MAAM,mBAAmB,CAAC;AAG3B,OAAO,KAAK,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAkBzE,eAAO,MAAM,UAAU,GAAI,QAAQ,cAAc,EAAE,MAAM,eAAe,YAEvE,CAAC;AAEF,eAAO,MAAM,YAAY,GAAI,QAAQ,cAAc,EAAE,MAAM,eAAe,SAEzE,CAAC;AAqBF;;;GAGG;AACH,eAAO,MAAM,kBAAkB,GAC7B,QAAQ,cAAc,EACtB,MAAM,YAAY,CAAC,CAAC,eAAe,GAAG,UAAU,CAAC,EAAE,CAAC,EACpD,UAAU,kBAAkB,KAC3B,GAAG,CAAC,MAAM,EAAE,aAAa,CAiC3B,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,sBAAsB,GAAI,WAAW,YAAY,CAAC,cAAc,CAAC,EAAE,OAAO,aAAa,KAAG,OAQtG,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,oBAAoB,GAAI,CAAC,SAAS,cAAc,EAC3D,WAAW,YAAY,CAAC,CAAC,CAAC,EAC1B,OAAO,YAAY,CAAC,aAAa,CAAC,KACjC,YAAY,CAAC,CAAC,CAKhB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hydrate/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,YAAY,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/index.d.ts
CHANGED