msw 0.0.0-fetch.rc-20 → 0.0.0-fetch.rc-22
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/README.md +5 -2
- package/lib/browser/index.d.ts +21 -8
- package/lib/browser/index.js +14 -6
- package/lib/browser/index.mjs +14 -6
- package/lib/core/{GraphQLHandler-ef45ae39.d.ts → GraphQLHandler-a441dd03.d.ts} +18 -6
- package/lib/core/HttpResponse.d.ts +1 -1
- package/lib/core/{RequestHandler-7c716cf7.d.ts → RequestHandler-b59044ae.d.ts} +36 -10
- package/lib/core/SetupApi.d.ts +1 -1
- package/lib/core/bypass.d.ts +3 -1
- package/lib/core/delay.d.ts +3 -0
- package/lib/core/graphql.d.ts +62 -30
- package/lib/core/graphql.js +30 -14
- package/lib/core/graphql.mjs +30 -14
- package/lib/core/handlers/GraphQLHandler.d.ts +2 -2
- package/lib/core/handlers/GraphQLHandler.js +23 -18
- package/lib/core/handlers/GraphQLHandler.mjs +23 -18
- package/lib/core/handlers/HttpHandler.d.ts +17 -5
- package/lib/core/handlers/HttpHandler.js +16 -15
- package/lib/core/handlers/HttpHandler.mjs +16 -15
- package/lib/core/handlers/RequestHandler.d.ts +1 -1
- package/lib/core/handlers/RequestHandler.js +37 -30
- package/lib/core/handlers/RequestHandler.mjs +37 -30
- package/lib/core/http.d.ts +10 -1
- package/lib/core/index.d.ts +2 -2
- package/lib/core/passthrough.d.ts +3 -1
- package/lib/core/sharedOptions.d.ts +1 -1
- package/lib/core/utils/HttpResponse/decorators.d.ts +1 -1
- package/lib/core/utils/getResponse.d.ts +1 -1
- package/lib/core/utils/getResponse.js +2 -2
- package/lib/core/utils/getResponse.mjs +2 -2
- package/lib/core/utils/handleRequest.d.ts +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.d.ts +2 -2
- package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
- package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
- package/lib/core/utils/request/onUnhandledRequest.d.ts +1 -1
- package/lib/core/utils/request/onUnhandledRequest.js +1 -1
- package/lib/core/utils/request/onUnhandledRequest.mjs +1 -1
- package/lib/iife/index.js +279 -138
- package/lib/mockServiceWorker.js +3 -3
- package/lib/native/index.d.ts +20 -7
- package/lib/native/index.js +5 -0
- package/lib/native/index.mjs +5 -0
- package/lib/node/index.d.ts +20 -7
- package/lib/node/index.js +5 -0
- package/lib/node/index.mjs +5 -0
- package/package.json +2 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import 'graphql';
|
|
2
|
-
import '../RequestHandler-
|
|
2
|
+
import '../RequestHandler-b59044ae.js';
|
|
3
3
|
import '../utils/matching/matchRequestUrl.js';
|
|
4
|
-
export { E as ExpectedOperationTypeNode, G as GraphQLHandler, k as GraphQLHandlerInfo,
|
|
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, l as isDocumentNode } from '../GraphQLHandler-a441dd03.js';
|
|
5
5
|
import '../typeUtils.js';
|
|
@@ -86,43 +86,48 @@ class GraphQLHandler extends import_RequestHandler.RequestHandler {
|
|
|
86
86
|
});
|
|
87
87
|
this.endpoint = endpoint;
|
|
88
88
|
}
|
|
89
|
-
parse(
|
|
89
|
+
parse(args) {
|
|
90
90
|
return __async(this, null, function* () {
|
|
91
|
-
return (0, import_parseGraphQLRequest.parseGraphQLRequest)(request).catch((error) => {
|
|
91
|
+
return (0, import_parseGraphQLRequest.parseGraphQLRequest)(args.request).catch((error) => {
|
|
92
92
|
console.error(error);
|
|
93
93
|
return void 0;
|
|
94
94
|
});
|
|
95
95
|
});
|
|
96
96
|
}
|
|
97
|
-
predicate(
|
|
98
|
-
if (!parsedResult) {
|
|
97
|
+
predicate(args) {
|
|
98
|
+
if (!args.parsedResult) {
|
|
99
99
|
return false;
|
|
100
100
|
}
|
|
101
|
-
if (!parsedResult.operationName && this.info.operationType !== "all") {
|
|
102
|
-
const publicUrl = (0, import_getPublicUrlFromRequest.getPublicUrlFromRequest)(request);
|
|
103
|
-
import_devUtils.devUtils.warn(`Failed to intercept a GraphQL request at "${request.method} ${publicUrl}": anonymous GraphQL operations are not supported.
|
|
101
|
+
if (!args.parsedResult.operationName && this.info.operationType !== "all") {
|
|
102
|
+
const publicUrl = (0, import_getPublicUrlFromRequest.getPublicUrlFromRequest)(args.request);
|
|
103
|
+
import_devUtils.devUtils.warn(`Failed to intercept a GraphQL request at "${args.request.method} ${publicUrl}": anonymous GraphQL operations are not supported.
|
|
104
104
|
|
|
105
105
|
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/operation`);
|
|
106
106
|
return false;
|
|
107
107
|
}
|
|
108
|
-
const hasMatchingUrl = (0, import_matchRequestUrl.matchRequestUrl)(
|
|
109
|
-
|
|
110
|
-
|
|
108
|
+
const hasMatchingUrl = (0, import_matchRequestUrl.matchRequestUrl)(
|
|
109
|
+
new URL(args.request.url),
|
|
110
|
+
this.endpoint
|
|
111
|
+
);
|
|
112
|
+
const hasMatchingOperationType = this.info.operationType === "all" || args.parsedResult.operationType === this.info.operationType;
|
|
113
|
+
const hasMatchingOperationName = this.info.operationName instanceof RegExp ? this.info.operationName.test(args.parsedResult.operationName || "") : args.parsedResult.operationName === this.info.operationName;
|
|
111
114
|
return hasMatchingUrl.matches && hasMatchingOperationType && hasMatchingOperationName;
|
|
112
115
|
}
|
|
113
|
-
|
|
116
|
+
extendResolverArgs(args) {
|
|
117
|
+
var _a, _b, _c;
|
|
114
118
|
return {
|
|
115
|
-
query: (parsedResult == null ? void 0 :
|
|
116
|
-
operationName: (parsedResult == null ? void 0 :
|
|
117
|
-
variables: (parsedResult == null ? void 0 :
|
|
119
|
+
query: ((_a = args.parsedResult) == null ? void 0 : _a.query) || "",
|
|
120
|
+
operationName: ((_b = args.parsedResult) == null ? void 0 : _b.operationName) || "",
|
|
121
|
+
variables: ((_c = args.parsedResult) == null ? void 0 : _c.variables) || {}
|
|
118
122
|
};
|
|
119
123
|
}
|
|
120
|
-
log(
|
|
124
|
+
log(args) {
|
|
121
125
|
return __async(this, null, function* () {
|
|
122
|
-
|
|
123
|
-
const
|
|
126
|
+
var _a, _b, _c, _d;
|
|
127
|
+
const loggedRequest = yield (0, import_serializeRequest.serializeRequest)(args.request);
|
|
128
|
+
const loggedResponse = yield (0, import_serializeResponse.serializeResponse)(args.response);
|
|
124
129
|
const statusColor = (0, import_getStatusCodeColor.getStatusCodeColor)(loggedResponse.status);
|
|
125
|
-
const requestInfo = (
|
|
130
|
+
const requestInfo = ((_a = args.parsedResult) == null ? void 0 : _a.operationName) ? `${(_b = args.parsedResult) == null ? void 0 : _b.operationType} ${(_c = args.parsedResult) == null ? void 0 : _c.operationName}` : `anonymous ${(_d = args.parsedResult) == null ? void 0 : _d.operationType}`;
|
|
126
131
|
console.groupCollapsed(
|
|
127
132
|
import_devUtils.devUtils.formatMessage("%s %s (%c%s%c)"),
|
|
128
133
|
(0, import_getTimestamp.getTimestamp)(),
|
|
@@ -67,43 +67,48 @@ class GraphQLHandler extends RequestHandler {
|
|
|
67
67
|
});
|
|
68
68
|
this.endpoint = endpoint;
|
|
69
69
|
}
|
|
70
|
-
parse(
|
|
70
|
+
parse(args) {
|
|
71
71
|
return __async(this, null, function* () {
|
|
72
|
-
return parseGraphQLRequest(request).catch((error) => {
|
|
72
|
+
return parseGraphQLRequest(args.request).catch((error) => {
|
|
73
73
|
console.error(error);
|
|
74
74
|
return void 0;
|
|
75
75
|
});
|
|
76
76
|
});
|
|
77
77
|
}
|
|
78
|
-
predicate(
|
|
79
|
-
if (!parsedResult) {
|
|
78
|
+
predicate(args) {
|
|
79
|
+
if (!args.parsedResult) {
|
|
80
80
|
return false;
|
|
81
81
|
}
|
|
82
|
-
if (!parsedResult.operationName && this.info.operationType !== "all") {
|
|
83
|
-
const publicUrl = getPublicUrlFromRequest(request);
|
|
84
|
-
devUtils.warn(`Failed to intercept a GraphQL request at "${request.method} ${publicUrl}": anonymous GraphQL operations are not supported.
|
|
82
|
+
if (!args.parsedResult.operationName && this.info.operationType !== "all") {
|
|
83
|
+
const publicUrl = getPublicUrlFromRequest(args.request);
|
|
84
|
+
devUtils.warn(`Failed to intercept a GraphQL request at "${args.request.method} ${publicUrl}": anonymous GraphQL operations are not supported.
|
|
85
85
|
|
|
86
86
|
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/operation`);
|
|
87
87
|
return false;
|
|
88
88
|
}
|
|
89
|
-
const hasMatchingUrl = matchRequestUrl(
|
|
90
|
-
|
|
91
|
-
|
|
89
|
+
const hasMatchingUrl = matchRequestUrl(
|
|
90
|
+
new URL(args.request.url),
|
|
91
|
+
this.endpoint
|
|
92
|
+
);
|
|
93
|
+
const hasMatchingOperationType = this.info.operationType === "all" || args.parsedResult.operationType === this.info.operationType;
|
|
94
|
+
const hasMatchingOperationName = this.info.operationName instanceof RegExp ? this.info.operationName.test(args.parsedResult.operationName || "") : args.parsedResult.operationName === this.info.operationName;
|
|
92
95
|
return hasMatchingUrl.matches && hasMatchingOperationType && hasMatchingOperationName;
|
|
93
96
|
}
|
|
94
|
-
|
|
97
|
+
extendResolverArgs(args) {
|
|
98
|
+
var _a, _b, _c;
|
|
95
99
|
return {
|
|
96
|
-
query: (parsedResult == null ? void 0 :
|
|
97
|
-
operationName: (parsedResult == null ? void 0 :
|
|
98
|
-
variables: (parsedResult == null ? void 0 :
|
|
100
|
+
query: ((_a = args.parsedResult) == null ? void 0 : _a.query) || "",
|
|
101
|
+
operationName: ((_b = args.parsedResult) == null ? void 0 : _b.operationName) || "",
|
|
102
|
+
variables: ((_c = args.parsedResult) == null ? void 0 : _c.variables) || {}
|
|
99
103
|
};
|
|
100
104
|
}
|
|
101
|
-
log(
|
|
105
|
+
log(args) {
|
|
102
106
|
return __async(this, null, function* () {
|
|
103
|
-
|
|
104
|
-
const
|
|
107
|
+
var _a, _b, _c, _d;
|
|
108
|
+
const loggedRequest = yield serializeRequest(args.request);
|
|
109
|
+
const loggedResponse = yield serializeResponse(args.response);
|
|
105
110
|
const statusColor = getStatusCodeColor(loggedResponse.status);
|
|
106
|
-
const requestInfo = (
|
|
111
|
+
const requestInfo = ((_a = args.parsedResult) == null ? void 0 : _a.operationName) ? `${(_b = args.parsedResult) == null ? void 0 : _b.operationType} ${(_c = args.parsedResult) == null ? void 0 : _c.operationName}` : `anonymous ${(_d = args.parsedResult) == null ? void 0 : _d.operationType}`;
|
|
107
112
|
console.groupCollapsed(
|
|
108
113
|
devUtils.formatMessage("%s %s (%c%s%c)"),
|
|
109
114
|
getTimestamp(),
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { g as RequestHandlerDefaultInfo, R as RequestHandler, a as ResponseResolver, f as RequestHandlerOptions, i as ResponseResolutionContext } from '../RequestHandler-
|
|
1
|
+
import { g as RequestHandlerDefaultInfo, R as RequestHandler, a as ResponseResolver, f as RequestHandlerOptions, i as ResponseResolutionContext } from '../RequestHandler-b59044ae.js';
|
|
2
2
|
import { Path, Match, PathParams } from '../utils/matching/matchRequestUrl.js';
|
|
3
3
|
import '../typeUtils.js';
|
|
4
4
|
|
|
@@ -34,17 +34,29 @@ type HttpRequestResolverExtras<Params extends PathParams> = {
|
|
|
34
34
|
declare class HttpHandler extends RequestHandler<HttpHandlerInfo, HttpRequestParsedResult, HttpRequestResolverExtras<any>> {
|
|
35
35
|
constructor(method: HttpHandlerMethod, path: Path, resolver: ResponseResolver<HttpRequestResolverExtras<any>, any, any>, options?: RequestHandlerOptions);
|
|
36
36
|
private checkRedundantQueryParameters;
|
|
37
|
-
parse(
|
|
37
|
+
parse(args: {
|
|
38
|
+
request: Request;
|
|
39
|
+
resolutionContext?: ResponseResolutionContext;
|
|
40
|
+
}): Promise<{
|
|
38
41
|
match: Match;
|
|
39
42
|
cookies: Record<string, string>;
|
|
40
43
|
}>;
|
|
41
|
-
predicate(
|
|
44
|
+
predicate(args: {
|
|
45
|
+
request: Request;
|
|
46
|
+
parsedResult: HttpRequestParsedResult;
|
|
47
|
+
}): boolean;
|
|
42
48
|
private matchMethod;
|
|
43
|
-
protected
|
|
49
|
+
protected extendResolverArgs(args: {
|
|
50
|
+
request: Request;
|
|
51
|
+
parsedResult: HttpRequestParsedResult;
|
|
52
|
+
}): {
|
|
44
53
|
params: PathParams<string>;
|
|
45
54
|
cookies: Record<string, string>;
|
|
46
55
|
};
|
|
47
|
-
log(
|
|
56
|
+
log(args: {
|
|
57
|
+
request: Request;
|
|
58
|
+
response: Response;
|
|
59
|
+
}): Promise<void>;
|
|
48
60
|
}
|
|
49
61
|
|
|
50
62
|
export { HttpHandler, HttpHandlerInfo, HttpMethods, HttpRequestParsedResult, HttpRequestResolverExtras, RequestQuery };
|
|
@@ -94,46 +94,47 @@ class HttpHandler extends import_RequestHandler.RequestHandler {
|
|
|
94
94
|
`Found a redundant usage of query parameters in the request handler URL for "${method} ${path}". Please match against a path instead and access query parameters in the response resolver function using "req.url.searchParams".`
|
|
95
95
|
);
|
|
96
96
|
}
|
|
97
|
-
parse(
|
|
97
|
+
parse(args) {
|
|
98
98
|
return __async(this, null, function* () {
|
|
99
|
-
|
|
99
|
+
var _a;
|
|
100
|
+
const url = new URL(args.request.url);
|
|
100
101
|
const match = (0, import_matchRequestUrl.matchRequestUrl)(
|
|
101
102
|
url,
|
|
102
103
|
this.info.path,
|
|
103
|
-
resolutionContext == null ? void 0 :
|
|
104
|
+
(_a = args.resolutionContext) == null ? void 0 : _a.baseUrl
|
|
104
105
|
);
|
|
105
|
-
const cookies = (0, import_getRequestCookies.getAllRequestCookies)(request);
|
|
106
|
+
const cookies = (0, import_getRequestCookies.getAllRequestCookies)(args.request);
|
|
106
107
|
return {
|
|
107
108
|
match,
|
|
108
109
|
cookies
|
|
109
110
|
};
|
|
110
111
|
});
|
|
111
112
|
}
|
|
112
|
-
predicate(
|
|
113
|
-
const hasMatchingMethod = this.matchMethod(request.method);
|
|
114
|
-
const hasMatchingUrl = parsedResult.match.matches;
|
|
113
|
+
predicate(args) {
|
|
114
|
+
const hasMatchingMethod = this.matchMethod(args.request.method);
|
|
115
|
+
const hasMatchingUrl = args.parsedResult.match.matches;
|
|
115
116
|
return hasMatchingMethod && hasMatchingUrl;
|
|
116
117
|
}
|
|
117
118
|
matchMethod(actualMethod) {
|
|
118
119
|
return this.info.method instanceof RegExp ? this.info.method.test(actualMethod) : (0, import_isStringEqual.isStringEqual)(this.info.method, actualMethod);
|
|
119
120
|
}
|
|
120
|
-
|
|
121
|
+
extendResolverArgs(args) {
|
|
121
122
|
var _a;
|
|
122
123
|
return {
|
|
123
|
-
params: ((_a = parsedResult.match) == null ? void 0 : _a.params) || {},
|
|
124
|
-
cookies: parsedResult.cookies
|
|
124
|
+
params: ((_a = args.parsedResult.match) == null ? void 0 : _a.params) || {},
|
|
125
|
+
cookies: args.parsedResult.cookies
|
|
125
126
|
};
|
|
126
127
|
}
|
|
127
|
-
log(
|
|
128
|
+
log(args) {
|
|
128
129
|
return __async(this, null, function* () {
|
|
129
|
-
const publicUrl = (0, import_getPublicUrlFromRequest.getPublicUrlFromRequest)(request);
|
|
130
|
-
const loggedRequest = yield (0, import_serializeRequest.serializeRequest)(request);
|
|
131
|
-
const loggedResponse = yield (0, import_serializeResponse.serializeResponse)(response);
|
|
130
|
+
const publicUrl = (0, import_getPublicUrlFromRequest.getPublicUrlFromRequest)(args.request);
|
|
131
|
+
const loggedRequest = yield (0, import_serializeRequest.serializeRequest)(args.request);
|
|
132
|
+
const loggedResponse = yield (0, import_serializeResponse.serializeResponse)(args.response);
|
|
132
133
|
const statusColor = (0, import_getStatusCodeColor.getStatusCodeColor)(loggedResponse.status);
|
|
133
134
|
console.groupCollapsed(
|
|
134
135
|
import_devUtils.devUtils.formatMessage("%s %s %s (%c%s%c)"),
|
|
135
136
|
(0, import_getTimestamp.getTimestamp)(),
|
|
136
|
-
request.method,
|
|
137
|
+
args.request.method,
|
|
137
138
|
publicUrl,
|
|
138
139
|
`color:${statusColor}`,
|
|
139
140
|
`${loggedResponse.status} ${loggedResponse.statusText}`,
|
|
@@ -74,46 +74,47 @@ class HttpHandler extends RequestHandler {
|
|
|
74
74
|
`Found a redundant usage of query parameters in the request handler URL for "${method} ${path}". Please match against a path instead and access query parameters in the response resolver function using "req.url.searchParams".`
|
|
75
75
|
);
|
|
76
76
|
}
|
|
77
|
-
parse(
|
|
77
|
+
parse(args) {
|
|
78
78
|
return __async(this, null, function* () {
|
|
79
|
-
|
|
79
|
+
var _a;
|
|
80
|
+
const url = new URL(args.request.url);
|
|
80
81
|
const match = matchRequestUrl(
|
|
81
82
|
url,
|
|
82
83
|
this.info.path,
|
|
83
|
-
resolutionContext == null ? void 0 :
|
|
84
|
+
(_a = args.resolutionContext) == null ? void 0 : _a.baseUrl
|
|
84
85
|
);
|
|
85
|
-
const cookies = getAllRequestCookies(request);
|
|
86
|
+
const cookies = getAllRequestCookies(args.request);
|
|
86
87
|
return {
|
|
87
88
|
match,
|
|
88
89
|
cookies
|
|
89
90
|
};
|
|
90
91
|
});
|
|
91
92
|
}
|
|
92
|
-
predicate(
|
|
93
|
-
const hasMatchingMethod = this.matchMethod(request.method);
|
|
94
|
-
const hasMatchingUrl = parsedResult.match.matches;
|
|
93
|
+
predicate(args) {
|
|
94
|
+
const hasMatchingMethod = this.matchMethod(args.request.method);
|
|
95
|
+
const hasMatchingUrl = args.parsedResult.match.matches;
|
|
95
96
|
return hasMatchingMethod && hasMatchingUrl;
|
|
96
97
|
}
|
|
97
98
|
matchMethod(actualMethod) {
|
|
98
99
|
return this.info.method instanceof RegExp ? this.info.method.test(actualMethod) : isStringEqual(this.info.method, actualMethod);
|
|
99
100
|
}
|
|
100
|
-
|
|
101
|
+
extendResolverArgs(args) {
|
|
101
102
|
var _a;
|
|
102
103
|
return {
|
|
103
|
-
params: ((_a = parsedResult.match) == null ? void 0 : _a.params) || {},
|
|
104
|
-
cookies: parsedResult.cookies
|
|
104
|
+
params: ((_a = args.parsedResult.match) == null ? void 0 : _a.params) || {},
|
|
105
|
+
cookies: args.parsedResult.cookies
|
|
105
106
|
};
|
|
106
107
|
}
|
|
107
|
-
log(
|
|
108
|
+
log(args) {
|
|
108
109
|
return __async(this, null, function* () {
|
|
109
|
-
const publicUrl = getPublicUrlFromRequest(request);
|
|
110
|
-
const loggedRequest = yield serializeRequest(request);
|
|
111
|
-
const loggedResponse = yield serializeResponse(response);
|
|
110
|
+
const publicUrl = getPublicUrlFromRequest(args.request);
|
|
111
|
+
const loggedRequest = yield serializeRequest(args.request);
|
|
112
|
+
const loggedResponse = yield serializeResponse(args.response);
|
|
112
113
|
const statusColor = getStatusCodeColor(loggedResponse.status);
|
|
113
114
|
console.groupCollapsed(
|
|
114
115
|
devUtils.formatMessage("%s %s %s (%c%s%c)"),
|
|
115
116
|
getTimestamp(),
|
|
116
|
-
request.method,
|
|
117
|
+
args.request.method,
|
|
117
118
|
publicUrl,
|
|
118
119
|
`color:${statusColor}`,
|
|
119
120
|
`${loggedResponse.status} ${loggedResponse.statusText}`,
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { A as AsyncResponseResolverReturnType, D as DefaultBodyType, c as DefaultRequestMultipartBody, M as MaybeAsyncResponseResolverReturnType, R as RequestHandler, m as RequestHandlerArgs, g as RequestHandlerDefaultInfo, n as RequestHandlerExecutionResult, k as RequestHandlerInternalInfo, f as RequestHandlerOptions, a as ResponseResolver, l as ResponseResolverInfo, b as ResponseResolverReturnType } from '../RequestHandler-
|
|
1
|
+
export { A as AsyncResponseResolverReturnType, D as DefaultBodyType, c as DefaultRequestMultipartBody, M as MaybeAsyncResponseResolverReturnType, R as RequestHandler, m as RequestHandlerArgs, g as RequestHandlerDefaultInfo, n as RequestHandlerExecutionResult, k as RequestHandlerInternalInfo, f as RequestHandlerOptions, a as ResponseResolver, l as ResponseResolverInfo, b as ResponseResolverReturnType } from '../RequestHandler-b59044ae.js';
|
|
2
2
|
import '../typeUtils.js';
|
|
@@ -72,10 +72,10 @@ class RequestHandler {
|
|
|
72
72
|
this.isUsed = false;
|
|
73
73
|
}
|
|
74
74
|
/**
|
|
75
|
-
* Parse the
|
|
75
|
+
* Parse the intercepted request to extract additional information from it.
|
|
76
76
|
* Parsed result is then exposed to other methods of this request handler.
|
|
77
77
|
*/
|
|
78
|
-
parse(
|
|
78
|
+
parse(_args) {
|
|
79
79
|
return __async(this, null, function* () {
|
|
80
80
|
return {};
|
|
81
81
|
});
|
|
@@ -83,54 +83,61 @@ class RequestHandler {
|
|
|
83
83
|
/**
|
|
84
84
|
* Test if this handler matches the given request.
|
|
85
85
|
*/
|
|
86
|
-
test(
|
|
86
|
+
test(args) {
|
|
87
87
|
return __async(this, null, function* () {
|
|
88
|
-
|
|
89
|
-
request,
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
88
|
+
const parsedResult = yield this.parse({
|
|
89
|
+
request: args.request,
|
|
90
|
+
resolutionContext: args.resolutionContext
|
|
91
|
+
});
|
|
92
|
+
return this.predicate({
|
|
93
|
+
request: args.request,
|
|
94
|
+
parsedResult,
|
|
95
|
+
resolutionContext: args.resolutionContext
|
|
96
|
+
});
|
|
93
97
|
});
|
|
94
98
|
}
|
|
95
|
-
|
|
99
|
+
extendResolverArgs(_args) {
|
|
96
100
|
return {};
|
|
97
101
|
}
|
|
98
102
|
/**
|
|
99
103
|
* Execute this request handler and produce a mocked response
|
|
100
104
|
* using the given resolver function.
|
|
101
105
|
*/
|
|
102
|
-
run(
|
|
106
|
+
run(args) {
|
|
103
107
|
return __async(this, null, function* () {
|
|
104
108
|
var _a;
|
|
105
109
|
if (this.isUsed && ((_a = this.options) == null ? void 0 : _a.once)) {
|
|
106
110
|
return null;
|
|
107
111
|
}
|
|
108
|
-
const mainRequestRef = request.clone();
|
|
112
|
+
const mainRequestRef = args.request.clone();
|
|
109
113
|
this.isUsed = true;
|
|
110
|
-
const parsedResult = yield this.parse(
|
|
111
|
-
|
|
112
|
-
resolutionContext
|
|
113
|
-
);
|
|
114
|
-
const shouldInterceptRequest = this.predicate(
|
|
115
|
-
|
|
114
|
+
const parsedResult = yield this.parse({
|
|
115
|
+
request: args.request,
|
|
116
|
+
resolutionContext: args.resolutionContext
|
|
117
|
+
});
|
|
118
|
+
const shouldInterceptRequest = this.predicate({
|
|
119
|
+
request: args.request,
|
|
116
120
|
parsedResult,
|
|
117
|
-
resolutionContext
|
|
118
|
-
);
|
|
121
|
+
resolutionContext: args.resolutionContext
|
|
122
|
+
});
|
|
119
123
|
if (!shouldInterceptRequest) {
|
|
120
124
|
return null;
|
|
121
125
|
}
|
|
122
126
|
const executeResolver = this.wrapResolver(this.resolver);
|
|
123
|
-
const resolverExtras = this.
|
|
127
|
+
const resolverExtras = this.extendResolverArgs({
|
|
128
|
+
request: args.request,
|
|
129
|
+
parsedResult
|
|
130
|
+
});
|
|
124
131
|
const mockedResponse = yield executeResolver(__spreadProps(__spreadValues({}, resolverExtras), {
|
|
125
|
-
request
|
|
132
|
+
request: args.request
|
|
126
133
|
}));
|
|
127
|
-
const executionResult = this.createExecutionResult(
|
|
134
|
+
const executionResult = this.createExecutionResult({
|
|
128
135
|
// Pass the cloned request to the result so that logging
|
|
129
136
|
// and other consumers could read its body once more.
|
|
130
|
-
mainRequestRef,
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
);
|
|
137
|
+
request: mainRequestRef,
|
|
138
|
+
response: mockedResponse,
|
|
139
|
+
parsedResult
|
|
140
|
+
});
|
|
134
141
|
return executionResult;
|
|
135
142
|
});
|
|
136
143
|
}
|
|
@@ -162,12 +169,12 @@ class RequestHandler {
|
|
|
162
169
|
return result;
|
|
163
170
|
});
|
|
164
171
|
}
|
|
165
|
-
createExecutionResult(
|
|
172
|
+
createExecutionResult(args) {
|
|
166
173
|
return {
|
|
167
174
|
handler: this,
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
175
|
+
request: args.request,
|
|
176
|
+
response: args.response,
|
|
177
|
+
parsedResult: args.parsedResult
|
|
171
178
|
};
|
|
172
179
|
}
|
|
173
180
|
}
|
|
@@ -51,10 +51,10 @@ class RequestHandler {
|
|
|
51
51
|
this.isUsed = false;
|
|
52
52
|
}
|
|
53
53
|
/**
|
|
54
|
-
* Parse the
|
|
54
|
+
* Parse the intercepted request to extract additional information from it.
|
|
55
55
|
* Parsed result is then exposed to other methods of this request handler.
|
|
56
56
|
*/
|
|
57
|
-
parse(
|
|
57
|
+
parse(_args) {
|
|
58
58
|
return __async(this, null, function* () {
|
|
59
59
|
return {};
|
|
60
60
|
});
|
|
@@ -62,54 +62,61 @@ class RequestHandler {
|
|
|
62
62
|
/**
|
|
63
63
|
* Test if this handler matches the given request.
|
|
64
64
|
*/
|
|
65
|
-
test(
|
|
65
|
+
test(args) {
|
|
66
66
|
return __async(this, null, function* () {
|
|
67
|
-
|
|
68
|
-
request,
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
67
|
+
const parsedResult = yield this.parse({
|
|
68
|
+
request: args.request,
|
|
69
|
+
resolutionContext: args.resolutionContext
|
|
70
|
+
});
|
|
71
|
+
return this.predicate({
|
|
72
|
+
request: args.request,
|
|
73
|
+
parsedResult,
|
|
74
|
+
resolutionContext: args.resolutionContext
|
|
75
|
+
});
|
|
72
76
|
});
|
|
73
77
|
}
|
|
74
|
-
|
|
78
|
+
extendResolverArgs(_args) {
|
|
75
79
|
return {};
|
|
76
80
|
}
|
|
77
81
|
/**
|
|
78
82
|
* Execute this request handler and produce a mocked response
|
|
79
83
|
* using the given resolver function.
|
|
80
84
|
*/
|
|
81
|
-
run(
|
|
85
|
+
run(args) {
|
|
82
86
|
return __async(this, null, function* () {
|
|
83
87
|
var _a;
|
|
84
88
|
if (this.isUsed && ((_a = this.options) == null ? void 0 : _a.once)) {
|
|
85
89
|
return null;
|
|
86
90
|
}
|
|
87
|
-
const mainRequestRef = request.clone();
|
|
91
|
+
const mainRequestRef = args.request.clone();
|
|
88
92
|
this.isUsed = true;
|
|
89
|
-
const parsedResult = yield this.parse(
|
|
90
|
-
|
|
91
|
-
resolutionContext
|
|
92
|
-
);
|
|
93
|
-
const shouldInterceptRequest = this.predicate(
|
|
94
|
-
|
|
93
|
+
const parsedResult = yield this.parse({
|
|
94
|
+
request: args.request,
|
|
95
|
+
resolutionContext: args.resolutionContext
|
|
96
|
+
});
|
|
97
|
+
const shouldInterceptRequest = this.predicate({
|
|
98
|
+
request: args.request,
|
|
95
99
|
parsedResult,
|
|
96
|
-
resolutionContext
|
|
97
|
-
);
|
|
100
|
+
resolutionContext: args.resolutionContext
|
|
101
|
+
});
|
|
98
102
|
if (!shouldInterceptRequest) {
|
|
99
103
|
return null;
|
|
100
104
|
}
|
|
101
105
|
const executeResolver = this.wrapResolver(this.resolver);
|
|
102
|
-
const resolverExtras = this.
|
|
106
|
+
const resolverExtras = this.extendResolverArgs({
|
|
107
|
+
request: args.request,
|
|
108
|
+
parsedResult
|
|
109
|
+
});
|
|
103
110
|
const mockedResponse = yield executeResolver(__spreadProps(__spreadValues({}, resolverExtras), {
|
|
104
|
-
request
|
|
111
|
+
request: args.request
|
|
105
112
|
}));
|
|
106
|
-
const executionResult = this.createExecutionResult(
|
|
113
|
+
const executionResult = this.createExecutionResult({
|
|
107
114
|
// Pass the cloned request to the result so that logging
|
|
108
115
|
// and other consumers could read its body once more.
|
|
109
|
-
mainRequestRef,
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
);
|
|
116
|
+
request: mainRequestRef,
|
|
117
|
+
response: mockedResponse,
|
|
118
|
+
parsedResult
|
|
119
|
+
});
|
|
113
120
|
return executionResult;
|
|
114
121
|
});
|
|
115
122
|
}
|
|
@@ -141,12 +148,12 @@ class RequestHandler {
|
|
|
141
148
|
return result;
|
|
142
149
|
});
|
|
143
150
|
}
|
|
144
|
-
createExecutionResult(
|
|
151
|
+
createExecutionResult(args) {
|
|
145
152
|
return {
|
|
146
153
|
handler: this,
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
154
|
+
request: args.request,
|
|
155
|
+
response: args.response,
|
|
156
|
+
parsedResult: args.parsedResult
|
|
150
157
|
};
|
|
151
158
|
}
|
|
152
159
|
}
|
package/lib/core/http.d.ts
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
|
-
import { D as DefaultBodyType, a as ResponseResolver, f as RequestHandlerOptions } from './RequestHandler-
|
|
1
|
+
import { D as DefaultBodyType, a as ResponseResolver, f as RequestHandlerOptions } from './RequestHandler-b59044ae.js';
|
|
2
2
|
import { HttpRequestResolverExtras, HttpHandler } from './handlers/HttpHandler.js';
|
|
3
3
|
import { PathParams, Path } from './utils/matching/matchRequestUrl.js';
|
|
4
4
|
import './typeUtils.js';
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* A namespace to intercept and mock HTTP requests.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* http.get('/user', resolver)
|
|
11
|
+
* http.post('/post/:id', resolver)
|
|
12
|
+
*
|
|
13
|
+
* @see {@link https://mswjs.io/docs/api/http `http` API reference}
|
|
14
|
+
*/
|
|
6
15
|
declare const http: {
|
|
7
16
|
all: <Params extends PathParams<keyof Params> = PathParams, RequestBodyType extends DefaultBodyType = DefaultBodyType, ResponseBodyType extends DefaultBodyType = undefined>(path: Path, resolver: ResponseResolver<HttpRequestResolverExtras<Params>, RequestBodyType, ResponseBodyType>, options?: RequestHandlerOptions) => HttpHandler;
|
|
8
17
|
head: <Params_1 extends PathParams<keyof Params_1> = PathParams, RequestBodyType_1 extends DefaultBodyType = DefaultBodyType, ResponseBodyType_1 extends DefaultBodyType = undefined>(path: Path, resolver: ResponseResolver<HttpRequestResolverExtras<Params_1>, RequestBodyType_1, ResponseBodyType_1>, options?: RequestHandlerOptions) => HttpHandler;
|
package/lib/core/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export { SetupApi } from './SetupApi.js';
|
|
2
|
-
export { A as AsyncResponseResolverReturnType, D as DefaultBodyType, c as DefaultRequestMultipartBody, e as HttpResponse, H as HttpResponseInit, R as RequestHandler, a as ResponseResolver, b as ResponseResolverReturnType, S as StrictRequest, d as StrictResponse } from './RequestHandler-
|
|
2
|
+
export { A as AsyncResponseResolverReturnType, D as DefaultBodyType, c as DefaultRequestMultipartBody, e as HttpResponse, H as HttpResponseInit, R as RequestHandler, a as ResponseResolver, b as ResponseResolverReturnType, S as StrictRequest, d as StrictResponse } from './RequestHandler-b59044ae.js';
|
|
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-a441dd03.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
|
/**
|
|
2
|
-
* Performs the
|
|
2
|
+
* Performs the intercepted request as-is.
|
|
3
3
|
*
|
|
4
4
|
* This stops request handler lookup so no other handlers
|
|
5
5
|
* can affect this request past this point.
|
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
* http.get('/resource', () => {
|
|
10
10
|
* return passthrough()
|
|
11
11
|
* })
|
|
12
|
+
*
|
|
13
|
+
* @see {@link https://mswjs.io/docs/api/passthrough `passthrough()` API reference}
|
|
12
14
|
*/
|
|
13
15
|
declare function passthrough(): Response;
|
|
14
16
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Emitter } from 'strict-event-emitter';
|
|
2
2
|
import { UnhandledRequestStrategy } from './utils/request/onUnhandledRequest.js';
|
|
3
|
-
import './RequestHandler-
|
|
3
|
+
import './RequestHandler-b59044ae.js';
|
|
4
4
|
import './typeUtils.js';
|
|
5
5
|
|
|
6
6
|
interface SharedOptions {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { h as ResponseLookupResult, i as ResponseResolutionContext, j as getResponse } from '../RequestHandler-
|
|
1
|
+
export { h as ResponseLookupResult, i as ResponseResolutionContext, j as getResponse } from '../RequestHandler-b59044ae.js';
|
|
2
2
|
import '../typeUtils.js';
|
|
@@ -45,7 +45,7 @@ const getResponse = (request, handlers, resolutionContext) => __async(void 0, nu
|
|
|
45
45
|
let matchingHandler = null;
|
|
46
46
|
let result = null;
|
|
47
47
|
for (const handler of handlers) {
|
|
48
|
-
result = yield handler.run(request, resolutionContext);
|
|
48
|
+
result = yield handler.run({ request, resolutionContext });
|
|
49
49
|
if (result !== null) {
|
|
50
50
|
matchingHandler = handler;
|
|
51
51
|
}
|
|
@@ -56,7 +56,7 @@ const getResponse = (request, handlers, resolutionContext) => __async(void 0, nu
|
|
|
56
56
|
if (matchingHandler) {
|
|
57
57
|
return {
|
|
58
58
|
handler: matchingHandler,
|
|
59
|
-
|
|
59
|
+
parsedResult: result == null ? void 0 : result.parsedResult,
|
|
60
60
|
response: result == null ? void 0 : result.response
|
|
61
61
|
};
|
|
62
62
|
}
|