msw 1.0.1 → 1.1.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/cli/init.js +0 -0
- package/config/scripts/postinstall.js +0 -0
- package/lib/{SetupServerApi-39df862c.d.ts → SetupServerApi-1855d9c6.d.ts} +1 -1
- package/lib/iife/index.js +1510 -1442
- package/lib/iife/index.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +547 -345
- package/lib/index.js.map +1 -1
- package/lib/mockServiceWorker.js +1 -1
- package/lib/native/index.d.ts +1 -1
- package/lib/native/index.js +329 -248
- package/lib/native/index.mjs +328 -248
- package/lib/node/index.d.ts +3 -4
- package/lib/node/index.js +334 -249
- package/lib/node/index.js.map +1 -1
- package/lib/node/index.mjs +333 -249
- package/lib/node/index.mjs.map +1 -1
- package/package.json +30 -27
- package/CHANGELOG.md +0 -4
package/lib/index.js
CHANGED
|
@@ -1,38 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
|
-
var __defProps = Object.defineProperties;
|
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
9
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
-
var __spreadValues = (a, b) => {
|
|
13
|
-
for (var prop in b || (b = {}))
|
|
14
|
-
if (__hasOwnProp.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
if (__getOwnPropSymbols)
|
|
17
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
-
if (__propIsEnum.call(b, prop))
|
|
19
|
-
__defNormalProp(a, prop, b[prop]);
|
|
20
|
-
}
|
|
21
|
-
return a;
|
|
22
|
-
};
|
|
23
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
-
var __objRest = (source, exclude) => {
|
|
25
|
-
var target = {};
|
|
26
|
-
for (var prop in source)
|
|
27
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
28
|
-
target[prop] = source[prop];
|
|
29
|
-
if (source != null && __getOwnPropSymbols)
|
|
30
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
|
31
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
32
|
-
target[prop] = source[prop];
|
|
33
|
-
}
|
|
34
|
-
return target;
|
|
35
|
-
};
|
|
36
8
|
var __export = (target, all) => {
|
|
37
9
|
for (var name in all)
|
|
38
10
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -45,7 +17,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
45
17
|
}
|
|
46
18
|
return to;
|
|
47
19
|
};
|
|
48
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
49
24
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
50
25
|
|
|
51
26
|
// src/index.ts
|
|
@@ -92,134 +67,71 @@ __export(context_exports, {
|
|
|
92
67
|
xml: () => xml
|
|
93
68
|
});
|
|
94
69
|
|
|
95
|
-
// node_modules/statuses/codes.json
|
|
96
|
-
var _00 = "Continue";
|
|
97
|
-
var _01 = "Switching Protocols";
|
|
98
|
-
var _02 = "Processing";
|
|
99
|
-
var _03 = "Early Hints";
|
|
100
|
-
var _002 = "OK";
|
|
101
|
-
var _012 = "Created";
|
|
102
|
-
var _022 = "Accepted";
|
|
103
|
-
var _032 = "Non-Authoritative Information";
|
|
104
|
-
var _04 = "No Content";
|
|
105
|
-
var _05 = "Reset Content";
|
|
106
|
-
var _06 = "Partial Content";
|
|
107
|
-
var _07 = "Multi-Status";
|
|
108
|
-
var _08 = "Already Reported";
|
|
109
|
-
var _26 = "IM Used";
|
|
110
|
-
var _003 = "Multiple Choices";
|
|
111
|
-
var _013 = "Moved Permanently";
|
|
112
|
-
var _023 = "Found";
|
|
113
|
-
var _033 = "See Other";
|
|
114
|
-
var _042 = "Not Modified";
|
|
115
|
-
var _052 = "Use Proxy";
|
|
116
|
-
var _072 = "Temporary Redirect";
|
|
117
|
-
var _082 = "Permanent Redirect";
|
|
118
|
-
var _004 = "Bad Request";
|
|
119
|
-
var _014 = "Unauthorized";
|
|
120
|
-
var _024 = "Payment Required";
|
|
121
|
-
var _034 = "Forbidden";
|
|
122
|
-
var _043 = "Not Found";
|
|
123
|
-
var _053 = "Method Not Allowed";
|
|
124
|
-
var _062 = "Not Acceptable";
|
|
125
|
-
var _073 = "Proxy Authentication Required";
|
|
126
|
-
var _083 = "Request Timeout";
|
|
127
|
-
var _09 = "Conflict";
|
|
128
|
-
var _10 = "Gone";
|
|
129
|
-
var _11 = "Length Required";
|
|
130
|
-
var _12 = "Precondition Failed";
|
|
131
|
-
var _13 = "Payload Too Large";
|
|
132
|
-
var _14 = "URI Too Long";
|
|
133
|
-
var _15 = "Unsupported Media Type";
|
|
134
|
-
var _16 = "Range Not Satisfiable";
|
|
135
|
-
var _17 = "Expectation Failed";
|
|
136
|
-
var _18 = "I'm a Teapot";
|
|
137
|
-
var _21 = "Misdirected Request";
|
|
138
|
-
var _22 = "Unprocessable Entity";
|
|
139
|
-
var _23 = "Locked";
|
|
140
|
-
var _24 = "Failed Dependency";
|
|
141
|
-
var _25 = "Too Early";
|
|
142
|
-
var _262 = "Upgrade Required";
|
|
143
|
-
var _28 = "Precondition Required";
|
|
144
|
-
var _29 = "Too Many Requests";
|
|
145
|
-
var _31 = "Request Header Fields Too Large";
|
|
146
|
-
var _51 = "Unavailable For Legal Reasons";
|
|
147
|
-
var _005 = "Internal Server Error";
|
|
148
|
-
var _015 = "Not Implemented";
|
|
149
|
-
var _025 = "Bad Gateway";
|
|
150
|
-
var _035 = "Service Unavailable";
|
|
151
|
-
var _044 = "Gateway Timeout";
|
|
152
|
-
var _054 = "HTTP Version Not Supported";
|
|
153
|
-
var _063 = "Variant Also Negotiates";
|
|
154
|
-
var _074 = "Insufficient Storage";
|
|
155
|
-
var _084 = "Loop Detected";
|
|
156
|
-
var _092 = "Bandwidth Limit Exceeded";
|
|
157
|
-
var _102 = "Not Extended";
|
|
158
|
-
var _112 = "Network Authentication Required";
|
|
70
|
+
// node_modules/.pnpm/statuses@2.0.1/node_modules/statuses/codes.json
|
|
159
71
|
var codes_default = {
|
|
160
|
-
"100":
|
|
161
|
-
"101":
|
|
162
|
-
"102":
|
|
163
|
-
"103":
|
|
164
|
-
"200":
|
|
165
|
-
"201":
|
|
166
|
-
"202":
|
|
167
|
-
"203":
|
|
168
|
-
"204":
|
|
169
|
-
"205":
|
|
170
|
-
"206":
|
|
171
|
-
"207":
|
|
172
|
-
"208":
|
|
173
|
-
"226":
|
|
174
|
-
"300":
|
|
175
|
-
"301":
|
|
176
|
-
"302":
|
|
177
|
-
"303":
|
|
178
|
-
"304":
|
|
179
|
-
"305":
|
|
180
|
-
"307":
|
|
181
|
-
"308":
|
|
182
|
-
"400":
|
|
183
|
-
"401":
|
|
184
|
-
"402":
|
|
185
|
-
"403":
|
|
186
|
-
"404":
|
|
187
|
-
"405":
|
|
188
|
-
"406":
|
|
189
|
-
"407":
|
|
190
|
-
"408":
|
|
191
|
-
"409":
|
|
192
|
-
"410":
|
|
193
|
-
"411":
|
|
194
|
-
"412":
|
|
195
|
-
"413":
|
|
196
|
-
"414":
|
|
197
|
-
"415":
|
|
198
|
-
"416":
|
|
199
|
-
"417":
|
|
200
|
-
"418":
|
|
201
|
-
"421":
|
|
202
|
-
"422":
|
|
203
|
-
"423":
|
|
204
|
-
"424":
|
|
205
|
-
"425":
|
|
206
|
-
"426":
|
|
207
|
-
"428":
|
|
208
|
-
"429":
|
|
209
|
-
"431":
|
|
210
|
-
"451":
|
|
211
|
-
"500":
|
|
212
|
-
"501":
|
|
213
|
-
"502":
|
|
214
|
-
"503":
|
|
215
|
-
"504":
|
|
216
|
-
"505":
|
|
217
|
-
"506":
|
|
218
|
-
"507":
|
|
219
|
-
"508":
|
|
220
|
-
"509":
|
|
221
|
-
"510":
|
|
222
|
-
"511":
|
|
72
|
+
"100": "Continue",
|
|
73
|
+
"101": "Switching Protocols",
|
|
74
|
+
"102": "Processing",
|
|
75
|
+
"103": "Early Hints",
|
|
76
|
+
"200": "OK",
|
|
77
|
+
"201": "Created",
|
|
78
|
+
"202": "Accepted",
|
|
79
|
+
"203": "Non-Authoritative Information",
|
|
80
|
+
"204": "No Content",
|
|
81
|
+
"205": "Reset Content",
|
|
82
|
+
"206": "Partial Content",
|
|
83
|
+
"207": "Multi-Status",
|
|
84
|
+
"208": "Already Reported",
|
|
85
|
+
"226": "IM Used",
|
|
86
|
+
"300": "Multiple Choices",
|
|
87
|
+
"301": "Moved Permanently",
|
|
88
|
+
"302": "Found",
|
|
89
|
+
"303": "See Other",
|
|
90
|
+
"304": "Not Modified",
|
|
91
|
+
"305": "Use Proxy",
|
|
92
|
+
"307": "Temporary Redirect",
|
|
93
|
+
"308": "Permanent Redirect",
|
|
94
|
+
"400": "Bad Request",
|
|
95
|
+
"401": "Unauthorized",
|
|
96
|
+
"402": "Payment Required",
|
|
97
|
+
"403": "Forbidden",
|
|
98
|
+
"404": "Not Found",
|
|
99
|
+
"405": "Method Not Allowed",
|
|
100
|
+
"406": "Not Acceptable",
|
|
101
|
+
"407": "Proxy Authentication Required",
|
|
102
|
+
"408": "Request Timeout",
|
|
103
|
+
"409": "Conflict",
|
|
104
|
+
"410": "Gone",
|
|
105
|
+
"411": "Length Required",
|
|
106
|
+
"412": "Precondition Failed",
|
|
107
|
+
"413": "Payload Too Large",
|
|
108
|
+
"414": "URI Too Long",
|
|
109
|
+
"415": "Unsupported Media Type",
|
|
110
|
+
"416": "Range Not Satisfiable",
|
|
111
|
+
"417": "Expectation Failed",
|
|
112
|
+
"418": "I'm a Teapot",
|
|
113
|
+
"421": "Misdirected Request",
|
|
114
|
+
"422": "Unprocessable Entity",
|
|
115
|
+
"423": "Locked",
|
|
116
|
+
"424": "Failed Dependency",
|
|
117
|
+
"425": "Too Early",
|
|
118
|
+
"426": "Upgrade Required",
|
|
119
|
+
"428": "Precondition Required",
|
|
120
|
+
"429": "Too Many Requests",
|
|
121
|
+
"431": "Request Header Fields Too Large",
|
|
122
|
+
"451": "Unavailable For Legal Reasons",
|
|
123
|
+
"500": "Internal Server Error",
|
|
124
|
+
"501": "Not Implemented",
|
|
125
|
+
"502": "Bad Gateway",
|
|
126
|
+
"503": "Service Unavailable",
|
|
127
|
+
"504": "Gateway Timeout",
|
|
128
|
+
"505": "HTTP Version Not Supported",
|
|
129
|
+
"506": "Variant Also Negotiates",
|
|
130
|
+
"507": "Insufficient Storage",
|
|
131
|
+
"508": "Loop Detected",
|
|
132
|
+
"509": "Bandwidth Limit Exceeded",
|
|
133
|
+
"510": "Not Extended",
|
|
134
|
+
"511": "Network Authentication Required"
|
|
223
135
|
};
|
|
224
136
|
|
|
225
137
|
// src/context/status.ts
|
|
@@ -337,7 +249,9 @@ var getRandomServerResponseTime = () => {
|
|
|
337
249
|
if ((0, import_is_node_process.isNodeProcess)()) {
|
|
338
250
|
return NODE_SERVER_RESPONSE_TIME;
|
|
339
251
|
}
|
|
340
|
-
return Math.floor(
|
|
252
|
+
return Math.floor(
|
|
253
|
+
Math.random() * (MAX_SERVER_RESPONSE_TIME - MIN_SERVER_RESPONSE_TIME) + MIN_SERVER_RESPONSE_TIME
|
|
254
|
+
);
|
|
341
255
|
};
|
|
342
256
|
var delay = (durationOrMode) => {
|
|
343
257
|
return (res) => {
|
|
@@ -353,14 +267,18 @@ var delay = (durationOrMode) => {
|
|
|
353
267
|
break;
|
|
354
268
|
}
|
|
355
269
|
default: {
|
|
356
|
-
throw new Error(
|
|
270
|
+
throw new Error(
|
|
271
|
+
`Failed to delay a response: unknown delay mode "${durationOrMode}". Please make sure you provide one of the supported modes ("real", "infinite") or a number to "ctx.delay".`
|
|
272
|
+
);
|
|
357
273
|
}
|
|
358
274
|
}
|
|
359
275
|
} else if (typeof durationOrMode === "undefined") {
|
|
360
276
|
delayTime = getRandomServerResponseTime();
|
|
361
277
|
} else {
|
|
362
278
|
if (durationOrMode > SET_TIMEOUT_MAX_ALLOWED_INT) {
|
|
363
|
-
throw new Error(
|
|
279
|
+
throw new Error(
|
|
280
|
+
`Failed to delay a response: provided delay duration (${durationOrMode}) exceeds the maximum allowed duration for "setTimeout" (${SET_TIMEOUT_MAX_ALLOWED_INT}). This will cause the response to be returned immediately. Please use a number within the allowed range to delay the response by exact duration, or consider the "infinite" delay mode to delay the response indefinitely.`
|
|
281
|
+
);
|
|
364
282
|
}
|
|
365
283
|
delayTime = durationOrMode;
|
|
366
284
|
}
|
|
@@ -384,19 +302,23 @@ var errors = (errorsList) => {
|
|
|
384
302
|
// src/context/fetch.ts
|
|
385
303
|
var import_is_node_process2 = require("is-node-process");
|
|
386
304
|
var import_headers_polyfill2 = require("headers-polyfill");
|
|
387
|
-
var useFetch = (0, import_is_node_process2.isNodeProcess)() ? (input, init) => Promise.resolve().then(() => __toESM(require("node-fetch"))).then(
|
|
305
|
+
var useFetch = (0, import_is_node_process2.isNodeProcess)() ? (input, init) => Promise.resolve().then(() => __toESM(require("node-fetch"))).then(
|
|
306
|
+
({ default: nodeFetch }) => nodeFetch(input, init)
|
|
307
|
+
) : globalThis.fetch;
|
|
388
308
|
var augmentRequestInit = (requestInit) => {
|
|
389
309
|
const headers = new import_headers_polyfill2.Headers(requestInit.headers);
|
|
390
310
|
headers.set("x-msw-bypass", "true");
|
|
391
|
-
return
|
|
311
|
+
return {
|
|
312
|
+
...requestInit,
|
|
392
313
|
headers: headers.all()
|
|
393
|
-
}
|
|
314
|
+
};
|
|
394
315
|
};
|
|
395
316
|
var createFetchRequestParameters = (input) => {
|
|
396
317
|
const { body: body2, method } = input;
|
|
397
|
-
const requestParameters =
|
|
318
|
+
const requestParameters = {
|
|
319
|
+
...input,
|
|
398
320
|
body: void 0
|
|
399
|
-
}
|
|
321
|
+
};
|
|
400
322
|
if (["GET", "HEAD"].includes(method)) {
|
|
401
323
|
return requestParameters;
|
|
402
324
|
}
|
|
@@ -458,7 +380,12 @@ var devUtils = {
|
|
|
458
380
|
|
|
459
381
|
// src/utils/internal/checkGlobals.ts
|
|
460
382
|
function checkGlobals() {
|
|
461
|
-
(0, import_outvariant2.invariant)(
|
|
383
|
+
(0, import_outvariant2.invariant)(
|
|
384
|
+
typeof URL !== "undefined",
|
|
385
|
+
devUtils.formatMessage(
|
|
386
|
+
`Global "URL" class is not defined. This likely means that you're running MSW in an environment that doesn't support all Node.js standard API (e.g. React Native). If that's the case, please use an appropriate polyfill for the "URL" class, like "react-native-url-polyfill".`
|
|
387
|
+
)
|
|
388
|
+
);
|
|
462
389
|
}
|
|
463
390
|
|
|
464
391
|
// src/setupWorker/setupWorker.ts
|
|
@@ -472,18 +399,20 @@ var import_until4 = require("@open-draft/until");
|
|
|
472
399
|
var import_until = require("@open-draft/until");
|
|
473
400
|
|
|
474
401
|
// src/setupWorker/start/utils/getWorkerByRegistration.ts
|
|
475
|
-
|
|
402
|
+
function getWorkerByRegistration(registration, absoluteWorkerUrl, findWorker) {
|
|
476
403
|
const allStates = [
|
|
477
404
|
registration.active,
|
|
478
405
|
registration.installing,
|
|
479
406
|
registration.waiting
|
|
480
407
|
];
|
|
481
|
-
const
|
|
482
|
-
|
|
483
|
-
return findWorker(worker.scriptURL, absoluteWorkerUrl);
|
|
408
|
+
const relevantStates = allStates.filter((state) => {
|
|
409
|
+
return state != null;
|
|
484
410
|
});
|
|
485
|
-
|
|
486
|
-
|
|
411
|
+
const worker = relevantStates.find((worker2) => {
|
|
412
|
+
return findWorker(worker2.scriptURL, absoluteWorkerUrl);
|
|
413
|
+
});
|
|
414
|
+
return worker || null;
|
|
415
|
+
}
|
|
487
416
|
|
|
488
417
|
// src/utils/url/getAbsoluteWorkerUrl.ts
|
|
489
418
|
function getAbsoluteWorkerUrl(workerUrl) {
|
|
@@ -493,7 +422,11 @@ function getAbsoluteWorkerUrl(workerUrl) {
|
|
|
493
422
|
// src/setupWorker/start/utils/getWorkerInstance.ts
|
|
494
423
|
var getWorkerInstance = async (url, options = {}, findWorker) => {
|
|
495
424
|
const absoluteWorkerUrl = getAbsoluteWorkerUrl(url);
|
|
496
|
-
const mockRegistrations = await navigator.serviceWorker.getRegistrations().then(
|
|
425
|
+
const mockRegistrations = await navigator.serviceWorker.getRegistrations().then(
|
|
426
|
+
(registrations) => registrations.filter(
|
|
427
|
+
(registration) => getWorkerByRegistration(registration, absoluteWorkerUrl, findWorker)
|
|
428
|
+
)
|
|
429
|
+
);
|
|
497
430
|
if (!navigator.serviceWorker.controller && mockRegistrations.length > 0) {
|
|
498
431
|
location.reload();
|
|
499
432
|
}
|
|
@@ -501,29 +434,42 @@ var getWorkerInstance = async (url, options = {}, findWorker) => {
|
|
|
501
434
|
if (existingRegistration) {
|
|
502
435
|
return existingRegistration.update().then(() => {
|
|
503
436
|
return [
|
|
504
|
-
getWorkerByRegistration(
|
|
437
|
+
getWorkerByRegistration(
|
|
438
|
+
existingRegistration,
|
|
439
|
+
absoluteWorkerUrl,
|
|
440
|
+
findWorker
|
|
441
|
+
),
|
|
505
442
|
existingRegistration
|
|
506
443
|
];
|
|
507
444
|
});
|
|
508
445
|
}
|
|
509
|
-
const [error2, instance] = await (0, import_until.until)(
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
446
|
+
const [error2, instance] = await (0, import_until.until)(
|
|
447
|
+
async () => {
|
|
448
|
+
const registration = await navigator.serviceWorker.register(url, options);
|
|
449
|
+
return [
|
|
450
|
+
getWorkerByRegistration(registration, absoluteWorkerUrl, findWorker),
|
|
451
|
+
registration
|
|
452
|
+
];
|
|
453
|
+
}
|
|
454
|
+
);
|
|
516
455
|
if (error2) {
|
|
517
456
|
const isWorkerMissing = error2.message.includes("(404)");
|
|
518
457
|
if (isWorkerMissing) {
|
|
519
458
|
const scopeUrl = new URL((options == null ? void 0 : options.scope) || "/", location.href);
|
|
520
|
-
throw new Error(
|
|
459
|
+
throw new Error(
|
|
460
|
+
devUtils.formatMessage(`Failed to register a Service Worker for scope ('${scopeUrl.href}') with script ('${absoluteWorkerUrl}'): Service Worker script does not exist at the given path.
|
|
521
461
|
|
|
522
462
|
Did you forget to run "npx msw init <PUBLIC_DIR>"?
|
|
523
463
|
|
|
524
|
-
Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/init`)
|
|
464
|
+
Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/init`)
|
|
465
|
+
);
|
|
525
466
|
}
|
|
526
|
-
throw new Error(
|
|
467
|
+
throw new Error(
|
|
468
|
+
devUtils.formatMessage(
|
|
469
|
+
"Failed to register the Service Worker:\n\n%s",
|
|
470
|
+
error2.message
|
|
471
|
+
)
|
|
472
|
+
);
|
|
527
473
|
}
|
|
528
474
|
return instance;
|
|
529
475
|
};
|
|
@@ -534,8 +480,15 @@ function printStartMessage(args = {}) {
|
|
|
534
480
|
return;
|
|
535
481
|
}
|
|
536
482
|
const message = args.message || "Mocking enabled.";
|
|
537
|
-
console.groupCollapsed(
|
|
538
|
-
|
|
483
|
+
console.groupCollapsed(
|
|
484
|
+
`%c${devUtils.formatMessage(message)}`,
|
|
485
|
+
"color:orangered;font-weight:bold;"
|
|
486
|
+
);
|
|
487
|
+
console.log(
|
|
488
|
+
"%cDocumentation: %chttps://mswjs.io/docs",
|
|
489
|
+
"font-weight:bold",
|
|
490
|
+
"font-weight:normal"
|
|
491
|
+
);
|
|
539
492
|
console.log("Found an issue? https://github.com/mswjs/msw/issues");
|
|
540
493
|
if (args.workerUrl) {
|
|
541
494
|
console.log("Worker script URL:", args.workerUrl);
|
|
@@ -552,7 +505,9 @@ async function enableMocking(context, options) {
|
|
|
552
505
|
context.workerChannel.send("MOCK_ACTIVATE");
|
|
553
506
|
await context.events.once("MOCKING_ENABLED");
|
|
554
507
|
if (context.isMockingEnabled) {
|
|
555
|
-
devUtils.warn(
|
|
508
|
+
devUtils.warn(
|
|
509
|
+
`Found a redundant "worker.start()" call. Note that starting the worker while mocking is already enabled will have no effect. Consider removing this "worker.start()" call.`
|
|
510
|
+
);
|
|
556
511
|
return;
|
|
557
512
|
}
|
|
558
513
|
context.isMockingEnabled = true;
|
|
@@ -739,15 +694,23 @@ var MockedRequest = class extends import_interceptors.IsomorphicRequest {
|
|
|
739
694
|
const requestCookiesString = this.headers.get("cookie");
|
|
740
695
|
const ownCookies = requestCookiesString ? cookieUtils3.parse(requestCookiesString) : {};
|
|
741
696
|
import_cookies.store.hydrate();
|
|
742
|
-
const cookiesFromStore = Array.from(
|
|
697
|
+
const cookiesFromStore = Array.from(
|
|
698
|
+
(_a = import_cookies.store.get({ ...this, url: this.url.href })) == null ? void 0 : _a.entries()
|
|
699
|
+
).reduce((cookies, [name, { value }]) => {
|
|
743
700
|
return Object.assign(cookies, { [name.trim()]: value });
|
|
744
701
|
}, {});
|
|
745
702
|
const cookiesFromDocument = getRequestCookies(this);
|
|
746
|
-
const forwardedCookies =
|
|
703
|
+
const forwardedCookies = {
|
|
704
|
+
...cookiesFromDocument,
|
|
705
|
+
...cookiesFromStore
|
|
706
|
+
};
|
|
747
707
|
for (const [name, value] of Object.entries(forwardedCookies)) {
|
|
748
708
|
this.headers.append("cookie", `${name}=${value}`);
|
|
749
709
|
}
|
|
750
|
-
return
|
|
710
|
+
return {
|
|
711
|
+
...forwardedCookies,
|
|
712
|
+
...ownCookies
|
|
713
|
+
};
|
|
751
714
|
}
|
|
752
715
|
};
|
|
753
716
|
|
|
@@ -755,10 +718,11 @@ var MockedRequest = class extends import_interceptors.IsomorphicRequest {
|
|
|
755
718
|
function parseWorkerRequest(rawRequest) {
|
|
756
719
|
const url = new URL(rawRequest.url);
|
|
757
720
|
const headers = new import_headers_polyfill5.Headers(rawRequest.headers);
|
|
758
|
-
return new MockedRequest(url,
|
|
721
|
+
return new MockedRequest(url, {
|
|
722
|
+
...rawRequest,
|
|
759
723
|
body: (0, import_interceptors2.encodeBuffer)(rawRequest.body || ""),
|
|
760
724
|
headers
|
|
761
|
-
})
|
|
725
|
+
});
|
|
762
726
|
}
|
|
763
727
|
|
|
764
728
|
// src/utils/handleRequest.ts
|
|
@@ -819,7 +783,10 @@ var import_graphql = require("graphql");
|
|
|
819
783
|
|
|
820
784
|
// src/utils/request/getPublicUrlFromRequest.ts
|
|
821
785
|
var getPublicUrlFromRequest = (request) => {
|
|
822
|
-
return request.referrer.startsWith(request.url.origin) ? request.url.pathname : new URL(
|
|
786
|
+
return request.referrer.startsWith(request.url.origin) ? request.url.pathname : new URL(
|
|
787
|
+
request.url.pathname,
|
|
788
|
+
`${request.url.protocol}//${request.url.host}`
|
|
789
|
+
).href;
|
|
823
790
|
};
|
|
824
791
|
|
|
825
792
|
// src/utils/internal/parseGraphQLRequest.ts
|
|
@@ -882,13 +849,19 @@ function getGraphQLInput(request) {
|
|
|
882
849
|
};
|
|
883
850
|
}
|
|
884
851
|
if ((_b = request.body) == null ? void 0 : _b.operations) {
|
|
885
|
-
const
|
|
886
|
-
const parsedOperations = jsonParse(
|
|
852
|
+
const { operations, map, ...files } = request.body;
|
|
853
|
+
const parsedOperations = jsonParse(
|
|
854
|
+
operations
|
|
855
|
+
) || {};
|
|
887
856
|
if (!parsedOperations.query) {
|
|
888
857
|
return null;
|
|
889
858
|
}
|
|
890
859
|
const parsedMap = jsonParse(map || "") || {};
|
|
891
|
-
const variables = parsedOperations.variables ? extractMultipartVariables(
|
|
860
|
+
const variables = parsedOperations.variables ? extractMultipartVariables(
|
|
861
|
+
parsedOperations.variables,
|
|
862
|
+
parsedMap,
|
|
863
|
+
files
|
|
864
|
+
) : {};
|
|
892
865
|
return {
|
|
893
866
|
query: parsedOperations.query,
|
|
894
867
|
variables
|
|
@@ -908,7 +881,14 @@ function parseGraphQLRequest(request) {
|
|
|
908
881
|
const parsedResult = parseQuery(query);
|
|
909
882
|
if (parsedResult instanceof Error) {
|
|
910
883
|
const requestPublicUrl = getPublicUrlFromRequest(request);
|
|
911
|
-
throw new Error(
|
|
884
|
+
throw new Error(
|
|
885
|
+
devUtils.formatMessage(
|
|
886
|
+
'Failed to intercept a GraphQL request to "%s %s": cannot parse query. See the error message from the parser below.\n\n%s',
|
|
887
|
+
request.method,
|
|
888
|
+
requestPublicUrl,
|
|
889
|
+
parsedResult.message
|
|
890
|
+
)
|
|
891
|
+
);
|
|
912
892
|
}
|
|
913
893
|
return {
|
|
914
894
|
operationType: parsedResult.operationType,
|
|
@@ -936,19 +916,21 @@ function getTimestamp() {
|
|
|
936
916
|
|
|
937
917
|
// src/utils/logging/prepareRequest.ts
|
|
938
918
|
function prepareRequest(request) {
|
|
939
|
-
return
|
|
919
|
+
return {
|
|
920
|
+
...request,
|
|
940
921
|
body: request.body,
|
|
941
922
|
headers: request.headers.all()
|
|
942
|
-
}
|
|
923
|
+
};
|
|
943
924
|
}
|
|
944
925
|
|
|
945
926
|
// src/utils/logging/prepareResponse.ts
|
|
946
927
|
var import_headers_polyfill6 = require("headers-polyfill");
|
|
947
928
|
function prepareResponse(res) {
|
|
948
929
|
const responseHeaders = (0, import_headers_polyfill6.objectToHeaders)(res.headers);
|
|
949
|
-
return
|
|
930
|
+
return {
|
|
931
|
+
...res,
|
|
950
932
|
body: parseBody(res.body, responseHeaders)
|
|
951
|
-
}
|
|
933
|
+
};
|
|
952
934
|
}
|
|
953
935
|
|
|
954
936
|
// src/utils/matching/matchRequestUrl.ts
|
|
@@ -992,13 +974,16 @@ function normalizePath(path, baseUrl) {
|
|
|
992
974
|
|
|
993
975
|
// src/utils/matching/matchRequestUrl.ts
|
|
994
976
|
function coercePath(path) {
|
|
995
|
-
return path.replace(
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
977
|
+
return path.replace(
|
|
978
|
+
/([:a-zA-Z_-]*)(\*{1,2})+/g,
|
|
979
|
+
(_, parameterName, wildcard) => {
|
|
980
|
+
const expression = "(.*)";
|
|
981
|
+
if (!parameterName) {
|
|
982
|
+
return expression;
|
|
983
|
+
}
|
|
984
|
+
return parameterName.startsWith(":") ? `${parameterName}${wildcard}` : `${parameterName}${expression}`;
|
|
999
985
|
}
|
|
1000
|
-
|
|
1001
|
-
}).replace(/([^\/])(:)(?=\d+)/, "$1\\$2").replace(/^([^\/]+)(:)(?=\/\/)/, "$1\\$2");
|
|
986
|
+
).replace(/([^\/])(:)(?=\d+)/, "$1\\$2").replace(/^([^\/]+)(:)(?=\/\/)/, "$1\\$2");
|
|
1002
987
|
}
|
|
1003
988
|
function matchRequestUrl(url, path, baseUrl) {
|
|
1004
989
|
const normalizedPath = normalizePath(path, baseUrl);
|
|
@@ -1039,11 +1024,16 @@ var defaultResponse = {
|
|
|
1039
1024
|
var defaultResponseTransformers = [];
|
|
1040
1025
|
function createResponseComposition(responseOverrides, defaultTransformers = defaultResponseTransformers) {
|
|
1041
1026
|
return async (...transformers) => {
|
|
1042
|
-
const initialResponse = Object.assign(
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1027
|
+
const initialResponse = Object.assign(
|
|
1028
|
+
{},
|
|
1029
|
+
defaultResponse,
|
|
1030
|
+
{
|
|
1031
|
+
headers: new import_headers_polyfill7.Headers({
|
|
1032
|
+
"x-powered-by": "msw"
|
|
1033
|
+
})
|
|
1034
|
+
},
|
|
1035
|
+
responseOverrides
|
|
1036
|
+
);
|
|
1047
1037
|
const resolvedTransformers = [
|
|
1048
1038
|
...defaultTransformers,
|
|
1049
1039
|
...transformers
|
|
@@ -1099,15 +1089,20 @@ var RequestHandler = class {
|
|
|
1099
1089
|
this.ctx = options.ctx || defaultContext;
|
|
1100
1090
|
this.resolver = options.resolver;
|
|
1101
1091
|
const callFrame = getCallFrame(new Error());
|
|
1102
|
-
this.info =
|
|
1092
|
+
this.info = {
|
|
1093
|
+
...options.info,
|
|
1103
1094
|
callFrame
|
|
1104
|
-
}
|
|
1095
|
+
};
|
|
1105
1096
|
}
|
|
1106
1097
|
parse(_request, _resolutionContext) {
|
|
1107
1098
|
return null;
|
|
1108
1099
|
}
|
|
1109
1100
|
test(request, resolutionContext) {
|
|
1110
|
-
return this.predicate(
|
|
1101
|
+
return this.predicate(
|
|
1102
|
+
request,
|
|
1103
|
+
this.parse(request, resolutionContext),
|
|
1104
|
+
resolutionContext
|
|
1105
|
+
);
|
|
1111
1106
|
}
|
|
1112
1107
|
getPublicRequest(request, _parsedResult) {
|
|
1113
1108
|
return request;
|
|
@@ -1120,14 +1115,26 @@ var RequestHandler = class {
|
|
|
1120
1115
|
return null;
|
|
1121
1116
|
}
|
|
1122
1117
|
const parsedResult = this.parse(request, resolutionContext);
|
|
1123
|
-
const shouldIntercept = this.predicate(
|
|
1118
|
+
const shouldIntercept = this.predicate(
|
|
1119
|
+
request,
|
|
1120
|
+
parsedResult,
|
|
1121
|
+
resolutionContext
|
|
1122
|
+
);
|
|
1124
1123
|
if (!shouldIntercept) {
|
|
1125
1124
|
return null;
|
|
1126
1125
|
}
|
|
1127
1126
|
const publicRequest = this.getPublicRequest(request, parsedResult);
|
|
1128
1127
|
const executeResolver = this.wrapResolver(this.resolver);
|
|
1129
|
-
const mockedResponse = await executeResolver(
|
|
1130
|
-
|
|
1128
|
+
const mockedResponse = await executeResolver(
|
|
1129
|
+
publicRequest,
|
|
1130
|
+
response,
|
|
1131
|
+
this.ctx
|
|
1132
|
+
);
|
|
1133
|
+
return this.createExecutionResult(
|
|
1134
|
+
parsedResult,
|
|
1135
|
+
publicRequest,
|
|
1136
|
+
mockedResponse
|
|
1137
|
+
);
|
|
1131
1138
|
}
|
|
1132
1139
|
wrapResolver(resolver) {
|
|
1133
1140
|
return async (req, res, ctx) => {
|
|
@@ -1168,18 +1175,20 @@ var RESTMethods = /* @__PURE__ */ ((RESTMethods2) => {
|
|
|
1168
1175
|
RESTMethods2["DELETE"] = "DELETE";
|
|
1169
1176
|
return RESTMethods2;
|
|
1170
1177
|
})(RESTMethods || {});
|
|
1171
|
-
var restContext =
|
|
1178
|
+
var restContext = {
|
|
1179
|
+
...defaultContext,
|
|
1172
1180
|
cookie,
|
|
1173
1181
|
body,
|
|
1174
1182
|
text,
|
|
1175
1183
|
json,
|
|
1176
1184
|
xml
|
|
1177
|
-
}
|
|
1185
|
+
};
|
|
1178
1186
|
var RestRequest = class extends MockedRequest {
|
|
1179
1187
|
constructor(request, params) {
|
|
1180
|
-
super(request.url,
|
|
1188
|
+
super(request.url, {
|
|
1189
|
+
...request,
|
|
1181
1190
|
body: request["_body"]
|
|
1182
|
-
})
|
|
1191
|
+
});
|
|
1183
1192
|
this.params = params;
|
|
1184
1193
|
this.id = request.id;
|
|
1185
1194
|
}
|
|
@@ -1211,10 +1220,16 @@ var RestHandler = class extends RequestHandler {
|
|
|
1211
1220
|
searchParams.forEach((_, paramName) => {
|
|
1212
1221
|
queryParams.push(paramName);
|
|
1213
1222
|
});
|
|
1214
|
-
devUtils.warn(
|
|
1223
|
+
devUtils.warn(
|
|
1224
|
+
`Found a redundant usage of query parameters in the request handler URL for "${method} ${path}". Please match against a path instead and access query parameters in the response resolver function using "req.url.searchParams".`
|
|
1225
|
+
);
|
|
1215
1226
|
}
|
|
1216
1227
|
parse(request, resolutionContext) {
|
|
1217
|
-
return matchRequestUrl(
|
|
1228
|
+
return matchRequestUrl(
|
|
1229
|
+
request.url,
|
|
1230
|
+
this.info.path,
|
|
1231
|
+
resolutionContext == null ? void 0 : resolutionContext.baseUrl
|
|
1232
|
+
);
|
|
1218
1233
|
}
|
|
1219
1234
|
getPublicRequest(request, parsedResult) {
|
|
1220
1235
|
return new RestRequest(request, parsedResult.params || {});
|
|
@@ -1228,7 +1243,15 @@ var RestHandler = class extends RequestHandler {
|
|
|
1228
1243
|
const loggedRequest = prepareRequest(request);
|
|
1229
1244
|
const loggedResponse = prepareResponse(response2);
|
|
1230
1245
|
const statusColor = getStatusCodeColor(response2.status);
|
|
1231
|
-
console.groupCollapsed(
|
|
1246
|
+
console.groupCollapsed(
|
|
1247
|
+
devUtils.formatMessage("%s %s %s (%c%s%c)"),
|
|
1248
|
+
getTimestamp(),
|
|
1249
|
+
request.method,
|
|
1250
|
+
publicUrl,
|
|
1251
|
+
`color:${statusColor}`,
|
|
1252
|
+
`${response2.status} ${response2.statusText}`,
|
|
1253
|
+
"color:inherit"
|
|
1254
|
+
);
|
|
1232
1255
|
console.log("Request", loggedRequest);
|
|
1233
1256
|
console.log("Handler:", this);
|
|
1234
1257
|
console.log("Response", loggedResponse);
|
|
@@ -1247,10 +1270,33 @@ var field = (fieldName, fieldValue) => {
|
|
|
1247
1270
|
};
|
|
1248
1271
|
};
|
|
1249
1272
|
function validateFieldName(fieldName) {
|
|
1250
|
-
(0, import_outvariant3.invariant)(
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1273
|
+
(0, import_outvariant3.invariant)(
|
|
1274
|
+
fieldName.trim() !== "",
|
|
1275
|
+
devUtils.formatMessage(
|
|
1276
|
+
"Failed to set a custom field on a GraphQL response: field name cannot be empty."
|
|
1277
|
+
)
|
|
1278
|
+
);
|
|
1279
|
+
(0, import_outvariant3.invariant)(
|
|
1280
|
+
fieldName !== "data",
|
|
1281
|
+
devUtils.formatMessage(
|
|
1282
|
+
'Failed to set a custom "%s" field on a mocked GraphQL response: forbidden field name. Did you mean to call "ctx.data()" instead?',
|
|
1283
|
+
fieldName
|
|
1284
|
+
)
|
|
1285
|
+
);
|
|
1286
|
+
(0, import_outvariant3.invariant)(
|
|
1287
|
+
fieldName !== "errors",
|
|
1288
|
+
devUtils.formatMessage(
|
|
1289
|
+
'Failed to set a custom "%s" field on a mocked GraphQL response: forbidden field name. Did you mean to call "ctx.errors()" instead?',
|
|
1290
|
+
fieldName
|
|
1291
|
+
)
|
|
1292
|
+
);
|
|
1293
|
+
(0, import_outvariant3.invariant)(
|
|
1294
|
+
fieldName !== "extensions",
|
|
1295
|
+
devUtils.formatMessage(
|
|
1296
|
+
'Failed to set a custom "%s" field on a mocked GraphQL response: forbidden field name. Did you mean to call "ctx.extensions()" instead?',
|
|
1297
|
+
fieldName
|
|
1298
|
+
)
|
|
1299
|
+
);
|
|
1254
1300
|
}
|
|
1255
1301
|
|
|
1256
1302
|
// src/utils/internal/tryCatch.ts
|
|
@@ -1264,13 +1310,14 @@ function tryCatch(fn, onException) {
|
|
|
1264
1310
|
}
|
|
1265
1311
|
|
|
1266
1312
|
// src/handlers/GraphQLHandler.ts
|
|
1267
|
-
var graphqlContext =
|
|
1313
|
+
var graphqlContext = {
|
|
1314
|
+
...defaultContext,
|
|
1268
1315
|
data,
|
|
1269
1316
|
extensions,
|
|
1270
1317
|
errors,
|
|
1271
1318
|
cookie,
|
|
1272
1319
|
field
|
|
1273
|
-
}
|
|
1320
|
+
};
|
|
1274
1321
|
function isDocumentNode(value) {
|
|
1275
1322
|
if (value == null) {
|
|
1276
1323
|
return false;
|
|
@@ -1279,9 +1326,10 @@ function isDocumentNode(value) {
|
|
|
1279
1326
|
}
|
|
1280
1327
|
var GraphQLRequest = class extends MockedRequest {
|
|
1281
1328
|
constructor(request, variables) {
|
|
1282
|
-
super(request.url,
|
|
1329
|
+
super(request.url, {
|
|
1330
|
+
...request,
|
|
1283
1331
|
body: request["_body"]
|
|
1284
|
-
})
|
|
1332
|
+
});
|
|
1285
1333
|
this.variables = variables;
|
|
1286
1334
|
}
|
|
1287
1335
|
};
|
|
@@ -1291,10 +1339,14 @@ var GraphQLHandler = class extends RequestHandler {
|
|
|
1291
1339
|
if (isDocumentNode(operationName)) {
|
|
1292
1340
|
const parsedNode = parseDocumentNode(operationName);
|
|
1293
1341
|
if (parsedNode.operationType !== operationType) {
|
|
1294
|
-
throw new Error(
|
|
1342
|
+
throw new Error(
|
|
1343
|
+
`Failed to create a GraphQL handler: provided a DocumentNode with a mismatched operation type (expected "${operationType}", but got "${parsedNode.operationType}").`
|
|
1344
|
+
);
|
|
1295
1345
|
}
|
|
1296
1346
|
if (!parsedNode.operationName) {
|
|
1297
|
-
throw new Error(
|
|
1347
|
+
throw new Error(
|
|
1348
|
+
`Failed to create a GraphQL handler: provided a DocumentNode with no operation name.`
|
|
1349
|
+
);
|
|
1298
1350
|
}
|
|
1299
1351
|
resolvedOperationName = parsedNode.operationName;
|
|
1300
1352
|
}
|
|
@@ -1311,7 +1363,10 @@ var GraphQLHandler = class extends RequestHandler {
|
|
|
1311
1363
|
this.endpoint = endpoint;
|
|
1312
1364
|
}
|
|
1313
1365
|
parse(request) {
|
|
1314
|
-
return tryCatch(
|
|
1366
|
+
return tryCatch(
|
|
1367
|
+
() => parseGraphQLRequest(request),
|
|
1368
|
+
(error2) => console.error(error2.message)
|
|
1369
|
+
);
|
|
1315
1370
|
}
|
|
1316
1371
|
getPublicRequest(request, parsedResult) {
|
|
1317
1372
|
return new GraphQLRequest(request, (parsedResult == null ? void 0 : parsedResult.variables) || {});
|
|
@@ -1324,7 +1379,7 @@ var GraphQLHandler = class extends RequestHandler {
|
|
|
1324
1379
|
const publicUrl = getPublicUrlFromRequest(request);
|
|
1325
1380
|
devUtils.warn(`Failed to intercept a GraphQL request at "${request.method} ${publicUrl}": anonymous GraphQL operations are not supported.
|
|
1326
1381
|
|
|
1327
|
-
Consider naming this operation or using "graphql.operation" request handler to intercept GraphQL requests regardless of their operation name/type. Read more: https://mswjs.io/docs/api/graphql/operation `);
|
|
1382
|
+
Consider naming this operation or using "graphql.operation()" request handler to intercept GraphQL requests regardless of their operation name/type. Read more: https://mswjs.io/docs/api/graphql/operation `);
|
|
1328
1383
|
return false;
|
|
1329
1384
|
}
|
|
1330
1385
|
const hasMatchingUrl = matchRequestUrl(request.url, this.endpoint);
|
|
@@ -1337,7 +1392,14 @@ Consider naming this operation or using "graphql.operation" request handler to i
|
|
|
1337
1392
|
const loggedResponse = prepareResponse(response2);
|
|
1338
1393
|
const statusColor = getStatusCodeColor(response2.status);
|
|
1339
1394
|
const requestInfo = (parsedRequest == null ? void 0 : parsedRequest.operationName) ? `${parsedRequest == null ? void 0 : parsedRequest.operationType} ${parsedRequest == null ? void 0 : parsedRequest.operationName}` : `anonymous ${parsedRequest == null ? void 0 : parsedRequest.operationType}`;
|
|
1340
|
-
console.groupCollapsed(
|
|
1395
|
+
console.groupCollapsed(
|
|
1396
|
+
devUtils.formatMessage("%s %s (%c%s%c)"),
|
|
1397
|
+
getTimestamp(),
|
|
1398
|
+
`${requestInfo}`,
|
|
1399
|
+
`color:${statusColor}`,
|
|
1400
|
+
`${response2.status} ${response2.statusText}`,
|
|
1401
|
+
"color:inherit"
|
|
1402
|
+
);
|
|
1341
1403
|
console.log("Request:", loggedRequest);
|
|
1342
1404
|
console.log("Handler:", this);
|
|
1343
1405
|
console.log("Response:", loggedResponse);
|
|
@@ -1350,18 +1412,21 @@ var MAX_MATCH_SCORE = 3;
|
|
|
1350
1412
|
var MAX_SUGGESTION_COUNT = 4;
|
|
1351
1413
|
var TYPE_MATCH_DELTA = 0.5;
|
|
1352
1414
|
function groupHandlersByType(handlers) {
|
|
1353
|
-
return handlers.reduce(
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1415
|
+
return handlers.reduce(
|
|
1416
|
+
(groups, handler) => {
|
|
1417
|
+
if (handler instanceof RestHandler) {
|
|
1418
|
+
groups.rest.push(handler);
|
|
1419
|
+
}
|
|
1420
|
+
if (handler instanceof GraphQLHandler) {
|
|
1421
|
+
groups.graphql.push(handler);
|
|
1422
|
+
}
|
|
1423
|
+
return groups;
|
|
1424
|
+
},
|
|
1425
|
+
{
|
|
1426
|
+
rest: [],
|
|
1427
|
+
graphql: []
|
|
1359
1428
|
}
|
|
1360
|
-
|
|
1361
|
-
}, {
|
|
1362
|
-
rest: [],
|
|
1363
|
-
graphql: []
|
|
1364
|
-
});
|
|
1429
|
+
);
|
|
1365
1430
|
}
|
|
1366
1431
|
function getRestHandlerScore() {
|
|
1367
1432
|
return (request, handler) => {
|
|
@@ -1411,7 +1476,11 @@ function onUnhandledRequest(request, handlers, strategy = "warn") {
|
|
|
1411
1476
|
function generateHandlerSuggestion() {
|
|
1412
1477
|
const handlerGroups = groupHandlersByType(handlers);
|
|
1413
1478
|
const relevantHandlers = parsedGraphQLQuery ? handlerGroups.graphql : handlerGroups.rest;
|
|
1414
|
-
const suggestedHandlers = getSuggestedHandler(
|
|
1479
|
+
const suggestedHandlers = getSuggestedHandler(
|
|
1480
|
+
request,
|
|
1481
|
+
relevantHandlers,
|
|
1482
|
+
parsedGraphQLQuery ? getGraphQLHandlerScore(parsedGraphQLQuery) : getRestHandlerScore()
|
|
1483
|
+
);
|
|
1415
1484
|
return suggestedHandlers.length > 0 ? getSuggestedHandlersMessage(suggestedHandlers) : "";
|
|
1416
1485
|
}
|
|
1417
1486
|
function generateUnhandledRequestMessage() {
|
|
@@ -1432,7 +1501,11 @@ Read more: https://mswjs.io/docs/getting-started/mocks`
|
|
|
1432
1501
|
switch (strategy2) {
|
|
1433
1502
|
case "error": {
|
|
1434
1503
|
devUtils.error("Error: %s", message);
|
|
1435
|
-
throw new Error(
|
|
1504
|
+
throw new Error(
|
|
1505
|
+
devUtils.formatMessage(
|
|
1506
|
+
'Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.'
|
|
1507
|
+
)
|
|
1508
|
+
);
|
|
1436
1509
|
}
|
|
1437
1510
|
case "warn": {
|
|
1438
1511
|
devUtils.warn("Warning: %s", message);
|
|
@@ -1441,7 +1514,12 @@ Read more: https://mswjs.io/docs/getting-started/mocks`
|
|
|
1441
1514
|
case "bypass":
|
|
1442
1515
|
break;
|
|
1443
1516
|
default:
|
|
1444
|
-
throw new Error(
|
|
1517
|
+
throw new Error(
|
|
1518
|
+
devUtils.formatMessage(
|
|
1519
|
+
'Failed to react to an unhandled request: unknown strategy "%s". Please provide one of the supported strategies ("bypass", "warn", "error") or a custom callback function as the value of the "onUnhandledRequest" option.',
|
|
1520
|
+
strategy2
|
|
1521
|
+
)
|
|
1522
|
+
);
|
|
1445
1523
|
}
|
|
1446
1524
|
}
|
|
1447
1525
|
if (typeof strategy === "function") {
|
|
@@ -1457,7 +1535,7 @@ Read more: https://mswjs.io/docs/getting-started/mocks`
|
|
|
1457
1535
|
// src/utils/request/readResponseCookies.ts
|
|
1458
1536
|
var import_cookies2 = require("@mswjs/cookies");
|
|
1459
1537
|
function readResponseCookies(request, response2) {
|
|
1460
|
-
import_cookies2.store.add(
|
|
1538
|
+
import_cookies2.store.add({ ...request, url: request.url.toString() }, response2);
|
|
1461
1539
|
import_cookies2.store.persist();
|
|
1462
1540
|
}
|
|
1463
1541
|
|
|
@@ -1471,7 +1549,11 @@ async function handleRequest(request, handlers, options, emitter, handleRequestO
|
|
|
1471
1549
|
return;
|
|
1472
1550
|
}
|
|
1473
1551
|
const [lookupError, lookupResult] = await (0, import_until2.until)(() => {
|
|
1474
|
-
return getResponse(
|
|
1552
|
+
return getResponse(
|
|
1553
|
+
request,
|
|
1554
|
+
handlers,
|
|
1555
|
+
handleRequestOptions == null ? void 0 : handleRequestOptions.resolutionContext
|
|
1556
|
+
);
|
|
1475
1557
|
});
|
|
1476
1558
|
if (lookupError) {
|
|
1477
1559
|
emitter.emit("unhandledException", lookupError, request);
|
|
@@ -1486,10 +1568,15 @@ async function handleRequest(request, handlers, options, emitter, handleRequestO
|
|
|
1486
1568
|
return;
|
|
1487
1569
|
}
|
|
1488
1570
|
if (!response2) {
|
|
1489
|
-
devUtils.warn(
|
|
1571
|
+
devUtils.warn(
|
|
1572
|
+
`Expected response resolver to return a mocked response Object, but got %s. The original response is going to be used instead.
|
|
1490
1573
|
|
|
1491
1574
|
\u2022 %s
|
|
1492
|
-
%s`,
|
|
1575
|
+
%s`,
|
|
1576
|
+
response2,
|
|
1577
|
+
handler.info.header,
|
|
1578
|
+
handler.info.callFrame
|
|
1579
|
+
);
|
|
1493
1580
|
emitter.emit("request:end", request);
|
|
1494
1581
|
(_c = handleRequestOptions == null ? void 0 : handleRequestOptions.onPassthroughResponse) == null ? void 0 : _c.call(handleRequestOptions, request);
|
|
1495
1582
|
return;
|
|
@@ -1503,7 +1590,11 @@ async function handleRequest(request, handlers, options, emitter, handleRequestO
|
|
|
1503
1590
|
emitter.emit("request:match", request);
|
|
1504
1591
|
const requiredLookupResult = lookupResult;
|
|
1505
1592
|
const transformedResponse = ((_e = handleRequestOptions == null ? void 0 : handleRequestOptions.transformResponse) == null ? void 0 : _e.call(handleRequestOptions, response2)) || response2;
|
|
1506
|
-
(_f = handleRequestOptions == null ? void 0 : handleRequestOptions.onMockedResponse) == null ? void 0 : _f.call(
|
|
1593
|
+
(_f = handleRequestOptions == null ? void 0 : handleRequestOptions.onMockedResponse) == null ? void 0 : _f.call(
|
|
1594
|
+
handleRequestOptions,
|
|
1595
|
+
transformedResponse,
|
|
1596
|
+
requiredLookupResult
|
|
1597
|
+
);
|
|
1507
1598
|
emitter.emit("request:end", request);
|
|
1508
1599
|
return transformedResponse;
|
|
1509
1600
|
}
|
|
@@ -1526,28 +1617,47 @@ var createRequestListener = (context, options) => {
|
|
|
1526
1617
|
const messageChannel = new WorkerChannel(event.ports[0]);
|
|
1527
1618
|
const request = parseWorkerRequest(message.payload);
|
|
1528
1619
|
try {
|
|
1529
|
-
await handleRequest(
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1620
|
+
await handleRequest(
|
|
1621
|
+
request,
|
|
1622
|
+
context.requestHandlers,
|
|
1623
|
+
options,
|
|
1624
|
+
context.emitter,
|
|
1625
|
+
{
|
|
1626
|
+
transformResponse,
|
|
1627
|
+
onPassthroughResponse() {
|
|
1628
|
+
messageChannel.postMessage("NOT_FOUND");
|
|
1629
|
+
},
|
|
1630
|
+
async onMockedResponse(response2, { handler, publicRequest, parsedRequest }) {
|
|
1631
|
+
if (response2.body instanceof ReadableStream) {
|
|
1632
|
+
throw new Error(
|
|
1633
|
+
devUtils.formatMessage(
|
|
1634
|
+
'Failed to construct a mocked response with a "ReadableStream" body: mocked streams are not supported. Follow https://github.com/mswjs/msw/issues/1336 for more details.'
|
|
1635
|
+
)
|
|
1636
|
+
);
|
|
1637
|
+
}
|
|
1638
|
+
const responseInstance = new Response(response2.body, response2);
|
|
1639
|
+
const responseBodyBuffer = await responseInstance.arrayBuffer();
|
|
1640
|
+
const responseBody = response2.body == null ? null : responseBodyBuffer;
|
|
1641
|
+
messageChannel.postMessage(
|
|
1642
|
+
"MOCK_RESPONSE",
|
|
1643
|
+
{
|
|
1644
|
+
...response2,
|
|
1645
|
+
body: responseBody
|
|
1646
|
+
},
|
|
1647
|
+
[responseBodyBuffer]
|
|
1648
|
+
);
|
|
1649
|
+
if (!options.quiet) {
|
|
1650
|
+
context.emitter.once("response:mocked", (response3) => {
|
|
1651
|
+
handler.log(
|
|
1652
|
+
publicRequest,
|
|
1653
|
+
serializeResponse(response3),
|
|
1654
|
+
parsedRequest
|
|
1655
|
+
);
|
|
1656
|
+
});
|
|
1657
|
+
}
|
|
1548
1658
|
}
|
|
1549
1659
|
}
|
|
1550
|
-
|
|
1660
|
+
);
|
|
1551
1661
|
} catch (error2) {
|
|
1552
1662
|
if (error2 instanceof NetworkError) {
|
|
1553
1663
|
messageChannel.postMessage("NETWORK_ERROR", {
|
|
@@ -1557,11 +1667,16 @@ var createRequestListener = (context, options) => {
|
|
|
1557
1667
|
return;
|
|
1558
1668
|
}
|
|
1559
1669
|
if (error2 instanceof Error) {
|
|
1560
|
-
devUtils.error(
|
|
1670
|
+
devUtils.error(
|
|
1671
|
+
`Uncaught exception in the request handler for "%s %s":
|
|
1561
1672
|
|
|
1562
1673
|
%s
|
|
1563
1674
|
|
|
1564
|
-
This exception has been gracefully handled as a 500 response, however, it's strongly recommended to resolve this error, as it indicates a mistake in your code. If you wish to mock an error response, please see this guide: https://mswjs.io/docs/recipes/mocking-error-responses`,
|
|
1675
|
+
This exception has been gracefully handled as a 500 response, however, it's strongly recommended to resolve this error, as it indicates a mistake in your code. If you wish to mock an error response, please see this guide: https://mswjs.io/docs/recipes/mocking-error-responses`,
|
|
1676
|
+
request.method,
|
|
1677
|
+
request.url,
|
|
1678
|
+
(_a = error2.stack) != null ? _a : error2
|
|
1679
|
+
);
|
|
1565
1680
|
messageChannel.postMessage("MOCK_RESPONSE", {
|
|
1566
1681
|
status: 500,
|
|
1567
1682
|
statusText: "Request Handler Error",
|
|
@@ -1591,9 +1706,13 @@ function transformResponse(response2) {
|
|
|
1591
1706
|
// src/utils/internal/requestIntegrityCheck.ts
|
|
1592
1707
|
async function requestIntegrityCheck(context, serviceWorker) {
|
|
1593
1708
|
context.workerChannel.send("INTEGRITY_CHECK_REQUEST");
|
|
1594
|
-
const { payload: actualChecksum } = await context.events.once(
|
|
1709
|
+
const { payload: actualChecksum } = await context.events.once(
|
|
1710
|
+
"INTEGRITY_CHECK_RESPONSE"
|
|
1711
|
+
);
|
|
1595
1712
|
if (actualChecksum !== "3d6b9f06410d179a7f7404d4bf4c3c70") {
|
|
1596
|
-
throw new Error(
|
|
1713
|
+
throw new Error(
|
|
1714
|
+
`Currently active Service Worker (${actualChecksum}) is behind the latest published one (${"3d6b9f06410d179a7f7404d4bf4c3c70"}).`
|
|
1715
|
+
);
|
|
1597
1716
|
}
|
|
1598
1717
|
return serviceWorker;
|
|
1599
1718
|
}
|
|
@@ -1637,10 +1756,12 @@ function createResponseListener(context) {
|
|
|
1637
1756
|
// src/setupWorker/start/utils/validateWorkerScope.ts
|
|
1638
1757
|
function validateWorkerScope(registration, options) {
|
|
1639
1758
|
if (!(options == null ? void 0 : options.quiet) && !location.href.startsWith(registration.scope)) {
|
|
1640
|
-
devUtils.warn(
|
|
1759
|
+
devUtils.warn(
|
|
1760
|
+
`Cannot intercept requests on this page because it's outside of the worker's scope ("${registration.scope}"). If you wish to mock API requests on this page, you must resolve this scope issue.
|
|
1641
1761
|
|
|
1642
1762
|
- (Recommended) Register the worker at the root level ("/") of your application.
|
|
1643
|
-
- Set the "Service-Worker-Allowed" response header to allow out-of-scope workers.`
|
|
1763
|
+
- Set the "Service-Worker-Allowed" response header to allow out-of-scope workers.`
|
|
1764
|
+
);
|
|
1644
1765
|
}
|
|
1645
1766
|
}
|
|
1646
1767
|
|
|
@@ -1649,20 +1770,34 @@ var createStartHandler = (context) => {
|
|
|
1649
1770
|
return function start(options, customOptions) {
|
|
1650
1771
|
const startWorkerInstance = async () => {
|
|
1651
1772
|
context.events.removeAllListeners();
|
|
1652
|
-
context.workerChannel.on(
|
|
1773
|
+
context.workerChannel.on(
|
|
1774
|
+
"REQUEST",
|
|
1775
|
+
createRequestListener(context, options)
|
|
1776
|
+
);
|
|
1653
1777
|
context.workerChannel.on("RESPONSE", createResponseListener(context));
|
|
1654
|
-
const instance = await getWorkerInstance(
|
|
1778
|
+
const instance = await getWorkerInstance(
|
|
1779
|
+
options.serviceWorker.url,
|
|
1780
|
+
options.serviceWorker.options,
|
|
1781
|
+
options.findWorker
|
|
1782
|
+
);
|
|
1655
1783
|
const [worker, registration] = instance;
|
|
1656
1784
|
if (!worker) {
|
|
1657
|
-
const missingWorkerMessage = (customOptions == null ? void 0 : customOptions.findWorker) ? devUtils.formatMessage(
|
|
1785
|
+
const missingWorkerMessage = (customOptions == null ? void 0 : customOptions.findWorker) ? devUtils.formatMessage(
|
|
1786
|
+
`Failed to locate the Service Worker registration using a custom "findWorker" predicate.
|
|
1658
1787
|
|
|
1659
1788
|
Please ensure that the custom predicate properly locates the Service Worker registration at "%s".
|
|
1660
1789
|
More details: https://mswjs.io/docs/api/setup-worker/start#findworker
|
|
1661
|
-
`,
|
|
1790
|
+
`,
|
|
1791
|
+
options.serviceWorker.url
|
|
1792
|
+
) : devUtils.formatMessage(
|
|
1793
|
+
`Failed to locate the Service Worker registration.
|
|
1662
1794
|
|
|
1663
1795
|
This most likely means that the worker script URL "%s" cannot resolve against the actual public hostname (%s). This may happen if your application runs behind a proxy, or has a dynamic hostname.
|
|
1664
1796
|
|
|
1665
|
-
Please consider using a custom "serviceWorker.url" option to point to the actual worker script location, or a custom "findWorker" option to resolve the Service Worker registration manually. More details: https://mswjs.io/docs/api/setup-worker/start`,
|
|
1797
|
+
Please consider using a custom "serviceWorker.url" option to point to the actual worker script location, or a custom "findWorker" option to resolve the Service Worker registration manually. More details: https://mswjs.io/docs/api/setup-worker/start`,
|
|
1798
|
+
options.serviceWorker.url,
|
|
1799
|
+
location.host
|
|
1800
|
+
);
|
|
1666
1801
|
throw new Error(missingWorkerMessage);
|
|
1667
1802
|
}
|
|
1668
1803
|
context.worker = worker;
|
|
@@ -1673,7 +1808,9 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
|
|
|
1673
1808
|
}
|
|
1674
1809
|
window.clearInterval(context.keepAliveInterval);
|
|
1675
1810
|
});
|
|
1676
|
-
const [integrityError] = await (0, import_until4.until)(
|
|
1811
|
+
const [integrityError] = await (0, import_until4.until)(
|
|
1812
|
+
() => requestIntegrityCheck(context, worker)
|
|
1813
|
+
);
|
|
1677
1814
|
if (integrityError) {
|
|
1678
1815
|
devUtils.error(`Detected outdated Service Worker: ${integrityError.message}
|
|
1679
1816
|
|
|
@@ -1684,26 +1821,31 @@ $ npx msw init <PUBLIC_DIR>
|
|
|
1684
1821
|
This is necessary to ensure that the Service Worker is in sync with the library to guarantee its stability.
|
|
1685
1822
|
If this message still persists after updating, please report an issue: https://github.com/open-draft/msw/issues `);
|
|
1686
1823
|
}
|
|
1687
|
-
context.keepAliveInterval = window.setInterval(
|
|
1824
|
+
context.keepAliveInterval = window.setInterval(
|
|
1825
|
+
() => context.workerChannel.send("KEEPALIVE_REQUEST"),
|
|
1826
|
+
5e3
|
|
1827
|
+
);
|
|
1688
1828
|
validateWorkerScope(registration, context.startOptions);
|
|
1689
1829
|
return registration;
|
|
1690
1830
|
};
|
|
1691
|
-
const workerRegistration = startWorkerInstance().then(
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1831
|
+
const workerRegistration = startWorkerInstance().then(
|
|
1832
|
+
async (registration) => {
|
|
1833
|
+
const pendingInstance = registration.installing || registration.waiting;
|
|
1834
|
+
if (pendingInstance) {
|
|
1835
|
+
await new Promise((resolve) => {
|
|
1836
|
+
pendingInstance.addEventListener("statechange", () => {
|
|
1837
|
+
if (pendingInstance.state === "activated") {
|
|
1838
|
+
return resolve();
|
|
1839
|
+
}
|
|
1840
|
+
});
|
|
1699
1841
|
});
|
|
1842
|
+
}
|
|
1843
|
+
await enableMocking(context, options).catch((error2) => {
|
|
1844
|
+
throw new Error(`Failed to enable mocking: ${error2 == null ? void 0 : error2.message}`);
|
|
1700
1845
|
});
|
|
1846
|
+
return registration;
|
|
1701
1847
|
}
|
|
1702
|
-
|
|
1703
|
-
throw new Error(`Failed to enable mocking: ${error2 == null ? void 0 : error2.message}`);
|
|
1704
|
-
});
|
|
1705
|
-
return registration;
|
|
1706
|
-
});
|
|
1848
|
+
);
|
|
1707
1849
|
if (options.waitUntilReady) {
|
|
1708
1850
|
deferNetworkRequestsUntil(workerRegistration);
|
|
1709
1851
|
}
|
|
@@ -1716,7 +1858,10 @@ function printStopMessage(args = {}) {
|
|
|
1716
1858
|
if (args.quiet) {
|
|
1717
1859
|
return;
|
|
1718
1860
|
}
|
|
1719
|
-
console.log(
|
|
1861
|
+
console.log(
|
|
1862
|
+
`%c${devUtils.formatMessage("Mocking disabled.")}`,
|
|
1863
|
+
"color:orangered;font-weight:bold;"
|
|
1864
|
+
);
|
|
1720
1865
|
}
|
|
1721
1866
|
|
|
1722
1867
|
// src/setupWorker/stop/createStop.ts
|
|
@@ -1724,7 +1869,9 @@ var createStop = (context) => {
|
|
|
1724
1869
|
return function stop() {
|
|
1725
1870
|
var _a;
|
|
1726
1871
|
if (!context.isMockingEnabled) {
|
|
1727
|
-
devUtils.warn(
|
|
1872
|
+
devUtils.warn(
|
|
1873
|
+
'Found a redundant "worker.stop()" call. Note that stopping the worker while mocking already stopped has no effect. Consider removing this "worker.stop()" call.'
|
|
1874
|
+
);
|
|
1728
1875
|
return;
|
|
1729
1876
|
}
|
|
1730
1877
|
context.workerChannel.send("MOCK_DEACTIVATE");
|
|
@@ -1759,7 +1906,8 @@ var noop = () => {
|
|
|
1759
1906
|
throw new Error("Not implemented");
|
|
1760
1907
|
};
|
|
1761
1908
|
function createResponseFromIsomorphicResponse(response2) {
|
|
1762
|
-
return
|
|
1909
|
+
return {
|
|
1910
|
+
...response2,
|
|
1763
1911
|
ok: response2.status >= 200 && response2.status < 300,
|
|
1764
1912
|
url: "",
|
|
1765
1913
|
type: "default",
|
|
@@ -1781,7 +1929,7 @@ function createResponseFromIsomorphicResponse(response2) {
|
|
|
1781
1929
|
formData: noop,
|
|
1782
1930
|
blob: noop,
|
|
1783
1931
|
clone: noop
|
|
1784
|
-
}
|
|
1932
|
+
};
|
|
1785
1933
|
}
|
|
1786
1934
|
|
|
1787
1935
|
// src/setupWorker/start/createFallbackRequestListener.ts
|
|
@@ -1791,27 +1939,38 @@ function createFallbackRequestListener(context, options) {
|
|
|
1791
1939
|
interceptors: [new import_fetch3.FetchInterceptor(), new import_XMLHttpRequest.XMLHttpRequestInterceptor()]
|
|
1792
1940
|
});
|
|
1793
1941
|
interceptor.on("request", async (request) => {
|
|
1794
|
-
const mockedRequest = new MockedRequest(request.url,
|
|
1942
|
+
const mockedRequest = new MockedRequest(request.url, {
|
|
1943
|
+
...request,
|
|
1795
1944
|
body: await request.arrayBuffer()
|
|
1796
|
-
})
|
|
1797
|
-
const response2 = await handleRequest(
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1945
|
+
});
|
|
1946
|
+
const response2 = await handleRequest(
|
|
1947
|
+
mockedRequest,
|
|
1948
|
+
context.requestHandlers,
|
|
1949
|
+
options,
|
|
1950
|
+
context.emitter,
|
|
1951
|
+
{
|
|
1952
|
+
transformResponse(response3) {
|
|
1953
|
+
return {
|
|
1954
|
+
status: response3.status,
|
|
1955
|
+
statusText: response3.statusText,
|
|
1956
|
+
headers: response3.headers.all(),
|
|
1957
|
+
body: response3.body,
|
|
1958
|
+
delay: response3.delay
|
|
1959
|
+
};
|
|
1960
|
+
},
|
|
1961
|
+
onMockedResponse(_, { handler, publicRequest, parsedRequest }) {
|
|
1962
|
+
if (!options.quiet) {
|
|
1963
|
+
context.emitter.once("response:mocked", (response3) => {
|
|
1964
|
+
handler.log(
|
|
1965
|
+
publicRequest,
|
|
1966
|
+
serializeResponse(response3),
|
|
1967
|
+
parsedRequest
|
|
1968
|
+
);
|
|
1969
|
+
});
|
|
1970
|
+
}
|
|
1812
1971
|
}
|
|
1813
1972
|
}
|
|
1814
|
-
|
|
1973
|
+
);
|
|
1815
1974
|
if (response2) {
|
|
1816
1975
|
request.respondWith(response2);
|
|
1817
1976
|
}
|
|
@@ -1834,7 +1993,10 @@ function createFallbackRequestListener(context, options) {
|
|
|
1834
1993
|
// src/setupWorker/start/createFallbackStart.ts
|
|
1835
1994
|
function createFallbackStart(context) {
|
|
1836
1995
|
return async function start(options) {
|
|
1837
|
-
context.fallbackInterceptor = createFallbackRequestListener(
|
|
1996
|
+
context.fallbackInterceptor = createFallbackRequestListener(
|
|
1997
|
+
context,
|
|
1998
|
+
options
|
|
1999
|
+
);
|
|
1838
2000
|
printStartMessage({
|
|
1839
2001
|
message: "Mocking enabled (fallback mode).",
|
|
1840
2002
|
quiet: options.quiet
|
|
@@ -1889,7 +2051,13 @@ var SetupApi = class {
|
|
|
1889
2051
|
}
|
|
1890
2052
|
validateHandlers(...handlers) {
|
|
1891
2053
|
for (const handler of handlers) {
|
|
1892
|
-
(0, import_outvariant4.invariant)(
|
|
2054
|
+
(0, import_outvariant4.invariant)(
|
|
2055
|
+
!Array.isArray(handler),
|
|
2056
|
+
devUtils.formatMessage(
|
|
2057
|
+
'Failed to construct "%s" given an Array of request handlers. Make sure you spread the request handlers when calling the respective setup function.'
|
|
2058
|
+
),
|
|
2059
|
+
this.constructor.name
|
|
2060
|
+
);
|
|
1893
2061
|
}
|
|
1894
2062
|
}
|
|
1895
2063
|
dispose() {
|
|
@@ -1931,7 +2099,12 @@ var SetupWorkerApi = class extends SetupApi {
|
|
|
1931
2099
|
super(...handlers);
|
|
1932
2100
|
this.startHandler = null;
|
|
1933
2101
|
this.stopHandler = null;
|
|
1934
|
-
(0, import_outvariant5.invariant)(
|
|
2102
|
+
(0, import_outvariant5.invariant)(
|
|
2103
|
+
!(0, import_is_node_process3.isNodeProcess)(),
|
|
2104
|
+
devUtils.formatMessage(
|
|
2105
|
+
"Failed to execute `setupWorker` in a non-browser environment. Consider using `setupServer` for Node.js environment instead."
|
|
2106
|
+
)
|
|
2107
|
+
);
|
|
1935
2108
|
this.listeners = [];
|
|
1936
2109
|
this.context = this.createWorkerContext();
|
|
1937
2110
|
}
|
|
@@ -1945,18 +2118,22 @@ var SetupWorkerApi = class extends SetupApi {
|
|
|
1945
2118
|
emitter: this.emitter,
|
|
1946
2119
|
workerChannel: {
|
|
1947
2120
|
on: (eventType, callback) => {
|
|
1948
|
-
this.context.events.addListener(
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
2121
|
+
this.context.events.addListener(
|
|
2122
|
+
navigator.serviceWorker,
|
|
2123
|
+
"message",
|
|
2124
|
+
(event) => {
|
|
2125
|
+
if (event.source !== this.context.worker) {
|
|
2126
|
+
return;
|
|
2127
|
+
}
|
|
2128
|
+
const message = event.data;
|
|
2129
|
+
if (!message) {
|
|
2130
|
+
return;
|
|
2131
|
+
}
|
|
2132
|
+
if (message.type === eventType) {
|
|
2133
|
+
callback(event, message);
|
|
2134
|
+
}
|
|
1958
2135
|
}
|
|
1959
|
-
|
|
2136
|
+
);
|
|
1960
2137
|
},
|
|
1961
2138
|
send: (type) => {
|
|
1962
2139
|
var _a;
|
|
@@ -1990,7 +2167,18 @@ var SetupWorkerApi = class extends SetupApi {
|
|
|
1990
2167
|
reject(error2);
|
|
1991
2168
|
}
|
|
1992
2169
|
};
|
|
1993
|
-
bindings.push(
|
|
2170
|
+
bindings.push(
|
|
2171
|
+
this.context.events.addListener(
|
|
2172
|
+
navigator.serviceWorker,
|
|
2173
|
+
"message",
|
|
2174
|
+
handleIncomingMessage
|
|
2175
|
+
),
|
|
2176
|
+
this.context.events.addListener(
|
|
2177
|
+
navigator.serviceWorker,
|
|
2178
|
+
"messageerror",
|
|
2179
|
+
reject
|
|
2180
|
+
)
|
|
2181
|
+
);
|
|
1994
2182
|
}).finally(() => {
|
|
1995
2183
|
bindings.forEach((unbind) => unbind());
|
|
1996
2184
|
});
|
|
@@ -2008,7 +2196,10 @@ var SetupWorkerApi = class extends SetupApi {
|
|
|
2008
2196
|
return context;
|
|
2009
2197
|
}
|
|
2010
2198
|
async start(options = {}) {
|
|
2011
|
-
this.context.startOptions = mergeRight(
|
|
2199
|
+
this.context.startOptions = mergeRight(
|
|
2200
|
+
DEFAULT_START_OPTIONS,
|
|
2201
|
+
options
|
|
2202
|
+
);
|
|
2012
2203
|
return await this.startHandler(this.context.startOptions, options);
|
|
2013
2204
|
}
|
|
2014
2205
|
printHandlers() {
|
|
@@ -2055,12 +2246,22 @@ var rest = {
|
|
|
2055
2246
|
// src/graphql.ts
|
|
2056
2247
|
function createScopedGraphQLHandler(operationType, url) {
|
|
2057
2248
|
return (operationName, resolver) => {
|
|
2058
|
-
return new GraphQLHandler(
|
|
2249
|
+
return new GraphQLHandler(
|
|
2250
|
+
operationType,
|
|
2251
|
+
operationName,
|
|
2252
|
+
url,
|
|
2253
|
+
resolver
|
|
2254
|
+
);
|
|
2059
2255
|
};
|
|
2060
2256
|
}
|
|
2061
2257
|
function createGraphQLOperationHandler(url) {
|
|
2062
2258
|
return (resolver) => {
|
|
2063
|
-
return new GraphQLHandler(
|
|
2259
|
+
return new GraphQLHandler(
|
|
2260
|
+
"all",
|
|
2261
|
+
new RegExp(".*"),
|
|
2262
|
+
url,
|
|
2263
|
+
resolver
|
|
2264
|
+
);
|
|
2064
2265
|
};
|
|
2065
2266
|
}
|
|
2066
2267
|
var standardGraphQLHandlers = {
|
|
@@ -2075,9 +2276,10 @@ function createGraphQLLink(url) {
|
|
|
2075
2276
|
mutation: createScopedGraphQLHandler("mutation", url)
|
|
2076
2277
|
};
|
|
2077
2278
|
}
|
|
2078
|
-
var graphql =
|
|
2279
|
+
var graphql = {
|
|
2280
|
+
...standardGraphQLHandlers,
|
|
2079
2281
|
link: createGraphQLLink
|
|
2080
|
-
}
|
|
2282
|
+
};
|
|
2081
2283
|
|
|
2082
2284
|
// src/index.ts
|
|
2083
2285
|
checkGlobals();
|