diff --git a/src/types.ts b/src/types.ts index 6351ea5..3795e7d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,12 +1,6 @@ export type Opaque = T & { __TYPE__: K }; -export type JSONValue = - | null - | string - | number - | boolean - | JSONObject - | JSONArray; +export type JSONValue = null | string | number | boolean | JSONObject | JSONArray; export type JSONObject = { [member: string]: JSONValue }; export interface JSONArray extends Array {} @@ -47,9 +41,7 @@ export type MockHandler = | ((args: HandlerArgument) => JSONValue) | JSONValue; -export type MockHandlerFunction = ( - args: HandlerArgument -) => Promise; +export type MockHandlerFunction = (args: HandlerArgument) => Promise; export type RequestUrl = Opaque<'RequestUrl', string>; export type MatcherUrl = Opaque<'MatcherUrl', string>; @@ -62,7 +54,9 @@ export type Middleware = ( request: HandlerArgument, response: ResponseData ) => ResponseData | Promise; + export interface Configuration { enableFallback: boolean; + ignoreMiddlewareIfFallback: boolean; middleware: Middleware; } diff --git a/src/yet-another-fetch-mock.ts b/src/yet-another-fetch-mock.ts index ff5678f..aee7e7a 100644 --- a/src/yet-another-fetch-mock.ts +++ b/src/yet-another-fetch-mock.ts @@ -20,6 +20,7 @@ import MatcherUtils from './matcher-utils'; const defaultConfiguration: Configuration = { enableFallback: true, + ignoreMiddlewareIfFallback: false, middleware: (request, response) => response }; @@ -80,6 +81,9 @@ class FetchMock { `Did not find any matching route for: ${method.toUpperCase()} ${url}. Defaulting to the real fetch-implementation.` ); response = this.realFetch.call(this.scope, input, init); + if (this.configuration.ignoreMiddlewareIfFallback) { + return response as Promise; + } } else { throw new Error(`Did not find any matching route for: ${method.toUpperCase()} ${url}.`); }