@hyper-fetch/core 7.5.3 → 8.0.1
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 +44 -17
- package/__tests__/features/adapter/adapter.bindings.spec.ts +35 -33
- package/__tests__/features/adapter/adapter.browser.spec.ts +5 -20
- package/__tests__/features/adapter/adapter.server.spec.ts +229 -37
- package/__tests__/features/cache/cache.base.spec.ts +23 -15
- package/__tests__/features/cache/cache.events.spec.ts +9 -5
- package/__tests__/features/cache/cache.garbage-collector.spec.ts +9 -6
- package/__tests__/features/cache/cache.lazy-storage.spec.ts +11 -8
- 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 +10 -3
- package/__tests__/features/client/client.auth.spec.ts +5 -5
- package/__tests__/features/client/client.base.spec.ts +16 -4
- package/__tests__/features/client/client.from-json.spec.ts +182 -0
- package/__tests__/features/client/client.hydration.spec.ts +69 -4
- package/__tests__/features/client/client.interceptor.spec.ts +4 -4
- package/__tests__/features/client/client.methods.spec.ts +5 -4
- package/__tests__/features/client/client.middleware.spec.ts +4 -4
- package/__tests__/features/client/client.utils.spec.ts +3 -3
- package/__tests__/features/dispatcher/dispatcher.base.spec.ts +8 -8
- package/__tests__/features/dispatcher/dispatcher.custom-storage.spec.ts +189 -0
- package/__tests__/features/dispatcher/dispatcher.events.spec.ts +17 -12
- package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +31 -31
- package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +15 -15
- package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +22 -5
- package/__tests__/features/managers/app/app.manager.base.spec.ts +23 -23
- package/__tests__/features/managers/app/app.manager.ssr.spec.ts +6 -3
- package/__tests__/features/managers/logger/logger.base.spec.ts +4 -2
- package/__tests__/features/managers/logger/logger.utils.spec.ts +10 -4
- package/__tests__/features/managers/request/request.manager.base.spec.ts +21 -19
- package/__tests__/features/managers/request/request.manager.events.spec.ts +137 -53
- package/__tests__/features/mocker/mocker.base.spec.ts +82 -10
- package/__tests__/features/plugin/plugin.base.spec.ts +19 -19
- package/__tests__/features/request/request.base.spec.ts +39 -13
- package/__tests__/features/request/request.cloning.spec.ts +1 -1
- 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 +107 -15
- package/__tests__/features/request/request.setters.spec.ts +11 -4
- package/__tests__/features/request/request.typescript.spec.ts +1 -1
- package/__tests__/features/request/request.utils.spec.ts +2 -2
- package/__tests__/features/sdk/sdk.base.spec.ts +5 -3
- 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 +3 -1
- package/__tests__/shared/progress.shared.ts +2 -1
- package/__tests__/tsconfig.json +14 -3
- 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 +3 -2
- package/__tests__/utils/cache.utils.ts +3 -2
- package/__tests__/utils/callbacks.utils.ts +5 -4
- package/__tests__/utils/dispatcher.utils.ts +3 -2
- 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 +7 -30
- 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 +149 -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/__tests__/global.d.ts +0 -1
- package/__tests__/jest.setup.ts +0 -13
- package/dist/browser/index.js +0 -3751
- package/dist/browser/index.js.map +0 -7
- package/dist/index.js +0 -4002
- package/dist/index.js.map +0 -7
- package/jest.config.ts +0 -6
- package/src/http-adapter/http-adapter.browser.ts +0 -102
- package/src/http-adapter/http-adapter.server.ts +0 -149
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment node
|
|
3
|
+
*/
|
|
4
|
+
import { Client } from "@hyper-fetch/core";
|
|
5
|
+
import { createE2EServer, echoHandler, jsonHandler, statusHandler, headHandler } from "@hyper-fetch/testing";
|
|
6
|
+
|
|
7
|
+
const { route, startServer, stopServer } = createE2EServer();
|
|
8
|
+
|
|
9
|
+
let baseUrl: string;
|
|
10
|
+
|
|
11
|
+
beforeAll(async () => {
|
|
12
|
+
// HTTP Methods
|
|
13
|
+
route("GET", "/users", jsonHandler([{ id: 1, name: "John" }]));
|
|
14
|
+
route("POST", "/users", echoHandler);
|
|
15
|
+
route("PUT", "/users/:id", echoHandler);
|
|
16
|
+
route("PATCH", "/users/:id", echoHandler);
|
|
17
|
+
route("DELETE", "/users/:id", jsonHandler({ deleted: true }));
|
|
18
|
+
route("HEAD", "/users", headHandler({ "X-Total-Count": "42" }));
|
|
19
|
+
|
|
20
|
+
// Status Codes
|
|
21
|
+
route("GET", "/status/200", statusHandler(200, { ok: true }));
|
|
22
|
+
route("GET", "/status/201", statusHandler(201, { created: true }));
|
|
23
|
+
route("GET", "/status/204", statusHandler(204));
|
|
24
|
+
route("GET", "/status/400", statusHandler(400, { message: "Bad Request" }));
|
|
25
|
+
route("GET", "/status/401", statusHandler(401, { message: "Unauthorized" }));
|
|
26
|
+
route("GET", "/status/403", statusHandler(403, { message: "Forbidden" }));
|
|
27
|
+
route("GET", "/status/404", statusHandler(404, { message: "Not Found" }));
|
|
28
|
+
route("GET", "/status/500", statusHandler(500, { message: "Internal Server Error" }));
|
|
29
|
+
|
|
30
|
+
// Headers
|
|
31
|
+
route("GET", "/echo-headers", echoHandler);
|
|
32
|
+
route("GET", "/custom-response-headers", (_req, res) => {
|
|
33
|
+
res.writeHead(200, {
|
|
34
|
+
"Content-Type": "application/json",
|
|
35
|
+
"X-Custom-Header": "custom-value",
|
|
36
|
+
"X-Request-Id": "abc-123",
|
|
37
|
+
});
|
|
38
|
+
res.end(JSON.stringify({ ok: true }));
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
baseUrl = await startServer();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
afterAll(async () => {
|
|
45
|
+
await stopServer();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe("E2E [ HTTP Methods ]", () => {
|
|
49
|
+
it("should perform GET request", async () => {
|
|
50
|
+
const client = new Client({ url: baseUrl });
|
|
51
|
+
const request = client.createRequest<{ response: { id: number; name: string }[] }>()({
|
|
52
|
+
endpoint: "/users",
|
|
53
|
+
method: "GET",
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const { data, error, status } = await request.send();
|
|
57
|
+
|
|
58
|
+
expect(error).toBeNull();
|
|
59
|
+
expect(status).toBe(200);
|
|
60
|
+
expect(data).toStrictEqual([{ id: 1, name: "John" }]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should perform POST request with JSON payload", async () => {
|
|
64
|
+
const client = new Client({ url: baseUrl });
|
|
65
|
+
const request = client.createRequest<{
|
|
66
|
+
response: { method: string; body: { username: string } };
|
|
67
|
+
payload: { username: string };
|
|
68
|
+
}>()({
|
|
69
|
+
endpoint: "/users",
|
|
70
|
+
method: "POST",
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const { data, error, status } = await request.send({ payload: { username: "Jane" } });
|
|
74
|
+
|
|
75
|
+
expect(error).toBeNull();
|
|
76
|
+
expect(status).toBe(200);
|
|
77
|
+
expect(data!.method).toBe("POST");
|
|
78
|
+
expect(data!.body).toStrictEqual({ username: "Jane" });
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("should perform PUT request with JSON payload", async () => {
|
|
82
|
+
const client = new Client({ url: baseUrl });
|
|
83
|
+
const request = client.createRequest<{
|
|
84
|
+
response: { method: string; body: { name: string } };
|
|
85
|
+
payload: { name: string };
|
|
86
|
+
}>()({
|
|
87
|
+
endpoint: "/users/:id",
|
|
88
|
+
method: "PUT",
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const { data, error, status } = await request.setParams({ id: "1" }).send({ payload: { name: "Updated" } });
|
|
92
|
+
|
|
93
|
+
expect(error).toBeNull();
|
|
94
|
+
expect(status).toBe(200);
|
|
95
|
+
expect(data!.method).toBe("PUT");
|
|
96
|
+
expect(data!.body).toStrictEqual({ name: "Updated" });
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("should perform PATCH request with JSON payload", async () => {
|
|
100
|
+
const client = new Client({ url: baseUrl });
|
|
101
|
+
const request = client.createRequest<{
|
|
102
|
+
response: { method: string; body: { name: string } };
|
|
103
|
+
payload: { name: string };
|
|
104
|
+
}>()({
|
|
105
|
+
endpoint: "/users/:id",
|
|
106
|
+
method: "PATCH",
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const { data, error, status } = await request.setParams({ id: "1" }).send({ payload: { name: "Patched" } });
|
|
110
|
+
|
|
111
|
+
expect(error).toBeNull();
|
|
112
|
+
expect(status).toBe(200);
|
|
113
|
+
expect(data!.method).toBe("PATCH");
|
|
114
|
+
expect(data!.body).toStrictEqual({ name: "Patched" });
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("should perform DELETE request", async () => {
|
|
118
|
+
const client = new Client({ url: baseUrl });
|
|
119
|
+
const request = client.createRequest<{ response: { deleted: boolean } }>()({
|
|
120
|
+
endpoint: "/users/:id",
|
|
121
|
+
method: "DELETE",
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const { data, error, status } = await request.setParams({ id: "1" }).send();
|
|
125
|
+
|
|
126
|
+
expect(error).toBeNull();
|
|
127
|
+
expect(status).toBe(200);
|
|
128
|
+
expect(data).toStrictEqual({ deleted: true });
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("should perform HEAD request", async () => {
|
|
132
|
+
const client = new Client({ url: baseUrl });
|
|
133
|
+
const request = client.createRequest<{ response: null }>()({
|
|
134
|
+
endpoint: "/users",
|
|
135
|
+
method: "HEAD" as any,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const { error, status, extra } = await request.send();
|
|
139
|
+
|
|
140
|
+
expect(error).toBeNull();
|
|
141
|
+
expect(status).toBe(200);
|
|
142
|
+
expect(extra!.headers["x-total-count"]).toBe("42");
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe("E2E [ Status Codes ]", () => {
|
|
147
|
+
const testSuccess = async (path: string, expectedStatus: number, expectedData?: unknown) => {
|
|
148
|
+
const client = new Client({ url: baseUrl });
|
|
149
|
+
const request = client.createRequest<{ response: any }>()({
|
|
150
|
+
endpoint: path,
|
|
151
|
+
method: "GET",
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
const { data, error, status } = await request.send();
|
|
155
|
+
|
|
156
|
+
expect(error).toBeNull();
|
|
157
|
+
expect(status).toBe(expectedStatus);
|
|
158
|
+
if (expectedData !== undefined) {
|
|
159
|
+
expect(data).toStrictEqual(expectedData);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const testError = async (path: string, expectedStatus: number, expectedError?: unknown) => {
|
|
164
|
+
const client = new Client({ url: baseUrl });
|
|
165
|
+
const request = client.createRequest<{ response: any }>()({
|
|
166
|
+
endpoint: path,
|
|
167
|
+
method: "GET",
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
const { data, error, status } = await request.send();
|
|
171
|
+
|
|
172
|
+
expect(data).toBeNull();
|
|
173
|
+
expect(status).toBe(expectedStatus);
|
|
174
|
+
if (expectedError !== undefined) {
|
|
175
|
+
expect(error).toStrictEqual(expectedError);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
it("should handle 200 OK", async () => {
|
|
180
|
+
await testSuccess("/status/200", 200, { ok: true });
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("should handle 201 Created", async () => {
|
|
184
|
+
await testSuccess("/status/201", 201, { created: true });
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("should handle 204 No Content", async () => {
|
|
188
|
+
await testSuccess("/status/204", 204);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("should handle 400 Bad Request", async () => {
|
|
192
|
+
await testError("/status/400", 400, { message: "Bad Request" });
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("should handle 401 Unauthorized", async () => {
|
|
196
|
+
await testError("/status/401", 401, { message: "Unauthorized" });
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("should handle 403 Forbidden", async () => {
|
|
200
|
+
await testError("/status/403", 403, { message: "Forbidden" });
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("should handle 404 Not Found", async () => {
|
|
204
|
+
await testError("/status/404", 404, { message: "Not Found" });
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("should handle 500 Internal Server Error", async () => {
|
|
208
|
+
await testError("/status/500", 500, { message: "Internal Server Error" });
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe("E2E [ Headers ]", () => {
|
|
213
|
+
it("should send custom request headers", async () => {
|
|
214
|
+
const client = new Client({ url: baseUrl });
|
|
215
|
+
const request = client
|
|
216
|
+
.createRequest<{ response: { headers: Record<string, string> } }>()({
|
|
217
|
+
endpoint: "/echo-headers",
|
|
218
|
+
method: "GET",
|
|
219
|
+
})
|
|
220
|
+
.setHeaders({ "X-Api-Key": "test-key", Authorization: "Bearer token123" });
|
|
221
|
+
|
|
222
|
+
const { data, error } = await request.send();
|
|
223
|
+
|
|
224
|
+
expect(error).toBeNull();
|
|
225
|
+
expect(data!.headers["x-api-key"]).toBe("test-key");
|
|
226
|
+
expect(data!.headers.authorization).toBe("Bearer token123");
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("should receive custom response headers", async () => {
|
|
230
|
+
const client = new Client({ url: baseUrl });
|
|
231
|
+
const request = client.createRequest<{ response: { ok: boolean } }>()({
|
|
232
|
+
endpoint: "/custom-response-headers",
|
|
233
|
+
method: "GET",
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
const { data, error, extra } = await request.send();
|
|
237
|
+
|
|
238
|
+
expect(error).toBeNull();
|
|
239
|
+
expect(data).toStrictEqual({ ok: true });
|
|
240
|
+
expect(extra!.headers["x-custom-header"]).toBe("custom-value");
|
|
241
|
+
expect(extra!.headers["x-request-id"]).toBe("abc-123");
|
|
242
|
+
});
|
|
243
|
+
});
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment node
|
|
3
|
+
*/
|
|
4
|
+
import { Client } from "@hyper-fetch/core";
|
|
5
|
+
import { createE2EServer, echoHandler } from "@hyper-fetch/testing";
|
|
6
|
+
|
|
7
|
+
const { route, startServer, stopServer } = createE2EServer();
|
|
8
|
+
|
|
9
|
+
describe("E2E [ Payloads ]", () => {
|
|
10
|
+
let baseUrl: string;
|
|
11
|
+
|
|
12
|
+
beforeAll(async () => {
|
|
13
|
+
route("POST", "/echo", echoHandler);
|
|
14
|
+
route("PUT", "/echo", echoHandler);
|
|
15
|
+
route("PATCH", "/echo", echoHandler);
|
|
16
|
+
|
|
17
|
+
baseUrl = await startServer();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterAll(async () => {
|
|
21
|
+
await stopServer();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("should send JSON object payload", async () => {
|
|
25
|
+
const client = new Client({ url: baseUrl });
|
|
26
|
+
const request = client.createRequest<{
|
|
27
|
+
response: { body: { name: string; age: number } };
|
|
28
|
+
payload: { name: string; age: number };
|
|
29
|
+
}>()({
|
|
30
|
+
endpoint: "/echo",
|
|
31
|
+
method: "POST",
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const { data, error, status } = await request.send({ payload: { name: "John", age: 30 } });
|
|
35
|
+
|
|
36
|
+
expect(error).toBeNull();
|
|
37
|
+
expect(status).toBe(200);
|
|
38
|
+
expect(data!.body).toStrictEqual({ name: "John", age: 30 });
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should send nested JSON payload", async () => {
|
|
42
|
+
const client = new Client({ url: baseUrl });
|
|
43
|
+
const payload = {
|
|
44
|
+
user: {
|
|
45
|
+
name: "John",
|
|
46
|
+
address: {
|
|
47
|
+
city: "NYC",
|
|
48
|
+
zip: "10001",
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
tags: ["admin", "user"],
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const request = client.createRequest<{
|
|
55
|
+
response: { body: typeof payload };
|
|
56
|
+
payload: typeof payload;
|
|
57
|
+
}>()({
|
|
58
|
+
endpoint: "/echo",
|
|
59
|
+
method: "POST",
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const { data, error } = await request.send({ payload });
|
|
63
|
+
|
|
64
|
+
expect(error).toBeNull();
|
|
65
|
+
expect(data!.body).toStrictEqual(payload);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("should send string payload", async () => {
|
|
69
|
+
const client = new Client({ url: baseUrl });
|
|
70
|
+
const request = client.createRequest<{
|
|
71
|
+
response: { body: string };
|
|
72
|
+
payload: string;
|
|
73
|
+
}>()({
|
|
74
|
+
endpoint: "/echo",
|
|
75
|
+
method: "POST",
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const { data, error } = await request.send({ payload: "raw-string-payload" });
|
|
79
|
+
|
|
80
|
+
expect(error).toBeNull();
|
|
81
|
+
expect(data!.body).toBe("raw-string-payload");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("should send empty payload on POST", async () => {
|
|
85
|
+
const client = new Client({ url: baseUrl });
|
|
86
|
+
const request = client.createRequest<{
|
|
87
|
+
response: { body: any; method: string };
|
|
88
|
+
payload: Record<string, never>;
|
|
89
|
+
}>()({
|
|
90
|
+
endpoint: "/echo",
|
|
91
|
+
method: "POST",
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const { data, error, status } = await request.send({ payload: {} });
|
|
95
|
+
|
|
96
|
+
expect(error).toBeNull();
|
|
97
|
+
expect(status).toBe(200);
|
|
98
|
+
expect(data!.method).toBe("POST");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("should send payload via PUT", async () => {
|
|
102
|
+
const client = new Client({ url: baseUrl });
|
|
103
|
+
const request = client.createRequest<{
|
|
104
|
+
response: { body: { updated: boolean }; method: string };
|
|
105
|
+
payload: { updated: boolean };
|
|
106
|
+
}>()({
|
|
107
|
+
endpoint: "/echo",
|
|
108
|
+
method: "PUT",
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const { data, error } = await request.send({ payload: { updated: true } });
|
|
112
|
+
|
|
113
|
+
expect(error).toBeNull();
|
|
114
|
+
expect(data!.method).toBe("PUT");
|
|
115
|
+
expect(data!.body).toStrictEqual({ updated: true });
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("should send payload via PATCH", async () => {
|
|
119
|
+
const client = new Client({ url: baseUrl });
|
|
120
|
+
const request = client.createRequest<{
|
|
121
|
+
response: { body: { partial: string }; method: string };
|
|
122
|
+
payload: { partial: string };
|
|
123
|
+
}>()({
|
|
124
|
+
endpoint: "/echo",
|
|
125
|
+
method: "PATCH",
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const { data, error } = await request.send({ payload: { partial: "update" } });
|
|
129
|
+
|
|
130
|
+
expect(error).toBeNull();
|
|
131
|
+
expect(data!.method).toBe("PATCH");
|
|
132
|
+
expect(data!.body).toStrictEqual({ partial: "update" });
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("should send large JSON payload", async () => {
|
|
136
|
+
const client = new Client({ url: baseUrl });
|
|
137
|
+
const largeArray = Array.from({ length: 1000 }, (_, i) => ({ id: i, value: `item-${i}` }));
|
|
138
|
+
|
|
139
|
+
const request = client.createRequest<{
|
|
140
|
+
response: { body: typeof largeArray };
|
|
141
|
+
payload: typeof largeArray;
|
|
142
|
+
}>()({
|
|
143
|
+
endpoint: "/echo",
|
|
144
|
+
method: "POST",
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const { data, error } = await request.send({ payload: largeArray });
|
|
148
|
+
|
|
149
|
+
expect(error).toBeNull();
|
|
150
|
+
expect(data!.body).toHaveLength(1000);
|
|
151
|
+
expect(data!.body[0]).toStrictEqual({ id: 0, value: "item-0" });
|
|
152
|
+
expect(data!.body[999]).toStrictEqual({ id: 999, value: "item-999" });
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Client, createClient } from "client";
|
|
2
|
-
import {
|
|
2
|
+
import type { AdapterInstance } from "adapter";
|
|
3
|
+
import { Adapter, RequestProcessingError, getAdapterHeaders, getAdapterPayload } from "adapter";
|
|
3
4
|
import { HttpMethods } from "../../../src/constants/http.constants";
|
|
4
|
-
import { RequestInstance } from "request";
|
|
5
5
|
|
|
6
6
|
describe("Adapter [ Base ]", () => {
|
|
7
7
|
let adapter: AdapterInstance;
|
|
@@ -41,7 +41,7 @@ describe("Adapter [ Base ]", () => {
|
|
|
41
41
|
expect(adapter.unstable_payloadMapperConfig).toBe(config);
|
|
42
42
|
});
|
|
43
43
|
it("should set devtools endpoint getter", () => {
|
|
44
|
-
const mapper = (
|
|
44
|
+
const mapper = (endpoint: string) => endpoint;
|
|
45
45
|
adapter.setDevtoolsEndpointGetter(mapper);
|
|
46
46
|
expect(adapter.unstable_devtoolsEndpointGetter).toBe(mapper);
|
|
47
47
|
});
|
|
@@ -49,7 +49,7 @@ describe("Adapter [ Base ]", () => {
|
|
|
49
49
|
|
|
50
50
|
describe("when using onInitialize", () => {
|
|
51
51
|
it("should store and execute initialization callback", () => {
|
|
52
|
-
const spy =
|
|
52
|
+
const spy = vi.fn();
|
|
53
53
|
const client = new Client({ url: "test" });
|
|
54
54
|
|
|
55
55
|
adapter.onInitialize(spy);
|
|
@@ -97,7 +97,7 @@ describe("Adapter [ Base ]", () => {
|
|
|
97
97
|
|
|
98
98
|
describe("when using mapper setters", () => {
|
|
99
99
|
it("should set header mapper with config", () => {
|
|
100
|
-
const headerMapper =
|
|
100
|
+
const headerMapper = vi.fn();
|
|
101
101
|
const config = { test: true };
|
|
102
102
|
|
|
103
103
|
adapter.setHeaderMapperConfig(config);
|
|
@@ -108,7 +108,7 @@ describe("Adapter [ Base ]", () => {
|
|
|
108
108
|
});
|
|
109
109
|
|
|
110
110
|
it("should set payload mapper with config", () => {
|
|
111
|
-
const payloadMapper =
|
|
111
|
+
const payloadMapper = vi.fn();
|
|
112
112
|
const config = { test: true };
|
|
113
113
|
|
|
114
114
|
adapter.setPayloadMapperConfig(config);
|
|
@@ -119,7 +119,7 @@ describe("Adapter [ Base ]", () => {
|
|
|
119
119
|
});
|
|
120
120
|
|
|
121
121
|
it("should set endpoint mapper with config", () => {
|
|
122
|
-
const endpointMapper =
|
|
122
|
+
const endpointMapper = vi.fn();
|
|
123
123
|
const config = { test: true };
|
|
124
124
|
|
|
125
125
|
adapter.setEndpointMapperConfig(config);
|
|
@@ -130,7 +130,7 @@ describe("Adapter [ Base ]", () => {
|
|
|
130
130
|
});
|
|
131
131
|
|
|
132
132
|
it("should set query params mapper with config", () => {
|
|
133
|
-
const queryParamsMapper =
|
|
133
|
+
const queryParamsMapper = vi.fn();
|
|
134
134
|
const config = { test: true };
|
|
135
135
|
|
|
136
136
|
adapter.setQueryParamsMapperConfig(config);
|
|
@@ -144,7 +144,7 @@ describe("Adapter [ Base ]", () => {
|
|
|
144
144
|
describe("when using defaults setters", () => {
|
|
145
145
|
it("should set request defaults", () => {
|
|
146
146
|
const defaults = { method: HttpMethods.POST };
|
|
147
|
-
const callback =
|
|
147
|
+
const callback = vi.fn().mockReturnValue(defaults);
|
|
148
148
|
|
|
149
149
|
adapter.setRequestDefaults(callback);
|
|
150
150
|
const result = adapter.unstable_getRequestDefaults?.({} as any);
|
|
@@ -155,7 +155,7 @@ describe("Adapter [ Base ]", () => {
|
|
|
155
155
|
|
|
156
156
|
it("should set adapter defaults", () => {
|
|
157
157
|
const defaults = { timeout: 5000 };
|
|
158
|
-
const callback =
|
|
158
|
+
const callback = vi.fn().mockReturnValue(defaults);
|
|
159
159
|
|
|
160
160
|
adapter.setAdapterDefaults(callback);
|
|
161
161
|
const result = adapter.unstable_getAdapterDefaults?.({} as any);
|
|
@@ -166,7 +166,7 @@ describe("Adapter [ Base ]", () => {
|
|
|
166
166
|
|
|
167
167
|
it("should set internal error mapping callback", () => {
|
|
168
168
|
const errorMapping = (error: any) => ({ customError: error });
|
|
169
|
-
const callback =
|
|
169
|
+
const callback = vi.fn().mockImplementation(errorMapping);
|
|
170
170
|
|
|
171
171
|
adapter.setInternalErrorMapping(callback);
|
|
172
172
|
|
|
@@ -174,6 +174,33 @@ describe("Adapter [ Base ]", () => {
|
|
|
174
174
|
});
|
|
175
175
|
});
|
|
176
176
|
|
|
177
|
+
describe("when using default unstable_devtoolsEndpointGetter", () => {
|
|
178
|
+
it("should return the endpoint unchanged by default", () => {
|
|
179
|
+
const result = adapter.unstable_devtoolsEndpointGetter("/test/endpoint");
|
|
180
|
+
expect(result).toBe("/test/endpoint");
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
describe("when using unstable_getRequestDefaults", () => {
|
|
185
|
+
it("should allow setting and calling unstable_getRequestDefaults", () => {
|
|
186
|
+
const defaults = { method: HttpMethods.POST };
|
|
187
|
+
const callback = vi.fn().mockReturnValue(defaults);
|
|
188
|
+
|
|
189
|
+
adapter.setRequestDefaults(callback);
|
|
190
|
+
expect(adapter.unstable_getRequestDefaults).toBeDefined();
|
|
191
|
+
|
|
192
|
+
const result = adapter.unstable_getRequestDefaults!({
|
|
193
|
+
endpoint: "/test",
|
|
194
|
+
} as any);
|
|
195
|
+
expect(callback).toHaveBeenCalledWith({ endpoint: "/test" });
|
|
196
|
+
expect(result).toBe(defaults);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("should be undefined by default", () => {
|
|
200
|
+
expect(adapter.unstable_getRequestDefaults).toBeUndefined();
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
177
204
|
describe("when checking default mappers", () => {
|
|
178
205
|
it("should have default header mapper", () => {
|
|
179
206
|
expect(adapter.unstable_headerMapper).toBe(getAdapterHeaders);
|
|
@@ -202,7 +229,7 @@ describe("Adapter [ Base ]", () => {
|
|
|
202
229
|
// hack to prevent logger from being missed (not initialized)
|
|
203
230
|
newAdapter.logger = client.loggerManager.initialize(client, "Adapter");
|
|
204
231
|
|
|
205
|
-
const data = await newAdapter.fetch(request, "test");
|
|
232
|
+
const data = await newAdapter.fetch(request as any, "test");
|
|
206
233
|
|
|
207
234
|
expect(data.error.message).toStrictEqual(
|
|
208
235
|
new RequestProcessingError("Adapter test-adapter is not initialized").message,
|
|
@@ -237,7 +264,7 @@ describe("Adapter [ Base ]", () => {
|
|
|
237
264
|
});
|
|
238
265
|
|
|
239
266
|
it("should execute fetcher with adapter bindings", async () => {
|
|
240
|
-
const fetcherSpy =
|
|
267
|
+
const fetcherSpy = vi.fn().mockImplementation(({ onSuccess }) => {
|
|
241
268
|
onSuccess({
|
|
242
269
|
data: {},
|
|
243
270
|
status: 200,
|
|
@@ -271,7 +298,7 @@ describe("Adapter [ Base ]", () => {
|
|
|
271
298
|
});
|
|
272
299
|
|
|
273
300
|
it("should trigger plugins on adapter fetch", async () => {
|
|
274
|
-
const fetcherSpy =
|
|
301
|
+
const fetcherSpy = vi.fn().mockImplementation(({ onSuccess }) => {
|
|
275
302
|
onSuccess({
|
|
276
303
|
data: {},
|
|
277
304
|
status: 200,
|
|
@@ -281,7 +308,7 @@ describe("Adapter [ Base ]", () => {
|
|
|
281
308
|
});
|
|
282
309
|
|
|
283
310
|
const client = new Client({ url: "test" });
|
|
284
|
-
const pluginSpy =
|
|
311
|
+
const pluginSpy = vi.fn();
|
|
285
312
|
client.triggerPlugins = pluginSpy;
|
|
286
313
|
|
|
287
314
|
const newAdapter = new Adapter({
|
|
@@ -330,7 +357,7 @@ describe("Adapter [ Base ]", () => {
|
|
|
330
357
|
|
|
331
358
|
it("should handle errors during fetch execution", async () => {
|
|
332
359
|
const testError = new Error("Test error");
|
|
333
|
-
const fetcherSpy =
|
|
360
|
+
const fetcherSpy = vi.fn().mockImplementation(() => {
|
|
334
361
|
throw testError;
|
|
335
362
|
});
|
|
336
363
|
|
|
@@ -361,7 +388,7 @@ describe("Adapter [ Base ]", () => {
|
|
|
361
388
|
});
|
|
362
389
|
|
|
363
390
|
it("should use current timestamp if startTime is not set during error", async () => {
|
|
364
|
-
const fetcherSpy =
|
|
391
|
+
const fetcherSpy = vi.fn().mockImplementation(() => {
|
|
365
392
|
throw new Error("Test error");
|
|
366
393
|
});
|
|
367
394
|
|