@mswjs/interceptors 0.22.4 → 0.22.5

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 (72) hide show
  1. package/README.md +3 -3
  2. package/lib/browser/{Interceptor-959f650e.d.ts → Interceptor-c794917d.d.ts} +2 -2
  3. package/lib/browser/{chunk-IIY3SHU3.js → chunk-2EIH6L6D.js} +25 -25
  4. package/lib/browser/{chunk-BQYA7ER5.mjs → chunk-2GVXYEMC.mjs} +5 -5
  5. package/lib/browser/chunk-2Z7B3HL5.js +106 -0
  6. package/lib/browser/{chunk-5B525MKF.mjs → chunk-65HGG3CV.mjs} +1 -1
  7. package/lib/browser/chunk-CNZUUPXM.mjs +682 -0
  8. package/lib/browser/chunk-GL6Y4E24.mjs +106 -0
  9. package/lib/browser/chunk-KDXWM46S.js +682 -0
  10. package/lib/browser/{chunk-QWL3EOEY.js → chunk-OK5YCL7L.js} +7 -7
  11. package/lib/browser/{chunk-ZJOF5MEZ.js → chunk-PCFJD76X.js} +11 -11
  12. package/lib/browser/{chunk-STA6QBYM.mjs → chunk-RT3ATOJH.mjs} +11 -11
  13. package/lib/browser/index.d.ts +6 -6
  14. package/lib/browser/index.js +4 -4
  15. package/lib/browser/index.mjs +2 -2
  16. package/lib/browser/interceptors/XMLHttpRequest/index.d.ts +3 -3
  17. package/lib/browser/interceptors/XMLHttpRequest/index.js +5 -677
  18. package/lib/browser/interceptors/XMLHttpRequest/index.mjs +5 -677
  19. package/lib/browser/interceptors/fetch/index.d.ts +2 -2
  20. package/lib/browser/interceptors/fetch/index.js +4 -101
  21. package/lib/browser/interceptors/fetch/index.mjs +4 -101
  22. package/lib/browser/presets/browser.d.ts +13 -0
  23. package/lib/browser/presets/browser.js +18 -0
  24. package/lib/browser/presets/browser.mjs +18 -0
  25. package/lib/node/{BatchInterceptor-a7261b26.d.ts → BatchInterceptor-5c1e5de3.d.ts} +5 -5
  26. package/lib/node/{Interceptor-997045eb.d.ts → Interceptor-b3a4098c.d.ts} +2 -2
  27. package/lib/node/RemoteHttpInterceptor.d.ts +3 -3
  28. package/lib/node/RemoteHttpInterceptor.js +9 -9
  29. package/lib/node/RemoteHttpInterceptor.mjs +5 -5
  30. package/lib/node/{chunk-FGPCRIW6.mjs → chunk-53LM4S3X.mjs} +59 -62
  31. package/lib/node/{chunk-WWHITCCI.js → chunk-62KFIM4W.js} +9 -8
  32. package/lib/node/{chunk-37CATPNG.mjs → chunk-6CRMKMDL.mjs} +6 -5
  33. package/lib/node/{chunk-XLZJAPVS.js → chunk-D7MOETL2.js} +59 -62
  34. package/lib/node/{chunk-P7NKDCFD.mjs → chunk-HGKO2BOA.mjs} +4 -4
  35. package/lib/node/{chunk-RVLLS44W.js → chunk-HTLVOEKP.js} +8 -8
  36. package/lib/node/{chunk-KZEQH4YW.mjs → chunk-IC6Y7RGW.mjs} +1 -1
  37. package/lib/node/{chunk-SNNL2EXF.js → chunk-MUUQLKVJ.js} +3 -3
  38. package/lib/node/{chunk-G6ZTHYZQ.mjs → chunk-RFVEKRYP.mjs} +1 -1
  39. package/lib/node/{chunk-Q56TMOP5.js → chunk-SFLY7F52.js} +2 -2
  40. package/lib/node/index.d.ts +3 -3
  41. package/lib/node/index.js +4 -4
  42. package/lib/node/index.mjs +3 -3
  43. package/lib/node/interceptors/ClientRequest/index.d.ts +3 -3
  44. package/lib/node/interceptors/ClientRequest/index.js +3 -3
  45. package/lib/node/interceptors/ClientRequest/index.mjs +2 -2
  46. package/lib/node/interceptors/XMLHttpRequest/index.d.ts +3 -3
  47. package/lib/node/interceptors/XMLHttpRequest/index.js +4 -4
  48. package/lib/node/interceptors/XMLHttpRequest/index.mjs +3 -3
  49. package/lib/node/interceptors/fetch/index.d.ts +2 -2
  50. package/lib/node/interceptors/fetch/index.js +2 -2
  51. package/lib/node/interceptors/fetch/index.mjs +1 -1
  52. package/lib/node/presets/node.d.ts +15 -0
  53. package/lib/node/presets/node.js +19 -0
  54. package/lib/node/presets/node.mjs +19 -0
  55. package/package.json +16 -1
  56. package/src/BatchInterceptor.ts +5 -5
  57. package/src/Interceptor.ts +2 -2
  58. package/src/interceptors/ClientRequest/NodeClientRequest.test.ts +1 -1
  59. package/src/interceptors/ClientRequest/http.request.ts +1 -1
  60. package/src/interceptors/ClientRequest/index.ts +2 -2
  61. package/src/interceptors/ClientRequest/utils/createRequest.test.ts +1 -1
  62. package/src/interceptors/ClientRequest/utils/getIncomingMessageBody.ts +1 -1
  63. package/src/interceptors/ClientRequest/utils/normalizeClientRequestArgs.ts +1 -1
  64. package/src/interceptors/ClientRequest/utils/normalizeClientRequestEndArgs.ts +5 -3
  65. package/src/interceptors/ClientRequest/utils/normalizeClientRequestWriteArgs.ts +1 -1
  66. package/src/interceptors/XMLHttpRequest/index.ts +2 -2
  67. package/src/presets/browser.ts +4 -1
  68. package/src/presets/node.ts +4 -1
  69. package/src/utils/AsyncEventEmitter.ts +1 -1
  70. package/src/utils/cloneObject.ts +8 -6
  71. package/src/utils/debug.ts +4 -0
  72. package/src/utils/getUrlByRequestOptions.ts +13 -12
@@ -0,0 +1,106 @@
1
+ import {
2
+ toInteractiveRequest,
3
+ uuidv4
4
+ } from "./chunk-RT3ATOJH.mjs";
5
+ import {
6
+ IS_PATCHED_MODULE,
7
+ Interceptor
8
+ } from "./chunk-2GVXYEMC.mjs";
9
+
10
+ // src/interceptors/fetch/index.ts
11
+ import { invariant } from "outvariant";
12
+ import { until } from "@open-draft/until";
13
+ var _FetchInterceptor = class extends Interceptor {
14
+ constructor() {
15
+ super(_FetchInterceptor.symbol);
16
+ }
17
+ checkEnvironment() {
18
+ return typeof globalThis !== "undefined" && typeof globalThis.fetch !== "undefined";
19
+ }
20
+ setup() {
21
+ const pureFetch = globalThis.fetch;
22
+ invariant(
23
+ !pureFetch[IS_PATCHED_MODULE],
24
+ 'Failed to patch the "fetch" module: already patched.'
25
+ );
26
+ globalThis.fetch = async (input, init) => {
27
+ const requestId = uuidv4();
28
+ const request = new Request(input, init);
29
+ this.log("[%s] %s", request.method, request.url);
30
+ const interactiveRequest = toInteractiveRequest(request);
31
+ this.log(
32
+ 'emitting the "request" event for %d listener(s)...',
33
+ this.emitter.listenerCount("request")
34
+ );
35
+ this.emitter.emit("request", interactiveRequest, requestId);
36
+ this.log("awaiting for the mocked response...");
37
+ const [middlewareException, mockedResponse] = await until(async () => {
38
+ await this.emitter.untilIdle(
39
+ "request",
40
+ ({ args: [, pendingRequestId] }) => {
41
+ return pendingRequestId === requestId;
42
+ }
43
+ );
44
+ this.log("all request listeners have been resolved!");
45
+ const [mockedResponse2] = await interactiveRequest.respondWith.invoked();
46
+ this.log("event.respondWith called with:", mockedResponse2);
47
+ return mockedResponse2;
48
+ });
49
+ if (middlewareException) {
50
+ console.error(`${request.method} ${request.url} net::ERR_FAILED`);
51
+ const error = Object.assign(new TypeError("Failed to fetch"), {
52
+ cause: middlewareException
53
+ });
54
+ return Promise.reject(error);
55
+ }
56
+ if (mockedResponse) {
57
+ this.log("received mocked response:", mockedResponse);
58
+ const responseCloine = mockedResponse.clone();
59
+ this.emitter.emit(
60
+ "response",
61
+ responseCloine,
62
+ interactiveRequest,
63
+ requestId
64
+ );
65
+ const response = new Response(mockedResponse.body, mockedResponse);
66
+ Object.defineProperty(response, "url", {
67
+ writable: false,
68
+ enumerable: true,
69
+ configurable: false,
70
+ value: request.url
71
+ });
72
+ return response;
73
+ }
74
+ this.log("no mocked response received!");
75
+ return pureFetch(request).then((response) => {
76
+ const responseClone = response.clone();
77
+ this.log("original fetch performed", responseClone);
78
+ this.emitter.emit(
79
+ "response",
80
+ responseClone,
81
+ interactiveRequest,
82
+ requestId
83
+ );
84
+ return response;
85
+ });
86
+ };
87
+ Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {
88
+ enumerable: true,
89
+ configurable: true,
90
+ value: true
91
+ });
92
+ this.subscriptions.push(() => {
93
+ Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {
94
+ value: void 0
95
+ });
96
+ globalThis.fetch = pureFetch;
97
+ this.log('restored native "globalThis.fetch"!', globalThis.fetch.name);
98
+ });
99
+ }
100
+ };
101
+ var FetchInterceptor = _FetchInterceptor;
102
+ FetchInterceptor.symbol = Symbol("fetch");
103
+
104
+ export {
105
+ FetchInterceptor
106
+ };