@hyper-fetch/core 8.2.0 → 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 +4 -4
- 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 +0 -1
- package/__tests__/features/sdk/sdk.request-fields.spec.ts +80 -0
- package/__tests__/features/sdk/sdk.types.spec.ts +3 -4
- 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 +4 -4
- package/dist/adapter/adapter.d.ts.map +1 -1
- package/dist/adapter/adapter.types.d.ts +1 -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 +3 -3
- package/dist/cache/cache.d.ts.map +1 -1
- package/dist/cache/cache.events.d.ts +2 -2
- package/dist/cache/cache.events.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 +3 -3
- 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 +3 -3
- 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 +2363 -2347
- 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.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 +2 -2
- package/dist/managers/request/request.manager.events.d.ts.map +1 -1
- package/dist/plugin/plugin.d.ts +1 -1
- package/dist/plugin/plugin.d.ts.map +1 -1
- package/dist/plugin/plugin.types.d.ts +4 -4
- package/dist/plugin/plugin.types.d.ts.map +1 -1
- package/dist/request/request.d.ts +5 -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 +3 -3
- 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 +1 -1
- 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 +10 -9
- package/src/adapter/adapter.types.ts +2 -1
- package/src/adapter/adapter.utils.ts +5 -5
- package/src/cache/cache.events.ts +2 -3
- package/src/cache/cache.ts +11 -11
- package/src/client/client.create.ts +1 -1
- package/src/client/client.ts +9 -8
- 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 +12 -12
- 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 +0 -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 +2 -3
- package/src/managers/request/request.manager.ts +4 -4
- package/src/mocker/mocker.ts +2 -2
- package/src/plugin/plugin.ts +1 -1
- package/src/plugin/plugin.types.ts +4 -4
- package/src/request/request.hooks.ts +6 -5
- package/src/request/request.ts +49 -34
- package/src/request/request.types.ts +4 -3
- package/src/request/request.utils.ts +17 -16
- package/src/sdk/sdk.ts +26 -19
- 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
|
@@ -30,7 +30,9 @@
|
|
|
30
30
|
|
|
31
31
|
## 📖 About
|
|
32
32
|
|
|
33
|
-
HyperFetch Core is the foundation of the HyperFetch ecosystem. Every API call is a typed, immutable request object with
|
|
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.
|
|
34
36
|
|
|
35
37
|
## 🎯 Key Capabilities
|
|
36
38
|
|
|
@@ -121,9 +123,7 @@ const listUsers = client.createRequest<{
|
|
|
121
123
|
});
|
|
122
124
|
|
|
123
125
|
// Query params are appended to the URL: /users?page=1&limit=20&search=john
|
|
124
|
-
const { data } = await listUsers
|
|
125
|
-
.setQueryParams({ page: 1, limit: 20, search: "john" })
|
|
126
|
-
.send();
|
|
126
|
+
const { data } = await listUsers.setQueryParams({ page: 1, limit: 20, search: "john" }).send();
|
|
127
127
|
```
|
|
128
128
|
|
|
129
129
|
### Lifecycle callbacks
|
|
@@ -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,
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { waitFor } from "@testing-library/dom";
|
|
2
|
-
|
|
3
2
|
import type { ResponseSuccessType } from "adapter";
|
|
4
|
-
import type { ResponseDetailsType } from "managers";
|
|
5
|
-
import { createCache, createLazyCacheAdapter } 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";
|
|
9
7
|
import { scopeKey } from "request";
|
|
10
8
|
|
|
9
|
+
import { createCache, createLazyCacheAdapter } from "../../utils";
|
|
10
|
+
|
|
11
11
|
describe("Cache [ Scoping ]", () => {
|
|
12
12
|
const response: ResponseSuccessType<unknown, HttpAdapterType> = {
|
|
13
13
|
data: { id: 1 },
|
|
@@ -20,10 +20,10 @@ describe("Cache [ Scoping ]", () => {
|
|
|
20
20
|
};
|
|
21
21
|
const details: ResponseDetailsType = {
|
|
22
22
|
retries: 0,
|
|
23
|
-
requestTimestamp:
|
|
24
|
-
responseTimestamp:
|
|
25
|
-
addedTimestamp:
|
|
26
|
-
triggerTimestamp:
|
|
23
|
+
requestTimestamp: Date.now(),
|
|
24
|
+
responseTimestamp: Date.now(),
|
|
25
|
+
addedTimestamp: Date.now(),
|
|
26
|
+
triggerTimestamp: Date.now(),
|
|
27
27
|
isCanceled: false,
|
|
28
28
|
isOffline: false,
|
|
29
29
|
willRetry: false,
|
|
@@ -41,7 +41,11 @@ describe("Cache [ Scoping ]", () => {
|
|
|
41
41
|
cache.set(a, { ...response, ...details, data: { id: 1 } });
|
|
42
42
|
cache.set(b, { ...response, ...details, data: { id: 2 } });
|
|
43
43
|
|
|
44
|
-
cache.set(a, (prev) => ({
|
|
44
|
+
cache.set(a, (prev) => ({
|
|
45
|
+
...response,
|
|
46
|
+
...details,
|
|
47
|
+
data: { id: ((prev?.data as { id: number } | undefined)?.id ?? 0) + 10 },
|
|
48
|
+
}));
|
|
45
49
|
|
|
46
50
|
expect(cache.get(scopeKey(base.cacheKey, "a"))?.data).toStrictEqual({ id: 11 });
|
|
47
51
|
expect(cache.get(scopeKey(base.cacheKey, "b"))?.data).toStrictEqual({ id: 2 });
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
2
|
import { expectFunctionParametersType, expectNotFunctionParametersType } from "@hyper-fetch/testing";
|
|
3
|
-
|
|
4
3
|
import type { ResponseSuccessType } from "adapter";
|
|
5
|
-
import type { ResponseDetailsType } from "managers";
|
|
6
4
|
import { Client } from "client";
|
|
7
5
|
import type { HttpAdapterType } from "http-adapter";
|
|
8
6
|
import { xhrExtra } from "http-adapter";
|
|
7
|
+
import type { ResponseDetailsType } from "managers";
|
|
8
|
+
|
|
9
9
|
import { createCache } from "../../utils";
|
|
10
10
|
|
|
11
11
|
interface UserResponse {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { getCacheData, getCacheByKey, getInvalidateByKey } from "cache";
|
|
2
1
|
import type { ResponseErrorType, ResponseSuccessType } from "adapter";
|
|
3
|
-
import
|
|
2
|
+
import { getCacheData, getCacheByKey, getInvalidateByKey } from "cache";
|
|
4
3
|
import type { HttpAdapterType } from "http-adapter";
|
|
5
4
|
import { xhrExtra } from "http-adapter";
|
|
5
|
+
import type { ResponseDetailsType } from "managers";
|
|
6
6
|
|
|
7
7
|
describe("Cache [ Utils ]", () => {
|
|
8
8
|
describe("when getCacheData function is used", () => {
|
|
@@ -20,10 +20,10 @@ describe("Cache [ Utils ]", () => {
|
|
|
20
20
|
success: false,
|
|
21
21
|
extra: xhrExtra,
|
|
22
22
|
retries: 0,
|
|
23
|
-
requestTimestamp:
|
|
24
|
-
responseTimestamp:
|
|
25
|
-
addedTimestamp:
|
|
26
|
-
triggerTimestamp:
|
|
23
|
+
requestTimestamp: Date.now(),
|
|
24
|
+
responseTimestamp: Date.now(),
|
|
25
|
+
addedTimestamp: Date.now(),
|
|
26
|
+
triggerTimestamp: Date.now(),
|
|
27
27
|
isCanceled: false,
|
|
28
28
|
isOffline: false,
|
|
29
29
|
willRetry: false,
|
|
@@ -54,10 +54,10 @@ describe("Cache [ Utils ]", () => {
|
|
|
54
54
|
success: true,
|
|
55
55
|
extra: xhrExtra,
|
|
56
56
|
retries: 0,
|
|
57
|
-
requestTimestamp:
|
|
58
|
-
responseTimestamp:
|
|
59
|
-
addedTimestamp:
|
|
60
|
-
triggerTimestamp:
|
|
57
|
+
requestTimestamp: Date.now(),
|
|
58
|
+
responseTimestamp: Date.now(),
|
|
59
|
+
addedTimestamp: Date.now(),
|
|
60
|
+
triggerTimestamp: Date.now(),
|
|
61
61
|
isCanceled: false,
|
|
62
62
|
isOffline: false,
|
|
63
63
|
willRetry: false,
|
|
@@ -69,10 +69,10 @@ describe("Cache [ Utils ]", () => {
|
|
|
69
69
|
success: true,
|
|
70
70
|
extra: xhrExtra,
|
|
71
71
|
retries: 0,
|
|
72
|
-
requestTimestamp:
|
|
73
|
-
responseTimestamp:
|
|
74
|
-
addedTimestamp:
|
|
75
|
-
triggerTimestamp:
|
|
72
|
+
requestTimestamp: Date.now(),
|
|
73
|
+
responseTimestamp: Date.now(),
|
|
74
|
+
addedTimestamp: Date.now(),
|
|
75
|
+
triggerTimestamp: Date.now(),
|
|
76
76
|
isCanceled: false,
|
|
77
77
|
isOffline: false,
|
|
78
78
|
willRetry: false,
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
-
|
|
2
|
+
import { Adapter } from "adapter";
|
|
3
3
|
import { Cache } from "cache";
|
|
4
|
+
import { Client } from "client";
|
|
4
5
|
import { Dispatcher } from "dispatcher";
|
|
5
6
|
import { AppManager } from "managers";
|
|
6
|
-
import { Client } from "client";
|
|
7
|
-
import { Adapter } from "adapter";
|
|
8
7
|
|
|
9
8
|
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
10
9
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Client } from "client";
|
|
2
1
|
import type { CacheValueType } from "cache";
|
|
2
|
+
import { Client } from "client";
|
|
3
3
|
import { xhrExtra } from "http-adapter";
|
|
4
4
|
import { scopeKey } from "request";
|
|
5
5
|
|
|
@@ -69,10 +69,10 @@ describe("Client [ Hydration ]", () => {
|
|
|
69
69
|
error: null,
|
|
70
70
|
success: true,
|
|
71
71
|
extra: {},
|
|
72
|
-
responseTimestamp:
|
|
73
|
-
requestTimestamp:
|
|
74
|
-
addedTimestamp:
|
|
75
|
-
triggerTimestamp:
|
|
72
|
+
responseTimestamp: Date.now(),
|
|
73
|
+
requestTimestamp: Date.now(),
|
|
74
|
+
addedTimestamp: Date.now(),
|
|
75
|
+
triggerTimestamp: Date.now(),
|
|
76
76
|
retries: 0,
|
|
77
77
|
isCanceled: false,
|
|
78
78
|
isOffline: false,
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
-
|
|
3
|
-
import { interceptorCallback } from "../../utils";
|
|
4
|
-
import { testCallbacksExecution } from "../../shared";
|
|
5
2
|
import { Client } from "client";
|
|
6
3
|
import { xhrExtra } from "http-adapter";
|
|
7
4
|
|
|
5
|
+
import { testCallbacksExecution } from "../../shared";
|
|
6
|
+
import { interceptorCallback } from "../../utils";
|
|
7
|
+
|
|
8
8
|
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
9
9
|
|
|
10
10
|
describe("Client [ Interceptor ]", () => {
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
-
|
|
3
|
-
import { Plugin } from "plugin";
|
|
4
2
|
import { Client } from "client";
|
|
3
|
+
import type { FetchAdapterOptionsType, QueryStringifyOptionsType } from "http-adapter";
|
|
4
|
+
import { LoggerManager } from "managers";
|
|
5
|
+
import { Plugin } from "plugin";
|
|
5
6
|
import type { RequestOptionsType } from "request";
|
|
6
7
|
import { Request } from "request";
|
|
7
|
-
|
|
8
|
+
|
|
8
9
|
import { createAdapter, interceptorCallback, middlewareCallback } from "../../utils";
|
|
9
|
-
import type { FetchAdapterOptionsType, QueryStringifyOptionsType } from "http-adapter";
|
|
10
10
|
|
|
11
11
|
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
12
12
|
|
|
@@ -36,7 +36,7 @@ describe("Client [ Methods ]", () => {
|
|
|
36
36
|
expect(req.method).toBe("POST");
|
|
37
37
|
});
|
|
38
38
|
it("should assign default adapter config [setAdapterDefaultOptions]", async () => {
|
|
39
|
-
const options: FetchAdapterOptionsType = { timeout:
|
|
39
|
+
const options: FetchAdapterOptionsType = { timeout: 12_312_312 };
|
|
40
40
|
client.adapter.setAdapterDefaults(() => options);
|
|
41
41
|
|
|
42
42
|
expect(client.adapter.unstable_getAdapterDefaults?.(request)).toEqual(options);
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
+
import { Client } from "client";
|
|
2
3
|
|
|
3
|
-
import { middlewareCallback } from "../../utils";
|
|
4
4
|
import { testCallbacksExecution } from "../../shared";
|
|
5
|
-
import {
|
|
5
|
+
import { middlewareCallback } from "../../utils";
|
|
6
6
|
|
|
7
7
|
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
8
8
|
|