Skip to content

Commit

Permalink
use matchRequestProps helper in prevent-xhr & prevent-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Oct 31, 2022
1 parent 53b1904 commit e50b102
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 42 deletions.
30 changes: 9 additions & 21 deletions src/scriptlets/prevent-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import {
hit,
getFetchData,
objectToString,
parseMatchProps,
validateParsedData,
getMatchPropsData,
noopPromiseResolve,
getWildcardSymbol,
matchRequestProps,
// following helpers should be imported and injected
// because they are used by helpers above
toRegExp,
Expand All @@ -16,6 +14,9 @@ import {
getRequestData,
getObjectEntries,
getObjectFromEntries,
parseMatchProps,
validateParsedData,
getMatchPropsData,
} from '../helpers/index';

/* eslint-disable max-len */
Expand Down Expand Up @@ -123,21 +124,7 @@ export function preventFetch(source, propsToMatch, responseBody = 'emptyObj', re
// prevent all fetch calls
shouldPrevent = true;
} else {
const parsedData = parseMatchProps(propsToMatch);
if (!validateParsedData(parsedData)) {
// eslint-disable-next-line no-console
console.log(`Invalid parameter: ${propsToMatch}`);
shouldPrevent = false;
} else {
const matchData = getMatchPropsData(parsedData);
// prevent only if all props match
shouldPrevent = Object.keys(matchData)
.every((matchKey) => {
const matchValue = matchData[matchKey];
return Object.prototype.hasOwnProperty.call(fetchData, matchKey)
&& matchValue.test(fetchData[matchKey]);
});
}
shouldPrevent = matchRequestProps(propsToMatch);
}

if (shouldPrevent) {
Expand Down Expand Up @@ -167,16 +154,17 @@ preventFetch.injections = [
hit,
getFetchData,
objectToString,
parseMatchProps,
validateParsedData,
getMatchPropsData,
noopPromiseResolve,
getWildcardSymbol,
matchRequestProps,
toRegExp,
isValidStrPattern,
escapeRegExp,
isEmptyObject,
getRequestData,
getObjectEntries,
getObjectFromEntries,
parseMatchProps,
validateParsedData,
getMatchPropsData,
];
30 changes: 9 additions & 21 deletions src/scriptlets/prevent-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import {
hit,
objectToString,
getWildcardSymbol,
parseMatchProps,
validateParsedData,
getMatchPropsData,
getRandomIntInclusive,
getRandomStrByLength,
generateRandomResponse,
matchRequestProps,
// following helpers should be imported and injected
// because they are used by helpers above
toRegExp,
Expand All @@ -18,6 +16,9 @@ import {
getNumberFromString,
nativeIsFinite,
nativeIsNaN,
parseMatchProps,
validateParsedData,
getMatchPropsData,
} from '../helpers/index';

/* eslint-disable max-len */
Expand Down Expand Up @@ -113,21 +114,7 @@ export function preventXHR(source, propsToMatch, customResponseText) {
// Prevent all fetch calls
shouldPrevent = true;
} else {
const parsedData = parseMatchProps(propsToMatch);
if (!validateParsedData(parsedData)) {
// eslint-disable-next-line no-console
console.log(`Invalid parameter: ${propsToMatch}`);
shouldPrevent = false;
} else {
const matchData = getMatchPropsData(parsedData);
// prevent only if all props match
shouldPrevent = Object.keys(matchData)
.every((matchKey) => {
const matchValue = matchData[matchKey];
return Object.prototype.hasOwnProperty.call(xhrData, matchKey)
&& matchValue.test(xhrData[matchKey]);
});
}
shouldPrevent = matchRequestProps(propsToMatch);
}

return Reflect.apply(target, thisArg, args);
Expand Down Expand Up @@ -205,9 +192,7 @@ preventXHR.injections = [
hit,
objectToString,
getWildcardSymbol,
parseMatchProps,
validateParsedData,
getMatchPropsData,
matchRequestProps,
getRandomIntInclusive,
getRandomStrByLength,
generateRandomResponse,
Expand All @@ -219,4 +204,7 @@ preventXHR.injections = [
getNumberFromString,
nativeIsFinite,
nativeIsNaN,
parseMatchProps,
validateParsedData,
getMatchPropsData,
];

0 comments on commit e50b102

Please sign in to comment.