Skip to content

Commit

Permalink
refactor!: defined route shorthand methods more declaratively
Browse files Browse the repository at this point in the history
have also removed {method}Any and {method}AnyOnce - not all that useful
  • Loading branch information
wheresrhys committed Jul 23, 2024
1 parent 10ec8af commit f42d240
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 318 deletions.
3 changes: 2 additions & 1 deletion jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"outDir": "packages/core/types",
"skipLibCheck": true,
"noEmit": false,
"target": "es2021"
"target": "es2021",
"removeComments": true
},
"include": [
"./packages/core/src/*.js"
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/CallHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class CallHistory {
this.callLogs.push(callLog);
}

/**
* @returns {void}
*/
clear() {
this.callLogs.forEach(({ route }) => route.reset());
this.callLogs = [];
Expand Down
175 changes: 80 additions & 95 deletions packages/core/src/FetchMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,65 @@ import * as requestUtils from './RequestUtils.js';
/** @typedef {import('./CallHistory').CallLog} CallLog */
/** @typedef {import('./Route').RouteResponseFunction} RouteResponseFunction */

/** @typedef {'get' |'post' |'put' |'delete' |'head' |'patch' |'once' |'sticky' |'any' |'anyOnce' |'getOnce' |'postOnce' |'putOnce' |'deleteOnce' |'headOnce' |'patchOnce' } AdditionalRouteMethodName */

/**
*
* @param {UserRouteConfig} shorthandOptions
*/
const defineShorthand = (shorthandOptions) => {
/**
* @overload
* @param {UserRouteConfig} matcher
* @this {FetchMock}
* @returns {FetchMock}
*/

/**
* @overload
* @param {RouteMatcher } matcher
* @param {RouteResponse} response
* @param {UserRouteConfig | string} [options]
* @this {FetchMock}
* @returns {FetchMock}
*/

/**
* @param {RouteMatcher | UserRouteConfig} matcher
* @param {RouteResponse} [response]
* @param {UserRouteConfig | string} [options]
* @this {FetchMock}
* @returns {FetchMock}
*/
return function (matcher, response, options) {
return this.route(
//@ts-ignore
matcher,
response,
Object.assign(options || {}, shorthandOptions),
);
};
};
/**
*
* @param {UserRouteConfig} shorthandOptions
*/
const defineGreedyShorthand = (shorthandOptions) => {
/**
* @param {RouteResponse} response
* @param {UserRouteConfig | string} [options]
* @this {FetchMock}
* @returns {FetchMock}
*/
return function (response, options) {
return this.route(
'*',
response,
Object.assign(options || {}, shorthandOptions),
);
};
};

/**
* @typedef FetchMockConfig
* @property {boolean} [sendAsJson]
Expand All @@ -35,9 +94,7 @@ const defaultConfig = {
fetch: globalThis.fetch,
};

/** @typedef {FetchMockCore & PresetRoutes} FetchMock*/

class FetchMockCore {
class FetchMock {
/**
*
* @param {FetchMockConfig} config
Expand All @@ -56,8 +113,7 @@ class FetchMockCore {
* @returns {FetchMock}
*/
createInstance() {
const instance = new FetchMockCore({ ...this.config }, this.router);
return Object.assign(instance, PresetRoutes);
return new FetchMock({ ...this.config }, this.router);
}
/**
*
Expand Down Expand Up @@ -89,19 +145,15 @@ class FetchMockCore {
/**
* @overload
* @param {UserRouteConfig} matcher
* @this {FetchMock}
* @returns {FetchMock}
*/

/**
* @overload
* @param {RouteMatcher } matcher
* @param {RouteResponse} response
* @param {UserRouteConfig | string} [options]
* @this {FetchMock}
* @returns {FetchMock}
*/

/**
* @param {RouteMatcher | UserRouteConfig} matcher
* @param {RouteResponse} [response]
Expand All @@ -116,6 +168,7 @@ class FetchMockCore {
/**
*
* @param {RouteResponse} [response]
* @this {FetchMock}
* @returns {FetchMock}
*/
catch(response) {
Expand All @@ -136,6 +189,7 @@ class FetchMockCore {
* @param {string[]} [options.names]
* @param {boolean} [options.includeSticky]
* @param {boolean} [options.includeFallback]
* @this {FetchMock}
* @returns {FetchMock}
*/
removeRoutes(options) {
Expand All @@ -150,93 +204,24 @@ class FetchMockCore {
this.callHistory.clear();
return this;
}
sticky = defineShorthand({ sticky: true });
once = defineShorthand({ repeat: 1 });
any = defineGreedyShorthand({});
anyOnce = defineGreedyShorthand({ repeat: 1 });
get = defineShorthand({ method: 'get' });
getOnce = defineShorthand({ method: 'get', repeat: 1 });
post = defineShorthand({ method: 'post' });
postOnce = defineShorthand({ method: 'post', repeat: 1 });
put = defineShorthand({ method: 'put' });
putOnce = defineShorthand({ method: 'put', repeat: 1 });
delete = defineShorthand({ method: 'delete' });
deleteOnce = defineShorthand({ method: 'delete', repeat: 1 });
head = defineShorthand({ method: 'head' });
headOnce = defineShorthand({ method: 'head', repeat: 1 });
patch = defineShorthand({ method: 'patch' });
patchOnce = defineShorthand({ method: 'patch', repeat: 1 });
}

/** @typedef {'get' |'post' |'put' |'delete' |'head' |'patch' |'once' |'sticky' |'any' |'anyOnce' |'getOnce' |'postOnce' |'putOnce' |'deleteOnce' |'headOnce' |'patchOnce' |'getAny' |'postAny' |'putAny' |'deleteAny' |'headAny' |'patchAny' |'getAnyOnce' |'postAnyOnce' |'putAnyOnce' |'deleteAnyOnce' |'headAnyOnce' |'patchAnyOnce'} PresetRouteMethodName} */
/** @typedef {Object.<PresetRouteMethodName, function(any,any,any): FetchMock>} PresetRoutes */

/** @type {PresetRoutes} */
const PresetRoutes = {};
/**
*
* @param {PresetRouteMethodName} methodName
* @param {string} underlyingMethod
* @param {UserRouteConfig} shorthandOptions
*/
const defineShorthand = (methodName, underlyingMethod, shorthandOptions) => {
/**
* @overload
* @param {UserRouteConfig} matcher
* @this {FetchMock}
* @returns {FetchMock}
*/

/**
* @overload
* @param {RouteMatcher } matcher
* @param {RouteResponse} response
* @param {UserRouteConfig | string} [options]
* @this {FetchMock}
* @returns {FetchMock}
*/

/**
* @param {RouteMatcher | UserRouteConfig} matcher
* @param {RouteResponse} [response]
* @param {UserRouteConfig | string} [options]
* @this {FetchMock}
* @returns {FetchMock}
*/
PresetRoutes[methodName] = function (matcher, response, options) {
return this[underlyingMethod](
matcher,
response,
Object.assign(options || {}, shorthandOptions),
);
};
};
/**
*
* @param {PresetRouteMethodName} methodName
* @param {string} underlyingMethod
*/
const defineGreedyShorthand = (methodName, underlyingMethod) => {
/**
* @param {RouteResponse} response
* @param {UserRouteConfig | string} [options]
* @this {FetchMock}
* @returns {FetchMock}
*/
PresetRoutes[methodName] = function (response, options) {
return this[underlyingMethod]('*', response, options);
};
};

defineShorthand('sticky', 'route', { sticky: true });
defineShorthand('once', 'route', { repeat: 1 });
defineGreedyShorthand('any', 'route');
defineGreedyShorthand('anyOnce', 'once');

['get', 'post', 'put', 'delete', 'head', 'patch'].forEach((method) => {
defineShorthand(/** @type {PresetRouteMethodName} */ (method), 'route', {
method,
});
defineShorthand(
/** @type {PresetRouteMethodName} */ (`${method}Once`),
'once',
{ method },
);
defineGreedyShorthand(
/** @type {PresetRouteMethodName} */ (`${method}Any`),
method,
);
defineGreedyShorthand(
/** @type {PresetRouteMethodName} */ (`${method}AnyOnce`),
`${method}Once`,
);
});

const fetchMock = new FetchMockCore({ ...defaultConfig }).createInstance();
const fetchMock = new FetchMock({ ...defaultConfig }).createInstance();

console.log(fetchMock);
export default fetchMock;
1 change: 0 additions & 1 deletion packages/core/src/RequestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export function normalizeUrl(url) {
return u.pathname + u.search;
}
/**
*
* @param {string|Request} urlOrRequest
* @param {typeof Request} Request
* @returns {urlOrRequest is Request}
Expand Down
14 changes: 0 additions & 14 deletions packages/core/src/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,20 +288,6 @@ export default class Router {
});
}

/**
* @overload
* @param {UserRouteConfig} matcher
* @returns {void}
*/

/**
* @overload
* @param {RouteMatcher } matcher
* @param {RouteResponse} response
* @param {UserRouteConfig | string} [nameOrOptions]
* @returns {void}
*/

/**
* @param {RouteMatcher | UserRouteConfig} matcher
* @param {RouteResponse} [response]
Expand Down
36 changes: 0 additions & 36 deletions packages/core/types/CallHistory.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,52 +24,16 @@ export type Matched = "matched";
export type Unmatched = "unmatched";
export type CallHistoryFilter = RouteName | Matched | Unmatched | boolean | RouteMatcher;
declare class CallHistory {
/**
* @param {FetchMockConfig} globalConfig
* @param {Router} router
*/
constructor(globalConfig: FetchMockConfig, router: Router);
/** @type {CallLog[]} */
callLogs: CallLog[];
config: import("./FetchMock").FetchMockConfig;
router: Router;
/**
*
* @param {CallLog} callLog
*/
recordCall(callLog: CallLog): void;
clear(): void;
/**
*
* @param {boolean} [waitForResponseMethods]
* @returns {Promise<void>}
*/
flush(waitForResponseMethods?: boolean): Promise<void>;
/**
*
* @param {CallHistoryFilter} filter
* @param {RouteConfig} options
* @returns {CallLog[]}
*/
calls(filter: CallHistoryFilter, options: RouteConfig): CallLog[];
/**
*
* @param {CallHistoryFilter} filter
* @param {RouteConfig} options
* @returns {boolean}
*/
called(filter: CallHistoryFilter, options: RouteConfig): boolean;
/**
*
* @param {CallHistoryFilter} filter
* @param {RouteConfig} options
* @returns {CallLog}
*/
lastCall(filter: CallHistoryFilter, options: RouteConfig): CallLog;
/**
* @param {RouteName|RouteName[]} [routeNames]
* @returns {boolean}
*/
done(routeNames?: RouteName | RouteName[]): boolean;
}
import Route from './Route.js';
Expand Down
Empty file.
Loading

0 comments on commit f42d240

Please sign in to comment.