msw 0.35.0 → 0.36.3
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/esm/RequestHandler-deps.js +387 -65
- package/lib/esm/errors-deps.js +15 -1
- package/lib/esm/fetch-deps.js +9 -5
- package/lib/esm/graphql-deps.js +14 -9
- package/lib/esm/graphql.js +1 -0
- package/lib/esm/index-deps.js +2 -1
- package/lib/esm/index.js +61 -4263
- package/lib/esm/index2.js +1 -1
- package/lib/esm/mockServiceWorker.js +5 -5
- package/lib/esm/rest-deps.js +7 -15
- package/lib/esm/rest.js +1 -0
- package/lib/iife/index.js +2 -2
- package/lib/iife/mockServiceWorker.js +5 -5
- package/lib/types/context/data.d.ts +2 -3
- package/lib/types/context/extensions.d.ts +8 -0
- package/lib/types/context/index.d.ts +1 -0
- package/lib/types/graphql.d.ts +2 -1
- package/lib/types/handlers/GraphQLHandler.d.ts +7 -6
- package/lib/types/handlers/RequestHandler.d.ts +8 -8
- package/lib/types/handlers/RestHandler.d.ts +10 -12
- package/lib/types/index.d.ts +17 -7
- package/lib/types/native/index.d.ts +1 -1
- package/lib/types/node/index.d.ts +1 -1
- package/lib/types/node/setupServer.d.ts +1 -1
- package/lib/types/rest.d.ts +10 -9
- package/lib/types/setupWorker/glossary.d.ts +12 -12
- package/lib/types/setupWorker/start/utils/prepareStartHandler.d.ts +3 -3
- package/lib/types/sharedOptions.d.ts +1 -1
- package/lib/types/typeUtils.d.ts +5 -3
- package/lib/types/utils/getResponse.d.ts +1 -1
- package/lib/types/utils/handleRequest.d.ts +5 -5
- package/lib/types/utils/internal/getCallFrame.d.ts +1 -1
- package/lib/types/utils/internal/jsonParse.d.ts +2 -2
- package/lib/types/utils/internal/requestHandlerUtils.d.ts +1 -1
- package/lib/types/utils/logging/prepareResponse.d.ts +1 -1
- package/lib/types/utils/matching/matchRequestUrl.d.ts +12 -2
- package/lib/types/utils/matching/normalizePath.d.ts +1 -1
- package/lib/types/utils/request/onUnhandledRequest.d.ts +1 -2
- package/lib/types/utils/request/parseBody.d.ts +2 -2
- package/lib/types/utils/request/parseWorkerRequest.d.ts +2 -2
- package/lib/types/utils/worker/createFallbackRequestListener.d.ts +2 -1
- package/lib/types/utils/worker/createRequestListener.d.ts +2 -1
- package/lib/umd/index.js +547 -134
- package/lib/umd/mockServiceWorker.js +5 -5
- package/native/lib/index.js +474 -517
- package/node/lib/index.js +476 -519
- package/package.json +19 -19
package/native/lib/index.js
CHANGED
|
@@ -6,6 +6,8 @@ var XMLHttpRequest = require('@mswjs/interceptors/lib/interceptors/XMLHttpReques
|
|
|
6
6
|
var chalk = require('chalk');
|
|
7
7
|
var require$$0 = require('events');
|
|
8
8
|
var interceptors = require('@mswjs/interceptors');
|
|
9
|
+
var cookies = require('@mswjs/cookies');
|
|
10
|
+
var getCleanUrl = require('@mswjs/interceptors/lib/utils/getCleanUrl');
|
|
9
11
|
|
|
10
12
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
13
|
|
|
@@ -50,7 +52,7 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
50
52
|
|
|
51
53
|
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
52
54
|
|
|
53
|
-
var lib$
|
|
55
|
+
var lib$3 = {exports: {}};
|
|
54
56
|
|
|
55
57
|
(function (module, exports) {
|
|
56
58
|
(function (global, factory) {
|
|
@@ -73,9 +75,9 @@ var lib$4 = {exports: {}};
|
|
|
73
75
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
74
76
|
|
|
75
77
|
})));
|
|
76
|
-
}(lib$
|
|
78
|
+
}(lib$3, lib$3.exports));
|
|
77
79
|
|
|
78
|
-
var lib$
|
|
80
|
+
var lib$2 = {};
|
|
79
81
|
|
|
80
82
|
var StrictEventEmitter$1 = {};
|
|
81
83
|
|
|
@@ -101,7 +103,7 @@ var __spreadArrays = (commonjsGlobal && commonjsGlobal.__spreadArrays) || functi
|
|
|
101
103
|
};
|
|
102
104
|
StrictEventEmitter$1.__esModule = true;
|
|
103
105
|
StrictEventEmitter$1.StrictEventEmitter = void 0;
|
|
104
|
-
var events_1 = require$$0__default[
|
|
106
|
+
var events_1 = require$$0__default["default"];
|
|
105
107
|
var StrictEventEmitter = /** @class */ (function (_super) {
|
|
106
108
|
__extends$1(StrictEventEmitter, _super);
|
|
107
109
|
function StrictEventEmitter() {
|
|
@@ -145,7 +147,7 @@ exports.__esModule = true;
|
|
|
145
147
|
exports.StrictEventEmitter = void 0;
|
|
146
148
|
var StrictEventEmitter_1 = StrictEventEmitter$1;
|
|
147
149
|
__createBinding(exports, StrictEventEmitter_1, "StrictEventEmitter");
|
|
148
|
-
}(lib$
|
|
150
|
+
}(lib$2));
|
|
149
151
|
|
|
150
152
|
function use(currentHandlers, ...handlers) {
|
|
151
153
|
currentHandlers.unshift(...handlers);
|
|
@@ -360,28 +362,20 @@ function tryDecode(str, decode) {
|
|
|
360
362
|
}
|
|
361
363
|
}
|
|
362
364
|
|
|
363
|
-
function uuidv4() {
|
|
364
|
-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
365
|
-
const r = (Math.random() * 16) | 0;
|
|
366
|
-
const v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
367
|
-
return v.toString(16);
|
|
368
|
-
});
|
|
369
|
-
}
|
|
370
|
-
|
|
371
365
|
/**
|
|
372
|
-
* Parses a given
|
|
366
|
+
* Parses a given value into a JSON.
|
|
373
367
|
* Does not throw an exception on an invalid JSON string.
|
|
374
368
|
*/
|
|
375
|
-
function jsonParse(
|
|
369
|
+
function jsonParse(value) {
|
|
376
370
|
try {
|
|
377
|
-
return JSON.parse(
|
|
371
|
+
return JSON.parse(value);
|
|
378
372
|
}
|
|
379
373
|
catch (error) {
|
|
380
374
|
return undefined;
|
|
381
375
|
}
|
|
382
376
|
}
|
|
383
377
|
|
|
384
|
-
var lib$
|
|
378
|
+
var lib$1 = {};
|
|
385
379
|
|
|
386
380
|
var Headers = {};
|
|
387
381
|
|
|
@@ -905,11 +899,11 @@ var flattenHeadersList_1 = flattenHeadersList$1;
|
|
|
905
899
|
Object.defineProperty(exports, "flattenHeadersList", { enumerable: true, get: function () { return flattenHeadersList_1.flattenHeadersList; } });
|
|
906
900
|
var flattenHeadersObject_1 = flattenHeadersObject$1;
|
|
907
901
|
Object.defineProperty(exports, "flattenHeadersObject", { enumerable: true, get: function () { return flattenHeadersObject_1.flattenHeadersObject; } });
|
|
908
|
-
}(lib$
|
|
902
|
+
}(lib$1));
|
|
909
903
|
|
|
910
904
|
function parseContentHeaders(headersString) {
|
|
911
905
|
var _a, _b;
|
|
912
|
-
const headers = lib$
|
|
906
|
+
const headers = lib$1.stringToHeaders(headersString);
|
|
913
907
|
const contentType = headers.get('content-type') || 'text/plain';
|
|
914
908
|
const disposition = headers.get('content-disposition');
|
|
915
909
|
if (!disposition) {
|
|
@@ -980,403 +974,30 @@ function parseMultipartData(data, headers) {
|
|
|
980
974
|
}
|
|
981
975
|
|
|
982
976
|
/**
|
|
983
|
-
* Parses a given request/response body based on the
|
|
977
|
+
* Parses a given request/response body based on the "Content-Type" header.
|
|
984
978
|
*/
|
|
985
979
|
function parseBody(body, headers) {
|
|
986
980
|
// Return whatever falsey body value is given.
|
|
987
981
|
if (!body) {
|
|
988
982
|
return body;
|
|
989
983
|
}
|
|
990
|
-
const contentType = headers === null || headers === void 0 ? void 0 : headers.get('content-type');
|
|
984
|
+
const contentType = (headers === null || headers === void 0 ? void 0 : headers.get('content-type')) || '';
|
|
991
985
|
// If the body has a Multipart Content-Type
|
|
992
986
|
// parse it into an object.
|
|
993
|
-
const hasMultipartContent = contentType
|
|
987
|
+
const hasMultipartContent = contentType.startsWith('multipart/form-data');
|
|
994
988
|
if (hasMultipartContent && typeof body !== 'object') {
|
|
995
|
-
return parseMultipartData(body, headers) || body;
|
|
989
|
+
return parseMultipartData(body.toString(), headers) || body;
|
|
996
990
|
}
|
|
997
991
|
// If the intercepted request's body has a JSON Content-Type
|
|
998
992
|
// parse it into an object.
|
|
999
|
-
const hasJsonContent = contentType
|
|
993
|
+
const hasJsonContent = contentType.includes('json');
|
|
1000
994
|
if (hasJsonContent && typeof body !== 'object') {
|
|
1001
|
-
return jsonParse(body) || body;
|
|
995
|
+
return jsonParse(body.toString()) || body;
|
|
1002
996
|
}
|
|
1003
997
|
// Otherwise leave as-is.
|
|
1004
998
|
return body;
|
|
1005
999
|
}
|
|
1006
1000
|
|
|
1007
|
-
var lib$1 = {};
|
|
1008
|
-
|
|
1009
|
-
var CookieStore = {};
|
|
1010
|
-
|
|
1011
|
-
var setCookie = {exports: {}};
|
|
1012
|
-
|
|
1013
|
-
var defaultParseOptions = {
|
|
1014
|
-
decodeValues: true,
|
|
1015
|
-
map: false,
|
|
1016
|
-
silent: false,
|
|
1017
|
-
};
|
|
1018
|
-
|
|
1019
|
-
function isNonEmptyString(str) {
|
|
1020
|
-
return typeof str === "string" && !!str.trim();
|
|
1021
|
-
}
|
|
1022
|
-
|
|
1023
|
-
function parseString(setCookieValue, options) {
|
|
1024
|
-
var parts = setCookieValue.split(";").filter(isNonEmptyString);
|
|
1025
|
-
var nameValue = parts.shift().split("=");
|
|
1026
|
-
var name = nameValue.shift();
|
|
1027
|
-
var value = nameValue.join("="); // everything after the first =, joined by a "=" if there was more than one part
|
|
1028
|
-
|
|
1029
|
-
options = options
|
|
1030
|
-
? Object.assign({}, defaultParseOptions, options)
|
|
1031
|
-
: defaultParseOptions;
|
|
1032
|
-
|
|
1033
|
-
try {
|
|
1034
|
-
value = options.decodeValues ? decodeURIComponent(value) : value; // decode cookie value
|
|
1035
|
-
} catch (e) {
|
|
1036
|
-
console.error(
|
|
1037
|
-
"set-cookie-parser encountered an error while decoding a cookie with value '" +
|
|
1038
|
-
value +
|
|
1039
|
-
"'. Set options.decodeValues to false to disable this feature.",
|
|
1040
|
-
e
|
|
1041
|
-
);
|
|
1042
|
-
}
|
|
1043
|
-
|
|
1044
|
-
var cookie = {
|
|
1045
|
-
name: name, // grab everything before the first =
|
|
1046
|
-
value: value,
|
|
1047
|
-
};
|
|
1048
|
-
|
|
1049
|
-
parts.forEach(function (part) {
|
|
1050
|
-
var sides = part.split("=");
|
|
1051
|
-
var key = sides.shift().trimLeft().toLowerCase();
|
|
1052
|
-
var value = sides.join("=");
|
|
1053
|
-
if (key === "expires") {
|
|
1054
|
-
cookie.expires = new Date(value);
|
|
1055
|
-
} else if (key === "max-age") {
|
|
1056
|
-
cookie.maxAge = parseInt(value, 10);
|
|
1057
|
-
} else if (key === "secure") {
|
|
1058
|
-
cookie.secure = true;
|
|
1059
|
-
} else if (key === "httponly") {
|
|
1060
|
-
cookie.httpOnly = true;
|
|
1061
|
-
} else if (key === "samesite") {
|
|
1062
|
-
cookie.sameSite = value;
|
|
1063
|
-
} else {
|
|
1064
|
-
cookie[key] = value;
|
|
1065
|
-
}
|
|
1066
|
-
});
|
|
1067
|
-
|
|
1068
|
-
return cookie;
|
|
1069
|
-
}
|
|
1070
|
-
|
|
1071
|
-
function parse$1(input, options) {
|
|
1072
|
-
options = options
|
|
1073
|
-
? Object.assign({}, defaultParseOptions, options)
|
|
1074
|
-
: defaultParseOptions;
|
|
1075
|
-
|
|
1076
|
-
if (!input) {
|
|
1077
|
-
if (!options.map) {
|
|
1078
|
-
return [];
|
|
1079
|
-
} else {
|
|
1080
|
-
return {};
|
|
1081
|
-
}
|
|
1082
|
-
}
|
|
1083
|
-
|
|
1084
|
-
if (input.headers && input.headers["set-cookie"]) {
|
|
1085
|
-
// fast-path for node.js (which automatically normalizes header names to lower-case
|
|
1086
|
-
input = input.headers["set-cookie"];
|
|
1087
|
-
} else if (input.headers) {
|
|
1088
|
-
// slow-path for other environments - see #25
|
|
1089
|
-
var sch =
|
|
1090
|
-
input.headers[
|
|
1091
|
-
Object.keys(input.headers).find(function (key) {
|
|
1092
|
-
return key.toLowerCase() === "set-cookie";
|
|
1093
|
-
})
|
|
1094
|
-
];
|
|
1095
|
-
// warn if called on a request-like object with a cookie header rather than a set-cookie header - see #34, 36
|
|
1096
|
-
if (!sch && input.headers.cookie && !options.silent) {
|
|
1097
|
-
console.warn(
|
|
1098
|
-
"Warning: set-cookie-parser appears to have been called on a request object. It is designed to parse Set-Cookie headers from responses, not Cookie headers from requests. Set the option {silent: true} to suppress this warning."
|
|
1099
|
-
);
|
|
1100
|
-
}
|
|
1101
|
-
input = sch;
|
|
1102
|
-
}
|
|
1103
|
-
if (!Array.isArray(input)) {
|
|
1104
|
-
input = [input];
|
|
1105
|
-
}
|
|
1106
|
-
|
|
1107
|
-
options = options
|
|
1108
|
-
? Object.assign({}, defaultParseOptions, options)
|
|
1109
|
-
: defaultParseOptions;
|
|
1110
|
-
|
|
1111
|
-
if (!options.map) {
|
|
1112
|
-
return input.filter(isNonEmptyString).map(function (str) {
|
|
1113
|
-
return parseString(str, options);
|
|
1114
|
-
});
|
|
1115
|
-
} else {
|
|
1116
|
-
var cookies = {};
|
|
1117
|
-
return input.filter(isNonEmptyString).reduce(function (cookies, str) {
|
|
1118
|
-
var cookie = parseString(str, options);
|
|
1119
|
-
cookies[cookie.name] = cookie;
|
|
1120
|
-
return cookies;
|
|
1121
|
-
}, cookies);
|
|
1122
|
-
}
|
|
1123
|
-
}
|
|
1124
|
-
|
|
1125
|
-
/*
|
|
1126
|
-
Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas
|
|
1127
|
-
that are within a single set-cookie field-value, such as in the Expires portion.
|
|
1128
|
-
|
|
1129
|
-
This is uncommon, but explicitly allowed - see https://tools.ietf.org/html/rfc2616#section-4.2
|
|
1130
|
-
Node.js does this for every header *except* set-cookie - see https://github.com/nodejs/node/blob/d5e363b77ebaf1caf67cd7528224b651c86815c1/lib/_http_incoming.js#L128
|
|
1131
|
-
React Native's fetch does this for *every* header, including set-cookie.
|
|
1132
|
-
|
|
1133
|
-
Based on: https://github.com/google/j2objc/commit/16820fdbc8f76ca0c33472810ce0cb03d20efe25
|
|
1134
|
-
Credits to: https://github.com/tomball for original and https://github.com/chrusart for JavaScript implementation
|
|
1135
|
-
*/
|
|
1136
|
-
function splitCookiesString(cookiesString) {
|
|
1137
|
-
if (Array.isArray(cookiesString)) {
|
|
1138
|
-
return cookiesString;
|
|
1139
|
-
}
|
|
1140
|
-
if (typeof cookiesString !== "string") {
|
|
1141
|
-
return [];
|
|
1142
|
-
}
|
|
1143
|
-
|
|
1144
|
-
var cookiesStrings = [];
|
|
1145
|
-
var pos = 0;
|
|
1146
|
-
var start;
|
|
1147
|
-
var ch;
|
|
1148
|
-
var lastComma;
|
|
1149
|
-
var nextStart;
|
|
1150
|
-
var cookiesSeparatorFound;
|
|
1151
|
-
|
|
1152
|
-
function skipWhitespace() {
|
|
1153
|
-
while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) {
|
|
1154
|
-
pos += 1;
|
|
1155
|
-
}
|
|
1156
|
-
return pos < cookiesString.length;
|
|
1157
|
-
}
|
|
1158
|
-
|
|
1159
|
-
function notSpecialChar() {
|
|
1160
|
-
ch = cookiesString.charAt(pos);
|
|
1161
|
-
|
|
1162
|
-
return ch !== "=" && ch !== ";" && ch !== ",";
|
|
1163
|
-
}
|
|
1164
|
-
|
|
1165
|
-
while (pos < cookiesString.length) {
|
|
1166
|
-
start = pos;
|
|
1167
|
-
cookiesSeparatorFound = false;
|
|
1168
|
-
|
|
1169
|
-
while (skipWhitespace()) {
|
|
1170
|
-
ch = cookiesString.charAt(pos);
|
|
1171
|
-
if (ch === ",") {
|
|
1172
|
-
// ',' is a cookie separator if we have later first '=', not ';' or ','
|
|
1173
|
-
lastComma = pos;
|
|
1174
|
-
pos += 1;
|
|
1175
|
-
|
|
1176
|
-
skipWhitespace();
|
|
1177
|
-
nextStart = pos;
|
|
1178
|
-
|
|
1179
|
-
while (pos < cookiesString.length && notSpecialChar()) {
|
|
1180
|
-
pos += 1;
|
|
1181
|
-
}
|
|
1182
|
-
|
|
1183
|
-
// currently special character
|
|
1184
|
-
if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") {
|
|
1185
|
-
// we found cookies separator
|
|
1186
|
-
cookiesSeparatorFound = true;
|
|
1187
|
-
// pos is inside the next cookie, so back up and return it.
|
|
1188
|
-
pos = nextStart;
|
|
1189
|
-
cookiesStrings.push(cookiesString.substring(start, lastComma));
|
|
1190
|
-
start = pos;
|
|
1191
|
-
} else {
|
|
1192
|
-
// in param ',' or param separator ';',
|
|
1193
|
-
// we continue from that comma
|
|
1194
|
-
pos = lastComma + 1;
|
|
1195
|
-
}
|
|
1196
|
-
} else {
|
|
1197
|
-
pos += 1;
|
|
1198
|
-
}
|
|
1199
|
-
}
|
|
1200
|
-
|
|
1201
|
-
if (!cookiesSeparatorFound || pos >= cookiesString.length) {
|
|
1202
|
-
cookiesStrings.push(cookiesString.substring(start, cookiesString.length));
|
|
1203
|
-
}
|
|
1204
|
-
}
|
|
1205
|
-
|
|
1206
|
-
return cookiesStrings;
|
|
1207
|
-
}
|
|
1208
|
-
|
|
1209
|
-
setCookie.exports = parse$1;
|
|
1210
|
-
setCookie.exports.parse = parse$1;
|
|
1211
|
-
setCookie.exports.parseString = parseString;
|
|
1212
|
-
setCookie.exports.splitCookiesString = splitCookiesString;
|
|
1213
|
-
|
|
1214
|
-
(function (exports) {
|
|
1215
|
-
var __rest = (commonjsGlobal && commonjsGlobal.__rest) || function (s, e) {
|
|
1216
|
-
var t = {};
|
|
1217
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
1218
|
-
t[p] = s[p];
|
|
1219
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
1220
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
1221
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
1222
|
-
t[p[i]] = s[p[i]];
|
|
1223
|
-
}
|
|
1224
|
-
return t;
|
|
1225
|
-
};
|
|
1226
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1227
|
-
exports.PERSISTENCY_KEY = void 0;
|
|
1228
|
-
const set_cookie_parser_1 = setCookie.exports;
|
|
1229
|
-
exports.PERSISTENCY_KEY = 'MSW_COOKIE_STORE';
|
|
1230
|
-
const SUPPORTS_LOCAL_STORAGE = typeof localStorage !== 'undefined';
|
|
1231
|
-
class CookieStore {
|
|
1232
|
-
constructor() {
|
|
1233
|
-
this.store = new Map();
|
|
1234
|
-
}
|
|
1235
|
-
/**
|
|
1236
|
-
* Sets the given request cookies into the store.
|
|
1237
|
-
* Respects the `request.credentials` policy.
|
|
1238
|
-
*/
|
|
1239
|
-
add(request, response) {
|
|
1240
|
-
if (request.credentials === 'omit') {
|
|
1241
|
-
return;
|
|
1242
|
-
}
|
|
1243
|
-
const requestUrl = new URL(request.url);
|
|
1244
|
-
const responseCookies = response.headers.get('set-cookie');
|
|
1245
|
-
if (!responseCookies) {
|
|
1246
|
-
return;
|
|
1247
|
-
}
|
|
1248
|
-
const now = Date.now();
|
|
1249
|
-
const parsedResponseCookies = set_cookie_parser_1.parse(responseCookies).map((_a) => {
|
|
1250
|
-
var { maxAge } = _a, cookie = __rest(_a, ["maxAge"]);
|
|
1251
|
-
return (Object.assign(Object.assign({}, cookie), { expires: maxAge === undefined ? cookie.expires : new Date(now + maxAge * 1000), maxAge }));
|
|
1252
|
-
});
|
|
1253
|
-
const prevCookies = this.store.get(requestUrl.origin) || new Map();
|
|
1254
|
-
parsedResponseCookies.forEach((cookie) => {
|
|
1255
|
-
this.store.set(requestUrl.origin, prevCookies.set(cookie.name, cookie));
|
|
1256
|
-
});
|
|
1257
|
-
}
|
|
1258
|
-
/**
|
|
1259
|
-
* Returns cookies relevant to the given request
|
|
1260
|
-
* and its `request.credentials` policy.
|
|
1261
|
-
*/
|
|
1262
|
-
get(request) {
|
|
1263
|
-
this.deleteExpiredCookies();
|
|
1264
|
-
const requestUrl = new URL(request.url);
|
|
1265
|
-
const originCookies = this.store.get(requestUrl.origin) || new Map();
|
|
1266
|
-
switch (request.credentials) {
|
|
1267
|
-
case 'include': {
|
|
1268
|
-
const documentCookies = set_cookie_parser_1.parse(document.cookie);
|
|
1269
|
-
documentCookies.forEach((cookie) => {
|
|
1270
|
-
originCookies.set(cookie.name, cookie);
|
|
1271
|
-
});
|
|
1272
|
-
return originCookies;
|
|
1273
|
-
}
|
|
1274
|
-
case 'same-origin': {
|
|
1275
|
-
return originCookies;
|
|
1276
|
-
}
|
|
1277
|
-
default:
|
|
1278
|
-
return new Map();
|
|
1279
|
-
}
|
|
1280
|
-
}
|
|
1281
|
-
/**
|
|
1282
|
-
* Returns a collection of all stored cookies.
|
|
1283
|
-
*/
|
|
1284
|
-
getAll() {
|
|
1285
|
-
this.deleteExpiredCookies();
|
|
1286
|
-
return this.store;
|
|
1287
|
-
}
|
|
1288
|
-
/**
|
|
1289
|
-
* Deletes all cookies associated with the given request.
|
|
1290
|
-
*/
|
|
1291
|
-
deleteAll(request) {
|
|
1292
|
-
const requestUrl = new URL(request.url);
|
|
1293
|
-
this.store.delete(requestUrl.origin);
|
|
1294
|
-
}
|
|
1295
|
-
/**
|
|
1296
|
-
* Clears the entire cookie store.
|
|
1297
|
-
*/
|
|
1298
|
-
clear() {
|
|
1299
|
-
this.store.clear();
|
|
1300
|
-
}
|
|
1301
|
-
/**
|
|
1302
|
-
* Hydrates the virtual cookie store from the `localStorage` if defined.
|
|
1303
|
-
*/
|
|
1304
|
-
hydrate() {
|
|
1305
|
-
if (!SUPPORTS_LOCAL_STORAGE) {
|
|
1306
|
-
return;
|
|
1307
|
-
}
|
|
1308
|
-
const persistedCookies = localStorage.getItem(exports.PERSISTENCY_KEY);
|
|
1309
|
-
if (persistedCookies) {
|
|
1310
|
-
try {
|
|
1311
|
-
const parsedCookies = JSON.parse(persistedCookies);
|
|
1312
|
-
parsedCookies.forEach(([origin, cookies]) => {
|
|
1313
|
-
this.store.set(origin, new Map(cookies.map((_a) => {
|
|
1314
|
-
var [token, _b] = _a, { expires } = _b, cookie = __rest(_b, ["expires"]);
|
|
1315
|
-
return [
|
|
1316
|
-
token,
|
|
1317
|
-
expires === undefined
|
|
1318
|
-
? cookie
|
|
1319
|
-
: Object.assign(Object.assign({}, cookie), { expires: new Date(expires) }),
|
|
1320
|
-
];
|
|
1321
|
-
})));
|
|
1322
|
-
});
|
|
1323
|
-
}
|
|
1324
|
-
catch (error) {
|
|
1325
|
-
console.warn(`
|
|
1326
|
-
[virtual-cookie] Failed to parse a stored cookie from the localStorage (key "${exports.PERSISTENCY_KEY}").
|
|
1327
|
-
|
|
1328
|
-
Stored value:
|
|
1329
|
-
${localStorage.getItem(exports.PERSISTENCY_KEY)}
|
|
1330
|
-
|
|
1331
|
-
Thrown exception:
|
|
1332
|
-
${error}
|
|
1333
|
-
|
|
1334
|
-
Invalid value has been removed from localStorage to prevent subsequent failed parsing attempts.`);
|
|
1335
|
-
localStorage.removeItem(exports.PERSISTENCY_KEY);
|
|
1336
|
-
}
|
|
1337
|
-
}
|
|
1338
|
-
}
|
|
1339
|
-
/**
|
|
1340
|
-
* Persists the current virtual cookies into the `localStorage` if defined,
|
|
1341
|
-
* so they are available on the next page load.
|
|
1342
|
-
*/
|
|
1343
|
-
persist() {
|
|
1344
|
-
if (!SUPPORTS_LOCAL_STORAGE) {
|
|
1345
|
-
return;
|
|
1346
|
-
}
|
|
1347
|
-
const serializedCookies = Array.from(this.store.entries()).map(([origin, cookies]) => {
|
|
1348
|
-
return [origin, Array.from(cookies.entries())];
|
|
1349
|
-
});
|
|
1350
|
-
localStorage.setItem(exports.PERSISTENCY_KEY, JSON.stringify(serializedCookies));
|
|
1351
|
-
}
|
|
1352
|
-
deleteExpiredCookies() {
|
|
1353
|
-
const now = Date.now();
|
|
1354
|
-
this.store.forEach((originCookies, origin) => {
|
|
1355
|
-
originCookies.forEach(({ expires, name }) => {
|
|
1356
|
-
if (expires !== undefined && expires.getTime() <= now) {
|
|
1357
|
-
originCookies.delete(name);
|
|
1358
|
-
}
|
|
1359
|
-
});
|
|
1360
|
-
if (originCookies.size === 0) {
|
|
1361
|
-
this.store.delete(origin);
|
|
1362
|
-
}
|
|
1363
|
-
});
|
|
1364
|
-
}
|
|
1365
|
-
}
|
|
1366
|
-
exports.default = new CookieStore();
|
|
1367
|
-
}(CookieStore));
|
|
1368
|
-
|
|
1369
|
-
(function (exports) {
|
|
1370
|
-
var __importDefault = (commonjsGlobal && commonjsGlobal.__importDefault) || function (mod) {
|
|
1371
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
1372
|
-
};
|
|
1373
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1374
|
-
exports.PERSISTENCY_KEY = exports.store = void 0;
|
|
1375
|
-
var CookieStore_1 = CookieStore;
|
|
1376
|
-
Object.defineProperty(exports, "store", { enumerable: true, get: function () { return __importDefault(CookieStore_1).default; } });
|
|
1377
|
-
Object.defineProperty(exports, "PERSISTENCY_KEY", { enumerable: true, get: function () { return CookieStore_1.PERSISTENCY_KEY; } });
|
|
1378
|
-
}(lib$1));
|
|
1379
|
-
|
|
1380
1001
|
function getAllCookies() {
|
|
1381
1002
|
return parse_1(document.cookie);
|
|
1382
1003
|
}
|
|
@@ -1387,7 +1008,7 @@ function getRequestCookies(request) {
|
|
|
1387
1008
|
/**
|
|
1388
1009
|
* @note No cookies persist on the document in Node.js: no document.
|
|
1389
1010
|
*/
|
|
1390
|
-
if (typeof location === 'undefined') {
|
|
1011
|
+
if (typeof document === 'undefined' || typeof location === 'undefined') {
|
|
1391
1012
|
return {};
|
|
1392
1013
|
}
|
|
1393
1014
|
switch (request.credentials) {
|
|
@@ -1408,8 +1029,8 @@ function getRequestCookies(request) {
|
|
|
1408
1029
|
|
|
1409
1030
|
function setRequestCookies(request) {
|
|
1410
1031
|
var _a;
|
|
1411
|
-
|
|
1412
|
-
request.cookies = Object.assign(Object.assign({}, getRequestCookies(request)), Array.from((_a =
|
|
1032
|
+
cookies.store.hydrate();
|
|
1033
|
+
request.cookies = Object.assign(Object.assign({}, getRequestCookies(request)), Array.from((_a = cookies.store.get(Object.assign(Object.assign({}, request), { url: request.url.toString() }))) === null || _a === void 0 ? void 0 : _a.entries()).reduce((cookies, [name, { value }]) => Object.assign(cookies, { [name]: value }), {}));
|
|
1413
1034
|
request.headers.set('cookie', Object.entries(request.cookies)
|
|
1414
1035
|
.map(([name, value]) => `${name}=${value}`)
|
|
1415
1036
|
.join('; '));
|
|
@@ -1419,10 +1040,8 @@ function setRequestCookies(request) {
|
|
|
1419
1040
|
* Converts a given isomorphic request to a `MockedRequest` instance.
|
|
1420
1041
|
*/
|
|
1421
1042
|
function parseIsomorphicRequest(request) {
|
|
1422
|
-
const requestId = uuidv4();
|
|
1423
|
-
request.headers.set('x-msw-request-id', requestId);
|
|
1424
1043
|
const mockedRequest = {
|
|
1425
|
-
id:
|
|
1044
|
+
id: request.id,
|
|
1426
1045
|
url: request.url,
|
|
1427
1046
|
method: request.method,
|
|
1428
1047
|
body: parseBody(request.body, request.headers),
|
|
@@ -2228,6 +1847,7 @@ function invariant(condition, message) {
|
|
|
2228
1847
|
|
|
2229
1848
|
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
|
|
2230
1849
|
var nodejsCustomInspectSymbol = typeof Symbol === 'function' && typeof Symbol.for === 'function' ? Symbol.for('nodejs.util.inspect.custom') : undefined;
|
|
1850
|
+
var nodejsCustomInspectSymbol$1 = nodejsCustomInspectSymbol;
|
|
2231
1851
|
|
|
2232
1852
|
/**
|
|
2233
1853
|
* The `defineInspect()` function defines `inspect()` prototype method as alias of `toJSON`
|
|
@@ -2238,8 +1858,8 @@ function defineInspect(classObject) {
|
|
|
2238
1858
|
typeof fn === 'function' || invariant(0);
|
|
2239
1859
|
classObject.prototype.inspect = fn; // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2317')
|
|
2240
1860
|
|
|
2241
|
-
if (nodejsCustomInspectSymbol) {
|
|
2242
|
-
classObject.prototype[nodejsCustomInspectSymbol] = fn;
|
|
1861
|
+
if (nodejsCustomInspectSymbol$1) {
|
|
1862
|
+
classObject.prototype[nodejsCustomInspectSymbol$1] = fn;
|
|
2243
1863
|
}
|
|
2244
1864
|
}
|
|
2245
1865
|
|
|
@@ -2482,7 +2102,7 @@ function formatArray(array, seenValues) {
|
|
|
2482
2102
|
}
|
|
2483
2103
|
|
|
2484
2104
|
function getCustomFn(object) {
|
|
2485
|
-
var customInspectFn = object[String(nodejsCustomInspectSymbol)];
|
|
2105
|
+
var customInspectFn = object[String(nodejsCustomInspectSymbol$1)];
|
|
2486
2106
|
|
|
2487
2107
|
if (typeof customInspectFn === 'function') {
|
|
2488
2108
|
return customInspectFn;
|
|
@@ -3402,7 +3022,7 @@ function isNameStart(code) {
|
|
|
3402
3022
|
* Given a GraphQL source, parses it into a Document.
|
|
3403
3023
|
* Throws GraphQLError if a syntax error is encountered.
|
|
3404
3024
|
*/
|
|
3405
|
-
function parse(source, options) {
|
|
3025
|
+
function parse$1(source, options) {
|
|
3406
3026
|
var parser = new Parser(source, options);
|
|
3407
3027
|
return parser.parseDocument();
|
|
3408
3028
|
}
|
|
@@ -4921,7 +4541,7 @@ function parseDocumentNode(node) {
|
|
|
4921
4541
|
}
|
|
4922
4542
|
function parseQuery(query) {
|
|
4923
4543
|
try {
|
|
4924
|
-
const ast = parse(query);
|
|
4544
|
+
const ast = parse$1(query);
|
|
4925
4545
|
return parseDocumentNode(ast);
|
|
4926
4546
|
}
|
|
4927
4547
|
catch (error) {
|
|
@@ -5117,7 +4737,7 @@ function set(...args) {
|
|
|
5117
4737
|
res.headers.append(name, value);
|
|
5118
4738
|
}
|
|
5119
4739
|
else {
|
|
5120
|
-
const headers = lib$
|
|
4740
|
+
const headers = lib$1.objectToHeaders(name);
|
|
5121
4741
|
headers.forEach((value, name) => {
|
|
5122
4742
|
res.headers.append(name, value);
|
|
5123
4743
|
});
|
|
@@ -5214,12 +4834,26 @@ const data = (payload) => {
|
|
|
5214
4834
|
};
|
|
5215
4835
|
};
|
|
5216
4836
|
|
|
4837
|
+
/**
|
|
4838
|
+
* Sets the GraphQL extensions on a given response.
|
|
4839
|
+
* @example
|
|
4840
|
+
* res(ctx.extensions({ tracing: { version: 1 }}))
|
|
4841
|
+
* @see {@link https://mswjs.io/docs/api/context/extensions `ctx.extensions()`}
|
|
4842
|
+
*/
|
|
4843
|
+
const extensions = (payload) => {
|
|
4844
|
+
return (res) => {
|
|
4845
|
+
const prevBody = jsonParse(res.body) || {};
|
|
4846
|
+
const nextBody = mergeRight(prevBody, { extensions: payload });
|
|
4847
|
+
return json(nextBody)(res);
|
|
4848
|
+
};
|
|
4849
|
+
};
|
|
4850
|
+
|
|
5217
4851
|
const SET_TIMEOUT_MAX_ALLOWED_INT = 2147483647;
|
|
5218
4852
|
const MIN_SERVER_RESPONSE_TIME = 100;
|
|
5219
4853
|
const MAX_SERVER_RESPONSE_TIME = 400;
|
|
5220
4854
|
const NODE_SERVER_RESPONSE_TIME = 5;
|
|
5221
4855
|
const getRandomServerResponseTime = () => {
|
|
5222
|
-
if (lib$
|
|
4856
|
+
if (lib$3.exports.isNodeProcess()) {
|
|
5223
4857
|
return NODE_SERVER_RESPONSE_TIME;
|
|
5224
4858
|
}
|
|
5225
4859
|
return Math.floor(Math.random() * (MAX_SERVER_RESPONSE_TIME - MIN_SERVER_RESPONSE_TIME) +
|
|
@@ -5286,9 +4920,9 @@ const errors = (errorsList) => {
|
|
|
5286
4920
|
};
|
|
5287
4921
|
};
|
|
5288
4922
|
|
|
5289
|
-
const useFetch = lib$
|
|
4923
|
+
const useFetch = lib$3.exports.isNodeProcess() ? require('node-fetch') : window.fetch;
|
|
5290
4924
|
const augmentRequestInit = (requestInit) => {
|
|
5291
|
-
const headers = new lib$
|
|
4925
|
+
const headers = new lib$1.Headers(requestInit.headers);
|
|
5292
4926
|
headers.set('x-msw-bypass', 'true');
|
|
5293
4927
|
return Object.assign(Object.assign({}, requestInit), { headers: headers.all() });
|
|
5294
4928
|
};
|
|
@@ -5298,8 +4932,12 @@ const createFetchRequestParameters = (input) => {
|
|
|
5298
4932
|
if (['GET', 'HEAD'].includes(method)) {
|
|
5299
4933
|
return requestParameters;
|
|
5300
4934
|
}
|
|
5301
|
-
|
|
5302
|
-
|
|
4935
|
+
if (typeof body === 'object' || typeof body === 'number') {
|
|
4936
|
+
requestParameters.body = JSON.stringify(body);
|
|
4937
|
+
}
|
|
4938
|
+
else {
|
|
4939
|
+
requestParameters.body = body;
|
|
4940
|
+
}
|
|
5303
4941
|
return requestParameters;
|
|
5304
4942
|
};
|
|
5305
4943
|
/**
|
|
@@ -5388,63 +5026,345 @@ function prepareRequest(request) {
|
|
|
5388
5026
|
* Formats a mocked response for introspection in the browser's console.
|
|
5389
5027
|
*/
|
|
5390
5028
|
function prepareResponse(res) {
|
|
5391
|
-
const responseHeaders = lib$
|
|
5029
|
+
const responseHeaders = lib$1.objectToHeaders(res.headers);
|
|
5392
5030
|
return Object.assign(Object.assign({}, res), {
|
|
5393
5031
|
// Parse a response JSON body for preview in the logs
|
|
5394
5032
|
body: parseBody(res.body, responseHeaders) });
|
|
5395
5033
|
}
|
|
5396
5034
|
|
|
5397
|
-
/**
|
|
5398
|
-
* Converts a string path to a Regular Expression.
|
|
5399
|
-
* Transforms path parameters into named RegExp groups.
|
|
5400
|
-
*/
|
|
5401
|
-
const pathToRegExp = (path) => {
|
|
5402
|
-
const pattern = path
|
|
5403
|
-
// Escape literal dots
|
|
5404
|
-
.replace(/\./g, '\\.')
|
|
5405
|
-
// Escape literal slashes
|
|
5406
|
-
.replace(/\//g, '/')
|
|
5407
|
-
// Escape literal question marks
|
|
5408
|
-
.replace(/\?/g, '\\?')
|
|
5409
|
-
// Ignore trailing slashes
|
|
5410
|
-
.replace(/\/+$/, '')
|
|
5411
|
-
// Replace wildcard with any zero-to-any character sequence
|
|
5412
|
-
.replace(/\*+/g, '.*')
|
|
5413
|
-
// Replace parameters with named capturing groups
|
|
5414
|
-
.replace(/:([^\d|^\/][a-zA-Z0-9_]*(?=(?:\/|\\.)|$))/g, (_, paramName) => `(?<${paramName}>[^\/]+?)`)
|
|
5415
|
-
// Allow optional trailing slash
|
|
5416
|
-
.concat('(\\/|$)');
|
|
5417
|
-
return new RegExp(pattern, 'gi');
|
|
5418
|
-
};
|
|
5419
|
-
|
|
5420
|
-
/**
|
|
5421
|
-
* Matches a given url against a path.
|
|
5422
|
-
*/
|
|
5423
|
-
const match = (path, url) => {
|
|
5424
|
-
const expression = path instanceof RegExp ? path : pathToRegExp(path);
|
|
5425
|
-
const match = expression.exec(url) || false;
|
|
5426
|
-
// Matches in strict mode: match string should equal to input (url)
|
|
5427
|
-
// Otherwise loose matches will be considered truthy:
|
|
5428
|
-
// match('/messages/:id', '/messages/123/users') // true
|
|
5429
|
-
const matches = path instanceof RegExp ? !!match : !!match && match[0] === match.input;
|
|
5430
|
-
return {
|
|
5431
|
-
matches,
|
|
5432
|
-
params: match && matches ? match.groups || null : null,
|
|
5433
|
-
};
|
|
5434
|
-
};
|
|
5435
|
-
|
|
5436
|
-
var getCleanUrl$1 = {};
|
|
5437
|
-
|
|
5438
|
-
Object.defineProperty(getCleanUrl$1, "__esModule", { value: true });
|
|
5439
|
-
var getCleanUrl_2 = getCleanUrl$1.getCleanUrl = void 0;
|
|
5440
5035
|
/**
|
|
5441
|
-
*
|
|
5036
|
+
* Tokenize input string.
|
|
5037
|
+
*/
|
|
5038
|
+
function lexer(str) {
|
|
5039
|
+
var tokens = [];
|
|
5040
|
+
var i = 0;
|
|
5041
|
+
while (i < str.length) {
|
|
5042
|
+
var char = str[i];
|
|
5043
|
+
if (char === "*" || char === "+" || char === "?") {
|
|
5044
|
+
tokens.push({ type: "MODIFIER", index: i, value: str[i++] });
|
|
5045
|
+
continue;
|
|
5046
|
+
}
|
|
5047
|
+
if (char === "\\") {
|
|
5048
|
+
tokens.push({ type: "ESCAPED_CHAR", index: i++, value: str[i++] });
|
|
5049
|
+
continue;
|
|
5050
|
+
}
|
|
5051
|
+
if (char === "{") {
|
|
5052
|
+
tokens.push({ type: "OPEN", index: i, value: str[i++] });
|
|
5053
|
+
continue;
|
|
5054
|
+
}
|
|
5055
|
+
if (char === "}") {
|
|
5056
|
+
tokens.push({ type: "CLOSE", index: i, value: str[i++] });
|
|
5057
|
+
continue;
|
|
5058
|
+
}
|
|
5059
|
+
if (char === ":") {
|
|
5060
|
+
var name = "";
|
|
5061
|
+
var j = i + 1;
|
|
5062
|
+
while (j < str.length) {
|
|
5063
|
+
var code = str.charCodeAt(j);
|
|
5064
|
+
if (
|
|
5065
|
+
// `0-9`
|
|
5066
|
+
(code >= 48 && code <= 57) ||
|
|
5067
|
+
// `A-Z`
|
|
5068
|
+
(code >= 65 && code <= 90) ||
|
|
5069
|
+
// `a-z`
|
|
5070
|
+
(code >= 97 && code <= 122) ||
|
|
5071
|
+
// `_`
|
|
5072
|
+
code === 95) {
|
|
5073
|
+
name += str[j++];
|
|
5074
|
+
continue;
|
|
5075
|
+
}
|
|
5076
|
+
break;
|
|
5077
|
+
}
|
|
5078
|
+
if (!name)
|
|
5079
|
+
throw new TypeError("Missing parameter name at " + i);
|
|
5080
|
+
tokens.push({ type: "NAME", index: i, value: name });
|
|
5081
|
+
i = j;
|
|
5082
|
+
continue;
|
|
5083
|
+
}
|
|
5084
|
+
if (char === "(") {
|
|
5085
|
+
var count = 1;
|
|
5086
|
+
var pattern = "";
|
|
5087
|
+
var j = i + 1;
|
|
5088
|
+
if (str[j] === "?") {
|
|
5089
|
+
throw new TypeError("Pattern cannot start with \"?\" at " + j);
|
|
5090
|
+
}
|
|
5091
|
+
while (j < str.length) {
|
|
5092
|
+
if (str[j] === "\\") {
|
|
5093
|
+
pattern += str[j++] + str[j++];
|
|
5094
|
+
continue;
|
|
5095
|
+
}
|
|
5096
|
+
if (str[j] === ")") {
|
|
5097
|
+
count--;
|
|
5098
|
+
if (count === 0) {
|
|
5099
|
+
j++;
|
|
5100
|
+
break;
|
|
5101
|
+
}
|
|
5102
|
+
}
|
|
5103
|
+
else if (str[j] === "(") {
|
|
5104
|
+
count++;
|
|
5105
|
+
if (str[j + 1] !== "?") {
|
|
5106
|
+
throw new TypeError("Capturing groups are not allowed at " + j);
|
|
5107
|
+
}
|
|
5108
|
+
}
|
|
5109
|
+
pattern += str[j++];
|
|
5110
|
+
}
|
|
5111
|
+
if (count)
|
|
5112
|
+
throw new TypeError("Unbalanced pattern at " + i);
|
|
5113
|
+
if (!pattern)
|
|
5114
|
+
throw new TypeError("Missing pattern at " + i);
|
|
5115
|
+
tokens.push({ type: "PATTERN", index: i, value: pattern });
|
|
5116
|
+
i = j;
|
|
5117
|
+
continue;
|
|
5118
|
+
}
|
|
5119
|
+
tokens.push({ type: "CHAR", index: i, value: str[i++] });
|
|
5120
|
+
}
|
|
5121
|
+
tokens.push({ type: "END", index: i, value: "" });
|
|
5122
|
+
return tokens;
|
|
5123
|
+
}
|
|
5124
|
+
/**
|
|
5125
|
+
* Parse a string for the raw tokens.
|
|
5126
|
+
*/
|
|
5127
|
+
function parse(str, options) {
|
|
5128
|
+
if (options === void 0) { options = {}; }
|
|
5129
|
+
var tokens = lexer(str);
|
|
5130
|
+
var _a = options.prefixes, prefixes = _a === void 0 ? "./" : _a;
|
|
5131
|
+
var defaultPattern = "[^" + escapeString(options.delimiter || "/#?") + "]+?";
|
|
5132
|
+
var result = [];
|
|
5133
|
+
var key = 0;
|
|
5134
|
+
var i = 0;
|
|
5135
|
+
var path = "";
|
|
5136
|
+
var tryConsume = function (type) {
|
|
5137
|
+
if (i < tokens.length && tokens[i].type === type)
|
|
5138
|
+
return tokens[i++].value;
|
|
5139
|
+
};
|
|
5140
|
+
var mustConsume = function (type) {
|
|
5141
|
+
var value = tryConsume(type);
|
|
5142
|
+
if (value !== undefined)
|
|
5143
|
+
return value;
|
|
5144
|
+
var _a = tokens[i], nextType = _a.type, index = _a.index;
|
|
5145
|
+
throw new TypeError("Unexpected " + nextType + " at " + index + ", expected " + type);
|
|
5146
|
+
};
|
|
5147
|
+
var consumeText = function () {
|
|
5148
|
+
var result = "";
|
|
5149
|
+
var value;
|
|
5150
|
+
// tslint:disable-next-line
|
|
5151
|
+
while ((value = tryConsume("CHAR") || tryConsume("ESCAPED_CHAR"))) {
|
|
5152
|
+
result += value;
|
|
5153
|
+
}
|
|
5154
|
+
return result;
|
|
5155
|
+
};
|
|
5156
|
+
while (i < tokens.length) {
|
|
5157
|
+
var char = tryConsume("CHAR");
|
|
5158
|
+
var name = tryConsume("NAME");
|
|
5159
|
+
var pattern = tryConsume("PATTERN");
|
|
5160
|
+
if (name || pattern) {
|
|
5161
|
+
var prefix = char || "";
|
|
5162
|
+
if (prefixes.indexOf(prefix) === -1) {
|
|
5163
|
+
path += prefix;
|
|
5164
|
+
prefix = "";
|
|
5165
|
+
}
|
|
5166
|
+
if (path) {
|
|
5167
|
+
result.push(path);
|
|
5168
|
+
path = "";
|
|
5169
|
+
}
|
|
5170
|
+
result.push({
|
|
5171
|
+
name: name || key++,
|
|
5172
|
+
prefix: prefix,
|
|
5173
|
+
suffix: "",
|
|
5174
|
+
pattern: pattern || defaultPattern,
|
|
5175
|
+
modifier: tryConsume("MODIFIER") || ""
|
|
5176
|
+
});
|
|
5177
|
+
continue;
|
|
5178
|
+
}
|
|
5179
|
+
var value = char || tryConsume("ESCAPED_CHAR");
|
|
5180
|
+
if (value) {
|
|
5181
|
+
path += value;
|
|
5182
|
+
continue;
|
|
5183
|
+
}
|
|
5184
|
+
if (path) {
|
|
5185
|
+
result.push(path);
|
|
5186
|
+
path = "";
|
|
5187
|
+
}
|
|
5188
|
+
var open = tryConsume("OPEN");
|
|
5189
|
+
if (open) {
|
|
5190
|
+
var prefix = consumeText();
|
|
5191
|
+
var name_1 = tryConsume("NAME") || "";
|
|
5192
|
+
var pattern_1 = tryConsume("PATTERN") || "";
|
|
5193
|
+
var suffix = consumeText();
|
|
5194
|
+
mustConsume("CLOSE");
|
|
5195
|
+
result.push({
|
|
5196
|
+
name: name_1 || (pattern_1 ? key++ : ""),
|
|
5197
|
+
pattern: name_1 && !pattern_1 ? defaultPattern : pattern_1,
|
|
5198
|
+
prefix: prefix,
|
|
5199
|
+
suffix: suffix,
|
|
5200
|
+
modifier: tryConsume("MODIFIER") || ""
|
|
5201
|
+
});
|
|
5202
|
+
continue;
|
|
5203
|
+
}
|
|
5204
|
+
mustConsume("END");
|
|
5205
|
+
}
|
|
5206
|
+
return result;
|
|
5207
|
+
}
|
|
5208
|
+
/**
|
|
5209
|
+
* Create path match function from `path-to-regexp` spec.
|
|
5210
|
+
*/
|
|
5211
|
+
function match(str, options) {
|
|
5212
|
+
var keys = [];
|
|
5213
|
+
var re = pathToRegexp(str, keys, options);
|
|
5214
|
+
return regexpToFunction(re, keys, options);
|
|
5215
|
+
}
|
|
5216
|
+
/**
|
|
5217
|
+
* Create a path match function from `path-to-regexp` output.
|
|
5218
|
+
*/
|
|
5219
|
+
function regexpToFunction(re, keys, options) {
|
|
5220
|
+
if (options === void 0) { options = {}; }
|
|
5221
|
+
var _a = options.decode, decode = _a === void 0 ? function (x) { return x; } : _a;
|
|
5222
|
+
return function (pathname) {
|
|
5223
|
+
var m = re.exec(pathname);
|
|
5224
|
+
if (!m)
|
|
5225
|
+
return false;
|
|
5226
|
+
var path = m[0], index = m.index;
|
|
5227
|
+
var params = Object.create(null);
|
|
5228
|
+
var _loop_1 = function (i) {
|
|
5229
|
+
// tslint:disable-next-line
|
|
5230
|
+
if (m[i] === undefined)
|
|
5231
|
+
return "continue";
|
|
5232
|
+
var key = keys[i - 1];
|
|
5233
|
+
if (key.modifier === "*" || key.modifier === "+") {
|
|
5234
|
+
params[key.name] = m[i].split(key.prefix + key.suffix).map(function (value) {
|
|
5235
|
+
return decode(value, key);
|
|
5236
|
+
});
|
|
5237
|
+
}
|
|
5238
|
+
else {
|
|
5239
|
+
params[key.name] = decode(m[i], key);
|
|
5240
|
+
}
|
|
5241
|
+
};
|
|
5242
|
+
for (var i = 1; i < m.length; i++) {
|
|
5243
|
+
_loop_1(i);
|
|
5244
|
+
}
|
|
5245
|
+
return { path: path, index: index, params: params };
|
|
5246
|
+
};
|
|
5247
|
+
}
|
|
5248
|
+
/**
|
|
5249
|
+
* Escape a regular expression string.
|
|
5250
|
+
*/
|
|
5251
|
+
function escapeString(str) {
|
|
5252
|
+
return str.replace(/([.+*?=^!:${}()[\]|/\\])/g, "\\$1");
|
|
5253
|
+
}
|
|
5254
|
+
/**
|
|
5255
|
+
* Get the flags for a regexp from the options.
|
|
5442
5256
|
*/
|
|
5443
|
-
function
|
|
5444
|
-
|
|
5445
|
-
|
|
5257
|
+
function flags(options) {
|
|
5258
|
+
return options && options.sensitive ? "" : "i";
|
|
5259
|
+
}
|
|
5260
|
+
/**
|
|
5261
|
+
* Pull out keys from a regexp.
|
|
5262
|
+
*/
|
|
5263
|
+
function regexpToRegexp(path, keys) {
|
|
5264
|
+
if (!keys)
|
|
5265
|
+
return path;
|
|
5266
|
+
var groupsRegex = /\((?:\?<(.*?)>)?(?!\?)/g;
|
|
5267
|
+
var index = 0;
|
|
5268
|
+
var execResult = groupsRegex.exec(path.source);
|
|
5269
|
+
while (execResult) {
|
|
5270
|
+
keys.push({
|
|
5271
|
+
// Use parenthesized substring match if available, index otherwise
|
|
5272
|
+
name: execResult[1] || index++,
|
|
5273
|
+
prefix: "",
|
|
5274
|
+
suffix: "",
|
|
5275
|
+
modifier: "",
|
|
5276
|
+
pattern: ""
|
|
5277
|
+
});
|
|
5278
|
+
execResult = groupsRegex.exec(path.source);
|
|
5279
|
+
}
|
|
5280
|
+
return path;
|
|
5281
|
+
}
|
|
5282
|
+
/**
|
|
5283
|
+
* Transform an array into a regexp.
|
|
5284
|
+
*/
|
|
5285
|
+
function arrayToRegexp(paths, keys, options) {
|
|
5286
|
+
var parts = paths.map(function (path) { return pathToRegexp(path, keys, options).source; });
|
|
5287
|
+
return new RegExp("(?:" + parts.join("|") + ")", flags(options));
|
|
5288
|
+
}
|
|
5289
|
+
/**
|
|
5290
|
+
* Create a path regexp from string input.
|
|
5291
|
+
*/
|
|
5292
|
+
function stringToRegexp(path, keys, options) {
|
|
5293
|
+
return tokensToRegexp(parse(path, options), keys, options);
|
|
5294
|
+
}
|
|
5295
|
+
/**
|
|
5296
|
+
* Expose a function for taking tokens and returning a RegExp.
|
|
5297
|
+
*/
|
|
5298
|
+
function tokensToRegexp(tokens, keys, options) {
|
|
5299
|
+
if (options === void 0) { options = {}; }
|
|
5300
|
+
var _a = options.strict, strict = _a === void 0 ? false : _a, _b = options.start, start = _b === void 0 ? true : _b, _c = options.end, end = _c === void 0 ? true : _c, _d = options.encode, encode = _d === void 0 ? function (x) { return x; } : _d;
|
|
5301
|
+
var endsWith = "[" + escapeString(options.endsWith || "") + "]|$";
|
|
5302
|
+
var delimiter = "[" + escapeString(options.delimiter || "/#?") + "]";
|
|
5303
|
+
var route = start ? "^" : "";
|
|
5304
|
+
// Iterate over the tokens and create our regexp string.
|
|
5305
|
+
for (var _i = 0, tokens_1 = tokens; _i < tokens_1.length; _i++) {
|
|
5306
|
+
var token = tokens_1[_i];
|
|
5307
|
+
if (typeof token === "string") {
|
|
5308
|
+
route += escapeString(encode(token));
|
|
5309
|
+
}
|
|
5310
|
+
else {
|
|
5311
|
+
var prefix = escapeString(encode(token.prefix));
|
|
5312
|
+
var suffix = escapeString(encode(token.suffix));
|
|
5313
|
+
if (token.pattern) {
|
|
5314
|
+
if (keys)
|
|
5315
|
+
keys.push(token);
|
|
5316
|
+
if (prefix || suffix) {
|
|
5317
|
+
if (token.modifier === "+" || token.modifier === "*") {
|
|
5318
|
+
var mod = token.modifier === "*" ? "?" : "";
|
|
5319
|
+
route += "(?:" + prefix + "((?:" + token.pattern + ")(?:" + suffix + prefix + "(?:" + token.pattern + "))*)" + suffix + ")" + mod;
|
|
5320
|
+
}
|
|
5321
|
+
else {
|
|
5322
|
+
route += "(?:" + prefix + "(" + token.pattern + ")" + suffix + ")" + token.modifier;
|
|
5323
|
+
}
|
|
5324
|
+
}
|
|
5325
|
+
else {
|
|
5326
|
+
route += "(" + token.pattern + ")" + token.modifier;
|
|
5327
|
+
}
|
|
5328
|
+
}
|
|
5329
|
+
else {
|
|
5330
|
+
route += "(?:" + prefix + suffix + ")" + token.modifier;
|
|
5331
|
+
}
|
|
5332
|
+
}
|
|
5333
|
+
}
|
|
5334
|
+
if (end) {
|
|
5335
|
+
if (!strict)
|
|
5336
|
+
route += delimiter + "?";
|
|
5337
|
+
route += !options.endsWith ? "$" : "(?=" + endsWith + ")";
|
|
5338
|
+
}
|
|
5339
|
+
else {
|
|
5340
|
+
var endToken = tokens[tokens.length - 1];
|
|
5341
|
+
var isEndDelimited = typeof endToken === "string"
|
|
5342
|
+
? delimiter.indexOf(endToken[endToken.length - 1]) > -1
|
|
5343
|
+
: // tslint:disable-next-line
|
|
5344
|
+
endToken === undefined;
|
|
5345
|
+
if (!strict) {
|
|
5346
|
+
route += "(?:" + delimiter + "(?=" + endsWith + "))?";
|
|
5347
|
+
}
|
|
5348
|
+
if (!isEndDelimited) {
|
|
5349
|
+
route += "(?=" + delimiter + "|" + endsWith + ")";
|
|
5350
|
+
}
|
|
5351
|
+
}
|
|
5352
|
+
return new RegExp(route, flags(options));
|
|
5353
|
+
}
|
|
5354
|
+
/**
|
|
5355
|
+
* Normalize the given path string, returning a regular expression.
|
|
5356
|
+
*
|
|
5357
|
+
* An empty array can be passed in for the keys, which will hold the
|
|
5358
|
+
* placeholder key descriptions. For example, using `/user/:id`, `keys` will
|
|
5359
|
+
* contain `[{ name: 'id', delimiter: '/', optional: false, repeat: false }]`.
|
|
5360
|
+
*/
|
|
5361
|
+
function pathToRegexp(path, keys, options) {
|
|
5362
|
+
if (path instanceof RegExp)
|
|
5363
|
+
return regexpToRegexp(path, keys);
|
|
5364
|
+
if (Array.isArray(path))
|
|
5365
|
+
return arrayToRegexp(path, keys, options);
|
|
5366
|
+
return stringToRegexp(path, keys, options);
|
|
5446
5367
|
}
|
|
5447
|
-
getCleanUrl_2 = getCleanUrl$1.getCleanUrl = getCleanUrl;
|
|
5448
5368
|
|
|
5449
5369
|
const REDUNDANT_CHARACTERS_EXP = /[\?|#].*$/g;
|
|
5450
5370
|
function getSearchParams(path) {
|
|
@@ -5466,8 +5386,8 @@ function getAbsoluteUrl(path, baseUrl) {
|
|
|
5466
5386
|
return path;
|
|
5467
5387
|
}
|
|
5468
5388
|
// Resolve a relative request URL against a given custom "baseUrl"
|
|
5469
|
-
// or the
|
|
5470
|
-
const origin = baseUrl || (typeof
|
|
5389
|
+
// or the document baseURI (in the case of browser/browser-like environments).
|
|
5390
|
+
const origin = baseUrl || (typeof document !== 'undefined' && document.baseURI);
|
|
5471
5391
|
return origin
|
|
5472
5392
|
? // Encode and decode the path to preserve escaped characters.
|
|
5473
5393
|
decodeURI(new URL(encodeURI(path), origin).href)
|
|
@@ -5490,12 +5410,48 @@ function normalizePath(path, baseUrl) {
|
|
|
5490
5410
|
return cleanUrl(maybeAbsoluteUrl);
|
|
5491
5411
|
}
|
|
5492
5412
|
|
|
5413
|
+
/**
|
|
5414
|
+
* Coerce a path supported by MSW into a path
|
|
5415
|
+
* supported by "path-to-regexp".
|
|
5416
|
+
*/
|
|
5417
|
+
function coercePath(path) {
|
|
5418
|
+
return (path
|
|
5419
|
+
/**
|
|
5420
|
+
* Replace wildcards ("*") with unnamed capturing groups
|
|
5421
|
+
* because "path-to-regexp" doesn't support wildcards.
|
|
5422
|
+
* Ignore path parameter' modifiers (i.e. ":name*").
|
|
5423
|
+
*/
|
|
5424
|
+
.replace(/([:a-zA-Z_-]*)(\*{1,2})+/g, (_, parameterName, wildcard) => {
|
|
5425
|
+
const expression = '(.*)';
|
|
5426
|
+
if (!parameterName) {
|
|
5427
|
+
return expression;
|
|
5428
|
+
}
|
|
5429
|
+
return parameterName.startsWith(':')
|
|
5430
|
+
? `${parameterName}${wildcard}`
|
|
5431
|
+
: `${parameterName}${expression}`;
|
|
5432
|
+
})
|
|
5433
|
+
/**
|
|
5434
|
+
* Escape the protocol so that "path-to-regexp" could match
|
|
5435
|
+
* absolute URL.
|
|
5436
|
+
* @see https://github.com/pillarjs/path-to-regexp/issues/259
|
|
5437
|
+
*/
|
|
5438
|
+
.replace(/^([^\/]+)(:)(?=\/\/)/g, '$1\\$2'));
|
|
5439
|
+
}
|
|
5493
5440
|
/**
|
|
5494
5441
|
* Returns the result of matching given request URL against a mask.
|
|
5495
5442
|
*/
|
|
5496
5443
|
function matchRequestUrl(url, path, baseUrl) {
|
|
5497
5444
|
const normalizedPath = normalizePath(path, baseUrl);
|
|
5498
|
-
|
|
5445
|
+
const cleanPath = typeof normalizedPath === 'string'
|
|
5446
|
+
? coercePath(normalizedPath)
|
|
5447
|
+
: normalizedPath;
|
|
5448
|
+
const cleanUrl = getCleanUrl.getCleanUrl(url);
|
|
5449
|
+
const result = match(cleanPath, { decode: decodeURIComponent })(cleanUrl);
|
|
5450
|
+
const params = (result && result.params) || {};
|
|
5451
|
+
return {
|
|
5452
|
+
matches: result !== false,
|
|
5453
|
+
params,
|
|
5454
|
+
};
|
|
5499
5455
|
}
|
|
5500
5456
|
|
|
5501
5457
|
/**
|
|
@@ -5530,7 +5486,7 @@ const defaultResponseTransformers = [];
|
|
|
5530
5486
|
function createResponseComposition(responseOverrides, defaultTransformers = defaultResponseTransformers) {
|
|
5531
5487
|
return (...transformers) => __awaiter(this, void 0, void 0, function* () {
|
|
5532
5488
|
const initialResponse = Object.assign({}, defaultResponse, {
|
|
5533
|
-
headers: new lib$
|
|
5489
|
+
headers: new lib$1.Headers({
|
|
5534
5490
|
'x-powered-by': 'msw',
|
|
5535
5491
|
}),
|
|
5536
5492
|
}, responseOverrides);
|
|
@@ -5551,18 +5507,21 @@ const response = Object.assign(createResponseComposition(), {
|
|
|
5551
5507
|
},
|
|
5552
5508
|
});
|
|
5553
5509
|
|
|
5510
|
+
const BUILD_FRAME = /(node_modules)?[\/\\]lib[\/\\](umd|esm|iief|cjs)[\/\\]|^[^\/\\]*$/;
|
|
5554
5511
|
/**
|
|
5555
5512
|
* Return the stack trace frame of a function's invocation.
|
|
5556
5513
|
*/
|
|
5557
|
-
function getCallFrame() {
|
|
5514
|
+
function getCallFrame(error) {
|
|
5558
5515
|
// In <IE11, new Error may return an undefined stack
|
|
5559
|
-
const stack =
|
|
5560
|
-
|
|
5516
|
+
const stack = error.stack;
|
|
5517
|
+
if (!stack) {
|
|
5518
|
+
return;
|
|
5519
|
+
}
|
|
5520
|
+
const frames = stack.split('\n').slice(1);
|
|
5561
5521
|
// Get the first frame that doesn't reference the library's internal trace.
|
|
5562
5522
|
// Assume that frame is the invocation frame.
|
|
5563
|
-
const
|
|
5564
|
-
|
|
5565
|
-
return !ignoreFrameRegExp.test(frame);
|
|
5523
|
+
const declarationFrame = frames.find((frame) => {
|
|
5524
|
+
return !BUILD_FRAME.test(frame);
|
|
5566
5525
|
});
|
|
5567
5526
|
if (!declarationFrame) {
|
|
5568
5527
|
return;
|
|
@@ -5595,7 +5554,7 @@ class RequestHandler {
|
|
|
5595
5554
|
this.shouldSkip = false;
|
|
5596
5555
|
this.ctx = options.ctx || defaultContext;
|
|
5597
5556
|
this.resolver = options.resolver;
|
|
5598
|
-
const callFrame = getCallFrame();
|
|
5557
|
+
const callFrame = getCallFrame(new Error());
|
|
5599
5558
|
this.info = Object.assign(Object.assign({}, options.info), { callFrame });
|
|
5600
5559
|
}
|
|
5601
5560
|
/**
|
|
@@ -5722,21 +5681,9 @@ class RestHandler extends RequestHandler {
|
|
|
5722
5681
|
return;
|
|
5723
5682
|
}
|
|
5724
5683
|
const searchParams = getSearchParams(path);
|
|
5725
|
-
const queryParams = [];
|
|
5726
5684
|
searchParams.forEach((_, paramName) => {
|
|
5727
|
-
queryParams.push(paramName);
|
|
5728
5685
|
});
|
|
5729
|
-
devUtils.warn(
|
|
5730
|
-
Found a redundant usage of query parameters in the request handler URL for "${method} ${path}". Please match against a path instead, and access query parameters in the response resolver function:
|
|
5731
|
-
|
|
5732
|
-
rest.${method.toLowerCase()}("${url}", (req, res, ctx) => {
|
|
5733
|
-
const query = req.url.searchParams
|
|
5734
|
-
${queryParams
|
|
5735
|
-
.map((paramName) => `\
|
|
5736
|
-
const ${paramName} = query.get("${paramName}")`)
|
|
5737
|
-
.join('\n')}
|
|
5738
|
-
})\
|
|
5739
|
-
`);
|
|
5686
|
+
devUtils.warn(`Found a redundant usage of query parameters in the request handler URL for "${method} ${path}". Please match against a path instead and access query parameters in the response resolver function using "req.url.searchParams".`);
|
|
5740
5687
|
}
|
|
5741
5688
|
parse(request, resolutionContext) {
|
|
5742
5689
|
return matchRequestUrl(request.url, this.info.path, resolutionContext === null || resolutionContext === void 0 ? void 0 : resolutionContext.baseUrl);
|
|
@@ -5745,7 +5692,10 @@ ${queryParams
|
|
|
5745
5692
|
return Object.assign(Object.assign({}, request), { params: parsedResult.params || {} });
|
|
5746
5693
|
}
|
|
5747
5694
|
predicate(request, parsedResult) {
|
|
5748
|
-
|
|
5695
|
+
const matchesMethod = this.info.method instanceof RegExp
|
|
5696
|
+
? this.info.method.test(request.method)
|
|
5697
|
+
: isStringEqual(this.info.method, request.method);
|
|
5698
|
+
return matchesMethod && parsedResult.matches;
|
|
5749
5699
|
}
|
|
5750
5700
|
log(request, response) {
|
|
5751
5701
|
const publicUrl = getPublicUrlFromRequest(request);
|
|
@@ -5779,6 +5729,7 @@ const graphqlContext = {
|
|
|
5779
5729
|
delay,
|
|
5780
5730
|
fetch,
|
|
5781
5731
|
data,
|
|
5732
|
+
extensions,
|
|
5782
5733
|
errors,
|
|
5783
5734
|
cookie,
|
|
5784
5735
|
};
|
|
@@ -5825,10 +5776,10 @@ class GraphQLHandler extends RequestHandler {
|
|
|
5825
5776
|
if (!parsedResult) {
|
|
5826
5777
|
return false;
|
|
5827
5778
|
}
|
|
5828
|
-
if (!parsedResult.operationName) {
|
|
5779
|
+
if (!parsedResult.operationName && this.info.operationType !== 'all') {
|
|
5829
5780
|
const publicUrl = getPublicUrlFromRequest(request);
|
|
5830
5781
|
devUtils.warn(`\
|
|
5831
|
-
Failed to intercept a GraphQL request at "${request.method} ${publicUrl}":
|
|
5782
|
+
Failed to intercept a GraphQL request at "${request.method} ${publicUrl}": anonymous GraphQL operations are not supported.
|
|
5832
5783
|
|
|
5833
5784
|
Consider naming this operation or using "graphql.operation" request handler to intercept GraphQL requests regardless of their operation name/type. Read more: https://mswjs.io/docs/api/graphql/operation\
|
|
5834
5785
|
`);
|
|
@@ -5838,7 +5789,7 @@ Consider naming this operation or using "graphql.operation" request handler to i
|
|
|
5838
5789
|
const hasMatchingOperationType = this.info.operationType === 'all' ||
|
|
5839
5790
|
parsedResult.operationType === this.info.operationType;
|
|
5840
5791
|
const hasMatchingOperationName = this.info.operationName instanceof RegExp
|
|
5841
|
-
? this.info.operationName.test(parsedResult.operationName)
|
|
5792
|
+
? this.info.operationName.test(parsedResult.operationName || '')
|
|
5842
5793
|
: parsedResult.operationName === this.info.operationName;
|
|
5843
5794
|
return (hasMatchingUrl.matches &&
|
|
5844
5795
|
hasMatchingOperationType &&
|
|
@@ -5848,7 +5799,10 @@ Consider naming this operation or using "graphql.operation" request handler to i
|
|
|
5848
5799
|
const loggedRequest = prepareRequest(request);
|
|
5849
5800
|
const loggedResponse = prepareResponse(response);
|
|
5850
5801
|
const statusColor = getStatusCodeColor(response.status);
|
|
5851
|
-
|
|
5802
|
+
const requestInfo = (parsedRequest === null || parsedRequest === void 0 ? void 0 : parsedRequest.operationName)
|
|
5803
|
+
? `${parsedRequest === null || parsedRequest === void 0 ? void 0 : parsedRequest.operationType} ${parsedRequest === null || parsedRequest === void 0 ? void 0 : parsedRequest.operationName}`
|
|
5804
|
+
: `anonymous ${parsedRequest === null || parsedRequest === void 0 ? void 0 : parsedRequest.operationType}`;
|
|
5805
|
+
console.groupCollapsed(devUtils.formatMessage('%s %s (%c%s%c)'), getTimestamp(), `${requestInfo}`, `color:${statusColor}`, `${response.status} ${response.statusText}`, 'color:inherit');
|
|
5852
5806
|
console.log('Request:', loggedRequest);
|
|
5853
5807
|
console.log('Handler:', this);
|
|
5854
5808
|
console.log('Response:', loggedResponse);
|
|
@@ -5873,10 +5827,10 @@ function groupHandlersByType(handlers) {
|
|
|
5873
5827
|
graphql: [],
|
|
5874
5828
|
});
|
|
5875
5829
|
}
|
|
5876
|
-
function
|
|
5830
|
+
function getRestHandlerScore() {
|
|
5877
5831
|
return (request, handler) => {
|
|
5878
5832
|
const { path, method } = handler.info;
|
|
5879
|
-
if (path instanceof RegExp) {
|
|
5833
|
+
if (path instanceof RegExp || method instanceof RegExp) {
|
|
5880
5834
|
return Infinity;
|
|
5881
5835
|
}
|
|
5882
5836
|
const hasSameMethod = isStringEqual(request.method, method);
|
|
@@ -5887,12 +5841,15 @@ function getScoreForRestHandler() {
|
|
|
5887
5841
|
return score - methodScoreDelta;
|
|
5888
5842
|
};
|
|
5889
5843
|
}
|
|
5890
|
-
function
|
|
5844
|
+
function getGraphQLHandlerScore(parsedQuery) {
|
|
5891
5845
|
return (_, handler) => {
|
|
5892
5846
|
if (typeof parsedQuery.operationName === 'undefined') {
|
|
5893
5847
|
return Infinity;
|
|
5894
5848
|
}
|
|
5895
5849
|
const { operationType, operationName } = handler.info;
|
|
5850
|
+
if (typeof operationName !== 'string') {
|
|
5851
|
+
return Infinity;
|
|
5852
|
+
}
|
|
5896
5853
|
const hasSameOperationType = parsedQuery.operationType === operationType;
|
|
5897
5854
|
// Always treat a handler with the same operation type as a more similar one.
|
|
5898
5855
|
const operationTypeScoreDelta = hasSameOperationType ? TYPE_MATCH_DELTA : 0;
|
|
@@ -5902,16 +5859,12 @@ function getScoreForGraphQLHandler(parsedQuery) {
|
|
|
5902
5859
|
}
|
|
5903
5860
|
function getSuggestedHandler(request, handlers, getScore) {
|
|
5904
5861
|
const suggestedHandlers = handlers
|
|
5905
|
-
.reduce((
|
|
5862
|
+
.reduce((suggestions, handler) => {
|
|
5906
5863
|
const score = getScore(request, handler);
|
|
5907
|
-
return
|
|
5864
|
+
return suggestions.concat([[score, handler]]);
|
|
5908
5865
|
}, [])
|
|
5909
|
-
.sort(([leftScore], [rightScore]) =>
|
|
5910
|
-
|
|
5911
|
-
})
|
|
5912
|
-
.filter(([score]) => {
|
|
5913
|
-
return score <= MAX_MATCH_SCORE;
|
|
5914
|
-
})
|
|
5866
|
+
.sort(([leftScore], [rightScore]) => leftScore - rightScore)
|
|
5867
|
+
.filter(([score]) => score <= MAX_MATCH_SCORE)
|
|
5915
5868
|
.slice(0, MAX_SUGGESTION_COUNT)
|
|
5916
5869
|
.map(([, handler]) => handler);
|
|
5917
5870
|
return suggestedHandlers;
|
|
@@ -5941,8 +5894,8 @@ function onUnhandledRequest(request, handlers, strategy = 'warn') {
|
|
|
5941
5894
|
? handlerGroups.graphql
|
|
5942
5895
|
: handlerGroups.rest;
|
|
5943
5896
|
const suggestedHandlers = getSuggestedHandler(request, relevantHandlers, parsedGraphQLQuery
|
|
5944
|
-
?
|
|
5945
|
-
:
|
|
5897
|
+
? getGraphQLHandlerScore(parsedGraphQLQuery)
|
|
5898
|
+
: getRestHandlerScore());
|
|
5946
5899
|
const handlerSuggestion = suggestedHandlers.length > 0
|
|
5947
5900
|
? getSuggestedHandlersMessage(suggestedHandlers)
|
|
5948
5901
|
: '';
|
|
@@ -5965,7 +5918,7 @@ Read more: https://mswjs.io/docs/getting-started/mocks\
|
|
|
5965
5918
|
// Print a developer-friendly error.
|
|
5966
5919
|
devUtils.error('Error: %s', message);
|
|
5967
5920
|
// Throw an exception to halt request processing and not perform the original request.
|
|
5968
|
-
throw new Error('Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.');
|
|
5921
|
+
throw new Error(devUtils.formatMessage('Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.'));
|
|
5969
5922
|
}
|
|
5970
5923
|
case 'warn': {
|
|
5971
5924
|
devUtils.warn('Warning: %s', message);
|
|
@@ -5979,8 +5932,8 @@ Read more: https://mswjs.io/docs/getting-started/mocks\
|
|
|
5979
5932
|
}
|
|
5980
5933
|
|
|
5981
5934
|
function readResponseCookies(request, response) {
|
|
5982
|
-
|
|
5983
|
-
|
|
5935
|
+
cookies.store.add(Object.assign(Object.assign({}, request), { url: request.url.toString() }), response);
|
|
5936
|
+
cookies.store.persist();
|
|
5984
5937
|
}
|
|
5985
5938
|
|
|
5986
5939
|
function handleRequest(request, handlers, options, emitter, handleRequestOptions) {
|
|
@@ -6008,7 +5961,12 @@ function handleRequest(request, handlers, options, emitter, handleRequestOptions
|
|
|
6008
5961
|
// When the handled request returned no mocked response, warn the developer,
|
|
6009
5962
|
// as it may be an oversight on their part. Perform the request as-is.
|
|
6010
5963
|
if (!response) {
|
|
6011
|
-
devUtils.warn(
|
|
5964
|
+
devUtils.warn(`\
|
|
5965
|
+
Expected response resolver to return a mocked response Object, but got %s. The original response is going to be used instead.\
|
|
5966
|
+
\n
|
|
5967
|
+
\u2022 %s
|
|
5968
|
+
%s\
|
|
5969
|
+
`, response, handler.info.header, handler.info.callFrame);
|
|
6012
5970
|
emitter.emit('request:end', request);
|
|
6013
5971
|
(_c = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.onBypassResponse) === null || _c === void 0 ? void 0 : _c.call(handleRequestOptions, request);
|
|
6014
5972
|
return;
|
|
@@ -6057,8 +6015,8 @@ const DEFAULT_LISTEN_OPTIONS = {
|
|
|
6057
6015
|
* Useful to generate identical API using different patches to request issuing modules.
|
|
6058
6016
|
*/
|
|
6059
6017
|
function createSetupServer(...interceptors$1) {
|
|
6060
|
-
const emitter = new lib$
|
|
6061
|
-
const publicEmitter = new lib$
|
|
6018
|
+
const emitter = new lib$2.StrictEventEmitter();
|
|
6019
|
+
const publicEmitter = new lib$2.StrictEventEmitter();
|
|
6062
6020
|
pipeEvents(emitter, publicEmitter);
|
|
6063
6021
|
return function setupServer(...requestHandlers) {
|
|
6064
6022
|
requestHandlers.forEach((handler) => {
|
|
@@ -6069,7 +6027,7 @@ function createSetupServer(...interceptors$1) {
|
|
|
6069
6027
|
// so it could be modified at a runtime.
|
|
6070
6028
|
let currentHandlers = [...requestHandlers];
|
|
6071
6029
|
// Error when attempting to run this function in a browser environment.
|
|
6072
|
-
if (!lib$
|
|
6030
|
+
if (!lib$3.exports.isNodeProcess()) {
|
|
6073
6031
|
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.'));
|
|
6074
6032
|
}
|
|
6075
6033
|
let resolvedOptions = {};
|
|
@@ -6092,15 +6050,14 @@ function createSetupServer(...interceptors$1) {
|
|
|
6092
6050
|
},
|
|
6093
6051
|
});
|
|
6094
6052
|
interceptor.on('response', (request, response) => {
|
|
6095
|
-
|
|
6096
|
-
if (!requestId) {
|
|
6053
|
+
if (!request.id) {
|
|
6097
6054
|
return;
|
|
6098
6055
|
}
|
|
6099
6056
|
if (response.headers.get('x-powered-by') === 'msw') {
|
|
6100
|
-
emitter.emit('response:mocked', response,
|
|
6057
|
+
emitter.emit('response:mocked', response, request.id);
|
|
6101
6058
|
}
|
|
6102
6059
|
else {
|
|
6103
|
-
emitter.emit('response:bypass', response,
|
|
6060
|
+
emitter.emit('response:bypass', response, request.id);
|
|
6104
6061
|
}
|
|
6105
6062
|
});
|
|
6106
6063
|
return {
|