msw 2.7.6 → 2.8.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 (116) hide show
  1. package/browser/package.json +28 -3
  2. package/cli/index.js +5 -3
  3. package/cli/init.js +13 -8
  4. package/cli/invariant.js +3 -2
  5. package/cli/package.json +3 -0
  6. package/config/constants.js +9 -13
  7. package/config/package.json +3 -0
  8. package/config/scripts/postinstall.js +3 -3
  9. package/lib/browser/index.d.mts +4 -4
  10. package/lib/browser/index.d.ts +4 -4
  11. package/lib/browser/index.js +25 -25
  12. package/lib/core/{GraphQLHandler-D1CSV926.d.ts → GraphQLHandler-DOfinX9J.d.ts} +1 -1
  13. package/lib/core/{GraphQLHandler-BNMGdWQe.d.mts → GraphQLHandler-GA1kyXQ7.d.mts} +1 -1
  14. package/lib/core/{HttpResponse-BRDnWbjc.d.mts → HttpResponse-Bls5AXtT.d.mts} +9 -9
  15. package/lib/core/{HttpResponse-Cy7ytzUn.d.ts → HttpResponse-DQOloMJy.d.ts} +9 -9
  16. package/lib/core/HttpResponse.d.mts +1 -1
  17. package/lib/core/HttpResponse.d.ts +1 -1
  18. package/lib/core/HttpResponse.js +3 -6
  19. package/lib/core/HttpResponse.js.map +1 -1
  20. package/lib/core/HttpResponse.mjs +2 -5
  21. package/lib/core/HttpResponse.mjs.map +1 -1
  22. package/lib/core/SetupApi.d.mts +1 -1
  23. package/lib/core/SetupApi.d.ts +1 -1
  24. package/lib/core/SetupApi.js +4 -4
  25. package/lib/core/getResponse.d.mts +1 -1
  26. package/lib/core/getResponse.d.ts +1 -1
  27. package/lib/core/getResponse.js +1 -1
  28. package/lib/core/graphql.d.mts +4 -4
  29. package/lib/core/graphql.d.ts +4 -4
  30. package/lib/core/graphql.js +2 -2
  31. package/lib/core/graphql.js.map +1 -1
  32. package/lib/core/graphql.mjs +1 -1
  33. package/lib/core/graphql.mjs.map +1 -1
  34. package/lib/core/handlers/GraphQLHandler.d.mts +2 -2
  35. package/lib/core/handlers/GraphQLHandler.d.ts +2 -2
  36. package/lib/core/handlers/GraphQLHandler.js +10 -10
  37. package/lib/core/handlers/HttpHandler.d.mts +2 -2
  38. package/lib/core/handlers/HttpHandler.d.ts +2 -2
  39. package/lib/core/handlers/HttpHandler.js +11 -11
  40. package/lib/core/handlers/RequestHandler.d.mts +1 -1
  41. package/lib/core/handlers/RequestHandler.d.ts +1 -1
  42. package/lib/core/handlers/RequestHandler.js +2 -2
  43. package/lib/core/handlers/RequestHandler.js.map +1 -1
  44. package/lib/core/handlers/RequestHandler.mjs.map +1 -1
  45. package/lib/core/handlers/WebSocketHandler.js +2 -2
  46. package/lib/core/http.d.mts +2 -2
  47. package/lib/core/http.d.ts +2 -2
  48. package/lib/core/http.js +1 -1
  49. package/lib/core/index.d.mts +2 -2
  50. package/lib/core/index.d.ts +2 -2
  51. package/lib/core/index.js +21 -21
  52. package/lib/core/index.js.map +1 -1
  53. package/lib/core/passthrough.d.mts +2 -2
  54. package/lib/core/passthrough.d.ts +2 -2
  55. package/lib/core/passthrough.js.map +1 -1
  56. package/lib/core/passthrough.mjs.map +1 -1
  57. package/lib/core/utils/HttpResponse/decorators.d.mts +1 -1
  58. package/lib/core/utils/HttpResponse/decorators.d.ts +1 -1
  59. package/lib/core/utils/HttpResponse/decorators.js +1 -1
  60. package/lib/core/utils/cookieStore.js +1 -1
  61. package/lib/core/utils/executeHandlers.d.mts +1 -1
  62. package/lib/core/utils/executeHandlers.d.ts +1 -1
  63. package/lib/core/utils/handleRequest.d.mts +1 -1
  64. package/lib/core/utils/handleRequest.d.ts +1 -1
  65. package/lib/core/utils/handleRequest.js +3 -3
  66. package/lib/core/utils/internal/checkGlobals.js +1 -1
  67. package/lib/core/utils/internal/isHandlerKind.d.mts +1 -1
  68. package/lib/core/utils/internal/isHandlerKind.d.ts +1 -1
  69. package/lib/core/utils/internal/mergeRight.js +1 -1
  70. package/lib/core/utils/internal/parseGraphQLRequest.d.mts +2 -2
  71. package/lib/core/utils/internal/parseGraphQLRequest.d.ts +2 -2
  72. package/lib/core/utils/internal/parseGraphQLRequest.js +4 -4
  73. package/lib/core/utils/internal/parseMultipartData.d.mts +1 -1
  74. package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
  75. package/lib/core/utils/internal/requestHandlerUtils.d.mts +1 -1
  76. package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
  77. package/lib/core/utils/logging/serializeResponse.js +1 -1
  78. package/lib/core/utils/matching/matchRequestUrl.js +1 -1
  79. package/lib/core/utils/matching/normalizePath.js +2 -2
  80. package/lib/core/utils/request/getRequestCookies.js +2 -2
  81. package/lib/core/utils/request/onUnhandledRequest.js +3 -3
  82. package/lib/core/utils/request/storeResponseCookies.js +2 -2
  83. package/lib/core/utils/url/getAbsoluteUrl.js +1 -1
  84. package/lib/core/ws/WebSocketClientManager.js +2 -2
  85. package/lib/core/ws/handleWebSocketEvent.d.mts +1 -1
  86. package/lib/core/ws/handleWebSocketEvent.d.ts +1 -1
  87. package/lib/core/ws/handleWebSocketEvent.js +4 -4
  88. package/lib/core/ws/utils/attachWebSocketLogger.js +5 -5
  89. package/lib/core/ws/utils/getPublicData.js +1 -1
  90. package/lib/core/ws.js +3 -3
  91. package/lib/iife/index.js +6 -9
  92. package/lib/iife/index.js.map +1 -1
  93. package/lib/mockServiceWorker.js +1 -1
  94. package/lib/native/index.d.mts +5 -7
  95. package/lib/native/index.d.ts +5 -7
  96. package/lib/native/index.js +12 -12
  97. package/lib/native/index.js.map +1 -1
  98. package/lib/native/index.mjs +2 -2
  99. package/lib/native/index.mjs.map +1 -1
  100. package/lib/node/index.d.mts +6 -8
  101. package/lib/node/index.d.ts +6 -8
  102. package/lib/node/index.js +17 -16
  103. package/lib/node/index.js.map +1 -1
  104. package/lib/node/index.mjs +7 -6
  105. package/lib/node/index.mjs.map +1 -1
  106. package/native/package.json +25 -1
  107. package/node/package.json +28 -1
  108. package/package.json +145 -47
  109. package/src/core/HttpResponse.ts +16 -18
  110. package/src/core/graphql.ts +1 -1
  111. package/src/core/handlers/RequestHandler.ts +5 -3
  112. package/src/core/passthrough.ts +3 -3
  113. package/src/native/index.ts +1 -1
  114. package/src/node/SetupServerApi.ts +10 -6
  115. package/src/node/SetupServerCommonApi.ts +2 -2
  116. package/src/package.json +3 -0
@@ -8,7 +8,7 @@
8
8
  * - Please do NOT serve this file on production.
9
9
  */
10
10
 
11
- const PACKAGE_VERSION = '2.7.6'
11
+ const PACKAGE_VERSION = '2.8.0'
12
12
  const INTEGRITY_CHECKSUM = '00729d72e3b82faf54ca8b9621dbb96f'
13
13
  const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
14
14
  const activeClientIds = new Set()
@@ -1,8 +1,8 @@
1
- import { RequestHandler } from "../core/handlers/RequestHandler";
1
+ import { RequestHandler } from "../core/handlers/RequestHandler.mjs";
2
2
  import { BatchInterceptor, Interceptor, HttpRequestEventMap } from '@mswjs/interceptors';
3
- import { SharedOptions, LifeCycleEventEmitter, LifeCycleEventsMap } from "../core/sharedOptions";
4
- import { SetupApi } from "../core/SetupApi";
5
- import { WebSocketHandler } from "../core/handlers/WebSocketHandler";
3
+ import { SharedOptions, LifeCycleEventEmitter, LifeCycleEventsMap } from "../core/sharedOptions.mjs";
4
+ import { SetupApi } from "../core/SetupApi.mjs";
5
+ import { WebSocketHandler } from "../core/handlers/WebSocketHandler.mjs";
6
6
  import { PartialDeep } from 'type-fest';
7
7
 
8
8
  interface SetupServerCommon {
@@ -54,9 +54,7 @@ interface SetupServerCommon {
54
54
  declare class SetupServerCommonApi extends SetupApi<LifeCycleEventsMap> implements SetupServerCommon {
55
55
  protected readonly interceptor: BatchInterceptor<Array<Interceptor<HttpRequestEventMap>>, HttpRequestEventMap>;
56
56
  private resolvedOptions;
57
- constructor(interceptors: Array<{
58
- new (): Interceptor<HttpRequestEventMap>;
59
- }>, handlers: Array<RequestHandler | WebSocketHandler>);
57
+ constructor(interceptors: Array<Interceptor<HttpRequestEventMap>>, handlers: Array<RequestHandler | WebSocketHandler>);
60
58
  /**
61
59
  * Subscribe to all requests that are using the interceptor object
62
60
  */
@@ -1,8 +1,8 @@
1
- import { RequestHandler } from "../core/handlers/RequestHandler";
1
+ import { RequestHandler } from "../core/handlers/RequestHandler.mjs";
2
2
  import { BatchInterceptor, Interceptor, HttpRequestEventMap } from '@mswjs/interceptors';
3
- import { SharedOptions, LifeCycleEventEmitter, LifeCycleEventsMap } from "../core/sharedOptions";
4
- import { SetupApi } from "../core/SetupApi";
5
- import { WebSocketHandler } from "../core/handlers/WebSocketHandler";
3
+ import { SharedOptions, LifeCycleEventEmitter, LifeCycleEventsMap } from "../core/sharedOptions.mjs";
4
+ import { SetupApi } from "../core/SetupApi.mjs";
5
+ import { WebSocketHandler } from "../core/handlers/WebSocketHandler.mjs";
6
6
  import { PartialDeep } from 'type-fest';
7
7
 
8
8
  interface SetupServerCommon {
@@ -54,9 +54,7 @@ interface SetupServerCommon {
54
54
  declare class SetupServerCommonApi extends SetupApi<LifeCycleEventsMap> implements SetupServerCommon {
55
55
  protected readonly interceptor: BatchInterceptor<Array<Interceptor<HttpRequestEventMap>>, HttpRequestEventMap>;
56
56
  private resolvedOptions;
57
- constructor(interceptors: Array<{
58
- new (): Interceptor<HttpRequestEventMap>;
59
- }>, handlers: Array<RequestHandler | WebSocketHandler>);
57
+ constructor(interceptors: Array<Interceptor<HttpRequestEventMap>>, handlers: Array<RequestHandler | WebSocketHandler>);
60
58
  /**
61
59
  * Subscribe to all requests that are using the interceptor object
62
60
  */
@@ -18,24 +18,24 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
20
  // src/native/index.ts
21
- var native_exports = {};
22
- __export(native_exports, {
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
23
  setupServer: () => setupServer
24
24
  });
25
- module.exports = __toCommonJS(native_exports);
25
+ module.exports = __toCommonJS(index_exports);
26
26
  var import_fetch = require("@mswjs/interceptors/fetch");
27
27
  var import_XMLHttpRequest = require("@mswjs/interceptors/XMLHttpRequest");
28
28
 
29
29
  // src/node/SetupServerCommonApi.ts
30
30
  var import_outvariant = require("outvariant");
31
31
  var import_interceptors = require("@mswjs/interceptors");
32
- var import_SetupApi = require("../core/SetupApi.js");
33
- var import_handleRequest = require("../core/utils/handleRequest.js");
34
- var import_mergeRight = require("../core/utils/internal/mergeRight.js");
35
- var import_devUtils = require("../core/utils/internal/devUtils.js");
36
- var import_handleWebSocketEvent = require("../core/ws/handleWebSocketEvent.js");
37
- var import_webSocketInterceptor = require("../core/ws/webSocketInterceptor.js");
38
- var import_isHandlerKind = require("../core/utils/internal/isHandlerKind.js");
32
+ var import_SetupApi = require("../core/SetupApi");
33
+ var import_handleRequest = require("../core/utils/handleRequest");
34
+ var import_mergeRight = require("../core/utils/internal/mergeRight");
35
+ var import_devUtils = require("../core/utils/internal/devUtils");
36
+ var import_handleWebSocketEvent = require("../core/ws/handleWebSocketEvent");
37
+ var import_webSocketInterceptor = require("../core/ws/webSocketInterceptor");
38
+ var import_isHandlerKind = require("../core/utils/internal/isHandlerKind");
39
39
  var DEFAULT_LISTEN_OPTIONS = {
40
40
  onUnhandledRequest: "warn"
41
41
  };
@@ -46,7 +46,7 @@ var SetupServerCommonApi = class extends import_SetupApi.SetupApi {
46
46
  super(...handlers);
47
47
  this.interceptor = new import_interceptors.BatchInterceptor({
48
48
  name: "setup-server",
49
- interceptors: interceptors.map((Interceptor) => new Interceptor())
49
+ interceptors
50
50
  });
51
51
  this.resolvedOptions = {};
52
52
  }
@@ -145,7 +145,7 @@ var SetupServerCommonApi = class extends import_SetupApi.SetupApi {
145
145
  // src/native/index.ts
146
146
  function setupServer(...handlers) {
147
147
  return new SetupServerCommonApi(
148
- [import_fetch.FetchInterceptor, import_XMLHttpRequest.XMLHttpRequestInterceptor],
148
+ [new import_fetch.FetchInterceptor(), new import_XMLHttpRequest.XMLHttpRequestInterceptor()],
149
149
  handlers
150
150
  );
151
151
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/native/index.ts","../../src/node/SetupServerCommonApi.ts"],"sourcesContent":["import { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { SetupServerCommonApi } from '../node/SetupServerCommonApi'\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): SetupServerCommonApi {\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 SetupServerCommonApi(\n [FetchInterceptor, XMLHttpRequestInterceptor],\n handlers,\n )\n}\n","/**\n * @note This API is extended by both \"msw/node\" and \"msw/native\"\n * so be minding about the things you import!\n */\nimport type { RequiredDeep } from 'type-fest'\nimport { invariant } from 'outvariant'\nimport {\n BatchInterceptor,\n InterceptorReadyState,\n type HttpRequestEventMap,\n type Interceptor,\n} from '@mswjs/interceptors'\nimport type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { SetupApi } from '~/core/SetupApi'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { InternalError, devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\nimport { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'\nimport { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'\nimport { isHandlerKind } from '~/core/utils/internal/isHandlerKind'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerCommonApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServerCommon\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{ new (): Interceptor<HttpRequestEventMap> }>,\n handlers: Array<RequestHandler | WebSocketHandler>,\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on(\n 'request',\n async ({ request, requestId, controller }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.handlersController\n .currentHandlers()\n .filter(isHandlerKind('RequestHandler')),\n this.resolvedOptions,\n this.emitter,\n {\n onPassthroughResponse(request) {\n const acceptHeader = request.headers.get('accept')\n\n /**\n * @note Remove the internal bypass request header.\n * In the browser, this is done by the worker script.\n * In Node.js, it has to be done here.\n */\n if (acceptHeader) {\n const nextAcceptHeader = acceptHeader.replace(\n /(,\\s+)?msw\\/passthrough/,\n '',\n )\n\n if (nextAcceptHeader) {\n request.headers.set('accept', nextAcceptHeader)\n } else {\n request.headers.delete('accept')\n }\n }\n },\n },\n )\n\n if (response) {\n controller.respondWith(response)\n }\n\n return\n },\n )\n\n this.interceptor.on('unhandledException', ({ error }) => {\n if (error instanceof InternalError) {\n throw error\n }\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 // Preconfigure the WebSocket interception but don't enable it just yet.\n // It will be enabled when the server starts.\n handleWebSocketEvent({\n getUnhandledRequestStrategy: () => {\n return this.resolvedOptions.onUnhandledRequest\n },\n getHandlers: () => {\n return this.handlersController.currentHandlers()\n },\n onMockedConnection: () => {},\n onPassthroughConnection: () => {},\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 // Attach the event listeners to the interceptor here\n // so they get re-attached whenever `.listen()` is called.\n this.interceptor.apply()\n this.init()\n this.subscriptions.push(() => this.interceptor.dispose())\n\n // Apply the WebSocket interception.\n webSocketInterceptor.apply()\n this.subscriptions.push(() => webSocketInterceptor.dispose())\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,mBAAiC;AACjC,4BAA0C;;;ACI1C,wBAA0B;AAC1B,0BAKO;AAEP,sBAAyB;AACzB,2BAA8B;AAG9B,wBAA2B;AAC3B,sBAAwC;AAExC,kCAAqC;AACrC,kCAAqC;AACrC,2BAA8B;AAE9B,IAAM,yBAAsD;AAAA,EAC1D,oBAAoB;AACtB;AAEO,IAAM,uBAAN,cACG,yBAEV;AAAA,EACqB;AAAA,EAIX;AAAA,EAER,YACE,cACA,UACA;AACA,UAAM,GAAG,QAAQ;AAEjB,SAAK,cAAc,IAAI,qCAAiB;AAAA,MACtC,MAAM;AAAA,MACN,cAAc,aAAa,IAAI,CAAC,gBAAgB,IAAI,YAAY,CAAC;AAAA,IACnE,CAAC;AAED,SAAK,kBAAkB,CAAC;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA,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MAAM;AAAA,MAAC;AAAA,IAClC,CAAC;AAAA,EACH;AAAA,EAEO,OAAO,UAAkC,CAAC,GAAS;AACxD,SAAK,sBAAkB;AAAA,MACrB;AAAA,MACA;AAAA,IACF;AAKA,SAAK,YAAY,MAAM;AACvB,SAAK,KAAK;AACV,SAAK,cAAc,KAAK,MAAM,KAAK,YAAY,QAAQ,CAAC;AAGxD,qDAAqB,MAAM;AAC3B,SAAK,cAAc,KAAK,MAAM,iDAAqB,QAAQ,CAAC;AAK5D;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;;;AD7JO,SAAS,eACX,UACmB;AAGtB,SAAO,IAAI;AAAA,IACT,CAAC,+BAAkB,+CAAyB;AAAA,IAC5C;AAAA,EACF;AACF;","names":["request"]}
1
+ {"version":3,"sources":["../../src/native/index.ts","../../src/node/SetupServerCommonApi.ts"],"sourcesContent":["import { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { SetupServerCommonApi } from '../node/SetupServerCommonApi'\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): SetupServerCommonApi {\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 SetupServerCommonApi(\n [new FetchInterceptor(), new XMLHttpRequestInterceptor()],\n handlers,\n )\n}\n","/**\n * @note This API is extended by both \"msw/node\" and \"msw/native\"\n * so be minding about the things you import!\n */\nimport type { RequiredDeep } from 'type-fest'\nimport { invariant } from 'outvariant'\nimport {\n BatchInterceptor,\n InterceptorReadyState,\n type HttpRequestEventMap,\n type Interceptor,\n} from '@mswjs/interceptors'\nimport type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { SetupApi } from '~/core/SetupApi'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { InternalError, devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\nimport { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'\nimport { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'\nimport { isHandlerKind } from '~/core/utils/internal/isHandlerKind'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerCommonApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServerCommon\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<Interceptor<HttpRequestEventMap>>,\n handlers: Array<RequestHandler | WebSocketHandler>,\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors,\n })\n\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on(\n 'request',\n async ({ request, requestId, controller }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.handlersController\n .currentHandlers()\n .filter(isHandlerKind('RequestHandler')),\n this.resolvedOptions,\n this.emitter,\n {\n onPassthroughResponse(request) {\n const acceptHeader = request.headers.get('accept')\n\n /**\n * @note Remove the internal bypass request header.\n * In the browser, this is done by the worker script.\n * In Node.js, it has to be done here.\n */\n if (acceptHeader) {\n const nextAcceptHeader = acceptHeader.replace(\n /(,\\s+)?msw\\/passthrough/,\n '',\n )\n\n if (nextAcceptHeader) {\n request.headers.set('accept', nextAcceptHeader)\n } else {\n request.headers.delete('accept')\n }\n }\n },\n },\n )\n\n if (response) {\n controller.respondWith(response)\n }\n\n return\n },\n )\n\n this.interceptor.on('unhandledException', ({ error }) => {\n if (error instanceof InternalError) {\n throw error\n }\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 // Preconfigure the WebSocket interception but don't enable it just yet.\n // It will be enabled when the server starts.\n handleWebSocketEvent({\n getUnhandledRequestStrategy: () => {\n return this.resolvedOptions.onUnhandledRequest\n },\n getHandlers: () => {\n return this.handlersController.currentHandlers()\n },\n onMockedConnection: () => {},\n onPassthroughConnection: () => {},\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 // Attach the event listeners to the interceptor here\n // so they get re-attached whenever `.listen()` is called.\n this.interceptor.apply()\n this.init()\n this.subscriptions.push(() => this.interceptor.dispose())\n\n // Apply the WebSocket interception.\n webSocketInterceptor.apply()\n this.subscriptions.push(() => webSocketInterceptor.dispose())\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 {\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 var SetupServerCommonApi = class extends SetupApi {
25
25
  super(...handlers);
26
26
  this.interceptor = new BatchInterceptor({
27
27
  name: "setup-server",
28
- interceptors: interceptors.map((Interceptor) => new Interceptor())
28
+ interceptors
29
29
  });
30
30
  this.resolvedOptions = {};
31
31
  }
@@ -124,7 +124,7 @@ var SetupServerCommonApi = class extends SetupApi {
124
124
  // src/native/index.ts
125
125
  function setupServer(...handlers) {
126
126
  return new SetupServerCommonApi(
127
- [FetchInterceptor, XMLHttpRequestInterceptor],
127
+ [new FetchInterceptor(), new XMLHttpRequestInterceptor()],
128
128
  handlers
129
129
  );
130
130
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/native/index.ts","../../src/node/SetupServerCommonApi.ts"],"sourcesContent":["import { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { SetupServerCommonApi } from '../node/SetupServerCommonApi'\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): SetupServerCommonApi {\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 SetupServerCommonApi(\n [FetchInterceptor, XMLHttpRequestInterceptor],\n handlers,\n )\n}\n","/**\n * @note This API is extended by both \"msw/node\" and \"msw/native\"\n * so be minding about the things you import!\n */\nimport type { RequiredDeep } from 'type-fest'\nimport { invariant } from 'outvariant'\nimport {\n BatchInterceptor,\n InterceptorReadyState,\n type HttpRequestEventMap,\n type Interceptor,\n} from '@mswjs/interceptors'\nimport type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { SetupApi } from '~/core/SetupApi'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { InternalError, devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\nimport { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'\nimport { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'\nimport { isHandlerKind } from '~/core/utils/internal/isHandlerKind'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerCommonApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServerCommon\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{ new (): Interceptor<HttpRequestEventMap> }>,\n handlers: Array<RequestHandler | WebSocketHandler>,\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on(\n 'request',\n async ({ request, requestId, controller }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.handlersController\n .currentHandlers()\n .filter(isHandlerKind('RequestHandler')),\n this.resolvedOptions,\n this.emitter,\n {\n onPassthroughResponse(request) {\n const acceptHeader = request.headers.get('accept')\n\n /**\n * @note Remove the internal bypass request header.\n * In the browser, this is done by the worker script.\n * In Node.js, it has to be done here.\n */\n if (acceptHeader) {\n const nextAcceptHeader = acceptHeader.replace(\n /(,\\s+)?msw\\/passthrough/,\n '',\n )\n\n if (nextAcceptHeader) {\n request.headers.set('accept', nextAcceptHeader)\n } else {\n request.headers.delete('accept')\n }\n }\n },\n },\n )\n\n if (response) {\n controller.respondWith(response)\n }\n\n return\n },\n )\n\n this.interceptor.on('unhandledException', ({ error }) => {\n if (error instanceof InternalError) {\n throw error\n }\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 // Preconfigure the WebSocket interception but don't enable it just yet.\n // It will be enabled when the server starts.\n handleWebSocketEvent({\n getUnhandledRequestStrategy: () => {\n return this.resolvedOptions.onUnhandledRequest\n },\n getHandlers: () => {\n return this.handlersController.currentHandlers()\n },\n onMockedConnection: () => {},\n onPassthroughConnection: () => {},\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 // Attach the event listeners to the interceptor here\n // so they get re-attached whenever `.listen()` is called.\n this.interceptor.apply()\n this.init()\n this.subscriptions.push(() => this.interceptor.dispose())\n\n // Apply the WebSocket interception.\n webSocketInterceptor.apply()\n this.subscriptions.push(() => webSocketInterceptor.dispose())\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,wBAAwB;AACjC,SAAS,iCAAiC;;;ACI1C,SAAS,iBAAiB;AAC1B;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAEP,SAAS,gBAAgB;AACzB,SAAS,qBAAqB;AAG9B,SAAS,kBAAkB;AAC3B,SAAS,eAAe,gBAAgB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A,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;AAIA,yBAAqB;AAAA,MACnB,6BAA6B,MAAM;AACjC,eAAO,KAAK,gBAAgB;AAAA,MAC9B;AAAA,MACA,aAAa,MAAM;AACjB,eAAO,KAAK,mBAAmB,gBAAgB;AAAA,MACjD;AAAA,MACA,oBAAoB,MAAM;AAAA,MAAC;AAAA,MAC3B,yBAAyB,MAAM;AAAA,MAAC;AAAA,IAClC,CAAC;AAAA,EACH;AAAA,EAEO,OAAO,UAAkC,CAAC,GAAS;AACxD,SAAK,kBAAkB;AAAA,MACrB;AAAA,MACA;AAAA,IACF;AAKA,SAAK,YAAY,MAAM;AACvB,SAAK,KAAK;AACV,SAAK,cAAc,KAAK,MAAM,KAAK,YAAY,QAAQ,CAAC;AAGxD,yBAAqB,MAAM;AAC3B,SAAK,cAAc,KAAK,MAAM,qBAAqB,QAAQ,CAAC;AAK5D;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;;;AD7JO,SAAS,eACX,UACmB;AAGtB,SAAO,IAAI;AAAA,IACT,CAAC,kBAAkB,yBAAyB;AAAA,IAC5C;AAAA,EACF;AACF;","names":["request"]}
1
+ {"version":3,"sources":["../../src/native/index.ts","../../src/node/SetupServerCommonApi.ts"],"sourcesContent":["import { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport { SetupServerCommonApi } from '../node/SetupServerCommonApi'\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): SetupServerCommonApi {\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 SetupServerCommonApi(\n [new FetchInterceptor(), new XMLHttpRequestInterceptor()],\n handlers,\n )\n}\n","/**\n * @note This API is extended by both \"msw/node\" and \"msw/native\"\n * so be minding about the things you import!\n */\nimport type { RequiredDeep } from 'type-fest'\nimport { invariant } from 'outvariant'\nimport {\n BatchInterceptor,\n InterceptorReadyState,\n type HttpRequestEventMap,\n type Interceptor,\n} from '@mswjs/interceptors'\nimport type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { SetupApi } from '~/core/SetupApi'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { InternalError, devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\nimport { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'\nimport { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'\nimport { isHandlerKind } from '~/core/utils/internal/isHandlerKind'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerCommonApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServerCommon\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<Interceptor<HttpRequestEventMap>>,\n handlers: Array<RequestHandler | WebSocketHandler>,\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors,\n })\n\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on(\n 'request',\n async ({ request, requestId, controller }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.handlersController\n .currentHandlers()\n .filter(isHandlerKind('RequestHandler')),\n this.resolvedOptions,\n this.emitter,\n {\n onPassthroughResponse(request) {\n const acceptHeader = request.headers.get('accept')\n\n /**\n * @note Remove the internal bypass request header.\n * In the browser, this is done by the worker script.\n * In Node.js, it has to be done here.\n */\n if (acceptHeader) {\n const nextAcceptHeader = acceptHeader.replace(\n /(,\\s+)?msw\\/passthrough/,\n '',\n )\n\n if (nextAcceptHeader) {\n request.headers.set('accept', nextAcceptHeader)\n } else {\n request.headers.delete('accept')\n }\n }\n },\n },\n )\n\n if (response) {\n controller.respondWith(response)\n }\n\n return\n },\n )\n\n this.interceptor.on('unhandledException', ({ error }) => {\n if (error instanceof InternalError) {\n throw error\n }\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 // Preconfigure the WebSocket interception but don't enable it just yet.\n // It will be enabled when the server starts.\n handleWebSocketEvent({\n getUnhandledRequestStrategy: () => {\n return this.resolvedOptions.onUnhandledRequest\n },\n getHandlers: () => {\n return this.handlersController.currentHandlers()\n },\n onMockedConnection: () => {},\n onPassthroughConnection: () => {},\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 // Attach the event listeners to the interceptor here\n // so they get re-attached whenever `.listen()` is called.\n this.interceptor.apply()\n this.init()\n this.subscriptions.push(() => this.interceptor.dispose())\n\n // Apply the WebSocket interception.\n webSocketInterceptor.apply()\n this.subscriptions.push(() => webSocketInterceptor.dispose())\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,wBAAwB;AACjC,SAAS,iCAAiC;;;ACI1C,SAAS,iBAAiB;AAC1B;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAEP,SAAS,gBAAgB;AACzB,SAAS,qBAAqB;AAG9B,SAAS,kBAAkB;AAC3B,SAAS,eAAe,gBAAgB;AAExC,SAAS,4BAA4B;AACrC,SAAS,4BAA4B;AACrC,SAAS,qBAAqB;AAE9B,IAAM,yBAAsD;AAAA,EAC1D,oBAAoB;AACtB;AAEO,IAAM,uBAAN,cACG,SAEV;AAAA,EACqB;AAAA,EAIX;AAAA,EAER,YACE,cACA,UACA;AACA,UAAM,GAAG,QAAQ;AAEjB,SAAK,cAAc,IAAI,iBAAiB;AAAA,MACtC,MAAM;AAAA,MACN;AAAA,IACF,CAAC;AAED,SAAK,kBAAkB,CAAC;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA,EAKQ,OAAa;AACnB,SAAK,YAAY;AAAA,MACf;AAAA,MACA,OAAO,EAAE,SAAS,WAAW,WAAW,MAAM;AAC5C,cAAM,WAAW,MAAM;AAAA,UACrB;AAAA,UACA;AAAA,UACA,KAAK,mBACF,gBAAgB,EAChB,OAAO,cAAc,gBAAgB,CAAC;AAAA,UACzC,KAAK;AAAA,UACL,KAAK;AAAA,UACL;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,MAAM;AAC3B,SAAK,cAAc,KAAK,MAAM,qBAAqB,QAAQ,CAAC;AAK5D;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;;;AD7JO,SAAS,eACX,UACmB;AAGtB,SAAO,IAAI;AAAA,IACT,CAAC,IAAI,iBAAiB,GAAG,IAAI,0BAA0B,CAAC;AAAA,IACxD;AAAA,EACF;AACF;","names":["request"]}
@@ -1,9 +1,9 @@
1
1
  import { PartialDeep } from 'type-fest';
2
- import { RequestHandler } from "../core/handlers/RequestHandler";
3
- import { WebSocketHandler } from "../core/handlers/WebSocketHandler";
4
- import { SharedOptions, LifeCycleEventEmitter, LifeCycleEventsMap } from "../core/sharedOptions";
2
+ import { RequestHandler } from "../core/handlers/RequestHandler.mjs";
3
+ import { WebSocketHandler } from "../core/handlers/WebSocketHandler.mjs";
4
+ import { SharedOptions, LifeCycleEventEmitter, LifeCycleEventsMap } from "../core/sharedOptions.mjs";
5
5
  import { BatchInterceptor, Interceptor, HttpRequestEventMap } from '@mswjs/interceptors';
6
- import { SetupApi } from "../core/SetupApi";
6
+ import { SetupApi } from "../core/SetupApi.mjs";
7
7
 
8
8
  interface SetupServerCommon {
9
9
  /**
@@ -65,9 +65,7 @@ interface SetupServer extends SetupServerCommon {
65
65
  declare class SetupServerCommonApi extends SetupApi<LifeCycleEventsMap> implements SetupServerCommon {
66
66
  protected readonly interceptor: BatchInterceptor<Array<Interceptor<HttpRequestEventMap>>, HttpRequestEventMap>;
67
67
  private resolvedOptions;
68
- constructor(interceptors: Array<{
69
- new (): Interceptor<HttpRequestEventMap>;
70
- }>, handlers: Array<RequestHandler | WebSocketHandler>);
68
+ constructor(interceptors: Array<Interceptor<HttpRequestEventMap>>, handlers: Array<RequestHandler | WebSocketHandler>);
71
69
  /**
72
70
  * Subscribe to all requests that are using the interceptor object
73
71
  */
@@ -77,7 +75,7 @@ declare class SetupServerCommonApi extends SetupApi<LifeCycleEventsMap> implemen
77
75
  }
78
76
 
79
77
  declare class SetupServerApi extends SetupServerCommonApi implements SetupServer {
80
- constructor(handlers: Array<RequestHandler | WebSocketHandler>);
78
+ constructor(handlers: Array<RequestHandler | WebSocketHandler>, interceptors?: Array<Interceptor<HttpRequestEventMap>>);
81
79
  boundary<Args extends Array<any>, R>(callback: (...args: Args) => R): (...args: Args) => R;
82
80
  close(): void;
83
81
  }
@@ -1,9 +1,9 @@
1
1
  import { PartialDeep } from 'type-fest';
2
- import { RequestHandler } from "../core/handlers/RequestHandler";
3
- import { WebSocketHandler } from "../core/handlers/WebSocketHandler";
4
- import { SharedOptions, LifeCycleEventEmitter, LifeCycleEventsMap } from "../core/sharedOptions";
2
+ import { RequestHandler } from "../core/handlers/RequestHandler.mjs";
3
+ import { WebSocketHandler } from "../core/handlers/WebSocketHandler.mjs";
4
+ import { SharedOptions, LifeCycleEventEmitter, LifeCycleEventsMap } from "../core/sharedOptions.mjs";
5
5
  import { BatchInterceptor, Interceptor, HttpRequestEventMap } from '@mswjs/interceptors';
6
- import { SetupApi } from "../core/SetupApi";
6
+ import { SetupApi } from "../core/SetupApi.mjs";
7
7
 
8
8
  interface SetupServerCommon {
9
9
  /**
@@ -65,9 +65,7 @@ interface SetupServer extends SetupServerCommon {
65
65
  declare class SetupServerCommonApi extends SetupApi<LifeCycleEventsMap> implements SetupServerCommon {
66
66
  protected readonly interceptor: BatchInterceptor<Array<Interceptor<HttpRequestEventMap>>, HttpRequestEventMap>;
67
67
  private resolvedOptions;
68
- constructor(interceptors: Array<{
69
- new (): Interceptor<HttpRequestEventMap>;
70
- }>, handlers: Array<RequestHandler | WebSocketHandler>);
68
+ constructor(interceptors: Array<Interceptor<HttpRequestEventMap>>, handlers: Array<RequestHandler | WebSocketHandler>);
71
69
  /**
72
70
  * Subscribe to all requests that are using the interceptor object
73
71
  */
@@ -77,7 +75,7 @@ declare class SetupServerCommonApi extends SetupApi<LifeCycleEventsMap> implemen
77
75
  }
78
76
 
79
77
  declare class SetupServerApi extends SetupServerCommonApi implements SetupServer {
80
- constructor(handlers: Array<RequestHandler | WebSocketHandler>);
78
+ constructor(handlers: Array<RequestHandler | WebSocketHandler>, interceptors?: Array<Interceptor<HttpRequestEventMap>>);
81
79
  boundary<Args extends Array<any>, R>(callback: (...args: Args) => R): (...args: Args) => R;
82
80
  close(): void;
83
81
  }
package/lib/node/index.js CHANGED
@@ -18,12 +18,12 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
20
  // src/node/index.ts
21
- var node_exports = {};
22
- __export(node_exports, {
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
23
  SetupServerApi: () => SetupServerApi,
24
24
  setupServer: () => setupServer
25
25
  });
26
- module.exports = __toCommonJS(node_exports);
26
+ module.exports = __toCommonJS(index_exports);
27
27
 
28
28
  // src/node/SetupServerApi.ts
29
29
  var import_node_async_hooks = require("async_hooks");
@@ -34,13 +34,13 @@ var import_fetch = require("@mswjs/interceptors/fetch");
34
34
  // src/node/SetupServerCommonApi.ts
35
35
  var import_outvariant = require("outvariant");
36
36
  var import_interceptors = require("@mswjs/interceptors");
37
- var import_SetupApi = require("../core/SetupApi.js");
38
- var import_handleRequest = require("../core/utils/handleRequest.js");
39
- var import_mergeRight = require("../core/utils/internal/mergeRight.js");
40
- var import_devUtils = require("../core/utils/internal/devUtils.js");
41
- var import_handleWebSocketEvent = require("../core/ws/handleWebSocketEvent.js");
42
- var import_webSocketInterceptor = require("../core/ws/webSocketInterceptor.js");
43
- var import_isHandlerKind = require("../core/utils/internal/isHandlerKind.js");
37
+ var import_SetupApi = require("../core/SetupApi");
38
+ var import_handleRequest = require("../core/utils/handleRequest");
39
+ var import_mergeRight = require("../core/utils/internal/mergeRight");
40
+ var import_devUtils = require("../core/utils/internal/devUtils");
41
+ var import_handleWebSocketEvent = require("../core/ws/handleWebSocketEvent");
42
+ var import_webSocketInterceptor = require("../core/ws/webSocketInterceptor");
43
+ var import_isHandlerKind = require("../core/utils/internal/isHandlerKind");
44
44
  var DEFAULT_LISTEN_OPTIONS = {
45
45
  onUnhandledRequest: "warn"
46
46
  };
@@ -51,7 +51,7 @@ var SetupServerCommonApi = class extends import_SetupApi.SetupApi {
51
51
  super(...handlers);
52
52
  this.interceptor = new import_interceptors.BatchInterceptor({
53
53
  name: "setup-server",
54
- interceptors: interceptors.map((Interceptor) => new Interceptor())
54
+ interceptors
55
55
  });
56
56
  this.resolvedOptions = {};
57
57
  }
@@ -171,11 +171,12 @@ var AsyncHandlersController = class {
171
171
  }
172
172
  };
173
173
  var SetupServerApi = class extends SetupServerCommonApi {
174
- constructor(handlers) {
175
- super(
176
- [import_ClientRequest.ClientRequestInterceptor, import_XMLHttpRequest.XMLHttpRequestInterceptor, import_fetch.FetchInterceptor],
177
- handlers
178
- );
174
+ constructor(handlers, interceptors = [
175
+ new import_ClientRequest.ClientRequestInterceptor(),
176
+ new import_XMLHttpRequest.XMLHttpRequestInterceptor(),
177
+ new import_fetch.FetchInterceptor()
178
+ ]) {
179
+ super(interceptors, handlers);
179
180
  this.handlersController = new AsyncHandlersController(handlers);
180
181
  }
181
182
  boundary(callback) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/node/index.ts","../../src/node/SetupServerApi.ts","../../src/node/SetupServerCommonApi.ts","../../src/node/setupServer.ts"],"sourcesContent":["export type { SetupServer } from './glossary'\nexport { SetupServerApi } from './SetupServerApi'\nexport { setupServer } from './setupServer'\n","import { AsyncLocalStorage } from 'node:async_hooks'\nimport { ClientRequestInterceptor } from '@mswjs/interceptors/ClientRequest'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { HandlersController } from '~/core/SetupApi'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport type { SetupServer } from './glossary'\nimport { SetupServerCommonApi } from './SetupServerCommonApi'\n\nconst store = new AsyncLocalStorage<RequestHandlersContext>()\n\ntype RequestHandlersContext = {\n initialHandlers: Array<RequestHandler | WebSocketHandler>\n handlers: Array<RequestHandler | WebSocketHandler>\n}\n\n/**\n * A handlers controller that utilizes `AsyncLocalStorage` in Node.js\n * to prevent the request handlers list from being a shared state\n * across mutliple tests.\n */\nclass AsyncHandlersController implements HandlersController {\n private rootContext: RequestHandlersContext\n\n constructor(initialHandlers: Array<RequestHandler | WebSocketHandler>) {\n this.rootContext = { initialHandlers, handlers: [] }\n }\n\n get context(): RequestHandlersContext {\n return store.getStore() || this.rootContext\n }\n\n public prepend(runtimeHandlers: Array<RequestHandler | WebSocketHandler>) {\n this.context.handlers.unshift(...runtimeHandlers)\n }\n\n public reset(nextHandlers: Array<RequestHandler | WebSocketHandler>) {\n const context = this.context\n context.handlers = []\n context.initialHandlers =\n nextHandlers.length > 0 ? nextHandlers : context.initialHandlers\n }\n\n public currentHandlers(): Array<RequestHandler | WebSocketHandler> {\n const { initialHandlers, handlers } = this.context\n return handlers.concat(initialHandlers)\n }\n}\n\nexport class SetupServerApi\n extends SetupServerCommonApi\n implements SetupServer\n{\n constructor(handlers: Array<RequestHandler | WebSocketHandler>) {\n super(\n [ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],\n handlers,\n )\n\n this.handlersController = new AsyncHandlersController(handlers)\n }\n\n public boundary<Args extends Array<any>, R>(\n callback: (...args: Args) => R,\n ): (...args: Args) => R {\n return (...args: Args): R => {\n return store.run<any, any>(\n {\n initialHandlers: this.handlersController.currentHandlers(),\n handlers: [],\n },\n callback,\n ...args,\n )\n }\n }\n\n public close(): void {\n super.close()\n store.disable()\n }\n}\n","/**\n * @note This API is extended by both \"msw/node\" and \"msw/native\"\n * so be minding about the things you import!\n */\nimport type { RequiredDeep } from 'type-fest'\nimport { invariant } from 'outvariant'\nimport {\n BatchInterceptor,\n InterceptorReadyState,\n type HttpRequestEventMap,\n type Interceptor,\n} from '@mswjs/interceptors'\nimport type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { SetupApi } from '~/core/SetupApi'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { InternalError, devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\nimport { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'\nimport { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'\nimport { isHandlerKind } from '~/core/utils/internal/isHandlerKind'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerCommonApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServerCommon\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{ new (): Interceptor<HttpRequestEventMap> }>,\n handlers: Array<RequestHandler | WebSocketHandler>,\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on(\n 'request',\n async ({ request, requestId, controller }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.handlersController\n .currentHandlers()\n .filter(isHandlerKind('RequestHandler')),\n this.resolvedOptions,\n this.emitter,\n {\n onPassthroughResponse(request) {\n const acceptHeader = request.headers.get('accept')\n\n /**\n * @note Remove the internal bypass request header.\n * In the browser, this is done by the worker script.\n * In Node.js, it has to be done here.\n */\n if (acceptHeader) {\n const nextAcceptHeader = acceptHeader.replace(\n /(,\\s+)?msw\\/passthrough/,\n '',\n )\n\n if (nextAcceptHeader) {\n request.headers.set('accept', nextAcceptHeader)\n } else {\n request.headers.delete('accept')\n }\n }\n },\n },\n )\n\n if (response) {\n controller.respondWith(response)\n }\n\n return\n },\n )\n\n this.interceptor.on('unhandledException', ({ error }) => {\n if (error instanceof InternalError) {\n throw error\n }\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 // Preconfigure the WebSocket interception but don't enable it just yet.\n // It will be enabled when the server starts.\n handleWebSocketEvent({\n getUnhandledRequestStrategy: () => {\n return this.resolvedOptions.onUnhandledRequest\n },\n getHandlers: () => {\n return this.handlersController.currentHandlers()\n },\n onMockedConnection: () => {},\n onPassthroughConnection: () => {},\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 // Attach the event listeners to the interceptor here\n // so they get re-attached whenever `.listen()` is called.\n this.interceptor.apply()\n this.init()\n this.subscriptions.push(() => this.interceptor.dispose())\n\n // Apply the WebSocket interception.\n webSocketInterceptor.apply()\n this.subscriptions.push(() => webSocketInterceptor.dispose())\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 type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { SetupServerApi } from './SetupServerApi'\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 | WebSocketHandler>\n): SetupServerApi => {\n return new SetupServerApi(handlers)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,8BAAkC;AAClC,2BAAyC;AACzC,4BAA0C;AAC1C,mBAAiC;;;ACEjC,wBAA0B;AAC1B,0BAKO;AAEP,sBAAyB;AACzB,2BAA8B;AAG9B,wBAA2B;AAC3B,sBAAwC;AAExC,kCAAqC;AACrC,kCAAqC;AACrC,2BAA8B;AAE9B,IAAM,yBAAsD;AAAA,EAC1D,oBAAoB;AACtB;AAEO,IAAM,uBAAN,cACG,yBAEV;AAAA,EACqB;AAAA,EAIX;AAAA,EAER,YACE,cACA,UACA;AACA,UAAM,GAAG,QAAQ;AAEjB,SAAK,cAAc,IAAI,qCAAiB;AAAA,MACtC,MAAM;AAAA,MACN,cAAc,aAAa,IAAI,CAAC,gBAAgB,IAAI,YAAY,CAAC;AAAA,IACnE,CAAC;AAED,SAAK,kBAAkB,CAAC;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA,EAKQ,OAAa;AACnB,SAAK,YAAY;AAAA,MACf;AAAA,MACA,OAAO,EAAE,SAAS,WAAW,WAAW,MAAM;AAC5C,cAAM,WAAW,UAAM;AAAA,UACrB;AAAA,UACA;AAAA,UACA,KAAK,mBACF,gBAAgB,EAChB,WAAO,oCAAc,gBAAgB,CAAC;AAAA,UACzC,KAAK;AAAA,UACL,K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AG;AAAA,MACL;AAAA,IACF;AAAA,EACF;AAAA,EAEO,QAAc;AACnB,UAAM,MAAM;AACZ,UAAM,QAAQ;AAAA,EAChB;AACF;;;AExEO,IAAM,cAAc,IACtB,aACgB;AACnB,SAAO,IAAI,eAAe,QAAQ;AACpC;","names":["request"]}
1
+ {"version":3,"sources":["../../src/node/index.ts","../../src/node/SetupServerApi.ts","../../src/node/SetupServerCommonApi.ts","../../src/node/setupServer.ts"],"sourcesContent":["export type { SetupServer } from './glossary'\nexport { SetupServerApi } from './SetupServerApi'\nexport { setupServer } from './setupServer'\n","import { AsyncLocalStorage } from 'node:async_hooks'\nimport type { HttpRequestEventMap, Interceptor } from '@mswjs/interceptors'\nimport { ClientRequestInterceptor } from '@mswjs/interceptors/ClientRequest'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { HandlersController } from '~/core/SetupApi'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport type { SetupServer } from './glossary'\nimport { SetupServerCommonApi } from './SetupServerCommonApi'\n\nconst store = new AsyncLocalStorage<RequestHandlersContext>()\n\ntype RequestHandlersContext = {\n initialHandlers: Array<RequestHandler | WebSocketHandler>\n handlers: Array<RequestHandler | WebSocketHandler>\n}\n\n/**\n * A handlers controller that utilizes `AsyncLocalStorage` in Node.js\n * to prevent the request handlers list from being a shared state\n * across mutliple tests.\n */\nclass AsyncHandlersController implements HandlersController {\n private rootContext: RequestHandlersContext\n\n constructor(initialHandlers: Array<RequestHandler | WebSocketHandler>) {\n this.rootContext = { initialHandlers, handlers: [] }\n }\n\n get context(): RequestHandlersContext {\n return store.getStore() || this.rootContext\n }\n\n public prepend(runtimeHandlers: Array<RequestHandler | WebSocketHandler>) {\n this.context.handlers.unshift(...runtimeHandlers)\n }\n\n public reset(nextHandlers: Array<RequestHandler | WebSocketHandler>) {\n const context = this.context\n context.handlers = []\n context.initialHandlers =\n nextHandlers.length > 0 ? nextHandlers : context.initialHandlers\n }\n\n public currentHandlers(): Array<RequestHandler | WebSocketHandler> {\n const { initialHandlers, handlers } = this.context\n return handlers.concat(initialHandlers)\n }\n}\nexport class SetupServerApi\n extends SetupServerCommonApi\n implements SetupServer\n{\n constructor(\n handlers: Array<RequestHandler | WebSocketHandler>,\n interceptors: Array<Interceptor<HttpRequestEventMap>> = [\n new ClientRequestInterceptor(),\n new XMLHttpRequestInterceptor(),\n new FetchInterceptor(),\n ],\n ) {\n super(interceptors, handlers)\n\n this.handlersController = new AsyncHandlersController(handlers)\n }\n\n public boundary<Args extends Array<any>, R>(\n callback: (...args: Args) => R,\n ): (...args: Args) => R {\n return (...args: Args): R => {\n return store.run<any, any>(\n {\n initialHandlers: this.handlersController.currentHandlers(),\n handlers: [],\n },\n callback,\n ...args,\n )\n }\n }\n\n public close(): void {\n super.close()\n store.disable()\n }\n}\n","/**\n * @note This API is extended by both \"msw/node\" and \"msw/native\"\n * so be minding about the things you import!\n */\nimport type { RequiredDeep } from 'type-fest'\nimport { invariant } from 'outvariant'\nimport {\n BatchInterceptor,\n InterceptorReadyState,\n type HttpRequestEventMap,\n type Interceptor,\n} from '@mswjs/interceptors'\nimport type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { SetupApi } from '~/core/SetupApi'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { InternalError, devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\nimport { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'\nimport { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'\nimport { isHandlerKind } from '~/core/utils/internal/isHandlerKind'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerCommonApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServerCommon\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<Interceptor<HttpRequestEventMap>>,\n handlers: Array<RequestHandler | WebSocketHandler>,\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors,\n })\n\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on(\n 'request',\n async ({ request, requestId, controller }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.handlersController\n .currentHandlers()\n .filter(isHandlerKind('RequestHandler')),\n this.resolvedOptions,\n this.emitter,\n {\n onPassthroughResponse(request) {\n const acceptHeader = request.headers.get('accept')\n\n /**\n * @note Remove the internal bypass request header.\n * In the browser, this is done by the worker script.\n * In Node.js, it has to be done here.\n */\n if (acceptHeader) {\n const nextAcceptHeader = acceptHeader.replace(\n /(,\\s+)?msw\\/passthrough/,\n '',\n )\n\n if (nextAcceptHeader) {\n request.headers.set('accept', nextAcceptHeader)\n } else {\n request.headers.delete('accept')\n }\n }\n },\n },\n )\n\n if (response) {\n controller.respondWith(response)\n }\n\n return\n },\n )\n\n this.interceptor.on('unhandledException', ({ error }) => {\n if (error instanceof InternalError) {\n throw error\n }\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 // Preconfigure the WebSocket interception but don't enable it just yet.\n // It will be enabled when the server starts.\n handleWebSocketEvent({\n getUnhandledRequestStrategy: () => {\n return this.resolvedOptions.onUnhandledRequest\n },\n getHandlers: () => {\n return this.handlersController.currentHandlers()\n },\n onMockedConnection: () => {},\n onPassthroughConnection: () => {},\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 // Attach the event listeners to the interceptor here\n // so they get re-attached whenever `.listen()` is called.\n this.interceptor.apply()\n this.init()\n this.subscriptions.push(() => this.interceptor.dispose())\n\n // Apply the WebSocket interception.\n webSocketInterceptor.apply()\n this.subscriptions.push(() => webSocketInterceptor.dispose())\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 type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { SetupServerApi } from './SetupServerApi'\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 | WebSocketHandler>\n): SetupServerApi => {\n return new SetupServerApi(handlers)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,8BAAkC;AAElC,2BAAyC;AACzC,4BAA0C;AAC1C,mBAAiC;;;ACCjC,wBAA0B;AAC1B,0BAKO;AAEP,sBAAyB;AACzB,2BAA8B;AAG9B,wBAA2B;AAC3B,sBAAwC;AAExC,kCAAqC;AACrC,kCAAqC;AACr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C;AACpB,YAAQ,kBACN,aAAa,SAAS,IAAI,eAAe,QAAQ;AAAA,EACrD;AAAA,EAEO,kBAA4D;AACjE,UAAM,EAAE,iBAAiB,SAAS,IAAI,KAAK;AAC3C,WAAO,SAAS,OAAO,eAAe;AAAA,EACxC;AACF;AACO,IAAM,iBAAN,cACG,qBAEV;AAAA,EACE,YACE,UACA,eAAwD;AAAA,IACtD,IAAI,8CAAyB;AAAA,IAC7B,IAAI,gDAA0B;AAAA,IAC9B,IAAI,8BAAiB;AAAA,EACvB,GACA;AACA,UAAM,cAAc,QAAQ;AAE5B,SAAK,qBAAqB,IAAI,wBAAwB,QAAQ;AAAA,EAChE;AAAA,EAEO,SACL,UACsB;AACtB,WAAO,IAAI,SAAkB;AAC3B,aAAO,MAAM;AAAA,QACX;AAAA,UACE,iBAAiB,KAAK,mBAAmB,gBAAgB;AAAA,UACzD,UAAU,CAAC;AAAA,QACb;AAAA,QACA;AAAA,QACA,GAAG;AAAA,MACL;AAAA,IACF;AAAA,EACF;AAAA,EAEO,QAAc;AACnB,UAAM,MAAM;AACZ,UAAM,QAAQ;AAAA,EAChB;AACF;;;AE5EO,IAAM,cAAc,IACtB,aACgB;AACnB,SAAO,IAAI,eAAe,QAAQ;AACpC;","names":["request"]}
@@ -27,7 +27,7 @@ var SetupServerCommonApi = class extends SetupApi {
27
27
  super(...handlers);
28
28
  this.interceptor = new BatchInterceptor({
29
29
  name: "setup-server",
30
- interceptors: interceptors.map((Interceptor) => new Interceptor())
30
+ interceptors
31
31
  });
32
32
  this.resolvedOptions = {};
33
33
  }
@@ -147,11 +147,12 @@ var AsyncHandlersController = class {
147
147
  }
148
148
  };
149
149
  var SetupServerApi = class extends SetupServerCommonApi {
150
- constructor(handlers) {
151
- super(
152
- [ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],
153
- handlers
154
- );
150
+ constructor(handlers, interceptors = [
151
+ new ClientRequestInterceptor(),
152
+ new XMLHttpRequestInterceptor(),
153
+ new FetchInterceptor()
154
+ ]) {
155
+ super(interceptors, handlers);
155
156
  this.handlersController = new AsyncHandlersController(handlers);
156
157
  }
157
158
  boundary(callback) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/node/SetupServerApi.ts","../../src/node/SetupServerCommonApi.ts","../../src/node/setupServer.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks'\nimport { ClientRequestInterceptor } from '@mswjs/interceptors/ClientRequest'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { HandlersController } from '~/core/SetupApi'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport type { SetupServer } from './glossary'\nimport { SetupServerCommonApi } from './SetupServerCommonApi'\n\nconst store = new AsyncLocalStorage<RequestHandlersContext>()\n\ntype RequestHandlersContext = {\n initialHandlers: Array<RequestHandler | WebSocketHandler>\n handlers: Array<RequestHandler | WebSocketHandler>\n}\n\n/**\n * A handlers controller that utilizes `AsyncLocalStorage` in Node.js\n * to prevent the request handlers list from being a shared state\n * across mutliple tests.\n */\nclass AsyncHandlersController implements HandlersController {\n private rootContext: RequestHandlersContext\n\n constructor(initialHandlers: Array<RequestHandler | WebSocketHandler>) {\n this.rootContext = { initialHandlers, handlers: [] }\n }\n\n get context(): RequestHandlersContext {\n return store.getStore() || this.rootContext\n }\n\n public prepend(runtimeHandlers: Array<RequestHandler | WebSocketHandler>) {\n this.context.handlers.unshift(...runtimeHandlers)\n }\n\n public reset(nextHandlers: Array<RequestHandler | WebSocketHandler>) {\n const context = this.context\n context.handlers = []\n context.initialHandlers =\n nextHandlers.length > 0 ? nextHandlers : context.initialHandlers\n }\n\n public currentHandlers(): Array<RequestHandler | WebSocketHandler> {\n const { initialHandlers, handlers } = this.context\n return handlers.concat(initialHandlers)\n }\n}\n\nexport class SetupServerApi\n extends SetupServerCommonApi\n implements SetupServer\n{\n constructor(handlers: Array<RequestHandler | WebSocketHandler>) {\n super(\n [ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],\n handlers,\n )\n\n this.handlersController = new AsyncHandlersController(handlers)\n }\n\n public boundary<Args extends Array<any>, R>(\n callback: (...args: Args) => R,\n ): (...args: Args) => R {\n return (...args: Args): R => {\n return store.run<any, any>(\n {\n initialHandlers: this.handlersController.currentHandlers(),\n handlers: [],\n },\n callback,\n ...args,\n )\n }\n }\n\n public close(): void {\n super.close()\n store.disable()\n }\n}\n","/**\n * @note This API is extended by both \"msw/node\" and \"msw/native\"\n * so be minding about the things you import!\n */\nimport type { RequiredDeep } from 'type-fest'\nimport { invariant } from 'outvariant'\nimport {\n BatchInterceptor,\n InterceptorReadyState,\n type HttpRequestEventMap,\n type Interceptor,\n} from '@mswjs/interceptors'\nimport type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { SetupApi } from '~/core/SetupApi'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { InternalError, devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\nimport { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'\nimport { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'\nimport { isHandlerKind } from '~/core/utils/internal/isHandlerKind'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerCommonApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServerCommon\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<{ new (): Interceptor<HttpRequestEventMap> }>,\n handlers: Array<RequestHandler | WebSocketHandler>,\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors: interceptors.map((Interceptor) => new Interceptor()),\n })\n\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on(\n 'request',\n async ({ request, requestId, controller }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.handlersController\n .currentHandlers()\n .filter(isHandlerKind('RequestHandler')),\n this.resolvedOptions,\n this.emitter,\n {\n onPassthroughResponse(request) {\n const acceptHeader = request.headers.get('accept')\n\n /**\n * @note Remove the internal bypass request header.\n * In the browser, this is done by the worker script.\n * In Node.js, it has to be done here.\n */\n if (acceptHeader) {\n const nextAcceptHeader = acceptHeader.replace(\n /(,\\s+)?msw\\/passthrough/,\n '',\n )\n\n if (nextAcceptHeader) {\n request.headers.set('accept', nextAcceptHeader)\n } else {\n request.headers.delete('accept')\n }\n }\n },\n },\n )\n\n if (response) {\n controller.respondWith(response)\n }\n\n return\n },\n )\n\n this.interceptor.on('unhandledException', ({ error }) => {\n if (error instanceof InternalError) {\n throw error\n }\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 // Preconfigure the WebSocket interception but don't enable it just yet.\n // It will be enabled when the server starts.\n handleWebSocketEvent({\n getUnhandledRequestStrategy: () => {\n return this.resolvedOptions.onUnhandledRequest\n },\n getHandlers: () => {\n return this.handlersController.currentHandlers()\n },\n onMockedConnection: () => {},\n onPassthroughConnection: () => {},\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 // Attach the event listeners to the interceptor here\n // so they get re-attached whenever `.listen()` is called.\n this.interceptor.apply()\n this.init()\n this.subscriptions.push(() => this.interceptor.dispose())\n\n // Apply the WebSocket interception.\n webSocketInterceptor.apply()\n this.subscriptions.push(() => webSocketInterceptor.dispose())\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 type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { SetupServerApi } from './SetupServerApi'\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 | WebSocketHandler>\n): SetupServerApi => {\n return new SetupServerApi(handlers)\n}\n"],"mappings":";AAAA,SAAS,yBAAyB;AAClC,SAAS,gCAAgC;AACzC,SAAS,iCAAiC;AAC1C,SAAS,wBAAwB;;;ACEjC,SAAS,iBAAiB;AAC1B;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAEP,SAAS,gBAAgB;AACzB,SAAS,qBAAqB;AAG9B,SAAS,kBAAkB;AAC3B,SAAS,eAAe,gBAAgB;AAExC,SAAS,4BAA4B;AACrC,SAAS,4BAA4B;AACrC,SAAS,qBAAqB;AAE9B,IAAM,yBAAsD;AAAA,EAC1D,oBAAoB;AACtB;AAEO,IAAM,uBAAN,cACG,SAEV;AAAA,EACqB;AAAA,EAIX;AAAA,EAER,YACE,cACA,UACA;AACA,UAAM,GAAG,QAAQ;AAEjB,SAAK,cAAc,IA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AAO,SAAS,OAAO,eAAe;AAAA,EACxC;AACF;AAEO,IAAM,iBAAN,cACG,qBAEV;AAAA,EACE,YAAY,UAAoD;AAC9D;AAAA,MACE,CAAC,0BAA0B,2BAA2B,gBAAgB;AAAA,MACtE;AAAA,IACF;AAEA,SAAK,qBAAqB,IAAI,wBAAwB,QAAQ;AAAA,EAChE;AAAA,EAEO,SACL,UACsB;AACtB,WAAO,IAAI,SAAkB;AAC3B,aAAO,MAAM;AAAA,QACX;AAAA,UACE,iBAAiB,KAAK,mBAAmB,gBAAgB;AAAA,UACzD,UAAU,CAAC;AAAA,QACb;AAAA,QACA;AAAA,QACA,GAAG;AAAA,MACL;AAAA,IACF;AAAA,EACF;AAAA,EAEO,QAAc;AACnB,UAAM,MAAM;AACZ,UAAM,QAAQ;AAAA,EAChB;AACF;;;AExEO,IAAM,cAAc,IACtB,aACgB;AACnB,SAAO,IAAI,eAAe,QAAQ;AACpC;","names":["request"]}
1
+ {"version":3,"sources":["../../src/node/SetupServerApi.ts","../../src/node/SetupServerCommonApi.ts","../../src/node/setupServer.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks'\nimport type { HttpRequestEventMap, Interceptor } from '@mswjs/interceptors'\nimport { ClientRequestInterceptor } from '@mswjs/interceptors/ClientRequest'\nimport { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'\nimport { FetchInterceptor } from '@mswjs/interceptors/fetch'\nimport { HandlersController } from '~/core/SetupApi'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport type { SetupServer } from './glossary'\nimport { SetupServerCommonApi } from './SetupServerCommonApi'\n\nconst store = new AsyncLocalStorage<RequestHandlersContext>()\n\ntype RequestHandlersContext = {\n initialHandlers: Array<RequestHandler | WebSocketHandler>\n handlers: Array<RequestHandler | WebSocketHandler>\n}\n\n/**\n * A handlers controller that utilizes `AsyncLocalStorage` in Node.js\n * to prevent the request handlers list from being a shared state\n * across mutliple tests.\n */\nclass AsyncHandlersController implements HandlersController {\n private rootContext: RequestHandlersContext\n\n constructor(initialHandlers: Array<RequestHandler | WebSocketHandler>) {\n this.rootContext = { initialHandlers, handlers: [] }\n }\n\n get context(): RequestHandlersContext {\n return store.getStore() || this.rootContext\n }\n\n public prepend(runtimeHandlers: Array<RequestHandler | WebSocketHandler>) {\n this.context.handlers.unshift(...runtimeHandlers)\n }\n\n public reset(nextHandlers: Array<RequestHandler | WebSocketHandler>) {\n const context = this.context\n context.handlers = []\n context.initialHandlers =\n nextHandlers.length > 0 ? nextHandlers : context.initialHandlers\n }\n\n public currentHandlers(): Array<RequestHandler | WebSocketHandler> {\n const { initialHandlers, handlers } = this.context\n return handlers.concat(initialHandlers)\n }\n}\nexport class SetupServerApi\n extends SetupServerCommonApi\n implements SetupServer\n{\n constructor(\n handlers: Array<RequestHandler | WebSocketHandler>,\n interceptors: Array<Interceptor<HttpRequestEventMap>> = [\n new ClientRequestInterceptor(),\n new XMLHttpRequestInterceptor(),\n new FetchInterceptor(),\n ],\n ) {\n super(interceptors, handlers)\n\n this.handlersController = new AsyncHandlersController(handlers)\n }\n\n public boundary<Args extends Array<any>, R>(\n callback: (...args: Args) => R,\n ): (...args: Args) => R {\n return (...args: Args): R => {\n return store.run<any, any>(\n {\n initialHandlers: this.handlersController.currentHandlers(),\n handlers: [],\n },\n callback,\n ...args,\n )\n }\n }\n\n public close(): void {\n super.close()\n store.disable()\n }\n}\n","/**\n * @note This API is extended by both \"msw/node\" and \"msw/native\"\n * so be minding about the things you import!\n */\nimport type { RequiredDeep } from 'type-fest'\nimport { invariant } from 'outvariant'\nimport {\n BatchInterceptor,\n InterceptorReadyState,\n type HttpRequestEventMap,\n type Interceptor,\n} from '@mswjs/interceptors'\nimport type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'\nimport { SetupApi } from '~/core/SetupApi'\nimport { handleRequest } from '~/core/utils/handleRequest'\nimport type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { InternalError, devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\nimport { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'\nimport { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'\nimport { isHandlerKind } from '~/core/utils/internal/isHandlerKind'\n\nconst DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {\n onUnhandledRequest: 'warn',\n}\n\nexport class SetupServerCommonApi\n extends SetupApi<LifeCycleEventsMap>\n implements SetupServerCommon\n{\n protected readonly interceptor: BatchInterceptor<\n Array<Interceptor<HttpRequestEventMap>>,\n HttpRequestEventMap\n >\n private resolvedOptions: RequiredDeep<SharedOptions>\n\n constructor(\n interceptors: Array<Interceptor<HttpRequestEventMap>>,\n handlers: Array<RequestHandler | WebSocketHandler>,\n ) {\n super(...handlers)\n\n this.interceptor = new BatchInterceptor({\n name: 'setup-server',\n interceptors,\n })\n\n this.resolvedOptions = {} as RequiredDeep<SharedOptions>\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on(\n 'request',\n async ({ request, requestId, controller }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.handlersController\n .currentHandlers()\n .filter(isHandlerKind('RequestHandler')),\n this.resolvedOptions,\n this.emitter,\n {\n onPassthroughResponse(request) {\n const acceptHeader = request.headers.get('accept')\n\n /**\n * @note Remove the internal bypass request header.\n * In the browser, this is done by the worker script.\n * In Node.js, it has to be done here.\n */\n if (acceptHeader) {\n const nextAcceptHeader = acceptHeader.replace(\n /(,\\s+)?msw\\/passthrough/,\n '',\n )\n\n if (nextAcceptHeader) {\n request.headers.set('accept', nextAcceptHeader)\n } else {\n request.headers.delete('accept')\n }\n }\n },\n },\n )\n\n if (response) {\n controller.respondWith(response)\n }\n\n return\n },\n )\n\n this.interceptor.on('unhandledException', ({ error }) => {\n if (error instanceof InternalError) {\n throw error\n }\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 // Preconfigure the WebSocket interception but don't enable it just yet.\n // It will be enabled when the server starts.\n handleWebSocketEvent({\n getUnhandledRequestStrategy: () => {\n return this.resolvedOptions.onUnhandledRequest\n },\n getHandlers: () => {\n return this.handlersController.currentHandlers()\n },\n onMockedConnection: () => {},\n onPassthroughConnection: () => {},\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 // Attach the event listeners to the interceptor here\n // so they get re-attached whenever `.listen()` is called.\n this.interceptor.apply()\n this.init()\n this.subscriptions.push(() => this.interceptor.dispose())\n\n // Apply the WebSocket interception.\n webSocketInterceptor.apply()\n this.subscriptions.push(() => webSocketInterceptor.dispose())\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 type { RequestHandler } from '~/core/handlers/RequestHandler'\nimport type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'\nimport { SetupServerApi } from './SetupServerApi'\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: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"../lib/native/index.mjs",
5
- "types": "../lib/native/index.d.ts"
6
+ "types": "../lib/native/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "react-native": {
10
+ "import": {
11
+ "types": "./../lib/native/index.d.mts",
12
+ "default": "./../lib/native/index.mjs"
13
+ },
14
+ "default": {
15
+ "types": "./../lib/native/index.d.ts",
16
+ "default": "./../lib/native/index.js"
17
+ }
18
+ },
19
+ "browser": null,
20
+ "import": {
21
+ "types": "./../lib/native/index.d.mts",
22
+ "default": "./../lib/native/index.mjs"
23
+ },
24
+ "default": {
25
+ "types": "./../lib/native/index.d.ts",
26
+ "default": "./../lib/native/index.js"
27
+ }
28
+ }
29
+ }
6
30
  }
package/node/package.json CHANGED
@@ -1,6 +1,33 @@
1
1
  {
2
+ "type": "commonjs",
2
3
  "browser": null,
3
4
  "main": "../lib/node/index.js",
4
5
  "module": "../lib/node/index.mjs",
5
- "types": "../lib/node/index.d.ts"
6
+ "types": "../lib/node/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "module-sync": {
10
+ "types": "./../lib/node/index.d.mts",
11
+ "default": "./../lib/node/index.mjs"
12
+ },
13
+ "module": {
14
+ "types": "./../lib/node/index.d.mts",
15
+ "default": "./../lib/node/index.mjs"
16
+ },
17
+ "node": {
18
+ "require": "./../lib/node/index.js",
19
+ "import": "./../lib/node/index.mjs"
20
+ },
21
+ "import": {
22
+ "types": "./../lib/node/index.d.mts",
23
+ "default": "./../lib/node/index.mjs"
24
+ },
25
+ "browser": null,
26
+ "react-native": null,
27
+ "default": {
28
+ "types": "./../lib/node/index.d.ts",
29
+ "default": "./../lib/node/index.js"
30
+ }
31
+ }
32
+ }
6
33
  }