const utils = require("./utils");

function mocker(middlewares, { app }) {
  app.use(async function middleware(req, res, next) {
    const mockConfig = utils.requireUncached("./mock.config");
    if (mockConfig.enable) {
      const path = req.path.split("?")[0];
      if (mockConfig.api[path]) {
        console.log("Mocker enter");
        console.log("mock:", req.path);
        const data = await mockConfig.api[path](req);
        data._debug = {
          msg: "这是一个mock数据",
        };
        res.json(data);
        return;
      }
    }
    next();
  });
  return middlewares;
}

module.exports = mocker;
