diff --git a/docs/docs/@fetch-mock/core/route/matcher.md b/docs/docs/@fetch-mock/core/route/matcher.md index b9b1a7fc..ac7abe04 100644 --- a/docs/docs/@fetch-mock/core/route/matcher.md +++ b/docs/docs/@fetch-mock/core/route/matcher.md @@ -141,7 +141,7 @@ This option can also be [set in the global configuration](/fetch-mock/docs/@fetc For use cases not covered by all the built in matchers, a custom function can be used. It should return `true` to indicate a route should respond to a request. It will be passed the `url` and `options` `fetch` was called with. If `fetch` was called with a `Request` instance, it will be passed `url` and `options` inferred from the `Request` instance, with the original `Request` available as a third argument. -As well as being passed as a standalone argument, it can also be added to the matcher object as the property `{func: ...}` when combining with other matchers or options. +As well as being passed as a standalone argument, it can also be added to the matcher object as the property `{matcherFunction: ...}` when combining with other matchers or options. ### Examples diff --git a/packages/core/src/Matchers.js b/packages/core/src/Matchers.js index c24ba2f7..50f540c5 100644 --- a/packages/core/src/Matchers.js +++ b/packages/core/src/Matchers.js @@ -196,7 +196,7 @@ const getFullUrlMatcher = (route, matcherUrl, query) => { /** * @type {MatcherGenerator} */ -const getFunctionMatcher = ({ func }) => func; +const getFunctionMatcher = ({ matcherFunction }) => matcherFunction; /** * @type {MatcherGenerator} */ @@ -236,6 +236,6 @@ export const builtInMatchers = [ { name: 'headers', matcher: getHeaderMatcher }, { name: 'params', matcher: getParamsMatcher }, { name: 'body', matcher: getBodyMatcher, usesBody: true }, - { name: 'func', matcher: getFunctionMatcher }, + { name: 'matcherFunction', matcher: getFunctionMatcher }, { name: 'url', matcher: getUrlMatcher }, ]; diff --git a/packages/core/src/Route.js b/packages/core/src/Route.js index 830401c3..1341b44e 100644 --- a/packages/core/src/Route.js +++ b/packages/core/src/Route.js @@ -41,7 +41,7 @@ import statusTextMap from './StatusTextMap'; * @property {{ [key: string]: string }} [query] * @property {{ [key: string]: string }} [params] * @property {object} [body] - * @property {RouteMatcherFunction} [func] + * @property {RouteMatcherFunction} [matcherFunction] * @property {RouteMatcher} [matcher] * @property {RouteMatcherUrl} [url] * @property {RouteResponse | RouteResponseFunction} [response] diff --git a/packages/core/src/Router.js b/packages/core/src/Router.js index da16a22c..7e3c0fb2 100644 --- a/packages/core/src/Router.js +++ b/packages/core/src/Router.js @@ -278,8 +278,8 @@ export default class Router { if (typeof response[name] === 'function') { //@ts-ignore return new Proxy(response[name], { - apply: (func, thisArg, args) => { - const result = func.apply(response, args); + apply: (matcherFunction, thisArg, args) => { + const result = matcherFunction.apply(response, args); if (result.then) { pendingPromises.push( result.catch(/** @type {function(): void} */ () => undefined), @@ -321,7 +321,7 @@ export default class Router { if (isUrlMatcher(matcher)) { config.url = matcher; } else if (isFunctionMatcher(matcher)) { - config.func = matcher; + config.matcherFunction = matcher; } else { Object.assign(config, matcher); } @@ -368,7 +368,7 @@ export default class Router { } this.fallbackRoute = new Route({ - func: () => true, + matcherFunction: () => true, response: response || 'ok', ...this.config, }); diff --git a/packages/core/src/__tests__/Matchers/function.test.js b/packages/core/src/__tests__/Matchers/function.test.js index 20705e1f..d0f4d0db 100644 --- a/packages/core/src/__tests__/Matchers/function.test.js +++ b/packages/core/src/__tests__/Matchers/function.test.js @@ -4,7 +4,7 @@ import Route from '../../Route.js'; describe('function matching', () => { it('match using custom function', () => { const route = new Route({ - func: (url, opts) => + matcherFunction: (url, opts) => url.indexOf('logged-in') > -1 && opts && opts.headers && @@ -27,7 +27,7 @@ describe('function matching', () => { it('match using custom function using request body', () => { const route = new Route({ - func: (url, opts) => opts.body === 'a string', + matcherFunction: (url, opts) => opts.body === 'a string', response: 200, }); expect(route.matcher('http://a.com/logged-in')).toBe(false); @@ -43,7 +43,7 @@ describe('function matching', () => { const route = new Route({ url: 'end:profile', response: 200, - func: (url, opts) => + matcherFunction: (url, opts) => opts && opts.headers && opts.headers.authorized === true, }); diff --git a/packages/core/src/__tests__/Matchers/method.test.js b/packages/core/src/__tests__/Matchers/method.test.js index 60fced91..3d8a940d 100644 --- a/packages/core/src/__tests__/Matchers/method.test.js +++ b/packages/core/src/__tests__/Matchers/method.test.js @@ -43,7 +43,7 @@ describe('method matching', () => { it('can be used alongside function matchers', () => { const route = new Route({ method: 'POST', - func: (url) => /a\.com/.test(url), + matcherFunction: (url) => /a\.com/.test(url), response: 200, }); diff --git a/packages/core/src/__tests__/Matchers/route-config-object.test.js b/packages/core/src/__tests__/Matchers/route-config-object.test.js index f8e95b58..fff37d0b 100644 --- a/packages/core/src/__tests__/Matchers/route-config-object.test.js +++ b/packages/core/src/__tests__/Matchers/route-config-object.test.js @@ -17,7 +17,7 @@ describe('matcher object', () => { it('can use function and url simultaneously', () => { const route = new Route({ url: 'end:path', - func: (url, opts) => + matcherFunction: (url, opts) => opts && opts.headers && opts.headers.authorized === true, response: 200, }); @@ -42,10 +42,10 @@ describe('matcher object', () => { }); //TODO be stronger on discouraging this - it.skip('deprecated message on using func (prefer matcher)', () => { + it.skip('deprecated message on using matcherFunction (prefer matcher)', () => { new Route({ url: 'end:profile', - func: (url, opts) => + matcherFunction: (url, opts) => opts && opts.headers && opts.headers.authorized === true, response: 200, });