@hyper-fetch/core 9.6.1 → 9.6.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.
Files changed (103) hide show
  1. package/__tests__/features/adapter/adapter.bindings.progress.spec.ts +120 -0
  2. package/__tests__/features/adapter/adapter.bindings.spec.ts +4 -6
  3. package/__tests__/features/adapter/adapter.defaults.spec.ts +87 -0
  4. package/__tests__/features/adapter/adapter.mapping.spec.ts +257 -0
  5. package/__tests__/features/cache/cache.lazy-storage.clear.spec.ts +145 -0
  6. package/__tests__/features/cache/cache.lazy-storage.resilience.spec.ts +667 -0
  7. package/__tests__/features/cache/cache.scoped-events.spec.ts +183 -0
  8. package/__tests__/features/cache/cache.semantics.spec.ts +785 -0
  9. package/__tests__/features/cache/cache.structural-sharing.spec.ts +565 -0
  10. package/__tests__/features/cache/cache.timing.spec.ts +448 -0
  11. package/__tests__/features/client/client.clear.spec.ts +313 -0
  12. package/__tests__/features/client/client.configuration.spec.ts +416 -0
  13. package/__tests__/features/client/client.hydration.round-trip.spec.ts +324 -0
  14. package/__tests__/features/client/client.interceptor.spec.ts +54 -45
  15. package/__tests__/features/client/client.interceptors.pipeline.spec.ts +599 -0
  16. package/__tests__/features/client/client.user-errors.spec.ts +220 -0
  17. package/__tests__/features/dispatcher/dispatcher.cleanup.spec.ts +534 -0
  18. package/__tests__/features/dispatcher/dispatcher.control.spec.ts +381 -0
  19. package/__tests__/features/dispatcher/dispatcher.modes.spec.ts +527 -0
  20. package/__tests__/features/dispatcher/dispatcher.offline.spec.ts +278 -0
  21. package/__tests__/features/dispatcher/dispatcher.queue.spec.ts +35 -12
  22. package/__tests__/features/dispatcher/dispatcher.reentrancy.spec.ts +309 -0
  23. package/__tests__/features/dispatcher/dispatcher.response-order.spec.ts +241 -0
  24. package/__tests__/features/dispatcher/dispatcher.retry-flow.spec.ts +554 -0
  25. package/__tests__/features/dispatcher/dispatcher.retry-removal.spec.ts +196 -0
  26. package/__tests__/features/dispatcher/dispatcher.retry.spec.ts +4 -3
  27. package/__tests__/features/http-adapter/http-adapter.e2e.spec.ts +917 -0
  28. package/__tests__/features/managers/app/app.manager.events.spec.ts +434 -0
  29. package/__tests__/features/managers/logger/logger.levels.spec.ts +199 -0
  30. package/__tests__/features/managers/request/request.manager.abort.spec.ts +449 -0
  31. package/__tests__/features/mocker/mocker.base.spec.ts +22 -3
  32. package/__tests__/features/mocker/mocker.errors.spec.ts +99 -0
  33. package/__tests__/features/mocker/mocker.outcomes.spec.ts +308 -0
  34. package/__tests__/features/plugin/plugin.lifecycle.spec.ts +457 -0
  35. package/__tests__/features/request/request.async-mapper-removal.spec.ts +138 -0
  36. package/__tests__/features/request/request.immutability.spec.ts +259 -0
  37. package/__tests__/features/request/request.inference.spec.ts +269 -0
  38. package/__tests__/features/request/request.lifecycle.spec.ts +682 -0
  39. package/__tests__/features/request/request.optimistic-settlement.spec.ts +35 -17
  40. package/__tests__/features/request/request.values.spec.ts +408 -0
  41. package/__tests__/features/sdk/sdk.builder.spec.ts +304 -0
  42. package/__tests__/features/utils/emitter.listeners.spec.ts +202 -0
  43. package/__tests__/utils/controlled-adapter.utils.ts +119 -0
  44. package/__tests__/utils/fixtures.utils.ts +193 -0
  45. package/dist/adapter/adapter.bindings.d.ts.map +1 -1
  46. package/dist/adapter/adapter.utils.d.ts.map +1 -1
  47. package/dist/cache/cache.d.ts +12 -1
  48. package/dist/cache/cache.d.ts.map +1 -1
  49. package/dist/cache/cache.events.d.ts +20 -6
  50. package/dist/cache/cache.events.d.ts.map +1 -1
  51. package/dist/cache/cache.utils.d.ts +1 -0
  52. package/dist/cache/cache.utils.d.ts.map +1 -1
  53. package/dist/client/client.d.ts +3 -3
  54. package/dist/client/client.d.ts.map +1 -1
  55. package/dist/client/client.utils.d.ts.map +1 -1
  56. package/dist/dispatcher/dispatcher.d.ts +26 -5
  57. package/dist/dispatcher/dispatcher.d.ts.map +1 -1
  58. package/dist/http-adapter/http-adapter.fetch.d.ts.map +1 -1
  59. package/dist/index.mjs +2127 -1741
  60. package/dist/index.mjs.map +1 -1
  61. package/dist/managers/app/app.manager.constants.d.ts.map +1 -1
  62. package/dist/managers/app/app.manager.d.ts +9 -0
  63. package/dist/managers/app/app.manager.d.ts.map +1 -1
  64. package/dist/managers/app/app.manager.utils.d.ts.map +1 -1
  65. package/dist/managers/logger/logger.manager.d.ts.map +1 -1
  66. package/dist/managers/request/request.manager.d.ts.map +1 -1
  67. package/dist/managers/request/request.manager.types.d.ts +2 -0
  68. package/dist/managers/request/request.manager.types.d.ts.map +1 -1
  69. package/dist/mocker/mocker.d.ts +1 -1
  70. package/dist/mocker/mocker.d.ts.map +1 -1
  71. package/dist/plugin/plugin.d.ts +2 -0
  72. package/dist/plugin/plugin.d.ts.map +1 -1
  73. package/dist/request/request.d.ts +1 -1
  74. package/dist/request/request.d.ts.map +1 -1
  75. package/dist/request/request.utils.d.ts.map +1 -1
  76. package/dist/utils/emitter.utils.d.ts.map +1 -1
  77. package/dist/utils/error.utils.d.ts +17 -0
  78. package/dist/utils/error.utils.d.ts.map +1 -0
  79. package/dist/utils/index.d.ts +1 -0
  80. package/dist/utils/index.d.ts.map +1 -1
  81. package/package.json +1 -1
  82. package/src/adapter/adapter.bindings.ts +46 -18
  83. package/src/adapter/adapter.utils.ts +47 -7
  84. package/src/cache/cache.events.ts +24 -8
  85. package/src/cache/cache.ts +109 -21
  86. package/src/cache/cache.utils.ts +33 -10
  87. package/src/client/client.ts +99 -7
  88. package/src/client/client.utils.ts +10 -4
  89. package/src/dispatcher/dispatcher.ts +273 -133
  90. package/src/http-adapter/http-adapter.fetch.ts +15 -3
  91. package/src/managers/app/app.manager.constants.ts +12 -1
  92. package/src/managers/app/app.manager.ts +43 -23
  93. package/src/managers/app/app.manager.utils.ts +8 -4
  94. package/src/managers/logger/logger.manager.ts +21 -8
  95. package/src/managers/request/request.manager.ts +7 -1
  96. package/src/managers/request/request.manager.types.ts +2 -0
  97. package/src/mocker/mocker.ts +14 -11
  98. package/src/plugin/plugin.ts +6 -0
  99. package/src/request/request.ts +31 -22
  100. package/src/request/request.utils.ts +23 -15
  101. package/src/utils/emitter.utils.ts +9 -2
  102. package/src/utils/error.utils.ts +29 -0
  103. package/src/utils/index.ts +1 -0
@@ -0,0 +1,416 @@
1
+ import type { ResponseType } from "adapter";
2
+ import { Cache } from "cache";
3
+ import { Client, createClient, resolveClientMode } from "client";
4
+ import type { ClientInstance } from "client";
5
+ import { Dispatcher } from "dispatcher";
6
+ import type { HttpAdapterType } from "http-adapter";
7
+ import { HttpAdapter } from "http-adapter";
8
+ import { AppManager, LoggerManager } from "managers";
9
+ import { Plugin } from "plugin";
10
+ import type { RequestInstance } from "request";
11
+ import type { ExtractErrorType, ExtractResponseType } from "types";
12
+
13
+ import { createCacheResponse, removeGlobal } from "../../utils/fixtures.utils";
14
+
15
+ type Data = { id: number };
16
+
17
+ describe("Client [ Configuration ]", () => {
18
+ let client = new Client({ url: "shared-base-url" });
19
+
20
+ beforeEach(() => {
21
+ client = new Client({ url: "shared-base-url" });
22
+ });
23
+
24
+ afterEach(() => {
25
+ client.clear();
26
+ vi.unstubAllGlobals();
27
+ });
28
+
29
+ describe("When the client is created", () => {
30
+ it("should create the client with the default modules", () => {
31
+ const created = createClient({ url: "shared-base-url" });
32
+
33
+ expect(created).toBeInstanceOf(Client);
34
+ expect(created.url).toBe("shared-base-url");
35
+ expect(created.cache).toBeInstanceOf(Cache);
36
+ expect(created.fetchDispatcher).toBeInstanceOf(Dispatcher);
37
+ expect(created.submitDispatcher).toBeInstanceOf(Dispatcher);
38
+ expect(created.appManager).toBeInstanceOf(AppManager);
39
+ expect(created.loggerManager).toBeInstanceOf(LoggerManager);
40
+ expect(created.fetchDispatcher).not.toBe(created.submitDispatcher);
41
+ expect(created.plugins).toStrictEqual([]);
42
+ expect(created.isMockerEnabled).toBeTrue();
43
+ });
44
+
45
+ it("should keep the options it was created with", () => {
46
+ const options = { url: "shared-base-url", mode: "server" as const };
47
+
48
+ expect(new Client(options).options).toBe(options);
49
+ });
50
+
51
+ it.each([
52
+ { option: "auto" as const, expected: "client" },
53
+ { option: "client" as const, expected: "client" },
54
+ { option: "server" as const, expected: "server" },
55
+ ])("should resolve the $option mode to $expected in the browser", ({ option, expected }) => {
56
+ expect(resolveClientMode(option)).toBe(expected);
57
+ expect(new Client({ url: "shared-base-url", mode: option }).mode).toBe(expected);
58
+ });
59
+
60
+ it("should detect the mode in the browser when it is not set", () => {
61
+ expect(new Client({ url: "shared-base-url" }).mode).toBe("client");
62
+ });
63
+
64
+ it("should resolve the automatic mode to the server one when there is no window", () => {
65
+ removeGlobal("window");
66
+
67
+ expect(new Client({ url: "shared-base-url" }).mode).toBe("server");
68
+ expect(resolveClientMode("auto")).toBe("server");
69
+ expect(resolveClientMode("client")).toBe("client");
70
+ });
71
+
72
+ it("should resolve the automatic mode to the server one when there is no document", () => {
73
+ removeGlobal("document");
74
+
75
+ expect(resolveClientMode("auto")).toBe("server");
76
+ });
77
+
78
+ it("should create the client without the window", () => {
79
+ removeGlobal("window");
80
+
81
+ const ssrClient = new Client({ url: "shared-base-url" });
82
+
83
+ expect(ssrClient.mode).toBe("server");
84
+ expect(ssrClient.appManager.isBrowser).toBeFalse();
85
+ expect(ssrClient.appManager.isFocused).toBeTrue();
86
+ expect(() => ssrClient.clear()).not.toThrow();
87
+ });
88
+ });
89
+
90
+ describe("When the setters are used", () => {
91
+ it("should return the client from every setter", () => {
92
+ const interceptor = (request: RequestInstance) => request;
93
+ const responseInterceptor = (response: ResponseType<any, any, HttpAdapterType>) => response;
94
+ const plugin = new Plugin({ name: "plugin" });
95
+ const mapper = () => "key";
96
+
97
+ const results = [
98
+ client.setDebug(true),
99
+ client.setLogLevel("debug"),
100
+ client.setLogger(() => new LoggerManager()),
101
+ client.setEnableGlobalMocking(false),
102
+ client.setUrl("other-url"),
103
+ client.setAdapter(HttpAdapter()),
104
+ client.onAuth(interceptor),
105
+ client.onRequest(interceptor),
106
+ client.onResponse(responseInterceptor),
107
+ client.onSuccess(responseInterceptor),
108
+ client.onError(responseInterceptor),
109
+ client.removeOnAuthInterceptors([interceptor]),
110
+ client.removeOnRequestInterceptors([interceptor]),
111
+ client.removeOnResponseInterceptors([responseInterceptor]),
112
+ client.removeOnSuccessInterceptors([responseInterceptor]),
113
+ client.removeOnErrorInterceptors([responseInterceptor]),
114
+ client.addPlugin(plugin),
115
+ client.triggerPlugins("onMount", { client }),
116
+ client.removePlugin(plugin),
117
+ client.setAbortKeyMapper(mapper),
118
+ client.setCacheKeyMapper(mapper),
119
+ client.setQueryKeyMapper(mapper),
120
+ client.setRequestIdMapper(mapper),
121
+ ];
122
+
123
+ results.forEach((result) => expect(result).toBe(client));
124
+ });
125
+
126
+ it("should apply the values of the setters", () => {
127
+ client.setDebug(true).setLogLevel("info").setEnableGlobalMocking(false).setUrl("other-url");
128
+
129
+ expect(client.debug).toBeTrue();
130
+ expect(client.loggerManager.level).toBe("info");
131
+ expect(client.isMockerEnabled).toBeFalse();
132
+ expect(client.url).toBe("other-url");
133
+ });
134
+
135
+ it("should initialize the adapter it receives", () => {
136
+ const adapter = HttpAdapter();
137
+
138
+ client.setAdapter(adapter);
139
+
140
+ expect(client.adapter).toBe(adapter);
141
+ expect(adapter.client).toBe(client);
142
+ });
143
+
144
+ it("should use the key mappers for the created requests", () => {
145
+ client
146
+ .setCacheKeyMapper((request) => `cache:${request.endpoint}`)
147
+ .setQueryKeyMapper((request) => `query:${request.endpoint}`)
148
+ .setAbortKeyMapper((request) => `abort:${request.endpoint}`);
149
+
150
+ const request = client.createRequest()({ endpoint: "/users" });
151
+
152
+ expect(request.cacheKey).toBe("cache:/users");
153
+ expect(request.queryKey).toBe("query:/users");
154
+ expect(request.abortKey).toBe("abort:/users");
155
+ });
156
+
157
+ it("should prefer the keys set on the request over the key mappers", () => {
158
+ client.setCacheKeyMapper(() => "mapped");
159
+
160
+ const request = client.createRequest()({ endpoint: "/users", cacheKey: "custom" });
161
+
162
+ expect(request.cacheKey).toBe("custom");
163
+ });
164
+
165
+ it("should use the request id mapper for the sent requests", async () => {
166
+ const onBeforeSent = vi.fn();
167
+ const request = client.createRequest<{ response: Data }>()({ endpoint: "/users" });
168
+ request.setMock(() => ({ data: { id: 1 }, status: 200 }));
169
+ client.setRequestIdMapper(() => "custom-request-id");
170
+
171
+ await request.send({ onBeforeSent });
172
+
173
+ expect(onBeforeSent.mock.calls[0][0].requestId).toBe("custom-request-id");
174
+ });
175
+ });
176
+
177
+ describe("When the logger is replaced", () => {
178
+ it("should send the logs of the client to the new logger", () => {
179
+ const logger = vi.fn();
180
+
181
+ client.setDebug(true).setLogger(() => new LoggerManager({ logger }));
182
+ client.logger.error({ title: "Client log", type: "system", extra: {} });
183
+
184
+ expect(logger).toHaveBeenCalledTimes(1);
185
+ expect(logger.mock.calls[0][0]).toStrictEqual(
186
+ expect.objectContaining({ title: "Client log", module: "Client", level: "error" }),
187
+ );
188
+ });
189
+
190
+ it("should send the logs of the modules created earlier to the new logger", () => {
191
+ const logger = vi.fn();
192
+ const request = client.createRequest<{ response: Data }>()({ endpoint: "/users" });
193
+
194
+ client.setDebug(true).setLogger(() => new LoggerManager({ logger, level: "debug" }));
195
+ client.cache.set(request, createCacheResponse({ id: 1 }));
196
+
197
+ const modules = new Set(logger.mock.calls.map(([log]) => log.module));
198
+ expect(modules.has("Cache")).toBeTrue();
199
+ });
200
+
201
+ it("should pass the client to the logger factory and keep the returned manager", () => {
202
+ const manager = new LoggerManager();
203
+ const factory = vi.fn((_: ClientInstance) => manager);
204
+
205
+ client.setLogger(factory);
206
+
207
+ expect(factory).toHaveBeenCalledWith(client);
208
+ expect(client.loggerManager).toBe(manager);
209
+ });
210
+
211
+ it("should stop using the previous logger", () => {
212
+ const previous = vi.fn();
213
+ const current = vi.fn();
214
+ client.setDebug(true).setLogger(() => new LoggerManager({ logger: previous }));
215
+
216
+ client.setLogger(() => new LoggerManager({ logger: current }));
217
+ client.logger.error({ title: "Client log", type: "system", extra: {} });
218
+
219
+ expect(previous).not.toHaveBeenCalled();
220
+ expect(current).toHaveBeenCalledTimes(1);
221
+ });
222
+
223
+ it("should change the level of the current logger", () => {
224
+ const logger = vi.fn();
225
+ client.setDebug(true).setLogger(() => new LoggerManager({ logger }));
226
+
227
+ client.logger.debug({ title: "Hidden", type: "system", extra: {} });
228
+ client.setLogLevel("debug");
229
+ client.logger.debug({ title: "Visible", type: "system", extra: {} });
230
+
231
+ expect(logger).toHaveBeenCalledTimes(1);
232
+ expect(logger.mock.calls[0][0].title).toBe("Visible");
233
+ });
234
+
235
+ it("should not log anything without the debug mode", () => {
236
+ const logger = vi.fn();
237
+ const request = client.createRequest<{ response: Data }>()({ endpoint: "/users" });
238
+
239
+ client.setLogger(() => new LoggerManager({ logger, level: "debug" }));
240
+ client.cache.set(request, createCacheResponse({ id: 1 }));
241
+ client.logger.error({ title: "Client log", type: "system", extra: {} });
242
+
243
+ expect(logger).not.toHaveBeenCalled();
244
+ });
245
+ });
246
+
247
+ describe("When many clients are used", () => {
248
+ let other = new Client({ url: "other-base-url" });
249
+
250
+ beforeEach(() => {
251
+ other = new Client({ url: "other-base-url" });
252
+ });
253
+
254
+ afterEach(() => {
255
+ other.clear();
256
+ });
257
+
258
+ it("should give every client its own modules", () => {
259
+ expect(client.cache).not.toBe(other.cache);
260
+ expect(client.cache.storage).not.toBe(other.cache.storage);
261
+ expect(client.cache.emitter).not.toBe(other.cache.emitter);
262
+ expect(client.fetchDispatcher).not.toBe(other.fetchDispatcher);
263
+ expect(client.submitDispatcher).not.toBe(other.submitDispatcher);
264
+ expect(client.appManager).not.toBe(other.appManager);
265
+ expect(client.requestManager).not.toBe(other.requestManager);
266
+ expect(client.loggerManager).not.toBe(other.loggerManager);
267
+ expect(client.adapter).not.toBe(other.adapter);
268
+ expect(client.plugins).not.toBe(other.plugins);
269
+ });
270
+
271
+ it("should not share the settings", () => {
272
+ client.setDebug(true).setLogLevel("debug").setEnableGlobalMocking(false).setUrl("changed-url");
273
+
274
+ expect(other.debug).toBeFalsy();
275
+ expect(other.loggerManager.level).toBe("warning");
276
+ expect(other.isMockerEnabled).toBeTrue();
277
+ expect(other.url).toBe("other-base-url");
278
+ });
279
+
280
+ it("should not share the interceptors and plugins", () => {
281
+ const interceptor = (request: RequestInstance) => request;
282
+ const responseInterceptor = (response: ResponseType<any, any, HttpAdapterType>) => response;
283
+ client
284
+ .onAuth(interceptor)
285
+ .onRequest(interceptor)
286
+ .onResponse(responseInterceptor)
287
+ .onSuccess(responseInterceptor)
288
+ .onError(responseInterceptor)
289
+ .addPlugin(new Plugin({ name: "plugin" }));
290
+
291
+ expect(other.unstable_onAuthCallbacks).toHaveLength(0);
292
+ expect(other.unstable_onRequestCallbacks).toHaveLength(0);
293
+ expect(other.unstable_onResponseCallbacks).toHaveLength(0);
294
+ expect(other.unstable_onSuccessCallbacks).toHaveLength(0);
295
+ expect(other.unstable_onErrorCallbacks).toHaveLength(0);
296
+ expect(other.plugins).toHaveLength(0);
297
+ });
298
+
299
+ it("should not share the key mappers", () => {
300
+ client.setCacheKeyMapper(() => "mapped");
301
+
302
+ expect(client.createRequest()({ endpoint: "/users" }).cacheKey).toBe("mapped");
303
+ expect(other.createRequest()({ endpoint: "/users" }).cacheKey).not.toBe("mapped");
304
+ });
305
+
306
+ it("should not share the request defaults of the adapters", () => {
307
+ client.adapter.setRequestDefaults(() => ({ retry: 5 }));
308
+
309
+ expect(client.createRequest()({ endpoint: "/users" }).retry).toBe(5);
310
+ expect(other.createRequest()({ endpoint: "/users" }).retry).toBe(0);
311
+ });
312
+
313
+ it("should not share the cached data", () => {
314
+ const spy = vi.fn();
315
+ const request = client.createRequest<{ response: Data }>()({ endpoint: "/users" });
316
+ const otherRequest = other.createRequest<{ response: Data }>()({ endpoint: "/users" });
317
+ other.cache.events.onData(spy);
318
+
319
+ client.cache.set(request, createCacheResponse({ id: 1 }));
320
+
321
+ expect(request.cacheKey).toBe(otherRequest.cacheKey);
322
+ expect(request.read()?.data).toStrictEqual({ id: 1 });
323
+ expect(otherRequest.read()).toBeUndefined();
324
+ expect(spy).not.toHaveBeenCalled();
325
+ });
326
+
327
+ it("should not share the application state", () => {
328
+ const spy = vi.fn();
329
+ other.appManager.events.onOffline(spy);
330
+ other.appManager.events.onBlur(spy);
331
+
332
+ client.appManager.setOnline(false);
333
+ client.appManager.setFocused(false);
334
+
335
+ expect(other.appManager.isOnline).toBeTrue();
336
+ expect(other.appManager.isFocused).toBeTrue();
337
+ expect(spy).not.toHaveBeenCalled();
338
+ });
339
+
340
+ it("should not trigger the plugins of another client", async () => {
341
+ const spy = vi.fn();
342
+ const request = other.createRequest<{ response: Data }>()({ endpoint: "/users" });
343
+ request.setMock(() => ({ data: { id: 1 }, status: 200 }));
344
+ client.addPlugin(
345
+ new Plugin({ name: "plugin" })
346
+ .onRequestCreate(spy)
347
+ .onRequestStart(spy)
348
+ .onRequestSuccess(spy)
349
+ .onCacheItemChange(spy),
350
+ );
351
+
352
+ await request.send();
353
+
354
+ expect(spy).not.toHaveBeenCalled();
355
+ });
356
+
357
+ it("should clear only the given client", () => {
358
+ const request = client.createRequest<{ response: Data }>()({ endpoint: "/users" });
359
+ const otherRequest = other.createRequest<{ response: Data }>()({ endpoint: "/users" });
360
+ client.cache.set(request, createCacheResponse({ id: 1 }));
361
+ other.cache.set(otherRequest, createCacheResponse({ id: 2 }));
362
+
363
+ client.clear();
364
+
365
+ expect(request.read()).toBeUndefined();
366
+ expect(otherRequest.read()?.data).toStrictEqual({ id: 2 });
367
+ });
368
+ });
369
+
370
+ describe("When the types are checked", () => {
371
+ it("should keep the client type in the chained setters", () => {
372
+ const typedClient = createClient<{ error: { message: string } }>({ url: "shared-base-url" });
373
+
374
+ expectTypeOf(typedClient.setDebug(true)).toEqualTypeOf(typedClient);
375
+ expectTypeOf(typedClient.setUrl("url")).toEqualTypeOf(typedClient);
376
+ expectTypeOf(typedClient.onRequest((request) => request)).toEqualTypeOf(typedClient);
377
+ expectTypeOf(typedClient.onResponse((response) => response)).toEqualTypeOf(typedClient);
378
+ expectTypeOf(typedClient.addPlugin(new Plugin({ name: "plugin" }))).toEqualTypeOf(typedClient);
379
+ });
380
+
381
+ it("should use the global error type for the requests", () => {
382
+ const typedClient = createClient<{ error: { message: string } }>({ url: "shared-base-url" });
383
+ const request = typedClient.createRequest<{ response: Data }>()({ endpoint: "/users" });
384
+ const withLocalError = typedClient.createRequest<{ response: Data; error: { code: number } }>()({
385
+ endpoint: "/users",
386
+ });
387
+
388
+ expectTypeOf<ExtractResponseType<typeof request>>().toEqualTypeOf<Data>();
389
+ expectTypeOf<ExtractErrorType<typeof request>>().toEqualTypeOf<{ message: string }>();
390
+ expectTypeOf<ExtractErrorType<typeof withLocalError>>().toEqualTypeOf<{ code: number } | { message: string }>();
391
+ });
392
+
393
+ it("should use the Error type by default", () => {
394
+ const request = createClient({ url: "shared-base-url" }).createRequest<{ response: Data }>()({
395
+ endpoint: "/users",
396
+ });
397
+
398
+ expectTypeOf<ExtractErrorType<typeof request>>().toEqualTypeOf<Error>();
399
+ });
400
+
401
+ it("should type the interceptors and the mode", () => {
402
+ expectTypeOf(client.mode).toEqualTypeOf<"client" | "server">();
403
+ expectTypeOf(client.onRequest).parameter(0).toBeFunction();
404
+ expectTypeOf(client.unstable_modifyRequest).returns.resolves.toEqualTypeOf<RequestInstance>();
405
+ expectTypeOf(client.cache).toEqualTypeOf<Cache<HttpAdapterType>>();
406
+ });
407
+
408
+ it("should reject the wrong options", () => {
409
+ // @ts-expect-error url is required
410
+ expectTypeOf(createClient).toBeCallableWith({});
411
+ // @ts-expect-error mode accepts only the known values
412
+ expectTypeOf(createClient).toBeCallableWith({ url: "url", mode: "browser" });
413
+ expectTypeOf(createClient).toBeCallableWith({ url: "url", mode: "auto" });
414
+ });
415
+ });
416
+ });