@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
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import EventEmitter from "events";
|
|
2
1
|
import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
|
|
3
|
-
|
|
2
|
+
import { Client } from "client";
|
|
3
|
+
import EventEmitter from "events";
|
|
4
4
|
import type { RequestProgressEventType } from "managers";
|
|
5
5
|
import { getRequestManagerEvents } from "managers";
|
|
6
|
-
import { Client } from "client";
|
|
7
6
|
import type { RequestInstance } from "request";
|
|
8
7
|
|
|
9
8
|
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
@@ -176,18 +175,18 @@ describe("RequestManager [ Base ]", () => {
|
|
|
176
175
|
status: 200,
|
|
177
176
|
success: true,
|
|
178
177
|
extra: null,
|
|
179
|
-
requestTimestamp:
|
|
180
|
-
responseTimestamp:
|
|
178
|
+
requestTimestamp: Date.now(),
|
|
179
|
+
responseTimestamp: Date.now(),
|
|
181
180
|
},
|
|
182
181
|
details: {
|
|
183
182
|
retries: 0,
|
|
184
183
|
isCanceled: false,
|
|
185
184
|
isOffline: false,
|
|
186
185
|
willRetry: false,
|
|
187
|
-
requestTimestamp:
|
|
188
|
-
responseTimestamp:
|
|
189
|
-
triggerTimestamp:
|
|
190
|
-
addedTimestamp:
|
|
186
|
+
requestTimestamp: Date.now(),
|
|
187
|
+
responseTimestamp: Date.now(),
|
|
188
|
+
triggerTimestamp: Date.now(),
|
|
189
|
+
addedTimestamp: Date.now(),
|
|
191
190
|
},
|
|
192
191
|
});
|
|
193
192
|
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { waitFor } from "@testing-library/dom";
|
|
2
1
|
import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
|
|
3
|
-
|
|
2
|
+
import { waitFor } from "@testing-library/dom";
|
|
4
3
|
import { Client } from "client";
|
|
5
4
|
|
|
6
5
|
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { waitFor } from "@testing-library/dom";
|
|
2
1
|
import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
|
|
2
|
+
import { waitFor } from "@testing-library/dom";
|
|
3
3
|
|
|
4
|
-
import { createAdapter, createDispatcher } from "../../utils";
|
|
5
4
|
import type {
|
|
6
5
|
HttpAdapterType,
|
|
7
6
|
RequestInstance,
|
|
@@ -10,6 +9,7 @@ import type {
|
|
|
10
9
|
ResponseType,
|
|
11
10
|
} from "../../../src";
|
|
12
11
|
import { Client, getErrorMessage, mocker } from "../../../src";
|
|
12
|
+
import { createAdapter, createDispatcher } from "../../utils";
|
|
13
13
|
|
|
14
14
|
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
15
15
|
|
|
@@ -92,7 +92,7 @@ describe("Mocker [ Base ]", () => {
|
|
|
92
92
|
const firstSpy = vi.fn();
|
|
93
93
|
const secondSpy = vi.fn();
|
|
94
94
|
const firstRequest = client
|
|
95
|
-
.createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint" })
|
|
95
|
+
.createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint", deduplicate: false })
|
|
96
96
|
.setMock(
|
|
97
97
|
() => ({
|
|
98
98
|
data: fixture,
|
|
@@ -103,7 +103,7 @@ describe("Mocker [ Base ]", () => {
|
|
|
103
103
|
},
|
|
104
104
|
);
|
|
105
105
|
const secondRequest = client
|
|
106
|
-
.createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint" })
|
|
106
|
+
.createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint", deduplicate: false })
|
|
107
107
|
.setMock(
|
|
108
108
|
() => ({
|
|
109
109
|
data: fixture,
|
|
@@ -361,9 +361,9 @@ describe("Mocker [ Base ]", () => {
|
|
|
361
361
|
}),
|
|
362
362
|
{ requestTime: 100, responseTime: 100 },
|
|
363
363
|
);
|
|
364
|
-
const startDate =
|
|
364
|
+
const startDate = Date.now();
|
|
365
365
|
const response = await mockedRequest.send();
|
|
366
|
-
const endDate =
|
|
366
|
+
const endDate = Date.now();
|
|
367
367
|
expect(endDate - startDate).toBeGreaterThanOrEqual(200);
|
|
368
368
|
expect(response).toStrictEqual({
|
|
369
369
|
data: fixture,
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
+
import type { ClientInstance } from "client";
|
|
3
|
+
import { createClient } from "client";
|
|
4
|
+
import { HttpMethods } from "constants/http.constants";
|
|
5
|
+
import { getRequestDispatcher } from "request";
|
|
6
|
+
|
|
7
|
+
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Safe reads (GET, QUERY) deduplicate by default on the HTTP adapter: concurrent
|
|
11
|
+
* identical requests collapse into a single network call. Mutations keep the old
|
|
12
|
+
* behavior, and an explicit `deduplicate` always wins over the default.
|
|
13
|
+
*/
|
|
14
|
+
describe("Request [ Deduplicate defaults ]", () => {
|
|
15
|
+
let client: ClientInstance;
|
|
16
|
+
|
|
17
|
+
beforeAll(() => {
|
|
18
|
+
startServer();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
resetMocks();
|
|
23
|
+
client = createClient({ url: "http://localhost:3000" });
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
afterAll(() => {
|
|
27
|
+
stopServer();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should deduplicate safe reads by default and leave mutations off", () => {
|
|
31
|
+
expect(client.createRequest()({ endpoint: "/users", method: HttpMethods.GET }).deduplicate).toBe(true);
|
|
32
|
+
expect(client.createRequest()({ endpoint: "/users", method: HttpMethods.QUERY }).deduplicate).toBe(true);
|
|
33
|
+
// No method falls back to the adapter default (GET)
|
|
34
|
+
expect(client.createRequest()({ endpoint: "/users" }).deduplicate).toBe(true);
|
|
35
|
+
|
|
36
|
+
expect(client.createRequest()({ endpoint: "/users", method: HttpMethods.POST }).deduplicate).toBe(false);
|
|
37
|
+
expect(client.createRequest()({ endpoint: "/users", method: HttpMethods.PUT }).deduplicate).toBe(false);
|
|
38
|
+
expect(client.createRequest()({ endpoint: "/users", method: HttpMethods.PATCH }).deduplicate).toBe(false);
|
|
39
|
+
expect(client.createRequest()({ endpoint: "/users", method: HttpMethods.DELETE }).deduplicate).toBe(false);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should let explicit deduplicate settings win over the default", () => {
|
|
43
|
+
const optedOut = client.createRequest()({ endpoint: "/users", method: HttpMethods.GET, deduplicate: false });
|
|
44
|
+
expect(optedOut.deduplicate).toBe(false);
|
|
45
|
+
|
|
46
|
+
const optedIn = client.createRequest()({ endpoint: "/users", method: HttpMethods.POST, deduplicate: true });
|
|
47
|
+
expect(optedIn.deduplicate).toBe(true);
|
|
48
|
+
|
|
49
|
+
const setterWins = client.createRequest()({ endpoint: "/users", method: HttpMethods.GET }).setDeduplicate(false);
|
|
50
|
+
expect(setterWins.deduplicate).toBe(false);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("should route QUERY to the fetch dispatcher like other safe reads", () => {
|
|
54
|
+
const query = client.createRequest()({ endpoint: "/search", method: HttpMethods.QUERY });
|
|
55
|
+
const get = client.createRequest()({ endpoint: "/search", method: HttpMethods.GET });
|
|
56
|
+
const post = client.createRequest()({ endpoint: "/search", method: HttpMethods.POST });
|
|
57
|
+
|
|
58
|
+
expect(getRequestDispatcher(query)[1]).toBe(true);
|
|
59
|
+
expect(getRequestDispatcher(get)[1]).toBe(true);
|
|
60
|
+
expect(getRequestDispatcher(post)[1]).toBe(false);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should collapse concurrent identical GET requests into one call", async () => {
|
|
64
|
+
const request = client.createRequest<{ response: { ok: boolean } }>()({
|
|
65
|
+
endpoint: "/users",
|
|
66
|
+
method: HttpMethods.GET,
|
|
67
|
+
});
|
|
68
|
+
mockRequest(request, { data: { ok: true } });
|
|
69
|
+
|
|
70
|
+
const deduplicatedSpy = vi.fn();
|
|
71
|
+
client.requestManager.events.onDeduplicated(deduplicatedSpy);
|
|
72
|
+
|
|
73
|
+
const [first, second] = await Promise.all([request.send(), request.send()]);
|
|
74
|
+
|
|
75
|
+
expect(first.data).toEqual({ ok: true });
|
|
76
|
+
expect(second.data).toEqual({ ok: true });
|
|
77
|
+
expect(deduplicatedSpy).toHaveBeenCalledTimes(1);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { waitFor } from "@testing-library/dom";
|
|
2
1
|
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
3
|
-
|
|
2
|
+
import { waitFor } from "@testing-library/dom";
|
|
4
3
|
import { Client } from "client";
|
|
5
4
|
|
|
6
5
|
const { resetMocks, startServer, stopServer, mockRequest } = createHttpMockingServer();
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { waitFor } from "@testing-library/dom";
|
|
2
1
|
import { createHttpMockingServer, sleep } from "@hyper-fetch/testing";
|
|
3
|
-
|
|
2
|
+
import { waitFor } from "@testing-library/dom";
|
|
4
3
|
import { getErrorMessage } from "adapter";
|
|
5
4
|
import { Client } from "client";
|
|
6
5
|
import { xhrExtra } from "http-adapter";
|
|
@@ -34,7 +33,11 @@ describe("Request [ Sending ]", () => {
|
|
|
34
33
|
const requestExecution = request.exec({});
|
|
35
34
|
await sleep(5);
|
|
36
35
|
expect(client.fetchDispatcher.getAllRunningRequests()).toHaveLength(0);
|
|
37
|
-
const {
|
|
36
|
+
const {
|
|
37
|
+
responseTimestamp: _responseTimestamp,
|
|
38
|
+
requestTimestamp: _requestTimestamp,
|
|
39
|
+
...response
|
|
40
|
+
} = await requestExecution;
|
|
38
41
|
expect(response).toStrictEqual({
|
|
39
42
|
data: fixture,
|
|
40
43
|
error: null,
|
|
@@ -49,7 +52,11 @@ describe("Request [ Sending ]", () => {
|
|
|
49
52
|
.exec({});
|
|
50
53
|
await sleep(5);
|
|
51
54
|
expect(client.fetchDispatcher.getAllRunningRequests()).toHaveLength(0);
|
|
52
|
-
const {
|
|
55
|
+
const {
|
|
56
|
+
responseTimestamp: _responseTimestamp,
|
|
57
|
+
requestTimestamp: _requestTimestamp,
|
|
58
|
+
...response
|
|
59
|
+
} = await requestExecution;
|
|
53
60
|
expect(response).toStrictEqual({
|
|
54
61
|
data: { nested: fixture },
|
|
55
62
|
error: null,
|
|
@@ -61,7 +68,11 @@ describe("Request [ Sending ]", () => {
|
|
|
61
68
|
});
|
|
62
69
|
describe("When using request's send method", () => {
|
|
63
70
|
it("should return adapter response", async () => {
|
|
64
|
-
const {
|
|
71
|
+
const {
|
|
72
|
+
responseTimestamp: _responseTimestamp,
|
|
73
|
+
requestTimestamp: _requestTimestamp,
|
|
74
|
+
...response
|
|
75
|
+
} = await request.send({});
|
|
65
76
|
|
|
66
77
|
expect(response).toStrictEqual({
|
|
67
78
|
data: fixture,
|
|
@@ -72,9 +83,11 @@ describe("Request [ Sending ]", () => {
|
|
|
72
83
|
});
|
|
73
84
|
});
|
|
74
85
|
it("should return mapped adapter response", async () => {
|
|
75
|
-
const {
|
|
76
|
-
|
|
77
|
-
|
|
86
|
+
const {
|
|
87
|
+
responseTimestamp: _responseTimestamp,
|
|
88
|
+
requestTimestamp: _requestTimestamp,
|
|
89
|
+
...response
|
|
90
|
+
} = await request.setResponseMapper((res) => ({ ...res, data: { nested: res.data } }) as typeof res).send({});
|
|
78
91
|
|
|
79
92
|
expect(response).toStrictEqual({
|
|
80
93
|
data: { nested: fixture },
|
|
@@ -85,8 +98,12 @@ describe("Request [ Sending ]", () => {
|
|
|
85
98
|
});
|
|
86
99
|
});
|
|
87
100
|
it("should return async mapped adapter response", async () => {
|
|
88
|
-
const {
|
|
89
|
-
|
|
101
|
+
const {
|
|
102
|
+
responseTimestamp: _responseTimestamp,
|
|
103
|
+
requestTimestamp: _requestTimestamp,
|
|
104
|
+
...response
|
|
105
|
+
} = await request
|
|
106
|
+
.setResponseMapper(async (res) => ({ ...res, data: { nested: res.data } }) as typeof res)
|
|
90
107
|
.send({});
|
|
91
108
|
|
|
92
109
|
expect(response).toStrictEqual({
|
|
@@ -111,7 +128,11 @@ describe("Request [ Sending ]", () => {
|
|
|
111
128
|
unmount();
|
|
112
129
|
});
|
|
113
130
|
|
|
114
|
-
const {
|
|
131
|
+
const {
|
|
132
|
+
responseTimestamp: _responseTimestamp,
|
|
133
|
+
requestTimestamp: _requestTimestamp,
|
|
134
|
+
...response
|
|
135
|
+
} = await requestExecution;
|
|
115
136
|
expect(response).toStrictEqual({
|
|
116
137
|
data: fixture,
|
|
117
138
|
error: null,
|
|
@@ -133,7 +154,11 @@ describe("Request [ Sending ]", () => {
|
|
|
133
154
|
unmount();
|
|
134
155
|
});
|
|
135
156
|
|
|
136
|
-
const {
|
|
157
|
+
const {
|
|
158
|
+
responseTimestamp: _responseTimestamp,
|
|
159
|
+
requestTimestamp: _requestTimestamp,
|
|
160
|
+
...response
|
|
161
|
+
} = await requestExecution;
|
|
137
162
|
expect(response).toStrictEqual({
|
|
138
163
|
data: fixture,
|
|
139
164
|
error: null,
|
|
@@ -151,7 +176,11 @@ describe("Request [ Sending ]", () => {
|
|
|
151
176
|
const runningRequests = client.fetchDispatcher.getAllRunningRequests();
|
|
152
177
|
client.fetchDispatcher.delete(request.queryKey, runningRequests[0].requestId, request.abortKey);
|
|
153
178
|
|
|
154
|
-
const {
|
|
179
|
+
const {
|
|
180
|
+
responseTimestamp: _responseTimestamp,
|
|
181
|
+
requestTimestamp: _requestTimestamp,
|
|
182
|
+
...response
|
|
183
|
+
} = await requestExecution;
|
|
155
184
|
expect(response).toStrictEqual({
|
|
156
185
|
data: null,
|
|
157
186
|
error: getErrorMessage("deleted"),
|
|
@@ -179,9 +208,9 @@ describe("Request [ Sending ]", () => {
|
|
|
179
208
|
const mock = mockRequest(newRequest);
|
|
180
209
|
|
|
181
210
|
const [
|
|
182
|
-
{ requestTimestamp:
|
|
183
|
-
{ requestTimestamp:
|
|
184
|
-
{ requestTimestamp:
|
|
211
|
+
{ requestTimestamp: _requestTimestamp1, responseTimestamp: _responseTimestamp1, ...res1 },
|
|
212
|
+
{ requestTimestamp: _requestTimestamp2, responseTimestamp: _responseTimestamp2, ...res2 },
|
|
213
|
+
{ requestTimestamp: _requestTimestamp3, responseTimestamp: _responseTimestamp3, ...res3 },
|
|
185
214
|
] = await Promise.all([newRequest.send({}), newRequest.send({}), newRequest.send({})]);
|
|
186
215
|
|
|
187
216
|
expect(res1).toStrictEqual({
|
|
@@ -219,9 +248,9 @@ describe("Request [ Sending ]", () => {
|
|
|
219
248
|
const mock = mockRequest(getUsers);
|
|
220
249
|
|
|
221
250
|
const [
|
|
222
|
-
{ requestTimestamp:
|
|
223
|
-
{ requestTimestamp:
|
|
224
|
-
{ requestTimestamp:
|
|
251
|
+
{ requestTimestamp: _requestTimestamp1, responseTimestamp: _responseTimestamp1, ...res1 },
|
|
252
|
+
{ requestTimestamp: _requestTimestamp2, responseTimestamp: _responseTimestamp2, ...res2 },
|
|
253
|
+
{ requestTimestamp: _requestTimestamp3, responseTimestamp: _responseTimestamp3, ...res3 },
|
|
225
254
|
] = await Promise.all([getUsers.send({}), getUsers.send({}), getUsers.send({})]);
|
|
226
255
|
|
|
227
256
|
expect(res1).toStrictEqual({
|
|
@@ -279,7 +308,11 @@ describe("Request [ Sending ]", () => {
|
|
|
279
308
|
await r.send({});
|
|
280
309
|
fetchSpy.mockClear();
|
|
281
310
|
|
|
282
|
-
const {
|
|
311
|
+
const {
|
|
312
|
+
responseTimestamp: _responseTimestamp,
|
|
313
|
+
requestTimestamp: _requestTimestamp,
|
|
314
|
+
...response
|
|
315
|
+
} = await r.send({ cachePolicy: "cache-first" });
|
|
283
316
|
|
|
284
317
|
expect(response).toStrictEqual({
|
|
285
318
|
data: fixture,
|
|
@@ -308,7 +341,11 @@ describe("Request [ Sending ]", () => {
|
|
|
308
341
|
fetchSpy.mockClear();
|
|
309
342
|
fetchSpy.mockImplementation(() => ({ data: { refreshed: true } as any, status: 200 }));
|
|
310
343
|
|
|
311
|
-
const {
|
|
344
|
+
const {
|
|
345
|
+
responseTimestamp: _responseTimestamp,
|
|
346
|
+
requestTimestamp: _requestTimestamp,
|
|
347
|
+
...immediate
|
|
348
|
+
} = await r.send({ cachePolicy: "revalidate" });
|
|
312
349
|
|
|
313
350
|
expect(immediate.data).toStrictEqual(fixture);
|
|
314
351
|
|
|
@@ -326,14 +363,22 @@ describe("Request [ Sending ]", () => {
|
|
|
326
363
|
const r = request.setMock(fetchSpy);
|
|
327
364
|
client.cache.clear();
|
|
328
365
|
|
|
329
|
-
const {
|
|
366
|
+
const {
|
|
367
|
+
responseTimestamp: _responseTimestamp,
|
|
368
|
+
requestTimestamp: _requestTimestamp,
|
|
369
|
+
...response
|
|
370
|
+
} = await r.send({ cachePolicy: "revalidate" });
|
|
330
371
|
|
|
331
372
|
expect(response.data).toStrictEqual(fixture);
|
|
332
373
|
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
333
374
|
});
|
|
334
375
|
|
|
335
376
|
it("network-only matches default send", async () => {
|
|
336
|
-
const {
|
|
377
|
+
const {
|
|
378
|
+
responseTimestamp: _responseTimestamp,
|
|
379
|
+
requestTimestamp: _requestTimestamp,
|
|
380
|
+
...response
|
|
381
|
+
} = await request.send({
|
|
337
382
|
cachePolicy: "network-only",
|
|
338
383
|
});
|
|
339
384
|
|
|
@@ -354,7 +399,11 @@ describe("Request [ Sending ]", () => {
|
|
|
354
399
|
await r.send({});
|
|
355
400
|
fetchSpy.mockClear();
|
|
356
401
|
|
|
357
|
-
const {
|
|
402
|
+
const {
|
|
403
|
+
responseTimestamp: _responseTimestamp,
|
|
404
|
+
requestTimestamp: _requestTimestamp,
|
|
405
|
+
...response
|
|
406
|
+
} = await r.send({ cachePolicy: "cache-first" });
|
|
358
407
|
|
|
359
408
|
expect(response.data).toStrictEqual({ nested: fixture });
|
|
360
409
|
expect(fetchSpy).not.toHaveBeenCalled();
|
|
@@ -105,9 +105,10 @@ describe("Request [ Setters ]", () => {
|
|
|
105
105
|
expect(updatedRequest.queryKey).toBe("test");
|
|
106
106
|
});
|
|
107
107
|
it("should allow for setting deduplicate", async () => {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
108
|
+
// GET requests deduplicate by default on the HTTP adapter
|
|
109
|
+
expect(request.deduplicate).toBeTrue();
|
|
110
|
+
const updatedRequest = request.setDeduplicate(false);
|
|
111
|
+
expect(updatedRequest.deduplicate).toBeFalse();
|
|
111
112
|
});
|
|
112
113
|
it("should allow for setting deduplicate time", async () => {
|
|
113
114
|
expect(request.deduplicateTime).toBe(null);
|
|
@@ -8,6 +8,7 @@ describe("Request [ Utils ]", () => {
|
|
|
8
8
|
const someString = "123";
|
|
9
9
|
expect(stringifyKey(response)).toBe(JSON.stringify(response));
|
|
10
10
|
expect(stringifyKey(someString)).toBe(someString);
|
|
11
|
+
// oxlint-disable-next-line unicorn/no-useless-undefined
|
|
11
12
|
expect(stringifyKey(undefined)).toBe("");
|
|
12
13
|
expect(stringifyKey(null)).toBe("");
|
|
13
14
|
});
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
-
|
|
3
2
|
import type { Client } from "client";
|
|
4
3
|
import { createClient } from "client";
|
|
5
4
|
import type { HttpAdapterType } from "http-adapter";
|
|
6
|
-
import type {
|
|
5
|
+
import type { RequestModel } from "request";
|
|
7
6
|
import { Request } from "request";
|
|
8
7
|
import { createSdk } from "sdk";
|
|
9
8
|
|
|
@@ -11,28 +10,25 @@ const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
|
11
10
|
|
|
12
11
|
type TestClient = Client<Error, HttpAdapterType>;
|
|
13
12
|
|
|
13
|
+
// Schema does not need to declare `client` per request - the SDK injects it from createSdk(client).
|
|
14
14
|
type TestSchema = {
|
|
15
15
|
users: {
|
|
16
|
-
$get:
|
|
17
|
-
client: TestClient;
|
|
16
|
+
$get: RequestModel<{
|
|
18
17
|
queryParams: { page: number; limit: number };
|
|
19
18
|
endpoint: "/users";
|
|
20
19
|
}>;
|
|
21
20
|
$userId: {
|
|
22
|
-
$get:
|
|
23
|
-
client: TestClient;
|
|
21
|
+
$get: RequestModel<{
|
|
24
22
|
response: { id: string; name: string };
|
|
25
23
|
endpoint: "/users/:userId";
|
|
26
24
|
}>;
|
|
27
|
-
$delete:
|
|
28
|
-
client: TestClient;
|
|
25
|
+
$delete: RequestModel<{
|
|
29
26
|
response: { id: string; name: string };
|
|
30
27
|
endpoint: "/users/:userId";
|
|
31
28
|
}>;
|
|
32
29
|
posts: {
|
|
33
30
|
$postId: {
|
|
34
|
-
$delete:
|
|
35
|
-
client: TestClient;
|
|
31
|
+
$delete: RequestModel<{
|
|
36
32
|
response: { id: string; title: string };
|
|
37
33
|
endpoint: "/users/:userId/posts/:postId";
|
|
38
34
|
}>;
|
|
@@ -41,8 +37,7 @@ type TestSchema = {
|
|
|
41
37
|
};
|
|
42
38
|
};
|
|
43
39
|
acceptInvitation: {
|
|
44
|
-
$post:
|
|
45
|
-
client: TestClient;
|
|
40
|
+
$post: RequestModel<{
|
|
46
41
|
endpoint: "/accept-invitation";
|
|
47
42
|
}>;
|
|
48
43
|
};
|
|
@@ -1,29 +1,27 @@
|
|
|
1
1
|
import type { Client } from "client";
|
|
2
2
|
import { createClient } from "client";
|
|
3
3
|
import type { HttpAdapterType } from "http-adapter";
|
|
4
|
-
import type { RequestInstance } from "request";
|
|
4
|
+
import type { RequestInstance, RequestModel } from "request";
|
|
5
5
|
import { Request } from "request";
|
|
6
6
|
import { createSdk, createConfiguration } from "sdk";
|
|
7
7
|
|
|
8
8
|
type TestClient = Client<Error, HttpAdapterType>;
|
|
9
9
|
|
|
10
|
+
// Schema does not need to declare `client` per request - the SDK injects it from createSdk(client).
|
|
10
11
|
type TestSchema = {
|
|
11
12
|
users: {
|
|
12
|
-
$get:
|
|
13
|
-
client: TestClient;
|
|
13
|
+
$get: RequestModel<{
|
|
14
14
|
endpoint: "/users";
|
|
15
15
|
}>;
|
|
16
16
|
$userId: {
|
|
17
|
-
$get:
|
|
18
|
-
client: TestClient;
|
|
17
|
+
$get: RequestModel<{
|
|
19
18
|
response: { id: string; name: string };
|
|
20
19
|
endpoint: "/users/:userId";
|
|
21
20
|
}>;
|
|
22
21
|
};
|
|
23
22
|
};
|
|
24
23
|
posts: {
|
|
25
|
-
$get:
|
|
26
|
-
client: TestClient;
|
|
24
|
+
$get: RequestModel<{
|
|
27
25
|
endpoint: "/posts";
|
|
28
26
|
}>;
|
|
29
27
|
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { Client } from "client";
|
|
2
|
+
import { createClient } from "client";
|
|
3
|
+
import type { HttpAdapterType } from "http-adapter";
|
|
4
|
+
import type { RequestModel } from "request";
|
|
5
|
+
import { createSdk } from "sdk";
|
|
6
|
+
|
|
7
|
+
type TestClient = Client<Error, HttpAdapterType>;
|
|
8
|
+
|
|
9
|
+
type TestSchema = {
|
|
10
|
+
users: {
|
|
11
|
+
$get: RequestModel<{
|
|
12
|
+
response: { id: string; name: string }[];
|
|
13
|
+
endpoint: "/users";
|
|
14
|
+
}>;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The SDK proxy decides between "this key is a request property" and "this key is a deeper
|
|
20
|
+
* API path" with an `in` check on the request instance. Optional class fields like
|
|
21
|
+
* `unstable_responseMapper` are declared without initializers, and some transpilers drop
|
|
22
|
+
* uninitialized class fields entirely - Metro (React Native) compiles class properties in
|
|
23
|
+
* loose mode, so a bundled Request instance never owns them. The `in` check then fails and
|
|
24
|
+
* the proxy hands back a nested path proxy: a truthy object. Anything that does
|
|
25
|
+
* `request.unstable_responseMapper?.(response)` crashes with "Object is not a function",
|
|
26
|
+
* and every `if (request.retryOnError)`-style feature check silently misfires.
|
|
27
|
+
*/
|
|
28
|
+
describe("SDK [ Request fields ]", () => {
|
|
29
|
+
// Every field the runtime feature-detects with a truthiness check. If one of these leaks
|
|
30
|
+
// a path proxy instead of undefined, requests break in ways that don't throw at the source.
|
|
31
|
+
const optionalFields = [
|
|
32
|
+
"unstable_mock",
|
|
33
|
+
"unstable_payloadMapper",
|
|
34
|
+
"unstable_requestMapper",
|
|
35
|
+
"unstable_responseMapper",
|
|
36
|
+
"retryOnError",
|
|
37
|
+
"optimistic",
|
|
38
|
+
] as const;
|
|
39
|
+
|
|
40
|
+
let client: TestClient;
|
|
41
|
+
|
|
42
|
+
beforeEach(() => {
|
|
43
|
+
client = createClient<{ error: Error }>({ url: "http://localhost:3000" });
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should expose optional request fields as own properties of a fresh request", () => {
|
|
47
|
+
const request = client.createRequest()({ endpoint: "/users", method: "GET" });
|
|
48
|
+
const ownProperties = Object.getOwnPropertyNames(request);
|
|
49
|
+
|
|
50
|
+
optionalFields.forEach((field) => {
|
|
51
|
+
expect(ownProperties).toContain(field);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("should return undefined for unset optional fields on SDK leaves", () => {
|
|
56
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
57
|
+
|
|
58
|
+
optionalFields.forEach((field) => {
|
|
59
|
+
expect(sdk.users.$get[field]).toBeUndefined();
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should not leak a path proxy when a transpiler drops uninitialized class fields", () => {
|
|
64
|
+
// Simulate Metro's loose class-property transform: the instance no longer owns the field.
|
|
65
|
+
const originalCreateRequest = client.createRequest.bind(client);
|
|
66
|
+
client.createRequest = (() => (options: { endpoint: string; method?: string }) => {
|
|
67
|
+
const request = originalCreateRequest()(options as never);
|
|
68
|
+
optionalFields.forEach((field) => {
|
|
69
|
+
delete (request as unknown as Record<string, unknown>)[field];
|
|
70
|
+
});
|
|
71
|
+
return request;
|
|
72
|
+
}) as typeof client.createRequest;
|
|
73
|
+
|
|
74
|
+
const sdk = createSdk<TestClient, TestSchema>(client);
|
|
75
|
+
|
|
76
|
+
optionalFields.forEach((field) => {
|
|
77
|
+
expect(sdk.users.$get[field]).toBeUndefined();
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
});
|