diff --git a/.husky/pre-commit b/.husky/pre-commit index 30ee92f7..0ccfe480 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1 +1,3 @@ -npm run lint && npm run types:lint && npm run types:check +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" +npx lint-staged diff --git a/package.json b/package.json index 63b08b2a..213887ac 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,7 @@ }, "lint-staged": { "**/*.js": [ - "npm run lint" + "npm run lint && npm run types:check" ] } } diff --git a/packages/core/src/FetchMock.js b/packages/core/src/FetchMock.js index 507aa966..ee524de8 100644 --- a/packages/core/src/FetchMock.js +++ b/packages/core/src/FetchMock.js @@ -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} [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 @@ -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} [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 { /** * diff --git a/packages/core/src/Route.js b/packages/core/src/Route.js index 19e8bc14..bd83e925 100644 --- a/packages/core/src/Route.js +++ b/packages/core/src/Route.js @@ -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 { @@ -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] diff --git a/packages/core/types/FetchMock.d.ts b/packages/core/types/FetchMock.d.ts index 092d3102..38dd8d8e 100644 --- a/packages/core/types/FetchMock.d.ts +++ b/packages/core/types/FetchMock.d.ts @@ -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; 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); diff --git a/packages/core/types/Route.d.ts b/packages/core/types/Route.d.ts index e3a79fc2..3feaae0b 100644 --- a/packages/core/types/Route.d.ts +++ b/packages/core/types/Route.d.ts @@ -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; -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; @@ -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; +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[];