@hyper-fetch/core 9.6.1 → 9.7.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/adapter/adapter.bindings.progress.spec.ts +120 -0
- package/__tests__/features/adapter/adapter.bindings.spec.ts +4 -6
- package/__tests__/features/adapter/adapter.defaults.spec.ts +87 -0
- package/__tests__/features/adapter/adapter.mapping.spec.ts +257 -0
- package/__tests__/features/cache/cache.lazy-storage.clear.spec.ts +145 -0
- package/__tests__/features/cache/cache.lazy-storage.resilience.spec.ts +667 -0
- package/__tests__/features/cache/cache.scoped-events.spec.ts +183 -0
- package/__tests__/features/cache/cache.semantics.spec.ts +785 -0
- package/__tests__/features/cache/cache.structural-sharing.spec.ts +565 -0
- package/__tests__/features/cache/cache.timing.spec.ts +448 -0
- package/__tests__/features/client/client.clear.spec.ts +313 -0
- package/__tests__/features/client/client.configuration.spec.ts +416 -0
- package/__tests__/features/client/client.hydration.round-trip.spec.ts +324 -0
- package/__tests__/features/client/client.interceptor.spec.ts +54 -45
- package/__tests__/features/client/client.interceptors.pipeline.spec.ts +599 -0
- package/__tests__/features/client/client.user-errors.spec.ts +220 -0
- package/__tests__/features/dispatcher/dispatcher.cleanup.spec.ts +534 -0
- package/__tests__/features/dispatcher/dispatcher.control.spec.ts +381 -0
- package/__tests__/features/dispatcher/dispatcher.modes.spec.ts +527 -0
- package/__tests__/features/dispatcher/dispatcher.offline.spec.ts +278 -0
- package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +35 -12
- package/__tests__/features/dispatcher/dispatcher.reentrancy.spec.ts +309 -0
- package/__tests__/features/dispatcher/dispatcher.response-order.spec.ts +241 -0
- package/__tests__/features/dispatcher/dispatcher.retry-flow.spec.ts +554 -0
- package/__tests__/features/dispatcher/dispatcher.retry-removal.spec.ts +196 -0
- package/__tests__/features/dispatcher/dispatcher.retry.spec.ts +4 -3
- package/__tests__/features/http-adapter/http-adapter.e2e.spec.ts +917 -0
- package/__tests__/features/managers/app/app.manager.events.spec.ts +434 -0
- package/__tests__/features/managers/logger/logger.levels.spec.ts +199 -0
- package/__tests__/features/managers/request/request.manager.abort.spec.ts +449 -0
- package/__tests__/features/mocker/mocker.base.spec.ts +22 -3
- package/__tests__/features/mocker/mocker.errors.spec.ts +99 -0
- package/__tests__/features/mocker/mocker.outcomes.spec.ts +308 -0
- package/__tests__/features/optimistic/optimistic.concurrency.spec.ts +278 -0
- package/__tests__/features/optimistic/optimistic.helpers.spec.ts +307 -0
- package/__tests__/features/optimistic/optimistic.matrix.spec.ts +315 -0
- package/__tests__/features/optimistic/optimistic.races.spec.ts +270 -0
- package/__tests__/features/optimistic/optimistic.random.spec.ts +191 -0
- package/__tests__/features/optimistic/optimistic.rollback.spec.ts +1001 -0
- package/__tests__/features/optimistic/optimistic.typescript.spec.ts +196 -0
- package/__tests__/features/plugin/plugin.lifecycle.spec.ts +457 -0
- package/__tests__/features/request/request.async-mapper-removal.spec.ts +138 -0
- package/__tests__/features/request/request.immutability.spec.ts +259 -0
- package/__tests__/features/request/request.inference.spec.ts +269 -0
- package/__tests__/features/request/request.lifecycle.spec.ts +682 -0
- package/__tests__/features/request/request.optimistic-settlement.spec.ts +35 -17
- package/__tests__/features/request/request.optimistic.spec.ts +12 -1
- package/__tests__/features/request/request.values.spec.ts +408 -0
- package/__tests__/features/sdk/sdk.builder.spec.ts +304 -0
- package/__tests__/features/utils/emitter.listeners.spec.ts +202 -0
- package/__tests__/utils/controlled-adapter.utils.ts +119 -0
- package/__tests__/utils/fixtures.utils.ts +193 -0
- package/__tests__/utils/optimistic.utils.ts +124 -0
- package/dist/adapter/adapter.bindings.d.ts.map +1 -1
- package/dist/adapter/adapter.utils.d.ts.map +1 -1
- package/dist/cache/cache.d.ts +12 -1
- package/dist/cache/cache.d.ts.map +1 -1
- package/dist/cache/cache.events.d.ts +20 -6
- package/dist/cache/cache.events.d.ts.map +1 -1
- package/dist/cache/cache.utils.d.ts +1 -0
- package/dist/cache/cache.utils.d.ts.map +1 -1
- package/dist/client/client.d.ts +3 -3
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.utils.d.ts.map +1 -1
- package/dist/dispatcher/dispatcher.d.ts +26 -5
- package/dist/dispatcher/dispatcher.d.ts.map +1 -1
- package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +2787 -1893
- package/dist/index.mjs.map +1 -1
- package/dist/managers/app/app.manager.constants.d.ts.map +1 -1
- package/dist/managers/app/app.manager.d.ts +9 -0
- package/dist/managers/app/app.manager.d.ts.map +1 -1
- package/dist/managers/app/app.manager.utils.d.ts.map +1 -1
- package/dist/managers/logger/logger.manager.d.ts.map +1 -1
- package/dist/managers/request/request.manager.d.ts.map +1 -1
- package/dist/managers/request/request.manager.types.d.ts +2 -0
- package/dist/managers/request/request.manager.types.d.ts.map +1 -1
- package/dist/mocker/mocker.d.ts +1 -1
- package/dist/mocker/mocker.d.ts.map +1 -1
- package/dist/optimistic/index.d.ts +5 -0
- package/dist/optimistic/index.d.ts.map +1 -0
- package/dist/optimistic/optimistic.d.ts +67 -0
- package/dist/optimistic/optimistic.d.ts.map +1 -0
- package/dist/optimistic/optimistic.settlement.d.ts +60 -0
- package/dist/optimistic/optimistic.settlement.d.ts.map +1 -0
- package/dist/optimistic/optimistic.types.d.ts +109 -0
- package/dist/optimistic/optimistic.types.d.ts.map +1 -0
- package/dist/optimistic/optimistic.utils.d.ts +29 -0
- package/dist/optimistic/optimistic.utils.d.ts.map +1 -0
- package/dist/plugin/plugin.d.ts +2 -0
- package/dist/plugin/plugin.d.ts.map +1 -1
- package/dist/request/request.d.ts +14 -6
- package/dist/request/request.d.ts.map +1 -1
- package/dist/request/request.types.d.ts +40 -1
- package/dist/request/request.types.d.ts.map +1 -1
- package/dist/request/request.utils.d.ts.map +1 -1
- package/dist/utils/emitter.utils.d.ts.map +1 -1
- package/dist/utils/error.utils.d.ts +17 -0
- package/dist/utils/error.utils.d.ts.map +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/adapter/adapter.bindings.ts +46 -18
- package/src/adapter/adapter.utils.ts +47 -7
- package/src/cache/cache.events.ts +24 -8
- package/src/cache/cache.ts +110 -21
- package/src/cache/cache.utils.ts +33 -10
- package/src/client/client.ts +101 -7
- package/src/client/client.utils.ts +10 -4
- package/src/dispatcher/dispatcher.ts +285 -132
- package/src/http-adapter/http-adapter.fetch.ts +15 -3
- package/src/index.ts +4 -0
- package/src/managers/app/app.manager.constants.ts +12 -1
- package/src/managers/app/app.manager.ts +43 -23
- package/src/managers/app/app.manager.utils.ts +8 -4
- package/src/managers/logger/logger.manager.ts +21 -8
- package/src/managers/request/request.manager.ts +7 -1
- package/src/managers/request/request.manager.types.ts +2 -0
- package/src/mocker/mocker.ts +14 -11
- package/src/optimistic/index.ts +4 -0
- package/src/optimistic/optimistic.settlement.ts +406 -0
- package/src/optimistic/optimistic.ts +162 -0
- package/src/optimistic/optimistic.types.ts +175 -0
- package/src/optimistic/optimistic.utils.ts +112 -0
- package/src/plugin/plugin.ts +6 -0
- package/src/request/request.ts +50 -28
- package/src/request/request.types.ts +41 -1
- package/src/request/request.utils.ts +51 -66
- package/src/utils/emitter.utils.ts +9 -2
- package/src/utils/error.utils.ts +29 -0
- package/src/utils/index.ts +1 -0
- package/tsconfig.json +2 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { Client } from "client";
|
|
2
|
+
import { xhrExtra } from "http-adapter";
|
|
3
|
+
import { scopeKey } from "request";
|
|
4
|
+
|
|
5
|
+
const createResponse = (data: unknown) => ({
|
|
6
|
+
data,
|
|
7
|
+
error: null,
|
|
8
|
+
status: 200,
|
|
9
|
+
success: true as const,
|
|
10
|
+
extra: xhrExtra,
|
|
11
|
+
retries: 0,
|
|
12
|
+
isCanceled: false,
|
|
13
|
+
isOffline: false,
|
|
14
|
+
willRetry: false,
|
|
15
|
+
addedTimestamp: Date.now(),
|
|
16
|
+
triggerTimestamp: Date.now(),
|
|
17
|
+
requestTimestamp: Date.now(),
|
|
18
|
+
responseTimestamp: Date.now(),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Cache events are emitted by the logical `cacheKey` of the request. Scopes share that key, so every event carries
|
|
23
|
+
* what a listener needs to tell the scopes apart: the data event has the `scope`, the invalidation and deletion
|
|
24
|
+
* events have the key of the stored element (`scopeKey(cacheKey, scope)`).
|
|
25
|
+
*/
|
|
26
|
+
describe("Cache [ Scoped events ]", () => {
|
|
27
|
+
let client = new Client({ url: "shared-base-url" });
|
|
28
|
+
let request = client.createRequest<{ response: unknown }>()({ endpoint: "/profile" });
|
|
29
|
+
let userA = request.setScope("user-a");
|
|
30
|
+
let userB = request.setScope("user-b");
|
|
31
|
+
|
|
32
|
+
const storageKeyOf = (scoped: typeof userA) => scopeKey(scoped.cacheKey, scoped.scope);
|
|
33
|
+
|
|
34
|
+
beforeEach(() => {
|
|
35
|
+
client = new Client({ url: "shared-base-url" });
|
|
36
|
+
request = client.createRequest<{ response: unknown }>()({ endpoint: "/profile" });
|
|
37
|
+
userA = request.setScope("user-a");
|
|
38
|
+
userB = request.setScope("user-b");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
afterEach(() => {
|
|
42
|
+
vi.useRealTimers();
|
|
43
|
+
client.clear();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe("When the data of a scoped request is set", () => {
|
|
47
|
+
it("should emit it by the cache key of the request with the scope it belongs to", () => {
|
|
48
|
+
const scopes: (string | null | undefined)[] = [];
|
|
49
|
+
client.cache.events.onDataByKey(userA.cacheKey, (data) => scopes.push(data.scope));
|
|
50
|
+
|
|
51
|
+
client.cache.set(userA, createResponse("data of user A"));
|
|
52
|
+
client.cache.set(userB, createResponse("data of user B"));
|
|
53
|
+
|
|
54
|
+
expect(scopes).toStrictEqual(["user-a", "user-b"]);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe("When a scoped element is deleted", () => {
|
|
59
|
+
it("should notify the listeners of the request cache key", () => {
|
|
60
|
+
const spy = vi.fn();
|
|
61
|
+
client.cache.set(userA, createResponse("data"));
|
|
62
|
+
client.cache.events.onDeleteByKey(userA.cacheKey, spy);
|
|
63
|
+
|
|
64
|
+
client.cache.delete(storageKeyOf(userA));
|
|
65
|
+
|
|
66
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("should pass the key of the deleted element, so listeners can tell the scopes apart", () => {
|
|
70
|
+
const deleted: string[] = [];
|
|
71
|
+
client.cache.set(userA, createResponse("a"));
|
|
72
|
+
client.cache.set(userB, createResponse("b"));
|
|
73
|
+
client.cache.events.onDeleteByKey(request.cacheKey, (key) => deleted.push(key));
|
|
74
|
+
|
|
75
|
+
client.cache.delete(storageKeyOf(userB));
|
|
76
|
+
|
|
77
|
+
expect(deleted).toStrictEqual([storageKeyOf(userB)]);
|
|
78
|
+
expect(client.cache.get(storageKeyOf(userA))?.data).toBe("a");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("should notify the listeners of the request cache key when the element is garbage collected", async () => {
|
|
82
|
+
vi.useFakeTimers();
|
|
83
|
+
const collected: string[] = [];
|
|
84
|
+
const shortLived = userA.setCacheTime(50).setStaleTime(10);
|
|
85
|
+
client.cache.set(shortLived, createResponse("data"));
|
|
86
|
+
client.cache.events.onDeleteByKey(shortLived.cacheKey, (key) => collected.push(key));
|
|
87
|
+
|
|
88
|
+
await vi.advanceTimersByTimeAsync(100);
|
|
89
|
+
|
|
90
|
+
expect(collected).toStrictEqual([storageKeyOf(shortLived)]);
|
|
91
|
+
expect(client.cache.get(storageKeyOf(shortLived))).toBeUndefined();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("should report the deleted key to the listeners of all keys", () => {
|
|
95
|
+
const deleted: string[] = [];
|
|
96
|
+
client.cache.set(userA, createResponse("a"));
|
|
97
|
+
client.cache.events.onDelete((key) => deleted.push(key));
|
|
98
|
+
|
|
99
|
+
client.cache.delete(storageKeyOf(userA));
|
|
100
|
+
|
|
101
|
+
expect(deleted).toStrictEqual([storageKeyOf(userA)]);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("should still notify by the given key when there is no such element", () => {
|
|
105
|
+
const spy = vi.fn();
|
|
106
|
+
client.cache.events.onDeleteByKey("unknown-key", spy);
|
|
107
|
+
|
|
108
|
+
client.cache.delete("unknown-key");
|
|
109
|
+
|
|
110
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
111
|
+
expect(spy).toHaveBeenCalledWith("unknown-key");
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe("When scoped elements are invalidated", () => {
|
|
116
|
+
it.each([
|
|
117
|
+
["the whole cache", () => client.cache.invalidate()],
|
|
118
|
+
["a regular expression", () => client.cache.invalidate(/profile/)],
|
|
119
|
+
["the key of the stored element", () => client.cache.invalidate(storageKeyOf(userA))],
|
|
120
|
+
["the request", () => client.cache.invalidate(userA)],
|
|
121
|
+
["a list of requests", () => client.cache.invalidate([userA])],
|
|
122
|
+
])("should notify the listeners of the request cache key when invalidating %s", (_name, invalidate) => {
|
|
123
|
+
const invalidated: string[] = [];
|
|
124
|
+
client.cache.set(userA, createResponse("data"));
|
|
125
|
+
client.cache.events.onInvalidateByKey(userA.cacheKey, (key) => invalidated.push(key));
|
|
126
|
+
|
|
127
|
+
invalidate();
|
|
128
|
+
|
|
129
|
+
expect(invalidated).toStrictEqual([storageKeyOf(userA)]);
|
|
130
|
+
expect(client.cache.get(storageKeyOf(userA))?.staleTime).toBe(0);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("should pass the key of every invalidated element, so listeners can tell the scopes apart", () => {
|
|
134
|
+
const invalidated: string[] = [];
|
|
135
|
+
client.cache.set(userA, createResponse("a"));
|
|
136
|
+
client.cache.set(userB, createResponse("b"));
|
|
137
|
+
client.cache.events.onInvalidateByKey(request.cacheKey, (key) => invalidated.push(key));
|
|
138
|
+
|
|
139
|
+
client.cache.invalidate(userB);
|
|
140
|
+
|
|
141
|
+
expect(invalidated).toStrictEqual([storageKeyOf(userB)]);
|
|
142
|
+
expect(client.cache.get(storageKeyOf(userA))?.staleTime).not.toBe(0);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("should notify once per scope when the whole cache is invalidated", () => {
|
|
146
|
+
const invalidated: string[] = [];
|
|
147
|
+
client.cache.set(userA, createResponse("a"));
|
|
148
|
+
client.cache.set(userB, createResponse("b"));
|
|
149
|
+
client.cache.events.onInvalidateByKey(request.cacheKey, (key) => invalidated.push(key));
|
|
150
|
+
|
|
151
|
+
client.cache.invalidate();
|
|
152
|
+
|
|
153
|
+
expect(invalidated.toSorted()).toStrictEqual([storageKeyOf(userA), storageKeyOf(userB)].toSorted());
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("should notify the listeners of a request that has nothing cached yet", () => {
|
|
157
|
+
const invalidated: string[] = [];
|
|
158
|
+
client.cache.events.onInvalidateByKey(userA.cacheKey, (key) => invalidated.push(key));
|
|
159
|
+
|
|
160
|
+
client.cache.invalidate(userA);
|
|
161
|
+
|
|
162
|
+
expect(invalidated).toStrictEqual([storageKeyOf(userA)]);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
describe("When the request has no scope", () => {
|
|
167
|
+
it("should emit the deletion and the invalidation by the cache key, with the cache key", () => {
|
|
168
|
+
const onDelete = vi.fn();
|
|
169
|
+
const onInvalidate = vi.fn();
|
|
170
|
+
client.cache.set(request, createResponse("data"));
|
|
171
|
+
client.cache.events.onInvalidateByKey(request.cacheKey, onInvalidate);
|
|
172
|
+
client.cache.events.onDeleteByKey(request.cacheKey, onDelete);
|
|
173
|
+
|
|
174
|
+
client.cache.invalidate(request);
|
|
175
|
+
client.cache.delete(request.cacheKey);
|
|
176
|
+
|
|
177
|
+
expect(onInvalidate).toHaveBeenCalledTimes(1);
|
|
178
|
+
expect(onInvalidate).toHaveBeenCalledWith(request.cacheKey);
|
|
179
|
+
expect(onDelete).toHaveBeenCalledTimes(1);
|
|
180
|
+
expect(onDelete).toHaveBeenCalledWith(request.cacheKey);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
});
|