msw-inspector
Advanced tools
Comparing version 3.1.0 to 3.2.0
@@ -18,6 +18,6 @@ import type { LifeCycleEventsMap } from 'msw'; | ||
* | ||
* @param {string} path Path of a network request (`/path`) | ||
* @param {string | RegExp} url Url of a network request (`http://origin.com/path`) | ||
* @return {*} {Promise<FunctionMock>} | ||
*/ | ||
getRequests(path: string, { debug }?: { | ||
getRequests(url: string | RegExp, { debug }?: { | ||
debug?: boolean | undefined; | ||
@@ -24,0 +24,0 @@ }): Promise<FunctionMock>; |
@@ -22,2 +22,37 @@ "use strict"; | ||
} | ||
function matchStringUrl(url) { | ||
let pathURL; | ||
try { | ||
pathURL = new URL(url); | ||
} | ||
catch (error) { | ||
throw new Error((0, makeErrorMessage_1.makeErrorMessage)({ | ||
message: `Provided url is invalid: ${url}`, | ||
interceptedRequests, | ||
})); | ||
} | ||
const pathRegex = (0, path_to_regexp_1.pathToRegexp)(pathURL.pathname); | ||
// Look for matching intercepted requests | ||
const matches = []; | ||
interceptedRequests.forEach((requests, requestHref) => { | ||
const loggedRequestURL = new URL(requestHref); | ||
// Test origins | ||
if (pathURL.origin !== loggedRequestURL.origin) { | ||
return; | ||
} | ||
// Test paths | ||
if (pathRegex.test(loggedRequestURL.pathname)) { | ||
matches.push(...requests); | ||
} | ||
}); | ||
return matches; | ||
} | ||
function matchRegexUrl(url) { | ||
const matches = []; | ||
interceptedRequests.forEach((requests) => { | ||
const matchingRequests = requests.filter((request) => url.test(request.url)); | ||
matches.push(...matchingRequests); | ||
}); | ||
return matches; | ||
} | ||
return { | ||
@@ -28,30 +63,7 @@ /** | ||
* | ||
* @param {string} path Path of a network request (`/path`) | ||
* @param {string | RegExp} url Url of a network request (`http://origin.com/path`) | ||
* @return {*} {Promise<FunctionMock>} | ||
*/ | ||
async getRequests(path, { debug = true } = {}) { | ||
let pathURL; | ||
try { | ||
pathURL = new URL(path); | ||
} | ||
catch (error) { | ||
throw new Error((0, makeErrorMessage_1.makeErrorMessage)({ | ||
message: `Provided path is invalid: ${path}`, | ||
interceptedRequests, | ||
})); | ||
} | ||
const pathRegex = (0, path_to_regexp_1.pathToRegexp)(pathURL.pathname); | ||
// Look for matching intercepted requests | ||
const matches = []; | ||
interceptedRequests.forEach((requests, requestHref) => { | ||
const loggedRequestURL = new URL(requestHref); | ||
// Test origins | ||
if (pathURL.origin !== loggedRequestURL.origin) { | ||
return; | ||
} | ||
// Test paths | ||
if (pathRegex.test(loggedRequestURL.pathname)) { | ||
matches.push(...requests); | ||
} | ||
}); | ||
async getRequests(url, { debug = true } = {}) { | ||
const matches = url instanceof RegExp ? matchRegexUrl(url) : matchStringUrl(url); | ||
const functionMock = mockFactory(); | ||
@@ -61,3 +73,3 @@ if (matches.length === 0) { | ||
throw new Error((0, makeErrorMessage_1.makeErrorMessage)({ | ||
message: `Cannot find a matching requests for path: ${path}`, | ||
message: `Cannot find a matching requests for url: ${url}`, | ||
interceptedRequests, | ||
@@ -64,0 +76,0 @@ })); |
{ | ||
"name": "msw-inspector", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Inspect requests intercepted by MSW", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -130,2 +130,3 @@ # MSW inspector | ||
- [path-to-regexp](https://www.npmjs.com/package/path-to-regexp) matching pattern | ||
- regular expression (also matches against query string) | ||
@@ -136,4 +137,7 @@ ```ts | ||
// Url matching patter | ||
// Url matching pattern | ||
await mswInspector.getRequests('http://my.url/path/:param'); | ||
// Full url regular expression match | ||
await mswInspector.getRequests(/.+\?query=.+/); | ||
``` | ||
@@ -140,0 +144,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17671
203
200