@mswjs/interceptors 0.38.3 → 0.38.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -0
- package/lib/browser/{chunk-FGSEOIC4.js → chunk-GTJ35JP4.js} +30 -12
- package/lib/browser/chunk-GTJ35JP4.js.map +1 -0
- package/lib/browser/{chunk-WVIAZNTB.js → chunk-HOXEZV62.js} +4 -4
- package/lib/browser/{chunk-P6ZNAT4K.mjs → chunk-JZOFC66D.mjs} +2 -2
- package/lib/browser/{chunk-AF44JPFO.js → chunk-K4XV34DA.js} +6 -6
- package/lib/browser/{chunk-XHFULOZS.mjs → chunk-KLI5672K.mjs} +2 -2
- package/lib/{node/chunk-5KMS5CTP.mjs → browser/chunk-L37TY7LC.mjs} +30 -14
- package/lib/browser/chunk-L37TY7LC.mjs.map +1 -0
- package/lib/browser/{glossary-6564c252.d.ts → glossary-7152281e.d.ts} +5 -3
- package/lib/browser/index.d.ts +1 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.d.ts +1 -1
- package/lib/browser/interceptors/XMLHttpRequest/index.js +3 -3
- package/lib/browser/interceptors/XMLHttpRequest/index.mjs +2 -2
- package/lib/browser/interceptors/fetch/index.d.ts +1 -1
- package/lib/browser/interceptors/fetch/index.js +3 -3
- package/lib/browser/interceptors/fetch/index.mjs +2 -2
- package/lib/browser/presets/browser.d.ts +1 -1
- package/lib/browser/presets/browser.js +5 -5
- package/lib/browser/presets/browser.mjs +3 -3
- package/lib/node/{BatchInterceptor-67bf41ba.d.ts → BatchInterceptor-5b72232f.d.ts} +1 -1
- package/lib/node/{Interceptor-436630be.d.ts → Interceptor-bc5a9d8e.d.ts} +5 -3
- package/lib/node/RemoteHttpInterceptor.d.ts +2 -2
- package/lib/node/RemoteHttpInterceptor.js +9 -9
- package/lib/node/RemoteHttpInterceptor.mjs +4 -4
- package/lib/node/{chunk-CV5EAVU6.mjs → chunk-6ZKXDFUW.mjs} +3 -7
- package/lib/node/chunk-6ZKXDFUW.mjs.map +1 -0
- package/lib/node/{chunk-6L3PFBGT.js → chunk-C2JSMMHY.js} +32 -13
- package/lib/node/chunk-C2JSMMHY.js.map +1 -0
- package/lib/node/{chunk-CNH7N6AT.js → chunk-EAFENFG6.js} +6 -6
- package/lib/{browser/chunk-H5O73WD2.mjs → node/chunk-LGXJ3UUF.mjs} +33 -12
- package/lib/node/chunk-LGXJ3UUF.mjs.map +1 -0
- package/lib/node/{chunk-VEYMW7A4.mjs → chunk-MS2P6MIR.mjs} +2 -2
- package/lib/node/{chunk-BURFIIB6.js → chunk-NH6SN3VB.js} +4 -4
- package/lib/node/{chunk-T3P6W5EK.mjs → chunk-S6JBRDIL.mjs} +2 -2
- package/lib/node/{chunk-OU5BNOD7.js → chunk-TBQOE5KZ.js} +11 -15
- package/lib/node/chunk-TBQOE5KZ.js.map +1 -0
- package/lib/node/index.d.ts +2 -2
- package/lib/node/interceptors/ClientRequest/index.d.ts +1 -1
- package/lib/node/interceptors/ClientRequest/index.js +3 -3
- package/lib/node/interceptors/ClientRequest/index.mjs +2 -2
- package/lib/node/interceptors/XMLHttpRequest/index.d.ts +1 -1
- package/lib/node/interceptors/XMLHttpRequest/index.js +3 -3
- package/lib/node/interceptors/XMLHttpRequest/index.mjs +2 -2
- package/lib/node/interceptors/fetch/index.d.ts +1 -1
- package/lib/node/interceptors/fetch/index.js +3 -3
- package/lib/node/interceptors/fetch/index.mjs +2 -2
- package/lib/node/presets/node.d.ts +1 -1
- package/lib/node/presets/node.js +7 -7
- package/lib/node/presets/node.mjs +4 -4
- package/package.json +1 -1
- package/src/RequestController.test.ts +8 -0
- package/src/RequestController.ts +9 -5
- package/src/utils/handleRequest.ts +27 -9
- package/src/utils/responseUtils.ts +22 -2
- package/lib/browser/chunk-FGSEOIC4.js.map +0 -1
- package/lib/browser/chunk-H5O73WD2.mjs.map +0 -1
- package/lib/node/chunk-5KMS5CTP.mjs.map +0 -1
- package/lib/node/chunk-6L3PFBGT.js.map +0 -1
- package/lib/node/chunk-CV5EAVU6.mjs.map +0 -1
- package/lib/node/chunk-OU5BNOD7.js.map +0 -1
- /package/lib/browser/{chunk-WVIAZNTB.js.map → chunk-HOXEZV62.js.map} +0 -0
- /package/lib/browser/{chunk-P6ZNAT4K.mjs.map → chunk-JZOFC66D.mjs.map} +0 -0
- /package/lib/browser/{chunk-AF44JPFO.js.map → chunk-K4XV34DA.js.map} +0 -0
- /package/lib/browser/{chunk-XHFULOZS.mjs.map → chunk-KLI5672K.mjs.map} +0 -0
- /package/lib/node/{chunk-CNH7N6AT.js.map → chunk-EAFENFG6.js.map} +0 -0
- /package/lib/node/{chunk-VEYMW7A4.mjs.map → chunk-MS2P6MIR.mjs.map} +0 -0
- /package/lib/node/{chunk-BURFIIB6.js.map → chunk-NH6SN3VB.js.map} +0 -0
- /package/lib/node/{chunk-T3P6W5EK.mjs.map → chunk-S6JBRDIL.mjs.map} +0 -0
package/README.md
CHANGED
|
@@ -104,6 +104,21 @@ You can respond to the intercepted HTTP request by constructing a Fetch API Resp
|
|
|
104
104
|
- Does **not** provide any request matching logic;
|
|
105
105
|
- Does **not** handle requests by default.
|
|
106
106
|
|
|
107
|
+
## Limitations
|
|
108
|
+
|
|
109
|
+
- Interceptors will hang indefinitely if you call `req.end()` in the `connect` event listener of the respective `socket`:
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
req.on('socket', (socket) => {
|
|
113
|
+
socket.on('connect', () => {
|
|
114
|
+
// ❌ While this is allowed in Node.js, this cannot be handled in Interceptors.
|
|
115
|
+
req.end()
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
> This limitation is intrinsic to the interception algorithm used by the library. In order for it to emit the `connect` event on the socket, the library must know if you've handled the request in any way (e.g. responded with a mocked response or errored it). For that, it emits the `request` event on the interceptor where you can handle the request. Since you can consume the request stream in the `request` event, it waits until the request body stream is complete (i.e. until `req.end()` is called). This creates a catch 22 that causes this limitation.
|
|
121
|
+
|
|
107
122
|
## Getting started
|
|
108
123
|
|
|
109
124
|
```bash
|
|
@@ -39,12 +39,14 @@ var RequestController = class {
|
|
|
39
39
|
this[kResponsePromise].resolve(response);
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
|
-
* Error this request with the given
|
|
42
|
+
* Error this request with the given reason.
|
|
43
|
+
*
|
|
43
44
|
* @example
|
|
44
45
|
* controller.errorWith()
|
|
45
46
|
* controller.errorWith(new Error('Oops!'))
|
|
47
|
+
* controller.errorWith({ message: 'Oops!'})
|
|
46
48
|
*/
|
|
47
|
-
errorWith(
|
|
49
|
+
errorWith(reason) {
|
|
48
50
|
_outvariant.invariant.as(
|
|
49
51
|
InterceptorError,
|
|
50
52
|
!this[kRequestHandled],
|
|
@@ -53,7 +55,7 @@ var RequestController = class {
|
|
|
53
55
|
this.request.url
|
|
54
56
|
);
|
|
55
57
|
this[kRequestHandled] = true;
|
|
56
|
-
this[kResponsePromise].resolve(
|
|
58
|
+
this[kResponsePromise].resolve(reason);
|
|
57
59
|
}
|
|
58
60
|
};
|
|
59
61
|
kResponsePromise, kRequestHandled;
|
|
@@ -73,6 +75,11 @@ async function emitAsync(emitter, eventName, ...data) {
|
|
|
73
75
|
|
|
74
76
|
var _until = require('@open-draft/until');
|
|
75
77
|
|
|
78
|
+
// src/utils/isObject.ts
|
|
79
|
+
function isObject(value, loose = false) {
|
|
80
|
+
return loose ? Object.prototype.toString.call(value).startsWith("[object ") : Object.prototype.toString.call(value) === "[object Object]";
|
|
81
|
+
}
|
|
82
|
+
|
|
76
83
|
// src/utils/isPropertyAccessible.ts
|
|
77
84
|
function isPropertyAccessible(obj, key) {
|
|
78
85
|
try {
|
|
@@ -103,7 +110,10 @@ function createServerErrorResponse(body) {
|
|
|
103
110
|
);
|
|
104
111
|
}
|
|
105
112
|
function isResponseError(response) {
|
|
106
|
-
return isPropertyAccessible(response, "type") && response.type === "error";
|
|
113
|
+
return response != null && response instanceof Response && isPropertyAccessible(response, "type") && response.type === "error";
|
|
114
|
+
}
|
|
115
|
+
function isResponseLike(value) {
|
|
116
|
+
return isObject(value, true) && isPropertyAccessible(value, "status") && isPropertyAccessible(value, "statusText") && isPropertyAccessible(value, "bodyUsed");
|
|
107
117
|
}
|
|
108
118
|
|
|
109
119
|
// src/utils/isNodeLikeError.ts
|
|
@@ -122,12 +132,21 @@ async function handleRequest(options) {
|
|
|
122
132
|
const handleResponse = async (response) => {
|
|
123
133
|
if (response instanceof Error) {
|
|
124
134
|
options.onError(response);
|
|
125
|
-
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
if (isResponseError(response)) {
|
|
126
138
|
options.onRequestError(response);
|
|
127
|
-
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
if (isResponseLike(response)) {
|
|
128
142
|
await options.onResponse(response);
|
|
143
|
+
return true;
|
|
129
144
|
}
|
|
130
|
-
|
|
145
|
+
if (isObject(response)) {
|
|
146
|
+
options.onError(response);
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
return false;
|
|
131
150
|
};
|
|
132
151
|
const handleResponseError = async (error) => {
|
|
133
152
|
if (error instanceof InterceptorError) {
|
|
@@ -165,7 +184,7 @@ async function handleRequest(options) {
|
|
|
165
184
|
}
|
|
166
185
|
}
|
|
167
186
|
const result = await _until.until.call(void 0, async () => {
|
|
168
|
-
const
|
|
187
|
+
const requestListenersPromise = emitAsync(options.emitter, "request", {
|
|
169
188
|
requestId: options.requestId,
|
|
170
189
|
request: options.request,
|
|
171
190
|
controller: options.controller
|
|
@@ -173,11 +192,10 @@ async function handleRequest(options) {
|
|
|
173
192
|
await Promise.race([
|
|
174
193
|
// Short-circuit the request handling promise if the request gets aborted.
|
|
175
194
|
requestAbortPromise,
|
|
176
|
-
|
|
195
|
+
requestListenersPromise,
|
|
177
196
|
options.controller[kResponsePromise]
|
|
178
197
|
]);
|
|
179
|
-
|
|
180
|
-
return mockedResponse;
|
|
198
|
+
return await options.controller[kResponsePromise];
|
|
181
199
|
});
|
|
182
200
|
if (requestAbortPromise.state === "rejected") {
|
|
183
201
|
options.onError(requestAbortPromise.rejectionReason);
|
|
@@ -225,4 +243,4 @@ async function handleRequest(options) {
|
|
|
225
243
|
|
|
226
244
|
|
|
227
245
|
exports.RequestController = RequestController; exports.emitAsync = emitAsync; exports.handleRequest = handleRequest;
|
|
228
|
-
//# sourceMappingURL=chunk-
|
|
246
|
+
//# sourceMappingURL=chunk-GTJ35JP4.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/RequestController.ts","../../src/InterceptorError.ts","../../src/utils/emitAsync.ts","../../src/utils/handleRequest.ts","../../src/utils/isObject.ts","../../src/utils/isPropertyAccessible.ts","../../src/utils/responseUtils.ts","../../src/utils/isNodeLikeError.ts"],"names":["DeferredPromise"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;;;ACDzB,IAAM,mBAAN,cAA+B,MAAM;AAAA,EAC1C,YAAY,SAAkB;AAC5B,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,WAAO,eAAe,MAAM,iBAAiB,SAAS;AAAA,EACxD;AACF;;;ADFA,IAAM,kBAAkB,OAAO,iBAAiB;AACzC,IAAM,mBAAmB,OAAO,kBAAkB;AAElD,IAAM,oBAAN,MAAwB;AAAA,EAkB7B,YAAoB,SAAkB;AAAlB;AAClB,SAAK,eAAe,IAAI;AACxB,SAAK,gBAAgB,IAAI,IAAI,gBAAgB;AAAA,EAC/C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASO,YAAY,UAA0B;AAC3C,cAAU;AAAA,MACR;AAAA,MACA,CAAC,KAAK,eAAe;AAAA,MACrB;AAAA,MACA,KAAK,QAAQ;AAAA,MACb,KAAK,QAAQ;AAAA,IACf;AAEA,SAAK,eAAe,IAAI;AACxB,SAAK,gBAAgB,EAAE,QAAQ,QAAQ;AAAA,EASzC;AAAA;AAAA;AAAA;AAAA;AA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QAClE;AAAA,MACF,CAAC;AAED,YAAM,aAAa,MAAM;AAAA,QACvB,MAAM,6BAA6B,gBAAgB;AAAA,MACrD;AASA,UAAI,WAAW,OAAO;AACpB,eAAO,oBAAoB,WAAW,KAAK;AAAA,MAC7C;AAEA,UAAI,WAAW,MAAM;AACnB,eAAO,eAAe,WAAW,IAAI;AAAA,MACvC;AAAA,IACF;AAGA,YAAQ,WAAW,0BAA0B,OAAO,KAAK,CAAC;AAC1D,WAAO;AAAA,EACT;AAQA,MAAI,OAAO,MAAM;AACf,WAAO,eAAe,OAAO,IAAI;AAAA,EACnC;AAGA,SAAO;AACT","sourcesContent":["import { invariant } from 'outvariant'\nimport { DeferredPromise } from '@open-draft/deferred-promise'\nimport { InterceptorError } from './InterceptorError'\n\nconst kRequestHandled = Symbol('kRequestHandled')\nexport const kResponsePromise = Symbol('kResponsePromise')\n\nexport class RequestController {\n /**\n * Internal response promise.\n * Available only for the library internals to grab the\n * response instance provided by the developer.\n * @note This promise cannot be rejected. It's either infinitely\n * pending or resolved with whichever Response was passed to `respondWith()`.\n */\n [kResponsePromise]: DeferredPromise<\n Response | Record<string, any> | undefined\n >;\n\n /**\n * Internal flag indicating if this request has been handled.\n * @note The response promise becomes \"fulfilled\" on the next tick.\n */\n [kRequestHandled]: boolean\n\n constructor(private request: Request) {\n this[kRequestHandled] = false\n this[kResponsePromise] = new DeferredPromise()\n }\n\n /**\n * Respond to this request with the given `Response` instance.\n * @example\n * controller.respondWith(new Response())\n * controller.respondWith(Response.json({ id }))\n * controller.respondWith(Response.error())\n */\n public respondWith(response: Response): void {\n invariant.as(\n InterceptorError,\n !this[kRequestHandled],\n 'Failed to respond to the \"%s %s\" request: the \"request\" event has already been handled.',\n this.request.method,\n this.request.url\n )\n\n this[kRequestHandled] = true\n this[kResponsePromise].resolve(response)\n\n /**\n * @note The request controller doesn't do anything\n * apart from letting the interceptor await the response\n * provided by the developer through the response promise.\n * Each interceptor implements the actual respondWith/errorWith\n * logic based on that interceptor's needs.\n */\n }\n\n /**\n * Error this request with the given reason.\n *\n * @example\n * controller.errorWith()\n * controller.errorWith(new Error('Oops!'))\n * controller.errorWith({ message: 'Oops!'})\n */\n public errorWith(reason?: Error | Record<string, any>): void {\n invariant.as(\n InterceptorError,\n !this[kRequestHandled],\n 'Failed to error the \"%s %s\" request: the \"request\" event has already been handled.',\n this.request.method,\n this.request.url\n )\n\n this[kRequestHandled] = true\n\n /**\n * @note Resolve the response promise, not reject.\n * This helps us differentiate between unhandled exceptions\n * and intended errors (\"errorWith\") while waiting for the response.\n */\n this[kResponsePromise].resolve(reason)\n }\n}\n","export class InterceptorError extends Error {\n constructor(message?: string) {\n super(message)\n this.name = 'InterceptorError'\n Object.setPrototypeOf(this, InterceptorError.prototype)\n }\n}\n","import { Emitter, EventMap } from 'strict-event-emitter'\n\n/**\n * Emits an event on the given emitter but executes\n * the listeners sequentially. This accounts for asynchronous\n * listeners (e.g. those having \"sleep\" and handling the request).\n */\nexport async function emitAsync<\n Events extends EventMap,\n EventName extends keyof Events\n>(\n emitter: Emitter<Events>,\n eventName: EventName,\n ...data: Events[EventName]\n): Promise<void> {\n const listners = emitter.listeners(eventName)\n\n if (listners.length === 0) {\n return\n }\n\n for (const listener of listners) {\n await listener.apply(emitter, data)\n }\n}\n","import type { Emitter } from 'strict-event-emitter'\nimport { DeferredPromise } from '@open-draft/deferred-promise'\nimport { until } from '@open-draft/until'\nimport type { HttpRequestEventMap } from '../glossary'\nimport { emitAsync } from './emitAsync'\nimport { kResponsePromise, RequestController } from '../RequestController'\nimport {\n createServerErrorResponse,\n isResponseError,\n isResponseLike,\n ResponseError,\n} from './responseUtils'\nimport { InterceptorError } from '../InterceptorError'\nimport { isNodeLikeError } from './isNodeLikeError'\nimport { isObject } from './isObject'\n\ninterface HandleRequestOptions {\n requestId: string\n request: Request\n emitter: Emitter<HttpRequestEventMap>\n controller: RequestController\n\n /**\n * Called when the request has been handled\n * with the given `Response` instance.\n */\n onResponse: (response: Response) => void | Promise<void>\n\n /**\n * Called when the request has been handled\n * with the given `Response.error()` instance.\n */\n onRequestError: (response: ResponseError) => void\n\n /**\n * Called when an unhandled error happens during the\n * request handling. This is never a thrown error/response.\n */\n onError: (error: unknown) => void\n}\n\n/**\n * @returns {Promise<boolean>} Indicates whether the request has been handled.\n */\nexport async function handleRequest(\n options: HandleRequestOptions\n): Promise<boolean> {\n const handleResponse = async (\n response: Response | Error | Record<string, any>\n ) => {\n if (response instanceof Error) {\n options.onError(response)\n return true\n }\n\n // Handle \"Response.error()\" instances.\n if (isResponseError(response)) {\n options.onRequestError(response)\n return true\n }\n\n /**\n * Handle normal responses or response-like objects.\n * @note This must come before the arbitrary object check\n * since Response instances are, in fact, objects.\n */\n if (isResponseLike(response)) {\n await options.onResponse(response)\n return true\n }\n\n // Handle arbitrary objects provided to `.errorWith(reason)`.\n if (isObject(response)) {\n options.onError(response)\n return true\n }\n\n return false\n }\n\n const handleResponseError = async (error: unknown): Promise<boolean> => {\n // Forward the special interceptor error instances\n // to the developer. These must not be handled in any way.\n if (error instanceof InterceptorError) {\n throw result.error\n }\n\n // Support mocking Node.js-like errors.\n if (isNodeLikeError(error)) {\n options.onError(error)\n return true\n }\n\n // Handle thrown responses.\n if (error instanceof Response) {\n return await handleResponse(error)\n }\n\n return false\n }\n\n // Add the last \"request\" listener to check if the request\n // has been handled in any way. If it hasn't, resolve the\n // response promise with undefined.\n options.emitter.once('request', ({ requestId: pendingRequestId }) => {\n if (pendingRequestId !== options.requestId) {\n return\n }\n\n if (options.controller[kResponsePromise].state === 'pending') {\n options.controller[kResponsePromise].resolve(undefined)\n }\n })\n\n const requestAbortPromise = new DeferredPromise<void, unknown>()\n\n /**\n * @note `signal` is not always defined in React Native.\n */\n if (options.request.signal) {\n if (options.request.signal.aborted) {\n requestAbortPromise.reject(options.request.signal.reason)\n } else {\n options.request.signal.addEventListener(\n 'abort',\n () => {\n requestAbortPromise.reject(options.request.signal.reason)\n },\n { once: true }\n )\n }\n }\n\n const result = await until(async () => {\n // Emit the \"request\" event and wait until all the listeners\n // for that event are finished (e.g. async listeners awaited).\n // By the end of this promise, the developer cannot affect the\n // request anymore.\n const requestListenersPromise = emitAsync(options.emitter, 'request', {\n requestId: options.requestId,\n request: options.request,\n controller: options.controller,\n })\n\n await Promise.race([\n // Short-circuit the request handling promise if the request gets aborted.\n requestAbortPromise,\n requestListenersPromise,\n options.controller[kResponsePromise],\n ])\n\n // The response promise will settle immediately once\n // the developer calls either \"respondWith\" or \"errorWith\".\n return await options.controller[kResponsePromise]\n })\n\n // Handle the request being aborted while waiting for the request listeners.\n if (requestAbortPromise.state === 'rejected') {\n options.onError(requestAbortPromise.rejectionReason)\n return true\n }\n\n if (result.error) {\n // Handle the error during the request listener execution.\n // These can be thrown responses or request errors.\n if (await handleResponseError(result.error)) {\n return true\n }\n\n // If the developer has added \"unhandledException\" listeners,\n // allow them to handle the error. They can translate it to a\n // mocked response, network error, or forward it as-is.\n if (options.emitter.listenerCount('unhandledException') > 0) {\n // Create a new request controller just for the unhandled exception case.\n // This is needed because the original controller might have been already\n // interacted with (e.g. \"respondWith\" or \"errorWith\" called on it).\n const unhandledExceptionController = new RequestController(\n options.request\n )\n\n await emitAsync(options.emitter, 'unhandledException', {\n error: result.error,\n request: options.request,\n requestId: options.requestId,\n controller: unhandledExceptionController,\n }).then(() => {\n // If all the \"unhandledException\" listeners have finished\n // but have not handled the response in any way, preemptively\n // resolve the pending response promise from the new controller.\n // This prevents it from hanging forever.\n if (\n unhandledExceptionController[kResponsePromise].state === 'pending'\n ) {\n unhandledExceptionController[kResponsePromise].resolve(undefined)\n }\n })\n\n const nextResult = await until(\n () => unhandledExceptionController[kResponsePromise]\n )\n\n /**\n * @note Handle the result of the unhandled controller\n * in the same way as the original request controller.\n * The exception here is that thrown errors within the\n * \"unhandledException\" event do NOT result in another\n * emit of the same event. They are forwarded as-is.\n */\n if (nextResult.error) {\n return handleResponseError(nextResult.error)\n }\n\n if (nextResult.data) {\n return handleResponse(nextResult.data)\n }\n }\n\n // Otherwise, coerce unhandled exceptions to a 500 Internal Server Error response.\n options.onResponse(createServerErrorResponse(result.error))\n return true\n }\n\n /**\n * Handle a mocked Response instance.\n * @note That this can also be an Error in case\n * the developer called \"errorWith\". This differentiates\n * unhandled exceptions from intended errors.\n */\n if (result.data) {\n return handleResponse(result.data)\n }\n\n // In all other cases, consider the request unhandled.\n return false\n}\n","/**\n * Determines if a given value is an instance of object.\n */\nexport function isObject<T>(value: any, loose = false): value is T {\n return loose\n ? Object.prototype.toString.call(value).startsWith('[object ')\n : Object.prototype.toString.call(value) === '[object Object]'\n}\n","/**\n * A function that validates if property access is possible on an object\n * without throwing. It returns `true` if the property access is possible\n * and `false` otherwise.\n *\n * Environments like miniflare will throw on property access on certain objects\n * like Request and Response, for unimplemented properties.\n */\nexport function isPropertyAccessible<Obj extends Record<string, any>>(\n obj: Obj,\n key: keyof Obj\n) {\n try {\n obj[key]\n return true\n } catch {\n return false\n }\n}\n","import { isObject } from './isObject'\nimport { isPropertyAccessible } from './isPropertyAccessible'\n\n/**\n * Creates a generic 500 Unhandled Exception response.\n */\nexport function createServerErrorResponse(body: unknown): Response {\n return new Response(\n JSON.stringify(\n body instanceof Error\n ? {\n name: body.name,\n message: body.message,\n stack: body.stack,\n }\n : body\n ),\n {\n status: 500,\n statusText: 'Unhandled Exception',\n headers: {\n 'Content-Type': 'application/json',\n },\n }\n )\n}\n\nexport type ResponseError = Response & { type: 'error' }\n\n/**\n * Check if the given response is a `Response.error()`.\n *\n * @note Some environments, like Miniflare (Cloudflare) do not\n * implement the \"Response.type\" property and throw on its access.\n * Safely check if we can access \"type\" on \"Response\" before continuing.\n * @see https://github.com/mswjs/msw/issues/1834\n */\nexport function isResponseError(response: unknown): response is ResponseError {\n return (\n response != null &&\n response instanceof Response &&\n isPropertyAccessible(response, 'type') &&\n response.type === 'error'\n )\n}\n\n/**\n * Check if the given value is a `Response` or a Response-like object.\n * This is different from `value instanceof Response` because it supports\n * custom `Response` constructors, like the one when using Undici directly.\n */\nexport function isResponseLike(value: unknown): value is Response {\n return (\n isObject<Record<string, any>>(value, true) &&\n isPropertyAccessible(value, 'status') &&\n isPropertyAccessible(value, 'statusText') &&\n isPropertyAccessible(value, 'bodyUsed')\n )\n}\n","export function isNodeLikeError(\n error: unknown\n): error is NodeJS.ErrnoException {\n if (error == null) {\n return false\n }\n\n if (!(error instanceof Error)) {\n return false\n }\n\n return 'code' in error && 'errno' in error\n}\n"]}
|
|
@@ -6,7 +6,7 @@ var _chunkLK6DILFKjs = require('./chunk-LK6DILFK.js');
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
var
|
|
9
|
+
var _chunkGTJ35JP4js = require('./chunk-GTJ35JP4.js');
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
|
|
@@ -743,13 +743,13 @@ function createXMLHttpRequestProxy({
|
|
|
743
743
|
logger
|
|
744
744
|
);
|
|
745
745
|
xhrRequestController.onRequest = async function({ request, requestId }) {
|
|
746
|
-
const controller = new (0,
|
|
746
|
+
const controller = new (0, _chunkGTJ35JP4js.RequestController)(request);
|
|
747
747
|
this.logger.info("awaiting mocked response...");
|
|
748
748
|
this.logger.info(
|
|
749
749
|
'emitting the "request" event for %s listener(s)...',
|
|
750
750
|
emitter.listenerCount("request")
|
|
751
751
|
);
|
|
752
|
-
const isRequestHandled = await
|
|
752
|
+
const isRequestHandled = await _chunkGTJ35JP4js.handleRequest.call(void 0, {
|
|
753
753
|
request,
|
|
754
754
|
requestId,
|
|
755
755
|
controller,
|
|
@@ -843,4 +843,4 @@ XMLHttpRequestInterceptor.interceptorSymbol = Symbol("xhr");
|
|
|
843
843
|
|
|
844
844
|
|
|
845
845
|
exports.XMLHttpRequestInterceptor = XMLHttpRequestInterceptor;
|
|
846
|
-
//# sourceMappingURL=chunk-
|
|
846
|
+
//# sourceMappingURL=chunk-HOXEZV62.js.map
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
import {
|
|
7
7
|
RequestController,
|
|
8
8
|
handleRequest
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-L37TY7LC.mjs";
|
|
10
10
|
import {
|
|
11
11
|
FetchResponse,
|
|
12
12
|
IS_PATCHED_MODULE,
|
|
@@ -843,4 +843,4 @@ XMLHttpRequestInterceptor.interceptorSymbol = Symbol("xhr");
|
|
|
843
843
|
export {
|
|
844
844
|
XMLHttpRequestInterceptor
|
|
845
845
|
};
|
|
846
|
-
//# sourceMappingURL=chunk-
|
|
846
|
+
//# sourceMappingURL=chunk-JZOFC66D.mjs.map
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
var
|
|
5
|
+
var _chunkGTJ35JP4js = require('./chunk-GTJ35JP4.js');
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
|
|
@@ -183,14 +183,14 @@ var _FetchInterceptor = class extends _chunkTIPR373Rjs.Interceptor {
|
|
|
183
183
|
Reflect.set(request, _chunkNZ7OXVVLjs.kRawRequest, input);
|
|
184
184
|
}
|
|
185
185
|
const responsePromise = new (0, _deferredpromise.DeferredPromise)();
|
|
186
|
-
const controller = new (0,
|
|
186
|
+
const controller = new (0, _chunkGTJ35JP4js.RequestController)(request);
|
|
187
187
|
this.logger.info("[%s] %s", request.method, request.url);
|
|
188
188
|
this.logger.info("awaiting for the mocked response...");
|
|
189
189
|
this.logger.info(
|
|
190
190
|
'emitting the "request" event for %s listener(s)...',
|
|
191
191
|
this.emitter.listenerCount("request")
|
|
192
192
|
);
|
|
193
|
-
const isRequestHandled = await
|
|
193
|
+
const isRequestHandled = await _chunkGTJ35JP4js.handleRequest.call(void 0, {
|
|
194
194
|
request,
|
|
195
195
|
requestId,
|
|
196
196
|
emitter: this.emitter,
|
|
@@ -221,7 +221,7 @@ var _FetchInterceptor = class extends _chunkTIPR373Rjs.Interceptor {
|
|
|
221
221
|
}
|
|
222
222
|
if (this.emitter.listenerCount("response") > 0) {
|
|
223
223
|
this.logger.info('emitting the "response" event...');
|
|
224
|
-
await
|
|
224
|
+
await _chunkGTJ35JP4js.emitAsync.call(void 0, this.emitter, "response", {
|
|
225
225
|
// Clone the mocked response for the "response" event listener.
|
|
226
226
|
// This way, the listener can read the response and not lock its body
|
|
227
227
|
// for the actual fetch consumer.
|
|
@@ -255,7 +255,7 @@ var _FetchInterceptor = class extends _chunkTIPR373Rjs.Interceptor {
|
|
|
255
255
|
if (this.emitter.listenerCount("response") > 0) {
|
|
256
256
|
this.logger.info('emitting the "response" event...');
|
|
257
257
|
const responseClone = response.clone();
|
|
258
|
-
await
|
|
258
|
+
await _chunkGTJ35JP4js.emitAsync.call(void 0, this.emitter, "response", {
|
|
259
259
|
response: responseClone,
|
|
260
260
|
isMockedResponse: false,
|
|
261
261
|
request: requestCloneForResponseEvent,
|
|
@@ -288,4 +288,4 @@ FetchInterceptor.symbol = Symbol("fetch");
|
|
|
288
288
|
|
|
289
289
|
|
|
290
290
|
exports.FetchInterceptor = FetchInterceptor;
|
|
291
|
-
//# sourceMappingURL=chunk-
|
|
291
|
+
//# sourceMappingURL=chunk-K4XV34DA.js.map
|
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
RequestController,
|
|
3
3
|
emitAsync,
|
|
4
4
|
handleRequest
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-L37TY7LC.mjs";
|
|
6
6
|
import {
|
|
7
7
|
FetchResponse,
|
|
8
8
|
IS_PATCHED_MODULE,
|
|
@@ -288,4 +288,4 @@ FetchInterceptor.symbol = Symbol("fetch");
|
|
|
288
288
|
export {
|
|
289
289
|
FetchInterceptor
|
|
290
290
|
};
|
|
291
|
-
//# sourceMappingURL=chunk-
|
|
291
|
+
//# sourceMappingURL=chunk-KLI5672K.mjs.map
|
|
@@ -39,12 +39,14 @@ var RequestController = class {
|
|
|
39
39
|
this[kResponsePromise].resolve(response);
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
|
-
* Error this request with the given
|
|
42
|
+
* Error this request with the given reason.
|
|
43
|
+
*
|
|
43
44
|
* @example
|
|
44
45
|
* controller.errorWith()
|
|
45
46
|
* controller.errorWith(new Error('Oops!'))
|
|
47
|
+
* controller.errorWith({ message: 'Oops!'})
|
|
46
48
|
*/
|
|
47
|
-
errorWith(
|
|
49
|
+
errorWith(reason) {
|
|
48
50
|
invariant.as(
|
|
49
51
|
InterceptorError,
|
|
50
52
|
!this[kRequestHandled],
|
|
@@ -53,7 +55,7 @@ var RequestController = class {
|
|
|
53
55
|
this.request.url
|
|
54
56
|
);
|
|
55
57
|
this[kRequestHandled] = true;
|
|
56
|
-
this[kResponsePromise].resolve(
|
|
58
|
+
this[kResponsePromise].resolve(reason);
|
|
57
59
|
}
|
|
58
60
|
};
|
|
59
61
|
kResponsePromise, kRequestHandled;
|
|
@@ -73,6 +75,11 @@ async function emitAsync(emitter, eventName, ...data) {
|
|
|
73
75
|
import { DeferredPromise as DeferredPromise2 } from "@open-draft/deferred-promise";
|
|
74
76
|
import { until } from "@open-draft/until";
|
|
75
77
|
|
|
78
|
+
// src/utils/isObject.ts
|
|
79
|
+
function isObject(value, loose = false) {
|
|
80
|
+
return loose ? Object.prototype.toString.call(value).startsWith("[object ") : Object.prototype.toString.call(value) === "[object Object]";
|
|
81
|
+
}
|
|
82
|
+
|
|
76
83
|
// src/utils/isPropertyAccessible.ts
|
|
77
84
|
function isPropertyAccessible(obj, key) {
|
|
78
85
|
try {
|
|
@@ -103,7 +110,10 @@ function createServerErrorResponse(body) {
|
|
|
103
110
|
);
|
|
104
111
|
}
|
|
105
112
|
function isResponseError(response) {
|
|
106
|
-
return isPropertyAccessible(response, "type") && response.type === "error";
|
|
113
|
+
return response != null && response instanceof Response && isPropertyAccessible(response, "type") && response.type === "error";
|
|
114
|
+
}
|
|
115
|
+
function isResponseLike(value) {
|
|
116
|
+
return isObject(value, true) && isPropertyAccessible(value, "status") && isPropertyAccessible(value, "statusText") && isPropertyAccessible(value, "bodyUsed");
|
|
107
117
|
}
|
|
108
118
|
|
|
109
119
|
// src/utils/isNodeLikeError.ts
|
|
@@ -122,12 +132,21 @@ async function handleRequest(options) {
|
|
|
122
132
|
const handleResponse = async (response) => {
|
|
123
133
|
if (response instanceof Error) {
|
|
124
134
|
options.onError(response);
|
|
125
|
-
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
if (isResponseError(response)) {
|
|
126
138
|
options.onRequestError(response);
|
|
127
|
-
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
if (isResponseLike(response)) {
|
|
128
142
|
await options.onResponse(response);
|
|
143
|
+
return true;
|
|
129
144
|
}
|
|
130
|
-
|
|
145
|
+
if (isObject(response)) {
|
|
146
|
+
options.onError(response);
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
return false;
|
|
131
150
|
};
|
|
132
151
|
const handleResponseError = async (error) => {
|
|
133
152
|
if (error instanceof InterceptorError) {
|
|
@@ -165,7 +184,7 @@ async function handleRequest(options) {
|
|
|
165
184
|
}
|
|
166
185
|
}
|
|
167
186
|
const result = await until(async () => {
|
|
168
|
-
const
|
|
187
|
+
const requestListenersPromise = emitAsync(options.emitter, "request", {
|
|
169
188
|
requestId: options.requestId,
|
|
170
189
|
request: options.request,
|
|
171
190
|
controller: options.controller
|
|
@@ -173,11 +192,10 @@ async function handleRequest(options) {
|
|
|
173
192
|
await Promise.race([
|
|
174
193
|
// Short-circuit the request handling promise if the request gets aborted.
|
|
175
194
|
requestAbortPromise,
|
|
176
|
-
|
|
195
|
+
requestListenersPromise,
|
|
177
196
|
options.controller[kResponsePromise]
|
|
178
197
|
]);
|
|
179
|
-
|
|
180
|
-
return mockedResponse;
|
|
198
|
+
return await options.controller[kResponsePromise];
|
|
181
199
|
});
|
|
182
200
|
if (requestAbortPromise.state === "rejected") {
|
|
183
201
|
options.onError(requestAbortPromise.rejectionReason);
|
|
@@ -221,10 +239,8 @@ async function handleRequest(options) {
|
|
|
221
239
|
}
|
|
222
240
|
|
|
223
241
|
export {
|
|
224
|
-
isPropertyAccessible,
|
|
225
|
-
createServerErrorResponse,
|
|
226
242
|
RequestController,
|
|
227
243
|
emitAsync,
|
|
228
244
|
handleRequest
|
|
229
245
|
};
|
|
230
|
-
//# sourceMappingURL=chunk-
|
|
246
|
+
//# sourceMappingURL=chunk-L37TY7LC.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/RequestController.ts","../../src/InterceptorError.ts","../../src/utils/emitAsync.ts","../../src/utils/handleRequest.ts","../../src/utils/isObject.ts","../../src/utils/isPropertyAccessible.ts","../../src/utils/responseUtils.ts","../../src/utils/isNodeLikeError.ts"],"sourcesContent":["import { invariant } from 'outvariant'\nimport { DeferredPromise } from '@open-draft/deferred-promise'\nimport { InterceptorError } from './InterceptorError'\n\nconst kRequestHandled = Symbol('kRequestHandled')\nexport const kResponsePromise = Symbol('kResponsePromise')\n\nexport class RequestController {\n /**\n * Internal response promise.\n * Available only for the library internals to grab the\n * response instance provided by the developer.\n * @note This promise cannot be rejected. It's either infinitely\n * pending or resolved with whichever Response was passed to `respondWith()`.\n */\n [kResponsePromise]: DeferredPromise<\n Response | Record<string, any> | undefined\n >;\n\n /**\n * Internal flag indicating if this request has been handled.\n * @note The response promise becomes \"fulfilled\" on the next tick.\n */\n [kRequestHandled]: boolean\n\n constructor(private request: Request) {\n this[kRequestHandled] = false\n this[kResponsePromise] = new DeferredPromise()\n }\n\n /**\n * Respond to this request with the given `Response` instance.\n * @example\n * controller.respondWith(new Response())\n * controller.respondWith(Response.json({ id }))\n * controller.respondWith(Response.error())\n */\n public respondWith(response: Response): void {\n invariant.as(\n InterceptorError,\n !this[kRequestHandled],\n 'Failed to respond to the \"%s %s\" request: the \"request\" event has already been handled.',\n this.request.method,\n this.request.url\n )\n\n this[kRequestHandled] = true\n this[kResponsePromise].resolve(response)\n\n /**\n * @note The request controller doesn't do anything\n * apart from letting the interceptor await the response\n * provided by the developer through the response promise.\n * Each interceptor implements the actual respondWith/errorWith\n * logic based on that interceptor's needs.\n */\n }\n\n /**\n * Error this request with the given reason.\n *\n * @example\n * controller.errorWith()\n * controller.errorWith(new Error('Oops!'))\n * controller.errorWith({ message: 'Oops!'})\n */\n public errorWith(reason?: Error | Record<string, any>): void {\n invariant.as(\n InterceptorError,\n !this[kRequestHandled],\n 'Failed to error the \"%s %s\" request: the \"request\" event has already been handled.',\n this.request.method,\n this.request.url\n )\n\n this[kRequestHandled] = true\n\n /**\n * @note Resolve the response promise, not reject.\n * This helps us differentiate between unhandled exceptions\n * and intended errors (\"errorWith\") while waiting for the response.\n */\n this[kResponsePromise].resolve(reason)\n }\n}\n","export class InterceptorError extends Error {\n constructor(message?: string) {\n super(message)\n this.name = 'InterceptorError'\n Object.setPrototypeOf(this, InterceptorError.prototype)\n }\n}\n","import { Emitter, EventMap } from 'strict-event-emitter'\n\n/**\n * Emits an event on the given emitter but executes\n * the listeners sequentially. This accounts for asynchronous\n * listeners (e.g. those having \"sleep\" and handling the request).\n */\nexport async function emitAsync<\n Events extends EventMap,\n EventName extends keyof Events\n>(\n emitter: Emitter<Events>,\n eventName: EventName,\n ...data: Events[EventName]\n): Promise<void> {\n const listners = emitter.listeners(eventName)\n\n if (listners.length === 0) {\n return\n }\n\n for (const listener of listners) {\n await listener.apply(emitter, data)\n }\n}\n","import type { Emitter } from 'strict-event-emitter'\nimport { DeferredPromise } from '@open-draft/deferred-promise'\nimport { until } from '@open-draft/until'\nimport type { HttpRequestEventMap } from '../glossary'\nimport { emitAsync } from './emitAsync'\nimport { kResponsePromise, RequestController } from '../RequestController'\nimport {\n createServerErrorResponse,\n isResponseError,\n isResponseLike,\n ResponseError,\n} from './responseUtils'\nimport { InterceptorError } from '../InterceptorError'\nimport { isNodeLikeError } from './isNodeLikeError'\nimport { isObject } from './isObject'\n\ninterface HandleRequestOptions {\n requestId: string\n request: Request\n emitter: Emitter<HttpRequestEventMap>\n controller: RequestController\n\n /**\n * Called when the request has been handled\n * with the given `Response` instance.\n */\n onResponse: (response: Response) => void | Promise<void>\n\n /**\n * Called when the request has been handled\n * with the given `Response.error()` instance.\n */\n onRequestError: (response: ResponseError) => void\n\n /**\n * Called when an unhandled error happens during the\n * request handling. This is never a thrown error/response.\n */\n onError: (error: unknown) => void\n}\n\n/**\n * @returns {Promise<boolean>} Indicates whether the request has been handled.\n */\nexport async function handleRequest(\n options: HandleRequestOptions\n): Promise<boolean> {\n const handleResponse = async (\n response: Response | Error | Record<string, any>\n ) => {\n if (response instanceof Error) {\n options.onError(response)\n return true\n }\n\n // Handle \"Response.error()\" instances.\n if (isResponseError(response)) {\n options.onRequestError(response)\n return true\n }\n\n /**\n * Handle normal responses or response-like objects.\n * @note This must come before the arbitrary object check\n * since Response instances are, in fact, objects.\n */\n if (isResponseLike(response)) {\n await options.onResponse(response)\n return true\n }\n\n // Handle arbitrary objects provided to `.errorWith(reason)`.\n if (isObject(response)) {\n options.onError(response)\n return true\n }\n\n return false\n }\n\n const handleResponseError = async (error: unknown): Promise<boolean> => {\n // Forward the special interceptor error instances\n // to the developer. These must not be handled in any way.\n if (error instanceof InterceptorError) {\n throw result.error\n }\n\n // Support mocking Node.js-like errors.\n if (isNodeLikeError(error)) {\n options.onError(error)\n return true\n }\n\n // Handle thrown responses.\n if (error instanceof Response) {\n return await handleResponse(error)\n }\n\n return false\n }\n\n // Add the last \"request\" listener to check if the request\n // has been handled in any way. If it hasn't, resolve the\n // response promise with undefined.\n options.emitter.once('request', ({ requestId: pendingRequestId }) => {\n if (pendingRequestId !== options.requestId) {\n return\n }\n\n if (options.controller[kResponsePromise].state === 'pending') {\n options.controller[kResponsePromise].resolve(undefined)\n }\n })\n\n const requestAbortPromise = new DeferredPromise<void, unknown>()\n\n /**\n * @note `signal` is not always defined in React Native.\n */\n if (options.request.signal) {\n if (options.request.signal.aborted) {\n requestAbortPromise.reject(options.request.signal.reason)\n } else {\n options.request.signal.addEventListener(\n 'abort',\n () => {\n requestAbortPromise.reject(options.request.signal.reason)\n },\n { once: true }\n )\n }\n }\n\n const result = await until(async () => {\n // Emit the \"request\" event and wait until all the listeners\n // for that event are finished (e.g. async listeners awaited).\n // By the end of this promise, the developer cannot affect the\n // request anymore.\n const requestListenersPromise = emitAsync(options.emitter, 'request', {\n requestId: options.requestId,\n request: options.request,\n controller: options.controller,\n })\n\n await Promise.race([\n // Short-circuit the request handling promise if the request gets aborted.\n requestAbortPromise,\n requestListenersPromise,\n options.controller[kResponsePromise],\n ])\n\n // The response promise will settle immediately once\n // the developer calls either \"respondWith\" or \"errorWith\".\n return await options.controller[kResponsePromise]\n })\n\n // Handle the request being aborted while waiting for the request listeners.\n if (requestAbortPromise.state === 'rejected') {\n options.onError(requestAbortPromise.rejectionReason)\n return true\n }\n\n if (result.error) {\n // Handle the error during the request listener execution.\n // These can be thrown responses or request errors.\n if (await handleResponseError(result.error)) {\n return true\n }\n\n // If the developer has added \"unhandledException\" listeners,\n // allow them to handle the error. They can translate it to a\n // mocked response, network error, or forward it as-is.\n if (options.emitter.listenerCount('unhandledException') > 0) {\n // Create a new request controller just for the unhandled exception case.\n // This is needed because the original controller might have been already\n // interacted with (e.g. \"respondWith\" or \"errorWith\" called on it).\n const unhandledExceptionController = new RequestController(\n options.request\n )\n\n await emitAsync(options.emitter, 'unhandledException', {\n error: result.error,\n request: options.request,\n requestId: options.requestId,\n controller: unhandledExceptionController,\n }).then(() => {\n // If all the \"unhandledException\" listeners have finished\n // but have not handled the response in any way, preemptively\n // resolve the pending response promise from the new controller.\n // This prevents it from hanging forever.\n if (\n unhandledExceptionController[kResponsePromise].state === 'pending'\n ) {\n unhandledExceptionController[kResponsePromise].resolve(undefined)\n }\n })\n\n const nextResult = await until(\n () => unhandledExceptionController[kResponsePromise]\n )\n\n /**\n * @note Handle the result of the unhandled controller\n * in the same way as the original request controller.\n * The exception here is that thrown errors within the\n * \"unhandledException\" event do NOT result in another\n * emit of the same event. They are forwarded as-is.\n */\n if (nextResult.error) {\n return handleResponseError(nextResult.error)\n }\n\n if (nextResult.data) {\n return handleResponse(nextResult.data)\n }\n }\n\n // Otherwise, coerce unhandled exceptions to a 500 Internal Server Error response.\n options.onResponse(createServerErrorResponse(result.error))\n return true\n }\n\n /**\n * Handle a mocked Response instance.\n * @note That this can also be an Error in case\n * the developer called \"errorWith\". This differentiates\n * unhandled exceptions from intended errors.\n */\n if (result.data) {\n return handleResponse(result.data)\n }\n\n // In all other cases, consider the request unhandled.\n return false\n}\n","/**\n * Determines if a given value is an instance of object.\n */\nexport function isObject<T>(value: any, loose = false): value is T {\n return loose\n ? Object.prototype.toString.call(value).startsWith('[object ')\n : Object.prototype.toString.call(value) === '[object Object]'\n}\n","/**\n * A function that validates if property access is possible on an object\n * without throwing. It returns `true` if the property access is possible\n * and `false` otherwise.\n *\n * Environments like miniflare will throw on property access on certain objects\n * like Request and Response, for unimplemented properties.\n */\nexport function isPropertyAccessible<Obj extends Record<string, any>>(\n obj: Obj,\n key: keyof Obj\n) {\n try {\n obj[key]\n return true\n } catch {\n return false\n }\n}\n","import { isObject } from './isObject'\nimport { isPropertyAccessible } from './isPropertyAccessible'\n\n/**\n * Creates a generic 500 Unhandled Exception response.\n */\nexport function createServerErrorResponse(body: unknown): Response {\n return new Response(\n JSON.stringify(\n body instanceof Error\n ? {\n name: body.name,\n message: body.message,\n stack: body.stack,\n }\n : body\n ),\n {\n status: 500,\n statusText: 'Unhandled Exception',\n headers: {\n 'Content-Type': 'application/json',\n },\n }\n )\n}\n\nexport type ResponseError = Response & { type: 'error' }\n\n/**\n * Check if the given response is a `Response.error()`.\n *\n * @note Some environments, like Miniflare (Cloudflare) do not\n * implement the \"Response.type\" property and throw on its access.\n * Safely check if we can access \"type\" on \"Response\" before continuing.\n * @see https://github.com/mswjs/msw/issues/1834\n */\nexport function isResponseError(response: unknown): response is ResponseError {\n return (\n response != null &&\n response instanceof Response &&\n isPropertyAccessible(response, 'type') &&\n response.type === 'error'\n )\n}\n\n/**\n * Check if the given value is a `Response` or a Response-like object.\n * This is different from `value instanceof Response` because it supports\n * custom `Response` constructors, like the one when using Undici directly.\n */\nexport function isResponseLike(value: unknown): value is Response {\n return (\n isObject<Record<string, any>>(value, true) &&\n isPropertyAccessible(value, 'status') &&\n isPropertyAccessible(value, 'statusText') &&\n isPropertyAccessible(value, 'bodyUsed')\n )\n}\n","export function isNodeLikeError(\n error: unknown\n): error is NodeJS.ErrnoException {\n if (error == null) {\n return false\n }\n\n if (!(error instanceof Error)) {\n return false\n }\n\n return 'code' in error && 'errno' in error\n}\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;;;ACDzB,IAAM,mBAAN,cAA+B,MAAM;AAAA,EAC1C,YAAY,SAAkB;AAC5B,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,WAAO,eAAe,MAAM,iBAAiB,SAAS;AAAA,EACxD;AACF;;;ADFA,IAAM,kBAAkB,OAAO,iBAAiB;AACzC,IAAM,mBAAmB,OAAO,kBAAkB;AAElD,IAAM,oBAAN,MAAwB;AAAA,EAkB7B,YAAoB,SAAkB;AAAlB;AAClB,SAAK,eAAe,IAAI;AACxB,SAAK,gBAAgB,IAAI,IAAI,gBAAgB;AAAA,EAC/C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASO,YAAY,UAA0B;AAC3C,cAAU;AAAA,MACR;AAAA,MACA,CAAC,KAAK,eAAe;AAAA,MACrB;AAAA,MACA,KAAK,QAAQ;AAAA,MACb,KAAK,QAAQ;AAAA,IACf;AAEA,SAAK,eAAe,IAAI;AACxB,SAAK,gBAAgB,EAAE,QAAQ,QAAQ;AAAA,EASzC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUO,UAAU,QAA4C;AAC3D,cAAU;AAAA,MACR;AAAA,MACA,CAAC,KAAK,eAAe;AAAA,MACrB;AAAA,MACA,KAAK,QAAQ;AAAA,MACb,KAAK,QAAQ;AAAA,IACf;AAEA,SAAK,eAAe,IAAI;AAOxB,SAAK,gB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AAI;AAAA,MACvC;AAAA,IACF;AAGA,YAAQ,WAAW,0BAA0B,OAAO,KAAK,CAAC;AAC1D,WAAO;AAAA,EACT;AAQA,MAAI,OAAO,MAAM;AACf,WAAO,eAAe,OAAO,IAAI;AAAA,EACnC;AAGA,SAAO;AACT;","names":["DeferredPromise","DeferredPromise"]}
|
|
@@ -11,7 +11,7 @@ declare class RequestController {
|
|
|
11
11
|
* @note This promise cannot be rejected. It's either infinitely
|
|
12
12
|
* pending or resolved with whichever Response was passed to `respondWith()`.
|
|
13
13
|
*/
|
|
14
|
-
[kResponsePromise]: DeferredPromise<Response |
|
|
14
|
+
[kResponsePromise]: DeferredPromise<Response | Record<string, any> | undefined>;
|
|
15
15
|
/**
|
|
16
16
|
* Internal flag indicating if this request has been handled.
|
|
17
17
|
* @note The response promise becomes "fulfilled" on the next tick.
|
|
@@ -27,12 +27,14 @@ declare class RequestController {
|
|
|
27
27
|
*/
|
|
28
28
|
respondWith(response: Response): void;
|
|
29
29
|
/**
|
|
30
|
-
* Error this request with the given
|
|
30
|
+
* Error this request with the given reason.
|
|
31
|
+
*
|
|
31
32
|
* @example
|
|
32
33
|
* controller.errorWith()
|
|
33
34
|
* controller.errorWith(new Error('Oops!'))
|
|
35
|
+
* controller.errorWith({ message: 'Oops!'})
|
|
34
36
|
*/
|
|
35
|
-
errorWith(
|
|
37
|
+
errorWith(reason?: Error | Record<string, any>): void;
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
declare const IS_PATCHED_MODULE: unique symbol;
|
package/lib/browser/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { H as HttpRequestEventMap, I as IS_PATCHED_MODULE, R as RequestController, a as RequestCredentials } from './glossary-
|
|
1
|
+
export { H as HttpRequestEventMap, I as IS_PATCHED_MODULE, R as RequestController, a as RequestCredentials } from './glossary-7152281e.js';
|
|
2
2
|
import { I as Interceptor, E as ExtractEventNames } from './Interceptor-af98b768.js';
|
|
3
3
|
export { c as INTERNAL_REQUEST_ID_HEADER_NAME, a as InterceptorEventMap, e as InterceptorReadyState, b as InterceptorSubscription, d as deleteGlobalSymbol, g as getGlobalSymbol } from './Interceptor-af98b768.js';
|
|
4
4
|
import { EventMap, Listener } from 'strict-event-emitter';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Emitter } from 'strict-event-emitter';
|
|
2
|
-
import { H as HttpRequestEventMap } from '../../glossary-
|
|
2
|
+
import { H as HttpRequestEventMap } from '../../glossary-7152281e.js';
|
|
3
3
|
import { I as Interceptor } from '../../Interceptor-af98b768.js';
|
|
4
4
|
import '@open-draft/deferred-promise';
|
|
5
5
|
import '@open-draft/logger';
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunkHOXEZV62js = require('../../chunk-HOXEZV62.js');
|
|
4
4
|
require('../../chunk-LK6DILFK.js');
|
|
5
|
-
require('../../chunk-
|
|
5
|
+
require('../../chunk-GTJ35JP4.js');
|
|
6
6
|
require('../../chunk-NZ7OXVVL.js');
|
|
7
7
|
require('../../chunk-PFGO5BSM.js');
|
|
8
8
|
require('../../chunk-TIPR373R.js');
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
exports.XMLHttpRequestInterceptor =
|
|
11
|
+
exports.XMLHttpRequestInterceptor = _chunkHOXEZV62js.XMLHttpRequestInterceptor;
|
|
12
12
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
XMLHttpRequestInterceptor
|
|
3
|
-
} from "../../chunk-
|
|
3
|
+
} from "../../chunk-JZOFC66D.mjs";
|
|
4
4
|
import "../../chunk-6HYIRFX2.mjs";
|
|
5
|
-
import "../../chunk-
|
|
5
|
+
import "../../chunk-L37TY7LC.mjs";
|
|
6
6
|
import "../../chunk-XDWMU6ZF.mjs";
|
|
7
7
|
import "../../chunk-TX5GBTFY.mjs";
|
|
8
8
|
import "../../chunk-QED3Q6Z2.mjs";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { H as HttpRequestEventMap } from '../../glossary-
|
|
1
|
+
import { H as HttpRequestEventMap } from '../../glossary-7152281e.js';
|
|
2
2
|
import { I as Interceptor } from '../../Interceptor-af98b768.js';
|
|
3
3
|
import '@open-draft/deferred-promise';
|
|
4
4
|
import '@open-draft/logger';
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
require('../../chunk-
|
|
3
|
+
var _chunkK4XV34DAjs = require('../../chunk-K4XV34DA.js');
|
|
4
|
+
require('../../chunk-GTJ35JP4.js');
|
|
5
5
|
require('../../chunk-NZ7OXVVL.js');
|
|
6
6
|
require('../../chunk-PFGO5BSM.js');
|
|
7
7
|
require('../../chunk-TIPR373R.js');
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
exports.FetchInterceptor =
|
|
10
|
+
exports.FetchInterceptor = _chunkK4XV34DAjs.FetchInterceptor;
|
|
11
11
|
//# sourceMappingURL=index.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
FetchInterceptor
|
|
3
|
-
} from "../../chunk-
|
|
4
|
-
import "../../chunk-
|
|
3
|
+
} from "../../chunk-KLI5672K.mjs";
|
|
4
|
+
import "../../chunk-L37TY7LC.mjs";
|
|
5
5
|
import "../../chunk-XDWMU6ZF.mjs";
|
|
6
6
|
import "../../chunk-TX5GBTFY.mjs";
|
|
7
7
|
import "../../chunk-QED3Q6Z2.mjs";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { FetchInterceptor } from '../interceptors/fetch/index.js';
|
|
2
2
|
import { XMLHttpRequestInterceptor } from '../interceptors/XMLHttpRequest/index.js';
|
|
3
|
-
import '../glossary-
|
|
3
|
+
import '../glossary-7152281e.js';
|
|
4
4
|
import '@open-draft/deferred-promise';
|
|
5
5
|
import '../Interceptor-af98b768.js';
|
|
6
6
|
import '@open-draft/logger';
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunkHOXEZV62js = require('../chunk-HOXEZV62.js');
|
|
4
4
|
require('../chunk-LK6DILFK.js');
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
var
|
|
8
|
-
require('../chunk-
|
|
7
|
+
var _chunkK4XV34DAjs = require('../chunk-K4XV34DA.js');
|
|
8
|
+
require('../chunk-GTJ35JP4.js');
|
|
9
9
|
require('../chunk-NZ7OXVVL.js');
|
|
10
10
|
require('../chunk-PFGO5BSM.js');
|
|
11
11
|
require('../chunk-TIPR373R.js');
|
|
12
12
|
|
|
13
13
|
// src/presets/browser.ts
|
|
14
14
|
var browser_default = [
|
|
15
|
-
new (0,
|
|
16
|
-
new (0,
|
|
15
|
+
new (0, _chunkK4XV34DAjs.FetchInterceptor)(),
|
|
16
|
+
new (0, _chunkHOXEZV62js.XMLHttpRequestInterceptor)()
|
|
17
17
|
];
|
|
18
18
|
|
|
19
19
|
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
XMLHttpRequestInterceptor
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-JZOFC66D.mjs";
|
|
4
4
|
import "../chunk-6HYIRFX2.mjs";
|
|
5
5
|
import {
|
|
6
6
|
FetchInterceptor
|
|
7
|
-
} from "../chunk-
|
|
8
|
-
import "../chunk-
|
|
7
|
+
} from "../chunk-KLI5672K.mjs";
|
|
8
|
+
import "../chunk-L37TY7LC.mjs";
|
|
9
9
|
import "../chunk-XDWMU6ZF.mjs";
|
|
10
10
|
import "../chunk-TX5GBTFY.mjs";
|
|
11
11
|
import "../chunk-QED3Q6Z2.mjs";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { EventMap, Listener } from 'strict-event-emitter';
|
|
2
|
-
import { h as Interceptor, E as ExtractEventNames } from './Interceptor-
|
|
2
|
+
import { h as Interceptor, E as ExtractEventNames } from './Interceptor-bc5a9d8e.js';
|
|
3
3
|
|
|
4
4
|
interface BatchInterceptorOptions<InterceptorList extends ReadonlyArray<Interceptor<any>>> {
|
|
5
5
|
name: string;
|
|
@@ -13,7 +13,7 @@ declare class RequestController {
|
|
|
13
13
|
* @note This promise cannot be rejected. It's either infinitely
|
|
14
14
|
* pending or resolved with whichever Response was passed to `respondWith()`.
|
|
15
15
|
*/
|
|
16
|
-
[kResponsePromise]: DeferredPromise<Response |
|
|
16
|
+
[kResponsePromise]: DeferredPromise<Response | Record<string, any> | undefined>;
|
|
17
17
|
/**
|
|
18
18
|
* Internal flag indicating if this request has been handled.
|
|
19
19
|
* @note The response promise becomes "fulfilled" on the next tick.
|
|
@@ -29,12 +29,14 @@ declare class RequestController {
|
|
|
29
29
|
*/
|
|
30
30
|
respondWith(response: Response): void;
|
|
31
31
|
/**
|
|
32
|
-
* Error this request with the given
|
|
32
|
+
* Error this request with the given reason.
|
|
33
|
+
*
|
|
33
34
|
* @example
|
|
34
35
|
* controller.errorWith()
|
|
35
36
|
* controller.errorWith(new Error('Oops!'))
|
|
37
|
+
* controller.errorWith({ message: 'Oops!'})
|
|
36
38
|
*/
|
|
37
|
-
errorWith(
|
|
39
|
+
errorWith(reason?: Error | Record<string, any>): void;
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
declare const IS_PATCHED_MODULE: unique symbol;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ChildProcess } from 'child_process';
|
|
2
|
-
import { h as Interceptor, H as HttpRequestEventMap } from './Interceptor-
|
|
3
|
-
import { a as BatchInterceptor } from './BatchInterceptor-
|
|
2
|
+
import { h as Interceptor, H as HttpRequestEventMap } from './Interceptor-bc5a9d8e.js';
|
|
3
|
+
import { a as BatchInterceptor } from './BatchInterceptor-5b72232f.js';
|
|
4
4
|
import { ClientRequestInterceptor } from './interceptors/ClientRequest/index.js';
|
|
5
5
|
import { XMLHttpRequestInterceptor } from './interceptors/XMLHttpRequest/index.js';
|
|
6
6
|
import { FetchInterceptor } from './interceptors/fetch/index.js';
|