msw 2.0.14 → 2.1.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.
Files changed (133) hide show
  1. package/cli/index.js +11 -3
  2. package/cli/init.js +126 -73
  3. package/config/scripts/postinstall.js +8 -20
  4. package/lib/browser/index.js +111 -176
  5. package/lib/browser/index.js.map +1 -1
  6. package/lib/browser/index.mjs +111 -179
  7. package/lib/browser/index.mjs.map +1 -1
  8. package/lib/core/{GraphQLHandler-JB4bsrLF.d.ts → GraphQLHandler-LZ8jH42-.d.ts} +1 -1
  9. package/lib/core/{GraphQLHandler-mT-MmgeB.d.mts → GraphQLHandler-c0pfLv8e.d.mts} +1 -1
  10. package/lib/core/HttpResponse.d.mts +1 -1
  11. package/lib/core/HttpResponse.d.ts +1 -1
  12. package/lib/core/{RequestHandler-rmY-HeFN.d.ts → RequestHandler-SdFwV297.d.ts} +8 -1
  13. package/lib/core/{RequestHandler-YiqamK0M.d.mts → RequestHandler-hEwneHZE.d.mts} +8 -1
  14. package/lib/core/SetupApi.d.mts +1 -1
  15. package/lib/core/SetupApi.d.ts +1 -1
  16. package/lib/core/SetupApi.js +5 -0
  17. package/lib/core/SetupApi.js.map +1 -1
  18. package/lib/core/SetupApi.mjs +5 -0
  19. package/lib/core/SetupApi.mjs.map +1 -1
  20. package/lib/core/delay.js +24 -46
  21. package/lib/core/delay.js.map +1 -1
  22. package/lib/core/delay.mjs +24 -46
  23. package/lib/core/delay.mjs.map +1 -1
  24. package/lib/core/graphql.d.mts +2 -2
  25. package/lib/core/graphql.d.ts +2 -2
  26. package/lib/core/graphql.js +3 -19
  27. package/lib/core/graphql.js.map +1 -1
  28. package/lib/core/graphql.mjs +3 -21
  29. package/lib/core/graphql.mjs.map +1 -1
  30. package/lib/core/handlers/GraphQLHandler.d.mts +2 -2
  31. package/lib/core/handlers/GraphQLHandler.d.ts +2 -2
  32. package/lib/core/handlers/GraphQLHandler.js +51 -77
  33. package/lib/core/handlers/GraphQLHandler.js.map +1 -1
  34. package/lib/core/handlers/GraphQLHandler.mjs +51 -77
  35. package/lib/core/handlers/GraphQLHandler.mjs.map +1 -1
  36. package/lib/core/handlers/HttpHandler.d.mts +1 -1
  37. package/lib/core/handlers/HttpHandler.d.ts +1 -1
  38. package/lib/core/handlers/HttpHandler.js +29 -55
  39. package/lib/core/handlers/HttpHandler.js.map +1 -1
  40. package/lib/core/handlers/HttpHandler.mjs +29 -55
  41. package/lib/core/handlers/HttpHandler.mjs.map +1 -1
  42. package/lib/core/handlers/RequestHandler.d.mts +1 -1
  43. package/lib/core/handlers/RequestHandler.d.ts +1 -1
  44. package/lib/core/handlers/RequestHandler.js +79 -103
  45. package/lib/core/handlers/RequestHandler.js.map +1 -1
  46. package/lib/core/handlers/RequestHandler.mjs +79 -105
  47. package/lib/core/handlers/RequestHandler.mjs.map +1 -1
  48. package/lib/core/http.d.mts +1 -1
  49. package/lib/core/http.d.ts +1 -1
  50. package/lib/core/index.d.mts +2 -2
  51. package/lib/core/index.d.ts +2 -2
  52. package/lib/core/sharedOptions.d.mts +1 -1
  53. package/lib/core/sharedOptions.d.ts +1 -1
  54. package/lib/core/utils/HttpResponse/decorators.d.mts +1 -1
  55. package/lib/core/utils/HttpResponse/decorators.d.ts +1 -1
  56. package/lib/core/utils/HttpResponse/decorators.js +7 -24
  57. package/lib/core/utils/HttpResponse/decorators.js.map +1 -1
  58. package/lib/core/utils/HttpResponse/decorators.mjs +7 -26
  59. package/lib/core/utils/HttpResponse/decorators.mjs.map +1 -1
  60. package/lib/core/utils/getResponse.d.mts +1 -1
  61. package/lib/core/utils/getResponse.d.ts +1 -1
  62. package/lib/core/utils/getResponse.js +11 -26
  63. package/lib/core/utils/getResponse.js.map +1 -1
  64. package/lib/core/utils/getResponse.mjs +11 -26
  65. package/lib/core/utils/getResponse.mjs.map +1 -1
  66. package/lib/core/utils/handleRequest.d.mts +1 -1
  67. package/lib/core/utils/handleRequest.d.ts +1 -1
  68. package/lib/core/utils/handleRequest.js +49 -72
  69. package/lib/core/utils/handleRequest.js.map +1 -1
  70. package/lib/core/utils/handleRequest.mjs +49 -72
  71. package/lib/core/utils/handleRequest.mjs.map +1 -1
  72. package/lib/core/utils/internal/Disposable.js +3 -27
  73. package/lib/core/utils/internal/Disposable.js.map +1 -1
  74. package/lib/core/utils/internal/Disposable.mjs +3 -27
  75. package/lib/core/utils/internal/Disposable.mjs.map +1 -1
  76. package/lib/core/utils/internal/parseGraphQLRequest.d.mts +2 -2
  77. package/lib/core/utils/internal/parseGraphQLRequest.d.ts +2 -2
  78. package/lib/core/utils/internal/parseGraphQLRequest.js +72 -112
  79. package/lib/core/utils/internal/parseGraphQLRequest.js.map +1 -1
  80. package/lib/core/utils/internal/parseGraphQLRequest.mjs +72 -113
  81. package/lib/core/utils/internal/parseGraphQLRequest.mjs.map +1 -1
  82. package/lib/core/utils/internal/parseMultipartData.d.mts +1 -1
  83. package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
  84. package/lib/core/utils/internal/parseMultipartData.js +3 -4
  85. package/lib/core/utils/internal/parseMultipartData.js.map +1 -1
  86. package/lib/core/utils/internal/parseMultipartData.mjs +3 -4
  87. package/lib/core/utils/internal/parseMultipartData.mjs.map +1 -1
  88. package/lib/core/utils/internal/requestHandlerUtils.d.mts +1 -1
  89. package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
  90. package/lib/core/utils/internal/tryCatch.js +1 -1
  91. package/lib/core/utils/internal/tryCatch.js.map +1 -1
  92. package/lib/core/utils/internal/tryCatch.mjs +1 -1
  93. package/lib/core/utils/internal/tryCatch.mjs.map +1 -1
  94. package/lib/core/utils/logging/serializeRequest.js +9 -31
  95. package/lib/core/utils/logging/serializeRequest.js.map +1 -1
  96. package/lib/core/utils/logging/serializeRequest.mjs +9 -31
  97. package/lib/core/utils/logging/serializeRequest.mjs.map +1 -1
  98. package/lib/core/utils/logging/serializeResponse.js +11 -33
  99. package/lib/core/utils/logging/serializeResponse.js.map +1 -1
  100. package/lib/core/utils/logging/serializeResponse.mjs +11 -33
  101. package/lib/core/utils/logging/serializeResponse.mjs.map +1 -1
  102. package/lib/core/utils/request/getRequestCookies.js +9 -18
  103. package/lib/core/utils/request/getRequestCookies.js.map +1 -1
  104. package/lib/core/utils/request/getRequestCookies.mjs +9 -20
  105. package/lib/core/utils/request/getRequestCookies.mjs.map +1 -1
  106. package/lib/core/utils/request/onUnhandledRequest.d.mts +1 -1
  107. package/lib/core/utils/request/onUnhandledRequest.d.ts +1 -1
  108. package/lib/core/utils/request/onUnhandledRequest.js +62 -84
  109. package/lib/core/utils/request/onUnhandledRequest.js.map +1 -1
  110. package/lib/core/utils/request/onUnhandledRequest.mjs +62 -84
  111. package/lib/core/utils/request/onUnhandledRequest.mjs.map +1 -1
  112. package/lib/core/utils/request/readResponseCookies.js +1 -18
  113. package/lib/core/utils/request/readResponseCookies.js.map +1 -1
  114. package/lib/core/utils/request/readResponseCookies.mjs +1 -20
  115. package/lib/core/utils/request/readResponseCookies.mjs.map +1 -1
  116. package/lib/iife/index.js +678 -958
  117. package/lib/iife/index.js.map +1 -1
  118. package/lib/mockServiceWorker.js +2 -2
  119. package/lib/native/index.js +5 -23
  120. package/lib/native/index.js.map +1 -1
  121. package/lib/native/index.mjs +5 -24
  122. package/lib/native/index.mjs.map +1 -1
  123. package/lib/node/index.js +5 -23
  124. package/lib/node/index.js.map +1 -1
  125. package/lib/node/index.mjs +5 -24
  126. package/lib/node/index.mjs.map +1 -1
  127. package/package.json +32 -37
  128. package/src/browser/setupWorker/setupWorker.ts +6 -0
  129. package/src/browser/setupWorker/start/createStartHandler.ts +0 -8
  130. package/src/core/handlers/RequestHandler.ts +23 -4
  131. package/src/core/utils/getResponse.ts +12 -6
  132. package/src/core/utils/handleRequest.test.ts +20 -0
  133. package/src/core/utils/handleRequest.ts +4 -3
@@ -2,13 +2,13 @@
2
2
  /* tslint:disable */
3
3
 
4
4
  /**
5
- * Mock Service Worker (2.0.14).
5
+ * Mock Service Worker (2.1.0).
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.
9
9
  */
10
10
 
11
- const INTEGRITY_CHECKSUM = 'c5f7f8e188b673ea4e677df7ea3c5a39'
11
+ const INTEGRITY_CHECKSUM = '223d191a56023cd36aa88c802961b911'
12
12
  const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
13
13
  const activeClientIds = new Set()
14
14
 
@@ -16,26 +16,6 @@ var __copyProps = (to, from, except, desc) => {
16
16
  return to;
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var __async = (__this, __arguments, generator) => {
20
- return new Promise((resolve, reject) => {
21
- var fulfilled = (value) => {
22
- try {
23
- step(generator.next(value));
24
- } catch (e) {
25
- reject(e);
26
- }
27
- };
28
- var rejected = (value) => {
29
- try {
30
- step(generator.throw(value));
31
- } catch (e) {
32
- reject(e);
33
- }
34
- };
35
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
36
- step((generator = generator.apply(__this, __arguments)).next());
37
- });
38
- };
39
19
 
40
20
  // src/native/index.ts
41
21
  var native_exports = {};
@@ -56,6 +36,8 @@ var DEFAULT_LISTEN_OPTIONS = {
56
36
  onUnhandledRequest: "warn"
57
37
  };
58
38
  var SetupServerApi = class extends import_SetupApi.SetupApi {
39
+ interceptor;
40
+ resolvedOptions;
59
41
  constructor(interceptors, ...handlers) {
60
42
  super(...handlers);
61
43
  this.interceptor = new import_interceptors.BatchInterceptor({
@@ -69,8 +51,8 @@ var SetupServerApi = class extends import_SetupApi.SetupApi {
69
51
  * Subscribe to all requests that are using the interceptor object
70
52
  */
71
53
  init() {
72
- this.interceptor.on("request", (_0) => __async(this, [_0], function* ({ request, requestId }) {
73
- const response = yield (0, import_handleRequest.handleRequest)(
54
+ this.interceptor.on("request", async ({ request, requestId }) => {
55
+ const response = await (0, import_handleRequest.handleRequest)(
74
56
  request,
75
57
  requestId,
76
58
  this.currentHandlers,
@@ -81,7 +63,7 @@ var SetupServerApi = class extends import_SetupApi.SetupApi {
81
63
  request.respondWith(response);
82
64
  }
83
65
  return;
84
- }));
66
+ });
85
67
  this.interceptor.on(
86
68
  "response",
87
69
  ({ response, isMockedResponse, request, requestId }) => {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/native/index.ts","../../src/node/SetupServerApi.ts"],"sourcesContent":["import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { SetupServerApi } from '../node/SetupServerApi'\n\n/**\n * Sets up a requests interception in React Native with the given request handlers.\n * @param {RequestHandler[]} handlers List of request handlers.\n *\n * @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}\n */\nexport function setupServer(\n ...handlers: Array<RequestHandler>\n): SetupServerApi {\n // Provision request interception via patching the `XMLHttpRequest` class only\n // in React Native. There is no `http`/`https` modules in that environment.\n return new SetupServerApi([XMLHttpRequestInterceptor], ...handlers)\n}\n","import {\n BatchInterceptor,\n HttpRequestEventMap,\n Interceptor,\n InterceptorReadyState,\n} from '@mswjs/interceptors'\nimport { invariant } from 'outvariant'\nimport { SetupApi } from '~/core/SetupApi'\nimport { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { RequiredDeep } from '~/core/typeUtils'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport { devUtils } from '~/core/utils/internal/devUtils'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { SetupServer } from './glossary'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServer\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{\n new (): Interceptor<HttpRequestEventMap>\n }>,\n ...handlers: Array<RequestHandler>\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n\n this.init()\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on('request', async ({ request, requestId }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.currentHandlers,\n this.resolvedOptions,\n this.emitter,\n )\n\n if (response) {\n request.respondWith(response)\n }\n\n return\n })\n\n this.interceptor.on(\n 'response',\n ({ response, isMockedResponse, request, requestId }) => {\n this.emitter.emit(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n response,\n request,\n requestId,\n },\n )\n },\n )\n }\n\n public listen(options: Partial<SharedOptions> = {}): void {\n this.resolvedOptions = mergeRight(\n DEFAULT_LISTEN_OPTIONS,\n options,\n ) as RequiredDeep<SharedOptions>\n\n // Apply the interceptor when starting the server.\n this.interceptor.apply()\n\n this.subscriptions.push(() => {\n this.interceptor.dispose()\n })\n\n // Assert that the interceptor has been applied successfully.\n // Also guards us from forgetting to call \"interceptor.apply()\"\n // as a part of the \"listen\" method.\n invariant(\n [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(\n this.interceptor.readyState,\n ),\n devUtils.formatMessage(\n 'Failed to start \"setupServer\": the interceptor failed to apply. This is likely an issue with the library and you should report it at \"%s\".',\n ),\n 'https://github.com/mswjs/msw/issues/new/choose',\n )\n }\n\n public close(): void {\n this.dispose()\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAA0C;;;ACA1C,0BAKO;AACP,wBAA0B;AAC1B,sBAAyB;AAIzB,2BAA8B;AAC9B,sBAAyB;AACzB,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ACF;AAAA,EAEO,QAAc;AACnB,SAAK,QAAQ;AAAA,EACf;AACF;;;ADtGO,SAAS,eACX,UACa;AAGhB,SAAO,IAAI,eAAe,CAAC,+CAAyB,GAAG,GAAG,QAAQ;AACpE;","names":["Interceptor"]}
1
+ {"version":3,"sources":["../../src/native/index.ts","../../src/node/SetupServerApi.ts"],"sourcesContent":["import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { SetupServerApi } from '../node/SetupServerApi'\n\n/**\n * Sets up a requests interception in React Native with the given request handlers.\n * @param {RequestHandler[]} handlers List of request handlers.\n *\n * @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}\n */\nexport function setupServer(\n ...handlers: Array<RequestHandler>\n): SetupServerApi {\n // Provision request interception via patching the `XMLHttpRequest` class only\n // in React Native. There is no `http`/`https` modules in that environment.\n return new SetupServerApi([XMLHttpRequestInterceptor], ...handlers)\n}\n","import {\n BatchInterceptor,\n HttpRequestEventMap,\n Interceptor,\n InterceptorReadyState,\n} from '@mswjs/interceptors'\nimport { invariant } from 'outvariant'\nimport { SetupApi } from '~/core/SetupApi'\nimport { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { RequiredDeep } from '~/core/typeUtils'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport { devUtils } from '~/core/utils/internal/devUtils'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { SetupServer } from './glossary'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServer\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{\n new (): Interceptor<HttpRequestEventMap>\n }>,\n ...handlers: Array<RequestHandler>\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n\n this.init()\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on('request', async ({ request, requestId }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.currentHandlers,\n this.resolvedOptions,\n this.emitter,\n )\n\n if (response) {\n request.respondWith(response)\n }\n\n return\n })\n\n this.interceptor.on(\n 'response',\n ({ response, isMockedResponse, request, requestId }) => {\n this.emitter.emit(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n response,\n request,\n requestId,\n },\n )\n },\n )\n }\n\n public listen(options: Partial<SharedOptions> = {}): void {\n this.resolvedOptions = mergeRight(\n DEFAULT_LISTEN_OPTIONS,\n options,\n ) as RequiredDeep<SharedOptions>\n\n // Apply the interceptor when starting the server.\n this.interceptor.apply()\n\n this.subscriptions.push(() => {\n this.interceptor.dispose()\n })\n\n // Assert that the interceptor has been applied successfully.\n // Also guards us from forgetting to call \"interceptor.apply()\"\n // as a part of the \"listen\" method.\n invariant(\n [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(\n this.interceptor.readyState,\n ),\n devUtils.formatMessage(\n 'Failed to start \"setupServer\": the interceptor failed to apply. This is likely an issue with the library and you should report it at \"%s\".',\n ),\n 'https://github.com/mswjs/msw/issues/new/choose',\n )\n }\n\n public close(): void {\n this.dispose()\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAA0C;;;ACA1C,0BAKO;AACP,wBAA0B;AAC1B,sBAAyB;AAIzB,2BAA8B;AAC9B,sBAAyB;AACzB,wBAA2B;AAG3B,IAAM,yBAAsD;AAAA,EAC1D,oBAAoB;AACtB;AAEO,IAAM,iBA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a;AAGhB,SAAO,IAAI,eAAe,CAAC,+CAAyB,GAAG,GAAG,QAAQ;AACpE;","names":["Interceptor"]}
@@ -1,24 +1,3 @@
1
- var __async = (__this, __arguments, generator) => {
2
- return new Promise((resolve, reject) => {
3
- var fulfilled = (value) => {
4
- try {
5
- step(generator.next(value));
6
- } catch (e) {
7
- reject(e);
8
- }
9
- };
10
- var rejected = (value) => {
11
- try {
12
- step(generator.throw(value));
13
- } catch (e) {
14
- reject(e);
15
- }
16
- };
17
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
- step((generator = generator.apply(__this, __arguments)).next());
19
- });
20
- };
21
-
22
1
  // src/native/index.ts
23
2
  import { XMLHttpRequestInterceptor } from "@mswjs/interceptors/XMLHttpRequest";
24
3
 
@@ -36,6 +15,8 @@ var DEFAULT_LISTEN_OPTIONS = {
36
15
  onUnhandledRequest: "warn"
37
16
  };
38
17
  var SetupServerApi = class extends SetupApi {
18
+ interceptor;
19
+ resolvedOptions;
39
20
  constructor(interceptors, ...handlers) {
40
21
  super(...handlers);
41
22
  this.interceptor = new BatchInterceptor({
@@ -49,8 +30,8 @@ var SetupServerApi = class extends SetupApi {
49
30
  * Subscribe to all requests that are using the interceptor object
50
31
  */
51
32
  init() {
52
- this.interceptor.on("request", (_0) => __async(this, [_0], function* ({ request, requestId }) {
53
- const response = yield handleRequest(
33
+ this.interceptor.on("request", async ({ request, requestId }) => {
34
+ const response = await handleRequest(
54
35
  request,
55
36
  requestId,
56
37
  this.currentHandlers,
@@ -61,7 +42,7 @@ var SetupServerApi = class extends SetupApi {
61
42
  request.respondWith(response);
62
43
  }
63
44
  return;
64
- }));
45
+ });
65
46
  this.interceptor.on(
66
47
  "response",
67
48
  ({ response, isMockedResponse, request, requestId }) => {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/native/index.ts","../../src/node/SetupServerApi.ts"],"sourcesContent":["import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { SetupServerApi } from '../node/SetupServerApi'\n\n/**\n * Sets up a requests interception in React Native with the given request handlers.\n * @param {RequestHandler[]} handlers List of request handlers.\n *\n * @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}\n */\nexport function setupServer(\n ...handlers: Array<RequestHandler>\n): SetupServerApi {\n // Provision request interception via patching the `XMLHttpRequest` class only\n // in React Native. There is no `http`/`https` modules in that environment.\n return new SetupServerApi([XMLHttpRequestInterceptor], ...handlers)\n}\n","import {\n BatchInterceptor,\n HttpRequestEventMap,\n Interceptor,\n InterceptorReadyState,\n} from '@mswjs/interceptors'\nimport { invariant } from 'outvariant'\nimport { SetupApi } from '~/core/SetupApi'\nimport { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { RequiredDeep } from '~/core/typeUtils'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport { devUtils } from '~/core/utils/internal/devUtils'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { SetupServer } from './glossary'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServer\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{\n new (): Interceptor<HttpRequestEventMap>\n }>,\n ...handlers: Array<RequestHandler>\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n\n this.init()\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on('request', async ({ request, requestId }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.currentHandlers,\n this.resolvedOptions,\n this.emitter,\n )\n\n if (response) {\n request.respondWith(response)\n }\n\n return\n })\n\n this.interceptor.on(\n 'response',\n ({ response, isMockedResponse, request, requestId }) => {\n this.emitter.emit(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n response,\n request,\n requestId,\n },\n )\n },\n )\n }\n\n public listen(options: Partial<SharedOptions> = {}): void {\n this.resolvedOptions = mergeRight(\n DEFAULT_LISTEN_OPTIONS,\n options,\n ) as RequiredDeep<SharedOptions>\n\n // Apply the interceptor when starting the server.\n this.interceptor.apply()\n\n this.subscriptions.push(() => {\n this.interceptor.dispose()\n })\n\n // Assert that the interceptor has been applied successfully.\n // Also guards us from forgetting to call \"interceptor.apply()\"\n // as a part of the \"listen\" method.\n invariant(\n [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(\n this.interceptor.readyState,\n ),\n devUtils.formatMessage(\n 'Failed to start \"setupServer\": the interceptor failed to apply. This is likely an issue with the library and you should report it at \"%s\".',\n ),\n 'https://github.com/mswjs/msw/issues/new/choose',\n )\n }\n\n public close(): void {\n this.dispose()\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,iCAAiC;;;ACA1C;AAAA,EACE;AAAA,EAGA;AAAA,OACK;AACP,SAAS,iBAAiB;AAC1B,SAAS,gBAAgB;AAIzB,SAAS,qBAAqB;AAC9B,SAAS,gBAAgB;AACzB,SAAS,kBAAkB;AAG3B,IAAM,yBAAsD;AAAA,EAC1D,oBAAoB;AACtB;AAEO,IAAM,iBAAN,cACG,SAEV;AAAA,EAOE,YACE,iBAGG,UACH;AACA,UAAM,GAAG,QAAQ;AAEjB,SAAK,cAAc,IAAI,iBAAiB;AAAA,MACtC,MAAM;AAAA,MACN,cAAc,aAAa,IAAI,CAACA,iBAAgB,IAAIA,aAAY,CAAC;AAAA,IACnE,CAAC;AACD,SAAK,kBAAkB,CAAC;AAExB,SAAK,KAAK;AAAA,EACZ;AAAA;AAAA;AAAA;AAAA,EAKQ,OAAa;AACnB,SAAK,YAAY,GAAG,WAAW,CAAO,OAA2B,eAA3B,KAA2B,WAA3B,EAAE,SAAS,UAAU,GAAM;AAC/D,YAAM,WAAW,MAAM;AAAA,QACrB;AAAA,QACA;AAAA,QACA,KAAK;AAAA,QACL,KAAK;AAAA,QACL,KAAK;AAAA,MACP;AAEA,UAAI,UAAU;AACZ,gBAAQ,YAAY,QAAQ;AAAA,MAC9B;AAEA;AAAA,IACF,EAAC;AAED,SAAK,YAAY;AAAA,MACf;AAAA,MACA,CAAC,EAAE,UAAU,kBAAkB,SAAS,UAAU,MAAM;AACtD,aAAK,QAAQ;AAAA,UACX,mBAAmB,oBAAoB;AAAA,UACvC;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEO,OAAO,UAAkC,CAAC,GAAS;AACxD,SAAK,kBAAkB;AAAA,MACrB;AAAA,MACA;AAAA,IACF;AAGA,SAAK,YAAY,MAAM;AAEvB,SAAK,cAAc,KAAK,MAAM;AAC5B,WAAK,YAAY,QAAQ;AAAA,IAC3B,CAAC;AAKD;AAAA,MACE,CAAC,sBAAsB,UAAU,sBAAsB,OAAO,EAAE;AAAA,QAC9D,KAAK,YAAY;AAAA,MACnB;AAAA,MACA,SAAS;AAAA,QACP;AAAA,MACF;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAAA,EAEO,QAAc;AACnB,SAAK,QAAQ;AAAA,EACf;AACF;;;ADtGO,SAAS,eACX,UACa;AAGhB,SAAO,IAAI,eAAe,CAAC,yBAAyB,GAAG,GAAG,QAAQ;AACpE;","names":["Interceptor"]}
1
+ {"version":3,"sources":["../../src/native/index.ts","../../src/node/SetupServerApi.ts"],"sourcesContent":["import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { SetupServerApi } from '../node/SetupServerApi'\n\n/**\n * Sets up a requests interception in React Native with the given request handlers.\n * @param {RequestHandler[]} handlers List of request handlers.\n *\n * @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}\n */\nexport function setupServer(\n ...handlers: Array<RequestHandler>\n): SetupServerApi {\n // Provision request interception via patching the `XMLHttpRequest` class only\n // in React Native. There is no `http`/`https` modules in that environment.\n return new SetupServerApi([XMLHttpRequestInterceptor], ...handlers)\n}\n","import {\n BatchInterceptor,\n HttpRequestEventMap,\n Interceptor,\n InterceptorReadyState,\n} from '@mswjs/interceptors'\nimport { invariant } from 'outvariant'\nimport { SetupApi } from '~/core/SetupApi'\nimport { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { RequiredDeep } from '~/core/typeUtils'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport { devUtils } from '~/core/utils/internal/devUtils'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { SetupServer } from './glossary'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServer\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{\n new (): Interceptor<HttpRequestEventMap>\n }>,\n ...handlers: Array<RequestHandler>\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n\n this.init()\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on('request', async ({ request, requestId }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.currentHandlers,\n this.resolvedOptions,\n this.emitter,\n )\n\n if (response) {\n request.respondWith(response)\n }\n\n return\n })\n\n this.interceptor.on(\n 'response',\n ({ response, isMockedResponse, request, requestId }) => {\n this.emitter.emit(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n response,\n request,\n requestId,\n },\n )\n },\n )\n }\n\n public listen(options: Partial<SharedOptions> = {}): void {\n this.resolvedOptions = mergeRight(\n DEFAULT_LISTEN_OPTIONS,\n options,\n ) as RequiredDeep<SharedOptions>\n\n // Apply the interceptor when starting the server.\n this.interceptor.apply()\n\n this.subscriptions.push(() => {\n this.interceptor.dispose()\n })\n\n // Assert that the interceptor has been applied successfully.\n // Also guards us from forgetting to call \"interceptor.apply()\"\n // as a part of the \"listen\" method.\n invariant(\n [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(\n this.interceptor.readyState,\n ),\n devUtils.formatMessage(\n 'Failed to start \"setupServer\": the interceptor failed to apply. This is likely an issue with the library and you should report it at \"%s\".',\n ),\n 'https://github.com/mswjs/msw/issues/new/choose',\n )\n }\n\n public close(): void {\n this.dispose()\n }\n}\n"],"mappings":";AAAA,SAAS,iCAAiC;;;ACA1C;AAAA,EACE;AAAA,EAGA;AAAA,OACK;AACP,SAAS,iBAAiB;AAC1B,SAAS,gBAAgB;AAIzB,SAAS,qBAAqB;AAC9B,SAAS,gBAAgB;AACzB,SAAS,kBAAkB;AAG3B,IAAM,yBAAsD;AAAA,EAC1D,oBAAoB;AACtB;AAEO,IAAM,iBAAN,cACG,SAEV;AAAA,EACqB;AAAA,EAIX;AAAA,EAER,YACE,iBAGG,UACH;AACA,UAAM,GAAG,QAAQ;AAEjB,SAAK,cAAc,IAAI,iBAAiB;AAAA,MACtC,MAAM;AAAA,MACN,cAAc,aAAa,IAAI,CAACA,iBAAgB,IAAIA,aAAY,CAAC;AAAA,IACnE,CAAC;AACD,SAAK,kBAAkB,CAAC;AAExB,SAAK,KAAK;AAAA,EACZ;AAAA;AAAA;AAAA;AAAA,EAKQ,OAAa;AACnB,SAAK,YAAY,GAAG,WAAW,OAAO,EAAE,SAAS,UAAU,MAAM;AAC/D,YAAM,WAAW,MAAM;AAAA,QACrB;AAAA,QACA;AAAA,QACA,KAAK;AAAA,QACL,KAAK;AAAA,QACL,KAAK;AAAA,MACP;AAEA,UAAI,UAAU;AACZ,gBAAQ,YAAY,QAAQ;AAAA,MAC9B;AAEA;AAAA,IACF,CAAC;AAED,SAAK,YAAY;AAAA,MACf;AAAA,MACA,CAAC,EAAE,UAAU,kBAAkB,SAAS,UAAU,MAAM;AACtD,aAAK,QAAQ;AAAA,UACX,mBAAmB,oBAAoB;AAAA,UACvC;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEO,OAAO,UAAkC,CAAC,GAAS;AACxD,SAAK,kBAAkB;AAAA,MACrB;AAAA,MACA;AAAA,IACF;AAGA,SAAK,YAAY,MAAM;AAEvB,SAAK,cAAc,KAAK,MAAM;AAC5B,WAAK,YAAY,QAAQ;AAAA,IAC3B,CAAC;AAKD;AAAA,MACE,CAAC,sBAAsB,UAAU,sBAAsB,OAAO,EAAE;AAAA,QAC9D,KAAK,YAAY;AAAA,MACnB;AAAA,MACA,SAAS;AAAA,QACP;AAAA,MACF;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAAA,EAEO,QAAc;AACnB,SAAK,QAAQ;AAAA,EACf;AACF;;;ADtGO,SAAS,eACX,UACa;AAGhB,SAAO,IAAI,eAAe,CAAC,yBAAyB,GAAG,GAAG,QAAQ;AACpE;","names":["Interceptor"]}
package/lib/node/index.js CHANGED
@@ -16,26 +16,6 @@ var __copyProps = (to, from, except, desc) => {
16
16
  return to;
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var __async = (__this, __arguments, generator) => {
20
- return new Promise((resolve, reject) => {
21
- var fulfilled = (value) => {
22
- try {
23
- step(generator.next(value));
24
- } catch (e) {
25
- reject(e);
26
- }
27
- };
28
- var rejected = (value) => {
29
- try {
30
- step(generator.throw(value));
31
- } catch (e) {
32
- reject(e);
33
- }
34
- };
35
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
36
- step((generator = generator.apply(__this, __arguments)).next());
37
- });
38
- };
39
19
 
40
20
  // src/node/index.ts
41
21
  var node_exports = {};
@@ -56,6 +36,8 @@ var DEFAULT_LISTEN_OPTIONS = {
56
36
  onUnhandledRequest: "warn"
57
37
  };
58
38
  var SetupServerApi = class extends import_SetupApi.SetupApi {
39
+ interceptor;
40
+ resolvedOptions;
59
41
  constructor(interceptors, ...handlers) {
60
42
  super(...handlers);
61
43
  this.interceptor = new import_interceptors.BatchInterceptor({
@@ -69,8 +51,8 @@ var SetupServerApi = class extends import_SetupApi.SetupApi {
69
51
  * Subscribe to all requests that are using the interceptor object
70
52
  */
71
53
  init() {
72
- this.interceptor.on("request", (_0) => __async(this, [_0], function* ({ request, requestId }) {
73
- const response = yield (0, import_handleRequest.handleRequest)(
54
+ this.interceptor.on("request", async ({ request, requestId }) => {
55
+ const response = await (0, import_handleRequest.handleRequest)(
74
56
  request,
75
57
  requestId,
76
58
  this.currentHandlers,
@@ -81,7 +63,7 @@ var SetupServerApi = class extends import_SetupApi.SetupApi {
81
63
  request.respondWith(response);
82
64
  }
83
65
  return;
84
- }));
66
+ });
85
67
  this.interceptor.on(
86
68
  "response",
87
69
  ({ response, isMockedResponse, request, requestId }) => {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/node/index.ts","../../src/node/SetupServerApi.ts","../../src/node/setupServer.ts"],"sourcesContent":["export type { SetupServer } from './glossary'\nexport { SetupServerApi } from './SetupServerApi'\nexport { setupServer } from './setupServer'\n","import {\n BatchInterceptor,\n HttpRequestEventMap,\n Interceptor,\n InterceptorReadyState,\n} from '@mswjs/interceptors'\nimport { invariant } from 'outvariant'\nimport { SetupApi } from '~/core/SetupApi'\nimport { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { RequiredDeep } from '~/core/typeUtils'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport { devUtils } from '~/core/utils/internal/devUtils'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { SetupServer } from './glossary'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServer\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{\n new (): Interceptor<HttpRequestEventMap>\n }>,\n ...handlers: Array<RequestHandler>\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n\n this.init()\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on('request', async ({ request, requestId }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.currentHandlers,\n this.resolvedOptions,\n this.emitter,\n )\n\n if (response) {\n request.respondWith(response)\n }\n\n return\n })\n\n this.interceptor.on(\n 'response',\n ({ response, isMockedResponse, request, requestId }) => {\n this.emitter.emit(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n response,\n request,\n requestId,\n },\n )\n },\n )\n }\n\n public listen(options: Partial<SharedOptions> = {}): void {\n this.resolvedOptions = mergeRight(\n DEFAULT_LISTEN_OPTIONS,\n options,\n ) as RequiredDeep<SharedOptions>\n\n // Apply the interceptor when starting the server.\n this.interceptor.apply()\n\n this.subscriptions.push(() => {\n this.interceptor.dispose()\n })\n\n // Assert that the interceptor has been applied successfully.\n // Also guards us from forgetting to call \"interceptor.apply()\"\n // as a part of the \"listen\" method.\n invariant(\n [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(\n this.interceptor.readyState,\n ),\n devUtils.formatMessage(\n 'Failed to start \"setupServer\": the interceptor failed to apply. This is likely an issue with the library and you should report it at \"%s\".',\n ),\n 'https://github.com/mswjs/msw/issues/new/choose',\n )\n }\n\n public close(): void {\n this.dispose()\n }\n}\n","import { ClientRequestInterceptor } from '@mswjs/interceptors/ClientRequest'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { SetupServerApi } from './SetupServerApi'\nimport { SetupServer } from './glossary'\n\n/**\n * Sets up a requests interception in Node.js with the given request handlers.\n * @param {RequestHandler[]} handlers List of request handlers.\n *\n * @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}\n */\nexport const setupServer = (\n ...handlers: Array<RequestHandler>\n): SetupServer => {\n return new SetupServerApi(\n [ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],\n ...handlers,\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,0BAKO;AACP,wBAA0B;AAC1B,sBAAyB;AAIzB,2BAA8B;AAC9B,sBAAyB;AACzB,wBAA2B;AAG3B,IAAM,yBAAsD;AAAA,EAC1D,oBAAoB;AACtB;AAEO,IAAM,iBAAN,cACG,yBAEV;AAAA,EAOE,YACE,iBAGG,UACH;AACA,UAAM,GAAG,QAAQ;AAEjB,SAAK,cAAc,IAAI,qCAAiB;AAAA,MACtC,MAAM;AAAA,MACN,cAAc,aAAa,IAAI,CAACA,iBAAgB,IAAIA,aAAY,CAAC;AAAA,IACnE,CAAC;AACD,SAAK,kBAAkB,CAAC;AAExB,SAAK,KAAK;AAAA,EACZ;AAAA;AAAA;AAAA;AAAA,EAKQ,OAAa;AACnB,SAAK,YAAY,GAAG,WAAW,CAAO,OAA2B,eAA3B,KAA2B,WAA3B,EAAE,SAAS,UAAU,GAAM;AAC/D,YAAM,WAAW,UAAM;AAAA,QACrB;AAAA,QACA;AAAA,QACA,KAAK;AAAA,QACL,KAAK;AAAA,QACL,KAAK;AAAA,MACP;AAEA,UAAI,UAAU;AACZ,gBAAQ,YAAY,QAAQ;AAAA,MAC9B;AAEA;AAAA,IACF,EAAC;AAED,SAAK,YAAY;AAAA,MACf;AAAA,MACA,CAAC,EAAE,UAAU,kBAAkB,SAAS,UAAU,MAAM;AACtD,aAAK,QAAQ;AAAA,UACX,mBAAmB,oBAAoB;AAAA,UACvC;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEO,OAAO,UAAkC,CAAC,GAAS;AACxD,SAAK,sBAAkB;AAAA,MACrB;AAAA,MACA;AAAA,IACF;AAGA,SAAK,YAAY,MAAM;AAEvB,SAAK,cAAc,KAAK,MAAM;AAC5B,WAAK,YAAY,QAAQ;AAAA,IAC3B,CAAC;AAKD;AAAA,MACE,CAAC,0CAAsB,UAAU,0CAAsB,OAAO,EAAE;AAAA,QAC9D,KAAK,YAAY;AAAA,MACnB;AAAA,MACA,yBAAS;AAAA,QACP;AAAA,MACF;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAAA,EAEO,QAAc;AACnB,SAAK,QAAQ;AAAA,EACf;AACF;;;AChHA,2BAAyC;AACzC,4BAA0C;AAC1C,mBAAiC;AAW1B,IAAM,cAAc,IACtB,aACa;AAChB,SAAO,IAAI;AAAA,IACT,CAAC,+CAA0B,iDAA2B,6BAAgB;AAAA,IACtE,GAAG;AAAA,EACL;AACF;","names":["Interceptor"]}
1
+ {"version":3,"sources":["../../src/node/index.ts","../../src/node/SetupServerApi.ts","../../src/node/setupServer.ts"],"sourcesContent":["export type { SetupServer } from './glossary'\nexport { SetupServerApi } from './SetupServerApi'\nexport { setupServer } from './setupServer'\n","import {\n BatchInterceptor,\n HttpRequestEventMap,\n Interceptor,\n InterceptorReadyState,\n} from '@mswjs/interceptors'\nimport { invariant } from 'outvariant'\nimport { SetupApi } from '~/core/SetupApi'\nimport { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { RequiredDeep } from '~/core/typeUtils'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport { devUtils } from '~/core/utils/internal/devUtils'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { SetupServer } from './glossary'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServer\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{\n new (): Interceptor<HttpRequestEventMap>\n }>,\n ...handlers: Array<RequestHandler>\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n\n this.init()\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on('request', async ({ request, requestId }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.currentHandlers,\n this.resolvedOptions,\n this.emitter,\n )\n\n if (response) {\n request.respondWith(response)\n }\n\n return\n })\n\n this.interceptor.on(\n 'response',\n ({ response, isMockedResponse, request, requestId }) => {\n this.emitter.emit(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n response,\n request,\n requestId,\n },\n )\n },\n )\n }\n\n public listen(options: Partial<SharedOptions> = {}): void {\n this.resolvedOptions = mergeRight(\n DEFAULT_LISTEN_OPTIONS,\n options,\n ) as RequiredDeep<SharedOptions>\n\n // Apply the interceptor when starting the server.\n this.interceptor.apply()\n\n this.subscriptions.push(() => {\n this.interceptor.dispose()\n })\n\n // Assert that the interceptor has been applied successfully.\n // Also guards us from forgetting to call \"interceptor.apply()\"\n // as a part of the \"listen\" method.\n invariant(\n [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(\n this.interceptor.readyState,\n ),\n devUtils.formatMessage(\n 'Failed to start \"setupServer\": the interceptor failed to apply. This is likely an issue with the library and you should report it at \"%s\".',\n ),\n 'https://github.com/mswjs/msw/issues/new/choose',\n )\n }\n\n public close(): void {\n this.dispose()\n }\n}\n","import { ClientRequestInterceptor } from '@mswjs/interceptors/ClientRequest'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { SetupServerApi } from './SetupServerApi'\nimport { SetupServer } from './glossary'\n\n/**\n * Sets up a requests interception in Node.js with the given request handlers.\n * @param {RequestHandler[]} handlers List of request handlers.\n *\n * @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}\n */\nexport const setupServer = (\n ...handlers: Array<RequestHandler>\n): SetupServer => {\n return new SetupServerApi(\n [ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],\n ...handlers,\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,0BAKO;AACP,wBAA0B;AAC1B,sBAAyB;AAIzB,2BAA8B;AAC9B,sBAAyB;AACzB,wBAA2B;AAG3B,IAAM,yBAAsD;AAAA,EAC1D,oBAAoB;AACtB;AAEO,IAAM,iBAAN,cACG,yBAEV;AAAA,EACqB;AAAA,EAIX;AAAA,EAER,YACE,iBAGG,UACH;AACA,UAAM,GAAG,QAAQ;AAEjB,SAAK,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,6BAAgB;AAAA,IACtE,GAAG;AAAA,EACL;AACF;","names":["Interceptor"]}
@@ -1,24 +1,3 @@
1
- var __async = (__this, __arguments, generator) => {
2
- return new Promise((resolve, reject) => {
3
- var fulfilled = (value) => {
4
- try {
5
- step(generator.next(value));
6
- } catch (e) {
7
- reject(e);
8
- }
9
- };
10
- var rejected = (value) => {
11
- try {
12
- step(generator.throw(value));
13
- } catch (e) {
14
- reject(e);
15
- }
16
- };
17
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
- step((generator = generator.apply(__this, __arguments)).next());
19
- });
20
- };
21
-
22
1
  // src/node/SetupServerApi.ts
23
2
  import {
24
3
  BatchInterceptor,
@@ -33,6 +12,8 @@ var DEFAULT_LISTEN_OPTIONS = {
33
12
  onUnhandledRequest: "warn"
34
13
  };
35
14
  var SetupServerApi = class extends SetupApi {
15
+ interceptor;
16
+ resolvedOptions;
36
17
  constructor(interceptors, ...handlers) {
37
18
  super(...handlers);
38
19
  this.interceptor = new BatchInterceptor({
@@ -46,8 +27,8 @@ var SetupServerApi = class extends SetupApi {
46
27
  * Subscribe to all requests that are using the interceptor object
47
28
  */
48
29
  init() {
49
- this.interceptor.on("request", (_0) => __async(this, [_0], function* ({ request, requestId }) {
50
- const response = yield handleRequest(
30
+ this.interceptor.on("request", async ({ request, requestId }) => {
31
+ const response = await handleRequest(
51
32
  request,
52
33
  requestId,
53
34
  this.currentHandlers,
@@ -58,7 +39,7 @@ var SetupServerApi = class extends SetupApi {
58
39
  request.respondWith(response);
59
40
  }
60
41
  return;
61
- }));
42
+ });
62
43
  this.interceptor.on(
63
44
  "response",
64
45
  ({ response, isMockedResponse, request, requestId }) => {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/node/SetupServerApi.ts","../../src/node/setupServer.ts"],"sourcesContent":["import {\n BatchInterceptor,\n HttpRequestEventMap,\n Interceptor,\n InterceptorReadyState,\n} from '@mswjs/interceptors'\nimport { invariant } from 'outvariant'\nimport { SetupApi } from '~/core/SetupApi'\nimport { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { RequiredDeep } from '~/core/typeUtils'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport { devUtils } from '~/core/utils/internal/devUtils'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { SetupServer } from './glossary'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServer\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{\n new (): Interceptor<HttpRequestEventMap>\n }>,\n ...handlers: Array<RequestHandler>\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n\n this.init()\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on('request', async ({ request, requestId }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.currentHandlers,\n this.resolvedOptions,\n this.emitter,\n )\n\n if (response) {\n request.respondWith(response)\n }\n\n return\n })\n\n this.interceptor.on(\n 'response',\n ({ response, isMockedResponse, request, requestId }) => {\n this.emitter.emit(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n response,\n request,\n requestId,\n },\n )\n },\n )\n }\n\n public listen(options: Partial<SharedOptions> = {}): void {\n this.resolvedOptions = mergeRight(\n DEFAULT_LISTEN_OPTIONS,\n options,\n ) as RequiredDeep<SharedOptions>\n\n // Apply the interceptor when starting the server.\n this.interceptor.apply()\n\n this.subscriptions.push(() => {\n this.interceptor.dispose()\n })\n\n // Assert that the interceptor has been applied successfully.\n // Also guards us from forgetting to call \"interceptor.apply()\"\n // as a part of the \"listen\" method.\n invariant(\n [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(\n this.interceptor.readyState,\n ),\n devUtils.formatMessage(\n 'Failed to start \"setupServer\": the interceptor failed to apply. This is likely an issue with the library and you should report it at \"%s\".',\n ),\n 'https://github.com/mswjs/msw/issues/new/choose',\n )\n }\n\n public close(): void {\n this.dispose()\n }\n}\n","import { ClientRequestInterceptor } from '@mswjs/interceptors/ClientRequest'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { SetupServerApi } from './SetupServerApi'\nimport { SetupServer } from './glossary'\n\n/**\n * Sets up a requests interception in Node.js with the given request handlers.\n * @param {RequestHandler[]} handlers List of request handlers.\n *\n * @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}\n */\nexport const setupServer = (\n ...handlers: Array<RequestHandler>\n): SetupServer => {\n return new SetupServerApi(\n [ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],\n ...handlers,\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA,EACE;AAAA,EAGA;AAAA,OACK;AACP,SAAS,iBAAiB;AAC1B,SAAS,gBAAgB;AAIzB,SAAS,qBAAqB;AAC9B,SAAS,gBAAgB;AACzB,SAAS,kBAAkB;AAG3B,IAAM,yBAAsD;AAAA,EAC1D,oBAAoB;AACtB;AAEO,IAAM,iBAAN,cACG,SAEV;AAAA,EAOE,YACE,iBAGG,UACH;AACA,UAAM,GAAG,QAAQ;AAEjB,SAAK,cAAc,IAAI,iBAAiB;AAAA,MACtC,MAAM;AAAA,MACN,cAAc,aAAa,IAAI,CAACA,iBAAgB,IAAIA,aAAY,CAAC;AAAA,IACnE,CAAC;AACD,SAAK,kBAAkB,CAAC;AAExB,SAAK,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ts"],"sourcesContent":["import {\n BatchInterceptor,\n HttpRequestEventMap,\n Interceptor,\n InterceptorReadyState,\n} from '@mswjs/interceptors'\nimport { invariant } from 'outvariant'\nimport { SetupApi } from '~/core/SetupApi'\nimport { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { RequiredDeep } from '~/core/typeUtils'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport { devUtils } from '~/core/utils/internal/devUtils'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { SetupServer } from './glossary'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServer\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{\n new (): Interceptor<HttpRequestEventMap>\n }>,\n ...handlers: Array<RequestHandler>\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n\n this.init()\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on('request', async ({ request, requestId }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.currentHandlers,\n this.resolvedOptions,\n this.emitter,\n )\n\n if (response) {\n request.respondWith(response)\n }\n\n return\n })\n\n this.interceptor.on(\n 'response',\n ({ response, isMockedResponse, request, requestId }) => {\n this.emitter.emit(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n response,\n request,\n requestId,\n },\n )\n },\n )\n }\n\n public listen(options: Partial<SharedOptions> = {}): void {\n this.resolvedOptions = mergeRight(\n DEFAULT_LISTEN_OPTIONS,\n options,\n ) as RequiredDeep<SharedOptions>\n\n // Apply the interceptor when starting the server.\n this.interceptor.apply()\n\n this.subscriptions.push(() => {\n this.interceptor.dispose()\n })\n\n // Assert that the interceptor has been applied successfully.\n // Also guards us from forgetting to call \"interceptor.apply()\"\n // as a part of the \"listen\" method.\n invariant(\n [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(\n this.interceptor.readyState,\n ),\n devUtils.formatMessage(\n 'Failed to start \"setupServer\": the interceptor failed to apply. This is likely an issue with the library and you should report it at \"%s\".',\n ),\n 'https://github.com/mswjs/msw/issues/new/choose',\n )\n }\n\n public close(): void {\n this.dispose()\n }\n}\n","import { ClientRequestInterceptor } from '@mswjs/interceptors/ClientRequest'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { SetupServerApi } from './SetupServerApi'\nimport { SetupServer } from './glossary'\n\n/**\n * Sets up a requests interception in Node.js with the given request handlers.\n * @param {RequestHandler[]} handlers List of request handlers.\n *\n * @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}\n */\nexport const setupServer = (\n ...handlers: Array<RequestHandler>\n): SetupServer => {\n return new SetupServerApi(\n [ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],\n ...handlers,\n )\n}\n"],"mappings":";AAAA;AAAA,EACE;AAAA,EAGA;AAAA,OACK;AACP,SAAS,iBAAiB;AAC1B,SAAS,gBAAgB;AAIzB,SAAS,qBAAqB;AAC9B,SAAS,gBAAgB;AACzB,SAAS,kBAAkB;AAG3B,IAAM,yBAAsD;AAAA,EAC1D,oBAAoB;AACtB;AAEO,IAAM,iBAAN,cACG,SAEV;AAAA,EACqB;AAAA,EAIX;AAAA,EAER,YACE,iBAGG,UACH;AACA,UAAM,GAAG,QAAQ;AAEjB,SAAK,cAAc,IAAI,iBAAiB;AAAA,MACtC,MAAM;AAAA,MACN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A,EACL;AACF;","names":["Interceptor"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "msw",
3
- "version": "2.0.14",
3
+ "version": "2.1.0",
4
4
  "description": "Seamless REST/GraphQL API mocking library for browser and Node.js.",
5
5
  "main": "./lib/core/index.js",
6
6
  "module": "./lib/core/index.mjs",
@@ -98,73 +98,68 @@
98
98
  "@mswjs/cookies": "^1.1.0",
99
99
  "@mswjs/interceptors": "^0.25.14",
100
100
  "@open-draft/until": "^2.1.0",
101
- "@types/cookie": "^0.4.1",
102
- "@types/js-levenshtein": "^1.1.1",
103
- "@types/statuses": "^2.0.1",
101
+ "@types/cookie": "^0.6.0",
102
+ "@types/js-levenshtein": "^1.1.3",
103
+ "@types/statuses": "^2.0.4",
104
104
  "chalk": "^4.1.2",
105
105
  "chokidar": "^3.4.2",
106
106
  "graphql": "^16.8.1",
107
- "headers-polyfill": "^4.0.1",
107
+ "headers-polyfill": "^4.0.2",
108
108
  "inquirer": "^8.2.0",
109
109
  "is-node-process": "^1.2.0",
110
110
  "js-levenshtein": "^1.1.6",
111
- "outvariant": "^1.4.0",
111
+ "outvariant": "^1.4.2",
112
112
  "path-to-regexp": "^6.2.0",
113
- "strict-event-emitter": "^0.5.0",
114
- "type-fest": "^2.19.0",
115
- "yargs": "^17.3.1"
113
+ "strict-event-emitter": "^0.5.1",
114
+ "type-fest": "^4.9.0",
115
+ "yargs": "^17.7.2"
116
116
  },
117
117
  "devDependencies": {
118
- "@babel/core": "^7.17.2",
119
- "@babel/preset-env": "^7.16.11",
120
- "@commitlint/cli": "^16.1.0",
121
- "@commitlint/config-conventional": "^16.0.0",
118
+ "@commitlint/cli": "^18.4.4",
119
+ "@commitlint/config-conventional": "^18.4.4",
122
120
  "@open-draft/test-server": "^0.4.2",
123
121
  "@ossjs/release": "^0.8.0",
124
- "@playwright/test": "^1.30.0",
125
- "@swc/core": "^1.3.35",
126
- "@types/express": "^4.17.17",
127
- "@types/fs-extra": "^9.0.13",
122
+ "@playwright/test": "^1.40.1",
123
+ "@swc/core": "^1.3.102",
124
+ "@types/express": "^4.17.21",
125
+ "@types/fs-extra": "^11.0.4",
128
126
  "@types/glob": "^8.1.0",
129
- "@types/json-bigint": "^1.0.1",
127
+ "@types/json-bigint": "^1.0.4",
130
128
  "@types/node": "18.x",
131
- "@types/puppeteer": "^5.4.4",
132
129
  "@typescript-eslint/eslint-plugin": "^5.11.0",
133
130
  "@typescript-eslint/parser": "^5.11.0",
134
131
  "@web/dev-server": "^0.1.38",
135
- "axios": "^1.6.1",
136
- "babel-loader": "^8.2.3",
132
+ "axios": "^1.6.5",
137
133
  "babel-minify": "^0.5.1",
138
134
  "commitizen": "^4.2.4",
139
135
  "cross-env": "^7.0.3",
140
- "cross-fetch": "^3.1.5",
136
+ "cross-fetch": "^4.0.0",
141
137
  "cz-conventional-changelog": "3.3.0",
142
138
  "esbuild": "^0.19.11",
143
- "esbuild-loader": "^2.21.0",
144
- "eslint": "^7.30.0",
145
- "eslint-config-prettier": "^8.3.0",
146
- "eslint-plugin-prettier": "^3.4.0",
139
+ "esbuild-loader": "^4.0.2",
140
+ "eslint": "^8.56.0",
141
+ "eslint-config-prettier": "^9.1.0",
142
+ "eslint-plugin-prettier": "^5.1.3",
147
143
  "express": "^4.18.2",
148
- "fs-extra": "^10.0.0",
144
+ "fs-extra": "^11.2.0",
149
145
  "fs-teardown": "^0.3.0",
150
- "glob": "^9.3.4",
151
- "jsdom": "^22.1.0",
146
+ "glob": "^10.3.10",
147
+ "jsdom": "^23.2.0",
152
148
  "json-bigint": "^1.0.0",
153
- "lint-staged": "^13.0.3",
149
+ "lint-staged": "^15.2.0",
154
150
  "page-with": "^0.6.1",
155
- "prettier": "^2.7.1",
156
- "regenerator-runtime": "^0.13.9",
157
- "rimraf": "^3.0.2",
158
- "simple-git-hooks": "^2.8.0",
159
- "ts-node": "^10.9.1",
151
+ "prettier": "^3.2.2",
152
+ "regenerator-runtime": "^0.14.1",
153
+ "rimraf": "^5.0.5",
154
+ "simple-git-hooks": "^2.9.0",
155
+ "ts-node": "^10.9.2",
160
156
  "tsup": "^8.0.1",
161
157
  "typescript": "^5.0.2",
162
158
  "undici": "^5.20.0",
163
159
  "url-loader": "^4.1.1",
164
160
  "vitest": "^0.34.6",
165
161
  "vitest-environment-miniflare": "^2.14.1",
166
- "webpack": "^5.68.0",
167
- "webpack-dev-server": "^3.11.2",
162
+ "webpack": "^5.89.0",
168
163
  "webpack-http-server": "^0.5.0"
169
164
  },
170
165
  "peerDependencies": {
@@ -172,6 +172,12 @@ export class SetupWorkerApi
172
172
  }
173
173
 
174
174
  public async start(options: StartOptions = {}): StartReturnType {
175
+ if (options.waitUntilReady === true) {
176
+ devUtils.warn(
177
+ 'The "waitUntilReady" option has been deprecated. Please remove it from this "worker.start()" call. Follow the recommended Browser integration (https://mswjs.io/docs/integrations/browser) to eliminate any race conditions between the Service Worker registration and any requests made by your application on initial render.',
178
+ )
179
+ }
180
+
175
181
  this.context.startOptions = mergeRight(
176
182
  DEFAULT_START_OPTIONS,
177
183
  options,
@@ -5,7 +5,6 @@ import { enableMocking } from './utils/enableMocking'
5
5
  import { SetupWorkerInternalContext, StartHandler } from '../glossary'
6
6
  import { createRequestListener } from './createRequestListener'
7
7
  import { requestIntegrityCheck } from '../../utils/requestIntegrityCheck'
8
- import { deferNetworkRequestsUntil } from '../../utils/deferNetworkRequestsUntil'
9
8
  import { createResponseListener } from './createResponseListener'
10
9
  import { validateWorkerScope } from './utils/validateWorkerScope'
11
10
 
@@ -131,13 +130,6 @@ If this message still persists after updating, please report an issue: https://g
131
130
  },
132
131
  )
133
132
 
134
- // Defer any network requests until the Service Worker instance is ready.
135
- // This prevents a race condition between the Service Worker registration
136
- // and application's runtime requests (i.e. requests on mount).
137
- if (options.waitUntilReady) {
138
- deferNetworkRequestsUntil(workerRegistration)
139
- }
140
-
141
133
  return workerRegistration
142
134
  }
143
135
  }
@@ -93,6 +93,7 @@ export interface RequestHandlerExecutionResult<
93
93
  handler: RequestHandler
94
94
  parsedResult?: ParsedResult
95
95
  request: Request
96
+ requestId: string
96
97
  response?: Response
97
98
  }
98
99
 
@@ -220,6 +221,7 @@ export abstract class RequestHandler<
220
221
  */
221
222
  public async run(args: {
222
223
  request: StrictRequest<any>
224
+ requestId: string
223
225
  resolutionContext?: ResponseResolutionContext
224
226
  }): Promise<RequestHandlerExecutionResult<ParsedResult> | null> {
225
227
  if (this.isUsed && this.options?.once) {
@@ -263,15 +265,30 @@ export abstract class RequestHandler<
263
265
  request: args.request,
264
266
  parsedResult,
265
267
  })
266
- const mockedResponse = (await executeResolver({
267
- ...resolverExtras,
268
- request: args.request,
269
- })) as Response
268
+
269
+ const mockedResponsePromise = (
270
+ executeResolver({
271
+ ...resolverExtras,
272
+ requestId: args.requestId,
273
+ request: args.request,
274
+ }) as Promise<Response>
275
+ ).catch((errorOrResponse) => {
276
+ // Allow throwing a Response instance in a response resolver.
277
+ if (errorOrResponse instanceof Response) {
278
+ return errorOrResponse
279
+ }
280
+
281
+ // Otherwise, throw the error as-is.
282
+ throw errorOrResponse
283
+ })
284
+
285
+ const mockedResponse = await mockedResponsePromise
270
286
 
271
287
  const executionResult = this.createExecutionResult({
272
288
  // Pass the cloned request to the result so that logging
273
289
  // and other consumers could read its body once more.
274
290
  request: requestClone,
291
+ requestId: args.requestId,
275
292
  response: mockedResponse,
276
293
  parsedResult,
277
294
  })
@@ -330,12 +347,14 @@ export abstract class RequestHandler<
330
347
 
331
348
  private createExecutionResult(args: {
332
349
  request: Request
350
+ requestId: string
333
351
  parsedResult: ParsedResult
334
352
  response?: Response
335
353
  }): RequestHandlerExecutionResult<ParsedResult> {
336
354
  return {
337
355
  handler: this,
338
356
  request: args.request,
357
+ requestId: args.requestId,
339
358
  response: args.response,
340
359
  parsedResult: args.parsedResult,
341
360
  }
@@ -16,16 +16,22 @@ export interface ResponseResolutionContext {
16
16
  /**
17
17
  * Returns a mocked response for a given request using following request handlers.
18
18
  */
19
- export const getResponse = async <Handler extends Array<RequestHandler>>(
20
- request: Request,
21
- handlers: Handler,
22
- resolutionContext?: ResponseResolutionContext,
23
- ): Promise<ResponseLookupResult | null> => {
19
+ export const getResponse = async <Handler extends Array<RequestHandler>>({
20
+ request,
21
+ requestId,
22
+ handlers,
23
+ resolutionContext,
24
+ }: {
25
+ request: Request
26
+ requestId: string
27
+ handlers: Handler
28
+ resolutionContext?: ResponseResolutionContext
29
+ }): Promise<ResponseLookupResult | null> => {
24
30
  let matchingHandler: RequestHandler | null = null
25
31
  let result: RequestHandlerExecutionResult<any> | null = null
26
32
 
27
33
  for (const handler of handlers) {
28
- result = await handler.run({ request, resolutionContext })
34
+ result = await handler.run({ request, requestId, resolutionContext })
29
35
 
30
36
  // If the handler produces some result for this request,
31
37
  // it automatically becomes matching.
@@ -355,6 +355,26 @@ it('returns undefined without warning on a passthrough request', async () => {
355
355
  expect(handleRequestOptions.onMockedResponse).not.toHaveBeenCalled()
356
356
  })
357
357
 
358
+ it('calls the handler with the requestId', async () => {
359
+ const { emitter } = setup()
360
+
361
+ const requestId = uuidv4()
362
+ const request = new Request(new URL('http://localhost/user'))
363
+ const handlerFn = vi.fn()
364
+ const handlers: Array<RequestHandler> = [http.get('/user', handlerFn)]
365
+
366
+ await handleRequest(
367
+ request,
368
+ requestId,
369
+ handlers,
370
+ options,
371
+ emitter,
372
+ handleRequestOptions,
373
+ )
374
+
375
+ expect(handlerFn).toHaveBeenCalledWith(expect.objectContaining({ requestId }))
376
+ })
377
+
358
378
  it('marks the first matching one-time handler as used', async () => {
359
379
  const { emitter } = setup()
360
380