@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.
- package/.eslintrc.cjs +12 -0
- package/__tests__/e2e/e2e.abort-timeout.spec.ts +101 -0
- package/__tests__/e2e/e2e.advanced.spec.ts +423 -0
- package/__tests__/e2e/e2e.methods.spec.ts +243 -0
- package/__tests__/e2e/e2e.payloads.spec.ts +154 -0
- package/__tests__/features/adapter/adapter.base.spec.ts +414 -0
- package/__tests__/features/adapter/adapter.bindings.spec.ts +1025 -0
- package/__tests__/features/adapter/adapter.browser.spec.ts +88 -0
- package/__tests__/features/adapter/adapter.server.spec.ts +369 -0
- package/__tests__/features/adapter/adapter.utils.spec.ts +165 -0
- package/__tests__/features/cache/cache.base.spec.ts +366 -0
- package/__tests__/features/cache/cache.events.spec.ts +219 -0
- package/__tests__/features/cache/cache.garbage-collector.spec.ts +175 -0
- package/__tests__/features/cache/cache.lazy-storage.spec.ts +132 -0
- package/__tests__/features/cache/cache.modes.spec.ts +145 -0
- package/__tests__/features/cache/cache.scoping.spec.ts +171 -0
- package/__tests__/features/cache/cache.typescript.spec.ts +147 -0
- package/__tests__/features/cache/cache.utils.spec.ts +126 -0
- package/__tests__/features/client/client.auth.spec.ts +125 -0
- package/__tests__/features/client/client.base.spec.ts +161 -0
- package/__tests__/features/client/client.from-json.spec.ts +182 -0
- package/__tests__/features/client/client.hydration.spec.ts +236 -0
- package/__tests__/features/client/client.interceptor.spec.ts +225 -0
- package/__tests__/features/client/client.methods.spec.ts +189 -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.custom-storage.spec.ts +189 -0
- package/__tests__/features/dispatcher/dispatcher.events.spec.ts +309 -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 +142 -0
- package/__tests__/features/managers/app/app.manager.base.spec.ts +235 -0
- package/__tests__/features/managers/app/app.manager.ssr.spec.ts +43 -0
- package/__tests__/features/managers/logger/logger.base.spec.ts +109 -0
- package/__tests__/features/managers/logger/logger.utils.spec.ts +106 -0
- package/__tests__/features/managers/request/request.manager.base.spec.ts +224 -0
- package/__tests__/features/managers/request/request.manager.events.spec.ts +364 -0
- package/__tests__/features/mocker/mocker.base.spec.ts +639 -0
- package/__tests__/features/plugin/plugin.base.spec.ts +255 -0
- package/__tests__/features/request/request.base.spec.ts +225 -0
- package/__tests__/features/request/request.cloning.spec.ts +47 -0
- package/__tests__/features/request/request.hooks.spec.ts +173 -0
- package/__tests__/features/request/request.optimistic.spec.ts +93 -0
- package/__tests__/features/request/request.retry-on-error.spec.ts +359 -0
- package/__tests__/features/request/request.sending.spec.ts +364 -0
- package/__tests__/features/request/request.setters.spec.ts +164 -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 +132 -0
- package/__tests__/features/sdk/sdk.configuration.spec.ts +262 -0
- package/__tests__/features/utils/emitter.utils.spec.ts +88 -0
- package/__tests__/shared/callbacks.shared.ts +11 -0
- package/__tests__/shared/index.ts +2 -0
- package/__tests__/shared/progress.shared.ts +36 -0
- package/__tests__/tsconfig.json +24 -0
- package/__tests__/type-stress/generate-stress-test.ts +92 -0
- package/__tests__/type-stress/stress-10.ts +35 -0
- package/__tests__/type-stress/stress-just-create.ts +2107 -0
- package/__tests__/type-stress/stress-minimal.ts +23 -0
- package/__tests__/type-stress/stress-test-500.ts +2319 -0
- package/__tests__/type-stress/stress-test-sdk.ts +210 -0
- package/__tests__/type-stress/tsconfig.json +6 -0
- package/__tests__/utils/adapter.utils.ts +39 -0
- package/__tests__/utils/cache.utils.ts +20 -0
- package/__tests__/utils/callbacks.utils.ts +48 -0
- package/__tests__/utils/dispatcher.utils.ts +7 -0
- package/__tests__/utils/index.ts +4 -0
- package/__tests__/vitest.setup.ts +15 -0
- package/dist/adapter/adapter.bindings.d.ts +71 -0
- package/dist/adapter/adapter.bindings.d.ts.map +1 -0
- package/dist/adapter/adapter.d.ts +127 -0
- package/dist/adapter/adapter.d.ts.map +1 -0
- package/dist/adapter/adapter.types.d.ts +108 -0
- package/dist/adapter/adapter.types.d.ts.map +1 -0
- package/dist/adapter/adapter.utils.d.ts +23 -0
- package/dist/adapter/adapter.utils.d.ts.map +1 -0
- package/dist/adapter/index.d.ts +5 -0
- package/dist/adapter/index.d.ts.map +1 -0
- package/dist/cache/cache.d.ts +95 -0
- package/dist/cache/cache.d.ts.map +1 -0
- package/dist/cache/cache.events.d.ts +51 -0
- package/dist/cache/cache.events.d.ts.map +1 -0
- package/dist/cache/cache.types.d.ts +44 -0
- package/dist/cache/cache.types.d.ts.map +1 -0
- package/dist/cache/cache.utils.d.ts +11 -0
- package/dist/cache/cache.utils.d.ts.map +1 -0
- package/dist/cache/index.d.ts +5 -0
- package/dist/cache/index.d.ts.map +1 -0
- package/dist/client/client.constants.d.ts +10 -0
- package/dist/client/client.constants.d.ts.map +1 -0
- package/dist/client/client.create.d.ts +8 -0
- package/dist/client/client.create.d.ts.map +1 -0
- package/dist/client/client.d.ts +195 -0
- package/dist/client/client.d.ts.map +1 -0
- package/dist/client/client.types.d.ts +71 -0
- package/dist/client/client.types.d.ts.map +1 -0
- package/dist/client/client.utils.d.ts +11 -0
- package/dist/client/client.utils.d.ts.map +1 -0
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/constants/http.constants.d.ts +8 -0
- package/dist/constants/http.constants.d.ts.map +1 -0
- package/dist/constants/time.constants.d.ts +12 -0
- package/dist/constants/time.constants.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.constants.d.ts +7 -0
- package/dist/dispatcher/dispatcher.constants.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.d.ts +168 -0
- package/dist/dispatcher/dispatcher.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.events.d.ts +49 -0
- package/dist/dispatcher/dispatcher.events.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.types.d.ts +44 -0
- package/dist/dispatcher/dispatcher.types.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.utils.d.ts +12 -0
- package/dist/dispatcher/dispatcher.utils.d.ts.map +1 -0
- package/dist/dispatcher/index.d.ts +6 -0
- package/dist/dispatcher/index.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.constants.d.ts +4 -0
- package/dist/http-adapter/http-adapter.constants.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.d.ts +3 -0
- package/dist/http-adapter/http-adapter.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.fetch.d.ts +6 -0
- package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.types.d.ts +73 -0
- package/dist/http-adapter/http-adapter.types.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.utils.d.ts +10 -0
- package/dist/http-adapter/http-adapter.utils.d.ts.map +1 -0
- package/dist/http-adapter/index.d.ts +6 -0
- package/dist/http-adapter/index.d.ts.map +1 -0
- package/dist/index.d.ts +15 -2083
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +4084 -0
- package/dist/index.mjs.map +1 -0
- package/dist/managers/app/app.manager.constants.d.ts +10 -0
- package/dist/managers/app/app.manager.constants.d.ts.map +1 -0
- package/dist/managers/app/app.manager.d.ts +35 -0
- package/dist/managers/app/app.manager.d.ts.map +1 -0
- package/dist/managers/app/app.manager.events.d.ts +12 -0
- package/dist/managers/app/app.manager.events.d.ts.map +1 -0
- package/dist/managers/app/app.manager.types.d.ts +7 -0
- package/dist/managers/app/app.manager.types.d.ts.map +1 -0
- package/dist/managers/app/app.manager.utils.d.ts +5 -0
- package/dist/managers/app/app.manager.utils.d.ts.map +1 -0
- package/dist/managers/app/index.d.ts +6 -0
- package/dist/managers/app/index.d.ts.map +1 -0
- package/dist/managers/index.d.ts +4 -0
- package/dist/managers/index.d.ts.map +1 -0
- package/dist/managers/logger/index.d.ts +5 -0
- package/dist/managers/logger/index.d.ts.map +1 -0
- package/dist/managers/logger/logger.manager.constants.d.ts +5 -0
- package/dist/managers/logger/logger.manager.constants.d.ts.map +1 -0
- package/dist/managers/logger/logger.manager.d.ts +22 -0
- package/dist/managers/logger/logger.manager.d.ts.map +1 -0
- package/dist/managers/logger/logger.manager.types.d.ts +43 -0
- package/dist/managers/logger/logger.manager.types.d.ts.map +1 -0
- package/dist/managers/logger/logger.manager.utils.d.ts +4 -0
- package/dist/managers/logger/logger.manager.utils.d.ts.map +1 -0
- package/dist/managers/request/index.d.ts +5 -0
- package/dist/managers/request/index.d.ts.map +1 -0
- package/dist/managers/request/request.manager.d.ts +60 -0
- package/dist/managers/request/request.manager.d.ts.map +1 -0
- package/dist/managers/request/request.manager.events.d.ts +55 -0
- package/dist/managers/request/request.manager.events.d.ts.map +1 -0
- package/dist/managers/request/request.manager.types.d.ts +48 -0
- package/dist/managers/request/request.manager.types.d.ts.map +1 -0
- package/dist/managers/request/request.manager.utils.d.ts +30 -0
- package/dist/managers/request/request.manager.utils.d.ts.map +1 -0
- package/dist/mocker/index.d.ts +3 -0
- package/dist/mocker/index.d.ts.map +1 -0
- package/dist/mocker/mocker.d.ts +3 -0
- package/dist/mocker/mocker.d.ts.map +1 -0
- package/dist/mocker/mocker.types.d.ts +25 -0
- package/dist/mocker/mocker.types.d.ts.map +1 -0
- package/dist/plugin/index.d.ts +3 -0
- package/dist/plugin/index.d.ts.map +1 -0
- package/dist/plugin/plugin.d.ts +55 -0
- package/dist/plugin/plugin.d.ts.map +1 -0
- package/dist/plugin/plugin.types.d.ts +122 -0
- package/dist/plugin/plugin.types.d.ts.map +1 -0
- package/dist/request/index.d.ts +5 -0
- package/dist/request/index.d.ts.map +1 -0
- package/dist/request/request.d.ts +195 -0
- package/dist/request/request.d.ts.map +1 -0
- package/dist/request/request.hooks.d.ts +19 -0
- package/dist/request/request.hooks.d.ts.map +1 -0
- package/dist/request/request.types.d.ts +240 -0
- package/dist/request/request.types.d.ts.map +1 -0
- package/dist/request/request.utils.d.ts +25 -0
- package/dist/request/request.utils.d.ts.map +1 -0
- package/dist/sdk/index.d.ts +2 -0
- package/dist/sdk/index.d.ts.map +1 -0
- package/dist/sdk/sdk.d.ts +69 -0
- package/dist/sdk/sdk.d.ts.map +1 -0
- package/dist/types/adapter.types.d.ts +22 -0
- package/dist/types/adapter.types.d.ts.map +1 -0
- package/dist/types/client.types.d.ts +4 -0
- package/dist/types/client.types.d.ts.map +1 -0
- package/dist/types/helpers.types.d.ts +17 -0
- package/dist/types/helpers.types.d.ts.map +1 -0
- package/dist/types/http.types.d.ts +3 -0
- package/dist/types/http.types.d.ts.map +1 -0
- package/dist/types/hydration.types.d.ts +15 -0
- package/dist/types/hydration.types.d.ts.map +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/request.types.d.ts +34 -0
- package/dist/types/request.types.d.ts.map +1 -0
- package/dist/utils/emitter.utils.d.ts +13 -0
- package/dist/utils/emitter.utils.d.ts.map +1 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/uuid.utils.d.ts +3 -0
- package/dist/utils/uuid.utils.d.ts.map +1 -0
- package/package.json +10 -25
- package/project.json +4 -0
- package/src/adapter/adapter.bindings.ts +8 -12
- package/src/adapter/adapter.ts +8 -7
- package/src/adapter/adapter.types.ts +10 -6
- package/src/adapter/adapter.utils.ts +1 -1
- package/src/cache/cache.events.ts +4 -11
- package/src/cache/cache.ts +28 -22
- package/src/cache/cache.types.ts +8 -4
- package/src/cache/cache.utils.ts +3 -3
- package/src/client/client.create.ts +4 -3
- package/src/client/client.ts +44 -12
- package/src/client/client.types.ts +25 -3
- package/src/client/client.utils.ts +24 -4
- package/src/dispatcher/dispatcher.events.ts +3 -3
- package/src/dispatcher/dispatcher.ts +59 -28
- package/src/dispatcher/dispatcher.types.ts +19 -2
- package/src/dispatcher/dispatcher.utils.ts +5 -4
- package/src/http-adapter/http-adapter.constants.ts +1 -1
- package/src/http-adapter/http-adapter.fetch.ts +174 -0
- package/src/http-adapter/http-adapter.ts +3 -8
- package/src/http-adapter/http-adapter.types.ts +17 -9
- package/src/http-adapter/http-adapter.utils.ts +6 -9
- package/src/http-adapter/index.ts +1 -0
- package/src/managers/app/app.manager.constants.ts +2 -2
- package/src/managers/app/app.manager.events.ts +1 -1
- package/src/managers/app/app.manager.ts +2 -1
- package/src/managers/logger/logger.manager.constants.ts +1 -1
- package/src/managers/logger/logger.manager.ts +2 -1
- package/src/managers/logger/logger.manager.types.ts +2 -2
- package/src/managers/logger/logger.manager.utils.ts +2 -1
- package/src/managers/request/request.manager.events.ts +13 -11
- package/src/managers/request/request.manager.types.ts +6 -3
- package/src/mocker/mocker.ts +3 -2
- package/src/mocker/mocker.types.ts +1 -1
- package/src/plugin/plugin.ts +2 -2
- package/src/plugin/plugin.types.ts +9 -9
- package/src/request/index.ts +1 -0
- package/src/request/request.hooks.ts +87 -0
- package/src/request/request.ts +155 -77
- package/src/request/request.types.ts +54 -22
- package/src/request/request.utils.ts +86 -23
- package/src/sdk/sdk.ts +153 -8
- package/src/types/adapter.types.ts +1 -1
- package/src/types/client.types.ts +1 -1
- package/src/types/hydration.types.ts +3 -3
- package/src/types/request.types.ts +30 -20
- package/src/utils/uuid.utils.ts +1 -1
- package/tsconfig.base.json +1 -6
- package/tsconfig.json +1 -4
- package/vite.config.ts +45 -0
- package/.eslintrc.json +0 -11
- package/dist/browser/index.js +0 -3751
- package/dist/browser/index.js.map +0 -7
- package/dist/index.js +0 -3991
- package/dist/index.js.map +0 -7
- package/src/http-adapter/http-adapter.browser.ts +0 -102
- package/src/http-adapter/http-adapter.server.ts +0 -135
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
+
|
|
3
|
+
import { Plugin } from "plugin";
|
|
4
|
+
import { Client } from "client";
|
|
5
|
+
import type { RequestOptionsType } from "request";
|
|
6
|
+
import { Request } from "request";
|
|
7
|
+
import { LoggerManager } from "managers";
|
|
8
|
+
import { createAdapter, interceptorCallback, middlewareCallback } from "../../utils";
|
|
9
|
+
import type { FetchAdapterOptionsType, QueryStringifyOptionsType } from "http-adapter";
|
|
10
|
+
|
|
11
|
+
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
12
|
+
|
|
13
|
+
describe("Client [ Methods ]", () => {
|
|
14
|
+
let client = new Client({ url: "shared-base-url" });
|
|
15
|
+
const request = client.createRequest()({ endpoint: "shared-base-endpoint" });
|
|
16
|
+
beforeAll(() => {
|
|
17
|
+
startServer();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
client = new Client({ url: "shared-base-url" });
|
|
22
|
+
resetMocks();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
afterAll(() => {
|
|
26
|
+
stopServer();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("When using config methods", () => {
|
|
30
|
+
it("should assign default request config [setRequestDefaults]", async () => {
|
|
31
|
+
const options: Partial<RequestOptionsType<string, FetchAdapterOptionsType>> = { method: "POST" };
|
|
32
|
+
client.adapter.setRequestDefaults(() => options);
|
|
33
|
+
const req = client.createRequest()({ endpoint: "test" });
|
|
34
|
+
|
|
35
|
+
expect(client.adapter.unstable_getRequestDefaults?.(request.requestOptions)).toEqual(options);
|
|
36
|
+
expect(req.method).toBe("POST");
|
|
37
|
+
});
|
|
38
|
+
it("should assign default adapter config [setAdapterDefaultOptions]", async () => {
|
|
39
|
+
const options: FetchAdapterOptionsType = { timeout: 12312312 };
|
|
40
|
+
client.adapter.setAdapterDefaults(() => options);
|
|
41
|
+
|
|
42
|
+
expect(client.adapter.unstable_getAdapterDefaults?.(request)).toEqual(options);
|
|
43
|
+
});
|
|
44
|
+
it("should assign query params stringify config [setQueryParamsConfig]", async () => {
|
|
45
|
+
const options: QueryStringifyOptionsType = {
|
|
46
|
+
strict: false,
|
|
47
|
+
dateParser: () => "date",
|
|
48
|
+
objectParser: () => "object",
|
|
49
|
+
};
|
|
50
|
+
client.adapter.setQueryParamsMapperConfig(options);
|
|
51
|
+
|
|
52
|
+
expect(client.adapter.unstable_queryParamsMapperConfig).toEqual(options);
|
|
53
|
+
expect(client.adapter.unstable_queryParamsMapper({ date: new Date() })).toBe("?date=date");
|
|
54
|
+
expect(client.adapter.unstable_queryParamsMapper({ object: {} })).toBe("?object=object");
|
|
55
|
+
});
|
|
56
|
+
it("should assign debug value [setDebug]", async () => {
|
|
57
|
+
client.setLogger(() => new LoggerManager({ logger: () => null })).setDebug(true);
|
|
58
|
+
expect(client.debug).toEqual(true);
|
|
59
|
+
});
|
|
60
|
+
it("should assign query params handling callback [setStringifyQueryParams]", async () => {
|
|
61
|
+
const callback = () => "?test=1";
|
|
62
|
+
client.adapter.setQueryParamsMapper(callback);
|
|
63
|
+
|
|
64
|
+
expect(client.adapter.unstable_queryParamsMapper({}, {})).toEqual("?test=1");
|
|
65
|
+
});
|
|
66
|
+
it("should assign logger levels [setLoggerLevel]", async () => {
|
|
67
|
+
client.setLogLevel("info");
|
|
68
|
+
|
|
69
|
+
expect(client.loggerManager.level).toEqual("info");
|
|
70
|
+
});
|
|
71
|
+
it("should assign new adapter [setAdapter]", async () => {
|
|
72
|
+
const callback = createAdapter();
|
|
73
|
+
client.setAdapter(callback);
|
|
74
|
+
|
|
75
|
+
expect(client.adapter).toEqual(callback);
|
|
76
|
+
});
|
|
77
|
+
it("should assign auth middleware [onAuth]", async () => {
|
|
78
|
+
const callback = middlewareCallback();
|
|
79
|
+
client.onAuth(callback).onAuth(callback);
|
|
80
|
+
|
|
81
|
+
expect(client.unstable_onAuthCallbacks).toHaveLength(2);
|
|
82
|
+
expect(client.unstable_onAuthCallbacks[0]).toEqual(callback);
|
|
83
|
+
expect(client.unstable_onAuthCallbacks[1]).toEqual(callback);
|
|
84
|
+
});
|
|
85
|
+
it("should assign error interceptors [onError]", async () => {
|
|
86
|
+
const callback = interceptorCallback();
|
|
87
|
+
client.onError(callback).onError(callback);
|
|
88
|
+
|
|
89
|
+
expect(client.unstable_onErrorCallbacks).toHaveLength(2);
|
|
90
|
+
expect(client.unstable_onErrorCallbacks[0]).toEqual(callback);
|
|
91
|
+
expect(client.unstable_onErrorCallbacks[1]).toEqual(callback);
|
|
92
|
+
});
|
|
93
|
+
it("should assign success interceptors [onSuccess]", async () => {
|
|
94
|
+
const callback = interceptorCallback();
|
|
95
|
+
client.onSuccess(callback).onSuccess(callback);
|
|
96
|
+
|
|
97
|
+
expect(client.unstable_onSuccessCallbacks).toHaveLength(2);
|
|
98
|
+
expect(client.unstable_onSuccessCallbacks[0]).toEqual(callback);
|
|
99
|
+
expect(client.unstable_onSuccessCallbacks[1]).toEqual(callback);
|
|
100
|
+
});
|
|
101
|
+
it("should assign request triggers [onRequest]", async () => {
|
|
102
|
+
const callback = middlewareCallback();
|
|
103
|
+
client.onRequest(callback).onRequest(callback);
|
|
104
|
+
|
|
105
|
+
expect(client.unstable_onRequestCallbacks).toHaveLength(2);
|
|
106
|
+
expect(client.unstable_onRequestCallbacks[0]).toEqual(callback);
|
|
107
|
+
expect(client.unstable_onRequestCallbacks[1]).toEqual(callback);
|
|
108
|
+
});
|
|
109
|
+
it("should assign response triggers [onResponse]", async () => {
|
|
110
|
+
const callback = interceptorCallback();
|
|
111
|
+
client.onResponse(callback).onResponse(callback);
|
|
112
|
+
|
|
113
|
+
expect(client.unstable_onResponseCallbacks).toHaveLength(2);
|
|
114
|
+
expect(client.unstable_onResponseCallbacks[0]).toEqual(callback);
|
|
115
|
+
expect(client.unstable_onResponseCallbacks[1]).toEqual(callback);
|
|
116
|
+
});
|
|
117
|
+
it("should create new request on createRequest method trigger", async () => {
|
|
118
|
+
const endpoint = "some-endpoint";
|
|
119
|
+
const newRequest = client.createRequest()({ endpoint });
|
|
120
|
+
|
|
121
|
+
expect(newRequest instanceof Request).toBeTrue();
|
|
122
|
+
expect(newRequest.endpoint).toBe(endpoint);
|
|
123
|
+
});
|
|
124
|
+
it("should add single effect listeners on addPlugin method trigger", async () => {
|
|
125
|
+
const plugin = new Plugin({ name: "NEW-EFFECT" });
|
|
126
|
+
|
|
127
|
+
client.addPlugin(plugin);
|
|
128
|
+
|
|
129
|
+
expect(client.plugins[0]).toBe(plugin);
|
|
130
|
+
expect(client.plugins).toHaveLength(1);
|
|
131
|
+
});
|
|
132
|
+
it("should add plugins array listeners on addPlugin method trigger", async () => {
|
|
133
|
+
const plugin = new Plugin({ name: "NEW-EFFECT" });
|
|
134
|
+
|
|
135
|
+
client.addPlugin(plugin);
|
|
136
|
+
|
|
137
|
+
expect(client.plugins[0]).toBe(plugin);
|
|
138
|
+
expect(client.plugins).toHaveLength(1);
|
|
139
|
+
});
|
|
140
|
+
it("should remove plugin listener on removePlugin method trigger", async () => {
|
|
141
|
+
const plugin = new Plugin({ name: "NEW-EFFECT" });
|
|
142
|
+
|
|
143
|
+
client.addPlugin(plugin);
|
|
144
|
+
client.removePlugin(plugin);
|
|
145
|
+
expect(client.plugins).toHaveLength(0);
|
|
146
|
+
|
|
147
|
+
client.addPlugin(plugin);
|
|
148
|
+
expect(client.plugins).toHaveLength(1);
|
|
149
|
+
});
|
|
150
|
+
it("should not remove plugin when wrong pluginKey is passed", async () => {
|
|
151
|
+
const plugin = new Plugin({ name: "NEW-EFFECT" });
|
|
152
|
+
const otherEffect = new Plugin({ name: "OTHER-EFFECT" });
|
|
153
|
+
|
|
154
|
+
client.addPlugin(plugin);
|
|
155
|
+
client.removePlugin(otherEffect);
|
|
156
|
+
expect(client.plugins).toHaveLength(1);
|
|
157
|
+
|
|
158
|
+
expect(client.plugins).toHaveLength(1);
|
|
159
|
+
});
|
|
160
|
+
it("should assign query params handling callback [setHeaderMapper]", async () => {
|
|
161
|
+
const callback = () => ({ test: 123 }) as any as HeadersInit;
|
|
162
|
+
client.adapter.setHeaderMapper(callback);
|
|
163
|
+
|
|
164
|
+
expect(client.adapter.unstable_headerMapper({}, {})).toEqual({ test: 123 });
|
|
165
|
+
});
|
|
166
|
+
it("should assign payload handling callback [setPayloadMapper]", async () => {
|
|
167
|
+
const callback = () => "test-123";
|
|
168
|
+
client.adapter.setPayloadMapper(callback);
|
|
169
|
+
|
|
170
|
+
expect(client.adapter.unstable_payloadMapper({}, {})).toEqual("test-123");
|
|
171
|
+
});
|
|
172
|
+
it("should assign endpoint handling callback [setEndpointMapper]", async () => {
|
|
173
|
+
const callback = () => "test-endpoint";
|
|
174
|
+
client.adapter.setEndpointMapper(callback);
|
|
175
|
+
|
|
176
|
+
expect(client.adapter.unstable_endpointMapper({}, {})).toEqual("test-endpoint");
|
|
177
|
+
});
|
|
178
|
+
it("should assign key mappers", async () => {
|
|
179
|
+
const callback = () => "";
|
|
180
|
+
client.setAbortKeyMapper(callback);
|
|
181
|
+
client.setCacheKeyMapper(callback);
|
|
182
|
+
client.setQueryKeyMapper(callback);
|
|
183
|
+
|
|
184
|
+
expect(client.unstable_abortKeyMapper).toEqual(callback);
|
|
185
|
+
expect(client.unstable_cacheKeyMapper).toEqual(callback);
|
|
186
|
+
expect(client.unstable_queryKeyMapper).toEqual(callback);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
});
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
+
|
|
3
|
+
import { middlewareCallback } from "../../utils";
|
|
4
|
+
import { testCallbacksExecution } from "../../shared";
|
|
5
|
+
import { Client } from "client";
|
|
6
|
+
|
|
7
|
+
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
8
|
+
|
|
9
|
+
describe("Client [ Middleware ]", () => {
|
|
10
|
+
let client = new Client({ url: "shared-base-url" });
|
|
11
|
+
let request = client.createRequest()({ endpoint: "shared-base-endpoint" });
|
|
12
|
+
|
|
13
|
+
const spy1 = vi.fn();
|
|
14
|
+
const spy2 = vi.fn();
|
|
15
|
+
const spy3 = vi.fn();
|
|
16
|
+
|
|
17
|
+
beforeAll(() => {
|
|
18
|
+
startServer();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
client = new Client({ url: "shared-base-url" });
|
|
23
|
+
request = client.createRequest()({ endpoint: "shared-base-endpoint" });
|
|
24
|
+
resetMocks();
|
|
25
|
+
vi.clearAllMocks();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
afterAll(() => {
|
|
29
|
+
stopServer();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe("When middleware callbacks being added", () => {
|
|
33
|
+
it("should assign onAuth middleware", async () => {
|
|
34
|
+
const callback = middlewareCallback();
|
|
35
|
+
client.onAuth(callback).onAuth(callback);
|
|
36
|
+
|
|
37
|
+
expect(client.unstable_onAuthCallbacks).toHaveLength(2);
|
|
38
|
+
expect(client.unstable_onAuthCallbacks[0]).toEqual(callback);
|
|
39
|
+
expect(client.unstable_onAuthCallbacks[1]).toEqual(callback);
|
|
40
|
+
});
|
|
41
|
+
it("should assign onRequest middleware", async () => {
|
|
42
|
+
const callback = middlewareCallback();
|
|
43
|
+
client.onRequest(callback).onRequest(callback);
|
|
44
|
+
|
|
45
|
+
expect(client.unstable_onRequestCallbacks).toHaveLength(2);
|
|
46
|
+
expect(client.unstable_onRequestCallbacks[0]).toEqual(callback);
|
|
47
|
+
expect(client.unstable_onRequestCallbacks[1]).toEqual(callback);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("When middleware callbacks go into the execution loop", () => {
|
|
52
|
+
it("should trigger unstable_modifyAuth async loop", async () => {
|
|
53
|
+
const callbackFirst = middlewareCallback({ callback: spy1, sleepTime: 10 });
|
|
54
|
+
const callbackMiddle = middlewareCallback({ callback: spy2 });
|
|
55
|
+
const callbackLast = middlewareCallback({ callback: spy3, sleepTime: 10 });
|
|
56
|
+
|
|
57
|
+
client.onAuth(callbackFirst).onAuth(callbackMiddle).onAuth(callbackLast);
|
|
58
|
+
await client.unstable_modifyAuth(request);
|
|
59
|
+
|
|
60
|
+
testCallbacksExecution([spy1, spy2, spy3]);
|
|
61
|
+
});
|
|
62
|
+
it("should trigger unstable_modifyRequest async loop", async () => {
|
|
63
|
+
const callbackFirst = middlewareCallback({ callback: spy1, sleepTime: 10 });
|
|
64
|
+
const callbackMiddle = middlewareCallback({ callback: spy2 });
|
|
65
|
+
const callbackLast = middlewareCallback({ callback: spy3, sleepTime: 10 });
|
|
66
|
+
|
|
67
|
+
client.onRequest(callbackFirst).onRequest(callbackMiddle).onRequest(callbackLast);
|
|
68
|
+
await client.unstable_modifyRequest(request);
|
|
69
|
+
|
|
70
|
+
testCallbacksExecution([spy1, spy2, spy3]);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe("When middleware returns undefined value", () => {
|
|
75
|
+
it("should throw onRequest method when request is not returned", async () => {
|
|
76
|
+
client.onRequest(() => undefined as any);
|
|
77
|
+
|
|
78
|
+
expect(client.unstable_modifyRequest(request)).rejects.toThrow();
|
|
79
|
+
});
|
|
80
|
+
it("should throw onAuth method when request is not returned", async () => {
|
|
81
|
+
client.onAuth(() => undefined as any);
|
|
82
|
+
|
|
83
|
+
expect(client.unstable_modifyAuth(request)).rejects.toThrow();
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe("When user wants to remove listeners", () => {
|
|
88
|
+
it("should allow for removing middleware on request", async () => {
|
|
89
|
+
const firstCallback = middlewareCallback({ callback: spy1 });
|
|
90
|
+
const secondCallback = middlewareCallback({ callback: spy2 });
|
|
91
|
+
client.onRequest(firstCallback).onRequest(secondCallback);
|
|
92
|
+
mockRequest(request);
|
|
93
|
+
|
|
94
|
+
await request.send({});
|
|
95
|
+
client.removeOnRequestInterceptors([secondCallback]);
|
|
96
|
+
|
|
97
|
+
await request.send({});
|
|
98
|
+
|
|
99
|
+
expect(spy1).toHaveBeenCalledTimes(2);
|
|
100
|
+
expect(spy2).toHaveBeenCalledTimes(1);
|
|
101
|
+
});
|
|
102
|
+
it("should allow for removing interceptors on auth", async () => {
|
|
103
|
+
const authRequest = client.createRequest()({ endpoint: "/auth" }).setAuth(true);
|
|
104
|
+
const firstCallback = middlewareCallback({ callback: spy1 });
|
|
105
|
+
const secondCallback = middlewareCallback({ callback: spy2 });
|
|
106
|
+
client.onAuth(firstCallback).onAuth(secondCallback);
|
|
107
|
+
mockRequest(authRequest);
|
|
108
|
+
|
|
109
|
+
await authRequest.send({});
|
|
110
|
+
client.removeOnAuthInterceptors([secondCallback]);
|
|
111
|
+
|
|
112
|
+
await authRequest.send({});
|
|
113
|
+
|
|
114
|
+
expect(spy1).toHaveBeenCalledTimes(2);
|
|
115
|
+
expect(spy2).toHaveBeenCalledTimes(1);
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
});
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
+
|
|
3
|
+
import { Client, interceptRequest, interceptResponse, stringifyDefaultOptions } from "client";
|
|
4
|
+
import { stringifyQueryParams } from "http-adapter";
|
|
5
|
+
|
|
6
|
+
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
7
|
+
|
|
8
|
+
const options = {
|
|
9
|
+
...stringifyDefaultOptions,
|
|
10
|
+
encode: true,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
describe("Client [ Utils ]", () => {
|
|
14
|
+
let client = new Client({ url: "shared-base-url" });
|
|
15
|
+
client.adapter.setQueryParamsMapperConfig(options);
|
|
16
|
+
let request = client.createRequest<{ payload: FormData; response: any }>()({ endpoint: "shared-nase-endpoint" });
|
|
17
|
+
|
|
18
|
+
beforeAll(() => {
|
|
19
|
+
startServer();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
client = new Client({ url: "shared-base-url" });
|
|
24
|
+
client.adapter.setQueryParamsMapperConfig(options);
|
|
25
|
+
request = client.createRequest<{ response: any; payload: FormData }>()({ endpoint: "shared-nase-endpoint" });
|
|
26
|
+
resetMocks();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
afterAll(() => {
|
|
30
|
+
stopServer();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe("When using stringifyQueryParams util", () => {
|
|
34
|
+
it("should encode falsy values", async () => {
|
|
35
|
+
const newClient = new Client({ url: "shared-base-url" });
|
|
36
|
+
newClient.adapter.setQueryParamsMapperConfig({
|
|
37
|
+
encode: false,
|
|
38
|
+
arrayFormat: "bracket",
|
|
39
|
+
});
|
|
40
|
+
expect(
|
|
41
|
+
newClient.adapter.unstable_queryParamsMapper({ value: { test: new Date("9,9,2020").toISOString() } }),
|
|
42
|
+
).toBe(`?value={"test":"${new Date("9,9,2020").toISOString()}"}`);
|
|
43
|
+
expect(
|
|
44
|
+
newClient.adapter.unstable_queryParamsMapper({
|
|
45
|
+
page: 0,
|
|
46
|
+
status: ["status1", "status2"],
|
|
47
|
+
lastFreeDay: '{"from":"2023-06-20T10:37:27.508Z","to":"2023-06-24T10:37:27.508Z"}',
|
|
48
|
+
account: "test-account/123+323-123|111:;",
|
|
49
|
+
}),
|
|
50
|
+
).toBe(
|
|
51
|
+
'?page=0&status[]=status1&status[]=status2&lastFreeDay={"from":"2023-06-20T10:37:27.508Z","to":"2023-06-24T10:37:27.508Z"}&account=test-account/123+323-123|111:;',
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
it("should encode falsy values", async () => {
|
|
55
|
+
expect(stringifyQueryParams({ value: 0 }, options)).toBe("?value=0");
|
|
56
|
+
expect(stringifyQueryParams({ value: "" }, { ...options, skipEmptyString: false })).toBe("?value=");
|
|
57
|
+
expect(stringifyQueryParams({ value: null }, { ...options, skipNull: false })).toBe("?value=null");
|
|
58
|
+
expect(stringifyQueryParams({ value: "" }, { ...options, skipEmptyString: true })).toBe("");
|
|
59
|
+
expect(stringifyQueryParams({ value: undefined }, { ...options, skipEmptyString: true })).toBe("");
|
|
60
|
+
expect(stringifyQueryParams({ value: null }, { ...options, skipNull: true })).toBe("");
|
|
61
|
+
});
|
|
62
|
+
it("should encode date value to isoString by default", async () => {
|
|
63
|
+
expect(stringifyQueryParams({ value: new Date("2023-05-12T22:32:32") }, options)).toInclude(`?value=2023-05-12`);
|
|
64
|
+
});
|
|
65
|
+
it("should encode truthy values", async () => {
|
|
66
|
+
expect(stringifyQueryParams({ value: 10 }, options)).toBe("?value=10");
|
|
67
|
+
expect(stringifyQueryParams({ value: "test" }, options)).toBe("?value=test");
|
|
68
|
+
expect(stringifyQueryParams({ value: [1, 2, 3] }, options)).toBe("?value[]=1&value[]=2&value[]=3");
|
|
69
|
+
expect(stringifyQueryParams({ value: { data: "test" } }, options)).toBe("?value=%7B%22data%22%3A%22test%22%7D");
|
|
70
|
+
expect(
|
|
71
|
+
stringifyQueryParams(
|
|
72
|
+
{
|
|
73
|
+
value: {
|
|
74
|
+
filter: {
|
|
75
|
+
firstName: "john",
|
|
76
|
+
other: {
|
|
77
|
+
hobbies: ["dancing", "singing"],
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
options,
|
|
83
|
+
),
|
|
84
|
+
).toBe(
|
|
85
|
+
"?value=%7B%22filter%22%3A%7B%22firstName%22%3A%22john%22%2C%22other%22%3A%7B%22hobbies%22%3A%5B%22dancing%22%2C%22singing%22%5D%7D%7D%7D",
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
it("should encode not allowed characters values", async () => {
|
|
89
|
+
expect(stringifyQueryParams({ value: "[]" }, options)).toBe("?value=%5B%5D");
|
|
90
|
+
expect(stringifyQueryParams({ value: "/" }, options)).toBe("?value=%2F");
|
|
91
|
+
expect(stringifyQueryParams({ value: "," }, options)).toBe("?value=%2C");
|
|
92
|
+
expect(stringifyQueryParams({ value: "|" }, options)).toBe("?value=%7C");
|
|
93
|
+
expect(stringifyQueryParams({ value: ":" }, options)).toBe("?value=%3A");
|
|
94
|
+
expect(stringifyQueryParams({ value: ";" }, options)).toBe("?value=%3B");
|
|
95
|
+
expect(stringifyQueryParams({ value: "+" }, options)).toBe("?value=%2B");
|
|
96
|
+
expect(stringifyQueryParams({ value: "(" }, options)).toBe("?value=%28");
|
|
97
|
+
expect(stringifyQueryParams({ value: ")" }, options)).toBe("?value=%29");
|
|
98
|
+
expect(stringifyQueryParams({ value: "*" }, options)).toBe("?value=%2A");
|
|
99
|
+
expect(stringifyQueryParams({ value: "&" }, options)).toBe("?value=%26");
|
|
100
|
+
expect(stringifyQueryParams({ value: "^" }, options)).toBe("?value=%5E");
|
|
101
|
+
expect(stringifyQueryParams({ value: "%" }, options)).toBe("?value=%25");
|
|
102
|
+
expect(stringifyQueryParams({ value: "$" }, options)).toBe("?value=%24");
|
|
103
|
+
expect(stringifyQueryParams({ value: "#" }, options)).toBe("?value=%23");
|
|
104
|
+
expect(stringifyQueryParams({ value: "@" }, options)).toBe("?value=%40");
|
|
105
|
+
expect(stringifyQueryParams({ value: "!" }, options)).toBe("?value=%21");
|
|
106
|
+
});
|
|
107
|
+
it("should encode array values with different formats", async () => {
|
|
108
|
+
expect(
|
|
109
|
+
stringifyQueryParams({ value: [1, 2, 3] }, { ...options, arraySeparator: "", arrayFormat: "bracket" }),
|
|
110
|
+
).toBe("?value[]=1&value[]=2&value[]=3");
|
|
111
|
+
expect(
|
|
112
|
+
stringifyQueryParams(
|
|
113
|
+
{ value: [1, 2, 3] },
|
|
114
|
+
{ ...options, arraySeparator: "", arrayFormat: "bracket-separator" },
|
|
115
|
+
),
|
|
116
|
+
).toBe("?value[]=1|2|3");
|
|
117
|
+
expect(stringifyQueryParams({ value: [1, 2, 3] }, { ...options, arraySeparator: "", arrayFormat: "comma" })).toBe(
|
|
118
|
+
"?value=1,2,3",
|
|
119
|
+
);
|
|
120
|
+
expect(stringifyQueryParams({ value: [1, 2, 3] }, { ...options, arraySeparator: "", arrayFormat: "index" })).toBe(
|
|
121
|
+
"?value[0]=1&value[1]=2&value[2]=3",
|
|
122
|
+
);
|
|
123
|
+
expect(stringifyQueryParams({ value: [1, 2, 3] }, { ...options, arraySeparator: "", arrayFormat: "none" })).toBe(
|
|
124
|
+
"?value=1&value=2&value=3",
|
|
125
|
+
);
|
|
126
|
+
expect(
|
|
127
|
+
stringifyQueryParams({ value: [1, 2, 3] }, { ...options, arraySeparator: "", arrayFormat: "separator" }),
|
|
128
|
+
).toBe("?value=1|2|3");
|
|
129
|
+
expect(
|
|
130
|
+
stringifyQueryParams({ value: [1, 2, 3] }, { ...options, arrayFormat: "separator", arraySeparator: "#" }),
|
|
131
|
+
).toBe("?value=1#2#3");
|
|
132
|
+
});
|
|
133
|
+
it("should encode values without strict option", async () => {
|
|
134
|
+
expect(stringifyQueryParams({ value: "![]" }, { ...options, encode: true, strict: false })).toBe(
|
|
135
|
+
"?value=!%5B%5D",
|
|
136
|
+
);
|
|
137
|
+
expect(stringifyQueryParams({ value: "![]" }, { ...options, encode: false, strict: false })).toBe("?value=![]");
|
|
138
|
+
expect(stringifyQueryParams({ value: "![]" }, { ...options, encode: false, strict: true })).toBe("?value=![]");
|
|
139
|
+
});
|
|
140
|
+
it("should add the question mark", async () => {
|
|
141
|
+
expect(stringifyQueryParams("something", options)).toBe("?something");
|
|
142
|
+
expect(stringifyQueryParams("?something", options)).toBe("?something");
|
|
143
|
+
});
|
|
144
|
+
it("should return empty string when no query params are passed", async () => {
|
|
145
|
+
expect(stringifyQueryParams("", options)).toBe("");
|
|
146
|
+
expect(stringifyQueryParams({}, options)).toBe("");
|
|
147
|
+
expect(stringifyQueryParams(null, options)).toBe("");
|
|
148
|
+
expect(stringifyQueryParams(undefined, options)).toBe("");
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
describe("When using headerMapper", () => {
|
|
153
|
+
it("should assign default headers with headers mapper", async () => {
|
|
154
|
+
const defaultHeaders = {};
|
|
155
|
+
const headers = client.adapter.unstable_headerMapper(request, undefined);
|
|
156
|
+
|
|
157
|
+
expect(headers).toEqual(defaultHeaders);
|
|
158
|
+
});
|
|
159
|
+
it("should assign form data headers", async () => {
|
|
160
|
+
const defaultHeaders = {};
|
|
161
|
+
const data = new FormData();
|
|
162
|
+
const formDataRequest = request.setPayload(data);
|
|
163
|
+
const headers = client.adapter.unstable_headerMapper(formDataRequest, undefined);
|
|
164
|
+
|
|
165
|
+
expect(headers).toEqual(defaultHeaders);
|
|
166
|
+
});
|
|
167
|
+
it("should not re-assign form data headers", async () => {
|
|
168
|
+
const defaultHeaders = { "Content-Type": "some-custom-format" };
|
|
169
|
+
const formDataRequest = request.setHeaders(defaultHeaders);
|
|
170
|
+
const headers = client.adapter.unstable_headerMapper(formDataRequest, undefined);
|
|
171
|
+
|
|
172
|
+
expect(headers).toEqual(defaultHeaders);
|
|
173
|
+
});
|
|
174
|
+
it("should add content type header for object payload", async () => {
|
|
175
|
+
const defaultHeaders = { "Content-Type": "application/json" };
|
|
176
|
+
const data = { data: [] } as any;
|
|
177
|
+
const formDataRequest = request.setPayload(data);
|
|
178
|
+
const headers = client.adapter.unstable_headerMapper(formDataRequest, undefined);
|
|
179
|
+
|
|
180
|
+
expect(headers).toEqual(defaultHeaders);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
describe("When using payloadMapper", () => {
|
|
185
|
+
it("should allow to stringify payload", async () => {
|
|
186
|
+
const data = { data: [] };
|
|
187
|
+
const payload = client.adapter.unstable_payloadMapper(data, undefined);
|
|
188
|
+
|
|
189
|
+
expect(payload).toEqual(JSON.stringify(data));
|
|
190
|
+
});
|
|
191
|
+
it("should not stringify FormData payload", async () => {
|
|
192
|
+
const data = new FormData();
|
|
193
|
+
const payload = client.adapter.unstable_payloadMapper(data, undefined);
|
|
194
|
+
|
|
195
|
+
expect(payload).toEqual(data);
|
|
196
|
+
});
|
|
197
|
+
it("should stringify null payload", async () => {
|
|
198
|
+
const data = null;
|
|
199
|
+
const payload = client.adapter.unstable_payloadMapper(data, undefined);
|
|
200
|
+
|
|
201
|
+
expect(payload).toEqual(JSON.stringify(data));
|
|
202
|
+
});
|
|
203
|
+
it("should not stringify undefined payload", async () => {
|
|
204
|
+
const payload = client.adapter.unstable_payloadMapper(undefined, undefined);
|
|
205
|
+
|
|
206
|
+
expect(payload).toBeUndefined();
|
|
207
|
+
});
|
|
208
|
+
it("should not stringify invalid payload", async () => {
|
|
209
|
+
const payload = client.adapter.unstable_payloadMapper(() => null, undefined);
|
|
210
|
+
expect(payload).toBeUndefined();
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
describe("When using stringifyValue util", () => {
|
|
215
|
+
it("should allow to stringify payload", async () => {
|
|
216
|
+
const data = { data: [] };
|
|
217
|
+
const payload = client.adapter.unstable_payloadMapper(data, undefined);
|
|
218
|
+
|
|
219
|
+
expect(payload).toEqual(JSON.stringify(data));
|
|
220
|
+
});
|
|
221
|
+
it("should not stringify FormData payload", async () => {
|
|
222
|
+
const data = new FormData();
|
|
223
|
+
const payload = client.adapter.unstable_payloadMapper(data, undefined);
|
|
224
|
+
|
|
225
|
+
expect(payload).toEqual(data);
|
|
226
|
+
});
|
|
227
|
+
it("should stringify null payload", async () => {
|
|
228
|
+
const data = null;
|
|
229
|
+
const payload = client.adapter.unstable_payloadMapper(data, undefined);
|
|
230
|
+
|
|
231
|
+
expect(payload).toEqual(JSON.stringify(data));
|
|
232
|
+
});
|
|
233
|
+
it("should not stringify undefined payload", async () => {
|
|
234
|
+
const payload = client.adapter.unstable_payloadMapper(undefined, undefined);
|
|
235
|
+
|
|
236
|
+
expect(payload).toBeUndefined();
|
|
237
|
+
});
|
|
238
|
+
it("should not stringify invalid payload", async () => {
|
|
239
|
+
const data: Record<string, unknown> = {};
|
|
240
|
+
data.a = { b: data };
|
|
241
|
+
|
|
242
|
+
const payload = client.adapter.unstable_payloadMapper(data, undefined);
|
|
243
|
+
expect(payload).toBe("");
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
describe("when interceptors are disabled", () => {
|
|
248
|
+
it("should skip request interceptors when disabled", async () => {
|
|
249
|
+
const mockInterceptor = vi.fn((req) => req);
|
|
250
|
+
const req = client.createRequest<{ response: any }>()({
|
|
251
|
+
endpoint: "shared-base-endpoint",
|
|
252
|
+
disableRequestInterceptors: true,
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
const result = await interceptRequest([mockInterceptor], req);
|
|
256
|
+
|
|
257
|
+
expect(mockInterceptor).not.toHaveBeenCalled();
|
|
258
|
+
expect(result).toBe(req);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it("should skip response interceptors when disabled", async () => {
|
|
262
|
+
const mockInterceptor = vi.fn((res) => res);
|
|
263
|
+
const response = {
|
|
264
|
+
data: { test: "data" },
|
|
265
|
+
status: 200,
|
|
266
|
+
success: true,
|
|
267
|
+
} as any;
|
|
268
|
+
const req = client.createRequest<{ response: any }>()({
|
|
269
|
+
endpoint: "shared-base-endpoint",
|
|
270
|
+
disableResponseInterceptors: true,
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
const result = await interceptResponse([mockInterceptor], response, req);
|
|
274
|
+
|
|
275
|
+
expect(mockInterceptor).not.toHaveBeenCalled();
|
|
276
|
+
expect(result).toBe(response);
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("should run interceptors when not disabled", async () => {
|
|
280
|
+
const mockInterceptor = vi.fn((req) => req);
|
|
281
|
+
|
|
282
|
+
const req = client.createRequest<{ response: any }>()({
|
|
283
|
+
endpoint: "shared-base-endpoint",
|
|
284
|
+
disableRequestInterceptors: false,
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
await interceptRequest([mockInterceptor], req);
|
|
288
|
+
|
|
289
|
+
expect(mockInterceptor).toHaveBeenCalledWith(req);
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
});
|