msw 0.36.1 → 0.36.5

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.
@@ -2,7 +2,7 @@
2
2
  /* tslint:disable */
3
3
 
4
4
  /**
5
- * Mock Service Worker (0.36.1).
5
+ * Mock Service Worker (0.36.5).
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.
@@ -362,14 +362,6 @@ function tryDecode(str, decode) {
362
362
  }
363
363
  }
364
364
 
365
- function uuidv4() {
366
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
367
- const r = (Math.random() * 16) | 0;
368
- const v = c == 'x' ? r : (r & 0x3) | 0x8;
369
- return v.toString(16);
370
- });
371
- }
372
-
373
365
  /**
374
366
  * Parses a given value into a JSON.
375
367
  * Does not throw an exception on an invalid JSON string.
@@ -985,11 +977,12 @@ function parseMultipartData(data, headers) {
985
977
  * Parses a given request/response body based on the "Content-Type" header.
986
978
  */
987
979
  function parseBody(body, headers) {
980
+ var _a;
988
981
  // Return whatever falsey body value is given.
989
982
  if (!body) {
990
983
  return body;
991
984
  }
992
- const contentType = (headers === null || headers === void 0 ? void 0 : headers.get('content-type')) || '';
985
+ const contentType = ((_a = headers === null || headers === void 0 ? void 0 : headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || '';
993
986
  // If the body has a Multipart Content-Type
994
987
  // parse it into an object.
995
988
  const hasMultipartContent = contentType.startsWith('multipart/form-data');
@@ -1048,10 +1041,8 @@ function setRequestCookies(request) {
1048
1041
  * Converts a given isomorphic request to a `MockedRequest` instance.
1049
1042
  */
1050
1043
  function parseIsomorphicRequest(request) {
1051
- const requestId = uuidv4();
1052
- request.headers.set('x-msw-request-id', requestId);
1053
1044
  const mockedRequest = {
1054
- id: requestId,
1045
+ id: request.id,
1055
1046
  url: request.url,
1056
1047
  method: request.method,
1057
1048
  body: parseBody(request.body, request.headers),
@@ -1234,11 +1225,9 @@ var InvariantError = /** @class */ (function (_super) {
1234
1225
  _this.name = 'Invariant Violation';
1235
1226
  _this.message = format_1.format.apply(void 0, __spreadArray([message], positionals));
1236
1227
  if (_this.stack) {
1237
- var prevStack = _this.stack;
1238
- _this.stack = prevStack
1239
- .split('\n')
1240
- .slice(STACK_FRAMES_TO_IGNORE)
1241
- .join('\n');
1228
+ var nextStack = _this.stack.split('\n');
1229
+ nextStack.splice(1, STACK_FRAMES_TO_IGNORE);
1230
+ _this.stack = nextStack.join('\n');
1242
1231
  }
1243
1232
  return _this;
1244
1233
  }
@@ -5387,12 +5376,23 @@ function cleanUrl(path) {
5387
5376
  return path.replace(REDUNDANT_CHARACTERS_EXP, '');
5388
5377
  }
5389
5378
 
5379
+ /**
5380
+ * Determines if the given URL string is an absolute URL.
5381
+ */
5382
+ function isAbsoluteUrl(url) {
5383
+ return /^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(url);
5384
+ }
5385
+
5390
5386
  /**
5391
5387
  * Returns an absolute URL based on the given path.
5392
5388
  */
5393
5389
  function getAbsoluteUrl(path, baseUrl) {
5394
- // Ignore absolute URLs.
5395
- if (!path.startsWith('/')) {
5390
+ // already absolute URL
5391
+ if (isAbsoluteUrl(path)) {
5392
+ return path;
5393
+ }
5394
+ // Ignore path with pattern start with *
5395
+ if (path.startsWith('*')) {
5396
5396
  return path;
5397
5397
  }
5398
5398
  // Resolve a relative request URL against a given custom "baseUrl"
@@ -5440,12 +5440,17 @@ function coercePath(path) {
5440
5440
  ? `${parameterName}${wildcard}`
5441
5441
  : `${parameterName}${expression}`;
5442
5442
  })
5443
+ /**
5444
+ * Escape the port so that "path-to-regexp" can match
5445
+ * absolute URLs including port numbers.
5446
+ */
5447
+ .replace(/([^\/])(:)(?=\d+)/, '$1\\$2')
5443
5448
  /**
5444
5449
  * Escape the protocol so that "path-to-regexp" could match
5445
5450
  * absolute URL.
5446
5451
  * @see https://github.com/pillarjs/path-to-regexp/issues/259
5447
5452
  */
5448
- .replace(/^([^\/]+)(:)(?=\/\/)/g, '$1\\$2'));
5453
+ .replace(/^([^\/]+)(:)(?=\/\/)/, '$1\\$2'));
5449
5454
  }
5450
5455
  /**
5451
5456
  * Returns the result of matching given request URL against a mask.
@@ -5705,7 +5710,6 @@ class RestHandler extends RequestHandler {
5705
5710
  const matchesMethod = this.info.method instanceof RegExp
5706
5711
  ? this.info.method.test(request.method)
5707
5712
  : isStringEqual(this.info.method, request.method);
5708
- // console.log({ request, matchesMethod, parsedResult })
5709
5713
  return matchesMethod && parsedResult.matches;
5710
5714
  }
5711
5715
  log(request, response) {
@@ -5838,10 +5842,10 @@ function groupHandlersByType(handlers) {
5838
5842
  graphql: [],
5839
5843
  });
5840
5844
  }
5841
- function getScoreForRestHandler() {
5845
+ function getRestHandlerScore() {
5842
5846
  return (request, handler) => {
5843
5847
  const { path, method } = handler.info;
5844
- if (path instanceof RegExp) {
5848
+ if (path instanceof RegExp || method instanceof RegExp) {
5845
5849
  return Infinity;
5846
5850
  }
5847
5851
  const hasSameMethod = isStringEqual(request.method, method);
@@ -5852,12 +5856,15 @@ function getScoreForRestHandler() {
5852
5856
  return score - methodScoreDelta;
5853
5857
  };
5854
5858
  }
5855
- function getScoreForGraphQLHandler(parsedQuery) {
5859
+ function getGraphQLHandlerScore(parsedQuery) {
5856
5860
  return (_, handler) => {
5857
5861
  if (typeof parsedQuery.operationName === 'undefined') {
5858
5862
  return Infinity;
5859
5863
  }
5860
5864
  const { operationType, operationName } = handler.info;
5865
+ if (typeof operationName !== 'string') {
5866
+ return Infinity;
5867
+ }
5861
5868
  const hasSameOperationType = parsedQuery.operationType === operationType;
5862
5869
  // Always treat a handler with the same operation type as a more similar one.
5863
5870
  const operationTypeScoreDelta = hasSameOperationType ? TYPE_MATCH_DELTA : 0;
@@ -5867,16 +5874,12 @@ function getScoreForGraphQLHandler(parsedQuery) {
5867
5874
  }
5868
5875
  function getSuggestedHandler(request, handlers, getScore) {
5869
5876
  const suggestedHandlers = handlers
5870
- .reduce((acc, handler) => {
5877
+ .reduce((suggestions, handler) => {
5871
5878
  const score = getScore(request, handler);
5872
- return acc.concat([[score, handler]]);
5879
+ return suggestions.concat([[score, handler]]);
5873
5880
  }, [])
5874
- .sort(([leftScore], [rightScore]) => {
5875
- return leftScore - rightScore;
5876
- })
5877
- .filter(([score]) => {
5878
- return score <= MAX_MATCH_SCORE;
5879
- })
5881
+ .sort(([leftScore], [rightScore]) => leftScore - rightScore)
5882
+ .filter(([score]) => score <= MAX_MATCH_SCORE)
5880
5883
  .slice(0, MAX_SUGGESTION_COUNT)
5881
5884
  .map(([, handler]) => handler);
5882
5885
  return suggestedHandlers;
@@ -5906,8 +5909,8 @@ function onUnhandledRequest(request, handlers, strategy = 'warn') {
5906
5909
  ? handlerGroups.graphql
5907
5910
  : handlerGroups.rest;
5908
5911
  const suggestedHandlers = getSuggestedHandler(request, relevantHandlers, parsedGraphQLQuery
5909
- ? getScoreForGraphQLHandler(parsedGraphQLQuery)
5910
- : getScoreForRestHandler());
5912
+ ? getGraphQLHandlerScore(parsedGraphQLQuery)
5913
+ : getRestHandlerScore());
5911
5914
  const handlerSuggestion = suggestedHandlers.length > 0
5912
5915
  ? getSuggestedHandlersMessage(suggestedHandlers)
5913
5916
  : '';
@@ -5930,7 +5933,7 @@ Read more: https://mswjs.io/docs/getting-started/mocks\
5930
5933
  // Print a developer-friendly error.
5931
5934
  devUtils.error('Error: %s', message);
5932
5935
  // Throw an exception to halt request processing and not perform the original request.
5933
- throw new Error('Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.');
5936
+ throw new Error(devUtils.formatMessage('Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.'));
5934
5937
  }
5935
5938
  case 'warn': {
5936
5939
  devUtils.warn('Warning: %s', message);
@@ -6062,15 +6065,14 @@ function createSetupServer(...interceptors$1) {
6062
6065
  },
6063
6066
  });
6064
6067
  interceptor.on('response', (request, response) => {
6065
- const requestId = request.headers.get('x-msw-request-id');
6066
- if (!requestId) {
6068
+ if (!request.id) {
6067
6069
  return;
6068
6070
  }
6069
6071
  if (response.headers.get('x-powered-by') === 'msw') {
6070
- emitter.emit('response:mocked', response, requestId);
6072
+ emitter.emit('response:mocked', response, request.id);
6071
6073
  }
6072
6074
  else {
6073
- emitter.emit('response:bypass', response, requestId);
6075
+ emitter.emit('response:bypass', response, request.id);
6074
6076
  }
6075
6077
  });
6076
6078
  return {
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.
@@ -2871,11 +2863,12 @@ function parseMultipartData(data, headers) {
2871
2863
  * Parses a given request/response body based on the "Content-Type" header.
2872
2864
  */
2873
2865
  function parseBody(body, headers) {
2866
+ var _a;
2874
2867
  // Return whatever falsey body value is given.
2875
2868
  if (!body) {
2876
2869
  return body;
2877
2870
  }
2878
- const contentType = (headers === null || headers === void 0 ? void 0 : headers.get('content-type')) || '';
2871
+ const contentType = ((_a = headers === null || headers === void 0 ? void 0 : headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || '';
2879
2872
  // If the body has a Multipart Content-Type
2880
2873
  // parse it into an object.
2881
2874
  const hasMultipartContent = contentType.startsWith('multipart/form-data');
@@ -2934,10 +2927,8 @@ function setRequestCookies(request) {
2934
2927
  * Converts a given isomorphic request to a `MockedRequest` instance.
2935
2928
  */
2936
2929
  function parseIsomorphicRequest(request) {
2937
- const requestId = uuidv4();
2938
- request.headers.set('x-msw-request-id', requestId);
2939
2930
  const mockedRequest = {
2940
- id: requestId,
2931
+ id: request.id,
2941
2932
  url: request.url,
2942
2933
  method: request.method,
2943
2934
  body: parseBody(request.body, request.headers),
@@ -3120,11 +3111,9 @@ var InvariantError = /** @class */ (function (_super) {
3120
3111
  _this.name = 'Invariant Violation';
3121
3112
  _this.message = format_1.format.apply(void 0, __spreadArray([message], positionals));
3122
3113
  if (_this.stack) {
3123
- var prevStack = _this.stack;
3124
- _this.stack = prevStack
3125
- .split('\n')
3126
- .slice(STACK_FRAMES_TO_IGNORE)
3127
- .join('\n');
3114
+ var nextStack = _this.stack.split('\n');
3115
+ nextStack.splice(1, STACK_FRAMES_TO_IGNORE);
3116
+ _this.stack = nextStack.join('\n');
3128
3117
  }
3129
3118
  return _this;
3130
3119
  }
@@ -7273,12 +7262,23 @@ function cleanUrl(path) {
7273
7262
  return path.replace(REDUNDANT_CHARACTERS_EXP, '');
7274
7263
  }
7275
7264
 
7265
+ /**
7266
+ * Determines if the given URL string is an absolute URL.
7267
+ */
7268
+ function isAbsoluteUrl(url) {
7269
+ return /^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(url);
7270
+ }
7271
+
7276
7272
  /**
7277
7273
  * Returns an absolute URL based on the given path.
7278
7274
  */
7279
7275
  function getAbsoluteUrl(path, baseUrl) {
7280
- // Ignore absolute URLs.
7281
- if (!path.startsWith('/')) {
7276
+ // already absolute URL
7277
+ if (isAbsoluteUrl(path)) {
7278
+ return path;
7279
+ }
7280
+ // Ignore path with pattern start with *
7281
+ if (path.startsWith('*')) {
7282
7282
  return path;
7283
7283
  }
7284
7284
  // Resolve a relative request URL against a given custom "baseUrl"
@@ -7326,12 +7326,17 @@ function coercePath(path) {
7326
7326
  ? `${parameterName}${wildcard}`
7327
7327
  : `${parameterName}${expression}`;
7328
7328
  })
7329
+ /**
7330
+ * Escape the port so that "path-to-regexp" can match
7331
+ * absolute URLs including port numbers.
7332
+ */
7333
+ .replace(/([^\/])(:)(?=\d+)/, '$1\\$2')
7329
7334
  /**
7330
7335
  * Escape the protocol so that "path-to-regexp" could match
7331
7336
  * absolute URL.
7332
7337
  * @see https://github.com/pillarjs/path-to-regexp/issues/259
7333
7338
  */
7334
- .replace(/^([^\/]+)(:)(?=\/\/)/g, '$1\\$2'));
7339
+ .replace(/^([^\/]+)(:)(?=\/\/)/, '$1\\$2'));
7335
7340
  }
7336
7341
  /**
7337
7342
  * Returns the result of matching given request URL against a mask.
@@ -7591,7 +7596,6 @@ class RestHandler extends RequestHandler {
7591
7596
  const matchesMethod = this.info.method instanceof RegExp
7592
7597
  ? this.info.method.test(request.method)
7593
7598
  : isStringEqual(this.info.method, request.method);
7594
- // console.log({ request, matchesMethod, parsedResult })
7595
7599
  return matchesMethod && parsedResult.matches;
7596
7600
  }
7597
7601
  log(request, response) {
@@ -7724,10 +7728,10 @@ function groupHandlersByType(handlers) {
7724
7728
  graphql: [],
7725
7729
  });
7726
7730
  }
7727
- function getScoreForRestHandler() {
7731
+ function getRestHandlerScore() {
7728
7732
  return (request, handler) => {
7729
7733
  const { path, method } = handler.info;
7730
- if (path instanceof RegExp) {
7734
+ if (path instanceof RegExp || method instanceof RegExp) {
7731
7735
  return Infinity;
7732
7736
  }
7733
7737
  const hasSameMethod = isStringEqual(request.method, method);
@@ -7738,12 +7742,15 @@ function getScoreForRestHandler() {
7738
7742
  return score - methodScoreDelta;
7739
7743
  };
7740
7744
  }
7741
- function getScoreForGraphQLHandler(parsedQuery) {
7745
+ function getGraphQLHandlerScore(parsedQuery) {
7742
7746
  return (_, handler) => {
7743
7747
  if (typeof parsedQuery.operationName === 'undefined') {
7744
7748
  return Infinity;
7745
7749
  }
7746
7750
  const { operationType, operationName } = handler.info;
7751
+ if (typeof operationName !== 'string') {
7752
+ return Infinity;
7753
+ }
7747
7754
  const hasSameOperationType = parsedQuery.operationType === operationType;
7748
7755
  // Always treat a handler with the same operation type as a more similar one.
7749
7756
  const operationTypeScoreDelta = hasSameOperationType ? TYPE_MATCH_DELTA : 0;
@@ -7753,16 +7760,12 @@ function getScoreForGraphQLHandler(parsedQuery) {
7753
7760
  }
7754
7761
  function getSuggestedHandler(request, handlers, getScore) {
7755
7762
  const suggestedHandlers = handlers
7756
- .reduce((acc, handler) => {
7763
+ .reduce((suggestions, handler) => {
7757
7764
  const score = getScore(request, handler);
7758
- return acc.concat([[score, handler]]);
7765
+ return suggestions.concat([[score, handler]]);
7759
7766
  }, [])
7760
- .sort(([leftScore], [rightScore]) => {
7761
- return leftScore - rightScore;
7762
- })
7763
- .filter(([score]) => {
7764
- return score <= MAX_MATCH_SCORE;
7765
- })
7767
+ .sort(([leftScore], [rightScore]) => leftScore - rightScore)
7768
+ .filter(([score]) => score <= MAX_MATCH_SCORE)
7766
7769
  .slice(0, MAX_SUGGESTION_COUNT)
7767
7770
  .map(([, handler]) => handler);
7768
7771
  return suggestedHandlers;
@@ -7792,8 +7795,8 @@ function onUnhandledRequest(request, handlers, strategy = 'warn') {
7792
7795
  ? handlerGroups.graphql
7793
7796
  : handlerGroups.rest;
7794
7797
  const suggestedHandlers = getSuggestedHandler(request, relevantHandlers, parsedGraphQLQuery
7795
- ? getScoreForGraphQLHandler(parsedGraphQLQuery)
7796
- : getScoreForRestHandler());
7798
+ ? getGraphQLHandlerScore(parsedGraphQLQuery)
7799
+ : getRestHandlerScore());
7797
7800
  const handlerSuggestion = suggestedHandlers.length > 0
7798
7801
  ? getSuggestedHandlersMessage(suggestedHandlers)
7799
7802
  : '';
@@ -7816,7 +7819,7 @@ Read more: https://mswjs.io/docs/getting-started/mocks\
7816
7819
  // Print a developer-friendly error.
7817
7820
  devUtils.error('Error: %s', message);
7818
7821
  // 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.');
7822
+ throw new Error(devUtils.formatMessage('Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.'));
7820
7823
  }
7821
7824
  case 'warn': {
7822
7825
  devUtils.warn('Warning: %s', message);
@@ -7948,15 +7951,14 @@ function createSetupServer(...interceptors$1) {
7948
7951
  },
7949
7952
  });
7950
7953
  interceptor.on('response', (request, response) => {
7951
- const requestId = request.headers.get('x-msw-request-id');
7952
- if (!requestId) {
7954
+ if (!request.id) {
7953
7955
  return;
7954
7956
  }
7955
7957
  if (response.headers.get('x-powered-by') === 'msw') {
7956
- emitter.emit('response:mocked', response, requestId);
7958
+ emitter.emit('response:mocked', response, request.id);
7957
7959
  }
7958
7960
  else {
7959
- emitter.emit('response:bypass', response, requestId);
7961
+ emitter.emit('response:bypass', response, request.id);
7960
7962
  }
7961
7963
  });
7962
7964
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "msw",
3
- "version": "0.36.1",
3
+ "version": "0.36.5",
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",
@@ -18,9 +18,8 @@
18
18
  "test:integration": "jest --config=test/jest.config.js --maxWorkers=3",
19
19
  "test:smoke": "config/scripts/smoke.sh",
20
20
  "test:ts": "tsc -p test/typings/tsconfig.json",
21
- "test:focused": "node node_modules/ts-node/dist/bin.js --project=test/tsconfig.json test/focusedTest.ts",
21
+ "prepare": "yarn simple-git-hooks init",
22
22
  "prepublishOnly": "yarn lint && yarn test:unit && yarn build && yarn test:integration",
23
- "prepare": "husky install",
24
23
  "postinstall": "node -e \"try{require('./config/scripts/postinstall')}catch(e){}\""
25
24
  },
26
25
  "lint-staged": {
@@ -88,6 +87,8 @@
88
87
  "devDependencies": {
89
88
  "@babel/core": "^7.16.0",
90
89
  "@babel/preset-env": "^7.16.4",
90
+ "@commitlint/cli": "^16.0.2",
91
+ "@commitlint/config-conventional": "^16.0.0",
91
92
  "@open-draft/test-server": "^0.2.3",
92
93
  "@rollup/plugin-commonjs": "^19.0.0",
93
94
  "@rollup/plugin-inject": "^4.0.3",
@@ -104,17 +105,19 @@
104
105
  "@typescript-eslint/parser": "^4.28.3",
105
106
  "babel-loader": "^8.2.3",
106
107
  "babel-minify": "^0.5.1",
108
+ "commitizen": "^4.2.4",
107
109
  "cross-env": "^7.0.3",
108
110
  "cross-fetch": "^3.1.4",
111
+ "cz-conventional-changelog": "3.3.0",
109
112
  "eslint": "^7.30.0",
110
113
  "eslint-config-prettier": "^8.3.0",
111
114
  "eslint-plugin-prettier": "^3.4.0",
112
115
  "fs-extra": "^10.0.0",
113
116
  "fs-teardown": "^0.3.0",
114
- "husky": "^5.1.1",
115
117
  "jest": "26",
116
118
  "json-bigint": "^1.0.0",
117
119
  "lint-staged": "^11.0.1",
120
+ "outvariant": "^1.2.1",
118
121
  "page-with": "^0.5.0",
119
122
  "prettier": "^2.3.2",
120
123
  "regenerator-runtime": "^0.13.9",
@@ -122,6 +125,7 @@
122
125
  "rollup": "^2.60.2",
123
126
  "rollup-plugin-terser": "^7.0.2",
124
127
  "rollup-plugin-typescript2": "^0.30.0",
128
+ "simple-git-hooks": "^2.7.0",
125
129
  "ts-jest": "26",
126
130
  "ts-loader": "^9.2.6",
127
131
  "ts-node": "^10.1.0",
@@ -132,5 +136,10 @@
132
136
  },
133
137
  "resolutions": {
134
138
  "chokidar": "3.4.1"
139
+ },
140
+ "config": {
141
+ "commitizen": {
142
+ "path": "./node_modules/cz-conventional-changelog"
143
+ }
135
144
  }
136
145
  }