Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
export type Opaque<K, T> = 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<JSONValue> {}

Expand Down Expand Up @@ -47,9 +41,7 @@ export type MockHandler =
| ((args: HandlerArgument) => JSONValue)
| JSONValue;

export type MockHandlerFunction = (
args: HandlerArgument
) => Promise<ResponseData>;
export type MockHandlerFunction = (args: HandlerArgument) => Promise<ResponseData>;
export type RequestUrl = Opaque<'RequestUrl', string>;
export type MatcherUrl = Opaque<'MatcherUrl', string>;

Expand All @@ -62,7 +54,9 @@ export type Middleware = (
request: HandlerArgument,
response: ResponseData
) => ResponseData | Promise<ResponseData>;

export interface Configuration {
enableFallback: boolean;
ignoreMiddlewareIfFallback: boolean;
middleware: Middleware;
}
4 changes: 4 additions & 0 deletions src/yet-another-fetch-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import MatcherUtils from './matcher-utils';

const defaultConfiguration: Configuration = {
enableFallback: true,
ignoreMiddlewareIfFallback: false,
middleware: (request, response) => response
};

Expand Down Expand Up @@ -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<Response>;
}
} else {
throw new Error(`Did not find any matching route for: ${method.toUpperCase()} ${url}.`);
}
Expand Down