@mswjs/interceptors 0.33.3 → 0.34.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/lib/browser/interceptors/WebSocket/index.d.ts +2 -0
- package/lib/browser/interceptors/WebSocket/index.js +30 -15
- package/lib/browser/interceptors/WebSocket/index.js.map +1 -1
- package/lib/browser/interceptors/WebSocket/index.mjs +30 -15
- package/lib/browser/interceptors/WebSocket/index.mjs.map +1 -1
- package/lib/node/chunk-RTGLFNO3.mjs +142 -0
- package/lib/node/chunk-RTGLFNO3.mjs.map +1 -0
- package/lib/node/chunk-UN335ZTD.js +142 -0
- package/lib/node/chunk-UN335ZTD.js.map +1 -0
- package/lib/node/interceptors/fetch/index.js +5 -136
- package/lib/node/interceptors/fetch/index.js.map +1 -1
- package/lib/node/interceptors/fetch/index.mjs +5 -136
- package/lib/node/interceptors/fetch/index.mjs.map +1 -1
- package/lib/node/presets/node.d.ts +2 -1
- package/lib/node/presets/node.js +5 -1
- package/lib/node/presets/node.js.map +1 -1
- package/lib/node/presets/node.mjs +5 -1
- package/lib/node/presets/node.mjs.map +1 -1
- package/package.json +1 -1
- package/src/interceptors/WebSocket/WebSocketOverride.ts +21 -14
- package/src/interceptors/WebSocket/index.ts +30 -7
- package/src/presets/node.ts +2 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import {
|
|
2
|
+
IS_PATCHED_MODULE
|
|
3
|
+
} from "./chunk-BZ3Y7YV5.mjs";
|
|
4
|
+
import {
|
|
5
|
+
RequestController,
|
|
6
|
+
emitAsync,
|
|
7
|
+
handleRequest
|
|
8
|
+
} from "./chunk-KY3RJ2M3.mjs";
|
|
9
|
+
import {
|
|
10
|
+
Interceptor,
|
|
11
|
+
createRequestId
|
|
12
|
+
} from "./chunk-BUCULLYM.mjs";
|
|
13
|
+
|
|
14
|
+
// src/interceptors/fetch/index.ts
|
|
15
|
+
import { invariant } from "outvariant";
|
|
16
|
+
import { DeferredPromise } from "@open-draft/deferred-promise";
|
|
17
|
+
|
|
18
|
+
// src/utils/canParseUrl.ts
|
|
19
|
+
function canParseUrl(url) {
|
|
20
|
+
try {
|
|
21
|
+
new URL(url);
|
|
22
|
+
return true;
|
|
23
|
+
} catch (_error) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// src/interceptors/fetch/index.ts
|
|
29
|
+
var _FetchInterceptor = class extends Interceptor {
|
|
30
|
+
constructor() {
|
|
31
|
+
super(_FetchInterceptor.symbol);
|
|
32
|
+
}
|
|
33
|
+
checkEnvironment() {
|
|
34
|
+
return typeof globalThis !== "undefined" && typeof globalThis.fetch !== "undefined";
|
|
35
|
+
}
|
|
36
|
+
async setup() {
|
|
37
|
+
const pureFetch = globalThis.fetch;
|
|
38
|
+
invariant(
|
|
39
|
+
!pureFetch[IS_PATCHED_MODULE],
|
|
40
|
+
'Failed to patch the "fetch" module: already patched.'
|
|
41
|
+
);
|
|
42
|
+
globalThis.fetch = async (input, init) => {
|
|
43
|
+
const requestId = createRequestId();
|
|
44
|
+
const resolvedInput = typeof input === "string" && typeof location !== "undefined" && !canParseUrl(input) ? new URL(input, location.origin) : input;
|
|
45
|
+
const request = new Request(resolvedInput, init);
|
|
46
|
+
const responsePromise = new DeferredPromise();
|
|
47
|
+
const controller = new RequestController(request);
|
|
48
|
+
this.logger.info("[%s] %s", request.method, request.url);
|
|
49
|
+
this.logger.info("awaiting for the mocked response...");
|
|
50
|
+
this.logger.info(
|
|
51
|
+
'emitting the "request" event for %s listener(s)...',
|
|
52
|
+
this.emitter.listenerCount("request")
|
|
53
|
+
);
|
|
54
|
+
const isRequestHandled = await handleRequest({
|
|
55
|
+
request,
|
|
56
|
+
requestId,
|
|
57
|
+
emitter: this.emitter,
|
|
58
|
+
controller,
|
|
59
|
+
onResponse: async (response) => {
|
|
60
|
+
this.logger.info("received mocked response!", {
|
|
61
|
+
response
|
|
62
|
+
});
|
|
63
|
+
if (this.emitter.listenerCount("response") > 0) {
|
|
64
|
+
this.logger.info('emitting the "response" event...');
|
|
65
|
+
await emitAsync(this.emitter, "response", {
|
|
66
|
+
// Clone the mocked response for the "response" event listener.
|
|
67
|
+
// This way, the listener can read the response and not lock its body
|
|
68
|
+
// for the actual fetch consumer.
|
|
69
|
+
response: response.clone(),
|
|
70
|
+
isMockedResponse: true,
|
|
71
|
+
request,
|
|
72
|
+
requestId
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
Object.defineProperty(response, "url", {
|
|
76
|
+
writable: false,
|
|
77
|
+
enumerable: true,
|
|
78
|
+
configurable: false,
|
|
79
|
+
value: request.url
|
|
80
|
+
});
|
|
81
|
+
responsePromise.resolve(response);
|
|
82
|
+
},
|
|
83
|
+
onRequestError: (response) => {
|
|
84
|
+
this.logger.info("request has errored!", { response });
|
|
85
|
+
responsePromise.reject(createNetworkError(response));
|
|
86
|
+
},
|
|
87
|
+
onError: (error) => {
|
|
88
|
+
this.logger.info("request has been aborted!", { error });
|
|
89
|
+
responsePromise.reject(error);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
if (isRequestHandled) {
|
|
93
|
+
this.logger.info("request has been handled, returning mock promise...");
|
|
94
|
+
return responsePromise;
|
|
95
|
+
}
|
|
96
|
+
this.logger.info(
|
|
97
|
+
"no mocked response received, performing request as-is..."
|
|
98
|
+
);
|
|
99
|
+
return pureFetch(request).then((response) => {
|
|
100
|
+
this.logger.info("original fetch performed", response);
|
|
101
|
+
if (this.emitter.listenerCount("response") > 0) {
|
|
102
|
+
this.logger.info('emitting the "response" event...');
|
|
103
|
+
const responseClone = response.clone();
|
|
104
|
+
this.emitter.emit("response", {
|
|
105
|
+
response: responseClone,
|
|
106
|
+
isMockedResponse: false,
|
|
107
|
+
request,
|
|
108
|
+
requestId
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
return response;
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {
|
|
115
|
+
enumerable: true,
|
|
116
|
+
configurable: true,
|
|
117
|
+
value: true
|
|
118
|
+
});
|
|
119
|
+
this.subscriptions.push(() => {
|
|
120
|
+
Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {
|
|
121
|
+
value: void 0
|
|
122
|
+
});
|
|
123
|
+
globalThis.fetch = pureFetch;
|
|
124
|
+
this.logger.info(
|
|
125
|
+
'restored native "globalThis.fetch"!',
|
|
126
|
+
globalThis.fetch.name
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
var FetchInterceptor = _FetchInterceptor;
|
|
132
|
+
FetchInterceptor.symbol = Symbol("fetch");
|
|
133
|
+
function createNetworkError(cause) {
|
|
134
|
+
return Object.assign(new TypeError("Failed to fetch"), {
|
|
135
|
+
cause
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export {
|
|
140
|
+
FetchInterceptor
|
|
141
|
+
};
|
|
142
|
+
//# sourceMappingURL=chunk-RTGLFNO3.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/interceptors/fetch/index.ts","../../src/utils/canParseUrl.ts"],"sourcesContent":["import { invariant } from 'outvariant'\nimport { DeferredPromise } from '@open-draft/deferred-promise'\nimport { HttpRequestEventMap, IS_PATCHED_MODULE } from '../../glossary'\nimport { Interceptor } from '../../Interceptor'\nimport { RequestController } from '../../RequestController'\nimport { emitAsync } from '../../utils/emitAsync'\nimport { handleRequest } from '../../utils/handleRequest'\nimport { canParseUrl } from '../../utils/canParseUrl'\nimport { createRequestId } from '../../createRequestId'\n\nexport class FetchInterceptor extends Interceptor<HttpRequestEventMap> {\n static symbol = Symbol('fetch')\n\n constructor() {\n super(FetchInterceptor.symbol)\n }\n\n protected checkEnvironment() {\n return (\n typeof globalThis !== 'undefined' &&\n typeof globalThis.fetch !== 'undefined'\n )\n }\n\n protected async setup() {\n const pureFetch = globalThis.fetch\n\n invariant(\n !(pureFetch as any)[IS_PATCHED_MODULE],\n 'Failed to patch the \"fetch\" module: already patched.'\n )\n\n globalThis.fetch = async (input, init) => {\n const requestId = createRequestId()\n\n /**\n * @note Resolve potentially relative request URL\n * against the present `location`. This is mainly\n * for native `fetch` in JSDOM.\n * @see https://github.com/mswjs/msw/issues/1625\n */\n const resolvedInput =\n typeof input === 'string' &&\n typeof location !== 'undefined' &&\n !canParseUrl(input)\n ? new URL(input, location.origin)\n : input\n\n const request = new Request(resolvedInput, init)\n const responsePromise = new DeferredPromise<Response>()\n const controller = new RequestController(request)\n\n this.logger.info('[%s] %s', request.method, request.url)\n this.logger.info('awaiting for the mocked response...')\n\n this.logger.info(\n 'emitting the \"request\" event for %s listener(s)...',\n this.emitter.listenerCount('request')\n )\n\n const isRequestHandled = await handleRequest({\n request,\n requestId,\n emitter: this.emitter,\n controller,\n onResponse: async (response) => {\n this.logger.info('received mocked response!', {\n response,\n })\n\n if (this.emitter.listenerCount('response') > 0) {\n this.logger.info('emitting the \"response\" event...')\n\n // Await the response listeners to finish before resolving\n // the response promise. This ensures all your logic finishes\n // before the interceptor resolves the pending response.\n await emitAsync(this.emitter, 'response', {\n // Clone the mocked response for the \"response\" event listener.\n // This way, the listener can read the response and not lock its body\n // for the actual fetch consumer.\n response: response.clone(),\n isMockedResponse: true,\n request,\n requestId,\n })\n }\n\n // Set the \"response.url\" property to equal the intercepted request URL.\n Object.defineProperty(response, 'url', {\n writable: false,\n enumerable: true,\n configurable: false,\n value: request.url,\n })\n\n responsePromise.resolve(response)\n },\n onRequestError: (response) => {\n this.logger.info('request has errored!', { response })\n responsePromise.reject(createNetworkError(response))\n },\n onError: (error) => {\n this.logger.info('request has been aborted!', { error })\n responsePromise.reject(error)\n },\n })\n\n if (isRequestHandled) {\n this.logger.info('request has been handled, returning mock promise...')\n return responsePromise\n }\n\n this.logger.info(\n 'no mocked response received, performing request as-is...'\n )\n\n return pureFetch(request).then((response) => {\n this.logger.info('original fetch performed', response)\n\n if (this.emitter.listenerCount('response') > 0) {\n this.logger.info('emitting the \"response\" event...')\n\n const responseClone = response.clone()\n\n this.emitter.emit('response', {\n response: responseClone,\n isMockedResponse: false,\n request,\n requestId,\n })\n }\n\n return response\n })\n }\n\n Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {\n enumerable: true,\n configurable: true,\n value: true,\n })\n\n this.subscriptions.push(() => {\n Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {\n value: undefined,\n })\n\n globalThis.fetch = pureFetch\n\n this.logger.info(\n 'restored native \"globalThis.fetch\"!',\n globalThis.fetch.name\n )\n })\n }\n}\n\nfunction createNetworkError(cause: unknown) {\n return Object.assign(new TypeError('Failed to fetch'), {\n cause,\n })\n}\n","/**\n * Returns a boolean indicating whether the given URL string\n * can be parsed into a `URL` instance.\n * A substitute for `URL.canParse()` for Node.js 18.\n */\nexport function canParseUrl(url: string): boolean {\n try {\n new URL(url)\n return true\n } catch (_error) {\n return false\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAAA,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;;;ACIzB,SAAS,YAAY,KAAsB;AAChD,MAAI;AACF,QAAI,IAAI,GAAG;AACX,WAAO;AAAA,EACT,SAAS,QAAP;AACA,WAAO;AAAA,EACT;AACF;;;ADFO,IAAM,oBAAN,cAA+B,YAAiC;AAAA,EAGrE,cAAc;AACZ,UAAM,kBAAiB,MAAM;AAAA,EAC/B;AAAA,EAEU,mBAAmB;AAC3B,WACE,OAAO,eAAe,eACtB,OAAO,WAAW,UAAU;AAAA,EAEhC;AAAA,EAEA,MAAgB,QAAQ;AACtB,UAAM,YAAY,WAAW;AAE7B;AAAA,MACE,CAAE,UAAkB,iBAAiB;AAAA,MACrC;AAAA,IACF;AAEA,eAAW,QAAQ,OAAO,OAAO,SAAS;AACxC,YAAM,YAAY,gBAAgB;AAQlC,YAAM,gBACJ,OAAO,UAAU,YACjB,OAAO,aAAa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AAkB;AACpB,aAAK,OAAO,KAAK,qDAAqD;AACtE,eAAO;AAAA,MACT;AAEA,WAAK,OAAO;AAAA,QACV;AAAA,MACF;AAEA,aAAO,UAAU,OAAO,EAAE,KAAK,CAAC,aAAa;AAC3C,aAAK,OAAO,KAAK,4BAA4B,QAAQ;AAErD,YAAI,KAAK,QAAQ,cAAc,UAAU,IAAI,GAAG;AAC9C,eAAK,OAAO,KAAK,kCAAkC;AAEnD,gBAAM,gBAAgB,SAAS,MAAM;AAErC,eAAK,QAAQ,KAAK,YAAY;AAAA,YAC5B,UAAU;AAAA,YACV,kBAAkB;AAAA,YAClB;AAAA,YACA;AAAA,UACF,CAAC;AAAA,QACH;AAEA,eAAO;AAAA,MACT,CAAC;AAAA,IACH;AAEA,WAAO,eAAe,WAAW,OAAO,mBAAmB;AAAA,MACzD,YAAY;AAAA,MACZ,cAAc;AAAA,MACd,OAAO;AAAA,IACT,CAAC;AAED,SAAK,cAAc,KAAK,MAAM;AAC5B,aAAO,eAAe,WAAW,OAAO,mBAAmB;AAAA,QACzD,OAAO;AAAA,MACT,CAAC;AAED,iBAAW,QAAQ;AAEnB,WAAK,OAAO;AAAA,QACV;AAAA,QACA,WAAW,MAAM;AAAA,MACnB;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAjJO,IAAM,mBAAN;AAAM,iBACJ,SAAS,OAAO,OAAO;AAkJhC,SAAS,mBAAmB,OAAgB;AAC1C,SAAO,OAAO,OAAO,IAAI,UAAU,iBAAiB,GAAG;AAAA,IACrD;AAAA,EACF,CAAC;AACH;","names":[]}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunkIDEEMJ3Fjs = require('./chunk-IDEEMJ3F.js');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
var _chunk5WWNCLB3js = require('./chunk-5WWNCLB3.js');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
var _chunkYGM3BCJUjs = require('./chunk-YGM3BCJU.js');
|
|
13
|
+
|
|
14
|
+
// src/interceptors/fetch/index.ts
|
|
15
|
+
var _outvariant = require('outvariant');
|
|
16
|
+
var _deferredpromise = require('@open-draft/deferred-promise');
|
|
17
|
+
|
|
18
|
+
// src/utils/canParseUrl.ts
|
|
19
|
+
function canParseUrl(url) {
|
|
20
|
+
try {
|
|
21
|
+
new URL(url);
|
|
22
|
+
return true;
|
|
23
|
+
} catch (_error) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// src/interceptors/fetch/index.ts
|
|
29
|
+
var _FetchInterceptor = class extends _chunkYGM3BCJUjs.Interceptor {
|
|
30
|
+
constructor() {
|
|
31
|
+
super(_FetchInterceptor.symbol);
|
|
32
|
+
}
|
|
33
|
+
checkEnvironment() {
|
|
34
|
+
return typeof globalThis !== "undefined" && typeof globalThis.fetch !== "undefined";
|
|
35
|
+
}
|
|
36
|
+
async setup() {
|
|
37
|
+
const pureFetch = globalThis.fetch;
|
|
38
|
+
_outvariant.invariant.call(void 0,
|
|
39
|
+
!pureFetch[_chunkIDEEMJ3Fjs.IS_PATCHED_MODULE],
|
|
40
|
+
'Failed to patch the "fetch" module: already patched.'
|
|
41
|
+
);
|
|
42
|
+
globalThis.fetch = async (input, init) => {
|
|
43
|
+
const requestId = _chunkYGM3BCJUjs.createRequestId.call(void 0, );
|
|
44
|
+
const resolvedInput = typeof input === "string" && typeof location !== "undefined" && !canParseUrl(input) ? new URL(input, location.origin) : input;
|
|
45
|
+
const request = new Request(resolvedInput, init);
|
|
46
|
+
const responsePromise = new (0, _deferredpromise.DeferredPromise)();
|
|
47
|
+
const controller = new (0, _chunk5WWNCLB3js.RequestController)(request);
|
|
48
|
+
this.logger.info("[%s] %s", request.method, request.url);
|
|
49
|
+
this.logger.info("awaiting for the mocked response...");
|
|
50
|
+
this.logger.info(
|
|
51
|
+
'emitting the "request" event for %s listener(s)...',
|
|
52
|
+
this.emitter.listenerCount("request")
|
|
53
|
+
);
|
|
54
|
+
const isRequestHandled = await _chunk5WWNCLB3js.handleRequest.call(void 0, {
|
|
55
|
+
request,
|
|
56
|
+
requestId,
|
|
57
|
+
emitter: this.emitter,
|
|
58
|
+
controller,
|
|
59
|
+
onResponse: async (response) => {
|
|
60
|
+
this.logger.info("received mocked response!", {
|
|
61
|
+
response
|
|
62
|
+
});
|
|
63
|
+
if (this.emitter.listenerCount("response") > 0) {
|
|
64
|
+
this.logger.info('emitting the "response" event...');
|
|
65
|
+
await _chunk5WWNCLB3js.emitAsync.call(void 0, this.emitter, "response", {
|
|
66
|
+
// Clone the mocked response for the "response" event listener.
|
|
67
|
+
// This way, the listener can read the response and not lock its body
|
|
68
|
+
// for the actual fetch consumer.
|
|
69
|
+
response: response.clone(),
|
|
70
|
+
isMockedResponse: true,
|
|
71
|
+
request,
|
|
72
|
+
requestId
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
Object.defineProperty(response, "url", {
|
|
76
|
+
writable: false,
|
|
77
|
+
enumerable: true,
|
|
78
|
+
configurable: false,
|
|
79
|
+
value: request.url
|
|
80
|
+
});
|
|
81
|
+
responsePromise.resolve(response);
|
|
82
|
+
},
|
|
83
|
+
onRequestError: (response) => {
|
|
84
|
+
this.logger.info("request has errored!", { response });
|
|
85
|
+
responsePromise.reject(createNetworkError(response));
|
|
86
|
+
},
|
|
87
|
+
onError: (error) => {
|
|
88
|
+
this.logger.info("request has been aborted!", { error });
|
|
89
|
+
responsePromise.reject(error);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
if (isRequestHandled) {
|
|
93
|
+
this.logger.info("request has been handled, returning mock promise...");
|
|
94
|
+
return responsePromise;
|
|
95
|
+
}
|
|
96
|
+
this.logger.info(
|
|
97
|
+
"no mocked response received, performing request as-is..."
|
|
98
|
+
);
|
|
99
|
+
return pureFetch(request).then((response) => {
|
|
100
|
+
this.logger.info("original fetch performed", response);
|
|
101
|
+
if (this.emitter.listenerCount("response") > 0) {
|
|
102
|
+
this.logger.info('emitting the "response" event...');
|
|
103
|
+
const responseClone = response.clone();
|
|
104
|
+
this.emitter.emit("response", {
|
|
105
|
+
response: responseClone,
|
|
106
|
+
isMockedResponse: false,
|
|
107
|
+
request,
|
|
108
|
+
requestId
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
return response;
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
Object.defineProperty(globalThis.fetch, _chunkIDEEMJ3Fjs.IS_PATCHED_MODULE, {
|
|
115
|
+
enumerable: true,
|
|
116
|
+
configurable: true,
|
|
117
|
+
value: true
|
|
118
|
+
});
|
|
119
|
+
this.subscriptions.push(() => {
|
|
120
|
+
Object.defineProperty(globalThis.fetch, _chunkIDEEMJ3Fjs.IS_PATCHED_MODULE, {
|
|
121
|
+
value: void 0
|
|
122
|
+
});
|
|
123
|
+
globalThis.fetch = pureFetch;
|
|
124
|
+
this.logger.info(
|
|
125
|
+
'restored native "globalThis.fetch"!',
|
|
126
|
+
globalThis.fetch.name
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
var FetchInterceptor = _FetchInterceptor;
|
|
132
|
+
FetchInterceptor.symbol = Symbol("fetch");
|
|
133
|
+
function createNetworkError(cause) {
|
|
134
|
+
return Object.assign(new TypeError("Failed to fetch"), {
|
|
135
|
+
cause
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
exports.FetchInterceptor = FetchInterceptor;
|
|
142
|
+
//# sourceMappingURL=chunk-UN335ZTD.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/interceptors/fetch/index.ts","../../src/utils/canParseUrl.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;;;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AK,OAAO;AAAA,QACV;AAAA,QACA,WAAW,MAAM;AAAA,MACnB;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAjJO,IAAM,mBAAN;AAAM,iBACJ,SAAS,OAAO,OAAO;AAkJhC,SAAS,mBAAmB,OAAgB;AAC1C,SAAO,OAAO,OAAO,IAAI,UAAU,iBAAiB,GAAG;AAAA,IACrD;AAAA,EACF,CAAC;AACH","sourcesContent":["import { invariant } from 'outvariant'\nimport { DeferredPromise } from '@open-draft/deferred-promise'\nimport { HttpRequestEventMap, IS_PATCHED_MODULE } from '../../glossary'\nimport { Interceptor } from '../../Interceptor'\nimport { RequestController } from '../../RequestController'\nimport { emitAsync } from '../../utils/emitAsync'\nimport { handleRequest } from '../../utils/handleRequest'\nimport { canParseUrl } from '../../utils/canParseUrl'\nimport { createRequestId } from '../../createRequestId'\n\nexport class FetchInterceptor extends Interceptor<HttpRequestEventMap> {\n static symbol = Symbol('fetch')\n\n constructor() {\n super(FetchInterceptor.symbol)\n }\n\n protected checkEnvironment() {\n return (\n typeof globalThis !== 'undefined' &&\n typeof globalThis.fetch !== 'undefined'\n )\n }\n\n protected async setup() {\n const pureFetch = globalThis.fetch\n\n invariant(\n !(pureFetch as any)[IS_PATCHED_MODULE],\n 'Failed to patch the \"fetch\" module: already patched.'\n )\n\n globalThis.fetch = async (input, init) => {\n const requestId = createRequestId()\n\n /**\n * @note Resolve potentially relative request URL\n * against the present `location`. This is mainly\n * for native `fetch` in JSDOM.\n * @see https://github.com/mswjs/msw/issues/1625\n */\n const resolvedInput =\n typeof input === 'string' &&\n typeof location !== 'undefined' &&\n !canParseUrl(input)\n ? new URL(input, location.origin)\n : input\n\n const request = new Request(resolvedInput, init)\n const responsePromise = new DeferredPromise<Response>()\n const controller = new RequestController(request)\n\n this.logger.info('[%s] %s', request.method, request.url)\n this.logger.info('awaiting for the mocked response...')\n\n this.logger.info(\n 'emitting the \"request\" event for %s listener(s)...',\n this.emitter.listenerCount('request')\n )\n\n const isRequestHandled = await handleRequest({\n request,\n requestId,\n emitter: this.emitter,\n controller,\n onResponse: async (response) => {\n this.logger.info('received mocked response!', {\n response,\n })\n\n if (this.emitter.listenerCount('response') > 0) {\n this.logger.info('emitting the \"response\" event...')\n\n // Await the response listeners to finish before resolving\n // the response promise. This ensures all your logic finishes\n // before the interceptor resolves the pending response.\n await emitAsync(this.emitter, 'response', {\n // Clone the mocked response for the \"response\" event listener.\n // This way, the listener can read the response and not lock its body\n // for the actual fetch consumer.\n response: response.clone(),\n isMockedResponse: true,\n request,\n requestId,\n })\n }\n\n // Set the \"response.url\" property to equal the intercepted request URL.\n Object.defineProperty(response, 'url', {\n writable: false,\n enumerable: true,\n configurable: false,\n value: request.url,\n })\n\n responsePromise.resolve(response)\n },\n onRequestError: (response) => {\n this.logger.info('request has errored!', { response })\n responsePromise.reject(createNetworkError(response))\n },\n onError: (error) => {\n this.logger.info('request has been aborted!', { error })\n responsePromise.reject(error)\n },\n })\n\n if (isRequestHandled) {\n this.logger.info('request has been handled, returning mock promise...')\n return responsePromise\n }\n\n this.logger.info(\n 'no mocked response received, performing request as-is...'\n )\n\n return pureFetch(request).then((response) => {\n this.logger.info('original fetch performed', response)\n\n if (this.emitter.listenerCount('response') > 0) {\n this.logger.info('emitting the \"response\" event...')\n\n const responseClone = response.clone()\n\n this.emitter.emit('response', {\n response: responseClone,\n isMockedResponse: false,\n request,\n requestId,\n })\n }\n\n return response\n })\n }\n\n Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {\n enumerable: true,\n configurable: true,\n value: true,\n })\n\n this.subscriptions.push(() => {\n Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {\n value: undefined,\n })\n\n globalThis.fetch = pureFetch\n\n this.logger.info(\n 'restored native \"globalThis.fetch\"!',\n globalThis.fetch.name\n )\n })\n }\n}\n\nfunction createNetworkError(cause: unknown) {\n return Object.assign(new TypeError('Failed to fetch'), {\n cause,\n })\n}\n","/**\n * Returns a boolean indicating whether the given URL string\n * can be parsed into a `URL` instance.\n * A substitute for `URL.canParse()` for Node.js 18.\n */\nexport function canParseUrl(url: string): boolean {\n try {\n new URL(url)\n return true\n } catch (_error) {\n return false\n }\n}\n"]}
|
|
@@ -1,141 +1,10 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunkUN335ZTDjs = require('../../chunk-UN335ZTD.js');
|
|
4
|
+
require('../../chunk-IDEEMJ3F.js');
|
|
5
|
+
require('../../chunk-5WWNCLB3.js');
|
|
6
|
+
require('../../chunk-YGM3BCJU.js');
|
|
4
7
|
|
|
5
8
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var _chunk5WWNCLB3js = require('../../chunk-5WWNCLB3.js');
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
var _chunkYGM3BCJUjs = require('../../chunk-YGM3BCJU.js');
|
|
13
|
-
|
|
14
|
-
// src/interceptors/fetch/index.ts
|
|
15
|
-
var _outvariant = require('outvariant');
|
|
16
|
-
var _deferredpromise = require('@open-draft/deferred-promise');
|
|
17
|
-
|
|
18
|
-
// src/utils/canParseUrl.ts
|
|
19
|
-
function canParseUrl(url) {
|
|
20
|
-
try {
|
|
21
|
-
new URL(url);
|
|
22
|
-
return true;
|
|
23
|
-
} catch (_error) {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// src/interceptors/fetch/index.ts
|
|
29
|
-
var _FetchInterceptor = class extends _chunkYGM3BCJUjs.Interceptor {
|
|
30
|
-
constructor() {
|
|
31
|
-
super(_FetchInterceptor.symbol);
|
|
32
|
-
}
|
|
33
|
-
checkEnvironment() {
|
|
34
|
-
return typeof globalThis !== "undefined" && typeof globalThis.fetch !== "undefined";
|
|
35
|
-
}
|
|
36
|
-
async setup() {
|
|
37
|
-
const pureFetch = globalThis.fetch;
|
|
38
|
-
_outvariant.invariant.call(void 0,
|
|
39
|
-
!pureFetch[_chunkIDEEMJ3Fjs.IS_PATCHED_MODULE],
|
|
40
|
-
'Failed to patch the "fetch" module: already patched.'
|
|
41
|
-
);
|
|
42
|
-
globalThis.fetch = async (input, init) => {
|
|
43
|
-
const requestId = _chunkYGM3BCJUjs.createRequestId.call(void 0, );
|
|
44
|
-
const resolvedInput = typeof input === "string" && typeof location !== "undefined" && !canParseUrl(input) ? new URL(input, location.origin) : input;
|
|
45
|
-
const request = new Request(resolvedInput, init);
|
|
46
|
-
const responsePromise = new (0, _deferredpromise.DeferredPromise)();
|
|
47
|
-
const controller = new (0, _chunk5WWNCLB3js.RequestController)(request);
|
|
48
|
-
this.logger.info("[%s] %s", request.method, request.url);
|
|
49
|
-
this.logger.info("awaiting for the mocked response...");
|
|
50
|
-
this.logger.info(
|
|
51
|
-
'emitting the "request" event for %s listener(s)...',
|
|
52
|
-
this.emitter.listenerCount("request")
|
|
53
|
-
);
|
|
54
|
-
const isRequestHandled = await _chunk5WWNCLB3js.handleRequest.call(void 0, {
|
|
55
|
-
request,
|
|
56
|
-
requestId,
|
|
57
|
-
emitter: this.emitter,
|
|
58
|
-
controller,
|
|
59
|
-
onResponse: async (response) => {
|
|
60
|
-
this.logger.info("received mocked response!", {
|
|
61
|
-
response
|
|
62
|
-
});
|
|
63
|
-
if (this.emitter.listenerCount("response") > 0) {
|
|
64
|
-
this.logger.info('emitting the "response" event...');
|
|
65
|
-
await _chunk5WWNCLB3js.emitAsync.call(void 0, this.emitter, "response", {
|
|
66
|
-
// Clone the mocked response for the "response" event listener.
|
|
67
|
-
// This way, the listener can read the response and not lock its body
|
|
68
|
-
// for the actual fetch consumer.
|
|
69
|
-
response: response.clone(),
|
|
70
|
-
isMockedResponse: true,
|
|
71
|
-
request,
|
|
72
|
-
requestId
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
Object.defineProperty(response, "url", {
|
|
76
|
-
writable: false,
|
|
77
|
-
enumerable: true,
|
|
78
|
-
configurable: false,
|
|
79
|
-
value: request.url
|
|
80
|
-
});
|
|
81
|
-
responsePromise.resolve(response);
|
|
82
|
-
},
|
|
83
|
-
onRequestError: (response) => {
|
|
84
|
-
this.logger.info("request has errored!", { response });
|
|
85
|
-
responsePromise.reject(createNetworkError(response));
|
|
86
|
-
},
|
|
87
|
-
onError: (error) => {
|
|
88
|
-
this.logger.info("request has been aborted!", { error });
|
|
89
|
-
responsePromise.reject(error);
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
if (isRequestHandled) {
|
|
93
|
-
this.logger.info("request has been handled, returning mock promise...");
|
|
94
|
-
return responsePromise;
|
|
95
|
-
}
|
|
96
|
-
this.logger.info(
|
|
97
|
-
"no mocked response received, performing request as-is..."
|
|
98
|
-
);
|
|
99
|
-
return pureFetch(request).then((response) => {
|
|
100
|
-
this.logger.info("original fetch performed", response);
|
|
101
|
-
if (this.emitter.listenerCount("response") > 0) {
|
|
102
|
-
this.logger.info('emitting the "response" event...');
|
|
103
|
-
const responseClone = response.clone();
|
|
104
|
-
this.emitter.emit("response", {
|
|
105
|
-
response: responseClone,
|
|
106
|
-
isMockedResponse: false,
|
|
107
|
-
request,
|
|
108
|
-
requestId
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
return response;
|
|
112
|
-
});
|
|
113
|
-
};
|
|
114
|
-
Object.defineProperty(globalThis.fetch, _chunkIDEEMJ3Fjs.IS_PATCHED_MODULE, {
|
|
115
|
-
enumerable: true,
|
|
116
|
-
configurable: true,
|
|
117
|
-
value: true
|
|
118
|
-
});
|
|
119
|
-
this.subscriptions.push(() => {
|
|
120
|
-
Object.defineProperty(globalThis.fetch, _chunkIDEEMJ3Fjs.IS_PATCHED_MODULE, {
|
|
121
|
-
value: void 0
|
|
122
|
-
});
|
|
123
|
-
globalThis.fetch = pureFetch;
|
|
124
|
-
this.logger.info(
|
|
125
|
-
'restored native "globalThis.fetch"!',
|
|
126
|
-
globalThis.fetch.name
|
|
127
|
-
);
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
var FetchInterceptor = _FetchInterceptor;
|
|
132
|
-
FetchInterceptor.symbol = Symbol("fetch");
|
|
133
|
-
function createNetworkError(cause) {
|
|
134
|
-
return Object.assign(new TypeError("Failed to fetch"), {
|
|
135
|
-
cause
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
exports.FetchInterceptor = FetchInterceptor;
|
|
9
|
+
exports.FetchInterceptor = _chunkUN335ZTDjs.FetchInterceptor;
|
|
141
10
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -1,140 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
} from "../../chunk-
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
handleRequest
|
|
8
|
-
} from "../../chunk-KY3RJ2M3.mjs";
|
|
9
|
-
import {
|
|
10
|
-
Interceptor,
|
|
11
|
-
createRequestId
|
|
12
|
-
} from "../../chunk-BUCULLYM.mjs";
|
|
13
|
-
|
|
14
|
-
// src/interceptors/fetch/index.ts
|
|
15
|
-
import { invariant } from "outvariant";
|
|
16
|
-
import { DeferredPromise } from "@open-draft/deferred-promise";
|
|
17
|
-
|
|
18
|
-
// src/utils/canParseUrl.ts
|
|
19
|
-
function canParseUrl(url) {
|
|
20
|
-
try {
|
|
21
|
-
new URL(url);
|
|
22
|
-
return true;
|
|
23
|
-
} catch (_error) {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// src/interceptors/fetch/index.ts
|
|
29
|
-
var _FetchInterceptor = class extends Interceptor {
|
|
30
|
-
constructor() {
|
|
31
|
-
super(_FetchInterceptor.symbol);
|
|
32
|
-
}
|
|
33
|
-
checkEnvironment() {
|
|
34
|
-
return typeof globalThis !== "undefined" && typeof globalThis.fetch !== "undefined";
|
|
35
|
-
}
|
|
36
|
-
async setup() {
|
|
37
|
-
const pureFetch = globalThis.fetch;
|
|
38
|
-
invariant(
|
|
39
|
-
!pureFetch[IS_PATCHED_MODULE],
|
|
40
|
-
'Failed to patch the "fetch" module: already patched.'
|
|
41
|
-
);
|
|
42
|
-
globalThis.fetch = async (input, init) => {
|
|
43
|
-
const requestId = createRequestId();
|
|
44
|
-
const resolvedInput = typeof input === "string" && typeof location !== "undefined" && !canParseUrl(input) ? new URL(input, location.origin) : input;
|
|
45
|
-
const request = new Request(resolvedInput, init);
|
|
46
|
-
const responsePromise = new DeferredPromise();
|
|
47
|
-
const controller = new RequestController(request);
|
|
48
|
-
this.logger.info("[%s] %s", request.method, request.url);
|
|
49
|
-
this.logger.info("awaiting for the mocked response...");
|
|
50
|
-
this.logger.info(
|
|
51
|
-
'emitting the "request" event for %s listener(s)...',
|
|
52
|
-
this.emitter.listenerCount("request")
|
|
53
|
-
);
|
|
54
|
-
const isRequestHandled = await handleRequest({
|
|
55
|
-
request,
|
|
56
|
-
requestId,
|
|
57
|
-
emitter: this.emitter,
|
|
58
|
-
controller,
|
|
59
|
-
onResponse: async (response) => {
|
|
60
|
-
this.logger.info("received mocked response!", {
|
|
61
|
-
response
|
|
62
|
-
});
|
|
63
|
-
if (this.emitter.listenerCount("response") > 0) {
|
|
64
|
-
this.logger.info('emitting the "response" event...');
|
|
65
|
-
await emitAsync(this.emitter, "response", {
|
|
66
|
-
// Clone the mocked response for the "response" event listener.
|
|
67
|
-
// This way, the listener can read the response and not lock its body
|
|
68
|
-
// for the actual fetch consumer.
|
|
69
|
-
response: response.clone(),
|
|
70
|
-
isMockedResponse: true,
|
|
71
|
-
request,
|
|
72
|
-
requestId
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
Object.defineProperty(response, "url", {
|
|
76
|
-
writable: false,
|
|
77
|
-
enumerable: true,
|
|
78
|
-
configurable: false,
|
|
79
|
-
value: request.url
|
|
80
|
-
});
|
|
81
|
-
responsePromise.resolve(response);
|
|
82
|
-
},
|
|
83
|
-
onRequestError: (response) => {
|
|
84
|
-
this.logger.info("request has errored!", { response });
|
|
85
|
-
responsePromise.reject(createNetworkError(response));
|
|
86
|
-
},
|
|
87
|
-
onError: (error) => {
|
|
88
|
-
this.logger.info("request has been aborted!", { error });
|
|
89
|
-
responsePromise.reject(error);
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
if (isRequestHandled) {
|
|
93
|
-
this.logger.info("request has been handled, returning mock promise...");
|
|
94
|
-
return responsePromise;
|
|
95
|
-
}
|
|
96
|
-
this.logger.info(
|
|
97
|
-
"no mocked response received, performing request as-is..."
|
|
98
|
-
);
|
|
99
|
-
return pureFetch(request).then((response) => {
|
|
100
|
-
this.logger.info("original fetch performed", response);
|
|
101
|
-
if (this.emitter.listenerCount("response") > 0) {
|
|
102
|
-
this.logger.info('emitting the "response" event...');
|
|
103
|
-
const responseClone = response.clone();
|
|
104
|
-
this.emitter.emit("response", {
|
|
105
|
-
response: responseClone,
|
|
106
|
-
isMockedResponse: false,
|
|
107
|
-
request,
|
|
108
|
-
requestId
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
return response;
|
|
112
|
-
});
|
|
113
|
-
};
|
|
114
|
-
Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {
|
|
115
|
-
enumerable: true,
|
|
116
|
-
configurable: true,
|
|
117
|
-
value: true
|
|
118
|
-
});
|
|
119
|
-
this.subscriptions.push(() => {
|
|
120
|
-
Object.defineProperty(globalThis.fetch, IS_PATCHED_MODULE, {
|
|
121
|
-
value: void 0
|
|
122
|
-
});
|
|
123
|
-
globalThis.fetch = pureFetch;
|
|
124
|
-
this.logger.info(
|
|
125
|
-
'restored native "globalThis.fetch"!',
|
|
126
|
-
globalThis.fetch.name
|
|
127
|
-
);
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
var FetchInterceptor = _FetchInterceptor;
|
|
132
|
-
FetchInterceptor.symbol = Symbol("fetch");
|
|
133
|
-
function createNetworkError(cause) {
|
|
134
|
-
return Object.assign(new TypeError("Failed to fetch"), {
|
|
135
|
-
cause
|
|
136
|
-
});
|
|
137
|
-
}
|
|
2
|
+
FetchInterceptor
|
|
3
|
+
} from "../../chunk-RTGLFNO3.mjs";
|
|
4
|
+
import "../../chunk-BZ3Y7YV5.mjs";
|
|
5
|
+
import "../../chunk-KY3RJ2M3.mjs";
|
|
6
|
+
import "../../chunk-BUCULLYM.mjs";
|
|
138
7
|
export {
|
|
139
8
|
FetchInterceptor
|
|
140
9
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|