msw 2.1.7 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/cli/init.js +9 -13
  2. package/lib/browser/index.js +12 -8
  3. package/lib/browser/index.js.map +1 -1
  4. package/lib/browser/index.mjs +12 -8
  5. package/lib/browser/index.mjs.map +1 -1
  6. package/lib/core/HttpResponse.js +14 -1
  7. package/lib/core/HttpResponse.js.map +1 -1
  8. package/lib/core/HttpResponse.mjs +14 -1
  9. package/lib/core/HttpResponse.mjs.map +1 -1
  10. package/lib/core/SetupApi.d.mts +15 -3
  11. package/lib/core/SetupApi.d.ts +15 -3
  12. package/lib/core/SetupApi.js +26 -8
  13. package/lib/core/SetupApi.js.map +1 -1
  14. package/lib/core/SetupApi.mjs +26 -8
  15. package/lib/core/SetupApi.mjs.map +1 -1
  16. package/lib/iife/index.js +48 -17
  17. package/lib/iife/index.js.map +1 -1
  18. package/lib/mockServiceWorker.js +1 -1
  19. package/lib/native/index.d.mts +10 -5
  20. package/lib/native/index.d.ts +10 -5
  21. package/lib/native/index.js +9 -9
  22. package/lib/native/index.js.map +1 -1
  23. package/lib/native/index.mjs +9 -9
  24. package/lib/native/index.mjs.map +1 -1
  25. package/lib/node/index.d.mts +26 -4
  26. package/lib/node/index.d.ts +26 -4
  27. package/lib/node/index.js +62 -13
  28. package/lib/node/index.js.map +1 -1
  29. package/lib/node/index.mjs +62 -13
  30. package/lib/node/index.mjs.map +1 -1
  31. package/package.json +15 -7
  32. package/src/browser/setupWorker/glossary.ts +1 -19
  33. package/src/browser/setupWorker/setupWorker.ts +3 -11
  34. package/src/browser/setupWorker/start/createFallbackRequestListener.ts +1 -1
  35. package/src/browser/setupWorker/start/createRequestListener.ts +1 -1
  36. package/src/browser/setupWorker/start/createResponseListener.ts +13 -0
  37. package/src/core/HttpResponse.test.ts +34 -3
  38. package/src/core/HttpResponse.ts +24 -1
  39. package/src/core/SetupApi.ts +33 -9
  40. package/src/native/index.ts +5 -5
  41. package/src/node/SetupServerApi.ts +64 -95
  42. package/src/node/SetupServerCommonApi.ts +116 -0
  43. package/src/node/glossary.ts +17 -3
  44. package/src/node/setupServer.ts +3 -10
@@ -0,0 +1,116 @@
1
+ /**
2
+ * @note This API is extended by both "msw/node" and "msw/native"
3
+ * so be minding about the things you import!
4
+ */
5
+ import type { RequiredDeep } from 'type-fest'
6
+ import { invariant } from 'outvariant'
7
+ import {
8
+ BatchInterceptor,
9
+ InterceptorReadyState,
10
+ type HttpRequestEventMap,
11
+ type Interceptor,
12
+ } from '@mswjs/interceptors'
13
+ import type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'
14
+ import { SetupApi } from '~/core/SetupApi'
15
+ import { handleRequest } from '~/core/utils/handleRequest'
16
+ import type { RequestHandler } from '~/core/handlers/RequestHandler'
17
+ import { mergeRight } from '~/core/utils/internal/mergeRight'
18
+ import { devUtils } from '~/core/utils/internal/devUtils'
19
+ import type { SetupServerCommon } from './glossary'
20
+
21
+ export const DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {
22
+ onUnhandledRequest: 'warn',
23
+ }
24
+
25
+ export class SetupServerCommonApi
26
+ extends SetupApi<LifeCycleEventsMap>
27
+ implements SetupServerCommon
28
+ {
29
+ protected readonly interceptor: BatchInterceptor<
30
+ Array<Interceptor<HttpRequestEventMap>>,
31
+ HttpRequestEventMap
32
+ >
33
+ private resolvedOptions: RequiredDeep<SharedOptions>
34
+
35
+ constructor(
36
+ interceptors: Array<{ new (): Interceptor<HttpRequestEventMap> }>,
37
+ handlers: Array<RequestHandler>,
38
+ ) {
39
+ super(...handlers)
40
+
41
+ this.interceptor = new BatchInterceptor({
42
+ name: 'setup-server',
43
+ interceptors: interceptors.map((Interceptor) => new Interceptor()),
44
+ })
45
+
46
+ this.resolvedOptions = {} as RequiredDeep<SharedOptions>
47
+
48
+ this.init()
49
+ }
50
+
51
+ /**
52
+ * Subscribe to all requests that are using the interceptor object
53
+ */
54
+ private init(): void {
55
+ this.interceptor.on('request', async ({ request, requestId }) => {
56
+ const response = await handleRequest(
57
+ request,
58
+ requestId,
59
+ this.handlersController.currentHandlers(),
60
+ this.resolvedOptions,
61
+ this.emitter,
62
+ )
63
+
64
+ if (response) {
65
+ request.respondWith(response)
66
+ }
67
+
68
+ return
69
+ })
70
+
71
+ this.interceptor.on(
72
+ 'response',
73
+ ({ response, isMockedResponse, request, requestId }) => {
74
+ this.emitter.emit(
75
+ isMockedResponse ? 'response:mocked' : 'response:bypass',
76
+ {
77
+ response,
78
+ request,
79
+ requestId,
80
+ },
81
+ )
82
+ },
83
+ )
84
+ }
85
+
86
+ public listen(options: Partial<SharedOptions> = {}): void {
87
+ this.resolvedOptions = mergeRight(
88
+ DEFAULT_LISTEN_OPTIONS,
89
+ options,
90
+ ) as RequiredDeep<SharedOptions>
91
+
92
+ // Apply the interceptor when starting the server.
93
+ this.interceptor.apply()
94
+
95
+ this.subscriptions.push(() => {
96
+ this.interceptor.dispose()
97
+ })
98
+
99
+ // Assert that the interceptor has been applied successfully.
100
+ // Also guards us from forgetting to call "interceptor.apply()"
101
+ // as a part of the "listen" method.
102
+ invariant(
103
+ [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(
104
+ this.interceptor.readyState,
105
+ ),
106
+ devUtils.formatMessage(
107
+ 'Failed to start "setupServer": the interceptor failed to apply. This is likely an issue with the library and you should report it at "%s".',
108
+ ),
109
+ 'https://github.com/mswjs/msw/issues/new/choose',
110
+ )
111
+ }
112
+
113
+ public close(): void {
114
+ this.dispose()
115
+ }
116
+ }
@@ -1,15 +1,15 @@
1
1
  import type { PartialDeep } from 'type-fest'
2
- import {
2
+ import type {
3
3
  RequestHandler,
4
4
  RequestHandlerDefaultInfo,
5
5
  } from '~/core/handlers/RequestHandler'
6
- import {
6
+ import type {
7
7
  LifeCycleEventEmitter,
8
8
  LifeCycleEventsMap,
9
9
  SharedOptions,
10
10
  } from '~/core/sharedOptions'
11
11
 
12
- export interface SetupServer {
12
+ export interface SetupServerCommon {
13
13
  /**
14
14
  * Starts requests interception based on the previously provided request handlers.
15
15
  *
@@ -60,3 +60,17 @@ export interface SetupServer {
60
60
  */
61
61
  events: LifeCycleEventEmitter<LifeCycleEventsMap>
62
62
  }
63
+
64
+ export interface SetupServer extends SetupServerCommon {
65
+ /**
66
+ * Wraps the given function in a boundary. Any changes to the
67
+ * network behavior (e.g. adding runtime request handlers via
68
+ * `server.use()`) will be scoped to this boundary only.
69
+ * @param callback A function to run (e.g. a test)
70
+ *
71
+ * @see {@link https://mswjs.io/docs/api/setup-server/boundary `server.boundary()` API reference}
72
+ */
73
+ boundary<Fn extends (...args: Array<any>) => unknown>(
74
+ callback: Fn,
75
+ ): (...args: Parameters<Fn>) => ReturnType<Fn>
76
+ }
@@ -1,9 +1,5 @@
1
- import { ClientRequestInterceptor } from '@mswjs/interceptors/ClientRequest'
2
- import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'
3
- import { FetchInterceptor } from '@mswjs/interceptors/fetch'
4
- import { RequestHandler } from '~/core/handlers/RequestHandler'
1
+ import type { RequestHandler } from '~/core/handlers/RequestHandler'
5
2
  import { SetupServerApi } from './SetupServerApi'
6
- import { SetupServer } from './glossary'
7
3
 
8
4
  /**
9
5
  * Sets up a requests interception in Node.js with the given request handlers.
@@ -13,9 +9,6 @@ import { SetupServer } from './glossary'
13
9
  */
14
10
  export const setupServer = (
15
11
  ...handlers: Array<RequestHandler>
16
- ): SetupServer => {
17
- return new SetupServerApi(
18
- [ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],
19
- ...handlers,
20
- )
12
+ ): SetupServerApi => {
13
+ return new SetupServerApi(handlers)
21
14
  }