msw 0.36.2 → 0.36.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -6
- package/lib/esm/RequestHandler-deps.js +24 -9
- package/lib/esm/fetch-deps.js +3 -1
- package/lib/esm/index.js +1 -11
- package/lib/esm/mockServiceWorker.js +1 -1
- package/lib/iife/index.js +3 -3
- package/lib/iife/mockServiceWorker.js +1 -1
- package/lib/types/handlers/RequestHandler.d.ts +1 -1
- package/lib/types/utils/handleRequest.d.ts +1 -1
- package/lib/types/utils/logging/prepareResponse.d.ts +1 -1
- package/lib/types/utils/matching/matchRequestUrl.d.ts +1 -1
- package/lib/types/utils/request/parseBody.d.ts +1 -1
- package/lib/types/utils/url/isAbsoluteUrl.d.ts +4 -0
- package/lib/umd/index.js +300 -140
- package/lib/umd/mockServiceWorker.js +1 -1
- package/native/lib/index.js +31 -25
- package/node/lib/index.js +31 -25
- package/package.json +15 -6
package/native/lib/index.js
CHANGED
|
@@ -362,14 +362,6 @@ function tryDecode(str, decode) {
|
|
|
362
362
|
}
|
|
363
363
|
}
|
|
364
364
|
|
|
365
|
-
function uuidv4() {
|
|
366
|
-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
367
|
-
const r = (Math.random() * 16) | 0;
|
|
368
|
-
const v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
369
|
-
return v.toString(16);
|
|
370
|
-
});
|
|
371
|
-
}
|
|
372
|
-
|
|
373
365
|
/**
|
|
374
366
|
* Parses a given value into a JSON.
|
|
375
367
|
* Does not throw an exception on an invalid JSON string.
|
|
@@ -985,11 +977,12 @@ function parseMultipartData(data, headers) {
|
|
|
985
977
|
* Parses a given request/response body based on the "Content-Type" header.
|
|
986
978
|
*/
|
|
987
979
|
function parseBody(body, headers) {
|
|
980
|
+
var _a;
|
|
988
981
|
// Return whatever falsey body value is given.
|
|
989
982
|
if (!body) {
|
|
990
983
|
return body;
|
|
991
984
|
}
|
|
992
|
-
const contentType = (headers === null || headers === void 0 ? void 0 : headers.get('content-type')) || '';
|
|
985
|
+
const contentType = ((_a = headers === null || headers === void 0 ? void 0 : headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || '';
|
|
993
986
|
// If the body has a Multipart Content-Type
|
|
994
987
|
// parse it into an object.
|
|
995
988
|
const hasMultipartContent = contentType.startsWith('multipart/form-data');
|
|
@@ -1048,10 +1041,8 @@ function setRequestCookies(request) {
|
|
|
1048
1041
|
* Converts a given isomorphic request to a `MockedRequest` instance.
|
|
1049
1042
|
*/
|
|
1050
1043
|
function parseIsomorphicRequest(request) {
|
|
1051
|
-
const requestId = uuidv4();
|
|
1052
|
-
request.headers.set('x-msw-request-id', requestId);
|
|
1053
1044
|
const mockedRequest = {
|
|
1054
|
-
id:
|
|
1045
|
+
id: request.id,
|
|
1055
1046
|
url: request.url,
|
|
1056
1047
|
method: request.method,
|
|
1057
1048
|
body: parseBody(request.body, request.headers),
|
|
@@ -1234,11 +1225,9 @@ var InvariantError = /** @class */ (function (_super) {
|
|
|
1234
1225
|
_this.name = 'Invariant Violation';
|
|
1235
1226
|
_this.message = format_1.format.apply(void 0, __spreadArray([message], positionals));
|
|
1236
1227
|
if (_this.stack) {
|
|
1237
|
-
var
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
.slice(STACK_FRAMES_TO_IGNORE)
|
|
1241
|
-
.join('\n');
|
|
1228
|
+
var nextStack = _this.stack.split('\n');
|
|
1229
|
+
nextStack.splice(1, STACK_FRAMES_TO_IGNORE);
|
|
1230
|
+
_this.stack = nextStack.join('\n');
|
|
1242
1231
|
}
|
|
1243
1232
|
return _this;
|
|
1244
1233
|
}
|
|
@@ -4942,7 +4931,9 @@ const createFetchRequestParameters = (input) => {
|
|
|
4942
4931
|
if (['GET', 'HEAD'].includes(method)) {
|
|
4943
4932
|
return requestParameters;
|
|
4944
4933
|
}
|
|
4945
|
-
if (typeof body === 'object' ||
|
|
4934
|
+
if (typeof body === 'object' ||
|
|
4935
|
+
typeof body === 'number' ||
|
|
4936
|
+
typeof body === 'boolean') {
|
|
4946
4937
|
requestParameters.body = JSON.stringify(body);
|
|
4947
4938
|
}
|
|
4948
4939
|
else {
|
|
@@ -5387,12 +5378,23 @@ function cleanUrl(path) {
|
|
|
5387
5378
|
return path.replace(REDUNDANT_CHARACTERS_EXP, '');
|
|
5388
5379
|
}
|
|
5389
5380
|
|
|
5381
|
+
/**
|
|
5382
|
+
* Determines if the given URL string is an absolute URL.
|
|
5383
|
+
*/
|
|
5384
|
+
function isAbsoluteUrl(url) {
|
|
5385
|
+
return /^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(url);
|
|
5386
|
+
}
|
|
5387
|
+
|
|
5390
5388
|
/**
|
|
5391
5389
|
* Returns an absolute URL based on the given path.
|
|
5392
5390
|
*/
|
|
5393
5391
|
function getAbsoluteUrl(path, baseUrl) {
|
|
5394
|
-
//
|
|
5395
|
-
if (
|
|
5392
|
+
// already absolute URL
|
|
5393
|
+
if (isAbsoluteUrl(path)) {
|
|
5394
|
+
return path;
|
|
5395
|
+
}
|
|
5396
|
+
// Ignore path with pattern start with *
|
|
5397
|
+
if (path.startsWith('*')) {
|
|
5396
5398
|
return path;
|
|
5397
5399
|
}
|
|
5398
5400
|
// Resolve a relative request URL against a given custom "baseUrl"
|
|
@@ -5440,12 +5442,17 @@ function coercePath(path) {
|
|
|
5440
5442
|
? `${parameterName}${wildcard}`
|
|
5441
5443
|
: `${parameterName}${expression}`;
|
|
5442
5444
|
})
|
|
5445
|
+
/**
|
|
5446
|
+
* Escape the port so that "path-to-regexp" can match
|
|
5447
|
+
* absolute URLs including port numbers.
|
|
5448
|
+
*/
|
|
5449
|
+
.replace(/([^\/])(:)(?=\d+)/, '$1\\$2')
|
|
5443
5450
|
/**
|
|
5444
5451
|
* Escape the protocol so that "path-to-regexp" could match
|
|
5445
5452
|
* absolute URL.
|
|
5446
5453
|
* @see https://github.com/pillarjs/path-to-regexp/issues/259
|
|
5447
5454
|
*/
|
|
5448
|
-
.replace(/^([^\/]+)(:)(?=\/\/)
|
|
5455
|
+
.replace(/^([^\/]+)(:)(?=\/\/)/, '$1\\$2'));
|
|
5449
5456
|
}
|
|
5450
5457
|
/**
|
|
5451
5458
|
* Returns the result of matching given request URL against a mask.
|
|
@@ -6060,15 +6067,14 @@ function createSetupServer(...interceptors$1) {
|
|
|
6060
6067
|
},
|
|
6061
6068
|
});
|
|
6062
6069
|
interceptor.on('response', (request, response) => {
|
|
6063
|
-
|
|
6064
|
-
if (!requestId) {
|
|
6070
|
+
if (!request.id) {
|
|
6065
6071
|
return;
|
|
6066
6072
|
}
|
|
6067
6073
|
if (response.headers.get('x-powered-by') === 'msw') {
|
|
6068
|
-
emitter.emit('response:mocked', response,
|
|
6074
|
+
emitter.emit('response:mocked', response, request.id);
|
|
6069
6075
|
}
|
|
6070
6076
|
else {
|
|
6071
|
-
emitter.emit('response:bypass', response,
|
|
6077
|
+
emitter.emit('response:bypass', response, request.id);
|
|
6072
6078
|
}
|
|
6073
6079
|
});
|
|
6074
6080
|
return {
|
package/node/lib/index.js
CHANGED
|
@@ -2248,14 +2248,6 @@ function tryDecode(str, decode) {
|
|
|
2248
2248
|
}
|
|
2249
2249
|
}
|
|
2250
2250
|
|
|
2251
|
-
function uuidv4() {
|
|
2252
|
-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
2253
|
-
const r = (Math.random() * 16) | 0;
|
|
2254
|
-
const v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
2255
|
-
return v.toString(16);
|
|
2256
|
-
});
|
|
2257
|
-
}
|
|
2258
|
-
|
|
2259
2251
|
/**
|
|
2260
2252
|
* Parses a given value into a JSON.
|
|
2261
2253
|
* Does not throw an exception on an invalid JSON string.
|
|
@@ -2871,11 +2863,12 @@ function parseMultipartData(data, headers) {
|
|
|
2871
2863
|
* Parses a given request/response body based on the "Content-Type" header.
|
|
2872
2864
|
*/
|
|
2873
2865
|
function parseBody(body, headers) {
|
|
2866
|
+
var _a;
|
|
2874
2867
|
// Return whatever falsey body value is given.
|
|
2875
2868
|
if (!body) {
|
|
2876
2869
|
return body;
|
|
2877
2870
|
}
|
|
2878
|
-
const contentType = (headers === null || headers === void 0 ? void 0 : headers.get('content-type')) || '';
|
|
2871
|
+
const contentType = ((_a = headers === null || headers === void 0 ? void 0 : headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || '';
|
|
2879
2872
|
// If the body has a Multipart Content-Type
|
|
2880
2873
|
// parse it into an object.
|
|
2881
2874
|
const hasMultipartContent = contentType.startsWith('multipart/form-data');
|
|
@@ -2934,10 +2927,8 @@ function setRequestCookies(request) {
|
|
|
2934
2927
|
* Converts a given isomorphic request to a `MockedRequest` instance.
|
|
2935
2928
|
*/
|
|
2936
2929
|
function parseIsomorphicRequest(request) {
|
|
2937
|
-
const requestId = uuidv4();
|
|
2938
|
-
request.headers.set('x-msw-request-id', requestId);
|
|
2939
2930
|
const mockedRequest = {
|
|
2940
|
-
id:
|
|
2931
|
+
id: request.id,
|
|
2941
2932
|
url: request.url,
|
|
2942
2933
|
method: request.method,
|
|
2943
2934
|
body: parseBody(request.body, request.headers),
|
|
@@ -3120,11 +3111,9 @@ var InvariantError = /** @class */ (function (_super) {
|
|
|
3120
3111
|
_this.name = 'Invariant Violation';
|
|
3121
3112
|
_this.message = format_1.format.apply(void 0, __spreadArray([message], positionals));
|
|
3122
3113
|
if (_this.stack) {
|
|
3123
|
-
var
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
.slice(STACK_FRAMES_TO_IGNORE)
|
|
3127
|
-
.join('\n');
|
|
3114
|
+
var nextStack = _this.stack.split('\n');
|
|
3115
|
+
nextStack.splice(1, STACK_FRAMES_TO_IGNORE);
|
|
3116
|
+
_this.stack = nextStack.join('\n');
|
|
3128
3117
|
}
|
|
3129
3118
|
return _this;
|
|
3130
3119
|
}
|
|
@@ -6828,7 +6817,9 @@ const createFetchRequestParameters = (input) => {
|
|
|
6828
6817
|
if (['GET', 'HEAD'].includes(method)) {
|
|
6829
6818
|
return requestParameters;
|
|
6830
6819
|
}
|
|
6831
|
-
if (typeof body === 'object' ||
|
|
6820
|
+
if (typeof body === 'object' ||
|
|
6821
|
+
typeof body === 'number' ||
|
|
6822
|
+
typeof body === 'boolean') {
|
|
6832
6823
|
requestParameters.body = JSON.stringify(body);
|
|
6833
6824
|
}
|
|
6834
6825
|
else {
|
|
@@ -7273,12 +7264,23 @@ function cleanUrl(path) {
|
|
|
7273
7264
|
return path.replace(REDUNDANT_CHARACTERS_EXP, '');
|
|
7274
7265
|
}
|
|
7275
7266
|
|
|
7267
|
+
/**
|
|
7268
|
+
* Determines if the given URL string is an absolute URL.
|
|
7269
|
+
*/
|
|
7270
|
+
function isAbsoluteUrl(url) {
|
|
7271
|
+
return /^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(url);
|
|
7272
|
+
}
|
|
7273
|
+
|
|
7276
7274
|
/**
|
|
7277
7275
|
* Returns an absolute URL based on the given path.
|
|
7278
7276
|
*/
|
|
7279
7277
|
function getAbsoluteUrl(path, baseUrl) {
|
|
7280
|
-
//
|
|
7281
|
-
if (
|
|
7278
|
+
// already absolute URL
|
|
7279
|
+
if (isAbsoluteUrl(path)) {
|
|
7280
|
+
return path;
|
|
7281
|
+
}
|
|
7282
|
+
// Ignore path with pattern start with *
|
|
7283
|
+
if (path.startsWith('*')) {
|
|
7282
7284
|
return path;
|
|
7283
7285
|
}
|
|
7284
7286
|
// Resolve a relative request URL against a given custom "baseUrl"
|
|
@@ -7326,12 +7328,17 @@ function coercePath(path) {
|
|
|
7326
7328
|
? `${parameterName}${wildcard}`
|
|
7327
7329
|
: `${parameterName}${expression}`;
|
|
7328
7330
|
})
|
|
7331
|
+
/**
|
|
7332
|
+
* Escape the port so that "path-to-regexp" can match
|
|
7333
|
+
* absolute URLs including port numbers.
|
|
7334
|
+
*/
|
|
7335
|
+
.replace(/([^\/])(:)(?=\d+)/, '$1\\$2')
|
|
7329
7336
|
/**
|
|
7330
7337
|
* Escape the protocol so that "path-to-regexp" could match
|
|
7331
7338
|
* absolute URL.
|
|
7332
7339
|
* @see https://github.com/pillarjs/path-to-regexp/issues/259
|
|
7333
7340
|
*/
|
|
7334
|
-
.replace(/^([^\/]+)(:)(?=\/\/)
|
|
7341
|
+
.replace(/^([^\/]+)(:)(?=\/\/)/, '$1\\$2'));
|
|
7335
7342
|
}
|
|
7336
7343
|
/**
|
|
7337
7344
|
* Returns the result of matching given request URL against a mask.
|
|
@@ -7946,15 +7953,14 @@ function createSetupServer(...interceptors$1) {
|
|
|
7946
7953
|
},
|
|
7947
7954
|
});
|
|
7948
7955
|
interceptor.on('response', (request, response) => {
|
|
7949
|
-
|
|
7950
|
-
if (!requestId) {
|
|
7956
|
+
if (!request.id) {
|
|
7951
7957
|
return;
|
|
7952
7958
|
}
|
|
7953
7959
|
if (response.headers.get('x-powered-by') === 'msw') {
|
|
7954
|
-
emitter.emit('response:mocked', response,
|
|
7960
|
+
emitter.emit('response:mocked', response, request.id);
|
|
7955
7961
|
}
|
|
7956
7962
|
else {
|
|
7957
|
-
emitter.emit('response:bypass', response,
|
|
7963
|
+
emitter.emit('response:bypass', response, request.id);
|
|
7958
7964
|
}
|
|
7959
7965
|
});
|
|
7960
7966
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "msw",
|
|
3
|
-
"version": "0.36.
|
|
3
|
+
"version": "0.36.7",
|
|
4
4
|
"description": "Seamless REST/GraphQL API mocking library for browser and Node.js.",
|
|
5
5
|
"main": "lib/umd/index.js",
|
|
6
6
|
"module": "lib/esm/index.js",
|
|
@@ -18,9 +18,8 @@
|
|
|
18
18
|
"test:integration": "jest --config=test/jest.config.js --maxWorkers=3",
|
|
19
19
|
"test:smoke": "config/scripts/smoke.sh",
|
|
20
20
|
"test:ts": "tsc -p test/typings/tsconfig.json",
|
|
21
|
-
"
|
|
21
|
+
"prepare": "yarn simple-git-hooks init",
|
|
22
22
|
"prepublishOnly": "yarn lint && yarn test:unit && yarn build && yarn test:integration",
|
|
23
|
-
"prepare": "husky install",
|
|
24
23
|
"postinstall": "node -e \"try{require('./config/scripts/postinstall')}catch(e){}\""
|
|
25
24
|
},
|
|
26
25
|
"lint-staged": {
|
|
@@ -64,7 +63,7 @@
|
|
|
64
63
|
],
|
|
65
64
|
"sideEffects": false,
|
|
66
65
|
"dependencies": {
|
|
67
|
-
"@mswjs/cookies": "^0.1.
|
|
66
|
+
"@mswjs/cookies": "^0.1.7",
|
|
68
67
|
"@mswjs/interceptors": "^0.12.7",
|
|
69
68
|
"@open-draft/until": "^1.0.3",
|
|
70
69
|
"@types/cookie": "^0.4.1",
|
|
@@ -78,7 +77,7 @@
|
|
|
78
77
|
"inquirer": "^8.2.0",
|
|
79
78
|
"is-node-process": "^1.0.1",
|
|
80
79
|
"js-levenshtein": "^1.1.6",
|
|
81
|
-
"node-fetch": "^2.6.
|
|
80
|
+
"node-fetch": "^2.6.7",
|
|
82
81
|
"path-to-regexp": "^6.2.0",
|
|
83
82
|
"statuses": "^2.0.0",
|
|
84
83
|
"strict-event-emitter": "^0.2.0",
|
|
@@ -88,6 +87,8 @@
|
|
|
88
87
|
"devDependencies": {
|
|
89
88
|
"@babel/core": "^7.16.0",
|
|
90
89
|
"@babel/preset-env": "^7.16.4",
|
|
90
|
+
"@commitlint/cli": "^16.0.2",
|
|
91
|
+
"@commitlint/config-conventional": "^16.0.0",
|
|
91
92
|
"@open-draft/test-server": "^0.2.3",
|
|
92
93
|
"@rollup/plugin-commonjs": "^19.0.0",
|
|
93
94
|
"@rollup/plugin-inject": "^4.0.3",
|
|
@@ -104,17 +105,19 @@
|
|
|
104
105
|
"@typescript-eslint/parser": "^4.28.3",
|
|
105
106
|
"babel-loader": "^8.2.3",
|
|
106
107
|
"babel-minify": "^0.5.1",
|
|
108
|
+
"commitizen": "^4.2.4",
|
|
107
109
|
"cross-env": "^7.0.3",
|
|
108
110
|
"cross-fetch": "^3.1.4",
|
|
111
|
+
"cz-conventional-changelog": "3.3.0",
|
|
109
112
|
"eslint": "^7.30.0",
|
|
110
113
|
"eslint-config-prettier": "^8.3.0",
|
|
111
114
|
"eslint-plugin-prettier": "^3.4.0",
|
|
112
115
|
"fs-extra": "^10.0.0",
|
|
113
116
|
"fs-teardown": "^0.3.0",
|
|
114
|
-
"husky": "^5.1.1",
|
|
115
117
|
"jest": "26",
|
|
116
118
|
"json-bigint": "^1.0.0",
|
|
117
119
|
"lint-staged": "^11.0.1",
|
|
120
|
+
"outvariant": "^1.2.1",
|
|
118
121
|
"page-with": "^0.5.0",
|
|
119
122
|
"prettier": "^2.3.2",
|
|
120
123
|
"regenerator-runtime": "^0.13.9",
|
|
@@ -122,6 +125,7 @@
|
|
|
122
125
|
"rollup": "^2.60.2",
|
|
123
126
|
"rollup-plugin-terser": "^7.0.2",
|
|
124
127
|
"rollup-plugin-typescript2": "^0.30.0",
|
|
128
|
+
"simple-git-hooks": "^2.7.0",
|
|
125
129
|
"ts-jest": "26",
|
|
126
130
|
"ts-loader": "^9.2.6",
|
|
127
131
|
"ts-node": "^10.1.0",
|
|
@@ -132,5 +136,10 @@
|
|
|
132
136
|
},
|
|
133
137
|
"resolutions": {
|
|
134
138
|
"chokidar": "3.4.1"
|
|
139
|
+
},
|
|
140
|
+
"config": {
|
|
141
|
+
"commitizen": {
|
|
142
|
+
"path": "./node_modules/cz-conventional-changelog"
|
|
143
|
+
}
|
|
135
144
|
}
|
|
136
145
|
}
|