msw 0.36.8 → 0.38.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/esm/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
- const handlerGroups = groupHandlersByType(handlers);
1036
- const relevantHandlers = parsedGraphQLQuery
1037
- ? handlerGroups.graphql
1038
- : handlerGroups.rest;
1039
- const suggestedHandlers = getSuggestedHandler(request, relevantHandlers, parsedGraphQLQuery
1040
- ? getGraphQLHandlerScore(parsedGraphQLQuery)
1041
- : getRestHandlerScore());
1042
- const handlerSuggestion = suggestedHandlers.length > 0
1043
- ? getSuggestedHandlersMessage(suggestedHandlers)
1044
- : '';
1045
- const publicUrl = getPublicUrlFromRequest(request);
1046
- const requestHeader = parsedGraphQLQuery
1047
- ? `${parsedGraphQLQuery.operationType} ${parsedGraphQLQuery.operationName} (${request.method} ${publicUrl})`
1048
- : `${request.method} ${publicUrl}`;
1049
- const messageTemplate = [
1050
- `captured a request without a matching request handler:`,
1051
- ` \u2022 ${requestHeader}`,
1052
- handlerSuggestion,
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
- ].filter(Boolean);
1058
- const message = messageTemplate.join('\n\n');
1059
- switch (strategy) {
1060
- case 'error': {
1061
- // Print a developer-friendly error.
1062
- devUtils.error('Error: %s', message);
1063
- // Throw an exception to halt request processing and not perform the original request.
1064
- throw new Error(devUtils.formatMessage('Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.'));
1065
- }
1066
- case 'warn': {
1067
- devUtils.warn('Warning: %s', message);
1068
- break;
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;
@@ -2,7 +2,7 @@
2
2
  /* tslint:disable */
3
3
 
4
4
  /**
5
- * Mock Service Worker (0.36.8).
5
+ * Mock Service Worker (0.38.2).
6
6
  * @see https://github.com/mswjs/msw
7
7
  * - Please do NOT modify this file.
8
8
  * - Please do NOT serve this file on production.