msw 0.36.5 → 0.38.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/README.md +3 -1
- package/lib/esm/fetch-deps.js +14 -12
- package/lib/esm/graphql-deps.js +1980 -1804
- package/lib/esm/index.js +60 -45
- package/lib/esm/mockServiceWorker.js +1 -1
- package/lib/iife/index.js +3 -3
- package/lib/iife/mockServiceWorker.js +1 -1
- package/lib/types/graphql.d.ts +6 -6
- package/lib/types/handlers/RequestHandler.d.ts +2 -2
- package/lib/types/response.d.ts +1 -1
- package/lib/types/setupWorker/glossary.d.ts +1 -1
- package/lib/types/utils/logging/prepareResponse.d.ts +2 -2
- package/lib/types/utils/request/onUnhandledRequest.d.ts +5 -1
- package/lib/types/utils/request/parseBody.d.ts +1 -1
- package/lib/umd/index.js +17091 -14331
- package/lib/umd/mockServiceWorker.js +1 -1
- package/native/lib/index.js +2026 -1833
- package/node/lib/index.js +2026 -1833
- package/package.json +23 -24
package/lib/esm/index.js
CHANGED
|
@@ -1022,56 +1022,71 @@ ${handlers.map((handler) => ` • ${handler.info.header}`).join('\n')}`;
|
|
|
1022
1022
|
return `Did you mean to request "${handlers[0].info.header}" instead?`;
|
|
1023
1023
|
}
|
|
1024
1024
|
function onUnhandledRequest(request, handlers, strategy = 'warn') {
|
|
1025
|
-
if (typeof strategy === 'function') {
|
|
1026
|
-
strategy(request);
|
|
1027
|
-
return;
|
|
1028
|
-
}
|
|
1029
|
-
/**
|
|
1030
|
-
* @note Ignore exceptions during GraphQL request parsing because at this point
|
|
1031
|
-
* we cannot assume the unhandled request is a valid GraphQL request.
|
|
1032
|
-
* If the GraphQL parsing fails, just don't treat it as a GraphQL request.
|
|
1033
|
-
*/
|
|
1034
1025
|
const parsedGraphQLQuery = tryCatch(() => parseGraphQLRequest(request));
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1026
|
+
function generateHandlerSuggestion() {
|
|
1027
|
+
/**
|
|
1028
|
+
* @note Ignore exceptions during GraphQL request parsing because at this point
|
|
1029
|
+
* we cannot assume the unhandled request is a valid GraphQL request.
|
|
1030
|
+
* If the GraphQL parsing fails, just don't treat it as a GraphQL request.
|
|
1031
|
+
*/
|
|
1032
|
+
const handlerGroups = groupHandlersByType(handlers);
|
|
1033
|
+
const relevantHandlers = parsedGraphQLQuery
|
|
1034
|
+
? handlerGroups.graphql
|
|
1035
|
+
: handlerGroups.rest;
|
|
1036
|
+
const suggestedHandlers = getSuggestedHandler(request, relevantHandlers, parsedGraphQLQuery
|
|
1037
|
+
? getGraphQLHandlerScore(parsedGraphQLQuery)
|
|
1038
|
+
: getRestHandlerScore());
|
|
1039
|
+
return suggestedHandlers.length > 0
|
|
1040
|
+
? getSuggestedHandlersMessage(suggestedHandlers)
|
|
1041
|
+
: '';
|
|
1042
|
+
}
|
|
1043
|
+
function generateUnhandledRequestMessage() {
|
|
1044
|
+
const publicUrl = getPublicUrlFromRequest(request);
|
|
1045
|
+
const requestHeader = parsedGraphQLQuery
|
|
1046
|
+
? `${parsedGraphQLQuery.operationType} ${parsedGraphQLQuery.operationName} (${request.method} ${publicUrl})`
|
|
1047
|
+
: `${request.method} ${publicUrl}`;
|
|
1048
|
+
const handlerSuggestion = generateHandlerSuggestion();
|
|
1049
|
+
const messageTemplate = [
|
|
1050
|
+
`captured a request without a matching request handler:`,
|
|
1051
|
+
` \u2022 ${requestHeader}`,
|
|
1052
|
+
handlerSuggestion,
|
|
1053
|
+
`\
|
|
1054
1054
|
If you still wish to intercept this unhandled request, please create a request handler for it.
|
|
1055
1055
|
Read more: https://mswjs.io/docs/getting-started/mocks\
|
|
1056
1056
|
`,
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1057
|
+
].filter(Boolean);
|
|
1058
|
+
return messageTemplate.join('\n\n');
|
|
1059
|
+
}
|
|
1060
|
+
function applyStrategy(strategy) {
|
|
1061
|
+
// Generate handler suggestions only when applying the strategy.
|
|
1062
|
+
// This saves bandwidth for scenarios when developers opt-out
|
|
1063
|
+
// from the default unhandled request handling strategy.
|
|
1064
|
+
const message = generateUnhandledRequestMessage();
|
|
1065
|
+
switch (strategy) {
|
|
1066
|
+
case 'error': {
|
|
1067
|
+
// Print a developer-friendly error.
|
|
1068
|
+
devUtils.error('Error: %s', message);
|
|
1069
|
+
// Throw an exception to halt request processing and not perform the original request.
|
|
1070
|
+
throw new Error(devUtils.formatMessage('Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.'));
|
|
1071
|
+
}
|
|
1072
|
+
case 'warn': {
|
|
1073
|
+
devUtils.warn('Warning: %s', message);
|
|
1074
|
+
break;
|
|
1075
|
+
}
|
|
1076
|
+
case 'bypass':
|
|
1077
|
+
break;
|
|
1078
|
+
default:
|
|
1079
|
+
throw new Error(devUtils.formatMessage('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.', strategy));
|
|
1069
1080
|
}
|
|
1070
|
-
case 'bypass':
|
|
1071
|
-
break;
|
|
1072
|
-
default:
|
|
1073
|
-
throw new Error(devUtils.formatMessage('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.', strategy));
|
|
1074
1081
|
}
|
|
1082
|
+
if (typeof strategy === 'function') {
|
|
1083
|
+
strategy(request, {
|
|
1084
|
+
warning: applyStrategy.bind(null, 'warn'),
|
|
1085
|
+
error: applyStrategy.bind(null, 'error'),
|
|
1086
|
+
});
|
|
1087
|
+
return;
|
|
1088
|
+
}
|
|
1089
|
+
applyStrategy(strategy);
|
|
1075
1090
|
}
|
|
1076
1091
|
|
|
1077
1092
|
function readResponseCookies(request, response) {
|
|
@@ -1084,7 +1099,7 @@ function handleRequest(request, handlers, options, emitter, handleRequestOptions
|
|
|
1084
1099
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1085
1100
|
emitter.emit('request:start', request);
|
|
1086
1101
|
// Perform bypassed requests (i.e. issued via "ctx.fetch") as-is.
|
|
1087
|
-
if (request.headers.get('x-msw-bypass')) {
|
|
1102
|
+
if (request.headers.get('x-msw-bypass') === 'true') {
|
|
1088
1103
|
emitter.emit('request:end', request);
|
|
1089
1104
|
(_a = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.onBypassResponse) === null || _a === void 0 ? void 0 : _a.call(handleRequestOptions, request);
|
|
1090
1105
|
return;
|