@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,304 @@
|
|
|
1
|
+
import { Client } from "client";
|
|
2
|
+
import type { RequestInstance, RequestModel } from "request";
|
|
3
|
+
import { Request } from "request";
|
|
4
|
+
import { createConfiguration, createSdk } from "sdk";
|
|
5
|
+
|
|
6
|
+
type User = { id: string; name: string };
|
|
7
|
+
|
|
8
|
+
type Schema = {
|
|
9
|
+
users: {
|
|
10
|
+
$get: RequestModel<{ response: User[]; endpoint: "/users" }>;
|
|
11
|
+
$post: RequestModel<{ response: User; payload: { name: string }; endpoint: "/users" }>;
|
|
12
|
+
$userId: {
|
|
13
|
+
$get: RequestModel<{ response: User; endpoint: "/users/:userId" }>;
|
|
14
|
+
$delete: RequestModel<{ response: null; endpoint: "/users/:userId" }>;
|
|
15
|
+
posts: {
|
|
16
|
+
$get: RequestModel<{ response: { id: string }[]; endpoint: "/users/:userId/posts" }>;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
usersArchive: {
|
|
21
|
+
$get: RequestModel<{ response: User[]; endpoint: "/users-archive" }>;
|
|
22
|
+
};
|
|
23
|
+
posts: {
|
|
24
|
+
$get: RequestModel<{ response: { id: string }[]; endpoint: "/posts"; queryParams: { page: number } }>;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
describe("SDK [ Builder ]", () => {
|
|
29
|
+
let client = new Client({ url: "shared-base-url" });
|
|
30
|
+
let sdk = createSdk<typeof client, Schema>(client);
|
|
31
|
+
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
client = new Client({ url: "shared-base-url" });
|
|
34
|
+
sdk = createSdk<typeof client, Schema>(client);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
afterEach(() => {
|
|
38
|
+
client.clear();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe("When the requests are built from the path", () => {
|
|
42
|
+
it.each([
|
|
43
|
+
{ name: "collection", getRequest: () => sdk.users.$get, endpoint: "/users", method: "GET" },
|
|
44
|
+
{ name: "collection with another method", getRequest: () => sdk.users.$post, endpoint: "/users", method: "POST" },
|
|
45
|
+
{ name: "dynamic segment", getRequest: () => sdk.users.$userId.$get, endpoint: "/users/:userId", method: "GET" },
|
|
46
|
+
{
|
|
47
|
+
name: "dynamic segment with another method",
|
|
48
|
+
getRequest: () => sdk.users.$userId.$delete,
|
|
49
|
+
endpoint: "/users/:userId",
|
|
50
|
+
method: "DELETE",
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
name: "nested resource",
|
|
54
|
+
getRequest: () => sdk.users.$userId.posts.$get,
|
|
55
|
+
endpoint: "/users/:userId/posts",
|
|
56
|
+
method: "GET",
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: "camel case segment",
|
|
60
|
+
getRequest: () => sdk.usersArchive.$get,
|
|
61
|
+
endpoint: "/users-archive",
|
|
62
|
+
method: "GET",
|
|
63
|
+
},
|
|
64
|
+
])("should build the $name request", ({ getRequest, endpoint, method }) => {
|
|
65
|
+
const request: RequestInstance = getRequest();
|
|
66
|
+
|
|
67
|
+
expect(request).toBeInstanceOf(Request);
|
|
68
|
+
expect(request.endpoint).toBe(endpoint);
|
|
69
|
+
expect(request.method).toBe(method);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("should bind the requests to the client of the sdk", () => {
|
|
73
|
+
expect(sdk.users.$get.client).toBe(client);
|
|
74
|
+
expect(sdk.users.$userId.posts.$get.client).toBe(client);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("should create a new request with every access", () => {
|
|
78
|
+
const first = sdk.users.$get;
|
|
79
|
+
const second = sdk.users.$get;
|
|
80
|
+
|
|
81
|
+
expect(first).not.toBe(second);
|
|
82
|
+
expect(first.toJSON()).toStrictEqual(second.toJSON());
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("should keep the camel case segments when the conversion is disabled", () => {
|
|
86
|
+
const rawSdk = createSdk<typeof client, Schema>(client, { camelCaseToKebabCase: false });
|
|
87
|
+
|
|
88
|
+
expect(rawSdk.usersArchive.$get.endpoint).toBe("/usersArchive");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("should transform the methods with the custom function", () => {
|
|
92
|
+
const lowerSdk = createSdk<typeof client, Schema>(client, { methodTransform: (method) => method.toLowerCase() });
|
|
93
|
+
|
|
94
|
+
expect(lowerSdk.users.$post.method).toBe("post");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("should serialize the built request", () => {
|
|
98
|
+
const serialized = JSON.stringify(sdk.users.$userId.$get);
|
|
99
|
+
const json = JSON.parse(serialized);
|
|
100
|
+
|
|
101
|
+
expect(json.endpoint).toBe("/users/:userId");
|
|
102
|
+
expect(json.method).toBe("GET");
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe("When the built requests are used", () => {
|
|
107
|
+
it("should apply the params and the query params", () => {
|
|
108
|
+
const withParams = sdk.users.$userId.$get.setParams({ userId: "1" });
|
|
109
|
+
const withQueryParams = sdk.posts.$get.setQueryParams({ page: 2 });
|
|
110
|
+
|
|
111
|
+
expect(withParams.endpoint).toBe("/users/1");
|
|
112
|
+
expect(withQueryParams.queryParams).toStrictEqual({ page: 2 });
|
|
113
|
+
expect(withQueryParams.cacheKey).not.toBe(sdk.posts.$get.cacheKey);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("should not change the request of the sdk with the setters", () => {
|
|
117
|
+
sdk.users.$get.setRetry(5).setCacheTime(1);
|
|
118
|
+
|
|
119
|
+
expect(sdk.users.$get.retry).toBe(0);
|
|
120
|
+
expect(sdk.users.$get.cacheTime).not.toBe(1);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("should send the request and cache its response", async () => {
|
|
124
|
+
const request = sdk.users.$userId.$get.setParams({ userId: "1" });
|
|
125
|
+
request.setMock(() => ({ data: { id: "1", name: "Kate" }, status: 200 }));
|
|
126
|
+
|
|
127
|
+
const response = await request.send();
|
|
128
|
+
|
|
129
|
+
expect(response.data).toStrictEqual({ id: "1", name: "Kate" });
|
|
130
|
+
expect(request.read()?.data).toStrictEqual({ id: "1", name: "Kate" });
|
|
131
|
+
expect(client.cache.keys()).toStrictEqual([request.cacheKey]);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("should share the cache between the requests built for the same endpoint", async () => {
|
|
135
|
+
const request = sdk.users.$get;
|
|
136
|
+
request.setMock(() => ({ data: [{ id: "1", name: "Kate" }], status: 200 }));
|
|
137
|
+
|
|
138
|
+
await request.send();
|
|
139
|
+
|
|
140
|
+
expect(sdk.users.$get.read()?.data).toStrictEqual([{ id: "1", name: "Kate" }]);
|
|
141
|
+
expect(sdk.posts.$get.read()).toBeUndefined();
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe("When the sdk is configured", () => {
|
|
146
|
+
it("should return a new sdk and leave the original one untouched", () => {
|
|
147
|
+
const configured = sdk.$configure({ "*": { retry: 3 } });
|
|
148
|
+
|
|
149
|
+
expect(configured).not.toBe(sdk);
|
|
150
|
+
expect(configured.users.$get.retry).toBe(3);
|
|
151
|
+
expect(sdk.users.$get.retry).toBe(0);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("should merge the configurations of the chained calls", () => {
|
|
155
|
+
const configured = sdk.$configure({ "*": { retry: 3 } }).$configure({ "/users": { cacheTime: 1000 } });
|
|
156
|
+
|
|
157
|
+
expect(configured.users.$get.retry).toBe(3);
|
|
158
|
+
expect(configured.users.$get.cacheTime).toBe(1000);
|
|
159
|
+
expect(configured.posts.$get.retry).toBe(3);
|
|
160
|
+
expect(configured.posts.$get.cacheTime).not.toBe(1000);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("should replace the configuration of the key configured again", () => {
|
|
164
|
+
const configured = sdk.$configure({ "*": { retry: 3, cacheTime: 1000 } }).$configure({ "*": { retry: 5 } });
|
|
165
|
+
|
|
166
|
+
expect(configured.users.$get.retry).toBe(5);
|
|
167
|
+
expect(configured.users.$get.cacheTime).not.toBe(1000);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("should merge the configuration with the defaults of the sdk", () => {
|
|
171
|
+
const withDefaults = createSdk<typeof client, Schema>(client, { defaults: { "*": { retry: 2 } } });
|
|
172
|
+
|
|
173
|
+
const configured = withDefaults.$configure({ "/posts": { retry: 4 } });
|
|
174
|
+
|
|
175
|
+
expect(withDefaults.posts.$get.retry).toBe(2);
|
|
176
|
+
expect(configured.users.$get.retry).toBe(2);
|
|
177
|
+
expect(configured.posts.$get.retry).toBe(4);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("should keep the options of the sdk in the configured one", () => {
|
|
181
|
+
const rawSdk = createSdk<typeof client, Schema>(client, { camelCaseToKebabCase: false });
|
|
182
|
+
|
|
183
|
+
expect(rawSdk.$configure({ "*": { retry: 1 } }).usersArchive.$get.endpoint).toBe("/usersArchive");
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it.each([
|
|
187
|
+
{ name: "retry", defaults: { retry: 0 }, read: (request: RequestInstance) => request.retry, expected: 0 },
|
|
188
|
+
{ name: "cache", defaults: { cache: false }, read: (request: RequestInstance) => request.cache, expected: false },
|
|
189
|
+
{ name: "auth", defaults: { auth: false }, read: (request: RequestInstance) => request.auth, expected: false },
|
|
190
|
+
{
|
|
191
|
+
name: "staleTime",
|
|
192
|
+
defaults: { staleTime: 0 },
|
|
193
|
+
read: (request: RequestInstance) => request.staleTime,
|
|
194
|
+
expected: 0,
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
name: "cacheTime",
|
|
198
|
+
defaults: { cacheTime: 0 },
|
|
199
|
+
read: (request: RequestInstance) => request.cacheTime,
|
|
200
|
+
expected: 0,
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
name: "deduplicate",
|
|
204
|
+
defaults: { deduplicate: false },
|
|
205
|
+
read: (request: RequestInstance) => request.deduplicate,
|
|
206
|
+
expected: false,
|
|
207
|
+
},
|
|
208
|
+
])("should apply the falsy value of $name", ({ defaults, read, expected }) => {
|
|
209
|
+
const configured = sdk.$configure({ "*": { retry: 5, staleTime: 1000, cacheTime: 1000 }, "/users": defaults });
|
|
210
|
+
|
|
211
|
+
expect(read(configured.users.$get)).toBe(expected);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("should apply the configuration in the documented order no matter how the keys are sorted", () => {
|
|
215
|
+
const configured = sdk.$configure({
|
|
216
|
+
"users.$get": { retry: 3 },
|
|
217
|
+
"/users": { retry: 2, cacheTime: 2000 },
|
|
218
|
+
"*": { retry: 1, cacheTime: 1000, staleTime: 500 },
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
expect(configured.users.$get.retry).toBe(3);
|
|
222
|
+
expect(configured.users.$get.cacheTime).toBe(2000);
|
|
223
|
+
expect(configured.users.$get.staleTime).toBe(500);
|
|
224
|
+
expect(configured.users.$post.retry).toBe(2);
|
|
225
|
+
expect(configured.posts.$get.retry).toBe(1);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("should pass the request to the configuration function and use the returned one", () => {
|
|
229
|
+
const configuration = vi.fn((request: RequestInstance) => request.setRetry(7));
|
|
230
|
+
const configured = sdk.$configure({ "users.$userId.$get": configuration });
|
|
231
|
+
|
|
232
|
+
const request = configured.users.$userId.$get;
|
|
233
|
+
|
|
234
|
+
expect(request.retry).toBe(7);
|
|
235
|
+
expect(configuration).toHaveBeenCalledTimes(1);
|
|
236
|
+
expect(configuration.mock.calls[0][0].endpoint).toBe("/users/:userId");
|
|
237
|
+
expect(configured.users.$userId.$delete.retry).toBe(0);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("should return the same map from the configuration factory", () => {
|
|
241
|
+
const configuration = { "*": { retry: 1 } };
|
|
242
|
+
|
|
243
|
+
expect(createConfiguration<Schema>()(configuration)).toBe(configuration);
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
describe("When the configuration keys are matched", () => {
|
|
248
|
+
it("should match the endpoint and everything below it with the wildcard", () => {
|
|
249
|
+
const configured = sdk.$configure({ "/users/*": { retry: 4 } });
|
|
250
|
+
|
|
251
|
+
expect(configured.users.$get.retry).toBe(4);
|
|
252
|
+
expect(configured.users.$userId.$get.retry).toBe(4);
|
|
253
|
+
expect(configured.users.$userId.posts.$get.retry).toBe(4);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it("should not match the endpoints which only start with the same text", () => {
|
|
257
|
+
const configured = sdk.$configure({ "/users/*": { retry: 4 }, "/users": { cacheTime: 1 } });
|
|
258
|
+
|
|
259
|
+
expect(configured.usersArchive.$get.retry).toBe(0);
|
|
260
|
+
expect(configured.usersArchive.$get.cacheTime).not.toBe(1);
|
|
261
|
+
expect(configured.posts.$get.retry).toBe(0);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it("should match only the exact endpoint with the endpoint key", () => {
|
|
265
|
+
const configured = sdk.$configure({ "/users": { retry: 4 } });
|
|
266
|
+
|
|
267
|
+
expect(configured.users.$get.retry).toBe(4);
|
|
268
|
+
expect(configured.users.$post.retry).toBe(4);
|
|
269
|
+
expect(configured.users.$userId.$get.retry).toBe(0);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("should match only the given method with the path key", () => {
|
|
273
|
+
const configured = sdk.$configure({ "users.$userId.$delete": { retry: 4 } });
|
|
274
|
+
|
|
275
|
+
expect(configured.users.$userId.$delete.retry).toBe(4);
|
|
276
|
+
expect(configured.users.$userId.$get.retry).toBe(0);
|
|
277
|
+
expect(configured.users.$get.retry).toBe(0);
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
describe("When many sdks are used", () => {
|
|
282
|
+
it("should not share the configuration between the sdks", () => {
|
|
283
|
+
const first = sdk.$configure({ "*": { retry: 1 } });
|
|
284
|
+
const second = sdk.$configure({ "*": { cacheTime: 1000 } });
|
|
285
|
+
|
|
286
|
+
expect(first.users.$get.cacheTime).not.toBe(1000);
|
|
287
|
+
expect(second.users.$get.retry).toBe(0);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
it("should bind every sdk to its own client", async () => {
|
|
291
|
+
const otherClient = new Client({ url: "other-base-url" });
|
|
292
|
+
const otherSdk = createSdk<typeof otherClient, Schema>(otherClient);
|
|
293
|
+
const request = sdk.users.$get;
|
|
294
|
+
request.setMock(() => ({ data: [], status: 200 }));
|
|
295
|
+
|
|
296
|
+
await request.send();
|
|
297
|
+
|
|
298
|
+
expect(otherSdk.users.$get.client).toBe(otherClient);
|
|
299
|
+
expect(otherSdk.users.$get.read()).toBeUndefined();
|
|
300
|
+
expect(sdk.users.$get.read()?.data).toStrictEqual([]);
|
|
301
|
+
otherClient.clear();
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
});
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { HttpMethods, SAFE_HTTP_METHODS } from "constants/http.constants";
|
|
2
|
+
import { Time } from "constants/time.constants";
|
|
3
|
+
import { EventEmitter, getUniqueRequestId } from "utils";
|
|
4
|
+
|
|
5
|
+
describe("EventEmitter [ Listeners ]", () => {
|
|
6
|
+
let emitter = new EventEmitter();
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
emitter = new EventEmitter();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe("When the listeners are counted", () => {
|
|
13
|
+
it("should report the number of the listeners when they are added and removed", () => {
|
|
14
|
+
const counts: number[] = [];
|
|
15
|
+
const first = () => {};
|
|
16
|
+
const second = () => {};
|
|
17
|
+
emitter.onListener("event", (count) => counts.push(count));
|
|
18
|
+
|
|
19
|
+
emitter.on("event", first);
|
|
20
|
+
emitter.on("event", second);
|
|
21
|
+
emitter.off("event", first);
|
|
22
|
+
emitter.off("event", second);
|
|
23
|
+
|
|
24
|
+
expect(counts).toStrictEqual([1, 2, 1, 0]);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("should report the listeners managed with the aliases", () => {
|
|
28
|
+
const counts: number[] = [];
|
|
29
|
+
const listener = () => {};
|
|
30
|
+
emitter.onListener("event", (count) => counts.push(count));
|
|
31
|
+
|
|
32
|
+
emitter.addListener("event", listener);
|
|
33
|
+
emitter.removeListener("event", listener);
|
|
34
|
+
|
|
35
|
+
expect(counts).toStrictEqual([1, 0]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should report only the listeners of the observed event", () => {
|
|
39
|
+
const spy = vi.fn();
|
|
40
|
+
emitter.onListener("event", spy);
|
|
41
|
+
|
|
42
|
+
emitter.on("other", () => {});
|
|
43
|
+
|
|
44
|
+
expect(spy).not.toHaveBeenCalled();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("should stop reporting after the observer is removed", () => {
|
|
48
|
+
const spy = vi.fn();
|
|
49
|
+
const unsubscribe = emitter.onListener("event", spy);
|
|
50
|
+
|
|
51
|
+
unsubscribe();
|
|
52
|
+
emitter.on("event", () => {});
|
|
53
|
+
|
|
54
|
+
expect(spy).not.toHaveBeenCalled();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("should report the removal of the listener which was called once", () => {
|
|
58
|
+
const counts: number[] = [];
|
|
59
|
+
const spy = vi.fn();
|
|
60
|
+
emitter.onListener("event", (count) => counts.push(count));
|
|
61
|
+
|
|
62
|
+
emitter.once("event", spy);
|
|
63
|
+
emitter.emit("event", "data", false);
|
|
64
|
+
emitter.emit("event", "data", false);
|
|
65
|
+
|
|
66
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
67
|
+
expect(counts).toStrictEqual([1, 0]);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("should return the emitter from the on and off methods", () => {
|
|
71
|
+
const listener = () => {};
|
|
72
|
+
|
|
73
|
+
expect(emitter.on("event", listener)).toBe(emitter);
|
|
74
|
+
expect(emitter.off("event", listener)).toBe(emitter);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe("When the events are emitted", () => {
|
|
79
|
+
it("should tell whether the event had any listeners", () => {
|
|
80
|
+
expect(emitter.emit("event", "data", false)).toBeFalse();
|
|
81
|
+
|
|
82
|
+
emitter.on("event", () => {});
|
|
83
|
+
|
|
84
|
+
expect(emitter.emit("event", "data", false)).toBeTrue();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("should call the listeners in the order they were added", () => {
|
|
88
|
+
const order: number[] = [];
|
|
89
|
+
emitter.on("event", () => order.push(1));
|
|
90
|
+
emitter.on("event", () => order.push(2));
|
|
91
|
+
emitter.on("event", () => order.push(3));
|
|
92
|
+
|
|
93
|
+
emitter.emit("event", "data", false);
|
|
94
|
+
|
|
95
|
+
expect(order).toStrictEqual([1, 2, 3]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("should call the emit observers before the listeners", () => {
|
|
99
|
+
const order: string[] = [];
|
|
100
|
+
emitter.on("event", () => order.push("listener"));
|
|
101
|
+
emitter.onEmit(() => order.push("observer"));
|
|
102
|
+
|
|
103
|
+
emitter.emit("event", "data", false);
|
|
104
|
+
|
|
105
|
+
expect(order).toStrictEqual(["observer", "listener"]);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("should call the emit observers for the events without listeners", () => {
|
|
109
|
+
const spy = vi.fn();
|
|
110
|
+
emitter.onEmit(spy);
|
|
111
|
+
|
|
112
|
+
emitter.emit("event", { id: 1 }, false);
|
|
113
|
+
|
|
114
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
115
|
+
expect(spy).toHaveBeenCalledWith("event", { id: 1 });
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("should call the remaining listeners when one of them removes itself during the emit", () => {
|
|
119
|
+
const spy = vi.fn();
|
|
120
|
+
const selfRemoving = () => emitter.off("event", selfRemoving);
|
|
121
|
+
emitter.on("event", selfRemoving);
|
|
122
|
+
emitter.on("event", spy);
|
|
123
|
+
|
|
124
|
+
emitter.emit("event", "data", false);
|
|
125
|
+
emitter.emit("event", "data", false);
|
|
126
|
+
|
|
127
|
+
expect(spy).toHaveBeenCalledTimes(2);
|
|
128
|
+
expect(emitter.listenerCount("event")).toBe(1);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("should not call the listeners of other events", () => {
|
|
132
|
+
const spy = vi.fn();
|
|
133
|
+
emitter.on("other", spy);
|
|
134
|
+
|
|
135
|
+
emitter.emit("event", "data", false);
|
|
136
|
+
|
|
137
|
+
expect(spy).not.toHaveBeenCalled();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("should remove every listener at once", () => {
|
|
141
|
+
const spy = vi.fn();
|
|
142
|
+
emitter.on("event", spy);
|
|
143
|
+
emitter.on("other", spy);
|
|
144
|
+
|
|
145
|
+
emitter.removeAllListeners();
|
|
146
|
+
emitter.emit("event", "data", false);
|
|
147
|
+
emitter.emit("other", "data", false);
|
|
148
|
+
|
|
149
|
+
expect(spy).not.toHaveBeenCalled();
|
|
150
|
+
expect(emitter.eventNames()).toHaveLength(0);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("should not share the listeners between the emitters", () => {
|
|
154
|
+
const spy = vi.fn();
|
|
155
|
+
const other = new EventEmitter();
|
|
156
|
+
other.on("event", spy);
|
|
157
|
+
other.onEmit(spy);
|
|
158
|
+
|
|
159
|
+
emitter.emit("event", "data", false);
|
|
160
|
+
|
|
161
|
+
expect(spy).not.toHaveBeenCalled();
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
describe("Utils [ Request Id ]", () => {
|
|
167
|
+
it("should start the request id with the query key", () => {
|
|
168
|
+
expect(getUniqueRequestId({ queryKey: "GET_/users" }).startsWith("GET_/users_")).toBeTrue();
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("should generate a different id with every call", () => {
|
|
172
|
+
const ids = new Set(Array.from({ length: 2000 }, () => getUniqueRequestId({ queryKey: "key" })));
|
|
173
|
+
|
|
174
|
+
expect(ids.size).toBe(2000);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("should generate the ids for the empty query key", () => {
|
|
178
|
+
expect(getUniqueRequestId({ queryKey: "" })).toMatch(/^_[0-9a-z]+_[0-9a-z]+$/);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe("Constants [ Values ]", () => {
|
|
183
|
+
it("should define the time units in milliseconds", () => {
|
|
184
|
+
expect(Time.SEC).toBe(1000);
|
|
185
|
+
expect(Time.MIN).toBe(60 * Time.SEC);
|
|
186
|
+
expect(Time.HOUR).toBe(60 * Time.MIN);
|
|
187
|
+
expect(Time.DAY).toBe(24 * Time.HOUR);
|
|
188
|
+
expect(Time.WEEK).toBe(7 * Time.DAY);
|
|
189
|
+
expect(Time.MONTH_30).toBe(30 * Time.DAY);
|
|
190
|
+
expect(Time.MONTH_31).toBe(31 * Time.DAY);
|
|
191
|
+
expect(Time.YEAR).toBe(365 * Time.DAY);
|
|
192
|
+
expect(Time.YEAR_LEAP).toBe(366 * Time.DAY);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("should define the http methods by their names", () => {
|
|
196
|
+
Object.entries(HttpMethods).forEach(([name, value]) => expect(value).toBe(name));
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("should treat only the reading methods as safe", () => {
|
|
200
|
+
expect(SAFE_HTTP_METHODS).toStrictEqual(["GET", "QUERY"]);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { Client } from "client";
|
|
2
|
+
import type { ClientOptionsType } from "client";
|
|
3
|
+
import type { RequestInstance } from "request";
|
|
4
|
+
|
|
5
|
+
export type ControlledCall = {
|
|
6
|
+
request: RequestInstance;
|
|
7
|
+
requestId: string;
|
|
8
|
+
/** True once the call got its outcome - success, error or abort */
|
|
9
|
+
isSettled: () => boolean;
|
|
10
|
+
isAborted: () => boolean;
|
|
11
|
+
succeed: (data?: unknown, status?: number) => Promise<void>;
|
|
12
|
+
fail: (error?: unknown, status?: number) => Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Client with an adapter which never answers on its own. Every request that reaches the adapter is listed in
|
|
17
|
+
* `calls` and stays "in flight" until the test decides about its outcome, so tests do not depend on timing.
|
|
18
|
+
*/
|
|
19
|
+
export const createControlledClient = (options?: Partial<ClientOptionsType<any>>) => {
|
|
20
|
+
const client = new Client({ url: "http://controlled", ...options });
|
|
21
|
+
const calls: ControlledCall[] = [];
|
|
22
|
+
const extra = { headers: {} };
|
|
23
|
+
const defaultData: unknown = { ok: true };
|
|
24
|
+
const defaultError: unknown = { message: "Failed" };
|
|
25
|
+
|
|
26
|
+
client.adapter.setFetcher(
|
|
27
|
+
async ({
|
|
28
|
+
request,
|
|
29
|
+
requestId,
|
|
30
|
+
onSuccess,
|
|
31
|
+
onError,
|
|
32
|
+
createAbortListener,
|
|
33
|
+
onBeforeRequest,
|
|
34
|
+
onRequestStart,
|
|
35
|
+
onRequestEnd,
|
|
36
|
+
onResponseStart,
|
|
37
|
+
onResponseEnd,
|
|
38
|
+
}) => {
|
|
39
|
+
let settled = false;
|
|
40
|
+
let aborted = false;
|
|
41
|
+
|
|
42
|
+
const unmountListener = createAbortListener({
|
|
43
|
+
status: 0,
|
|
44
|
+
extra,
|
|
45
|
+
onAbort: () => {
|
|
46
|
+
settled = true;
|
|
47
|
+
aborted = true;
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const settle = () => {
|
|
52
|
+
settled = true;
|
|
53
|
+
unmountListener();
|
|
54
|
+
onRequestEnd();
|
|
55
|
+
onResponseStart();
|
|
56
|
+
onResponseEnd();
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const call: ControlledCall = {
|
|
60
|
+
request,
|
|
61
|
+
requestId,
|
|
62
|
+
isSettled: () => settled,
|
|
63
|
+
isAborted: () => aborted,
|
|
64
|
+
succeed: async (data = defaultData, status = 200) => {
|
|
65
|
+
if (settled) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
settle();
|
|
69
|
+
await onSuccess({ data, status, extra });
|
|
70
|
+
},
|
|
71
|
+
fail: async (error = defaultError, status = 500) => {
|
|
72
|
+
if (settled) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
settle();
|
|
76
|
+
await onError({ error, status, extra });
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
if (aborted) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
onBeforeRequest();
|
|
85
|
+
onRequestStart();
|
|
86
|
+
calls.push(call);
|
|
87
|
+
},
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
/** Calls which wait for the test to answer them */
|
|
91
|
+
const getPending = () => calls.filter((call) => !call.isSettled());
|
|
92
|
+
|
|
93
|
+
return { client, calls, getPending };
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export const createDeferred = <T = void>() => {
|
|
97
|
+
let resolve!: (value: T) => void;
|
|
98
|
+
const promise = new Promise<T>((promiseResolve) => {
|
|
99
|
+
resolve = promiseResolve;
|
|
100
|
+
});
|
|
101
|
+
return { promise, resolve };
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const pendingMark = Symbol("pending");
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Tells if a promise is still pending after everything that was already scheduled has run.
|
|
108
|
+
* Works with the fake timers - no time has to pass.
|
|
109
|
+
*/
|
|
110
|
+
export const isPending = async (promise: Promise<unknown>) => {
|
|
111
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
112
|
+
const result = await Promise.race([promise, Promise.resolve(pendingMark)]);
|
|
113
|
+
return result === pendingMark;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
/** Let all the already scheduled promise callbacks run (requires fake timers) */
|
|
117
|
+
export const flushPromises = async () => {
|
|
118
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
119
|
+
};
|