msw 0.43.1 → 0.44.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/{glossary-58eca5a8.d.ts → glossary-17949ef9.d.ts} +49 -26
- package/lib/iife/index.js +4722 -2148
- package/lib/iife/index.js.map +1 -1
- package/lib/index.d.ts +11 -14
- package/lib/index.js +247 -248
- package/lib/index.js.map +1 -1
- package/lib/mockServiceWorker.js +3 -3
- package/lib/native/index.d.ts +1 -1
- package/lib/native/index.js +485 -458
- package/lib/native/index.mjs +475 -448
- package/lib/node/index.d.ts +2 -2
- package/lib/node/index.js +485 -458
- package/lib/node/index.js.map +1 -1
- package/lib/node/index.mjs +475 -448
- package/lib/node/index.mjs.map +1 -1
- package/package.json +3 -3
package/lib/node/index.js
CHANGED
|
@@ -67,441 +67,19 @@ var import_XMLHttpRequest = require("@mswjs/interceptors/lib/interceptors/XMLHtt
|
|
|
67
67
|
var import_chalk = require("chalk");
|
|
68
68
|
var import_is_node_process3 = require("is-node-process");
|
|
69
69
|
var import_strict_event_emitter = require("strict-event-emitter");
|
|
70
|
-
var
|
|
71
|
-
|
|
72
|
-
// src/utils/internal/requestHandlerUtils.ts
|
|
73
|
-
function use(currentHandlers, ...handlers) {
|
|
74
|
-
currentHandlers.unshift(...handlers);
|
|
75
|
-
}
|
|
76
|
-
function restoreHandlers(handlers) {
|
|
77
|
-
handlers.forEach((handler) => {
|
|
78
|
-
handler.markAsSkipped(false);
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
function resetHandlers(initialHandlers, ...nextHandlers) {
|
|
82
|
-
return nextHandlers.length > 0 ? [...nextHandlers] : [...initialHandlers];
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// src/handlers/RequestHandler.ts
|
|
86
|
-
var import_headers_polyfill4 = require("headers-polyfill");
|
|
87
|
-
|
|
88
|
-
// src/response.ts
|
|
89
|
-
var import_headers_polyfill = require("headers-polyfill");
|
|
90
|
-
|
|
91
|
-
// src/utils/internal/compose.ts
|
|
92
|
-
function compose(...fns) {
|
|
93
|
-
return (...args) => {
|
|
94
|
-
return fns.reduceRight((leftFn, rightFn) => {
|
|
95
|
-
return leftFn instanceof Promise ? Promise.resolve(leftFn).then(rightFn) : rightFn(leftFn);
|
|
96
|
-
}, args[0]);
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// src/utils/NetworkError.ts
|
|
101
|
-
var NetworkError = class extends Error {
|
|
102
|
-
constructor(message) {
|
|
103
|
-
super(message);
|
|
104
|
-
this.name = "NetworkError";
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
// src/response.ts
|
|
109
|
-
var defaultResponse = {
|
|
110
|
-
status: 200,
|
|
111
|
-
statusText: "OK",
|
|
112
|
-
body: null,
|
|
113
|
-
delay: 0,
|
|
114
|
-
once: false,
|
|
115
|
-
passthrough: false
|
|
116
|
-
};
|
|
117
|
-
var defaultResponseTransformers = [];
|
|
118
|
-
function createResponseComposition(responseOverrides, defaultTransformers = defaultResponseTransformers) {
|
|
119
|
-
return async (...transformers) => {
|
|
120
|
-
const initialResponse = Object.assign({}, defaultResponse, {
|
|
121
|
-
headers: new import_headers_polyfill.Headers({
|
|
122
|
-
"x-powered-by": "msw"
|
|
123
|
-
})
|
|
124
|
-
}, responseOverrides);
|
|
125
|
-
const resolvedTransformers = [
|
|
126
|
-
...defaultTransformers,
|
|
127
|
-
...transformers
|
|
128
|
-
].filter(Boolean);
|
|
129
|
-
const resolvedResponse = resolvedTransformers.length > 0 ? compose(...resolvedTransformers)(initialResponse) : initialResponse;
|
|
130
|
-
return resolvedResponse;
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
var response = Object.assign(createResponseComposition(), {
|
|
134
|
-
once: createResponseComposition({ once: true }),
|
|
135
|
-
networkError(message) {
|
|
136
|
-
throw new NetworkError(message);
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
// src/utils/internal/getCallFrame.ts
|
|
141
|
-
var SOURCE_FRAME = /\/msw\/src\/(.+)/;
|
|
142
|
-
var BUILD_FRAME = /(node_modules)?[\/\\]lib[\/\\](umd|esm|iief|cjs)[\/\\]|^[^\/\\]*$/;
|
|
143
|
-
function getCallFrame(error2) {
|
|
144
|
-
const stack = error2.stack;
|
|
145
|
-
if (!stack) {
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
const frames = stack.split("\n").slice(1);
|
|
149
|
-
const declarationFrame = frames.find((frame) => {
|
|
150
|
-
return !(SOURCE_FRAME.test(frame) || BUILD_FRAME.test(frame));
|
|
151
|
-
});
|
|
152
|
-
if (!declarationFrame) {
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
const declarationPath = declarationFrame.replace(/\s*at [^()]*\(([^)]+)\)/, "$1").replace(/^@/, "");
|
|
156
|
-
return declarationPath;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// src/utils/internal/isIterable.ts
|
|
160
|
-
function isIterable(fn) {
|
|
161
|
-
if (!fn) {
|
|
162
|
-
return false;
|
|
163
|
-
}
|
|
164
|
-
return typeof fn[Symbol.iterator] == "function";
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// src/context/status.ts
|
|
168
|
-
var import_codes = __toESM(require("statuses/codes.json"));
|
|
169
|
-
var status = (statusCode, statusText) => {
|
|
170
|
-
return (res) => {
|
|
171
|
-
res.status = statusCode;
|
|
172
|
-
res.statusText = statusText || import_codes.default[String(statusCode)];
|
|
173
|
-
return res;
|
|
174
|
-
};
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
// src/context/set.ts
|
|
178
|
-
var import_headers_polyfill2 = require("headers-polyfill");
|
|
179
|
-
function set(...args) {
|
|
180
|
-
return (res) => {
|
|
181
|
-
const [name, value] = args;
|
|
182
|
-
if (typeof name === "string") {
|
|
183
|
-
res.headers.append(name, value);
|
|
184
|
-
} else {
|
|
185
|
-
const headers = (0, import_headers_polyfill2.objectToHeaders)(name);
|
|
186
|
-
headers.forEach((value2, name2) => {
|
|
187
|
-
res.headers.append(name2, value2);
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
return res;
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// src/context/delay.ts
|
|
195
|
-
var import_is_node_process = require("is-node-process");
|
|
196
|
-
var SET_TIMEOUT_MAX_ALLOWED_INT = 2147483647;
|
|
197
|
-
var MIN_SERVER_RESPONSE_TIME = 100;
|
|
198
|
-
var MAX_SERVER_RESPONSE_TIME = 400;
|
|
199
|
-
var NODE_SERVER_RESPONSE_TIME = 5;
|
|
200
|
-
var getRandomServerResponseTime = () => {
|
|
201
|
-
if ((0, import_is_node_process.isNodeProcess)()) {
|
|
202
|
-
return NODE_SERVER_RESPONSE_TIME;
|
|
203
|
-
}
|
|
204
|
-
return Math.floor(Math.random() * (MAX_SERVER_RESPONSE_TIME - MIN_SERVER_RESPONSE_TIME) + MIN_SERVER_RESPONSE_TIME);
|
|
205
|
-
};
|
|
206
|
-
var delay = (durationOrMode) => {
|
|
207
|
-
return (res) => {
|
|
208
|
-
let delayTime;
|
|
209
|
-
if (typeof durationOrMode === "string") {
|
|
210
|
-
switch (durationOrMode) {
|
|
211
|
-
case "infinite": {
|
|
212
|
-
delayTime = SET_TIMEOUT_MAX_ALLOWED_INT;
|
|
213
|
-
break;
|
|
214
|
-
}
|
|
215
|
-
case "real": {
|
|
216
|
-
delayTime = getRandomServerResponseTime();
|
|
217
|
-
break;
|
|
218
|
-
}
|
|
219
|
-
default: {
|
|
220
|
-
throw new Error(`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".`);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
} else if (typeof durationOrMode === "undefined") {
|
|
224
|
-
delayTime = getRandomServerResponseTime();
|
|
225
|
-
} else {
|
|
226
|
-
if (durationOrMode > SET_TIMEOUT_MAX_ALLOWED_INT) {
|
|
227
|
-
throw new Error(`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.`);
|
|
228
|
-
}
|
|
229
|
-
delayTime = durationOrMode;
|
|
230
|
-
}
|
|
231
|
-
res.delay = delayTime;
|
|
232
|
-
return res;
|
|
233
|
-
};
|
|
234
|
-
};
|
|
235
|
-
|
|
236
|
-
// src/context/fetch.ts
|
|
237
|
-
var import_is_node_process2 = require("is-node-process");
|
|
238
|
-
var import_headers_polyfill3 = require("headers-polyfill");
|
|
239
|
-
var useFetch = (0, import_is_node_process2.isNodeProcess)() ? require("node-fetch") : window.fetch;
|
|
240
|
-
var augmentRequestInit = (requestInit) => {
|
|
241
|
-
const headers = new import_headers_polyfill3.Headers(requestInit.headers);
|
|
242
|
-
headers.set("x-msw-bypass", "true");
|
|
243
|
-
return __spreadProps(__spreadValues({}, requestInit), {
|
|
244
|
-
headers: headers.all()
|
|
245
|
-
});
|
|
246
|
-
};
|
|
247
|
-
var createFetchRequestParameters = (input) => {
|
|
248
|
-
const { body: body2, method } = input;
|
|
249
|
-
const requestParameters = __spreadProps(__spreadValues({}, input), {
|
|
250
|
-
body: void 0
|
|
251
|
-
});
|
|
252
|
-
if (["GET", "HEAD"].includes(method)) {
|
|
253
|
-
return requestParameters;
|
|
254
|
-
}
|
|
255
|
-
if (typeof body2 === "object" || typeof body2 === "number" || typeof body2 === "boolean") {
|
|
256
|
-
requestParameters.body = JSON.stringify(body2);
|
|
257
|
-
} else {
|
|
258
|
-
requestParameters.body = body2;
|
|
259
|
-
}
|
|
260
|
-
return requestParameters;
|
|
261
|
-
};
|
|
262
|
-
var fetch = (input, requestInit = {}) => {
|
|
263
|
-
if (typeof input === "string") {
|
|
264
|
-
return useFetch(input, augmentRequestInit(requestInit));
|
|
265
|
-
}
|
|
266
|
-
const requestParameters = createFetchRequestParameters(input);
|
|
267
|
-
const derivedRequestInit = augmentRequestInit(requestParameters);
|
|
268
|
-
return useFetch(input.url.href, derivedRequestInit);
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
// src/handlers/RequestHandler.ts
|
|
272
|
-
var defaultContext = {
|
|
273
|
-
status,
|
|
274
|
-
set,
|
|
275
|
-
delay,
|
|
276
|
-
fetch
|
|
277
|
-
};
|
|
278
|
-
var RequestHandler = class {
|
|
279
|
-
constructor(options) {
|
|
280
|
-
this.shouldSkip = false;
|
|
281
|
-
this.ctx = options.ctx || defaultContext;
|
|
282
|
-
this.resolver = options.resolver;
|
|
283
|
-
const callFrame = getCallFrame(new Error());
|
|
284
|
-
this.info = __spreadProps(__spreadValues({}, options.info), {
|
|
285
|
-
callFrame
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
parse(_request, _resolutionContext) {
|
|
289
|
-
return null;
|
|
290
|
-
}
|
|
291
|
-
test(request, resolutionContext) {
|
|
292
|
-
return this.predicate(request, this.parse(request, resolutionContext), resolutionContext);
|
|
293
|
-
}
|
|
294
|
-
getPublicRequest(request, _parsedResult) {
|
|
295
|
-
return request;
|
|
296
|
-
}
|
|
297
|
-
markAsSkipped(shouldSkip = true) {
|
|
298
|
-
this.shouldSkip = shouldSkip;
|
|
299
|
-
}
|
|
300
|
-
async run(request, resolutionContext) {
|
|
301
|
-
if (this.shouldSkip) {
|
|
302
|
-
return null;
|
|
303
|
-
}
|
|
304
|
-
const parsedResult = this.parse(request, resolutionContext);
|
|
305
|
-
const shouldIntercept = this.predicate(request, parsedResult, resolutionContext);
|
|
306
|
-
if (!shouldIntercept) {
|
|
307
|
-
return null;
|
|
308
|
-
}
|
|
309
|
-
const publicRequest = this.getPublicRequest(request, parsedResult);
|
|
310
|
-
const executeResolver = this.wrapResolver(this.resolver);
|
|
311
|
-
const mockedResponse = await executeResolver(publicRequest, response, this.ctx);
|
|
312
|
-
return this.createExecutionResult(parsedResult, publicRequest, mockedResponse);
|
|
313
|
-
}
|
|
314
|
-
wrapResolver(resolver) {
|
|
315
|
-
return async (req, res, ctx) => {
|
|
316
|
-
const result = this.resolverGenerator || await resolver(req, res, ctx);
|
|
317
|
-
if (isIterable(result)) {
|
|
318
|
-
const { value, done } = result[Symbol.iterator]().next();
|
|
319
|
-
const nextResponse = await value;
|
|
320
|
-
if (!nextResponse && done) {
|
|
321
|
-
return this.resolverGeneratorResult;
|
|
322
|
-
}
|
|
323
|
-
if (!this.resolverGenerator) {
|
|
324
|
-
this.resolverGenerator = result;
|
|
325
|
-
}
|
|
326
|
-
this.resolverGeneratorResult = nextResponse;
|
|
327
|
-
return nextResponse;
|
|
328
|
-
}
|
|
329
|
-
return result;
|
|
330
|
-
};
|
|
331
|
-
}
|
|
332
|
-
createExecutionResult(parsedResult, request, response2) {
|
|
333
|
-
return {
|
|
334
|
-
handler: this,
|
|
335
|
-
parsedResult: parsedResult || null,
|
|
336
|
-
request,
|
|
337
|
-
response: response2 || null
|
|
338
|
-
};
|
|
339
|
-
}
|
|
340
|
-
};
|
|
341
|
-
function passthrough() {
|
|
342
|
-
return {
|
|
343
|
-
status: 101,
|
|
344
|
-
statusText: "Continue",
|
|
345
|
-
headers: new import_headers_polyfill4.Headers(),
|
|
346
|
-
body: null,
|
|
347
|
-
passthrough: true,
|
|
348
|
-
once: false
|
|
349
|
-
};
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
// src/utils/internal/jsonParse.ts
|
|
353
|
-
function jsonParse(value) {
|
|
354
|
-
try {
|
|
355
|
-
return JSON.parse(value);
|
|
356
|
-
} catch (error2) {
|
|
357
|
-
return void 0;
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
// src/utils/internal/parseMultipartData.ts
|
|
362
|
-
var import_headers_polyfill5 = require("headers-polyfill");
|
|
363
|
-
function parseContentHeaders(headersString) {
|
|
364
|
-
var _a, _b;
|
|
365
|
-
const headers = (0, import_headers_polyfill5.stringToHeaders)(headersString);
|
|
366
|
-
const contentType = headers.get("content-type") || "text/plain";
|
|
367
|
-
const disposition = headers.get("content-disposition");
|
|
368
|
-
if (!disposition) {
|
|
369
|
-
throw new Error('"Content-Disposition" header is required.');
|
|
370
|
-
}
|
|
371
|
-
const directives = disposition.split(";").reduce((acc, chunk) => {
|
|
372
|
-
const [name2, ...rest] = chunk.trim().split("=");
|
|
373
|
-
acc[name2] = rest.join("=");
|
|
374
|
-
return acc;
|
|
375
|
-
}, {});
|
|
376
|
-
const name = (_a = directives.name) == null ? void 0 : _a.slice(1, -1);
|
|
377
|
-
const filename = (_b = directives.filename) == null ? void 0 : _b.slice(1, -1);
|
|
378
|
-
return {
|
|
379
|
-
name,
|
|
380
|
-
filename,
|
|
381
|
-
contentType
|
|
382
|
-
};
|
|
383
|
-
}
|
|
384
|
-
function parseMultipartData(data2, headers) {
|
|
385
|
-
const contentType = headers == null ? void 0 : headers.get("content-type");
|
|
386
|
-
if (!contentType) {
|
|
387
|
-
return void 0;
|
|
388
|
-
}
|
|
389
|
-
const [, ...directives] = contentType.split(/; */);
|
|
390
|
-
const boundary = directives.filter((d) => d.startsWith("boundary=")).map((s) => s.replace(/^boundary=/, ""))[0];
|
|
391
|
-
if (!boundary) {
|
|
392
|
-
return void 0;
|
|
393
|
-
}
|
|
394
|
-
const boundaryRegExp = new RegExp(`--+${boundary}`);
|
|
395
|
-
const fields = data2.split(boundaryRegExp).filter((chunk) => chunk.startsWith("\r\n") && chunk.endsWith("\r\n")).map((chunk) => chunk.trimStart().replace(/\r\n$/, ""));
|
|
396
|
-
if (!fields.length) {
|
|
397
|
-
return void 0;
|
|
398
|
-
}
|
|
399
|
-
const parsedBody = {};
|
|
400
|
-
try {
|
|
401
|
-
for (const field2 of fields) {
|
|
402
|
-
const [contentHeaders, ...rest] = field2.split("\r\n\r\n");
|
|
403
|
-
const contentBody = rest.join("\r\n\r\n");
|
|
404
|
-
const { contentType: contentType2, filename, name } = parseContentHeaders(contentHeaders);
|
|
405
|
-
const value = filename === void 0 ? contentBody : new File([contentBody], filename, { type: contentType2 });
|
|
406
|
-
const parsedValue = parsedBody[name];
|
|
407
|
-
if (parsedValue === void 0) {
|
|
408
|
-
parsedBody[name] = value;
|
|
409
|
-
} else if (Array.isArray(parsedValue)) {
|
|
410
|
-
parsedBody[name] = [...parsedValue, value];
|
|
411
|
-
} else {
|
|
412
|
-
parsedBody[name] = [parsedValue, value];
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
return parsedBody;
|
|
416
|
-
} catch (error2) {
|
|
417
|
-
return void 0;
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
// src/utils/request/parseBody.ts
|
|
422
|
-
function parseBody(body2, headers) {
|
|
423
|
-
var _a;
|
|
424
|
-
if (!body2) {
|
|
425
|
-
return body2;
|
|
426
|
-
}
|
|
427
|
-
const contentType = ((_a = headers == null ? void 0 : headers.get("content-type")) == null ? void 0 : _a.toLowerCase()) || "";
|
|
428
|
-
const hasMultipartContent = contentType.startsWith("multipart/form-data");
|
|
429
|
-
if (hasMultipartContent && typeof body2 !== "object") {
|
|
430
|
-
return parseMultipartData(body2.toString(), headers) || body2;
|
|
431
|
-
}
|
|
432
|
-
const hasJsonContent = contentType.includes("json");
|
|
433
|
-
if (hasJsonContent && typeof body2 !== "object") {
|
|
434
|
-
return jsonParse(body2.toString()) || body2;
|
|
435
|
-
}
|
|
436
|
-
return body2;
|
|
437
|
-
}
|
|
70
|
+
var import_interceptors2 = require("@mswjs/interceptors");
|
|
438
71
|
|
|
439
|
-
// src/utils/
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
// src/utils/request/getRequestCookies.ts
|
|
444
|
-
var cookieUtils = __toESM(require("cookie"));
|
|
445
|
-
function getAllCookies() {
|
|
446
|
-
return cookieUtils.parse(document.cookie);
|
|
447
|
-
}
|
|
448
|
-
function getRequestCookies(request) {
|
|
449
|
-
if (typeof document === "undefined" || typeof location === "undefined") {
|
|
450
|
-
return {};
|
|
451
|
-
}
|
|
452
|
-
switch (request.credentials) {
|
|
453
|
-
case "same-origin": {
|
|
454
|
-
return location.origin === request.url.origin ? getAllCookies() : {};
|
|
455
|
-
}
|
|
456
|
-
case "include": {
|
|
457
|
-
return getAllCookies();
|
|
458
|
-
}
|
|
459
|
-
default: {
|
|
460
|
-
return {};
|
|
461
|
-
}
|
|
462
|
-
}
|
|
72
|
+
// src/utils/internal/requestHandlerUtils.ts
|
|
73
|
+
function use(currentHandlers, ...handlers) {
|
|
74
|
+
currentHandlers.unshift(...handlers);
|
|
463
75
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
const requestCookiesString = request.headers.get("cookie");
|
|
469
|
-
import_cookies.store.hydrate();
|
|
470
|
-
const cookiesFromStore = Array.from((_a = import_cookies.store.get(__spreadProps(__spreadValues({}, request), { url: request.url.toString() }))) == null ? void 0 : _a.entries()).reduce((cookies, [name, { value }]) => {
|
|
471
|
-
return Object.assign(cookies, { [name.trim()]: value });
|
|
472
|
-
}, {});
|
|
473
|
-
const cookiesFromDocument = getRequestCookies(request);
|
|
474
|
-
const forwardedCookies = __spreadValues(__spreadValues({}, cookiesFromDocument), cookiesFromStore);
|
|
475
|
-
for (const [name, value] of Object.entries(forwardedCookies)) {
|
|
476
|
-
request.headers.append("cookie", `${name}=${value}`);
|
|
477
|
-
}
|
|
478
|
-
const ownCookies = requestCookiesString ? cookieUtils2.parse(requestCookiesString) : {};
|
|
479
|
-
request.cookies = __spreadValues(__spreadValues(__spreadValues({}, request.cookies), forwardedCookies), ownCookies);
|
|
76
|
+
function restoreHandlers(handlers) {
|
|
77
|
+
handlers.forEach((handler) => {
|
|
78
|
+
handler.markAsSkipped(false);
|
|
79
|
+
});
|
|
480
80
|
}
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
function parseIsomorphicRequest(request) {
|
|
484
|
-
const mockedRequest = {
|
|
485
|
-
id: request.id,
|
|
486
|
-
url: request.url,
|
|
487
|
-
method: request.method,
|
|
488
|
-
body: parseBody(request.body, request.headers),
|
|
489
|
-
credentials: request.credentials || "same-origin",
|
|
490
|
-
headers: request.headers,
|
|
491
|
-
cookies: {},
|
|
492
|
-
redirect: "manual",
|
|
493
|
-
referrer: "",
|
|
494
|
-
keepalive: false,
|
|
495
|
-
cache: "default",
|
|
496
|
-
mode: "cors",
|
|
497
|
-
referrerPolicy: "no-referrer",
|
|
498
|
-
integrity: "",
|
|
499
|
-
destination: "document",
|
|
500
|
-
bodyUsed: false,
|
|
501
|
-
passthrough
|
|
502
|
-
};
|
|
503
|
-
setRequestCookies(mockedRequest);
|
|
504
|
-
return mockedRequest;
|
|
81
|
+
function resetHandlers(initialHandlers, ...nextHandlers) {
|
|
82
|
+
return nextHandlers.length > 0 ? [...nextHandlers] : [...initialHandlers];
|
|
505
83
|
}
|
|
506
84
|
|
|
507
85
|
// src/utils/handleRequest.ts
|
|
@@ -584,6 +162,15 @@ var getPublicUrlFromRequest = (request) => {
|
|
|
584
162
|
return request.referrer.startsWith(request.url.origin) ? request.url.pathname : new URL(request.url.pathname, `${request.url.protocol}//${request.url.host}`).href;
|
|
585
163
|
};
|
|
586
164
|
|
|
165
|
+
// src/utils/internal/jsonParse.ts
|
|
166
|
+
function jsonParse(value) {
|
|
167
|
+
try {
|
|
168
|
+
return JSON.parse(value);
|
|
169
|
+
} catch (error2) {
|
|
170
|
+
return void 0;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
587
174
|
// src/utils/internal/parseGraphQLRequest.ts
|
|
588
175
|
function parseDocumentNode(node) {
|
|
589
176
|
var _a;
|
|
@@ -684,11 +271,38 @@ function isStringEqual(actual, expected) {
|
|
|
684
271
|
return actual.toLowerCase() === expected.toLowerCase();
|
|
685
272
|
}
|
|
686
273
|
|
|
274
|
+
// src/context/status.ts
|
|
275
|
+
var import_codes = __toESM(require("statuses/codes.json"));
|
|
276
|
+
var status = (statusCode, statusText) => {
|
|
277
|
+
return (res) => {
|
|
278
|
+
res.status = statusCode;
|
|
279
|
+
res.statusText = statusText || import_codes.default[String(statusCode)];
|
|
280
|
+
return res;
|
|
281
|
+
};
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
// src/context/set.ts
|
|
285
|
+
var import_headers_polyfill = require("headers-polyfill");
|
|
286
|
+
function set(...args) {
|
|
287
|
+
return (res) => {
|
|
288
|
+
const [name, value] = args;
|
|
289
|
+
if (typeof name === "string") {
|
|
290
|
+
res.headers.append(name, value);
|
|
291
|
+
} else {
|
|
292
|
+
const headers = (0, import_headers_polyfill.objectToHeaders)(name);
|
|
293
|
+
headers.forEach((value2, name2) => {
|
|
294
|
+
res.headers.append(name2, value2);
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
return res;
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
687
301
|
// src/context/cookie.ts
|
|
688
|
-
var
|
|
302
|
+
var cookieUtils = __toESM(require("cookie"));
|
|
689
303
|
var cookie = (name, value, options) => {
|
|
690
304
|
return (res) => {
|
|
691
|
-
const serializedCookie =
|
|
305
|
+
const serializedCookie = cookieUtils.serialize(name, value, options);
|
|
692
306
|
res.headers.append("Set-Cookie", serializedCookie);
|
|
693
307
|
if (typeof document !== "undefined") {
|
|
694
308
|
document.cookie = serializedCookie;
|
|
@@ -754,6 +368,48 @@ var extensions = (payload) => {
|
|
|
754
368
|
};
|
|
755
369
|
};
|
|
756
370
|
|
|
371
|
+
// src/context/delay.ts
|
|
372
|
+
var import_is_node_process = require("is-node-process");
|
|
373
|
+
var SET_TIMEOUT_MAX_ALLOWED_INT = 2147483647;
|
|
374
|
+
var MIN_SERVER_RESPONSE_TIME = 100;
|
|
375
|
+
var MAX_SERVER_RESPONSE_TIME = 400;
|
|
376
|
+
var NODE_SERVER_RESPONSE_TIME = 5;
|
|
377
|
+
var getRandomServerResponseTime = () => {
|
|
378
|
+
if ((0, import_is_node_process.isNodeProcess)()) {
|
|
379
|
+
return NODE_SERVER_RESPONSE_TIME;
|
|
380
|
+
}
|
|
381
|
+
return Math.floor(Math.random() * (MAX_SERVER_RESPONSE_TIME - MIN_SERVER_RESPONSE_TIME) + MIN_SERVER_RESPONSE_TIME);
|
|
382
|
+
};
|
|
383
|
+
var delay = (durationOrMode) => {
|
|
384
|
+
return (res) => {
|
|
385
|
+
let delayTime;
|
|
386
|
+
if (typeof durationOrMode === "string") {
|
|
387
|
+
switch (durationOrMode) {
|
|
388
|
+
case "infinite": {
|
|
389
|
+
delayTime = SET_TIMEOUT_MAX_ALLOWED_INT;
|
|
390
|
+
break;
|
|
391
|
+
}
|
|
392
|
+
case "real": {
|
|
393
|
+
delayTime = getRandomServerResponseTime();
|
|
394
|
+
break;
|
|
395
|
+
}
|
|
396
|
+
default: {
|
|
397
|
+
throw new Error(`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".`);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
} else if (typeof durationOrMode === "undefined") {
|
|
401
|
+
delayTime = getRandomServerResponseTime();
|
|
402
|
+
} else {
|
|
403
|
+
if (durationOrMode > SET_TIMEOUT_MAX_ALLOWED_INT) {
|
|
404
|
+
throw new Error(`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.`);
|
|
405
|
+
}
|
|
406
|
+
delayTime = durationOrMode;
|
|
407
|
+
}
|
|
408
|
+
res.delay = delayTime;
|
|
409
|
+
return res;
|
|
410
|
+
};
|
|
411
|
+
};
|
|
412
|
+
|
|
757
413
|
// src/context/errors.ts
|
|
758
414
|
var errors = (errorsList) => {
|
|
759
415
|
return (res) => {
|
|
@@ -766,6 +422,41 @@ var errors = (errorsList) => {
|
|
|
766
422
|
};
|
|
767
423
|
};
|
|
768
424
|
|
|
425
|
+
// src/context/fetch.ts
|
|
426
|
+
var import_is_node_process2 = require("is-node-process");
|
|
427
|
+
var import_headers_polyfill2 = require("headers-polyfill");
|
|
428
|
+
var useFetch = (0, import_is_node_process2.isNodeProcess)() ? require("node-fetch") : window.fetch;
|
|
429
|
+
var augmentRequestInit = (requestInit) => {
|
|
430
|
+
const headers = new import_headers_polyfill2.Headers(requestInit.headers);
|
|
431
|
+
headers.set("x-msw-bypass", "true");
|
|
432
|
+
return __spreadProps(__spreadValues({}, requestInit), {
|
|
433
|
+
headers: headers.all()
|
|
434
|
+
});
|
|
435
|
+
};
|
|
436
|
+
var createFetchRequestParameters = (input) => {
|
|
437
|
+
const { body: body2, method } = input;
|
|
438
|
+
const requestParameters = __spreadProps(__spreadValues({}, input), {
|
|
439
|
+
body: void 0
|
|
440
|
+
});
|
|
441
|
+
if (["GET", "HEAD"].includes(method)) {
|
|
442
|
+
return requestParameters;
|
|
443
|
+
}
|
|
444
|
+
if (typeof body2 === "object" || typeof body2 === "number" || typeof body2 === "boolean") {
|
|
445
|
+
requestParameters.body = JSON.stringify(body2);
|
|
446
|
+
} else {
|
|
447
|
+
requestParameters.body = body2;
|
|
448
|
+
}
|
|
449
|
+
return requestParameters;
|
|
450
|
+
};
|
|
451
|
+
var fetch = (input, requestInit = {}) => {
|
|
452
|
+
if (typeof input === "string") {
|
|
453
|
+
return useFetch(input, augmentRequestInit(requestInit));
|
|
454
|
+
}
|
|
455
|
+
const requestParameters = createFetchRequestParameters(input);
|
|
456
|
+
const derivedRequestInit = augmentRequestInit(requestParameters);
|
|
457
|
+
return useFetch(input.url.href, derivedRequestInit);
|
|
458
|
+
};
|
|
459
|
+
|
|
769
460
|
// src/context/text.ts
|
|
770
461
|
var text = (body2) => {
|
|
771
462
|
return (res) => {
|
|
@@ -789,29 +480,110 @@ function getStatusCodeColor(status2) {
|
|
|
789
480
|
if (status2 < 300) {
|
|
790
481
|
return "#69AB32" /* Success */;
|
|
791
482
|
}
|
|
792
|
-
if (status2 < 400) {
|
|
793
|
-
return "#F0BB4B" /* Warning */;
|
|
483
|
+
if (status2 < 400) {
|
|
484
|
+
return "#F0BB4B" /* Warning */;
|
|
485
|
+
}
|
|
486
|
+
return "#E95F5D" /* Danger */;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// src/utils/logging/getTimestamp.ts
|
|
490
|
+
function getTimestamp() {
|
|
491
|
+
const now = new Date();
|
|
492
|
+
return [now.getHours(), now.getMinutes(), now.getSeconds()].map(String).map((chunk) => chunk.slice(0, 2)).map((chunk) => chunk.padStart(2, "0")).join(":");
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// src/utils/logging/prepareRequest.ts
|
|
496
|
+
function prepareRequest(request) {
|
|
497
|
+
return __spreadProps(__spreadValues({}, request), {
|
|
498
|
+
body: request.body,
|
|
499
|
+
headers: request.headers.all()
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// src/utils/logging/prepareResponse.ts
|
|
504
|
+
var import_headers_polyfill4 = require("headers-polyfill");
|
|
505
|
+
|
|
506
|
+
// src/utils/internal/parseMultipartData.ts
|
|
507
|
+
var import_headers_polyfill3 = require("headers-polyfill");
|
|
508
|
+
function parseContentHeaders(headersString) {
|
|
509
|
+
var _a, _b;
|
|
510
|
+
const headers = (0, import_headers_polyfill3.stringToHeaders)(headersString);
|
|
511
|
+
const contentType = headers.get("content-type") || "text/plain";
|
|
512
|
+
const disposition = headers.get("content-disposition");
|
|
513
|
+
if (!disposition) {
|
|
514
|
+
throw new Error('"Content-Disposition" header is required.');
|
|
515
|
+
}
|
|
516
|
+
const directives = disposition.split(";").reduce((acc, chunk) => {
|
|
517
|
+
const [name2, ...rest] = chunk.trim().split("=");
|
|
518
|
+
acc[name2] = rest.join("=");
|
|
519
|
+
return acc;
|
|
520
|
+
}, {});
|
|
521
|
+
const name = (_a = directives.name) == null ? void 0 : _a.slice(1, -1);
|
|
522
|
+
const filename = (_b = directives.filename) == null ? void 0 : _b.slice(1, -1);
|
|
523
|
+
return {
|
|
524
|
+
name,
|
|
525
|
+
filename,
|
|
526
|
+
contentType
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
function parseMultipartData(data2, headers) {
|
|
530
|
+
const contentType = headers == null ? void 0 : headers.get("content-type");
|
|
531
|
+
if (!contentType) {
|
|
532
|
+
return void 0;
|
|
533
|
+
}
|
|
534
|
+
const [, ...directives] = contentType.split(/; */);
|
|
535
|
+
const boundary = directives.filter((d) => d.startsWith("boundary=")).map((s) => s.replace(/^boundary=/, ""))[0];
|
|
536
|
+
if (!boundary) {
|
|
537
|
+
return void 0;
|
|
538
|
+
}
|
|
539
|
+
const boundaryRegExp = new RegExp(`--+${boundary}`);
|
|
540
|
+
const fields = data2.split(boundaryRegExp).filter((chunk) => chunk.startsWith("\r\n") && chunk.endsWith("\r\n")).map((chunk) => chunk.trimStart().replace(/\r\n$/, ""));
|
|
541
|
+
if (!fields.length) {
|
|
542
|
+
return void 0;
|
|
543
|
+
}
|
|
544
|
+
const parsedBody = {};
|
|
545
|
+
try {
|
|
546
|
+
for (const field2 of fields) {
|
|
547
|
+
const [contentHeaders, ...rest] = field2.split("\r\n\r\n");
|
|
548
|
+
const contentBody = rest.join("\r\n\r\n");
|
|
549
|
+
const { contentType: contentType2, filename, name } = parseContentHeaders(contentHeaders);
|
|
550
|
+
const value = filename === void 0 ? contentBody : new File([contentBody], filename, { type: contentType2 });
|
|
551
|
+
const parsedValue = parsedBody[name];
|
|
552
|
+
if (parsedValue === void 0) {
|
|
553
|
+
parsedBody[name] = value;
|
|
554
|
+
} else if (Array.isArray(parsedValue)) {
|
|
555
|
+
parsedBody[name] = [...parsedValue, value];
|
|
556
|
+
} else {
|
|
557
|
+
parsedBody[name] = [parsedValue, value];
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
return parsedBody;
|
|
561
|
+
} catch (error2) {
|
|
562
|
+
return void 0;
|
|
794
563
|
}
|
|
795
|
-
return "#E95F5D" /* Danger */;
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
// src/utils/logging/getTimestamp.ts
|
|
799
|
-
function getTimestamp() {
|
|
800
|
-
const now = new Date();
|
|
801
|
-
return [now.getHours(), now.getMinutes(), now.getSeconds()].map(String).map((chunk) => chunk.slice(0, 2)).map((chunk) => chunk.padStart(2, "0")).join(":");
|
|
802
564
|
}
|
|
803
565
|
|
|
804
|
-
// src/utils/
|
|
805
|
-
function
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
566
|
+
// src/utils/request/parseBody.ts
|
|
567
|
+
function parseBody(body2, headers) {
|
|
568
|
+
var _a;
|
|
569
|
+
if (!body2) {
|
|
570
|
+
return body2;
|
|
571
|
+
}
|
|
572
|
+
const contentType = ((_a = headers == null ? void 0 : headers.get("content-type")) == null ? void 0 : _a.toLowerCase()) || "";
|
|
573
|
+
const hasMultipartContent = contentType.startsWith("multipart/form-data");
|
|
574
|
+
if (hasMultipartContent && typeof body2 !== "object") {
|
|
575
|
+
return parseMultipartData(body2.toString(), headers) || body2;
|
|
576
|
+
}
|
|
577
|
+
const hasJsonContent = contentType.includes("json");
|
|
578
|
+
if (hasJsonContent && typeof body2 !== "object") {
|
|
579
|
+
return jsonParse(body2.toString()) || body2;
|
|
580
|
+
}
|
|
581
|
+
return body2;
|
|
809
582
|
}
|
|
810
583
|
|
|
811
584
|
// src/utils/logging/prepareResponse.ts
|
|
812
|
-
var import_headers_polyfill6 = require("headers-polyfill");
|
|
813
585
|
function prepareResponse(res) {
|
|
814
|
-
const responseHeaders = (0,
|
|
586
|
+
const responseHeaders = (0, import_headers_polyfill4.objectToHeaders)(res.headers);
|
|
815
587
|
return __spreadProps(__spreadValues({}, res), {
|
|
816
588
|
body: parseBody(res.body, responseHeaders)
|
|
817
589
|
});
|
|
@@ -878,6 +650,248 @@ function matchRequestUrl(url, path, baseUrl) {
|
|
|
878
650
|
};
|
|
879
651
|
}
|
|
880
652
|
|
|
653
|
+
// src/utils/request/MockedRequest.ts
|
|
654
|
+
var cookieUtils3 = __toESM(require("cookie"));
|
|
655
|
+
var import_cookies = require("@mswjs/cookies");
|
|
656
|
+
var import_interceptors = require("@mswjs/interceptors");
|
|
657
|
+
var import_bufferUtils = require("@mswjs/interceptors/lib/utils/bufferUtils");
|
|
658
|
+
var import_lib = require("headers-polyfill/lib");
|
|
659
|
+
|
|
660
|
+
// src/utils/request/getRequestCookies.ts
|
|
661
|
+
var cookieUtils2 = __toESM(require("cookie"));
|
|
662
|
+
function getAllCookies() {
|
|
663
|
+
return cookieUtils2.parse(document.cookie);
|
|
664
|
+
}
|
|
665
|
+
function getRequestCookies(request) {
|
|
666
|
+
if (typeof document === "undefined" || typeof location === "undefined") {
|
|
667
|
+
return {};
|
|
668
|
+
}
|
|
669
|
+
switch (request.credentials) {
|
|
670
|
+
case "same-origin": {
|
|
671
|
+
return location.origin === request.url.origin ? getAllCookies() : {};
|
|
672
|
+
}
|
|
673
|
+
case "include": {
|
|
674
|
+
return getAllCookies();
|
|
675
|
+
}
|
|
676
|
+
default: {
|
|
677
|
+
return {};
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
// src/utils/request/MockedRequest.ts
|
|
683
|
+
var MockedRequest = class extends import_interceptors.IsomorphicRequest {
|
|
684
|
+
constructor(input, init = {}) {
|
|
685
|
+
var __super = (...args) => {
|
|
686
|
+
super(...args);
|
|
687
|
+
};
|
|
688
|
+
if (input instanceof import_interceptors.IsomorphicRequest) {
|
|
689
|
+
__super(input);
|
|
690
|
+
} else {
|
|
691
|
+
__super(input, init);
|
|
692
|
+
}
|
|
693
|
+
if (init.id) {
|
|
694
|
+
this.id = init.id;
|
|
695
|
+
}
|
|
696
|
+
this.cache = init.cache || "default";
|
|
697
|
+
this.destination = init.destination || "";
|
|
698
|
+
this.integrity = init.integrity || "";
|
|
699
|
+
this.keepalive = init.keepalive || false;
|
|
700
|
+
this.mode = init.mode || "cors";
|
|
701
|
+
this.priority = init.priority || "auto";
|
|
702
|
+
this.redirect = init.redirect || "follow";
|
|
703
|
+
this.referrer = init.referrer || "";
|
|
704
|
+
this.referrerPolicy = init.referrerPolicy || "no-referrer";
|
|
705
|
+
this.cookies = init.cookies || this.getCookies();
|
|
706
|
+
}
|
|
707
|
+
get body() {
|
|
708
|
+
const text2 = (0, import_bufferUtils.decodeBuffer)(this["_body"]);
|
|
709
|
+
const body2 = parseBody(text2, this.headers);
|
|
710
|
+
if (isStringEqual(this.method, "GET") && body2 === "") {
|
|
711
|
+
return void 0;
|
|
712
|
+
}
|
|
713
|
+
return body2;
|
|
714
|
+
}
|
|
715
|
+
passthrough() {
|
|
716
|
+
return {
|
|
717
|
+
status: 101,
|
|
718
|
+
statusText: "Continue",
|
|
719
|
+
headers: new import_lib.Headers(),
|
|
720
|
+
body: null,
|
|
721
|
+
passthrough: true,
|
|
722
|
+
once: false
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
getCookies() {
|
|
726
|
+
var _a;
|
|
727
|
+
const requestCookiesString = this.headers.get("cookie");
|
|
728
|
+
const ownCookies = requestCookiesString ? cookieUtils3.parse(requestCookiesString) : {};
|
|
729
|
+
import_cookies.store.hydrate();
|
|
730
|
+
const cookiesFromStore = Array.from((_a = import_cookies.store.get(__spreadProps(__spreadValues({}, this), { url: this.url.href }))) == null ? void 0 : _a.entries()).reduce((cookies, [name, { value }]) => {
|
|
731
|
+
return Object.assign(cookies, { [name.trim()]: value });
|
|
732
|
+
}, {});
|
|
733
|
+
const cookiesFromDocument = getRequestCookies(this);
|
|
734
|
+
const forwardedCookies = __spreadValues(__spreadValues({}, cookiesFromDocument), cookiesFromStore);
|
|
735
|
+
for (const [name, value] of Object.entries(forwardedCookies)) {
|
|
736
|
+
this.headers.append("cookie", `${name}=${value}`);
|
|
737
|
+
}
|
|
738
|
+
return __spreadValues(__spreadValues({}, forwardedCookies), ownCookies);
|
|
739
|
+
}
|
|
740
|
+
};
|
|
741
|
+
|
|
742
|
+
// src/handlers/RequestHandler.ts
|
|
743
|
+
var import_headers_polyfill6 = require("headers-polyfill");
|
|
744
|
+
|
|
745
|
+
// src/response.ts
|
|
746
|
+
var import_headers_polyfill5 = require("headers-polyfill");
|
|
747
|
+
|
|
748
|
+
// src/utils/internal/compose.ts
|
|
749
|
+
function compose(...fns) {
|
|
750
|
+
return (...args) => {
|
|
751
|
+
return fns.reduceRight((leftFn, rightFn) => {
|
|
752
|
+
return leftFn instanceof Promise ? Promise.resolve(leftFn).then(rightFn) : rightFn(leftFn);
|
|
753
|
+
}, args[0]);
|
|
754
|
+
};
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
// src/utils/NetworkError.ts
|
|
758
|
+
var NetworkError = class extends Error {
|
|
759
|
+
constructor(message) {
|
|
760
|
+
super(message);
|
|
761
|
+
this.name = "NetworkError";
|
|
762
|
+
}
|
|
763
|
+
};
|
|
764
|
+
|
|
765
|
+
// src/response.ts
|
|
766
|
+
var defaultResponse = {
|
|
767
|
+
status: 200,
|
|
768
|
+
statusText: "OK",
|
|
769
|
+
body: null,
|
|
770
|
+
delay: 0,
|
|
771
|
+
once: false,
|
|
772
|
+
passthrough: false
|
|
773
|
+
};
|
|
774
|
+
var defaultResponseTransformers = [];
|
|
775
|
+
function createResponseComposition(responseOverrides, defaultTransformers = defaultResponseTransformers) {
|
|
776
|
+
return async (...transformers) => {
|
|
777
|
+
const initialResponse = Object.assign({}, defaultResponse, {
|
|
778
|
+
headers: new import_headers_polyfill5.Headers({
|
|
779
|
+
"x-powered-by": "msw"
|
|
780
|
+
})
|
|
781
|
+
}, responseOverrides);
|
|
782
|
+
const resolvedTransformers = [
|
|
783
|
+
...defaultTransformers,
|
|
784
|
+
...transformers
|
|
785
|
+
].filter(Boolean);
|
|
786
|
+
const resolvedResponse = resolvedTransformers.length > 0 ? compose(...resolvedTransformers)(initialResponse) : initialResponse;
|
|
787
|
+
return resolvedResponse;
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
var response = Object.assign(createResponseComposition(), {
|
|
791
|
+
once: createResponseComposition({ once: true }),
|
|
792
|
+
networkError(message) {
|
|
793
|
+
throw new NetworkError(message);
|
|
794
|
+
}
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
// src/utils/internal/getCallFrame.ts
|
|
798
|
+
var SOURCE_FRAME = /\/msw\/src\/(.+)/;
|
|
799
|
+
var BUILD_FRAME = /(node_modules)?[\/\\]lib[\/\\](umd|esm|iief|cjs)[\/\\]|^[^\/\\]*$/;
|
|
800
|
+
function getCallFrame(error2) {
|
|
801
|
+
const stack = error2.stack;
|
|
802
|
+
if (!stack) {
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
805
|
+
const frames = stack.split("\n").slice(1);
|
|
806
|
+
const declarationFrame = frames.find((frame) => {
|
|
807
|
+
return !(SOURCE_FRAME.test(frame) || BUILD_FRAME.test(frame));
|
|
808
|
+
});
|
|
809
|
+
if (!declarationFrame) {
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
const declarationPath = declarationFrame.replace(/\s*at [^()]*\(([^)]+)\)/, "$1").replace(/^@/, "");
|
|
813
|
+
return declarationPath;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
// src/utils/internal/isIterable.ts
|
|
817
|
+
function isIterable(fn) {
|
|
818
|
+
if (!fn) {
|
|
819
|
+
return false;
|
|
820
|
+
}
|
|
821
|
+
return typeof fn[Symbol.iterator] == "function";
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
// src/handlers/RequestHandler.ts
|
|
825
|
+
var defaultContext = {
|
|
826
|
+
status,
|
|
827
|
+
set,
|
|
828
|
+
delay,
|
|
829
|
+
fetch
|
|
830
|
+
};
|
|
831
|
+
var RequestHandler = class {
|
|
832
|
+
constructor(options) {
|
|
833
|
+
this.shouldSkip = false;
|
|
834
|
+
this.ctx = options.ctx || defaultContext;
|
|
835
|
+
this.resolver = options.resolver;
|
|
836
|
+
const callFrame = getCallFrame(new Error());
|
|
837
|
+
this.info = __spreadProps(__spreadValues({}, options.info), {
|
|
838
|
+
callFrame
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
parse(_request, _resolutionContext) {
|
|
842
|
+
return null;
|
|
843
|
+
}
|
|
844
|
+
test(request, resolutionContext) {
|
|
845
|
+
return this.predicate(request, this.parse(request, resolutionContext), resolutionContext);
|
|
846
|
+
}
|
|
847
|
+
getPublicRequest(request, _parsedResult) {
|
|
848
|
+
return request;
|
|
849
|
+
}
|
|
850
|
+
markAsSkipped(shouldSkip = true) {
|
|
851
|
+
this.shouldSkip = shouldSkip;
|
|
852
|
+
}
|
|
853
|
+
async run(request, resolutionContext) {
|
|
854
|
+
if (this.shouldSkip) {
|
|
855
|
+
return null;
|
|
856
|
+
}
|
|
857
|
+
const parsedResult = this.parse(request, resolutionContext);
|
|
858
|
+
const shouldIntercept = this.predicate(request, parsedResult, resolutionContext);
|
|
859
|
+
if (!shouldIntercept) {
|
|
860
|
+
return null;
|
|
861
|
+
}
|
|
862
|
+
const publicRequest = this.getPublicRequest(request, parsedResult);
|
|
863
|
+
const executeResolver = this.wrapResolver(this.resolver);
|
|
864
|
+
const mockedResponse = await executeResolver(publicRequest, response, this.ctx);
|
|
865
|
+
return this.createExecutionResult(parsedResult, publicRequest, mockedResponse);
|
|
866
|
+
}
|
|
867
|
+
wrapResolver(resolver) {
|
|
868
|
+
return async (req, res, ctx) => {
|
|
869
|
+
const result = this.resolverGenerator || await resolver(req, res, ctx);
|
|
870
|
+
if (isIterable(result)) {
|
|
871
|
+
const { value, done } = result[Symbol.iterator]().next();
|
|
872
|
+
const nextResponse = await value;
|
|
873
|
+
if (!nextResponse && done) {
|
|
874
|
+
return this.resolverGeneratorResult;
|
|
875
|
+
}
|
|
876
|
+
if (!this.resolverGenerator) {
|
|
877
|
+
this.resolverGenerator = result;
|
|
878
|
+
}
|
|
879
|
+
this.resolverGeneratorResult = nextResponse;
|
|
880
|
+
return nextResponse;
|
|
881
|
+
}
|
|
882
|
+
return result;
|
|
883
|
+
};
|
|
884
|
+
}
|
|
885
|
+
createExecutionResult(parsedResult, request, response2) {
|
|
886
|
+
return {
|
|
887
|
+
handler: this,
|
|
888
|
+
parsedResult: parsedResult || null,
|
|
889
|
+
request,
|
|
890
|
+
response: response2 || null
|
|
891
|
+
};
|
|
892
|
+
}
|
|
893
|
+
};
|
|
894
|
+
|
|
881
895
|
// src/handlers/RestHandler.ts
|
|
882
896
|
var restContext = __spreadProps(__spreadValues({}, defaultContext), {
|
|
883
897
|
cookie,
|
|
@@ -886,6 +900,15 @@ var restContext = __spreadProps(__spreadValues({}, defaultContext), {
|
|
|
886
900
|
json,
|
|
887
901
|
xml
|
|
888
902
|
});
|
|
903
|
+
var RestRequest = class extends MockedRequest {
|
|
904
|
+
constructor(request, params) {
|
|
905
|
+
super(request.url, __spreadProps(__spreadValues({}, request), {
|
|
906
|
+
body: request["_body"]
|
|
907
|
+
}));
|
|
908
|
+
this.params = params;
|
|
909
|
+
this.id = request.id;
|
|
910
|
+
}
|
|
911
|
+
};
|
|
889
912
|
var RestHandler = class extends RequestHandler {
|
|
890
913
|
constructor(method, path, resolver) {
|
|
891
914
|
super({
|
|
@@ -919,9 +942,7 @@ var RestHandler = class extends RequestHandler {
|
|
|
919
942
|
return matchRequestUrl(request.url, this.info.path, resolutionContext == null ? void 0 : resolutionContext.baseUrl);
|
|
920
943
|
}
|
|
921
944
|
getPublicRequest(request, parsedResult) {
|
|
922
|
-
return
|
|
923
|
-
params: parsedResult.params || {}
|
|
924
|
-
});
|
|
945
|
+
return new RestRequest(request, parsedResult.params || {});
|
|
925
946
|
}
|
|
926
947
|
predicate(request, parsedResult) {
|
|
927
948
|
const matchesMethod = this.info.method instanceof RegExp ? this.info.method.test(request.method) : isStringEqual(this.info.method, request.method);
|
|
@@ -984,6 +1005,14 @@ function isDocumentNode(value) {
|
|
|
984
1005
|
}
|
|
985
1006
|
return typeof value === "object" && "kind" in value && "definitions" in value;
|
|
986
1007
|
}
|
|
1008
|
+
var GraphQLRequest = class extends MockedRequest {
|
|
1009
|
+
constructor(request, variables) {
|
|
1010
|
+
super(request.url, __spreadProps(__spreadValues({}, request), {
|
|
1011
|
+
body: request["_body"]
|
|
1012
|
+
}));
|
|
1013
|
+
this.variables = variables;
|
|
1014
|
+
}
|
|
1015
|
+
};
|
|
987
1016
|
var GraphQLHandler = class extends RequestHandler {
|
|
988
1017
|
constructor(operationType, operationName, endpoint, resolver) {
|
|
989
1018
|
let resolvedOperationName = operationName;
|
|
@@ -1013,9 +1042,7 @@ var GraphQLHandler = class extends RequestHandler {
|
|
|
1013
1042
|
return tryCatch(() => parseGraphQLRequest(request), (error2) => console.error(error2.message));
|
|
1014
1043
|
}
|
|
1015
1044
|
getPublicRequest(request, parsedResult) {
|
|
1016
|
-
return
|
|
1017
|
-
variables: (parsedResult == null ? void 0 : parsedResult.variables) || {}
|
|
1018
|
-
});
|
|
1045
|
+
return new GraphQLRequest(request, (parsedResult == null ? void 0 : parsedResult.variables) || {});
|
|
1019
1046
|
}
|
|
1020
1047
|
predicate(request, parsedResult) {
|
|
1021
1048
|
if (!parsedResult) {
|
|
@@ -1241,12 +1268,12 @@ function createSetupServer(...interceptors) {
|
|
|
1241
1268
|
throw new Error(devUtils.formatMessage("Failed to execute `setupServer` in the environment that is not Node.js (i.e. a browser). Consider using `setupWorker` instead."));
|
|
1242
1269
|
}
|
|
1243
1270
|
let resolvedOptions = {};
|
|
1244
|
-
const interceptor = new
|
|
1271
|
+
const interceptor = new import_interceptors2.BatchInterceptor({
|
|
1245
1272
|
name: "setup-server",
|
|
1246
1273
|
interceptors: interceptors.map((Interceptor2) => new Interceptor2())
|
|
1247
1274
|
});
|
|
1248
1275
|
interceptor.on("request", async function setupServerListener(request) {
|
|
1249
|
-
const mockedRequest =
|
|
1276
|
+
const mockedRequest = new MockedRequest(request);
|
|
1250
1277
|
const response2 = await handleRequest(mockedRequest, currentHandlers, resolvedOptions, emitter, {
|
|
1251
1278
|
transformResponse(response3) {
|
|
1252
1279
|
return {
|