msw 0.33.3 → 0.36.1
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 +14 -9
- package/lib/esm/graphql.js +1 -0
- package/lib/esm/index-deps.js +2 -1
- package/lib/esm/index.js +231 -5257
- package/lib/esm/index2.js +1 -1
- package/lib/esm/mockServiceWorker.js +21 -12
- package/lib/esm/rest-deps.js +8 -15
- package/lib/esm/rest.js +1 -0
- package/lib/iife/index.js +3 -3
- package/lib/iife/mockServiceWorker.js +21 -12
- 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 +5 -4
- package/lib/types/handlers/RequestHandler.d.ts +1 -1
- package/lib/types/handlers/RestHandler.d.ts +8 -10
- package/lib/types/index.d.ts +17 -7
- package/lib/types/node/glossary.d.ts +4 -14
- package/lib/types/node/index.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/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/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/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 +1383 -1777
- package/lib/umd/mockServiceWorker.js +21 -12
- package/native/lib/index.js +494 -491
- package/node/lib/index.js +496 -493
- 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('; '));
|
|
@@ -4114,6 +3743,7 @@ function invariant(condition, message) {
|
|
|
4114
3743
|
|
|
4115
3744
|
// istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
|
|
4116
3745
|
var nodejsCustomInspectSymbol = typeof Symbol === 'function' && typeof Symbol.for === 'function' ? Symbol.for('nodejs.util.inspect.custom') : undefined;
|
|
3746
|
+
var nodejsCustomInspectSymbol$1 = nodejsCustomInspectSymbol;
|
|
4117
3747
|
|
|
4118
3748
|
/**
|
|
4119
3749
|
* The `defineInspect()` function defines `inspect()` prototype method as alias of `toJSON`
|
|
@@ -4124,8 +3754,8 @@ function defineInspect(classObject) {
|
|
|
4124
3754
|
typeof fn === 'function' || invariant(0);
|
|
4125
3755
|
classObject.prototype.inspect = fn; // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2317')
|
|
4126
3756
|
|
|
4127
|
-
if (nodejsCustomInspectSymbol) {
|
|
4128
|
-
classObject.prototype[nodejsCustomInspectSymbol] = fn;
|
|
3757
|
+
if (nodejsCustomInspectSymbol$1) {
|
|
3758
|
+
classObject.prototype[nodejsCustomInspectSymbol$1] = fn;
|
|
4129
3759
|
}
|
|
4130
3760
|
}
|
|
4131
3761
|
|
|
@@ -4368,7 +3998,7 @@ function formatArray(array, seenValues) {
|
|
|
4368
3998
|
}
|
|
4369
3999
|
|
|
4370
4000
|
function getCustomFn(object) {
|
|
4371
|
-
var customInspectFn = object[String(nodejsCustomInspectSymbol)];
|
|
4001
|
+
var customInspectFn = object[String(nodejsCustomInspectSymbol$1)];
|
|
4372
4002
|
|
|
4373
4003
|
if (typeof customInspectFn === 'function') {
|
|
4374
4004
|
return customInspectFn;
|
|
@@ -5288,7 +4918,7 @@ function isNameStart(code) {
|
|
|
5288
4918
|
* Given a GraphQL source, parses it into a Document.
|
|
5289
4919
|
* Throws GraphQLError if a syntax error is encountered.
|
|
5290
4920
|
*/
|
|
5291
|
-
function parse(source, options) {
|
|
4921
|
+
function parse$1(source, options) {
|
|
5292
4922
|
var parser = new Parser(source, options);
|
|
5293
4923
|
return parser.parseDocument();
|
|
5294
4924
|
}
|
|
@@ -6807,7 +6437,7 @@ function parseDocumentNode(node) {
|
|
|
6807
6437
|
}
|
|
6808
6438
|
function parseQuery(query) {
|
|
6809
6439
|
try {
|
|
6810
|
-
const ast = parse(query);
|
|
6440
|
+
const ast = parse$1(query);
|
|
6811
6441
|
return parseDocumentNode(ast);
|
|
6812
6442
|
}
|
|
6813
6443
|
catch (error) {
|
|
@@ -7003,7 +6633,7 @@ function set(...args) {
|
|
|
7003
6633
|
res.headers.append(name, value);
|
|
7004
6634
|
}
|
|
7005
6635
|
else {
|
|
7006
|
-
const headers = lib$
|
|
6636
|
+
const headers = lib$1.objectToHeaders(name);
|
|
7007
6637
|
headers.forEach((value, name) => {
|
|
7008
6638
|
res.headers.append(name, value);
|
|
7009
6639
|
});
|
|
@@ -7100,12 +6730,26 @@ const data = (payload) => {
|
|
|
7100
6730
|
};
|
|
7101
6731
|
};
|
|
7102
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
|
+
|
|
7103
6747
|
const SET_TIMEOUT_MAX_ALLOWED_INT = 2147483647;
|
|
7104
6748
|
const MIN_SERVER_RESPONSE_TIME = 100;
|
|
7105
6749
|
const MAX_SERVER_RESPONSE_TIME = 400;
|
|
7106
6750
|
const NODE_SERVER_RESPONSE_TIME = 5;
|
|
7107
6751
|
const getRandomServerResponseTime = () => {
|
|
7108
|
-
if (lib$
|
|
6752
|
+
if (lib$3.exports.isNodeProcess()) {
|
|
7109
6753
|
return NODE_SERVER_RESPONSE_TIME;
|
|
7110
6754
|
}
|
|
7111
6755
|
return Math.floor(Math.random() * (MAX_SERVER_RESPONSE_TIME - MIN_SERVER_RESPONSE_TIME) +
|
|
@@ -7172,9 +6816,9 @@ const errors = (errorsList) => {
|
|
|
7172
6816
|
};
|
|
7173
6817
|
};
|
|
7174
6818
|
|
|
7175
|
-
const useFetch = lib$
|
|
6819
|
+
const useFetch = lib$3.exports.isNodeProcess() ? require('node-fetch') : window.fetch;
|
|
7176
6820
|
const augmentRequestInit = (requestInit) => {
|
|
7177
|
-
const headers = new lib$
|
|
6821
|
+
const headers = new lib$1.Headers(requestInit.headers);
|
|
7178
6822
|
headers.set('x-msw-bypass', 'true');
|
|
7179
6823
|
return Object.assign(Object.assign({}, requestInit), { headers: headers.all() });
|
|
7180
6824
|
};
|
|
@@ -7184,8 +6828,12 @@ const createFetchRequestParameters = (input) => {
|
|
|
7184
6828
|
if (['GET', 'HEAD'].includes(method)) {
|
|
7185
6829
|
return requestParameters;
|
|
7186
6830
|
}
|
|
7187
|
-
|
|
7188
|
-
|
|
6831
|
+
if (typeof body === 'object' || typeof body === 'number') {
|
|
6832
|
+
requestParameters.body = JSON.stringify(body);
|
|
6833
|
+
}
|
|
6834
|
+
else {
|
|
6835
|
+
requestParameters.body = body;
|
|
6836
|
+
}
|
|
7189
6837
|
return requestParameters;
|
|
7190
6838
|
};
|
|
7191
6839
|
/**
|
|
@@ -7274,63 +6922,345 @@ function prepareRequest(request) {
|
|
|
7274
6922
|
* Formats a mocked response for introspection in the browser's console.
|
|
7275
6923
|
*/
|
|
7276
6924
|
function prepareResponse(res) {
|
|
7277
|
-
const responseHeaders = lib$
|
|
6925
|
+
const responseHeaders = lib$1.objectToHeaders(res.headers);
|
|
7278
6926
|
return Object.assign(Object.assign({}, res), {
|
|
7279
6927
|
// Parse a response JSON body for preview in the logs
|
|
7280
6928
|
body: parseBody(res.body, responseHeaders) });
|
|
7281
6929
|
}
|
|
7282
6930
|
|
|
7283
|
-
/**
|
|
7284
|
-
* Converts a string path to a Regular Expression.
|
|
7285
|
-
* Transforms path parameters into named RegExp groups.
|
|
7286
|
-
*/
|
|
7287
|
-
const pathToRegExp = (path) => {
|
|
7288
|
-
const pattern = path
|
|
7289
|
-
// Escape literal dots
|
|
7290
|
-
.replace(/\./g, '\\.')
|
|
7291
|
-
// Escape literal slashes
|
|
7292
|
-
.replace(/\//g, '/')
|
|
7293
|
-
// Escape literal question marks
|
|
7294
|
-
.replace(/\?/g, '\\?')
|
|
7295
|
-
// Ignore trailing slashes
|
|
7296
|
-
.replace(/\/+$/, '')
|
|
7297
|
-
// Replace wildcard with any zero-to-any character sequence
|
|
7298
|
-
.replace(/\*+/g, '.*')
|
|
7299
|
-
// Replace parameters with named capturing groups
|
|
7300
|
-
.replace(/:([^\d|^\/][a-zA-Z0-9_]*(?=(?:\/|\\.)|$))/g, (_, paramName) => `(?<${paramName}>[^\/]+?)`)
|
|
7301
|
-
// Allow optional trailing slash
|
|
7302
|
-
.concat('(\\/|$)');
|
|
7303
|
-
return new RegExp(pattern, 'gi');
|
|
7304
|
-
};
|
|
7305
|
-
|
|
7306
|
-
/**
|
|
7307
|
-
* Matches a given url against a path.
|
|
7308
|
-
*/
|
|
7309
|
-
const match = (path, url) => {
|
|
7310
|
-
const expression = path instanceof RegExp ? path : pathToRegExp(path);
|
|
7311
|
-
const match = expression.exec(url) || false;
|
|
7312
|
-
// Matches in strict mode: match string should equal to input (url)
|
|
7313
|
-
// Otherwise loose matches will be considered truthy:
|
|
7314
|
-
// match('/messages/:id', '/messages/123/users') // true
|
|
7315
|
-
const matches = path instanceof RegExp ? !!match : !!match && match[0] === match.input;
|
|
7316
|
-
return {
|
|
7317
|
-
matches,
|
|
7318
|
-
params: match && matches ? match.groups || null : null,
|
|
7319
|
-
};
|
|
7320
|
-
};
|
|
7321
|
-
|
|
7322
|
-
var getCleanUrl$1 = {};
|
|
7323
|
-
|
|
7324
|
-
Object.defineProperty(getCleanUrl$1, "__esModule", { value: true });
|
|
7325
|
-
var getCleanUrl_2 = getCleanUrl$1.getCleanUrl = void 0;
|
|
7326
6931
|
/**
|
|
7327
|
-
*
|
|
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.
|
|
7328
7106
|
*/
|
|
7329
|
-
function
|
|
7330
|
-
|
|
7331
|
-
|
|
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.
|
|
7146
|
+
*/
|
|
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);
|
|
7332
7263
|
}
|
|
7333
|
-
getCleanUrl_2 = getCleanUrl$1.getCleanUrl = getCleanUrl;
|
|
7334
7264
|
|
|
7335
7265
|
const REDUNDANT_CHARACTERS_EXP = /[\?|#].*$/g;
|
|
7336
7266
|
function getSearchParams(path) {
|
|
@@ -7352,9 +7282,12 @@ function getAbsoluteUrl(path, baseUrl) {
|
|
|
7352
7282
|
return path;
|
|
7353
7283
|
}
|
|
7354
7284
|
// Resolve a relative request URL against a given custom "baseUrl"
|
|
7355
|
-
// or the
|
|
7356
|
-
const origin = baseUrl || (typeof
|
|
7357
|
-
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;
|
|
7358
7291
|
}
|
|
7359
7292
|
|
|
7360
7293
|
/**
|
|
@@ -7373,12 +7306,48 @@ function normalizePath(path, baseUrl) {
|
|
|
7373
7306
|
return cleanUrl(maybeAbsoluteUrl);
|
|
7374
7307
|
}
|
|
7375
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
|
+
}
|
|
7376
7336
|
/**
|
|
7377
7337
|
* Returns the result of matching given request URL against a mask.
|
|
7378
7338
|
*/
|
|
7379
7339
|
function matchRequestUrl(url, path, baseUrl) {
|
|
7380
7340
|
const normalizedPath = normalizePath(path, baseUrl);
|
|
7381
|
-
|
|
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
|
+
};
|
|
7382
7351
|
}
|
|
7383
7352
|
|
|
7384
7353
|
/**
|
|
@@ -7413,7 +7382,7 @@ const defaultResponseTransformers = [];
|
|
|
7413
7382
|
function createResponseComposition(responseOverrides, defaultTransformers = defaultResponseTransformers) {
|
|
7414
7383
|
return (...transformers) => __awaiter(this, void 0, void 0, function* () {
|
|
7415
7384
|
const initialResponse = Object.assign({}, defaultResponse, {
|
|
7416
|
-
headers: new lib$
|
|
7385
|
+
headers: new lib$1.Headers({
|
|
7417
7386
|
'x-powered-by': 'msw',
|
|
7418
7387
|
}),
|
|
7419
7388
|
}, responseOverrides);
|
|
@@ -7434,18 +7403,21 @@ const response = Object.assign(createResponseComposition(), {
|
|
|
7434
7403
|
},
|
|
7435
7404
|
});
|
|
7436
7405
|
|
|
7406
|
+
const BUILD_FRAME = /(node_modules)?[\/\\]lib[\/\\](umd|esm|iief|cjs)[\/\\]|^[^\/\\]*$/;
|
|
7437
7407
|
/**
|
|
7438
7408
|
* Return the stack trace frame of a function's invocation.
|
|
7439
7409
|
*/
|
|
7440
|
-
function getCallFrame() {
|
|
7410
|
+
function getCallFrame(error) {
|
|
7441
7411
|
// In <IE11, new Error may return an undefined stack
|
|
7442
|
-
const stack =
|
|
7443
|
-
|
|
7412
|
+
const stack = error.stack;
|
|
7413
|
+
if (!stack) {
|
|
7414
|
+
return;
|
|
7415
|
+
}
|
|
7416
|
+
const frames = stack.split('\n').slice(1);
|
|
7444
7417
|
// Get the first frame that doesn't reference the library's internal trace.
|
|
7445
7418
|
// Assume that frame is the invocation frame.
|
|
7446
|
-
const
|
|
7447
|
-
|
|
7448
|
-
return !ignoreFrameRegExp.test(frame);
|
|
7419
|
+
const declarationFrame = frames.find((frame) => {
|
|
7420
|
+
return !BUILD_FRAME.test(frame);
|
|
7449
7421
|
});
|
|
7450
7422
|
if (!declarationFrame) {
|
|
7451
7423
|
return;
|
|
@@ -7478,7 +7450,7 @@ class RequestHandler {
|
|
|
7478
7450
|
this.shouldSkip = false;
|
|
7479
7451
|
this.ctx = options.ctx || defaultContext;
|
|
7480
7452
|
this.resolver = options.resolver;
|
|
7481
|
-
const callFrame = getCallFrame();
|
|
7453
|
+
const callFrame = getCallFrame(new Error());
|
|
7482
7454
|
this.info = Object.assign(Object.assign({}, options.info), { callFrame });
|
|
7483
7455
|
}
|
|
7484
7456
|
/**
|
|
@@ -7605,21 +7577,9 @@ class RestHandler extends RequestHandler {
|
|
|
7605
7577
|
return;
|
|
7606
7578
|
}
|
|
7607
7579
|
const searchParams = getSearchParams(path);
|
|
7608
|
-
const queryParams = [];
|
|
7609
7580
|
searchParams.forEach((_, paramName) => {
|
|
7610
|
-
queryParams.push(paramName);
|
|
7611
7581
|
});
|
|
7612
|
-
devUtils.warn(
|
|
7613
|
-
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:
|
|
7614
|
-
|
|
7615
|
-
rest.${method.toLowerCase()}("${url}", (req, res, ctx) => {
|
|
7616
|
-
const query = req.url.searchParams
|
|
7617
|
-
${queryParams
|
|
7618
|
-
.map((paramName) => `\
|
|
7619
|
-
const ${paramName} = query.get("${paramName}")`)
|
|
7620
|
-
.join('\n')}
|
|
7621
|
-
})\
|
|
7622
|
-
`);
|
|
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".`);
|
|
7623
7583
|
}
|
|
7624
7584
|
parse(request, resolutionContext) {
|
|
7625
7585
|
return matchRequestUrl(request.url, this.info.path, resolutionContext === null || resolutionContext === void 0 ? void 0 : resolutionContext.baseUrl);
|
|
@@ -7628,7 +7588,11 @@ ${queryParams
|
|
|
7628
7588
|
return Object.assign(Object.assign({}, request), { params: parsedResult.params || {} });
|
|
7629
7589
|
}
|
|
7630
7590
|
predicate(request, parsedResult) {
|
|
7631
|
-
|
|
7591
|
+
const matchesMethod = this.info.method instanceof RegExp
|
|
7592
|
+
? this.info.method.test(request.method)
|
|
7593
|
+
: isStringEqual(this.info.method, request.method);
|
|
7594
|
+
// console.log({ request, matchesMethod, parsedResult })
|
|
7595
|
+
return matchesMethod && parsedResult.matches;
|
|
7632
7596
|
}
|
|
7633
7597
|
log(request, response) {
|
|
7634
7598
|
const publicUrl = getPublicUrlFromRequest(request);
|
|
@@ -7662,6 +7626,7 @@ const graphqlContext = {
|
|
|
7662
7626
|
delay,
|
|
7663
7627
|
fetch,
|
|
7664
7628
|
data,
|
|
7629
|
+
extensions,
|
|
7665
7630
|
errors,
|
|
7666
7631
|
cookie,
|
|
7667
7632
|
};
|
|
@@ -7708,10 +7673,10 @@ class GraphQLHandler extends RequestHandler {
|
|
|
7708
7673
|
if (!parsedResult) {
|
|
7709
7674
|
return false;
|
|
7710
7675
|
}
|
|
7711
|
-
if (!parsedResult.operationName) {
|
|
7676
|
+
if (!parsedResult.operationName && this.info.operationType !== 'all') {
|
|
7712
7677
|
const publicUrl = getPublicUrlFromRequest(request);
|
|
7713
7678
|
devUtils.warn(`\
|
|
7714
|
-
Failed to intercept a GraphQL request at "${request.method} ${publicUrl}":
|
|
7679
|
+
Failed to intercept a GraphQL request at "${request.method} ${publicUrl}": anonymous GraphQL operations are not supported.
|
|
7715
7680
|
|
|
7716
7681
|
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\
|
|
7717
7682
|
`);
|
|
@@ -7721,7 +7686,7 @@ Consider naming this operation or using "graphql.operation" request handler to i
|
|
|
7721
7686
|
const hasMatchingOperationType = this.info.operationType === 'all' ||
|
|
7722
7687
|
parsedResult.operationType === this.info.operationType;
|
|
7723
7688
|
const hasMatchingOperationName = this.info.operationName instanceof RegExp
|
|
7724
|
-
? this.info.operationName.test(parsedResult.operationName)
|
|
7689
|
+
? this.info.operationName.test(parsedResult.operationName || '')
|
|
7725
7690
|
: parsedResult.operationName === this.info.operationName;
|
|
7726
7691
|
return (hasMatchingUrl.matches &&
|
|
7727
7692
|
hasMatchingOperationType &&
|
|
@@ -7731,7 +7696,10 @@ Consider naming this operation or using "graphql.operation" request handler to i
|
|
|
7731
7696
|
const loggedRequest = prepareRequest(request);
|
|
7732
7697
|
const loggedResponse = prepareResponse(response);
|
|
7733
7698
|
const statusColor = getStatusCodeColor(response.status);
|
|
7734
|
-
|
|
7699
|
+
const requestInfo = (parsedRequest === null || parsedRequest === void 0 ? void 0 : parsedRequest.operationName)
|
|
7700
|
+
? `${parsedRequest === null || parsedRequest === void 0 ? void 0 : parsedRequest.operationType} ${parsedRequest === null || parsedRequest === void 0 ? void 0 : parsedRequest.operationName}`
|
|
7701
|
+
: `anonymous ${parsedRequest === null || parsedRequest === void 0 ? void 0 : parsedRequest.operationType}`;
|
|
7702
|
+
console.groupCollapsed(devUtils.formatMessage('%s %s (%c%s%c)'), getTimestamp(), `${requestInfo}`, `color:${statusColor}`, `${response.status} ${response.statusText}`, 'color:inherit');
|
|
7735
7703
|
console.log('Request:', loggedRequest);
|
|
7736
7704
|
console.log('Handler:', this);
|
|
7737
7705
|
console.log('Response:', loggedResponse);
|
|
@@ -7845,8 +7813,10 @@ Read more: https://mswjs.io/docs/getting-started/mocks\
|
|
|
7845
7813
|
const message = messageTemplate.join('\n\n');
|
|
7846
7814
|
switch (strategy) {
|
|
7847
7815
|
case 'error': {
|
|
7816
|
+
// Print a developer-friendly error.
|
|
7848
7817
|
devUtils.error('Error: %s', message);
|
|
7849
|
-
|
|
7818
|
+
// Throw an exception to halt request processing and not perform the original request.
|
|
7819
|
+
throw new Error('Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.');
|
|
7850
7820
|
}
|
|
7851
7821
|
case 'warn': {
|
|
7852
7822
|
devUtils.warn('Warning: %s', message);
|
|
@@ -7860,8 +7830,8 @@ Read more: https://mswjs.io/docs/getting-started/mocks\
|
|
|
7860
7830
|
}
|
|
7861
7831
|
|
|
7862
7832
|
function readResponseCookies(request, response) {
|
|
7863
|
-
|
|
7864
|
-
|
|
7833
|
+
cookies.store.add(Object.assign(Object.assign({}, request), { url: request.url.toString() }), response);
|
|
7834
|
+
cookies.store.persist();
|
|
7865
7835
|
}
|
|
7866
7836
|
|
|
7867
7837
|
function handleRequest(request, handlers, options, emitter, handleRequestOptions) {
|
|
@@ -7889,7 +7859,12 @@ function handleRequest(request, handlers, options, emitter, handleRequestOptions
|
|
|
7889
7859
|
// When the handled request returned no mocked response, warn the developer,
|
|
7890
7860
|
// as it may be an oversight on their part. Perform the request as-is.
|
|
7891
7861
|
if (!response) {
|
|
7892
|
-
devUtils.warn(
|
|
7862
|
+
devUtils.warn(`\
|
|
7863
|
+
Expected response resolver to return a mocked response Object, but got %s. The original response is going to be used instead.\
|
|
7864
|
+
\n
|
|
7865
|
+
\u2022 %s
|
|
7866
|
+
%s\
|
|
7867
|
+
`, response, handler.info.header, handler.info.callFrame);
|
|
7893
7868
|
emitter.emit('request:end', request);
|
|
7894
7869
|
(_c = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.onBypassResponse) === null || _c === void 0 ? void 0 : _c.call(handleRequestOptions, request);
|
|
7895
7870
|
return;
|
|
@@ -7913,6 +7888,23 @@ function handleRequest(request, handlers, options, emitter, handleRequestOptions
|
|
|
7913
7888
|
});
|
|
7914
7889
|
}
|
|
7915
7890
|
|
|
7891
|
+
/**
|
|
7892
|
+
* Pipes all emitted events from one emitter to another.
|
|
7893
|
+
*/
|
|
7894
|
+
function pipeEvents(source, destination) {
|
|
7895
|
+
const rawEmit = source.emit;
|
|
7896
|
+
// @ts-ignore
|
|
7897
|
+
if (rawEmit._isPiped) {
|
|
7898
|
+
return;
|
|
7899
|
+
}
|
|
7900
|
+
source.emit = function (event, ...data) {
|
|
7901
|
+
destination.emit(event, ...data);
|
|
7902
|
+
return rawEmit.call(this, event, ...data);
|
|
7903
|
+
};
|
|
7904
|
+
// @ts-ignore
|
|
7905
|
+
source.emit._isPiped = true;
|
|
7906
|
+
}
|
|
7907
|
+
|
|
7916
7908
|
const DEFAULT_LISTEN_OPTIONS = {
|
|
7917
7909
|
onUnhandledRequest: 'warn',
|
|
7918
7910
|
};
|
|
@@ -7921,7 +7913,9 @@ const DEFAULT_LISTEN_OPTIONS = {
|
|
|
7921
7913
|
* Useful to generate identical API using different patches to request issuing modules.
|
|
7922
7914
|
*/
|
|
7923
7915
|
function createSetupServer(...interceptors$1) {
|
|
7924
|
-
const emitter = new lib$
|
|
7916
|
+
const emitter = new lib$2.StrictEventEmitter();
|
|
7917
|
+
const publicEmitter = new lib$2.StrictEventEmitter();
|
|
7918
|
+
pipeEvents(emitter, publicEmitter);
|
|
7925
7919
|
return function setupServer(...requestHandlers) {
|
|
7926
7920
|
requestHandlers.forEach((handler) => {
|
|
7927
7921
|
if (Array.isArray(handler))
|
|
@@ -7931,7 +7925,7 @@ function createSetupServer(...interceptors$1) {
|
|
|
7931
7925
|
// so it could be modified at a runtime.
|
|
7932
7926
|
let currentHandlers = [...requestHandlers];
|
|
7933
7927
|
// Error when attempting to run this function in a browser environment.
|
|
7934
|
-
if (!lib$
|
|
7928
|
+
if (!lib$3.exports.isNodeProcess()) {
|
|
7935
7929
|
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.'));
|
|
7936
7930
|
}
|
|
7937
7931
|
let resolvedOptions = {};
|
|
@@ -7991,11 +7985,20 @@ ${source.bold(`${pragma} ${header}`)}
|
|
|
7991
7985
|
`);
|
|
7992
7986
|
});
|
|
7993
7987
|
},
|
|
7994
|
-
|
|
7995
|
-
|
|
7988
|
+
events: {
|
|
7989
|
+
on(...args) {
|
|
7990
|
+
return publicEmitter.on(...args);
|
|
7991
|
+
},
|
|
7992
|
+
removeListener(...args) {
|
|
7993
|
+
return publicEmitter.removeListener(...args);
|
|
7994
|
+
},
|
|
7995
|
+
removeAllListeners(...args) {
|
|
7996
|
+
return publicEmitter.removeAllListeners(...args);
|
|
7997
|
+
},
|
|
7996
7998
|
},
|
|
7997
7999
|
close() {
|
|
7998
8000
|
emitter.removeAllListeners();
|
|
8001
|
+
publicEmitter.removeAllListeners();
|
|
7999
8002
|
interceptor.restore();
|
|
8000
8003
|
},
|
|
8001
8004
|
};
|