@hyper-fetch/core 7.5.2 → 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 (271) 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 +414 -0
  7. package/__tests__/features/adapter/adapter.bindings.spec.ts +1025 -0
  8. package/__tests__/features/adapter/adapter.browser.spec.ts +88 -0
  9. package/__tests__/features/adapter/adapter.server.spec.ts +369 -0
  10. package/__tests__/features/adapter/adapter.utils.spec.ts +165 -0
  11. package/__tests__/features/cache/cache.base.spec.ts +366 -0
  12. package/__tests__/features/cache/cache.events.spec.ts +219 -0
  13. package/__tests__/features/cache/cache.garbage-collector.spec.ts +175 -0
  14. package/__tests__/features/cache/cache.lazy-storage.spec.ts +132 -0
  15. package/__tests__/features/cache/cache.modes.spec.ts +145 -0
  16. package/__tests__/features/cache/cache.scoping.spec.ts +171 -0
  17. package/__tests__/features/cache/cache.typescript.spec.ts +147 -0
  18. package/__tests__/features/cache/cache.utils.spec.ts +126 -0
  19. package/__tests__/features/client/client.auth.spec.ts +125 -0
  20. package/__tests__/features/client/client.base.spec.ts +161 -0
  21. package/__tests__/features/client/client.from-json.spec.ts +182 -0
  22. package/__tests__/features/client/client.hydration.spec.ts +236 -0
  23. package/__tests__/features/client/client.interceptor.spec.ts +225 -0
  24. package/__tests__/features/client/client.methods.spec.ts +189 -0
  25. package/__tests__/features/client/client.middleware.spec.ts +118 -0
  26. package/__tests__/features/client/client.utils.spec.ts +292 -0
  27. package/__tests__/features/dispatcher/dispatcher.base.spec.ts +123 -0
  28. package/__tests__/features/dispatcher/dispatcher.custom-storage.spec.ts +189 -0
  29. package/__tests__/features/dispatcher/dispatcher.events.spec.ts +309 -0
  30. package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +462 -0
  31. package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +241 -0
  32. package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +142 -0
  33. package/__tests__/features/managers/app/app.manager.base.spec.ts +235 -0
  34. package/__tests__/features/managers/app/app.manager.ssr.spec.ts +43 -0
  35. package/__tests__/features/managers/logger/logger.base.spec.ts +109 -0
  36. package/__tests__/features/managers/logger/logger.utils.spec.ts +106 -0
  37. package/__tests__/features/managers/request/request.manager.base.spec.ts +224 -0
  38. package/__tests__/features/managers/request/request.manager.events.spec.ts +364 -0
  39. package/__tests__/features/mocker/mocker.base.spec.ts +639 -0
  40. package/__tests__/features/plugin/plugin.base.spec.ts +255 -0
  41. package/__tests__/features/request/request.base.spec.ts +225 -0
  42. package/__tests__/features/request/request.cloning.spec.ts +47 -0
  43. package/__tests__/features/request/request.hooks.spec.ts +173 -0
  44. package/__tests__/features/request/request.optimistic.spec.ts +93 -0
  45. package/__tests__/features/request/request.retry-on-error.spec.ts +359 -0
  46. package/__tests__/features/request/request.sending.spec.ts +364 -0
  47. package/__tests__/features/request/request.setters.spec.ts +164 -0
  48. package/__tests__/features/request/request.typescript.spec.ts +423 -0
  49. package/__tests__/features/request/request.utils.spec.ts +77 -0
  50. package/__tests__/features/sdk/sdk.base.spec.ts +132 -0
  51. package/__tests__/features/sdk/sdk.configuration.spec.ts +262 -0
  52. package/__tests__/features/utils/emitter.utils.spec.ts +88 -0
  53. package/__tests__/shared/callbacks.shared.ts +11 -0
  54. package/__tests__/shared/index.ts +2 -0
  55. package/__tests__/shared/progress.shared.ts +36 -0
  56. package/__tests__/tsconfig.json +24 -0
  57. package/__tests__/type-stress/generate-stress-test.ts +92 -0
  58. package/__tests__/type-stress/stress-10.ts +35 -0
  59. package/__tests__/type-stress/stress-just-create.ts +2107 -0
  60. package/__tests__/type-stress/stress-minimal.ts +23 -0
  61. package/__tests__/type-stress/stress-test-500.ts +2319 -0
  62. package/__tests__/type-stress/stress-test-sdk.ts +210 -0
  63. package/__tests__/type-stress/tsconfig.json +6 -0
  64. package/__tests__/utils/adapter.utils.ts +39 -0
  65. package/__tests__/utils/cache.utils.ts +20 -0
  66. package/__tests__/utils/callbacks.utils.ts +48 -0
  67. package/__tests__/utils/dispatcher.utils.ts +7 -0
  68. package/__tests__/utils/index.ts +4 -0
  69. package/__tests__/vitest.setup.ts +15 -0
  70. package/dist/adapter/adapter.bindings.d.ts +71 -0
  71. package/dist/adapter/adapter.bindings.d.ts.map +1 -0
  72. package/dist/adapter/adapter.d.ts +127 -0
  73. package/dist/adapter/adapter.d.ts.map +1 -0
  74. package/dist/adapter/adapter.types.d.ts +108 -0
  75. package/dist/adapter/adapter.types.d.ts.map +1 -0
  76. package/dist/adapter/adapter.utils.d.ts +23 -0
  77. package/dist/adapter/adapter.utils.d.ts.map +1 -0
  78. package/dist/adapter/index.d.ts +5 -0
  79. package/dist/adapter/index.d.ts.map +1 -0
  80. package/dist/cache/cache.d.ts +95 -0
  81. package/dist/cache/cache.d.ts.map +1 -0
  82. package/dist/cache/cache.events.d.ts +51 -0
  83. package/dist/cache/cache.events.d.ts.map +1 -0
  84. package/dist/cache/cache.types.d.ts +44 -0
  85. package/dist/cache/cache.types.d.ts.map +1 -0
  86. package/dist/cache/cache.utils.d.ts +11 -0
  87. package/dist/cache/cache.utils.d.ts.map +1 -0
  88. package/dist/cache/index.d.ts +5 -0
  89. package/dist/cache/index.d.ts.map +1 -0
  90. package/dist/client/client.constants.d.ts +10 -0
  91. package/dist/client/client.constants.d.ts.map +1 -0
  92. package/dist/client/client.create.d.ts +8 -0
  93. package/dist/client/client.create.d.ts.map +1 -0
  94. package/dist/client/client.d.ts +195 -0
  95. package/dist/client/client.d.ts.map +1 -0
  96. package/dist/client/client.types.d.ts +71 -0
  97. package/dist/client/client.types.d.ts.map +1 -0
  98. package/dist/client/client.utils.d.ts +11 -0
  99. package/dist/client/client.utils.d.ts.map +1 -0
  100. package/dist/client/index.d.ts +6 -0
  101. package/dist/client/index.d.ts.map +1 -0
  102. package/dist/constants/http.constants.d.ts +8 -0
  103. package/dist/constants/http.constants.d.ts.map +1 -0
  104. package/dist/constants/time.constants.d.ts +12 -0
  105. package/dist/constants/time.constants.d.ts.map +1 -0
  106. package/dist/dispatcher/dispatcher.constants.d.ts +7 -0
  107. package/dist/dispatcher/dispatcher.constants.d.ts.map +1 -0
  108. package/dist/dispatcher/dispatcher.d.ts +168 -0
  109. package/dist/dispatcher/dispatcher.d.ts.map +1 -0
  110. package/dist/dispatcher/dispatcher.events.d.ts +49 -0
  111. package/dist/dispatcher/dispatcher.events.d.ts.map +1 -0
  112. package/dist/dispatcher/dispatcher.types.d.ts +44 -0
  113. package/dist/dispatcher/dispatcher.types.d.ts.map +1 -0
  114. package/dist/dispatcher/dispatcher.utils.d.ts +12 -0
  115. package/dist/dispatcher/dispatcher.utils.d.ts.map +1 -0
  116. package/dist/dispatcher/index.d.ts +6 -0
  117. package/dist/dispatcher/index.d.ts.map +1 -0
  118. package/dist/http-adapter/http-adapter.constants.d.ts +4 -0
  119. package/dist/http-adapter/http-adapter.constants.d.ts.map +1 -0
  120. package/dist/http-adapter/http-adapter.d.ts +3 -0
  121. package/dist/http-adapter/http-adapter.d.ts.map +1 -0
  122. package/dist/http-adapter/http-adapter.fetch.d.ts +6 -0
  123. package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -0
  124. package/dist/http-adapter/http-adapter.types.d.ts +73 -0
  125. package/dist/http-adapter/http-adapter.types.d.ts.map +1 -0
  126. package/dist/http-adapter/http-adapter.utils.d.ts +10 -0
  127. package/dist/http-adapter/http-adapter.utils.d.ts.map +1 -0
  128. package/dist/http-adapter/index.d.ts +6 -0
  129. package/dist/http-adapter/index.d.ts.map +1 -0
  130. package/dist/index.d.ts +15 -2083
  131. package/dist/index.d.ts.map +1 -0
  132. package/dist/index.mjs +4084 -0
  133. package/dist/index.mjs.map +1 -0
  134. package/dist/managers/app/app.manager.constants.d.ts +10 -0
  135. package/dist/managers/app/app.manager.constants.d.ts.map +1 -0
  136. package/dist/managers/app/app.manager.d.ts +35 -0
  137. package/dist/managers/app/app.manager.d.ts.map +1 -0
  138. package/dist/managers/app/app.manager.events.d.ts +12 -0
  139. package/dist/managers/app/app.manager.events.d.ts.map +1 -0
  140. package/dist/managers/app/app.manager.types.d.ts +7 -0
  141. package/dist/managers/app/app.manager.types.d.ts.map +1 -0
  142. package/dist/managers/app/app.manager.utils.d.ts +5 -0
  143. package/dist/managers/app/app.manager.utils.d.ts.map +1 -0
  144. package/dist/managers/app/index.d.ts +6 -0
  145. package/dist/managers/app/index.d.ts.map +1 -0
  146. package/dist/managers/index.d.ts +4 -0
  147. package/dist/managers/index.d.ts.map +1 -0
  148. package/dist/managers/logger/index.d.ts +5 -0
  149. package/dist/managers/logger/index.d.ts.map +1 -0
  150. package/dist/managers/logger/logger.manager.constants.d.ts +5 -0
  151. package/dist/managers/logger/logger.manager.constants.d.ts.map +1 -0
  152. package/dist/managers/logger/logger.manager.d.ts +22 -0
  153. package/dist/managers/logger/logger.manager.d.ts.map +1 -0
  154. package/dist/managers/logger/logger.manager.types.d.ts +43 -0
  155. package/dist/managers/logger/logger.manager.types.d.ts.map +1 -0
  156. package/dist/managers/logger/logger.manager.utils.d.ts +4 -0
  157. package/dist/managers/logger/logger.manager.utils.d.ts.map +1 -0
  158. package/dist/managers/request/index.d.ts +5 -0
  159. package/dist/managers/request/index.d.ts.map +1 -0
  160. package/dist/managers/request/request.manager.d.ts +60 -0
  161. package/dist/managers/request/request.manager.d.ts.map +1 -0
  162. package/dist/managers/request/request.manager.events.d.ts +55 -0
  163. package/dist/managers/request/request.manager.events.d.ts.map +1 -0
  164. package/dist/managers/request/request.manager.types.d.ts +48 -0
  165. package/dist/managers/request/request.manager.types.d.ts.map +1 -0
  166. package/dist/managers/request/request.manager.utils.d.ts +30 -0
  167. package/dist/managers/request/request.manager.utils.d.ts.map +1 -0
  168. package/dist/mocker/index.d.ts +3 -0
  169. package/dist/mocker/index.d.ts.map +1 -0
  170. package/dist/mocker/mocker.d.ts +3 -0
  171. package/dist/mocker/mocker.d.ts.map +1 -0
  172. package/dist/mocker/mocker.types.d.ts +25 -0
  173. package/dist/mocker/mocker.types.d.ts.map +1 -0
  174. package/dist/plugin/index.d.ts +3 -0
  175. package/dist/plugin/index.d.ts.map +1 -0
  176. package/dist/plugin/plugin.d.ts +55 -0
  177. package/dist/plugin/plugin.d.ts.map +1 -0
  178. package/dist/plugin/plugin.types.d.ts +122 -0
  179. package/dist/plugin/plugin.types.d.ts.map +1 -0
  180. package/dist/request/index.d.ts +5 -0
  181. package/dist/request/index.d.ts.map +1 -0
  182. package/dist/request/request.d.ts +195 -0
  183. package/dist/request/request.d.ts.map +1 -0
  184. package/dist/request/request.hooks.d.ts +19 -0
  185. package/dist/request/request.hooks.d.ts.map +1 -0
  186. package/dist/request/request.types.d.ts +240 -0
  187. package/dist/request/request.types.d.ts.map +1 -0
  188. package/dist/request/request.utils.d.ts +25 -0
  189. package/dist/request/request.utils.d.ts.map +1 -0
  190. package/dist/sdk/index.d.ts +2 -0
  191. package/dist/sdk/index.d.ts.map +1 -0
  192. package/dist/sdk/sdk.d.ts +69 -0
  193. package/dist/sdk/sdk.d.ts.map +1 -0
  194. package/dist/types/adapter.types.d.ts +22 -0
  195. package/dist/types/adapter.types.d.ts.map +1 -0
  196. package/dist/types/client.types.d.ts +4 -0
  197. package/dist/types/client.types.d.ts.map +1 -0
  198. package/dist/types/helpers.types.d.ts +17 -0
  199. package/dist/types/helpers.types.d.ts.map +1 -0
  200. package/dist/types/http.types.d.ts +3 -0
  201. package/dist/types/http.types.d.ts.map +1 -0
  202. package/dist/types/hydration.types.d.ts +15 -0
  203. package/dist/types/hydration.types.d.ts.map +1 -0
  204. package/dist/types/index.d.ts +7 -0
  205. package/dist/types/index.d.ts.map +1 -0
  206. package/dist/types/request.types.d.ts +34 -0
  207. package/dist/types/request.types.d.ts.map +1 -0
  208. package/dist/utils/emitter.utils.d.ts +13 -0
  209. package/dist/utils/emitter.utils.d.ts.map +1 -0
  210. package/dist/utils/index.d.ts +3 -0
  211. package/dist/utils/index.d.ts.map +1 -0
  212. package/dist/utils/uuid.utils.d.ts +3 -0
  213. package/dist/utils/uuid.utils.d.ts.map +1 -0
  214. package/package.json +10 -25
  215. package/project.json +4 -0
  216. package/src/adapter/adapter.bindings.ts +8 -12
  217. package/src/adapter/adapter.ts +8 -7
  218. package/src/adapter/adapter.types.ts +10 -6
  219. package/src/adapter/adapter.utils.ts +1 -1
  220. package/src/cache/cache.events.ts +4 -11
  221. package/src/cache/cache.ts +28 -22
  222. package/src/cache/cache.types.ts +8 -4
  223. package/src/cache/cache.utils.ts +3 -3
  224. package/src/client/client.create.ts +4 -3
  225. package/src/client/client.ts +44 -12
  226. package/src/client/client.types.ts +25 -3
  227. package/src/client/client.utils.ts +24 -4
  228. package/src/dispatcher/dispatcher.events.ts +3 -3
  229. package/src/dispatcher/dispatcher.ts +59 -28
  230. package/src/dispatcher/dispatcher.types.ts +19 -2
  231. package/src/dispatcher/dispatcher.utils.ts +5 -4
  232. package/src/http-adapter/http-adapter.constants.ts +1 -1
  233. package/src/http-adapter/http-adapter.fetch.ts +174 -0
  234. package/src/http-adapter/http-adapter.ts +3 -8
  235. package/src/http-adapter/http-adapter.types.ts +17 -9
  236. package/src/http-adapter/http-adapter.utils.ts +6 -9
  237. package/src/http-adapter/index.ts +1 -0
  238. package/src/managers/app/app.manager.constants.ts +2 -2
  239. package/src/managers/app/app.manager.events.ts +1 -1
  240. package/src/managers/app/app.manager.ts +2 -1
  241. package/src/managers/logger/logger.manager.constants.ts +1 -1
  242. package/src/managers/logger/logger.manager.ts +2 -1
  243. package/src/managers/logger/logger.manager.types.ts +2 -2
  244. package/src/managers/logger/logger.manager.utils.ts +2 -1
  245. package/src/managers/request/request.manager.events.ts +13 -11
  246. package/src/managers/request/request.manager.types.ts +6 -3
  247. package/src/mocker/mocker.ts +3 -2
  248. package/src/mocker/mocker.types.ts +1 -1
  249. package/src/plugin/plugin.ts +2 -2
  250. package/src/plugin/plugin.types.ts +9 -9
  251. package/src/request/index.ts +1 -0
  252. package/src/request/request.hooks.ts +87 -0
  253. package/src/request/request.ts +155 -77
  254. package/src/request/request.types.ts +54 -22
  255. package/src/request/request.utils.ts +86 -23
  256. package/src/sdk/sdk.ts +153 -8
  257. package/src/types/adapter.types.ts +1 -1
  258. package/src/types/client.types.ts +1 -1
  259. package/src/types/hydration.types.ts +3 -3
  260. package/src/types/request.types.ts +30 -20
  261. package/src/utils/uuid.utils.ts +1 -1
  262. package/tsconfig.base.json +1 -6
  263. package/tsconfig.json +1 -4
  264. package/vite.config.ts +45 -0
  265. package/.eslintrc.json +0 -11
  266. package/dist/browser/index.js +0 -3751
  267. package/dist/browser/index.js.map +0 -7
  268. package/dist/index.js +0 -3991
  269. package/dist/index.js.map +0 -7
  270. package/src/http-adapter/http-adapter.browser.ts +0 -102
  271. package/src/http-adapter/http-adapter.server.ts +0 -135
@@ -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
+ });
@@ -0,0 +1,126 @@
1
+ import { getCacheData, getCacheByKey, getInvalidateByKey } from "cache";
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";
6
+
7
+ describe("Cache [ Utils ]", () => {
8
+ describe("when getCacheData function is used", () => {
9
+ it("should not override cache on retry or refresh error response", async () => {
10
+ const previousResponse = {
11
+ data: {},
12
+ error: null,
13
+ status: 200,
14
+ extra: xhrExtra,
15
+ } as ResponseSuccessType<Record<string, string>, HttpAdapterType>;
16
+ const errorResponse: ResponseErrorType<Record<string, string>, HttpAdapterType> & ResponseDetailsType = {
17
+ data: null,
18
+ error: {},
19
+ status: 400,
20
+ success: false,
21
+ extra: xhrExtra,
22
+ retries: 0,
23
+ requestTimestamp: +new Date(),
24
+ responseTimestamp: +new Date(),
25
+ addedTimestamp: +new Date(),
26
+ triggerTimestamp: +new Date(),
27
+ isCanceled: false,
28
+ isOffline: false,
29
+ willRetry: false,
30
+ };
31
+
32
+ expect(getCacheData(previousResponse, errorResponse)).toStrictEqual({
33
+ data: previousResponse.data,
34
+ error: errorResponse.error,
35
+ status: errorResponse.status,
36
+ success: errorResponse.success,
37
+ extra: errorResponse.extra,
38
+ retries: 0,
39
+ requestTimestamp: expect.toBeNumber(),
40
+ responseTimestamp: expect.toBeNumber(),
41
+ addedTimestamp: expect.toBeNumber(),
42
+ triggerTimestamp: expect.toBeNumber(),
43
+ isCanceled: false,
44
+ isOffline: false,
45
+ willRetry: false,
46
+ } satisfies ReturnType<typeof getCacheData>);
47
+ });
48
+
49
+ it("should use successful response over previous data", async () => {
50
+ const previousResponse = {
51
+ data: { test: "1" },
52
+ error: null,
53
+ status: 200,
54
+ success: true,
55
+ extra: xhrExtra,
56
+ retries: 0,
57
+ requestTimestamp: +new Date(),
58
+ responseTimestamp: +new Date(),
59
+ addedTimestamp: +new Date(),
60
+ triggerTimestamp: +new Date(),
61
+ isCanceled: false,
62
+ isOffline: false,
63
+ willRetry: false,
64
+ } as ResponseSuccessType<Record<string, string>, HttpAdapterType> & ResponseDetailsType;
65
+ const newResponse = {
66
+ data: { test: "2" },
67
+ error: null,
68
+ status: 200,
69
+ success: true,
70
+ extra: xhrExtra,
71
+ retries: 0,
72
+ requestTimestamp: +new Date(),
73
+ responseTimestamp: +new Date(),
74
+ addedTimestamp: +new Date(),
75
+ triggerTimestamp: +new Date(),
76
+ isCanceled: false,
77
+ isOffline: false,
78
+ willRetry: false,
79
+ } as ResponseSuccessType<Record<string, string>, HttpAdapterType> & ResponseDetailsType;
80
+ expect(getCacheData(previousResponse, newResponse)).toStrictEqual(newResponse);
81
+ });
82
+
83
+ it("should use any response if there is no cached data", async () => {
84
+ const newResponse = {
85
+ data: {},
86
+ error: null,
87
+ status: 200,
88
+ success: true,
89
+ extra: xhrExtra,
90
+ retries: 0,
91
+ requestTimestamp: expect.toBeNumber(),
92
+ responseTimestamp: expect.toBeNumber(),
93
+ addedTimestamp: expect.toBeNumber(),
94
+ triggerTimestamp: expect.toBeNumber(),
95
+ isCanceled: false,
96
+ isOffline: false,
97
+ willRetry: false,
98
+ } as ResponseSuccessType<Record<string, string>, HttpAdapterType> & ResponseDetailsType;
99
+ const errorResponse = {
100
+ data: null,
101
+ error: {},
102
+ status: 400,
103
+ success: false,
104
+ extra: xhrExtra,
105
+ retries: 0,
106
+ requestTimestamp: expect.toBeNumber(),
107
+ responseTimestamp: expect.toBeNumber(),
108
+ addedTimestamp: expect.toBeNumber(),
109
+ triggerTimestamp: expect.toBeNumber(),
110
+ isCanceled: false,
111
+ isOffline: false,
112
+ willRetry: false,
113
+ } as ResponseErrorType<Record<string, string>, HttpAdapterType> & ResponseDetailsType;
114
+ expect(getCacheData(undefined, newResponse)).toStrictEqual(newResponse);
115
+ expect(getCacheData(undefined, errorResponse)).toStrictEqual(errorResponse);
116
+ });
117
+ });
118
+ describe("when key getters are triggered", () => {
119
+ it("should get proper key from getInvalidateByKey", async () => {
120
+ expect(getInvalidateByKey("1")).toBe("1_invalidate");
121
+ });
122
+ it("should get proper key from getCacheByKey", async () => {
123
+ expect(getCacheByKey("1")).toBe("1_cache");
124
+ });
125
+ });
126
+ });
@@ -0,0 +1,125 @@
1
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
2
+
3
+ import { Client } from "client";
4
+
5
+ const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
6
+
7
+ describe("Client [ Auth ]", () => {
8
+ const requestFixture = { data: "" };
9
+
10
+ const refreshFixture = { token: "new-token" };
11
+ const refreshEndpoint = "/refresh-token";
12
+
13
+ let client = new Client({ url: "shared-base-url" });
14
+ let request = client.createRequest<{ response: any }>()({ endpoint: "/shared-endpoint" }).setAuth(false);
15
+ let authRequest = client.createRequest<{ response: any }>()({ endpoint: "/auth" }).setAuth(true);
16
+ let refreshRequest = client.createRequest<{ response: any }>()({ endpoint: refreshEndpoint });
17
+
18
+ beforeEach(() => {
19
+ mockRequest(refreshRequest, { data: refreshFixture });
20
+ });
21
+
22
+ beforeAll(() => {
23
+ startServer();
24
+ });
25
+
26
+ afterEach(() => {
27
+ client = new Client({ url: "shared-base-url" });
28
+ request = client.createRequest<{ response: any }>()({ endpoint: "/shared-endpoint" }).setAuth(false);
29
+ authRequest = client.createRequest<{ response: any }>()({ endpoint: "/auth" }).setAuth(true);
30
+ refreshRequest = client.createRequest<{ response: any }>()({ endpoint: refreshEndpoint });
31
+ resetMocks();
32
+ vi.resetAllMocks();
33
+ });
34
+
35
+ afterAll(() => {
36
+ stopServer();
37
+ });
38
+
39
+ describe("When authenticated request get send", () => {
40
+ it("should trigger auth middleware callback", async () => {
41
+ mockRequest(authRequest, { data: requestFixture });
42
+
43
+ const trigger = vi.fn();
44
+
45
+ client.onAuth((cmd) => {
46
+ trigger();
47
+ return cmd;
48
+ });
49
+
50
+ await authRequest.send({});
51
+
52
+ expect(trigger).toHaveBeenCalledTimes(1);
53
+ });
54
+ });
55
+
56
+ describe("When non-authenticated request get send", () => {
57
+ it("should trigger auth middleware callback", async () => {
58
+ mockRequest(request, { data: requestFixture });
59
+
60
+ const trigger = vi.fn();
61
+
62
+ client.onAuth((cmd) => {
63
+ trigger();
64
+ return cmd;
65
+ });
66
+
67
+ await request.send({});
68
+
69
+ expect(trigger).toHaveBeenCalledTimes(0);
70
+ });
71
+ });
72
+
73
+ describe("When token is out of date", () => {
74
+ const interceptor = vi.fn();
75
+
76
+ const handleErrorIntercept = (callback?: () => void) => {
77
+ client.onError(async (res, req) => {
78
+ interceptor();
79
+ const { status } = res;
80
+
81
+ if (!req.used && status === 401) {
82
+ const { data } = await refreshRequest.send({});
83
+ if (data) {
84
+ callback?.();
85
+ return req.setUsed(true).send({});
86
+ }
87
+ }
88
+ return res;
89
+ });
90
+ };
91
+
92
+ it("should intercept error response and get the data with new token", async () => {
93
+ mockRequest(request, { status: 401 });
94
+
95
+ handleErrorIntercept(() => mockRequest(request, { data: requestFixture }));
96
+
97
+ const response = await request.send({});
98
+ expect(response.data).toEqual(requestFixture);
99
+ expect(interceptor).toHaveBeenCalledTimes(1);
100
+ });
101
+ it("should intercept error response and return error after another failure", async () => {
102
+ const errorResponse = mockRequest(request, { status: 401 });
103
+
104
+ handleErrorIntercept();
105
+ const { data, error, status } = await request.send({});
106
+
107
+ expect(status).toBe(401);
108
+ expect(data).toBe(null);
109
+ expect(error).toEqual(errorResponse);
110
+ expect(interceptor).toHaveBeenCalledTimes(2);
111
+ });
112
+
113
+ it("should try to repeat request only once", async () => {
114
+ mockRequest(request, { status: 401 });
115
+
116
+ const retry = vi.fn();
117
+
118
+ handleErrorIntercept(retry);
119
+
120
+ await request.send({});
121
+
122
+ expect(retry).toHaveBeenCalledTimes(1);
123
+ });
124
+ });
125
+ });
@@ -0,0 +1,161 @@
1
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
2
+
3
+ import { Cache } from "cache";
4
+ import { Dispatcher } from "dispatcher";
5
+ import { AppManager } from "managers";
6
+ import { Client } from "client";
7
+ import { Adapter } from "adapter";
8
+
9
+ const { resetMocks, startServer, stopServer } = createHttpMockingServer();
10
+
11
+ describe("Client [ Base ]", () => {
12
+ beforeAll(() => {
13
+ startServer();
14
+ });
15
+
16
+ beforeEach(() => {
17
+ resetMocks();
18
+ });
19
+
20
+ afterAll(() => {
21
+ stopServer();
22
+ });
23
+
24
+ describe("When client properties are passed", () => {
25
+ it("should assign url", async () => {
26
+ const url = "test-123";
27
+ const client = new Client({ url });
28
+
29
+ expect(client.url).toBe(url);
30
+ });
31
+ it("should assign new adapter", async () => {
32
+ const adapter = new Adapter({
33
+ name: "test",
34
+ defaultMethod: "GET",
35
+ defaultExtra: {},
36
+ systemErrorStatus: 0,
37
+ systemErrorExtra: {},
38
+ });
39
+ const client = new Client({ url: "shared-base-url" }).setAdapter(adapter);
40
+
41
+ expect(client.adapter).toBe(adapter);
42
+ });
43
+ it("should assign new appManager", async () => {
44
+ const appManager = new AppManager();
45
+ const client = new Client({ url: "shared-base-url", appManager: () => appManager });
46
+
47
+ expect(client.appManager).toBe(appManager);
48
+ });
49
+ it("should assign new cache", async () => {
50
+ const cache = new Cache();
51
+ const client = new Client({ url: "shared-base-url", cache: () => cache });
52
+
53
+ expect(client.cache).toBe(cache);
54
+ });
55
+ it("should assign new fetchDispatcher", async () => {
56
+ const fetchDispatcher = new Dispatcher();
57
+ const client = new Client({ url: "shared-base-url", fetchDispatcher: () => fetchDispatcher });
58
+
59
+ expect(client.fetchDispatcher).toBe(fetchDispatcher);
60
+ });
61
+ it("should assign new submitDispatcher", async () => {
62
+ const submitDispatcher = new Dispatcher();
63
+ const client = new Client({ url: "shared-base-url", submitDispatcher: () => submitDispatcher });
64
+
65
+ expect(client.submitDispatcher).toBe(submitDispatcher);
66
+ });
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
+
80
+ describe("When client is getting cleared", () => {
81
+ it("should assign new appManager", async () => {
82
+ const spy = vi.fn();
83
+ const appManager = new AppManager();
84
+ const client = new Client({
85
+ url: "shared-base-url",
86
+ appManager: () => {
87
+ spy();
88
+ return appManager;
89
+ },
90
+ });
91
+ expect(spy).toHaveBeenCalledTimes(1);
92
+ client.clear();
93
+ expect(spy).toHaveBeenCalledTimes(2);
94
+ });
95
+ it("should assign new cache", async () => {
96
+ const spy = vi.fn();
97
+ const cache = new Cache();
98
+ const client = new Client({
99
+ url: "shared-base-url",
100
+ cache: () => {
101
+ spy();
102
+ return cache;
103
+ },
104
+ });
105
+
106
+ expect(spy).toHaveBeenCalledTimes(1);
107
+ client.clear();
108
+ expect(spy).toHaveBeenCalledTimes(2);
109
+ });
110
+ it("should assign new fetchDispatcher", async () => {
111
+ const spy = vi.fn();
112
+ const fetchDispatcher = new Dispatcher();
113
+ const client = new Client({
114
+ url: "shared-base-url",
115
+ fetchDispatcher: () => {
116
+ spy();
117
+ return fetchDispatcher;
118
+ },
119
+ });
120
+
121
+ expect(spy).toHaveBeenCalledTimes(1);
122
+ client.clear();
123
+ expect(spy).toHaveBeenCalledTimes(2);
124
+ });
125
+ it("should assign new submitDispatcher", async () => {
126
+ const spy = vi.fn();
127
+ const submitDispatcher = new Dispatcher();
128
+ const client = new Client({
129
+ url: "shared-base-url",
130
+ submitDispatcher: () => {
131
+ spy();
132
+ return submitDispatcher;
133
+ },
134
+ });
135
+
136
+ expect(spy).toHaveBeenCalledTimes(1);
137
+ client.clear();
138
+ expect(spy).toHaveBeenCalledTimes(2);
139
+ });
140
+ it("should assign new defaultExtra", async () => {
141
+ const defaultExtra = { headers: { test: "1" } };
142
+ const client = new Client({
143
+ url: "shared-base-url",
144
+ });
145
+
146
+ client.adapter.setDefaultExtra(defaultExtra);
147
+
148
+ expect(client.adapter.defaultExtra).toStrictEqual(defaultExtra);
149
+ });
150
+ it("should assign new defaultMethod", async () => {
151
+ const defaultMethod = "POST";
152
+ const client = new Client({
153
+ url: "shared-base-url",
154
+ });
155
+
156
+ client.adapter.setDefaultMethod(defaultMethod);
157
+
158
+ expect(client.adapter.defaultMethod).toStrictEqual(defaultMethod);
159
+ });
160
+ });
161
+ });