Skip to content

Commit

Permalink
perf[Mock]: set responseFake to mock-server.js
Browse files Browse the repository at this point in the history
  • Loading branch information
潘嘉晨 committed Jun 4, 2020
1 parent 95c7bf5 commit d0518bc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
16 changes: 1 addition & 15 deletions mock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,4 @@ export function mockXHR() {
}
}

// 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))
}
}
}

export default mocks.map(route => {
return responseFake(route.url, route.type, route.response)
})
export default mocks
20 changes: 18 additions & 2 deletions mock/mock-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ 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 { default: mocks } = require('./index.js')
for (const mock of mocks) {
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(mocks).length
const mockRoutesLength = Object.keys(mocksForServer).length
return {
mockRoutesLength: mockRoutesLength,
mockStartIndex: mockLastIndex - mockRoutesLength
Expand All @@ -27,6 +31,18 @@ function unregisterRoutes() {
})
}

// 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 => {
// es6 polyfill
require('@babel/register')
Expand Down

0 comments on commit d0518bc

Please sign in to comment.