msw 2.0.8 → 2.0.9
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/lib/browser/index.js +1 -8
- package/lib/browser/index.mjs +1 -8
- package/lib/core/{GraphQLHandler-d4787f91.d.ts → GraphQLHandler-efb5b362.d.ts} +21 -7
- package/lib/core/SetupApi.js +14 -11
- package/lib/core/SetupApi.mjs +14 -11
- package/lib/core/graphql.d.ts +1 -1
- package/lib/core/handlers/GraphQLHandler.d.ts +1 -1
- package/lib/core/handlers/GraphQLHandler.js +11 -13
- package/lib/core/handlers/GraphQLHandler.mjs +11 -13
- package/lib/core/index.d.ts +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.d.ts +1 -1
- package/lib/core/utils/internal/pipeEvents.js +3 -2
- package/lib/core/utils/internal/pipeEvents.mjs +3 -2
- package/lib/iife/index.js +29 -34
- package/lib/mockServiceWorker.js +1 -1
- package/lib/native/index.d.ts +1 -12
- package/lib/native/index.js +4 -58
- package/lib/native/index.mjs +4 -48
- package/lib/node/index.d.ts +1 -12
- package/lib/node/index.js +31 -54
- package/lib/node/index.mjs +31 -44
- package/package.json +3 -2
package/lib/browser/index.js
CHANGED
|
@@ -209,14 +209,7 @@ var WorkerChannel = class {
|
|
|
209
209
|
}
|
|
210
210
|
postMessage(event, ...rest) {
|
|
211
211
|
const [data, transfer] = rest;
|
|
212
|
-
this.port.postMessage(
|
|
213
|
-
{ type: event, data },
|
|
214
|
-
{
|
|
215
|
-
// @ts-ignore ReadableStream can be transferred
|
|
216
|
-
// but TypeScript doesn't acknowledge that.
|
|
217
|
-
transfer
|
|
218
|
-
}
|
|
219
|
-
);
|
|
212
|
+
this.port.postMessage({ type: event, data }, { transfer });
|
|
220
213
|
}
|
|
221
214
|
};
|
|
222
215
|
|
package/lib/browser/index.mjs
CHANGED
|
@@ -185,14 +185,7 @@ var WorkerChannel = class {
|
|
|
185
185
|
}
|
|
186
186
|
postMessage(event, ...rest) {
|
|
187
187
|
const [data, transfer] = rest;
|
|
188
|
-
this.port.postMessage(
|
|
189
|
-
{ type: event, data },
|
|
190
|
-
{
|
|
191
|
-
// @ts-ignore ReadableStream can be transferred
|
|
192
|
-
// but TypeScript doesn't acknowledge that.
|
|
193
|
-
transfer
|
|
194
|
-
}
|
|
195
|
-
);
|
|
188
|
+
this.port.postMessage({ type: event, data }, { transfer });
|
|
196
189
|
}
|
|
197
190
|
};
|
|
198
191
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { OperationTypeNode, DocumentNode, GraphQLError } from 'graphql';
|
|
2
2
|
import { g as RequestHandlerDefaultInfo, D as DefaultBodyType, R as RequestHandler, a as ResponseResolver, c as RequestHandlerOptions } from './RequestHandler-bb5cbb8f.js';
|
|
3
|
-
import { Path } from './utils/matching/matchRequestUrl.js';
|
|
3
|
+
import { Match, Path } from './utils/matching/matchRequestUrl.js';
|
|
4
4
|
|
|
5
5
|
interface ParsedGraphQLQuery {
|
|
6
6
|
operationType: OperationTypeNode;
|
|
@@ -31,6 +31,20 @@ interface GraphQLHandlerInfo extends RequestHandlerDefaultInfo {
|
|
|
31
31
|
operationType: ExpectedOperationTypeNode;
|
|
32
32
|
operationName: GraphQLHandlerNameSelector;
|
|
33
33
|
}
|
|
34
|
+
type GraphQLRequestParsedResult = {
|
|
35
|
+
match: Match;
|
|
36
|
+
} & (ParsedGraphQLRequest<GraphQLVariables>
|
|
37
|
+
/**
|
|
38
|
+
* An empty version of the ParsedGraphQLRequest
|
|
39
|
+
* which simplifies the return type of the resolver
|
|
40
|
+
* when the request is to a non-matching endpoint
|
|
41
|
+
*/
|
|
42
|
+
| {
|
|
43
|
+
operationType?: undefined;
|
|
44
|
+
operationName?: undefined;
|
|
45
|
+
query?: undefined;
|
|
46
|
+
variables?: undefined;
|
|
47
|
+
});
|
|
34
48
|
type GraphQLResolverExtras<Variables extends GraphQLVariables> = {
|
|
35
49
|
query: string;
|
|
36
50
|
operationName: string;
|
|
@@ -47,19 +61,19 @@ interface GraphQLResponseBody<BodyType extends DefaultBodyType> {
|
|
|
47
61
|
errors?: readonly Partial<GraphQLError>[] | null;
|
|
48
62
|
}
|
|
49
63
|
declare function isDocumentNode(value: DocumentNode | any): value is DocumentNode;
|
|
50
|
-
declare class GraphQLHandler extends RequestHandler<GraphQLHandlerInfo,
|
|
64
|
+
declare class GraphQLHandler extends RequestHandler<GraphQLHandlerInfo, GraphQLRequestParsedResult, GraphQLResolverExtras<any>> {
|
|
51
65
|
private endpoint;
|
|
52
66
|
constructor(operationType: ExpectedOperationTypeNode, operationName: GraphQLHandlerNameSelector, endpoint: Path, resolver: ResponseResolver<GraphQLResolverExtras<any>, any, any>, options?: RequestHandlerOptions);
|
|
53
67
|
parse(args: {
|
|
54
68
|
request: Request;
|
|
55
|
-
}): Promise<
|
|
69
|
+
}): Promise<GraphQLRequestParsedResult>;
|
|
56
70
|
predicate(args: {
|
|
57
71
|
request: Request;
|
|
58
|
-
parsedResult:
|
|
72
|
+
parsedResult: GraphQLRequestParsedResult;
|
|
59
73
|
}): boolean;
|
|
60
74
|
protected extendResolverArgs(args: {
|
|
61
75
|
request: Request;
|
|
62
|
-
parsedResult:
|
|
76
|
+
parsedResult: GraphQLRequestParsedResult;
|
|
63
77
|
}): {
|
|
64
78
|
query: string;
|
|
65
79
|
operationName: string;
|
|
@@ -69,8 +83,8 @@ declare class GraphQLHandler extends RequestHandler<GraphQLHandlerInfo, ParsedGr
|
|
|
69
83
|
log(args: {
|
|
70
84
|
request: Request;
|
|
71
85
|
response: Response;
|
|
72
|
-
parsedResult:
|
|
86
|
+
parsedResult: GraphQLRequestParsedResult;
|
|
73
87
|
}): Promise<void>;
|
|
74
88
|
}
|
|
75
89
|
|
|
76
|
-
export { ExpectedOperationTypeNode as E, GraphQLHandler as G, ParsedGraphQLRequest as P, GraphQLVariables as a, GraphQLRequestBody as b, GraphQLJsonRequestBody as c, GraphQLHandlerNameSelector as d, GraphQLResolverExtras as e, GraphQLResponseBody as f, ParsedGraphQLQuery as g, GraphQLParsedOperationsMap as h, GraphQLMultipartRequestBody as i, parseGraphQLRequest as j, GraphQLHandlerInfo as k,
|
|
90
|
+
export { ExpectedOperationTypeNode as E, GraphQLHandler as G, ParsedGraphQLRequest as P, GraphQLVariables as a, GraphQLRequestBody as b, GraphQLJsonRequestBody as c, GraphQLHandlerNameSelector as d, GraphQLResolverExtras as e, GraphQLResponseBody as f, ParsedGraphQLQuery as g, GraphQLParsedOperationsMap as h, GraphQLMultipartRequestBody as i, parseGraphQLRequest as j, GraphQLHandlerInfo as k, GraphQLRequestParsedResult as l, isDocumentNode as m, parseDocumentNode as p };
|
package/lib/core/SetupApi.js
CHANGED
|
@@ -30,7 +30,12 @@ var import_Disposable = require("./utils/internal/Disposable.js");
|
|
|
30
30
|
class SetupApi extends import_Disposable.Disposable {
|
|
31
31
|
constructor(...initialHandlers) {
|
|
32
32
|
super();
|
|
33
|
-
|
|
33
|
+
(0, import_outvariant.invariant)(
|
|
34
|
+
this.validateHandlers(initialHandlers),
|
|
35
|
+
import_devUtils.devUtils.formatMessage(
|
|
36
|
+
`Failed to apply given request handlers: invalid input. Did you forget to spread the request handlers Array?`
|
|
37
|
+
)
|
|
38
|
+
);
|
|
34
39
|
this.initialHandlers = (0, import_toReadonlyArray.toReadonlyArray)(initialHandlers);
|
|
35
40
|
this.currentHandlers = [...initialHandlers];
|
|
36
41
|
this.emitter = new import_strict_event_emitter.Emitter();
|
|
@@ -42,18 +47,16 @@ class SetupApi extends import_Disposable.Disposable {
|
|
|
42
47
|
this.publicEmitter.removeAllListeners();
|
|
43
48
|
});
|
|
44
49
|
}
|
|
45
|
-
validateHandlers(
|
|
46
|
-
|
|
47
|
-
(0, import_outvariant.invariant)(
|
|
48
|
-
!Array.isArray(handler),
|
|
49
|
-
import_devUtils.devUtils.formatMessage(
|
|
50
|
-
'Failed to construct "%s" given an Array of request handlers. Make sure you spread the request handlers when calling the respective setup function.'
|
|
51
|
-
),
|
|
52
|
-
this.constructor.name
|
|
53
|
-
);
|
|
54
|
-
}
|
|
50
|
+
validateHandlers(handlers) {
|
|
51
|
+
return handlers.every((handler) => !Array.isArray(handler));
|
|
55
52
|
}
|
|
56
53
|
use(...runtimeHandlers) {
|
|
54
|
+
(0, import_outvariant.invariant)(
|
|
55
|
+
this.validateHandlers(runtimeHandlers),
|
|
56
|
+
import_devUtils.devUtils.formatMessage(
|
|
57
|
+
`Failed to call "use()" with the given request handlers: invalid input. Did you forget to spread the array of request handlers?`
|
|
58
|
+
)
|
|
59
|
+
);
|
|
57
60
|
this.currentHandlers.unshift(...runtimeHandlers);
|
|
58
61
|
}
|
|
59
62
|
restoreHandlers() {
|
package/lib/core/SetupApi.mjs
CHANGED
|
@@ -7,7 +7,12 @@ import { Disposable } from './utils/internal/Disposable.mjs';
|
|
|
7
7
|
class SetupApi extends Disposable {
|
|
8
8
|
constructor(...initialHandlers) {
|
|
9
9
|
super();
|
|
10
|
-
|
|
10
|
+
invariant(
|
|
11
|
+
this.validateHandlers(initialHandlers),
|
|
12
|
+
devUtils.formatMessage(
|
|
13
|
+
`Failed to apply given request handlers: invalid input. Did you forget to spread the request handlers Array?`
|
|
14
|
+
)
|
|
15
|
+
);
|
|
11
16
|
this.initialHandlers = toReadonlyArray(initialHandlers);
|
|
12
17
|
this.currentHandlers = [...initialHandlers];
|
|
13
18
|
this.emitter = new Emitter();
|
|
@@ -19,18 +24,16 @@ class SetupApi extends Disposable {
|
|
|
19
24
|
this.publicEmitter.removeAllListeners();
|
|
20
25
|
});
|
|
21
26
|
}
|
|
22
|
-
validateHandlers(
|
|
23
|
-
|
|
24
|
-
invariant(
|
|
25
|
-
!Array.isArray(handler),
|
|
26
|
-
devUtils.formatMessage(
|
|
27
|
-
'Failed to construct "%s" given an Array of request handlers. Make sure you spread the request handlers when calling the respective setup function.'
|
|
28
|
-
),
|
|
29
|
-
this.constructor.name
|
|
30
|
-
);
|
|
31
|
-
}
|
|
27
|
+
validateHandlers(handlers) {
|
|
28
|
+
return handlers.every((handler) => !Array.isArray(handler));
|
|
32
29
|
}
|
|
33
30
|
use(...runtimeHandlers) {
|
|
31
|
+
invariant(
|
|
32
|
+
this.validateHandlers(runtimeHandlers),
|
|
33
|
+
devUtils.formatMessage(
|
|
34
|
+
`Failed to call "use()" with the given request handlers: invalid input. Did you forget to spread the array of request handlers?`
|
|
35
|
+
)
|
|
36
|
+
);
|
|
34
37
|
this.currentHandlers.unshift(...runtimeHandlers);
|
|
35
38
|
}
|
|
36
39
|
restoreHandlers() {
|
package/lib/core/graphql.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DocumentNode } from 'graphql';
|
|
2
2
|
import { a as ResponseResolver, c as RequestHandlerOptions } from './RequestHandler-bb5cbb8f.js';
|
|
3
|
-
import { a as GraphQLVariables, d as GraphQLHandlerNameSelector, e as GraphQLResolverExtras, f as GraphQLResponseBody, G as GraphQLHandler } from './GraphQLHandler-
|
|
3
|
+
import { a as GraphQLVariables, d as GraphQLHandlerNameSelector, e as GraphQLResolverExtras, f as GraphQLResponseBody, G as GraphQLHandler } from './GraphQLHandler-efb5b362.js';
|
|
4
4
|
import { Path } from './utils/matching/matchRequestUrl.js';
|
|
5
5
|
import './typeUtils.js';
|
|
6
6
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import 'graphql';
|
|
2
2
|
import '../RequestHandler-bb5cbb8f.js';
|
|
3
3
|
import '../utils/matching/matchRequestUrl.js';
|
|
4
|
-
export { E as ExpectedOperationTypeNode, G as GraphQLHandler, k as GraphQLHandlerInfo, d as GraphQLHandlerNameSelector, c as GraphQLJsonRequestBody, b as GraphQLRequestBody, e as GraphQLResolverExtras, f as GraphQLResponseBody, a as GraphQLVariables,
|
|
4
|
+
export { E as ExpectedOperationTypeNode, G as GraphQLHandler, k as GraphQLHandlerInfo, d as GraphQLHandlerNameSelector, c as GraphQLJsonRequestBody, b as GraphQLRequestBody, l as GraphQLRequestParsedResult, e as GraphQLResolverExtras, f as GraphQLResponseBody, a as GraphQLVariables, m as isDocumentNode } from '../GraphQLHandler-efb5b362.js';
|
|
5
5
|
import '../typeUtils.js';
|
|
@@ -89,6 +89,9 @@ class GraphQLHandler extends import_RequestHandler.RequestHandler {
|
|
|
89
89
|
}
|
|
90
90
|
parse(args) {
|
|
91
91
|
return __async(this, null, function* () {
|
|
92
|
+
const match = (0, import_matchRequestUrl.matchRequestUrl)(new URL(args.request.url), this.endpoint);
|
|
93
|
+
if (!match.matches)
|
|
94
|
+
return { match };
|
|
92
95
|
const parsedResult = yield (0, import_parseGraphQLRequest.parseGraphQLRequest)(args.request).catch(
|
|
93
96
|
(error) => {
|
|
94
97
|
console.error(error);
|
|
@@ -96,9 +99,10 @@ class GraphQLHandler extends import_RequestHandler.RequestHandler {
|
|
|
96
99
|
}
|
|
97
100
|
);
|
|
98
101
|
if (typeof parsedResult === "undefined") {
|
|
99
|
-
return
|
|
102
|
+
return { match };
|
|
100
103
|
}
|
|
101
104
|
return {
|
|
105
|
+
match,
|
|
102
106
|
query: parsedResult.query,
|
|
103
107
|
operationType: parsedResult.operationType,
|
|
104
108
|
operationName: parsedResult.operationName,
|
|
@@ -107,7 +111,7 @@ class GraphQLHandler extends import_RequestHandler.RequestHandler {
|
|
|
107
111
|
});
|
|
108
112
|
}
|
|
109
113
|
predicate(args) {
|
|
110
|
-
if (
|
|
114
|
+
if (args.parsedResult.operationType === void 0) {
|
|
111
115
|
return false;
|
|
112
116
|
}
|
|
113
117
|
if (!args.parsedResult.operationName && this.info.operationType !== "all") {
|
|
@@ -117,31 +121,25 @@ class GraphQLHandler extends import_RequestHandler.RequestHandler {
|
|
|
117
121
|
Consider naming this operation or using "graphql.operation()" request handler to intercept GraphQL requests regardless of their operation name/type. Read more: https://mswjs.io/docs/api/graphql/#graphqloperationresolver`);
|
|
118
122
|
return false;
|
|
119
123
|
}
|
|
120
|
-
const hasMatchingUrl = (0, import_matchRequestUrl.matchRequestUrl)(
|
|
121
|
-
new URL(args.request.url),
|
|
122
|
-
this.endpoint
|
|
123
|
-
);
|
|
124
124
|
const hasMatchingOperationType = this.info.operationType === "all" || args.parsedResult.operationType === this.info.operationType;
|
|
125
125
|
const hasMatchingOperationName = this.info.operationName instanceof RegExp ? this.info.operationName.test(args.parsedResult.operationName || "") : args.parsedResult.operationName === this.info.operationName;
|
|
126
|
-
return
|
|
126
|
+
return args.parsedResult.match.matches && hasMatchingOperationType && hasMatchingOperationName;
|
|
127
127
|
}
|
|
128
128
|
extendResolverArgs(args) {
|
|
129
|
-
var _a, _b, _c;
|
|
130
129
|
const cookies = (0, import_getRequestCookies.getAllRequestCookies)(args.request);
|
|
131
130
|
return {
|
|
132
|
-
query:
|
|
133
|
-
operationName:
|
|
134
|
-
variables:
|
|
131
|
+
query: args.parsedResult.query || "",
|
|
132
|
+
operationName: args.parsedResult.operationName || "",
|
|
133
|
+
variables: args.parsedResult.variables || {},
|
|
135
134
|
cookies
|
|
136
135
|
};
|
|
137
136
|
}
|
|
138
137
|
log(args) {
|
|
139
138
|
return __async(this, null, function* () {
|
|
140
|
-
var _a, _b, _c, _d;
|
|
141
139
|
const loggedRequest = yield (0, import_serializeRequest.serializeRequest)(args.request);
|
|
142
140
|
const loggedResponse = yield (0, import_serializeResponse.serializeResponse)(args.response);
|
|
143
141
|
const statusColor = (0, import_getStatusCodeColor.getStatusCodeColor)(loggedResponse.status);
|
|
144
|
-
const requestInfo =
|
|
142
|
+
const requestInfo = args.parsedResult.operationName ? `${args.parsedResult.operationType} ${args.parsedResult.operationName}` : `anonymous ${args.parsedResult.operationType}`;
|
|
145
143
|
console.groupCollapsed(
|
|
146
144
|
import_devUtils.devUtils.formatMessage(
|
|
147
145
|
`${(0, import_getTimestamp.getTimestamp)()} ${requestInfo} (%c${loggedResponse.status} ${loggedResponse.statusText}%c)`
|
|
@@ -70,6 +70,9 @@ class GraphQLHandler extends RequestHandler {
|
|
|
70
70
|
}
|
|
71
71
|
parse(args) {
|
|
72
72
|
return __async(this, null, function* () {
|
|
73
|
+
const match = matchRequestUrl(new URL(args.request.url), this.endpoint);
|
|
74
|
+
if (!match.matches)
|
|
75
|
+
return { match };
|
|
73
76
|
const parsedResult = yield parseGraphQLRequest(args.request).catch(
|
|
74
77
|
(error) => {
|
|
75
78
|
console.error(error);
|
|
@@ -77,9 +80,10 @@ class GraphQLHandler extends RequestHandler {
|
|
|
77
80
|
}
|
|
78
81
|
);
|
|
79
82
|
if (typeof parsedResult === "undefined") {
|
|
80
|
-
return
|
|
83
|
+
return { match };
|
|
81
84
|
}
|
|
82
85
|
return {
|
|
86
|
+
match,
|
|
83
87
|
query: parsedResult.query,
|
|
84
88
|
operationType: parsedResult.operationType,
|
|
85
89
|
operationName: parsedResult.operationName,
|
|
@@ -88,7 +92,7 @@ class GraphQLHandler extends RequestHandler {
|
|
|
88
92
|
});
|
|
89
93
|
}
|
|
90
94
|
predicate(args) {
|
|
91
|
-
if (
|
|
95
|
+
if (args.parsedResult.operationType === void 0) {
|
|
92
96
|
return false;
|
|
93
97
|
}
|
|
94
98
|
if (!args.parsedResult.operationName && this.info.operationType !== "all") {
|
|
@@ -98,31 +102,25 @@ class GraphQLHandler extends RequestHandler {
|
|
|
98
102
|
Consider naming this operation or using "graphql.operation()" request handler to intercept GraphQL requests regardless of their operation name/type. Read more: https://mswjs.io/docs/api/graphql/#graphqloperationresolver`);
|
|
99
103
|
return false;
|
|
100
104
|
}
|
|
101
|
-
const hasMatchingUrl = matchRequestUrl(
|
|
102
|
-
new URL(args.request.url),
|
|
103
|
-
this.endpoint
|
|
104
|
-
);
|
|
105
105
|
const hasMatchingOperationType = this.info.operationType === "all" || args.parsedResult.operationType === this.info.operationType;
|
|
106
106
|
const hasMatchingOperationName = this.info.operationName instanceof RegExp ? this.info.operationName.test(args.parsedResult.operationName || "") : args.parsedResult.operationName === this.info.operationName;
|
|
107
|
-
return
|
|
107
|
+
return args.parsedResult.match.matches && hasMatchingOperationType && hasMatchingOperationName;
|
|
108
108
|
}
|
|
109
109
|
extendResolverArgs(args) {
|
|
110
|
-
var _a, _b, _c;
|
|
111
110
|
const cookies = getAllRequestCookies(args.request);
|
|
112
111
|
return {
|
|
113
|
-
query:
|
|
114
|
-
operationName:
|
|
115
|
-
variables:
|
|
112
|
+
query: args.parsedResult.query || "",
|
|
113
|
+
operationName: args.parsedResult.operationName || "",
|
|
114
|
+
variables: args.parsedResult.variables || {},
|
|
116
115
|
cookies
|
|
117
116
|
};
|
|
118
117
|
}
|
|
119
118
|
log(args) {
|
|
120
119
|
return __async(this, null, function* () {
|
|
121
|
-
var _a, _b, _c, _d;
|
|
122
120
|
const loggedRequest = yield serializeRequest(args.request);
|
|
123
121
|
const loggedResponse = yield serializeResponse(args.response);
|
|
124
122
|
const statusColor = getStatusCodeColor(loggedResponse.status);
|
|
125
|
-
const requestInfo =
|
|
123
|
+
const requestInfo = args.parsedResult.operationName ? `${args.parsedResult.operationType} ${args.parsedResult.operationName}` : `anonymous ${args.parsedResult.operationType}`;
|
|
126
124
|
console.groupCollapsed(
|
|
127
125
|
devUtils.formatMessage(
|
|
128
126
|
`${getTimestamp()} ${requestInfo} (%c${loggedResponse.status} ${loggedResponse.statusText}%c)`
|
package/lib/core/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export { A as AsyncResponseResolverReturnType, D as DefaultBodyType, d as Defaul
|
|
|
3
3
|
export { http } from './http.js';
|
|
4
4
|
export { HttpHandler, HttpMethods, HttpRequestParsedResult, RequestQuery } from './handlers/HttpHandler.js';
|
|
5
5
|
export { graphql } from './graphql.js';
|
|
6
|
-
export { G as GraphQLHandler, c as GraphQLJsonRequestBody, b as GraphQLRequestBody, a as GraphQLVariables, P as ParsedGraphQLRequest } from './GraphQLHandler-
|
|
6
|
+
export { G as GraphQLHandler, c as GraphQLJsonRequestBody, b as GraphQLRequestBody, a as GraphQLVariables, P as ParsedGraphQLRequest } from './GraphQLHandler-efb5b362.js';
|
|
7
7
|
export { Match, Path, PathParams, matchRequestUrl } from './utils/matching/matchRequestUrl.js';
|
|
8
8
|
export { HandleRequestOptions, handleRequest } from './utils/handleRequest.js';
|
|
9
9
|
export { cleanUrl } from './utils/url/cleanUrl.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import 'graphql';
|
|
2
|
-
export { i as GraphQLMultipartRequestBody, h as GraphQLParsedOperationsMap, g as ParsedGraphQLQuery, P as ParsedGraphQLRequest, p as parseDocumentNode, j as parseGraphQLRequest } from '../../GraphQLHandler-
|
|
2
|
+
export { i as GraphQLMultipartRequestBody, h as GraphQLParsedOperationsMap, g as ParsedGraphQLQuery, P as ParsedGraphQLRequest, p as parseDocumentNode, j as parseGraphQLRequest } from '../../GraphQLHandler-efb5b362.js';
|
|
3
3
|
import '../../RequestHandler-bb5cbb8f.js';
|
|
4
4
|
import '../../typeUtils.js';
|
|
5
5
|
import '../matching/matchRequestUrl.js';
|
|
@@ -26,9 +26,10 @@ function pipeEvents(source, destination) {
|
|
|
26
26
|
if (rawEmit._isPiped) {
|
|
27
27
|
return;
|
|
28
28
|
}
|
|
29
|
-
|
|
29
|
+
const sourceEmit = function sourceEmit2(event, ...data) {
|
|
30
30
|
destination.emit(event, ...data);
|
|
31
31
|
return rawEmit.call(this, event, ...data);
|
|
32
32
|
};
|
|
33
|
-
|
|
33
|
+
sourceEmit._isPiped = true;
|
|
34
|
+
source.emit = sourceEmit;
|
|
34
35
|
}
|
|
@@ -3,11 +3,12 @@ function pipeEvents(source, destination) {
|
|
|
3
3
|
if (rawEmit._isPiped) {
|
|
4
4
|
return;
|
|
5
5
|
}
|
|
6
|
-
|
|
6
|
+
const sourceEmit = function sourceEmit2(event, ...data) {
|
|
7
7
|
destination.emit(event, ...data);
|
|
8
8
|
return rawEmit.call(this, event, ...data);
|
|
9
9
|
};
|
|
10
|
-
|
|
10
|
+
sourceEmit._isPiped = true;
|
|
11
|
+
source.emit = sourceEmit;
|
|
11
12
|
}
|
|
12
13
|
export {
|
|
13
14
|
pipeEvents
|
package/lib/iife/index.js
CHANGED
|
@@ -393,11 +393,12 @@ var MockServiceWorker = (() => {
|
|
|
393
393
|
if (rawEmit._isPiped) {
|
|
394
394
|
return;
|
|
395
395
|
}
|
|
396
|
-
|
|
396
|
+
const sourceEmit = function sourceEmit2(event, ...data) {
|
|
397
397
|
destination.emit(event, ...data);
|
|
398
398
|
return rawEmit.call(this, event, ...data);
|
|
399
399
|
};
|
|
400
|
-
|
|
400
|
+
sourceEmit._isPiped = true;
|
|
401
|
+
source.emit = sourceEmit;
|
|
401
402
|
}
|
|
402
403
|
|
|
403
404
|
// src/core/utils/internal/toReadonlyArray.ts
|
|
@@ -423,7 +424,12 @@ var MockServiceWorker = (() => {
|
|
|
423
424
|
var SetupApi = class extends Disposable {
|
|
424
425
|
constructor(...initialHandlers) {
|
|
425
426
|
super();
|
|
426
|
-
|
|
427
|
+
invariant(
|
|
428
|
+
this.validateHandlers(initialHandlers),
|
|
429
|
+
devUtils.formatMessage(
|
|
430
|
+
`Failed to apply given request handlers: invalid input. Did you forget to spread the request handlers Array?`
|
|
431
|
+
)
|
|
432
|
+
);
|
|
427
433
|
this.initialHandlers = toReadonlyArray(initialHandlers);
|
|
428
434
|
this.currentHandlers = [...initialHandlers];
|
|
429
435
|
this.emitter = new Emitter();
|
|
@@ -435,18 +441,16 @@ var MockServiceWorker = (() => {
|
|
|
435
441
|
this.publicEmitter.removeAllListeners();
|
|
436
442
|
});
|
|
437
443
|
}
|
|
438
|
-
validateHandlers(
|
|
439
|
-
|
|
440
|
-
invariant(
|
|
441
|
-
!Array.isArray(handler),
|
|
442
|
-
devUtils.formatMessage(
|
|
443
|
-
'Failed to construct "%s" given an Array of request handlers. Make sure you spread the request handlers when calling the respective setup function.'
|
|
444
|
-
),
|
|
445
|
-
this.constructor.name
|
|
446
|
-
);
|
|
447
|
-
}
|
|
444
|
+
validateHandlers(handlers) {
|
|
445
|
+
return handlers.every((handler) => !Array.isArray(handler));
|
|
448
446
|
}
|
|
449
447
|
use(...runtimeHandlers) {
|
|
448
|
+
invariant(
|
|
449
|
+
this.validateHandlers(runtimeHandlers),
|
|
450
|
+
devUtils.formatMessage(
|
|
451
|
+
`Failed to call "use()" with the given request handlers: invalid input. Did you forget to spread the array of request handlers?`
|
|
452
|
+
)
|
|
453
|
+
);
|
|
450
454
|
this.currentHandlers.unshift(...runtimeHandlers);
|
|
451
455
|
}
|
|
452
456
|
restoreHandlers() {
|
|
@@ -5636,6 +5640,9 @@ spurious results.`);
|
|
|
5636
5640
|
}
|
|
5637
5641
|
parse(args) {
|
|
5638
5642
|
return __async(this, null, function* () {
|
|
5643
|
+
const match2 = matchRequestUrl(new URL(args.request.url), this.endpoint);
|
|
5644
|
+
if (!match2.matches)
|
|
5645
|
+
return { match: match2 };
|
|
5639
5646
|
const parsedResult = yield parseGraphQLRequest(args.request).catch(
|
|
5640
5647
|
(error3) => {
|
|
5641
5648
|
console.error(error3);
|
|
@@ -5643,9 +5650,10 @@ spurious results.`);
|
|
|
5643
5650
|
}
|
|
5644
5651
|
);
|
|
5645
5652
|
if (typeof parsedResult === "undefined") {
|
|
5646
|
-
return
|
|
5653
|
+
return { match: match2 };
|
|
5647
5654
|
}
|
|
5648
5655
|
return {
|
|
5656
|
+
match: match2,
|
|
5649
5657
|
query: parsedResult.query,
|
|
5650
5658
|
operationType: parsedResult.operationType,
|
|
5651
5659
|
operationName: parsedResult.operationName,
|
|
@@ -5654,7 +5662,7 @@ spurious results.`);
|
|
|
5654
5662
|
});
|
|
5655
5663
|
}
|
|
5656
5664
|
predicate(args) {
|
|
5657
|
-
if (
|
|
5665
|
+
if (args.parsedResult.operationType === void 0) {
|
|
5658
5666
|
return false;
|
|
5659
5667
|
}
|
|
5660
5668
|
if (!args.parsedResult.operationName && this.info.operationType !== "all") {
|
|
@@ -5664,31 +5672,25 @@ spurious results.`);
|
|
|
5664
5672
|
Consider naming this operation or using "graphql.operation()" request handler to intercept GraphQL requests regardless of their operation name/type. Read more: https://mswjs.io/docs/api/graphql/#graphqloperationresolver`);
|
|
5665
5673
|
return false;
|
|
5666
5674
|
}
|
|
5667
|
-
const hasMatchingUrl = matchRequestUrl(
|
|
5668
|
-
new URL(args.request.url),
|
|
5669
|
-
this.endpoint
|
|
5670
|
-
);
|
|
5671
5675
|
const hasMatchingOperationType = this.info.operationType === "all" || args.parsedResult.operationType === this.info.operationType;
|
|
5672
5676
|
const hasMatchingOperationName = this.info.operationName instanceof RegExp ? this.info.operationName.test(args.parsedResult.operationName || "") : args.parsedResult.operationName === this.info.operationName;
|
|
5673
|
-
return
|
|
5677
|
+
return args.parsedResult.match.matches && hasMatchingOperationType && hasMatchingOperationName;
|
|
5674
5678
|
}
|
|
5675
5679
|
extendResolverArgs(args) {
|
|
5676
|
-
var _a3, _b2, _c;
|
|
5677
5680
|
const cookies = getAllRequestCookies(args.request);
|
|
5678
5681
|
return {
|
|
5679
|
-
query:
|
|
5680
|
-
operationName:
|
|
5681
|
-
variables:
|
|
5682
|
+
query: args.parsedResult.query || "",
|
|
5683
|
+
operationName: args.parsedResult.operationName || "",
|
|
5684
|
+
variables: args.parsedResult.variables || {},
|
|
5682
5685
|
cookies
|
|
5683
5686
|
};
|
|
5684
5687
|
}
|
|
5685
5688
|
log(args) {
|
|
5686
5689
|
return __async(this, null, function* () {
|
|
5687
|
-
var _a3, _b2, _c, _d;
|
|
5688
5690
|
const loggedRequest = yield serializeRequest(args.request);
|
|
5689
5691
|
const loggedResponse = yield serializeResponse(args.response);
|
|
5690
5692
|
const statusColor = getStatusCodeColor(loggedResponse.status);
|
|
5691
|
-
const requestInfo =
|
|
5693
|
+
const requestInfo = args.parsedResult.operationName ? `${args.parsedResult.operationType} ${args.parsedResult.operationName}` : `anonymous ${args.parsedResult.operationType}`;
|
|
5692
5694
|
console.groupCollapsed(
|
|
5693
5695
|
devUtils.formatMessage(
|
|
5694
5696
|
`${getTimestamp()} ${requestInfo} (%c${loggedResponse.status} ${loggedResponse.statusText}%c)`
|
|
@@ -6437,14 +6439,7 @@ Learn more about creating the Service Worker script: https://mswjs.io/docs/cli/i
|
|
|
6437
6439
|
}
|
|
6438
6440
|
postMessage(event, ...rest) {
|
|
6439
6441
|
const [data, transfer] = rest;
|
|
6440
|
-
this.port.postMessage(
|
|
6441
|
-
{ type: event, data },
|
|
6442
|
-
{
|
|
6443
|
-
// @ts-ignore ReadableStream can be transferred
|
|
6444
|
-
// but TypeScript doesn't acknowledge that.
|
|
6445
|
-
transfer
|
|
6446
|
-
}
|
|
6447
|
-
);
|
|
6442
|
+
this.port.postMessage({ type: event, data }, { transfer });
|
|
6448
6443
|
}
|
|
6449
6444
|
};
|
|
6450
6445
|
|
package/lib/mockServiceWorker.js
CHANGED
package/lib/native/index.d.ts
CHANGED
|
@@ -51,29 +51,18 @@ interface SetupServer {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
declare class SetupServerApi extends SetupApi<LifeCycleEventsMap> implements SetupServer {
|
|
54
|
-
private context;
|
|
55
54
|
protected readonly interceptor: BatchInterceptor<Array<Interceptor<HttpRequestEventMap>>, HttpRequestEventMap>;
|
|
56
55
|
private resolvedOptions;
|
|
57
56
|
constructor(interceptors: Array<{
|
|
58
57
|
new (): Interceptor<HttpRequestEventMap>;
|
|
59
58
|
}>, ...handlers: Array<RequestHandler>);
|
|
60
|
-
private createContext;
|
|
61
59
|
/**
|
|
62
60
|
* Subscribe to all requests that are using the interceptor object
|
|
63
61
|
*/
|
|
64
62
|
private init;
|
|
65
63
|
listen(options?: Partial<SharedOptions>): void;
|
|
66
64
|
close(): void;
|
|
67
|
-
|
|
68
|
-
* Bump the maximum number of event listeners on the
|
|
69
|
-
* request's "AbortSignal". This prepares the request
|
|
70
|
-
* for each request handler cloning it at least once.
|
|
71
|
-
* Note that cloning a request automatically appends a
|
|
72
|
-
* new "abort" event listener to the parent request's
|
|
73
|
-
* "AbortController" so if the parent aborts, all the
|
|
74
|
-
* clones are automatically aborted.
|
|
75
|
-
*/
|
|
76
|
-
private setRequestAbortSignalMaxListeners;
|
|
65
|
+
protected onRequest(request: Request): void;
|
|
77
66
|
}
|
|
78
67
|
|
|
79
68
|
/**
|
package/lib/native/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
var __async = (__this, __arguments, generator) => {
|
|
30
20
|
return new Promise((resolve, reject) => {
|
|
@@ -56,26 +46,18 @@ module.exports = __toCommonJS(native_exports);
|
|
|
56
46
|
var import_XMLHttpRequest = require("@mswjs/interceptors/XMLHttpRequest");
|
|
57
47
|
|
|
58
48
|
// src/node/SetupServerApi.ts
|
|
59
|
-
var import_outvariant = require("outvariant");
|
|
60
49
|
var import_interceptors = require("@mswjs/interceptors");
|
|
50
|
+
var import_outvariant = require("outvariant");
|
|
61
51
|
var import_SetupApi = require("../core/SetupApi.js");
|
|
62
|
-
var import_mergeRight = require("../core/utils/internal/mergeRight.js");
|
|
63
52
|
var import_handleRequest = require("../core/utils/handleRequest.js");
|
|
64
53
|
var import_devUtils = require("../core/utils/internal/devUtils.js");
|
|
65
|
-
|
|
66
|
-
// src/node/utils/isNodeExceptionLike.ts
|
|
67
|
-
function isNodeExceptionLike(error) {
|
|
68
|
-
return !!error && typeof error === "object" && "code" in error;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// src/node/SetupServerApi.ts
|
|
54
|
+
var import_mergeRight = require("../core/utils/internal/mergeRight.js");
|
|
72
55
|
var DEFAULT_LISTEN_OPTIONS = {
|
|
73
56
|
onUnhandledRequest: "warn"
|
|
74
57
|
};
|
|
75
58
|
var SetupServerApi = class extends import_SetupApi.SetupApi {
|
|
76
59
|
constructor(interceptors, ...handlers) {
|
|
77
60
|
super(...handlers);
|
|
78
|
-
this.context = this.createContext();
|
|
79
61
|
this.interceptor = new import_interceptors.BatchInterceptor({
|
|
80
62
|
name: "setup-server",
|
|
81
63
|
interceptors: interceptors.map((Interceptor2) => new Interceptor2())
|
|
@@ -83,19 +65,12 @@ var SetupServerApi = class extends import_SetupApi.SetupApi {
|
|
|
83
65
|
this.resolvedOptions = {};
|
|
84
66
|
this.init();
|
|
85
67
|
}
|
|
86
|
-
createContext() {
|
|
87
|
-
return {
|
|
88
|
-
get nodeEvents() {
|
|
89
|
-
return import("events").then((events) => events).catch(() => void 0);
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
68
|
/**
|
|
94
69
|
* Subscribe to all requests that are using the interceptor object
|
|
95
70
|
*/
|
|
96
71
|
init() {
|
|
97
72
|
this.interceptor.on("request", (_0) => __async(this, [_0], function* ({ request, requestId }) {
|
|
98
|
-
|
|
73
|
+
this.onRequest(request);
|
|
99
74
|
const response = yield (0, import_handleRequest.handleRequest)(
|
|
100
75
|
request,
|
|
101
76
|
requestId,
|
|
@@ -144,36 +119,7 @@ var SetupServerApi = class extends import_SetupApi.SetupApi {
|
|
|
144
119
|
close() {
|
|
145
120
|
this.dispose();
|
|
146
121
|
}
|
|
147
|
-
|
|
148
|
-
* Bump the maximum number of event listeners on the
|
|
149
|
-
* request's "AbortSignal". This prepares the request
|
|
150
|
-
* for each request handler cloning it at least once.
|
|
151
|
-
* Note that cloning a request automatically appends a
|
|
152
|
-
* new "abort" event listener to the parent request's
|
|
153
|
-
* "AbortController" so if the parent aborts, all the
|
|
154
|
-
* clones are automatically aborted.
|
|
155
|
-
*/
|
|
156
|
-
setRequestAbortSignalMaxListeners(request) {
|
|
157
|
-
return __async(this, null, function* () {
|
|
158
|
-
const events = yield this.context.nodeEvents;
|
|
159
|
-
if (typeof events === "undefined") {
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
const { setMaxListeners, defaultMaxListeners } = events;
|
|
163
|
-
if (typeof setMaxListeners !== "function") {
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
try {
|
|
167
|
-
setMaxListeners(
|
|
168
|
-
Math.max(defaultMaxListeners, this.currentHandlers.length),
|
|
169
|
-
request.signal
|
|
170
|
-
);
|
|
171
|
-
} catch (error) {
|
|
172
|
-
if (!(isNodeExceptionLike(error) && error.code === "ERR_INVALID_ARG_TYPE")) {
|
|
173
|
-
throw error;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
});
|
|
122
|
+
onRequest(request) {
|
|
177
123
|
}
|
|
178
124
|
};
|
|
179
125
|
|
package/lib/native/index.mjs
CHANGED
|
@@ -23,29 +23,21 @@ var __async = (__this, __arguments, generator) => {
|
|
|
23
23
|
import { XMLHttpRequestInterceptor } from "@mswjs/interceptors/XMLHttpRequest";
|
|
24
24
|
|
|
25
25
|
// src/node/SetupServerApi.ts
|
|
26
|
-
import { invariant } from "outvariant";
|
|
27
26
|
import {
|
|
28
27
|
BatchInterceptor,
|
|
29
28
|
InterceptorReadyState
|
|
30
29
|
} from "@mswjs/interceptors";
|
|
30
|
+
import { invariant } from "outvariant";
|
|
31
31
|
import { SetupApi } from '../core/SetupApi.mjs';
|
|
32
|
-
import { mergeRight } from '../core/utils/internal/mergeRight.mjs';
|
|
33
32
|
import { handleRequest } from '../core/utils/handleRequest.mjs';
|
|
34
33
|
import { devUtils } from '../core/utils/internal/devUtils.mjs';
|
|
35
|
-
|
|
36
|
-
// src/node/utils/isNodeExceptionLike.ts
|
|
37
|
-
function isNodeExceptionLike(error) {
|
|
38
|
-
return !!error && typeof error === "object" && "code" in error;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// src/node/SetupServerApi.ts
|
|
34
|
+
import { mergeRight } from '../core/utils/internal/mergeRight.mjs';
|
|
42
35
|
var DEFAULT_LISTEN_OPTIONS = {
|
|
43
36
|
onUnhandledRequest: "warn"
|
|
44
37
|
};
|
|
45
38
|
var SetupServerApi = class extends SetupApi {
|
|
46
39
|
constructor(interceptors, ...handlers) {
|
|
47
40
|
super(...handlers);
|
|
48
|
-
this.context = this.createContext();
|
|
49
41
|
this.interceptor = new BatchInterceptor({
|
|
50
42
|
name: "setup-server",
|
|
51
43
|
interceptors: interceptors.map((Interceptor2) => new Interceptor2())
|
|
@@ -53,19 +45,12 @@ var SetupServerApi = class extends SetupApi {
|
|
|
53
45
|
this.resolvedOptions = {};
|
|
54
46
|
this.init();
|
|
55
47
|
}
|
|
56
|
-
createContext() {
|
|
57
|
-
return {
|
|
58
|
-
get nodeEvents() {
|
|
59
|
-
return import("events").then((events) => events).catch(() => void 0);
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
48
|
/**
|
|
64
49
|
* Subscribe to all requests that are using the interceptor object
|
|
65
50
|
*/
|
|
66
51
|
init() {
|
|
67
52
|
this.interceptor.on("request", (_0) => __async(this, [_0], function* ({ request, requestId }) {
|
|
68
|
-
|
|
53
|
+
this.onRequest(request);
|
|
69
54
|
const response = yield handleRequest(
|
|
70
55
|
request,
|
|
71
56
|
requestId,
|
|
@@ -114,36 +99,7 @@ var SetupServerApi = class extends SetupApi {
|
|
|
114
99
|
close() {
|
|
115
100
|
this.dispose();
|
|
116
101
|
}
|
|
117
|
-
|
|
118
|
-
* Bump the maximum number of event listeners on the
|
|
119
|
-
* request's "AbortSignal". This prepares the request
|
|
120
|
-
* for each request handler cloning it at least once.
|
|
121
|
-
* Note that cloning a request automatically appends a
|
|
122
|
-
* new "abort" event listener to the parent request's
|
|
123
|
-
* "AbortController" so if the parent aborts, all the
|
|
124
|
-
* clones are automatically aborted.
|
|
125
|
-
*/
|
|
126
|
-
setRequestAbortSignalMaxListeners(request) {
|
|
127
|
-
return __async(this, null, function* () {
|
|
128
|
-
const events = yield this.context.nodeEvents;
|
|
129
|
-
if (typeof events === "undefined") {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
const { setMaxListeners, defaultMaxListeners } = events;
|
|
133
|
-
if (typeof setMaxListeners !== "function") {
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
try {
|
|
137
|
-
setMaxListeners(
|
|
138
|
-
Math.max(defaultMaxListeners, this.currentHandlers.length),
|
|
139
|
-
request.signal
|
|
140
|
-
);
|
|
141
|
-
} catch (error) {
|
|
142
|
-
if (!(isNodeExceptionLike(error) && error.code === "ERR_INVALID_ARG_TYPE")) {
|
|
143
|
-
throw error;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
});
|
|
102
|
+
onRequest(request) {
|
|
147
103
|
}
|
|
148
104
|
};
|
|
149
105
|
|
package/lib/node/index.d.ts
CHANGED
|
@@ -51,29 +51,18 @@ interface SetupServer {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
declare class SetupServerApi extends SetupApi<LifeCycleEventsMap> implements SetupServer {
|
|
54
|
-
private context;
|
|
55
54
|
protected readonly interceptor: BatchInterceptor<Array<Interceptor<HttpRequestEventMap>>, HttpRequestEventMap>;
|
|
56
55
|
private resolvedOptions;
|
|
57
56
|
constructor(interceptors: Array<{
|
|
58
57
|
new (): Interceptor<HttpRequestEventMap>;
|
|
59
58
|
}>, ...handlers: Array<RequestHandler>);
|
|
60
|
-
private createContext;
|
|
61
59
|
/**
|
|
62
60
|
* Subscribe to all requests that are using the interceptor object
|
|
63
61
|
*/
|
|
64
62
|
private init;
|
|
65
63
|
listen(options?: Partial<SharedOptions>): void;
|
|
66
64
|
close(): void;
|
|
67
|
-
|
|
68
|
-
* Bump the maximum number of event listeners on the
|
|
69
|
-
* request's "AbortSignal". This prepares the request
|
|
70
|
-
* for each request handler cloning it at least once.
|
|
71
|
-
* Note that cloning a request automatically appends a
|
|
72
|
-
* new "abort" event listener to the parent request's
|
|
73
|
-
* "AbortController" so if the parent aborts, all the
|
|
74
|
-
* clones are automatically aborted.
|
|
75
|
-
*/
|
|
76
|
-
private setRequestAbortSignalMaxListeners;
|
|
65
|
+
protected onRequest(request: Request): void;
|
|
77
66
|
}
|
|
78
67
|
|
|
79
68
|
/**
|
package/lib/node/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
var __async = (__this, __arguments, generator) => {
|
|
30
20
|
return new Promise((resolve, reject) => {
|
|
@@ -56,26 +46,18 @@ __export(node_exports, {
|
|
|
56
46
|
module.exports = __toCommonJS(node_exports);
|
|
57
47
|
|
|
58
48
|
// src/node/SetupServerApi.ts
|
|
59
|
-
var import_outvariant = require("outvariant");
|
|
60
49
|
var import_interceptors = require("@mswjs/interceptors");
|
|
50
|
+
var import_outvariant = require("outvariant");
|
|
61
51
|
var import_SetupApi = require("../core/SetupApi.js");
|
|
62
|
-
var import_mergeRight = require("../core/utils/internal/mergeRight.js");
|
|
63
52
|
var import_handleRequest = require("../core/utils/handleRequest.js");
|
|
64
53
|
var import_devUtils = require("../core/utils/internal/devUtils.js");
|
|
65
|
-
|
|
66
|
-
// src/node/utils/isNodeExceptionLike.ts
|
|
67
|
-
function isNodeExceptionLike(error) {
|
|
68
|
-
return !!error && typeof error === "object" && "code" in error;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// src/node/SetupServerApi.ts
|
|
54
|
+
var import_mergeRight = require("../core/utils/internal/mergeRight.js");
|
|
72
55
|
var DEFAULT_LISTEN_OPTIONS = {
|
|
73
56
|
onUnhandledRequest: "warn"
|
|
74
57
|
};
|
|
75
58
|
var SetupServerApi = class extends import_SetupApi.SetupApi {
|
|
76
59
|
constructor(interceptors, ...handlers) {
|
|
77
60
|
super(...handlers);
|
|
78
|
-
this.context = this.createContext();
|
|
79
61
|
this.interceptor = new import_interceptors.BatchInterceptor({
|
|
80
62
|
name: "setup-server",
|
|
81
63
|
interceptors: interceptors.map((Interceptor2) => new Interceptor2())
|
|
@@ -83,19 +65,12 @@ var SetupServerApi = class extends import_SetupApi.SetupApi {
|
|
|
83
65
|
this.resolvedOptions = {};
|
|
84
66
|
this.init();
|
|
85
67
|
}
|
|
86
|
-
createContext() {
|
|
87
|
-
return {
|
|
88
|
-
get nodeEvents() {
|
|
89
|
-
return import("events").then((events) => events).catch(() => void 0);
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
68
|
/**
|
|
94
69
|
* Subscribe to all requests that are using the interceptor object
|
|
95
70
|
*/
|
|
96
71
|
init() {
|
|
97
72
|
this.interceptor.on("request", (_0) => __async(this, [_0], function* ({ request, requestId }) {
|
|
98
|
-
|
|
73
|
+
this.onRequest(request);
|
|
99
74
|
const response = yield (0, import_handleRequest.handleRequest)(
|
|
100
75
|
request,
|
|
101
76
|
requestId,
|
|
@@ -144,6 +119,23 @@ var SetupServerApi = class extends import_SetupApi.SetupApi {
|
|
|
144
119
|
close() {
|
|
145
120
|
this.dispose();
|
|
146
121
|
}
|
|
122
|
+
onRequest(request) {
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
// src/node/setupServer.ts
|
|
127
|
+
var import_ClientRequest = require("@mswjs/interceptors/ClientRequest");
|
|
128
|
+
var import_XMLHttpRequest = require("@mswjs/interceptors/XMLHttpRequest");
|
|
129
|
+
var import_fetch = require("@mswjs/interceptors/fetch");
|
|
130
|
+
var import_node_events = require("events");
|
|
131
|
+
|
|
132
|
+
// src/node/utils/isNodeExceptionLike.ts
|
|
133
|
+
function isNodeExceptionLike(error) {
|
|
134
|
+
return !!error && typeof error === "object" && "code" in error;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// src/node/setupServer.ts
|
|
138
|
+
var SetupServerApi2 = class extends SetupServerApi {
|
|
147
139
|
/**
|
|
148
140
|
* Bump the maximum number of event listeners on the
|
|
149
141
|
* request's "AbortSignal". This prepares the request
|
|
@@ -153,36 +145,21 @@ var SetupServerApi = class extends import_SetupApi.SetupApi {
|
|
|
153
145
|
* "AbortController" so if the parent aborts, all the
|
|
154
146
|
* clones are automatically aborted.
|
|
155
147
|
*/
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
try {
|
|
167
|
-
setMaxListeners(
|
|
168
|
-
Math.max(defaultMaxListeners, this.currentHandlers.length),
|
|
169
|
-
request.signal
|
|
170
|
-
);
|
|
171
|
-
} catch (error) {
|
|
172
|
-
if (!(isNodeExceptionLike(error) && error.code === "ERR_INVALID_ARG_TYPE")) {
|
|
173
|
-
throw error;
|
|
174
|
-
}
|
|
148
|
+
onRequest(request) {
|
|
149
|
+
try {
|
|
150
|
+
(0, import_node_events.setMaxListeners)(
|
|
151
|
+
Math.max(import_node_events.defaultMaxListeners, this.currentHandlers.length),
|
|
152
|
+
request.signal
|
|
153
|
+
);
|
|
154
|
+
} catch (error) {
|
|
155
|
+
if (!(isNodeExceptionLike(error) && error.code === "ERR_INVALID_ARG_TYPE")) {
|
|
156
|
+
throw error;
|
|
175
157
|
}
|
|
176
|
-
}
|
|
158
|
+
}
|
|
177
159
|
}
|
|
178
160
|
};
|
|
179
|
-
|
|
180
|
-
// src/node/setupServer.ts
|
|
181
|
-
var import_ClientRequest = require("@mswjs/interceptors/ClientRequest");
|
|
182
|
-
var import_XMLHttpRequest = require("@mswjs/interceptors/XMLHttpRequest");
|
|
183
|
-
var import_fetch = require("@mswjs/interceptors/fetch");
|
|
184
161
|
var setupServer = (...handlers) => {
|
|
185
|
-
return new
|
|
162
|
+
return new SetupServerApi2(
|
|
186
163
|
[import_ClientRequest.ClientRequestInterceptor, import_XMLHttpRequest.XMLHttpRequestInterceptor, import_fetch.FetchInterceptor],
|
|
187
164
|
...handlers
|
|
188
165
|
);
|
package/lib/node/index.mjs
CHANGED
|
@@ -20,29 +20,21 @@ var __async = (__this, __arguments, generator) => {
|
|
|
20
20
|
};
|
|
21
21
|
|
|
22
22
|
// src/node/SetupServerApi.ts
|
|
23
|
-
import { invariant } from "outvariant";
|
|
24
23
|
import {
|
|
25
24
|
BatchInterceptor,
|
|
26
25
|
InterceptorReadyState
|
|
27
26
|
} from "@mswjs/interceptors";
|
|
27
|
+
import { invariant } from "outvariant";
|
|
28
28
|
import { SetupApi } from '../core/SetupApi.mjs';
|
|
29
|
-
import { mergeRight } from '../core/utils/internal/mergeRight.mjs';
|
|
30
29
|
import { handleRequest } from '../core/utils/handleRequest.mjs';
|
|
31
30
|
import { devUtils } from '../core/utils/internal/devUtils.mjs';
|
|
32
|
-
|
|
33
|
-
// src/node/utils/isNodeExceptionLike.ts
|
|
34
|
-
function isNodeExceptionLike(error) {
|
|
35
|
-
return !!error && typeof error === "object" && "code" in error;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// src/node/SetupServerApi.ts
|
|
31
|
+
import { mergeRight } from '../core/utils/internal/mergeRight.mjs';
|
|
39
32
|
var DEFAULT_LISTEN_OPTIONS = {
|
|
40
33
|
onUnhandledRequest: "warn"
|
|
41
34
|
};
|
|
42
35
|
var SetupServerApi = class extends SetupApi {
|
|
43
36
|
constructor(interceptors, ...handlers) {
|
|
44
37
|
super(...handlers);
|
|
45
|
-
this.context = this.createContext();
|
|
46
38
|
this.interceptor = new BatchInterceptor({
|
|
47
39
|
name: "setup-server",
|
|
48
40
|
interceptors: interceptors.map((Interceptor2) => new Interceptor2())
|
|
@@ -50,19 +42,12 @@ var SetupServerApi = class extends SetupApi {
|
|
|
50
42
|
this.resolvedOptions = {};
|
|
51
43
|
this.init();
|
|
52
44
|
}
|
|
53
|
-
createContext() {
|
|
54
|
-
return {
|
|
55
|
-
get nodeEvents() {
|
|
56
|
-
return import("events").then((events) => events).catch(() => void 0);
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
45
|
/**
|
|
61
46
|
* Subscribe to all requests that are using the interceptor object
|
|
62
47
|
*/
|
|
63
48
|
init() {
|
|
64
49
|
this.interceptor.on("request", (_0) => __async(this, [_0], function* ({ request, requestId }) {
|
|
65
|
-
|
|
50
|
+
this.onRequest(request);
|
|
66
51
|
const response = yield handleRequest(
|
|
67
52
|
request,
|
|
68
53
|
requestId,
|
|
@@ -111,6 +96,23 @@ var SetupServerApi = class extends SetupApi {
|
|
|
111
96
|
close() {
|
|
112
97
|
this.dispose();
|
|
113
98
|
}
|
|
99
|
+
onRequest(request) {
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// src/node/setupServer.ts
|
|
104
|
+
import { ClientRequestInterceptor } from "@mswjs/interceptors/ClientRequest";
|
|
105
|
+
import { XMLHttpRequestInterceptor } from "@mswjs/interceptors/XMLHttpRequest";
|
|
106
|
+
import { FetchInterceptor } from "@mswjs/interceptors/fetch";
|
|
107
|
+
import { defaultMaxListeners, setMaxListeners } from "events";
|
|
108
|
+
|
|
109
|
+
// src/node/utils/isNodeExceptionLike.ts
|
|
110
|
+
function isNodeExceptionLike(error) {
|
|
111
|
+
return !!error && typeof error === "object" && "code" in error;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// src/node/setupServer.ts
|
|
115
|
+
var SetupServerApi2 = class extends SetupServerApi {
|
|
114
116
|
/**
|
|
115
117
|
* Bump the maximum number of event listeners on the
|
|
116
118
|
* request's "AbortSignal". This prepares the request
|
|
@@ -120,36 +122,21 @@ var SetupServerApi = class extends SetupApi {
|
|
|
120
122
|
* "AbortController" so if the parent aborts, all the
|
|
121
123
|
* clones are automatically aborted.
|
|
122
124
|
*/
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
if (
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
try {
|
|
134
|
-
setMaxListeners(
|
|
135
|
-
Math.max(defaultMaxListeners, this.currentHandlers.length),
|
|
136
|
-
request.signal
|
|
137
|
-
);
|
|
138
|
-
} catch (error) {
|
|
139
|
-
if (!(isNodeExceptionLike(error) && error.code === "ERR_INVALID_ARG_TYPE")) {
|
|
140
|
-
throw error;
|
|
141
|
-
}
|
|
125
|
+
onRequest(request) {
|
|
126
|
+
try {
|
|
127
|
+
setMaxListeners(
|
|
128
|
+
Math.max(defaultMaxListeners, this.currentHandlers.length),
|
|
129
|
+
request.signal
|
|
130
|
+
);
|
|
131
|
+
} catch (error) {
|
|
132
|
+
if (!(isNodeExceptionLike(error) && error.code === "ERR_INVALID_ARG_TYPE")) {
|
|
133
|
+
throw error;
|
|
142
134
|
}
|
|
143
|
-
}
|
|
135
|
+
}
|
|
144
136
|
}
|
|
145
137
|
};
|
|
146
|
-
|
|
147
|
-
// src/node/setupServer.ts
|
|
148
|
-
import { ClientRequestInterceptor } from "@mswjs/interceptors/ClientRequest";
|
|
149
|
-
import { XMLHttpRequestInterceptor } from "@mswjs/interceptors/XMLHttpRequest";
|
|
150
|
-
import { FetchInterceptor } from "@mswjs/interceptors/fetch";
|
|
151
138
|
var setupServer = (...handlers) => {
|
|
152
|
-
return new
|
|
139
|
+
return new SetupServerApi2(
|
|
153
140
|
[ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],
|
|
154
141
|
...handlers
|
|
155
142
|
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "msw",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.9",
|
|
4
4
|
"description": "Seamless REST/GraphQL API mocking library for browser and Node.js.",
|
|
5
5
|
"main": "./lib/core/index.js",
|
|
6
6
|
"module": "./lib/core/index.mjs",
|
|
@@ -193,9 +193,10 @@
|
|
|
193
193
|
"build": "pnpm clean && cross-env NODE_ENV=production tsup && pnpm patch:dts",
|
|
194
194
|
"patch:dts": "node \"./config/scripts/patch-ts.js\"",
|
|
195
195
|
"check:exports": "node \"./config/scripts/validate-esm.js\"",
|
|
196
|
-
"test": "pnpm test:unit && pnpm test:node && pnpm test:browser",
|
|
196
|
+
"test": "pnpm test:unit && pnpm test:node && pnpm test:browser && pnpm test:native",
|
|
197
197
|
"test:unit": "vitest",
|
|
198
198
|
"test:node": "vitest run --config=./test/node/vitest.config.ts",
|
|
199
|
+
"test:native": "vitest run --config=./test/native/vitest.config.ts",
|
|
199
200
|
"test:browser": "playwright test -c ./test/browser/playwright.config.ts",
|
|
200
201
|
"test:modules:node": "vitest run --config=./test/modules/node/vitest.config.ts",
|
|
201
202
|
"test:modules:browser": "playwright test -c ./test/modules/browser/playwright.config.ts",
|