Skip to content

Commit 42aeac7

Browse files
author
Nicklas Utgaard
committed
feat(edge): added ignoreMiddlewareIfFallback flag
added to short-circuit middleswares if realFetch is used
1 parent 93dbd12 commit 42aeac7

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

src/types.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
export type Opaque<K, T> = T & { __TYPE__: K };
22

3-
export type JSONValue =
4-
| null
5-
| string
6-
| number
7-
| boolean
8-
| JSONObject
9-
| JSONArray;
3+
export type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
104
export type JSONObject = { [member: string]: JSONValue };
115
export interface JSONArray extends Array<JSONValue> {}
126

@@ -47,9 +41,7 @@ export type MockHandler =
4741
| ((args: HandlerArgument) => JSONValue)
4842
| JSONValue;
4943

50-
export type MockHandlerFunction = (
51-
args: HandlerArgument
52-
) => Promise<ResponseData>;
44+
export type MockHandlerFunction = (args: HandlerArgument) => Promise<ResponseData>;
5345
export type RequestUrl = Opaque<'RequestUrl', string>;
5446
export type MatcherUrl = Opaque<'MatcherUrl', string>;
5547

@@ -62,7 +54,9 @@ export type Middleware = (
6254
request: HandlerArgument,
6355
response: ResponseData
6456
) => ResponseData | Promise<ResponseData>;
57+
6558
export interface Configuration {
6659
enableFallback: boolean;
60+
ignoreMiddlewareIfFallback: boolean;
6761
middleware: Middleware;
6862
}

src/yet-another-fetch-mock.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import MatcherUtils from './matcher-utils';
2020

2121
const defaultConfiguration: Configuration = {
2222
enableFallback: true,
23+
ignoreMiddlewareIfFallback: false,
2324
middleware: (request, response) => response
2425
};
2526

@@ -80,6 +81,9 @@ class FetchMock {
8081
`Did not find any matching route for: ${method.toUpperCase()} ${url}. Defaulting to the real fetch-implementation.`
8182
);
8283
response = this.realFetch.call(this.scope, input, init);
84+
if (this.configuration.ignoreMiddlewareIfFallback) {
85+
return response as Promise<Response>;
86+
}
8387
} else {
8488
throw new Error(`Did not find any matching route for: ${method.toUpperCase()} ${url}.`);
8589
}

0 commit comments

Comments
 (0)