msw 1.3.1 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -29
- package/browser/package.json +5 -0
- package/cli/init.js +3 -3
- package/config/constants.js +1 -2
- package/lib/browser/index.d.ts +104 -0
- package/lib/browser/index.js +751 -0
- package/lib/browser/index.mjs +733 -0
- package/lib/core/GraphQLHandler-b42ca96f.d.ts +74 -0
- package/lib/core/HttpResponse.d.ts +2 -0
- package/lib/core/HttpResponse.js +94 -0
- package/lib/core/HttpResponse.mjs +77 -0
- package/lib/core/RequestHandler-1a5dbdc7.d.ts +172 -0
- package/lib/core/SetupApi.d.ts +26 -0
- package/lib/core/SetupApi.js +83 -0
- package/lib/core/SetupApi.mjs +63 -0
- package/lib/core/bypass.d.ts +16 -0
- package/lib/core/bypass.js +36 -0
- package/lib/core/bypass.mjs +16 -0
- package/lib/core/delay.d.ts +18 -0
- package/lib/core/delay.js +92 -0
- package/lib/core/delay.mjs +72 -0
- package/lib/core/graphql.d.ts +109 -0
- package/lib/core/graphql.js +112 -0
- package/lib/core/graphql.mjs +96 -0
- package/lib/core/handlers/GraphQLHandler.d.ts +5 -0
- package/lib/core/handlers/GraphQLHandler.js +145 -0
- package/lib/core/handlers/GraphQLHandler.mjs +130 -0
- package/lib/core/handlers/HttpHandler.d.ts +62 -0
- package/lib/core/handlers/HttpHandler.js +149 -0
- package/lib/core/handlers/HttpHandler.mjs +133 -0
- package/lib/core/handlers/RequestHandler.d.ts +2 -0
- package/lib/core/handlers/RequestHandler.js +180 -0
- package/lib/core/handlers/RequestHandler.mjs +162 -0
- package/lib/core/http.d.ts +26 -0
- package/lib/core/http.js +39 -0
- package/lib/core/http.mjs +22 -0
- package/lib/core/http.spec.d.ts +2 -0
- package/lib/core/http.spec.js +15 -0
- package/lib/core/http.spec.mjs +14 -0
- package/lib/core/index.d.ts +18 -0
- package/lib/core/index.js +49 -0
- package/lib/core/index.mjs +28 -0
- package/lib/core/passthrough.d.ts +17 -0
- package/lib/core/passthrough.js +32 -0
- package/lib/core/passthrough.mjs +12 -0
- package/lib/core/sharedOptions.d.ts +66 -0
- package/lib/core/sharedOptions.js +16 -0
- package/lib/core/sharedOptions.mjs +0 -0
- package/lib/core/typeUtils.d.ts +7 -0
- package/lib/core/typeUtils.js +16 -0
- package/lib/core/typeUtils.mjs +0 -0
- package/lib/core/utils/HttpResponse/decorators.d.ts +12 -0
- package/lib/core/utils/HttpResponse/decorators.js +80 -0
- package/lib/core/utils/HttpResponse/decorators.mjs +52 -0
- package/lib/core/utils/getResponse.d.ts +2 -0
- package/lib/core/utils/getResponse.js +64 -0
- package/lib/core/utils/getResponse.mjs +44 -0
- package/lib/core/utils/handleRequest.d.ts +30 -0
- package/lib/core/utils/handleRequest.js +102 -0
- package/lib/core/utils/handleRequest.mjs +82 -0
- package/lib/core/utils/internal/Disposable.d.ts +7 -0
- package/lib/core/utils/internal/Disposable.js +53 -0
- package/lib/core/utils/internal/Disposable.mjs +33 -0
- package/lib/core/utils/internal/checkGlobals.d.ts +3 -0
- package/lib/core/utils/internal/checkGlobals.js +33 -0
- package/lib/core/utils/internal/checkGlobals.mjs +13 -0
- package/lib/core/utils/internal/devUtils.d.ts +19 -0
- package/lib/core/utils/internal/devUtils.js +40 -0
- package/lib/core/utils/internal/devUtils.mjs +20 -0
- package/lib/core/utils/internal/getCallFrame.d.ts +6 -0
- package/lib/core/utils/internal/getCallFrame.js +40 -0
- package/lib/core/utils/internal/getCallFrame.mjs +20 -0
- package/lib/core/utils/internal/isIterable.d.ts +6 -0
- package/lib/core/utils/internal/isIterable.js +29 -0
- package/lib/core/utils/internal/isIterable.mjs +9 -0
- package/lib/core/utils/internal/isObject.d.ts +6 -0
- package/lib/core/utils/internal/isObject.js +26 -0
- package/lib/core/utils/internal/isObject.mjs +6 -0
- package/lib/core/utils/internal/isStringEqual.d.ts +6 -0
- package/lib/core/utils/internal/isStringEqual.js +26 -0
- package/lib/core/utils/internal/isStringEqual.mjs +6 -0
- package/lib/core/utils/internal/jsonParse.d.ts +7 -0
- package/lib/core/utils/internal/jsonParse.js +30 -0
- package/lib/core/utils/internal/jsonParse.mjs +10 -0
- package/lib/core/utils/internal/mergeRight.d.ts +7 -0
- package/lib/core/utils/internal/mergeRight.js +39 -0
- package/lib/core/utils/internal/mergeRight.mjs +19 -0
- package/lib/core/utils/internal/parseGraphQLRequest.d.ts +5 -0
- package/lib/core/utils/internal/parseGraphQLRequest.js +184 -0
- package/lib/core/utils/internal/parseGraphQLRequest.mjs +165 -0
- package/lib/core/utils/internal/parseMultipartData.d.ts +10 -0
- package/lib/core/utils/internal/parseMultipartData.js +81 -0
- package/lib/core/utils/internal/parseMultipartData.mjs +61 -0
- package/lib/core/utils/internal/pipeEvents.d.ts +8 -0
- package/lib/core/utils/internal/pipeEvents.js +34 -0
- package/lib/core/utils/internal/pipeEvents.mjs +14 -0
- package/lib/core/utils/internal/requestHandlerUtils.d.ts +8 -0
- package/lib/core/utils/internal/requestHandlerUtils.js +36 -0
- package/lib/core/utils/internal/requestHandlerUtils.mjs +16 -0
- package/lib/core/utils/internal/toReadonlyArray.d.ts +6 -0
- package/lib/core/utils/internal/toReadonlyArray.js +28 -0
- package/lib/core/utils/internal/toReadonlyArray.mjs +8 -0
- package/lib/core/utils/internal/tryCatch.d.ts +3 -0
- package/lib/core/utils/internal/tryCatch.js +31 -0
- package/lib/core/utils/internal/tryCatch.mjs +11 -0
- package/lib/core/utils/internal/uuidv4.d.ts +3 -0
- package/lib/core/utils/internal/uuidv4.js +26 -0
- package/lib/core/utils/internal/uuidv4.mjs +6 -0
- package/lib/core/utils/logging/getStatusCodeColor.d.ts +11 -0
- package/lib/core/utils/logging/getStatusCodeColor.js +39 -0
- package/lib/core/utils/logging/getStatusCodeColor.mjs +19 -0
- package/lib/core/utils/logging/getTimestamp.d.ts +6 -0
- package/lib/core/utils/logging/getTimestamp.js +27 -0
- package/lib/core/utils/logging/getTimestamp.mjs +7 -0
- package/lib/core/utils/logging/serializeRequest.d.ts +12 -0
- package/lib/core/utils/logging/serializeRequest.js +55 -0
- package/lib/core/utils/logging/serializeRequest.mjs +35 -0
- package/lib/core/utils/logging/serializeResponse.d.ts +9 -0
- package/lib/core/utils/logging/serializeResponse.js +69 -0
- package/lib/core/utils/logging/serializeResponse.mjs +39 -0
- package/lib/core/utils/matching/matchRequestUrl.d.ts +19 -0
- package/lib/core/utils/matching/matchRequestUrl.js +50 -0
- package/lib/core/utils/matching/matchRequestUrl.mjs +30 -0
- package/lib/core/utils/matching/normalizePath.d.ts +12 -0
- package/lib/core/utils/matching/normalizePath.js +32 -0
- package/lib/core/utils/matching/normalizePath.mjs +12 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.d.ts +7 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.js +30 -0
- package/lib/core/utils/request/getPublicUrlFromRequest.mjs +10 -0
- package/lib/core/utils/request/getRequestCookies.d.ts +8 -0
- package/lib/core/utils/request/getRequestCookies.js +88 -0
- package/lib/core/utils/request/getRequestCookies.mjs +60 -0
- package/lib/core/utils/request/onUnhandledRequest.d.ts +12 -0
- package/lib/core/utils/request/onUnhandledRequest.js +193 -0
- package/lib/core/utils/request/onUnhandledRequest.mjs +165 -0
- package/lib/core/utils/request/readResponseCookies.d.ts +3 -0
- package/lib/core/utils/request/readResponseCookies.js +45 -0
- package/lib/core/utils/request/readResponseCookies.mjs +27 -0
- package/lib/core/utils/toResponseInit.d.ts +3 -0
- package/lib/core/utils/toResponseInit.js +30 -0
- package/lib/core/utils/toResponseInit.mjs +10 -0
- package/lib/core/utils/url/cleanUrl.d.ts +7 -0
- package/lib/core/utils/url/cleanUrl.js +31 -0
- package/lib/core/utils/url/cleanUrl.mjs +11 -0
- package/lib/core/utils/url/getAbsoluteUrl.d.ts +6 -0
- package/lib/core/utils/url/getAbsoluteUrl.js +37 -0
- package/lib/core/utils/url/getAbsoluteUrl.mjs +17 -0
- package/lib/core/utils/url/isAbsoluteUrl.d.ts +6 -0
- package/lib/core/utils/url/isAbsoluteUrl.js +26 -0
- package/lib/core/utils/url/isAbsoluteUrl.mjs +6 -0
- package/lib/iife/index.js +6238 -12395
- package/lib/mockServiceWorker.js +82 -93
- package/lib/native/index.d.ts +67 -7
- package/lib/native/index.js +74 -1500
- package/lib/native/index.mjs +69 -1514
- package/lib/node/index.d.ts +68 -9
- package/lib/node/index.js +77 -1508
- package/lib/node/index.mjs +71 -1521
- package/native/package.json +2 -1
- package/node/package.json +2 -1
- package/package.json +55 -25
- package/lib/SetupServerApi-1855d9c6.d.ts +0 -20
- package/lib/glossary-de6278a9.d.ts +0 -385
- package/lib/iife/index.js.map +0 -1
- package/lib/index.d.ts +0 -365
- package/lib/index.js +0 -2295
- package/lib/index.js.map +0 -1
- package/lib/node/index.js.map +0 -1
- package/lib/node/index.mjs.map +0 -1
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var __async = (__this, __arguments, generator) => {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
var fulfilled = (value) => {
|
|
32
|
+
try {
|
|
33
|
+
step(generator.next(value));
|
|
34
|
+
} catch (e) {
|
|
35
|
+
reject(e);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var rejected = (value) => {
|
|
39
|
+
try {
|
|
40
|
+
step(generator.throw(value));
|
|
41
|
+
} catch (e) {
|
|
42
|
+
reject(e);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
46
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
var onUnhandledRequest_exports = {};
|
|
50
|
+
__export(onUnhandledRequest_exports, {
|
|
51
|
+
onUnhandledRequest: () => onUnhandledRequest
|
|
52
|
+
});
|
|
53
|
+
module.exports = __toCommonJS(onUnhandledRequest_exports);
|
|
54
|
+
var import_js_levenshtein = __toESM(require("@bundled-es-modules/js-levenshtein"));
|
|
55
|
+
var import__ = require("../../index.js");
|
|
56
|
+
var import_parseGraphQLRequest = require("../internal/parseGraphQLRequest.js");
|
|
57
|
+
var import_getPublicUrlFromRequest = require("./getPublicUrlFromRequest.js");
|
|
58
|
+
var import_isStringEqual = require("../internal/isStringEqual.js");
|
|
59
|
+
var import_devUtils = require("../internal/devUtils.js");
|
|
60
|
+
const getStringMatchScore = import_js_levenshtein.default;
|
|
61
|
+
const MAX_MATCH_SCORE = 3;
|
|
62
|
+
const MAX_SUGGESTION_COUNT = 4;
|
|
63
|
+
const TYPE_MATCH_DELTA = 0.5;
|
|
64
|
+
function groupHandlersByType(handlers) {
|
|
65
|
+
return handlers.reduce(
|
|
66
|
+
(groups, handler) => {
|
|
67
|
+
if (handler instanceof import__.HttpHandler) {
|
|
68
|
+
groups.http.push(handler);
|
|
69
|
+
}
|
|
70
|
+
if (handler instanceof import__.GraphQLHandler) {
|
|
71
|
+
groups.graphql.push(handler);
|
|
72
|
+
}
|
|
73
|
+
return groups;
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
http: [],
|
|
77
|
+
graphql: []
|
|
78
|
+
}
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
function getHttpHandlerScore() {
|
|
82
|
+
return (request, handler) => {
|
|
83
|
+
const { path, method } = handler.info;
|
|
84
|
+
if (path instanceof RegExp || method instanceof RegExp) {
|
|
85
|
+
return Infinity;
|
|
86
|
+
}
|
|
87
|
+
const hasSameMethod = (0, import_isStringEqual.isStringEqual)(request.method, method);
|
|
88
|
+
const methodScoreDelta = hasSameMethod ? TYPE_MATCH_DELTA : 0;
|
|
89
|
+
const requestPublicUrl = (0, import_getPublicUrlFromRequest.getPublicUrlFromRequest)(request);
|
|
90
|
+
const score = getStringMatchScore(requestPublicUrl, path);
|
|
91
|
+
return score - methodScoreDelta;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function getGraphQLHandlerScore(parsedQuery) {
|
|
95
|
+
return (_, handler) => {
|
|
96
|
+
if (typeof parsedQuery.operationName === "undefined") {
|
|
97
|
+
return Infinity;
|
|
98
|
+
}
|
|
99
|
+
const { operationType, operationName } = handler.info;
|
|
100
|
+
if (typeof operationName !== "string") {
|
|
101
|
+
return Infinity;
|
|
102
|
+
}
|
|
103
|
+
const hasSameOperationType = parsedQuery.operationType === operationType;
|
|
104
|
+
const operationTypeScoreDelta = hasSameOperationType ? TYPE_MATCH_DELTA : 0;
|
|
105
|
+
const score = getStringMatchScore(parsedQuery.operationName, operationName);
|
|
106
|
+
return score - operationTypeScoreDelta;
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function getSuggestedHandler(request, handlers, getScore) {
|
|
110
|
+
const suggestedHandlers = handlers.reduce((suggestions, handler) => {
|
|
111
|
+
const score = getScore(request, handler);
|
|
112
|
+
return suggestions.concat([[score, handler]]);
|
|
113
|
+
}, []).sort(([leftScore], [rightScore]) => leftScore - rightScore).filter(([score]) => score <= MAX_MATCH_SCORE).slice(0, MAX_SUGGESTION_COUNT).map(([, handler]) => handler);
|
|
114
|
+
return suggestedHandlers;
|
|
115
|
+
}
|
|
116
|
+
function getSuggestedHandlersMessage(handlers) {
|
|
117
|
+
if (handlers.length > 1) {
|
|
118
|
+
return `Did you mean to request one of the following resources instead?
|
|
119
|
+
|
|
120
|
+
${handlers.map((handler) => ` \u2022 ${handler.info.header}`).join("\n")}`;
|
|
121
|
+
}
|
|
122
|
+
return `Did you mean to request "${handlers[0].info.header}" instead?`;
|
|
123
|
+
}
|
|
124
|
+
function onUnhandledRequest(request, handlers, strategy = "warn") {
|
|
125
|
+
return __async(this, null, function* () {
|
|
126
|
+
const parsedGraphQLQuery = yield (0, import_parseGraphQLRequest.parseGraphQLRequest)(request).catch(
|
|
127
|
+
() => null
|
|
128
|
+
);
|
|
129
|
+
const publicUrl = (0, import_getPublicUrlFromRequest.getPublicUrlFromRequest)(request);
|
|
130
|
+
function generateHandlerSuggestion() {
|
|
131
|
+
const handlerGroups = groupHandlersByType(handlers);
|
|
132
|
+
const relevantHandlers = parsedGraphQLQuery ? handlerGroups.graphql : handlerGroups.http;
|
|
133
|
+
const suggestedHandlers = getSuggestedHandler(
|
|
134
|
+
request,
|
|
135
|
+
relevantHandlers,
|
|
136
|
+
parsedGraphQLQuery ? getGraphQLHandlerScore(parsedGraphQLQuery) : getHttpHandlerScore()
|
|
137
|
+
);
|
|
138
|
+
return suggestedHandlers.length > 0 ? getSuggestedHandlersMessage(suggestedHandlers) : "";
|
|
139
|
+
}
|
|
140
|
+
function getGraphQLRequestHeader(parsedGraphQLRequest) {
|
|
141
|
+
if (!(parsedGraphQLRequest == null ? void 0 : parsedGraphQLRequest.operationName)) {
|
|
142
|
+
return `anonymous ${parsedGraphQLRequest == null ? void 0 : parsedGraphQLRequest.operationType} (${request.method} ${publicUrl})`;
|
|
143
|
+
}
|
|
144
|
+
return `${parsedGraphQLRequest.operationType} ${parsedGraphQLRequest.operationName} (${request.method} ${publicUrl})`;
|
|
145
|
+
}
|
|
146
|
+
function generateUnhandledRequestMessage() {
|
|
147
|
+
const requestHeader = parsedGraphQLQuery ? getGraphQLRequestHeader(parsedGraphQLQuery) : `${request.method} ${publicUrl}`;
|
|
148
|
+
const handlerSuggestion = generateHandlerSuggestion();
|
|
149
|
+
const messageTemplate = [
|
|
150
|
+
`intercepted a request without a matching request handler:`,
|
|
151
|
+
` \u2022 ${requestHeader}`,
|
|
152
|
+
handlerSuggestion,
|
|
153
|
+
`If you still wish to intercept this unhandled request, please create a request handler for it.
|
|
154
|
+
Read more: https://mswjs.io/docs/getting-started/mocks`
|
|
155
|
+
].filter(Boolean);
|
|
156
|
+
return messageTemplate.join("\n\n");
|
|
157
|
+
}
|
|
158
|
+
function applyStrategy(strategy2) {
|
|
159
|
+
const message = generateUnhandledRequestMessage();
|
|
160
|
+
switch (strategy2) {
|
|
161
|
+
case "error": {
|
|
162
|
+
import_devUtils.devUtils.error("Error: %s", message);
|
|
163
|
+
throw new Error(
|
|
164
|
+
import_devUtils.devUtils.formatMessage(
|
|
165
|
+
'Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.'
|
|
166
|
+
)
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
case "warn": {
|
|
170
|
+
import_devUtils.devUtils.warn("Warning: %s", message);
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
case "bypass":
|
|
174
|
+
break;
|
|
175
|
+
default:
|
|
176
|
+
throw new Error(
|
|
177
|
+
import_devUtils.devUtils.formatMessage(
|
|
178
|
+
'Failed to react to an unhandled request: unknown strategy "%s". Please provide one of the supported strategies ("bypass", "warn", "error") or a custom callback function as the value of the "onUnhandledRequest" option.',
|
|
179
|
+
strategy2
|
|
180
|
+
)
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (typeof strategy === "function") {
|
|
185
|
+
strategy(request, {
|
|
186
|
+
warning: applyStrategy.bind(null, "warn"),
|
|
187
|
+
error: applyStrategy.bind(null, "error")
|
|
188
|
+
});
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
applyStrategy(strategy);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
import jsLevenshtein from "@bundled-es-modules/js-levenshtein";
|
|
22
|
+
import { HttpHandler, GraphQLHandler } from '../../index.mjs';
|
|
23
|
+
import {
|
|
24
|
+
parseGraphQLRequest
|
|
25
|
+
} from '../internal/parseGraphQLRequest.mjs';
|
|
26
|
+
import { getPublicUrlFromRequest } from './getPublicUrlFromRequest.mjs';
|
|
27
|
+
import { isStringEqual } from '../internal/isStringEqual.mjs';
|
|
28
|
+
import { devUtils } from '../internal/devUtils.mjs';
|
|
29
|
+
const getStringMatchScore = jsLevenshtein;
|
|
30
|
+
const MAX_MATCH_SCORE = 3;
|
|
31
|
+
const MAX_SUGGESTION_COUNT = 4;
|
|
32
|
+
const TYPE_MATCH_DELTA = 0.5;
|
|
33
|
+
function groupHandlersByType(handlers) {
|
|
34
|
+
return handlers.reduce(
|
|
35
|
+
(groups, handler) => {
|
|
36
|
+
if (handler instanceof HttpHandler) {
|
|
37
|
+
groups.http.push(handler);
|
|
38
|
+
}
|
|
39
|
+
if (handler instanceof GraphQLHandler) {
|
|
40
|
+
groups.graphql.push(handler);
|
|
41
|
+
}
|
|
42
|
+
return groups;
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
http: [],
|
|
46
|
+
graphql: []
|
|
47
|
+
}
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
function getHttpHandlerScore() {
|
|
51
|
+
return (request, handler) => {
|
|
52
|
+
const { path, method } = handler.info;
|
|
53
|
+
if (path instanceof RegExp || method instanceof RegExp) {
|
|
54
|
+
return Infinity;
|
|
55
|
+
}
|
|
56
|
+
const hasSameMethod = isStringEqual(request.method, method);
|
|
57
|
+
const methodScoreDelta = hasSameMethod ? TYPE_MATCH_DELTA : 0;
|
|
58
|
+
const requestPublicUrl = getPublicUrlFromRequest(request);
|
|
59
|
+
const score = getStringMatchScore(requestPublicUrl, path);
|
|
60
|
+
return score - methodScoreDelta;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function getGraphQLHandlerScore(parsedQuery) {
|
|
64
|
+
return (_, handler) => {
|
|
65
|
+
if (typeof parsedQuery.operationName === "undefined") {
|
|
66
|
+
return Infinity;
|
|
67
|
+
}
|
|
68
|
+
const { operationType, operationName } = handler.info;
|
|
69
|
+
if (typeof operationName !== "string") {
|
|
70
|
+
return Infinity;
|
|
71
|
+
}
|
|
72
|
+
const hasSameOperationType = parsedQuery.operationType === operationType;
|
|
73
|
+
const operationTypeScoreDelta = hasSameOperationType ? TYPE_MATCH_DELTA : 0;
|
|
74
|
+
const score = getStringMatchScore(parsedQuery.operationName, operationName);
|
|
75
|
+
return score - operationTypeScoreDelta;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function getSuggestedHandler(request, handlers, getScore) {
|
|
79
|
+
const suggestedHandlers = handlers.reduce((suggestions, handler) => {
|
|
80
|
+
const score = getScore(request, handler);
|
|
81
|
+
return suggestions.concat([[score, handler]]);
|
|
82
|
+
}, []).sort(([leftScore], [rightScore]) => leftScore - rightScore).filter(([score]) => score <= MAX_MATCH_SCORE).slice(0, MAX_SUGGESTION_COUNT).map(([, handler]) => handler);
|
|
83
|
+
return suggestedHandlers;
|
|
84
|
+
}
|
|
85
|
+
function getSuggestedHandlersMessage(handlers) {
|
|
86
|
+
if (handlers.length > 1) {
|
|
87
|
+
return `Did you mean to request one of the following resources instead?
|
|
88
|
+
|
|
89
|
+
${handlers.map((handler) => ` \u2022 ${handler.info.header}`).join("\n")}`;
|
|
90
|
+
}
|
|
91
|
+
return `Did you mean to request "${handlers[0].info.header}" instead?`;
|
|
92
|
+
}
|
|
93
|
+
function onUnhandledRequest(request, handlers, strategy = "warn") {
|
|
94
|
+
return __async(this, null, function* () {
|
|
95
|
+
const parsedGraphQLQuery = yield parseGraphQLRequest(request).catch(
|
|
96
|
+
() => null
|
|
97
|
+
);
|
|
98
|
+
const publicUrl = getPublicUrlFromRequest(request);
|
|
99
|
+
function generateHandlerSuggestion() {
|
|
100
|
+
const handlerGroups = groupHandlersByType(handlers);
|
|
101
|
+
const relevantHandlers = parsedGraphQLQuery ? handlerGroups.graphql : handlerGroups.http;
|
|
102
|
+
const suggestedHandlers = getSuggestedHandler(
|
|
103
|
+
request,
|
|
104
|
+
relevantHandlers,
|
|
105
|
+
parsedGraphQLQuery ? getGraphQLHandlerScore(parsedGraphQLQuery) : getHttpHandlerScore()
|
|
106
|
+
);
|
|
107
|
+
return suggestedHandlers.length > 0 ? getSuggestedHandlersMessage(suggestedHandlers) : "";
|
|
108
|
+
}
|
|
109
|
+
function getGraphQLRequestHeader(parsedGraphQLRequest) {
|
|
110
|
+
if (!(parsedGraphQLRequest == null ? void 0 : parsedGraphQLRequest.operationName)) {
|
|
111
|
+
return `anonymous ${parsedGraphQLRequest == null ? void 0 : parsedGraphQLRequest.operationType} (${request.method} ${publicUrl})`;
|
|
112
|
+
}
|
|
113
|
+
return `${parsedGraphQLRequest.operationType} ${parsedGraphQLRequest.operationName} (${request.method} ${publicUrl})`;
|
|
114
|
+
}
|
|
115
|
+
function generateUnhandledRequestMessage() {
|
|
116
|
+
const requestHeader = parsedGraphQLQuery ? getGraphQLRequestHeader(parsedGraphQLQuery) : `${request.method} ${publicUrl}`;
|
|
117
|
+
const handlerSuggestion = generateHandlerSuggestion();
|
|
118
|
+
const messageTemplate = [
|
|
119
|
+
`intercepted a request without a matching request handler:`,
|
|
120
|
+
` \u2022 ${requestHeader}`,
|
|
121
|
+
handlerSuggestion,
|
|
122
|
+
`If you still wish to intercept this unhandled request, please create a request handler for it.
|
|
123
|
+
Read more: https://mswjs.io/docs/getting-started/mocks`
|
|
124
|
+
].filter(Boolean);
|
|
125
|
+
return messageTemplate.join("\n\n");
|
|
126
|
+
}
|
|
127
|
+
function applyStrategy(strategy2) {
|
|
128
|
+
const message = generateUnhandledRequestMessage();
|
|
129
|
+
switch (strategy2) {
|
|
130
|
+
case "error": {
|
|
131
|
+
devUtils.error("Error: %s", message);
|
|
132
|
+
throw new Error(
|
|
133
|
+
devUtils.formatMessage(
|
|
134
|
+
'Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.'
|
|
135
|
+
)
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
case "warn": {
|
|
139
|
+
devUtils.warn("Warning: %s", message);
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
case "bypass":
|
|
143
|
+
break;
|
|
144
|
+
default:
|
|
145
|
+
throw new Error(
|
|
146
|
+
devUtils.formatMessage(
|
|
147
|
+
'Failed to react to an unhandled request: unknown strategy "%s". Please provide one of the supported strategies ("bypass", "warn", "error") or a custom callback function as the value of the "onUnhandledRequest" option.',
|
|
148
|
+
strategy2
|
|
149
|
+
)
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (typeof strategy === "function") {
|
|
154
|
+
strategy(request, {
|
|
155
|
+
warning: applyStrategy.bind(null, "warn"),
|
|
156
|
+
error: applyStrategy.bind(null, "error")
|
|
157
|
+
});
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
applyStrategy(strategy);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
export {
|
|
164
|
+
onUnhandledRequest
|
|
165
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __spreadValues = (a, b) => {
|
|
12
|
+
for (var prop in b || (b = {}))
|
|
13
|
+
if (__hasOwnProp.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
if (__getOwnPropSymbols)
|
|
16
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
17
|
+
if (__propIsEnum.call(b, prop))
|
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
};
|
|
22
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
23
|
+
var __export = (target, all) => {
|
|
24
|
+
for (var name in all)
|
|
25
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
26
|
+
};
|
|
27
|
+
var __copyProps = (to, from, except, desc) => {
|
|
28
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
29
|
+
for (let key of __getOwnPropNames(from))
|
|
30
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
31
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
32
|
+
}
|
|
33
|
+
return to;
|
|
34
|
+
};
|
|
35
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
36
|
+
var readResponseCookies_exports = {};
|
|
37
|
+
__export(readResponseCookies_exports, {
|
|
38
|
+
readResponseCookies: () => readResponseCookies
|
|
39
|
+
});
|
|
40
|
+
module.exports = __toCommonJS(readResponseCookies_exports);
|
|
41
|
+
var import_cookies = require("@mswjs/cookies");
|
|
42
|
+
function readResponseCookies(request, response) {
|
|
43
|
+
import_cookies.store.add(__spreadProps(__spreadValues({}, request), { url: request.url.toString() }), response);
|
|
44
|
+
import_cookies.store.persist();
|
|
45
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
import { store } from "@mswjs/cookies";
|
|
21
|
+
function readResponseCookies(request, response) {
|
|
22
|
+
store.add(__spreadProps(__spreadValues({}, request), { url: request.url.toString() }), response);
|
|
23
|
+
store.persist();
|
|
24
|
+
}
|
|
25
|
+
export {
|
|
26
|
+
readResponseCookies
|
|
27
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var toResponseInit_exports = {};
|
|
20
|
+
__export(toResponseInit_exports, {
|
|
21
|
+
toResponseInit: () => toResponseInit
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(toResponseInit_exports);
|
|
24
|
+
function toResponseInit(response) {
|
|
25
|
+
return {
|
|
26
|
+
status: response.status,
|
|
27
|
+
statusText: response.statusText,
|
|
28
|
+
headers: Object.fromEntries(response.headers.entries())
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var cleanUrl_exports = {};
|
|
20
|
+
__export(cleanUrl_exports, {
|
|
21
|
+
cleanUrl: () => cleanUrl,
|
|
22
|
+
getSearchParams: () => getSearchParams
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(cleanUrl_exports);
|
|
25
|
+
const REDUNDANT_CHARACTERS_EXP = /[\?|#].*$/g;
|
|
26
|
+
function getSearchParams(path) {
|
|
27
|
+
return new URL(`/${path}`, "http://localhost").searchParams;
|
|
28
|
+
}
|
|
29
|
+
function cleanUrl(path) {
|
|
30
|
+
return path.replace(REDUNDANT_CHARACTERS_EXP, "");
|
|
31
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const REDUNDANT_CHARACTERS_EXP = /[\?|#].*$/g;
|
|
2
|
+
function getSearchParams(path) {
|
|
3
|
+
return new URL(`/${path}`, "http://localhost").searchParams;
|
|
4
|
+
}
|
|
5
|
+
function cleanUrl(path) {
|
|
6
|
+
return path.replace(REDUNDANT_CHARACTERS_EXP, "");
|
|
7
|
+
}
|
|
8
|
+
export {
|
|
9
|
+
cleanUrl,
|
|
10
|
+
getSearchParams
|
|
11
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var getAbsoluteUrl_exports = {};
|
|
20
|
+
__export(getAbsoluteUrl_exports, {
|
|
21
|
+
getAbsoluteUrl: () => getAbsoluteUrl
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(getAbsoluteUrl_exports);
|
|
24
|
+
var import_isAbsoluteUrl = require("./isAbsoluteUrl.js");
|
|
25
|
+
function getAbsoluteUrl(path, baseUrl) {
|
|
26
|
+
if ((0, import_isAbsoluteUrl.isAbsoluteUrl)(path)) {
|
|
27
|
+
return path;
|
|
28
|
+
}
|
|
29
|
+
if (path.startsWith("*")) {
|
|
30
|
+
return path;
|
|
31
|
+
}
|
|
32
|
+
const origin = baseUrl || typeof document !== "undefined" && document.baseURI;
|
|
33
|
+
return origin ? (
|
|
34
|
+
// Encode and decode the path to preserve escaped characters.
|
|
35
|
+
decodeURI(new URL(encodeURI(path), origin).href)
|
|
36
|
+
) : path;
|
|
37
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { isAbsoluteUrl } from './isAbsoluteUrl.mjs';
|
|
2
|
+
function getAbsoluteUrl(path, baseUrl) {
|
|
3
|
+
if (isAbsoluteUrl(path)) {
|
|
4
|
+
return path;
|
|
5
|
+
}
|
|
6
|
+
if (path.startsWith("*")) {
|
|
7
|
+
return path;
|
|
8
|
+
}
|
|
9
|
+
const origin = baseUrl || typeof document !== "undefined" && document.baseURI;
|
|
10
|
+
return origin ? (
|
|
11
|
+
// Encode and decode the path to preserve escaped characters.
|
|
12
|
+
decodeURI(new URL(encodeURI(path), origin).href)
|
|
13
|
+
) : path;
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
getAbsoluteUrl
|
|
17
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var isAbsoluteUrl_exports = {};
|
|
20
|
+
__export(isAbsoluteUrl_exports, {
|
|
21
|
+
isAbsoluteUrl: () => isAbsoluteUrl
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(isAbsoluteUrl_exports);
|
|
24
|
+
function isAbsoluteUrl(url) {
|
|
25
|
+
return /^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(url);
|
|
26
|
+
}
|