fetch-mock
Advanced tools
Comparing version 9.2.2 to 9.3.0
@@ -1,14 +0,3 @@ | ||
const { getDebug } = require('./debug'); | ||
const generateMatcher = require('./generate-matcher'); | ||
const { debug, setDebugNamespace, getDebug } = require('./debug'); | ||
const matcherProperties = [ | ||
'query', | ||
'method', | ||
'headers', | ||
'params', | ||
'body', | ||
'functionMatcher', | ||
'url' | ||
]; | ||
const isUrlMatcher = matcher => | ||
@@ -66,3 +55,3 @@ matcher instanceof RegExp || | ||
const validateRoute = route => { | ||
const validateRoute = function(route) { | ||
if (!('response' in route)) { | ||
@@ -72,3 +61,3 @@ throw new Error('fetch-mock: Each route must define a response'); | ||
if (!matcherProperties.some(matcherType => matcherType in route)) { | ||
if (!this._matchers.some(({ name }) => name in route)) { | ||
throw new Error( | ||
@@ -121,2 +110,21 @@ "fetch-mock: Each route must specify some criteria for matching calls to fetch. To match all calls use '*'" | ||
const generateMatcher = function(route) { | ||
setDebugNamespace('generateMatcher()'); | ||
debug('Compiling matcher for route'); | ||
const matchers = this._matchers | ||
.map( | ||
({ name, matcher, usesBody }) => | ||
route[name] && { matcher: matcher(route, this), usesBody } | ||
) | ||
.filter(matcher => !!matcher); | ||
route.usesBody = matchers.some(({ usesBody }) => usesBody); | ||
debug('Compiled matcher for route'); | ||
setDebugNamespace(); | ||
return (url, options = {}, request) => | ||
matchers.every(({ matcher }) => matcher(url, options, request)); | ||
}; | ||
const compileRoute = function(args) { | ||
@@ -126,4 +134,4 @@ const debug = getDebug('compileRoute()'); | ||
const route = sanitizeRoute(argsToRoute(args)); | ||
validateRoute(route); | ||
route.matcher = generateMatcher(route, this); | ||
this.validateRoute(route); | ||
route.matcher = this.generateMatcher(route); | ||
limit(route); | ||
@@ -136,3 +144,5 @@ delayResponse(route); | ||
compileRoute, | ||
sanitizeRoute | ||
sanitizeRoute, | ||
generateMatcher, | ||
validateRoute | ||
}; |
@@ -84,3 +84,3 @@ const { debug, setDebugPhase, getDebug } = require('./debug'); | ||
if (request && this.routes.some(({ body }) => !!body)) { | ||
if (request && this.routes.some(({ usesBody }) => usesBody)) { | ||
debug( | ||
@@ -87,0 +87,0 @@ 'Need to wait for Body to be streamed before calling router: switching to async mode' |
@@ -5,4 +5,13 @@ const { debug } = require('./debug'); | ||
const inspecting = require('./inspecting'); | ||
const matchers = require('./matchers'); | ||
const compileRoute = require('./compile-route'); | ||
const FetchMock = Object.assign({}, fetchHandler, setUpAndTearDown, inspecting); | ||
const FetchMock = Object.assign( | ||
{}, | ||
fetchHandler, | ||
setUpAndTearDown, | ||
inspecting, | ||
compileRoute, | ||
matchers | ||
); | ||
@@ -21,2 +30,3 @@ FetchMock.config = { | ||
instance._uncompiledRoutes = (this._uncompiledRoutes || []).slice(); | ||
instance._matchers = this._matchers.slice(); | ||
instance.routes = instance._uncompiledRoutes.map(config => | ||
@@ -23,0 +33,0 @@ instance.compileRoute(config) |
const { setDebugPhase, setDebugNamespace, debug } = require('./debug'); | ||
const { normalizeUrl } = require('./request-utils'); | ||
const FetchMock = {}; | ||
const { sanitizeRoute } = require('./compile-route'); | ||
const generateMatcher = require('./generate-matcher'); | ||
const isName = nameOrMatcher => | ||
@@ -10,5 +8,4 @@ typeof nameOrMatcher === 'string' && /^[\da-zA-Z\-]+$/.test(nameOrMatcher); | ||
const filterCallsWithMatcher = function(matcher, options = {}, calls) { | ||
matcher = generateMatcher( | ||
sanitizeRoute(Object.assign({ matcher }, options)), | ||
this | ||
matcher = this.generateMatcher( | ||
this.sanitizeRoute(Object.assign({ matcher }, options)) | ||
); | ||
@@ -15,0 +12,0 @@ return calls.filter(([url, options]) => matcher(normalizeUrl(url), options)); |
const { debug, setDebugPhase } = require('./debug'); | ||
const { compileRoute } = require('./compile-route'); | ||
const FetchMock = {}; | ||
@@ -72,4 +71,2 @@ | ||
FetchMock.compileRoute = compileRoute; | ||
const defineShorthand = (methodName, underlyingMethod, shorthandOptions) => { | ||
@@ -76,0 +73,0 @@ FetchMock[methodName] = function(matcher, response, options) { |
@@ -22,8 +22,6 @@ 'use strict'; | ||
var _require = require('./debug'), | ||
debug = _require.debug, | ||
setDebugNamespace = _require.setDebugNamespace, | ||
getDebug = _require.getDebug; | ||
var generateMatcher = require('./generate-matcher'); | ||
var matcherProperties = ['query', 'method', 'headers', 'params', 'body', 'functionMatcher', 'url']; | ||
var isUrlMatcher = function isUrlMatcher(matcher) { | ||
@@ -91,4 +89,5 @@ return matcher instanceof RegExp || typeof matcher === 'string' || (typeof matcher === 'undefined' ? 'undefined' : (0, _typeof3.default)(matcher)) === 'object' && 'href' in matcher; | ||
if (!matcherProperties.some(function (matcherType) { | ||
return matcherType in route; | ||
if (!this._matchers.some(function (_ref) { | ||
var name = _ref.name; | ||
return name in route; | ||
})) { | ||
@@ -143,2 +142,34 @@ throw new Error("fetch-mock: Each route must specify some criteria for matching calls to fetch. To match all calls use '*'"); | ||
var generateMatcher = function generateMatcher(route) { | ||
var _this = this; | ||
setDebugNamespace('generateMatcher()'); | ||
debug('Compiling matcher for route'); | ||
var matchers = this._matchers.map(function (_ref2) { | ||
var name = _ref2.name, | ||
matcher = _ref2.matcher, | ||
usesBody = _ref2.usesBody; | ||
return route[name] && { matcher: matcher(route, _this), usesBody: usesBody }; | ||
}).filter(function (matcher) { | ||
return !!matcher; | ||
}); | ||
route.usesBody = matchers.some(function (_ref3) { | ||
var usesBody = _ref3.usesBody; | ||
return usesBody; | ||
}); | ||
debug('Compiled matcher for route'); | ||
setDebugNamespace(); | ||
return function (url) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var request = arguments[2]; | ||
return matchers.every(function (_ref4) { | ||
var matcher = _ref4.matcher; | ||
return matcher(url, options, request); | ||
}); | ||
}; | ||
}; | ||
var compileRoute = function compileRoute(args) { | ||
@@ -148,4 +179,4 @@ var debug = getDebug('compileRoute()'); | ||
var route = sanitizeRoute(argsToRoute(args)); | ||
validateRoute(route); | ||
route.matcher = generateMatcher(route, this); | ||
this.validateRoute(route); | ||
route.matcher = this.generateMatcher(route); | ||
limit(route); | ||
@@ -158,3 +189,5 @@ delayResponse(route); | ||
compileRoute: compileRoute, | ||
sanitizeRoute: sanitizeRoute | ||
sanitizeRoute: sanitizeRoute, | ||
generateMatcher: generateMatcher, | ||
validateRoute: validateRoute | ||
}; |
@@ -180,4 +180,4 @@ 'use strict'; | ||
if (request && this.routes.some(function (_ref3) { | ||
var body = _ref3.body; | ||
return !!body; | ||
var usesBody = _ref3.usesBody; | ||
return usesBody; | ||
})) { | ||
@@ -184,0 +184,0 @@ debug('Need to wait for Body to be streamed before calling router: switching to async mode'); |
@@ -19,4 +19,6 @@ 'use strict'; | ||
var inspecting = require('./inspecting'); | ||
var matchers = require('./matchers'); | ||
var compileRoute = require('./compile-route'); | ||
var FetchMock = (0, _assign2.default)({}, fetchHandler, setUpAndTearDown, inspecting); | ||
var FetchMock = (0, _assign2.default)({}, fetchHandler, setUpAndTearDown, inspecting, compileRoute, matchers); | ||
@@ -35,2 +37,3 @@ FetchMock.config = { | ||
instance._uncompiledRoutes = (this._uncompiledRoutes || []).slice(); | ||
instance._matchers = this._matchers.slice(); | ||
instance.routes = instance._uncompiledRoutes.map(function (config) { | ||
@@ -37,0 +40,0 @@ return instance.compileRoute(config); |
@@ -38,7 +38,2 @@ 'use strict'; | ||
var FetchMock = {}; | ||
var _require3 = require('./compile-route'), | ||
sanitizeRoute = _require3.sanitizeRoute; | ||
var generateMatcher = require('./generate-matcher'); | ||
var isName = function isName(nameOrMatcher) { | ||
@@ -52,3 +47,3 @@ return typeof nameOrMatcher === 'string' && /^[\da-zA-Z\-]+$/.test(nameOrMatcher); | ||
matcher = generateMatcher(sanitizeRoute((0, _assign2.default)({ matcher: matcher }, options)), this); | ||
matcher = this.generateMatcher(this.sanitizeRoute((0, _assign2.default)({ matcher: matcher }, options))); | ||
return calls.filter(function (_ref) { | ||
@@ -55,0 +50,0 @@ var _ref2 = (0, _slicedToArray3.default)(_ref, 2), |
@@ -13,5 +13,2 @@ 'use strict'; | ||
var _require2 = require('./compile-route'), | ||
compileRoute = _require2.compileRoute; | ||
var FetchMock = {}; | ||
@@ -89,4 +86,2 @@ | ||
FetchMock.compileRoute = compileRoute; | ||
var defineShorthand = function defineShorthand(methodName, underlyingMethod, shorthandOptions) { | ||
@@ -93,0 +88,0 @@ FetchMock[methodName] = function (matcher, response, options) { |
{ | ||
"name": "fetch-mock", | ||
"type": "module", | ||
"version": "9.2.2", | ||
"version": "9.3.0", | ||
"description": "Mock http requests made using fetch (or isomorphic-fetch)", | ||
@@ -6,0 +6,0 @@ "main": "./cjs/server.js", |
@@ -12,2 +12,3 @@ # fetch-mock | ||
- can be used as a spy to observe real network requests | ||
- can be extended with your own reusable custom matchers that can be used both for matching fetch-calls and inspecting the results | ||
- isomorphic, and supports either a global fetch instance or a locally required instance | ||
@@ -14,0 +15,0 @@ |
@@ -12,2 +12,9 @@ // Project: https://github.com/wheresrhys/fetch-mock, http://www.wheresrhys.co.uk/fetch-mock | ||
// Katsuya Hino <https://github.com/dobogo> | ||
// | ||
// Please note that I - wheresrys - don't use Typescript | ||
// These types have ben copied in here as a convenience for (some of) | ||
// fetch-mock's users | ||
// If you are a Typescript user and find a problem in these types, please | ||
// submit a PR | ||
// | ||
// TypeScript Version: 2.2 | ||
@@ -14,0 +21,0 @@ |
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 too big to display
Sorry, the diff of this file is too big to display
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
2808727
80960
68