msw 0.42.3 → 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/README.md +10 -1
- package/lib/{glossary-58eca5a8.d.ts → glossary-17949ef9.d.ts} +49 -26
- package/lib/iife/index.js +4777 -2157
- package/lib/iife/index.js.map +1 -1
- package/lib/index.d.ts +11 -14
- package/lib/index.js +310 -265
- package/lib/index.js.map +1 -1
- package/lib/mockServiceWorker.js +163 -134
- package/lib/native/index.d.ts +1 -1
- package/lib/native/index.js +486 -459
- package/lib/native/index.mjs +476 -449
- package/lib/node/index.d.ts +2 -2
- package/lib/node/index.js +486 -459
- package/lib/node/index.js.map +1 -1
- package/lib/node/index.mjs +476 -449
- package/lib/node/index.mjs.map +1 -1
- package/package.json +3 -3
package/lib/node/index.mjs
CHANGED
|
@@ -66,428 +66,6 @@ function resetHandlers(initialHandlers, ...nextHandlers) {
|
|
|
66
66
|
return nextHandlers.length > 0 ? [...nextHandlers] : [...initialHandlers];
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
// src/handlers/RequestHandler.ts
|
|
70
|
-
import { Headers as Headers3 } from "headers-polyfill";
|
|
71
|
-
|
|
72
|
-
// src/response.ts
|
|
73
|
-
import { Headers } from "headers-polyfill";
|
|
74
|
-
|
|
75
|
-
// src/utils/internal/compose.ts
|
|
76
|
-
function compose(...fns) {
|
|
77
|
-
return (...args) => {
|
|
78
|
-
return fns.reduceRight((leftFn, rightFn) => {
|
|
79
|
-
return leftFn instanceof Promise ? Promise.resolve(leftFn).then(rightFn) : rightFn(leftFn);
|
|
80
|
-
}, args[0]);
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// src/utils/NetworkError.ts
|
|
85
|
-
var NetworkError = class extends Error {
|
|
86
|
-
constructor(message) {
|
|
87
|
-
super(message);
|
|
88
|
-
this.name = "NetworkError";
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
// src/response.ts
|
|
93
|
-
var defaultResponse = {
|
|
94
|
-
status: 200,
|
|
95
|
-
statusText: "OK",
|
|
96
|
-
body: null,
|
|
97
|
-
delay: 0,
|
|
98
|
-
once: false,
|
|
99
|
-
passthrough: false
|
|
100
|
-
};
|
|
101
|
-
var defaultResponseTransformers = [];
|
|
102
|
-
function createResponseComposition(responseOverrides, defaultTransformers = defaultResponseTransformers) {
|
|
103
|
-
return async (...transformers) => {
|
|
104
|
-
const initialResponse = Object.assign({}, defaultResponse, {
|
|
105
|
-
headers: new Headers({
|
|
106
|
-
"x-powered-by": "msw"
|
|
107
|
-
})
|
|
108
|
-
}, responseOverrides);
|
|
109
|
-
const resolvedTransformers = [
|
|
110
|
-
...defaultTransformers,
|
|
111
|
-
...transformers
|
|
112
|
-
].filter(Boolean);
|
|
113
|
-
const resolvedResponse = resolvedTransformers.length > 0 ? compose(...resolvedTransformers)(initialResponse) : initialResponse;
|
|
114
|
-
return resolvedResponse;
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
var response = Object.assign(createResponseComposition(), {
|
|
118
|
-
once: createResponseComposition({ once: true }),
|
|
119
|
-
networkError(message) {
|
|
120
|
-
throw new NetworkError(message);
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
// src/utils/internal/getCallFrame.ts
|
|
125
|
-
var SOURCE_FRAME = /\/msw\/src\/(.+)/;
|
|
126
|
-
var BUILD_FRAME = /(node_modules)?[\/\\]lib[\/\\](umd|esm|iief|cjs)[\/\\]|^[^\/\\]*$/;
|
|
127
|
-
function getCallFrame(error2) {
|
|
128
|
-
const stack = error2.stack;
|
|
129
|
-
if (!stack) {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
const frames = stack.split("\n").slice(1);
|
|
133
|
-
const declarationFrame = frames.find((frame) => {
|
|
134
|
-
return !(SOURCE_FRAME.test(frame) || BUILD_FRAME.test(frame));
|
|
135
|
-
});
|
|
136
|
-
if (!declarationFrame) {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
const declarationPath = declarationFrame.replace(/\s*at [^()]*\(([^)]+)\)/, "$1").replace(/^@/, "");
|
|
140
|
-
return declarationPath;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// src/utils/internal/isIterable.ts
|
|
144
|
-
function isIterable(fn) {
|
|
145
|
-
if (!fn) {
|
|
146
|
-
return false;
|
|
147
|
-
}
|
|
148
|
-
return typeof fn[Symbol.iterator] == "function";
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// src/context/status.ts
|
|
152
|
-
import statuses from "statuses/codes.json";
|
|
153
|
-
var status = (statusCode, statusText) => {
|
|
154
|
-
return (res) => {
|
|
155
|
-
res.status = statusCode;
|
|
156
|
-
res.statusText = statusText || statuses[String(statusCode)];
|
|
157
|
-
return res;
|
|
158
|
-
};
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
// src/context/set.ts
|
|
162
|
-
import { objectToHeaders } from "headers-polyfill";
|
|
163
|
-
function set(...args) {
|
|
164
|
-
return (res) => {
|
|
165
|
-
const [name, value] = args;
|
|
166
|
-
if (typeof name === "string") {
|
|
167
|
-
res.headers.append(name, value);
|
|
168
|
-
} else {
|
|
169
|
-
const headers = objectToHeaders(name);
|
|
170
|
-
headers.forEach((value2, name2) => {
|
|
171
|
-
res.headers.append(name2, value2);
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
return res;
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// src/context/delay.ts
|
|
179
|
-
import { isNodeProcess } from "is-node-process";
|
|
180
|
-
var SET_TIMEOUT_MAX_ALLOWED_INT = 2147483647;
|
|
181
|
-
var MIN_SERVER_RESPONSE_TIME = 100;
|
|
182
|
-
var MAX_SERVER_RESPONSE_TIME = 400;
|
|
183
|
-
var NODE_SERVER_RESPONSE_TIME = 5;
|
|
184
|
-
var getRandomServerResponseTime = () => {
|
|
185
|
-
if (isNodeProcess()) {
|
|
186
|
-
return NODE_SERVER_RESPONSE_TIME;
|
|
187
|
-
}
|
|
188
|
-
return Math.floor(Math.random() * (MAX_SERVER_RESPONSE_TIME - MIN_SERVER_RESPONSE_TIME) + MIN_SERVER_RESPONSE_TIME);
|
|
189
|
-
};
|
|
190
|
-
var delay = (durationOrMode) => {
|
|
191
|
-
return (res) => {
|
|
192
|
-
let delayTime;
|
|
193
|
-
if (typeof durationOrMode === "string") {
|
|
194
|
-
switch (durationOrMode) {
|
|
195
|
-
case "infinite": {
|
|
196
|
-
delayTime = SET_TIMEOUT_MAX_ALLOWED_INT;
|
|
197
|
-
break;
|
|
198
|
-
}
|
|
199
|
-
case "real": {
|
|
200
|
-
delayTime = getRandomServerResponseTime();
|
|
201
|
-
break;
|
|
202
|
-
}
|
|
203
|
-
default: {
|
|
204
|
-
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".`);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
} else if (typeof durationOrMode === "undefined") {
|
|
208
|
-
delayTime = getRandomServerResponseTime();
|
|
209
|
-
} else {
|
|
210
|
-
if (durationOrMode > SET_TIMEOUT_MAX_ALLOWED_INT) {
|
|
211
|
-
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.`);
|
|
212
|
-
}
|
|
213
|
-
delayTime = durationOrMode;
|
|
214
|
-
}
|
|
215
|
-
res.delay = delayTime;
|
|
216
|
-
return res;
|
|
217
|
-
};
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
// src/context/fetch.ts
|
|
221
|
-
import { isNodeProcess as isNodeProcess2 } from "is-node-process";
|
|
222
|
-
import { Headers as Headers2 } from "headers-polyfill";
|
|
223
|
-
var useFetch = isNodeProcess2() ? __require("node-fetch") : window.fetch;
|
|
224
|
-
var augmentRequestInit = (requestInit) => {
|
|
225
|
-
const headers = new Headers2(requestInit.headers);
|
|
226
|
-
headers.set("x-msw-bypass", "true");
|
|
227
|
-
return __spreadProps(__spreadValues({}, requestInit), {
|
|
228
|
-
headers: headers.all()
|
|
229
|
-
});
|
|
230
|
-
};
|
|
231
|
-
var createFetchRequestParameters = (input) => {
|
|
232
|
-
const { body: body2, method } = input;
|
|
233
|
-
const requestParameters = __spreadProps(__spreadValues({}, input), {
|
|
234
|
-
body: void 0
|
|
235
|
-
});
|
|
236
|
-
if (["GET", "HEAD"].includes(method)) {
|
|
237
|
-
return requestParameters;
|
|
238
|
-
}
|
|
239
|
-
if (typeof body2 === "object" || typeof body2 === "number" || typeof body2 === "boolean") {
|
|
240
|
-
requestParameters.body = JSON.stringify(body2);
|
|
241
|
-
} else {
|
|
242
|
-
requestParameters.body = body2;
|
|
243
|
-
}
|
|
244
|
-
return requestParameters;
|
|
245
|
-
};
|
|
246
|
-
var fetch = (input, requestInit = {}) => {
|
|
247
|
-
if (typeof input === "string") {
|
|
248
|
-
return useFetch(input, augmentRequestInit(requestInit));
|
|
249
|
-
}
|
|
250
|
-
const requestParameters = createFetchRequestParameters(input);
|
|
251
|
-
const derivedRequestInit = augmentRequestInit(requestParameters);
|
|
252
|
-
return useFetch(input.url.href, derivedRequestInit);
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
// src/handlers/RequestHandler.ts
|
|
256
|
-
var defaultContext = {
|
|
257
|
-
status,
|
|
258
|
-
set,
|
|
259
|
-
delay,
|
|
260
|
-
fetch
|
|
261
|
-
};
|
|
262
|
-
var RequestHandler = class {
|
|
263
|
-
constructor(options) {
|
|
264
|
-
this.shouldSkip = false;
|
|
265
|
-
this.ctx = options.ctx || defaultContext;
|
|
266
|
-
this.resolver = options.resolver;
|
|
267
|
-
const callFrame = getCallFrame(new Error());
|
|
268
|
-
this.info = __spreadProps(__spreadValues({}, options.info), {
|
|
269
|
-
callFrame
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
parse(_request, _resolutionContext) {
|
|
273
|
-
return null;
|
|
274
|
-
}
|
|
275
|
-
test(request, resolutionContext) {
|
|
276
|
-
return this.predicate(request, this.parse(request, resolutionContext), resolutionContext);
|
|
277
|
-
}
|
|
278
|
-
getPublicRequest(request, _parsedResult) {
|
|
279
|
-
return request;
|
|
280
|
-
}
|
|
281
|
-
markAsSkipped(shouldSkip = true) {
|
|
282
|
-
this.shouldSkip = shouldSkip;
|
|
283
|
-
}
|
|
284
|
-
async run(request, resolutionContext) {
|
|
285
|
-
if (this.shouldSkip) {
|
|
286
|
-
return null;
|
|
287
|
-
}
|
|
288
|
-
const parsedResult = this.parse(request, resolutionContext);
|
|
289
|
-
const shouldIntercept = this.predicate(request, parsedResult, resolutionContext);
|
|
290
|
-
if (!shouldIntercept) {
|
|
291
|
-
return null;
|
|
292
|
-
}
|
|
293
|
-
const publicRequest = this.getPublicRequest(request, parsedResult);
|
|
294
|
-
const executeResolver = this.wrapResolver(this.resolver);
|
|
295
|
-
const mockedResponse = await executeResolver(publicRequest, response, this.ctx);
|
|
296
|
-
return this.createExecutionResult(parsedResult, publicRequest, mockedResponse);
|
|
297
|
-
}
|
|
298
|
-
wrapResolver(resolver) {
|
|
299
|
-
return async (req, res, ctx) => {
|
|
300
|
-
const result = this.resolverGenerator || await resolver(req, res, ctx);
|
|
301
|
-
if (isIterable(result)) {
|
|
302
|
-
const { value, done } = result[Symbol.iterator]().next();
|
|
303
|
-
const nextResponse = await value;
|
|
304
|
-
if (!nextResponse && done) {
|
|
305
|
-
return this.resolverGeneratorResult;
|
|
306
|
-
}
|
|
307
|
-
if (!this.resolverGenerator) {
|
|
308
|
-
this.resolverGenerator = result;
|
|
309
|
-
}
|
|
310
|
-
this.resolverGeneratorResult = nextResponse;
|
|
311
|
-
return nextResponse;
|
|
312
|
-
}
|
|
313
|
-
return result;
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
createExecutionResult(parsedResult, request, response2) {
|
|
317
|
-
return {
|
|
318
|
-
handler: this,
|
|
319
|
-
parsedResult: parsedResult || null,
|
|
320
|
-
request,
|
|
321
|
-
response: response2 || null
|
|
322
|
-
};
|
|
323
|
-
}
|
|
324
|
-
};
|
|
325
|
-
function passthrough() {
|
|
326
|
-
return {
|
|
327
|
-
status: 101,
|
|
328
|
-
statusText: "Continue",
|
|
329
|
-
headers: new Headers3(),
|
|
330
|
-
body: null,
|
|
331
|
-
passthrough: true,
|
|
332
|
-
once: false
|
|
333
|
-
};
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
// src/utils/internal/jsonParse.ts
|
|
337
|
-
function jsonParse(value) {
|
|
338
|
-
try {
|
|
339
|
-
return JSON.parse(value);
|
|
340
|
-
} catch (error2) {
|
|
341
|
-
return void 0;
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
// src/utils/internal/parseMultipartData.ts
|
|
346
|
-
import { stringToHeaders } from "headers-polyfill";
|
|
347
|
-
function parseContentHeaders(headersString) {
|
|
348
|
-
var _a, _b;
|
|
349
|
-
const headers = stringToHeaders(headersString);
|
|
350
|
-
const contentType = headers.get("content-type") || "text/plain";
|
|
351
|
-
const disposition = headers.get("content-disposition");
|
|
352
|
-
if (!disposition) {
|
|
353
|
-
throw new Error('"Content-Disposition" header is required.');
|
|
354
|
-
}
|
|
355
|
-
const directives = disposition.split(";").reduce((acc, chunk) => {
|
|
356
|
-
const [name2, ...rest] = chunk.trim().split("=");
|
|
357
|
-
acc[name2] = rest.join("=");
|
|
358
|
-
return acc;
|
|
359
|
-
}, {});
|
|
360
|
-
const name = (_a = directives.name) == null ? void 0 : _a.slice(1, -1);
|
|
361
|
-
const filename = (_b = directives.filename) == null ? void 0 : _b.slice(1, -1);
|
|
362
|
-
return {
|
|
363
|
-
name,
|
|
364
|
-
filename,
|
|
365
|
-
contentType
|
|
366
|
-
};
|
|
367
|
-
}
|
|
368
|
-
function parseMultipartData(data2, headers) {
|
|
369
|
-
const contentType = headers == null ? void 0 : headers.get("content-type");
|
|
370
|
-
if (!contentType) {
|
|
371
|
-
return void 0;
|
|
372
|
-
}
|
|
373
|
-
const [, ...directives] = contentType.split(/; */);
|
|
374
|
-
const boundary = directives.filter((d) => d.startsWith("boundary=")).map((s) => s.replace(/^boundary=/, ""))[0];
|
|
375
|
-
if (!boundary) {
|
|
376
|
-
return void 0;
|
|
377
|
-
}
|
|
378
|
-
const boundaryRegExp = new RegExp(`--+${boundary}`);
|
|
379
|
-
const fields = data2.split(boundaryRegExp).filter((chunk) => chunk.startsWith("\r\n") && chunk.endsWith("\r\n")).map((chunk) => chunk.trimStart().replace(/\r\n$/, ""));
|
|
380
|
-
if (!fields.length) {
|
|
381
|
-
return void 0;
|
|
382
|
-
}
|
|
383
|
-
const parsedBody = {};
|
|
384
|
-
try {
|
|
385
|
-
for (const field2 of fields) {
|
|
386
|
-
const [contentHeaders, ...rest] = field2.split("\r\n\r\n");
|
|
387
|
-
const contentBody = rest.join("\r\n\r\n");
|
|
388
|
-
const { contentType: contentType2, filename, name } = parseContentHeaders(contentHeaders);
|
|
389
|
-
const value = filename === void 0 ? contentBody : new File([contentBody], filename, { type: contentType2 });
|
|
390
|
-
const parsedValue = parsedBody[name];
|
|
391
|
-
if (parsedValue === void 0) {
|
|
392
|
-
parsedBody[name] = value;
|
|
393
|
-
} else if (Array.isArray(parsedValue)) {
|
|
394
|
-
parsedBody[name] = [...parsedValue, value];
|
|
395
|
-
} else {
|
|
396
|
-
parsedBody[name] = [parsedValue, value];
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
return parsedBody;
|
|
400
|
-
} catch (error2) {
|
|
401
|
-
return void 0;
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
// src/utils/request/parseBody.ts
|
|
406
|
-
function parseBody(body2, headers) {
|
|
407
|
-
var _a;
|
|
408
|
-
if (!body2) {
|
|
409
|
-
return body2;
|
|
410
|
-
}
|
|
411
|
-
const contentType = ((_a = headers == null ? void 0 : headers.get("content-type")) == null ? void 0 : _a.toLowerCase()) || "";
|
|
412
|
-
const hasMultipartContent = contentType.startsWith("multipart/form-data");
|
|
413
|
-
if (hasMultipartContent && typeof body2 !== "object") {
|
|
414
|
-
return parseMultipartData(body2.toString(), headers) || body2;
|
|
415
|
-
}
|
|
416
|
-
const hasJsonContent = contentType.includes("json");
|
|
417
|
-
if (hasJsonContent && typeof body2 !== "object") {
|
|
418
|
-
return jsonParse(body2.toString()) || body2;
|
|
419
|
-
}
|
|
420
|
-
return body2;
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
// src/utils/request/setRequestCookies.ts
|
|
424
|
-
import * as cookieUtils2 from "cookie";
|
|
425
|
-
import { store } from "@mswjs/cookies";
|
|
426
|
-
|
|
427
|
-
// src/utils/request/getRequestCookies.ts
|
|
428
|
-
import * as cookieUtils from "cookie";
|
|
429
|
-
function getAllCookies() {
|
|
430
|
-
return cookieUtils.parse(document.cookie);
|
|
431
|
-
}
|
|
432
|
-
function getRequestCookies(request) {
|
|
433
|
-
if (typeof document === "undefined" || typeof location === "undefined") {
|
|
434
|
-
return {};
|
|
435
|
-
}
|
|
436
|
-
switch (request.credentials) {
|
|
437
|
-
case "same-origin": {
|
|
438
|
-
return location.origin === request.url.origin ? getAllCookies() : {};
|
|
439
|
-
}
|
|
440
|
-
case "include": {
|
|
441
|
-
return getAllCookies();
|
|
442
|
-
}
|
|
443
|
-
default: {
|
|
444
|
-
return {};
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
// src/utils/request/setRequestCookies.ts
|
|
450
|
-
function setRequestCookies(request) {
|
|
451
|
-
var _a;
|
|
452
|
-
const requestCookiesString = request.headers.get("cookie");
|
|
453
|
-
store.hydrate();
|
|
454
|
-
const cookiesFromStore = Array.from((_a = store.get(__spreadProps(__spreadValues({}, request), { url: request.url.toString() }))) == null ? void 0 : _a.entries()).reduce((cookies, [name, { value }]) => {
|
|
455
|
-
return Object.assign(cookies, { [name.trim()]: value });
|
|
456
|
-
}, {});
|
|
457
|
-
const cookiesFromDocument = getRequestCookies(request);
|
|
458
|
-
const forwardedCookies = __spreadValues(__spreadValues({}, cookiesFromDocument), cookiesFromStore);
|
|
459
|
-
for (const [name, value] of Object.entries(forwardedCookies)) {
|
|
460
|
-
request.headers.append("cookie", `${name}=${value}`);
|
|
461
|
-
}
|
|
462
|
-
const ownCookies = requestCookiesString ? cookieUtils2.parse(requestCookiesString) : {};
|
|
463
|
-
request.cookies = __spreadValues(__spreadValues(__spreadValues({}, request.cookies), forwardedCookies), ownCookies);
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
// src/utils/request/parseIsomorphicRequest.ts
|
|
467
|
-
function parseIsomorphicRequest(request) {
|
|
468
|
-
const mockedRequest = {
|
|
469
|
-
id: request.id,
|
|
470
|
-
url: request.url,
|
|
471
|
-
method: request.method,
|
|
472
|
-
body: parseBody(request.body, request.headers),
|
|
473
|
-
credentials: request.credentials || "same-origin",
|
|
474
|
-
headers: request.headers,
|
|
475
|
-
cookies: {},
|
|
476
|
-
redirect: "manual",
|
|
477
|
-
referrer: "",
|
|
478
|
-
keepalive: false,
|
|
479
|
-
cache: "default",
|
|
480
|
-
mode: "cors",
|
|
481
|
-
referrerPolicy: "no-referrer",
|
|
482
|
-
integrity: "",
|
|
483
|
-
destination: "document",
|
|
484
|
-
bodyUsed: false,
|
|
485
|
-
passthrough
|
|
486
|
-
};
|
|
487
|
-
setRequestCookies(mockedRequest);
|
|
488
|
-
return mockedRequest;
|
|
489
|
-
}
|
|
490
|
-
|
|
491
69
|
// src/utils/handleRequest.ts
|
|
492
70
|
import { until } from "@open-draft/until";
|
|
493
71
|
|
|
@@ -562,7 +140,7 @@ import getStringMatchScore from "js-levenshtein";
|
|
|
562
140
|
|
|
563
141
|
// src/utils/internal/parseGraphQLRequest.ts
|
|
564
142
|
import {
|
|
565
|
-
parse
|
|
143
|
+
parse
|
|
566
144
|
} from "graphql";
|
|
567
145
|
|
|
568
146
|
// src/utils/request/getPublicUrlFromRequest.ts
|
|
@@ -570,6 +148,15 @@ var getPublicUrlFromRequest = (request) => {
|
|
|
570
148
|
return request.referrer.startsWith(request.url.origin) ? request.url.pathname : new URL(request.url.pathname, `${request.url.protocol}//${request.url.host}`).href;
|
|
571
149
|
};
|
|
572
150
|
|
|
151
|
+
// src/utils/internal/jsonParse.ts
|
|
152
|
+
function jsonParse(value) {
|
|
153
|
+
try {
|
|
154
|
+
return JSON.parse(value);
|
|
155
|
+
} catch (error2) {
|
|
156
|
+
return void 0;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
573
160
|
// src/utils/internal/parseGraphQLRequest.ts
|
|
574
161
|
function parseDocumentNode(node) {
|
|
575
162
|
var _a;
|
|
@@ -583,7 +170,7 @@ function parseDocumentNode(node) {
|
|
|
583
170
|
}
|
|
584
171
|
function parseQuery(query) {
|
|
585
172
|
try {
|
|
586
|
-
const ast =
|
|
173
|
+
const ast = parse(query);
|
|
587
174
|
return parseDocumentNode(ast);
|
|
588
175
|
} catch (error2) {
|
|
589
176
|
return error2;
|
|
@@ -670,12 +257,39 @@ function isStringEqual(actual, expected) {
|
|
|
670
257
|
return actual.toLowerCase() === expected.toLowerCase();
|
|
671
258
|
}
|
|
672
259
|
|
|
260
|
+
// src/context/status.ts
|
|
261
|
+
import statuses from "statuses/codes.json";
|
|
262
|
+
var status = (statusCode, statusText) => {
|
|
263
|
+
return (res) => {
|
|
264
|
+
res.status = statusCode;
|
|
265
|
+
res.statusText = statusText || statuses[String(statusCode)];
|
|
266
|
+
return res;
|
|
267
|
+
};
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
// src/context/set.ts
|
|
271
|
+
import { objectToHeaders } from "headers-polyfill";
|
|
272
|
+
function set(...args) {
|
|
273
|
+
return (res) => {
|
|
274
|
+
const [name, value] = args;
|
|
275
|
+
if (typeof name === "string") {
|
|
276
|
+
res.headers.append(name, value);
|
|
277
|
+
} else {
|
|
278
|
+
const headers = objectToHeaders(name);
|
|
279
|
+
headers.forEach((value2, name2) => {
|
|
280
|
+
res.headers.append(name2, value2);
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
return res;
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
673
287
|
// src/context/cookie.ts
|
|
674
|
-
import * as
|
|
288
|
+
import * as cookieUtils from "cookie";
|
|
675
289
|
var cookie = (name, value, options) => {
|
|
676
290
|
return (res) => {
|
|
677
|
-
const serializedCookie =
|
|
678
|
-
res.headers.
|
|
291
|
+
const serializedCookie = cookieUtils.serialize(name, value, options);
|
|
292
|
+
res.headers.append("Set-Cookie", serializedCookie);
|
|
679
293
|
if (typeof document !== "undefined") {
|
|
680
294
|
document.cookie = serializedCookie;
|
|
681
295
|
}
|
|
@@ -740,6 +354,48 @@ var extensions = (payload) => {
|
|
|
740
354
|
};
|
|
741
355
|
};
|
|
742
356
|
|
|
357
|
+
// src/context/delay.ts
|
|
358
|
+
import { isNodeProcess } from "is-node-process";
|
|
359
|
+
var SET_TIMEOUT_MAX_ALLOWED_INT = 2147483647;
|
|
360
|
+
var MIN_SERVER_RESPONSE_TIME = 100;
|
|
361
|
+
var MAX_SERVER_RESPONSE_TIME = 400;
|
|
362
|
+
var NODE_SERVER_RESPONSE_TIME = 5;
|
|
363
|
+
var getRandomServerResponseTime = () => {
|
|
364
|
+
if (isNodeProcess()) {
|
|
365
|
+
return NODE_SERVER_RESPONSE_TIME;
|
|
366
|
+
}
|
|
367
|
+
return Math.floor(Math.random() * (MAX_SERVER_RESPONSE_TIME - MIN_SERVER_RESPONSE_TIME) + MIN_SERVER_RESPONSE_TIME);
|
|
368
|
+
};
|
|
369
|
+
var delay = (durationOrMode) => {
|
|
370
|
+
return (res) => {
|
|
371
|
+
let delayTime;
|
|
372
|
+
if (typeof durationOrMode === "string") {
|
|
373
|
+
switch (durationOrMode) {
|
|
374
|
+
case "infinite": {
|
|
375
|
+
delayTime = SET_TIMEOUT_MAX_ALLOWED_INT;
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
case "real": {
|
|
379
|
+
delayTime = getRandomServerResponseTime();
|
|
380
|
+
break;
|
|
381
|
+
}
|
|
382
|
+
default: {
|
|
383
|
+
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".`);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
} else if (typeof durationOrMode === "undefined") {
|
|
387
|
+
delayTime = getRandomServerResponseTime();
|
|
388
|
+
} else {
|
|
389
|
+
if (durationOrMode > SET_TIMEOUT_MAX_ALLOWED_INT) {
|
|
390
|
+
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.`);
|
|
391
|
+
}
|
|
392
|
+
delayTime = durationOrMode;
|
|
393
|
+
}
|
|
394
|
+
res.delay = delayTime;
|
|
395
|
+
return res;
|
|
396
|
+
};
|
|
397
|
+
};
|
|
398
|
+
|
|
743
399
|
// src/context/errors.ts
|
|
744
400
|
var errors = (errorsList) => {
|
|
745
401
|
return (res) => {
|
|
@@ -752,6 +408,41 @@ var errors = (errorsList) => {
|
|
|
752
408
|
};
|
|
753
409
|
};
|
|
754
410
|
|
|
411
|
+
// src/context/fetch.ts
|
|
412
|
+
import { isNodeProcess as isNodeProcess2 } from "is-node-process";
|
|
413
|
+
import { Headers } from "headers-polyfill";
|
|
414
|
+
var useFetch = isNodeProcess2() ? __require("node-fetch") : window.fetch;
|
|
415
|
+
var augmentRequestInit = (requestInit) => {
|
|
416
|
+
const headers = new Headers(requestInit.headers);
|
|
417
|
+
headers.set("x-msw-bypass", "true");
|
|
418
|
+
return __spreadProps(__spreadValues({}, requestInit), {
|
|
419
|
+
headers: headers.all()
|
|
420
|
+
});
|
|
421
|
+
};
|
|
422
|
+
var createFetchRequestParameters = (input) => {
|
|
423
|
+
const { body: body2, method } = input;
|
|
424
|
+
const requestParameters = __spreadProps(__spreadValues({}, input), {
|
|
425
|
+
body: void 0
|
|
426
|
+
});
|
|
427
|
+
if (["GET", "HEAD"].includes(method)) {
|
|
428
|
+
return requestParameters;
|
|
429
|
+
}
|
|
430
|
+
if (typeof body2 === "object" || typeof body2 === "number" || typeof body2 === "boolean") {
|
|
431
|
+
requestParameters.body = JSON.stringify(body2);
|
|
432
|
+
} else {
|
|
433
|
+
requestParameters.body = body2;
|
|
434
|
+
}
|
|
435
|
+
return requestParameters;
|
|
436
|
+
};
|
|
437
|
+
var fetch = (input, requestInit = {}) => {
|
|
438
|
+
if (typeof input === "string") {
|
|
439
|
+
return useFetch(input, augmentRequestInit(requestInit));
|
|
440
|
+
}
|
|
441
|
+
const requestParameters = createFetchRequestParameters(input);
|
|
442
|
+
const derivedRequestInit = augmentRequestInit(requestParameters);
|
|
443
|
+
return useFetch(input.url.href, derivedRequestInit);
|
|
444
|
+
};
|
|
445
|
+
|
|
755
446
|
// src/context/text.ts
|
|
756
447
|
var text = (body2) => {
|
|
757
448
|
return (res) => {
|
|
@@ -775,27 +466,108 @@ function getStatusCodeColor(status2) {
|
|
|
775
466
|
if (status2 < 300) {
|
|
776
467
|
return "#69AB32" /* Success */;
|
|
777
468
|
}
|
|
778
|
-
if (status2 < 400) {
|
|
779
|
-
return "#F0BB4B" /* Warning */;
|
|
469
|
+
if (status2 < 400) {
|
|
470
|
+
return "#F0BB4B" /* Warning */;
|
|
471
|
+
}
|
|
472
|
+
return "#E95F5D" /* Danger */;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// src/utils/logging/getTimestamp.ts
|
|
476
|
+
function getTimestamp() {
|
|
477
|
+
const now = new Date();
|
|
478
|
+
return [now.getHours(), now.getMinutes(), now.getSeconds()].map(String).map((chunk) => chunk.slice(0, 2)).map((chunk) => chunk.padStart(2, "0")).join(":");
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// src/utils/logging/prepareRequest.ts
|
|
482
|
+
function prepareRequest(request) {
|
|
483
|
+
return __spreadProps(__spreadValues({}, request), {
|
|
484
|
+
body: request.body,
|
|
485
|
+
headers: request.headers.all()
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// src/utils/logging/prepareResponse.ts
|
|
490
|
+
import { objectToHeaders as objectToHeaders2 } from "headers-polyfill";
|
|
491
|
+
|
|
492
|
+
// src/utils/internal/parseMultipartData.ts
|
|
493
|
+
import { stringToHeaders } from "headers-polyfill";
|
|
494
|
+
function parseContentHeaders(headersString) {
|
|
495
|
+
var _a, _b;
|
|
496
|
+
const headers = stringToHeaders(headersString);
|
|
497
|
+
const contentType = headers.get("content-type") || "text/plain";
|
|
498
|
+
const disposition = headers.get("content-disposition");
|
|
499
|
+
if (!disposition) {
|
|
500
|
+
throw new Error('"Content-Disposition" header is required.');
|
|
501
|
+
}
|
|
502
|
+
const directives = disposition.split(";").reduce((acc, chunk) => {
|
|
503
|
+
const [name2, ...rest] = chunk.trim().split("=");
|
|
504
|
+
acc[name2] = rest.join("=");
|
|
505
|
+
return acc;
|
|
506
|
+
}, {});
|
|
507
|
+
const name = (_a = directives.name) == null ? void 0 : _a.slice(1, -1);
|
|
508
|
+
const filename = (_b = directives.filename) == null ? void 0 : _b.slice(1, -1);
|
|
509
|
+
return {
|
|
510
|
+
name,
|
|
511
|
+
filename,
|
|
512
|
+
contentType
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
function parseMultipartData(data2, headers) {
|
|
516
|
+
const contentType = headers == null ? void 0 : headers.get("content-type");
|
|
517
|
+
if (!contentType) {
|
|
518
|
+
return void 0;
|
|
519
|
+
}
|
|
520
|
+
const [, ...directives] = contentType.split(/; */);
|
|
521
|
+
const boundary = directives.filter((d) => d.startsWith("boundary=")).map((s) => s.replace(/^boundary=/, ""))[0];
|
|
522
|
+
if (!boundary) {
|
|
523
|
+
return void 0;
|
|
524
|
+
}
|
|
525
|
+
const boundaryRegExp = new RegExp(`--+${boundary}`);
|
|
526
|
+
const fields = data2.split(boundaryRegExp).filter((chunk) => chunk.startsWith("\r\n") && chunk.endsWith("\r\n")).map((chunk) => chunk.trimStart().replace(/\r\n$/, ""));
|
|
527
|
+
if (!fields.length) {
|
|
528
|
+
return void 0;
|
|
529
|
+
}
|
|
530
|
+
const parsedBody = {};
|
|
531
|
+
try {
|
|
532
|
+
for (const field2 of fields) {
|
|
533
|
+
const [contentHeaders, ...rest] = field2.split("\r\n\r\n");
|
|
534
|
+
const contentBody = rest.join("\r\n\r\n");
|
|
535
|
+
const { contentType: contentType2, filename, name } = parseContentHeaders(contentHeaders);
|
|
536
|
+
const value = filename === void 0 ? contentBody : new File([contentBody], filename, { type: contentType2 });
|
|
537
|
+
const parsedValue = parsedBody[name];
|
|
538
|
+
if (parsedValue === void 0) {
|
|
539
|
+
parsedBody[name] = value;
|
|
540
|
+
} else if (Array.isArray(parsedValue)) {
|
|
541
|
+
parsedBody[name] = [...parsedValue, value];
|
|
542
|
+
} else {
|
|
543
|
+
parsedBody[name] = [parsedValue, value];
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
return parsedBody;
|
|
547
|
+
} catch (error2) {
|
|
548
|
+
return void 0;
|
|
780
549
|
}
|
|
781
|
-
return "#E95F5D" /* Danger */;
|
|
782
|
-
}
|
|
783
|
-
|
|
784
|
-
// src/utils/logging/getTimestamp.ts
|
|
785
|
-
function getTimestamp() {
|
|
786
|
-
const now = new Date();
|
|
787
|
-
return [now.getHours(), now.getMinutes(), now.getSeconds()].map(String).map((chunk) => chunk.slice(0, 2)).map((chunk) => chunk.padStart(2, "0")).join(":");
|
|
788
550
|
}
|
|
789
551
|
|
|
790
|
-
// src/utils/
|
|
791
|
-
function
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
552
|
+
// src/utils/request/parseBody.ts
|
|
553
|
+
function parseBody(body2, headers) {
|
|
554
|
+
var _a;
|
|
555
|
+
if (!body2) {
|
|
556
|
+
return body2;
|
|
557
|
+
}
|
|
558
|
+
const contentType = ((_a = headers == null ? void 0 : headers.get("content-type")) == null ? void 0 : _a.toLowerCase()) || "";
|
|
559
|
+
const hasMultipartContent = contentType.startsWith("multipart/form-data");
|
|
560
|
+
if (hasMultipartContent && typeof body2 !== "object") {
|
|
561
|
+
return parseMultipartData(body2.toString(), headers) || body2;
|
|
562
|
+
}
|
|
563
|
+
const hasJsonContent = contentType.includes("json");
|
|
564
|
+
if (hasJsonContent && typeof body2 !== "object") {
|
|
565
|
+
return jsonParse(body2.toString()) || body2;
|
|
566
|
+
}
|
|
567
|
+
return body2;
|
|
795
568
|
}
|
|
796
569
|
|
|
797
570
|
// src/utils/logging/prepareResponse.ts
|
|
798
|
-
import { objectToHeaders as objectToHeaders2 } from "headers-polyfill";
|
|
799
571
|
function prepareResponse(res) {
|
|
800
572
|
const responseHeaders = objectToHeaders2(res.headers);
|
|
801
573
|
return __spreadProps(__spreadValues({}, res), {
|
|
@@ -864,6 +636,248 @@ function matchRequestUrl(url, path, baseUrl) {
|
|
|
864
636
|
};
|
|
865
637
|
}
|
|
866
638
|
|
|
639
|
+
// src/utils/request/MockedRequest.ts
|
|
640
|
+
import * as cookieUtils3 from "cookie";
|
|
641
|
+
import { store } from "@mswjs/cookies";
|
|
642
|
+
import { IsomorphicRequest } from "@mswjs/interceptors";
|
|
643
|
+
import { decodeBuffer } from "@mswjs/interceptors/lib/utils/bufferUtils";
|
|
644
|
+
import { Headers as Headers2 } from "headers-polyfill/lib";
|
|
645
|
+
|
|
646
|
+
// src/utils/request/getRequestCookies.ts
|
|
647
|
+
import * as cookieUtils2 from "cookie";
|
|
648
|
+
function getAllCookies() {
|
|
649
|
+
return cookieUtils2.parse(document.cookie);
|
|
650
|
+
}
|
|
651
|
+
function getRequestCookies(request) {
|
|
652
|
+
if (typeof document === "undefined" || typeof location === "undefined") {
|
|
653
|
+
return {};
|
|
654
|
+
}
|
|
655
|
+
switch (request.credentials) {
|
|
656
|
+
case "same-origin": {
|
|
657
|
+
return location.origin === request.url.origin ? getAllCookies() : {};
|
|
658
|
+
}
|
|
659
|
+
case "include": {
|
|
660
|
+
return getAllCookies();
|
|
661
|
+
}
|
|
662
|
+
default: {
|
|
663
|
+
return {};
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
// src/utils/request/MockedRequest.ts
|
|
669
|
+
var MockedRequest = class extends IsomorphicRequest {
|
|
670
|
+
constructor(input, init = {}) {
|
|
671
|
+
var __super = (...args) => {
|
|
672
|
+
super(...args);
|
|
673
|
+
};
|
|
674
|
+
if (input instanceof IsomorphicRequest) {
|
|
675
|
+
__super(input);
|
|
676
|
+
} else {
|
|
677
|
+
__super(input, init);
|
|
678
|
+
}
|
|
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 = 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 Headers2(),
|
|
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
|
+
store.hydrate();
|
|
716
|
+
const cookiesFromStore = Array.from((_a = 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
|
+
import { Headers as Headers4 } from "headers-polyfill";
|
|
730
|
+
|
|
731
|
+
// src/response.ts
|
|
732
|
+
import { Headers as Headers3 } from "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 Headers3({
|
|
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
|
+
|
|
867
881
|
// src/handlers/RestHandler.ts
|
|
868
882
|
var restContext = __spreadProps(__spreadValues({}, defaultContext), {
|
|
869
883
|
cookie,
|
|
@@ -872,6 +886,15 @@ var restContext = __spreadProps(__spreadValues({}, defaultContext), {
|
|
|
872
886
|
json,
|
|
873
887
|
xml
|
|
874
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
|
+
};
|
|
875
898
|
var RestHandler = class extends RequestHandler {
|
|
876
899
|
constructor(method, path, resolver) {
|
|
877
900
|
super({
|
|
@@ -905,9 +928,7 @@ var RestHandler = class extends RequestHandler {
|
|
|
905
928
|
return matchRequestUrl(request.url, this.info.path, resolutionContext == null ? void 0 : resolutionContext.baseUrl);
|
|
906
929
|
}
|
|
907
930
|
getPublicRequest(request, parsedResult) {
|
|
908
|
-
return
|
|
909
|
-
params: parsedResult.params || {}
|
|
910
|
-
});
|
|
931
|
+
return new RestRequest(request, parsedResult.params || {});
|
|
911
932
|
}
|
|
912
933
|
predicate(request, parsedResult) {
|
|
913
934
|
const matchesMethod = this.info.method instanceof RegExp ? this.info.method.test(request.method) : isStringEqual(this.info.method, request.method);
|
|
@@ -970,6 +991,14 @@ function isDocumentNode(value) {
|
|
|
970
991
|
}
|
|
971
992
|
return typeof value === "object" && "kind" in value && "definitions" in value;
|
|
972
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
|
+
};
|
|
973
1002
|
var GraphQLHandler = class extends RequestHandler {
|
|
974
1003
|
constructor(operationType, operationName, endpoint, resolver) {
|
|
975
1004
|
let resolvedOperationName = operationName;
|
|
@@ -999,9 +1028,7 @@ var GraphQLHandler = class extends RequestHandler {
|
|
|
999
1028
|
return tryCatch(() => parseGraphQLRequest(request), (error2) => console.error(error2.message));
|
|
1000
1029
|
}
|
|
1001
1030
|
getPublicRequest(request, parsedResult) {
|
|
1002
|
-
return
|
|
1003
|
-
variables: (parsedResult == null ? void 0 : parsedResult.variables) || {}
|
|
1004
|
-
});
|
|
1031
|
+
return new GraphQLRequest(request, (parsedResult == null ? void 0 : parsedResult.variables) || {});
|
|
1005
1032
|
}
|
|
1006
1033
|
predicate(request, parsedResult) {
|
|
1007
1034
|
if (!parsedResult) {
|
|
@@ -1232,7 +1259,7 @@ function createSetupServer(...interceptors) {
|
|
|
1232
1259
|
interceptors: interceptors.map((Interceptor2) => new Interceptor2())
|
|
1233
1260
|
});
|
|
1234
1261
|
interceptor.on("request", async function setupServerListener(request) {
|
|
1235
|
-
const mockedRequest =
|
|
1262
|
+
const mockedRequest = new MockedRequest(request);
|
|
1236
1263
|
const response2 = await handleRequest(mockedRequest, currentHandlers, resolvedOptions, emitter, {
|
|
1237
1264
|
transformResponse(response3) {
|
|
1238
1265
|
return {
|