@hyper-fetch/core 7.5.3 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.cjs +12 -0
- package/__tests__/e2e/e2e.abort-timeout.spec.ts +101 -0
- package/__tests__/e2e/e2e.advanced.spec.ts +423 -0
- package/__tests__/e2e/e2e.methods.spec.ts +243 -0
- package/__tests__/e2e/e2e.payloads.spec.ts +154 -0
- package/__tests__/features/adapter/adapter.base.spec.ts +44 -17
- package/__tests__/features/adapter/adapter.bindings.spec.ts +35 -33
- package/__tests__/features/adapter/adapter.browser.spec.ts +5 -20
- package/__tests__/features/adapter/adapter.server.spec.ts +229 -37
- package/__tests__/features/cache/cache.base.spec.ts +23 -15
- package/__tests__/features/cache/cache.events.spec.ts +9 -5
- package/__tests__/features/cache/cache.garbage-collector.spec.ts +9 -6
- package/__tests__/features/cache/cache.lazy-storage.spec.ts +11 -8
- package/__tests__/features/cache/cache.modes.spec.ts +145 -0
- package/__tests__/features/cache/cache.scoping.spec.ts +171 -0
- package/__tests__/features/cache/cache.typescript.spec.ts +147 -0
- package/__tests__/features/cache/cache.utils.spec.ts +10 -3
- package/__tests__/features/client/client.auth.spec.ts +5 -5
- package/__tests__/features/client/client.base.spec.ts +16 -4
- package/__tests__/features/client/client.from-json.spec.ts +182 -0
- package/__tests__/features/client/client.hydration.spec.ts +69 -4
- package/__tests__/features/client/client.interceptor.spec.ts +4 -4
- package/__tests__/features/client/client.methods.spec.ts +5 -4
- package/__tests__/features/client/client.middleware.spec.ts +4 -4
- package/__tests__/features/client/client.utils.spec.ts +3 -3
- package/__tests__/features/dispatcher/dispatcher.base.spec.ts +8 -8
- package/__tests__/features/dispatcher/dispatcher.custom-storage.spec.ts +189 -0
- package/__tests__/features/dispatcher/dispatcher.events.spec.ts +17 -12
- package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +31 -31
- package/__tests__/features/dispatcher/dispatcher.requests.spec.ts +15 -15
- package/__tests__/features/dispatcher/dispatcher.utils.spec.ts +22 -5
- package/__tests__/features/managers/app/app.manager.base.spec.ts +23 -23
- package/__tests__/features/managers/app/app.manager.ssr.spec.ts +6 -3
- package/__tests__/features/managers/logger/logger.base.spec.ts +4 -2
- package/__tests__/features/managers/logger/logger.utils.spec.ts +10 -4
- package/__tests__/features/managers/request/request.manager.base.spec.ts +21 -19
- package/__tests__/features/managers/request/request.manager.events.spec.ts +137 -53
- package/__tests__/features/mocker/mocker.base.spec.ts +82 -10
- package/__tests__/features/plugin/plugin.base.spec.ts +19 -19
- package/__tests__/features/request/request.base.spec.ts +39 -13
- package/__tests__/features/request/request.cloning.spec.ts +1 -1
- package/__tests__/features/request/request.hooks.spec.ts +173 -0
- package/__tests__/features/request/request.optimistic.spec.ts +93 -0
- package/__tests__/features/request/request.retry-on-error.spec.ts +359 -0
- package/__tests__/features/request/request.sending.spec.ts +107 -15
- package/__tests__/features/request/request.setters.spec.ts +11 -4
- package/__tests__/features/request/request.typescript.spec.ts +1 -1
- package/__tests__/features/request/request.utils.spec.ts +2 -2
- package/__tests__/features/sdk/sdk.base.spec.ts +5 -3
- package/__tests__/features/sdk/sdk.configuration.spec.ts +262 -0
- package/__tests__/features/utils/emitter.utils.spec.ts +88 -0
- package/__tests__/shared/callbacks.shared.ts +3 -1
- package/__tests__/shared/progress.shared.ts +2 -1
- package/__tests__/tsconfig.json +14 -3
- package/__tests__/type-stress/generate-stress-test.ts +92 -0
- package/__tests__/type-stress/stress-10.ts +35 -0
- package/__tests__/type-stress/stress-just-create.ts +2107 -0
- package/__tests__/type-stress/stress-minimal.ts +23 -0
- package/__tests__/type-stress/stress-test-500.ts +2319 -0
- package/__tests__/type-stress/stress-test-sdk.ts +210 -0
- package/__tests__/type-stress/tsconfig.json +6 -0
- package/__tests__/utils/adapter.utils.ts +3 -2
- package/__tests__/utils/cache.utils.ts +3 -2
- package/__tests__/utils/callbacks.utils.ts +5 -4
- package/__tests__/utils/dispatcher.utils.ts +3 -2
- package/__tests__/vitest.setup.ts +15 -0
- package/dist/adapter/adapter.bindings.d.ts +71 -0
- package/dist/adapter/adapter.bindings.d.ts.map +1 -0
- package/dist/adapter/adapter.d.ts +127 -0
- package/dist/adapter/adapter.d.ts.map +1 -0
- package/dist/adapter/adapter.types.d.ts +108 -0
- package/dist/adapter/adapter.types.d.ts.map +1 -0
- package/dist/adapter/adapter.utils.d.ts +23 -0
- package/dist/adapter/adapter.utils.d.ts.map +1 -0
- package/dist/adapter/index.d.ts +5 -0
- package/dist/adapter/index.d.ts.map +1 -0
- package/dist/cache/cache.d.ts +95 -0
- package/dist/cache/cache.d.ts.map +1 -0
- package/dist/cache/cache.events.d.ts +51 -0
- package/dist/cache/cache.events.d.ts.map +1 -0
- package/dist/cache/cache.types.d.ts +44 -0
- package/dist/cache/cache.types.d.ts.map +1 -0
- package/dist/cache/cache.utils.d.ts +11 -0
- package/dist/cache/cache.utils.d.ts.map +1 -0
- package/dist/cache/index.d.ts +5 -0
- package/dist/cache/index.d.ts.map +1 -0
- package/dist/client/client.constants.d.ts +10 -0
- package/dist/client/client.constants.d.ts.map +1 -0
- package/dist/client/client.create.d.ts +8 -0
- package/dist/client/client.create.d.ts.map +1 -0
- package/dist/client/client.d.ts +195 -0
- package/dist/client/client.d.ts.map +1 -0
- package/dist/client/client.types.d.ts +71 -0
- package/dist/client/client.types.d.ts.map +1 -0
- package/dist/client/client.utils.d.ts +11 -0
- package/dist/client/client.utils.d.ts.map +1 -0
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/constants/http.constants.d.ts +8 -0
- package/dist/constants/http.constants.d.ts.map +1 -0
- package/dist/constants/time.constants.d.ts +12 -0
- package/dist/constants/time.constants.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.constants.d.ts +7 -0
- package/dist/dispatcher/dispatcher.constants.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.d.ts +168 -0
- package/dist/dispatcher/dispatcher.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.events.d.ts +49 -0
- package/dist/dispatcher/dispatcher.events.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.types.d.ts +44 -0
- package/dist/dispatcher/dispatcher.types.d.ts.map +1 -0
- package/dist/dispatcher/dispatcher.utils.d.ts +12 -0
- package/dist/dispatcher/dispatcher.utils.d.ts.map +1 -0
- package/dist/dispatcher/index.d.ts +6 -0
- package/dist/dispatcher/index.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.constants.d.ts +4 -0
- package/dist/http-adapter/http-adapter.constants.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.d.ts +3 -0
- package/dist/http-adapter/http-adapter.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.fetch.d.ts +6 -0
- package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.types.d.ts +73 -0
- package/dist/http-adapter/http-adapter.types.d.ts.map +1 -0
- package/dist/http-adapter/http-adapter.utils.d.ts +10 -0
- package/dist/http-adapter/http-adapter.utils.d.ts.map +1 -0
- package/dist/http-adapter/index.d.ts +6 -0
- package/dist/http-adapter/index.d.ts.map +1 -0
- package/dist/index.d.ts +15 -2083
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +4084 -0
- package/dist/index.mjs.map +1 -0
- package/dist/managers/app/app.manager.constants.d.ts +10 -0
- package/dist/managers/app/app.manager.constants.d.ts.map +1 -0
- package/dist/managers/app/app.manager.d.ts +35 -0
- package/dist/managers/app/app.manager.d.ts.map +1 -0
- package/dist/managers/app/app.manager.events.d.ts +12 -0
- package/dist/managers/app/app.manager.events.d.ts.map +1 -0
- package/dist/managers/app/app.manager.types.d.ts +7 -0
- package/dist/managers/app/app.manager.types.d.ts.map +1 -0
- package/dist/managers/app/app.manager.utils.d.ts +5 -0
- package/dist/managers/app/app.manager.utils.d.ts.map +1 -0
- package/dist/managers/app/index.d.ts +6 -0
- package/dist/managers/app/index.d.ts.map +1 -0
- package/dist/managers/index.d.ts +4 -0
- package/dist/managers/index.d.ts.map +1 -0
- package/dist/managers/logger/index.d.ts +5 -0
- package/dist/managers/logger/index.d.ts.map +1 -0
- package/dist/managers/logger/logger.manager.constants.d.ts +5 -0
- package/dist/managers/logger/logger.manager.constants.d.ts.map +1 -0
- package/dist/managers/logger/logger.manager.d.ts +22 -0
- package/dist/managers/logger/logger.manager.d.ts.map +1 -0
- package/dist/managers/logger/logger.manager.types.d.ts +43 -0
- package/dist/managers/logger/logger.manager.types.d.ts.map +1 -0
- package/dist/managers/logger/logger.manager.utils.d.ts +4 -0
- package/dist/managers/logger/logger.manager.utils.d.ts.map +1 -0
- package/dist/managers/request/index.d.ts +5 -0
- package/dist/managers/request/index.d.ts.map +1 -0
- package/dist/managers/request/request.manager.d.ts +60 -0
- package/dist/managers/request/request.manager.d.ts.map +1 -0
- package/dist/managers/request/request.manager.events.d.ts +55 -0
- package/dist/managers/request/request.manager.events.d.ts.map +1 -0
- package/dist/managers/request/request.manager.types.d.ts +48 -0
- package/dist/managers/request/request.manager.types.d.ts.map +1 -0
- package/dist/managers/request/request.manager.utils.d.ts +30 -0
- package/dist/managers/request/request.manager.utils.d.ts.map +1 -0
- package/dist/mocker/index.d.ts +3 -0
- package/dist/mocker/index.d.ts.map +1 -0
- package/dist/mocker/mocker.d.ts +3 -0
- package/dist/mocker/mocker.d.ts.map +1 -0
- package/dist/mocker/mocker.types.d.ts +25 -0
- package/dist/mocker/mocker.types.d.ts.map +1 -0
- package/dist/plugin/index.d.ts +3 -0
- package/dist/plugin/index.d.ts.map +1 -0
- package/dist/plugin/plugin.d.ts +55 -0
- package/dist/plugin/plugin.d.ts.map +1 -0
- package/dist/plugin/plugin.types.d.ts +122 -0
- package/dist/plugin/plugin.types.d.ts.map +1 -0
- package/dist/request/index.d.ts +5 -0
- package/dist/request/index.d.ts.map +1 -0
- package/dist/request/request.d.ts +195 -0
- package/dist/request/request.d.ts.map +1 -0
- package/dist/request/request.hooks.d.ts +19 -0
- package/dist/request/request.hooks.d.ts.map +1 -0
- package/dist/request/request.types.d.ts +240 -0
- package/dist/request/request.types.d.ts.map +1 -0
- package/dist/request/request.utils.d.ts +25 -0
- package/dist/request/request.utils.d.ts.map +1 -0
- package/dist/sdk/index.d.ts +2 -0
- package/dist/sdk/index.d.ts.map +1 -0
- package/dist/sdk/sdk.d.ts +69 -0
- package/dist/sdk/sdk.d.ts.map +1 -0
- package/dist/types/adapter.types.d.ts +22 -0
- package/dist/types/adapter.types.d.ts.map +1 -0
- package/dist/types/client.types.d.ts +4 -0
- package/dist/types/client.types.d.ts.map +1 -0
- package/dist/types/helpers.types.d.ts +17 -0
- package/dist/types/helpers.types.d.ts.map +1 -0
- package/dist/types/http.types.d.ts +3 -0
- package/dist/types/http.types.d.ts.map +1 -0
- package/dist/types/hydration.types.d.ts +15 -0
- package/dist/types/hydration.types.d.ts.map +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/request.types.d.ts +34 -0
- package/dist/types/request.types.d.ts.map +1 -0
- package/dist/utils/emitter.utils.d.ts +13 -0
- package/dist/utils/emitter.utils.d.ts.map +1 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/uuid.utils.d.ts +3 -0
- package/dist/utils/uuid.utils.d.ts.map +1 -0
- package/package.json +10 -25
- package/src/adapter/adapter.bindings.ts +8 -12
- package/src/adapter/adapter.ts +8 -7
- package/src/adapter/adapter.types.ts +10 -6
- package/src/adapter/adapter.utils.ts +1 -1
- package/src/cache/cache.events.ts +4 -11
- package/src/cache/cache.ts +28 -22
- package/src/cache/cache.types.ts +8 -4
- package/src/cache/cache.utils.ts +3 -3
- package/src/client/client.create.ts +4 -3
- package/src/client/client.ts +44 -12
- package/src/client/client.types.ts +25 -3
- package/src/client/client.utils.ts +24 -4
- package/src/dispatcher/dispatcher.events.ts +3 -3
- package/src/dispatcher/dispatcher.ts +59 -28
- package/src/dispatcher/dispatcher.types.ts +19 -2
- package/src/dispatcher/dispatcher.utils.ts +5 -4
- package/src/http-adapter/http-adapter.constants.ts +1 -1
- package/src/http-adapter/http-adapter.fetch.ts +174 -0
- package/src/http-adapter/http-adapter.ts +3 -8
- package/src/http-adapter/http-adapter.types.ts +17 -9
- package/src/http-adapter/http-adapter.utils.ts +6 -9
- package/src/http-adapter/index.ts +1 -0
- package/src/managers/app/app.manager.constants.ts +2 -2
- package/src/managers/app/app.manager.events.ts +1 -1
- package/src/managers/app/app.manager.ts +2 -1
- package/src/managers/logger/logger.manager.constants.ts +1 -1
- package/src/managers/logger/logger.manager.ts +2 -1
- package/src/managers/logger/logger.manager.types.ts +2 -2
- package/src/managers/logger/logger.manager.utils.ts +2 -1
- package/src/managers/request/request.manager.events.ts +13 -11
- package/src/managers/request/request.manager.types.ts +6 -3
- package/src/mocker/mocker.ts +3 -2
- package/src/mocker/mocker.types.ts +1 -1
- package/src/plugin/plugin.ts +2 -2
- package/src/plugin/plugin.types.ts +9 -9
- package/src/request/index.ts +1 -0
- package/src/request/request.hooks.ts +87 -0
- package/src/request/request.ts +155 -77
- package/src/request/request.types.ts +54 -22
- package/src/request/request.utils.ts +86 -23
- package/src/sdk/sdk.ts +153 -8
- package/src/types/adapter.types.ts +1 -1
- package/src/types/client.types.ts +1 -1
- package/src/types/hydration.types.ts +3 -3
- package/src/types/request.types.ts +30 -20
- package/src/utils/uuid.utils.ts +1 -1
- package/tsconfig.base.json +1 -6
- package/tsconfig.json +1 -4
- package/vite.config.ts +45 -0
- package/.eslintrc.json +0 -11
- package/__tests__/global.d.ts +0 -1
- package/__tests__/jest.setup.ts +0 -13
- package/dist/browser/index.js +0 -3751
- package/dist/browser/index.js.map +0 -7
- package/dist/index.js +0 -4002
- package/dist/index.js.map +0 -7
- package/jest.config.ts +0 -6
- package/src/http-adapter/http-adapter.browser.ts +0 -102
- package/src/http-adapter/http-adapter.server.ts +0 -149
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import { expectFunctionParametersType, expectNotFunctionParametersType } from "@hyper-fetch/testing";
|
|
3
|
+
|
|
4
|
+
import type { ResponseSuccessType } from "adapter";
|
|
5
|
+
import type { ResponseDetailsType } from "managers";
|
|
6
|
+
import { Client } from "client";
|
|
7
|
+
import type { HttpAdapterType } from "http-adapter";
|
|
8
|
+
import { xhrExtra } from "http-adapter";
|
|
9
|
+
import { createCache } from "../../utils";
|
|
10
|
+
|
|
11
|
+
interface UserResponse {
|
|
12
|
+
id: number;
|
|
13
|
+
name: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface UserError {
|
|
17
|
+
code: string;
|
|
18
|
+
message: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const client = new Client({ url: "http://localhost:3000" });
|
|
22
|
+
|
|
23
|
+
const userRequest = client.createRequest<{ response: UserResponse; error: UserError }>()({
|
|
24
|
+
method: "GET",
|
|
25
|
+
endpoint: "/users/:id",
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const anyRequest = client.createRequest<{ response: any }>()({
|
|
29
|
+
method: "GET",
|
|
30
|
+
endpoint: "/any",
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const cache = createCache(client);
|
|
34
|
+
|
|
35
|
+
const successResponse: ResponseSuccessType<UserResponse, HttpAdapterType> & ResponseDetailsType = {
|
|
36
|
+
data: { id: 1, name: "John" },
|
|
37
|
+
error: null,
|
|
38
|
+
status: 200,
|
|
39
|
+
success: true,
|
|
40
|
+
extra: xhrExtra,
|
|
41
|
+
requestTimestamp: Date.now(),
|
|
42
|
+
responseTimestamp: Date.now(),
|
|
43
|
+
retries: 0,
|
|
44
|
+
addedTimestamp: Date.now(),
|
|
45
|
+
triggerTimestamp: Date.now(),
|
|
46
|
+
isCanceled: false,
|
|
47
|
+
isOffline: false,
|
|
48
|
+
willRetry: false,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
describe("Cache [Types]", () => {
|
|
52
|
+
describe("when using cache.set", () => {
|
|
53
|
+
it("should allow setting correct response type for a typed request", () => {
|
|
54
|
+
expectFunctionParametersType(cache.set).assert([userRequest, successResponse]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("should allow setting response with null data", () => {
|
|
58
|
+
expectFunctionParametersType(cache.set).assert([
|
|
59
|
+
userRequest,
|
|
60
|
+
{
|
|
61
|
+
...successResponse,
|
|
62
|
+
data: null,
|
|
63
|
+
error: { code: "ERR", message: "fail" },
|
|
64
|
+
success: false as const,
|
|
65
|
+
},
|
|
66
|
+
]);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("should not allow setting response with wrong data type", () => {
|
|
70
|
+
expectNotFunctionParametersType(cache.set).assert([
|
|
71
|
+
userRequest,
|
|
72
|
+
{
|
|
73
|
+
...successResponse,
|
|
74
|
+
data: "wrong-type",
|
|
75
|
+
},
|
|
76
|
+
]);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("should not allow setting response with wrong error type", () => {
|
|
80
|
+
expectNotFunctionParametersType(cache.set).assert([
|
|
81
|
+
userRequest,
|
|
82
|
+
{
|
|
83
|
+
...successResponse,
|
|
84
|
+
data: null,
|
|
85
|
+
error: "string-error",
|
|
86
|
+
success: false as const,
|
|
87
|
+
},
|
|
88
|
+
]);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("should allow setting response with correct error type", () => {
|
|
92
|
+
expectFunctionParametersType(cache.set).assert([
|
|
93
|
+
userRequest,
|
|
94
|
+
{
|
|
95
|
+
...successResponse,
|
|
96
|
+
data: null,
|
|
97
|
+
error: { code: "NOT_FOUND", message: "User not found" },
|
|
98
|
+
success: false as const,
|
|
99
|
+
},
|
|
100
|
+
]);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("should allow any data type when request has response: any", () => {
|
|
104
|
+
expectFunctionParametersType(cache.set).assert([
|
|
105
|
+
anyRequest,
|
|
106
|
+
{
|
|
107
|
+
...successResponse,
|
|
108
|
+
data: "string-data",
|
|
109
|
+
},
|
|
110
|
+
]);
|
|
111
|
+
|
|
112
|
+
expectFunctionParametersType(cache.set).assert([
|
|
113
|
+
anyRequest,
|
|
114
|
+
{
|
|
115
|
+
...successResponse,
|
|
116
|
+
data: { anything: true },
|
|
117
|
+
},
|
|
118
|
+
]);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe("when using cache.update", () => {
|
|
123
|
+
it("should allow partial response update for a typed request", () => {
|
|
124
|
+
expectFunctionParametersType(cache.update).assert([userRequest, { data: { id: 2, name: "Jane" } }]);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("should allow partial update with only error field", () => {
|
|
128
|
+
expectFunctionParametersType(cache.update).assert([userRequest, { error: { code: "ERR", message: "fail" } }]);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("should not allow partial update with wrong data type", () => {
|
|
132
|
+
expectNotFunctionParametersType(cache.update).assert([userRequest, { data: "wrong-type" }]);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("should not allow partial update with wrong error type", () => {
|
|
136
|
+
expectNotFunctionParametersType(cache.update).assert([userRequest, { error: "string-error" }]);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("should allow partial update with correct error type", () => {
|
|
140
|
+
expectFunctionParametersType(cache.update).assert([userRequest, { error: { code: "ERR", message: "fail" } }]);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("should allow updating only metadata fields", () => {
|
|
144
|
+
expectFunctionParametersType(cache.update).assert([userRequest, { success: true as const }]);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
});
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { getCacheData, getCacheByKey, getInvalidateByKey } from "cache";
|
|
2
|
-
import { ResponseErrorType, ResponseSuccessType } from "adapter";
|
|
3
|
-
import { ResponseDetailsType } from "managers";
|
|
4
|
-
import { HttpAdapterType
|
|
2
|
+
import type { ResponseErrorType, ResponseSuccessType } from "adapter";
|
|
3
|
+
import type { ResponseDetailsType } from "managers";
|
|
4
|
+
import type { HttpAdapterType } from "http-adapter";
|
|
5
|
+
import { xhrExtra } from "http-adapter";
|
|
5
6
|
|
|
6
7
|
describe("Cache [ Utils ]", () => {
|
|
7
8
|
describe("when getCacheData function is used", () => {
|
|
@@ -25,6 +26,7 @@ describe("Cache [ Utils ]", () => {
|
|
|
25
26
|
triggerTimestamp: +new Date(),
|
|
26
27
|
isCanceled: false,
|
|
27
28
|
isOffline: false,
|
|
29
|
+
willRetry: false,
|
|
28
30
|
};
|
|
29
31
|
|
|
30
32
|
expect(getCacheData(previousResponse, errorResponse)).toStrictEqual({
|
|
@@ -40,6 +42,7 @@ describe("Cache [ Utils ]", () => {
|
|
|
40
42
|
triggerTimestamp: expect.toBeNumber(),
|
|
41
43
|
isCanceled: false,
|
|
42
44
|
isOffline: false,
|
|
45
|
+
willRetry: false,
|
|
43
46
|
} satisfies ReturnType<typeof getCacheData>);
|
|
44
47
|
});
|
|
45
48
|
|
|
@@ -57,6 +60,7 @@ describe("Cache [ Utils ]", () => {
|
|
|
57
60
|
triggerTimestamp: +new Date(),
|
|
58
61
|
isCanceled: false,
|
|
59
62
|
isOffline: false,
|
|
63
|
+
willRetry: false,
|
|
60
64
|
} as ResponseSuccessType<Record<string, string>, HttpAdapterType> & ResponseDetailsType;
|
|
61
65
|
const newResponse = {
|
|
62
66
|
data: { test: "2" },
|
|
@@ -71,6 +75,7 @@ describe("Cache [ Utils ]", () => {
|
|
|
71
75
|
triggerTimestamp: +new Date(),
|
|
72
76
|
isCanceled: false,
|
|
73
77
|
isOffline: false,
|
|
78
|
+
willRetry: false,
|
|
74
79
|
} as ResponseSuccessType<Record<string, string>, HttpAdapterType> & ResponseDetailsType;
|
|
75
80
|
expect(getCacheData(previousResponse, newResponse)).toStrictEqual(newResponse);
|
|
76
81
|
});
|
|
@@ -89,6 +94,7 @@ describe("Cache [ Utils ]", () => {
|
|
|
89
94
|
triggerTimestamp: expect.toBeNumber(),
|
|
90
95
|
isCanceled: false,
|
|
91
96
|
isOffline: false,
|
|
97
|
+
willRetry: false,
|
|
92
98
|
} as ResponseSuccessType<Record<string, string>, HttpAdapterType> & ResponseDetailsType;
|
|
93
99
|
const errorResponse = {
|
|
94
100
|
data: null,
|
|
@@ -103,6 +109,7 @@ describe("Cache [ Utils ]", () => {
|
|
|
103
109
|
triggerTimestamp: expect.toBeNumber(),
|
|
104
110
|
isCanceled: false,
|
|
105
111
|
isOffline: false,
|
|
112
|
+
willRetry: false,
|
|
106
113
|
} as ResponseErrorType<Record<string, string>, HttpAdapterType> & ResponseDetailsType;
|
|
107
114
|
expect(getCacheData(undefined, newResponse)).toStrictEqual(newResponse);
|
|
108
115
|
expect(getCacheData(undefined, errorResponse)).toStrictEqual(errorResponse);
|
|
@@ -29,7 +29,7 @@ describe("Client [ Auth ]", () => {
|
|
|
29
29
|
authRequest = client.createRequest<{ response: any }>()({ endpoint: "/auth" }).setAuth(true);
|
|
30
30
|
refreshRequest = client.createRequest<{ response: any }>()({ endpoint: refreshEndpoint });
|
|
31
31
|
resetMocks();
|
|
32
|
-
|
|
32
|
+
vi.resetAllMocks();
|
|
33
33
|
});
|
|
34
34
|
|
|
35
35
|
afterAll(() => {
|
|
@@ -40,7 +40,7 @@ describe("Client [ Auth ]", () => {
|
|
|
40
40
|
it("should trigger auth middleware callback", async () => {
|
|
41
41
|
mockRequest(authRequest, { data: requestFixture });
|
|
42
42
|
|
|
43
|
-
const trigger =
|
|
43
|
+
const trigger = vi.fn();
|
|
44
44
|
|
|
45
45
|
client.onAuth((cmd) => {
|
|
46
46
|
trigger();
|
|
@@ -57,7 +57,7 @@ describe("Client [ Auth ]", () => {
|
|
|
57
57
|
it("should trigger auth middleware callback", async () => {
|
|
58
58
|
mockRequest(request, { data: requestFixture });
|
|
59
59
|
|
|
60
|
-
const trigger =
|
|
60
|
+
const trigger = vi.fn();
|
|
61
61
|
|
|
62
62
|
client.onAuth((cmd) => {
|
|
63
63
|
trigger();
|
|
@@ -71,7 +71,7 @@ describe("Client [ Auth ]", () => {
|
|
|
71
71
|
});
|
|
72
72
|
|
|
73
73
|
describe("When token is out of date", () => {
|
|
74
|
-
const interceptor =
|
|
74
|
+
const interceptor = vi.fn();
|
|
75
75
|
|
|
76
76
|
const handleErrorIntercept = (callback?: () => void) => {
|
|
77
77
|
client.onError(async (res, req) => {
|
|
@@ -113,7 +113,7 @@ describe("Client [ Auth ]", () => {
|
|
|
113
113
|
it("should try to repeat request only once", async () => {
|
|
114
114
|
mockRequest(request, { status: 401 });
|
|
115
115
|
|
|
116
|
-
const retry =
|
|
116
|
+
const retry = vi.fn();
|
|
117
117
|
|
|
118
118
|
handleErrorIntercept(retry);
|
|
119
119
|
|
|
@@ -65,9 +65,21 @@ describe("Client [ Base ]", () => {
|
|
|
65
65
|
expect(client.submitDispatcher).toBe(submitDispatcher);
|
|
66
66
|
});
|
|
67
67
|
});
|
|
68
|
+
describe("When using key mapper setters", () => {
|
|
69
|
+
it("should set request id mapper", () => {
|
|
70
|
+
const client = new Client({ url: "shared-base-url" });
|
|
71
|
+
const customMapper = (request: any) => `custom-${request.endpoint}`;
|
|
72
|
+
|
|
73
|
+
const result = client.setRequestIdMapper(customMapper);
|
|
74
|
+
|
|
75
|
+
expect(client.unstable_requestIdMapper).toBe(customMapper);
|
|
76
|
+
expect(result).toBe(client);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
68
80
|
describe("When client is getting cleared", () => {
|
|
69
81
|
it("should assign new appManager", async () => {
|
|
70
|
-
const spy =
|
|
82
|
+
const spy = vi.fn();
|
|
71
83
|
const appManager = new AppManager();
|
|
72
84
|
const client = new Client({
|
|
73
85
|
url: "shared-base-url",
|
|
@@ -81,7 +93,7 @@ describe("Client [ Base ]", () => {
|
|
|
81
93
|
expect(spy).toHaveBeenCalledTimes(2);
|
|
82
94
|
});
|
|
83
95
|
it("should assign new cache", async () => {
|
|
84
|
-
const spy =
|
|
96
|
+
const spy = vi.fn();
|
|
85
97
|
const cache = new Cache();
|
|
86
98
|
const client = new Client({
|
|
87
99
|
url: "shared-base-url",
|
|
@@ -96,7 +108,7 @@ describe("Client [ Base ]", () => {
|
|
|
96
108
|
expect(spy).toHaveBeenCalledTimes(2);
|
|
97
109
|
});
|
|
98
110
|
it("should assign new fetchDispatcher", async () => {
|
|
99
|
-
const spy =
|
|
111
|
+
const spy = vi.fn();
|
|
100
112
|
const fetchDispatcher = new Dispatcher();
|
|
101
113
|
const client = new Client({
|
|
102
114
|
url: "shared-base-url",
|
|
@@ -111,7 +123,7 @@ describe("Client [ Base ]", () => {
|
|
|
111
123
|
expect(spy).toHaveBeenCalledTimes(2);
|
|
112
124
|
});
|
|
113
125
|
it("should assign new submitDispatcher", async () => {
|
|
114
|
-
const spy =
|
|
126
|
+
const spy = vi.fn();
|
|
115
127
|
const submitDispatcher = new Dispatcher();
|
|
116
128
|
const client = new Client({
|
|
117
129
|
url: "shared-base-url",
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
2
|
+
|
|
3
|
+
import { Client } from "client";
|
|
4
|
+
import { Request } from "request";
|
|
5
|
+
|
|
6
|
+
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
7
|
+
|
|
8
|
+
describe("Client [ fromJSON ]", () => {
|
|
9
|
+
let client = new Client({ url: "shared-base-url" });
|
|
10
|
+
|
|
11
|
+
beforeAll(() => {
|
|
12
|
+
startServer();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
client = new Client({ url: "shared-base-url" });
|
|
17
|
+
resetMocks();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterAll(() => {
|
|
21
|
+
stopServer();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe("When reconstructing a request from JSON", () => {
|
|
25
|
+
it("should return a Request class instance", () => {
|
|
26
|
+
const original = client.createRequest()({ endpoint: "/users", method: "GET" });
|
|
27
|
+
const json = original.toJSON();
|
|
28
|
+
const restored = client.fromJSON(json);
|
|
29
|
+
|
|
30
|
+
expect(restored).toBeInstanceOf(Request);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should preserve endpoint from the original request", () => {
|
|
34
|
+
const original = client.createRequest()({ endpoint: "/users/:id", method: "GET" });
|
|
35
|
+
const json = original.toJSON();
|
|
36
|
+
const restored = client.fromJSON(json);
|
|
37
|
+
|
|
38
|
+
expect(restored.endpoint).toBe("/users/:id");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should preserve method from the original request", () => {
|
|
42
|
+
const original = client.createRequest()({ endpoint: "/users", method: "POST" });
|
|
43
|
+
const json = original.toJSON();
|
|
44
|
+
const restored = client.fromJSON(json);
|
|
45
|
+
|
|
46
|
+
expect(restored.method).toBe("POST");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("should preserve request options through serialization", () => {
|
|
50
|
+
const original = client.createRequest()({
|
|
51
|
+
endpoint: "/data",
|
|
52
|
+
method: "PUT",
|
|
53
|
+
cache: true,
|
|
54
|
+
retry: 3,
|
|
55
|
+
cacheTime: 5000,
|
|
56
|
+
});
|
|
57
|
+
const json = original.toJSON();
|
|
58
|
+
const restored = client.fromJSON(json);
|
|
59
|
+
|
|
60
|
+
expect(restored.cache).toBe(true);
|
|
61
|
+
expect(restored.retry).toBe(3);
|
|
62
|
+
expect(restored.cacheTime).toBe(5000);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("should bind the restored request to the client", () => {
|
|
66
|
+
const original = client.createRequest()({ endpoint: "/test", method: "GET" });
|
|
67
|
+
const json = original.toJSON();
|
|
68
|
+
const restored = client.fromJSON(json);
|
|
69
|
+
|
|
70
|
+
expect(restored.client).toBe(client);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe("When using type generics", () => {
|
|
75
|
+
it("should produce the same response type as createRequest", () => {
|
|
76
|
+
type Props = { response: { id: number; name: string } };
|
|
77
|
+
|
|
78
|
+
const created = client.createRequest<Props>()({ endpoint: "/users", method: "GET" });
|
|
79
|
+
const restored = client.fromJSON<Props>(created.toJSON());
|
|
80
|
+
|
|
81
|
+
type CreatedData = Awaited<ReturnType<typeof created.send>>["data"];
|
|
82
|
+
type RestoredData = Awaited<ReturnType<typeof restored.send>>["data"];
|
|
83
|
+
|
|
84
|
+
expectTypeOf<RestoredData>().toEqualTypeOf<CreatedData>();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("should produce the same payload type as createRequest", () => {
|
|
88
|
+
type Props = { response: { id: number }; payload: { name: string; email: string } };
|
|
89
|
+
|
|
90
|
+
const created = client.createRequest<Props>()({ endpoint: "/users", method: "POST" });
|
|
91
|
+
const restored = client.fromJSON<Props>(created.toJSON());
|
|
92
|
+
|
|
93
|
+
type CreatedPayload = Parameters<typeof created.setPayload>[0];
|
|
94
|
+
type RestoredPayload = Parameters<typeof restored.setPayload>[0];
|
|
95
|
+
|
|
96
|
+
expectTypeOf<RestoredPayload>().toEqualTypeOf<CreatedPayload>();
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("should produce the same error type as createRequest", () => {
|
|
100
|
+
type Props = { error: { code: string; message: string } };
|
|
101
|
+
|
|
102
|
+
const created = client.createRequest<Props>()({ endpoint: "/fail", method: "GET" });
|
|
103
|
+
const restored = client.fromJSON<Props>(created.toJSON());
|
|
104
|
+
|
|
105
|
+
type CreatedError = Awaited<ReturnType<typeof created.send>>["error"];
|
|
106
|
+
type RestoredError = Awaited<ReturnType<typeof restored.send>>["error"];
|
|
107
|
+
|
|
108
|
+
expectTypeOf<RestoredError>().toEqualTypeOf<CreatedError>();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("should produce the same defaults as createRequest when no generics are passed", () => {
|
|
112
|
+
const created = client.createRequest()({ endpoint: "/test", method: "GET" });
|
|
113
|
+
const restored = client.fromJSON(created.toJSON());
|
|
114
|
+
|
|
115
|
+
type CreatedData = Awaited<ReturnType<typeof created.send>>["data"];
|
|
116
|
+
type RestoredData = Awaited<ReturnType<typeof restored.send>>["data"];
|
|
117
|
+
|
|
118
|
+
type CreatedPayload = Parameters<typeof created.setPayload>[0];
|
|
119
|
+
type RestoredPayload = Parameters<typeof restored.setPayload>[0];
|
|
120
|
+
|
|
121
|
+
expectTypeOf<RestoredData>().toEqualTypeOf<CreatedData>();
|
|
122
|
+
expectTypeOf<RestoredPayload>().toEqualTypeOf<CreatedPayload>();
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("should produce the same endpoint params type as createRequest", () => {
|
|
126
|
+
type Props = { response: { id: number }; endpoint: "/users/:userId" };
|
|
127
|
+
|
|
128
|
+
const created = client.createRequest<Props>()({ endpoint: "/users/:userId", method: "GET" });
|
|
129
|
+
const restored = client.fromJSON<Props>(created.toJSON());
|
|
130
|
+
|
|
131
|
+
type CreatedParams = Parameters<typeof created.setParams>[0];
|
|
132
|
+
type RestoredParams = Parameters<typeof restored.setParams>[0];
|
|
133
|
+
|
|
134
|
+
expectTypeOf<RestoredParams>().toEqualTypeOf<CreatedParams>();
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("should produce the same query params type as createRequest", () => {
|
|
138
|
+
type Props = { queryParams: { search?: string; page?: number } };
|
|
139
|
+
|
|
140
|
+
const created = client.createRequest<Props>()({ endpoint: "/search", method: "GET" });
|
|
141
|
+
const restored = client.fromJSON<Props>(created.toJSON());
|
|
142
|
+
|
|
143
|
+
type CreatedQueryParams = Parameters<typeof created.setQueryParams>[0];
|
|
144
|
+
type RestoredQueryParams = Parameters<typeof restored.setQueryParams>[0];
|
|
145
|
+
|
|
146
|
+
expectTypeOf<RestoredQueryParams>().toEqualTypeOf<CreatedQueryParams>();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("should produce matching types for all generics combined", () => {
|
|
150
|
+
type Props = {
|
|
151
|
+
response: { id: number };
|
|
152
|
+
payload: { name: string };
|
|
153
|
+
error: { msg: string };
|
|
154
|
+
queryParams: { limit: number };
|
|
155
|
+
endpoint: "/items/:itemId";
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const created = client.createRequest<Props>()({ endpoint: "/items/:itemId", method: "POST" });
|
|
159
|
+
const restored = client.fromJSON<Props>(created.toJSON());
|
|
160
|
+
|
|
161
|
+
type CreatedData = Awaited<ReturnType<typeof created.send>>["data"];
|
|
162
|
+
type RestoredData = Awaited<ReturnType<typeof restored.send>>["data"];
|
|
163
|
+
expectTypeOf<RestoredData>().toEqualTypeOf<CreatedData>();
|
|
164
|
+
|
|
165
|
+
type CreatedError = Awaited<ReturnType<typeof created.send>>["error"];
|
|
166
|
+
type RestoredError = Awaited<ReturnType<typeof restored.send>>["error"];
|
|
167
|
+
expectTypeOf<RestoredError>().toEqualTypeOf<CreatedError>();
|
|
168
|
+
|
|
169
|
+
type CreatedPayload = Parameters<typeof created.setPayload>[0];
|
|
170
|
+
type RestoredPayload = Parameters<typeof restored.setPayload>[0];
|
|
171
|
+
expectTypeOf<RestoredPayload>().toEqualTypeOf<CreatedPayload>();
|
|
172
|
+
|
|
173
|
+
type CreatedParams = Parameters<typeof created.setParams>[0];
|
|
174
|
+
type RestoredParams = Parameters<typeof restored.setParams>[0];
|
|
175
|
+
expectTypeOf<RestoredParams>().toEqualTypeOf<CreatedParams>();
|
|
176
|
+
|
|
177
|
+
type CreatedQueryParams = Parameters<typeof created.setQueryParams>[0];
|
|
178
|
+
type RestoredQueryParams = Parameters<typeof restored.setQueryParams>[0];
|
|
179
|
+
expectTypeOf<RestoredQueryParams>().toEqualTypeOf<CreatedQueryParams>();
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
});
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { Client } from "client";
|
|
2
|
+
import type { CacheValueType } from "cache";
|
|
3
|
+
import { xhrExtra } from "http-adapter";
|
|
4
|
+
import { scopeKey } from "request";
|
|
2
5
|
|
|
3
6
|
describe("Client [ Hydration ]", () => {
|
|
4
7
|
let client = new Client({ url: "shared-base-url" });
|
|
@@ -60,7 +63,7 @@ describe("Client [ Hydration ]", () => {
|
|
|
60
63
|
expect(dehydratedResponse).toStrictEqual(expect.objectContaining({ cacheKey: request.cacheKey, response }));
|
|
61
64
|
|
|
62
65
|
const newClient = new Client({ url: "shared-base-url" });
|
|
63
|
-
const cacheData
|
|
66
|
+
const cacheData = {
|
|
64
67
|
data: "123456789",
|
|
65
68
|
status: 200,
|
|
66
69
|
error: null,
|
|
@@ -75,7 +78,7 @@ describe("Client [ Hydration ]", () => {
|
|
|
75
78
|
isOffline: false,
|
|
76
79
|
hydrated: false,
|
|
77
80
|
cached: true,
|
|
78
|
-
};
|
|
81
|
+
} as CacheValueType;
|
|
79
82
|
|
|
80
83
|
newClient.cache.set(mockedRequest, cacheData);
|
|
81
84
|
|
|
@@ -85,6 +88,7 @@ describe("Client [ Hydration ]", () => {
|
|
|
85
88
|
cacheTime: mockedRequest.cacheTime,
|
|
86
89
|
staleTime: mockedRequest.staleTime,
|
|
87
90
|
version: newClient.cache.version,
|
|
91
|
+
scope: null,
|
|
88
92
|
});
|
|
89
93
|
|
|
90
94
|
newClient.hydrate([dehydratedResponse]);
|
|
@@ -95,6 +99,7 @@ describe("Client [ Hydration ]", () => {
|
|
|
95
99
|
cacheTime: mockedRequest.cacheTime,
|
|
96
100
|
staleTime: mockedRequest.staleTime,
|
|
97
101
|
version: newClient.cache.version,
|
|
102
|
+
scope: null,
|
|
98
103
|
});
|
|
99
104
|
});
|
|
100
105
|
it("should handle null/undefined items in hydration data", async () => {
|
|
@@ -114,7 +119,7 @@ describe("Client [ Hydration ]", () => {
|
|
|
114
119
|
const dehydratedResponse = mockedRequest.dehydrate();
|
|
115
120
|
|
|
116
121
|
const newClient = new Client({ url: "shared-base-url" });
|
|
117
|
-
const optionsFn =
|
|
122
|
+
const optionsFn = vi.fn().mockReturnValue({ cache: false });
|
|
118
123
|
|
|
119
124
|
newClient.hydrate([dehydratedResponse], optionsFn);
|
|
120
125
|
|
|
@@ -162,10 +167,70 @@ describe("Client [ Hydration ]", () => {
|
|
|
162
167
|
|
|
163
168
|
client.cache.clear();
|
|
164
169
|
|
|
165
|
-
const spy =
|
|
170
|
+
const spy = vi.spyOn(client.cache, "set");
|
|
166
171
|
client.hydrate([dehydratedResponse], { override: false });
|
|
167
172
|
|
|
168
173
|
expect(spy).toHaveBeenCalled();
|
|
169
174
|
});
|
|
175
|
+
|
|
176
|
+
it("should hydrate and read scoped cache via scopeKey", async () => {
|
|
177
|
+
const scoped = request.setScope("hydr-scope").setMock(() => ({ data: "scoped-val", status: 200 }));
|
|
178
|
+
const response = await scoped.send();
|
|
179
|
+
const dehydratedResponse = scoped.dehydrate();
|
|
180
|
+
expect(dehydratedResponse).toEqual(expect.objectContaining({ scope: "hydr-scope", cacheKey: scoped.cacheKey }));
|
|
181
|
+
|
|
182
|
+
const newClient = new Client({ url: "shared-base-url" });
|
|
183
|
+
const sk = scopeKey(scoped.cacheKey, "hydr-scope");
|
|
184
|
+
expect(newClient.cache.get(scoped.cacheKey)).not.toBeDefined();
|
|
185
|
+
|
|
186
|
+
newClient.hydrate([dehydratedResponse]);
|
|
187
|
+
|
|
188
|
+
expect(newClient.cache.get(sk)).toStrictEqual(
|
|
189
|
+
expect.objectContaining({ ...response, cacheKey: scoped.cacheKey, staleTime: scoped.staleTime }),
|
|
190
|
+
);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("should skip hydrate when override is false and scoped slot already has data", async () => {
|
|
194
|
+
const scoped = request.setScope("no-over").setMock(() => ({ data: "a", status: 200 }));
|
|
195
|
+
await scoped.send();
|
|
196
|
+
const ts = Date.now();
|
|
197
|
+
const dehydratedResponse = scoped.dehydrate({
|
|
198
|
+
response: {
|
|
199
|
+
data: "b",
|
|
200
|
+
status: 200,
|
|
201
|
+
success: true,
|
|
202
|
+
error: null,
|
|
203
|
+
extra: xhrExtra,
|
|
204
|
+
requestTimestamp: ts,
|
|
205
|
+
responseTimestamp: ts,
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
const newClient = new Client({ url: "shared-base-url" });
|
|
210
|
+
const existingReq = newClient
|
|
211
|
+
.createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint" })
|
|
212
|
+
.setScope("no-over");
|
|
213
|
+
const sk = scopeKey(existingReq.cacheKey, "no-over");
|
|
214
|
+
|
|
215
|
+
newClient.cache.set(existingReq.setCache(true), {
|
|
216
|
+
data: "existing",
|
|
217
|
+
error: null,
|
|
218
|
+
status: 200,
|
|
219
|
+
success: true,
|
|
220
|
+
extra: xhrExtra,
|
|
221
|
+
retries: 0,
|
|
222
|
+
requestTimestamp: Date.now(),
|
|
223
|
+
responseTimestamp: Date.now(),
|
|
224
|
+
addedTimestamp: Date.now(),
|
|
225
|
+
triggerTimestamp: Date.now(),
|
|
226
|
+
isCanceled: false,
|
|
227
|
+
isOffline: false,
|
|
228
|
+
willRetry: false,
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
newClient.hydrate([dehydratedResponse], { override: false });
|
|
232
|
+
|
|
233
|
+
expect(newClient.cache.get(sk)?.data).toBe("existing");
|
|
234
|
+
});
|
|
170
235
|
});
|
|
171
236
|
});
|
|
@@ -11,9 +11,9 @@ describe("Client [ Interceptor ]", () => {
|
|
|
11
11
|
let client = new Client({ url: "shared-base-url/" });
|
|
12
12
|
let request = client.createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint" });
|
|
13
13
|
|
|
14
|
-
const spy1 =
|
|
15
|
-
const spy2 =
|
|
16
|
-
const spy3 =
|
|
14
|
+
const spy1 = vi.fn();
|
|
15
|
+
const spy2 = vi.fn();
|
|
16
|
+
const spy3 = vi.fn();
|
|
17
17
|
|
|
18
18
|
beforeAll(() => {
|
|
19
19
|
startServer();
|
|
@@ -23,7 +23,7 @@ describe("Client [ Interceptor ]", () => {
|
|
|
23
23
|
client = new Client({ url: "shared-base-url/" });
|
|
24
24
|
request = client.createRequest<{ response: any }>()({ endpoint: "shared-base-endpoint" });
|
|
25
25
|
resetMocks();
|
|
26
|
-
|
|
26
|
+
vi.clearAllMocks();
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
afterAll(() => {
|
|
@@ -2,10 +2,11 @@ import { createHttpMockingServer } from "@hyper-fetch/testing";
|
|
|
2
2
|
|
|
3
3
|
import { Plugin } from "plugin";
|
|
4
4
|
import { Client } from "client";
|
|
5
|
-
import { RequestOptionsType
|
|
5
|
+
import type { RequestOptionsType } from "request";
|
|
6
|
+
import { Request } from "request";
|
|
6
7
|
import { LoggerManager } from "managers";
|
|
7
8
|
import { createAdapter, interceptorCallback, middlewareCallback } from "../../utils";
|
|
8
|
-
import {
|
|
9
|
+
import type { FetchAdapterOptionsType, QueryStringifyOptionsType } from "http-adapter";
|
|
9
10
|
|
|
10
11
|
const { resetMocks, startServer, stopServer } = createHttpMockingServer();
|
|
11
12
|
|
|
@@ -27,7 +28,7 @@ describe("Client [ Methods ]", () => {
|
|
|
27
28
|
|
|
28
29
|
describe("When using config methods", () => {
|
|
29
30
|
it("should assign default request config [setRequestDefaults]", async () => {
|
|
30
|
-
const options: Partial<RequestOptionsType<string,
|
|
31
|
+
const options: Partial<RequestOptionsType<string, FetchAdapterOptionsType>> = { method: "POST" };
|
|
31
32
|
client.adapter.setRequestDefaults(() => options);
|
|
32
33
|
const req = client.createRequest()({ endpoint: "test" });
|
|
33
34
|
|
|
@@ -35,7 +36,7 @@ describe("Client [ Methods ]", () => {
|
|
|
35
36
|
expect(req.method).toBe("POST");
|
|
36
37
|
});
|
|
37
38
|
it("should assign default adapter config [setAdapterDefaultOptions]", async () => {
|
|
38
|
-
const options:
|
|
39
|
+
const options: FetchAdapterOptionsType = { timeout: 12312312 };
|
|
39
40
|
client.adapter.setAdapterDefaults(() => options);
|
|
40
41
|
|
|
41
42
|
expect(client.adapter.unstable_getAdapterDefaults?.(request)).toEqual(options);
|
|
@@ -10,9 +10,9 @@ describe("Client [ Middleware ]", () => {
|
|
|
10
10
|
let client = new Client({ url: "shared-base-url" });
|
|
11
11
|
let request = client.createRequest()({ endpoint: "shared-base-endpoint" });
|
|
12
12
|
|
|
13
|
-
const spy1 =
|
|
14
|
-
const spy2 =
|
|
15
|
-
const spy3 =
|
|
13
|
+
const spy1 = vi.fn();
|
|
14
|
+
const spy2 = vi.fn();
|
|
15
|
+
const spy3 = vi.fn();
|
|
16
16
|
|
|
17
17
|
beforeAll(() => {
|
|
18
18
|
startServer();
|
|
@@ -22,7 +22,7 @@ describe("Client [ Middleware ]", () => {
|
|
|
22
22
|
client = new Client({ url: "shared-base-url" });
|
|
23
23
|
request = client.createRequest()({ endpoint: "shared-base-endpoint" });
|
|
24
24
|
resetMocks();
|
|
25
|
-
|
|
25
|
+
vi.clearAllMocks();
|
|
26
26
|
});
|
|
27
27
|
|
|
28
28
|
afterAll(() => {
|