msw 0.39.1 → 0.40.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/lib/esm/graphql-deps.js +4 -10
- package/lib/esm/graphql.js +2 -2
- package/lib/esm/index.js +82 -26
- package/lib/esm/{RequestHandler-deps.js → matchRequestUrl-deps.js} +192 -173
- package/lib/esm/mockServiceWorker.js +1 -1
- package/lib/esm/rest-deps.js +4 -10
- package/lib/esm/rest.js +1 -1
- package/lib/iife/index.js +2 -2
- package/lib/iife/mockServiceWorker.js +1 -1
- package/lib/types/handlers/GraphQLHandler.d.ts +2 -10
- package/lib/types/handlers/RequestHandler.d.ts +18 -8
- package/lib/types/handlers/RestHandler.d.ts +5 -9
- package/lib/types/index.d.ts +1 -1
- package/lib/types/native/index.d.ts +1 -1
- package/lib/types/node/setupServer.d.ts +1 -1
- package/lib/types/response.d.ts +4 -2
- package/lib/types/rest.d.ts +9 -9
- package/lib/types/setupWorker/glossary.d.ts +1 -0
- package/lib/types/setupWorker/start/utils/printStartMessage.d.ts +3 -2
- package/lib/types/sharedOptions.d.ts +1 -0
- package/lib/types/utils/getResponse.d.ts +1 -1
- package/lib/types/utils/handleRequest.d.ts +2 -2
- package/lib/types/utils/internal/requestHandlerUtils.d.ts +1 -1
- package/lib/types/utils/logging/prepareRequest.d.ts +2 -1
- package/lib/types/utils/matching/matchRequestUrl.d.ts +3 -1
- package/lib/types/utils/request/setRequestCookies.d.ts +7 -0
- package/lib/umd/index.js +274 -211
- package/lib/umd/mockServiceWorker.js +1 -1
- package/native/lib/index.js +789 -729
- package/native/package.json +1 -1
- package/node/lib/index.js +789 -729
- package/node/package.json +1 -1
- package/package.json +19 -9
package/CHANGELOG.md
ADDED
package/lib/esm/graphql-deps.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { j as jsonParse, b as set, s as status, e as delay, f as fetch, d as cookie } from './fetch-deps.js';
|
|
2
1
|
import { d as data, e as extensions, a as errors } from './errors-deps.js';
|
|
3
|
-
import {
|
|
2
|
+
import { j as jsonParse, d as cookie } from './fetch-deps.js';
|
|
3
|
+
import { g as getPublicUrlFromRequest, d as devUtils, o as __rest, e as defaultContext, R as RequestHandler, m as matchRequestUrl, j as prepareRequest, k as prepareResponse, l as getStatusCodeColor, n as getTimestamp } from './matchRequestUrl-deps.js';
|
|
4
4
|
|
|
5
5
|
function devAssert(condition, message) {
|
|
6
6
|
const booleanCondition = Boolean(condition);
|
|
@@ -3413,16 +3413,10 @@ function tryCatch(fn, onException) {
|
|
|
3413
3413
|
}
|
|
3414
3414
|
}
|
|
3415
3415
|
|
|
3416
|
-
const graphqlContext = {
|
|
3417
|
-
set,
|
|
3418
|
-
status,
|
|
3419
|
-
delay,
|
|
3420
|
-
fetch,
|
|
3421
|
-
data,
|
|
3416
|
+
const graphqlContext = Object.assign(Object.assign({}, defaultContext), { data,
|
|
3422
3417
|
extensions,
|
|
3423
3418
|
errors,
|
|
3424
|
-
cookie
|
|
3425
|
-
};
|
|
3419
|
+
cookie });
|
|
3426
3420
|
function isDocumentNode(value) {
|
|
3427
3421
|
if (value == null) {
|
|
3428
3422
|
return false;
|
package/lib/esm/graphql.js
CHANGED
package/lib/esm/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { i as context } from './index-deps.js';
|
|
2
2
|
import { c as commonjsGlobal, p as parse_1, l as lib$2, a as lib$3, j as jsonParse } from './fetch-deps.js';
|
|
3
|
-
import { _ as __awaiter, d as devUtils, p as parseBody, g as getPublicUrlFromRequest, N as NetworkError } from './
|
|
4
|
-
export { R as RequestHandler,
|
|
3
|
+
import { _ as __awaiter, d as devUtils, p as passthrough, a as parseBody, g as getPublicUrlFromRequest, N as NetworkError } from './matchRequestUrl-deps.js';
|
|
4
|
+
export { R as RequestHandler, h as cleanUrl, f as compose, c as createResponseComposition, e as defaultContext, b as defaultResponse, m as matchRequestUrl, r as response } from './matchRequestUrl-deps.js';
|
|
5
5
|
import { store } from '@mswjs/cookies';
|
|
6
6
|
import { i as isStringEqual, R as RestHandler } from './rest-deps.js';
|
|
7
7
|
export { a as RESTMethods, R as RestHandler, r as rest, b as restContext } from './rest-deps.js';
|
|
@@ -683,6 +683,12 @@ function printStartMessage(args = {}) {
|
|
|
683
683
|
console.groupCollapsed(`%c${devUtils.formatMessage(message)}`, 'color:orangered;font-weight:bold;');
|
|
684
684
|
console.log('%cDocumentation: %chttps://mswjs.io/docs', 'font-weight:bold', 'font-weight:normal');
|
|
685
685
|
console.log('Found an issue? https://github.com/mswjs/msw/issues');
|
|
686
|
+
if (args.workerUrl) {
|
|
687
|
+
console.log('Worker script URL:', args.workerUrl);
|
|
688
|
+
}
|
|
689
|
+
if (args.workerScope) {
|
|
690
|
+
console.log('Worker scope:', args.workerScope);
|
|
691
|
+
}
|
|
686
692
|
console.groupEnd();
|
|
687
693
|
}
|
|
688
694
|
|
|
@@ -690,10 +696,22 @@ function printStartMessage(args = {}) {
|
|
|
690
696
|
* Signals the worker to enable the interception of requests.
|
|
691
697
|
*/
|
|
692
698
|
function enableMocking(context, options) {
|
|
699
|
+
var _a, _b;
|
|
693
700
|
return __awaiter(this, void 0, void 0, function* () {
|
|
694
701
|
context.workerChannel.send('MOCK_ACTIVATE');
|
|
695
|
-
|
|
696
|
-
|
|
702
|
+
yield context.events.once('MOCKING_ENABLED');
|
|
703
|
+
// Warn the developer on multiple "worker.start()" calls.
|
|
704
|
+
// While this will not affect the worker in any way,
|
|
705
|
+
// it likely indicates an issue with the developer's code.
|
|
706
|
+
if (context.isMockingEnabled) {
|
|
707
|
+
devUtils.warn(`Found a redundant "worker.start()" call. Note that starting the worker while mocking is already enabled will have no effect. Consider removing this "worker.start()" call.`);
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
710
|
+
context.isMockingEnabled = true;
|
|
711
|
+
printStartMessage({
|
|
712
|
+
quiet: options.quiet,
|
|
713
|
+
workerScope: (_a = context.registration) === null || _a === void 0 ? void 0 : _a.scope,
|
|
714
|
+
workerUrl: (_b = context.worker) === null || _b === void 0 ? void 0 : _b.scriptURL,
|
|
697
715
|
});
|
|
698
716
|
});
|
|
699
717
|
}
|
|
@@ -745,13 +763,36 @@ function getRequestCookies(request) {
|
|
|
745
763
|
}
|
|
746
764
|
}
|
|
747
765
|
|
|
766
|
+
/**
|
|
767
|
+
* Sets relevant cookies on the request.
|
|
768
|
+
* Request cookies are taken from the following sources:
|
|
769
|
+
* - Immediate (own) request cookies (those in the "Cookie" request header);
|
|
770
|
+
* - From the `document.cookie` based on the request's `credentials` value;
|
|
771
|
+
* - From the internal cookie store that persists/hydrates cookies in Node.js
|
|
772
|
+
*/
|
|
748
773
|
function setRequestCookies(request) {
|
|
749
774
|
var _a;
|
|
775
|
+
// Set mocked request cookies from the `cookie` header of the original request.
|
|
776
|
+
// No need to take `credentials` into account, because in Node.js requests are intercepted
|
|
777
|
+
// _after_ they happen. Request issuer should have already taken care of sending relevant cookies.
|
|
778
|
+
// Unlike browser, where interception is on the worker level, _before_ the request happens.
|
|
779
|
+
const requestCookiesString = request.headers.get('cookie');
|
|
750
780
|
store.hydrate();
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
781
|
+
const cookiesFromStore = Array.from((_a = store.get(Object.assign(Object.assign({}, request), { url: request.url.toString() }))) === null || _a === void 0 ? void 0 : _a.entries()).reduce((cookies, [name, { value }]) => {
|
|
782
|
+
return Object.assign(cookies, { [name.trim()]: value });
|
|
783
|
+
}, {});
|
|
784
|
+
const cookiesFromDocument = getRequestCookies(request);
|
|
785
|
+
const forwardedCookies = Object.assign(Object.assign({}, cookiesFromDocument), cookiesFromStore);
|
|
786
|
+
// Ensure the persisted (document) cookies are propagated to the request.
|
|
787
|
+
// Propagated the cookies persisted in the Cookuie Store to the request headers.
|
|
788
|
+
// This forwards relevant request cookies based on the request's credentials.
|
|
789
|
+
for (const [name, value] of Object.entries(forwardedCookies)) {
|
|
790
|
+
request.headers.append('cookie', `${name}=${value}`);
|
|
791
|
+
}
|
|
792
|
+
const ownCookies = requestCookiesString
|
|
793
|
+
? parse_1(requestCookiesString)
|
|
794
|
+
: {};
|
|
795
|
+
request.cookies = Object.assign(Object.assign(Object.assign({}, request.cookies), forwardedCookies), ownCookies);
|
|
755
796
|
}
|
|
756
797
|
|
|
757
798
|
/**
|
|
@@ -789,6 +830,7 @@ function parseWorkerRequest(rawRequest) {
|
|
|
789
830
|
body: pruneGetRequestBody(rawRequest),
|
|
790
831
|
bodyUsed: rawRequest.bodyUsed,
|
|
791
832
|
headers: new lib$2.Headers(rawRequest.headers),
|
|
833
|
+
passthrough,
|
|
792
834
|
};
|
|
793
835
|
// Set document cookies on the request.
|
|
794
836
|
setRequestCookies(request);
|
|
@@ -1095,17 +1137,24 @@ function readResponseCookies(request, response) {
|
|
|
1095
1137
|
}
|
|
1096
1138
|
|
|
1097
1139
|
function handleRequest(request, handlers, options, emitter, handleRequestOptions) {
|
|
1098
|
-
var _a, _b, _c;
|
|
1140
|
+
var _a, _b, _c, _d;
|
|
1099
1141
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1100
1142
|
emitter.emit('request:start', request);
|
|
1101
1143
|
// Perform bypassed requests (i.e. issued via "ctx.fetch") as-is.
|
|
1102
1144
|
if (request.headers.get('x-msw-bypass') === 'true') {
|
|
1103
1145
|
emitter.emit('request:end', request);
|
|
1104
|
-
(_a = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.
|
|
1146
|
+
(_a = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.onPassthroughResponse) === null || _a === void 0 ? void 0 : _a.call(handleRequestOptions, request);
|
|
1105
1147
|
return;
|
|
1106
1148
|
}
|
|
1107
1149
|
// Resolve a mocked response from the list of request handlers.
|
|
1108
|
-
const lookupResult = yield
|
|
1150
|
+
const [lookupError, lookupResult] = yield until(() => {
|
|
1151
|
+
return getResponse(request, handlers, handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.resolutionContext);
|
|
1152
|
+
});
|
|
1153
|
+
if (lookupError) {
|
|
1154
|
+
// Allow developers to react to unhandled exceptions in request handlers.
|
|
1155
|
+
emitter.emit('unhandledException', lookupError, request);
|
|
1156
|
+
throw lookupError;
|
|
1157
|
+
}
|
|
1109
1158
|
const { handler, response } = lookupResult;
|
|
1110
1159
|
// When there's no handler for the request, consider it unhandled.
|
|
1111
1160
|
// Allow the developer to react to such cases.
|
|
@@ -1113,7 +1162,7 @@ function handleRequest(request, handlers, options, emitter, handleRequestOptions
|
|
|
1113
1162
|
onUnhandledRequest(request, handlers, options.onUnhandledRequest);
|
|
1114
1163
|
emitter.emit('request:unhandled', request);
|
|
1115
1164
|
emitter.emit('request:end', request);
|
|
1116
|
-
(_b = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.
|
|
1165
|
+
(_b = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.onPassthroughResponse) === null || _b === void 0 ? void 0 : _b.call(handleRequestOptions, request);
|
|
1117
1166
|
return;
|
|
1118
1167
|
}
|
|
1119
1168
|
// When the handled request returned no mocked response, warn the developer,
|
|
@@ -1126,7 +1175,14 @@ Expected response resolver to return a mocked response Object, but got %s. The o
|
|
|
1126
1175
|
%s\
|
|
1127
1176
|
`, response, handler.info.header, handler.info.callFrame);
|
|
1128
1177
|
emitter.emit('request:end', request);
|
|
1129
|
-
(_c = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.
|
|
1178
|
+
(_c = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.onPassthroughResponse) === null || _c === void 0 ? void 0 : _c.call(handleRequestOptions, request);
|
|
1179
|
+
return;
|
|
1180
|
+
}
|
|
1181
|
+
// When the developer explicitly returned "req.passthrough()" do not warn them.
|
|
1182
|
+
// Perform the request as-is.
|
|
1183
|
+
if (response.passthrough) {
|
|
1184
|
+
emitter.emit('request:end', request);
|
|
1185
|
+
(_d = handleRequestOptions === null || handleRequestOptions === void 0 ? void 0 : handleRequestOptions.onPassthroughResponse) === null || _d === void 0 ? void 0 : _d.call(handleRequestOptions, request);
|
|
1130
1186
|
return;
|
|
1131
1187
|
}
|
|
1132
1188
|
// Store all the received response cookies in the virtual cookie store.
|
|
@@ -1157,7 +1213,7 @@ const createRequestListener = (context, options) => {
|
|
|
1157
1213
|
transformResponse(response) {
|
|
1158
1214
|
return Object.assign(Object.assign({}, response), { headers: response.headers.all() });
|
|
1159
1215
|
},
|
|
1160
|
-
|
|
1216
|
+
onPassthroughResponse() {
|
|
1161
1217
|
return channel.send({
|
|
1162
1218
|
type: 'MOCK_NOT_FOUND',
|
|
1163
1219
|
});
|
|
@@ -1379,12 +1435,19 @@ function printStopMessage(args = {}) {
|
|
|
1379
1435
|
const createStop = (context) => {
|
|
1380
1436
|
return function stop() {
|
|
1381
1437
|
var _a;
|
|
1438
|
+
// Warn developers calling "worker.stop()" more times than necessary.
|
|
1439
|
+
// This likely indicates a mistake in their code.
|
|
1440
|
+
if (!context.isMockingEnabled) {
|
|
1441
|
+
devUtils.warn('Found a redundant "worker.stop()" call. Note that stopping the worker while mocking already stopped has no effect. Consider removing this "worker.stop()" call.');
|
|
1442
|
+
return;
|
|
1443
|
+
}
|
|
1382
1444
|
/**
|
|
1383
1445
|
* Signal the Service Worker to disable mocking for this client.
|
|
1384
1446
|
* Use this an an explicit way to stop the mocking, while preserving
|
|
1385
1447
|
* the worker-client relation. Does not affect the worker's lifecycle.
|
|
1386
1448
|
*/
|
|
1387
1449
|
context.workerChannel.send('MOCK_DEACTIVATE');
|
|
1450
|
+
context.isMockingEnabled = false;
|
|
1388
1451
|
window.clearInterval(context.keepAliveInterval);
|
|
1389
1452
|
printStopMessage({ quiet: (_a = context.startOptions) === null || _a === void 0 ? void 0 : _a.quiet });
|
|
1390
1453
|
};
|
|
@@ -1437,6 +1500,7 @@ function parseIsomorphicRequest(request) {
|
|
|
1437
1500
|
url: request.url,
|
|
1438
1501
|
method: request.method,
|
|
1439
1502
|
body: parseBody(request.body, request.headers),
|
|
1503
|
+
credentials: request.credentials || 'same-origin',
|
|
1440
1504
|
headers: request.headers,
|
|
1441
1505
|
cookies: {},
|
|
1442
1506
|
redirect: 'manual',
|
|
@@ -1448,21 +1512,10 @@ function parseIsomorphicRequest(request) {
|
|
|
1448
1512
|
integrity: '',
|
|
1449
1513
|
destination: 'document',
|
|
1450
1514
|
bodyUsed: false,
|
|
1451
|
-
|
|
1515
|
+
passthrough,
|
|
1452
1516
|
};
|
|
1453
|
-
// Set mocked request cookies from the `cookie` header of the original request.
|
|
1454
|
-
// No need to take `credentials` into account, because in Node.js requests are intercepted
|
|
1455
|
-
// _after_ they happen. Request issuer should have already taken care of sending relevant cookies.
|
|
1456
|
-
// Unlike browser, where interception is on the worker level, _before_ the request happens.
|
|
1457
|
-
const requestCookiesString = request.headers.get('cookie');
|
|
1458
1517
|
// Attach all the cookies from the virtual cookie store.
|
|
1459
1518
|
setRequestCookies(mockedRequest);
|
|
1460
|
-
const requestCookies = requestCookiesString
|
|
1461
|
-
? parse_1(requestCookiesString)
|
|
1462
|
-
: {};
|
|
1463
|
-
// Merge both direct request cookies and the cookies inherited
|
|
1464
|
-
// from other same-origin requests in the cookie store.
|
|
1465
|
-
mockedRequest.cookies = Object.assign(Object.assign({}, mockedRequest.cookies), requestCookies);
|
|
1466
1519
|
return mockedRequest;
|
|
1467
1520
|
}
|
|
1468
1521
|
|
|
@@ -1554,6 +1607,9 @@ function setupWorker(...requestHandlers) {
|
|
|
1554
1607
|
const publicEmitter = new lib$1.StrictEventEmitter();
|
|
1555
1608
|
pipeEvents(emitter, publicEmitter);
|
|
1556
1609
|
const context = {
|
|
1610
|
+
// Mocking is not considered enabled until the worker
|
|
1611
|
+
// signals back the successful activation event.
|
|
1612
|
+
isMockingEnabled: false,
|
|
1557
1613
|
startOptions: undefined,
|
|
1558
1614
|
worker: null,
|
|
1559
1615
|
registration: null,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { c as commonjsGlobal, l as lib$1,
|
|
1
|
+
import { c as commonjsGlobal, l as lib$1, s as status, b as set, e as delay, f as fetch, j as jsonParse } from './fetch-deps.js';
|
|
2
2
|
import { getCleanUrl } from '@mswjs/interceptors/lib/utils/getCleanUrl';
|
|
3
3
|
|
|
4
4
|
/*! *****************************************************************************
|
|
@@ -205,6 +205,196 @@ class NetworkError extends Error {
|
|
|
205
205
|
}
|
|
206
206
|
}
|
|
207
207
|
|
|
208
|
+
/**
|
|
209
|
+
* Composes a given list of functions into a new function that
|
|
210
|
+
* executes from right to left.
|
|
211
|
+
*/
|
|
212
|
+
function compose(...fns) {
|
|
213
|
+
return (...args) => {
|
|
214
|
+
return fns.reduceRight((leftFn, rightFn) => {
|
|
215
|
+
return leftFn instanceof Promise
|
|
216
|
+
? Promise.resolve(leftFn).then(rightFn)
|
|
217
|
+
: rightFn(leftFn);
|
|
218
|
+
}, args[0]);
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const defaultResponse = {
|
|
223
|
+
status: 200,
|
|
224
|
+
statusText: 'OK',
|
|
225
|
+
body: null,
|
|
226
|
+
delay: 0,
|
|
227
|
+
once: false,
|
|
228
|
+
passthrough: false,
|
|
229
|
+
};
|
|
230
|
+
const defaultResponseTransformers = [];
|
|
231
|
+
function createResponseComposition(responseOverrides, defaultTransformers = defaultResponseTransformers) {
|
|
232
|
+
return (...transformers) => __awaiter(this, void 0, void 0, function* () {
|
|
233
|
+
const initialResponse = Object.assign({}, defaultResponse, {
|
|
234
|
+
headers: new lib$1.Headers({
|
|
235
|
+
'x-powered-by': 'msw',
|
|
236
|
+
}),
|
|
237
|
+
}, responseOverrides);
|
|
238
|
+
const resolvedTransformers = [
|
|
239
|
+
...defaultTransformers,
|
|
240
|
+
...transformers,
|
|
241
|
+
].filter(Boolean);
|
|
242
|
+
const resolvedResponse = resolvedTransformers.length > 0
|
|
243
|
+
? compose(...resolvedTransformers)(initialResponse)
|
|
244
|
+
: initialResponse;
|
|
245
|
+
return resolvedResponse;
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
const response = Object.assign(createResponseComposition(), {
|
|
249
|
+
once: createResponseComposition({ once: true }),
|
|
250
|
+
networkError(message) {
|
|
251
|
+
throw new NetworkError(message);
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
const BUILD_FRAME = /(node_modules)?[\/\\]lib[\/\\](umd|esm|iief|cjs)[\/\\]|^[^\/\\]*$/;
|
|
256
|
+
/**
|
|
257
|
+
* Return the stack trace frame of a function's invocation.
|
|
258
|
+
*/
|
|
259
|
+
function getCallFrame(error) {
|
|
260
|
+
// In <IE11, new Error may return an undefined stack
|
|
261
|
+
const stack = error.stack;
|
|
262
|
+
if (!stack) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const frames = stack.split('\n').slice(1);
|
|
266
|
+
// Get the first frame that doesn't reference the library's internal trace.
|
|
267
|
+
// Assume that frame is the invocation frame.
|
|
268
|
+
const declarationFrame = frames.find((frame) => {
|
|
269
|
+
return !BUILD_FRAME.test(frame);
|
|
270
|
+
});
|
|
271
|
+
if (!declarationFrame) {
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
// Extract file reference from the stack frame.
|
|
275
|
+
const declarationPath = declarationFrame
|
|
276
|
+
.replace(/\s*at [^()]*\(([^)]+)\)/, '$1')
|
|
277
|
+
.replace(/^@/, '');
|
|
278
|
+
return declarationPath;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Determines if the given function is an iterator.
|
|
283
|
+
*/
|
|
284
|
+
function isIterable(fn) {
|
|
285
|
+
if (!fn) {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
return typeof fn[Symbol.iterator] == 'function';
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const defaultContext = {
|
|
292
|
+
status,
|
|
293
|
+
set,
|
|
294
|
+
delay,
|
|
295
|
+
fetch,
|
|
296
|
+
};
|
|
297
|
+
class RequestHandler {
|
|
298
|
+
constructor(options) {
|
|
299
|
+
this.shouldSkip = false;
|
|
300
|
+
this.ctx = options.ctx || defaultContext;
|
|
301
|
+
this.resolver = options.resolver;
|
|
302
|
+
const callFrame = getCallFrame(new Error());
|
|
303
|
+
this.info = Object.assign(Object.assign({}, options.info), { callFrame });
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Parse the captured request to extract additional information from it.
|
|
307
|
+
* Parsed result is then exposed to other methods of this request handler.
|
|
308
|
+
*/
|
|
309
|
+
parse(_request, _resolutionContext) {
|
|
310
|
+
return null;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Test if this handler matches the given request.
|
|
314
|
+
*/
|
|
315
|
+
test(request, resolutionContext) {
|
|
316
|
+
return this.predicate(request, this.parse(request, resolutionContext), resolutionContext);
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Derive the publicly exposed request (`req`) instance of the response resolver
|
|
320
|
+
* from the captured request and its parsed result.
|
|
321
|
+
*/
|
|
322
|
+
getPublicRequest(request, _parsedResult) {
|
|
323
|
+
return request;
|
|
324
|
+
}
|
|
325
|
+
markAsSkipped(shouldSkip = true) {
|
|
326
|
+
this.shouldSkip = shouldSkip;
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Execute this request handler and produce a mocked response
|
|
330
|
+
* using the given resolver function.
|
|
331
|
+
*/
|
|
332
|
+
run(request, resolutionContext) {
|
|
333
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
334
|
+
if (this.shouldSkip) {
|
|
335
|
+
return null;
|
|
336
|
+
}
|
|
337
|
+
const parsedResult = this.parse(request, resolutionContext);
|
|
338
|
+
const shouldIntercept = this.predicate(request, parsedResult, resolutionContext);
|
|
339
|
+
if (!shouldIntercept) {
|
|
340
|
+
return null;
|
|
341
|
+
}
|
|
342
|
+
const publicRequest = this.getPublicRequest(request, parsedResult);
|
|
343
|
+
// Create a response extraction wrapper around the resolver
|
|
344
|
+
// since it can be both an async function and a generator.
|
|
345
|
+
const executeResolver = this.wrapResolver(this.resolver);
|
|
346
|
+
const mockedResponse = yield executeResolver(publicRequest, response, this.ctx);
|
|
347
|
+
return this.createExecutionResult(parsedResult, publicRequest, mockedResponse);
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
wrapResolver(resolver) {
|
|
351
|
+
return (req, res, ctx) => __awaiter(this, void 0, void 0, function* () {
|
|
352
|
+
const result = this.resolverGenerator || (yield resolver(req, res, ctx));
|
|
353
|
+
if (isIterable(result)) {
|
|
354
|
+
const { value, done } = result[Symbol.iterator]().next();
|
|
355
|
+
const nextResponse = yield value;
|
|
356
|
+
// If the generator is done and there is no next value,
|
|
357
|
+
// return the previous generator's value.
|
|
358
|
+
if (!nextResponse && done) {
|
|
359
|
+
return this.resolverGeneratorResult;
|
|
360
|
+
}
|
|
361
|
+
if (!this.resolverGenerator) {
|
|
362
|
+
this.resolverGenerator = result;
|
|
363
|
+
}
|
|
364
|
+
this.resolverGeneratorResult = nextResponse;
|
|
365
|
+
return nextResponse;
|
|
366
|
+
}
|
|
367
|
+
return result;
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
createExecutionResult(parsedResult, request, response) {
|
|
371
|
+
return {
|
|
372
|
+
handler: this,
|
|
373
|
+
parsedResult: parsedResult || null,
|
|
374
|
+
request,
|
|
375
|
+
response: response || null,
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Bypass this intercepted request.
|
|
381
|
+
* This will make a call to the actual endpoint requested.
|
|
382
|
+
*/
|
|
383
|
+
function passthrough() {
|
|
384
|
+
// Constructing a dummy "101 Continue" mocked response
|
|
385
|
+
// to keep the return type of the resolver consistent.
|
|
386
|
+
return {
|
|
387
|
+
status: 101,
|
|
388
|
+
statusText: 'Continue',
|
|
389
|
+
headers: new lib$1.Headers(),
|
|
390
|
+
body: null,
|
|
391
|
+
// Setting "passthrough" to true will signal the response pipeline
|
|
392
|
+
// to perform this intercepted request as-is.
|
|
393
|
+
passthrough: true,
|
|
394
|
+
once: false,
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
|
|
208
398
|
function parseContentHeaders(headersString) {
|
|
209
399
|
var _a, _b;
|
|
210
400
|
const headers = lib$1.stringToHeaders(headersString);
|
|
@@ -799,175 +989,4 @@ function matchRequestUrl(url, path, baseUrl) {
|
|
|
799
989
|
};
|
|
800
990
|
}
|
|
801
991
|
|
|
802
|
-
|
|
803
|
-
* Composes a given list of functions into a new function that
|
|
804
|
-
* executes from right to left.
|
|
805
|
-
*/
|
|
806
|
-
function compose(...fns) {
|
|
807
|
-
return (...args) => {
|
|
808
|
-
return fns.reduceRight((leftFn, rightFn) => {
|
|
809
|
-
return leftFn instanceof Promise
|
|
810
|
-
? Promise.resolve(leftFn).then(rightFn)
|
|
811
|
-
: rightFn(leftFn);
|
|
812
|
-
}, args[0]);
|
|
813
|
-
};
|
|
814
|
-
}
|
|
815
|
-
|
|
816
|
-
const defaultResponse = {
|
|
817
|
-
status: 200,
|
|
818
|
-
statusText: 'OK',
|
|
819
|
-
body: null,
|
|
820
|
-
delay: 0,
|
|
821
|
-
once: false,
|
|
822
|
-
};
|
|
823
|
-
const defaultResponseTransformers = [];
|
|
824
|
-
function createResponseComposition(responseOverrides, defaultTransformers = defaultResponseTransformers) {
|
|
825
|
-
return (...transformers) => __awaiter(this, void 0, void 0, function* () {
|
|
826
|
-
const initialResponse = Object.assign({}, defaultResponse, {
|
|
827
|
-
headers: new lib$1.Headers({
|
|
828
|
-
'x-powered-by': 'msw',
|
|
829
|
-
}),
|
|
830
|
-
}, responseOverrides);
|
|
831
|
-
const resolvedTransformers = [
|
|
832
|
-
...defaultTransformers,
|
|
833
|
-
...transformers,
|
|
834
|
-
].filter(Boolean);
|
|
835
|
-
const resolvedResponse = resolvedTransformers.length > 0
|
|
836
|
-
? compose(...resolvedTransformers)(initialResponse)
|
|
837
|
-
: initialResponse;
|
|
838
|
-
return resolvedResponse;
|
|
839
|
-
});
|
|
840
|
-
}
|
|
841
|
-
const response = Object.assign(createResponseComposition(), {
|
|
842
|
-
once: createResponseComposition({ once: true }),
|
|
843
|
-
networkError(message) {
|
|
844
|
-
throw new NetworkError(message);
|
|
845
|
-
},
|
|
846
|
-
});
|
|
847
|
-
|
|
848
|
-
const BUILD_FRAME = /(node_modules)?[\/\\]lib[\/\\](umd|esm|iief|cjs)[\/\\]|^[^\/\\]*$/;
|
|
849
|
-
/**
|
|
850
|
-
* Return the stack trace frame of a function's invocation.
|
|
851
|
-
*/
|
|
852
|
-
function getCallFrame(error) {
|
|
853
|
-
// In <IE11, new Error may return an undefined stack
|
|
854
|
-
const stack = error.stack;
|
|
855
|
-
if (!stack) {
|
|
856
|
-
return;
|
|
857
|
-
}
|
|
858
|
-
const frames = stack.split('\n').slice(1);
|
|
859
|
-
// Get the first frame that doesn't reference the library's internal trace.
|
|
860
|
-
// Assume that frame is the invocation frame.
|
|
861
|
-
const declarationFrame = frames.find((frame) => {
|
|
862
|
-
return !BUILD_FRAME.test(frame);
|
|
863
|
-
});
|
|
864
|
-
if (!declarationFrame) {
|
|
865
|
-
return;
|
|
866
|
-
}
|
|
867
|
-
// Extract file reference from the stack frame.
|
|
868
|
-
const declarationPath = declarationFrame
|
|
869
|
-
.replace(/\s*at [^()]*\(([^)]+)\)/, '$1')
|
|
870
|
-
.replace(/^@/, '');
|
|
871
|
-
return declarationPath;
|
|
872
|
-
}
|
|
873
|
-
|
|
874
|
-
/**
|
|
875
|
-
* Determines if the given function is an iterator.
|
|
876
|
-
*/
|
|
877
|
-
function isIterable(fn) {
|
|
878
|
-
if (!fn) {
|
|
879
|
-
return false;
|
|
880
|
-
}
|
|
881
|
-
return typeof fn[Symbol.iterator] == 'function';
|
|
882
|
-
}
|
|
883
|
-
|
|
884
|
-
const defaultContext = {
|
|
885
|
-
status,
|
|
886
|
-
set,
|
|
887
|
-
delay,
|
|
888
|
-
fetch,
|
|
889
|
-
};
|
|
890
|
-
class RequestHandler {
|
|
891
|
-
constructor(options) {
|
|
892
|
-
this.shouldSkip = false;
|
|
893
|
-
this.ctx = options.ctx || defaultContext;
|
|
894
|
-
this.resolver = options.resolver;
|
|
895
|
-
const callFrame = getCallFrame(new Error());
|
|
896
|
-
this.info = Object.assign(Object.assign({}, options.info), { callFrame });
|
|
897
|
-
}
|
|
898
|
-
/**
|
|
899
|
-
* Parse the captured request to extract additional information from it.
|
|
900
|
-
* Parsed result is then exposed to other methods of this request handler.
|
|
901
|
-
*/
|
|
902
|
-
parse(_request, _resolutionContext) {
|
|
903
|
-
return null;
|
|
904
|
-
}
|
|
905
|
-
/**
|
|
906
|
-
* Test if this handler matches the given request.
|
|
907
|
-
*/
|
|
908
|
-
test(request, resolutionContext) {
|
|
909
|
-
return this.predicate(request, this.parse(request, resolutionContext), resolutionContext);
|
|
910
|
-
}
|
|
911
|
-
/**
|
|
912
|
-
* Derive the publicly exposed request (`req`) instance of the response resolver
|
|
913
|
-
* from the captured request and its parsed result.
|
|
914
|
-
*/
|
|
915
|
-
getPublicRequest(request, _parsedResult) {
|
|
916
|
-
return request;
|
|
917
|
-
}
|
|
918
|
-
markAsSkipped(shouldSkip = true) {
|
|
919
|
-
this.shouldSkip = shouldSkip;
|
|
920
|
-
}
|
|
921
|
-
/**
|
|
922
|
-
* Execute this request handler and produce a mocked response
|
|
923
|
-
* using the given resolver function.
|
|
924
|
-
*/
|
|
925
|
-
run(request, resolutionContext) {
|
|
926
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
927
|
-
if (this.shouldSkip) {
|
|
928
|
-
return null;
|
|
929
|
-
}
|
|
930
|
-
const parsedResult = this.parse(request, resolutionContext);
|
|
931
|
-
const shouldIntercept = this.predicate(request, parsedResult, resolutionContext);
|
|
932
|
-
if (!shouldIntercept) {
|
|
933
|
-
return null;
|
|
934
|
-
}
|
|
935
|
-
const publicRequest = this.getPublicRequest(request, parsedResult);
|
|
936
|
-
// Create a response extraction wrapper around the resolver
|
|
937
|
-
// since it can be both an async function and a generator.
|
|
938
|
-
const executeResolver = this.wrapResolver(this.resolver);
|
|
939
|
-
const mockedResponse = yield executeResolver(publicRequest, response, this.ctx);
|
|
940
|
-
return this.createExecutionResult(parsedResult, publicRequest, mockedResponse);
|
|
941
|
-
});
|
|
942
|
-
}
|
|
943
|
-
wrapResolver(resolver) {
|
|
944
|
-
return (req, res, ctx) => __awaiter(this, void 0, void 0, function* () {
|
|
945
|
-
const result = this.resolverGenerator || (yield resolver(req, res, ctx));
|
|
946
|
-
if (isIterable(result)) {
|
|
947
|
-
const { value, done } = result[Symbol.iterator]().next();
|
|
948
|
-
const nextResponse = yield value;
|
|
949
|
-
// If the generator is done and there is no next value,
|
|
950
|
-
// return the previous generator's value.
|
|
951
|
-
if (!nextResponse && done) {
|
|
952
|
-
return this.resolverGeneratorResult;
|
|
953
|
-
}
|
|
954
|
-
if (!this.resolverGenerator) {
|
|
955
|
-
this.resolverGenerator = result;
|
|
956
|
-
}
|
|
957
|
-
this.resolverGeneratorResult = nextResponse;
|
|
958
|
-
return nextResponse;
|
|
959
|
-
}
|
|
960
|
-
return result;
|
|
961
|
-
});
|
|
962
|
-
}
|
|
963
|
-
createExecutionResult(parsedResult, request, response) {
|
|
964
|
-
return {
|
|
965
|
-
handler: this,
|
|
966
|
-
parsedResult: parsedResult || null,
|
|
967
|
-
request,
|
|
968
|
-
response: response || null,
|
|
969
|
-
};
|
|
970
|
-
}
|
|
971
|
-
}
|
|
972
|
-
|
|
973
|
-
export { NetworkError as N, RequestHandler as R, __awaiter as _, defaultResponse as a, defaultContext as b, createResponseComposition as c, devUtils as d, compose as e, cleanUrl as f, getPublicUrlFromRequest as g, getSearchParams as h, prepareRequest as i, prepareResponse as j, getStatusCodeColor as k, getTimestamp as l, matchRequestUrl as m, __rest as n, parseBody as p, response as r };
|
|
992
|
+
export { NetworkError as N, RequestHandler as R, __awaiter as _, parseBody as a, defaultResponse as b, createResponseComposition as c, devUtils as d, defaultContext as e, compose as f, getPublicUrlFromRequest as g, cleanUrl as h, getSearchParams as i, prepareRequest as j, prepareResponse as k, getStatusCodeColor as l, matchRequestUrl as m, getTimestamp as n, __rest as o, passthrough as p, response as r };
|
package/lib/esm/rest-deps.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { R as RequestHandler,
|
|
2
|
-
import {
|
|
1
|
+
import { e as defaultContext, R as RequestHandler, h as cleanUrl, i as getSearchParams, d as devUtils, m as matchRequestUrl, g as getPublicUrlFromRequest, j as prepareRequest, k as prepareResponse, l as getStatusCodeColor, n as getTimestamp } from './matchRequestUrl-deps.js';
|
|
2
|
+
import { d as cookie, g as json } from './fetch-deps.js';
|
|
3
3
|
import { b as body, t as text, x as xml } from './xml-deps.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -19,17 +19,11 @@ var RESTMethods;
|
|
|
19
19
|
RESTMethods["OPTIONS"] = "OPTIONS";
|
|
20
20
|
RESTMethods["DELETE"] = "DELETE";
|
|
21
21
|
})(RESTMethods || (RESTMethods = {}));
|
|
22
|
-
const restContext = {
|
|
23
|
-
set,
|
|
24
|
-
status,
|
|
25
|
-
cookie,
|
|
22
|
+
const restContext = Object.assign(Object.assign({}, defaultContext), { cookie,
|
|
26
23
|
body,
|
|
27
24
|
text,
|
|
28
25
|
json,
|
|
29
|
-
xml
|
|
30
|
-
delay,
|
|
31
|
-
fetch,
|
|
32
|
-
};
|
|
26
|
+
xml });
|
|
33
27
|
/**
|
|
34
28
|
* Request handler for REST API requests.
|
|
35
29
|
* Provides request matching based on method and URL.
|