msw 1.3.2 → 2.0.1

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 (169) hide show
  1. package/README.md +45 -29
  2. package/browser/package.json +5 -0
  3. package/cli/init.js +3 -3
  4. package/config/constants.js +1 -2
  5. package/lib/browser/index.d.ts +104 -0
  6. package/lib/browser/index.js +752 -0
  7. package/lib/browser/index.mjs +734 -0
  8. package/lib/core/GraphQLHandler-b42ca96f.d.ts +74 -0
  9. package/lib/core/HttpResponse.d.ts +2 -0
  10. package/lib/core/HttpResponse.js +94 -0
  11. package/lib/core/HttpResponse.mjs +77 -0
  12. package/lib/core/RequestHandler-1a5dbdc7.d.ts +172 -0
  13. package/lib/core/SetupApi.d.ts +26 -0
  14. package/lib/core/SetupApi.js +83 -0
  15. package/lib/core/SetupApi.mjs +63 -0
  16. package/lib/core/bypass.d.ts +16 -0
  17. package/lib/core/bypass.js +36 -0
  18. package/lib/core/bypass.mjs +16 -0
  19. package/lib/core/delay.d.ts +18 -0
  20. package/lib/core/delay.js +92 -0
  21. package/lib/core/delay.mjs +72 -0
  22. package/lib/core/graphql.d.ts +109 -0
  23. package/lib/core/graphql.js +112 -0
  24. package/lib/core/graphql.mjs +96 -0
  25. package/lib/core/handlers/GraphQLHandler.d.ts +5 -0
  26. package/lib/core/handlers/GraphQLHandler.js +145 -0
  27. package/lib/core/handlers/GraphQLHandler.mjs +130 -0
  28. package/lib/core/handlers/HttpHandler.d.ts +62 -0
  29. package/lib/core/handlers/HttpHandler.js +149 -0
  30. package/lib/core/handlers/HttpHandler.mjs +133 -0
  31. package/lib/core/handlers/RequestHandler.d.ts +2 -0
  32. package/lib/core/handlers/RequestHandler.js +180 -0
  33. package/lib/core/handlers/RequestHandler.mjs +162 -0
  34. package/lib/core/http.d.ts +26 -0
  35. package/lib/core/http.js +39 -0
  36. package/lib/core/http.mjs +22 -0
  37. package/lib/core/http.spec.d.ts +2 -0
  38. package/lib/core/http.spec.js +15 -0
  39. package/lib/core/http.spec.mjs +14 -0
  40. package/lib/core/index.d.ts +18 -0
  41. package/lib/core/index.js +49 -0
  42. package/lib/core/index.mjs +28 -0
  43. package/lib/core/passthrough.d.ts +17 -0
  44. package/lib/core/passthrough.js +32 -0
  45. package/lib/core/passthrough.mjs +12 -0
  46. package/lib/core/sharedOptions.d.ts +66 -0
  47. package/lib/core/sharedOptions.js +16 -0
  48. package/lib/core/sharedOptions.mjs +0 -0
  49. package/lib/core/typeUtils.d.ts +7 -0
  50. package/lib/core/typeUtils.js +16 -0
  51. package/lib/core/typeUtils.mjs +0 -0
  52. package/lib/core/utils/HttpResponse/decorators.d.ts +12 -0
  53. package/lib/core/utils/HttpResponse/decorators.js +80 -0
  54. package/lib/core/utils/HttpResponse/decorators.mjs +52 -0
  55. package/lib/core/utils/getResponse.d.ts +2 -0
  56. package/lib/core/utils/getResponse.js +64 -0
  57. package/lib/core/utils/getResponse.mjs +44 -0
  58. package/lib/core/utils/handleRequest.d.ts +30 -0
  59. package/lib/core/utils/handleRequest.js +102 -0
  60. package/lib/core/utils/handleRequest.mjs +82 -0
  61. package/lib/core/utils/internal/Disposable.d.ts +7 -0
  62. package/lib/core/utils/internal/Disposable.js +53 -0
  63. package/lib/core/utils/internal/Disposable.mjs +33 -0
  64. package/lib/core/utils/internal/checkGlobals.d.ts +3 -0
  65. package/lib/core/utils/internal/checkGlobals.js +33 -0
  66. package/lib/core/utils/internal/checkGlobals.mjs +13 -0
  67. package/lib/core/utils/internal/devUtils.d.ts +19 -0
  68. package/lib/core/utils/internal/devUtils.js +40 -0
  69. package/lib/core/utils/internal/devUtils.mjs +20 -0
  70. package/lib/core/utils/internal/getCallFrame.d.ts +6 -0
  71. package/lib/core/utils/internal/getCallFrame.js +40 -0
  72. package/lib/core/utils/internal/getCallFrame.mjs +20 -0
  73. package/lib/core/utils/internal/isIterable.d.ts +6 -0
  74. package/lib/core/utils/internal/isIterable.js +29 -0
  75. package/lib/core/utils/internal/isIterable.mjs +9 -0
  76. package/lib/core/utils/internal/isObject.d.ts +6 -0
  77. package/lib/core/utils/internal/isObject.js +26 -0
  78. package/lib/core/utils/internal/isObject.mjs +6 -0
  79. package/lib/core/utils/internal/isStringEqual.d.ts +6 -0
  80. package/lib/core/utils/internal/isStringEqual.js +26 -0
  81. package/lib/core/utils/internal/isStringEqual.mjs +6 -0
  82. package/lib/core/utils/internal/jsonParse.d.ts +7 -0
  83. package/lib/core/utils/internal/jsonParse.js +30 -0
  84. package/lib/core/utils/internal/jsonParse.mjs +10 -0
  85. package/lib/core/utils/internal/mergeRight.d.ts +7 -0
  86. package/lib/core/utils/internal/mergeRight.js +39 -0
  87. package/lib/core/utils/internal/mergeRight.mjs +19 -0
  88. package/lib/core/utils/internal/parseGraphQLRequest.d.ts +5 -0
  89. package/lib/core/utils/internal/parseGraphQLRequest.js +184 -0
  90. package/lib/core/utils/internal/parseGraphQLRequest.mjs +165 -0
  91. package/lib/core/utils/internal/parseMultipartData.d.ts +10 -0
  92. package/lib/core/utils/internal/parseMultipartData.js +81 -0
  93. package/lib/core/utils/internal/parseMultipartData.mjs +61 -0
  94. package/lib/core/utils/internal/pipeEvents.d.ts +8 -0
  95. package/lib/core/utils/internal/pipeEvents.js +34 -0
  96. package/lib/core/utils/internal/pipeEvents.mjs +14 -0
  97. package/lib/core/utils/internal/requestHandlerUtils.d.ts +8 -0
  98. package/lib/core/utils/internal/requestHandlerUtils.js +36 -0
  99. package/lib/core/utils/internal/requestHandlerUtils.mjs +16 -0
  100. package/lib/core/utils/internal/toReadonlyArray.d.ts +6 -0
  101. package/lib/core/utils/internal/toReadonlyArray.js +28 -0
  102. package/lib/core/utils/internal/toReadonlyArray.mjs +8 -0
  103. package/lib/core/utils/internal/tryCatch.d.ts +3 -0
  104. package/lib/core/utils/internal/tryCatch.js +31 -0
  105. package/lib/core/utils/internal/tryCatch.mjs +11 -0
  106. package/lib/core/utils/internal/uuidv4.d.ts +3 -0
  107. package/lib/core/utils/internal/uuidv4.js +26 -0
  108. package/lib/core/utils/internal/uuidv4.mjs +6 -0
  109. package/lib/core/utils/logging/getStatusCodeColor.d.ts +11 -0
  110. package/lib/core/utils/logging/getStatusCodeColor.js +39 -0
  111. package/lib/core/utils/logging/getStatusCodeColor.mjs +19 -0
  112. package/lib/core/utils/logging/getTimestamp.d.ts +6 -0
  113. package/lib/core/utils/logging/getTimestamp.js +27 -0
  114. package/lib/core/utils/logging/getTimestamp.mjs +7 -0
  115. package/lib/core/utils/logging/serializeRequest.d.ts +12 -0
  116. package/lib/core/utils/logging/serializeRequest.js +55 -0
  117. package/lib/core/utils/logging/serializeRequest.mjs +35 -0
  118. package/lib/core/utils/logging/serializeResponse.d.ts +9 -0
  119. package/lib/core/utils/logging/serializeResponse.js +69 -0
  120. package/lib/core/utils/logging/serializeResponse.mjs +39 -0
  121. package/lib/core/utils/matching/matchRequestUrl.d.ts +19 -0
  122. package/lib/core/utils/matching/matchRequestUrl.js +50 -0
  123. package/lib/core/utils/matching/matchRequestUrl.mjs +30 -0
  124. package/lib/core/utils/matching/normalizePath.d.ts +12 -0
  125. package/lib/core/utils/matching/normalizePath.js +32 -0
  126. package/lib/core/utils/matching/normalizePath.mjs +12 -0
  127. package/lib/core/utils/request/getPublicUrlFromRequest.d.ts +7 -0
  128. package/lib/core/utils/request/getPublicUrlFromRequest.js +30 -0
  129. package/lib/core/utils/request/getPublicUrlFromRequest.mjs +10 -0
  130. package/lib/core/utils/request/getRequestCookies.d.ts +8 -0
  131. package/lib/core/utils/request/getRequestCookies.js +88 -0
  132. package/lib/core/utils/request/getRequestCookies.mjs +60 -0
  133. package/lib/core/utils/request/onUnhandledRequest.d.ts +12 -0
  134. package/lib/core/utils/request/onUnhandledRequest.js +193 -0
  135. package/lib/core/utils/request/onUnhandledRequest.mjs +165 -0
  136. package/lib/core/utils/request/readResponseCookies.d.ts +3 -0
  137. package/lib/core/utils/request/readResponseCookies.js +45 -0
  138. package/lib/core/utils/request/readResponseCookies.mjs +27 -0
  139. package/lib/core/utils/toResponseInit.d.ts +3 -0
  140. package/lib/core/utils/toResponseInit.js +30 -0
  141. package/lib/core/utils/toResponseInit.mjs +10 -0
  142. package/lib/core/utils/url/cleanUrl.d.ts +7 -0
  143. package/lib/core/utils/url/cleanUrl.js +31 -0
  144. package/lib/core/utils/url/cleanUrl.mjs +11 -0
  145. package/lib/core/utils/url/getAbsoluteUrl.d.ts +6 -0
  146. package/lib/core/utils/url/getAbsoluteUrl.js +37 -0
  147. package/lib/core/utils/url/getAbsoluteUrl.mjs +17 -0
  148. package/lib/core/utils/url/isAbsoluteUrl.d.ts +6 -0
  149. package/lib/core/utils/url/isAbsoluteUrl.js +26 -0
  150. package/lib/core/utils/url/isAbsoluteUrl.mjs +6 -0
  151. package/lib/iife/index.js +6204 -12360
  152. package/lib/mockServiceWorker.js +82 -93
  153. package/lib/native/index.d.ts +67 -7
  154. package/lib/native/index.js +74 -1500
  155. package/lib/native/index.mjs +69 -1514
  156. package/lib/node/index.d.ts +68 -9
  157. package/lib/node/index.js +77 -1508
  158. package/lib/node/index.mjs +71 -1521
  159. package/native/package.json +2 -1
  160. package/node/package.json +2 -1
  161. package/package.json +53 -23
  162. package/lib/SetupServerApi-1855d9c6.d.ts +0 -20
  163. package/lib/glossary-de6278a9.d.ts +0 -385
  164. package/lib/iife/index.js.map +0 -1
  165. package/lib/index.d.ts +0 -365
  166. package/lib/index.js +0 -2295
  167. package/lib/index.js.map +0 -1
  168. package/lib/node/index.js.map +0 -1
  169. package/lib/node/index.mjs.map +0 -1
@@ -0,0 +1,20 @@
1
+ const SOURCE_FRAME = /[\/\\]msw[\/\\]src[\/\\](.+)/;
2
+ const BUILD_FRAME = /(node_modules)?[\/\\]lib[\/\\](core|browser|node|native|iife)[\/\\]|^[^\/\\]*$/;
3
+ function getCallFrame(error) {
4
+ const stack = error.stack;
5
+ if (!stack) {
6
+ return;
7
+ }
8
+ const frames = stack.split("\n").slice(1);
9
+ const declarationFrame = frames.find((frame) => {
10
+ return !(SOURCE_FRAME.test(frame) || BUILD_FRAME.test(frame));
11
+ });
12
+ if (!declarationFrame) {
13
+ return;
14
+ }
15
+ const declarationPath = declarationFrame.replace(/\s*at [^()]*\(([^)]+)\)/, "$1").replace(/^@/, "");
16
+ return declarationPath;
17
+ }
18
+ export {
19
+ getCallFrame
20
+ };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Determines if the given function is an iterator.
3
+ */
4
+ declare function isIterable<IteratorType>(fn: any): fn is Generator<IteratorType, IteratorType, IteratorType>;
5
+
6
+ export { isIterable };
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var isIterable_exports = {};
20
+ __export(isIterable_exports, {
21
+ isIterable: () => isIterable
22
+ });
23
+ module.exports = __toCommonJS(isIterable_exports);
24
+ function isIterable(fn) {
25
+ if (!fn) {
26
+ return false;
27
+ }
28
+ return typeof fn[Symbol.iterator] == "function";
29
+ }
@@ -0,0 +1,9 @@
1
+ function isIterable(fn) {
2
+ if (!fn) {
3
+ return false;
4
+ }
5
+ return typeof fn[Symbol.iterator] == "function";
6
+ }
7
+ export {
8
+ isIterable
9
+ };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Determines if the given value is an object.
3
+ */
4
+ declare function isObject(value: any): boolean;
5
+
6
+ export { isObject };
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var isObject_exports = {};
20
+ __export(isObject_exports, {
21
+ isObject: () => isObject
22
+ });
23
+ module.exports = __toCommonJS(isObject_exports);
24
+ function isObject(value) {
25
+ return value != null && typeof value === "object" && !Array.isArray(value);
26
+ }
@@ -0,0 +1,6 @@
1
+ function isObject(value) {
2
+ return value != null && typeof value === "object" && !Array.isArray(value);
3
+ }
4
+ export {
5
+ isObject
6
+ };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Performs a case-insensitive comparison of two given strings.
3
+ */
4
+ declare function isStringEqual(actual: string, expected: string): boolean;
5
+
6
+ export { isStringEqual };
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var isStringEqual_exports = {};
20
+ __export(isStringEqual_exports, {
21
+ isStringEqual: () => isStringEqual
22
+ });
23
+ module.exports = __toCommonJS(isStringEqual_exports);
24
+ function isStringEqual(actual, expected) {
25
+ return actual.toLowerCase() === expected.toLowerCase();
26
+ }
@@ -0,0 +1,6 @@
1
+ function isStringEqual(actual, expected) {
2
+ return actual.toLowerCase() === expected.toLowerCase();
3
+ }
4
+ export {
5
+ isStringEqual
6
+ };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Parses a given value into a JSON.
3
+ * Does not throw an exception on an invalid JSON string.
4
+ */
5
+ declare function jsonParse<ValueType extends Record<string, any>>(value: any): ValueType | undefined;
6
+
7
+ export { jsonParse };
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var jsonParse_exports = {};
20
+ __export(jsonParse_exports, {
21
+ jsonParse: () => jsonParse
22
+ });
23
+ module.exports = __toCommonJS(jsonParse_exports);
24
+ function jsonParse(value) {
25
+ try {
26
+ return JSON.parse(value);
27
+ } catch (error) {
28
+ return void 0;
29
+ }
30
+ }
@@ -0,0 +1,10 @@
1
+ function jsonParse(value) {
2
+ try {
3
+ return JSON.parse(value);
4
+ } catch (error) {
5
+ return void 0;
6
+ }
7
+ }
8
+ export {
9
+ jsonParse
10
+ };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Deeply merges two given objects with the right one
3
+ * having a priority during property assignment.
4
+ */
5
+ declare function mergeRight(left: Record<string, any>, right: Record<string, any>): Record<string, any>;
6
+
7
+ export { mergeRight };
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var mergeRight_exports = {};
20
+ __export(mergeRight_exports, {
21
+ mergeRight: () => mergeRight
22
+ });
23
+ module.exports = __toCommonJS(mergeRight_exports);
24
+ var import_isObject = require("./isObject.js");
25
+ function mergeRight(left, right) {
26
+ return Object.entries(right).reduce((result, [key, rightValue]) => {
27
+ const leftValue = result[key];
28
+ if (Array.isArray(leftValue) && Array.isArray(rightValue)) {
29
+ result[key] = leftValue.concat(rightValue);
30
+ return result;
31
+ }
32
+ if ((0, import_isObject.isObject)(leftValue) && (0, import_isObject.isObject)(rightValue)) {
33
+ result[key] = mergeRight(leftValue, rightValue);
34
+ return result;
35
+ }
36
+ result[key] = rightValue;
37
+ return result;
38
+ }, Object.assign({}, left));
39
+ }
@@ -0,0 +1,19 @@
1
+ import { isObject } from './isObject.mjs';
2
+ function mergeRight(left, right) {
3
+ return Object.entries(right).reduce((result, [key, rightValue]) => {
4
+ const leftValue = result[key];
5
+ if (Array.isArray(leftValue) && Array.isArray(rightValue)) {
6
+ result[key] = leftValue.concat(rightValue);
7
+ return result;
8
+ }
9
+ if (isObject(leftValue) && isObject(rightValue)) {
10
+ result[key] = mergeRight(leftValue, rightValue);
11
+ return result;
12
+ }
13
+ result[key] = rightValue;
14
+ return result;
15
+ }, Object.assign({}, left));
16
+ }
17
+ export {
18
+ mergeRight
19
+ };
@@ -0,0 +1,5 @@
1
+ import 'graphql';
2
+ export { i as GraphQLMultipartRequestBody, h as GraphQLParsedOperationsMap, g as ParsedGraphQLQuery, P as ParsedGraphQLRequest, p as parseDocumentNode, j as parseGraphQLRequest } from '../../GraphQLHandler-b42ca96f.js';
3
+ import '../../RequestHandler-1a5dbdc7.js';
4
+ import '../../typeUtils.js';
5
+ import '../matching/matchRequestUrl.js';
@@ -0,0 +1,184 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
+ var __objRest = (source, exclude) => {
9
+ var target = {};
10
+ for (var prop in source)
11
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
12
+ target[prop] = source[prop];
13
+ if (source != null && __getOwnPropSymbols)
14
+ for (var prop of __getOwnPropSymbols(source)) {
15
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
16
+ target[prop] = source[prop];
17
+ }
18
+ return target;
19
+ };
20
+ var __export = (target, all) => {
21
+ for (var name in all)
22
+ __defProp(target, name, { get: all[name], enumerable: true });
23
+ };
24
+ var __copyProps = (to, from, except, desc) => {
25
+ if (from && typeof from === "object" || typeof from === "function") {
26
+ for (let key of __getOwnPropNames(from))
27
+ if (!__hasOwnProp.call(to, key) && key !== except)
28
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
29
+ }
30
+ return to;
31
+ };
32
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
33
+ var __async = (__this, __arguments, generator) => {
34
+ return new Promise((resolve, reject) => {
35
+ var fulfilled = (value) => {
36
+ try {
37
+ step(generator.next(value));
38
+ } catch (e) {
39
+ reject(e);
40
+ }
41
+ };
42
+ var rejected = (value) => {
43
+ try {
44
+ step(generator.throw(value));
45
+ } catch (e) {
46
+ reject(e);
47
+ }
48
+ };
49
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
50
+ step((generator = generator.apply(__this, __arguments)).next());
51
+ });
52
+ };
53
+ var parseGraphQLRequest_exports = {};
54
+ __export(parseGraphQLRequest_exports, {
55
+ parseDocumentNode: () => parseDocumentNode,
56
+ parseGraphQLRequest: () => parseGraphQLRequest
57
+ });
58
+ module.exports = __toCommonJS(parseGraphQLRequest_exports);
59
+ var import_graphql = require("graphql");
60
+ var import_getPublicUrlFromRequest = require("../request/getPublicUrlFromRequest.js");
61
+ var import_devUtils = require("./devUtils.js");
62
+ var import_jsonParse = require("./jsonParse.js");
63
+ var import_parseMultipartData = require("./parseMultipartData.js");
64
+ function parseDocumentNode(node) {
65
+ var _a;
66
+ const operationDef = node.definitions.find((definition) => {
67
+ return definition.kind === "OperationDefinition";
68
+ });
69
+ return {
70
+ operationType: operationDef == null ? void 0 : operationDef.operation,
71
+ operationName: (_a = operationDef == null ? void 0 : operationDef.name) == null ? void 0 : _a.value
72
+ };
73
+ }
74
+ function parseQuery(query) {
75
+ try {
76
+ const ast = (0, import_graphql.parse)(query);
77
+ return parseDocumentNode(ast);
78
+ } catch (error) {
79
+ return error;
80
+ }
81
+ }
82
+ function extractMultipartVariables(variables, map, files) {
83
+ const operations = { variables };
84
+ for (const [key, pathArray] of Object.entries(map)) {
85
+ if (!(key in files)) {
86
+ throw new Error(`Given files do not have a key '${key}' .`);
87
+ }
88
+ for (const dotPath of pathArray) {
89
+ const [lastPath, ...reversedPaths] = dotPath.split(".").reverse();
90
+ const paths = reversedPaths.reverse();
91
+ let target = operations;
92
+ for (const path of paths) {
93
+ if (!(path in target)) {
94
+ throw new Error(`Property '${paths}' is not in operations.`);
95
+ }
96
+ target = target[path];
97
+ }
98
+ target[lastPath] = files[key];
99
+ }
100
+ }
101
+ return operations.variables;
102
+ }
103
+ function getGraphQLInput(request) {
104
+ return __async(this, null, function* () {
105
+ var _a;
106
+ switch (request.method) {
107
+ case "GET": {
108
+ const url = new URL(request.url);
109
+ const query = url.searchParams.get("query");
110
+ const variables = url.searchParams.get("variables") || "";
111
+ return {
112
+ query,
113
+ variables: (0, import_jsonParse.jsonParse)(variables)
114
+ };
115
+ }
116
+ case "POST": {
117
+ const requestClone = request.clone();
118
+ if ((_a = request.headers.get("content-type")) == null ? void 0 : _a.includes("multipart/form-data")) {
119
+ const responseJson = (0, import_parseMultipartData.parseMultipartData)(
120
+ yield requestClone.text(),
121
+ request.headers
122
+ );
123
+ if (!responseJson) {
124
+ return null;
125
+ }
126
+ const _b = responseJson, { operations, map } = _b, files = __objRest(_b, ["operations", "map"]);
127
+ const parsedOperations = (0, import_jsonParse.jsonParse)(
128
+ operations
129
+ ) || {};
130
+ if (!parsedOperations.query) {
131
+ return null;
132
+ }
133
+ const parsedMap = (0, import_jsonParse.jsonParse)(map || "") || {};
134
+ const variables = parsedOperations.variables ? extractMultipartVariables(
135
+ parsedOperations.variables,
136
+ parsedMap,
137
+ files
138
+ ) : {};
139
+ return {
140
+ query: parsedOperations.query,
141
+ variables
142
+ };
143
+ }
144
+ const requestJson = yield requestClone.json().catch(() => null);
145
+ if (requestJson == null ? void 0 : requestJson.query) {
146
+ const { query, variables } = requestJson;
147
+ return {
148
+ query,
149
+ variables
150
+ };
151
+ }
152
+ }
153
+ default:
154
+ return null;
155
+ }
156
+ });
157
+ }
158
+ function parseGraphQLRequest(request) {
159
+ return __async(this, null, function* () {
160
+ const input = yield getGraphQLInput(request);
161
+ if (!input || !input.query) {
162
+ return;
163
+ }
164
+ const { query, variables } = input;
165
+ const parsedResult = parseQuery(query);
166
+ if (parsedResult instanceof Error) {
167
+ const requestPublicUrl = (0, import_getPublicUrlFromRequest.getPublicUrlFromRequest)(request);
168
+ throw new Error(
169
+ import_devUtils.devUtils.formatMessage(
170
+ 'Failed to intercept a GraphQL request to "%s %s": cannot parse query. See the error message from the parser below.\n\n%s',
171
+ request.method,
172
+ requestPublicUrl,
173
+ parsedResult.message
174
+ )
175
+ );
176
+ }
177
+ return {
178
+ query: input.query,
179
+ operationType: parsedResult.operationType,
180
+ operationName: parsedResult.operationName,
181
+ variables
182
+ };
183
+ });
184
+ }
@@ -0,0 +1,165 @@
1
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
2
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
3
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
4
+ var __objRest = (source, exclude) => {
5
+ var target = {};
6
+ for (var prop in source)
7
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
8
+ target[prop] = source[prop];
9
+ if (source != null && __getOwnPropSymbols)
10
+ for (var prop of __getOwnPropSymbols(source)) {
11
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
12
+ target[prop] = source[prop];
13
+ }
14
+ return target;
15
+ };
16
+ var __async = (__this, __arguments, generator) => {
17
+ return new Promise((resolve, reject) => {
18
+ var fulfilled = (value) => {
19
+ try {
20
+ step(generator.next(value));
21
+ } catch (e) {
22
+ reject(e);
23
+ }
24
+ };
25
+ var rejected = (value) => {
26
+ try {
27
+ step(generator.throw(value));
28
+ } catch (e) {
29
+ reject(e);
30
+ }
31
+ };
32
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
33
+ step((generator = generator.apply(__this, __arguments)).next());
34
+ });
35
+ };
36
+ import { parse } from "graphql";
37
+ import { getPublicUrlFromRequest } from '../request/getPublicUrlFromRequest.mjs';
38
+ import { devUtils } from './devUtils.mjs';
39
+ import { jsonParse } from './jsonParse.mjs';
40
+ import { parseMultipartData } from './parseMultipartData.mjs';
41
+ function parseDocumentNode(node) {
42
+ var _a;
43
+ const operationDef = node.definitions.find((definition) => {
44
+ return definition.kind === "OperationDefinition";
45
+ });
46
+ return {
47
+ operationType: operationDef == null ? void 0 : operationDef.operation,
48
+ operationName: (_a = operationDef == null ? void 0 : operationDef.name) == null ? void 0 : _a.value
49
+ };
50
+ }
51
+ function parseQuery(query) {
52
+ try {
53
+ const ast = parse(query);
54
+ return parseDocumentNode(ast);
55
+ } catch (error) {
56
+ return error;
57
+ }
58
+ }
59
+ function extractMultipartVariables(variables, map, files) {
60
+ const operations = { variables };
61
+ for (const [key, pathArray] of Object.entries(map)) {
62
+ if (!(key in files)) {
63
+ throw new Error(`Given files do not have a key '${key}' .`);
64
+ }
65
+ for (const dotPath of pathArray) {
66
+ const [lastPath, ...reversedPaths] = dotPath.split(".").reverse();
67
+ const paths = reversedPaths.reverse();
68
+ let target = operations;
69
+ for (const path of paths) {
70
+ if (!(path in target)) {
71
+ throw new Error(`Property '${paths}' is not in operations.`);
72
+ }
73
+ target = target[path];
74
+ }
75
+ target[lastPath] = files[key];
76
+ }
77
+ }
78
+ return operations.variables;
79
+ }
80
+ function getGraphQLInput(request) {
81
+ return __async(this, null, function* () {
82
+ var _a;
83
+ switch (request.method) {
84
+ case "GET": {
85
+ const url = new URL(request.url);
86
+ const query = url.searchParams.get("query");
87
+ const variables = url.searchParams.get("variables") || "";
88
+ return {
89
+ query,
90
+ variables: jsonParse(variables)
91
+ };
92
+ }
93
+ case "POST": {
94
+ const requestClone = request.clone();
95
+ if ((_a = request.headers.get("content-type")) == null ? void 0 : _a.includes("multipart/form-data")) {
96
+ const responseJson = parseMultipartData(
97
+ yield requestClone.text(),
98
+ request.headers
99
+ );
100
+ if (!responseJson) {
101
+ return null;
102
+ }
103
+ const _b = responseJson, { operations, map } = _b, files = __objRest(_b, ["operations", "map"]);
104
+ const parsedOperations = jsonParse(
105
+ operations
106
+ ) || {};
107
+ if (!parsedOperations.query) {
108
+ return null;
109
+ }
110
+ const parsedMap = jsonParse(map || "") || {};
111
+ const variables = parsedOperations.variables ? extractMultipartVariables(
112
+ parsedOperations.variables,
113
+ parsedMap,
114
+ files
115
+ ) : {};
116
+ return {
117
+ query: parsedOperations.query,
118
+ variables
119
+ };
120
+ }
121
+ const requestJson = yield requestClone.json().catch(() => null);
122
+ if (requestJson == null ? void 0 : requestJson.query) {
123
+ const { query, variables } = requestJson;
124
+ return {
125
+ query,
126
+ variables
127
+ };
128
+ }
129
+ }
130
+ default:
131
+ return null;
132
+ }
133
+ });
134
+ }
135
+ function parseGraphQLRequest(request) {
136
+ return __async(this, null, function* () {
137
+ const input = yield getGraphQLInput(request);
138
+ if (!input || !input.query) {
139
+ return;
140
+ }
141
+ const { query, variables } = input;
142
+ const parsedResult = parseQuery(query);
143
+ if (parsedResult instanceof Error) {
144
+ const requestPublicUrl = getPublicUrlFromRequest(request);
145
+ throw new Error(
146
+ devUtils.formatMessage(
147
+ 'Failed to intercept a GraphQL request to "%s %s": cannot parse query. See the error message from the parser below.\n\n%s',
148
+ request.method,
149
+ requestPublicUrl,
150
+ parsedResult.message
151
+ )
152
+ );
153
+ }
154
+ return {
155
+ query: input.query,
156
+ operationType: parsedResult.operationType,
157
+ operationName: parsedResult.operationName,
158
+ variables
159
+ };
160
+ });
161
+ }
162
+ export {
163
+ parseDocumentNode,
164
+ parseGraphQLRequest
165
+ };
@@ -0,0 +1,10 @@
1
+ import { d as DefaultRequestMultipartBody } from '../../RequestHandler-1a5dbdc7.js';
2
+ import '../../typeUtils.js';
3
+
4
+ /**
5
+ * Parses a given string as a multipart/form-data.
6
+ * Does not throw an exception on an invalid multipart string.
7
+ */
8
+ declare function parseMultipartData<T extends DefaultRequestMultipartBody>(data: string, headers?: Headers): T | undefined;
9
+
10
+ export { parseMultipartData };