msw 0.22.2 → 0.22.3

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.
@@ -179,4 +179,29 @@ function matchRequestUrl(url, mask) {
179
179
  return match(cleanMask, cleanRequestUrl);
180
180
  }
181
181
 
182
- export { prepareRequest as a, prepareResponse as b, getTimestamp as c, getStatusCodeColor as d, getUrlByMask as g, jsonParse as j, matchRequestUrl as m, parseBody as p };
182
+ /**
183
+ * Return the stack trace frame of a function's invocation.
184
+ */
185
+ function getCallFrame() {
186
+ try {
187
+ const inspectionError = new Error();
188
+ inspectionError.name = 'Inspection Error';
189
+ throw inspectionError;
190
+ }
191
+ catch (error) {
192
+ const frames = error.stack.split('\n');
193
+ // Get the first frame that doesn't reference the library's internal trace.
194
+ // Assume that frame is the invocation frame.
195
+ const declarationFrame = frames.slice(1).find((frame) => {
196
+ return !/(node_modules)?\/lib\/(umd|esm)\//.test(frame);
197
+ });
198
+ if (!declarationFrame) {
199
+ return;
200
+ }
201
+ // Extract file reference from the stack frame.
202
+ const [, declarationPath] = declarationFrame.match(/\((.+?)\)$/) || [];
203
+ return declarationPath;
204
+ }
205
+ }
206
+
207
+ export { getCallFrame as a, prepareRequest as b, prepareResponse as c, getTimestamp as d, getStatusCodeColor as e, getUrlByMask as g, jsonParse as j, matchRequestUrl as m, parseBody as p };
@@ -1,6 +1,6 @@
1
1
  import { a as set, s as status, d as delay, f as fetch } from './fetch-deps.js';
2
2
  import { d as data, e as errors } from './errors-deps.js';
3
- import { j as jsonParse, m as matchRequestUrl, a as prepareRequest, b as prepareResponse, c as getTimestamp, d as getStatusCodeColor } from './matchRequestUrl-deps.js';
3
+ import { j as jsonParse, m as matchRequestUrl, b as prepareRequest, c as prepareResponse, d as getTimestamp, e as getStatusCodeColor, a as getCallFrame } from './getCallFrame-deps.js';
4
4
 
5
5
  function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
6
6
 
@@ -3143,6 +3143,7 @@ function parseQuery(query, definitionOperation = 'query') {
3143
3143
  };
3144
3144
  }
3145
3145
  function graphQLRequestHandler(expectedOperationType, expectedOperationName, mask, resolver) {
3146
+ const callFrame = getCallFrame();
3146
3147
  return {
3147
3148
  resolver,
3148
3149
  parse(req) {
@@ -3214,6 +3215,17 @@ function graphQLRequestHandler(expectedOperationType, expectedOperationName, mas
3214
3215
  console.log('Response:', loggedResponse);
3215
3216
  console.groupEnd();
3216
3217
  },
3218
+ getMetaInfo() {
3219
+ const header = expectedOperationType === 'all'
3220
+ ? `[graphql] ${expectedOperationType} (origin: ${mask.toString()})`
3221
+ : `[graphql] ${expectedOperationType} ${expectedOperationName} (origin: ${mask.toString()})`;
3222
+ return {
3223
+ type: 'graphql',
3224
+ header,
3225
+ mask,
3226
+ callFrame,
3227
+ };
3228
+ },
3217
3229
  };
3218
3230
  }
3219
3231
  const createGraphQLScopedHandler = (expectedOperationType, mask) => {
package/lib/esm/index.js CHANGED
@@ -4,8 +4,8 @@ import './errors-deps.js';
4
4
  export { i as context } from './index-deps.js';
5
5
  import { g as getPublicUrlFromRequest, i as isStringEqual } from './rest-deps.js';
6
6
  export { R as RESTMethods, r as rest, a as restContext } from './rest-deps.js';
7
- import { p as parseBody } from './matchRequestUrl-deps.js';
8
- export { m as matchRequestUrl } from './matchRequestUrl-deps.js';
7
+ import { p as parseBody } from './getCallFrame-deps.js';
8
+ export { m as matchRequestUrl } from './getCallFrame-deps.js';
9
9
  export { graphql, graphqlContext } from './graphql.js';
10
10
 
11
11
  /*! *****************************************************************************
@@ -685,6 +685,18 @@ function setupWorker(...requestHandlers) {
685
685
  resetHandlers(...nextHandlers) {
686
686
  context.requestHandlers = resetHandlers(requestHandlers, ...nextHandlers);
687
687
  },
688
+ printHandlers() {
689
+ context.requestHandlers.forEach((handler) => {
690
+ const meta = handler.getMetaInfo();
691
+ console.groupCollapsed(meta.header);
692
+ console.log(`Declaration: ${meta.callFrame}`);
693
+ console.log('Resolver: %s', handler.resolver);
694
+ if (['rest'].includes(meta.type)) {
695
+ console.log('Match:', `https://mswjs.io/repl?path=${meta.mask}`);
696
+ }
697
+ console.groupEnd();
698
+ });
699
+ },
688
700
  };
689
701
  }
690
702
 
@@ -1,6 +1,6 @@
1
1
  import { c as createCommonjsModule, b as commonjsGlobal, a as set, s as status, j as json, d as delay, f as fetch } from './fetch-deps.js';
2
2
  import { c as cookie, b as body, t as text, x as xml } from './xml-deps.js';
3
- import { g as getUrlByMask, m as matchRequestUrl, a as prepareRequest, b as prepareResponse, c as getTimestamp, d as getStatusCodeColor } from './matchRequestUrl-deps.js';
3
+ import { g as getUrlByMask, a as getCallFrame, m as matchRequestUrl, b as prepareRequest, c as prepareResponse, d as getTimestamp, e as getStatusCodeColor } from './getCallFrame-deps.js';
4
4
 
5
5
  var punycode = createCommonjsModule(function (module, exports) {
6
6
  (function(root) {
@@ -1388,6 +1388,7 @@ const restContext = {
1388
1388
  const createRestHandler = (method) => {
1389
1389
  return (mask, resolver) => {
1390
1390
  const resolvedMask = getUrlByMask(mask);
1391
+ const callFrame = getCallFrame();
1391
1392
  return {
1392
1393
  parse(req) {
1393
1394
  // Match the request during parsing to prevent matching it twice
@@ -1438,6 +1439,14 @@ ${queryParams
1438
1439
  console.log('Response', loggedResponse);
1439
1440
  console.groupEnd();
1440
1441
  },
1442
+ getMetaInfo() {
1443
+ return {
1444
+ type: 'rest',
1445
+ header: `[rest] ${method} ${mask.toString()}`,
1446
+ mask,
1447
+ callFrame,
1448
+ };
1449
+ },
1441
1450
  };
1442
1451
  };
1443
1452
  };
package/lib/esm/rest.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import './fetch-deps.js';
2
2
  import './xml-deps.js';
3
3
  export { R as RESTMethods, r as rest, a as restContext } from './rest-deps.js';
4
- import './matchRequestUrl-deps.js';
4
+ import './getCallFrame-deps.js';
@@ -3,7 +3,7 @@ export { setupWorker } from './setupWorker/setupWorker';
3
3
  export { SetupWorkerApi } from './setupWorker/glossary';
4
4
  export { response, defaultResponse, createResponseComposition, MockedResponse, ResponseTransformer, ResponseComposition, ResponseCompositionOptions, ResponseFunction, } from './response';
5
5
  export { context };
6
- export { MockedRequest, RequestHandler, RequestParams, RequestQuery, ResponseResolver, ResponseResolverReturnType, AsyncResponseResolverReturnType, defaultContext, } from './utils/handlers/requestHandler';
6
+ export { defaultContext, MockedRequest, RequestHandler, RequestHandlerMetaInfo, RequestParams, RequestQuery, ResponseResolver, ResponseResolverReturnType, AsyncResponseResolverReturnType, } from './utils/handlers/requestHandler';
7
7
  export { rest, restContext, RESTMethods, ParsedRestRequest } from './rest';
8
8
  export { graphql, graphqlContext, GraphQLMockedRequest, GraphQLMockedContext, GraphQLRequestPayload, GraphQLResponseResolver, GraphQLRequestParsedResult, } from './graphql';
9
9
  export { matchRequestUrl } from './utils/matching/matchRequestUrl';
@@ -17,6 +17,10 @@ export interface SetupServerApi {
17
17
  * Resets request handlers to the initial list given to the `setupServer` call, or to the explicit next request handlers list, if given.
18
18
  */
19
19
  resetHandlers: (...nextHandlers: RequestHandlersList) => void;
20
+ /**
21
+ * Lists all active request handlers.
22
+ */
23
+ printHandlers: () => void;
20
24
  /**
21
25
  * Stops requests interception by restoring all augmented modules.
22
26
  */
@@ -73,4 +73,8 @@ export interface SetupWorkerApi {
73
73
  * Resets request handlers to the initial list given to the `setupWorker` call, or to the explicit next request handlers list, if given.
74
74
  */
75
75
  resetHandlers: (...nextHandlers: RequestHandlersList) => void;
76
+ /**
77
+ * Lists all active request handlers.
78
+ */
79
+ printHandlers: () => void;
76
80
  }
@@ -1,5 +1,5 @@
1
1
  import { Headers } from 'headers-utils';
2
- import { ResponseWithSerializedHeaders } from '../../setupWorker/glossary';
2
+ import { Mask, ResponseWithSerializedHeaders } from '../../setupWorker/glossary';
3
3
  import { ResponseComposition, MockedResponse } from '../../response';
4
4
  import { set } from '../../context/set';
5
5
  export declare const defaultContext: {
@@ -36,6 +36,13 @@ export declare type RequestParams = {
36
36
  export declare type ResponseResolverReturnType<R> = R | undefined | void;
37
37
  export declare type AsyncResponseResolverReturnType<R> = Promise<ResponseResolverReturnType<R>> | ResponseResolverReturnType<R>;
38
38
  export declare type ResponseResolver<RequestType = MockedRequest, ContextType = typeof defaultContext, BodyType = any> = (req: RequestType, res: ResponseComposition<BodyType>, context: ContextType) => AsyncResponseResolverReturnType<MockedResponse<BodyType>>;
39
+ declare type RequestHandlerType = 'rest' | 'graphql';
40
+ export interface RequestHandlerMetaInfo<Type = RequestHandlerType> {
41
+ type: Type;
42
+ header: string;
43
+ mask: Mask;
44
+ callFrame: string | undefined;
45
+ }
39
46
  export interface RequestHandler<RequestType = MockedRequest, ContextType = typeof defaultContext, ParsedRequest = any, PublicRequest = RequestType, ResponseBodyType = any> {
40
47
  /**
41
48
  * Parses a captured request to retrieve additional
@@ -69,6 +76,11 @@ export interface RequestHandler<RequestType = MockedRequest, ContextType = typeo
69
76
  * when dealing with any subsequent matching requests.
70
77
  */
71
78
  shouldSkip?: boolean;
79
+ /**
80
+ * Returns request handler's meta information used
81
+ * when listing each current request handler.
82
+ */
83
+ getMetaInfo: () => RequestHandlerMetaInfo;
72
84
  }
73
85
  declare const _default: null;
74
86
  export default _default;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Return the stack trace frame of a function's invocation.
3
+ */
4
+ export declare function getCallFrame(): string | undefined;
package/lib/umd/index.js CHANGED
@@ -2818,6 +2818,18 @@ If this message still persists after updating, please report an issue: https://g
2818
2818
  resetHandlers(...nextHandlers) {
2819
2819
  context.requestHandlers = resetHandlers(requestHandlers, ...nextHandlers);
2820
2820
  },
2821
+ printHandlers() {
2822
+ context.requestHandlers.forEach((handler) => {
2823
+ const meta = handler.getMetaInfo();
2824
+ console.groupCollapsed(meta.header);
2825
+ console.log(`Declaration: ${meta.callFrame}`);
2826
+ console.log('Resolver: %s', handler.resolver);
2827
+ if (['rest'].includes(meta.type)) {
2828
+ console.log('Match:', `https://mswjs.io/repl?path=${meta.mask}`);
2829
+ }
2830
+ console.groupEnd();
2831
+ });
2832
+ },
2821
2833
  };
2822
2834
  }
2823
2835
 
@@ -2969,6 +2981,31 @@ If this message still persists after updating, please report an issue: https://g
2969
2981
  return match(cleanMask, cleanRequestUrl);
2970
2982
  }
2971
2983
 
2984
+ /**
2985
+ * Return the stack trace frame of a function's invocation.
2986
+ */
2987
+ function getCallFrame() {
2988
+ try {
2989
+ const inspectionError = new Error();
2990
+ inspectionError.name = 'Inspection Error';
2991
+ throw inspectionError;
2992
+ }
2993
+ catch (error) {
2994
+ const frames = error.stack.split('\n');
2995
+ // Get the first frame that doesn't reference the library's internal trace.
2996
+ // Assume that frame is the invocation frame.
2997
+ const declarationFrame = frames.slice(1).find((frame) => {
2998
+ return !/(node_modules)?\/lib\/(umd|esm)\//.test(frame);
2999
+ });
3000
+ if (!declarationFrame) {
3001
+ return;
3002
+ }
3003
+ // Extract file reference from the stack frame.
3004
+ const [, declarationPath] = declarationFrame.match(/\((.+?)\)$/) || [];
3005
+ return declarationPath;
3006
+ }
3007
+ }
3008
+
2972
3009
  (function (RESTMethods) {
2973
3010
  RESTMethods["HEAD"] = "HEAD";
2974
3011
  RESTMethods["GET"] = "GET";
@@ -2992,6 +3029,7 @@ If this message still persists after updating, please report an issue: https://g
2992
3029
  const createRestHandler = (method) => {
2993
3030
  return (mask, resolver) => {
2994
3031
  const resolvedMask = getUrlByMask(mask);
3032
+ const callFrame = getCallFrame();
2995
3033
  return {
2996
3034
  parse(req) {
2997
3035
  // Match the request during parsing to prevent matching it twice
@@ -3042,6 +3080,14 @@ ${queryParams
3042
3080
  console.log('Response', loggedResponse);
3043
3081
  console.groupEnd();
3044
3082
  },
3083
+ getMetaInfo() {
3084
+ return {
3085
+ type: 'rest',
3086
+ header: `[rest] ${method} ${mask.toString()}`,
3087
+ mask,
3088
+ callFrame,
3089
+ };
3090
+ },
3045
3091
  };
3046
3092
  };
3047
3093
  };
@@ -6196,6 +6242,7 @@ ${queryParams
6196
6242
  };
6197
6243
  }
6198
6244
  function graphQLRequestHandler(expectedOperationType, expectedOperationName, mask, resolver) {
6245
+ const callFrame = getCallFrame();
6199
6246
  return {
6200
6247
  resolver,
6201
6248
  parse(req) {
@@ -6267,6 +6314,17 @@ ${queryParams
6267
6314
  console.log('Response:', loggedResponse);
6268
6315
  console.groupEnd();
6269
6316
  },
6317
+ getMetaInfo() {
6318
+ const header = expectedOperationType === 'all'
6319
+ ? `[graphql] ${expectedOperationType} (origin: ${mask.toString()})`
6320
+ : `[graphql] ${expectedOperationType} ${expectedOperationName} (origin: ${mask.toString()})`;
6321
+ return {
6322
+ type: 'graphql',
6323
+ header,
6324
+ mask,
6325
+ callFrame,
6326
+ };
6327
+ },
6270
6328
  };
6271
6329
  }
6272
6330
  const createGraphQLScopedHandler = (expectedOperationType, mask) => {