Skip to content

Commit

Permalink
Merge pull request #741 from wheresrhys/rhys/config-types-split
Browse files Browse the repository at this point in the history
chore: split user route config more dryly
  • Loading branch information
wheresrhys authored Jul 24, 2024
2 parents acdd8a3 + f4f3fea commit 57eb6f3
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 80 deletions.
4 changes: 3 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
npm run lint && npm run types:lint && npm run types:check
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx lint-staged
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
},
"lint-staged": {
"**/*.js": [
"npm run lint"
"npm run lint && npm run types:check"
]
}
}
52 changes: 28 additions & 24 deletions packages/core/src/FetchMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,34 @@ import * as requestUtils from './RequestUtils.js';
/** @typedef {import('./CallHistory').CallLog} CallLog */
/** @typedef {import('./Route').RouteResponseFunction} RouteResponseFunction */

/**
* @typedef FetchMockGlobalConfig
* @property {boolean} [sendAsJson]
* @property {boolean} [includeContentLength]
* @property {boolean} [matchPartialBody]
*/

/**
* @typedef FetchImplementations
* @property {function(string | Request, RequestInit): Promise<Response>} [fetch]
* @property {typeof Headers} [Headers]
* @property {typeof Request} [Request]
* @property {typeof Response} [Response]
*/

/** @typedef {FetchMockGlobalConfig & FetchImplementations} FetchMockConfig */

/** @type {FetchMockConfig} */
const defaultConfig = {
includeContentLength: true,
sendAsJson: true,
matchPartialBody: false,
Request: globalThis.Request,
Response: globalThis.Response,
Headers: globalThis.Headers,
fetch: globalThis.fetch,
};

/**
*
* @param {UserRouteConfig} shorthandOptions
Expand Down Expand Up @@ -68,30 +96,6 @@ const defineGreedyShorthand = (shorthandOptions) => {
};
};

/**
* @typedef FetchMockConfig
* @property {boolean} [sendAsJson]
* @property {boolean} [includeContentLength]
* @property {boolean} [warnOnFallback]
* @property {boolean} [matchPartialBody]
* @property {function(string | Request, RequestInit): Promise<Response>} [fetch]
* @property {typeof Headers} [Headers]
* @property {typeof Request} [Request]
* @property {typeof Response} [Response]
*/

/** @type {FetchMockConfig} */
const defaultConfig = {
includeContentLength: true,
sendAsJson: true,
warnOnFallback: true,
matchPartialBody: false,
Request: globalThis.Request,
Response: globalThis.Response,
Headers: globalThis.Headers,
fetch: globalThis.fetch,
};

class FetchMock {
/**
*
Expand Down
52 changes: 27 additions & 25 deletions packages/core/src/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,33 @@ import statusTextMap from './StatusTextMap';
/** @typedef {import('./Matchers').RouteMatcherFunction} RouteMatcherFunction */
/** @typedef {import('./Matchers').RouteMatcherUrl} RouteMatcherUrl */
/** @typedef {import('./Matchers').MatcherDefinition} MatcherDefinition */
/** @typedef {import('./FetchMock').FetchMockConfig} FetchMockConfig */
/** @typedef {import('./FetchMock').FetchMockGlobalConfig} FetchMockGlobalConfig */
/** @typedef {import('./FetchMock').FetchImplementations} FetchImplementations */

/**
* @typedef UserRouteConfig
* @property {RouteName} [name]
* @property {string} [method]
* @property {{ [key: string]: string | number }} [headers]
* @property {{ [key: string]: string }} [query]
* @property {{ [key: string]: string }} [params]
* @property {object} [body]
* @property {RouteMatcherFunction} [matcherFunction]
* @property {RouteMatcherUrl} [url]
* @property {RouteResponse | RouteResponseFunction} [response]
* @property {number} [repeat]
* @property {number} [delay]
* @property {boolean} [sticky]
*/

/**
* @typedef InternalRouteConfig
* @property {boolean} [usesBody]
* @property {boolean} [isFallback]
*/

/** @typedef {UserRouteConfig & FetchMockGlobalConfig} ExtendedUserRouteConfig */
/** @typedef {ExtendedUserRouteConfig & FetchImplementations & InternalRouteConfig} RouteConfig */

/**
* @typedef RouteResponseConfig {
Expand All @@ -34,30 +60,6 @@ import statusTextMap from './StatusTextMap';

/** @typedef {string} RouteName */

/**
* @typedef UserRouteConfig
* @property {RouteName} [name]
* @property {string} [method]
* @property {{ [key: string]: string | number }} [headers]
* @property {{ [key: string]: string }} [query]
* @property {{ [key: string]: string }} [params]
* @property {object} [body]
* @property {RouteMatcherFunction} [matcherFunction]
* @property {RouteMatcher} [matcher]
* @property {RouteMatcherUrl} [url]
* @property {RouteResponse | RouteResponseFunction} [response]
* @property {number} [repeat]
* @property {number} [delay]
* @property {boolean} [sendAsJson] - TODO this is global
* @property {boolean} [includeContentLength] - TODO this is global
* @property {boolean} [matchPartialBody] - TODO this is global
* @property {boolean} [sticky]
* @property {boolean} [usesBody] - TODO this shoudl not be in user config
* @property {boolean} [isFallback]
*/

/** @typedef {UserRouteConfig & FetchMockConfig} RouteConfig*/

/**
*
* @param {number} [status]
Expand Down
6 changes: 4 additions & 2 deletions packages/core/types/FetchMock.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ export type RouteResponse = import("./Router").RouteResponse;
export type MatcherDefinition = import("./Matchers").MatcherDefinition;
export type CallLog = import("./CallHistory").CallLog;
export type RouteResponseFunction = import("./Route").RouteResponseFunction;
export type FetchMockConfig = {
export type FetchMockGlobalConfig = {
sendAsJson?: boolean;
includeContentLength?: boolean;
warnOnFallback?: boolean;
matchPartialBody?: boolean;
};
export type FetchImplementations = {
fetch?: (arg0: string | Request, arg1: RequestInit) => Promise<Response>;
Headers?: typeof Headers;
Request?: typeof Request;
Response?: typeof Response;
};
export type FetchMockConfig = FetchMockGlobalConfig & FetchImplementations;
declare const fetchMock: FetchMock;
declare class FetchMock {
constructor(config: FetchMockConfig, router?: Router);
Expand Down
54 changes: 27 additions & 27 deletions packages/core/types/Route.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,8 @@ export type CallLog = import("./CallHistory").CallLog;
export type RouteMatcherFunction = import("./Matchers").RouteMatcherFunction;
export type RouteMatcherUrl = import("./Matchers").RouteMatcherUrl;
export type MatcherDefinition = import("./Matchers").MatcherDefinition;
export type FetchMockConfig = import("./FetchMock").FetchMockConfig;
export type RouteResponseConfig = {
body?: string | {};
status?: number;
headers?: {
[key: string]: string;
};
throws?: Error;
redirectUrl?: string;
options?: ResponseInit;
};
export type ResponseInitUsingHeaders = {
status: number;
statusText: string;
headers: Headers;
};
export type RouteResponseObjectData = RouteResponseConfig | object;
export type RouteResponseData = Response | number | string | RouteResponseObjectData;
export type RouteResponsePromise = Promise<RouteResponseData>;
export type RouteResponseFunction = (arg0: CallLog) => (RouteResponseData | RouteResponsePromise);
export type RouteResponse = RouteResponseData | RouteResponsePromise | RouteResponseFunction;
export type RouteName = string;
export type FetchMockGlobalConfig = import("./FetchMock").FetchMockGlobalConfig;
export type FetchImplementations = import("./FetchMock").FetchImplementations;
export type UserRouteConfig = {
name?: RouteName;
method?: string;
Expand All @@ -40,19 +20,39 @@ export type UserRouteConfig = {
};
body?: object;
matcherFunction?: RouteMatcherFunction;
matcher?: RouteMatcher;
url?: RouteMatcherUrl;
response?: RouteResponse | RouteResponseFunction;
repeat?: number;
delay?: number;
sendAsJson?: boolean;
includeContentLength?: boolean;
matchPartialBody?: boolean;
sticky?: boolean;
};
export type InternalRouteConfig = {
usesBody?: boolean;
isFallback?: boolean;
};
export type RouteConfig = UserRouteConfig & FetchMockConfig;
export type ExtendedUserRouteConfig = UserRouteConfig & FetchMockGlobalConfig;
export type RouteConfig = ExtendedUserRouteConfig & FetchImplementations & InternalRouteConfig;
export type RouteResponseConfig = {
body?: string | {};
status?: number;
headers?: {
[key: string]: string;
};
throws?: Error;
redirectUrl?: string;
options?: ResponseInit;
};
export type ResponseInitUsingHeaders = {
status: number;
statusText: string;
headers: Headers;
};
export type RouteResponseObjectData = RouteResponseConfig | object;
export type RouteResponseData = Response | number | string | RouteResponseObjectData;
export type RouteResponsePromise = Promise<RouteResponseData>;
export type RouteResponseFunction = (arg0: CallLog) => (RouteResponseData | RouteResponsePromise);
export type RouteResponse = RouteResponseData | RouteResponsePromise | RouteResponseFunction;
export type RouteName = string;
declare class Route {
static defineMatcher(matcher: MatcherDefinition): void;
static registeredMatchers: MatcherDefinition[];
Expand Down

0 comments on commit 57eb6f3

Please sign in to comment.