Skip to content

Commit

Permalink
refactor: types mostly work now
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Jul 21, 2024
1 parent bd6e7ac commit 16fc353
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/CallHistory.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//@type-check
/** @typedef {import('./Route').RouteConfig} RouteConfig */
/** @typedef {import('./Route').RouteName} RouteName */
/** @typedef {import('./Router')} Router */
/** @typedef {import('./RequestUtils').NormalizedRequestOptions} NormalizedRequestOptions */
/** @typedef {import('./Matchers').RouteMatcher} RouteMatcher */
/** @typedef {import('./FetchMock').FetchMockConfig} FetchMockConfig */
import { createCallLog } from './RequestUtils.js';
import { isUrlMatcher } from './Matchers.js';
import Route from './Route.js';
import Router from './Router.js';

/**
* @typedef CallLog
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/FetchMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ const defaultConfig = {
* @property {function():void} clearHistory
*/

const defaultRouter = new Router(defaultConfig)

/** @type {FetchMockCore} */
const FetchMock = {
config: defaultConfig,
router: new Router(defaultConfig),
callHistory: new CallHistory(defaultConfig, this.router),
router: defaultRouter,
callHistory: new CallHistory(defaultConfig, defaultRouter),
createInstance() {
const instance = Object.create(FetchMock);
instance.config = { ...this.config };
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/Matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ export const isUrlMatcher = (matcher) =>
export const isFunctionMatcher = (matcher) => typeof matcher === 'function';

/** @typedef {string | RegExp | URL} RouteMatcherUrl */
/** @typedef {function(string): boolean} UrlMatcher */
/** @typedef {function(string): UrlMatcher} UrlMatcherGenerator */
/** @typedef {function(string): RouteMatcherFunction} UrlMatcherGenerator */
/** @typedef {function(CallLog): boolean} RouteMatcherFunction */
/** @typedef {function(RouteConfig): RouteMatcherFunction} MatcherGenerator */
/** @typedef {RouteMatcherUrl | RouteMatcherFunction} RouteMatcher */
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { builtInMatchers } from './Matchers';
import statusTextMap from './StatusTextMap';

/** @typedef {import('./Matchers').RouteMatcher} RouteMatcher */
/** @typedef {import('./CallHistory').CallLog} CallLog */
/** @typedef {import('./Matchers').RouteMatcherFunction} RouteMatcherFunction */
/** @typedef {import('./Matchers').RouteMatcherUrl} RouteMatcherUrl */
/** @typedef {import('./Matchers').MatcherDefinition} MatcherDefinition */
Expand All @@ -28,7 +29,7 @@ import statusTextMap from './StatusTextMap';
/** @typedef {RouteResponseConfig | object} RouteResponseObjectData */
/** @typedef {Response | number| string | RouteResponseObjectData } RouteResponseData */
/** @typedef {Promise<RouteResponseData>} RouteResponsePromise */
/** @typedef {function(string, RequestInit, Request=): (RouteResponseData|RouteResponsePromise)} RouteResponseFunction */
/** @typedef {function(CallLog): (RouteResponseData|RouteResponsePromise)} RouteResponseFunction */
/** @typedef {RouteResponseData | RouteResponsePromise | RouteResponseFunction} RouteResponse*/

/** @typedef {string} RouteName */
Expand Down Expand Up @@ -161,8 +162,8 @@ class Route {
}
const originalMatcher = this.matcher;
let timesLeft = this.config.repeat;
this.matcher = (url, options, request) => {
const match = timesLeft && originalMatcher(url, options, request);
this.matcher = (callLog) => {
const match = timesLeft && originalMatcher(callLog);
if (match) {
timesLeft--;
return true;
Expand Down

0 comments on commit 16fc353

Please sign in to comment.