chenjun 2 ani în urmă
părinte
comite
3e873c0646
6 a modificat fișierele cu 0 adăugiri și 378 ștergeri
  1. 0 156
      mock/gateway.js
  2. 0 61
      mock/index.js
  3. 0 26
      mock/log.js
  4. 0 81
      mock/mock-server.js
  5. 0 29
      mock/table.js
  6. 0 25
      mock/utils.js

+ 0 - 156
mock/gateway.js

@@ -1,156 +0,0 @@
-const Mock = require('mockjs')
-
-const PROTOCOLS = ['http', 'https', 'MQ', 'FTP']
-const APITYPE = ['post', 'get', 'head', 'put', 'delete', 'options', 'trace', 'connect']
-const DATATYPE = ['number', 'string', 'boolean', 'array', 'object']
-
-module.exports = [
-  {
-    url: '/vue-admin-template/gateway/interface/list',
-    type: 'get',
-    response: config => {
-      const data = Mock.mock({
-        'items|11': [{
-          'id|+1': 1,
-          title: '@word(4, 10)',
-          description: '/api/@word(10,20)/@word(5,10)',
-          'protocol|1': PROTOCOLS,
-          flag: '@boolean'
-        }]
-      })
-      const items = data.items
-      return {
-        code: 20000,
-        data: {
-          items: items
-        }
-      }
-    }
-  },
-  {
-    url: '/vue-admin-template/gateway/limit/list',
-    type: 'get',
-    response: config => {
-      const data = Mock.mock({
-        'items|11': [{
-          'id|+1': 1,
-          title: '@word(4, 10)',
-          visitTimes: '@natural(0, 30000)',
-          visitDuration: '@natural(0, 100)',
-          inBlacklist: '@boolean',
-          flag: '@boolean'
-        }]
-      })
-      const items = data.items
-      return {
-        code: 20000,
-        data: {
-          items: items
-        }
-      }
-    }
-  },
-  {
-    url: '/vue-admin-template/gateway/circuit-break/list',
-    type: 'get',
-    response: config => {
-      const data = Mock.mock({
-        'items|11': [{
-          'id|+1': 1,
-          title: '@word(4, 10)',
-          circuitBreakDuration: '@natural(1, 100)',
-          timeoutPeriod: '@natural(1, 100)',
-          circuitBreakStandard: '@natural(1, 10)',
-          interfaceBinding: '@natural(0, 10)',
-          flag: '@boolean'
-        }]
-      })
-      const items = data.items
-      return {
-        code: 20000,
-        data: {
-          items: items
-        }
-      }
-    }
-  },
-  {
-    url: '/vue-admin-template/gateway/document/list',
-    type: 'get',
-    response: config => {
-      const data = Mock.mock({
-        'items|11': [{
-          'id|+1': 265912187337729,
-          name: '/api/@word(10,20)/@word(5,10)',
-          'protocol|1': PROTOCOLS,
-          'type|1': APITYPE,
-          time: '@datetime("yyyy/MM/dd HH:mm:ss")',
-          method() {
-            return this.name
-          },
-          description() {
-            return this.name
-          },
-          params: {
-            'input|0-2': [
-              {
-                name: '@word(4,10)',
-                'type|1': DATATYPE,
-                length: '@natural(1,100)',
-                default() {
-                  switch (this.type) {
-                    case 'number':
-                      return 1
-                    case 'string':
-                      return 'AAA'
-                    case 'boolean':
-                      return 'true'
-                    case 'array':
-                      return '[]'
-                    case 'object':
-                      return '{}'
-                  }
-                },
-                reference: '无',
-                required: '@boolean',
-                superior: '',
-                description: '@csentence'
-              }
-            ],
-            'output|0-2': [
-              {
-                name: '@word(4,10)',
-                'type|1': DATATYPE,
-                length: '@natural(1,100)',
-                default() {
-                  switch (this.type) {
-                    case 'number':
-                      return 1
-                    case 'string':
-                      return 'AAA'
-                    case 'boolean':
-                      return 'true'
-                    case 'array':
-                      return '[]'
-                    case 'object':
-                      return '{}'
-                  }
-                },
-                reference: '无',
-                superior: '',
-                description: '@csentence'
-              }
-            ]
-          }
-        }]
-      })
-      const items = data.items
-      return {
-        code: 20000,
-        data: {
-          items: items
-        }
-      }
-    }
-  }
-]

+ 0 - 61
mock/index.js

@@ -1,61 +0,0 @@
-const Mock = require('mockjs')
-const { param2Obj } = require('./utils')
-
-const user = require('./user')
-const table = require('./table')
-const log = require('./log')
-const gateway = require('./gateway')
-
-const mocks = [
-  ...user,
-  ...table,
-  ...log,
-  ...gateway
-]
-
-// for front mock
-// please use it cautiously, it will redefine XMLHttpRequest,
-// which will cause many of your third-party libraries to be invalidated(like progress event).
-function mockXHR() {
-  // mock patch
-  // https://github.com/nuysoft/Mock/issues/300
-  Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send
-  Mock.XHR.prototype.send = function() {
-    if (this.custom.xhr) {
-      this.custom.xhr.withCredentials = this.withCredentials || false
-
-      if (this.responseType) {
-        this.custom.xhr.responseType = this.responseType
-      }
-    }
-    this.proxy_send(...arguments)
-  }
-
-  function XHR2ExpressReqWrap(respond) {
-    return function(options) {
-      let result = null
-      if (respond instanceof Function) {
-        const { body, type, url } = options
-        // https://expressjs.com/en/4x/api.html#req
-        result = respond({
-          method: type,
-          body: JSON.parse(body),
-          query: param2Obj(url)
-        })
-      } else {
-        result = respond
-      }
-      return Mock.mock(result)
-    }
-  }
-
-  for (const i of mocks) {
-    Mock.mock(new RegExp(i.url), i.type || 'get', XHR2ExpressReqWrap(i.response))
-  }
-}
-
-module.exports = {
-  mocks,
-  mockXHR
-}
-

+ 0 - 26
mock/log.js

@@ -1,26 +0,0 @@
-const Mock = require('mockjs')
-
-module.exports = [
-  {
-    url: '/vue-admin-template/log/list',
-    type: 'get',
-    response: config => {
-      const data = Mock.mock({
-        'items|9': [{
-          content: '@csentence',
-          category: '@word(2, 3)',
-          level: '@integer(1, 3)',
-          time: '@datetime'
-        }]
-      })
-      const items = data.items
-      return {
-        code: 20000,
-        data: {
-          total: 9 * 10,
-          items: items
-        }
-      }
-    }
-  }
-]

+ 0 - 81
mock/mock-server.js

@@ -1,81 +0,0 @@
-const chokidar = require('chokidar')
-const bodyParser = require('body-parser')
-const chalk = require('chalk')
-const path = require('path')
-const Mock = require('mockjs')
-
-const mockDir = path.join(process.cwd(), 'mock')
-
-function registerRoutes(app) {
-  let mockLastIndex
-  const { mocks } = require('./index.js')
-  const mocksForServer = mocks.map(route => {
-    return responseFake(route.url, route.type, route.response)
-  })
-  for (const mock of mocksForServer) {
-    app[mock.type](mock.url, mock.response)
-    mockLastIndex = app._router.stack.length
-  }
-  const mockRoutesLength = Object.keys(mocksForServer).length
-  return {
-    mockRoutesLength: mockRoutesLength,
-    mockStartIndex: mockLastIndex - mockRoutesLength
-  }
-}
-
-function unregisterRoutes() {
-  Object.keys(require.cache).forEach(i => {
-    if (i.includes(mockDir)) {
-      delete require.cache[require.resolve(i)]
-    }
-  })
-}
-
-// for mock server
-const responseFake = (url, type, respond) => {
-  return {
-    url: new RegExp(`${process.env.VUE_APP_BASE_API}${url}`),
-    type: type || 'get',
-    response(req, res) {
-      console.log('request invoke:' + req.path)
-      res.json(Mock.mock(respond instanceof Function ? respond(req, res) : respond))
-    }
-  }
-}
-
-module.exports = app => {
-  // parse app.body
-  // https://expressjs.com/en/4x/api.html#req.body
-  app.use(bodyParser.json())
-  app.use(bodyParser.urlencoded({
-    extended: true
-  }))
-
-  const mockRoutes = registerRoutes(app)
-  var mockRoutesLength = mockRoutes.mockRoutesLength
-  var mockStartIndex = mockRoutes.mockStartIndex
-
-  // watch files, hot reload mock server
-  chokidar.watch(mockDir, {
-    ignored: /mock-server/,
-    ignoreInitial: true
-  }).on('all', (event, path) => {
-    if (event === 'change' || event === 'add') {
-      try {
-        // remove mock routes stack
-        app._router.stack.splice(mockStartIndex, mockRoutesLength)
-
-        // clear routes cache
-        unregisterRoutes()
-
-        const mockRoutes = registerRoutes(app)
-        mockRoutesLength = mockRoutes.mockRoutesLength
-        mockStartIndex = mockRoutes.mockStartIndex
-
-        console.log(chalk.magentaBright(`\n > Mock Server hot reload success! changed  ${path}`))
-      } catch (error) {
-        console.log(chalk.redBright(error))
-      }
-    }
-  })
-}

+ 0 - 29
mock/table.js

@@ -1,29 +0,0 @@
-const Mock = require('mockjs')
-
-const data = Mock.mock({
-  'items|30': [{
-    id: '@id',
-    title: '@sentence(10, 20)',
-    'status|1': ['published', 'draft', 'deleted'],
-    author: 'name',
-    display_time: '@datetime',
-    pageviews: '@integer(300, 5000)'
-  }]
-})
-
-module.exports = [
-  {
-    url: '/vue-admin-template/table/list',
-    type: 'get',
-    response: config => {
-      const items = data.items
-      return {
-        code: 20000,
-        data: {
-          total: items.length,
-          items: items
-        }
-      }
-    }
-  }
-]

+ 0 - 25
mock/utils.js

@@ -1,25 +0,0 @@
-/**
- * @param {string} url
- * @returns {Object}
- */
-function param2Obj(url) {
-  const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
-  if (!search) {
-    return {}
-  }
-  const obj = {}
-  const searchArr = search.split('&')
-  searchArr.forEach(v => {
-    const index = v.indexOf('=')
-    if (index !== -1) {
-      const name = v.substring(0, index)
-      const val = v.substring(index + 1, v.length)
-      obj[name] = val
-    }
-  })
-  return obj
-}
-
-module.exports = {
-  param2Obj
-}