Browse Source

refactor: 移除冗余代码

wll8 5 months ago
parent
commit
40ff42d7bd
3 changed files with 16 additions and 85 deletions
  1. 0 23
      mm/api/index.js
  2. 16 43
      mm/mm.config.js
  3. 0 19
      mm/util.js

+ 0 - 23
mm/api/index.js

@@ -1,23 +0,0 @@
-const { wrapApiData } = require(`../util.js`)
-
-/** @type {import('mockm/@types/config').Config} */
-module.exports = (util) => {
-  const {
-    libObj: { mockjs },
-  } = util
-  return {
-    api: {
-      // 创建接口并使用 mockjs 生成数据
-      'get /api/test': wrapApiData(
-        mockjs.mock({
-          'data|3-7': [
-            {
-              userId: `@id`,
-              userName: `@cname`,
-            },
-          ],
-        })
-      ),
-    },
-  }
-}

+ 16 - 43
mm/mm.config.js

@@ -1,6 +1,3 @@
-const api = require(`./api/index.js`)
-const { wrapApiData } = require(`./util.js`)
-
 /**
  * 配置说明请参考文档:
  * https://hongqiye.com/doc/mockm/config/option.html
@@ -30,8 +27,6 @@ module.exports = async (util) => {
       '/anything/intercept': [`origin`, `127.0.0.1`], // 修改接口返回的数据
     },
     api: {
-      // 在其他文件里的 api
-      ...api(util).api,
       'get /api/currentWeather': util.side({
         tags: [`公用`],
         summary: `当前天气`,
@@ -365,44 +360,6 @@ module.exports = async (util) => {
           )
         },
       }),
-      // 当为基本数据类型时, 直接返回数据, 这个接口返回 {"msg":"ok"}
-      '/api/1': { msg: `ok` },
-
-      // 也可以像 express 一样返回数据
-      '/api/2'(req, res) {
-        res.send({ msg: `ok` })
-      },
-
-      // 一个只能使用 post 方法访问的接口
-      'post /api/3': { msg: `ok` },
-
-      // // 一个 websocket 接口, 会发送收到的消息
-      // 'ws /api/4' (ws, req) {
-      //   ws.on('message', (msg) => ws.send(msg))
-      // },
-
-      // 一个下载文件的接口
-      '/file'(req, res) {
-        res.download(__filename, `mm.config.js`)
-      },
-
-      // 获取动态的接口路径的参数 code
-      '/status/:code'(req, res) {
-        res.json({ statusCode: req.params.code })
-      },
-
-      // 使用 mockjs 生成数据
-      '/user'(req, res) {
-        const json = util.libObj.mockjs.mock({
-          'data|3-7': [
-            {
-              userId: `@id`,
-              userName: `@cname`,
-            },
-          ],
-        })
-        res.json(json)
-      },
     },
     static: [],
     dbCover: true,
@@ -445,3 +402,19 @@ module.exports = async (util) => {
       wrapApiData({ code, data }),
   }
 }
+
+/**
+ * 包裹 api 的返回值
+ * @param {*} param0
+ * @param {object} param0.data - 原始数据
+ * @param {number|string} [param0.code=200] - http状态码
+ * @returns
+ */
+function wrapApiData({ data, code = 200 }) {
+  code = String(code)
+  return {
+    code,
+    success: Boolean(code.match(/^[2]/)), // 如果状态码以2开头则为 true
+    data,
+  }
+}

+ 0 - 19
mm/util.js

@@ -1,19 +0,0 @@
-/**
- * 包裹 api 的返回值
- * @param {*} param0
- * @param {object} param0.data - 原始数据
- * @param {number|string} [param0.code=200] - http状态码
- * @returns
- */
-function wrapApiData({ data, code = 200 }) {
-  code = String(code)
-  return {
-    code,
-    success: Boolean(code.match(/^[2]/)), // 如果状态码以2开头则为 true
-    data,
-  }
-}
-
-module.exports = {
-  wrapApiData,
-}