msw 0.36.0 → 0.36.4

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/node/lib/index.js CHANGED
@@ -2248,14 +2248,6 @@ function tryDecode(str, decode) {
2248
2248
  }
2249
2249
  }
2250
2250
 
2251
- function uuidv4() {
2252
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
2253
- const r = (Math.random() * 16) | 0;
2254
- const v = c == 'x' ? r : (r & 0x3) | 0x8;
2255
- return v.toString(16);
2256
- });
2257
- }
2258
-
2259
2251
  /**
2260
2252
  * Parses a given value into a JSON.
2261
2253
  * Does not throw an exception on an invalid JSON string.
@@ -2934,10 +2926,8 @@ function setRequestCookies(request) {
2934
2926
  * Converts a given isomorphic request to a `MockedRequest` instance.
2935
2927
  */
2936
2928
  function parseIsomorphicRequest(request) {
2937
- const requestId = uuidv4();
2938
- request.headers.set('x-msw-request-id', requestId);
2939
2929
  const mockedRequest = {
2940
- id: requestId,
2930
+ id: request.id,
2941
2931
  url: request.url,
2942
2932
  method: request.method,
2943
2933
  body: parseBody(request.body, request.headers),
@@ -7273,12 +7263,23 @@ function cleanUrl(path) {
7273
7263
  return path.replace(REDUNDANT_CHARACTERS_EXP, '');
7274
7264
  }
7275
7265
 
7266
+ /**
7267
+ * Determines if the given URL string is an absolute URL.
7268
+ */
7269
+ function isAbsoluteUrl(url) {
7270
+ return /^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(url);
7271
+ }
7272
+
7276
7273
  /**
7277
7274
  * Returns an absolute URL based on the given path.
7278
7275
  */
7279
7276
  function getAbsoluteUrl(path, baseUrl) {
7280
- // Ignore absolute URLs.
7281
- if (!path.startsWith('/')) {
7277
+ // already absolute URL
7278
+ if (isAbsoluteUrl(path)) {
7279
+ return path;
7280
+ }
7281
+ // Ignore path with pattern start with *
7282
+ if (path.startsWith('*')) {
7282
7283
  return path;
7283
7284
  }
7284
7285
  // Resolve a relative request URL against a given custom "baseUrl"
@@ -7312,18 +7313,31 @@ function normalizePath(path, baseUrl) {
7312
7313
  */
7313
7314
  function coercePath(path) {
7314
7315
  return (path
7315
- /**
7316
- * Escape the protocol so that "path-to-regexp" could match
7317
- * absolute URL.
7318
- * @see https://github.com/pillarjs/path-to-regexp/issues/259
7319
- */
7320
- .replace(/^([^\/]+)(:)(?=\/\/)/g, '$1\\$2')
7321
7316
  /**
7322
7317
  * Replace wildcards ("*") with unnamed capturing groups
7323
7318
  * because "path-to-regexp" doesn't support wildcards.
7324
7319
  * Ignore path parameter' modifiers (i.e. ":name*").
7325
7320
  */
7326
- .replace(/(?<!(^|\/|\*+):[\w]+)(\*{1,2})/g, '(.*)'));
7321
+ .replace(/([:a-zA-Z_-]*)(\*{1,2})+/g, (_, parameterName, wildcard) => {
7322
+ const expression = '(.*)';
7323
+ if (!parameterName) {
7324
+ return expression;
7325
+ }
7326
+ return parameterName.startsWith(':')
7327
+ ? `${parameterName}${wildcard}`
7328
+ : `${parameterName}${expression}`;
7329
+ })
7330
+ /**
7331
+ * Escape the port so that "path-to-regexp" can match
7332
+ * absolute URLs including port numbers.
7333
+ */
7334
+ .replace(/([^\/])(:)(?=\d+)/, '$1\\$2')
7335
+ /**
7336
+ * Escape the protocol so that "path-to-regexp" could match
7337
+ * absolute URL.
7338
+ * @see https://github.com/pillarjs/path-to-regexp/issues/259
7339
+ */
7340
+ .replace(/^([^\/]+)(:)(?=\/\/)/, '$1\\$2'));
7327
7341
  }
7328
7342
  /**
7329
7343
  * Returns the result of matching given request URL against a mask.
@@ -7583,7 +7597,6 @@ class RestHandler extends RequestHandler {
7583
7597
  const matchesMethod = this.info.method instanceof RegExp
7584
7598
  ? this.info.method.test(request.method)
7585
7599
  : isStringEqual(this.info.method, request.method);
7586
- // console.log({ request, matchesMethod, parsedResult })
7587
7600
  return matchesMethod && parsedResult.matches;
7588
7601
  }
7589
7602
  log(request, response) {
@@ -7716,10 +7729,10 @@ function groupHandlersByType(handlers) {
7716
7729
  graphql: [],
7717
7730
  });
7718
7731
  }
7719
- function getScoreForRestHandler() {
7732
+ function getRestHandlerScore() {
7720
7733
  return (request, handler) => {
7721
7734
  const { path, method } = handler.info;
7722
- if (path instanceof RegExp) {
7735
+ if (path instanceof RegExp || method instanceof RegExp) {
7723
7736
  return Infinity;
7724
7737
  }
7725
7738
  const hasSameMethod = isStringEqual(request.method, method);
@@ -7730,12 +7743,15 @@ function getScoreForRestHandler() {
7730
7743
  return score - methodScoreDelta;
7731
7744
  };
7732
7745
  }
7733
- function getScoreForGraphQLHandler(parsedQuery) {
7746
+ function getGraphQLHandlerScore(parsedQuery) {
7734
7747
  return (_, handler) => {
7735
7748
  if (typeof parsedQuery.operationName === 'undefined') {
7736
7749
  return Infinity;
7737
7750
  }
7738
7751
  const { operationType, operationName } = handler.info;
7752
+ if (typeof operationName !== 'string') {
7753
+ return Infinity;
7754
+ }
7739
7755
  const hasSameOperationType = parsedQuery.operationType === operationType;
7740
7756
  // Always treat a handler with the same operation type as a more similar one.
7741
7757
  const operationTypeScoreDelta = hasSameOperationType ? TYPE_MATCH_DELTA : 0;
@@ -7745,16 +7761,12 @@ function getScoreForGraphQLHandler(parsedQuery) {
7745
7761
  }
7746
7762
  function getSuggestedHandler(request, handlers, getScore) {
7747
7763
  const suggestedHandlers = handlers
7748
- .reduce((acc, handler) => {
7764
+ .reduce((suggestions, handler) => {
7749
7765
  const score = getScore(request, handler);
7750
- return acc.concat([[score, handler]]);
7766
+ return suggestions.concat([[score, handler]]);
7751
7767
  }, [])
7752
- .sort(([leftScore], [rightScore]) => {
7753
- return leftScore - rightScore;
7754
- })
7755
- .filter(([score]) => {
7756
- return score <= MAX_MATCH_SCORE;
7757
- })
7768
+ .sort(([leftScore], [rightScore]) => leftScore - rightScore)
7769
+ .filter(([score]) => score <= MAX_MATCH_SCORE)
7758
7770
  .slice(0, MAX_SUGGESTION_COUNT)
7759
7771
  .map(([, handler]) => handler);
7760
7772
  return suggestedHandlers;
@@ -7784,8 +7796,8 @@ function onUnhandledRequest(request, handlers, strategy = 'warn') {
7784
7796
  ? handlerGroups.graphql
7785
7797
  : handlerGroups.rest;
7786
7798
  const suggestedHandlers = getSuggestedHandler(request, relevantHandlers, parsedGraphQLQuery
7787
- ? getScoreForGraphQLHandler(parsedGraphQLQuery)
7788
- : getScoreForRestHandler());
7799
+ ? getGraphQLHandlerScore(parsedGraphQLQuery)
7800
+ : getRestHandlerScore());
7789
7801
  const handlerSuggestion = suggestedHandlers.length > 0
7790
7802
  ? getSuggestedHandlersMessage(suggestedHandlers)
7791
7803
  : '';
@@ -7808,7 +7820,7 @@ Read more: https://mswjs.io/docs/getting-started/mocks\
7808
7820
  // Print a developer-friendly error.
7809
7821
  devUtils.error('Error: %s', message);
7810
7822
  // Throw an exception to halt request processing and not perform the original request.
7811
- throw new Error('Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.');
7823
+ throw new Error(devUtils.formatMessage('Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.'));
7812
7824
  }
7813
7825
  case 'warn': {
7814
7826
  devUtils.warn('Warning: %s', message);
@@ -7940,15 +7952,14 @@ function createSetupServer(...interceptors$1) {
7940
7952
  },
7941
7953
  });
7942
7954
  interceptor.on('response', (request, response) => {
7943
- const requestId = request.headers.get('x-msw-request-id');
7944
- if (!requestId) {
7955
+ if (!request.id) {
7945
7956
  return;
7946
7957
  }
7947
7958
  if (response.headers.get('x-powered-by') === 'msw') {
7948
- emitter.emit('response:mocked', response, requestId);
7959
+ emitter.emit('response:mocked', response, request.id);
7949
7960
  }
7950
7961
  else {
7951
- emitter.emit('response:bypass', response, requestId);
7962
+ emitter.emit('response:bypass', response, request.id);
7952
7963
  }
7953
7964
  });
7954
7965
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "msw",
3
- "version": "0.36.0",
3
+ "version": "0.36.4",
4
4
  "description": "Seamless REST/GraphQL API mocking library for browser and Node.js.",
5
5
  "main": "lib/umd/index.js",
6
6
  "module": "lib/esm/index.js",