@hyper-fetch/core 7.5.1 → 7.5.3

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.
Files changed (57) hide show
  1. package/__tests__/features/adapter/adapter.base.spec.ts +387 -0
  2. package/__tests__/features/adapter/adapter.bindings.spec.ts +1023 -0
  3. package/__tests__/features/adapter/adapter.browser.spec.ts +103 -0
  4. package/__tests__/features/adapter/adapter.server.spec.ts +177 -0
  5. package/__tests__/features/adapter/adapter.utils.spec.ts +165 -0
  6. package/__tests__/features/cache/cache.base.spec.ts +358 -0
  7. package/__tests__/features/cache/cache.events.spec.ts +215 -0
  8. package/__tests__/features/cache/cache.garbage-collector.spec.ts +172 -0
  9. package/__tests__/features/cache/cache.lazy-storage.spec.ts +129 -0
  10. package/__tests__/features/cache/cache.utils.spec.ts +119 -0
  11. package/__tests__/features/client/client.auth.spec.ts +125 -0
  12. package/__tests__/features/client/client.base.spec.ts +149 -0
  13. package/__tests__/features/client/client.hydration.spec.ts +171 -0
  14. package/__tests__/features/client/client.interceptor.spec.ts +225 -0
  15. package/__tests__/features/client/client.methods.spec.ts +188 -0
  16. package/__tests__/features/client/client.middleware.spec.ts +118 -0
  17. package/__tests__/features/client/client.utils.spec.ts +292 -0
  18. package/__tests__/features/dispatcher/dispatcher.base.spec.ts +123 -0
  19. package/__tests__/features/dispatcher/dispatcher.events.spec.ts +304 -0
  20. package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +462 -0
  21. package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +241 -0
  22. package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +125 -0
  23. package/__tests__/features/managers/app/app.manager.base.spec.ts +235 -0
  24. package/__tests__/features/managers/app/app.manager.ssr.spec.ts +40 -0
  25. package/__tests__/features/managers/logger/logger.base.spec.ts +107 -0
  26. package/__tests__/features/managers/logger/logger.utils.spec.ts +100 -0
  27. package/__tests__/features/managers/request/request.manager.base.spec.ts +222 -0
  28. package/__tests__/features/managers/request/request.manager.events.spec.ts +280 -0
  29. package/__tests__/features/mocker/mocker.base.spec.ts +567 -0
  30. package/__tests__/features/plugin/plugin.base.spec.ts +255 -0
  31. package/__tests__/features/request/request.base.spec.ts +199 -0
  32. package/__tests__/features/request/request.cloning.spec.ts +47 -0
  33. package/__tests__/features/request/request.sending.spec.ts +272 -0
  34. package/__tests__/features/request/request.setters.spec.ts +157 -0
  35. package/__tests__/features/request/request.typescript.spec.ts +423 -0
  36. package/__tests__/features/request/request.utils.spec.ts +77 -0
  37. package/__tests__/features/sdk/sdk.base.spec.ts +130 -0
  38. package/__tests__/global.d.ts +1 -0
  39. package/__tests__/jest.setup.ts +13 -0
  40. package/__tests__/shared/callbacks.shared.ts +9 -0
  41. package/__tests__/shared/index.ts +2 -0
  42. package/__tests__/shared/progress.shared.ts +35 -0
  43. package/__tests__/tsconfig.json +13 -0
  44. package/__tests__/utils/adapter.utils.ts +38 -0
  45. package/__tests__/utils/cache.utils.ts +19 -0
  46. package/__tests__/utils/callbacks.utils.ts +47 -0
  47. package/__tests__/utils/dispatcher.utils.ts +6 -0
  48. package/__tests__/utils/index.ts +4 -0
  49. package/dist/browser/index.js +2 -1
  50. package/dist/browser/index.js.map +2 -2
  51. package/dist/index.js +14 -2
  52. package/dist/index.js.map +2 -2
  53. package/jest.config.ts +6 -0
  54. package/package.json +1 -1
  55. package/project.json +4 -0
  56. package/src/adapter/adapter.utils.ts +2 -1
  57. package/src/http-adapter/http-adapter.server.ts +15 -1
@@ -0,0 +1,423 @@
1
+ import {
2
+ expectFunctionParametersType,
3
+ expectNotFunctionParametersType,
4
+ expectNotType,
5
+ expectType,
6
+ } from "@hyper-fetch/testing";
7
+
8
+ import { Client } from "client";
9
+ import { ExtractLocalErrorType } from "types";
10
+
11
+ const client = new Client({
12
+ url: "http://localhost:3000",
13
+ });
14
+
15
+ const getUsers = client.createRequest<{ response: { id: string }[]; queryParams?: string }>()({
16
+ method: "GET",
17
+ endpoint: "/users",
18
+ });
19
+
20
+ const getUser = client.createRequest<{ response: { id: string } }>()({
21
+ method: "GET",
22
+ endpoint: "/users/:id",
23
+ });
24
+
25
+ const postUser = client.createRequest<{ response: { id: string }; payload: { name: string } }>()({
26
+ method: "POST",
27
+ endpoint: "/users",
28
+ });
29
+
30
+ const patchUser = client.createRequest<{ response: { id: string }; payload: { name: string } }>()({
31
+ method: "PATCH",
32
+ endpoint: "/users/:id",
33
+ });
34
+
35
+ describe("Request [Types]", () => {
36
+ describe("when request does not require params and data", () => {
37
+ it("should allow to make request with no parameters", () => {
38
+ expectFunctionParametersType(getUsers.send).assert([
39
+ {
40
+ payload: null,
41
+ params: null,
42
+ queryParams: "",
43
+ },
44
+ ]);
45
+ });
46
+ it("should allow to make request with query params", () => {
47
+ expectFunctionParametersType(getUsers.setQueryParams("").send).assert([
48
+ {
49
+ payload: null,
50
+ params: null,
51
+ queryParams: null,
52
+ },
53
+ ]);
54
+ });
55
+ it("should not allow to add data to request", () => {
56
+ expectNotFunctionParametersType(getUsers.setQueryParams("").send).assert([
57
+ {
58
+ payload: { value: "test" },
59
+ params: null,
60
+ queryParams: null,
61
+ },
62
+ ]);
63
+ });
64
+ it("should not allow to add data to request", () => {
65
+ expectNotFunctionParametersType(getUsers.setQueryParams("").send).assert([
66
+ {
67
+ payload: null,
68
+ params: { id: "test" },
69
+ queryParams: null,
70
+ },
71
+ ]);
72
+ });
73
+ });
74
+
75
+ describe("when request does not require data and has required params", () => {
76
+ it("should allow to make request with parameters", () => {
77
+ expectFunctionParametersType(getUser.send).assert([
78
+ {
79
+ payload: null,
80
+ params: { id: "test" },
81
+ queryParams: undefined,
82
+ },
83
+ ]);
84
+
85
+ expectFunctionParametersType(getUser.setParams({ id: "test" }).send).assert([
86
+ {
87
+ payload: null,
88
+ params: null,
89
+ queryParams: undefined,
90
+ },
91
+ ]);
92
+ });
93
+ it("should allow to make request with query params", () => {
94
+ expectFunctionParametersType(getUser.send).assert([
95
+ {
96
+ payload: null,
97
+ params: { id: "test" },
98
+ },
99
+ ]);
100
+
101
+ expectFunctionParametersType(getUser.setParams({ id: "test" }).send).assert([
102
+ {
103
+ payload: null,
104
+ params: null,
105
+ },
106
+ ]);
107
+ });
108
+ it("should not allow to add data to request", () => {
109
+ expectNotFunctionParametersType(getUser.send).assert([
110
+ {
111
+ payload: { value: "test" },
112
+ params: { id: "test" },
113
+ queryParams: null,
114
+ },
115
+ ]);
116
+ });
117
+ it("should not allow to make request without params", () => {
118
+ expectFunctionParametersType(getUser.send).assert([
119
+ {
120
+ payload: null,
121
+ params: { id: "test" },
122
+ },
123
+ ]);
124
+ });
125
+ it("should not allow to make request with query params", () => {
126
+ expectFunctionParametersType(getUser.setQueryParams).assert([undefined]);
127
+ });
128
+ it("should not allow to make request with incorrect params", () => {
129
+ expectNotType<(options?: { payload: null; params: { id: null }; queryParams: null }) => Promise<any>>().assert(
130
+ getUser.send,
131
+ );
132
+ expectNotType<(options?: { payload: null; params: null; queryParams: null }) => Promise<any>>().assert(
133
+ getUser.send,
134
+ );
135
+ });
136
+ it("should not allow to redeclare params", () => {
137
+ expectNotType<(options?: { payload: null; params: { id: null }; queryParams: null }) => Promise<any>>().assert(
138
+ getUser.setParams({ id: 1 }).send,
139
+ );
140
+ });
141
+ });
142
+
143
+ describe("when request does not require params and has required data", () => {
144
+ it("should allow to make request with data", () => {
145
+ expectFunctionParametersType(postUser.send).assert([
146
+ {
147
+ payload: { name: "Kacper" },
148
+ params: null,
149
+ },
150
+ ]);
151
+ });
152
+ it("should allow to make request with query params", () => {
153
+ expectFunctionParametersType(postUser.send).assert([
154
+ {
155
+ payload: { name: "Kacper" },
156
+ params: null,
157
+ },
158
+ ]);
159
+
160
+ expectFunctionParametersType(postUser.setPayload({ name: "Kacper" }).send).assert([
161
+ {
162
+ payload: null,
163
+ params: null,
164
+ },
165
+ ]);
166
+ });
167
+ it("should not allow to add params to request", () => {
168
+ expectNotFunctionParametersType(postUser.setPayload({ name: "Kacper" }).send).assert([
169
+ {
170
+ payload: null,
171
+ params: { id: "test" },
172
+ },
173
+ ]);
174
+ });
175
+ it("should not allow to make request without data", () => {
176
+ expectNotFunctionParametersType(postUser.send).assert([
177
+ {
178
+ payload: null,
179
+ params: null,
180
+ },
181
+ ]);
182
+
183
+ expectNotFunctionParametersType(postUser.send).assert([
184
+ {
185
+ payload: null,
186
+ },
187
+ ]);
188
+ });
189
+ it("should not allow to make request with incorrect data", () => {
190
+ expectNotFunctionParametersType(postUser.send).assert([
191
+ {
192
+ payload: { something: "test" },
193
+ params: null,
194
+ queryParams: null,
195
+ },
196
+ ]);
197
+
198
+ expectNotFunctionParametersType(postUser.send).assert([
199
+ {
200
+ payload: "test",
201
+ },
202
+ ]);
203
+ });
204
+ it("should not allow to redeclare data", () => {
205
+ expectNotFunctionParametersType(postUser.setPayload({ name: "Kacper" }).send).assert([
206
+ {
207
+ payload: { name: "string" },
208
+ },
209
+ ]);
210
+ });
211
+ });
212
+
213
+ describe("when request require data and params", () => {
214
+ it("should allow to make request with parameters", () => {
215
+ expectFunctionParametersType(patchUser.send).assert([
216
+ {
217
+ payload: {
218
+ name: "Kacper",
219
+ },
220
+ params: { id: 1 },
221
+ },
222
+ ]);
223
+
224
+ expectFunctionParametersType(patchUser.setParams({ id: 1 }).send).assert([
225
+ {
226
+ payload: {
227
+ name: "Kacper",
228
+ },
229
+ params: null,
230
+ },
231
+ ]);
232
+ });
233
+ it("should allow to make request with query params", () => {
234
+ expectFunctionParametersType(patchUser.send).assert([
235
+ {
236
+ payload: {
237
+ name: "Kacper",
238
+ },
239
+ params: { id: 1 },
240
+ },
241
+ ]);
242
+
243
+ expectFunctionParametersType(patchUser.setPayload({ name: "Kacper" }).setParams({ id: 1 }).send).assert([
244
+ {
245
+ payload: null,
246
+ params: null,
247
+ },
248
+ ]);
249
+ });
250
+ it("should not allow to redeclare options", () => {
251
+ expectNotFunctionParametersType(patchUser.setPayload({ name: "Kacper" }).setParams({ id: 1 }).send).assert([
252
+ {
253
+ payload: { name: "Maciej" },
254
+ params: null,
255
+ queryParams: null,
256
+ },
257
+ ]);
258
+ expectNotFunctionParametersType(patchUser.setPayload({ name: "Kacper" }).setParams({ id: 1 }).send).assert([
259
+ {
260
+ payload: null,
261
+ params: { id: 2 },
262
+ queryParams: null,
263
+ },
264
+ ]);
265
+ });
266
+ it("should not allow to make request without values", () => {
267
+ expectFunctionParametersType(patchUser.send).assert([
268
+ {
269
+ payload: { name: "Maciej" },
270
+ params: { id: 1 },
271
+ },
272
+ ]);
273
+
274
+ expectNotFunctionParametersType(patchUser.send).assert([
275
+ {
276
+ payload: null,
277
+ params: null,
278
+ queryParams: null,
279
+ },
280
+ ]);
281
+ });
282
+
283
+ it("should not allow to make request with incorrect params", () => {
284
+ expectNotFunctionParametersType(patchUser.setPayload({ name: "Kacper" }).send).assert([
285
+ {
286
+ params: { error: "test" },
287
+ },
288
+ ]);
289
+
290
+ expectNotFunctionParametersType(patchUser.setPayload({ name: "Kacper" }).send).assert([
291
+ {
292
+ params: null,
293
+ },
294
+ ]);
295
+ });
296
+ });
297
+ describe("when request has query params", () => {
298
+ describe("when query params are optional", () => {
299
+ const getResourceWithOptionalQuery = client.createRequest<{
300
+ queryParams?: { search?: string; page?: number } | undefined;
301
+ }>()({
302
+ method: "GET",
303
+ endpoint: "/resources",
304
+ });
305
+
306
+ it("should allow to make request without query params when they are optional", () => {
307
+ expectFunctionParametersType(getResourceWithOptionalQuery.send).assert([
308
+ {
309
+ queryParams: undefined,
310
+ },
311
+ ]);
312
+ expectFunctionParametersType(getResourceWithOptionalQuery.send).assert([
313
+ {
314
+ queryParams: {},
315
+ },
316
+ ]);
317
+ expectFunctionParametersType(getResourceWithOptionalQuery.send).assert([undefined]);
318
+ });
319
+
320
+ it("should allow to make request with some optional query params", () => {
321
+ expectFunctionParametersType(getResourceWithOptionalQuery.send).assert([
322
+ {
323
+ queryParams: { search: "test" },
324
+ },
325
+ ]);
326
+ expectFunctionParametersType(getResourceWithOptionalQuery.send).assert([
327
+ {
328
+ queryParams: { page: 1 },
329
+ },
330
+ ]);
331
+ expectFunctionParametersType(getResourceWithOptionalQuery.send).assert([
332
+ {
333
+ queryParams: { search: "test", page: 1 },
334
+ },
335
+ ]);
336
+ });
337
+
338
+ it("should allow to set optional query params", () => {
339
+ expectFunctionParametersType(getResourceWithOptionalQuery.setQueryParams({ search: "test" }).send).assert([
340
+ undefined,
341
+ ]);
342
+ });
343
+
344
+ it("should not allow to make request with incorrect optional query params", () => {
345
+ expectNotFunctionParametersType(getResourceWithOptionalQuery.send).assert([
346
+ {
347
+ queryParams: { wrong: "value" },
348
+ },
349
+ ]);
350
+ expectNotFunctionParametersType(getResourceWithOptionalQuery.send).assert([
351
+ {
352
+ queryParams: { search: 123 },
353
+ },
354
+ ]);
355
+ });
356
+ });
357
+
358
+ describe("when query params are required", () => {
359
+ const getResourceWithRequiredQuery = client.createRequest<{
360
+ queryParams: { search: string; page: number };
361
+ }>()({
362
+ method: "GET",
363
+ endpoint: "/resources",
364
+ });
365
+
366
+ it("should not allow to make request without required query params", () => {
367
+ expectNotFunctionParametersType(getResourceWithRequiredQuery.send).assert([undefined]);
368
+ expectNotFunctionParametersType(getResourceWithRequiredQuery.send).assert([
369
+ {
370
+ queryParams: undefined,
371
+ },
372
+ ]);
373
+ expectNotFunctionParametersType(getResourceWithRequiredQuery.send).assert([
374
+ {
375
+ queryParams: { search: "test" },
376
+ },
377
+ ]);
378
+ });
379
+
380
+ it("should allow to make request with all required query params", () => {
381
+ expectFunctionParametersType(getResourceWithRequiredQuery.send).assert([
382
+ {
383
+ queryParams: { search: "test", page: 1 },
384
+ },
385
+ ]);
386
+ });
387
+
388
+ it("should not allow to make request with incorrect required query params", () => {
389
+ expectNotFunctionParametersType(getResourceWithRequiredQuery.send).assert([
390
+ {
391
+ queryParams: { search: "test", page: "1" },
392
+ },
393
+ ]);
394
+ });
395
+
396
+ it("should allow to set required query params", () => {
397
+ expectFunctionParametersType(
398
+ getResourceWithRequiredQuery.setQueryParams({ search: "test", page: 1 }).send,
399
+ ).assert([undefined]);
400
+ });
401
+
402
+ it("should not allow to set partial required query params", () => {
403
+ expectNotFunctionParametersType(getResourceWithRequiredQuery.setQueryParams).assert([{ search: "test" }]);
404
+ });
405
+ });
406
+ });
407
+ it("should add correct error types", () => {
408
+ expectType<Error>().assert<ExtractLocalErrorType<typeof getUser>>(new Error("message"));
409
+ expectNotType<string>().assert<ExtractLocalErrorType<typeof getUser>>(new Error("message"));
410
+ });
411
+ });
412
+
413
+ // // OK
414
+ // mappedReq.send({ payload: { name: "" } });
415
+ // mappedReq.setPayload({ name: "" }).send();
416
+ // // Fail
417
+ // mappedReq.send({ queryParams: "" });
418
+ // mappedReq.send({ payload: undefined });
419
+ // mappedReq.setPayload(null).send();
420
+ // mappedReq.setPayload(null).send({ payload: null, queryParams: () => null });
421
+ // mappedReq.send();
422
+ // mappedReq.send({ payload: new FormData() });
423
+ // mappedReq.setPayload({ name: "" }).send({ payload: { name: "" } });
@@ -0,0 +1,77 @@
1
+ import { Client } from "client";
2
+ import { getRequestDispatcher, getRequestKey, getProgressData, stringifyKey } from "request";
3
+
4
+ describe("Request [ Utils ]", () => {
5
+ describe("when stringifyKey function is used", () => {
6
+ it("should stringify valid values", async () => {
7
+ const response = { something: 123 };
8
+ const someString = "123";
9
+ expect(stringifyKey(response)).toBe(JSON.stringify(response));
10
+ expect(stringifyKey(someString)).toBe(someString);
11
+ expect(stringifyKey(undefined)).toBe("");
12
+ expect(stringifyKey(null)).toBe("");
13
+ });
14
+
15
+ it("should not stringify invalid values", async () => {
16
+ expect(stringifyKey(() => null)).toBe("");
17
+ });
18
+ });
19
+ describe("when getProgressData function is used", () => {
20
+ it("should process valid values on NaN progress values", async () => {
21
+ const date = new Date();
22
+ const response = getProgressData(date, date, { total: NaN, loaded: NaN });
23
+ expect(response).toStrictEqual({
24
+ progress: 0,
25
+ timeLeft: 0,
26
+ sizeLeft: 0,
27
+ total: 0,
28
+ loaded: 0,
29
+ startTimestamp: +date,
30
+ });
31
+ });
32
+ });
33
+ describe("when getRequestKey function is used", () => {
34
+ it("should return exact endpoint key", async () => {
35
+ const client = new Client({ url: "shared-base-url" });
36
+ const request = client
37
+ .createRequest()({ endpoint: "/users/:userId" })
38
+ .setParams({ userId: 1 })
39
+ .setQueryParams("?test=1");
40
+ expect(getRequestKey(request)).toBe("GET_/users/1_?test=1");
41
+ });
42
+ it("should useInitialValues and return generic endpoint key", async () => {
43
+ const client = new Client({ url: "shared-base-url" });
44
+ const request = client
45
+ .createRequest()({ endpoint: "/users/:userId" })
46
+ .setParams({ userId: 1 })
47
+ .setQueryParams("?test=1");
48
+ expect(getRequestKey(request, true)).toBe("GET_/users/:userId_");
49
+ });
50
+ });
51
+ describe("when getRequestDispatcher function is used", () => {
52
+ it("should give proper auto dispatcher", async () => {
53
+ const client = new Client({ url: "shared-base-url" });
54
+ const fetchRequest = client.createRequest()({ endpoint: "/users/:userId" });
55
+ const submitRequest = client.createRequest()({ endpoint: "/users/:userId", method: "POST" });
56
+
57
+ const valueFetch = getRequestDispatcher(fetchRequest, "auto");
58
+ const valueSubmit = getRequestDispatcher(submitRequest, "auto");
59
+ expect(valueFetch[0]).toBe(client.fetchDispatcher);
60
+ expect(valueSubmit[0]).toBe(client.submitDispatcher);
61
+ expect(valueFetch[1]).toBe(true);
62
+ expect(valueSubmit[1]).toBe(false);
63
+ });
64
+ it("should give selected dispatcher", async () => {
65
+ const client = new Client({ url: "shared-base-url" });
66
+ const fetchRequest = client.createRequest()({ endpoint: "/users/:userId" });
67
+ const submitRequest = client.createRequest()({ endpoint: "/users/:userId", method: "POST" });
68
+
69
+ const valueFetch = getRequestDispatcher(fetchRequest, "submit");
70
+ const valueSubmit = getRequestDispatcher(submitRequest, "fetch");
71
+ expect(valueSubmit[0]).toBe(client.fetchDispatcher);
72
+ expect(valueFetch[0]).toBe(client.submitDispatcher);
73
+ expect(valueSubmit[1]).toBe(true);
74
+ expect(valueFetch[1]).toBe(false);
75
+ });
76
+ });
77
+ });
@@ -0,0 +1,130 @@
1
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
2
+
3
+ import { Client, createClient } from "client";
4
+ import { HttpAdapterType } from "http-adapter";
5
+ import { Request, RequestInstance } from "request";
6
+ import { createSdk } from "sdk";
7
+
8
+ const { resetMocks, startServer, stopServer } = createHttpMockingServer();
9
+
10
+ type TestClient = Client<Error, HttpAdapterType>;
11
+
12
+ type TestSchema = {
13
+ users: {
14
+ $get: RequestInstance<{
15
+ client: TestClient;
16
+ queryParams: { page: number; limit: number };
17
+ endpoint: "/users";
18
+ }>;
19
+ $userId: {
20
+ $get: RequestInstance<{
21
+ client: TestClient;
22
+ response: { id: string; name: string };
23
+ endpoint: "/users/:userId";
24
+ }>;
25
+ $delete: RequestInstance<{
26
+ client: TestClient;
27
+ response: { id: string; name: string };
28
+ endpoint: "/users/:userId";
29
+ }>;
30
+ posts: {
31
+ $postId: {
32
+ $delete: RequestInstance<{
33
+ client: TestClient;
34
+ response: { id: string; title: string };
35
+ endpoint: "/users/:userId/posts/:postId";
36
+ }>;
37
+ };
38
+ };
39
+ };
40
+ };
41
+ acceptInvitation: {
42
+ $post: RequestInstance<{
43
+ client: TestClient;
44
+ endpoint: "/accept-invitation";
45
+ }>;
46
+ };
47
+ };
48
+
49
+ describe("SDK [ Base ]", () => {
50
+ let client: TestClient;
51
+
52
+ beforeAll(() => {
53
+ startServer();
54
+ });
55
+
56
+ beforeEach(() => {
57
+ resetMocks();
58
+ client = createClient<{ error: Error }>({ url: "http://localhost:3000" });
59
+ });
60
+
61
+ afterAll(() => {
62
+ stopServer();
63
+ });
64
+
65
+ it("should create sdk", () => {
66
+ const sdk = createSdk<TestClient, TestSchema>(client);
67
+ expect(sdk).toBeDefined();
68
+
69
+ const request = sdk.users.$userId.$get;
70
+
71
+ expect(request).toBeInstanceOf(Request);
72
+ expect(request.method).toBe("GET");
73
+ expect(request.endpoint).toBe("/users/:userId");
74
+ });
75
+
76
+ it("should create sdk with nested requests", () => {
77
+ const sdk = createSdk<TestClient, TestSchema>(client);
78
+ expect(sdk).toBeDefined();
79
+
80
+ const request = sdk.users.$userId.posts.$postId.$delete;
81
+
82
+ expect(request).toBeInstanceOf(Request);
83
+ expect(request.method).toBe("DELETE");
84
+ expect(request.endpoint).toBe("/users/:userId/posts/:postId");
85
+ });
86
+
87
+ it("should not create request from Request own properties", () => {
88
+ const sdk = createSdk<TestClient, TestSchema>(client);
89
+ expect(sdk).toBeDefined();
90
+
91
+ const request = sdk.users.$userId.$get.setParams({ userId: "1" });
92
+
93
+ expect(request).toBeInstanceOf(Request);
94
+ expect(request.endpoint).toBe("/users/1");
95
+ expect(request.params).toEqual({ userId: "1" });
96
+ });
97
+
98
+ it("should allow to set many params", () => {
99
+ const sdk = createSdk<TestClient, TestSchema>(client);
100
+ expect(sdk).toBeDefined();
101
+
102
+ const request = sdk.users.$userId.posts.$postId.$delete.setParams({ userId: "1", postId: "2" });
103
+
104
+ expect(request).toBeInstanceOf(Request);
105
+ expect(request.endpoint).toBe("/users/1/posts/2");
106
+ expect(request.params).toEqual({ userId: "1", postId: "2" });
107
+ });
108
+
109
+ it("should handle kebab-case to camelCase conversion", () => {
110
+ const sdk = createSdk<TestClient, TestSchema>(client, { camelCaseToKebabCase: true });
111
+ expect(sdk).toBeDefined();
112
+
113
+ const request = sdk.acceptInvitation.$post;
114
+
115
+ expect(request).toBeInstanceOf(Request);
116
+ expect(request.method).toBe("POST");
117
+ expect(request.endpoint).toBe("/accept-invitation");
118
+ });
119
+
120
+ it("should not handle kebab-case to camelCase conversion", () => {
121
+ const sdk = createSdk<TestClient, TestSchema>(client, { camelCaseToKebabCase: false });
122
+ expect(sdk).toBeDefined();
123
+
124
+ const request = sdk.acceptInvitation.$post;
125
+
126
+ expect(request).toBeInstanceOf(Request);
127
+ expect(request.method).toBe("POST");
128
+ expect(request.endpoint).toBe("/acceptInvitation");
129
+ });
130
+ });
@@ -0,0 +1 @@
1
+ import "jest-extended";
@@ -0,0 +1,13 @@
1
+ import "jest-extended";
2
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
3
+ // @ts-ignore
4
+ import { AbortController } from "abortcontroller-polyfill/dist/cjs-ponyfill";
5
+ import { BroadcastChannel } from "node:worker_threads";
6
+
7
+ if (!global.AbortController) {
8
+ global.AbortController = AbortController as any;
9
+ }
10
+
11
+ Object.defineProperties(globalThis, {
12
+ BroadcastChannel: { value: BroadcastChannel },
13
+ });
@@ -0,0 +1,9 @@
1
+ export const testCallbacksExecution = (callbacks: Array<jest.Mock<any, any>>) => {
2
+ callbacks.forEach((spyFn, index) => {
3
+ const nextSpy = callbacks[index + 1];
4
+ if (nextSpy) {
5
+ expect(spyFn).toHaveBeenCalledBefore(nextSpy);
6
+ }
7
+ expect(spyFn).toHaveBeenCalledTimes(1);
8
+ });
9
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./callbacks.shared";
2
+ export * from "./progress.shared";
@@ -0,0 +1,35 @@
1
+ import { RequestInstance, getProgressValue, getRequestEta } from "request";
2
+
3
+ export const testProgressSpy = (props: {
4
+ spy: () => void;
5
+ requestId: string;
6
+ request: RequestInstance;
7
+ startTimestamp: number;
8
+ progressTimestamp?: number;
9
+ total?: number;
10
+ loaded?: number;
11
+ timeLeft?: number;
12
+ }) => {
13
+ const { spy, requestId, request, startTimestamp, total, loaded, timeLeft, progressTimestamp } = props;
14
+
15
+ const totalValue = total ?? 1;
16
+ const loadedValue = loaded ?? 0;
17
+
18
+ const estimatedTime = getRequestEta(new Date(startTimestamp), new Date(progressTimestamp || startTimestamp), {
19
+ total: totalValue,
20
+ loaded: loadedValue,
21
+ }).timeLeft;
22
+ const timeToSend = loadedValue ? estimatedTime : null;
23
+
24
+ expect(spy).toHaveBeenCalledTimes(1);
25
+ expect(spy).toHaveBeenCalledWith({
26
+ request,
27
+ requestId,
28
+ loaded: loadedValue,
29
+ progress: getProgressValue({ total: totalValue, loaded: loadedValue }),
30
+ sizeLeft: totalValue - loadedValue,
31
+ startTimestamp,
32
+ timeLeft: timeLeft ?? timeToSend,
33
+ total: totalValue,
34
+ });
35
+ };