-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2c2a56
commit 892dab4
Showing
4 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { AxiosResponse } from 'axios'; | ||
|
||
export type CachePredicate = CachePredicateObject | ((response: AxiosResponse) => boolean); | ||
|
||
export type CachePredicateObject = { | ||
/** | ||
* The status predicate, if a tuple is returned, | ||
* the first and seconds value means the interval (inclusive) accepted. | ||
* Can also be a function. | ||
*/ | ||
statusCheck?: [start: number, end: number] | ((status: number) => boolean); | ||
|
||
/** | ||
* Matches if the response header container all keys. A tuple also checks for values. | ||
*/ | ||
containsHeaders?: (string | [string, string])[]; | ||
|
||
/** | ||
* Check if the desired response matches this predicate. | ||
*/ | ||
responseMatch?: (res: any | undefined) => boolean; | ||
}; | ||
|
||
export function checkPredicateObject( | ||
response: AxiosResponse, | ||
{ statusCheck, containsHeaders: containsHeader, responseMatch }: CachePredicateObject | ||
): boolean { | ||
if (statusCheck) { | ||
if (typeof statusCheck === 'function') { | ||
if (!statusCheck(response.status)) { | ||
return false; | ||
} | ||
} else { | ||
const [start, end] = statusCheck; | ||
if (response.status <= start || response.status >= end) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
if (containsHeader) { | ||
for (const entry of containsHeader) { | ||
if (typeof entry === 'string') { | ||
if (!response.headers[entry]) { | ||
return false; | ||
} | ||
} else { | ||
const [key, value] = entry; | ||
if (!response.headers[key] || response.headers[key] == value) { | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (responseMatch) { | ||
if (!responseMatch(response.data)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} |