msw 0.22.2 → 0.24.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/lib/esm/errors-deps.js +48 -8
- package/lib/esm/fetch-deps.js +37 -35
- package/lib/esm/{matchRequestUrl-deps.js → getCallFrame-deps.js} +27 -15
- package/lib/esm/graphql.js +46 -2
- package/lib/esm/index.js +34 -23
- package/lib/esm/rest-deps.js +71 -1
- package/lib/esm/rest.js +1 -1
- package/lib/esm/xml-deps.js +14 -10
- package/lib/types/context/body.d.ts +4 -2
- package/lib/types/context/cookie.d.ts +2 -3
- package/lib/types/context/data.d.ts +5 -3
- package/lib/types/context/delay.d.ts +4 -3
- package/lib/types/context/errors.d.ts +3 -3
- package/lib/types/context/fetch.d.ts +4 -3
- package/lib/types/context/json.d.ts +7 -8
- package/lib/types/context/set.d.ts +4 -0
- package/lib/types/context/status.d.ts +7 -0
- package/lib/types/context/text.d.ts +4 -3
- package/lib/types/context/xml.d.ts +4 -2
- package/lib/types/graphql.d.ts +58 -2
- package/lib/types/index.d.ts +2 -1
- package/lib/types/node/glossary.d.ts +13 -3
- package/lib/types/node/setupServer.d.ts +5 -0
- package/lib/types/response.d.ts +5 -2
- package/lib/types/rest.d.ts +87 -187
- package/lib/types/setupWorker/glossary.d.ts +23 -2
- package/lib/types/setupWorker/setupWorker.d.ts +6 -0
- package/lib/types/utils/handlers/requestHandler.d.ts +13 -1
- package/lib/types/utils/internal/compose.d.ts +9 -1
- package/lib/types/utils/internal/getCallFrame.d.ts +4 -0
- package/lib/umd/index.js +242 -58
- package/native/index.js +2011 -114
- package/node/index.js +2016 -114
- package/package.json +1 -1
package/lib/esm/errors-deps.js
CHANGED
|
@@ -1,20 +1,60 @@
|
|
|
1
|
-
import { j as json } from './fetch-deps.js';
|
|
1
|
+
import { e as jsonParse, j as json } from './fetch-deps.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Determines if the given value is an object.
|
|
5
|
+
*/
|
|
6
|
+
function isObject(value) {
|
|
7
|
+
return value != null && typeof value === 'object' && !Array.isArray(value);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Deeply merges two given objects with the right one
|
|
12
|
+
* having a priority during property assignment.
|
|
13
|
+
*/
|
|
14
|
+
function mergeRight(left, right) {
|
|
15
|
+
return Object.entries(right).reduce((result, [key, rightValue]) => {
|
|
16
|
+
const leftValue = result[key];
|
|
17
|
+
if (Array.isArray(leftValue) && Array.isArray(rightValue)) {
|
|
18
|
+
result[key] = leftValue.concat(rightValue);
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
if (isObject(leftValue) && isObject(rightValue)) {
|
|
22
|
+
result[key] = mergeRight(leftValue, rightValue);
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
result[key] = rightValue;
|
|
26
|
+
return result;
|
|
27
|
+
}, Object.assign({}, left));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Sets a given payload as a GraphQL response body.
|
|
32
|
+
* @example
|
|
33
|
+
* res(ctx.data({ user: { firstName: 'John' }}))
|
|
34
|
+
* @see {@link https://mswjs.io/docs/api/context/data `ctx.data()`}
|
|
5
35
|
*/
|
|
6
36
|
const data = (payload) => {
|
|
7
|
-
return
|
|
37
|
+
return (res) => {
|
|
38
|
+
const prevBody = jsonParse(res.body) || {};
|
|
39
|
+
const nextBody = mergeRight(prevBody, { data: payload });
|
|
40
|
+
return json(nextBody)(res);
|
|
41
|
+
};
|
|
8
42
|
};
|
|
9
43
|
|
|
10
44
|
/**
|
|
11
45
|
* Sets a given list of GraphQL errors on the mocked response.
|
|
46
|
+
* @example res(ctx.errors([{ message: 'Unauthorized' }]))
|
|
47
|
+
* @see {@link https://mswjs.io/docs/api/context/errors}
|
|
12
48
|
*/
|
|
13
49
|
const errors = (errorsList) => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
50
|
+
return (res) => {
|
|
51
|
+
if (errorsList == null) {
|
|
52
|
+
return res;
|
|
53
|
+
}
|
|
54
|
+
const prevBody = jsonParse(res.body) || {};
|
|
55
|
+
const nextBody = mergeRight(prevBody, { errors: errorsList });
|
|
56
|
+
return json(nextBody)(res);
|
|
57
|
+
};
|
|
18
58
|
};
|
|
19
59
|
|
|
20
|
-
export { data as d, errors as e };
|
|
60
|
+
export { data as d, errors as e, mergeRight as m };
|
package/lib/esm/fetch-deps.js
CHANGED
|
@@ -64,6 +64,13 @@ var statuses = {
|
|
|
64
64
|
"511": "Network Authentication Required"
|
|
65
65
|
};
|
|
66
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Sets a response status code and text.
|
|
69
|
+
* @example
|
|
70
|
+
* res(ctx.status(301))
|
|
71
|
+
* res(ctx.status(400, 'Custom status text'))
|
|
72
|
+
* @see {@link https://mswjs.io/docs/api/context/status `ctx.status()`}
|
|
73
|
+
*/
|
|
67
74
|
const status = (statusCode, statusText) => {
|
|
68
75
|
return (res) => {
|
|
69
76
|
res.status = statusCode;
|
|
@@ -327,6 +334,10 @@ exports.flattenHeadersList = flattenHeadersList_1.flattenHeadersList;
|
|
|
327
334
|
exports.flattenHeadersObject = flattenHeadersObject_1.flattenHeadersObject;
|
|
328
335
|
});
|
|
329
336
|
|
|
337
|
+
/**
|
|
338
|
+
* Sets one or multiple response headers.
|
|
339
|
+
* @see {@link https://mswjs.io/docs/api/context/set `ctx.set()`}
|
|
340
|
+
*/
|
|
330
341
|
function set(...args) {
|
|
331
342
|
return (res) => {
|
|
332
343
|
const [name, value] = args;
|
|
@@ -344,43 +355,32 @@ function set(...args) {
|
|
|
344
355
|
}
|
|
345
356
|
|
|
346
357
|
/**
|
|
347
|
-
*
|
|
348
|
-
|
|
349
|
-
function isObject(value) {
|
|
350
|
-
return value != null && typeof value === 'object' && !Array.isArray(value);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* Deeply merges two given objects with the right one
|
|
355
|
-
* having a priority during property assignment.
|
|
358
|
+
* Parses a given string into a JSON.
|
|
359
|
+
* Does not throw an exception on an invalid JSON string.
|
|
356
360
|
*/
|
|
357
|
-
function
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
if (isObject(leftValue) && isObject(rightValue)) {
|
|
365
|
-
result[key] = mergeRight(leftValue, rightValue);
|
|
366
|
-
return result;
|
|
367
|
-
}
|
|
368
|
-
result[key] = rightValue;
|
|
369
|
-
return result;
|
|
370
|
-
}, Object.assign({}, left));
|
|
361
|
+
function jsonParse(str) {
|
|
362
|
+
try {
|
|
363
|
+
return JSON.parse(str);
|
|
364
|
+
}
|
|
365
|
+
catch (error) {
|
|
366
|
+
return undefined;
|
|
367
|
+
}
|
|
371
368
|
}
|
|
372
369
|
|
|
373
370
|
/**
|
|
374
371
|
* Sets the given value as the JSON body of the response.
|
|
372
|
+
* Appends a `Content-Type: application/json` header on the
|
|
373
|
+
* mocked response.
|
|
375
374
|
* @example
|
|
376
|
-
* res(json(
|
|
377
|
-
* res(json('
|
|
378
|
-
* res(json([1, '2', false, { ok: true }]))
|
|
375
|
+
* res(ctx.json('Some string'))
|
|
376
|
+
* res(ctx.json({ key: 'value' }))
|
|
377
|
+
* res(ctx.json([1, '2', false, { ok: true }]))
|
|
378
|
+
* @see {@link https://mswjs.io/docs/api/context/json `ctx.json()`}
|
|
379
379
|
*/
|
|
380
|
-
const json = (body
|
|
380
|
+
const json = (body) => {
|
|
381
381
|
return (res) => {
|
|
382
382
|
res.headers.set('Content-Type', 'application/json');
|
|
383
|
-
res.body =
|
|
383
|
+
res.body = JSON.stringify(body);
|
|
384
384
|
return res;
|
|
385
385
|
};
|
|
386
386
|
};
|
|
@@ -412,10 +412,11 @@ const getRandomServerResponseTime = () => {
|
|
|
412
412
|
MIN_SERVER_RESPONSE_TIME);
|
|
413
413
|
};
|
|
414
414
|
/**
|
|
415
|
-
* Delays the
|
|
415
|
+
* Delays the response by the given duration (ms).
|
|
416
416
|
* @example
|
|
417
|
-
* res(delay()) // realistic server response time
|
|
418
|
-
* res(delay(
|
|
417
|
+
* res(ctx.delay()) // realistic server response time
|
|
418
|
+
* res(ctx.delay(1200))
|
|
419
|
+
* @see {@link https://mswjs.io/docs/api/context/delay `ctx.delay()`}
|
|
419
420
|
*/
|
|
420
421
|
const delay = (durationMs) => {
|
|
421
422
|
return (res) => {
|
|
@@ -441,9 +442,10 @@ const createFetchRequestParameters = (input) => {
|
|
|
441
442
|
return requestParameters;
|
|
442
443
|
};
|
|
443
444
|
/**
|
|
444
|
-
*
|
|
445
|
-
*
|
|
446
|
-
*
|
|
445
|
+
* Performs a bypassed request inside a request handler.
|
|
446
|
+
* @example
|
|
447
|
+
* const originalResponse = await ctx.fetch(req)
|
|
448
|
+
* @see {@link https://mswjs.io/docs/api/context/fetch `ctx.fetch()`}
|
|
447
449
|
*/
|
|
448
450
|
const fetch = (input, requestInit = {}) => {
|
|
449
451
|
// Keep the default `window.fetch()` call signature
|
|
@@ -455,4 +457,4 @@ const fetch = (input, requestInit = {}) => {
|
|
|
455
457
|
return useFetch(input.url.href, compliantRequest);
|
|
456
458
|
};
|
|
457
459
|
|
|
458
|
-
export { set as a, commonjsGlobal as b, createCommonjsModule as c, delay as d, fetch as f, isNodeProcess as i, json as j, lib as l,
|
|
460
|
+
export { set as a, commonjsGlobal as b, createCommonjsModule as c, delay as d, jsonParse as e, fetch as f, isNodeProcess as i, json as j, lib as l, status as s };
|
|
@@ -1,17 +1,4 @@
|
|
|
1
|
-
import { l as lib, c as createCommonjsModule } from './fetch-deps.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Parses a given string into a JSON.
|
|
5
|
-
* Does not throw an exception on an invalid JSON string.
|
|
6
|
-
*/
|
|
7
|
-
function jsonParse(str) {
|
|
8
|
-
try {
|
|
9
|
-
return JSON.parse(str);
|
|
10
|
-
}
|
|
11
|
-
catch (error) {
|
|
12
|
-
return undefined;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
1
|
+
import { e as jsonParse, l as lib, c as createCommonjsModule } from './fetch-deps.js';
|
|
15
2
|
|
|
16
3
|
/**
|
|
17
4
|
* Parses a given request/response body based on the `Content-Type` header.
|
|
@@ -179,4 +166,29 @@ function matchRequestUrl(url, mask) {
|
|
|
179
166
|
return match(cleanMask, cleanRequestUrl);
|
|
180
167
|
}
|
|
181
168
|
|
|
182
|
-
|
|
169
|
+
/**
|
|
170
|
+
* Return the stack trace frame of a function's invocation.
|
|
171
|
+
*/
|
|
172
|
+
function getCallFrame() {
|
|
173
|
+
try {
|
|
174
|
+
const inspectionError = new Error();
|
|
175
|
+
inspectionError.name = 'Inspection Error';
|
|
176
|
+
throw inspectionError;
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
const frames = error.stack.split('\n');
|
|
180
|
+
// Get the first frame that doesn't reference the library's internal trace.
|
|
181
|
+
// Assume that frame is the invocation frame.
|
|
182
|
+
const declarationFrame = frames.slice(1).find((frame) => {
|
|
183
|
+
return !/(node_modules)?\/lib\/(umd|esm)\//.test(frame);
|
|
184
|
+
});
|
|
185
|
+
if (!declarationFrame) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
// Extract file reference from the stack frame.
|
|
189
|
+
const [, declarationPath] = declarationFrame.match(/\((.+?)\)$/) || [];
|
|
190
|
+
return declarationPath;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export { getCallFrame as a, prepareRequest as b, prepareResponse as c, getTimestamp as d, getStatusCodeColor as e, getUrlByMask as g, matchRequestUrl as m, parseBody as p };
|
package/lib/esm/graphql.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { a as set, s as status, d as delay, f as fetch } from './fetch-deps.js';
|
|
1
|
+
import { a as set, s as status, d as delay, f as fetch, e as jsonParse } from './fetch-deps.js';
|
|
2
2
|
import { d as data, e as errors } from './errors-deps.js';
|
|
3
|
-
import {
|
|
3
|
+
import { m as matchRequestUrl, b as prepareRequest, c as prepareResponse, d as getTimestamp, e as getStatusCodeColor, a as getCallFrame } from './getCallFrame-deps.js';
|
|
4
4
|
|
|
5
5
|
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
|
6
6
|
|
|
@@ -3143,6 +3143,7 @@ function parseQuery(query, definitionOperation = 'query') {
|
|
|
3143
3143
|
};
|
|
3144
3144
|
}
|
|
3145
3145
|
function graphQLRequestHandler(expectedOperationType, expectedOperationName, mask, resolver) {
|
|
3146
|
+
const callFrame = getCallFrame();
|
|
3146
3147
|
return {
|
|
3147
3148
|
resolver,
|
|
3148
3149
|
parse(req) {
|
|
@@ -3214,6 +3215,17 @@ function graphQLRequestHandler(expectedOperationType, expectedOperationName, mas
|
|
|
3214
3215
|
console.log('Response:', loggedResponse);
|
|
3215
3216
|
console.groupEnd();
|
|
3216
3217
|
},
|
|
3218
|
+
getMetaInfo() {
|
|
3219
|
+
const header = expectedOperationType === 'all'
|
|
3220
|
+
? `[graphql] ${expectedOperationType} (origin: ${mask.toString()})`
|
|
3221
|
+
: `[graphql] ${expectedOperationType} ${expectedOperationName} (origin: ${mask.toString()})`;
|
|
3222
|
+
return {
|
|
3223
|
+
type: 'graphql',
|
|
3224
|
+
header,
|
|
3225
|
+
mask,
|
|
3226
|
+
callFrame,
|
|
3227
|
+
};
|
|
3228
|
+
},
|
|
3217
3229
|
};
|
|
3218
3230
|
}
|
|
3219
3231
|
const createGraphQLScopedHandler = (expectedOperationType, mask) => {
|
|
@@ -3227,10 +3239,42 @@ const createGraphQLOperationHandler = (mask) => {
|
|
|
3227
3239
|
};
|
|
3228
3240
|
};
|
|
3229
3241
|
const graphqlStandardHandlers = {
|
|
3242
|
+
/**
|
|
3243
|
+
* Captures any GraphQL operation, regardless of its name, under the current scope.
|
|
3244
|
+
* @example
|
|
3245
|
+
* graphql.operation((req, res, ctx) => {
|
|
3246
|
+
* return res(ctx.data({ name: 'John' }))
|
|
3247
|
+
* })
|
|
3248
|
+
* @see {@link https://mswjs.io/docs/api/graphql/operation `graphql.operation()`}
|
|
3249
|
+
*/
|
|
3230
3250
|
operation: createGraphQLOperationHandler('*'),
|
|
3251
|
+
/**
|
|
3252
|
+
* Captures a GraphQL query by a given name.
|
|
3253
|
+
* @example
|
|
3254
|
+
* graphql.query('GetUser', (req, res, ctx) => {
|
|
3255
|
+
* return res(ctx.data({ user: { name: 'John' } }))
|
|
3256
|
+
* })
|
|
3257
|
+
* @see {@link https://mswjs.io/docs/api/graphql/query `graphql.query()`}
|
|
3258
|
+
*/
|
|
3231
3259
|
query: createGraphQLScopedHandler('query', '*'),
|
|
3260
|
+
/**
|
|
3261
|
+
* Captures a GraphQL mutation by a given name.
|
|
3262
|
+
* @example
|
|
3263
|
+
* graphql.mutation('SavePost', (req, res, ctx) => {
|
|
3264
|
+
* return res(ctx.data({ post: { id: 'abc-123' } }))
|
|
3265
|
+
* })
|
|
3266
|
+
* @see {@link https://mswjs.io/docs/api/graphql/mutation `graphql.mutation()`}
|
|
3267
|
+
*/
|
|
3232
3268
|
mutation: createGraphQLScopedHandler('mutation', '*'),
|
|
3233
3269
|
};
|
|
3270
|
+
/**
|
|
3271
|
+
* Creates a GraphQL mocking API scoped to the given endpoint.
|
|
3272
|
+
* @param uri Endpoint URL, or path.
|
|
3273
|
+
* @example
|
|
3274
|
+
* const api = graphql.link('https://api.site.com/graphql)
|
|
3275
|
+
* api.query('GetUser', resolver)
|
|
3276
|
+
* @see {@link https://mswjs.io/docs/api/graphql/link `graphql.link()`}
|
|
3277
|
+
*/
|
|
3234
3278
|
function createGraphQLLink(uri) {
|
|
3235
3279
|
return {
|
|
3236
3280
|
operation: createGraphQLOperationHandler(uri),
|
package/lib/esm/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { c as createCommonjsModule, s as status, a as set, d as delay, f as fetch, l as lib$1,
|
|
1
|
+
import { c as createCommonjsModule, s as status, a as set, d as delay, f as fetch, l as lib$1, i as isNodeProcess } from './fetch-deps.js';
|
|
2
2
|
import { p as parse_1 } from './xml-deps.js';
|
|
3
|
-
import './errors-deps.js';
|
|
3
|
+
import { m as mergeRight } from './errors-deps.js';
|
|
4
4
|
export { i as context } from './index-deps.js';
|
|
5
5
|
import { g as getPublicUrlFromRequest, i as isStringEqual } from './rest-deps.js';
|
|
6
6
|
export { R as RESTMethods, r as rest, a as restContext } from './rest-deps.js';
|
|
7
|
-
import { p as parseBody } from './
|
|
8
|
-
export { m as matchRequestUrl } from './
|
|
7
|
+
import { p as parseBody } from './getCallFrame-deps.js';
|
|
8
|
+
export { m as matchRequestUrl } from './getCallFrame-deps.js';
|
|
9
9
|
export { graphql, graphqlContext } from './graphql.js';
|
|
10
10
|
|
|
11
11
|
/*! *****************************************************************************
|
|
@@ -190,8 +190,14 @@ const defaultContext = {
|
|
|
190
190
|
* Composes a given list of functions into a new function that
|
|
191
191
|
* executes from right to left.
|
|
192
192
|
*/
|
|
193
|
-
function compose(...
|
|
194
|
-
return
|
|
193
|
+
function compose(...fns) {
|
|
194
|
+
return (...args) => {
|
|
195
|
+
return fns.reduceRight((leftFn, rightFn) => {
|
|
196
|
+
return leftFn instanceof Promise
|
|
197
|
+
? Promise.resolve(leftFn).then(rightFn)
|
|
198
|
+
: rightFn(leftFn);
|
|
199
|
+
}, args[0]);
|
|
200
|
+
};
|
|
195
201
|
}
|
|
196
202
|
|
|
197
203
|
class NetworkError extends Error {
|
|
@@ -201,17 +207,6 @@ class NetworkError extends Error {
|
|
|
201
207
|
}
|
|
202
208
|
}
|
|
203
209
|
|
|
204
|
-
/**
|
|
205
|
-
* Internal response transformer to ensure response JSON body
|
|
206
|
-
* is always stringified.
|
|
207
|
-
*/
|
|
208
|
-
const stringifyJsonBody = (res) => {
|
|
209
|
-
var _a, _b;
|
|
210
|
-
if (res.body && ((_b = (_a = res.headers) === null || _a === void 0 ? void 0 : _a.get('content-type')) === null || _b === void 0 ? void 0 : _b.endsWith('json'))) {
|
|
211
|
-
res.body = JSON.stringify(res.body);
|
|
212
|
-
}
|
|
213
|
-
return res;
|
|
214
|
-
};
|
|
215
210
|
const defaultResponse = {
|
|
216
211
|
status: 200,
|
|
217
212
|
statusText: 'OK',
|
|
@@ -219,11 +214,9 @@ const defaultResponse = {
|
|
|
219
214
|
delay: 0,
|
|
220
215
|
once: false,
|
|
221
216
|
};
|
|
222
|
-
const defaultResponseTransformers = [
|
|
223
|
-
stringifyJsonBody,
|
|
224
|
-
];
|
|
217
|
+
const defaultResponseTransformers = [];
|
|
225
218
|
function createResponseComposition(responseOverrides, defaultTransformers = defaultResponseTransformers) {
|
|
226
|
-
return (...transformers) => {
|
|
219
|
+
return (...transformers) => __awaiter(this, void 0, void 0, function* () {
|
|
227
220
|
const initialResponse = Object.assign({}, defaultResponse, {
|
|
228
221
|
headers: new lib$1.Headers({
|
|
229
222
|
'x-powered-by': 'msw',
|
|
@@ -237,7 +230,7 @@ function createResponseComposition(responseOverrides, defaultTransformers = defa
|
|
|
237
230
|
? compose(...resolvedTransformers)(initialResponse)
|
|
238
231
|
: initialResponse;
|
|
239
232
|
return resolvedResponse;
|
|
240
|
-
};
|
|
233
|
+
});
|
|
241
234
|
}
|
|
242
235
|
const response = Object.assign(createResponseComposition(), {
|
|
243
236
|
once: createResponseComposition({ once: true }),
|
|
@@ -625,6 +618,12 @@ function resetHandlers(initialHandlers, ...nextHandlers) {
|
|
|
625
618
|
// Declare the list of event handlers on the module's scope
|
|
626
619
|
// so it persists between Fash refreshes of the application's code.
|
|
627
620
|
let listeners = [];
|
|
621
|
+
/**
|
|
622
|
+
* Creates a new mock Service Worker registration
|
|
623
|
+
* with the given request handlers.
|
|
624
|
+
* @param {RequestHandler[]} requestHandlers List of request handlers
|
|
625
|
+
* @see {@link https://mswjs.io/docs/api/setup-worker `setupWorker`}
|
|
626
|
+
*/
|
|
628
627
|
function setupWorker(...requestHandlers) {
|
|
629
628
|
requestHandlers.forEach((handler) => {
|
|
630
629
|
if (Array.isArray(handler))
|
|
@@ -685,7 +684,19 @@ function setupWorker(...requestHandlers) {
|
|
|
685
684
|
resetHandlers(...nextHandlers) {
|
|
686
685
|
context.requestHandlers = resetHandlers(requestHandlers, ...nextHandlers);
|
|
687
686
|
},
|
|
687
|
+
printHandlers() {
|
|
688
|
+
context.requestHandlers.forEach((handler) => {
|
|
689
|
+
const meta = handler.getMetaInfo();
|
|
690
|
+
console.groupCollapsed(meta.header);
|
|
691
|
+
console.log(`Declaration: ${meta.callFrame}`);
|
|
692
|
+
console.log('Resolver: %s', handler.resolver);
|
|
693
|
+
if (['rest'].includes(meta.type)) {
|
|
694
|
+
console.log('Match:', `https://mswjs.io/repl?path=${meta.mask}`);
|
|
695
|
+
}
|
|
696
|
+
console.groupEnd();
|
|
697
|
+
});
|
|
698
|
+
},
|
|
688
699
|
};
|
|
689
700
|
}
|
|
690
701
|
|
|
691
|
-
export { createResponseComposition, defaultContext, defaultResponse, response, setupWorker };
|
|
702
|
+
export { compose, createResponseComposition, defaultContext, defaultResponse, response, setupWorker };
|
package/lib/esm/rest-deps.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { c as createCommonjsModule, b as commonjsGlobal, a as set, s as status, j as json, d as delay, f as fetch } from './fetch-deps.js';
|
|
2
2
|
import { c as cookie, b as body, t as text, x as xml } from './xml-deps.js';
|
|
3
|
-
import { g as getUrlByMask, m as matchRequestUrl,
|
|
3
|
+
import { g as getUrlByMask, a as getCallFrame, m as matchRequestUrl, b as prepareRequest, c as prepareResponse, d as getTimestamp, e as getStatusCodeColor } from './getCallFrame-deps.js';
|
|
4
4
|
|
|
5
5
|
var punycode = createCommonjsModule(function (module, exports) {
|
|
6
6
|
(function(root) {
|
|
@@ -1388,6 +1388,7 @@ const restContext = {
|
|
|
1388
1388
|
const createRestHandler = (method) => {
|
|
1389
1389
|
return (mask, resolver) => {
|
|
1390
1390
|
const resolvedMask = getUrlByMask(mask);
|
|
1391
|
+
const callFrame = getCallFrame();
|
|
1391
1392
|
return {
|
|
1392
1393
|
parse(req) {
|
|
1393
1394
|
// Match the request during parsing to prevent matching it twice
|
|
@@ -1438,16 +1439,85 @@ ${queryParams
|
|
|
1438
1439
|
console.log('Response', loggedResponse);
|
|
1439
1440
|
console.groupEnd();
|
|
1440
1441
|
},
|
|
1442
|
+
getMetaInfo() {
|
|
1443
|
+
return {
|
|
1444
|
+
type: 'rest',
|
|
1445
|
+
header: `[rest] ${method} ${mask.toString()}`,
|
|
1446
|
+
mask,
|
|
1447
|
+
callFrame,
|
|
1448
|
+
};
|
|
1449
|
+
},
|
|
1441
1450
|
};
|
|
1442
1451
|
};
|
|
1443
1452
|
};
|
|
1444
1453
|
const rest = {
|
|
1454
|
+
/**
|
|
1455
|
+
* Captures a HEAD request by a given path.
|
|
1456
|
+
* @example
|
|
1457
|
+
* rest.head('/numbers', (req, res, ctx) => {
|
|
1458
|
+
* return res(ctx.status(302))
|
|
1459
|
+
* })
|
|
1460
|
+
* @see {@link https://mswjs.io/docs/api/rest `rest`}
|
|
1461
|
+
*/
|
|
1445
1462
|
head: createRestHandler(RESTMethods.HEAD),
|
|
1463
|
+
/**
|
|
1464
|
+
* Captures a GET request by a given path.
|
|
1465
|
+
* @example
|
|
1466
|
+
* rest.get('/numbers', (req, res, ctx) => {
|
|
1467
|
+
* return res(ctx.json([1, 2, 3]))
|
|
1468
|
+
* })
|
|
1469
|
+
* @see {@link https://mswjs.io/docs/api/rest `rest`}
|
|
1470
|
+
*/
|
|
1446
1471
|
get: createRestHandler(RESTMethods.GET),
|
|
1472
|
+
/**
|
|
1473
|
+
* Captures a POST request by a given path.
|
|
1474
|
+
* @example
|
|
1475
|
+
* rest.post('/numbers', (req, res, ctx) => {
|
|
1476
|
+
* return res(ctx.text('success'))
|
|
1477
|
+
* })
|
|
1478
|
+
* @see {@link https://mswjs.io/docs/api/rest `rest`}
|
|
1479
|
+
*/
|
|
1447
1480
|
post: createRestHandler(RESTMethods.POST),
|
|
1481
|
+
/**
|
|
1482
|
+
* Captures a PUT request by a given path.
|
|
1483
|
+
* @example
|
|
1484
|
+
* rest.put('/numbers', (req, res, ctx) => {
|
|
1485
|
+
* const { numbers } = req.body
|
|
1486
|
+
* return res(ctx.json(numbers))
|
|
1487
|
+
* })
|
|
1488
|
+
* @see {@link https://mswjs.io/docs/api/rest `rest`}
|
|
1489
|
+
*/
|
|
1448
1490
|
put: createRestHandler(RESTMethods.PUT),
|
|
1491
|
+
/**
|
|
1492
|
+
* Captures a DELETE request by a given path.
|
|
1493
|
+
* @example
|
|
1494
|
+
* rest.delete('/numbers', (req, res, ctx) => {
|
|
1495
|
+
* const index = req.url.searchParams.get('index')
|
|
1496
|
+
* prevNumbers.splice(index, 1)
|
|
1497
|
+
* return res(ctx.json(nextNumbers))
|
|
1498
|
+
* })
|
|
1499
|
+
* @see {@link https://mswjs.io/docs/api/rest `rest`}
|
|
1500
|
+
*/
|
|
1449
1501
|
delete: createRestHandler(RESTMethods.DELETE),
|
|
1502
|
+
/**
|
|
1503
|
+
* Captures a PATCH request by a given path.
|
|
1504
|
+
* @example
|
|
1505
|
+
* rest.patch('/numbers', (req, res, ctx) => {
|
|
1506
|
+
* const { numbers } = req.body
|
|
1507
|
+
* const nextNumbers = prevNumbers.concat(number)
|
|
1508
|
+
* return res(ctx.json(nextNumbers))
|
|
1509
|
+
* })
|
|
1510
|
+
* @see {@link https://mswjs.io/docs/api/rest `rest`}
|
|
1511
|
+
*/
|
|
1450
1512
|
patch: createRestHandler(RESTMethods.PATCH),
|
|
1513
|
+
/**
|
|
1514
|
+
* Captures an OPTIONS request by a given path.
|
|
1515
|
+
* @example
|
|
1516
|
+
* rest.options('/numbers', (req, res, ctx) => {
|
|
1517
|
+
* return res(ctx.set('Allow', 'GET,HEAD,POST'))
|
|
1518
|
+
* })
|
|
1519
|
+
* @see {@link https://mswjs.io/docs/api/rest `rest`}
|
|
1520
|
+
*/
|
|
1451
1521
|
options: createRestHandler(RESTMethods.OPTIONS),
|
|
1452
1522
|
};
|
|
1453
1523
|
|
package/lib/esm/rest.js
CHANGED
package/lib/esm/xml-deps.js
CHANGED
|
@@ -200,9 +200,8 @@ function tryDecode(str, decode) {
|
|
|
200
200
|
}
|
|
201
201
|
|
|
202
202
|
/**
|
|
203
|
-
* Sets a given cookie on the response.
|
|
204
|
-
* @example
|
|
205
|
-
* res(cookie('name', 'value'))
|
|
203
|
+
* Sets a given cookie on the mocked response.
|
|
204
|
+
* @example res(ctx.cookie('name', 'value'))
|
|
206
205
|
*/
|
|
207
206
|
const cookie = (name, value, options) => {
|
|
208
207
|
return (res) => {
|
|
@@ -216,9 +215,11 @@ const cookie = (name, value, options) => {
|
|
|
216
215
|
};
|
|
217
216
|
|
|
218
217
|
/**
|
|
219
|
-
* Sets
|
|
218
|
+
* Sets a raw response body. Does not append any `Content-Type` headers.
|
|
220
219
|
* @example
|
|
221
|
-
* res(body('
|
|
220
|
+
* res(ctx.body('Successful response'))
|
|
221
|
+
* res(ctx.body(JSON.stringify({ key: 'value' })))
|
|
222
|
+
* @see {@link https://mswjs.io/docs/api/context/body `ctx.body()`}
|
|
222
223
|
*/
|
|
223
224
|
const body = (value) => {
|
|
224
225
|
return (res) => {
|
|
@@ -228,9 +229,10 @@ const body = (value) => {
|
|
|
228
229
|
};
|
|
229
230
|
|
|
230
231
|
/**
|
|
231
|
-
* Sets a
|
|
232
|
-
*
|
|
233
|
-
* res(text('
|
|
232
|
+
* Sets a textual response body. Appends a `Content-Type: text/plain`
|
|
233
|
+
* header on the mocked response.
|
|
234
|
+
* @example res(ctx.text('Successful response'))
|
|
235
|
+
* @see {@link https://mswjs.io/docs/api/context/text `ctx.text()`}
|
|
234
236
|
*/
|
|
235
237
|
const text = (body) => {
|
|
236
238
|
return (res) => {
|
|
@@ -241,9 +243,11 @@ const text = (body) => {
|
|
|
241
243
|
};
|
|
242
244
|
|
|
243
245
|
/**
|
|
244
|
-
* Sets
|
|
246
|
+
* Sets an XML response body. Appends a `Content-Type: text/xml` header
|
|
247
|
+
* on the mocked response.
|
|
245
248
|
* @example
|
|
246
|
-
* res(xml('<key>
|
|
249
|
+
* res(ctx.xml('<node key="value">Content</node>'))
|
|
250
|
+
* @see {@link https://mswjs.io/docs/api/context/xml `ctx.xml()`}
|
|
247
251
|
*/
|
|
248
252
|
const xml = (body) => {
|
|
249
253
|
return (res) => {
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { ResponseTransformer } from '../response';
|
|
2
2
|
/**
|
|
3
|
-
* Sets
|
|
3
|
+
* Sets a raw response body. Does not append any `Content-Type` headers.
|
|
4
4
|
* @example
|
|
5
|
-
* res(body('
|
|
5
|
+
* res(ctx.body('Successful response'))
|
|
6
|
+
* res(ctx.body(JSON.stringify({ key: 'value' })))
|
|
7
|
+
* @see {@link https://mswjs.io/docs/api/context/body `ctx.body()`}
|
|
6
8
|
*/
|
|
7
9
|
export declare const body: <BodyType extends string | Blob | ArrayBufferView | ArrayBuffer | ReadableStream<any> | FormData>(value: BodyType) => ResponseTransformer<BodyType>;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import * as cookieUtils from 'cookie';
|
|
2
2
|
import { ResponseTransformer } from '../response';
|
|
3
3
|
/**
|
|
4
|
-
* Sets a given cookie on the response.
|
|
5
|
-
* @example
|
|
6
|
-
* res(cookie('name', 'value'))
|
|
4
|
+
* Sets a given cookie on the mocked response.
|
|
5
|
+
* @example res(ctx.cookie('name', 'value'))
|
|
7
6
|
*/
|
|
8
7
|
export declare const cookie: (name: string, value: string, options?: cookieUtils.CookieSerializeOptions | undefined) => ResponseTransformer;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { ResponseTransformer } from '../response';
|
|
2
|
-
export declare type DataContext<T> = (payload: T) => ResponseTransformer;
|
|
3
2
|
/**
|
|
4
|
-
*
|
|
3
|
+
* Sets a given payload as a GraphQL response body.
|
|
4
|
+
* @example
|
|
5
|
+
* res(ctx.data({ user: { firstName: 'John' }}))
|
|
6
|
+
* @see {@link https://mswjs.io/docs/api/context/data `ctx.data()`}
|
|
5
7
|
*/
|
|
6
|
-
export declare const data:
|
|
8
|
+
export declare const data: <T extends Record<string, any>>(payload: T) => ResponseTransformer;
|
|
@@ -3,9 +3,10 @@ export declare const MIN_SERVER_RESPONSE_TIME = 100;
|
|
|
3
3
|
export declare const MAX_SERVER_RESPONSE_TIME = 400;
|
|
4
4
|
export declare const NODE_SERVER_RESPONSE_TIME = 5;
|
|
5
5
|
/**
|
|
6
|
-
* Delays the
|
|
6
|
+
* Delays the response by the given duration (ms).
|
|
7
7
|
* @example
|
|
8
|
-
* res(delay()) // realistic server response time
|
|
9
|
-
* res(delay(
|
|
8
|
+
* res(ctx.delay()) // realistic server response time
|
|
9
|
+
* res(ctx.delay(1200))
|
|
10
|
+
* @see {@link https://mswjs.io/docs/api/context/delay `ctx.delay()`}
|
|
10
11
|
*/
|
|
11
12
|
export declare const delay: (durationMs?: number | undefined) => ResponseTransformer;
|
|
@@ -2,7 +2,7 @@ import { GraphQLError } from 'graphql';
|
|
|
2
2
|
import { ResponseTransformer } from '../response';
|
|
3
3
|
/**
|
|
4
4
|
* Sets a given list of GraphQL errors on the mocked response.
|
|
5
|
+
* @example res(ctx.errors([{ message: 'Unauthorized' }]))
|
|
6
|
+
* @see {@link https://mswjs.io/docs/api/context/errors}
|
|
5
7
|
*/
|
|
6
|
-
export declare const errors: <ErrorsType extends Partial<GraphQLError>[] | null | undefined>(errorsList: ErrorsType) => ResponseTransformer<
|
|
7
|
-
errors: ErrorsType;
|
|
8
|
-
}>;
|
|
8
|
+
export declare const errors: <ErrorsType extends Partial<GraphQLError>[] | null | undefined>(errorsList: ErrorsType) => ResponseTransformer<string>;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { MockedRequest } from '../utils/handlers/requestHandler';
|
|
2
2
|
export declare const augmentRequestInit: (requestInit: RequestInit) => RequestInit;
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Performs a bypassed request inside a request handler.
|
|
5
|
+
* @example
|
|
6
|
+
* const originalResponse = await ctx.fetch(req)
|
|
7
|
+
* @see {@link https://mswjs.io/docs/api/context/fetch `ctx.fetch()`}
|
|
7
8
|
*/
|
|
8
9
|
export declare const fetch: (input: string | MockedRequest, requestInit?: RequestInit) => any;
|