@hyper-fetch/core 8.1.1 → 8.2.2

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 (166) hide show
  1. package/README.md +137 -52
  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 +7 -12
  44. package/__tests__/features/sdk/sdk.configuration.spec.ts +5 -7
  45. package/__tests__/features/sdk/sdk.request-fields.spec.ts +80 -0
  46. package/__tests__/features/sdk/sdk.types.spec.ts +283 -0
  47. package/__tests__/shared/callbacks.shared.ts +1 -1
  48. package/__tests__/tsconfig.json +4 -17
  49. package/__tests__/utils/adapter.utils.ts +1 -2
  50. package/__tests__/utils/callbacks.utils.ts +1 -2
  51. package/__tests__/utils/dispatcher.utils.ts +1 -1
  52. package/__tests__/vitest.setup.ts +1 -1
  53. package/dist/adapter/adapter.d.ts +22 -5
  54. package/dist/adapter/adapter.d.ts.map +1 -1
  55. package/dist/adapter/adapter.types.d.ts +9 -1
  56. package/dist/adapter/adapter.types.d.ts.map +1 -1
  57. package/dist/adapter/adapter.utils.d.ts.map +1 -1
  58. package/dist/cache/cache.d.ts +6 -9
  59. package/dist/cache/cache.d.ts.map +1 -1
  60. package/dist/cache/cache.events.d.ts +7 -4
  61. package/dist/cache/cache.events.d.ts.map +1 -1
  62. package/dist/cache/cache.types.d.ts +1 -0
  63. package/dist/cache/cache.types.d.ts.map +1 -1
  64. package/dist/client/client.create.d.ts +1 -1
  65. package/dist/client/client.create.d.ts.map +1 -1
  66. package/dist/client/client.d.ts +8 -4
  67. package/dist/client/client.d.ts.map +1 -1
  68. package/dist/client/client.types.d.ts +1 -1
  69. package/dist/client/client.types.d.ts.map +1 -1
  70. package/dist/client/client.utils.d.ts.map +1 -1
  71. package/dist/constants/http.constants.d.ts +5 -1
  72. package/dist/constants/http.constants.d.ts.map +1 -1
  73. package/dist/constants/time.constants.d.ts.map +1 -1
  74. package/dist/dispatcher/dispatcher.d.ts +4 -4
  75. package/dist/dispatcher/dispatcher.d.ts.map +1 -1
  76. package/dist/dispatcher/dispatcher.events.d.ts +1 -1
  77. package/dist/dispatcher/dispatcher.events.d.ts.map +1 -1
  78. package/dist/dispatcher/dispatcher.utils.d.ts +1 -1
  79. package/dist/dispatcher/dispatcher.utils.d.ts.map +1 -1
  80. package/dist/http-adapter/http-adapter.constants.d.ts.map +1 -1
  81. package/dist/http-adapter/http-adapter.d.ts.map +1 -1
  82. package/dist/http-adapter/http-adapter.fetch.d.ts +2 -2
  83. package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -1
  84. package/dist/http-adapter/http-adapter.types.d.ts +2 -2
  85. package/dist/http-adapter/http-adapter.types.d.ts.map +1 -1
  86. package/dist/http-adapter/http-adapter.utils.d.ts.map +1 -1
  87. package/dist/index.mjs +2382 -2301
  88. package/dist/index.mjs.map +1 -1
  89. package/dist/managers/app/app.manager.constants.d.ts +1 -1
  90. package/dist/managers/app/app.manager.constants.d.ts.map +1 -1
  91. package/dist/managers/app/app.manager.d.ts +1 -1
  92. package/dist/managers/app/app.manager.d.ts.map +1 -1
  93. package/dist/managers/app/app.manager.events.d.ts +9 -0
  94. package/dist/managers/app/app.manager.events.d.ts.map +1 -1
  95. package/dist/managers/logger/logger.manager.d.ts +1 -1
  96. package/dist/managers/logger/logger.manager.d.ts.map +1 -1
  97. package/dist/managers/request/request.manager.d.ts.map +1 -1
  98. package/dist/managers/request/request.manager.events.d.ts +14 -3
  99. package/dist/managers/request/request.manager.events.d.ts.map +1 -1
  100. package/dist/mocker/mocker.types.d.ts +1 -0
  101. package/dist/mocker/mocker.types.d.ts.map +1 -1
  102. package/dist/plugin/plugin.d.ts +17 -1
  103. package/dist/plugin/plugin.d.ts.map +1 -1
  104. package/dist/plugin/plugin.types.d.ts +5 -4
  105. package/dist/plugin/plugin.types.d.ts.map +1 -1
  106. package/dist/request/request.d.ts +33 -4
  107. package/dist/request/request.d.ts.map +1 -1
  108. package/dist/request/request.hooks.d.ts +2 -2
  109. package/dist/request/request.hooks.d.ts.map +1 -1
  110. package/dist/request/request.types.d.ts +92 -5
  111. package/dist/request/request.types.d.ts.map +1 -1
  112. package/dist/request/request.utils.d.ts.map +1 -1
  113. package/dist/sdk/sdk.d.ts +25 -5
  114. package/dist/sdk/sdk.d.ts.map +1 -1
  115. package/dist/types/http.types.d.ts +1 -1
  116. package/dist/types/http.types.d.ts.map +1 -1
  117. package/dist/types/request.types.d.ts +2 -2
  118. package/dist/types/request.types.d.ts.map +1 -1
  119. package/dist/utils/emitter.utils.d.ts +1 -1
  120. package/dist/utils/emitter.utils.d.ts.map +1 -1
  121. package/package.json +8 -8
  122. package/src/adapter/adapter.bindings.ts +14 -14
  123. package/src/adapter/adapter.ts +28 -10
  124. package/src/adapter/adapter.types.ts +10 -1
  125. package/src/adapter/adapter.utils.ts +5 -5
  126. package/src/cache/cache.events.ts +7 -5
  127. package/src/cache/cache.ts +14 -17
  128. package/src/cache/cache.types.ts +1 -0
  129. package/src/client/client.create.ts +1 -1
  130. package/src/client/client.ts +14 -9
  131. package/src/client/client.types.ts +2 -2
  132. package/src/client/client.utils.ts +3 -2
  133. package/src/constants/http.constants.ts +5 -0
  134. package/src/constants/time.constants.ts +1 -1
  135. package/src/dispatcher/dispatcher.events.ts +1 -2
  136. package/src/dispatcher/dispatcher.ts +13 -13
  137. package/src/dispatcher/dispatcher.utils.ts +10 -4
  138. package/src/http-adapter/http-adapter.constants.ts +1 -0
  139. package/src/http-adapter/http-adapter.fetch.ts +19 -9
  140. package/src/http-adapter/http-adapter.ts +2 -1
  141. package/src/http-adapter/http-adapter.types.ts +3 -2
  142. package/src/http-adapter/http-adapter.utils.ts +15 -11
  143. package/src/managers/app/app.manager.constants.ts +2 -1
  144. package/src/managers/app/app.manager.events.ts +9 -1
  145. package/src/managers/app/app.manager.ts +1 -1
  146. package/src/managers/app/app.manager.utils.ts +2 -2
  147. package/src/managers/logger/logger.manager.ts +1 -2
  148. package/src/managers/request/request.manager.events.ts +14 -12
  149. package/src/managers/request/request.manager.ts +4 -4
  150. package/src/mocker/mocker.ts +2 -2
  151. package/src/mocker/mocker.types.ts +1 -0
  152. package/src/plugin/plugin.ts +17 -1
  153. package/src/plugin/plugin.types.ts +5 -4
  154. package/src/request/request.hooks.ts +6 -5
  155. package/src/request/request.ts +77 -34
  156. package/src/request/request.types.ts +107 -5
  157. package/src/request/request.utils.ts +17 -16
  158. package/src/sdk/sdk.ts +79 -23
  159. package/src/types/http.types.ts +1 -1
  160. package/src/types/request.types.ts +3 -2
  161. package/src/utils/emitter.utils.ts +1 -1
  162. package/src/utils/uuid.utils.ts +1 -1
  163. package/tsconfig.json +30 -2
  164. package/vite.config.ts +14 -20
  165. package/.eslintrc.cjs +0 -12
  166. package/tsconfig.base.json +0 -7
package/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # ⚡ Hyper Fetch
2
2
 
3
- <p>
3
+ <p align="center">
4
+ <b>One SDK for every API. The type-safe API layer for TypeScript apps — REST, GraphQL, WebSockets, SSE, Firebase, and more.</b>
5
+ </p>
6
+
7
+ <p align="center">
4
8
  <a href="https://bettertyped.com/">
5
9
  <img src="https://custom-icon-badges.demolab.com/static/v1?label=&message=BetterTyped&color=333&logo=BT" />
6
10
  </a>
@@ -13,61 +17,142 @@
13
17
  <a href="https://www.npmjs.com/package/@hyper-fetch/core">
14
18
  <img src="https://custom-icon-badges.demolab.com/npm/v/@hyper-fetch/core.svg?logo=npm&color=e76f51" />
15
19
  </a>
20
+ <a href="https://www.npmjs.com/package/@hyper-fetch/core">
21
+ <img src="https://custom-icon-badges.demolab.com/npm/dm/@hyper-fetch/core?logoColor=fff&logo=trending-up" />
22
+ </a>
16
23
  <a href="https://github.com/BetterTyped/hyper-fetch">
17
24
  <img src="https://custom-icon-badges.demolab.com/badge/typescript-%23007ACC.svg?logo=typescript&logoColor=white" />
18
25
  </a>
19
- <a href="https://www.npmjs.com/package/@hyper-fetch/core">
26
+ <a href="https://bundlephobia.com/package/@hyper-fetch/core">
20
27
  <img src="https://custom-icon-badges.demolab.com/bundlephobia/minzip/@hyper-fetch/core?color=64BC4B&logo=package" />
21
28
  </a>
22
29
  </p>
23
30
 
24
- ## About
25
-
26
- **`Hyper Fetch`** is a data-exchange framework. What makes it unique is the **typesafe design** and the **ease of use**.
27
- This library is `backend and framework agnostic`, with aim to provide as many great and useful features as possible. In
28
- particular `caching`, `queuing`, `persistence`, `offline first support`, `request deduplication`, `authentication`,
29
- `progress tracking`, `structure and architecture` guidelines.
30
-
31
- ## Features
32
-
33
- 🔮 **Simple setup** - [Read more](https://hyperfetch.bettertyped.com/docs/guides/basic/setup)
34
-
35
- 🎯 **Request cancellation** - [Read more](https://hyperfetch.bettertyped.com/docs/guides/advanced/cancellation)
36
-
37
- ✨ **Window Focus/Blur Events** -
38
- [Read more](https://hyperfetch.bettertyped.com/docs/guides/react/core/window-focus-blur)
39
-
40
- 🚀 **Queueing** - [Read more](https://hyperfetch.bettertyped.com/docs/guides/advanced/queueing)
41
-
42
- 💎 **Automatic caching** - [Read more](https://hyperfetch.bettertyped.com/docs/core/cache)
43
-
44
- 🪄 **Persistence** - [Read more](https://hyperfetch.bettertyped.com/docs/guides/advanced/persistence)
45
-
46
- 🎊 **SSR Support** - [Read more](https://hyperfetch.bettertyped.com/docs/getting-started/environment)
47
-
48
- 🔋 **Offline First** - [Read more](https://hyperfetch.bettertyped.com/docs/guides/advanced/offline)
49
-
50
- 📡 **Built-in adapter** - [Read more](https://hyperfetch.bettertyped.com/docs/core/adapter)
51
-
52
- 🧪 **Easy to test** - [Read more](https://hyperfetch.bettertyped.com/docs/getting-started/testing)
53
-
54
- 🎟 **Authentication** - [Read more](https://hyperfetch.bettertyped.com/docs/guides/basic/authentication)
55
-
56
- 💡 **Prefetching** - [Read more](https://hyperfetch.bettertyped.com/docs/guides/advanced/prefetching)
57
-
58
- ## Help me keep working on this project ❤️
59
-
60
- - [Become a Sponsor on GitHub](https://github.com/sponsors/prc5)
61
-
62
- ## Sources
63
-
64
- - #### [Installation](https://hyperfetch.bettertyped.com/docs/getting-started/installation)
65
- - #### [Docs](https://hyperfetch.bettertyped.com/)
66
- - #### [API](https://hyperfetch.bettertyped.com/api/)
67
- - #### [NPM](https://www.npmjs.com/package/@hyper-fetch/core)
68
- - #### [Guides](https://hyperfetch.bettertyped.com/guides/Basic/Setup)
69
-
70
- ## Other Packages
71
-
72
- - #### [Hyper Fetch Sockets](https://github.com/BetterTyped/hyper-fetch/tree/main/packages/sockets)
73
- - #### [React Hyper Fetch](https://github.com/BetterTyped/hyper-fetch/tree/main/packages/react)
31
+ ## 📖 About
32
+
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.
36
+
37
+ ## 🎯 Key Capabilities
38
+
39
+ - 🔮 **Zero guesswork** — Full autocompletion for every request, response, param, and error. Never cast to `any` again
40
+ - 📡 **One interface for everything** — REST, GraphQL, Firebase, WebSockets, SSE — switch APIs without rewriting a line
41
+ - 💾 **Stop re-fetching what you already have** — Smart cache with TTL and invalidation out of the box
42
+ - 🔁 **Failed requests retry themselves** — Configurable retry with backoff so users never see random failures
43
+ - 📴 **Works without internet** — Queues requests offline and replays them when connection returns
44
+ - 🚫 **No duplicate requests** — Concurrent identical calls are deduplicated automatically
45
+ - ⏱️ **Know exactly what's happening** — Upload and download progress with time estimates for every request
46
+ - 🎯 **Cancel anything, anytime** — Kill pending requests individually or by group with one call
47
+ - 🔌 **Swap engines without touching your code** — Switch between Axios, GraphQL, Firebase adapters in one line
48
+ - 🪄 **Generate your SDK** — Point CLI at an OpenAPI schema and get a fully typed client instantly
49
+
50
+ ## 🚀 Quick Start
51
+
52
+ ```bash
53
+ npm install @hyper-fetch/core
54
+ ```
55
+
56
+ ```ts
57
+ import { createClient } from "@hyper-fetch/core";
58
+
59
+ // Single entry point — all requests inherit this base URL
60
+ const client = createClient({ url: "https://api.example.com" });
61
+
62
+ // Define a request with typed response — :userId becomes a required param
63
+ const getUser = client.createRequest<{ response: { id: number; name: string } }>()({
64
+ endpoint: "/users/:userId",
65
+ method: "GET",
66
+ });
67
+
68
+ // setParams is typed from the endpoint string, send() returns typed data
69
+ const { data, error } = await getUser.setParams({ userId: 1 }).send();
70
+ ```
71
+
72
+ ## 📚 Documentation
73
+
74
+ - [Getting Started](https://hyperfetch.bettertyped.com/docs/getting-started/quick-start)
75
+ - [Core Overview](https://hyperfetch.bettertyped.com/docs/core/overview)
76
+ - [API Reference](https://hyperfetch.bettertyped.com/api/)
77
+
78
+ ## 💡 Examples
79
+
80
+ ### Typed requests with params, payload, and query params
81
+
82
+ ```ts
83
+ interface User {
84
+ id: number;
85
+ name: string;
86
+ email: string;
87
+ }
88
+
89
+ // GET request — only response type needed, params inferred from :userId
90
+ const getUser = client.createRequest<{ response: User }>()({
91
+ endpoint: "/users/:userId",
92
+ method: "GET",
93
+ });
94
+
95
+ // POST request — define both response and payload types
96
+ const createUser = client.createRequest<{
97
+ response: User;
98
+ payload: { name: string; email: string };
99
+ }>()({
100
+ endpoint: "/users",
101
+ method: "POST",
102
+ });
103
+
104
+ // Params are type-checked: { userId: number } required here
105
+ const { data } = await getUser.setParams({ userId: 1 }).send();
106
+
107
+ // Payload is type-checked: { name, email } required here
108
+ const { data: newUser } = await createUser.send({
109
+ data: { name: "Jane", email: "jane@example.com" },
110
+ });
111
+ ```
112
+
113
+ ### Dynamic data with query params
114
+
115
+ ```ts
116
+ // Define allowed query params — all are optional and type-checked
117
+ const listUsers = client.createRequest<{
118
+ response: User[];
119
+ queryParams: { page?: number; limit?: number; search?: string };
120
+ }>()({
121
+ endpoint: "/users",
122
+ method: "GET",
123
+ });
124
+
125
+ // Query params are appended to the URL: /users?page=1&limit=20&search=john
126
+ const { data } = await listUsers.setQueryParams({ page: 1, limit: 20, search: "john" }).send();
127
+ ```
128
+
129
+ ### Lifecycle callbacks
130
+
131
+ ```ts
132
+ // Hook into the request lifecycle — track progress, log events, handle responses
133
+ const { data } = await getUser.setParams({ userId: 1 }).send({
134
+ onStart: ({ requestId }) => console.log(`Request ${requestId} started`),
135
+ onResponse: ({ response }) => console.log("Got data:", response.data),
136
+ onUploadProgress: ({ progress }) => console.log(`Upload: ${progress}%`),
137
+ onDownloadProgress: ({ progress }) => console.log(`Download: ${progress}%`),
138
+ });
139
+ ```
140
+
141
+ ### Response mapping
142
+
143
+ ```ts
144
+ // Transform the response before it reaches your code
145
+ const getUser = client
146
+ .createRequest<{ response: User }>()({
147
+ endpoint: "/users/:userId",
148
+ method: "GET",
149
+ })
150
+ .setResponseMapper((response) => ({
151
+ ...response,
152
+ data: { ...response.data, name: response.data.name.toUpperCase() },
153
+ }));
154
+ ```
155
+
156
+ ## License
157
+
158
+ [MIT](https://github.com/BetterTyped/hyper-fetch/blob/main/License.md)
@@ -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,