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