Skip to content

Commit

Permalink
feat!: renamed func to matcherFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Jul 20, 2024
1 parent f41d8f9 commit e5679a7
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/docs/@fetch-mock/core/route/matcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/Matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ const getFullUrlMatcher = (route, matcherUrl, query) => {
/**
* @type {MatcherGenerator}
*/
const getFunctionMatcher = ({ func }) => func;
const getFunctionMatcher = ({ matcherFunction }) => matcherFunction;
/**
* @type {MatcherGenerator}
*/
Expand Down Expand Up @@ -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 },
];
2 changes: 1 addition & 1 deletion packages/core/src/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -368,7 +368,7 @@ export default class Router {
}

this.fallbackRoute = new Route({
func: () => true,
matcherFunction: () => true,
response: response || 'ok',
...this.config,
});
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/__tests__/Matchers/function.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand All @@ -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);
Expand All @@ -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,
});

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/__tests__/Matchers/method.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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,
});
Expand Down

0 comments on commit e5679a7

Please sign in to comment.