msw 0.21.3 → 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.
Files changed (49) hide show
  1. package/README.md +3 -9
  2. package/lib/esm/errors-deps.js +6 -7
  3. package/lib/esm/fetch-deps.js +44 -22
  4. package/lib/esm/{matchRequestUrl-deps.js → getCallFrame-deps.js} +32 -5
  5. package/lib/esm/graphql.js +601 -465
  6. package/lib/esm/index.js +56 -50
  7. package/lib/esm/mockServiceWorker.js +16 -10
  8. package/lib/esm/rest-deps.js +10 -1
  9. package/lib/esm/rest.js +1 -1
  10. package/lib/types/context/errors.d.ts +3 -3
  11. package/lib/types/context/json.d.ts +5 -1
  12. package/lib/types/index.d.ts +3 -2
  13. package/lib/types/native/index.d.ts +1 -7
  14. package/lib/types/node/createSetupServer.d.ts +2 -23
  15. package/{node/node/createSetupServer.d.ts → lib/types/node/glossary.d.ts} +12 -13
  16. package/lib/types/node/index.d.ts +1 -0
  17. package/lib/types/node/setupServer.d.ts +1 -7
  18. package/lib/types/response.d.ts +7 -2
  19. package/lib/types/rest.d.ts +60 -30
  20. package/lib/types/setupWorker/glossary.d.ts +22 -0
  21. package/lib/types/setupWorker/setupWorker.d.ts +1 -19
  22. package/lib/types/utils/handlers/requestHandler.d.ts +13 -1
  23. package/lib/types/utils/internal/getCallFrame.d.ts +4 -0
  24. package/lib/types/utils/internal/isObject.d.ts +4 -0
  25. package/lib/types/utils/internal/mergeRight.d.ts +1 -1
  26. package/lib/types/utils/request/onUnhandledRequest.d.ts +1 -1
  27. package/lib/umd/index.js +720 -519
  28. package/lib/umd/mockServiceWorker.js +16 -10
  29. package/native/index.js +2017 -125
  30. package/node/index.js +2017 -125
  31. package/package.json +34 -32
  32. package/lib/types/LiveStorage.d.ts +0 -17
  33. package/node/context/delay.d.ts +0 -11
  34. package/node/context/fetch.d.ts +0 -8
  35. package/node/context/set.d.ts +0 -2
  36. package/node/context/status.d.ts +0 -2
  37. package/node/node/index.d.ts +0 -5
  38. package/node/node/setupServer.d.ts +0 -7
  39. package/node/response.d.ts +0 -25
  40. package/node/utils/NetworkError.d.ts +0 -3
  41. package/node/utils/getResponse.d.ts +0 -14
  42. package/node/utils/handlers/requestHandler.d.ts +0 -74
  43. package/node/utils/handlers/requestHandlerUtils.d.ts +0 -4
  44. package/node/utils/internal/compose.d.ts +0 -5
  45. package/node/utils/internal/isNodeProcess.d.ts +0 -5
  46. package/node/utils/internal/jsonParse.d.ts +0 -5
  47. package/node/utils/request/getPublicUrlFromRequest.d.ts +0 -6
  48. package/node/utils/request/onUnhandledRequest.d.ts +0 -5
  49. package/node/utils/request/parseBody.d.ts +0 -5
package/README.md CHANGED
@@ -9,12 +9,10 @@
9
9
  <a href="https://circleci.com/gh/mswjs/msw" target="_blank">
10
10
  <img src="https://img.shields.io/circleci/project/github/mswjs/msw/master.svg" alt="Build status" />
11
11
  </a>
12
- <a href="https://david-dm.org/mswjs/msw" target="_blank">
13
- <img src="https://img.shields.io/david/mswjs/msw.svg" alt="Dependencies status" />
14
- </a>
15
- <a href="https://david-dm.org/mswjs/msw?type=dev" target="_blank">
16
- <img src="https://img.shields.io/david/dev/mswjs/msw.svg" alt="Dev dependencies status" />
12
+ <a href="https://www.npmjs.com/package/msw" target="_blank">
13
+ <img src="https://img.shields.io/npm/dw/msw" alt="Download rate" />
17
14
  </a>
15
+ <img alt="Libraries.io dependency status for latest release" src="https://img.shields.io/librariesio/release/npm/msw"/ >
18
16
  <a href="https://kcd.im/discord" target="_blank">
19
17
  <img src="https://img.shields.io/badge/chat-online-green" alt="Discord server" />
20
18
  </a>
@@ -188,10 +186,6 @@ test('handles login exception', () => {
188
186
 
189
187
  > **Tip:** Did you know that although the API is called `setupServer`, there are no actual servers established? The name is kept for familiarity, and API is designed to resemble operating with an actual server.
190
188
 
191
- ## Using the library?
192
-
193
- Do you use `msw` at your company? [**Let us know**](https://twitter.com/compose/tweet?text=We%20are%20using%20@ApiMocking%20at%20%3CCOMPANY_NAME%3E%20and%20we%20love%20it!) and get your company's logo featured on [our website](https://mswjs.io) as a token of appreciation.
194
-
195
189
  ## Awards & Mentions
196
190
 
197
191
  <br />
@@ -4,18 +4,17 @@ import { j as json } from './fetch-deps.js';
4
4
  * Returns a GraphQL body payload.
5
5
  */
6
6
  const data = (payload) => {
7
- return (res) => {
8
- res.headers.set('Content-Type', 'application/json');
9
- res.body = JSON.stringify({ data: payload });
10
- return res;
11
- };
7
+ return json({ data: payload }, { merge: true });
12
8
  };
13
9
 
14
10
  /**
15
- * Returns a list of GraphQL errors.
11
+ * Sets a given list of GraphQL errors on the mocked response.
16
12
  */
17
13
  const errors = (errorsList) => {
18
- return json({ errors: errorsList });
14
+ if (errorsList == null) {
15
+ return (res) => res;
16
+ }
17
+ return json({ errors: errorsList }, { merge: true });
19
18
  };
20
19
 
21
20
  export { data as d, errors as e };
@@ -1,4 +1,4 @@
1
- var codes = {
1
+ var statuses = {
2
2
  "100": "Continue",
3
3
  "101": "Switching Protocols",
4
4
  "102": "Processing",
@@ -64,11 +64,6 @@ var codes = {
64
64
  "511": "Network Authentication Required"
65
65
  };
66
66
 
67
- var statuses = /*#__PURE__*/Object.freeze({
68
- __proto__: null,
69
- 'default': codes
70
- });
71
-
72
67
  const status = (statusCode, statusText) => {
73
68
  return (res) => {
74
69
  res.status = statusCode;
@@ -348,6 +343,48 @@ function set(...args) {
348
343
  };
349
344
  }
350
345
 
346
+ /**
347
+ * Determines if the given value is an object.
348
+ */
349
+ function isObject(value) {
350
+ return value != null && typeof value === 'object' && !Array.isArray(value);
351
+ }
352
+
353
+ /**
354
+ * Deeply merges two given objects with the right one
355
+ * having a priority during property assignment.
356
+ */
357
+ function mergeRight(left, right) {
358
+ return Object.entries(right).reduce((result, [key, rightValue]) => {
359
+ const leftValue = result[key];
360
+ if (Array.isArray(leftValue) && Array.isArray(rightValue)) {
361
+ result[key] = leftValue.concat(rightValue);
362
+ return result;
363
+ }
364
+ if (isObject(leftValue) && isObject(rightValue)) {
365
+ result[key] = mergeRight(leftValue, rightValue);
366
+ return result;
367
+ }
368
+ result[key] = rightValue;
369
+ return result;
370
+ }, Object.assign({}, left));
371
+ }
372
+
373
+ /**
374
+ * Sets the given value as the JSON body of the response.
375
+ * @example
376
+ * res(json({ key: 'value' }))
377
+ * res(json('Some string'))
378
+ * res(json([1, '2', false, { ok: true }]))
379
+ */
380
+ const json = (body, { merge = false } = {}) => {
381
+ return (res) => {
382
+ res.headers.set('Content-Type', 'application/json');
383
+ res.body = merge ? mergeRight(res.body || {}, body) : body;
384
+ return res;
385
+ };
386
+ };
387
+
351
388
  /**
352
389
  * Returns a boolean indicating if the current process is running in NodeJS environment.
353
390
  * @see https://github.com/mswjs/msw/pull/255
@@ -387,21 +424,6 @@ const delay = (durationMs) => {
387
424
  };
388
425
  };
389
426
 
390
- /**
391
- * Sets the given value as the JSON body of the response.
392
- * @example
393
- * res(json({ key: 'value' }))
394
- * res(json('Some string'))
395
- * res(json([1, '2', false, { ok: true }]))
396
- */
397
- const json = (body) => {
398
- return (res) => {
399
- res.headers.set('Content-Type', 'application/json');
400
- res.body = JSON.stringify(body);
401
- return res;
402
- };
403
- };
404
-
405
427
  const useFetch = isNodeProcess() ? require('node-fetch') : window.fetch;
406
428
  const augmentRequestInit = (requestInit) => {
407
429
  const headers = new lib.Headers(requestInit.headers);
@@ -433,4 +455,4 @@ const fetch = (input, requestInit = {}) => {
433
455
  return useFetch(input.url.href, compliantRequest);
434
456
  };
435
457
 
436
- export { set as a, commonjsGlobal as b, createCommonjsModule as c, delay as d, fetch as f, isNodeProcess as i, json as j, lib as l, status as s };
458
+ export { set as a, commonjsGlobal as b, createCommonjsModule as c, delay as d, fetch as f, isNodeProcess as i, json as j, lib as l, mergeRight as m, status as s };
@@ -84,13 +84,13 @@ const pathToRegExp = (path) => {
84
84
  .replace(/\?/g, '\\?')
85
85
  // Ignore trailing slashes
86
86
  .replace(/\/+$/, '')
87
- // Replace wildcard with any single character sequence
88
- .replace(/\*+/g, '.+')
87
+ // Replace wildcard with any zero-to-any character sequence
88
+ .replace(/\*+/g, '.*')
89
89
  // Replace parameters with named capturing groups
90
- .replace(/:([^\d|^\/][a-zA-Z0-9]*(?=(?:\/|\\.)|$))/g, (_, match) => `(?<${match}>.+?)`)
90
+ .replace(/:([^\d|^\/][a-zA-Z0-9_]*(?=(?:\/|\\.)|$))/g, (_, paramName) => `(?<${paramName}>[^\/]+?)`)
91
91
  // Allow optional trailing slash
92
92
  .concat('(\\/|$)');
93
- return new RegExp(pattern, 'g');
93
+ return new RegExp(pattern, 'gi');
94
94
  };
95
95
 
96
96
  /**
@@ -111,6 +111,7 @@ const match = (path, url) => {
111
111
 
112
112
  var getCleanUrl_1 = createCommonjsModule(function (module, exports) {
113
113
  Object.defineProperty(exports, "__esModule", { value: true });
114
+ exports.getCleanUrl = void 0;
114
115
  /**
115
116
  * Removes query parameters and hashes from a given URL.
116
117
  */
@@ -119,6 +120,7 @@ function getCleanUrl(url, isAbsolute) {
119
120
  return [isAbsolute && url.origin, url.pathname].filter(Boolean).join('');
120
121
  }
121
122
  exports.getCleanUrl = getCleanUrl;
123
+
122
124
  });
123
125
 
124
126
  /**
@@ -177,4 +179,29 @@ function matchRequestUrl(url, mask) {
177
179
  return match(cleanMask, cleanRequestUrl);
178
180
  }
179
181
 
180
- 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 };