@hyper-fetch/core 8.1.1 → 8.2.2
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/README.md +137 -52
- package/__tests__/e2e/e2e.advanced.spec.ts +7 -7
- package/__tests__/features/adapter/adapter.base.spec.ts +2 -1
- package/__tests__/features/adapter/adapter.bindings.spec.ts +4 -4
- package/__tests__/features/adapter/adapter.browser.spec.ts +0 -1
- package/__tests__/features/adapter/adapter.server.spec.ts +4 -5
- package/__tests__/features/adapter/adapter.utils.spec.ts +6 -7
- package/__tests__/features/cache/cache.base.spec.ts +7 -7
- package/__tests__/features/cache/cache.events.spec.ts +15 -15
- package/__tests__/features/cache/cache.garbage-collector.spec.ts +18 -18
- package/__tests__/features/cache/cache.lazy-storage.spec.ts +8 -8
- package/__tests__/features/cache/cache.modes.spec.ts +7 -6
- package/__tests__/features/cache/cache.scoping.spec.ts +12 -8
- package/__tests__/features/cache/cache.typescript.spec.ts +2 -2
- package/__tests__/features/cache/cache.utils.spec.ts +14 -14
- package/__tests__/features/client/client.auth.spec.ts +0 -1
- package/__tests__/features/client/client.base.spec.ts +2 -3
- package/__tests__/features/client/client.from-json.spec.ts +0 -1
- package/__tests__/features/client/client.hydration.spec.ts +5 -5
- package/__tests__/features/client/client.interceptor.spec.ts +3 -3
- package/__tests__/features/client/client.methods.spec.ts +5 -5
- package/__tests__/features/client/client.middleware.spec.ts +2 -2
- package/__tests__/features/client/client.utils.spec.ts +16 -15
- package/__tests__/features/dispatcher/dispatcher.base.spec.ts +3 -3
- package/__tests__/features/dispatcher/dispatcher.custom-storage.spec.ts +4 -5
- package/__tests__/features/dispatcher/dispatcher.events.spec.ts +4 -4
- package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +13 -13
- package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +10 -10
- package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +35 -8
- package/__tests__/features/managers/app/app.manager.base.spec.ts +2 -3
- package/__tests__/features/managers/logger/logger.base.spec.ts +1 -2
- package/__tests__/features/managers/request/request.manager.base.spec.ts +8 -9
- package/__tests__/features/managers/request/request.manager.events.spec.ts +1 -2
- package/__tests__/features/mocker/mocker.base.spec.ts +6 -6
- package/__tests__/features/plugin/plugin.base.spec.ts +1 -2
- package/__tests__/features/request/request.cloning.spec.ts +0 -1
- package/__tests__/features/request/request.deduplicate-defaults.spec.ts +79 -0
- package/__tests__/features/request/request.retry-on-error.spec.ts +1 -2
- package/__tests__/features/request/request.sending.spec.ts +73 -24
- package/__tests__/features/request/request.setters.spec.ts +4 -3
- package/__tests__/features/request/request.typescript.spec.ts +0 -1
- package/__tests__/features/request/request.utils.spec.ts +1 -0
- package/__tests__/features/sdk/sdk.base.spec.ts +7 -12
- package/__tests__/features/sdk/sdk.configuration.spec.ts +5 -7
- package/__tests__/features/sdk/sdk.request-fields.spec.ts +80 -0
- package/__tests__/features/sdk/sdk.types.spec.ts +283 -0
- package/__tests__/shared/callbacks.shared.ts +1 -1
- package/__tests__/tsconfig.json +4 -17
- package/__tests__/utils/adapter.utils.ts +1 -2
- package/__tests__/utils/callbacks.utils.ts +1 -2
- package/__tests__/utils/dispatcher.utils.ts +1 -1
- package/__tests__/vitest.setup.ts +1 -1
- package/dist/adapter/adapter.d.ts +22 -5
- package/dist/adapter/adapter.d.ts.map +1 -1
- package/dist/adapter/adapter.types.d.ts +9 -1
- package/dist/adapter/adapter.types.d.ts.map +1 -1
- package/dist/adapter/adapter.utils.d.ts.map +1 -1
- package/dist/cache/cache.d.ts +6 -9
- package/dist/cache/cache.d.ts.map +1 -1
- package/dist/cache/cache.events.d.ts +7 -4
- package/dist/cache/cache.events.d.ts.map +1 -1
- package/dist/cache/cache.types.d.ts +1 -0
- package/dist/cache/cache.types.d.ts.map +1 -1
- package/dist/client/client.create.d.ts +1 -1
- package/dist/client/client.create.d.ts.map +1 -1
- package/dist/client/client.d.ts +8 -4
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.types.d.ts +1 -1
- package/dist/client/client.types.d.ts.map +1 -1
- package/dist/client/client.utils.d.ts.map +1 -1
- package/dist/constants/http.constants.d.ts +5 -1
- package/dist/constants/http.constants.d.ts.map +1 -1
- package/dist/constants/time.constants.d.ts.map +1 -1
- package/dist/dispatcher/dispatcher.d.ts +4 -4
- package/dist/dispatcher/dispatcher.d.ts.map +1 -1
- package/dist/dispatcher/dispatcher.events.d.ts +1 -1
- package/dist/dispatcher/dispatcher.events.d.ts.map +1 -1
- package/dist/dispatcher/dispatcher.utils.d.ts +1 -1
- package/dist/dispatcher/dispatcher.utils.d.ts.map +1 -1
- package/dist/http-adapter/http-adapter.constants.d.ts.map +1 -1
- package/dist/http-adapter/http-adapter.d.ts.map +1 -1
- package/dist/http-adapter/http-adapter.fetch.d.ts +2 -2
- package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -1
- package/dist/http-adapter/http-adapter.types.d.ts +2 -2
- package/dist/http-adapter/http-adapter.types.d.ts.map +1 -1
- package/dist/http-adapter/http-adapter.utils.d.ts.map +1 -1
- package/dist/index.mjs +2382 -2301
- package/dist/index.mjs.map +1 -1
- package/dist/managers/app/app.manager.constants.d.ts +1 -1
- package/dist/managers/app/app.manager.constants.d.ts.map +1 -1
- package/dist/managers/app/app.manager.d.ts +1 -1
- package/dist/managers/app/app.manager.d.ts.map +1 -1
- package/dist/managers/app/app.manager.events.d.ts +9 -0
- package/dist/managers/app/app.manager.events.d.ts.map +1 -1
- package/dist/managers/logger/logger.manager.d.ts +1 -1
- package/dist/managers/logger/logger.manager.d.ts.map +1 -1
- package/dist/managers/request/request.manager.d.ts.map +1 -1
- package/dist/managers/request/request.manager.events.d.ts +14 -3
- package/dist/managers/request/request.manager.events.d.ts.map +1 -1
- package/dist/mocker/mocker.types.d.ts +1 -0
- package/dist/mocker/mocker.types.d.ts.map +1 -1
- package/dist/plugin/plugin.d.ts +17 -1
- package/dist/plugin/plugin.d.ts.map +1 -1
- package/dist/plugin/plugin.types.d.ts +5 -4
- package/dist/plugin/plugin.types.d.ts.map +1 -1
- package/dist/request/request.d.ts +33 -4
- package/dist/request/request.d.ts.map +1 -1
- package/dist/request/request.hooks.d.ts +2 -2
- package/dist/request/request.hooks.d.ts.map +1 -1
- package/dist/request/request.types.d.ts +92 -5
- package/dist/request/request.types.d.ts.map +1 -1
- package/dist/request/request.utils.d.ts.map +1 -1
- package/dist/sdk/sdk.d.ts +25 -5
- package/dist/sdk/sdk.d.ts.map +1 -1
- package/dist/types/http.types.d.ts +1 -1
- package/dist/types/http.types.d.ts.map +1 -1
- package/dist/types/request.types.d.ts +2 -2
- package/dist/types/request.types.d.ts.map +1 -1
- package/dist/utils/emitter.utils.d.ts +1 -1
- package/dist/utils/emitter.utils.d.ts.map +1 -1
- package/package.json +8 -8
- package/src/adapter/adapter.bindings.ts +14 -14
- package/src/adapter/adapter.ts +28 -10
- package/src/adapter/adapter.types.ts +10 -1
- package/src/adapter/adapter.utils.ts +5 -5
- package/src/cache/cache.events.ts +7 -5
- package/src/cache/cache.ts +14 -17
- package/src/cache/cache.types.ts +1 -0
- package/src/client/client.create.ts +1 -1
- package/src/client/client.ts +14 -9
- package/src/client/client.types.ts +2 -2
- package/src/client/client.utils.ts +3 -2
- package/src/constants/http.constants.ts +5 -0
- package/src/constants/time.constants.ts +1 -1
- package/src/dispatcher/dispatcher.events.ts +1 -2
- package/src/dispatcher/dispatcher.ts +13 -13
- package/src/dispatcher/dispatcher.utils.ts +10 -4
- package/src/http-adapter/http-adapter.constants.ts +1 -0
- package/src/http-adapter/http-adapter.fetch.ts +19 -9
- package/src/http-adapter/http-adapter.ts +2 -1
- package/src/http-adapter/http-adapter.types.ts +3 -2
- package/src/http-adapter/http-adapter.utils.ts +15 -11
- package/src/managers/app/app.manager.constants.ts +2 -1
- package/src/managers/app/app.manager.events.ts +9 -1
- package/src/managers/app/app.manager.ts +1 -1
- package/src/managers/app/app.manager.utils.ts +2 -2
- package/src/managers/logger/logger.manager.ts +1 -2
- package/src/managers/request/request.manager.events.ts +14 -12
- package/src/managers/request/request.manager.ts +4 -4
- package/src/mocker/mocker.ts +2 -2
- package/src/mocker/mocker.types.ts +1 -0
- package/src/plugin/plugin.ts +17 -1
- package/src/plugin/plugin.types.ts +5 -4
- package/src/request/request.hooks.ts +6 -5
- package/src/request/request.ts +77 -34
- package/src/request/request.types.ts +107 -5
- package/src/request/request.utils.ts +17 -16
- package/src/sdk/sdk.ts +79 -23
- package/src/types/http.types.ts +1 -1
- package/src/types/request.types.ts +3 -2
- package/src/utils/emitter.utils.ts +1 -1
- package/src/utils/uuid.utils.ts +1 -1
- package/tsconfig.json +30 -2
- package/vite.config.ts +14 -20
- package/.eslintrc.cjs +0 -12
- package/tsconfig.base.json +0 -7
package/README.md
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
# ⚡ Hyper Fetch
|
|
2
2
|
|
|
3
|
-
<p>
|
|
3
|
+
<p align="center">
|
|
4
|
+
<b>One SDK for every API. The type-safe API layer for TypeScript apps — REST, GraphQL, WebSockets, SSE, Firebase, and more.</b>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
4
8
|
<a href="https://bettertyped.com/">
|
|
5
9
|
<img src="https://custom-icon-badges.demolab.com/static/v1?label=&message=BetterTyped&color=333&logo=BT" />
|
|
6
10
|
</a>
|
|
@@ -13,61 +17,142 @@
|
|
|
13
17
|
<a href="https://www.npmjs.com/package/@hyper-fetch/core">
|
|
14
18
|
<img src="https://custom-icon-badges.demolab.com/npm/v/@hyper-fetch/core.svg?logo=npm&color=e76f51" />
|
|
15
19
|
</a>
|
|
20
|
+
<a href="https://www.npmjs.com/package/@hyper-fetch/core">
|
|
21
|
+
<img src="https://custom-icon-badges.demolab.com/npm/dm/@hyper-fetch/core?logoColor=fff&logo=trending-up" />
|
|
22
|
+
</a>
|
|
16
23
|
<a href="https://github.com/BetterTyped/hyper-fetch">
|
|
17
24
|
<img src="https://custom-icon-badges.demolab.com/badge/typescript-%23007ACC.svg?logo=typescript&logoColor=white" />
|
|
18
25
|
</a>
|
|
19
|
-
<a href="https://
|
|
26
|
+
<a href="https://bundlephobia.com/package/@hyper-fetch/core">
|
|
20
27
|
<img src="https://custom-icon-badges.demolab.com/bundlephobia/minzip/@hyper-fetch/core?color=64BC4B&logo=package" />
|
|
21
28
|
</a>
|
|
22
29
|
</p>
|
|
23
30
|
|
|
24
|
-
## About
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
-
|
|
68
|
-
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
31
|
+
## 📖 About
|
|
32
|
+
|
|
33
|
+
HyperFetch Core is the foundation of the HyperFetch ecosystem. Every API call is a typed, immutable request object with
|
|
34
|
+
built-in caching, queuing, retries, and offline support. It works in any JavaScript environment — browser, server, or
|
|
35
|
+
edge — and connects to any API through pluggable adapters.
|
|
36
|
+
|
|
37
|
+
## 🎯 Key Capabilities
|
|
38
|
+
|
|
39
|
+
- 🔮 **Zero guesswork** — Full autocompletion for every request, response, param, and error. Never cast to `any` again
|
|
40
|
+
- 📡 **One interface for everything** — REST, GraphQL, Firebase, WebSockets, SSE — switch APIs without rewriting a line
|
|
41
|
+
- 💾 **Stop re-fetching what you already have** — Smart cache with TTL and invalidation out of the box
|
|
42
|
+
- 🔁 **Failed requests retry themselves** — Configurable retry with backoff so users never see random failures
|
|
43
|
+
- 📴 **Works without internet** — Queues requests offline and replays them when connection returns
|
|
44
|
+
- 🚫 **No duplicate requests** — Concurrent identical calls are deduplicated automatically
|
|
45
|
+
- ⏱️ **Know exactly what's happening** — Upload and download progress with time estimates for every request
|
|
46
|
+
- 🎯 **Cancel anything, anytime** — Kill pending requests individually or by group with one call
|
|
47
|
+
- 🔌 **Swap engines without touching your code** — Switch between Axios, GraphQL, Firebase adapters in one line
|
|
48
|
+
- 🪄 **Generate your SDK** — Point CLI at an OpenAPI schema and get a fully typed client instantly
|
|
49
|
+
|
|
50
|
+
## 🚀 Quick Start
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
npm install @hyper-fetch/core
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { createClient } from "@hyper-fetch/core";
|
|
58
|
+
|
|
59
|
+
// Single entry point — all requests inherit this base URL
|
|
60
|
+
const client = createClient({ url: "https://api.example.com" });
|
|
61
|
+
|
|
62
|
+
// Define a request with typed response — :userId becomes a required param
|
|
63
|
+
const getUser = client.createRequest<{ response: { id: number; name: string } }>()({
|
|
64
|
+
endpoint: "/users/:userId",
|
|
65
|
+
method: "GET",
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// setParams is typed from the endpoint string, send() returns typed data
|
|
69
|
+
const { data, error } = await getUser.setParams({ userId: 1 }).send();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## 📚 Documentation
|
|
73
|
+
|
|
74
|
+
- [Getting Started](https://hyperfetch.bettertyped.com/docs/getting-started/quick-start)
|
|
75
|
+
- [Core Overview](https://hyperfetch.bettertyped.com/docs/core/overview)
|
|
76
|
+
- [API Reference](https://hyperfetch.bettertyped.com/api/)
|
|
77
|
+
|
|
78
|
+
## 💡 Examples
|
|
79
|
+
|
|
80
|
+
### Typed requests with params, payload, and query params
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
interface User {
|
|
84
|
+
id: number;
|
|
85
|
+
name: string;
|
|
86
|
+
email: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// GET request — only response type needed, params inferred from :userId
|
|
90
|
+
const getUser = client.createRequest<{ response: User }>()({
|
|
91
|
+
endpoint: "/users/:userId",
|
|
92
|
+
method: "GET",
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// POST request — define both response and payload types
|
|
96
|
+
const createUser = client.createRequest<{
|
|
97
|
+
response: User;
|
|
98
|
+
payload: { name: string; email: string };
|
|
99
|
+
}>()({
|
|
100
|
+
endpoint: "/users",
|
|
101
|
+
method: "POST",
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// Params are type-checked: { userId: number } required here
|
|
105
|
+
const { data } = await getUser.setParams({ userId: 1 }).send();
|
|
106
|
+
|
|
107
|
+
// Payload is type-checked: { name, email } required here
|
|
108
|
+
const { data: newUser } = await createUser.send({
|
|
109
|
+
data: { name: "Jane", email: "jane@example.com" },
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Dynamic data with query params
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
// Define allowed query params — all are optional and type-checked
|
|
117
|
+
const listUsers = client.createRequest<{
|
|
118
|
+
response: User[];
|
|
119
|
+
queryParams: { page?: number; limit?: number; search?: string };
|
|
120
|
+
}>()({
|
|
121
|
+
endpoint: "/users",
|
|
122
|
+
method: "GET",
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// Query params are appended to the URL: /users?page=1&limit=20&search=john
|
|
126
|
+
const { data } = await listUsers.setQueryParams({ page: 1, limit: 20, search: "john" }).send();
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Lifecycle callbacks
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
// Hook into the request lifecycle — track progress, log events, handle responses
|
|
133
|
+
const { data } = await getUser.setParams({ userId: 1 }).send({
|
|
134
|
+
onStart: ({ requestId }) => console.log(`Request ${requestId} started`),
|
|
135
|
+
onResponse: ({ response }) => console.log("Got data:", response.data),
|
|
136
|
+
onUploadProgress: ({ progress }) => console.log(`Upload: ${progress}%`),
|
|
137
|
+
onDownloadProgress: ({ progress }) => console.log(`Download: ${progress}%`),
|
|
138
|
+
});
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Response mapping
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
// Transform the response before it reaches your code
|
|
145
|
+
const getUser = client
|
|
146
|
+
.createRequest<{ response: User }>()({
|
|
147
|
+
endpoint: "/users/:userId",
|
|
148
|
+
method: "GET",
|
|
149
|
+
})
|
|
150
|
+
.setResponseMapper((response) => ({
|
|
151
|
+
...response,
|
|
152
|
+
data: { ...response.data, name: response.data.name.toUpperCase() },
|
|
153
|
+
}));
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## License
|
|
157
|
+
|
|
158
|
+
[MIT](https://github.com/BetterTyped/hyper-fetch/blob/main/License.md)
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @vitest-environment node
|
|
3
|
-
*/
|
|
4
|
-
import { URL } from "url";
|
|
5
1
|
import { Client } from "@hyper-fetch/core";
|
|
6
2
|
import type { RouteHandler } from "@hyper-fetch/testing";
|
|
7
3
|
import {
|
|
@@ -12,6 +8,10 @@ import {
|
|
|
12
8
|
fileDownloadHandler,
|
|
13
9
|
echoHandler,
|
|
14
10
|
} from "@hyper-fetch/testing";
|
|
11
|
+
/**
|
|
12
|
+
* @vitest-environment node
|
|
13
|
+
*/
|
|
14
|
+
import { URL } from "url";
|
|
15
15
|
|
|
16
16
|
const { route, startServer, stopServer } = createE2EServer();
|
|
17
17
|
|
|
@@ -73,7 +73,7 @@ beforeAll(async () => {
|
|
|
73
73
|
// File Download
|
|
74
74
|
const fileContent = Buffer.from("Hello, this is a test file content!");
|
|
75
75
|
route("GET", "/download/test.txt", fileDownloadHandler(fileContent, "test.txt", "text/plain"));
|
|
76
|
-
const binaryContent = Buffer.alloc(1024,
|
|
76
|
+
const binaryContent = Buffer.alloc(1024, 0xAB);
|
|
77
77
|
route("GET", "/download/binary.bin", fileDownloadHandler(binaryContent, "binary.bin"));
|
|
78
78
|
|
|
79
79
|
// Concurrent
|
|
@@ -265,7 +265,7 @@ describe("E2E [ Streaming ]", () => {
|
|
|
265
265
|
expect(status).toBe(200);
|
|
266
266
|
expect(data).toBeDefined();
|
|
267
267
|
expect(progressEvents.length).toBeGreaterThanOrEqual(1);
|
|
268
|
-
const lastLoaded = progressEvents
|
|
268
|
+
const lastLoaded = progressEvents.at(-1);
|
|
269
269
|
expect(lastLoaded).toBeGreaterThan(0);
|
|
270
270
|
});
|
|
271
271
|
|
|
@@ -293,7 +293,7 @@ describe("E2E [ Streaming ]", () => {
|
|
|
293
293
|
while (true) {
|
|
294
294
|
// eslint-disable-next-line no-await-in-loop
|
|
295
295
|
const { done, value } = await reader.read();
|
|
296
|
-
if (done) break;
|
|
296
|
+
if (done) {break;}
|
|
297
297
|
chunks.push(decoder.decode(value, { stream: true }));
|
|
298
298
|
}
|
|
299
299
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { Client, createClient } from "client";
|
|
2
1
|
import type { AdapterInstance } from "adapter";
|
|
3
2
|
import { Adapter, RequestProcessingError, getAdapterHeaders, getAdapterPayload } from "adapter";
|
|
3
|
+
import { Client, createClient } from "client";
|
|
4
|
+
|
|
4
5
|
import { HttpMethods } from "../../../src/constants/http.constants";
|
|
5
6
|
|
|
6
7
|
describe("Adapter [ Base ]", () => {
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
|
|
2
|
-
|
|
3
|
-
import { Plugin } from "plugin";
|
|
4
2
|
import type { ResponseType, ResponseErrorType } from "adapter";
|
|
5
3
|
import { getAdapterBindings, getErrorMessage } from "adapter";
|
|
6
|
-
import { testProgressSpy } from "../../shared";
|
|
7
4
|
import { Client } from "client";
|
|
8
|
-
import type { RequestInstance } from "request";
|
|
9
5
|
import type { FetchAdapterOptionsType, HttpAdapterType } from "http-adapter";
|
|
10
6
|
import { xhrExtra } from "http-adapter";
|
|
7
|
+
import { Plugin } from "plugin";
|
|
8
|
+
import type { RequestInstance } from "request";
|
|
9
|
+
|
|
10
|
+
import { testProgressSpy } from "../../shared";
|
|
11
11
|
|
|
12
12
|
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
13
13
|
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @vitest-environment node
|
|
3
|
-
*/
|
|
4
|
-
import type { Mock } from "vitest";
|
|
5
1
|
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
6
|
-
|
|
7
2
|
import { getErrorMessage } from "adapter";
|
|
8
3
|
import { Client } from "client";
|
|
9
4
|
import { HttpAdapter } from "http-adapter";
|
|
5
|
+
/**
|
|
6
|
+
* @vitest-environment node
|
|
7
|
+
*/
|
|
8
|
+
import type { Mock } from "vitest";
|
|
10
9
|
|
|
11
10
|
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
12
11
|
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
-
|
|
3
|
-
import { createClient } from "client";
|
|
4
2
|
import { getAdapterHeaders, getAdapterPayload, getErrorMessage } from "adapter";
|
|
3
|
+
import { createClient } from "client";
|
|
5
4
|
import { parseResponse, parseErrorResponse, handleResponse, getResponseHeaders } from "http-adapter";
|
|
6
5
|
|
|
7
6
|
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
@@ -126,7 +125,7 @@ describe("Adapter [ Utils ]", () => {
|
|
|
126
125
|
describe("When handleResponse util gets triggered", () => {
|
|
127
126
|
it("should return Buffer for arraybuffer response type", () => {
|
|
128
127
|
const chunks = [Buffer.from("Hello"), Buffer.from("World")];
|
|
129
|
-
const result = handleResponse(chunks, "arraybuffer", "
|
|
128
|
+
const result = handleResponse(chunks, "arraybuffer", "utf8");
|
|
130
129
|
|
|
131
130
|
expect(Buffer.isBuffer(result)).toBe(true);
|
|
132
131
|
expect(result.toString()).toBe("HelloWorld");
|
|
@@ -135,14 +134,14 @@ describe("Adapter [ Utils ]", () => {
|
|
|
135
134
|
it("should parse JSON response type", () => {
|
|
136
135
|
const data = { message: "test" };
|
|
137
136
|
const chunks = [Buffer.from(JSON.stringify(data))];
|
|
138
|
-
const result = handleResponse(chunks, "json", "
|
|
137
|
+
const result = handleResponse(chunks, "json", "utf8");
|
|
139
138
|
|
|
140
139
|
expect(result).toEqual(data);
|
|
141
140
|
});
|
|
142
141
|
|
|
143
142
|
it("should return string for default response type", () => {
|
|
144
143
|
const chunks = [Buffer.from("Hello"), Buffer.from("World")];
|
|
145
|
-
const result = handleResponse(chunks, "text", "
|
|
144
|
+
const result = handleResponse(chunks, "text", "utf8");
|
|
146
145
|
|
|
147
146
|
expect(typeof result).toBe("string");
|
|
148
147
|
expect(result).toBe("HelloWorld");
|
|
@@ -150,14 +149,14 @@ describe("Adapter [ Utils ]", () => {
|
|
|
150
149
|
|
|
151
150
|
it("should handle empty chunks", () => {
|
|
152
151
|
const chunks: Buffer[] = [];
|
|
153
|
-
const result = handleResponse(chunks, "text", "
|
|
152
|
+
const result = handleResponse(chunks, "text", "utf8");
|
|
154
153
|
|
|
155
154
|
expect(result).toBe("");
|
|
156
155
|
});
|
|
157
156
|
|
|
158
157
|
it("should handle invalid JSON for json response type", () => {
|
|
159
158
|
const chunks = [Buffer.from("invalid json")];
|
|
160
|
-
const result = handleResponse(chunks, "json", "
|
|
159
|
+
const result = handleResponse(chunks, "json", "utf8");
|
|
161
160
|
|
|
162
161
|
expect(result).toBe("invalid json");
|
|
163
162
|
});
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
|
|
2
|
-
|
|
3
2
|
import type { ResponseSuccessType } from "adapter";
|
|
4
|
-
import type { ResponseDetailsType } from "managers";
|
|
5
|
-
import { createCache } from "../../utils";
|
|
6
3
|
import { Client } from "client";
|
|
7
4
|
import type { HttpAdapterType } from "http-adapter";
|
|
8
5
|
import { xhrExtra } from "http-adapter";
|
|
6
|
+
import type { ResponseDetailsType } from "managers";
|
|
7
|
+
|
|
8
|
+
import { createCache } from "../../utils";
|
|
9
9
|
|
|
10
10
|
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
11
11
|
|
|
@@ -21,10 +21,10 @@ describe("Cache [ Base ]", () => {
|
|
|
21
21
|
};
|
|
22
22
|
const details: ResponseDetailsType = {
|
|
23
23
|
retries: 0,
|
|
24
|
-
requestTimestamp:
|
|
25
|
-
responseTimestamp:
|
|
26
|
-
addedTimestamp:
|
|
27
|
-
triggerTimestamp:
|
|
24
|
+
requestTimestamp: Date.now(),
|
|
25
|
+
responseTimestamp: Date.now(),
|
|
26
|
+
addedTimestamp: Date.now(),
|
|
27
|
+
triggerTimestamp: Date.now(),
|
|
28
28
|
isCanceled: false,
|
|
29
29
|
isOffline: false,
|
|
30
30
|
willRetry: false,
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { sleep } from "@hyper-fetch/testing";
|
|
2
|
-
|
|
3
2
|
import type { CacheValueType } from "cache";
|
|
4
|
-
import { createCache } from "../../utils";
|
|
5
3
|
import { Client } from "client";
|
|
6
4
|
import { xhrExtra } from "http-adapter";
|
|
7
5
|
import { Plugin } from "plugin";
|
|
8
6
|
|
|
7
|
+
import { createCache } from "../../utils";
|
|
8
|
+
|
|
9
9
|
describe("Cache [ Events ]", () => {
|
|
10
10
|
const cacheKey = "test";
|
|
11
11
|
|
|
@@ -21,10 +21,10 @@ describe("Cache [ Events ]", () => {
|
|
|
21
21
|
success: true,
|
|
22
22
|
extra: xhrExtra,
|
|
23
23
|
retries: 0,
|
|
24
|
-
requestTimestamp:
|
|
25
|
-
responseTimestamp:
|
|
26
|
-
addedTimestamp:
|
|
27
|
-
triggerTimestamp:
|
|
24
|
+
requestTimestamp: Date.now(),
|
|
25
|
+
responseTimestamp: Date.now(),
|
|
26
|
+
addedTimestamp: Date.now(),
|
|
27
|
+
triggerTimestamp: Date.now(),
|
|
28
28
|
cached: true,
|
|
29
29
|
isCanceled: false,
|
|
30
30
|
isOffline: false,
|
|
@@ -33,7 +33,7 @@ describe("Cache [ Events ]", () => {
|
|
|
33
33
|
scope: null,
|
|
34
34
|
staleTime: request.staleTime,
|
|
35
35
|
version: cache.version,
|
|
36
|
-
cacheTime:
|
|
36
|
+
cacheTime: 300_000,
|
|
37
37
|
};
|
|
38
38
|
|
|
39
39
|
beforeEach(() => {
|
|
@@ -151,10 +151,10 @@ describe("Cache [ Events ]", () => {
|
|
|
151
151
|
success: true,
|
|
152
152
|
extra: xhrExtra,
|
|
153
153
|
retries: 0,
|
|
154
|
-
requestTimestamp:
|
|
155
|
-
responseTimestamp:
|
|
156
|
-
addedTimestamp:
|
|
157
|
-
triggerTimestamp:
|
|
154
|
+
requestTimestamp: Date.now(),
|
|
155
|
+
responseTimestamp: Date.now(),
|
|
156
|
+
addedTimestamp: Date.now(),
|
|
157
|
+
triggerTimestamp: Date.now(),
|
|
158
158
|
isCanceled: false,
|
|
159
159
|
isOffline: false,
|
|
160
160
|
willRetry: false,
|
|
@@ -172,10 +172,10 @@ describe("Cache [ Events ]", () => {
|
|
|
172
172
|
success: true,
|
|
173
173
|
extra: xhrExtra,
|
|
174
174
|
retries: 0,
|
|
175
|
-
requestTimestamp:
|
|
176
|
-
responseTimestamp:
|
|
177
|
-
addedTimestamp:
|
|
178
|
-
triggerTimestamp:
|
|
175
|
+
requestTimestamp: Date.now(),
|
|
176
|
+
responseTimestamp: Date.now(),
|
|
177
|
+
addedTimestamp: Date.now(),
|
|
178
|
+
triggerTimestamp: Date.now(),
|
|
179
179
|
isCanceled: false,
|
|
180
180
|
isOffline: false,
|
|
181
181
|
willRetry: false,
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { waitFor } from "@testing-library/dom";
|
|
2
|
-
|
|
3
2
|
import type { CacheValueType } from "cache";
|
|
4
3
|
import { Client, Time, xhrExtra } from "index";
|
|
4
|
+
|
|
5
5
|
import { createCache, createLazyCacheAdapter } from "../../utils";
|
|
6
6
|
|
|
7
7
|
describe("Cache [ Garbage Collector ]", () => {
|
|
8
8
|
const cacheKey = "test";
|
|
9
|
-
const staleTime =
|
|
9
|
+
const staleTime = 30_000;
|
|
10
10
|
const version = "test";
|
|
11
|
-
const cacheTime =
|
|
11
|
+
const cacheTime = 10_000;
|
|
12
12
|
const cacheData: CacheValueType = {
|
|
13
13
|
data: null,
|
|
14
14
|
error: null,
|
|
@@ -16,10 +16,10 @@ describe("Cache [ Garbage Collector ]", () => {
|
|
|
16
16
|
success: true,
|
|
17
17
|
extra: xhrExtra,
|
|
18
18
|
retries: 0,
|
|
19
|
-
requestTimestamp:
|
|
20
|
-
responseTimestamp:
|
|
21
|
-
addedTimestamp:
|
|
22
|
-
triggerTimestamp:
|
|
19
|
+
requestTimestamp: Date.now(),
|
|
20
|
+
responseTimestamp: Date.now(),
|
|
21
|
+
addedTimestamp: Date.now(),
|
|
22
|
+
triggerTimestamp: Date.now(),
|
|
23
23
|
isCanceled: false,
|
|
24
24
|
isOffline: false,
|
|
25
25
|
willRetry: false,
|
|
@@ -46,16 +46,16 @@ describe("Cache [ Garbage Collector ]", () => {
|
|
|
46
46
|
vi.resetAllMocks();
|
|
47
47
|
vi.clearAllMocks();
|
|
48
48
|
|
|
49
|
-
cacheData.requestTimestamp =
|
|
50
|
-
cacheData.responseTimestamp =
|
|
51
|
-
cacheData.addedTimestamp =
|
|
52
|
-
cacheData.triggerTimestamp =
|
|
49
|
+
cacheData.requestTimestamp = Date.now();
|
|
50
|
+
cacheData.responseTimestamp = Date.now();
|
|
51
|
+
cacheData.addedTimestamp = Date.now();
|
|
52
|
+
cacheData.triggerTimestamp = Date.now();
|
|
53
53
|
});
|
|
54
54
|
|
|
55
55
|
describe("when garbage collector is triggered", () => {
|
|
56
56
|
it("should not schedule garbage collector for non existing key", async () => {
|
|
57
57
|
cache.scheduleGarbageCollector(cacheKey);
|
|
58
|
-
expect(
|
|
58
|
+
expect([...cache.garbageCollectors.keys()]).toHaveLength(0);
|
|
59
59
|
});
|
|
60
60
|
it("should garbage collect data from sync storage", async () => {
|
|
61
61
|
cache.set(request.setCacheTime(10), cacheData);
|
|
@@ -78,7 +78,7 @@ describe("Cache [ Garbage Collector ]", () => {
|
|
|
78
78
|
storage,
|
|
79
79
|
version,
|
|
80
80
|
});
|
|
81
|
-
expect(
|
|
81
|
+
expect([...cacheInstance.garbageCollectors.keys()]).toHaveLength(1);
|
|
82
82
|
});
|
|
83
83
|
it("should not schedule lazy storage garbage collection on mount", async () => {
|
|
84
84
|
const lazyStorage = new Map<string, CacheValueType>();
|
|
@@ -89,7 +89,7 @@ describe("Cache [ Garbage Collector ]", () => {
|
|
|
89
89
|
});
|
|
90
90
|
|
|
91
91
|
await waitFor(() => {
|
|
92
|
-
expect(
|
|
92
|
+
expect([...cacheInstance.garbageCollectors.keys()]).toHaveLength(0);
|
|
93
93
|
});
|
|
94
94
|
});
|
|
95
95
|
it("should schedule garbage collection when resource is added", async () => {
|
|
@@ -142,15 +142,15 @@ describe("Cache [ Garbage Collector ]", () => {
|
|
|
142
142
|
});
|
|
143
143
|
|
|
144
144
|
await waitFor(() => {
|
|
145
|
-
expect(
|
|
145
|
+
expect([...cacheInstance.garbageCollectors.keys()]).toHaveLength(0);
|
|
146
146
|
});
|
|
147
147
|
});
|
|
148
148
|
it("should garbage collect when time left is less than zero", async () => {
|
|
149
|
-
const pastTimestamp =
|
|
149
|
+
const pastTimestamp = Date.now() - 20_000; // 20 seconds ago
|
|
150
150
|
const staleData = {
|
|
151
151
|
...cacheData,
|
|
152
152
|
responseTimestamp: pastTimestamp,
|
|
153
|
-
cacheTime:
|
|
153
|
+
cacheTime: 10_000, // 10 seconds
|
|
154
154
|
};
|
|
155
155
|
|
|
156
156
|
cache.set(request, staleData);
|
|
@@ -168,7 +168,7 @@ describe("Cache [ Garbage Collector ]", () => {
|
|
|
168
168
|
});
|
|
169
169
|
|
|
170
170
|
await waitFor(() => {
|
|
171
|
-
expect(
|
|
171
|
+
expect([...cacheInstance.garbageCollectors.keys()]).toHaveLength(0);
|
|
172
172
|
});
|
|
173
173
|
});
|
|
174
174
|
});
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { sleep } from "@hyper-fetch/testing";
|
|
2
|
-
|
|
3
2
|
import type { CacheAsyncStorageType, CacheValueType } from "cache";
|
|
4
|
-
import { createCache, createLazyCacheAdapter } from "../../utils";
|
|
5
3
|
import { Client } from "client";
|
|
6
4
|
import { xhrExtra } from "http-adapter";
|
|
7
5
|
|
|
6
|
+
import { createCache, createLazyCacheAdapter } from "../../utils";
|
|
7
|
+
|
|
8
8
|
describe("Cache [ Lazy Storage ]", () => {
|
|
9
|
-
const staleTime =
|
|
9
|
+
const staleTime = 10_000;
|
|
10
10
|
const version = "test";
|
|
11
11
|
const cacheKey = "1";
|
|
12
12
|
const cacheData: CacheValueType = {
|
|
@@ -16,10 +16,10 @@ describe("Cache [ Lazy Storage ]", () => {
|
|
|
16
16
|
success: true,
|
|
17
17
|
extra: xhrExtra,
|
|
18
18
|
retries: 0,
|
|
19
|
-
addedTimestamp:
|
|
20
|
-
requestTimestamp:
|
|
21
|
-
responseTimestamp:
|
|
22
|
-
triggerTimestamp:
|
|
19
|
+
addedTimestamp: Date.now(),
|
|
20
|
+
requestTimestamp: Date.now(),
|
|
21
|
+
responseTimestamp: Date.now(),
|
|
22
|
+
triggerTimestamp: Date.now(),
|
|
23
23
|
isCanceled: false,
|
|
24
24
|
isOffline: false,
|
|
25
25
|
willRetry: false,
|
|
@@ -79,7 +79,7 @@ describe("Cache [ Lazy Storage ]", () => {
|
|
|
79
79
|
expect(spy).toHaveBeenCalledTimes(1);
|
|
80
80
|
});
|
|
81
81
|
it("should not emit stale data", async () => {
|
|
82
|
-
await cache.options?.lazyStorage?.set(cacheKey, { ...cacheData, responseTimestamp:
|
|
82
|
+
await cache.options?.lazyStorage?.set(cacheKey, { ...cacheData, responseTimestamp: Date.now() - staleTime });
|
|
83
83
|
cache.events.onDataByKey(cacheKey, spy);
|
|
84
84
|
const data = cache.get(cacheKey);
|
|
85
85
|
await sleep(50);
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { ResponseSuccessType } from "adapter";
|
|
2
|
-
import type { ResponseDetailsType } from "managers";
|
|
3
|
-
import { createCache } from "../../utils";
|
|
4
2
|
import { Client } from "client";
|
|
5
3
|
import type { HttpAdapterType } from "http-adapter";
|
|
6
4
|
import { xhrExtra } from "http-adapter";
|
|
5
|
+
import type { ResponseDetailsType } from "managers";
|
|
6
|
+
|
|
7
|
+
import { createCache } from "../../utils";
|
|
7
8
|
|
|
8
9
|
const createServerClient = (url: string) => new Client({ url, mode: "server" });
|
|
9
10
|
|
|
@@ -19,10 +20,10 @@ describe("Cache [ Modes ]", () => {
|
|
|
19
20
|
};
|
|
20
21
|
const details: ResponseDetailsType = {
|
|
21
22
|
retries: 0,
|
|
22
|
-
requestTimestamp:
|
|
23
|
-
responseTimestamp:
|
|
24
|
-
addedTimestamp:
|
|
25
|
-
triggerTimestamp:
|
|
23
|
+
requestTimestamp: Date.now(),
|
|
24
|
+
responseTimestamp: Date.now(),
|
|
25
|
+
addedTimestamp: Date.now(),
|
|
26
|
+
triggerTimestamp: Date.now(),
|
|
26
27
|
isCanceled: false,
|
|
27
28
|
isOffline: false,
|
|
28
29
|
willRetry: false,
|