msw 0.33.0 → 0.34.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.
@@ -1511,6 +1511,65 @@ var lib = {};
1511
1511
 
1512
1512
  var invariant$2 = {};
1513
1513
 
1514
+ var format$1 = {};
1515
+
1516
+ Object.defineProperty(format$1, "__esModule", { value: true });
1517
+ format$1.format = void 0;
1518
+ var POSITIONALS_EXP = /(%?)(%([sdjo]))/g;
1519
+ function serializePositional(positional, flag) {
1520
+ switch (flag) {
1521
+ // Strings.
1522
+ case 's':
1523
+ return positional;
1524
+ // Digits.
1525
+ case 'd':
1526
+ case 'i':
1527
+ return Number(positional);
1528
+ // JSON.
1529
+ case 'j':
1530
+ return JSON.stringify(positional);
1531
+ // Objects.
1532
+ case 'o': {
1533
+ // Preserve stings to prevent extra quotes around them.
1534
+ if (typeof positional === 'string') {
1535
+ return positional;
1536
+ }
1537
+ var json = JSON.stringify(positional);
1538
+ // If the positional isn't serializable, return it as-is.
1539
+ if (json === '{}' || json === '[]' || /^\[object .+?\]$/.test(json)) {
1540
+ return positional;
1541
+ }
1542
+ return json;
1543
+ }
1544
+ }
1545
+ }
1546
+ function format(message) {
1547
+ var positionals = [];
1548
+ for (var _i = 1; _i < arguments.length; _i++) {
1549
+ positionals[_i - 1] = arguments[_i];
1550
+ }
1551
+ if (positionals.length === 0) {
1552
+ return message;
1553
+ }
1554
+ var positionalIndex = 0;
1555
+ var formattedMessage = message.replace(POSITIONALS_EXP, function (match, isEscaped, _, flag) {
1556
+ var positional = positionals[positionalIndex];
1557
+ var value = serializePositional(positional, flag);
1558
+ if (!isEscaped) {
1559
+ positionalIndex++;
1560
+ return value;
1561
+ }
1562
+ return match;
1563
+ });
1564
+ // Append unresolved positionals to string as-is.
1565
+ if (positionalIndex < positionals.length) {
1566
+ formattedMessage += " " + positionals.slice(positionalIndex).join(' ');
1567
+ }
1568
+ formattedMessage = formattedMessage.replace(/%{2,2}/g, '%');
1569
+ return formattedMessage;
1570
+ }
1571
+ format$1.format = format;
1572
+
1514
1573
  var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () {
1515
1574
  var extendStatics = function (d, b) {
1516
1575
  extendStatics = Object.setPrototypeOf ||
@@ -1532,21 +1591,9 @@ var __spreadArray = (commonjsGlobal && commonjsGlobal.__spreadArray) || function
1532
1591
  return to;
1533
1592
  };
1534
1593
  Object.defineProperty(invariant$2, "__esModule", { value: true });
1535
- invariant$2.invariant = invariant$2.InvariantError = invariant$2.interpolate = void 0;
1594
+ invariant$2.invariant = invariant$2.InvariantError = void 0;
1595
+ var format_1 = format$1;
1536
1596
  var STACK_FRAMES_TO_IGNORE = 2;
1537
- function interpolate(message) {
1538
- var positionals = [];
1539
- for (var _i = 1; _i < arguments.length; _i++) {
1540
- positionals[_i - 1] = arguments[_i];
1541
- }
1542
- var index = 0;
1543
- return message.replace(/%[s|d|o]/g, function (match) {
1544
- var _a;
1545
- var value = (_a = positionals[index++]) !== null && _a !== void 0 ? _a : match;
1546
- return typeof value === 'object' ? JSON.stringify(value) : value;
1547
- });
1548
- }
1549
- invariant$2.interpolate = interpolate;
1550
1597
  var InvariantError = /** @class */ (function (_super) {
1551
1598
  __extends(InvariantError, _super);
1552
1599
  function InvariantError(message) {
@@ -1556,7 +1603,7 @@ var InvariantError = /** @class */ (function (_super) {
1556
1603
  }
1557
1604
  var _this = _super.call(this, message) || this;
1558
1605
  _this.name = 'Invariant Violation';
1559
- _this.message = interpolate.apply(void 0, __spreadArray([message], positionals));
1606
+ _this.message = format_1.format.apply(void 0, __spreadArray([message], positionals));
1560
1607
  if (_this.stack) {
1561
1608
  var prevStack = _this.stack;
1562
1609
  _this.stack = prevStack
@@ -1593,6 +1640,7 @@ var __exportStar = (commonjsGlobal && commonjsGlobal.__exportStar) || function(m
1593
1640
  };
1594
1641
  Object.defineProperty(exports, "__esModule", { value: true });
1595
1642
  __exportStar(invariant$2, exports);
1643
+ __exportStar(format$1, exports);
1596
1644
  }(lib));
1597
1645
 
1598
1646
  const LIBRARY_PREFIX = '[MSW]';
@@ -1600,7 +1648,7 @@ const LIBRARY_PREFIX = '[MSW]';
1600
1648
  * Formats a given message by appending the library's prefix string.
1601
1649
  */
1602
1650
  function formatMessage(message, ...positionals) {
1603
- const interpolatedMessage = lib.interpolate(message, ...positionals);
1651
+ const interpolatedMessage = lib.format(message, ...positionals);
1604
1652
  return `${LIBRARY_PREFIX} ${interpolatedMessage}`;
1605
1653
  }
1606
1654
  /**
@@ -2180,6 +2228,7 @@ function invariant(condition, message) {
2180
2228
 
2181
2229
  // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
2182
2230
  var nodejsCustomInspectSymbol = typeof Symbol === 'function' && typeof Symbol.for === 'function' ? Symbol.for('nodejs.util.inspect.custom') : undefined;
2231
+ var nodejsCustomInspectSymbol$1 = nodejsCustomInspectSymbol;
2183
2232
 
2184
2233
  /**
2185
2234
  * The `defineInspect()` function defines `inspect()` prototype method as alias of `toJSON`
@@ -2190,8 +2239,8 @@ function defineInspect(classObject) {
2190
2239
  typeof fn === 'function' || invariant(0);
2191
2240
  classObject.prototype.inspect = fn; // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2317')
2192
2241
 
2193
- if (nodejsCustomInspectSymbol) {
2194
- classObject.prototype[nodejsCustomInspectSymbol] = fn;
2242
+ if (nodejsCustomInspectSymbol$1) {
2243
+ classObject.prototype[nodejsCustomInspectSymbol$1] = fn;
2195
2244
  }
2196
2245
  }
2197
2246
 
@@ -2434,7 +2483,7 @@ function formatArray(array, seenValues) {
2434
2483
  }
2435
2484
 
2436
2485
  function getCustomFn(object) {
2437
- var customInspectFn = object[String(nodejsCustomInspectSymbol)];
2486
+ var customInspectFn = object[String(nodejsCustomInspectSymbol$1)];
2438
2487
 
2439
2488
  if (typeof customInspectFn === 'function') {
2440
2489
  return customInspectFn;
@@ -4861,17 +4910,20 @@ const getPublicUrlFromRequest = (request) => {
4861
4910
  : new URL(request.url.pathname, `${request.url.protocol}//${request.url.host}`).href;
4862
4911
  };
4863
4912
 
4864
- function parseQuery(query) {
4913
+ function parseDocumentNode(node) {
4865
4914
  var _a;
4915
+ const operationDef = node.definitions.find((def) => {
4916
+ return def.kind === 'OperationDefinition';
4917
+ });
4918
+ return {
4919
+ operationType: operationDef === null || operationDef === void 0 ? void 0 : operationDef.operation,
4920
+ operationName: (_a = operationDef === null || operationDef === void 0 ? void 0 : operationDef.name) === null || _a === void 0 ? void 0 : _a.value,
4921
+ };
4922
+ }
4923
+ function parseQuery(query) {
4866
4924
  try {
4867
4925
  const ast = parse(query);
4868
- const operationDef = ast.definitions.find((def) => {
4869
- return def.kind === 'OperationDefinition';
4870
- });
4871
- return {
4872
- operationType: operationDef === null || operationDef === void 0 ? void 0 : operationDef.operation,
4873
- operationName: (_a = operationDef === null || operationDef === void 0 ? void 0 : operationDef.name) === null || _a === void 0 ? void 0 : _a.value,
4874
- };
4926
+ return parseDocumentNode(ast);
4875
4927
  }
4876
4928
  catch (error) {
4877
4929
  return error;
@@ -4951,7 +5003,7 @@ function parseGraphQLRequest(request) {
4951
5003
  const parsedResult = parseQuery(query);
4952
5004
  if (parsedResult instanceof Error) {
4953
5005
  const requestPublicUrl = getPublicUrlFromRequest(request);
4954
- throw new Error(devUtils.formatMessage('Failed to intercept a GraphQL request to "%s %s": cannot parse query. See the error message from the parser below.\n\n%o', request.method, requestPublicUrl, parsedResult.message));
5006
+ throw new Error(devUtils.formatMessage('Failed to intercept a GraphQL request to "%s %s": cannot parse query. See the error message from the parser below.\n\n%s', request.method, requestPublicUrl, parsedResult.message));
4955
5007
  }
4956
5008
  return {
4957
5009
  operationType: parsedResult.operationType,
@@ -5295,19 +5347,28 @@ const xml = (body) => {
5295
5347
  };
5296
5348
  };
5297
5349
 
5350
+ var StatusCodeColor;
5351
+ (function (StatusCodeColor) {
5352
+ StatusCodeColor["Success"] = "#69AB32";
5353
+ StatusCodeColor["Warning"] = "#F0BB4B";
5354
+ StatusCodeColor["Danger"] = "#E95F5D";
5355
+ })(StatusCodeColor || (StatusCodeColor = {}));
5298
5356
  /**
5299
5357
  * Returns a HEX color for a given response status code number.
5300
5358
  */
5301
5359
  function getStatusCodeColor(status) {
5302
5360
  if (status < 300) {
5303
- return '#69AB32';
5361
+ return StatusCodeColor.Success;
5304
5362
  }
5305
5363
  if (status < 400) {
5306
- return '#F0BB4B';
5364
+ return StatusCodeColor.Warning;
5307
5365
  }
5308
- return '#E95F5D';
5366
+ return StatusCodeColor.Danger;
5309
5367
  }
5310
5368
 
5369
+ /**
5370
+ * Returns a timestamp string in a "HH:MM:SS" format.
5371
+ */
5311
5372
  function getTimestamp() {
5312
5373
  const now = new Date();
5313
5374
  return [now.getHours(), now.getMinutes(), now.getSeconds()]
@@ -5325,7 +5386,7 @@ function prepareRequest(request) {
5325
5386
  }
5326
5387
 
5327
5388
  /**
5328
- * Formats a mocked response for introspection in browser's console.
5389
+ * Formats a mocked response for introspection in the browser's console.
5329
5390
  */
5330
5391
  function prepareResponse(res) {
5331
5392
  const responseHeaders = lib$2.objectToHeaders(res.headers);
@@ -5688,7 +5749,8 @@ ${queryParams
5688
5749
  const publicUrl = getPublicUrlFromRequest(request);
5689
5750
  const loggedRequest = prepareRequest(request);
5690
5751
  const loggedResponse = prepareResponse(response);
5691
- console.groupCollapsed(devUtils.formatMessage('%s %s %s (%c%s%c)'), getTimestamp(), request.method, publicUrl, `color:${getStatusCodeColor(response.status)}`, response.status, 'color:inherit');
5752
+ const statusColor = getStatusCodeColor(response.status);
5753
+ console.groupCollapsed(devUtils.formatMessage('%s %s %s (%c%s%c)'), getTimestamp(), request.method, publicUrl, `color:${statusColor}`, `${response.status} ${response.statusText}`, 'color:inherit');
5692
5754
  console.log('Request', loggedRequest);
5693
5755
  console.log('Handler:', {
5694
5756
  mask: this.info.path,
@@ -5718,16 +5780,33 @@ const graphqlContext = {
5718
5780
  errors,
5719
5781
  cookie,
5720
5782
  };
5783
+ function isDocumentNode(value) {
5784
+ if (value == null) {
5785
+ return false;
5786
+ }
5787
+ return typeof value === 'object' && 'kind' in value && 'definitions' in value;
5788
+ }
5721
5789
  class GraphQLHandler extends RequestHandler {
5722
5790
  constructor(operationType, operationName, endpoint, resolver) {
5791
+ let resolvedOperationName = operationName;
5792
+ if (isDocumentNode(operationName)) {
5793
+ const parsedNode = parseDocumentNode(operationName);
5794
+ if (parsedNode.operationType !== operationType) {
5795
+ throw new Error(`Failed to create a GraphQL handler: provided a DocumentNode with a mismatched operation type (expected "${operationType}", but got "${parsedNode.operationType}").`);
5796
+ }
5797
+ if (!parsedNode.operationName) {
5798
+ throw new Error(`Failed to create a GraphQL handler: provided a DocumentNode with no operation name.`);
5799
+ }
5800
+ resolvedOperationName = parsedNode.operationName;
5801
+ }
5723
5802
  const header = operationType === 'all'
5724
5803
  ? `${operationType} (origin: ${endpoint.toString()})`
5725
- : `${operationType} ${operationName} (origin: ${endpoint.toString()})`;
5804
+ : `${operationType} ${resolvedOperationName} (origin: ${endpoint.toString()})`;
5726
5805
  super({
5727
5806
  info: {
5728
5807
  header,
5729
5808
  operationType,
5730
- operationName,
5809
+ operationName: resolvedOperationName,
5731
5810
  },
5732
5811
  ctx: graphqlContext,
5733
5812
  resolver,
@@ -5763,10 +5842,11 @@ Consider naming this operation or using "graphql.operation" request handler to i
5763
5842
  hasMatchingOperationType &&
5764
5843
  hasMatchingOperationName);
5765
5844
  }
5766
- log(request, response) {
5845
+ log(request, response, handler, parsedRequest) {
5767
5846
  const loggedRequest = prepareRequest(request);
5768
5847
  const loggedResponse = prepareResponse(response);
5769
- console.groupCollapsed(devUtils.formatMessage('%s %s (%c%s%c)'), getTimestamp(), this.info.operationName, `color:${getStatusCodeColor(response.status)}`, response.status, 'color:inherit');
5848
+ const statusColor = getStatusCodeColor(response.status);
5849
+ console.groupCollapsed(devUtils.formatMessage('%s %s (%c%s%c)'), getTimestamp(), `${parsedRequest === null || parsedRequest === void 0 ? void 0 : parsedRequest.operationType} ${parsedRequest === null || parsedRequest === void 0 ? void 0 : parsedRequest.operationName}`, `color:${statusColor}`, `${response.status} ${response.statusText}`, 'color:inherit');
5770
5850
  console.log('Request:', loggedRequest);
5771
5851
  console.log('Handler:', this);
5772
5852
  console.log('Response:', loggedResponse);
package/node/lib/index.js CHANGED
@@ -3397,6 +3397,65 @@ var lib = {};
3397
3397
 
3398
3398
  var invariant$2 = {};
3399
3399
 
3400
+ var format$1 = {};
3401
+
3402
+ Object.defineProperty(format$1, "__esModule", { value: true });
3403
+ format$1.format = void 0;
3404
+ var POSITIONALS_EXP = /(%?)(%([sdjo]))/g;
3405
+ function serializePositional(positional, flag) {
3406
+ switch (flag) {
3407
+ // Strings.
3408
+ case 's':
3409
+ return positional;
3410
+ // Digits.
3411
+ case 'd':
3412
+ case 'i':
3413
+ return Number(positional);
3414
+ // JSON.
3415
+ case 'j':
3416
+ return JSON.stringify(positional);
3417
+ // Objects.
3418
+ case 'o': {
3419
+ // Preserve stings to prevent extra quotes around them.
3420
+ if (typeof positional === 'string') {
3421
+ return positional;
3422
+ }
3423
+ var json = JSON.stringify(positional);
3424
+ // If the positional isn't serializable, return it as-is.
3425
+ if (json === '{}' || json === '[]' || /^\[object .+?\]$/.test(json)) {
3426
+ return positional;
3427
+ }
3428
+ return json;
3429
+ }
3430
+ }
3431
+ }
3432
+ function format(message) {
3433
+ var positionals = [];
3434
+ for (var _i = 1; _i < arguments.length; _i++) {
3435
+ positionals[_i - 1] = arguments[_i];
3436
+ }
3437
+ if (positionals.length === 0) {
3438
+ return message;
3439
+ }
3440
+ var positionalIndex = 0;
3441
+ var formattedMessage = message.replace(POSITIONALS_EXP, function (match, isEscaped, _, flag) {
3442
+ var positional = positionals[positionalIndex];
3443
+ var value = serializePositional(positional, flag);
3444
+ if (!isEscaped) {
3445
+ positionalIndex++;
3446
+ return value;
3447
+ }
3448
+ return match;
3449
+ });
3450
+ // Append unresolved positionals to string as-is.
3451
+ if (positionalIndex < positionals.length) {
3452
+ formattedMessage += " " + positionals.slice(positionalIndex).join(' ');
3453
+ }
3454
+ formattedMessage = formattedMessage.replace(/%{2,2}/g, '%');
3455
+ return formattedMessage;
3456
+ }
3457
+ format$1.format = format;
3458
+
3400
3459
  var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () {
3401
3460
  var extendStatics = function (d, b) {
3402
3461
  extendStatics = Object.setPrototypeOf ||
@@ -3418,21 +3477,9 @@ var __spreadArray = (commonjsGlobal && commonjsGlobal.__spreadArray) || function
3418
3477
  return to;
3419
3478
  };
3420
3479
  Object.defineProperty(invariant$2, "__esModule", { value: true });
3421
- invariant$2.invariant = invariant$2.InvariantError = invariant$2.interpolate = void 0;
3480
+ invariant$2.invariant = invariant$2.InvariantError = void 0;
3481
+ var format_1 = format$1;
3422
3482
  var STACK_FRAMES_TO_IGNORE = 2;
3423
- function interpolate(message) {
3424
- var positionals = [];
3425
- for (var _i = 1; _i < arguments.length; _i++) {
3426
- positionals[_i - 1] = arguments[_i];
3427
- }
3428
- var index = 0;
3429
- return message.replace(/%[s|d|o]/g, function (match) {
3430
- var _a;
3431
- var value = (_a = positionals[index++]) !== null && _a !== void 0 ? _a : match;
3432
- return typeof value === 'object' ? JSON.stringify(value) : value;
3433
- });
3434
- }
3435
- invariant$2.interpolate = interpolate;
3436
3483
  var InvariantError = /** @class */ (function (_super) {
3437
3484
  __extends(InvariantError, _super);
3438
3485
  function InvariantError(message) {
@@ -3442,7 +3489,7 @@ var InvariantError = /** @class */ (function (_super) {
3442
3489
  }
3443
3490
  var _this = _super.call(this, message) || this;
3444
3491
  _this.name = 'Invariant Violation';
3445
- _this.message = interpolate.apply(void 0, __spreadArray([message], positionals));
3492
+ _this.message = format_1.format.apply(void 0, __spreadArray([message], positionals));
3446
3493
  if (_this.stack) {
3447
3494
  var prevStack = _this.stack;
3448
3495
  _this.stack = prevStack
@@ -3479,6 +3526,7 @@ var __exportStar = (commonjsGlobal && commonjsGlobal.__exportStar) || function(m
3479
3526
  };
3480
3527
  Object.defineProperty(exports, "__esModule", { value: true });
3481
3528
  __exportStar(invariant$2, exports);
3529
+ __exportStar(format$1, exports);
3482
3530
  }(lib));
3483
3531
 
3484
3532
  const LIBRARY_PREFIX = '[MSW]';
@@ -3486,7 +3534,7 @@ const LIBRARY_PREFIX = '[MSW]';
3486
3534
  * Formats a given message by appending the library's prefix string.
3487
3535
  */
3488
3536
  function formatMessage(message, ...positionals) {
3489
- const interpolatedMessage = lib.interpolate(message, ...positionals);
3537
+ const interpolatedMessage = lib.format(message, ...positionals);
3490
3538
  return `${LIBRARY_PREFIX} ${interpolatedMessage}`;
3491
3539
  }
3492
3540
  /**
@@ -4066,6 +4114,7 @@ function invariant(condition, message) {
4066
4114
 
4067
4115
  // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2317')
4068
4116
  var nodejsCustomInspectSymbol = typeof Symbol === 'function' && typeof Symbol.for === 'function' ? Symbol.for('nodejs.util.inspect.custom') : undefined;
4117
+ var nodejsCustomInspectSymbol$1 = nodejsCustomInspectSymbol;
4069
4118
 
4070
4119
  /**
4071
4120
  * The `defineInspect()` function defines `inspect()` prototype method as alias of `toJSON`
@@ -4076,8 +4125,8 @@ function defineInspect(classObject) {
4076
4125
  typeof fn === 'function' || invariant(0);
4077
4126
  classObject.prototype.inspect = fn; // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2317')
4078
4127
 
4079
- if (nodejsCustomInspectSymbol) {
4080
- classObject.prototype[nodejsCustomInspectSymbol] = fn;
4128
+ if (nodejsCustomInspectSymbol$1) {
4129
+ classObject.prototype[nodejsCustomInspectSymbol$1] = fn;
4081
4130
  }
4082
4131
  }
4083
4132
 
@@ -4320,7 +4369,7 @@ function formatArray(array, seenValues) {
4320
4369
  }
4321
4370
 
4322
4371
  function getCustomFn(object) {
4323
- var customInspectFn = object[String(nodejsCustomInspectSymbol)];
4372
+ var customInspectFn = object[String(nodejsCustomInspectSymbol$1)];
4324
4373
 
4325
4374
  if (typeof customInspectFn === 'function') {
4326
4375
  return customInspectFn;
@@ -6747,17 +6796,20 @@ const getPublicUrlFromRequest = (request) => {
6747
6796
  : new URL(request.url.pathname, `${request.url.protocol}//${request.url.host}`).href;
6748
6797
  };
6749
6798
 
6750
- function parseQuery(query) {
6799
+ function parseDocumentNode(node) {
6751
6800
  var _a;
6801
+ const operationDef = node.definitions.find((def) => {
6802
+ return def.kind === 'OperationDefinition';
6803
+ });
6804
+ return {
6805
+ operationType: operationDef === null || operationDef === void 0 ? void 0 : operationDef.operation,
6806
+ operationName: (_a = operationDef === null || operationDef === void 0 ? void 0 : operationDef.name) === null || _a === void 0 ? void 0 : _a.value,
6807
+ };
6808
+ }
6809
+ function parseQuery(query) {
6752
6810
  try {
6753
6811
  const ast = parse(query);
6754
- const operationDef = ast.definitions.find((def) => {
6755
- return def.kind === 'OperationDefinition';
6756
- });
6757
- return {
6758
- operationType: operationDef === null || operationDef === void 0 ? void 0 : operationDef.operation,
6759
- operationName: (_a = operationDef === null || operationDef === void 0 ? void 0 : operationDef.name) === null || _a === void 0 ? void 0 : _a.value,
6760
- };
6812
+ return parseDocumentNode(ast);
6761
6813
  }
6762
6814
  catch (error) {
6763
6815
  return error;
@@ -6837,7 +6889,7 @@ function parseGraphQLRequest(request) {
6837
6889
  const parsedResult = parseQuery(query);
6838
6890
  if (parsedResult instanceof Error) {
6839
6891
  const requestPublicUrl = getPublicUrlFromRequest(request);
6840
- throw new Error(devUtils.formatMessage('Failed to intercept a GraphQL request to "%s %s": cannot parse query. See the error message from the parser below.\n\n%o', request.method, requestPublicUrl, parsedResult.message));
6892
+ throw new Error(devUtils.formatMessage('Failed to intercept a GraphQL request to "%s %s": cannot parse query. See the error message from the parser below.\n\n%s', request.method, requestPublicUrl, parsedResult.message));
6841
6893
  }
6842
6894
  return {
6843
6895
  operationType: parsedResult.operationType,
@@ -7181,19 +7233,28 @@ const xml = (body) => {
7181
7233
  };
7182
7234
  };
7183
7235
 
7236
+ var StatusCodeColor;
7237
+ (function (StatusCodeColor) {
7238
+ StatusCodeColor["Success"] = "#69AB32";
7239
+ StatusCodeColor["Warning"] = "#F0BB4B";
7240
+ StatusCodeColor["Danger"] = "#E95F5D";
7241
+ })(StatusCodeColor || (StatusCodeColor = {}));
7184
7242
  /**
7185
7243
  * Returns a HEX color for a given response status code number.
7186
7244
  */
7187
7245
  function getStatusCodeColor(status) {
7188
7246
  if (status < 300) {
7189
- return '#69AB32';
7247
+ return StatusCodeColor.Success;
7190
7248
  }
7191
7249
  if (status < 400) {
7192
- return '#F0BB4B';
7250
+ return StatusCodeColor.Warning;
7193
7251
  }
7194
- return '#E95F5D';
7252
+ return StatusCodeColor.Danger;
7195
7253
  }
7196
7254
 
7255
+ /**
7256
+ * Returns a timestamp string in a "HH:MM:SS" format.
7257
+ */
7197
7258
  function getTimestamp() {
7198
7259
  const now = new Date();
7199
7260
  return [now.getHours(), now.getMinutes(), now.getSeconds()]
@@ -7211,7 +7272,7 @@ function prepareRequest(request) {
7211
7272
  }
7212
7273
 
7213
7274
  /**
7214
- * Formats a mocked response for introspection in browser's console.
7275
+ * Formats a mocked response for introspection in the browser's console.
7215
7276
  */
7216
7277
  function prepareResponse(res) {
7217
7278
  const responseHeaders = lib$2.objectToHeaders(res.headers);
@@ -7574,7 +7635,8 @@ ${queryParams
7574
7635
  const publicUrl = getPublicUrlFromRequest(request);
7575
7636
  const loggedRequest = prepareRequest(request);
7576
7637
  const loggedResponse = prepareResponse(response);
7577
- console.groupCollapsed(devUtils.formatMessage('%s %s %s (%c%s%c)'), getTimestamp(), request.method, publicUrl, `color:${getStatusCodeColor(response.status)}`, response.status, 'color:inherit');
7638
+ const statusColor = getStatusCodeColor(response.status);
7639
+ console.groupCollapsed(devUtils.formatMessage('%s %s %s (%c%s%c)'), getTimestamp(), request.method, publicUrl, `color:${statusColor}`, `${response.status} ${response.statusText}`, 'color:inherit');
7578
7640
  console.log('Request', loggedRequest);
7579
7641
  console.log('Handler:', {
7580
7642
  mask: this.info.path,
@@ -7604,16 +7666,33 @@ const graphqlContext = {
7604
7666
  errors,
7605
7667
  cookie,
7606
7668
  };
7669
+ function isDocumentNode(value) {
7670
+ if (value == null) {
7671
+ return false;
7672
+ }
7673
+ return typeof value === 'object' && 'kind' in value && 'definitions' in value;
7674
+ }
7607
7675
  class GraphQLHandler extends RequestHandler {
7608
7676
  constructor(operationType, operationName, endpoint, resolver) {
7677
+ let resolvedOperationName = operationName;
7678
+ if (isDocumentNode(operationName)) {
7679
+ const parsedNode = parseDocumentNode(operationName);
7680
+ if (parsedNode.operationType !== operationType) {
7681
+ throw new Error(`Failed to create a GraphQL handler: provided a DocumentNode with a mismatched operation type (expected "${operationType}", but got "${parsedNode.operationType}").`);
7682
+ }
7683
+ if (!parsedNode.operationName) {
7684
+ throw new Error(`Failed to create a GraphQL handler: provided a DocumentNode with no operation name.`);
7685
+ }
7686
+ resolvedOperationName = parsedNode.operationName;
7687
+ }
7609
7688
  const header = operationType === 'all'
7610
7689
  ? `${operationType} (origin: ${endpoint.toString()})`
7611
- : `${operationType} ${operationName} (origin: ${endpoint.toString()})`;
7690
+ : `${operationType} ${resolvedOperationName} (origin: ${endpoint.toString()})`;
7612
7691
  super({
7613
7692
  info: {
7614
7693
  header,
7615
7694
  operationType,
7616
- operationName,
7695
+ operationName: resolvedOperationName,
7617
7696
  },
7618
7697
  ctx: graphqlContext,
7619
7698
  resolver,
@@ -7649,10 +7728,11 @@ Consider naming this operation or using "graphql.operation" request handler to i
7649
7728
  hasMatchingOperationType &&
7650
7729
  hasMatchingOperationName);
7651
7730
  }
7652
- log(request, response) {
7731
+ log(request, response, handler, parsedRequest) {
7653
7732
  const loggedRequest = prepareRequest(request);
7654
7733
  const loggedResponse = prepareResponse(response);
7655
- console.groupCollapsed(devUtils.formatMessage('%s %s (%c%s%c)'), getTimestamp(), this.info.operationName, `color:${getStatusCodeColor(response.status)}`, response.status, 'color:inherit');
7734
+ const statusColor = getStatusCodeColor(response.status);
7735
+ console.groupCollapsed(devUtils.formatMessage('%s %s (%c%s%c)'), getTimestamp(), `${parsedRequest === null || parsedRequest === void 0 ? void 0 : parsedRequest.operationType} ${parsedRequest === null || parsedRequest === void 0 ? void 0 : parsedRequest.operationName}`, `color:${statusColor}`, `${response.status} ${response.statusText}`, 'color:inherit');
7656
7736
  console.log('Request:', loggedRequest);
7657
7737
  console.log('Handler:', this);
7658
7738
  console.log('Response:', loggedResponse);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "msw",
3
- "version": "0.33.0",
3
+ "version": "0.34.0",
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",
@@ -38,6 +38,10 @@
38
38
  "url": "https://github.com/kettanaito"
39
39
  },
40
40
  "license": "MIT",
41
+ "funding": {
42
+ "type": "opencollective",
43
+ "url": "https://opencollective.com/mswjs"
44
+ },
41
45
  "files": [
42
46
  "config/constants.js",
43
47
  "config/scripts/postinstall.js",
@@ -61,7 +65,7 @@
61
65
  "sideEffects": false,
62
66
  "dependencies": {
63
67
  "@mswjs/cookies": "^0.1.6",
64
- "@mswjs/interceptors": "^0.12.3",
68
+ "@mswjs/interceptors": "^0.12.5",
65
69
  "@open-draft/until": "^1.0.3",
66
70
  "@types/cookie": "^0.4.1",
67
71
  "@types/inquirer": "^7.3.3",
@@ -111,7 +115,7 @@
111
115
  "jest": "26",
112
116
  "json-bigint": "^1.0.0",
113
117
  "lint-staged": "^11.0.1",
114
- "page-with": "^0.4.0",
118
+ "page-with": "^0.4.1",
115
119
  "prettier": "^2.3.2",
116
120
  "regenerator-runtime": "^0.13.7",
117
121
  "rimraf": "^3.0.2",