@hyper-fetch/core 7.5.2 → 7.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/__tests__/features/adapter/adapter.base.spec.ts +387 -0
  2. package/__tests__/features/adapter/adapter.bindings.spec.ts +1023 -0
  3. package/__tests__/features/adapter/adapter.browser.spec.ts +103 -0
  4. package/__tests__/features/adapter/adapter.server.spec.ts +177 -0
  5. package/__tests__/features/adapter/adapter.utils.spec.ts +165 -0
  6. package/__tests__/features/cache/cache.base.spec.ts +358 -0
  7. package/__tests__/features/cache/cache.events.spec.ts +215 -0
  8. package/__tests__/features/cache/cache.garbage-collector.spec.ts +172 -0
  9. package/__tests__/features/cache/cache.lazy-storage.spec.ts +129 -0
  10. package/__tests__/features/cache/cache.utils.spec.ts +119 -0
  11. package/__tests__/features/client/client.auth.spec.ts +125 -0
  12. package/__tests__/features/client/client.base.spec.ts +149 -0
  13. package/__tests__/features/client/client.hydration.spec.ts +171 -0
  14. package/__tests__/features/client/client.interceptor.spec.ts +225 -0
  15. package/__tests__/features/client/client.methods.spec.ts +188 -0
  16. package/__tests__/features/client/client.middleware.spec.ts +118 -0
  17. package/__tests__/features/client/client.utils.spec.ts +292 -0
  18. package/__tests__/features/dispatcher/dispatcher.base.spec.ts +123 -0
  19. package/__tests__/features/dispatcher/dispatcher.events.spec.ts +304 -0
  20. package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +462 -0
  21. package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +241 -0
  22. package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +125 -0
  23. package/__tests__/features/managers/app/app.manager.base.spec.ts +235 -0
  24. package/__tests__/features/managers/app/app.manager.ssr.spec.ts +40 -0
  25. package/__tests__/features/managers/logger/logger.base.spec.ts +107 -0
  26. package/__tests__/features/managers/logger/logger.utils.spec.ts +100 -0
  27. package/__tests__/features/managers/request/request.manager.base.spec.ts +222 -0
  28. package/__tests__/features/managers/request/request.manager.events.spec.ts +280 -0
  29. package/__tests__/features/mocker/mocker.base.spec.ts +567 -0
  30. package/__tests__/features/plugin/plugin.base.spec.ts +255 -0
  31. package/__tests__/features/request/request.base.spec.ts +199 -0
  32. package/__tests__/features/request/request.cloning.spec.ts +47 -0
  33. package/__tests__/features/request/request.sending.spec.ts +272 -0
  34. package/__tests__/features/request/request.setters.spec.ts +157 -0
  35. package/__tests__/features/request/request.typescript.spec.ts +423 -0
  36. package/__tests__/features/request/request.utils.spec.ts +77 -0
  37. package/__tests__/features/sdk/sdk.base.spec.ts +130 -0
  38. package/__tests__/global.d.ts +1 -0
  39. package/__tests__/jest.setup.ts +13 -0
  40. package/__tests__/shared/callbacks.shared.ts +9 -0
  41. package/__tests__/shared/index.ts +2 -0
  42. package/__tests__/shared/progress.shared.ts +35 -0
  43. package/__tests__/tsconfig.json +13 -0
  44. package/__tests__/utils/adapter.utils.ts +38 -0
  45. package/__tests__/utils/cache.utils.ts +19 -0
  46. package/__tests__/utils/callbacks.utils.ts +47 -0
  47. package/__tests__/utils/dispatcher.utils.ts +6 -0
  48. package/__tests__/utils/index.ts +4 -0
  49. package/dist/index.js +12 -1
  50. package/dist/index.js.map +2 -2
  51. package/jest.config.ts +6 -0
  52. package/package.json +1 -1
  53. package/project.json +4 -0
  54. package/src/http-adapter/http-adapter.server.ts +15 -1
@@ -0,0 +1,255 @@
1
+ import { waitFor } from "@testing-library/dom";
2
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
3
+
4
+ import { Client } from "client";
5
+ import { Plugin } from "plugin";
6
+
7
+ const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
8
+
9
+ describe("Plugin [ Base ]", () => {
10
+ let client = new Client({ url: "shared-base-url" });
11
+ let request = client.createRequest()({ endpoint: "shared-nase-endpoint" });
12
+
13
+ beforeAll(() => {
14
+ startServer();
15
+ });
16
+
17
+ beforeEach(() => {
18
+ resetMocks();
19
+ jest.resetAllMocks();
20
+ client = new Client({ url: "shared-base-url" });
21
+ request = client.createRequest()({ endpoint: "shared-nase-endpoint" });
22
+ });
23
+
24
+ afterAll(() => {
25
+ stopServer();
26
+ });
27
+
28
+ describe("When initializing plugin", () => {
29
+ it("should properly set initial values", () => {
30
+ const pluginData = { test: true };
31
+ const plugin = new Plugin({
32
+ name: "test-plugin",
33
+ data: pluginData,
34
+ });
35
+
36
+ expect(plugin.name).toBe("test-plugin");
37
+ expect(plugin.data).toEqual(pluginData);
38
+ expect(plugin.client).toBeUndefined();
39
+ });
40
+
41
+ it("should initialize with client", () => {
42
+ const plugin = new Plugin({ name: "test-plugin" });
43
+ const initializedPlugin = plugin.initialize(client);
44
+
45
+ expect(initializedPlugin.client).toBe(client);
46
+ expect(initializedPlugin).toBe(plugin);
47
+ });
48
+ });
49
+
50
+ describe("When using plugin trigger", () => {
51
+ it("should trigger registered method", () => {
52
+ const callback = jest.fn();
53
+ const plugin = new Plugin({ name: "test-plugin" });
54
+
55
+ plugin.onDispatcherCleared(callback);
56
+ plugin.trigger("onDispatcherCleared", {
57
+ dispatcher: client.fetchDispatcher,
58
+ });
59
+
60
+ expect(callback).toHaveBeenCalledWith({ dispatcher: client.fetchDispatcher });
61
+ });
62
+
63
+ it("should not throw when triggering unregistered method", () => {
64
+ const plugin = new Plugin({ name: "test-plugin" });
65
+
66
+ expect(() => {
67
+ plugin.trigger("onDispatcherCleared", {} as any);
68
+ }).not.toThrow();
69
+ });
70
+ });
71
+
72
+ describe("When using dispatcher effects", () => {
73
+ it("should register dispatcher lifecycle methods", () => {
74
+ const plugin = new Plugin({ name: "test-plugin" });
75
+ const callback = jest.fn();
76
+
77
+ const methods = [
78
+ "onDispatcherCleared",
79
+ "onDispatcherQueueDrained",
80
+ "onDispatcherQueueRunning",
81
+ "onDispatcherItemAdded",
82
+ "onDispatcherItemDeleted",
83
+ "onDispatcherQueueCreated",
84
+ "onDispatcherQueueCleared",
85
+ ];
86
+
87
+ methods.forEach((method) => {
88
+ const result = (plugin as any)[method](callback);
89
+ expect(result).toBe(plugin);
90
+ plugin.trigger(method as any, {});
91
+ expect(callback).toHaveBeenCalledTimes(1);
92
+ callback.mockClear();
93
+ });
94
+ });
95
+ });
96
+
97
+ describe("When using cache effects", () => {
98
+ it("should register cache lifecycle methods", () => {
99
+ const plugin = new Plugin({ name: "test-plugin" });
100
+ const callback = jest.fn();
101
+
102
+ const methods = ["onCacheItemChange", "onCacheItemDelete"];
103
+
104
+ methods.forEach((method) => {
105
+ const result = (plugin as any)[method](callback as any);
106
+ expect(result).toBe(plugin);
107
+ plugin.trigger(method as any, {});
108
+ expect(callback).toHaveBeenCalledTimes(1);
109
+ callback.mockClear();
110
+ });
111
+ });
112
+ });
113
+
114
+ describe("When using adapter effects", () => {
115
+ it("should register adapter fetch method", () => {
116
+ const plugin = new Plugin({ name: "test-plugin" });
117
+ const callback = jest.fn();
118
+
119
+ const result = plugin.onAdapterFetch(callback);
120
+ expect(result).toBe(plugin);
121
+ plugin.trigger("onAdapterFetch", {
122
+ request: request as any,
123
+ requestId: "123",
124
+ adapter: client.adapter,
125
+ });
126
+ expect(callback).toHaveBeenCalledTimes(1);
127
+ });
128
+ });
129
+
130
+ describe("When using fetch effects", () => {
131
+ it("should trigger success effects", async () => {
132
+ mockRequest(request);
133
+ const spy1 = jest.fn();
134
+ const spy2 = jest.fn();
135
+ const spy3 = jest.fn();
136
+ const spy4 = jest.fn();
137
+ const spy5 = jest.fn();
138
+
139
+ const effect = new Plugin({
140
+ name: "123",
141
+ })
142
+ .onRequestError(spy1)
143
+ .onRequestSuccess(spy2)
144
+ .onRequestFinished(spy3)
145
+ .onRequestStart(spy4)
146
+ .onRequestTrigger(spy5);
147
+
148
+ client.addPlugin(effect);
149
+
150
+ request.send({});
151
+
152
+ await waitFor(() => {
153
+ expect(spy1).toHaveBeenCalledTimes(0);
154
+ expect(spy2).toHaveBeenCalledTimes(1);
155
+ expect(spy3).toHaveBeenCalledTimes(1);
156
+ expect(spy4).toHaveBeenCalledTimes(1);
157
+ expect(spy5).toHaveBeenCalledTimes(1);
158
+ });
159
+ });
160
+ it("should trigger error effects", async () => {
161
+ mockRequest(request, { status: 400 });
162
+ const spy1 = jest.fn();
163
+ const spy2 = jest.fn();
164
+ const spy3 = jest.fn();
165
+ const spy4 = jest.fn();
166
+ const spy5 = jest.fn();
167
+
168
+ const effect = new Plugin({
169
+ name: "123",
170
+ })
171
+ .onRequestError(spy1)
172
+ .onRequestSuccess(spy2)
173
+ .onRequestFinished(spy3)
174
+ .onRequestStart(spy4)
175
+ .onRequestTrigger(spy5);
176
+
177
+ client.addPlugin(effect);
178
+
179
+ request.send({});
180
+
181
+ await waitFor(() => {
182
+ expect(spy1).toHaveBeenCalledTimes(1);
183
+ expect(spy2).toHaveBeenCalledTimes(0);
184
+ expect(spy3).toHaveBeenCalledTimes(1);
185
+ expect(spy4).toHaveBeenCalledTimes(1);
186
+ expect(spy5).toHaveBeenCalledTimes(1);
187
+ });
188
+ });
189
+ it("should not throw when effects are empty", async () => {
190
+ const effect = new Plugin({
191
+ name: "123",
192
+ });
193
+
194
+ expect(effect.onRequestTrigger).not.toThrow();
195
+ expect(effect.onRequestStart).not.toThrow();
196
+ expect(effect.onRequestSuccess).not.toThrow();
197
+ expect(effect.onRequestError).not.toThrow();
198
+ expect(effect.onRequestFinished).not.toThrow();
199
+ });
200
+ });
201
+
202
+ describe("When using mount/unmount lifecycle", () => {
203
+ it("should register mount and unmount callbacks", () => {
204
+ const plugin = new Plugin({ name: "test-plugin" });
205
+ const mountCallback = jest.fn();
206
+ const unmountCallback = jest.fn();
207
+
208
+ const mountResult = plugin.onMount(mountCallback);
209
+ const unmountResult = plugin.onUnmount(unmountCallback);
210
+
211
+ expect(mountResult).toBe(plugin);
212
+ expect(unmountResult).toBe(plugin);
213
+
214
+ plugin.trigger("onMount", { client });
215
+ plugin.trigger("onUnmount", { client });
216
+
217
+ expect(mountCallback).toHaveBeenCalledWith({ client });
218
+ expect(unmountCallback).toHaveBeenCalledWith({ client });
219
+ });
220
+ });
221
+
222
+ describe("When using request lifecycle", () => {
223
+ it("should register request create callback", () => {
224
+ const plugin = new Plugin({ name: "test-plugin" });
225
+ const callback = jest.fn();
226
+
227
+ const result = plugin.onRequestCreate(callback);
228
+ expect(result).toBe(plugin);
229
+
230
+ plugin.trigger("onRequestCreate", {
231
+ request,
232
+ });
233
+
234
+ expect(callback).toHaveBeenCalledWith({
235
+ request,
236
+ });
237
+ });
238
+
239
+ it("should register request trigger callback", () => {
240
+ const plugin = new Plugin({ name: "test-plugin" });
241
+ const callback = jest.fn();
242
+
243
+ const result = plugin.onRequestTrigger(callback);
244
+ expect(result).toBe(plugin);
245
+
246
+ plugin.trigger("onRequestTrigger", {
247
+ request,
248
+ });
249
+
250
+ expect(callback).toHaveBeenCalledWith({
251
+ request,
252
+ });
253
+ });
254
+ });
255
+ });
@@ -0,0 +1,199 @@
1
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
2
+
3
+ import { Client, QueryParamsType } from "../../../src";
4
+
5
+ const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
6
+
7
+ describe("Fetch Adapter [ Base ]", () => {
8
+ let client = new Client({ url: "shared-base-url" });
9
+ let request = client.createRequest()({ endpoint: "/shared-endpoint" });
10
+ beforeAll(() => {
11
+ startServer();
12
+ });
13
+
14
+ beforeEach(() => {
15
+ client = new Client({ url: "shared-base-url" });
16
+ request = client.createRequest()({ endpoint: "/shared-endpoint" });
17
+ resetMocks();
18
+ jest.resetAllMocks();
19
+ });
20
+
21
+ afterAll(() => {
22
+ stopServer();
23
+ });
24
+
25
+ it("should allow for setting custom header", async () => {
26
+ // The queue should receive request with the appropriate header
27
+ const spy = jest.fn(client.fetchDispatcher.add);
28
+ jest.spyOn(client.fetchDispatcher, "add").mockImplementation(spy);
29
+ const header = { it: "works" };
30
+ const c = request.setHeaders(header);
31
+ mockRequest(request);
32
+
33
+ await c.send({});
34
+
35
+ expect(c.headers).toEqual(header);
36
+ expect(spy).toHaveBeenCalled();
37
+ expect(spy.mock.calls[0][0].headers).toEqual(header);
38
+ });
39
+
40
+ it("should allow for setting auth", async () => {
41
+ // The queue should receive request with the appropriate header
42
+ const spy = jest.fn(client.fetchDispatcher.add);
43
+ jest.spyOn(client.fetchDispatcher, "add").mockImplementation(spy);
44
+ mockRequest(request);
45
+
46
+ expect(request.auth).toBe(true);
47
+
48
+ const c = request.setAuth(false);
49
+
50
+ await c.send({});
51
+
52
+ expect(c.auth).toBe(false);
53
+ expect(spy).toHaveBeenCalled();
54
+ expect(spy.mock.calls[0][0].auth).toBe(false);
55
+ });
56
+
57
+ it("should allow for setting custom params", async () => {
58
+ // The queue should receive request with the appropriate header
59
+ const comm = client.createRequest<{ queryParams: QueryParamsType }>()({
60
+ endpoint: "/some-endpoint/:shopId/:productId",
61
+ });
62
+ const spy = jest.fn(client.fetchDispatcher.add);
63
+ jest.spyOn(client.fetchDispatcher, "add").mockImplementation(spy);
64
+ const params = { shopId: 11, productId: 1 };
65
+
66
+ const c = comm.setParams(params);
67
+ mockRequest(c);
68
+
69
+ await c.send({
70
+ queryParams: {},
71
+ });
72
+
73
+ expect(c.params).toEqual(params);
74
+ expect(spy).toHaveBeenCalled();
75
+ expect(spy.mock.calls[0][0].params).toEqual(params);
76
+ });
77
+
78
+ it("should allow for setting data to the request", async () => {
79
+ const spy = jest.fn(client.fetchDispatcher.add);
80
+ jest.spyOn(client.fetchDispatcher, "add").mockImplementation(spy);
81
+ const data = {
82
+ userId: 11,
83
+ role: "ADMIN",
84
+ };
85
+ const req = client.createRequest<{ response: void; payload: { userId: number; role: string } }>()({
86
+ endpoint: "/shared-endpoint",
87
+ });
88
+
89
+ const c = req.setPayload(data);
90
+ mockRequest(c);
91
+
92
+ await c.send({});
93
+
94
+ expect(c.payload).toStrictEqual(data);
95
+ expect(spy).toHaveBeenCalled();
96
+ expect(spy.mock.calls[0][0].payload).toEqual(data);
97
+ });
98
+
99
+ it("Should allow for setting queryParams", async () => {
100
+ const queryParams = { userId: 11 };
101
+ const comm = client.createRequest<{ queryParams: QueryParamsType }>()({
102
+ endpoint: "/some-endpoint/",
103
+ });
104
+ mockRequest(comm);
105
+ const spy = jest.fn(client.fetchDispatcher.add);
106
+ jest.spyOn(client.fetchDispatcher, "add").mockImplementation(spy);
107
+
108
+ const requestQueryParams = comm.setQueryParams(queryParams);
109
+ requestQueryParams.send({});
110
+
111
+ expect(requestQueryParams.queryParams).toStrictEqual(queryParams);
112
+ expect(spy).toHaveBeenCalled();
113
+ expect(spy.mock.calls[0][0].queryParams).toStrictEqual(queryParams);
114
+ });
115
+
116
+ it("Should allow to validate before sending request", async () => {
117
+ const message = "something went wrong";
118
+ const mapper = () => {
119
+ throw new Error(message);
120
+ };
121
+ const mapperRequest = client
122
+ .createRequest<{ response: null }>()({ endpoint: "/some-endpoint/" })
123
+ .setRequestMapper(mapper);
124
+ mockRequest(mapperRequest);
125
+
126
+ const { data, error } = await mapperRequest.send({});
127
+
128
+ expect(error?.message).toStrictEqual(message);
129
+ expect(data).toBe(null);
130
+ });
131
+
132
+ it("Should allow to validate response", async () => {
133
+ const message = "something went wrong";
134
+
135
+ const mapperRequest = client
136
+ .createRequest<{ response: null }>()({ endpoint: "/some-endpoint/" })
137
+ .setResponseMapper((res) => {
138
+ return { ...res, data: null, error: new Error(message) };
139
+ });
140
+ mockRequest(mapperRequest);
141
+
142
+ const { data, error } = await mapperRequest.send({});
143
+
144
+ expect(error?.message).toStrictEqual(message);
145
+ expect(data).toBe(null);
146
+ });
147
+
148
+ it("Should allow to validate in data mapper", async () => {
149
+ const message = "something went wrong";
150
+ const mapper = () => {
151
+ throw new Error(message);
152
+ };
153
+ const mapperRequest = client
154
+ .createRequest<{ response: null }>()({ endpoint: "/some-endpoint/" })
155
+ .setPayloadMapper(mapper);
156
+ mockRequest(mapperRequest);
157
+
158
+ const { data, error } = await mapperRequest.send({});
159
+
160
+ expect(error?.message).toStrictEqual(message);
161
+ expect(data).toBe(null);
162
+ });
163
+
164
+ describe("read()", () => {
165
+ it("should return cached data when available", async () => {
166
+ const cachedData = {
167
+ data: { foo: "bar" },
168
+ status: 200,
169
+ };
170
+
171
+ await request
172
+ .setMock(() => ({
173
+ data: { foo: "bar" } as any,
174
+ status: 200,
175
+ }))
176
+ .send();
177
+
178
+ const result = request.read();
179
+
180
+ expect(result).toEqual({
181
+ ...cachedData,
182
+ error: null,
183
+ extra: request.client.adapter.defaultExtra,
184
+ success: true,
185
+ requestTimestamp: expect.any(Number),
186
+ responseTimestamp: expect.any(Number),
187
+ });
188
+ });
189
+
190
+ it("should return undefined when no cached data exists", () => {
191
+ // Ensure cache is empty
192
+ client.cache.clear();
193
+
194
+ const result = request.read();
195
+
196
+ expect(result).toBeUndefined();
197
+ });
198
+ });
199
+ });
@@ -0,0 +1,47 @@
1
+ import { createHttpMockingServer } from "@hyper-fetch/testing";
2
+
3
+ import { Client } from "client";
4
+
5
+ const { resetMocks, startServer, stopServer } = createHttpMockingServer();
6
+
7
+ describe("Request [ Cloning ]", () => {
8
+ let client = new Client({ url: "shared-base-url" });
9
+ let request = client.createRequest()({ endpoint: "/users/:userId" });
10
+ beforeAll(() => {
11
+ startServer();
12
+ });
13
+
14
+ beforeEach(() => {
15
+ client = new Client({ url: "shared-base-url" });
16
+ request = client.createRequest()({ endpoint: "/users/:userId" });
17
+ resetMocks();
18
+ jest.resetAllMocks();
19
+ });
20
+
21
+ afterAll(() => {
22
+ stopServer();
23
+ });
24
+
25
+ describe("When cloning request", () => {
26
+ it("should generate new keys", async () => {
27
+ const clone = request.setParams({ userId: 1 }).clone();
28
+ expect(clone.abortKey).toBe(request.abortKey);
29
+ expect(clone.queryKey).not.toBe(request.queryKey);
30
+ expect(clone.cacheKey).not.toBe(request.cacheKey);
31
+ });
32
+ it("should not generate new keys when we set custom ones", async () => {
33
+ const clone = request.setAbortKey("test").setCacheKey("test").setQueryKey("test").clone().clone();
34
+
35
+ expect(clone.abortKey).toBe("test");
36
+ expect(clone.queryKey).toBe("test");
37
+ expect(clone.cacheKey).toBe("test");
38
+ });
39
+ it("should not generate new keys when we set custom ones", async () => {
40
+ const clone = request.setAbortKey("test").setCacheKey("test").setQueryKey("test").clone().clone();
41
+
42
+ expect(clone.abortKey).toBe("test");
43
+ expect(clone.queryKey).toBe("test");
44
+ expect(clone.cacheKey).toBe("test");
45
+ });
46
+ });
47
+ });