msw 2.1.7 → 2.2.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.
- package/cli/init.js +9 -13
- package/lib/browser/index.js +12 -8
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +12 -8
- package/lib/browser/index.mjs.map +1 -1
- package/lib/core/HttpResponse.js +14 -1
- package/lib/core/HttpResponse.js.map +1 -1
- package/lib/core/HttpResponse.mjs +14 -1
- package/lib/core/HttpResponse.mjs.map +1 -1
- package/lib/core/SetupApi.d.mts +15 -3
- package/lib/core/SetupApi.d.ts +15 -3
- package/lib/core/SetupApi.js +26 -8
- package/lib/core/SetupApi.js.map +1 -1
- package/lib/core/SetupApi.mjs +26 -8
- package/lib/core/SetupApi.mjs.map +1 -1
- package/lib/iife/index.js +48 -17
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +1 -1
- package/lib/native/index.d.mts +10 -5
- package/lib/native/index.d.ts +10 -5
- package/lib/native/index.js +9 -9
- package/lib/native/index.js.map +1 -1
- package/lib/native/index.mjs +9 -9
- package/lib/native/index.mjs.map +1 -1
- package/lib/node/index.d.mts +26 -4
- package/lib/node/index.d.ts +26 -4
- package/lib/node/index.js +62 -13
- package/lib/node/index.js.map +1 -1
- package/lib/node/index.mjs +62 -13
- package/lib/node/index.mjs.map +1 -1
- package/package.json +15 -7
- package/src/browser/setupWorker/glossary.ts +1 -1
- package/src/browser/setupWorker/setupWorker.ts +3 -11
- package/src/browser/setupWorker/start/createFallbackRequestListener.ts +1 -1
- package/src/browser/setupWorker/start/createRequestListener.ts +1 -1
- package/src/browser/setupWorker/start/createResponseListener.ts +13 -0
- package/src/core/HttpResponse.test.ts +34 -3
- package/src/core/HttpResponse.ts +24 -1
- package/src/core/SetupApi.ts +33 -9
- package/src/native/index.ts +5 -5
- package/src/node/SetupServerApi.ts +64 -95
- package/src/node/SetupServerCommonApi.ts +116 -0
- package/src/node/glossary.ts +17 -3
- package/src/node/setupServer.ts +3 -10
package/lib/mockServiceWorker.js
CHANGED
package/lib/native/index.d.mts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { RequestHandler, RequestHandlerDefaultInfo } from '~/core/handlers/RequestHandler';
|
|
2
2
|
import { BatchInterceptor, Interceptor, HttpRequestEventMap } from '@mswjs/interceptors';
|
|
3
|
-
import { SetupApi } from '~/core/SetupApi';
|
|
4
3
|
import { SharedOptions, LifeCycleEventEmitter, LifeCycleEventsMap } from '~/core/sharedOptions';
|
|
4
|
+
import { SetupApi } from '~/core/SetupApi';
|
|
5
5
|
import { PartialDeep } from 'type-fest';
|
|
6
6
|
|
|
7
|
-
interface
|
|
7
|
+
interface SetupServerCommon {
|
|
8
8
|
/**
|
|
9
9
|
* Starts requests interception based on the previously provided request handlers.
|
|
10
10
|
*
|
|
@@ -50,12 +50,17 @@ interface SetupServer {
|
|
|
50
50
|
events: LifeCycleEventEmitter<LifeCycleEventsMap>;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
/**
|
|
54
|
+
* @note This API is extended by both "msw/node" and "msw/native"
|
|
55
|
+
* so be minding about the things you import!
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
declare class SetupServerCommonApi extends SetupApi<LifeCycleEventsMap> implements SetupServerCommon {
|
|
54
59
|
protected readonly interceptor: BatchInterceptor<Array<Interceptor<HttpRequestEventMap>>, HttpRequestEventMap>;
|
|
55
60
|
private resolvedOptions;
|
|
56
61
|
constructor(interceptors: Array<{
|
|
57
62
|
new (): Interceptor<HttpRequestEventMap>;
|
|
58
|
-
}>,
|
|
63
|
+
}>, handlers: Array<RequestHandler>);
|
|
59
64
|
/**
|
|
60
65
|
* Subscribe to all requests that are using the interceptor object
|
|
61
66
|
*/
|
|
@@ -70,6 +75,6 @@ declare class SetupServerApi extends SetupApi<LifeCycleEventsMap> implements Set
|
|
|
70
75
|
*
|
|
71
76
|
* @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}
|
|
72
77
|
*/
|
|
73
|
-
declare function setupServer(...handlers: Array<RequestHandler>):
|
|
78
|
+
declare function setupServer(...handlers: Array<RequestHandler>): SetupServerCommonApi;
|
|
74
79
|
|
|
75
80
|
export { setupServer };
|
package/lib/native/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { RequestHandler, RequestHandlerDefaultInfo } from "../core/handlers/RequestHandler";
|
|
2
2
|
import { BatchInterceptor, Interceptor, HttpRequestEventMap } from '@mswjs/interceptors';
|
|
3
|
-
import { SetupApi } from "../core/SetupApi";
|
|
4
3
|
import { SharedOptions, LifeCycleEventEmitter, LifeCycleEventsMap } from "../core/sharedOptions";
|
|
4
|
+
import { SetupApi } from "../core/SetupApi";
|
|
5
5
|
import { PartialDeep } from 'type-fest';
|
|
6
6
|
|
|
7
|
-
interface
|
|
7
|
+
interface SetupServerCommon {
|
|
8
8
|
/**
|
|
9
9
|
* Starts requests interception based on the previously provided request handlers.
|
|
10
10
|
*
|
|
@@ -50,12 +50,17 @@ interface SetupServer {
|
|
|
50
50
|
events: LifeCycleEventEmitter<LifeCycleEventsMap>;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
/**
|
|
54
|
+
* @note This API is extended by both "msw/node" and "msw/native"
|
|
55
|
+
* so be minding about the things you import!
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
declare class SetupServerCommonApi extends SetupApi<LifeCycleEventsMap> implements SetupServerCommon {
|
|
54
59
|
protected readonly interceptor: BatchInterceptor<Array<Interceptor<HttpRequestEventMap>>, HttpRequestEventMap>;
|
|
55
60
|
private resolvedOptions;
|
|
56
61
|
constructor(interceptors: Array<{
|
|
57
62
|
new (): Interceptor<HttpRequestEventMap>;
|
|
58
|
-
}>,
|
|
63
|
+
}>, handlers: Array<RequestHandler>);
|
|
59
64
|
/**
|
|
60
65
|
* Subscribe to all requests that are using the interceptor object
|
|
61
66
|
*/
|
|
@@ -70,6 +75,6 @@ declare class SetupServerApi extends SetupApi<LifeCycleEventsMap> implements Set
|
|
|
70
75
|
*
|
|
71
76
|
* @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}
|
|
72
77
|
*/
|
|
73
|
-
declare function setupServer(...handlers: Array<RequestHandler>):
|
|
78
|
+
declare function setupServer(...handlers: Array<RequestHandler>): SetupServerCommonApi;
|
|
74
79
|
|
|
75
80
|
export { setupServer };
|
package/lib/native/index.js
CHANGED
|
@@ -26,24 +26,24 @@ module.exports = __toCommonJS(native_exports);
|
|
|
26
26
|
var import_fetch = require("@mswjs/interceptors/fetch");
|
|
27
27
|
var import_XMLHttpRequest = require("@mswjs/interceptors/XMLHttpRequest");
|
|
28
28
|
|
|
29
|
-
// src/node/
|
|
30
|
-
var import_interceptors = require("@mswjs/interceptors");
|
|
29
|
+
// src/node/SetupServerCommonApi.ts
|
|
31
30
|
var import_outvariant = require("outvariant");
|
|
31
|
+
var import_interceptors = require("@mswjs/interceptors");
|
|
32
32
|
var import_SetupApi = require("../core/SetupApi.js");
|
|
33
33
|
var import_handleRequest = require("../core/utils/handleRequest.js");
|
|
34
|
-
var import_devUtils = require("../core/utils/internal/devUtils.js");
|
|
35
34
|
var import_mergeRight = require("../core/utils/internal/mergeRight.js");
|
|
35
|
+
var import_devUtils = require("../core/utils/internal/devUtils.js");
|
|
36
36
|
var DEFAULT_LISTEN_OPTIONS = {
|
|
37
37
|
onUnhandledRequest: "warn"
|
|
38
38
|
};
|
|
39
|
-
var
|
|
39
|
+
var SetupServerCommonApi = class extends import_SetupApi.SetupApi {
|
|
40
40
|
interceptor;
|
|
41
41
|
resolvedOptions;
|
|
42
|
-
constructor(interceptors,
|
|
42
|
+
constructor(interceptors, handlers) {
|
|
43
43
|
super(...handlers);
|
|
44
44
|
this.interceptor = new import_interceptors.BatchInterceptor({
|
|
45
45
|
name: "setup-server",
|
|
46
|
-
interceptors: interceptors.map((
|
|
46
|
+
interceptors: interceptors.map((Interceptor) => new Interceptor())
|
|
47
47
|
});
|
|
48
48
|
this.resolvedOptions = {};
|
|
49
49
|
this.init();
|
|
@@ -56,7 +56,7 @@ var SetupServerApi = class extends import_SetupApi.SetupApi {
|
|
|
56
56
|
const response = await (0, import_handleRequest.handleRequest)(
|
|
57
57
|
request,
|
|
58
58
|
requestId,
|
|
59
|
-
this.currentHandlers,
|
|
59
|
+
this.handlersController.currentHandlers(),
|
|
60
60
|
this.resolvedOptions,
|
|
61
61
|
this.emitter
|
|
62
62
|
);
|
|
@@ -105,9 +105,9 @@ var SetupServerApi = class extends import_SetupApi.SetupApi {
|
|
|
105
105
|
|
|
106
106
|
// src/native/index.ts
|
|
107
107
|
function setupServer(...handlers) {
|
|
108
|
-
return new
|
|
108
|
+
return new SetupServerCommonApi(
|
|
109
109
|
[import_fetch.FetchInterceptor, import_XMLHttpRequest.XMLHttpRequestInterceptor],
|
|
110
|
-
|
|
110
|
+
handlers
|
|
111
111
|
);
|
|
112
112
|
}
|
|
113
113
|
// Annotate the CommonJS export names for ESM import in node:
|
package/lib/native/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/native/index.ts","../../src/node/
|
|
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 { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\n\nexport const 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>,\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 this.init()\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on('request', async ({ request, requestId }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.handlersController.currentHandlers(),\n this.resolvedOptions,\n this.emitter,\n )\n\n if (response) {\n request.respondWith(response)\n }\n\n return\n })\n\n this.interceptor.on(\n 'response',\n ({ response, isMockedResponse, request, requestId }) => {\n this.emitter.emit(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n response,\n request,\n requestId,\n },\n )\n },\n )\n }\n\n public listen(options: Partial<SharedOptions> = {}): void {\n this.resolvedOptions = mergeRight(\n DEFAULT_LISTEN_OPTIONS,\n options,\n ) as RequiredDeep<SharedOptions>\n\n // Apply the interceptor when starting the server.\n this.interceptor.apply()\n\n this.subscriptions.push(() => {\n this.interceptor.dispose()\n })\n\n // Assert that the interceptor has been applied successfully.\n // Also guards us from forgetting to call \"interceptor.apply()\"\n // as a part of the \"listen\" method.\n invariant(\n [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(\n this.interceptor.readyState,\n ),\n devUtils.formatMessage(\n 'Failed to start \"setupServer\": the interceptor failed to apply. This is likely an issue with the library and you should report it at \"%s\".',\n ),\n 'https://github.com/mswjs/msw/issues/new/choose',\n )\n }\n\n public close(): void {\n this.dispose()\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAiC;AACjC,4BAA0C;;;ACI1C,wBAA0B;AAC1B,0BAKO;AAEP,sBAAyB;AACzB,2BAA8B;AAE9B,wBAA2B;AAC3B,sBAAyB;AAGlB,IAAM,yBAAsD;AAAA,EACjE,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;AAExB,SAAK,KAAK;AAAA,EACZ;AAAA;AAAA;AAAA;AAAA,EAKQ,OAAa;AACnB,SAAK,YAAY,GAAG,WAAW,OAAO,EAAE,SAAS,UAAU,MAAM;AAC/D,YAAM,WAAW,UAAM;AAAA,QACrB;AAAA,QACA;AAAA,QACA,KAAK,mBAAmB,gBAAgB;AAAA,QACxC,KAAK;AAAA,QACL,KAAK;AAAA,MACP;AAEA,UAAI,UAAU;AACZ,gBAAQ,YAAY,QAAQ;AAAA,MAC9B;AAEA;AAAA,IACF,CAAC;AAED,SAAK,YAAY;AAAA,MACf;AAAA,MACA,CAAC,EAAE,UAAU,kBAAkB,SAAS,UAAU,MAAM;AACtD,aAAK,QAAQ;AAAA,UACX,mBAAmB,oBAAoB;AAAA,UACvC;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEO,OAAO,UAAkC,CAAC,GAAS;AACxD,SAAK,sBAAkB;AAAA,MACrB;AAAA,MACA;AAAA,IACF;AAGA,SAAK,YAAY,MAAM;AAEvB,SAAK,cAAc,KAAK,MAAM;AAC5B,WAAK,YAAY,QAAQ;AAAA,IAC3B,CAAC;AAKD;AAAA,MACE,CAAC,0CAAsB,UAAU,0CAAsB,OAAO,EAAE;AAAA,QAC9D,KAAK,YAAY;AAAA,MACnB;AAAA,MACA,yBAAS;AAAA,QACP;AAAA,MACF;AAAA,MACA;AAAA,IACF;AAAA,EACF;AAAA,EAEO,QAAc;AACnB,SAAK,QAAQ;AAAA,EACf;AACF;;;ADxGO,SAAS,eACX,UACmB;AAGtB,SAAO,IAAI;AAAA,IACT,CAAC,+BAAkB,+CAAyB;AAAA,IAC5C;AAAA,EACF;AACF;","names":[]}
|
package/lib/native/index.mjs
CHANGED
|
@@ -2,27 +2,27 @@
|
|
|
2
2
|
import { FetchInterceptor } from "@mswjs/interceptors/fetch";
|
|
3
3
|
import { XMLHttpRequestInterceptor } from "@mswjs/interceptors/XMLHttpRequest";
|
|
4
4
|
|
|
5
|
-
// src/node/
|
|
5
|
+
// src/node/SetupServerCommonApi.ts
|
|
6
|
+
import { invariant } from "outvariant";
|
|
6
7
|
import {
|
|
7
8
|
BatchInterceptor,
|
|
8
9
|
InterceptorReadyState
|
|
9
10
|
} from "@mswjs/interceptors";
|
|
10
|
-
import { invariant } from "outvariant";
|
|
11
11
|
import { SetupApi } from '../core/SetupApi.mjs';
|
|
12
12
|
import { handleRequest } from '../core/utils/handleRequest.mjs';
|
|
13
|
-
import { devUtils } from '../core/utils/internal/devUtils.mjs';
|
|
14
13
|
import { mergeRight } from '../core/utils/internal/mergeRight.mjs';
|
|
14
|
+
import { devUtils } from '../core/utils/internal/devUtils.mjs';
|
|
15
15
|
var DEFAULT_LISTEN_OPTIONS = {
|
|
16
16
|
onUnhandledRequest: "warn"
|
|
17
17
|
};
|
|
18
|
-
var
|
|
18
|
+
var SetupServerCommonApi = class extends SetupApi {
|
|
19
19
|
interceptor;
|
|
20
20
|
resolvedOptions;
|
|
21
|
-
constructor(interceptors,
|
|
21
|
+
constructor(interceptors, handlers) {
|
|
22
22
|
super(...handlers);
|
|
23
23
|
this.interceptor = new BatchInterceptor({
|
|
24
24
|
name: "setup-server",
|
|
25
|
-
interceptors: interceptors.map((
|
|
25
|
+
interceptors: interceptors.map((Interceptor) => new Interceptor())
|
|
26
26
|
});
|
|
27
27
|
this.resolvedOptions = {};
|
|
28
28
|
this.init();
|
|
@@ -35,7 +35,7 @@ var SetupServerApi = class extends SetupApi {
|
|
|
35
35
|
const response = await handleRequest(
|
|
36
36
|
request,
|
|
37
37
|
requestId,
|
|
38
|
-
this.currentHandlers,
|
|
38
|
+
this.handlersController.currentHandlers(),
|
|
39
39
|
this.resolvedOptions,
|
|
40
40
|
this.emitter
|
|
41
41
|
);
|
|
@@ -84,9 +84,9 @@ var SetupServerApi = class extends SetupApi {
|
|
|
84
84
|
|
|
85
85
|
// src/native/index.ts
|
|
86
86
|
function setupServer(...handlers) {
|
|
87
|
-
return new
|
|
87
|
+
return new SetupServerCommonApi(
|
|
88
88
|
[FetchInterceptor, XMLHttpRequestInterceptor],
|
|
89
|
-
|
|
89
|
+
handlers
|
|
90
90
|
);
|
|
91
91
|
}
|
|
92
92
|
export {
|
package/lib/native/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/native/index.ts","../../src/node/
|
|
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 { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\n\nexport const 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>,\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 this.init()\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on('request', async ({ request, requestId }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.handlersController.currentHandlers(),\n this.resolvedOptions,\n this.emitter,\n )\n\n if (response) {\n request.respondWith(response)\n }\n\n return\n })\n\n this.interceptor.on(\n 'response',\n ({ response, isMockedResponse, request, requestId }) => {\n this.emitter.emit(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n response,\n request,\n requestId,\n },\n )\n },\n )\n }\n\n public listen(options: Partial<SharedOptions> = {}): void {\n this.resolvedOptions = mergeRight(\n DEFAULT_LISTEN_OPTIONS,\n options,\n ) as RequiredDeep<SharedOptions>\n\n // Apply the interceptor when starting the server.\n this.interceptor.apply()\n\n this.subscriptions.push(() => {\n this.interceptor.dispose()\n })\n\n // Assert that the interceptor has been applied successfully.\n // Also guards us from forgetting to call \"interceptor.apply()\"\n // as a part of the \"listen\" method.\n invariant(\n [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(\n this.interceptor.readyState,\n ),\n devUtils.formatMessage(\n 'Failed to start \"setupServer\": the interceptor failed to apply. This is likely an issue with the library and you should report it at \"%s\".',\n ),\n 'https://github.com/mswjs/msw/issues/new/choose',\n )\n }\n\n public close(): void {\n this.dispose()\n }\n}\n"],"mappings":";AAAA,SAAS,wBAAwB;AACjC,SAAS,iCAAiC;;;ACI1C,SAAS,iBAAiB;AAC1B;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAEP,SAAS,gBAAgB;AACzB,SAAS,qBAAqB;AAE9B,SAAS,kBAAkB;AAC3B,SAAS,gBAAgB;AAGlB,IA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O,QAAc;AACnB,SAAK,QAAQ;AAAA,EACf;AACF;;;ADxGO,SAAS,eACX,UACmB;AAGtB,SAAO,IAAI;AAAA,IACT,CAAC,kBAAkB,yBAAyB;AAAA,IAC5C;AAAA,EACF;AACF;","names":[]}
|
package/lib/node/index.d.mts
CHANGED
|
@@ -4,7 +4,7 @@ import { SharedOptions, LifeCycleEventEmitter, LifeCycleEventsMap } from '~/core
|
|
|
4
4
|
import { BatchInterceptor, Interceptor, HttpRequestEventMap } from '@mswjs/interceptors';
|
|
5
5
|
import { SetupApi } from '~/core/SetupApi';
|
|
6
6
|
|
|
7
|
-
interface
|
|
7
|
+
interface SetupServerCommon {
|
|
8
8
|
/**
|
|
9
9
|
* Starts requests interception based on the previously provided request handlers.
|
|
10
10
|
*
|
|
@@ -49,13 +49,29 @@ interface SetupServer {
|
|
|
49
49
|
*/
|
|
50
50
|
events: LifeCycleEventEmitter<LifeCycleEventsMap>;
|
|
51
51
|
}
|
|
52
|
+
interface SetupServer extends SetupServerCommon {
|
|
53
|
+
/**
|
|
54
|
+
* Wraps the given function in a boundary. Any changes to the
|
|
55
|
+
* network behavior (e.g. adding runtime request handlers via
|
|
56
|
+
* `server.use()`) will be scoped to this boundary only.
|
|
57
|
+
* @param callback A function to run (e.g. a test)
|
|
58
|
+
*
|
|
59
|
+
* @see {@link https://mswjs.io/docs/api/setup-server/boundary `server.boundary()` API reference}
|
|
60
|
+
*/
|
|
61
|
+
boundary<Fn extends (...args: Array<any>) => unknown>(callback: Fn): (...args: Parameters<Fn>) => ReturnType<Fn>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @note This API is extended by both "msw/node" and "msw/native"
|
|
66
|
+
* so be minding about the things you import!
|
|
67
|
+
*/
|
|
52
68
|
|
|
53
|
-
declare class
|
|
69
|
+
declare class SetupServerCommonApi extends SetupApi<LifeCycleEventsMap> implements SetupServerCommon {
|
|
54
70
|
protected readonly interceptor: BatchInterceptor<Array<Interceptor<HttpRequestEventMap>>, HttpRequestEventMap>;
|
|
55
71
|
private resolvedOptions;
|
|
56
72
|
constructor(interceptors: Array<{
|
|
57
73
|
new (): Interceptor<HttpRequestEventMap>;
|
|
58
|
-
}>,
|
|
74
|
+
}>, handlers: Array<RequestHandler>);
|
|
59
75
|
/**
|
|
60
76
|
* Subscribe to all requests that are using the interceptor object
|
|
61
77
|
*/
|
|
@@ -64,12 +80,18 @@ declare class SetupServerApi extends SetupApi<LifeCycleEventsMap> implements Set
|
|
|
64
80
|
close(): void;
|
|
65
81
|
}
|
|
66
82
|
|
|
83
|
+
declare class SetupServerApi extends SetupServerCommonApi implements SetupServer {
|
|
84
|
+
constructor(handlers: Array<RequestHandler>);
|
|
85
|
+
boundary<Fn extends (...args: Array<any>) => unknown>(callback: Fn): (...args: Parameters<Fn>) => ReturnType<Fn>;
|
|
86
|
+
close(): void;
|
|
87
|
+
}
|
|
88
|
+
|
|
67
89
|
/**
|
|
68
90
|
* Sets up a requests interception in Node.js with the given request handlers.
|
|
69
91
|
* @param {RequestHandler[]} handlers List of request handlers.
|
|
70
92
|
*
|
|
71
93
|
* @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}
|
|
72
94
|
*/
|
|
73
|
-
declare const setupServer: (...handlers: Array<RequestHandler>) =>
|
|
95
|
+
declare const setupServer: (...handlers: Array<RequestHandler>) => SetupServerApi;
|
|
74
96
|
|
|
75
97
|
export { type SetupServer, SetupServerApi, setupServer };
|
package/lib/node/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { SharedOptions, LifeCycleEventEmitter, LifeCycleEventsMap } from "../cor
|
|
|
4
4
|
import { BatchInterceptor, Interceptor, HttpRequestEventMap } from '@mswjs/interceptors';
|
|
5
5
|
import { SetupApi } from "../core/SetupApi";
|
|
6
6
|
|
|
7
|
-
interface
|
|
7
|
+
interface SetupServerCommon {
|
|
8
8
|
/**
|
|
9
9
|
* Starts requests interception based on the previously provided request handlers.
|
|
10
10
|
*
|
|
@@ -49,13 +49,29 @@ interface SetupServer {
|
|
|
49
49
|
*/
|
|
50
50
|
events: LifeCycleEventEmitter<LifeCycleEventsMap>;
|
|
51
51
|
}
|
|
52
|
+
interface SetupServer extends SetupServerCommon {
|
|
53
|
+
/**
|
|
54
|
+
* Wraps the given function in a boundary. Any changes to the
|
|
55
|
+
* network behavior (e.g. adding runtime request handlers via
|
|
56
|
+
* `server.use()`) will be scoped to this boundary only.
|
|
57
|
+
* @param callback A function to run (e.g. a test)
|
|
58
|
+
*
|
|
59
|
+
* @see {@link https://mswjs.io/docs/api/setup-server/boundary `server.boundary()` API reference}
|
|
60
|
+
*/
|
|
61
|
+
boundary<Fn extends (...args: Array<any>) => unknown>(callback: Fn): (...args: Parameters<Fn>) => ReturnType<Fn>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* @note This API is extended by both "msw/node" and "msw/native"
|
|
66
|
+
* so be minding about the things you import!
|
|
67
|
+
*/
|
|
52
68
|
|
|
53
|
-
declare class
|
|
69
|
+
declare class SetupServerCommonApi extends SetupApi<LifeCycleEventsMap> implements SetupServerCommon {
|
|
54
70
|
protected readonly interceptor: BatchInterceptor<Array<Interceptor<HttpRequestEventMap>>, HttpRequestEventMap>;
|
|
55
71
|
private resolvedOptions;
|
|
56
72
|
constructor(interceptors: Array<{
|
|
57
73
|
new (): Interceptor<HttpRequestEventMap>;
|
|
58
|
-
}>,
|
|
74
|
+
}>, handlers: Array<RequestHandler>);
|
|
59
75
|
/**
|
|
60
76
|
* Subscribe to all requests that are using the interceptor object
|
|
61
77
|
*/
|
|
@@ -64,12 +80,18 @@ declare class SetupServerApi extends SetupApi<LifeCycleEventsMap> implements Set
|
|
|
64
80
|
close(): void;
|
|
65
81
|
}
|
|
66
82
|
|
|
83
|
+
declare class SetupServerApi extends SetupServerCommonApi implements SetupServer {
|
|
84
|
+
constructor(handlers: Array<RequestHandler>);
|
|
85
|
+
boundary<Fn extends (...args: Array<any>) => unknown>(callback: Fn): (...args: Parameters<Fn>) => ReturnType<Fn>;
|
|
86
|
+
close(): void;
|
|
87
|
+
}
|
|
88
|
+
|
|
67
89
|
/**
|
|
68
90
|
* Sets up a requests interception in Node.js with the given request handlers.
|
|
69
91
|
* @param {RequestHandler[]} handlers List of request handlers.
|
|
70
92
|
*
|
|
71
93
|
* @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}
|
|
72
94
|
*/
|
|
73
|
-
declare const setupServer: (...handlers: Array<RequestHandler>) =>
|
|
95
|
+
declare const setupServer: (...handlers: Array<RequestHandler>) => SetupServerApi;
|
|
74
96
|
|
|
75
97
|
export { type SetupServer, SetupServerApi, setupServer };
|
package/lib/node/index.js
CHANGED
|
@@ -26,23 +26,29 @@ __export(node_exports, {
|
|
|
26
26
|
module.exports = __toCommonJS(node_exports);
|
|
27
27
|
|
|
28
28
|
// src/node/SetupServerApi.ts
|
|
29
|
-
var
|
|
29
|
+
var import_node_async_hooks = require("async_hooks");
|
|
30
|
+
var import_ClientRequest = require("@mswjs/interceptors/ClientRequest");
|
|
31
|
+
var import_XMLHttpRequest = require("@mswjs/interceptors/XMLHttpRequest");
|
|
32
|
+
var import_fetch = require("@mswjs/interceptors/fetch");
|
|
33
|
+
|
|
34
|
+
// src/node/SetupServerCommonApi.ts
|
|
30
35
|
var import_outvariant = require("outvariant");
|
|
36
|
+
var import_interceptors = require("@mswjs/interceptors");
|
|
31
37
|
var import_SetupApi = require("../core/SetupApi.js");
|
|
32
38
|
var import_handleRequest = require("../core/utils/handleRequest.js");
|
|
33
|
-
var import_devUtils = require("../core/utils/internal/devUtils.js");
|
|
34
39
|
var import_mergeRight = require("../core/utils/internal/mergeRight.js");
|
|
40
|
+
var import_devUtils = require("../core/utils/internal/devUtils.js");
|
|
35
41
|
var DEFAULT_LISTEN_OPTIONS = {
|
|
36
42
|
onUnhandledRequest: "warn"
|
|
37
43
|
};
|
|
38
|
-
var
|
|
44
|
+
var SetupServerCommonApi = class extends import_SetupApi.SetupApi {
|
|
39
45
|
interceptor;
|
|
40
46
|
resolvedOptions;
|
|
41
|
-
constructor(interceptors,
|
|
47
|
+
constructor(interceptors, handlers) {
|
|
42
48
|
super(...handlers);
|
|
43
49
|
this.interceptor = new import_interceptors.BatchInterceptor({
|
|
44
50
|
name: "setup-server",
|
|
45
|
-
interceptors: interceptors.map((
|
|
51
|
+
interceptors: interceptors.map((Interceptor) => new Interceptor())
|
|
46
52
|
});
|
|
47
53
|
this.resolvedOptions = {};
|
|
48
54
|
this.init();
|
|
@@ -55,7 +61,7 @@ var SetupServerApi = class extends import_SetupApi.SetupApi {
|
|
|
55
61
|
const response = await (0, import_handleRequest.handleRequest)(
|
|
56
62
|
request,
|
|
57
63
|
requestId,
|
|
58
|
-
this.currentHandlers,
|
|
64
|
+
this.handlersController.currentHandlers(),
|
|
59
65
|
this.resolvedOptions,
|
|
60
66
|
this.emitter
|
|
61
67
|
);
|
|
@@ -102,15 +108,58 @@ var SetupServerApi = class extends import_SetupApi.SetupApi {
|
|
|
102
108
|
}
|
|
103
109
|
};
|
|
104
110
|
|
|
111
|
+
// src/node/SetupServerApi.ts
|
|
112
|
+
var store = new import_node_async_hooks.AsyncLocalStorage();
|
|
113
|
+
var AsyncHandlersController = class {
|
|
114
|
+
rootContext;
|
|
115
|
+
constructor(initialHandlers) {
|
|
116
|
+
this.rootContext = { initialHandlers, handlers: [] };
|
|
117
|
+
}
|
|
118
|
+
get context() {
|
|
119
|
+
return store.getStore() || this.rootContext;
|
|
120
|
+
}
|
|
121
|
+
prepend(runtimeHandlers) {
|
|
122
|
+
this.context.handlers.unshift(...runtimeHandlers);
|
|
123
|
+
}
|
|
124
|
+
reset(nextHandlers) {
|
|
125
|
+
const context = this.context;
|
|
126
|
+
context.handlers = [];
|
|
127
|
+
context.initialHandlers = nextHandlers.length > 0 ? nextHandlers : context.initialHandlers;
|
|
128
|
+
}
|
|
129
|
+
currentHandlers() {
|
|
130
|
+
const { initialHandlers, handlers } = this.context;
|
|
131
|
+
return handlers.concat(initialHandlers);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
var SetupServerApi = class extends SetupServerCommonApi {
|
|
135
|
+
constructor(handlers) {
|
|
136
|
+
super(
|
|
137
|
+
[import_ClientRequest.ClientRequestInterceptor, import_XMLHttpRequest.XMLHttpRequestInterceptor, import_fetch.FetchInterceptor],
|
|
138
|
+
handlers
|
|
139
|
+
);
|
|
140
|
+
this.handlersController = new AsyncHandlersController(handlers);
|
|
141
|
+
}
|
|
142
|
+
boundary(callback) {
|
|
143
|
+
return (...args) => {
|
|
144
|
+
return store.run(
|
|
145
|
+
{
|
|
146
|
+
initialHandlers: this.handlersController.currentHandlers(),
|
|
147
|
+
handlers: []
|
|
148
|
+
},
|
|
149
|
+
callback,
|
|
150
|
+
...args
|
|
151
|
+
);
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
close() {
|
|
155
|
+
super.close();
|
|
156
|
+
store.disable();
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
105
160
|
// src/node/setupServer.ts
|
|
106
|
-
var import_ClientRequest = require("@mswjs/interceptors/ClientRequest");
|
|
107
|
-
var import_XMLHttpRequest = require("@mswjs/interceptors/XMLHttpRequest");
|
|
108
|
-
var import_fetch = require("@mswjs/interceptors/fetch");
|
|
109
161
|
var setupServer = (...handlers) => {
|
|
110
|
-
return new SetupServerApi(
|
|
111
|
-
[import_ClientRequest.ClientRequestInterceptor, import_XMLHttpRequest.XMLHttpRequestInterceptor, import_fetch.FetchInterceptor],
|
|
112
|
-
...handlers
|
|
113
|
-
);
|
|
162
|
+
return new SetupServerApi(handlers);
|
|
114
163
|
};
|
|
115
164
|
// Annotate the CommonJS export names for ESM import in node:
|
|
116
165
|
0 && (module.exports = {
|
package/lib/node/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/node/index.ts","../../src/node/SetupServerApi.ts","../../src/node/setupServer.ts"],"sourcesContent":["export type { SetupServer } from './glossary'\nexport { SetupServerApi } from './SetupServerApi'\nexport { setupServer } from './setupServer'\n","import {\
|
|
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 { SetupServer } from './glossary'\nimport { SetupServerCommonApi } from './SetupServerCommonApi'\n\nconst store = new AsyncLocalStorage<RequestHandlersContext>()\n\ntype RequestHandlersContext = {\n initialHandlers: Array<RequestHandler>\n handlers: Array<RequestHandler>\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>) {\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>) {\n this.context.handlers.unshift(...runtimeHandlers)\n }\n\n public reset(nextHandlers: Array<RequestHandler>) {\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> {\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>) {\n super(\n [ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],\n handlers,\n )\n\n this.handlersController = new AsyncHandlersController(handlers)\n }\n\n public boundary<Fn extends (...args: Array<any>) => unknown>(\n callback: Fn,\n ): (...args: Parameters<Fn>) => ReturnType<Fn> {\n return (...args: Parameters<Fn>): ReturnType<Fn> => {\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 { mergeRight } from '~/core/utils/internal/mergeRight'\nimport { devUtils } from '~/core/utils/internal/devUtils'\nimport type { SetupServerCommon } from './glossary'\n\nexport const 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>,\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 this.init()\n }\n\n /**\n * Subscribe to all requests that are using the interceptor object\n */\n private init(): void {\n this.interceptor.on('request', async ({ request, requestId }) => {\n const response = await handleRequest(\n request,\n requestId,\n this.handlersController.currentHandlers(),\n this.resolvedOptions,\n this.emitter,\n )\n\n if (response) {\n request.respondWith(response)\n }\n\n return\n })\n\n this.interceptor.on(\n 'response',\n ({ response, isMockedResponse, request, requestId }) => {\n this.emitter.emit(\n isMockedResponse ? 'response:mocked' : 'response:bypass',\n {\n response,\n request,\n requestId,\n },\n )\n },\n )\n }\n\n public listen(options: Partial<SharedOptions> = {}): void {\n this.resolvedOptions = mergeRight(\n DEFAULT_LISTEN_OPTIONS,\n options,\n ) as RequiredDeep<SharedOptions>\n\n // Apply the interceptor when starting the server.\n this.interceptor.apply()\n\n this.subscriptions.push(() => {\n this.interceptor.dispose()\n })\n\n // Assert that the interceptor has been applied successfully.\n // Also guards us from forgetting to call \"interceptor.apply()\"\n // as a part of the \"listen\" method.\n invariant(\n [InterceptorReadyState.APPLYING, InterceptorReadyState.APPLIED].includes(\n this.interceptor.readyState,\n ),\n devUtils.formatMessage(\n 'Failed to start \"setupServer\": the interceptor failed to apply. This is likely an issue with the library and you should report it at \"%s\".',\n ),\n 'https://github.com/mswjs/msw/issues/new/choose',\n )\n }\n\n public close(): void {\n this.dispose()\n }\n}\n","import type { RequestHandler } from '~/core/handlers/RequestHandler'\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>\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;AAE9B,wBAA2B;A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,30 @@
|
|
|
1
1
|
// src/node/SetupServerApi.ts
|
|
2
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
|
+
import { ClientRequestInterceptor } from "@mswjs/interceptors/ClientRequest";
|
|
4
|
+
import { XMLHttpRequestInterceptor } from "@mswjs/interceptors/XMLHttpRequest";
|
|
5
|
+
import { FetchInterceptor } from "@mswjs/interceptors/fetch";
|
|
6
|
+
|
|
7
|
+
// src/node/SetupServerCommonApi.ts
|
|
8
|
+
import { invariant } from "outvariant";
|
|
2
9
|
import {
|
|
3
10
|
BatchInterceptor,
|
|
4
11
|
InterceptorReadyState
|
|
5
12
|
} from "@mswjs/interceptors";
|
|
6
|
-
import { invariant } from "outvariant";
|
|
7
13
|
import { SetupApi } from '../core/SetupApi.mjs';
|
|
8
14
|
import { handleRequest } from '../core/utils/handleRequest.mjs';
|
|
9
|
-
import { devUtils } from '../core/utils/internal/devUtils.mjs';
|
|
10
15
|
import { mergeRight } from '../core/utils/internal/mergeRight.mjs';
|
|
16
|
+
import { devUtils } from '../core/utils/internal/devUtils.mjs';
|
|
11
17
|
var DEFAULT_LISTEN_OPTIONS = {
|
|
12
18
|
onUnhandledRequest: "warn"
|
|
13
19
|
};
|
|
14
|
-
var
|
|
20
|
+
var SetupServerCommonApi = class extends SetupApi {
|
|
15
21
|
interceptor;
|
|
16
22
|
resolvedOptions;
|
|
17
|
-
constructor(interceptors,
|
|
23
|
+
constructor(interceptors, handlers) {
|
|
18
24
|
super(...handlers);
|
|
19
25
|
this.interceptor = new BatchInterceptor({
|
|
20
26
|
name: "setup-server",
|
|
21
|
-
interceptors: interceptors.map((
|
|
27
|
+
interceptors: interceptors.map((Interceptor) => new Interceptor())
|
|
22
28
|
});
|
|
23
29
|
this.resolvedOptions = {};
|
|
24
30
|
this.init();
|
|
@@ -31,7 +37,7 @@ var SetupServerApi = class extends SetupApi {
|
|
|
31
37
|
const response = await handleRequest(
|
|
32
38
|
request,
|
|
33
39
|
requestId,
|
|
34
|
-
this.currentHandlers,
|
|
40
|
+
this.handlersController.currentHandlers(),
|
|
35
41
|
this.resolvedOptions,
|
|
36
42
|
this.emitter
|
|
37
43
|
);
|
|
@@ -78,15 +84,58 @@ var SetupServerApi = class extends SetupApi {
|
|
|
78
84
|
}
|
|
79
85
|
};
|
|
80
86
|
|
|
87
|
+
// src/node/SetupServerApi.ts
|
|
88
|
+
var store = new AsyncLocalStorage();
|
|
89
|
+
var AsyncHandlersController = class {
|
|
90
|
+
rootContext;
|
|
91
|
+
constructor(initialHandlers) {
|
|
92
|
+
this.rootContext = { initialHandlers, handlers: [] };
|
|
93
|
+
}
|
|
94
|
+
get context() {
|
|
95
|
+
return store.getStore() || this.rootContext;
|
|
96
|
+
}
|
|
97
|
+
prepend(runtimeHandlers) {
|
|
98
|
+
this.context.handlers.unshift(...runtimeHandlers);
|
|
99
|
+
}
|
|
100
|
+
reset(nextHandlers) {
|
|
101
|
+
const context = this.context;
|
|
102
|
+
context.handlers = [];
|
|
103
|
+
context.initialHandlers = nextHandlers.length > 0 ? nextHandlers : context.initialHandlers;
|
|
104
|
+
}
|
|
105
|
+
currentHandlers() {
|
|
106
|
+
const { initialHandlers, handlers } = this.context;
|
|
107
|
+
return handlers.concat(initialHandlers);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
var SetupServerApi = class extends SetupServerCommonApi {
|
|
111
|
+
constructor(handlers) {
|
|
112
|
+
super(
|
|
113
|
+
[ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],
|
|
114
|
+
handlers
|
|
115
|
+
);
|
|
116
|
+
this.handlersController = new AsyncHandlersController(handlers);
|
|
117
|
+
}
|
|
118
|
+
boundary(callback) {
|
|
119
|
+
return (...args) => {
|
|
120
|
+
return store.run(
|
|
121
|
+
{
|
|
122
|
+
initialHandlers: this.handlersController.currentHandlers(),
|
|
123
|
+
handlers: []
|
|
124
|
+
},
|
|
125
|
+
callback,
|
|
126
|
+
...args
|
|
127
|
+
);
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
close() {
|
|
131
|
+
super.close();
|
|
132
|
+
store.disable();
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
81
136
|
// src/node/setupServer.ts
|
|
82
|
-
import { ClientRequestInterceptor } from "@mswjs/interceptors/ClientRequest";
|
|
83
|
-
import { XMLHttpRequestInterceptor } from "@mswjs/interceptors/XMLHttpRequest";
|
|
84
|
-
import { FetchInterceptor } from "@mswjs/interceptors/fetch";
|
|
85
137
|
var setupServer = (...handlers) => {
|
|
86
|
-
return new SetupServerApi(
|
|
87
|
-
[ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],
|
|
88
|
-
...handlers
|
|
89
|
-
);
|
|
138
|
+
return new SetupServerApi(handlers);
|
|
90
139
|
};
|
|
91
140
|
export {
|
|
92
141
|
SetupServerApi,
|