@hyper-fetch/core 7.5.3 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (270) hide show
  1. package/.eslintrc.cjs +12 -0
  2. package/__tests__/e2e/e2e.abort-timeout.spec.ts +101 -0
  3. package/__tests__/e2e/e2e.advanced.spec.ts +423 -0
  4. package/__tests__/e2e/e2e.methods.spec.ts +243 -0
  5. package/__tests__/e2e/e2e.payloads.spec.ts +154 -0
  6. package/__tests__/features/adapter/adapter.base.spec.ts +44 -17
  7. package/__tests__/features/adapter/adapter.bindings.spec.ts +35 -33
  8. package/__tests__/features/adapter/adapter.browser.spec.ts +5 -20
  9. package/__tests__/features/adapter/adapter.server.spec.ts +229 -37
  10. package/__tests__/features/cache/cache.base.spec.ts +23 -15
  11. package/__tests__/features/cache/cache.events.spec.ts +9 -5
  12. package/__tests__/features/cache/cache.garbage-collector.spec.ts +9 -6
  13. package/__tests__/features/cache/cache.lazy-storage.spec.ts +11 -8
  14. package/__tests__/features/cache/cache.modes.spec.ts +145 -0
  15. package/__tests__/features/cache/cache.scoping.spec.ts +171 -0
  16. package/__tests__/features/cache/cache.typescript.spec.ts +147 -0
  17. package/__tests__/features/cache/cache.utils.spec.ts +10 -3
  18. package/__tests__/features/client/client.auth.spec.ts +5 -5
  19. package/__tests__/features/client/client.base.spec.ts +16 -4
  20. package/__tests__/features/client/client.from-json.spec.ts +182 -0
  21. package/__tests__/features/client/client.hydration.spec.ts +69 -4
  22. package/__tests__/features/client/client.interceptor.spec.ts +4 -4
  23. package/__tests__/features/client/client.methods.spec.ts +5 -4
  24. package/__tests__/features/client/client.middleware.spec.ts +4 -4
  25. package/__tests__/features/client/client.utils.spec.ts +3 -3
  26. package/__tests__/features/dispatcher/dispatcher.base.spec.ts +8 -8
  27. package/__tests__/features/dispatcher/dispatcher.custom-storage.spec.ts +189 -0
  28. package/__tests__/features/dispatcher/dispatcher.events.spec.ts +17 -12
  29. package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +31 -31
  30. package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +15 -15
  31. package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +22 -5
  32. package/__tests__/features/managers/app/app.manager.base.spec.ts +23 -23
  33. package/__tests__/features/managers/app/app.manager.ssr.spec.ts +6 -3
  34. package/__tests__/features/managers/logger/logger.base.spec.ts +4 -2
  35. package/__tests__/features/managers/logger/logger.utils.spec.ts +10 -4
  36. package/__tests__/features/managers/request/request.manager.base.spec.ts +21 -19
  37. package/__tests__/features/managers/request/request.manager.events.spec.ts +137 -53
  38. package/__tests__/features/mocker/mocker.base.spec.ts +82 -10
  39. package/__tests__/features/plugin/plugin.base.spec.ts +19 -19
  40. package/__tests__/features/request/request.base.spec.ts +39 -13
  41. package/__tests__/features/request/request.cloning.spec.ts +1 -1
  42. package/__tests__/features/request/request.hooks.spec.ts +173 -0
  43. package/__tests__/features/request/request.optimistic.spec.ts +93 -0
  44. package/__tests__/features/request/request.retry-on-error.spec.ts +359 -0
  45. package/__tests__/features/request/request.sending.spec.ts +107 -15
  46. package/__tests__/features/request/request.setters.spec.ts +11 -4
  47. package/__tests__/features/request/request.typescript.spec.ts +1 -1
  48. package/__tests__/features/request/request.utils.spec.ts +2 -2
  49. package/__tests__/features/sdk/sdk.base.spec.ts +5 -3
  50. package/__tests__/features/sdk/sdk.configuration.spec.ts +262 -0
  51. package/__tests__/features/utils/emitter.utils.spec.ts +88 -0
  52. package/__tests__/shared/callbacks.shared.ts +3 -1
  53. package/__tests__/shared/progress.shared.ts +2 -1
  54. package/__tests__/tsconfig.json +14 -3
  55. package/__tests__/type-stress/generate-stress-test.ts +92 -0
  56. package/__tests__/type-stress/stress-10.ts +35 -0
  57. package/__tests__/type-stress/stress-just-create.ts +2107 -0
  58. package/__tests__/type-stress/stress-minimal.ts +23 -0
  59. package/__tests__/type-stress/stress-test-500.ts +2319 -0
  60. package/__tests__/type-stress/stress-test-sdk.ts +210 -0
  61. package/__tests__/type-stress/tsconfig.json +6 -0
  62. package/__tests__/utils/adapter.utils.ts +3 -2
  63. package/__tests__/utils/cache.utils.ts +3 -2
  64. package/__tests__/utils/callbacks.utils.ts +5 -4
  65. package/__tests__/utils/dispatcher.utils.ts +3 -2
  66. package/__tests__/vitest.setup.ts +15 -0
  67. package/dist/adapter/adapter.bindings.d.ts +71 -0
  68. package/dist/adapter/adapter.bindings.d.ts.map +1 -0
  69. package/dist/adapter/adapter.d.ts +127 -0
  70. package/dist/adapter/adapter.d.ts.map +1 -0
  71. package/dist/adapter/adapter.types.d.ts +108 -0
  72. package/dist/adapter/adapter.types.d.ts.map +1 -0
  73. package/dist/adapter/adapter.utils.d.ts +23 -0
  74. package/dist/adapter/adapter.utils.d.ts.map +1 -0
  75. package/dist/adapter/index.d.ts +5 -0
  76. package/dist/adapter/index.d.ts.map +1 -0
  77. package/dist/cache/cache.d.ts +95 -0
  78. package/dist/cache/cache.d.ts.map +1 -0
  79. package/dist/cache/cache.events.d.ts +51 -0
  80. package/dist/cache/cache.events.d.ts.map +1 -0
  81. package/dist/cache/cache.types.d.ts +44 -0
  82. package/dist/cache/cache.types.d.ts.map +1 -0
  83. package/dist/cache/cache.utils.d.ts +11 -0
  84. package/dist/cache/cache.utils.d.ts.map +1 -0
  85. package/dist/cache/index.d.ts +5 -0
  86. package/dist/cache/index.d.ts.map +1 -0
  87. package/dist/client/client.constants.d.ts +10 -0
  88. package/dist/client/client.constants.d.ts.map +1 -0
  89. package/dist/client/client.create.d.ts +8 -0
  90. package/dist/client/client.create.d.ts.map +1 -0
  91. package/dist/client/client.d.ts +195 -0
  92. package/dist/client/client.d.ts.map +1 -0
  93. package/dist/client/client.types.d.ts +71 -0
  94. package/dist/client/client.types.d.ts.map +1 -0
  95. package/dist/client/client.utils.d.ts +11 -0
  96. package/dist/client/client.utils.d.ts.map +1 -0
  97. package/dist/client/index.d.ts +6 -0
  98. package/dist/client/index.d.ts.map +1 -0
  99. package/dist/constants/http.constants.d.ts +8 -0
  100. package/dist/constants/http.constants.d.ts.map +1 -0
  101. package/dist/constants/time.constants.d.ts +12 -0
  102. package/dist/constants/time.constants.d.ts.map +1 -0
  103. package/dist/dispatcher/dispatcher.constants.d.ts +7 -0
  104. package/dist/dispatcher/dispatcher.constants.d.ts.map +1 -0
  105. package/dist/dispatcher/dispatcher.d.ts +168 -0
  106. package/dist/dispatcher/dispatcher.d.ts.map +1 -0
  107. package/dist/dispatcher/dispatcher.events.d.ts +49 -0
  108. package/dist/dispatcher/dispatcher.events.d.ts.map +1 -0
  109. package/dist/dispatcher/dispatcher.types.d.ts +44 -0
  110. package/dist/dispatcher/dispatcher.types.d.ts.map +1 -0
  111. package/dist/dispatcher/dispatcher.utils.d.ts +12 -0
  112. package/dist/dispatcher/dispatcher.utils.d.ts.map +1 -0
  113. package/dist/dispatcher/index.d.ts +6 -0
  114. package/dist/dispatcher/index.d.ts.map +1 -0
  115. package/dist/http-adapter/http-adapter.constants.d.ts +4 -0
  116. package/dist/http-adapter/http-adapter.constants.d.ts.map +1 -0
  117. package/dist/http-adapter/http-adapter.d.ts +3 -0
  118. package/dist/http-adapter/http-adapter.d.ts.map +1 -0
  119. package/dist/http-adapter/http-adapter.fetch.d.ts +6 -0
  120. package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -0
  121. package/dist/http-adapter/http-adapter.types.d.ts +73 -0
  122. package/dist/http-adapter/http-adapter.types.d.ts.map +1 -0
  123. package/dist/http-adapter/http-adapter.utils.d.ts +10 -0
  124. package/dist/http-adapter/http-adapter.utils.d.ts.map +1 -0
  125. package/dist/http-adapter/index.d.ts +6 -0
  126. package/dist/http-adapter/index.d.ts.map +1 -0
  127. package/dist/index.d.ts +15 -2083
  128. package/dist/index.d.ts.map +1 -0
  129. package/dist/index.mjs +4084 -0
  130. package/dist/index.mjs.map +1 -0
  131. package/dist/managers/app/app.manager.constants.d.ts +10 -0
  132. package/dist/managers/app/app.manager.constants.d.ts.map +1 -0
  133. package/dist/managers/app/app.manager.d.ts +35 -0
  134. package/dist/managers/app/app.manager.d.ts.map +1 -0
  135. package/dist/managers/app/app.manager.events.d.ts +12 -0
  136. package/dist/managers/app/app.manager.events.d.ts.map +1 -0
  137. package/dist/managers/app/app.manager.types.d.ts +7 -0
  138. package/dist/managers/app/app.manager.types.d.ts.map +1 -0
  139. package/dist/managers/app/app.manager.utils.d.ts +5 -0
  140. package/dist/managers/app/app.manager.utils.d.ts.map +1 -0
  141. package/dist/managers/app/index.d.ts +6 -0
  142. package/dist/managers/app/index.d.ts.map +1 -0
  143. package/dist/managers/index.d.ts +4 -0
  144. package/dist/managers/index.d.ts.map +1 -0
  145. package/dist/managers/logger/index.d.ts +5 -0
  146. package/dist/managers/logger/index.d.ts.map +1 -0
  147. package/dist/managers/logger/logger.manager.constants.d.ts +5 -0
  148. package/dist/managers/logger/logger.manager.constants.d.ts.map +1 -0
  149. package/dist/managers/logger/logger.manager.d.ts +22 -0
  150. package/dist/managers/logger/logger.manager.d.ts.map +1 -0
  151. package/dist/managers/logger/logger.manager.types.d.ts +43 -0
  152. package/dist/managers/logger/logger.manager.types.d.ts.map +1 -0
  153. package/dist/managers/logger/logger.manager.utils.d.ts +4 -0
  154. package/dist/managers/logger/logger.manager.utils.d.ts.map +1 -0
  155. package/dist/managers/request/index.d.ts +5 -0
  156. package/dist/managers/request/index.d.ts.map +1 -0
  157. package/dist/managers/request/request.manager.d.ts +60 -0
  158. package/dist/managers/request/request.manager.d.ts.map +1 -0
  159. package/dist/managers/request/request.manager.events.d.ts +55 -0
  160. package/dist/managers/request/request.manager.events.d.ts.map +1 -0
  161. package/dist/managers/request/request.manager.types.d.ts +48 -0
  162. package/dist/managers/request/request.manager.types.d.ts.map +1 -0
  163. package/dist/managers/request/request.manager.utils.d.ts +30 -0
  164. package/dist/managers/request/request.manager.utils.d.ts.map +1 -0
  165. package/dist/mocker/index.d.ts +3 -0
  166. package/dist/mocker/index.d.ts.map +1 -0
  167. package/dist/mocker/mocker.d.ts +3 -0
  168. package/dist/mocker/mocker.d.ts.map +1 -0
  169. package/dist/mocker/mocker.types.d.ts +25 -0
  170. package/dist/mocker/mocker.types.d.ts.map +1 -0
  171. package/dist/plugin/index.d.ts +3 -0
  172. package/dist/plugin/index.d.ts.map +1 -0
  173. package/dist/plugin/plugin.d.ts +55 -0
  174. package/dist/plugin/plugin.d.ts.map +1 -0
  175. package/dist/plugin/plugin.types.d.ts +122 -0
  176. package/dist/plugin/plugin.types.d.ts.map +1 -0
  177. package/dist/request/index.d.ts +5 -0
  178. package/dist/request/index.d.ts.map +1 -0
  179. package/dist/request/request.d.ts +195 -0
  180. package/dist/request/request.d.ts.map +1 -0
  181. package/dist/request/request.hooks.d.ts +19 -0
  182. package/dist/request/request.hooks.d.ts.map +1 -0
  183. package/dist/request/request.types.d.ts +240 -0
  184. package/dist/request/request.types.d.ts.map +1 -0
  185. package/dist/request/request.utils.d.ts +25 -0
  186. package/dist/request/request.utils.d.ts.map +1 -0
  187. package/dist/sdk/index.d.ts +2 -0
  188. package/dist/sdk/index.d.ts.map +1 -0
  189. package/dist/sdk/sdk.d.ts +69 -0
  190. package/dist/sdk/sdk.d.ts.map +1 -0
  191. package/dist/types/adapter.types.d.ts +22 -0
  192. package/dist/types/adapter.types.d.ts.map +1 -0
  193. package/dist/types/client.types.d.ts +4 -0
  194. package/dist/types/client.types.d.ts.map +1 -0
  195. package/dist/types/helpers.types.d.ts +17 -0
  196. package/dist/types/helpers.types.d.ts.map +1 -0
  197. package/dist/types/http.types.d.ts +3 -0
  198. package/dist/types/http.types.d.ts.map +1 -0
  199. package/dist/types/hydration.types.d.ts +15 -0
  200. package/dist/types/hydration.types.d.ts.map +1 -0
  201. package/dist/types/index.d.ts +7 -0
  202. package/dist/types/index.d.ts.map +1 -0
  203. package/dist/types/request.types.d.ts +34 -0
  204. package/dist/types/request.types.d.ts.map +1 -0
  205. package/dist/utils/emitter.utils.d.ts +13 -0
  206. package/dist/utils/emitter.utils.d.ts.map +1 -0
  207. package/dist/utils/index.d.ts +3 -0
  208. package/dist/utils/index.d.ts.map +1 -0
  209. package/dist/utils/uuid.utils.d.ts +3 -0
  210. package/dist/utils/uuid.utils.d.ts.map +1 -0
  211. package/package.json +10 -25
  212. package/src/adapter/adapter.bindings.ts +8 -12
  213. package/src/adapter/adapter.ts +8 -7
  214. package/src/adapter/adapter.types.ts +10 -6
  215. package/src/adapter/adapter.utils.ts +1 -1
  216. package/src/cache/cache.events.ts +4 -11
  217. package/src/cache/cache.ts +28 -22
  218. package/src/cache/cache.types.ts +8 -4
  219. package/src/cache/cache.utils.ts +3 -3
  220. package/src/client/client.create.ts +4 -3
  221. package/src/client/client.ts +44 -12
  222. package/src/client/client.types.ts +25 -3
  223. package/src/client/client.utils.ts +24 -4
  224. package/src/dispatcher/dispatcher.events.ts +3 -3
  225. package/src/dispatcher/dispatcher.ts +59 -28
  226. package/src/dispatcher/dispatcher.types.ts +19 -2
  227. package/src/dispatcher/dispatcher.utils.ts +5 -4
  228. package/src/http-adapter/http-adapter.constants.ts +1 -1
  229. package/src/http-adapter/http-adapter.fetch.ts +174 -0
  230. package/src/http-adapter/http-adapter.ts +3 -8
  231. package/src/http-adapter/http-adapter.types.ts +17 -9
  232. package/src/http-adapter/http-adapter.utils.ts +6 -9
  233. package/src/http-adapter/index.ts +1 -0
  234. package/src/managers/app/app.manager.constants.ts +2 -2
  235. package/src/managers/app/app.manager.events.ts +1 -1
  236. package/src/managers/app/app.manager.ts +2 -1
  237. package/src/managers/logger/logger.manager.constants.ts +1 -1
  238. package/src/managers/logger/logger.manager.ts +2 -1
  239. package/src/managers/logger/logger.manager.types.ts +2 -2
  240. package/src/managers/logger/logger.manager.utils.ts +2 -1
  241. package/src/managers/request/request.manager.events.ts +13 -11
  242. package/src/managers/request/request.manager.types.ts +6 -3
  243. package/src/mocker/mocker.ts +3 -2
  244. package/src/mocker/mocker.types.ts +1 -1
  245. package/src/plugin/plugin.ts +2 -2
  246. package/src/plugin/plugin.types.ts +9 -9
  247. package/src/request/index.ts +1 -0
  248. package/src/request/request.hooks.ts +87 -0
  249. package/src/request/request.ts +155 -77
  250. package/src/request/request.types.ts +54 -22
  251. package/src/request/request.utils.ts +86 -23
  252. package/src/sdk/sdk.ts +153 -8
  253. package/src/types/adapter.types.ts +1 -1
  254. package/src/types/client.types.ts +1 -1
  255. package/src/types/hydration.types.ts +3 -3
  256. package/src/types/request.types.ts +30 -20
  257. package/src/utils/uuid.utils.ts +1 -1
  258. package/tsconfig.base.json +1 -6
  259. package/tsconfig.json +1 -4
  260. package/vite.config.ts +45 -0
  261. package/.eslintrc.json +0 -11
  262. package/__tests__/global.d.ts +0 -1
  263. package/__tests__/jest.setup.ts +0 -13
  264. package/dist/browser/index.js +0 -3751
  265. package/dist/browser/index.js.map +0 -7
  266. package/dist/index.js +0 -4002
  267. package/dist/index.js.map +0 -7
  268. package/jest.config.ts +0 -6
  269. package/src/http-adapter/http-adapter.browser.ts +0 -102
  270. package/src/http-adapter/http-adapter.server.ts +0 -149
@@ -0,0 +1,147 @@
1
+ /* eslint-disable @typescript-eslint/no-unused-vars */
2
+ import { expectFunctionParametersType, expectNotFunctionParametersType } from "@hyper-fetch/testing";
3
+
4
+ import type { ResponseSuccessType } from "adapter";
5
+ import type { ResponseDetailsType } from "managers";
6
+ import { Client } from "client";
7
+ import type { HttpAdapterType } from "http-adapter";
8
+ import { xhrExtra } from "http-adapter";
9
+ import { createCache } from "../../utils";
10
+
11
+ interface UserResponse {
12
+ id: number;
13
+ name: string;
14
+ }
15
+
16
+ interface UserError {
17
+ code: string;
18
+ message: string;
19
+ }
20
+
21
+ const client = new Client({ url: "http://localhost:3000" });
22
+
23
+ const userRequest = client.createRequest<{ response: UserResponse; error: UserError }>()({
24
+ method: "GET",
25
+ endpoint: "/users/:id",
26
+ });
27
+
28
+ const anyRequest = client.createRequest<{ response: any }>()({
29
+ method: "GET",
30
+ endpoint: "/any",
31
+ });
32
+
33
+ const cache = createCache(client);
34
+
35
+ const successResponse: ResponseSuccessType<UserResponse, HttpAdapterType> & ResponseDetailsType = {
36
+ data: { id: 1, name: "John" },
37
+ error: null,
38
+ status: 200,
39
+ success: true,
40
+ extra: xhrExtra,
41
+ requestTimestamp: Date.now(),
42
+ responseTimestamp: Date.now(),
43
+ retries: 0,
44
+ addedTimestamp: Date.now(),
45
+ triggerTimestamp: Date.now(),
46
+ isCanceled: false,
47
+ isOffline: false,
48
+ willRetry: false,
49
+ };
50
+
51
+ describe("Cache [Types]", () => {
52
+ describe("when using cache.set", () => {
53
+ it("should allow setting correct response type for a typed request", () => {
54
+ expectFunctionParametersType(cache.set).assert([userRequest, successResponse]);
55
+ });
56
+
57
+ it("should allow setting response with null data", () => {
58
+ expectFunctionParametersType(cache.set).assert([
59
+ userRequest,
60
+ {
61
+ ...successResponse,
62
+ data: null,
63
+ error: { code: "ERR", message: "fail" },
64
+ success: false as const,
65
+ },
66
+ ]);
67
+ });
68
+
69
+ it("should not allow setting response with wrong data type", () => {
70
+ expectNotFunctionParametersType(cache.set).assert([
71
+ userRequest,
72
+ {
73
+ ...successResponse,
74
+ data: "wrong-type",
75
+ },
76
+ ]);
77
+ });
78
+
79
+ it("should not allow setting response with wrong error type", () => {
80
+ expectNotFunctionParametersType(cache.set).assert([
81
+ userRequest,
82
+ {
83
+ ...successResponse,
84
+ data: null,
85
+ error: "string-error",
86
+ success: false as const,
87
+ },
88
+ ]);
89
+ });
90
+
91
+ it("should allow setting response with correct error type", () => {
92
+ expectFunctionParametersType(cache.set).assert([
93
+ userRequest,
94
+ {
95
+ ...successResponse,
96
+ data: null,
97
+ error: { code: "NOT_FOUND", message: "User not found" },
98
+ success: false as const,
99
+ },
100
+ ]);
101
+ });
102
+
103
+ it("should allow any data type when request has response: any", () => {
104
+ expectFunctionParametersType(cache.set).assert([
105
+ anyRequest,
106
+ {
107
+ ...successResponse,
108
+ data: "string-data",
109
+ },
110
+ ]);
111
+
112
+ expectFunctionParametersType(cache.set).assert([
113
+ anyRequest,
114
+ {
115
+ ...successResponse,
116
+ data: { anything: true },
117
+ },
118
+ ]);
119
+ });
120
+ });
121
+
122
+ describe("when using cache.update", () => {
123
+ it("should allow partial response update for a typed request", () => {
124
+ expectFunctionParametersType(cache.update).assert([userRequest, { data: { id: 2, name: "Jane" } }]);
125
+ });
126
+
127
+ it("should allow partial update with only error field", () => {
128
+ expectFunctionParametersType(cache.update).assert([userRequest, { error: { code: "ERR", message: "fail" } }]);
129
+ });
130
+
131
+ it("should not allow partial update with wrong data type", () => {
132
+ expectNotFunctionParametersType(cache.update).assert([userRequest, { data: "wrong-type" }]);
133
+ });
134
+
135
+ it("should not allow partial update with wrong error type", () => {
136
+ expectNotFunctionParametersType(cache.update).assert([userRequest, { error: "string-error" }]);
137
+ });
138
+
139
+ it("should allow partial update with correct error type", () => {
140
+ expectFunctionParametersType(cache.update).assert([userRequest, { error: { code: "ERR", message: "fail" } }]);
141
+ });
142
+
143
+ it("should allow updating only metadata fields", () => {
144
+ expectFunctionParametersType(cache.update).assert([userRequest, { success: true as const }]);
145
+ });
146
+ });
147
+ });
@@ -1,7 +1,8 @@
1
1
  import { getCacheData, getCacheByKey, getInvalidateByKey } from "cache";
2
- import { ResponseErrorType, ResponseSuccessType } from "adapter";
3
- import { ResponseDetailsType } from "managers";
4
- import { HttpAdapterType, xhrExtra } from "http-adapter";
2
+ import type { ResponseErrorType, ResponseSuccessType } from "adapter";
3
+ import type { ResponseDetailsType } from "managers";
4
+ import type { HttpAdapterType } from "http-adapter";
5
+ import { xhrExtra } from "http-adapter";
5
6
 
6
7
  describe("Cache [ Utils ]", () => {
7
8
  describe("when getCacheData function is used", () => {
@@ -25,6 +26,7 @@ describe("Cache [ Utils ]", () => {
25
26
  triggerTimestamp: +new Date(),
26
27
  isCanceled: false,
27
28
  isOffline: false,
29
+ willRetry: false,
28
30
  };
29
31
 
30
32
  expect(getCacheData(previousResponse, errorResponse)).toStrictEqual({
@@ -40,6 +42,7 @@ describe("Cache [ Utils ]", () => {
40
42
  triggerTimestamp: expect.toBeNumber(),
41
43
  isCanceled: false,
42
44
  isOffline: false,
45
+ willRetry: false,
43
46
  } satisfies ReturnType<typeof getCacheData>);
44
47
  });
45
48
 
@@ -57,6 +60,7 @@ describe("Cache [ Utils ]", () => {
57
60
  triggerTimestamp: +new Date(),
58
61
  isCanceled: false,
59
62
  isOffline: false,
63
+ willRetry: false,
60
64
  } as ResponseSuccessType<Record<string, string>, HttpAdapterType> & ResponseDetailsType;
61
65
  const newResponse = {
62
66
  data: { test: "2" },
@@ -71,6 +75,7 @@ describe("Cache [ Utils ]", () => {
71
75
  triggerTimestamp: +new Date(),
72
76
  isCanceled: false,
73
77
  isOffline: false,
78
+ willRetry: false,
74
79
  } as ResponseSuccessType<Record<string, string>, HttpAdapterType> & ResponseDetailsType;
75
80
  expect(getCacheData(previousResponse, newResponse)).toStrictEqual(newResponse);
76
81
  });
@@ -89,6 +94,7 @@ describe("Cache [ Utils ]", () => {
89
94
  triggerTimestamp: expect.toBeNumber(),
90
95
  isCanceled: false,
91
96
  isOffline: false,
97
+ willRetry: false,
92
98
  } as ResponseSuccessType<Record<string, string>, HttpAdapterType> & ResponseDetailsType;
93
99
  const errorResponse = {
94
100
  data: null,
@@ -103,6 +109,7 @@ describe("Cache [ Utils ]", () => {
103
109
  triggerTimestamp: expect.toBeNumber(),
104
110
  isCanceled: false,
105
111
  isOffline: false,
112
+ willRetry: false,
106
113
  } as ResponseErrorType<Record<string, string>, HttpAdapterType> & ResponseDetailsType;
107
114
  expect(getCacheData(undefined, newResponse)).toStrictEqual(newResponse);
108
115
  expect(getCacheData(undefined, errorResponse)).toStrictEqual(errorResponse);
@@ -29,7 +29,7 @@ describe("Client [ Auth ]", () => {
29
29
  authRequest = client.createRequest<{ response: any }>()({ endpoint: "/auth" }).setAuth(true);
30
30
  refreshRequest = client.createRequest<{ response: any }>()({ endpoint: refreshEndpoint });
31
31
  resetMocks();
32
- jest.resetAllMocks();
32
+ vi.resetAllMocks();
33
33
  });
34
34
 
35
35
  afterAll(() => {
@@ -40,7 +40,7 @@ describe("Client [ Auth ]", () => {
40
40
  it("should trigger auth middleware callback", async () => {
41
41
  mockRequest(authRequest, { data: requestFixture });
42
42
 
43
- const trigger = jest.fn();
43
+ const trigger = vi.fn();
44
44
 
45
45
  client.onAuth((cmd) => {
46
46
  trigger();
@@ -57,7 +57,7 @@ describe("Client [ Auth ]", () => {
57
57
  it("should trigger auth middleware callback", async () => {
58
58
  mockRequest(request, { data: requestFixture });
59
59
 
60
- const trigger = jest.fn();
60
+ const trigger = vi.fn();
61
61
 
62
62
  client.onAuth((cmd) => {
63
63
  trigger();
@@ -71,7 +71,7 @@ describe("Client [ Auth ]", () => {
71
71
  });
72
72
 
73
73
  describe("When token is out of date", () => {
74
- const interceptor = jest.fn();
74
+ const interceptor = vi.fn();
75
75
 
76
76
  const handleErrorIntercept = (callback?: () => void) => {
77
77
  client.onError(async (res, req) => {
@@ -113,7 +113,7 @@ describe("Client [ Auth ]", () => {
113
113
  it("should try to repeat request only once", async () => {
114
114
  mockRequest(request, { status: 401 });
115
115
 
116
- const retry = jest.fn();
116
+ const retry = vi.fn();
117
117
 
118
118
  handleErrorIntercept(retry);
119
119
 
@@ -65,9 +65,21 @@ describe("Client [ Base ]", () => {
65
65
  expect(client.submitDispatcher).toBe(submitDispatcher);
66
66
  });
67
67
  });
68
+ describe("When using key mapper setters", () => {
69
+ it("should set request id mapper", () => {
70
+ const client = new Client({ url: "shared-base-url" });
71
+ const customMapper = (request: any) => `custom-${request.endpoint}`;
72
+
73
+ const result = client.setRequestIdMapper(customMapper);
74
+
75
+ expect(client.unstable_requestIdMapper).toBe(customMapper);
76
+ expect(result).toBe(client);
77
+ });
78
+ });
79
+
68
80
  describe("When client is getting cleared", () => {
69
81
  it("should assign new appManager", async () => {
70
- const spy = jest.fn();
82
+ const spy = vi.fn();
71
83
  const appManager = new AppManager();
72
84
  const client = new Client({
73
85
  url: "shared-base-url",
@@ -81,7 +93,7 @@ describe("Client [ Base ]", () => {
81
93
  expect(spy).toHaveBeenCalledTimes(2);
82
94
  });
83
95
  it("should assign new cache", async () => {
84
- const spy = jest.fn();
96
+ const spy = vi.fn();
85
97
  const cache = new Cache();
86
98
  const client = new Client({
87
99
  url: "shared-base-url",
@@ -96,7 +108,7 @@ describe("Client [ Base ]", () => {
96
108
  expect(spy).toHaveBeenCalledTimes(2);
97
109
  });
98
110
  it("should assign new fetchDispatcher", async () => {
99
- const spy = jest.fn();
111
+ const spy = vi.fn();
100
112
  const fetchDispatcher = new Dispatcher();
101
113
  const client = new Client({
102
114
  url: "shared-base-url",
@@ -111,7 +123,7 @@ describe("Client [ Base ]", () => {
111
123
  expect(spy).toHaveBeenCalledTimes(2);
112
124
  });
113
125
  it("should assign new submitDispatcher", async () => {
114
- const spy = jest.fn();
126
+ const spy = vi.fn();
115
127
  const submitDispatcher = new Dispatcher();
116
128
  const client = new Client({
117
129
  url: "shared-base-url",
@@ -0,0 +1,182 @@
1
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
2
+
3
+ import { Client } from "client";
4
+ import { Request } from "request";
5
+
6
+ const { resetMocks, startServer, stopServer } = createHttpMockingServer();
7
+
8
+ describe("Client [ fromJSON ]", () => {
9
+ let client = new Client({ url: "shared-base-url" });
10
+
11
+ beforeAll(() => {
12
+ startServer();
13
+ });
14
+
15
+ beforeEach(() => {
16
+ client = new Client({ url: "shared-base-url" });
17
+ resetMocks();
18
+ });
19
+
20
+ afterAll(() => {
21
+ stopServer();
22
+ });
23
+
24
+ describe("When reconstructing a request from JSON", () => {
25
+ it("should return a Request class instance", () => {
26
+ const original = client.createRequest()({ endpoint: "/users", method: "GET" });
27
+ const json = original.toJSON();
28
+ const restored = client.fromJSON(json);
29
+
30
+ expect(restored).toBeInstanceOf(Request);
31
+ });
32
+
33
+ it("should preserve endpoint from the original request", () => {
34
+ const original = client.createRequest()({ endpoint: "/users/:id", method: "GET" });
35
+ const json = original.toJSON();
36
+ const restored = client.fromJSON(json);
37
+
38
+ expect(restored.endpoint).toBe("/users/:id");
39
+ });
40
+
41
+ it("should preserve method from the original request", () => {
42
+ const original = client.createRequest()({ endpoint: "/users", method: "POST" });
43
+ const json = original.toJSON();
44
+ const restored = client.fromJSON(json);
45
+
46
+ expect(restored.method).toBe("POST");
47
+ });
48
+
49
+ it("should preserve request options through serialization", () => {
50
+ const original = client.createRequest()({
51
+ endpoint: "/data",
52
+ method: "PUT",
53
+ cache: true,
54
+ retry: 3,
55
+ cacheTime: 5000,
56
+ });
57
+ const json = original.toJSON();
58
+ const restored = client.fromJSON(json);
59
+
60
+ expect(restored.cache).toBe(true);
61
+ expect(restored.retry).toBe(3);
62
+ expect(restored.cacheTime).toBe(5000);
63
+ });
64
+
65
+ it("should bind the restored request to the client", () => {
66
+ const original = client.createRequest()({ endpoint: "/test", method: "GET" });
67
+ const json = original.toJSON();
68
+ const restored = client.fromJSON(json);
69
+
70
+ expect(restored.client).toBe(client);
71
+ });
72
+ });
73
+
74
+ describe("When using type generics", () => {
75
+ it("should produce the same response type as createRequest", () => {
76
+ type Props = { response: { id: number; name: string } };
77
+
78
+ const created = client.createRequest<Props>()({ endpoint: "/users", method: "GET" });
79
+ const restored = client.fromJSON<Props>(created.toJSON());
80
+
81
+ type CreatedData = Awaited<ReturnType<typeof created.send>>["data"];
82
+ type RestoredData = Awaited<ReturnType<typeof restored.send>>["data"];
83
+
84
+ expectTypeOf<RestoredData>().toEqualTypeOf<CreatedData>();
85
+ });
86
+
87
+ it("should produce the same payload type as createRequest", () => {
88
+ type Props = { response: { id: number }; payload: { name: string; email: string } };
89
+
90
+ const created = client.createRequest<Props>()({ endpoint: "/users", method: "POST" });
91
+ const restored = client.fromJSON<Props>(created.toJSON());
92
+
93
+ type CreatedPayload = Parameters<typeof created.setPayload>[0];
94
+ type RestoredPayload = Parameters<typeof restored.setPayload>[0];
95
+
96
+ expectTypeOf<RestoredPayload>().toEqualTypeOf<CreatedPayload>();
97
+ });
98
+
99
+ it("should produce the same error type as createRequest", () => {
100
+ type Props = { error: { code: string; message: string } };
101
+
102
+ const created = client.createRequest<Props>()({ endpoint: "/fail", method: "GET" });
103
+ const restored = client.fromJSON<Props>(created.toJSON());
104
+
105
+ type CreatedError = Awaited<ReturnType<typeof created.send>>["error"];
106
+ type RestoredError = Awaited<ReturnType<typeof restored.send>>["error"];
107
+
108
+ expectTypeOf<RestoredError>().toEqualTypeOf<CreatedError>();
109
+ });
110
+
111
+ it("should produce the same defaults as createRequest when no generics are passed", () => {
112
+ const created = client.createRequest()({ endpoint: "/test", method: "GET" });
113
+ const restored = client.fromJSON(created.toJSON());
114
+
115
+ type CreatedData = Awaited<ReturnType<typeof created.send>>["data"];
116
+ type RestoredData = Awaited<ReturnType<typeof restored.send>>["data"];
117
+
118
+ type CreatedPayload = Parameters<typeof created.setPayload>[0];
119
+ type RestoredPayload = Parameters<typeof restored.setPayload>[0];
120
+
121
+ expectTypeOf<RestoredData>().toEqualTypeOf<CreatedData>();
122
+ expectTypeOf<RestoredPayload>().toEqualTypeOf<CreatedPayload>();
123
+ });
124
+
125
+ it("should produce the same endpoint params type as createRequest", () => {
126
+ type Props = { response: { id: number }; endpoint: "/users/:userId" };
127
+
128
+ const created = client.createRequest<Props>()({ endpoint: "/users/:userId", method: "GET" });
129
+ const restored = client.fromJSON<Props>(created.toJSON());
130
+
131
+ type CreatedParams = Parameters<typeof created.setParams>[0];
132
+ type RestoredParams = Parameters<typeof restored.setParams>[0];
133
+
134
+ expectTypeOf<RestoredParams>().toEqualTypeOf<CreatedParams>();
135
+ });
136
+
137
+ it("should produce the same query params type as createRequest", () => {
138
+ type Props = { queryParams: { search?: string; page?: number } };
139
+
140
+ const created = client.createRequest<Props>()({ endpoint: "/search", method: "GET" });
141
+ const restored = client.fromJSON<Props>(created.toJSON());
142
+
143
+ type CreatedQueryParams = Parameters<typeof created.setQueryParams>[0];
144
+ type RestoredQueryParams = Parameters<typeof restored.setQueryParams>[0];
145
+
146
+ expectTypeOf<RestoredQueryParams>().toEqualTypeOf<CreatedQueryParams>();
147
+ });
148
+
149
+ it("should produce matching types for all generics combined", () => {
150
+ type Props = {
151
+ response: { id: number };
152
+ payload: { name: string };
153
+ error: { msg: string };
154
+ queryParams: { limit: number };
155
+ endpoint: "/items/:itemId";
156
+ };
157
+
158
+ const created = client.createRequest<Props>()({ endpoint: "/items/:itemId", method: "POST" });
159
+ const restored = client.fromJSON<Props>(created.toJSON());
160
+
161
+ type CreatedData = Awaited<ReturnType<typeof created.send>>["data"];
162
+ type RestoredData = Awaited<ReturnType<typeof restored.send>>["data"];
163
+ expectTypeOf<RestoredData>().toEqualTypeOf<CreatedData>();
164
+
165
+ type CreatedError = Awaited<ReturnType<typeof created.send>>["error"];
166
+ type RestoredError = Awaited<ReturnType<typeof restored.send>>["error"];
167
+ expectTypeOf<RestoredError>().toEqualTypeOf<CreatedError>();
168
+
169
+ type CreatedPayload = Parameters<typeof created.setPayload>[0];
170
+ type RestoredPayload = Parameters<typeof restored.setPayload>[0];
171
+ expectTypeOf<RestoredPayload>().toEqualTypeOf<CreatedPayload>();
172
+
173
+ type CreatedParams = Parameters<typeof created.setParams>[0];
174
+ type RestoredParams = Parameters<typeof restored.setParams>[0];
175
+ expectTypeOf<RestoredParams>().toEqualTypeOf<CreatedParams>();
176
+
177
+ type CreatedQueryParams = Parameters<typeof created.setQueryParams>[0];
178
+ type RestoredQueryParams = Parameters<typeof restored.setQueryParams>[0];
179
+ expectTypeOf<RestoredQueryParams>().toEqualTypeOf<CreatedQueryParams>();
180
+ });
181
+ });
182
+ });
@@ -1,4 +1,7 @@
1
1
  import { Client } from "client";
2
+ import type { CacheValueType } from "cache";
3
+ import { xhrExtra } from "http-adapter";
4
+ import { scopeKey } from "request";
2
5
 
3
6
  describe("Client [ Hydration ]", () => {
4
7
  let client = new Client({ url: "shared-base-url" });
@@ -60,7 +63,7 @@ describe("Client [ Hydration ]", () => {
60
63
  expect(dehydratedResponse).toStrictEqual(expect.objectContaining({ cacheKey: request.cacheKey, response }));
61
64
 
62
65
  const newClient = new Client({ url: "shared-base-url" });
63
- const cacheData: CacheValueType = {
66
+ const cacheData = {
64
67
  data: "123456789",
65
68
  status: 200,
66
69
  error: null,
@@ -75,7 +78,7 @@ describe("Client [ Hydration ]", () => {
75
78
  isOffline: false,
76
79
  hydrated: false,
77
80
  cached: true,
78
- };
81
+ } as CacheValueType;
79
82
 
80
83
  newClient.cache.set(mockedRequest, cacheData);
81
84
 
@@ -85,6 +88,7 @@ describe("Client [ Hydration ]", () => {
85
88
  cacheTime: mockedRequest.cacheTime,
86
89
  staleTime: mockedRequest.staleTime,
87
90
  version: newClient.cache.version,
91
+ scope: null,
88
92
  });
89
93
 
90
94
  newClient.hydrate([dehydratedResponse]);
@@ -95,6 +99,7 @@ describe("Client [ Hydration ]", () => {
95
99
  cacheTime: mockedRequest.cacheTime,
96
100
  staleTime: mockedRequest.staleTime,
97
101
  version: newClient.cache.version,
102
+ scope: null,
98
103
  });
99
104
  });
100
105
  it("should handle null/undefined items in hydration data", async () => {
@@ -114,7 +119,7 @@ describe("Client [ Hydration ]", () => {
114
119
  const dehydratedResponse = mockedRequest.dehydrate();
115
120
 
116
121
  const newClient = new Client({ url: "shared-base-url" });
117
- const optionsFn = jest.fn().mockReturnValue({ cache: false });
122
+ const optionsFn = vi.fn().mockReturnValue({ cache: false });
118
123
 
119
124
  newClient.hydrate([dehydratedResponse], optionsFn);
120
125
 
@@ -162,10 +167,70 @@ describe("Client [ Hydration ]", () => {
162
167
 
163
168
  client.cache.clear();
164
169
 
165
- const spy = jest.spyOn(client.cache, "set");
170
+ const spy = vi.spyOn(client.cache, "set");
166
171
  client.hydrate([dehydratedResponse], { override: false });
167
172
 
168
173
  expect(spy).toHaveBeenCalled();
169
174
  });
175
+
176
+ it("should hydrate and read scoped cache via scopeKey", async () => {
177
+ const scoped = request.setScope("hydr-scope").setMock(() => ({ data: "scoped-val", status: 200 }));
178
+ const response = await scoped.send();
179
+ const dehydratedResponse = scoped.dehydrate();
180
+ expect(dehydratedResponse).toEqual(expect.objectContaining({ scope: "hydr-scope", cacheKey: scoped.cacheKey }));
181
+
182
+ const newClient = new Client({ url: "shared-base-url" });
183
+ const sk = scopeKey(scoped.cacheKey, "hydr-scope");
184
+ expect(newClient.cache.get(scoped.cacheKey)).not.toBeDefined();
185
+
186
+ newClient.hydrate([dehydratedResponse]);
187
+
188
+ expect(newClient.cache.get(sk)).toStrictEqual(
189
+ expect.objectContaining({ ...response, cacheKey: scoped.cacheKey, staleTime: scoped.staleTime }),
190
+ );
191
+ });
192
+
193
+ it("should skip hydrate when override is false and scoped slot already has data", async () => {
194
+ const scoped = request.setScope("no-over").setMock(() => ({ data: "a", status: 200 }));
195
+ await scoped.send();
196
+ const ts = Date.now();
197
+ const dehydratedResponse = scoped.dehydrate({
198
+ response: {
199
+ data: "b",
200
+ status: 200,
201
+ success: true,
202
+ error: null,
203
+ extra: xhrExtra,
204
+ requestTimestamp: ts,
205
+ responseTimestamp: ts,
206
+ },
207
+ });
208
+
209
+ const newClient = new Client({ url: "shared-base-url" });
210
+ const existingReq = newClient
211
+ .createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint" })
212
+ .setScope("no-over");
213
+ const sk = scopeKey(existingReq.cacheKey, "no-over");
214
+
215
+ newClient.cache.set(existingReq.setCache(true), {
216
+ data: "existing",
217
+ error: null,
218
+ status: 200,
219
+ success: true,
220
+ extra: xhrExtra,
221
+ retries: 0,
222
+ requestTimestamp: Date.now(),
223
+ responseTimestamp: Date.now(),
224
+ addedTimestamp: Date.now(),
225
+ triggerTimestamp: Date.now(),
226
+ isCanceled: false,
227
+ isOffline: false,
228
+ willRetry: false,
229
+ });
230
+
231
+ newClient.hydrate([dehydratedResponse], { override: false });
232
+
233
+ expect(newClient.cache.get(sk)?.data).toBe("existing");
234
+ });
170
235
  });
171
236
  });
@@ -11,9 +11,9 @@ describe("Client [ Interceptor ]", () => {
11
11
  let client = new Client({ url: "shared-base-url/" });
12
12
  let request = client.createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint" });
13
13
 
14
- const spy1 = jest.fn();
15
- const spy2 = jest.fn();
16
- const spy3 = jest.fn();
14
+ const spy1 = vi.fn();
15
+ const spy2 = vi.fn();
16
+ const spy3 = vi.fn();
17
17
 
18
18
  beforeAll(() => {
19
19
  startServer();
@@ -23,7 +23,7 @@ describe("Client [ Interceptor ]", () => {
23
23
  client = new Client({ url: "shared-base-url/" });
24
24
  request = client.createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint" });
25
25
  resetMocks();
26
- jest.clearAllMocks();
26
+ vi.clearAllMocks();
27
27
  });
28
28
 
29
29
  afterAll(() => {
@@ -2,10 +2,11 @@ import { createHttpMockingServer } from "@hyper-fetch/testing";
2
2
 
3
3
  import { Plugin } from "plugin";
4
4
  import { Client } from "client";
5
- import { RequestOptionsType, Request } from "request";
5
+ import type { RequestOptionsType } from "request";
6
+ import { Request } from "request";
6
7
  import { LoggerManager } from "managers";
7
8
  import { createAdapter, interceptorCallback, middlewareCallback } from "../../utils";
8
- import { HttpAdapterOptionsType, QueryStringifyOptionsType } from "http-adapter";
9
+ import type { FetchAdapterOptionsType, QueryStringifyOptionsType } from "http-adapter";
9
10
 
10
11
  const { resetMocks, startServer, stopServer } = createHttpMockingServer();
11
12
 
@@ -27,7 +28,7 @@ describe("Client [ Methods ]", () => {
27
28
 
28
29
  describe("When using config methods", () => {
29
30
  it("should assign default request config [setRequestDefaults]", async () => {
30
- const options: Partial<RequestOptionsType<string, HttpAdapterOptionsType>> = { method: "POST" };
31
+ const options: Partial<RequestOptionsType<string, FetchAdapterOptionsType>> = { method: "POST" };
31
32
  client.adapter.setRequestDefaults(() => options);
32
33
  const req = client.createRequest()({ endpoint: "test" });
33
34
 
@@ -35,7 +36,7 @@ describe("Client [ Methods ]", () => {
35
36
  expect(req.method).toBe("POST");
36
37
  });
37
38
  it("should assign default adapter config [setAdapterDefaultOptions]", async () => {
38
- const options: HttpAdapterOptionsType = { timeout: 12312312 };
39
+ const options: FetchAdapterOptionsType = { timeout: 12312312 };
39
40
  client.adapter.setAdapterDefaults(() => options);
40
41
 
41
42
  expect(client.adapter.unstable_getAdapterDefaults?.(request)).toEqual(options);
@@ -10,9 +10,9 @@ describe("Client [ Middleware ]", () => {
10
10
  let client = new Client({ url: "shared-base-url" });
11
11
  let request = client.createRequest()({ endpoint: "shared-base-endpoint" });
12
12
 
13
- const spy1 = jest.fn();
14
- const spy2 = jest.fn();
15
- const spy3 = jest.fn();
13
+ const spy1 = vi.fn();
14
+ const spy2 = vi.fn();
15
+ const spy3 = vi.fn();
16
16
 
17
17
  beforeAll(() => {
18
18
  startServer();
@@ -22,7 +22,7 @@ describe("Client [ Middleware ]", () => {
22
22
  client = new Client({ url: "shared-base-url" });
23
23
  request = client.createRequest()({ endpoint: "shared-base-endpoint" });
24
24
  resetMocks();
25
- jest.clearAllMocks();
25
+ vi.clearAllMocks();
26
26
  });
27
27
 
28
28
  afterAll(() => {