diff --git a/src/leonardo/configuration.srv.ts b/src/leonardo/configuration.srv.ts
index 5ad87b5..193313d 100644
--- a/src/leonardo/configuration.srv.ts
+++ b/src/leonardo/configuration.srv.ts
@@ -1,4 +1,6 @@
///
+import MockRequest from "xhr-mock/lib/MockRequest";
+
declare let Object: any;
export function leoConfiguration() {
let _states = [],
@@ -34,6 +36,7 @@ export function leoConfiguration() {
addSavedState: addSavedState,
addOrUpdateSavedState: addOrUpdateSavedState,
fetchStatesByUrlAndMethod: fetchStatesByUrlAndMethod,
+ fetchStatesByRequest: fetchStatesByRequest,
removeState: removeState,
removeOption: removeOption,
onStateChange: onSetStates,
@@ -175,6 +178,29 @@ export function leoConfiguration() {
})[0];
}
+ function fetchStatesByRequest(request: MockRequest) {
+ return fetchStates()
+ .filter((state) => state.url || state.verb || typeof state.match === 'function')
+ .filter((state) => {
+ if (state.url) {
+ return isMatchUrl(state.url, request.url().toString());
+ }
+ return true;
+ })
+ .filter((state) => {
+ if (state.verb) {
+ return request.method().toLocaleLowerCase() === state.verb.toLowerCase();
+ }
+ return true;
+ })
+ .filter((state) => {
+ if (typeof state.match === 'function') {
+ return !!this.state.match.call(this.state, request);
+ }
+ return true;
+ })[0];
+ }
+
function isMatchUrl(stateUrlPattern, url) {
const urlRegexp = new RegExp(stateUrlPattern);
const decodedUrl = decodeURIComponent(url);
diff --git a/src/leonardo/xhr-mock.srv.ts b/src/leonardo/xhr-mock.srv.ts
index 7e94bfe..d04c24f 100644
--- a/src/leonardo/xhr-mock.srv.ts
+++ b/src/leonardo/xhr-mock.srv.ts
@@ -11,7 +11,7 @@ export class XhrMock {
private init() {
xhrMock.setup();
xhrMock.use(async (request: MockRequest, response: MockResponse) => {
- const state = Leonardo.fetchStatesByUrlAndMethod(request.url().toString(), request.method());
+ const state = Leonardo.fetchStatesByRequest(request);
if (state && state.active) {
const activeOption = Leonardo.getActiveStateOption(state.name);
if (!!activeOption) {
@@ -50,7 +50,7 @@ export class XhrMock {
const res = response.status(resStatus).headers(resHeaders).body(deepCopy(resData));
this.log(request, res);
- return new Promise((resolve, reject) => {
+ return new Promise((resolve) => {
setTimeout(() => resolve(res), delay);
});
}