vbenjs / vite-plugin-mock

A mock plugin for vite.use mockjs.
MIT License
625 stars 96 forks source link

response 返回不支持异步逻辑 #48

Open dangjingtao opened 3 years ago

dangjingtao commented 3 years ago
module.exports = [
  {
    url: "/api/xxx/query",
    method: "get",
    response: async (req, res) => {
      const params = req.body;

      const list = await getFile('./xxx/extend.js');

      const ret = {
        code: 'SUCCESS',
        data: {
          list
        },
        message: '查询接口成功',
        success: true,
      }

      return ret
    },
  },
]

以上代码不支持从持久储存(extend.js文件)中异步读取接口信息。返回为{}。 看源码发现requestMiddleware中定义返回部分,尝试对response定义await:

const mockResponse = isFunction(response) ? **await** response.bind(self)({ url: req.url, body, query, headers: req.headers }) : response;

可以达到预期返回,求教这样修改是否有影响?如无影响,是否考虑在下个版本中支持异步写法

Broderick890 commented 1 year ago

+1