@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.
- package/__tests__/features/adapter/adapter.base.spec.ts +387 -0
- package/__tests__/features/adapter/adapter.bindings.spec.ts +1023 -0
- package/__tests__/features/adapter/adapter.browser.spec.ts +103 -0
- package/__tests__/features/adapter/adapter.server.spec.ts +177 -0
- package/__tests__/features/adapter/adapter.utils.spec.ts +165 -0
- package/__tests__/features/cache/cache.base.spec.ts +358 -0
- package/__tests__/features/cache/cache.events.spec.ts +215 -0
- package/__tests__/features/cache/cache.garbage-collector.spec.ts +172 -0
- package/__tests__/features/cache/cache.lazy-storage.spec.ts +129 -0
- package/__tests__/features/cache/cache.utils.spec.ts +119 -0
- package/__tests__/features/client/client.auth.spec.ts +125 -0
- package/__tests__/features/client/client.base.spec.ts +149 -0
- package/__tests__/features/client/client.hydration.spec.ts +171 -0
- package/__tests__/features/client/client.interceptor.spec.ts +225 -0
- package/__tests__/features/client/client.methods.spec.ts +188 -0
- package/__tests__/features/client/client.middleware.spec.ts +118 -0
- package/__tests__/features/client/client.utils.spec.ts +292 -0
- package/__tests__/features/dispatcher/dispatcher.base.spec.ts +123 -0
- package/__tests__/features/dispatcher/dispatcher.events.spec.ts +304 -0
- package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +462 -0
- package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +241 -0
- package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +125 -0
- package/__tests__/features/managers/app/app.manager.base.spec.ts +235 -0
- package/__tests__/features/managers/app/app.manager.ssr.spec.ts +40 -0
- package/__tests__/features/managers/logger/logger.base.spec.ts +107 -0
- package/__tests__/features/managers/logger/logger.utils.spec.ts +100 -0
- package/__tests__/features/managers/request/request.manager.base.spec.ts +222 -0
- package/__tests__/features/managers/request/request.manager.events.spec.ts +280 -0
- package/__tests__/features/mocker/mocker.base.spec.ts +567 -0
- package/__tests__/features/plugin/plugin.base.spec.ts +255 -0
- package/__tests__/features/request/request.base.spec.ts +199 -0
- package/__tests__/features/request/request.cloning.spec.ts +47 -0
- package/__tests__/features/request/request.sending.spec.ts +272 -0
- package/__tests__/features/request/request.setters.spec.ts +157 -0
- package/__tests__/features/request/request.typescript.spec.ts +423 -0
- package/__tests__/features/request/request.utils.spec.ts +77 -0
- package/__tests__/features/sdk/sdk.base.spec.ts +130 -0
- package/__tests__/global.d.ts +1 -0
- package/__tests__/jest.setup.ts +13 -0
- package/__tests__/shared/callbacks.shared.ts +9 -0
- package/__tests__/shared/index.ts +2 -0
- package/__tests__/shared/progress.shared.ts +35 -0
- package/__tests__/tsconfig.json +13 -0
- package/__tests__/utils/adapter.utils.ts +38 -0
- package/__tests__/utils/cache.utils.ts +19 -0
- package/__tests__/utils/callbacks.utils.ts +47 -0
- package/__tests__/utils/dispatcher.utils.ts +6 -0
- package/__tests__/utils/index.ts +4 -0
- package/dist/index.js +12 -1
- package/dist/index.js.map +2 -2
- package/jest.config.ts +6 -0
- package/package.json +1 -1
- package/project.json +4 -0
- package/src/http-adapter/http-adapter.server.ts +15 -1
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
import { Client, createClient } from "client";
|
|
2
|
+
import { Adapter, AdapterInstance, RequestProcessingError, getAdapterHeaders, getAdapterPayload } from "adapter";
|
|
3
|
+
import { HttpMethods } from "../../../src/constants/http.constants";
|
|
4
|
+
import { RequestInstance } from "request";
|
|
5
|
+
|
|
6
|
+
describe("Adapter [ Base ]", () => {
|
|
7
|
+
let adapter: AdapterInstance;
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
adapter = new Adapter({
|
|
11
|
+
name: "test-adapter",
|
|
12
|
+
defaultMethod: HttpMethods.GET,
|
|
13
|
+
defaultExtra: {},
|
|
14
|
+
systemErrorStatus: 0,
|
|
15
|
+
systemErrorExtra: {},
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe("when setting mapper configs", () => {
|
|
20
|
+
it("should set query params mapper config", () => {
|
|
21
|
+
const config = { test: true };
|
|
22
|
+
adapter.setQueryParamsMapperConfig(config);
|
|
23
|
+
expect(adapter.unstable_queryParamsMapperConfig).toBe(config);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should set header mapper config", () => {
|
|
27
|
+
const config = { test: true };
|
|
28
|
+
adapter.setHeaderMapperConfig(config);
|
|
29
|
+
expect(adapter.unstable_headerMapperConfig).toBe(config);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should set endpoint mapper config", () => {
|
|
33
|
+
const config = { test: true };
|
|
34
|
+
adapter.setEndpointMapperConfig(config);
|
|
35
|
+
expect(adapter.unstable_endpointMapperConfig).toBe(config);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should set payload mapper config", () => {
|
|
39
|
+
const config = { test: true };
|
|
40
|
+
adapter.setPayloadMapperConfig(config);
|
|
41
|
+
expect(adapter.unstable_payloadMapperConfig).toBe(config);
|
|
42
|
+
});
|
|
43
|
+
it("should set devtools endpoint getter", () => {
|
|
44
|
+
const mapper = (req: RequestInstance) => req.endpoint;
|
|
45
|
+
adapter.setDevtoolsEndpointGetter(mapper);
|
|
46
|
+
expect(adapter.unstable_devtoolsEndpointGetter).toBe(mapper);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe("when using onInitialize", () => {
|
|
51
|
+
it("should store and execute initialization callback", () => {
|
|
52
|
+
const spy = jest.fn();
|
|
53
|
+
const client = new Client({ url: "test" });
|
|
54
|
+
|
|
55
|
+
adapter.onInitialize(spy);
|
|
56
|
+
adapter.initialize(client);
|
|
57
|
+
|
|
58
|
+
expect(spy).toHaveBeenCalledWith({ client });
|
|
59
|
+
expect(adapter.unstable_onInitializeCallback).toBe(spy);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("should handle initialization without callback", () => {
|
|
63
|
+
const client = new Client({ url: "test" });
|
|
64
|
+
|
|
65
|
+
// Initialize without setting onInitialize callback
|
|
66
|
+
expect(() => adapter.initialize(client)).not.toThrow();
|
|
67
|
+
expect(adapter.unstable_onInitializeCallback).toBeUndefined();
|
|
68
|
+
expect(adapter.initialized).toBe(true);
|
|
69
|
+
expect(adapter.client).toBe(client);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("should handle initialization with callback set to undefined", () => {
|
|
73
|
+
const client = new Client({ url: "test" });
|
|
74
|
+
|
|
75
|
+
// Initialize without setting onInitialize callback
|
|
76
|
+
adapter.unstable_onInitializeCallback = undefined;
|
|
77
|
+
expect(() => adapter.initialize(client)).not.toThrow();
|
|
78
|
+
expect(adapter.unstable_onInitializeCallback).toBeUndefined();
|
|
79
|
+
expect(adapter.initialized).toBe(true);
|
|
80
|
+
expect(adapter.client).toBe(client);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe("when using setFetcher", () => {
|
|
85
|
+
it("should bind fetcher function to adapter instance", () => {
|
|
86
|
+
const mock = { test: 123 } as any;
|
|
87
|
+
const fetcher = () => {
|
|
88
|
+
return mock;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
adapter.setFetcher(fetcher);
|
|
92
|
+
const result = adapter.unstable_fetcher({} as any);
|
|
93
|
+
|
|
94
|
+
expect(result).toBe(mock);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe("when using mapper setters", () => {
|
|
99
|
+
it("should set header mapper with config", () => {
|
|
100
|
+
const headerMapper = jest.fn();
|
|
101
|
+
const config = { test: true };
|
|
102
|
+
|
|
103
|
+
adapter.setHeaderMapperConfig(config);
|
|
104
|
+
adapter.setHeaderMapper(headerMapper);
|
|
105
|
+
adapter.unstable_headerMapper({} as any);
|
|
106
|
+
|
|
107
|
+
expect(headerMapper).toHaveBeenCalledWith({}, config);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("should set payload mapper with config", () => {
|
|
111
|
+
const payloadMapper = jest.fn();
|
|
112
|
+
const config = { test: true };
|
|
113
|
+
|
|
114
|
+
adapter.setPayloadMapperConfig(config);
|
|
115
|
+
adapter.setPayloadMapper(payloadMapper);
|
|
116
|
+
adapter.unstable_payloadMapper({} as any);
|
|
117
|
+
|
|
118
|
+
expect(payloadMapper).toHaveBeenCalledWith({}, config);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("should set endpoint mapper with config", () => {
|
|
122
|
+
const endpointMapper = jest.fn();
|
|
123
|
+
const config = { test: true };
|
|
124
|
+
|
|
125
|
+
adapter.setEndpointMapperConfig(config);
|
|
126
|
+
adapter.setEndpointMapper(endpointMapper);
|
|
127
|
+
adapter.unstable_endpointMapper("test");
|
|
128
|
+
|
|
129
|
+
expect(endpointMapper).toHaveBeenCalledWith("test", config);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("should set query params mapper with config", () => {
|
|
133
|
+
const queryParamsMapper = jest.fn();
|
|
134
|
+
const config = { test: true };
|
|
135
|
+
|
|
136
|
+
adapter.setQueryParamsMapperConfig(config);
|
|
137
|
+
adapter.setQueryParamsMapper(queryParamsMapper);
|
|
138
|
+
adapter.unstable_queryParamsMapper({} as any);
|
|
139
|
+
|
|
140
|
+
expect(queryParamsMapper).toHaveBeenCalledWith({}, config);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
describe("when using defaults setters", () => {
|
|
145
|
+
it("should set request defaults", () => {
|
|
146
|
+
const defaults = { method: HttpMethods.POST };
|
|
147
|
+
const callback = jest.fn().mockReturnValue(defaults);
|
|
148
|
+
|
|
149
|
+
adapter.setRequestDefaults(callback);
|
|
150
|
+
const result = adapter.unstable_getRequestDefaults?.({} as any);
|
|
151
|
+
|
|
152
|
+
expect(callback).toHaveBeenCalledWith({});
|
|
153
|
+
expect(result).toBe(defaults);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("should set adapter defaults", () => {
|
|
157
|
+
const defaults = { timeout: 5000 };
|
|
158
|
+
const callback = jest.fn().mockReturnValue(defaults);
|
|
159
|
+
|
|
160
|
+
adapter.setAdapterDefaults(callback);
|
|
161
|
+
const result = adapter.unstable_getAdapterDefaults?.({} as any);
|
|
162
|
+
|
|
163
|
+
expect(callback).toHaveBeenCalledWith({});
|
|
164
|
+
expect(result).toBe(defaults);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("should set internal error mapping callback", () => {
|
|
168
|
+
const errorMapping = (error: any) => ({ customError: error });
|
|
169
|
+
const callback = jest.fn().mockImplementation(errorMapping);
|
|
170
|
+
|
|
171
|
+
adapter.setInternalErrorMapping(callback);
|
|
172
|
+
|
|
173
|
+
expect(adapter.unstable_internalErrorMapping).toBe(callback);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
describe("when checking default mappers", () => {
|
|
178
|
+
it("should have default header mapper", () => {
|
|
179
|
+
expect(adapter.unstable_headerMapper).toBe(getAdapterHeaders);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("should have default payload mapper", () => {
|
|
183
|
+
expect(adapter.unstable_payloadMapper).toBe(getAdapterPayload);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
describe("when using fetch", () => {
|
|
188
|
+
it("should throw error if adapter is not initialized", async () => {
|
|
189
|
+
const newAdapter = new Adapter({
|
|
190
|
+
name: "test-adapter",
|
|
191
|
+
defaultMethod: HttpMethods.GET,
|
|
192
|
+
defaultExtra: {},
|
|
193
|
+
systemErrorStatus: 0,
|
|
194
|
+
systemErrorExtra: {},
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const client = createClient({ url: "test" });
|
|
198
|
+
const request = client.createRequest()({
|
|
199
|
+
endpoint: "test",
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// hack to prevent logger from being missed (not initialized)
|
|
203
|
+
newAdapter.logger = client.loggerManager.initialize(client, "Adapter");
|
|
204
|
+
|
|
205
|
+
const data = await newAdapter.fetch(request, "test");
|
|
206
|
+
|
|
207
|
+
expect(data.error.message).toStrictEqual(
|
|
208
|
+
new RequestProcessingError("Adapter test-adapter is not initialized").message,
|
|
209
|
+
);
|
|
210
|
+
expect(data.error.description).toStrictEqual(
|
|
211
|
+
new RequestProcessingError("Adapter test-adapter is not initialized").description,
|
|
212
|
+
);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("should throw error if fetcher is not set", async () => {
|
|
216
|
+
const newAdapter = new Adapter({
|
|
217
|
+
name: "test-adapter",
|
|
218
|
+
defaultMethod: HttpMethods.GET,
|
|
219
|
+
defaultExtra: {},
|
|
220
|
+
systemErrorStatus: 0,
|
|
221
|
+
systemErrorExtra: {},
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const client = createClient({ url: "test" }).setAdapter(newAdapter);
|
|
225
|
+
const request = client.createRequest()({
|
|
226
|
+
endpoint: "test",
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
const data = await client.adapter.fetch(request, "test");
|
|
230
|
+
|
|
231
|
+
expect(data.error.message).toStrictEqual(
|
|
232
|
+
new RequestProcessingError("Fetcher for test-adapter adapter is not set").message,
|
|
233
|
+
);
|
|
234
|
+
expect(data.error.description).toStrictEqual(
|
|
235
|
+
new RequestProcessingError("Fetcher for test-adapter adapter is not set").description,
|
|
236
|
+
);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("should execute fetcher with adapter bindings", async () => {
|
|
240
|
+
const fetcherSpy = jest.fn().mockImplementation(({ onSuccess }) => {
|
|
241
|
+
onSuccess({
|
|
242
|
+
data: {},
|
|
243
|
+
status: 200,
|
|
244
|
+
extra: {},
|
|
245
|
+
error: null,
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
const newAdapter = new Adapter({
|
|
250
|
+
name: "test-adapter",
|
|
251
|
+
defaultMethod: HttpMethods.GET,
|
|
252
|
+
defaultExtra: {},
|
|
253
|
+
systemErrorStatus: 0,
|
|
254
|
+
systemErrorExtra: {},
|
|
255
|
+
}).setFetcher(fetcherSpy);
|
|
256
|
+
|
|
257
|
+
const client = new Client({ url: "test" });
|
|
258
|
+
const request = client.createRequest()({
|
|
259
|
+
endpoint: "test",
|
|
260
|
+
});
|
|
261
|
+
client.setAdapter(newAdapter);
|
|
262
|
+
|
|
263
|
+
await client.adapter.fetch(request, "test");
|
|
264
|
+
|
|
265
|
+
expect(fetcherSpy).toHaveBeenCalledWith(
|
|
266
|
+
expect.objectContaining({
|
|
267
|
+
request: expect.any(Object),
|
|
268
|
+
requestId: "test",
|
|
269
|
+
}),
|
|
270
|
+
);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it("should trigger plugins on adapter fetch", async () => {
|
|
274
|
+
const fetcherSpy = jest.fn().mockImplementation(({ onSuccess }) => {
|
|
275
|
+
onSuccess({
|
|
276
|
+
data: {},
|
|
277
|
+
status: 200,
|
|
278
|
+
extra: {},
|
|
279
|
+
error: null,
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
const client = new Client({ url: "test" });
|
|
284
|
+
const pluginSpy = jest.fn();
|
|
285
|
+
client.triggerPlugins = pluginSpy;
|
|
286
|
+
|
|
287
|
+
const newAdapter = new Adapter({
|
|
288
|
+
name: "test-adapter",
|
|
289
|
+
defaultMethod: HttpMethods.GET,
|
|
290
|
+
defaultExtra: {},
|
|
291
|
+
systemErrorStatus: 0,
|
|
292
|
+
systemErrorExtra: {},
|
|
293
|
+
}).setFetcher(fetcherSpy);
|
|
294
|
+
|
|
295
|
+
const request = client.createRequest()({
|
|
296
|
+
endpoint: "test",
|
|
297
|
+
});
|
|
298
|
+
client.setAdapter(newAdapter);
|
|
299
|
+
|
|
300
|
+
await client.adapter.fetch(request, "test");
|
|
301
|
+
|
|
302
|
+
expect(pluginSpy).toHaveBeenCalledWith("onAdapterFetch", {
|
|
303
|
+
adapter: newAdapter,
|
|
304
|
+
request,
|
|
305
|
+
requestId: "test",
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it("should use mocker when mock is enabled", async () => {
|
|
310
|
+
const mockResponse = {
|
|
311
|
+
data: { mocked: true } as any,
|
|
312
|
+
status: 200,
|
|
313
|
+
extra: { headers: {} },
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
const client = new Client({ url: "test" });
|
|
317
|
+
const request = client
|
|
318
|
+
.createRequest()({
|
|
319
|
+
endpoint: "test",
|
|
320
|
+
})
|
|
321
|
+
.setMock(() => {
|
|
322
|
+
return mockResponse;
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
client.requestManager.addAbortController(request.abortKey, "test");
|
|
326
|
+
const response = await client.adapter.fetch(request, "test");
|
|
327
|
+
|
|
328
|
+
expect(response).toStrictEqual(expect.objectContaining(mockResponse));
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it("should handle errors during fetch execution", async () => {
|
|
332
|
+
const testError = new Error("Test error");
|
|
333
|
+
const fetcherSpy = jest.fn().mockImplementation(() => {
|
|
334
|
+
throw testError;
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
const client = new Client({ url: "test" });
|
|
338
|
+
const newAdapter = new Adapter({
|
|
339
|
+
name: "test-adapter",
|
|
340
|
+
defaultMethod: HttpMethods.GET,
|
|
341
|
+
defaultExtra: { extraData: true },
|
|
342
|
+
systemErrorStatus: 500,
|
|
343
|
+
systemErrorExtra: { systemError: true },
|
|
344
|
+
}).setFetcher(fetcherSpy);
|
|
345
|
+
|
|
346
|
+
const request = client.createRequest()({
|
|
347
|
+
endpoint: "test",
|
|
348
|
+
});
|
|
349
|
+
client.setAdapter(newAdapter);
|
|
350
|
+
|
|
351
|
+
const response = await client.adapter.fetch(request, "test");
|
|
352
|
+
|
|
353
|
+
expect(response).toEqual(
|
|
354
|
+
expect.objectContaining({
|
|
355
|
+
data: null,
|
|
356
|
+
error: testError,
|
|
357
|
+
status: 500,
|
|
358
|
+
extra: { systemError: true },
|
|
359
|
+
}),
|
|
360
|
+
);
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
it("should use current timestamp if startTime is not set during error", async () => {
|
|
364
|
+
const fetcherSpy = jest.fn().mockImplementation(() => {
|
|
365
|
+
throw new Error("Test error");
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
const client = new Client({ url: "test" });
|
|
369
|
+
const newAdapter = new Adapter({
|
|
370
|
+
name: "test-adapter",
|
|
371
|
+
defaultMethod: HttpMethods.GET,
|
|
372
|
+
defaultExtra: {},
|
|
373
|
+
systemErrorStatus: 500,
|
|
374
|
+
systemErrorExtra: {},
|
|
375
|
+
}).setFetcher(fetcherSpy);
|
|
376
|
+
|
|
377
|
+
const request = client.createRequest()({
|
|
378
|
+
endpoint: "test",
|
|
379
|
+
});
|
|
380
|
+
client.setAdapter(newAdapter);
|
|
381
|
+
|
|
382
|
+
const response = await client.adapter.fetch(request, "test");
|
|
383
|
+
|
|
384
|
+
expect(response.requestTimestamp).toBeNumber();
|
|
385
|
+
});
|
|
386
|
+
});
|
|
387
|
+
});
|