-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiMock.js
51 lines (48 loc) · 1.67 KB
/
apiMock.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const fs = require("fs");
const path = require("path");
const chokidar = require("chokidar");
const { pathToRegexp } = require("path-to-regexp");
const mocker = {};
const cacheUrlInFile = {};
// “?:”非获取匹配,匹配冒号后的内容但不获取匹配结果,不进行存储供以后使用。
// pathToRegexp('/api/users/:id') => /^\/api\/users(?:\/([^\/#\?]+?))[\/#\?]?$/i
const getMockerKey = (url, method) => {
return Object.keys(mocker).find((key) => {
return pathToRegexp(key.replace(new RegExp(`^${method} `, "i"), "")).exec(
url
);
});
};
module.exports.apiMock = (app, dir) => {
app.all("/*", (req, res, next) => {
const { path, method } = req;
const mockerKey = getMockerKey(path, method);
if (mockerKey && mocker[mockerKey]) {
return res.send(mocker[mockerKey]);
}
next();
});
chokidar.watch(dir).on("all", (event, curPath) => {
if (event === "unlink") {
console.log("删除mock文件:", curPath);
const toDeleteArr = cacheUrlInFile[curPath];
toDeleteArr.forEach((item) => {
delete mocker[item];
});
} else if (["add", "change"].includes(event)) {
if (fs.statSync(curPath).isFile()) {
console.log(`${event === "add" ? "新增" : "修改"}mock文件:`, curPath);
try {
delete require.cache[path.resolve(curPath)];
const module = require(curPath);
Object.assign(mocker, module);
Object.assign(cacheUrlInFile, {
[curPath]: Object.keys(module),
});
} catch (err) {
console.warn(`读取mock文件${curPath}失败,请按照规则刷新文件内容`);
}
}
}
});
};