msw 0.39.2 → 0.40.2
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 +69 -23
- 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/umd/index.js +261 -208
- package/lib/umd/mockServiceWorker.js +1 -1
- package/native/lib/index.js +537 -489
- package/native/package.json +1 -1
- package/node/lib/index.js +537 -489
- package/node/package.json +1 -1
- package/package.json +14 -7
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
|
}
|
|
@@ -812,6 +830,7 @@ function parseWorkerRequest(rawRequest) {
|
|
|
812
830
|
body: pruneGetRequestBody(rawRequest),
|
|
813
831
|
bodyUsed: rawRequest.bodyUsed,
|
|
814
832
|
headers: new lib$2.Headers(rawRequest.headers),
|
|
833
|
+
passthrough,
|
|
815
834
|
};
|
|
816
835
|
// Set document cookies on the request.
|
|
817
836
|
setRequestCookies(request);
|
|
@@ -1118,17 +1137,24 @@ function readResponseCookies(request, response) {
|
|
|
1118
1137
|
}
|
|
1119
1138
|
|
|
1120
1139
|
function handleRequest(request, handlers, options, emitter, handleRequestOptions) {
|
|
1121
|
-
var _a, _b, _c;
|
|
1140
|
+
var _a, _b, _c, _d;
|
|
1122
1141
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1123
1142
|
emitter.emit('request:start', request);
|
|
1124
1143
|
// Perform bypassed requests (i.e. issued via "ctx.fetch") as-is.
|
|
1125
1144
|
if (request.headers.get('x-msw-bypass') === 'true') {
|
|
1126
1145
|
emitter.emit('request:end', request);
|
|
1127
|
-
(_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);
|
|
1128
1147
|
return;
|
|
1129
1148
|
}
|
|
1130
1149
|
// Resolve a mocked response from the list of request handlers.
|
|
1131
|
-
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
|
+
}
|
|
1132
1158
|
const { handler, response } = lookupResult;
|
|
1133
1159
|
// When there's no handler for the request, consider it unhandled.
|
|
1134
1160
|
// Allow the developer to react to such cases.
|
|
@@ -1136,7 +1162,7 @@ function handleRequest(request, handlers, options, emitter, handleRequestOptions
|
|
|
1136
1162
|
onUnhandledRequest(request, handlers, options.onUnhandledRequest);
|
|
1137
1163
|
emitter.emit('request:unhandled', request);
|
|
1138
1164
|
emitter.emit('request:end', request);
|
|
1139
|
-
(_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);
|
|
1140
1166
|
return;
|
|
1141
1167
|
}
|
|
1142
1168
|
// When the handled request returned no mocked response, warn the developer,
|
|
@@ -1149,7 +1175,14 @@ Expected response resolver to return a mocked response Object, but got %s. The o
|
|
|
1149
1175
|
%s\
|
|
1150
1176
|
`, response, handler.info.header, handler.info.callFrame);
|
|
1151
1177
|
emitter.emit('request:end', request);
|
|
1152
|
-
(_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);
|
|
1153
1186
|
return;
|
|
1154
1187
|
}
|
|
1155
1188
|
// Store all the received response cookies in the virtual cookie store.
|
|
@@ -1180,7 +1213,7 @@ const createRequestListener = (context, options) => {
|
|
|
1180
1213
|
transformResponse(response) {
|
|
1181
1214
|
return Object.assign(Object.assign({}, response), { headers: response.headers.all() });
|
|
1182
1215
|
},
|
|
1183
|
-
|
|
1216
|
+
onPassthroughResponse() {
|
|
1184
1217
|
return channel.send({
|
|
1185
1218
|
type: 'MOCK_NOT_FOUND',
|
|
1186
1219
|
});
|
|
@@ -1210,19 +1243,21 @@ const createRequestListener = (context, options) => {
|
|
|
1210
1243
|
},
|
|
1211
1244
|
});
|
|
1212
1245
|
}
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1246
|
+
if (error instanceof Error) {
|
|
1247
|
+
// Treat all the other exceptions in a request handler
|
|
1248
|
+
// as unintended, alerting that there is a problem needs fixing.
|
|
1249
|
+
channel.send({
|
|
1250
|
+
type: 'INTERNAL_ERROR',
|
|
1251
|
+
payload: {
|
|
1252
|
+
status: 500,
|
|
1253
|
+
body: JSON.stringify({
|
|
1254
|
+
errorType: error.constructor.name,
|
|
1255
|
+
message: error.message,
|
|
1256
|
+
location: error.stack,
|
|
1257
|
+
}),
|
|
1258
|
+
},
|
|
1259
|
+
});
|
|
1260
|
+
}
|
|
1226
1261
|
}
|
|
1227
1262
|
});
|
|
1228
1263
|
};
|
|
@@ -1402,12 +1437,19 @@ function printStopMessage(args = {}) {
|
|
|
1402
1437
|
const createStop = (context) => {
|
|
1403
1438
|
return function stop() {
|
|
1404
1439
|
var _a;
|
|
1440
|
+
// Warn developers calling "worker.stop()" more times than necessary.
|
|
1441
|
+
// This likely indicates a mistake in their code.
|
|
1442
|
+
if (!context.isMockingEnabled) {
|
|
1443
|
+
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.');
|
|
1444
|
+
return;
|
|
1445
|
+
}
|
|
1405
1446
|
/**
|
|
1406
1447
|
* Signal the Service Worker to disable mocking for this client.
|
|
1407
1448
|
* Use this an an explicit way to stop the mocking, while preserving
|
|
1408
1449
|
* the worker-client relation. Does not affect the worker's lifecycle.
|
|
1409
1450
|
*/
|
|
1410
1451
|
context.workerChannel.send('MOCK_DEACTIVATE');
|
|
1452
|
+
context.isMockingEnabled = false;
|
|
1411
1453
|
window.clearInterval(context.keepAliveInterval);
|
|
1412
1454
|
printStopMessage({ quiet: (_a = context.startOptions) === null || _a === void 0 ? void 0 : _a.quiet });
|
|
1413
1455
|
};
|
|
@@ -1472,6 +1514,7 @@ function parseIsomorphicRequest(request) {
|
|
|
1472
1514
|
integrity: '',
|
|
1473
1515
|
destination: 'document',
|
|
1474
1516
|
bodyUsed: false,
|
|
1517
|
+
passthrough,
|
|
1475
1518
|
};
|
|
1476
1519
|
// Attach all the cookies from the virtual cookie store.
|
|
1477
1520
|
setRequestCookies(mockedRequest);
|
|
@@ -1566,6 +1609,9 @@ function setupWorker(...requestHandlers) {
|
|
|
1566
1609
|
const publicEmitter = new lib$1.StrictEventEmitter();
|
|
1567
1610
|
pipeEvents(emitter, publicEmitter);
|
|
1568
1611
|
const context = {
|
|
1612
|
+
// Mocking is not considered enabled until the worker
|
|
1613
|
+
// signals back the successful activation event.
|
|
1614
|
+
isMockingEnabled: false,
|
|
1569
1615
|
startOptions: undefined,
|
|
1570
1616
|
worker: null,
|
|
1571
1617
|
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.
|
package/lib/esm/rest.js
CHANGED