@hyper-fetch/core 8.2.0 → 8.2.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 (158) hide show
  1. package/README.md +4 -4
  2. package/__tests__/e2e/e2e.advanced.spec.ts +7 -7
  3. package/__tests__/features/adapter/adapter.base.spec.ts +2 -1
  4. package/__tests__/features/adapter/adapter.bindings.spec.ts +4 -4
  5. package/__tests__/features/adapter/adapter.browser.spec.ts +0 -1
  6. package/__tests__/features/adapter/adapter.server.spec.ts +4 -5
  7. package/__tests__/features/adapter/adapter.utils.spec.ts +6 -7
  8. package/__tests__/features/cache/cache.base.spec.ts +7 -7
  9. package/__tests__/features/cache/cache.events.spec.ts +15 -15
  10. package/__tests__/features/cache/cache.garbage-collector.spec.ts +18 -18
  11. package/__tests__/features/cache/cache.lazy-storage.spec.ts +8 -8
  12. package/__tests__/features/cache/cache.modes.spec.ts +7 -6
  13. package/__tests__/features/cache/cache.scoping.spec.ts +12 -8
  14. package/__tests__/features/cache/cache.typescript.spec.ts +2 -2
  15. package/__tests__/features/cache/cache.utils.spec.ts +14 -14
  16. package/__tests__/features/client/client.auth.spec.ts +0 -1
  17. package/__tests__/features/client/client.base.spec.ts +2 -3
  18. package/__tests__/features/client/client.from-json.spec.ts +0 -1
  19. package/__tests__/features/client/client.hydration.spec.ts +5 -5
  20. package/__tests__/features/client/client.interceptor.spec.ts +3 -3
  21. package/__tests__/features/client/client.methods.spec.ts +5 -5
  22. package/__tests__/features/client/client.middleware.spec.ts +2 -2
  23. package/__tests__/features/client/client.utils.spec.ts +16 -15
  24. package/__tests__/features/dispatcher/dispatcher.base.spec.ts +3 -3
  25. package/__tests__/features/dispatcher/dispatcher.custom-storage.spec.ts +4 -5
  26. package/__tests__/features/dispatcher/dispatcher.events.spec.ts +4 -4
  27. package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +13 -13
  28. package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +10 -10
  29. package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +35 -8
  30. package/__tests__/features/managers/app/app.manager.base.spec.ts +2 -3
  31. package/__tests__/features/managers/logger/logger.base.spec.ts +1 -2
  32. package/__tests__/features/managers/request/request.manager.base.spec.ts +8 -9
  33. package/__tests__/features/managers/request/request.manager.events.spec.ts +1 -2
  34. package/__tests__/features/mocker/mocker.base.spec.ts +6 -6
  35. package/__tests__/features/plugin/plugin.base.spec.ts +1 -2
  36. package/__tests__/features/request/request.cloning.spec.ts +0 -1
  37. package/__tests__/features/request/request.deduplicate-defaults.spec.ts +79 -0
  38. package/__tests__/features/request/request.retry-on-error.spec.ts +1 -2
  39. package/__tests__/features/request/request.sending.spec.ts +73 -24
  40. package/__tests__/features/request/request.setters.spec.ts +4 -3
  41. package/__tests__/features/request/request.typescript.spec.ts +0 -1
  42. package/__tests__/features/request/request.utils.spec.ts +1 -0
  43. package/__tests__/features/sdk/sdk.base.spec.ts +0 -1
  44. package/__tests__/features/sdk/sdk.request-fields.spec.ts +80 -0
  45. package/__tests__/features/sdk/sdk.types.spec.ts +3 -4
  46. package/__tests__/shared/callbacks.shared.ts +1 -1
  47. package/__tests__/tsconfig.json +4 -17
  48. package/__tests__/utils/adapter.utils.ts +1 -2
  49. package/__tests__/utils/callbacks.utils.ts +1 -2
  50. package/__tests__/utils/dispatcher.utils.ts +1 -1
  51. package/__tests__/vitest.setup.ts +1 -1
  52. package/dist/adapter/adapter.d.ts +4 -4
  53. package/dist/adapter/adapter.d.ts.map +1 -1
  54. package/dist/adapter/adapter.types.d.ts +1 -1
  55. package/dist/adapter/adapter.types.d.ts.map +1 -1
  56. package/dist/adapter/adapter.utils.d.ts.map +1 -1
  57. package/dist/cache/cache.d.ts +3 -3
  58. package/dist/cache/cache.d.ts.map +1 -1
  59. package/dist/cache/cache.events.d.ts +2 -2
  60. package/dist/cache/cache.events.d.ts.map +1 -1
  61. package/dist/client/client.create.d.ts +1 -1
  62. package/dist/client/client.create.d.ts.map +1 -1
  63. package/dist/client/client.d.ts +3 -3
  64. package/dist/client/client.d.ts.map +1 -1
  65. package/dist/client/client.types.d.ts +1 -1
  66. package/dist/client/client.types.d.ts.map +1 -1
  67. package/dist/client/client.utils.d.ts.map +1 -1
  68. package/dist/constants/http.constants.d.ts +5 -1
  69. package/dist/constants/http.constants.d.ts.map +1 -1
  70. package/dist/constants/time.constants.d.ts.map +1 -1
  71. package/dist/dispatcher/dispatcher.d.ts +3 -3
  72. package/dist/dispatcher/dispatcher.d.ts.map +1 -1
  73. package/dist/dispatcher/dispatcher.events.d.ts +1 -1
  74. package/dist/dispatcher/dispatcher.events.d.ts.map +1 -1
  75. package/dist/dispatcher/dispatcher.utils.d.ts +1 -1
  76. package/dist/dispatcher/dispatcher.utils.d.ts.map +1 -1
  77. package/dist/http-adapter/http-adapter.constants.d.ts.map +1 -1
  78. package/dist/http-adapter/http-adapter.d.ts.map +1 -1
  79. package/dist/http-adapter/http-adapter.fetch.d.ts +2 -2
  80. package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -1
  81. package/dist/http-adapter/http-adapter.types.d.ts +2 -2
  82. package/dist/http-adapter/http-adapter.types.d.ts.map +1 -1
  83. package/dist/http-adapter/http-adapter.utils.d.ts.map +1 -1
  84. package/dist/index.mjs +2363 -2347
  85. package/dist/index.mjs.map +1 -1
  86. package/dist/managers/app/app.manager.constants.d.ts +1 -1
  87. package/dist/managers/app/app.manager.constants.d.ts.map +1 -1
  88. package/dist/managers/app/app.manager.d.ts +1 -1
  89. package/dist/managers/app/app.manager.d.ts.map +1 -1
  90. package/dist/managers/app/app.manager.events.d.ts.map +1 -1
  91. package/dist/managers/logger/logger.manager.d.ts +1 -1
  92. package/dist/managers/logger/logger.manager.d.ts.map +1 -1
  93. package/dist/managers/request/request.manager.d.ts.map +1 -1
  94. package/dist/managers/request/request.manager.events.d.ts +2 -2
  95. package/dist/managers/request/request.manager.events.d.ts.map +1 -1
  96. package/dist/plugin/plugin.d.ts +1 -1
  97. package/dist/plugin/plugin.d.ts.map +1 -1
  98. package/dist/plugin/plugin.types.d.ts +4 -4
  99. package/dist/plugin/plugin.types.d.ts.map +1 -1
  100. package/dist/request/request.d.ts +5 -4
  101. package/dist/request/request.d.ts.map +1 -1
  102. package/dist/request/request.hooks.d.ts +2 -2
  103. package/dist/request/request.hooks.d.ts.map +1 -1
  104. package/dist/request/request.types.d.ts +3 -3
  105. package/dist/request/request.types.d.ts.map +1 -1
  106. package/dist/request/request.utils.d.ts.map +1 -1
  107. package/dist/sdk/sdk.d.ts +1 -1
  108. package/dist/sdk/sdk.d.ts.map +1 -1
  109. package/dist/types/http.types.d.ts +1 -1
  110. package/dist/types/http.types.d.ts.map +1 -1
  111. package/dist/types/request.types.d.ts +2 -2
  112. package/dist/types/request.types.d.ts.map +1 -1
  113. package/dist/utils/emitter.utils.d.ts +1 -1
  114. package/dist/utils/emitter.utils.d.ts.map +1 -1
  115. package/package.json +8 -8
  116. package/src/adapter/adapter.bindings.ts +14 -14
  117. package/src/adapter/adapter.ts +10 -9
  118. package/src/adapter/adapter.types.ts +2 -1
  119. package/src/adapter/adapter.utils.ts +5 -5
  120. package/src/cache/cache.events.ts +2 -3
  121. package/src/cache/cache.ts +11 -11
  122. package/src/client/client.create.ts +1 -1
  123. package/src/client/client.ts +9 -8
  124. package/src/client/client.types.ts +2 -2
  125. package/src/client/client.utils.ts +3 -2
  126. package/src/constants/http.constants.ts +5 -0
  127. package/src/constants/time.constants.ts +1 -1
  128. package/src/dispatcher/dispatcher.events.ts +1 -2
  129. package/src/dispatcher/dispatcher.ts +12 -12
  130. package/src/dispatcher/dispatcher.utils.ts +10 -4
  131. package/src/http-adapter/http-adapter.constants.ts +1 -0
  132. package/src/http-adapter/http-adapter.fetch.ts +19 -9
  133. package/src/http-adapter/http-adapter.ts +2 -1
  134. package/src/http-adapter/http-adapter.types.ts +3 -2
  135. package/src/http-adapter/http-adapter.utils.ts +15 -11
  136. package/src/managers/app/app.manager.constants.ts +2 -1
  137. package/src/managers/app/app.manager.events.ts +0 -1
  138. package/src/managers/app/app.manager.ts +1 -1
  139. package/src/managers/app/app.manager.utils.ts +2 -2
  140. package/src/managers/logger/logger.manager.ts +1 -2
  141. package/src/managers/request/request.manager.events.ts +2 -3
  142. package/src/managers/request/request.manager.ts +4 -4
  143. package/src/mocker/mocker.ts +2 -2
  144. package/src/plugin/plugin.ts +1 -1
  145. package/src/plugin/plugin.types.ts +4 -4
  146. package/src/request/request.hooks.ts +6 -5
  147. package/src/request/request.ts +49 -34
  148. package/src/request/request.types.ts +4 -3
  149. package/src/request/request.utils.ts +17 -16
  150. package/src/sdk/sdk.ts +26 -19
  151. package/src/types/http.types.ts +1 -1
  152. package/src/types/request.types.ts +3 -2
  153. package/src/utils/emitter.utils.ts +1 -1
  154. package/src/utils/uuid.utils.ts +1 -1
  155. package/tsconfig.json +30 -2
  156. package/vite.config.ts +14 -20
  157. package/.eslintrc.cjs +0 -12
  158. package/tsconfig.base.json +0 -7
package/README.md CHANGED
@@ -30,7 +30,9 @@
30
30
 
31
31
  ## 📖 About
32
32
 
33
- HyperFetch Core is the foundation of the HyperFetch ecosystem. Every API call is a typed, immutable request object with built-in caching, queuing, retries, and offline support. It works in any JavaScript environment — browser, server, or edge — and connects to any API through pluggable adapters.
33
+ HyperFetch Core is the foundation of the HyperFetch ecosystem. Every API call is a typed, immutable request object with
34
+ built-in caching, queuing, retries, and offline support. It works in any JavaScript environment — browser, server, or
35
+ edge — and connects to any API through pluggable adapters.
34
36
 
35
37
  ## 🎯 Key Capabilities
36
38
 
@@ -121,9 +123,7 @@ const listUsers = client.createRequest<{
121
123
  });
122
124
 
123
125
  // Query params are appended to the URL: /users?page=1&limit=20&search=john
124
- const { data } = await listUsers
125
- .setQueryParams({ page: 1, limit: 20, search: "john" })
126
- .send();
126
+ const { data } = await listUsers.setQueryParams({ page: 1, limit: 20, search: "john" }).send();
127
127
  ```
128
128
 
129
129
  ### Lifecycle callbacks
@@ -1,7 +1,3 @@
1
- /**
2
- * @vitest-environment node
3
- */
4
- import { URL } from "url";
5
1
  import { Client } from "@hyper-fetch/core";
6
2
  import type { RouteHandler } from "@hyper-fetch/testing";
7
3
  import {
@@ -12,6 +8,10 @@ import {
12
8
  fileDownloadHandler,
13
9
  echoHandler,
14
10
  } from "@hyper-fetch/testing";
11
+ /**
12
+ * @vitest-environment node
13
+ */
14
+ import { URL } from "url";
15
15
 
16
16
  const { route, startServer, stopServer } = createE2EServer();
17
17
 
@@ -73,7 +73,7 @@ beforeAll(async () => {
73
73
  // File Download
74
74
  const fileContent = Buffer.from("Hello, this is a test file content!");
75
75
  route("GET", "/download/test.txt", fileDownloadHandler(fileContent, "test.txt", "text/plain"));
76
- const binaryContent = Buffer.alloc(1024, 0xab);
76
+ const binaryContent = Buffer.alloc(1024, 0xAB);
77
77
  route("GET", "/download/binary.bin", fileDownloadHandler(binaryContent, "binary.bin"));
78
78
 
79
79
  // Concurrent
@@ -265,7 +265,7 @@ describe("E2E [ Streaming ]", () => {
265
265
  expect(status).toBe(200);
266
266
  expect(data).toBeDefined();
267
267
  expect(progressEvents.length).toBeGreaterThanOrEqual(1);
268
- const lastLoaded = progressEvents[progressEvents.length - 1];
268
+ const lastLoaded = progressEvents.at(-1);
269
269
  expect(lastLoaded).toBeGreaterThan(0);
270
270
  });
271
271
 
@@ -293,7 +293,7 @@ describe("E2E [ Streaming ]", () => {
293
293
  while (true) {
294
294
  // eslint-disable-next-line no-await-in-loop
295
295
  const { done, value } = await reader.read();
296
- if (done) break;
296
+ if (done) {break;}
297
297
  chunks.push(decoder.decode(value, { stream: true }));
298
298
  }
299
299
 
@@ -1,6 +1,7 @@
1
- import { Client, createClient } from "client";
2
1
  import type { AdapterInstance } from "adapter";
3
2
  import { Adapter, RequestProcessingError, getAdapterHeaders, getAdapterPayload } from "adapter";
3
+ import { Client, createClient } from "client";
4
+
4
5
  import { HttpMethods } from "../../../src/constants/http.constants";
5
6
 
6
7
  describe("Adapter [ Base ]", () => {
@@ -1,13 +1,13 @@
1
1
  import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
2
-
3
- import { Plugin } from "plugin";
4
2
  import type { ResponseType, ResponseErrorType } from "adapter";
5
3
  import { getAdapterBindings, getErrorMessage } from "adapter";
6
- import { testProgressSpy } from "../../shared";
7
4
  import { Client } from "client";
8
- import type { RequestInstance } from "request";
9
5
  import type { FetchAdapterOptionsType, HttpAdapterType } from "http-adapter";
10
6
  import { xhrExtra } from "http-adapter";
7
+ import { Plugin } from "plugin";
8
+ import type { RequestInstance } from "request";
9
+
10
+ import { testProgressSpy } from "../../shared";
11
11
 
12
12
  const { resetMocks, startServer, stopServer } = createHttpMockingServer();
13
13
 
@@ -2,7 +2,6 @@
2
2
  * @vitest-environment jsdom
3
3
  */
4
4
  import { createHttpMockingServer } from "@hyper-fetch/testing";
5
-
6
5
  import { getErrorMessage } from "adapter";
7
6
  import { Client } from "client";
8
7
  import { HttpAdapter } from "http-adapter";
@@ -1,12 +1,11 @@
1
- /**
2
- * @vitest-environment node
3
- */
4
- import type { Mock } from "vitest";
5
1
  import { createHttpMockingServer } from "@hyper-fetch/testing";
6
-
7
2
  import { getErrorMessage } from "adapter";
8
3
  import { Client } from "client";
9
4
  import { HttpAdapter } from "http-adapter";
5
+ /**
6
+ * @vitest-environment node
7
+ */
8
+ import type { Mock } from "vitest";
10
9
 
11
10
  const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
12
11
 
@@ -1,7 +1,6 @@
1
1
  import { createHttpMockingServer } from "@hyper-fetch/testing";
2
-
3
- import { createClient } from "client";
4
2
  import { getAdapterHeaders, getAdapterPayload, getErrorMessage } from "adapter";
3
+ import { createClient } from "client";
5
4
  import { parseResponse, parseErrorResponse, handleResponse, getResponseHeaders } from "http-adapter";
6
5
 
7
6
  const { resetMocks, startServer, stopServer } = createHttpMockingServer();
@@ -126,7 +125,7 @@ describe("Adapter [ Utils ]", () => {
126
125
  describe("When handleResponse util gets triggered", () => {
127
126
  it("should return Buffer for arraybuffer response type", () => {
128
127
  const chunks = [Buffer.from("Hello"), Buffer.from("World")];
129
- const result = handleResponse(chunks, "arraybuffer", "utf-8");
128
+ const result = handleResponse(chunks, "arraybuffer", "utf8");
130
129
 
131
130
  expect(Buffer.isBuffer(result)).toBe(true);
132
131
  expect(result.toString()).toBe("HelloWorld");
@@ -135,14 +134,14 @@ describe("Adapter [ Utils ]", () => {
135
134
  it("should parse JSON response type", () => {
136
135
  const data = { message: "test" };
137
136
  const chunks = [Buffer.from(JSON.stringify(data))];
138
- const result = handleResponse(chunks, "json", "utf-8");
137
+ const result = handleResponse(chunks, "json", "utf8");
139
138
 
140
139
  expect(result).toEqual(data);
141
140
  });
142
141
 
143
142
  it("should return string for default response type", () => {
144
143
  const chunks = [Buffer.from("Hello"), Buffer.from("World")];
145
- const result = handleResponse(chunks, "text", "utf-8");
144
+ const result = handleResponse(chunks, "text", "utf8");
146
145
 
147
146
  expect(typeof result).toBe("string");
148
147
  expect(result).toBe("HelloWorld");
@@ -150,14 +149,14 @@ describe("Adapter [ Utils ]", () => {
150
149
 
151
150
  it("should handle empty chunks", () => {
152
151
  const chunks: Buffer[] = [];
153
- const result = handleResponse(chunks, "text", "utf-8");
152
+ const result = handleResponse(chunks, "text", "utf8");
154
153
 
155
154
  expect(result).toBe("");
156
155
  });
157
156
 
158
157
  it("should handle invalid JSON for json response type", () => {
159
158
  const chunks = [Buffer.from("invalid json")];
160
- const result = handleResponse(chunks, "json", "utf-8");
159
+ const result = handleResponse(chunks, "json", "utf8");
161
160
 
162
161
  expect(result).toBe("invalid json");
163
162
  });
@@ -1,11 +1,11 @@
1
1
  import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
2
-
3
2
  import type { ResponseSuccessType } from "adapter";
4
- import type { ResponseDetailsType } from "managers";
5
- import { createCache } from "../../utils";
6
3
  import { Client } from "client";
7
4
  import type { HttpAdapterType } from "http-adapter";
8
5
  import { xhrExtra } from "http-adapter";
6
+ import type { ResponseDetailsType } from "managers";
7
+
8
+ import { createCache } from "../../utils";
9
9
 
10
10
  const { resetMocks, startServer, stopServer } = createHttpMockingServer();
11
11
 
@@ -21,10 +21,10 @@ describe("Cache [ Base ]", () => {
21
21
  };
22
22
  const details: ResponseDetailsType = {
23
23
  retries: 0,
24
- requestTimestamp: +new Date(),
25
- responseTimestamp: +new Date(),
26
- addedTimestamp: +new Date(),
27
- triggerTimestamp: +new Date(),
24
+ requestTimestamp: Date.now(),
25
+ responseTimestamp: Date.now(),
26
+ addedTimestamp: Date.now(),
27
+ triggerTimestamp: Date.now(),
28
28
  isCanceled: false,
29
29
  isOffline: false,
30
30
  willRetry: false,
@@ -1,11 +1,11 @@
1
1
  import { sleep } from "@hyper-fetch/testing";
2
-
3
2
  import type { CacheValueType } from "cache";
4
- import { createCache } from "../../utils";
5
3
  import { Client } from "client";
6
4
  import { xhrExtra } from "http-adapter";
7
5
  import { Plugin } from "plugin";
8
6
 
7
+ import { createCache } from "../../utils";
8
+
9
9
  describe("Cache [ Events ]", () => {
10
10
  const cacheKey = "test";
11
11
 
@@ -21,10 +21,10 @@ describe("Cache [ Events ]", () => {
21
21
  success: true,
22
22
  extra: xhrExtra,
23
23
  retries: 0,
24
- requestTimestamp: +new Date(),
25
- responseTimestamp: +new Date(),
26
- addedTimestamp: +new Date(),
27
- triggerTimestamp: +new Date(),
24
+ requestTimestamp: Date.now(),
25
+ responseTimestamp: Date.now(),
26
+ addedTimestamp: Date.now(),
27
+ triggerTimestamp: Date.now(),
28
28
  cached: true,
29
29
  isCanceled: false,
30
30
  isOffline: false,
@@ -33,7 +33,7 @@ describe("Cache [ Events ]", () => {
33
33
  scope: null,
34
34
  staleTime: request.staleTime,
35
35
  version: cache.version,
36
- cacheTime: 300000,
36
+ cacheTime: 300_000,
37
37
  };
38
38
 
39
39
  beforeEach(() => {
@@ -151,10 +151,10 @@ describe("Cache [ Events ]", () => {
151
151
  success: true,
152
152
  extra: xhrExtra,
153
153
  retries: 0,
154
- requestTimestamp: +new Date(),
155
- responseTimestamp: +new Date(),
156
- addedTimestamp: +new Date(),
157
- triggerTimestamp: +new Date(),
154
+ requestTimestamp: Date.now(),
155
+ responseTimestamp: Date.now(),
156
+ addedTimestamp: Date.now(),
157
+ triggerTimestamp: Date.now(),
158
158
  isCanceled: false,
159
159
  isOffline: false,
160
160
  willRetry: false,
@@ -172,10 +172,10 @@ describe("Cache [ Events ]", () => {
172
172
  success: true,
173
173
  extra: xhrExtra,
174
174
  retries: 0,
175
- requestTimestamp: +new Date(),
176
- responseTimestamp: +new Date(),
177
- addedTimestamp: +new Date(),
178
- triggerTimestamp: +new Date(),
175
+ requestTimestamp: Date.now(),
176
+ responseTimestamp: Date.now(),
177
+ addedTimestamp: Date.now(),
178
+ triggerTimestamp: Date.now(),
179
179
  isCanceled: false,
180
180
  isOffline: false,
181
181
  willRetry: false,
@@ -1,14 +1,14 @@
1
1
  import { waitFor } from "@testing-library/dom";
2
-
3
2
  import type { CacheValueType } from "cache";
4
3
  import { Client, Time, xhrExtra } from "index";
4
+
5
5
  import { createCache, createLazyCacheAdapter } from "../../utils";
6
6
 
7
7
  describe("Cache [ Garbage Collector ]", () => {
8
8
  const cacheKey = "test";
9
- const staleTime = 30000;
9
+ const staleTime = 30_000;
10
10
  const version = "test";
11
- const cacheTime = 10000;
11
+ const cacheTime = 10_000;
12
12
  const cacheData: CacheValueType = {
13
13
  data: null,
14
14
  error: null,
@@ -16,10 +16,10 @@ describe("Cache [ Garbage Collector ]", () => {
16
16
  success: true,
17
17
  extra: xhrExtra,
18
18
  retries: 0,
19
- requestTimestamp: +new Date(),
20
- responseTimestamp: +new Date(),
21
- addedTimestamp: +new Date(),
22
- triggerTimestamp: +new Date(),
19
+ requestTimestamp: Date.now(),
20
+ responseTimestamp: Date.now(),
21
+ addedTimestamp: Date.now(),
22
+ triggerTimestamp: Date.now(),
23
23
  isCanceled: false,
24
24
  isOffline: false,
25
25
  willRetry: false,
@@ -46,16 +46,16 @@ describe("Cache [ Garbage Collector ]", () => {
46
46
  vi.resetAllMocks();
47
47
  vi.clearAllMocks();
48
48
 
49
- cacheData.requestTimestamp = +new Date();
50
- cacheData.responseTimestamp = +new Date();
51
- cacheData.addedTimestamp = +new Date();
52
- cacheData.triggerTimestamp = +new Date();
49
+ cacheData.requestTimestamp = Date.now();
50
+ cacheData.responseTimestamp = Date.now();
51
+ cacheData.addedTimestamp = Date.now();
52
+ cacheData.triggerTimestamp = Date.now();
53
53
  });
54
54
 
55
55
  describe("when garbage collector is triggered", () => {
56
56
  it("should not schedule garbage collector for non existing key", async () => {
57
57
  cache.scheduleGarbageCollector(cacheKey);
58
- expect(Array.from(cache.garbageCollectors.keys())).toHaveLength(0);
58
+ expect([...cache.garbageCollectors.keys()]).toHaveLength(0);
59
59
  });
60
60
  it("should garbage collect data from sync storage", async () => {
61
61
  cache.set(request.setCacheTime(10), cacheData);
@@ -78,7 +78,7 @@ describe("Cache [ Garbage Collector ]", () => {
78
78
  storage,
79
79
  version,
80
80
  });
81
- expect(Array.from(cacheInstance.garbageCollectors.keys())).toHaveLength(1);
81
+ expect([...cacheInstance.garbageCollectors.keys()]).toHaveLength(1);
82
82
  });
83
83
  it("should not schedule lazy storage garbage collection on mount", async () => {
84
84
  const lazyStorage = new Map<string, CacheValueType>();
@@ -89,7 +89,7 @@ describe("Cache [ Garbage Collector ]", () => {
89
89
  });
90
90
 
91
91
  await waitFor(() => {
92
- expect(Array.from(cacheInstance.garbageCollectors.keys())).toHaveLength(0);
92
+ expect([...cacheInstance.garbageCollectors.keys()]).toHaveLength(0);
93
93
  });
94
94
  });
95
95
  it("should schedule garbage collection when resource is added", async () => {
@@ -142,15 +142,15 @@ describe("Cache [ Garbage Collector ]", () => {
142
142
  });
143
143
 
144
144
  await waitFor(() => {
145
- expect(Array.from(cacheInstance.garbageCollectors.keys())).toHaveLength(0);
145
+ expect([...cacheInstance.garbageCollectors.keys()]).toHaveLength(0);
146
146
  });
147
147
  });
148
148
  it("should garbage collect when time left is less than zero", async () => {
149
- const pastTimestamp = +new Date() - 20000; // 20 seconds ago
149
+ const pastTimestamp = Date.now() - 20_000; // 20 seconds ago
150
150
  const staleData = {
151
151
  ...cacheData,
152
152
  responseTimestamp: pastTimestamp,
153
- cacheTime: 10000, // 10 seconds
153
+ cacheTime: 10_000, // 10 seconds
154
154
  };
155
155
 
156
156
  cache.set(request, staleData);
@@ -168,7 +168,7 @@ describe("Cache [ Garbage Collector ]", () => {
168
168
  });
169
169
 
170
170
  await waitFor(() => {
171
- expect(Array.from(cacheInstance.garbageCollectors.keys())).toHaveLength(0);
171
+ expect([...cacheInstance.garbageCollectors.keys()]).toHaveLength(0);
172
172
  });
173
173
  });
174
174
  });
@@ -1,12 +1,12 @@
1
1
  import { sleep } from "@hyper-fetch/testing";
2
-
3
2
  import type { CacheAsyncStorageType, CacheValueType } from "cache";
4
- import { createCache, createLazyCacheAdapter } from "../../utils";
5
3
  import { Client } from "client";
6
4
  import { xhrExtra } from "http-adapter";
7
5
 
6
+ import { createCache, createLazyCacheAdapter } from "../../utils";
7
+
8
8
  describe("Cache [ Lazy Storage ]", () => {
9
- const staleTime = 10000;
9
+ const staleTime = 10_000;
10
10
  const version = "test";
11
11
  const cacheKey = "1";
12
12
  const cacheData: CacheValueType = {
@@ -16,10 +16,10 @@ describe("Cache [ Lazy Storage ]", () => {
16
16
  success: true,
17
17
  extra: xhrExtra,
18
18
  retries: 0,
19
- addedTimestamp: +new Date(),
20
- requestTimestamp: +new Date(),
21
- responseTimestamp: +new Date(),
22
- triggerTimestamp: +new Date(),
19
+ addedTimestamp: Date.now(),
20
+ requestTimestamp: Date.now(),
21
+ responseTimestamp: Date.now(),
22
+ triggerTimestamp: Date.now(),
23
23
  isCanceled: false,
24
24
  isOffline: false,
25
25
  willRetry: false,
@@ -79,7 +79,7 @@ describe("Cache [ Lazy Storage ]", () => {
79
79
  expect(spy).toHaveBeenCalledTimes(1);
80
80
  });
81
81
  it("should not emit stale data", async () => {
82
- await cache.options?.lazyStorage?.set(cacheKey, { ...cacheData, responseTimestamp: +new Date() - staleTime });
82
+ await cache.options?.lazyStorage?.set(cacheKey, { ...cacheData, responseTimestamp: Date.now() - staleTime });
83
83
  cache.events.onDataByKey(cacheKey, spy);
84
84
  const data = cache.get(cacheKey);
85
85
  await sleep(50);
@@ -1,9 +1,10 @@
1
1
  import type { ResponseSuccessType } from "adapter";
2
- import type { ResponseDetailsType } from "managers";
3
- import { createCache } from "../../utils";
4
2
  import { Client } from "client";
5
3
  import type { HttpAdapterType } from "http-adapter";
6
4
  import { xhrExtra } from "http-adapter";
5
+ import type { ResponseDetailsType } from "managers";
6
+
7
+ import { createCache } from "../../utils";
7
8
 
8
9
  const createServerClient = (url: string) => new Client({ url, mode: "server" });
9
10
 
@@ -19,10 +20,10 @@ describe("Cache [ Modes ]", () => {
19
20
  };
20
21
  const details: ResponseDetailsType = {
21
22
  retries: 0,
22
- requestTimestamp: +new Date(),
23
- responseTimestamp: +new Date(),
24
- addedTimestamp: +new Date(),
25
- triggerTimestamp: +new Date(),
23
+ requestTimestamp: Date.now(),
24
+ responseTimestamp: Date.now(),
25
+ addedTimestamp: Date.now(),
26
+ triggerTimestamp: Date.now(),
26
27
  isCanceled: false,
27
28
  isOffline: false,
28
29
  willRetry: false,
@@ -1,13 +1,13 @@
1
1
  import { waitFor } from "@testing-library/dom";
2
-
3
2
  import type { ResponseSuccessType } from "adapter";
4
- import type { ResponseDetailsType } from "managers";
5
- import { createCache, createLazyCacheAdapter } from "../../utils";
6
3
  import { Client } from "client";
7
4
  import type { HttpAdapterType } from "http-adapter";
8
5
  import { xhrExtra } from "http-adapter";
6
+ import type { ResponseDetailsType } from "managers";
9
7
  import { scopeKey } from "request";
10
8
 
9
+ import { createCache, createLazyCacheAdapter } from "../../utils";
10
+
11
11
  describe("Cache [ Scoping ]", () => {
12
12
  const response: ResponseSuccessType<unknown, HttpAdapterType> = {
13
13
  data: { id: 1 },
@@ -20,10 +20,10 @@ describe("Cache [ Scoping ]", () => {
20
20
  };
21
21
  const details: ResponseDetailsType = {
22
22
  retries: 0,
23
- requestTimestamp: +new Date(),
24
- responseTimestamp: +new Date(),
25
- addedTimestamp: +new Date(),
26
- triggerTimestamp: +new Date(),
23
+ requestTimestamp: Date.now(),
24
+ responseTimestamp: Date.now(),
25
+ addedTimestamp: Date.now(),
26
+ triggerTimestamp: Date.now(),
27
27
  isCanceled: false,
28
28
  isOffline: false,
29
29
  willRetry: false,
@@ -41,7 +41,11 @@ describe("Cache [ Scoping ]", () => {
41
41
  cache.set(a, { ...response, ...details, data: { id: 1 } });
42
42
  cache.set(b, { ...response, ...details, data: { id: 2 } });
43
43
 
44
- cache.set(a, (prev) => ({ ...response, ...details, data: { id: (prev?.data as { id: number }).id + 10 } }));
44
+ cache.set(a, (prev) => ({
45
+ ...response,
46
+ ...details,
47
+ data: { id: ((prev?.data as { id: number } | undefined)?.id ?? 0) + 10 },
48
+ }));
45
49
 
46
50
  expect(cache.get(scopeKey(base.cacheKey, "a"))?.data).toStrictEqual({ id: 11 });
47
51
  expect(cache.get(scopeKey(base.cacheKey, "b"))?.data).toStrictEqual({ id: 2 });
@@ -1,11 +1,11 @@
1
1
  /* eslint-disable @typescript-eslint/no-unused-vars */
2
2
  import { expectFunctionParametersType, expectNotFunctionParametersType } from "@hyper-fetch/testing";
3
-
4
3
  import type { ResponseSuccessType } from "adapter";
5
- import type { ResponseDetailsType } from "managers";
6
4
  import { Client } from "client";
7
5
  import type { HttpAdapterType } from "http-adapter";
8
6
  import { xhrExtra } from "http-adapter";
7
+ import type { ResponseDetailsType } from "managers";
8
+
9
9
  import { createCache } from "../../utils";
10
10
 
11
11
  interface UserResponse {
@@ -1,8 +1,8 @@
1
- import { getCacheData, getCacheByKey, getInvalidateByKey } from "cache";
2
1
  import type { ResponseErrorType, ResponseSuccessType } from "adapter";
3
- import type { ResponseDetailsType } from "managers";
2
+ import { getCacheData, getCacheByKey, getInvalidateByKey } from "cache";
4
3
  import type { HttpAdapterType } from "http-adapter";
5
4
  import { xhrExtra } from "http-adapter";
5
+ import type { ResponseDetailsType } from "managers";
6
6
 
7
7
  describe("Cache [ Utils ]", () => {
8
8
  describe("when getCacheData function is used", () => {
@@ -20,10 +20,10 @@ describe("Cache [ Utils ]", () => {
20
20
  success: false,
21
21
  extra: xhrExtra,
22
22
  retries: 0,
23
- requestTimestamp: +new Date(),
24
- responseTimestamp: +new Date(),
25
- addedTimestamp: +new Date(),
26
- triggerTimestamp: +new Date(),
23
+ requestTimestamp: Date.now(),
24
+ responseTimestamp: Date.now(),
25
+ addedTimestamp: Date.now(),
26
+ triggerTimestamp: Date.now(),
27
27
  isCanceled: false,
28
28
  isOffline: false,
29
29
  willRetry: false,
@@ -54,10 +54,10 @@ describe("Cache [ Utils ]", () => {
54
54
  success: true,
55
55
  extra: xhrExtra,
56
56
  retries: 0,
57
- requestTimestamp: +new Date(),
58
- responseTimestamp: +new Date(),
59
- addedTimestamp: +new Date(),
60
- triggerTimestamp: +new Date(),
57
+ requestTimestamp: Date.now(),
58
+ responseTimestamp: Date.now(),
59
+ addedTimestamp: Date.now(),
60
+ triggerTimestamp: Date.now(),
61
61
  isCanceled: false,
62
62
  isOffline: false,
63
63
  willRetry: false,
@@ -69,10 +69,10 @@ describe("Cache [ Utils ]", () => {
69
69
  success: true,
70
70
  extra: xhrExtra,
71
71
  retries: 0,
72
- requestTimestamp: +new Date(),
73
- responseTimestamp: +new Date(),
74
- addedTimestamp: +new Date(),
75
- triggerTimestamp: +new Date(),
72
+ requestTimestamp: Date.now(),
73
+ responseTimestamp: Date.now(),
74
+ addedTimestamp: Date.now(),
75
+ triggerTimestamp: Date.now(),
76
76
  isCanceled: false,
77
77
  isOffline: false,
78
78
  willRetry: false,
@@ -1,5 +1,4 @@
1
1
  import { createHttpMockingServer } from "@hyper-fetch/testing";
2
-
3
2
  import { Client } from "client";
4
3
 
5
4
  const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
@@ -1,10 +1,9 @@
1
1
  import { createHttpMockingServer } from "@hyper-fetch/testing";
2
-
2
+ import { Adapter } from "adapter";
3
3
  import { Cache } from "cache";
4
+ import { Client } from "client";
4
5
  import { Dispatcher } from "dispatcher";
5
6
  import { AppManager } from "managers";
6
- import { Client } from "client";
7
- import { Adapter } from "adapter";
8
7
 
9
8
  const { resetMocks, startServer, stopServer } = createHttpMockingServer();
10
9
 
@@ -1,5 +1,4 @@
1
1
  import { createHttpMockingServer } from "@hyper-fetch/testing";
2
-
3
2
  import { Client } from "client";
4
3
  import { Request } from "request";
5
4
 
@@ -1,5 +1,5 @@
1
- import { Client } from "client";
2
1
  import type { CacheValueType } from "cache";
2
+ import { Client } from "client";
3
3
  import { xhrExtra } from "http-adapter";
4
4
  import { scopeKey } from "request";
5
5
 
@@ -69,10 +69,10 @@ describe("Client [ Hydration ]", () => {
69
69
  error: null,
70
70
  success: true,
71
71
  extra: {},
72
- responseTimestamp: +new Date(),
73
- requestTimestamp: +new Date(),
74
- addedTimestamp: +new Date(),
75
- triggerTimestamp: +new Date(),
72
+ responseTimestamp: Date.now(),
73
+ requestTimestamp: Date.now(),
74
+ addedTimestamp: Date.now(),
75
+ triggerTimestamp: Date.now(),
76
76
  retries: 0,
77
77
  isCanceled: false,
78
78
  isOffline: false,
@@ -1,10 +1,10 @@
1
1
  import { createHttpMockingServer } from "@hyper-fetch/testing";
2
-
3
- import { interceptorCallback } from "../../utils";
4
- import { testCallbacksExecution } from "../../shared";
5
2
  import { Client } from "client";
6
3
  import { xhrExtra } from "http-adapter";
7
4
 
5
+ import { testCallbacksExecution } from "../../shared";
6
+ import { interceptorCallback } from "../../utils";
7
+
8
8
  const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
9
9
 
10
10
  describe("Client [ Interceptor ]", () => {
@@ -1,12 +1,12 @@
1
1
  import { createHttpMockingServer } from "@hyper-fetch/testing";
2
-
3
- import { Plugin } from "plugin";
4
2
  import { Client } from "client";
3
+ import type { FetchAdapterOptionsType, QueryStringifyOptionsType } from "http-adapter";
4
+ import { LoggerManager } from "managers";
5
+ import { Plugin } from "plugin";
5
6
  import type { RequestOptionsType } from "request";
6
7
  import { Request } from "request";
7
- import { LoggerManager } from "managers";
8
+
8
9
  import { createAdapter, interceptorCallback, middlewareCallback } from "../../utils";
9
- import type { FetchAdapterOptionsType, QueryStringifyOptionsType } from "http-adapter";
10
10
 
11
11
  const { resetMocks, startServer, stopServer } = createHttpMockingServer();
12
12
 
@@ -36,7 +36,7 @@ describe("Client [ Methods ]", () => {
36
36
  expect(req.method).toBe("POST");
37
37
  });
38
38
  it("should assign default adapter config [setAdapterDefaultOptions]", async () => {
39
- const options: FetchAdapterOptionsType = { timeout: 12312312 };
39
+ const options: FetchAdapterOptionsType = { timeout: 12_312_312 };
40
40
  client.adapter.setAdapterDefaults(() => options);
41
41
 
42
42
  expect(client.adapter.unstable_getAdapterDefaults?.(request)).toEqual(options);
@@ -1,8 +1,8 @@
1
1
  import { createHttpMockingServer } from "@hyper-fetch/testing";
2
+ import { Client } from "client";
2
3
 
3
- import { middlewareCallback } from "../../utils";
4
4
  import { testCallbacksExecution } from "../../shared";
5
- import { Client } from "client";
5
+ import { middlewareCallback } from "../../utils";
6
6
 
7
7
  const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
8
8