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