@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
package/.eslintrc.cjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
extends: ["plugin:@nx/react", "../../.eslintrc.js"],
|
|
3
|
+
parserOptions: {
|
|
4
|
+
tsconfigRootDir: __dirname,
|
|
5
|
+
project: ["./tsconfig.json", "./__tests__/tsconfig.json"],
|
|
6
|
+
},
|
|
7
|
+
rules: {
|
|
8
|
+
"react/jsx-props-no-spreading": 0,
|
|
9
|
+
"react/require-default-props": 0,
|
|
10
|
+
},
|
|
11
|
+
ignorePatterns: ["dist/", "coverage/", "vite.config.ts", "/*.js"],
|
|
12
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment node
|
|
3
|
+
*/
|
|
4
|
+
import { Client } from "@hyper-fetch/core";
|
|
5
|
+
import { createE2EServer, delayHandler, jsonHandler } from "@hyper-fetch/testing";
|
|
6
|
+
|
|
7
|
+
const { route, startServer, stopServer } = createE2EServer();
|
|
8
|
+
|
|
9
|
+
describe("E2E [ Abort & Timeout ]", () => {
|
|
10
|
+
let baseUrl: string;
|
|
11
|
+
|
|
12
|
+
beforeAll(async () => {
|
|
13
|
+
route("GET", "/slow", delayHandler(2000, { result: "slow" }));
|
|
14
|
+
route("GET", "/fast", jsonHandler({ result: "fast" }));
|
|
15
|
+
|
|
16
|
+
baseUrl = await startServer();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
afterAll(async () => {
|
|
20
|
+
await stopServer();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("should abort a request mid-flight", async () => {
|
|
24
|
+
const client = new Client({ url: baseUrl });
|
|
25
|
+
const request = client.createRequest<{ response: { result: string } }>()({
|
|
26
|
+
endpoint: "/slow",
|
|
27
|
+
method: "GET",
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const promise = request.send();
|
|
31
|
+
|
|
32
|
+
setTimeout(() => {
|
|
33
|
+
request.abort();
|
|
34
|
+
}, 50);
|
|
35
|
+
|
|
36
|
+
const { data, status } = await promise;
|
|
37
|
+
|
|
38
|
+
expect(data).toBeNull();
|
|
39
|
+
expect(status).toBe(0);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should timeout when server takes too long", async () => {
|
|
43
|
+
const client = new Client({ url: baseUrl });
|
|
44
|
+
const request = client
|
|
45
|
+
.createRequest<{ response: { result: string } }>()({
|
|
46
|
+
endpoint: "/slow",
|
|
47
|
+
method: "GET",
|
|
48
|
+
})
|
|
49
|
+
.setOptions({ timeout: 50 });
|
|
50
|
+
|
|
51
|
+
const { data, error, status } = await request.send();
|
|
52
|
+
|
|
53
|
+
expect(data).toBeNull();
|
|
54
|
+
expect(status).toBe(0);
|
|
55
|
+
expect(error).toBeDefined();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("should complete fast request before timeout", async () => {
|
|
59
|
+
const client = new Client({ url: baseUrl });
|
|
60
|
+
const request = client
|
|
61
|
+
.createRequest<{ response: { result: string } }>()({
|
|
62
|
+
endpoint: "/fast",
|
|
63
|
+
method: "GET",
|
|
64
|
+
})
|
|
65
|
+
.setOptions({ timeout: 5000 });
|
|
66
|
+
|
|
67
|
+
const { data, error, status } = await request.send();
|
|
68
|
+
|
|
69
|
+
expect(error).toBeNull();
|
|
70
|
+
expect(status).toBe(200);
|
|
71
|
+
expect(data).toStrictEqual({ result: "fast" });
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("should abort one request without affecting another", async () => {
|
|
75
|
+
const client = new Client({ url: baseUrl });
|
|
76
|
+
const slowRequest = client.createRequest<{ response: { result: string } }>()({
|
|
77
|
+
endpoint: "/slow",
|
|
78
|
+
method: "GET",
|
|
79
|
+
cancelable: true,
|
|
80
|
+
});
|
|
81
|
+
const fastRequest = client.createRequest<{ response: { result: string } }>()({
|
|
82
|
+
endpoint: "/fast",
|
|
83
|
+
method: "GET",
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const slowPromise = slowRequest.send();
|
|
87
|
+
|
|
88
|
+
setTimeout(() => {
|
|
89
|
+
slowRequest.abort();
|
|
90
|
+
}, 50);
|
|
91
|
+
|
|
92
|
+
const [slowResult, fastResult] = await Promise.all([slowPromise, fastRequest.send()]);
|
|
93
|
+
|
|
94
|
+
expect(slowResult.data).toBeNull();
|
|
95
|
+
expect(slowResult.status).toBe(0);
|
|
96
|
+
|
|
97
|
+
expect(fastResult.error).toBeNull();
|
|
98
|
+
expect(fastResult.status).toBe(200);
|
|
99
|
+
expect(fastResult.data).toStrictEqual({ result: "fast" });
|
|
100
|
+
});
|
|
101
|
+
});
|
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment node
|
|
3
|
+
*/
|
|
4
|
+
import { URL } from "url";
|
|
5
|
+
import { Client } from "@hyper-fetch/core";
|
|
6
|
+
import type { RouteHandler } from "@hyper-fetch/testing";
|
|
7
|
+
import {
|
|
8
|
+
createE2EServer,
|
|
9
|
+
jsonHandler,
|
|
10
|
+
redirectHandler,
|
|
11
|
+
streamHandler,
|
|
12
|
+
fileDownloadHandler,
|
|
13
|
+
echoHandler,
|
|
14
|
+
} from "@hyper-fetch/testing";
|
|
15
|
+
|
|
16
|
+
const { route, startServer, stopServer } = createE2EServer();
|
|
17
|
+
|
|
18
|
+
let baseUrl: string;
|
|
19
|
+
|
|
20
|
+
beforeAll(async () => {
|
|
21
|
+
// Query Params
|
|
22
|
+
route("GET", "/search", (req, res) => {
|
|
23
|
+
const url = new URL(req.url || "/", `http://localhost`);
|
|
24
|
+
const params: Record<string, string> = {};
|
|
25
|
+
url.searchParams.forEach((value: string, key: string) => {
|
|
26
|
+
params[key] = value;
|
|
27
|
+
});
|
|
28
|
+
const body = JSON.stringify({ params });
|
|
29
|
+
res.writeHead(200, { "Content-Type": "application/json", "Content-Length": Buffer.byteLength(body).toString() });
|
|
30
|
+
res.end(body);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// URL Params
|
|
34
|
+
// eslint-disable-next-line max-params
|
|
35
|
+
const usersHandler: RouteHandler = (req, res, _body, params) => {
|
|
36
|
+
const responseBody = JSON.stringify({ id: Number(params.id), name: params.id === "1" ? "Alice" : "Bob" });
|
|
37
|
+
res.writeHead(200, {
|
|
38
|
+
"Content-Type": "application/json",
|
|
39
|
+
"Content-Length": Buffer.byteLength(responseBody).toString(),
|
|
40
|
+
});
|
|
41
|
+
res.end(responseBody);
|
|
42
|
+
};
|
|
43
|
+
route("GET", "/users/:id", usersHandler);
|
|
44
|
+
|
|
45
|
+
// eslint-disable-next-line max-params
|
|
46
|
+
const orgsHandler: RouteHandler = (_req, res, _body, params) => {
|
|
47
|
+
const responseBody = JSON.stringify({ org: params.orgId, repo: params.repoId });
|
|
48
|
+
res.writeHead(200, {
|
|
49
|
+
"Content-Type": "application/json",
|
|
50
|
+
"Content-Length": Buffer.byteLength(responseBody).toString(),
|
|
51
|
+
});
|
|
52
|
+
res.end(responseBody);
|
|
53
|
+
};
|
|
54
|
+
route("GET", "/orgs/:orgId/repos/:repoId", orgsHandler);
|
|
55
|
+
|
|
56
|
+
// Redirects
|
|
57
|
+
route("GET", "/redirect-302", redirectHandler("/final"));
|
|
58
|
+
route("GET", "/redirect-301", redirectHandler("/final", 301));
|
|
59
|
+
route("GET", "/redirect-chain", redirectHandler("/redirect-302"));
|
|
60
|
+
route("GET", "/final", jsonHandler({ redirected: true }));
|
|
61
|
+
|
|
62
|
+
// Streaming
|
|
63
|
+
route("GET", "/stream", streamHandler(["chunk1", "chunk2", "chunk3"], 30));
|
|
64
|
+
route(
|
|
65
|
+
"GET",
|
|
66
|
+
"/stream-large",
|
|
67
|
+
streamHandler(
|
|
68
|
+
Array.from({ length: 20 }, (_, i) => `data-${i}\n`),
|
|
69
|
+
10,
|
|
70
|
+
),
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
// File Download
|
|
74
|
+
const fileContent = Buffer.from("Hello, this is a test file content!");
|
|
75
|
+
route("GET", "/download/test.txt", fileDownloadHandler(fileContent, "test.txt", "text/plain"));
|
|
76
|
+
const binaryContent = Buffer.alloc(1024, 0xab);
|
|
77
|
+
route("GET", "/download/binary.bin", fileDownloadHandler(binaryContent, "binary.bin"));
|
|
78
|
+
|
|
79
|
+
// Concurrent
|
|
80
|
+
route("GET", "/item/1", jsonHandler({ id: 1 }));
|
|
81
|
+
route("GET", "/item/2", jsonHandler({ id: 2 }));
|
|
82
|
+
route("GET", "/item/3", jsonHandler({ id: 3 }));
|
|
83
|
+
|
|
84
|
+
// Callbacks
|
|
85
|
+
route("POST", "/callback-test", echoHandler);
|
|
86
|
+
|
|
87
|
+
// Validation
|
|
88
|
+
route("POST", "/validate", (req, res, body) => {
|
|
89
|
+
try {
|
|
90
|
+
const parsed = JSON.parse(body.toString());
|
|
91
|
+
if (!parsed.email) {
|
|
92
|
+
const errBody = JSON.stringify({ errors: { email: "Email is required" } });
|
|
93
|
+
res.writeHead(422, {
|
|
94
|
+
"Content-Type": "application/json",
|
|
95
|
+
"Content-Length": Buffer.byteLength(errBody).toString(),
|
|
96
|
+
});
|
|
97
|
+
res.end(errBody);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const okBody = JSON.stringify({ success: true });
|
|
101
|
+
res.writeHead(200, {
|
|
102
|
+
"Content-Type": "application/json",
|
|
103
|
+
"Content-Length": Buffer.byteLength(okBody).toString(),
|
|
104
|
+
});
|
|
105
|
+
res.end(okBody);
|
|
106
|
+
} catch {
|
|
107
|
+
const errBody = JSON.stringify({ message: "Invalid JSON" });
|
|
108
|
+
res.writeHead(400, {
|
|
109
|
+
"Content-Type": "application/json",
|
|
110
|
+
"Content-Length": Buffer.byteLength(errBody).toString(),
|
|
111
|
+
});
|
|
112
|
+
res.end(errBody);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
baseUrl = await startServer();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
afterAll(async () => {
|
|
120
|
+
await stopServer();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe("E2E [ Query Params ]", () => {
|
|
124
|
+
it("should send query params", async () => {
|
|
125
|
+
const client = new Client({ url: baseUrl });
|
|
126
|
+
const request = client
|
|
127
|
+
.createRequest<{
|
|
128
|
+
response: { params: Record<string, string> };
|
|
129
|
+
queryParams: { q: string; page: string };
|
|
130
|
+
}>()({
|
|
131
|
+
endpoint: "/search",
|
|
132
|
+
method: "GET",
|
|
133
|
+
})
|
|
134
|
+
.setQueryParams({ q: "hyper-fetch", page: "1" });
|
|
135
|
+
|
|
136
|
+
const { data, error } = await request.send();
|
|
137
|
+
|
|
138
|
+
expect(error).toBeNull();
|
|
139
|
+
expect(data!.params.q).toBe("hyper-fetch");
|
|
140
|
+
expect(data!.params.page).toBe("1");
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("should handle special characters in query params", async () => {
|
|
144
|
+
const client = new Client({ url: baseUrl });
|
|
145
|
+
const request = client
|
|
146
|
+
.createRequest<{
|
|
147
|
+
response: { params: Record<string, string> };
|
|
148
|
+
queryParams: { q: string };
|
|
149
|
+
}>()({
|
|
150
|
+
endpoint: "/search",
|
|
151
|
+
method: "GET",
|
|
152
|
+
})
|
|
153
|
+
.setQueryParams({ q: "hello world&foo=bar" });
|
|
154
|
+
|
|
155
|
+
const { data, error } = await request.send();
|
|
156
|
+
|
|
157
|
+
expect(error).toBeNull();
|
|
158
|
+
expect(data!.params.q).toBe("hello world&foo=bar");
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
describe("E2E [ URL Params ]", () => {
|
|
163
|
+
it("should resolve single URL param", async () => {
|
|
164
|
+
const client = new Client({ url: baseUrl });
|
|
165
|
+
const request = client.createRequest<{ response: { id: number; name: string } }>()({
|
|
166
|
+
endpoint: "/users/:id",
|
|
167
|
+
method: "GET",
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
const { data, error } = await request.setParams({ id: "1" }).send();
|
|
171
|
+
|
|
172
|
+
expect(error).toBeNull();
|
|
173
|
+
expect(data).toStrictEqual({ id: 1, name: "Alice" });
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it("should resolve multiple URL params", async () => {
|
|
177
|
+
const client = new Client({ url: baseUrl });
|
|
178
|
+
const request = client.createRequest<{ response: { org: string; repo: string } }>()({
|
|
179
|
+
endpoint: "/orgs/:orgId/repos/:repoId",
|
|
180
|
+
method: "GET",
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const { data, error } = await request.setParams({ orgId: "acme", repoId: "hyper-fetch" }).send();
|
|
184
|
+
|
|
185
|
+
expect(error).toBeNull();
|
|
186
|
+
expect(data).toStrictEqual({ org: "acme", repo: "hyper-fetch" });
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe("E2E [ Redirects ]", () => {
|
|
191
|
+
it("should follow 302 redirect", async () => {
|
|
192
|
+
const client = new Client({ url: baseUrl });
|
|
193
|
+
const request = client.createRequest<{ response: { redirected: boolean } }>()({
|
|
194
|
+
endpoint: "/redirect-302",
|
|
195
|
+
method: "GET",
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
const { data, error, status } = await request.send();
|
|
199
|
+
|
|
200
|
+
expect(error).toBeNull();
|
|
201
|
+
expect(status).toBe(200);
|
|
202
|
+
expect(data).toStrictEqual({ redirected: true });
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("should follow 301 redirect", async () => {
|
|
206
|
+
const client = new Client({ url: baseUrl });
|
|
207
|
+
const request = client.createRequest<{ response: { redirected: boolean } }>()({
|
|
208
|
+
endpoint: "/redirect-301",
|
|
209
|
+
method: "GET",
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
const { data, error, status } = await request.send();
|
|
213
|
+
|
|
214
|
+
expect(error).toBeNull();
|
|
215
|
+
expect(status).toBe(200);
|
|
216
|
+
expect(data).toStrictEqual({ redirected: true });
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("should follow redirect chain", async () => {
|
|
220
|
+
const client = new Client({ url: baseUrl });
|
|
221
|
+
const request = client.createRequest<{ response: { redirected: boolean } }>()({
|
|
222
|
+
endpoint: "/redirect-chain",
|
|
223
|
+
method: "GET",
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
const { data, error, status } = await request.send();
|
|
227
|
+
|
|
228
|
+
expect(error).toBeNull();
|
|
229
|
+
expect(status).toBe(200);
|
|
230
|
+
expect(data).toStrictEqual({ redirected: true });
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
describe("E2E [ Streaming ]", () => {
|
|
235
|
+
it("should receive streamed response", async () => {
|
|
236
|
+
const client = new Client({ url: baseUrl });
|
|
237
|
+
const request = client.createRequest<{ response: any }>()({
|
|
238
|
+
endpoint: "/stream",
|
|
239
|
+
method: "GET",
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
const { data, error, status } = await request.send();
|
|
243
|
+
|
|
244
|
+
expect(error).toBeNull();
|
|
245
|
+
expect(status).toBe(200);
|
|
246
|
+
expect(data).toBe("chunk1chunk2chunk3");
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("should report download progress for streamed response", async () => {
|
|
250
|
+
const client = new Client({ url: baseUrl });
|
|
251
|
+
const request = client.createRequest<{ response: any }>()({
|
|
252
|
+
endpoint: "/stream-large",
|
|
253
|
+
method: "GET",
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const progressEvents: number[] = [];
|
|
257
|
+
|
|
258
|
+
const { data, error, status } = await request.send({
|
|
259
|
+
onDownloadProgress: ({ loaded }) => {
|
|
260
|
+
progressEvents.push(loaded);
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
expect(error).toBeNull();
|
|
265
|
+
expect(status).toBe(200);
|
|
266
|
+
expect(data).toBeDefined();
|
|
267
|
+
expect(progressEvents.length).toBeGreaterThanOrEqual(1);
|
|
268
|
+
const lastLoaded = progressEvents[progressEvents.length - 1];
|
|
269
|
+
expect(lastLoaded).toBeGreaterThan(0);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("should return ReadableStream when streaming option is enabled", async () => {
|
|
273
|
+
const client = new Client({ url: baseUrl });
|
|
274
|
+
const request = client
|
|
275
|
+
.createRequest<{ response: any }>()({
|
|
276
|
+
endpoint: "/stream",
|
|
277
|
+
method: "GET",
|
|
278
|
+
})
|
|
279
|
+
.setOptions({ streaming: true });
|
|
280
|
+
|
|
281
|
+
const { data, error, status } = await request.send();
|
|
282
|
+
|
|
283
|
+
expect(error).toBeNull();
|
|
284
|
+
expect(status).toBe(200);
|
|
285
|
+
expect(data).toBeDefined();
|
|
286
|
+
expect(typeof (data as any).getReader).toBe("function");
|
|
287
|
+
|
|
288
|
+
const reader = (data as any as ReadableStream<Uint8Array>).getReader();
|
|
289
|
+
const chunks: string[] = [];
|
|
290
|
+
const decoder = new TextDecoder();
|
|
291
|
+
|
|
292
|
+
// eslint-disable-next-line no-constant-condition
|
|
293
|
+
while (true) {
|
|
294
|
+
// eslint-disable-next-line no-await-in-loop
|
|
295
|
+
const { done, value } = await reader.read();
|
|
296
|
+
if (done) break;
|
|
297
|
+
chunks.push(decoder.decode(value, { stream: true }));
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
expect(chunks.join("")).toBe("chunk1chunk2chunk3");
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
describe("E2E [ File Download ]", () => {
|
|
305
|
+
it("should download text file", async () => {
|
|
306
|
+
const client = new Client({ url: baseUrl });
|
|
307
|
+
const request = client.createRequest<{ response: any }>()({
|
|
308
|
+
endpoint: "/download/test.txt",
|
|
309
|
+
method: "GET",
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
const { data, error, status, extra } = await request.send();
|
|
313
|
+
|
|
314
|
+
expect(error).toBeNull();
|
|
315
|
+
expect(status).toBe(200);
|
|
316
|
+
expect(extra!.headers["content-disposition"]).toContain("test.txt");
|
|
317
|
+
expect(data).toBe("Hello, this is a test file content!");
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it("should download binary file with correct size", async () => {
|
|
321
|
+
const client = new Client({ url: baseUrl });
|
|
322
|
+
const request = client.createRequest<{ response: any }>()({
|
|
323
|
+
endpoint: "/download/binary.bin",
|
|
324
|
+
method: "GET",
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
const { error, status, extra } = await request.send();
|
|
328
|
+
|
|
329
|
+
expect(error).toBeNull();
|
|
330
|
+
expect(status).toBe(200);
|
|
331
|
+
expect(extra!.headers["content-length"]).toBe("1024");
|
|
332
|
+
expect(extra!.headers["content-disposition"]).toContain("binary.bin");
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
describe("E2E [ Concurrent Requests ]", () => {
|
|
337
|
+
it("should handle multiple concurrent requests", async () => {
|
|
338
|
+
const client = new Client({ url: baseUrl });
|
|
339
|
+
|
|
340
|
+
const requests = [1, 2, 3].map((id) =>
|
|
341
|
+
client
|
|
342
|
+
.createRequest<{ response: { id: number } }>()({
|
|
343
|
+
endpoint: `/item/${id}`,
|
|
344
|
+
method: "GET",
|
|
345
|
+
})
|
|
346
|
+
.send(),
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
const results = await Promise.all(requests);
|
|
350
|
+
|
|
351
|
+
results.forEach((result, index) => {
|
|
352
|
+
expect(result.error).toBeNull();
|
|
353
|
+
expect(result.status).toBe(200);
|
|
354
|
+
expect(result.data).toStrictEqual({ id: index + 1 });
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
describe("E2E [ Request Callbacks ]", () => {
|
|
360
|
+
it("should fire lifecycle callbacks in order", async () => {
|
|
361
|
+
const client = new Client({ url: baseUrl });
|
|
362
|
+
const request = client.createRequest<{
|
|
363
|
+
response: { body: { test: boolean } };
|
|
364
|
+
payload: { test: boolean };
|
|
365
|
+
}>()({
|
|
366
|
+
endpoint: "/callback-test",
|
|
367
|
+
method: "POST",
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
const callOrder: string[] = [];
|
|
371
|
+
|
|
372
|
+
await request.send({
|
|
373
|
+
payload: { test: true },
|
|
374
|
+
onRequestStart: () => {
|
|
375
|
+
callOrder.push("requestStart");
|
|
376
|
+
},
|
|
377
|
+
onResponse: () => {
|
|
378
|
+
callOrder.push("response");
|
|
379
|
+
},
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
expect(callOrder).toContain("requestStart");
|
|
383
|
+
expect(callOrder).toContain("response");
|
|
384
|
+
expect(callOrder.indexOf("requestStart")).toBeLessThan(callOrder.indexOf("response"));
|
|
385
|
+
});
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
describe("E2E [ Error Responses ]", () => {
|
|
389
|
+
it("should return structured validation error", async () => {
|
|
390
|
+
const client = new Client({ url: baseUrl });
|
|
391
|
+
const request = client.createRequest<{
|
|
392
|
+
response: { success: boolean };
|
|
393
|
+
payload: { email?: string };
|
|
394
|
+
error: { errors: { email: string } };
|
|
395
|
+
}>()({
|
|
396
|
+
endpoint: "/validate",
|
|
397
|
+
method: "POST",
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
const { data, error, status } = await request.send({ payload: {} });
|
|
401
|
+
|
|
402
|
+
expect(data).toBeNull();
|
|
403
|
+
expect(status).toBe(422);
|
|
404
|
+
expect(error).toStrictEqual({ errors: { email: "Email is required" } });
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it("should return success when payload is valid", async () => {
|
|
408
|
+
const client = new Client({ url: baseUrl });
|
|
409
|
+
const request = client.createRequest<{
|
|
410
|
+
response: { success: boolean };
|
|
411
|
+
payload: { email: string };
|
|
412
|
+
}>()({
|
|
413
|
+
endpoint: "/validate",
|
|
414
|
+
method: "POST",
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
const { data, error, status } = await request.send({ payload: { email: "test@example.com" } });
|
|
418
|
+
|
|
419
|
+
expect(error).toBeNull();
|
|
420
|
+
expect(status).toBe(200);
|
|
421
|
+
expect(data).toStrictEqual({ success: true });
|
|
422
|
+
});
|
|
423
|
+
});
|