msw 0.34.0 → 0.36.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/esm/RequestHandler-deps.js +391 -66
- package/lib/esm/errors-deps.js +15 -1
- package/lib/esm/fetch-deps.js +9 -5
- package/lib/esm/graphql-deps.js +10 -6
- package/lib/esm/graphql.js +1 -0
- package/lib/esm/index-deps.js +2 -1
- package/lib/esm/index.js +102 -3893
- 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 +3 -3
- 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/glossary.d.ts +4 -14
- 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 +16 -26
- package/lib/types/setupWorker/start/utils/prepareStartHandler.d.ts +3 -3
- package/lib/types/sharedOptions.d.ts +12 -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 +4 -4
- 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/pipeEvents.d.ts +6 -0
- 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 +1070 -243
- package/lib/umd/mockServiceWorker.js +5 -5
- package/native/lib/index.js +501 -501
- package/node/lib/index.js +503 -503
- package/package.json +19 -19
package/node/lib/index.js
CHANGED
|
@@ -8,7 +8,9 @@ var require$$0 = require('os');
|
|
|
8
8
|
var require$$1 = require('tty');
|
|
9
9
|
var require$$0$1 = require('events');
|
|
10
10
|
var interceptors = require('@mswjs/interceptors');
|
|
11
|
+
var cookies = require('@mswjs/cookies');
|
|
11
12
|
var timers = require('timers');
|
|
13
|
+
var getCleanUrl = require('@mswjs/interceptors/lib/utils/getCleanUrl');
|
|
12
14
|
|
|
13
15
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
14
16
|
|
|
@@ -1400,8 +1402,8 @@ var hasFlag$1 = (flag, argv = process.argv) => {
|
|
|
1400
1402
|
return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
|
|
1401
1403
|
};
|
|
1402
1404
|
|
|
1403
|
-
const os = require$$0__default[
|
|
1404
|
-
const tty = require$$1__default[
|
|
1405
|
+
const os = require$$0__default["default"];
|
|
1406
|
+
const tty = require$$1__default["default"];
|
|
1405
1407
|
const hasFlag = hasFlag$1;
|
|
1406
1408
|
|
|
1407
1409
|
const {env} = process;
|
|
@@ -1936,7 +1938,7 @@ chalk.stderr.supportsColor = stderrColor;
|
|
|
1936
1938
|
|
|
1937
1939
|
var source = chalk;
|
|
1938
1940
|
|
|
1939
|
-
var lib$
|
|
1941
|
+
var lib$3 = {exports: {}};
|
|
1940
1942
|
|
|
1941
1943
|
(function (module, exports) {
|
|
1942
1944
|
(function (global, factory) {
|
|
@@ -1959,9 +1961,9 @@ var lib$4 = {exports: {}};
|
|
|
1959
1961
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
1960
1962
|
|
|
1961
1963
|
})));
|
|
1962
|
-
}(lib$
|
|
1964
|
+
}(lib$3, lib$3.exports));
|
|
1963
1965
|
|
|
1964
|
-
var lib$
|
|
1966
|
+
var lib$2 = {};
|
|
1965
1967
|
|
|
1966
1968
|
var StrictEventEmitter$1 = {};
|
|
1967
1969
|
|
|
@@ -1987,7 +1989,7 @@ var __spreadArrays = (commonjsGlobal && commonjsGlobal.__spreadArrays) || functi
|
|
|
1987
1989
|
};
|
|
1988
1990
|
StrictEventEmitter$1.__esModule = true;
|
|
1989
1991
|
StrictEventEmitter$1.StrictEventEmitter = void 0;
|
|
1990
|
-
var events_1 = require$$0__default$1[
|
|
1992
|
+
var events_1 = require$$0__default$1["default"];
|
|
1991
1993
|
var StrictEventEmitter = /** @class */ (function (_super) {
|
|
1992
1994
|
__extends$1(StrictEventEmitter, _super);
|
|
1993
1995
|
function StrictEventEmitter() {
|
|
@@ -2031,7 +2033,7 @@ exports.__esModule = true;
|
|
|
2031
2033
|
exports.StrictEventEmitter = void 0;
|
|
2032
2034
|
var StrictEventEmitter_1 = StrictEventEmitter$1;
|
|
2033
2035
|
__createBinding(exports, StrictEventEmitter_1, "StrictEventEmitter");
|
|
2034
|
-
}(lib$
|
|
2036
|
+
}(lib$2));
|
|
2035
2037
|
|
|
2036
2038
|
function use(currentHandlers, ...handlers) {
|
|
2037
2039
|
currentHandlers.unshift(...handlers);
|
|
@@ -2255,19 +2257,19 @@ function uuidv4() {
|
|
|
2255
2257
|
}
|
|
2256
2258
|
|
|
2257
2259
|
/**
|
|
2258
|
-
* Parses a given
|
|
2260
|
+
* Parses a given value into a JSON.
|
|
2259
2261
|
* Does not throw an exception on an invalid JSON string.
|
|
2260
2262
|
*/
|
|
2261
|
-
function jsonParse(
|
|
2263
|
+
function jsonParse(value) {
|
|
2262
2264
|
try {
|
|
2263
|
-
return JSON.parse(
|
|
2265
|
+
return JSON.parse(value);
|
|
2264
2266
|
}
|
|
2265
2267
|
catch (error) {
|
|
2266
2268
|
return undefined;
|
|
2267
2269
|
}
|
|
2268
2270
|
}
|
|
2269
2271
|
|
|
2270
|
-
var lib$
|
|
2272
|
+
var lib$1 = {};
|
|
2271
2273
|
|
|
2272
2274
|
var Headers = {};
|
|
2273
2275
|
|
|
@@ -2791,11 +2793,11 @@ var flattenHeadersList_1 = flattenHeadersList$1;
|
|
|
2791
2793
|
Object.defineProperty(exports, "flattenHeadersList", { enumerable: true, get: function () { return flattenHeadersList_1.flattenHeadersList; } });
|
|
2792
2794
|
var flattenHeadersObject_1 = flattenHeadersObject$1;
|
|
2793
2795
|
Object.defineProperty(exports, "flattenHeadersObject", { enumerable: true, get: function () { return flattenHeadersObject_1.flattenHeadersObject; } });
|
|
2794
|
-
}(lib$
|
|
2796
|
+
}(lib$1));
|
|
2795
2797
|
|
|
2796
2798
|
function parseContentHeaders(headersString) {
|
|
2797
2799
|
var _a, _b;
|
|
2798
|
-
const headers = lib$
|
|
2800
|
+
const headers = lib$1.stringToHeaders(headersString);
|
|
2799
2801
|
const contentType = headers.get('content-type') || 'text/plain';
|
|
2800
2802
|
const disposition = headers.get('content-disposition');
|
|
2801
2803
|
if (!disposition) {
|
|
@@ -2866,403 +2868,30 @@ function parseMultipartData(data, headers) {
|
|
|
2866
2868
|
}
|
|
2867
2869
|
|
|
2868
2870
|
/**
|
|
2869
|
-
* Parses a given request/response body based on the
|
|
2871
|
+
* Parses a given request/response body based on the "Content-Type" header.
|
|
2870
2872
|
*/
|
|
2871
2873
|
function parseBody(body, headers) {
|
|
2872
2874
|
// Return whatever falsey body value is given.
|
|
2873
2875
|
if (!body) {
|
|
2874
2876
|
return body;
|
|
2875
2877
|
}
|
|
2876
|
-
const contentType = headers === null || headers === void 0 ? void 0 : headers.get('content-type');
|
|
2878
|
+
const contentType = (headers === null || headers === void 0 ? void 0 : headers.get('content-type')) || '';
|
|
2877
2879
|
// If the body has a Multipart Content-Type
|
|
2878
2880
|
// parse it into an object.
|
|
2879
|
-
const hasMultipartContent = contentType
|
|
2881
|
+
const hasMultipartContent = contentType.startsWith('multipart/form-data');
|
|
2880
2882
|
if (hasMultipartContent && typeof body !== 'object') {
|
|
2881
|
-
return parseMultipartData(body, headers) || body;
|
|
2883
|
+
return parseMultipartData(body.toString(), headers) || body;
|
|
2882
2884
|
}
|
|
2883
2885
|
// If the intercepted request's body has a JSON Content-Type
|
|
2884
2886
|
// parse it into an object.
|
|
2885
|
-
const hasJsonContent = contentType
|
|
2887
|
+
const hasJsonContent = contentType.includes('json');
|
|
2886
2888
|
if (hasJsonContent && typeof body !== 'object') {
|
|
2887
|
-
return jsonParse(body) || body;
|
|
2889
|
+
return jsonParse(body.toString()) || body;
|
|
2888
2890
|
}
|
|
2889
2891
|
// Otherwise leave as-is.
|
|
2890
2892
|
return body;
|
|
2891
2893
|
}
|
|
2892
2894
|
|
|
2893
|
-
var lib$1 = {};
|
|
2894
|
-
|
|
2895
|
-
var CookieStore = {};
|
|
2896
|
-
|
|
2897
|
-
var setCookie = {exports: {}};
|
|
2898
|
-
|
|
2899
|
-
var defaultParseOptions = {
|
|
2900
|
-
decodeValues: true,
|
|
2901
|
-
map: false,
|
|
2902
|
-
silent: false,
|
|
2903
|
-
};
|
|
2904
|
-
|
|
2905
|
-
function isNonEmptyString(str) {
|
|
2906
|
-
return typeof str === "string" && !!str.trim();
|
|
2907
|
-
}
|
|
2908
|
-
|
|
2909
|
-
function parseString(setCookieValue, options) {
|
|
2910
|
-
var parts = setCookieValue.split(";").filter(isNonEmptyString);
|
|
2911
|
-
var nameValue = parts.shift().split("=");
|
|
2912
|
-
var name = nameValue.shift();
|
|
2913
|
-
var value = nameValue.join("="); // everything after the first =, joined by a "=" if there was more than one part
|
|
2914
|
-
|
|
2915
|
-
options = options
|
|
2916
|
-
? Object.assign({}, defaultParseOptions, options)
|
|
2917
|
-
: defaultParseOptions;
|
|
2918
|
-
|
|
2919
|
-
try {
|
|
2920
|
-
value = options.decodeValues ? decodeURIComponent(value) : value; // decode cookie value
|
|
2921
|
-
} catch (e) {
|
|
2922
|
-
console.error(
|
|
2923
|
-
"set-cookie-parser encountered an error while decoding a cookie with value '" +
|
|
2924
|
-
value +
|
|
2925
|
-
"'. Set options.decodeValues to false to disable this feature.",
|
|
2926
|
-
e
|
|
2927
|
-
);
|
|
2928
|
-
}
|
|
2929
|
-
|
|
2930
|
-
var cookie = {
|
|
2931
|
-
name: name, // grab everything before the first =
|
|
2932
|
-
value: value,
|
|
2933
|
-
};
|
|
2934
|
-
|
|
2935
|
-
parts.forEach(function (part) {
|
|
2936
|
-
var sides = part.split("=");
|
|
2937
|
-
var key = sides.shift().trimLeft().toLowerCase();
|
|
2938
|
-
var value = sides.join("=");
|
|
2939
|
-
if (key === "expires") {
|
|
2940
|
-
cookie.expires = new Date(value);
|
|
2941
|
-
} else if (key === "max-age") {
|
|
2942
|
-
cookie.maxAge = parseInt(value, 10);
|
|
2943
|
-
} else if (key === "secure") {
|
|
2944
|
-
cookie.secure = true;
|
|
2945
|
-
} else if (key === "httponly") {
|
|
2946
|
-
cookie.httpOnly = true;
|
|
2947
|
-
} else if (key === "samesite") {
|
|
2948
|
-
cookie.sameSite = value;
|
|
2949
|
-
} else {
|
|
2950
|
-
cookie[key] = value;
|
|
2951
|
-
}
|
|
2952
|
-
});
|
|
2953
|
-
|
|
2954
|
-
return cookie;
|
|
2955
|
-
}
|
|
2956
|
-
|
|
2957
|
-
function parse$1(input, options) {
|
|
2958
|
-
options = options
|
|
2959
|
-
? Object.assign({}, defaultParseOptions, options)
|
|
2960
|
-
: defaultParseOptions;
|
|
2961
|
-
|
|
2962
|
-
if (!input) {
|
|
2963
|
-
if (!options.map) {
|
|
2964
|
-
return [];
|
|
2965
|
-
} else {
|
|
2966
|
-
return {};
|
|
2967
|
-
}
|
|
2968
|
-
}
|
|
2969
|
-
|
|
2970
|
-
if (input.headers && input.headers["set-cookie"]) {
|
|
2971
|
-
// fast-path for node.js (which automatically normalizes header names to lower-case
|
|
2972
|
-
input = input.headers["set-cookie"];
|
|
2973
|
-
} else if (input.headers) {
|
|
2974
|
-
// slow-path for other environments - see #25
|
|
2975
|
-
var sch =
|
|
2976
|
-
input.headers[
|
|
2977
|
-
Object.keys(input.headers).find(function (key) {
|
|
2978
|
-
return key.toLowerCase() === "set-cookie";
|
|
2979
|
-
})
|
|
2980
|
-
];
|
|
2981
|
-
// warn if called on a request-like object with a cookie header rather than a set-cookie header - see #34, 36
|
|
2982
|
-
if (!sch && input.headers.cookie && !options.silent) {
|
|
2983
|
-
console.warn(
|
|
2984
|
-
"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."
|
|
2985
|
-
);
|
|
2986
|
-
}
|
|
2987
|
-
input = sch;
|
|
2988
|
-
}
|
|
2989
|
-
if (!Array.isArray(input)) {
|
|
2990
|
-
input = [input];
|
|
2991
|
-
}
|
|
2992
|
-
|
|
2993
|
-
options = options
|
|
2994
|
-
? Object.assign({}, defaultParseOptions, options)
|
|
2995
|
-
: defaultParseOptions;
|
|
2996
|
-
|
|
2997
|
-
if (!options.map) {
|
|
2998
|
-
return input.filter(isNonEmptyString).map(function (str) {
|
|
2999
|
-
return parseString(str, options);
|
|
3000
|
-
});
|
|
3001
|
-
} else {
|
|
3002
|
-
var cookies = {};
|
|
3003
|
-
return input.filter(isNonEmptyString).reduce(function (cookies, str) {
|
|
3004
|
-
var cookie = parseString(str, options);
|
|
3005
|
-
cookies[cookie.name] = cookie;
|
|
3006
|
-
return cookies;
|
|
3007
|
-
}, cookies);
|
|
3008
|
-
}
|
|
3009
|
-
}
|
|
3010
|
-
|
|
3011
|
-
/*
|
|
3012
|
-
Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas
|
|
3013
|
-
that are within a single set-cookie field-value, such as in the Expires portion.
|
|
3014
|
-
|
|
3015
|
-
This is uncommon, but explicitly allowed - see https://tools.ietf.org/html/rfc2616#section-4.2
|
|
3016
|
-
Node.js does this for every header *except* set-cookie - see https://github.com/nodejs/node/blob/d5e363b77ebaf1caf67cd7528224b651c86815c1/lib/_http_incoming.js#L128
|
|
3017
|
-
React Native's fetch does this for *every* header, including set-cookie.
|
|
3018
|
-
|
|
3019
|
-
Based on: https://github.com/google/j2objc/commit/16820fdbc8f76ca0c33472810ce0cb03d20efe25
|
|
3020
|
-
Credits to: https://github.com/tomball for original and https://github.com/chrusart for JavaScript implementation
|
|
3021
|
-
*/
|
|
3022
|
-
function splitCookiesString(cookiesString) {
|
|
3023
|
-
if (Array.isArray(cookiesString)) {
|
|
3024
|
-
return cookiesString;
|
|
3025
|
-
}
|
|
3026
|
-
if (typeof cookiesString !== "string") {
|
|
3027
|
-
return [];
|
|
3028
|
-
}
|
|
3029
|
-
|
|
3030
|
-
var cookiesStrings = [];
|
|
3031
|
-
var pos = 0;
|
|
3032
|
-
var start;
|
|
3033
|
-
var ch;
|
|
3034
|
-
var lastComma;
|
|
3035
|
-
var nextStart;
|
|
3036
|
-
var cookiesSeparatorFound;
|
|
3037
|
-
|
|
3038
|
-
function skipWhitespace() {
|
|
3039
|
-
while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) {
|
|
3040
|
-
pos += 1;
|
|
3041
|
-
}
|
|
3042
|
-
return pos < cookiesString.length;
|
|
3043
|
-
}
|
|
3044
|
-
|
|
3045
|
-
function notSpecialChar() {
|
|
3046
|
-
ch = cookiesString.charAt(pos);
|
|
3047
|
-
|
|
3048
|
-
return ch !== "=" && ch !== ";" && ch !== ",";
|
|
3049
|
-
}
|
|
3050
|
-
|
|
3051
|
-
while (pos < cookiesString.length) {
|
|
3052
|
-
start = pos;
|
|
3053
|
-
cookiesSeparatorFound = false;
|
|
3054
|
-
|
|
3055
|
-
while (skipWhitespace()) {
|
|
3056
|
-
ch = cookiesString.charAt(pos);
|
|
3057
|
-
if (ch === ",") {
|
|
3058
|
-
// ',' is a cookie separator if we have later first '=', not ';' or ','
|
|
3059
|
-
lastComma = pos;
|
|
3060
|
-
pos += 1;
|
|
3061
|
-
|
|
3062
|
-
skipWhitespace();
|
|
3063
|
-
nextStart = pos;
|
|
3064
|
-
|
|
3065
|
-
while (pos < cookiesString.length && notSpecialChar()) {
|
|
3066
|
-
pos += 1;
|
|
3067
|
-
}
|
|
3068
|
-
|
|
3069
|
-
// currently special character
|
|
3070
|
-
if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") {
|
|
3071
|
-
// we found cookies separator
|
|
3072
|
-
cookiesSeparatorFound = true;
|
|
3073
|
-
// pos is inside the next cookie, so back up and return it.
|
|
3074
|
-
pos = nextStart;
|
|
3075
|
-
cookiesStrings.push(cookiesString.substring(start, lastComma));
|
|
3076
|
-
start = pos;
|
|
3077
|
-
} else {
|
|
3078
|
-
// in param ',' or param separator ';',
|
|
3079
|
-
// we continue from that comma
|
|
3080
|
-
pos = lastComma + 1;
|
|
3081
|
-
}
|
|
3082
|
-
} else {
|
|
3083
|
-
pos += 1;
|
|
3084
|
-
}
|
|
3085
|
-
}
|
|
3086
|
-
|
|
3087
|
-
if (!cookiesSeparatorFound || pos >= cookiesString.length) {
|
|
3088
|
-
cookiesStrings.push(cookiesString.substring(start, cookiesString.length));
|
|
3089
|
-
}
|
|
3090
|
-
}
|
|
3091
|
-
|
|
3092
|
-
return cookiesStrings;
|
|
3093
|
-
}
|
|
3094
|
-
|
|
3095
|
-
setCookie.exports = parse$1;
|
|
3096
|
-
setCookie.exports.parse = parse$1;
|
|
3097
|
-
setCookie.exports.parseString = parseString;
|
|
3098
|
-
setCookie.exports.splitCookiesString = splitCookiesString;
|
|
3099
|
-
|
|
3100
|
-
(function (exports) {
|
|
3101
|
-
var __rest = (commonjsGlobal && commonjsGlobal.__rest) || function (s, e) {
|
|
3102
|
-
var t = {};
|
|
3103
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
3104
|
-
t[p] = s[p];
|
|
3105
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
3106
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
3107
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
3108
|
-
t[p[i]] = s[p[i]];
|
|
3109
|
-
}
|
|
3110
|
-
return t;
|
|
3111
|
-
};
|
|
3112
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3113
|
-
exports.PERSISTENCY_KEY = void 0;
|
|
3114
|
-
const set_cookie_parser_1 = setCookie.exports;
|
|
3115
|
-
exports.PERSISTENCY_KEY = 'MSW_COOKIE_STORE';
|
|
3116
|
-
const SUPPORTS_LOCAL_STORAGE = typeof localStorage !== 'undefined';
|
|
3117
|
-
class CookieStore {
|
|
3118
|
-
constructor() {
|
|
3119
|
-
this.store = new Map();
|
|
3120
|
-
}
|
|
3121
|
-
/**
|
|
3122
|
-
* Sets the given request cookies into the store.
|
|
3123
|
-
* Respects the `request.credentials` policy.
|
|
3124
|
-
*/
|
|
3125
|
-
add(request, response) {
|
|
3126
|
-
if (request.credentials === 'omit') {
|
|
3127
|
-
return;
|
|
3128
|
-
}
|
|
3129
|
-
const requestUrl = new URL(request.url);
|
|
3130
|
-
const responseCookies = response.headers.get('set-cookie');
|
|
3131
|
-
if (!responseCookies) {
|
|
3132
|
-
return;
|
|
3133
|
-
}
|
|
3134
|
-
const now = Date.now();
|
|
3135
|
-
const parsedResponseCookies = set_cookie_parser_1.parse(responseCookies).map((_a) => {
|
|
3136
|
-
var { maxAge } = _a, cookie = __rest(_a, ["maxAge"]);
|
|
3137
|
-
return (Object.assign(Object.assign({}, cookie), { expires: maxAge === undefined ? cookie.expires : new Date(now + maxAge * 1000), maxAge }));
|
|
3138
|
-
});
|
|
3139
|
-
const prevCookies = this.store.get(requestUrl.origin) || new Map();
|
|
3140
|
-
parsedResponseCookies.forEach((cookie) => {
|
|
3141
|
-
this.store.set(requestUrl.origin, prevCookies.set(cookie.name, cookie));
|
|
3142
|
-
});
|
|
3143
|
-
}
|
|
3144
|
-
/**
|
|
3145
|
-
* Returns cookies relevant to the given request
|
|
3146
|
-
* and its `request.credentials` policy.
|
|
3147
|
-
*/
|
|
3148
|
-
get(request) {
|
|
3149
|
-
this.deleteExpiredCookies();
|
|
3150
|
-
const requestUrl = new URL(request.url);
|
|
3151
|
-
const originCookies = this.store.get(requestUrl.origin) || new Map();
|
|
3152
|
-
switch (request.credentials) {
|
|
3153
|
-
case 'include': {
|
|
3154
|
-
const documentCookies = set_cookie_parser_1.parse(document.cookie);
|
|
3155
|
-
documentCookies.forEach((cookie) => {
|
|
3156
|
-
originCookies.set(cookie.name, cookie);
|
|
3157
|
-
});
|
|
3158
|
-
return originCookies;
|
|
3159
|
-
}
|
|
3160
|
-
case 'same-origin': {
|
|
3161
|
-
return originCookies;
|
|
3162
|
-
}
|
|
3163
|
-
default:
|
|
3164
|
-
return new Map();
|
|
3165
|
-
}
|
|
3166
|
-
}
|
|
3167
|
-
/**
|
|
3168
|
-
* Returns a collection of all stored cookies.
|
|
3169
|
-
*/
|
|
3170
|
-
getAll() {
|
|
3171
|
-
this.deleteExpiredCookies();
|
|
3172
|
-
return this.store;
|
|
3173
|
-
}
|
|
3174
|
-
/**
|
|
3175
|
-
* Deletes all cookies associated with the given request.
|
|
3176
|
-
*/
|
|
3177
|
-
deleteAll(request) {
|
|
3178
|
-
const requestUrl = new URL(request.url);
|
|
3179
|
-
this.store.delete(requestUrl.origin);
|
|
3180
|
-
}
|
|
3181
|
-
/**
|
|
3182
|
-
* Clears the entire cookie store.
|
|
3183
|
-
*/
|
|
3184
|
-
clear() {
|
|
3185
|
-
this.store.clear();
|
|
3186
|
-
}
|
|
3187
|
-
/**
|
|
3188
|
-
* Hydrates the virtual cookie store from the `localStorage` if defined.
|
|
3189
|
-
*/
|
|
3190
|
-
hydrate() {
|
|
3191
|
-
if (!SUPPORTS_LOCAL_STORAGE) {
|
|
3192
|
-
return;
|
|
3193
|
-
}
|
|
3194
|
-
const persistedCookies = localStorage.getItem(exports.PERSISTENCY_KEY);
|
|
3195
|
-
if (persistedCookies) {
|
|
3196
|
-
try {
|
|
3197
|
-
const parsedCookies = JSON.parse(persistedCookies);
|
|
3198
|
-
parsedCookies.forEach(([origin, cookies]) => {
|
|
3199
|
-
this.store.set(origin, new Map(cookies.map((_a) => {
|
|
3200
|
-
var [token, _b] = _a, { expires } = _b, cookie = __rest(_b, ["expires"]);
|
|
3201
|
-
return [
|
|
3202
|
-
token,
|
|
3203
|
-
expires === undefined
|
|
3204
|
-
? cookie
|
|
3205
|
-
: Object.assign(Object.assign({}, cookie), { expires: new Date(expires) }),
|
|
3206
|
-
];
|
|
3207
|
-
})));
|
|
3208
|
-
});
|
|
3209
|
-
}
|
|
3210
|
-
catch (error) {
|
|
3211
|
-
console.warn(`
|
|
3212
|
-
[virtual-cookie] Failed to parse a stored cookie from the localStorage (key "${exports.PERSISTENCY_KEY}").
|
|
3213
|
-
|
|
3214
|
-
Stored value:
|
|
3215
|
-
${localStorage.getItem(exports.PERSISTENCY_KEY)}
|
|
3216
|
-
|
|
3217
|
-
Thrown exception:
|
|
3218
|
-
${error}
|
|
3219
|
-
|
|
3220
|
-
Invalid value has been removed from localStorage to prevent subsequent failed parsing attempts.`);
|
|
3221
|
-
localStorage.removeItem(exports.PERSISTENCY_KEY);
|
|
3222
|
-
}
|
|
3223
|
-
}
|
|
3224
|
-
}
|
|
3225
|
-
/**
|
|
3226
|
-
* Persists the current virtual cookies into the `localStorage` if defined,
|
|
3227
|
-
* so they are available on the next page load.
|
|
3228
|
-
*/
|
|
3229
|
-
persist() {
|
|
3230
|
-
if (!SUPPORTS_LOCAL_STORAGE) {
|
|
3231
|
-
return;
|
|
3232
|
-
}
|
|
3233
|
-
const serializedCookies = Array.from(this.store.entries()).map(([origin, cookies]) => {
|
|
3234
|
-
return [origin, Array.from(cookies.entries())];
|
|
3235
|
-
});
|
|
3236
|
-
localStorage.setItem(exports.PERSISTENCY_KEY, JSON.stringify(serializedCookies));
|
|
3237
|
-
}
|
|
3238
|
-
deleteExpiredCookies() {
|
|
3239
|
-
const now = Date.now();
|
|
3240
|
-
this.store.forEach((originCookies, origin) => {
|
|
3241
|
-
originCookies.forEach(({ expires, name }) => {
|
|
3242
|
-
if (expires !== undefined && expires.getTime() <= now) {
|
|
3243
|
-
originCookies.delete(name);
|
|
3244
|
-
}
|
|
3245
|
-
});
|
|
3246
|
-
if (originCookies.size === 0) {
|
|
3247
|
-
this.store.delete(origin);
|
|
3248
|
-
}
|
|
3249
|
-
});
|
|
3250
|
-
}
|
|
3251
|
-
}
|
|
3252
|
-
exports.default = new CookieStore();
|
|
3253
|
-
}(CookieStore));
|
|
3254
|
-
|
|
3255
|
-
(function (exports) {
|
|
3256
|
-
var __importDefault = (commonjsGlobal && commonjsGlobal.__importDefault) || function (mod) {
|
|
3257
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
3258
|
-
};
|
|
3259
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3260
|
-
exports.PERSISTENCY_KEY = exports.store = void 0;
|
|
3261
|
-
var CookieStore_1 = CookieStore;
|
|
3262
|
-
Object.defineProperty(exports, "store", { enumerable: true, get: function () { return __importDefault(CookieStore_1).default; } });
|
|
3263
|
-
Object.defineProperty(exports, "PERSISTENCY_KEY", { enumerable: true, get: function () { return CookieStore_1.PERSISTENCY_KEY; } });
|
|
3264
|
-
}(lib$1));
|
|
3265
|
-
|
|
3266
2895
|
function getAllCookies() {
|
|
3267
2896
|
return parse_1(document.cookie);
|
|
3268
2897
|
}
|
|
@@ -3273,7 +2902,7 @@ function getRequestCookies(request) {
|
|
|
3273
2902
|
/**
|
|
3274
2903
|
* @note No cookies persist on the document in Node.js: no document.
|
|
3275
2904
|
*/
|
|
3276
|
-
if (typeof location === 'undefined') {
|
|
2905
|
+
if (typeof document === 'undefined' || typeof location === 'undefined') {
|
|
3277
2906
|
return {};
|
|
3278
2907
|
}
|
|
3279
2908
|
switch (request.credentials) {
|
|
@@ -3294,8 +2923,8 @@ function getRequestCookies(request) {
|
|
|
3294
2923
|
|
|
3295
2924
|
function setRequestCookies(request) {
|
|
3296
2925
|
var _a;
|
|
3297
|
-
|
|
3298
|
-
request.cookies = Object.assign(Object.assign({}, getRequestCookies(request)), Array.from((_a =
|
|
2926
|
+
cookies.store.hydrate();
|
|
2927
|
+
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 }), {}));
|
|
3299
2928
|
request.headers.set('cookie', Object.entries(request.cookies)
|
|
3300
2929
|
.map(([name, value]) => `${name}=${value}`)
|
|
3301
2930
|
.join('; '));
|
|
@@ -5289,7 +4918,7 @@ function isNameStart(code) {
|
|
|
5289
4918
|
* Given a GraphQL source, parses it into a Document.
|
|
5290
4919
|
* Throws GraphQLError if a syntax error is encountered.
|
|
5291
4920
|
*/
|
|
5292
|
-
function parse(source, options) {
|
|
4921
|
+
function parse$1(source, options) {
|
|
5293
4922
|
var parser = new Parser(source, options);
|
|
5294
4923
|
return parser.parseDocument();
|
|
5295
4924
|
}
|
|
@@ -6808,7 +6437,7 @@ function parseDocumentNode(node) {
|
|
|
6808
6437
|
}
|
|
6809
6438
|
function parseQuery(query) {
|
|
6810
6439
|
try {
|
|
6811
|
-
const ast = parse(query);
|
|
6440
|
+
const ast = parse$1(query);
|
|
6812
6441
|
return parseDocumentNode(ast);
|
|
6813
6442
|
}
|
|
6814
6443
|
catch (error) {
|
|
@@ -7004,7 +6633,7 @@ function set(...args) {
|
|
|
7004
6633
|
res.headers.append(name, value);
|
|
7005
6634
|
}
|
|
7006
6635
|
else {
|
|
7007
|
-
const headers = lib$
|
|
6636
|
+
const headers = lib$1.objectToHeaders(name);
|
|
7008
6637
|
headers.forEach((value, name) => {
|
|
7009
6638
|
res.headers.append(name, value);
|
|
7010
6639
|
});
|
|
@@ -7101,12 +6730,26 @@ const data = (payload) => {
|
|
|
7101
6730
|
};
|
|
7102
6731
|
};
|
|
7103
6732
|
|
|
6733
|
+
/**
|
|
6734
|
+
* Sets the GraphQL extensions on a given response.
|
|
6735
|
+
* @example
|
|
6736
|
+
* res(ctx.extensions({ tracing: { version: 1 }}))
|
|
6737
|
+
* @see {@link https://mswjs.io/docs/api/context/extensions `ctx.extensions()`}
|
|
6738
|
+
*/
|
|
6739
|
+
const extensions = (payload) => {
|
|
6740
|
+
return (res) => {
|
|
6741
|
+
const prevBody = jsonParse(res.body) || {};
|
|
6742
|
+
const nextBody = mergeRight(prevBody, { extensions: payload });
|
|
6743
|
+
return json(nextBody)(res);
|
|
6744
|
+
};
|
|
6745
|
+
};
|
|
6746
|
+
|
|
7104
6747
|
const SET_TIMEOUT_MAX_ALLOWED_INT = 2147483647;
|
|
7105
6748
|
const MIN_SERVER_RESPONSE_TIME = 100;
|
|
7106
6749
|
const MAX_SERVER_RESPONSE_TIME = 400;
|
|
7107
6750
|
const NODE_SERVER_RESPONSE_TIME = 5;
|
|
7108
6751
|
const getRandomServerResponseTime = () => {
|
|
7109
|
-
if (lib$
|
|
6752
|
+
if (lib$3.exports.isNodeProcess()) {
|
|
7110
6753
|
return NODE_SERVER_RESPONSE_TIME;
|
|
7111
6754
|
}
|
|
7112
6755
|
return Math.floor(Math.random() * (MAX_SERVER_RESPONSE_TIME - MIN_SERVER_RESPONSE_TIME) +
|
|
@@ -7173,9 +6816,9 @@ const errors = (errorsList) => {
|
|
|
7173
6816
|
};
|
|
7174
6817
|
};
|
|
7175
6818
|
|
|
7176
|
-
const useFetch = lib$
|
|
6819
|
+
const useFetch = lib$3.exports.isNodeProcess() ? require('node-fetch') : window.fetch;
|
|
7177
6820
|
const augmentRequestInit = (requestInit) => {
|
|
7178
|
-
const headers = new lib$
|
|
6821
|
+
const headers = new lib$1.Headers(requestInit.headers);
|
|
7179
6822
|
headers.set('x-msw-bypass', 'true');
|
|
7180
6823
|
return Object.assign(Object.assign({}, requestInit), { headers: headers.all() });
|
|
7181
6824
|
};
|
|
@@ -7185,8 +6828,12 @@ const createFetchRequestParameters = (input) => {
|
|
|
7185
6828
|
if (['GET', 'HEAD'].includes(method)) {
|
|
7186
6829
|
return requestParameters;
|
|
7187
6830
|
}
|
|
7188
|
-
|
|
7189
|
-
|
|
6831
|
+
if (typeof body === 'object' || typeof body === 'number') {
|
|
6832
|
+
requestParameters.body = JSON.stringify(body);
|
|
6833
|
+
}
|
|
6834
|
+
else {
|
|
6835
|
+
requestParameters.body = body;
|
|
6836
|
+
}
|
|
7190
6837
|
return requestParameters;
|
|
7191
6838
|
};
|
|
7192
6839
|
/**
|
|
@@ -7275,63 +6922,345 @@ function prepareRequest(request) {
|
|
|
7275
6922
|
* Formats a mocked response for introspection in the browser's console.
|
|
7276
6923
|
*/
|
|
7277
6924
|
function prepareResponse(res) {
|
|
7278
|
-
const responseHeaders = lib$
|
|
6925
|
+
const responseHeaders = lib$1.objectToHeaders(res.headers);
|
|
7279
6926
|
return Object.assign(Object.assign({}, res), {
|
|
7280
6927
|
// Parse a response JSON body for preview in the logs
|
|
7281
6928
|
body: parseBody(res.body, responseHeaders) });
|
|
7282
6929
|
}
|
|
7283
6930
|
|
|
7284
|
-
/**
|
|
7285
|
-
* Converts a string path to a Regular Expression.
|
|
7286
|
-
* Transforms path parameters into named RegExp groups.
|
|
7287
|
-
*/
|
|
7288
|
-
const pathToRegExp = (path) => {
|
|
7289
|
-
const pattern = path
|
|
7290
|
-
// Escape literal dots
|
|
7291
|
-
.replace(/\./g, '\\.')
|
|
7292
|
-
// Escape literal slashes
|
|
7293
|
-
.replace(/\//g, '/')
|
|
7294
|
-
// Escape literal question marks
|
|
7295
|
-
.replace(/\?/g, '\\?')
|
|
7296
|
-
// Ignore trailing slashes
|
|
7297
|
-
.replace(/\/+$/, '')
|
|
7298
|
-
// Replace wildcard with any zero-to-any character sequence
|
|
7299
|
-
.replace(/\*+/g, '.*')
|
|
7300
|
-
// Replace parameters with named capturing groups
|
|
7301
|
-
.replace(/:([^\d|^\/][a-zA-Z0-9_]*(?=(?:\/|\\.)|$))/g, (_, paramName) => `(?<${paramName}>[^\/]+?)`)
|
|
7302
|
-
// Allow optional trailing slash
|
|
7303
|
-
.concat('(\\/|$)');
|
|
7304
|
-
return new RegExp(pattern, 'gi');
|
|
7305
|
-
};
|
|
7306
|
-
|
|
7307
|
-
/**
|
|
7308
|
-
* Matches a given url against a path.
|
|
7309
|
-
*/
|
|
7310
|
-
const match = (path, url) => {
|
|
7311
|
-
const expression = path instanceof RegExp ? path : pathToRegExp(path);
|
|
7312
|
-
const match = expression.exec(url) || false;
|
|
7313
|
-
// Matches in strict mode: match string should equal to input (url)
|
|
7314
|
-
// Otherwise loose matches will be considered truthy:
|
|
7315
|
-
// match('/messages/:id', '/messages/123/users') // true
|
|
7316
|
-
const matches = path instanceof RegExp ? !!match : !!match && match[0] === match.input;
|
|
7317
|
-
return {
|
|
7318
|
-
matches,
|
|
7319
|
-
params: match && matches ? match.groups || null : null,
|
|
7320
|
-
};
|
|
7321
|
-
};
|
|
7322
|
-
|
|
7323
|
-
var getCleanUrl$1 = {};
|
|
7324
|
-
|
|
7325
|
-
Object.defineProperty(getCleanUrl$1, "__esModule", { value: true });
|
|
7326
|
-
var getCleanUrl_2 = getCleanUrl$1.getCleanUrl = void 0;
|
|
7327
6931
|
/**
|
|
7328
|
-
*
|
|
6932
|
+
* Tokenize input string.
|
|
6933
|
+
*/
|
|
6934
|
+
function lexer(str) {
|
|
6935
|
+
var tokens = [];
|
|
6936
|
+
var i = 0;
|
|
6937
|
+
while (i < str.length) {
|
|
6938
|
+
var char = str[i];
|
|
6939
|
+
if (char === "*" || char === "+" || char === "?") {
|
|
6940
|
+
tokens.push({ type: "MODIFIER", index: i, value: str[i++] });
|
|
6941
|
+
continue;
|
|
6942
|
+
}
|
|
6943
|
+
if (char === "\\") {
|
|
6944
|
+
tokens.push({ type: "ESCAPED_CHAR", index: i++, value: str[i++] });
|
|
6945
|
+
continue;
|
|
6946
|
+
}
|
|
6947
|
+
if (char === "{") {
|
|
6948
|
+
tokens.push({ type: "OPEN", index: i, value: str[i++] });
|
|
6949
|
+
continue;
|
|
6950
|
+
}
|
|
6951
|
+
if (char === "}") {
|
|
6952
|
+
tokens.push({ type: "CLOSE", index: i, value: str[i++] });
|
|
6953
|
+
continue;
|
|
6954
|
+
}
|
|
6955
|
+
if (char === ":") {
|
|
6956
|
+
var name = "";
|
|
6957
|
+
var j = i + 1;
|
|
6958
|
+
while (j < str.length) {
|
|
6959
|
+
var code = str.charCodeAt(j);
|
|
6960
|
+
if (
|
|
6961
|
+
// `0-9`
|
|
6962
|
+
(code >= 48 && code <= 57) ||
|
|
6963
|
+
// `A-Z`
|
|
6964
|
+
(code >= 65 && code <= 90) ||
|
|
6965
|
+
// `a-z`
|
|
6966
|
+
(code >= 97 && code <= 122) ||
|
|
6967
|
+
// `_`
|
|
6968
|
+
code === 95) {
|
|
6969
|
+
name += str[j++];
|
|
6970
|
+
continue;
|
|
6971
|
+
}
|
|
6972
|
+
break;
|
|
6973
|
+
}
|
|
6974
|
+
if (!name)
|
|
6975
|
+
throw new TypeError("Missing parameter name at " + i);
|
|
6976
|
+
tokens.push({ type: "NAME", index: i, value: name });
|
|
6977
|
+
i = j;
|
|
6978
|
+
continue;
|
|
6979
|
+
}
|
|
6980
|
+
if (char === "(") {
|
|
6981
|
+
var count = 1;
|
|
6982
|
+
var pattern = "";
|
|
6983
|
+
var j = i + 1;
|
|
6984
|
+
if (str[j] === "?") {
|
|
6985
|
+
throw new TypeError("Pattern cannot start with \"?\" at " + j);
|
|
6986
|
+
}
|
|
6987
|
+
while (j < str.length) {
|
|
6988
|
+
if (str[j] === "\\") {
|
|
6989
|
+
pattern += str[j++] + str[j++];
|
|
6990
|
+
continue;
|
|
6991
|
+
}
|
|
6992
|
+
if (str[j] === ")") {
|
|
6993
|
+
count--;
|
|
6994
|
+
if (count === 0) {
|
|
6995
|
+
j++;
|
|
6996
|
+
break;
|
|
6997
|
+
}
|
|
6998
|
+
}
|
|
6999
|
+
else if (str[j] === "(") {
|
|
7000
|
+
count++;
|
|
7001
|
+
if (str[j + 1] !== "?") {
|
|
7002
|
+
throw new TypeError("Capturing groups are not allowed at " + j);
|
|
7003
|
+
}
|
|
7004
|
+
}
|
|
7005
|
+
pattern += str[j++];
|
|
7006
|
+
}
|
|
7007
|
+
if (count)
|
|
7008
|
+
throw new TypeError("Unbalanced pattern at " + i);
|
|
7009
|
+
if (!pattern)
|
|
7010
|
+
throw new TypeError("Missing pattern at " + i);
|
|
7011
|
+
tokens.push({ type: "PATTERN", index: i, value: pattern });
|
|
7012
|
+
i = j;
|
|
7013
|
+
continue;
|
|
7014
|
+
}
|
|
7015
|
+
tokens.push({ type: "CHAR", index: i, value: str[i++] });
|
|
7016
|
+
}
|
|
7017
|
+
tokens.push({ type: "END", index: i, value: "" });
|
|
7018
|
+
return tokens;
|
|
7019
|
+
}
|
|
7020
|
+
/**
|
|
7021
|
+
* Parse a string for the raw tokens.
|
|
7022
|
+
*/
|
|
7023
|
+
function parse(str, options) {
|
|
7024
|
+
if (options === void 0) { options = {}; }
|
|
7025
|
+
var tokens = lexer(str);
|
|
7026
|
+
var _a = options.prefixes, prefixes = _a === void 0 ? "./" : _a;
|
|
7027
|
+
var defaultPattern = "[^" + escapeString(options.delimiter || "/#?") + "]+?";
|
|
7028
|
+
var result = [];
|
|
7029
|
+
var key = 0;
|
|
7030
|
+
var i = 0;
|
|
7031
|
+
var path = "";
|
|
7032
|
+
var tryConsume = function (type) {
|
|
7033
|
+
if (i < tokens.length && tokens[i].type === type)
|
|
7034
|
+
return tokens[i++].value;
|
|
7035
|
+
};
|
|
7036
|
+
var mustConsume = function (type) {
|
|
7037
|
+
var value = tryConsume(type);
|
|
7038
|
+
if (value !== undefined)
|
|
7039
|
+
return value;
|
|
7040
|
+
var _a = tokens[i], nextType = _a.type, index = _a.index;
|
|
7041
|
+
throw new TypeError("Unexpected " + nextType + " at " + index + ", expected " + type);
|
|
7042
|
+
};
|
|
7043
|
+
var consumeText = function () {
|
|
7044
|
+
var result = "";
|
|
7045
|
+
var value;
|
|
7046
|
+
// tslint:disable-next-line
|
|
7047
|
+
while ((value = tryConsume("CHAR") || tryConsume("ESCAPED_CHAR"))) {
|
|
7048
|
+
result += value;
|
|
7049
|
+
}
|
|
7050
|
+
return result;
|
|
7051
|
+
};
|
|
7052
|
+
while (i < tokens.length) {
|
|
7053
|
+
var char = tryConsume("CHAR");
|
|
7054
|
+
var name = tryConsume("NAME");
|
|
7055
|
+
var pattern = tryConsume("PATTERN");
|
|
7056
|
+
if (name || pattern) {
|
|
7057
|
+
var prefix = char || "";
|
|
7058
|
+
if (prefixes.indexOf(prefix) === -1) {
|
|
7059
|
+
path += prefix;
|
|
7060
|
+
prefix = "";
|
|
7061
|
+
}
|
|
7062
|
+
if (path) {
|
|
7063
|
+
result.push(path);
|
|
7064
|
+
path = "";
|
|
7065
|
+
}
|
|
7066
|
+
result.push({
|
|
7067
|
+
name: name || key++,
|
|
7068
|
+
prefix: prefix,
|
|
7069
|
+
suffix: "",
|
|
7070
|
+
pattern: pattern || defaultPattern,
|
|
7071
|
+
modifier: tryConsume("MODIFIER") || ""
|
|
7072
|
+
});
|
|
7073
|
+
continue;
|
|
7074
|
+
}
|
|
7075
|
+
var value = char || tryConsume("ESCAPED_CHAR");
|
|
7076
|
+
if (value) {
|
|
7077
|
+
path += value;
|
|
7078
|
+
continue;
|
|
7079
|
+
}
|
|
7080
|
+
if (path) {
|
|
7081
|
+
result.push(path);
|
|
7082
|
+
path = "";
|
|
7083
|
+
}
|
|
7084
|
+
var open = tryConsume("OPEN");
|
|
7085
|
+
if (open) {
|
|
7086
|
+
var prefix = consumeText();
|
|
7087
|
+
var name_1 = tryConsume("NAME") || "";
|
|
7088
|
+
var pattern_1 = tryConsume("PATTERN") || "";
|
|
7089
|
+
var suffix = consumeText();
|
|
7090
|
+
mustConsume("CLOSE");
|
|
7091
|
+
result.push({
|
|
7092
|
+
name: name_1 || (pattern_1 ? key++ : ""),
|
|
7093
|
+
pattern: name_1 && !pattern_1 ? defaultPattern : pattern_1,
|
|
7094
|
+
prefix: prefix,
|
|
7095
|
+
suffix: suffix,
|
|
7096
|
+
modifier: tryConsume("MODIFIER") || ""
|
|
7097
|
+
});
|
|
7098
|
+
continue;
|
|
7099
|
+
}
|
|
7100
|
+
mustConsume("END");
|
|
7101
|
+
}
|
|
7102
|
+
return result;
|
|
7103
|
+
}
|
|
7104
|
+
/**
|
|
7105
|
+
* Create path match function from `path-to-regexp` spec.
|
|
7106
|
+
*/
|
|
7107
|
+
function match(str, options) {
|
|
7108
|
+
var keys = [];
|
|
7109
|
+
var re = pathToRegexp(str, keys, options);
|
|
7110
|
+
return regexpToFunction(re, keys, options);
|
|
7111
|
+
}
|
|
7112
|
+
/**
|
|
7113
|
+
* Create a path match function from `path-to-regexp` output.
|
|
7114
|
+
*/
|
|
7115
|
+
function regexpToFunction(re, keys, options) {
|
|
7116
|
+
if (options === void 0) { options = {}; }
|
|
7117
|
+
var _a = options.decode, decode = _a === void 0 ? function (x) { return x; } : _a;
|
|
7118
|
+
return function (pathname) {
|
|
7119
|
+
var m = re.exec(pathname);
|
|
7120
|
+
if (!m)
|
|
7121
|
+
return false;
|
|
7122
|
+
var path = m[0], index = m.index;
|
|
7123
|
+
var params = Object.create(null);
|
|
7124
|
+
var _loop_1 = function (i) {
|
|
7125
|
+
// tslint:disable-next-line
|
|
7126
|
+
if (m[i] === undefined)
|
|
7127
|
+
return "continue";
|
|
7128
|
+
var key = keys[i - 1];
|
|
7129
|
+
if (key.modifier === "*" || key.modifier === "+") {
|
|
7130
|
+
params[key.name] = m[i].split(key.prefix + key.suffix).map(function (value) {
|
|
7131
|
+
return decode(value, key);
|
|
7132
|
+
});
|
|
7133
|
+
}
|
|
7134
|
+
else {
|
|
7135
|
+
params[key.name] = decode(m[i], key);
|
|
7136
|
+
}
|
|
7137
|
+
};
|
|
7138
|
+
for (var i = 1; i < m.length; i++) {
|
|
7139
|
+
_loop_1(i);
|
|
7140
|
+
}
|
|
7141
|
+
return { path: path, index: index, params: params };
|
|
7142
|
+
};
|
|
7143
|
+
}
|
|
7144
|
+
/**
|
|
7145
|
+
* Escape a regular expression string.
|
|
7329
7146
|
*/
|
|
7330
|
-
function
|
|
7331
|
-
|
|
7332
|
-
|
|
7147
|
+
function escapeString(str) {
|
|
7148
|
+
return str.replace(/([.+*?=^!:${}()[\]|/\\])/g, "\\$1");
|
|
7149
|
+
}
|
|
7150
|
+
/**
|
|
7151
|
+
* Get the flags for a regexp from the options.
|
|
7152
|
+
*/
|
|
7153
|
+
function flags(options) {
|
|
7154
|
+
return options && options.sensitive ? "" : "i";
|
|
7155
|
+
}
|
|
7156
|
+
/**
|
|
7157
|
+
* Pull out keys from a regexp.
|
|
7158
|
+
*/
|
|
7159
|
+
function regexpToRegexp(path, keys) {
|
|
7160
|
+
if (!keys)
|
|
7161
|
+
return path;
|
|
7162
|
+
var groupsRegex = /\((?:\?<(.*?)>)?(?!\?)/g;
|
|
7163
|
+
var index = 0;
|
|
7164
|
+
var execResult = groupsRegex.exec(path.source);
|
|
7165
|
+
while (execResult) {
|
|
7166
|
+
keys.push({
|
|
7167
|
+
// Use parenthesized substring match if available, index otherwise
|
|
7168
|
+
name: execResult[1] || index++,
|
|
7169
|
+
prefix: "",
|
|
7170
|
+
suffix: "",
|
|
7171
|
+
modifier: "",
|
|
7172
|
+
pattern: ""
|
|
7173
|
+
});
|
|
7174
|
+
execResult = groupsRegex.exec(path.source);
|
|
7175
|
+
}
|
|
7176
|
+
return path;
|
|
7177
|
+
}
|
|
7178
|
+
/**
|
|
7179
|
+
* Transform an array into a regexp.
|
|
7180
|
+
*/
|
|
7181
|
+
function arrayToRegexp(paths, keys, options) {
|
|
7182
|
+
var parts = paths.map(function (path) { return pathToRegexp(path, keys, options).source; });
|
|
7183
|
+
return new RegExp("(?:" + parts.join("|") + ")", flags(options));
|
|
7184
|
+
}
|
|
7185
|
+
/**
|
|
7186
|
+
* Create a path regexp from string input.
|
|
7187
|
+
*/
|
|
7188
|
+
function stringToRegexp(path, keys, options) {
|
|
7189
|
+
return tokensToRegexp(parse(path, options), keys, options);
|
|
7190
|
+
}
|
|
7191
|
+
/**
|
|
7192
|
+
* Expose a function for taking tokens and returning a RegExp.
|
|
7193
|
+
*/
|
|
7194
|
+
function tokensToRegexp(tokens, keys, options) {
|
|
7195
|
+
if (options === void 0) { options = {}; }
|
|
7196
|
+
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;
|
|
7197
|
+
var endsWith = "[" + escapeString(options.endsWith || "") + "]|$";
|
|
7198
|
+
var delimiter = "[" + escapeString(options.delimiter || "/#?") + "]";
|
|
7199
|
+
var route = start ? "^" : "";
|
|
7200
|
+
// Iterate over the tokens and create our regexp string.
|
|
7201
|
+
for (var _i = 0, tokens_1 = tokens; _i < tokens_1.length; _i++) {
|
|
7202
|
+
var token = tokens_1[_i];
|
|
7203
|
+
if (typeof token === "string") {
|
|
7204
|
+
route += escapeString(encode(token));
|
|
7205
|
+
}
|
|
7206
|
+
else {
|
|
7207
|
+
var prefix = escapeString(encode(token.prefix));
|
|
7208
|
+
var suffix = escapeString(encode(token.suffix));
|
|
7209
|
+
if (token.pattern) {
|
|
7210
|
+
if (keys)
|
|
7211
|
+
keys.push(token);
|
|
7212
|
+
if (prefix || suffix) {
|
|
7213
|
+
if (token.modifier === "+" || token.modifier === "*") {
|
|
7214
|
+
var mod = token.modifier === "*" ? "?" : "";
|
|
7215
|
+
route += "(?:" + prefix + "((?:" + token.pattern + ")(?:" + suffix + prefix + "(?:" + token.pattern + "))*)" + suffix + ")" + mod;
|
|
7216
|
+
}
|
|
7217
|
+
else {
|
|
7218
|
+
route += "(?:" + prefix + "(" + token.pattern + ")" + suffix + ")" + token.modifier;
|
|
7219
|
+
}
|
|
7220
|
+
}
|
|
7221
|
+
else {
|
|
7222
|
+
route += "(" + token.pattern + ")" + token.modifier;
|
|
7223
|
+
}
|
|
7224
|
+
}
|
|
7225
|
+
else {
|
|
7226
|
+
route += "(?:" + prefix + suffix + ")" + token.modifier;
|
|
7227
|
+
}
|
|
7228
|
+
}
|
|
7229
|
+
}
|
|
7230
|
+
if (end) {
|
|
7231
|
+
if (!strict)
|
|
7232
|
+
route += delimiter + "?";
|
|
7233
|
+
route += !options.endsWith ? "$" : "(?=" + endsWith + ")";
|
|
7234
|
+
}
|
|
7235
|
+
else {
|
|
7236
|
+
var endToken = tokens[tokens.length - 1];
|
|
7237
|
+
var isEndDelimited = typeof endToken === "string"
|
|
7238
|
+
? delimiter.indexOf(endToken[endToken.length - 1]) > -1
|
|
7239
|
+
: // tslint:disable-next-line
|
|
7240
|
+
endToken === undefined;
|
|
7241
|
+
if (!strict) {
|
|
7242
|
+
route += "(?:" + delimiter + "(?=" + endsWith + "))?";
|
|
7243
|
+
}
|
|
7244
|
+
if (!isEndDelimited) {
|
|
7245
|
+
route += "(?=" + delimiter + "|" + endsWith + ")";
|
|
7246
|
+
}
|
|
7247
|
+
}
|
|
7248
|
+
return new RegExp(route, flags(options));
|
|
7249
|
+
}
|
|
7250
|
+
/**
|
|
7251
|
+
* Normalize the given path string, returning a regular expression.
|
|
7252
|
+
*
|
|
7253
|
+
* An empty array can be passed in for the keys, which will hold the
|
|
7254
|
+
* placeholder key descriptions. For example, using `/user/:id`, `keys` will
|
|
7255
|
+
* contain `[{ name: 'id', delimiter: '/', optional: false, repeat: false }]`.
|
|
7256
|
+
*/
|
|
7257
|
+
function pathToRegexp(path, keys, options) {
|
|
7258
|
+
if (path instanceof RegExp)
|
|
7259
|
+
return regexpToRegexp(path, keys);
|
|
7260
|
+
if (Array.isArray(path))
|
|
7261
|
+
return arrayToRegexp(path, keys, options);
|
|
7262
|
+
return stringToRegexp(path, keys, options);
|
|
7333
7263
|
}
|
|
7334
|
-
getCleanUrl_2 = getCleanUrl$1.getCleanUrl = getCleanUrl;
|
|
7335
7264
|
|
|
7336
7265
|
const REDUNDANT_CHARACTERS_EXP = /[\?|#].*$/g;
|
|
7337
7266
|
function getSearchParams(path) {
|
|
@@ -7353,9 +7282,12 @@ function getAbsoluteUrl(path, baseUrl) {
|
|
|
7353
7282
|
return path;
|
|
7354
7283
|
}
|
|
7355
7284
|
// Resolve a relative request URL against a given custom "baseUrl"
|
|
7356
|
-
// or the
|
|
7357
|
-
const origin = baseUrl || (typeof
|
|
7358
|
-
return origin
|
|
7285
|
+
// or the document baseURI (in the case of browser/browser-like environments).
|
|
7286
|
+
const origin = baseUrl || (typeof document !== 'undefined' && document.baseURI);
|
|
7287
|
+
return origin
|
|
7288
|
+
? // Encode and decode the path to preserve escaped characters.
|
|
7289
|
+
decodeURI(new URL(encodeURI(path), origin).href)
|
|
7290
|
+
: path;
|
|
7359
7291
|
}
|
|
7360
7292
|
|
|
7361
7293
|
/**
|
|
@@ -7374,12 +7306,48 @@ function normalizePath(path, baseUrl) {
|
|
|
7374
7306
|
return cleanUrl(maybeAbsoluteUrl);
|
|
7375
7307
|
}
|
|
7376
7308
|
|
|
7309
|
+
/**
|
|
7310
|
+
* Coerce a path supported by MSW into a path
|
|
7311
|
+
* supported by "path-to-regexp".
|
|
7312
|
+
*/
|
|
7313
|
+
function coercePath(path) {
|
|
7314
|
+
return (path
|
|
7315
|
+
/**
|
|
7316
|
+
* Replace wildcards ("*") with unnamed capturing groups
|
|
7317
|
+
* because "path-to-regexp" doesn't support wildcards.
|
|
7318
|
+
* Ignore path parameter' modifiers (i.e. ":name*").
|
|
7319
|
+
*/
|
|
7320
|
+
.replace(/([:a-zA-Z_-]*)(\*{1,2})+/g, (_, parameterName, wildcard) => {
|
|
7321
|
+
const expression = '(.*)';
|
|
7322
|
+
if (!parameterName) {
|
|
7323
|
+
return expression;
|
|
7324
|
+
}
|
|
7325
|
+
return parameterName.startsWith(':')
|
|
7326
|
+
? `${parameterName}${wildcard}`
|
|
7327
|
+
: `${parameterName}${expression}`;
|
|
7328
|
+
})
|
|
7329
|
+
/**
|
|
7330
|
+
* Escape the protocol so that "path-to-regexp" could match
|
|
7331
|
+
* absolute URL.
|
|
7332
|
+
* @see https://github.com/pillarjs/path-to-regexp/issues/259
|
|
7333
|
+
*/
|
|
7334
|
+
.replace(/^([^\/]+)(:)(?=\/\/)/g, '$1\\$2'));
|
|
7335
|
+
}
|
|
7377
7336
|
/**
|
|
7378
7337
|
* Returns the result of matching given request URL against a mask.
|
|
7379
7338
|
*/
|
|
7380
7339
|
function matchRequestUrl(url, path, baseUrl) {
|
|
7381
7340
|
const normalizedPath = normalizePath(path, baseUrl);
|
|
7382
|
-
|
|
7341
|
+
const cleanPath = typeof normalizedPath === 'string'
|
|
7342
|
+
? coercePath(normalizedPath)
|
|
7343
|
+
: normalizedPath;
|
|
7344
|
+
const cleanUrl = getCleanUrl.getCleanUrl(url);
|
|
7345
|
+
const result = match(cleanPath, { decode: decodeURIComponent })(cleanUrl);
|
|
7346
|
+
const params = (result && result.params) || {};
|
|
7347
|
+
return {
|
|
7348
|
+
matches: result !== false,
|
|
7349
|
+
params,
|
|
7350
|
+
};
|
|
7383
7351
|
}
|
|
7384
7352
|
|
|
7385
7353
|
/**
|
|
@@ -7414,7 +7382,7 @@ const defaultResponseTransformers = [];
|
|
|
7414
7382
|
function createResponseComposition(responseOverrides, defaultTransformers = defaultResponseTransformers) {
|
|
7415
7383
|
return (...transformers) => __awaiter(this, void 0, void 0, function* () {
|
|
7416
7384
|
const initialResponse = Object.assign({}, defaultResponse, {
|
|
7417
|
-
headers: new lib$
|
|
7385
|
+
headers: new lib$1.Headers({
|
|
7418
7386
|
'x-powered-by': 'msw',
|
|
7419
7387
|
}),
|
|
7420
7388
|
}, responseOverrides);
|
|
@@ -7435,18 +7403,21 @@ const response = Object.assign(createResponseComposition(), {
|
|
|
7435
7403
|
},
|
|
7436
7404
|
});
|
|
7437
7405
|
|
|
7406
|
+
const BUILD_FRAME = /(node_modules)?[\/\\]lib[\/\\](umd|esm|iief|cjs)[\/\\]|^[^\/\\]*$/;
|
|
7438
7407
|
/**
|
|
7439
7408
|
* Return the stack trace frame of a function's invocation.
|
|
7440
7409
|
*/
|
|
7441
|
-
function getCallFrame() {
|
|
7410
|
+
function getCallFrame(error) {
|
|
7442
7411
|
// In <IE11, new Error may return an undefined stack
|
|
7443
|
-
const stack =
|
|
7444
|
-
|
|
7412
|
+
const stack = error.stack;
|
|
7413
|
+
if (!stack) {
|
|
7414
|
+
return;
|
|
7415
|
+
}
|
|
7416
|
+
const frames = stack.split('\n').slice(1);
|
|
7445
7417
|
// Get the first frame that doesn't reference the library's internal trace.
|
|
7446
7418
|
// Assume that frame is the invocation frame.
|
|
7447
|
-
const
|
|
7448
|
-
|
|
7449
|
-
return !ignoreFrameRegExp.test(frame);
|
|
7419
|
+
const declarationFrame = frames.find((frame) => {
|
|
7420
|
+
return !BUILD_FRAME.test(frame);
|
|
7450
7421
|
});
|
|
7451
7422
|
if (!declarationFrame) {
|
|
7452
7423
|
return;
|
|
@@ -7479,7 +7450,7 @@ class RequestHandler {
|
|
|
7479
7450
|
this.shouldSkip = false;
|
|
7480
7451
|
this.ctx = options.ctx || defaultContext;
|
|
7481
7452
|
this.resolver = options.resolver;
|
|
7482
|
-
const callFrame = getCallFrame();
|
|
7453
|
+
const callFrame = getCallFrame(new Error());
|
|
7483
7454
|
this.info = Object.assign(Object.assign({}, options.info), { callFrame });
|
|
7484
7455
|
}
|
|
7485
7456
|
/**
|
|
@@ -7606,21 +7577,9 @@ class RestHandler extends RequestHandler {
|
|
|
7606
7577
|
return;
|
|
7607
7578
|
}
|
|
7608
7579
|
const searchParams = getSearchParams(path);
|
|
7609
|
-
const queryParams = [];
|
|
7610
7580
|
searchParams.forEach((_, paramName) => {
|
|
7611
|
-
queryParams.push(paramName);
|
|
7612
7581
|
});
|
|
7613
|
-
devUtils.warn(
|
|
7614
|
-
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:
|
|
7615
|
-
|
|
7616
|
-
rest.${method.toLowerCase()}("${url}", (req, res, ctx) => {
|
|
7617
|
-
const query = req.url.searchParams
|
|
7618
|
-
${queryParams
|
|
7619
|
-
.map((paramName) => `\
|
|
7620
|
-
const ${paramName} = query.get("${paramName}")`)
|
|
7621
|
-
.join('\n')}
|
|
7622
|
-
})\
|
|
7623
|
-
`);
|
|
7582
|
+
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".`);
|
|
7624
7583
|
}
|
|
7625
7584
|
parse(request, resolutionContext) {
|
|
7626
7585
|
return matchRequestUrl(request.url, this.info.path, resolutionContext === null || resolutionContext === void 0 ? void 0 : resolutionContext.baseUrl);
|
|
@@ -7629,7 +7588,10 @@ ${queryParams
|
|
|
7629
7588
|
return Object.assign(Object.assign({}, request), { params: parsedResult.params || {} });
|
|
7630
7589
|
}
|
|
7631
7590
|
predicate(request, parsedResult) {
|
|
7632
|
-
|
|
7591
|
+
const matchesMethod = this.info.method instanceof RegExp
|
|
7592
|
+
? this.info.method.test(request.method)
|
|
7593
|
+
: isStringEqual(this.info.method, request.method);
|
|
7594
|
+
return matchesMethod && parsedResult.matches;
|
|
7633
7595
|
}
|
|
7634
7596
|
log(request, response) {
|
|
7635
7597
|
const publicUrl = getPublicUrlFromRequest(request);
|
|
@@ -7663,6 +7625,7 @@ const graphqlContext = {
|
|
|
7663
7625
|
delay,
|
|
7664
7626
|
fetch,
|
|
7665
7627
|
data,
|
|
7628
|
+
extensions,
|
|
7666
7629
|
errors,
|
|
7667
7630
|
cookie,
|
|
7668
7631
|
};
|
|
@@ -7709,10 +7672,10 @@ class GraphQLHandler extends RequestHandler {
|
|
|
7709
7672
|
if (!parsedResult) {
|
|
7710
7673
|
return false;
|
|
7711
7674
|
}
|
|
7712
|
-
if (!parsedResult.operationName) {
|
|
7675
|
+
if (!parsedResult.operationName && this.info.operationType !== 'all') {
|
|
7713
7676
|
const publicUrl = getPublicUrlFromRequest(request);
|
|
7714
7677
|
devUtils.warn(`\
|
|
7715
|
-
Failed to intercept a GraphQL request at "${request.method} ${publicUrl}":
|
|
7678
|
+
Failed to intercept a GraphQL request at "${request.method} ${publicUrl}": anonymous GraphQL operations are not supported.
|
|
7716
7679
|
|
|
7717
7680
|
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\
|
|
7718
7681
|
`);
|
|
@@ -7722,7 +7685,7 @@ Consider naming this operation or using "graphql.operation" request handler to i
|
|
|
7722
7685
|
const hasMatchingOperationType = this.info.operationType === 'all' ||
|
|
7723
7686
|
parsedResult.operationType === this.info.operationType;
|
|
7724
7687
|
const hasMatchingOperationName = this.info.operationName instanceof RegExp
|
|
7725
|
-
? this.info.operationName.test(parsedResult.operationName)
|
|
7688
|
+
? this.info.operationName.test(parsedResult.operationName || '')
|
|
7726
7689
|
: parsedResult.operationName === this.info.operationName;
|
|
7727
7690
|
return (hasMatchingUrl.matches &&
|
|
7728
7691
|
hasMatchingOperationType &&
|
|
@@ -7732,7 +7695,10 @@ Consider naming this operation or using "graphql.operation" request handler to i
|
|
|
7732
7695
|
const loggedRequest = prepareRequest(request);
|
|
7733
7696
|
const loggedResponse = prepareResponse(response);
|
|
7734
7697
|
const statusColor = getStatusCodeColor(response.status);
|
|
7735
|
-
|
|
7698
|
+
const requestInfo = (parsedRequest === null || parsedRequest === void 0 ? void 0 : parsedRequest.operationName)
|
|
7699
|
+
? `${parsedRequest === null || parsedRequest === void 0 ? void 0 : parsedRequest.operationType} ${parsedRequest === null || parsedRequest === void 0 ? void 0 : parsedRequest.operationName}`
|
|
7700
|
+
: `anonymous ${parsedRequest === null || parsedRequest === void 0 ? void 0 : parsedRequest.operationType}`;
|
|
7701
|
+
console.groupCollapsed(devUtils.formatMessage('%s %s (%c%s%c)'), getTimestamp(), `${requestInfo}`, `color:${statusColor}`, `${response.status} ${response.statusText}`, 'color:inherit');
|
|
7736
7702
|
console.log('Request:', loggedRequest);
|
|
7737
7703
|
console.log('Handler:', this);
|
|
7738
7704
|
console.log('Response:', loggedResponse);
|
|
@@ -7757,10 +7723,10 @@ function groupHandlersByType(handlers) {
|
|
|
7757
7723
|
graphql: [],
|
|
7758
7724
|
});
|
|
7759
7725
|
}
|
|
7760
|
-
function
|
|
7726
|
+
function getRestHandlerScore() {
|
|
7761
7727
|
return (request, handler) => {
|
|
7762
7728
|
const { path, method } = handler.info;
|
|
7763
|
-
if (path instanceof RegExp) {
|
|
7729
|
+
if (path instanceof RegExp || method instanceof RegExp) {
|
|
7764
7730
|
return Infinity;
|
|
7765
7731
|
}
|
|
7766
7732
|
const hasSameMethod = isStringEqual(request.method, method);
|
|
@@ -7771,12 +7737,15 @@ function getScoreForRestHandler() {
|
|
|
7771
7737
|
return score - methodScoreDelta;
|
|
7772
7738
|
};
|
|
7773
7739
|
}
|
|
7774
|
-
function
|
|
7740
|
+
function getGraphQLHandlerScore(parsedQuery) {
|
|
7775
7741
|
return (_, handler) => {
|
|
7776
7742
|
if (typeof parsedQuery.operationName === 'undefined') {
|
|
7777
7743
|
return Infinity;
|
|
7778
7744
|
}
|
|
7779
7745
|
const { operationType, operationName } = handler.info;
|
|
7746
|
+
if (typeof operationName !== 'string') {
|
|
7747
|
+
return Infinity;
|
|
7748
|
+
}
|
|
7780
7749
|
const hasSameOperationType = parsedQuery.operationType === operationType;
|
|
7781
7750
|
// Always treat a handler with the same operation type as a more similar one.
|
|
7782
7751
|
const operationTypeScoreDelta = hasSameOperationType ? TYPE_MATCH_DELTA : 0;
|
|
@@ -7786,16 +7755,12 @@ function getScoreForGraphQLHandler(parsedQuery) {
|
|
|
7786
7755
|
}
|
|
7787
7756
|
function getSuggestedHandler(request, handlers, getScore) {
|
|
7788
7757
|
const suggestedHandlers = handlers
|
|
7789
|
-
.reduce((
|
|
7758
|
+
.reduce((suggestions, handler) => {
|
|
7790
7759
|
const score = getScore(request, handler);
|
|
7791
|
-
return
|
|
7760
|
+
return suggestions.concat([[score, handler]]);
|
|
7792
7761
|
}, [])
|
|
7793
|
-
.sort(([leftScore], [rightScore]) =>
|
|
7794
|
-
|
|
7795
|
-
})
|
|
7796
|
-
.filter(([score]) => {
|
|
7797
|
-
return score <= MAX_MATCH_SCORE;
|
|
7798
|
-
})
|
|
7762
|
+
.sort(([leftScore], [rightScore]) => leftScore - rightScore)
|
|
7763
|
+
.filter(([score]) => score <= MAX_MATCH_SCORE)
|
|
7799
7764
|
.slice(0, MAX_SUGGESTION_COUNT)
|
|
7800
7765
|
.map(([, handler]) => handler);
|
|
7801
7766
|
return suggestedHandlers;
|
|
@@ -7825,8 +7790,8 @@ function onUnhandledRequest(request, handlers, strategy = 'warn') {
|
|
|
7825
7790
|
? handlerGroups.graphql
|
|
7826
7791
|
: handlerGroups.rest;
|
|
7827
7792
|
const suggestedHandlers = getSuggestedHandler(request, relevantHandlers, parsedGraphQLQuery
|
|
7828
|
-
?
|
|
7829
|
-
:
|
|
7793
|
+
? getGraphQLHandlerScore(parsedGraphQLQuery)
|
|
7794
|
+
: getRestHandlerScore());
|
|
7830
7795
|
const handlerSuggestion = suggestedHandlers.length > 0
|
|
7831
7796
|
? getSuggestedHandlersMessage(suggestedHandlers)
|
|
7832
7797
|
: '';
|
|
@@ -7846,8 +7811,10 @@ Read more: https://mswjs.io/docs/getting-started/mocks\
|
|
|
7846
7811
|
const message = messageTemplate.join('\n\n');
|
|
7847
7812
|
switch (strategy) {
|
|
7848
7813
|
case 'error': {
|
|
7814
|
+
// Print a developer-friendly error.
|
|
7849
7815
|
devUtils.error('Error: %s', message);
|
|
7850
|
-
|
|
7816
|
+
// Throw an exception to halt request processing and not perform the original request.
|
|
7817
|
+
throw new Error(devUtils.formatMessage('Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.'));
|
|
7851
7818
|
}
|
|
7852
7819
|
case 'warn': {
|
|
7853
7820
|
devUtils.warn('Warning: %s', message);
|
|
@@ -7861,8 +7828,8 @@ Read more: https://mswjs.io/docs/getting-started/mocks\
|
|
|
7861
7828
|
}
|
|
7862
7829
|
|
|
7863
7830
|
function readResponseCookies(request, response) {
|
|
7864
|
-
|
|
7865
|
-
|
|
7831
|
+
cookies.store.add(Object.assign(Object.assign({}, request), { url: request.url.toString() }), response);
|
|
7832
|
+
cookies.store.persist();
|
|
7866
7833
|
}
|
|
7867
7834
|
|
|
7868
7835
|
function handleRequest(request, handlers, options, emitter, handleRequestOptions) {
|
|
@@ -7890,7 +7857,12 @@ function handleRequest(request, handlers, options, emitter, handleRequestOptions
|
|
|
7890
7857
|
// When the handled request returned no mocked response, warn the developer,
|
|
7891
7858
|
// as it may be an oversight on their part. Perform the request as-is.
|
|
7892
7859
|
if (!response) {
|
|
7893
|
-
devUtils.warn(
|
|
7860
|
+
devUtils.warn(`\
|
|
7861
|
+
Expected response resolver to return a mocked response Object, but got %s. The original response is going to be used instead.\
|
|
7862
|
+
\n
|
|
7863
|
+
\u2022 %s
|
|
7864
|
+
%s\
|
|
7865
|
+
`, response, handler.info.header, handler.info.callFrame);
|
|
7894
7866
|
emitter.emit('request:end', request);
|
|
7895
7867
|
(_c = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.onBypassResponse) === null || _c === void 0 ? void 0 : _c.call(handleRequestOptions, request);
|
|
7896
7868
|
return;
|
|
@@ -7914,6 +7886,23 @@ function handleRequest(request, handlers, options, emitter, handleRequestOptions
|
|
|
7914
7886
|
});
|
|
7915
7887
|
}
|
|
7916
7888
|
|
|
7889
|
+
/**
|
|
7890
|
+
* Pipes all emitted events from one emitter to another.
|
|
7891
|
+
*/
|
|
7892
|
+
function pipeEvents(source, destination) {
|
|
7893
|
+
const rawEmit = source.emit;
|
|
7894
|
+
// @ts-ignore
|
|
7895
|
+
if (rawEmit._isPiped) {
|
|
7896
|
+
return;
|
|
7897
|
+
}
|
|
7898
|
+
source.emit = function (event, ...data) {
|
|
7899
|
+
destination.emit(event, ...data);
|
|
7900
|
+
return rawEmit.call(this, event, ...data);
|
|
7901
|
+
};
|
|
7902
|
+
// @ts-ignore
|
|
7903
|
+
source.emit._isPiped = true;
|
|
7904
|
+
}
|
|
7905
|
+
|
|
7917
7906
|
const DEFAULT_LISTEN_OPTIONS = {
|
|
7918
7907
|
onUnhandledRequest: 'warn',
|
|
7919
7908
|
};
|
|
@@ -7922,7 +7911,9 @@ const DEFAULT_LISTEN_OPTIONS = {
|
|
|
7922
7911
|
* Useful to generate identical API using different patches to request issuing modules.
|
|
7923
7912
|
*/
|
|
7924
7913
|
function createSetupServer(...interceptors$1) {
|
|
7925
|
-
const emitter = new lib$
|
|
7914
|
+
const emitter = new lib$2.StrictEventEmitter();
|
|
7915
|
+
const publicEmitter = new lib$2.StrictEventEmitter();
|
|
7916
|
+
pipeEvents(emitter, publicEmitter);
|
|
7926
7917
|
return function setupServer(...requestHandlers) {
|
|
7927
7918
|
requestHandlers.forEach((handler) => {
|
|
7928
7919
|
if (Array.isArray(handler))
|
|
@@ -7932,7 +7923,7 @@ function createSetupServer(...interceptors$1) {
|
|
|
7932
7923
|
// so it could be modified at a runtime.
|
|
7933
7924
|
let currentHandlers = [...requestHandlers];
|
|
7934
7925
|
// Error when attempting to run this function in a browser environment.
|
|
7935
|
-
if (!lib$
|
|
7926
|
+
if (!lib$3.exports.isNodeProcess()) {
|
|
7936
7927
|
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.'));
|
|
7937
7928
|
}
|
|
7938
7929
|
let resolvedOptions = {};
|
|
@@ -7992,11 +7983,20 @@ ${source.bold(`${pragma} ${header}`)}
|
|
|
7992
7983
|
`);
|
|
7993
7984
|
});
|
|
7994
7985
|
},
|
|
7995
|
-
|
|
7996
|
-
|
|
7986
|
+
events: {
|
|
7987
|
+
on(...args) {
|
|
7988
|
+
return publicEmitter.on(...args);
|
|
7989
|
+
},
|
|
7990
|
+
removeListener(...args) {
|
|
7991
|
+
return publicEmitter.removeListener(...args);
|
|
7992
|
+
},
|
|
7993
|
+
removeAllListeners(...args) {
|
|
7994
|
+
return publicEmitter.removeAllListeners(...args);
|
|
7995
|
+
},
|
|
7997
7996
|
},
|
|
7998
7997
|
close() {
|
|
7999
7998
|
emitter.removeAllListeners();
|
|
7999
|
+
publicEmitter.removeAllListeners();
|
|
8000
8000
|
interceptor.restore();
|
|
8001
8001
|
},
|
|
8002
8002
|
};
|