fetch-mock
Advanced tools
Comparing version 8.0.1 to 8.0.3
const compileRoute = require('./compile-route'); | ||
const FetchMock = {}; | ||
const argsToRoute = args => { | ||
const [matcher, response, options = {}] = args; | ||
// Handle the variety of parameters accepted by mock (see README) | ||
if (matcher && response) { | ||
return Object.assign( | ||
{ | ||
matcher, | ||
response | ||
}, | ||
options | ||
); | ||
} else if (matcher && matcher.matcher) { | ||
return matcher; | ||
} else { | ||
throw new Error('fetch-mock: Invalid parameters passed to fetch-mock'); | ||
} | ||
}; | ||
FetchMock.mock = function(...args) { | ||
if (args.length) { | ||
const [matcher, response, options = {}] = args; | ||
let route; | ||
// Handle the variety of parameters accepted by mock (see README) | ||
if (matcher && response) { | ||
route = Object.assign( | ||
{ | ||
matcher, | ||
response | ||
}, | ||
options | ||
); | ||
} else if (matcher && matcher.matcher) { | ||
route = matcher; | ||
} else { | ||
throw new Error('fetch-mock: Invalid parameters passed to fetch-mock'); | ||
} | ||
this.addRoute(route); | ||
this.addRoute(argsToRoute(args)); | ||
} | ||
@@ -94,20 +94,14 @@ | ||
FetchMock.once = function(matcher, response, options = {}) { | ||
return this.mock( | ||
matcher, | ||
response, | ||
Object.assign({}, options, { repeat: 1 }) | ||
); | ||
const defineShorthand = (methodName, underlyingMethod, shorthandOptions) => { | ||
FetchMock[methodName] = function(...args) { | ||
return this[underlyingMethod]( | ||
Object.assign(argsToRoute(args), shorthandOptions) | ||
); | ||
}; | ||
}; | ||
defineShorthand('once', 'mock', { repeat: 1 }); | ||
['get', 'post', 'put', 'delete', 'head', 'patch'].forEach(method => { | ||
const extendOptions = options => | ||
Object.assign({}, options, { method: method.toUpperCase() }); | ||
FetchMock[method] = function(matcher, response, options = {}) { | ||
return this.mock(matcher, response, extendOptions(options)); | ||
}; | ||
FetchMock[`${method}Once`] = function(matcher, response, options = {}) { | ||
return this.once(matcher, response, extendOptions(options)); | ||
}; | ||
defineShorthand(method, 'mock', { method }); | ||
defineShorthand(`${method}Once`, 'once', { method }); | ||
}); | ||
@@ -114,0 +108,0 @@ |
@@ -7,2 +7,6 @@ 'use strict'; | ||
var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray'); | ||
var _slicedToArray3 = _interopRequireDefault(_slicedToArray2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -13,2 +17,23 @@ | ||
var argsToRoute = function argsToRoute(args) { | ||
var _args = (0, _slicedToArray3.default)(args, 3), | ||
matcher = _args[0], | ||
response = _args[1], | ||
_args$ = _args[2], | ||
options = _args$ === undefined ? {} : _args$; | ||
// Handle the variety of parameters accepted by mock (see README) | ||
if (matcher && response) { | ||
return (0, _assign2.default)({ | ||
matcher: matcher, | ||
response: response | ||
}, options); | ||
} else if (matcher && matcher.matcher) { | ||
return matcher; | ||
} else { | ||
throw new Error('fetch-mock: Invalid parameters passed to fetch-mock'); | ||
} | ||
}; | ||
FetchMock.mock = function () { | ||
@@ -20,22 +45,3 @@ for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
if (args.length) { | ||
var matcher = args[0], | ||
response = args[1], | ||
_args$ = args[2], | ||
options = _args$ === undefined ? {} : _args$; | ||
var route = void 0; | ||
// Handle the variety of parameters accepted by mock (see README) | ||
if (matcher && response) { | ||
route = (0, _assign2.default)({ | ||
matcher: matcher, | ||
response: response | ||
}, options); | ||
} else if (matcher && matcher.matcher) { | ||
route = matcher; | ||
} else { | ||
throw new Error('fetch-mock: Invalid parameters passed to fetch-mock'); | ||
} | ||
this.addRoute(route); | ||
this.addRoute(argsToRoute(args)); | ||
} | ||
@@ -105,23 +111,16 @@ | ||
FetchMock.once = function (matcher, response) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var defineShorthand = function defineShorthand(methodName, underlyingMethod, shorthandOptions) { | ||
FetchMock[methodName] = function () { | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return this.mock(matcher, response, (0, _assign2.default)({}, options, { repeat: 1 })); | ||
return this[underlyingMethod]((0, _assign2.default)(argsToRoute(args), shorthandOptions)); | ||
}; | ||
}; | ||
defineShorthand('once', 'mock', { repeat: 1 }); | ||
['get', 'post', 'put', 'delete', 'head', 'patch'].forEach(function (method) { | ||
var extendOptions = function extendOptions(options) { | ||
return (0, _assign2.default)({}, options, { method: method.toUpperCase() }); | ||
}; | ||
FetchMock[method] = function (matcher, response) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this.mock(matcher, response, extendOptions(options)); | ||
}; | ||
FetchMock[method + 'Once'] = function (matcher, response) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this.once(matcher, response, extendOptions(options)); | ||
}; | ||
defineShorthand(method, 'mock', { method: method }); | ||
defineShorthand(method + 'Once', 'once', { method: method }); | ||
}); | ||
@@ -128,0 +127,0 @@ |
{ | ||
"name": "fetch-mock", | ||
"type": "module", | ||
"version": "8.0.1", | ||
"version": "8.0.3", | ||
"description": "Mock http requests made using fetch (or isomorphic-fetch)", | ||
@@ -12,5 +12,8 @@ "main": "./cjs/server.js", | ||
"test": "make lint test", | ||
"prepublishOnly": "make build", | ||
"postinstall": "node scripts/support-fetch-mock.js" | ||
"prepublishOnly": "make build" | ||
}, | ||
"funding": { | ||
"type": "charity", | ||
"url": "https://www.justgiving.com/refugee-support-europe" | ||
}, | ||
"babel": { | ||
@@ -17,0 +20,0 @@ "presets": [ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
0
2612340
36
74553