-
Notifications
You must be signed in to change notification settings - Fork 0
/
axiosMocker.ts
31 lines (27 loc) · 919 Bytes
/
axiosMocker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import { Utils } from "./core/utils";
let adapter: MockAdapter;
export interface AxiosMocker {
start: () => void;
}
const axiosMocker: AxiosMocker = {
start: () => {
console.log("axiosMocker.start()");
adapter = new MockAdapter(axios, { delayResponse: 300 });
adapter.onAny().passThrough();
},
};
export const toMockPath = (path: string): RegExp => {
if (path.endsWith(".json") || path.endsWith(".csv")) {
if (!path.endsWith("vername.json")) {
const ext = "." + path.substr(path.lastIndexOf(".") + 1);
const withVersion = path.replace(ext, `_v(.+)${ext}`);
return new RegExp(Utils.escapeRegExp(withVersion) + "$");
}
} else {
path = Utils.formatText(path, ".*");
return new RegExp("^" + Utils.escapeRegExp(path) + "$");
}
};
export default axiosMocker;