-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
633 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
NODE_ENV = 'development' | ||
BASE_URL = '/' | ||
VUE_APP_BASE_API = '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
NODE_ENV = 'production' | ||
BASE_URL = './' | ||
VUE_APP_BASE_API = 'https://easy-mock.com/' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import Mock from 'mockjs' | ||
|
||
const List = [] | ||
const count = 100 | ||
|
||
const baseContent = '<p>I am testing data, I am testing data.</p><p><img src="https://wpimg.wallstcn.com/4c69009c-0fd4-4153-b112-6cb53d1cf943"></p>' | ||
const imageUri = 'https://wpimg.wallstcn.com/e4558086-631c-425c-9430-56ffb46e70b3' | ||
|
||
for (let i = 0; i < count; i++) { | ||
List.push(Mock.mock({ | ||
id: '@increment', | ||
timestamp: +Mock.Random.date('T'), | ||
author: '@first', | ||
reviewer: '@first', | ||
title: '@title(5, 10)', | ||
content_short: 'mock data', | ||
content: baseContent, | ||
forecast: '@float(0, 100, 2, 2)', | ||
importance: '@integer(1, 3)', | ||
'type|1': ['CN', 'US', 'JP', 'EU'], | ||
'status|1': ['published', 'draft', 'deleted'], | ||
display_time: '@datetime', | ||
comment_disabled: true, | ||
pageviews: '@integer(300, 5000)', | ||
imageUri, | ||
platforms: ['a-platform'] | ||
})) | ||
} | ||
|
||
export default [ | ||
{ | ||
url: '/article/list', | ||
type: 'get', | ||
response: config => { | ||
const { importance, type, title, page = 1, limit = 20, sort } = config.query | ||
|
||
let mockList = List.filter(item => { | ||
if (importance && item.importance !== +importance) return false | ||
if (type && item.type !== type) return false | ||
if (title && item.title.indexOf(title) < 0) return false | ||
return true | ||
}) | ||
|
||
if (sort === '-id') { | ||
mockList = mockList.reverse() | ||
} | ||
|
||
const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1)) | ||
|
||
return { | ||
code: 200, | ||
data: { | ||
total: mockList.length, | ||
items: pageList | ||
} | ||
} | ||
} | ||
}, | ||
|
||
{ | ||
url: '/article/detail', | ||
type: 'get', | ||
response: config => { | ||
const { id } = config.query | ||
for (const article of List) { | ||
if (article.id === +id) { | ||
return { | ||
code: 200, | ||
data: article | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
|
||
{ | ||
url: '/article/pv', | ||
type: 'get', | ||
response: _ => { | ||
return { | ||
code: 200, | ||
data: { | ||
pvData: [ | ||
{ key: 'PC', pv: 1024 }, | ||
{ key: 'mobile', pv: 1024 }, | ||
{ key: 'ios', pv: 1024 }, | ||
{ key: 'android', pv: 1024 } | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
|
||
{ | ||
url: '/article/create', | ||
type: 'post', | ||
response: _ => { | ||
return { | ||
code: 200, | ||
data: 'success' | ||
} | ||
} | ||
}, | ||
|
||
{ | ||
url: '/article/update', | ||
type: 'post', | ||
response: _ => { | ||
return { | ||
code: 200, | ||
data: 'success' | ||
} | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import Mock from 'mockjs' | ||
import { param2Obj } from '../src/utils' | ||
|
||
import user from './user' | ||
import article from './article' | ||
import search from './remote-search' | ||
|
||
const mocks = [ | ||
...user, | ||
...article, | ||
...search | ||
] | ||
|
||
// 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). | ||
export 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)) | ||
} | ||
} | ||
|
||
// for mock server | ||
const responseFake = (url, type, respond) => { | ||
return { | ||
url: new RegExp(`/mock${url}`), | ||
type: type || 'get', | ||
response (req, res) { | ||
res.json(Mock.mock(respond instanceof Function ? respond(req, res) : respond)) | ||
} | ||
} | ||
} | ||
|
||
export default mocks.map(route => { | ||
return responseFake(route.url, route.type, route.response) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
const chokidar = require('chokidar') | ||
const bodyParser = require('body-parser') | ||
const chalk = require('chalk') | ||
const path = require('path') | ||
|
||
const mockDir = path.join(process.cwd(), 'mock') | ||
|
||
function registerRoutes (app) { | ||
let mockLastIndex | ||
const { default: mocks } = require('./index.js') | ||
for (const mock of mocks) { | ||
app[mock.type](mock.url, mock.response) | ||
mockLastIndex = app._router.stack.length | ||
} | ||
const mockRoutesLength = Object.keys(mocks).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)] | ||
} | ||
}) | ||
} | ||
|
||
module.exports = app => { | ||
// es6 polyfill | ||
require('@babel/register') | ||
|
||
// 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)) | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import Mock from 'mockjs' | ||
|
||
const NameList = [] | ||
const count = 100 | ||
|
||
for (let i = 0; i < count; i++) { | ||
NameList.push(Mock.mock({ | ||
name: '@first' | ||
})) | ||
} | ||
NameList.push({ name: 'mock-Pan' }) | ||
|
||
export default [ | ||
// username search | ||
{ | ||
url: '/search/user', | ||
type: 'get', | ||
response: config => { | ||
const { name } = config.query | ||
const mockNameList = NameList.filter(item => { | ||
const lowerCaseName = item.name.toLowerCase() | ||
return !(name && lowerCaseName.indexOf(name.toLowerCase()) < 0) | ||
}) | ||
return { | ||
code: 20000, | ||
data: { items: mockNameList } | ||
} | ||
} | ||
}, | ||
|
||
// transaction list | ||
{ | ||
url: '/transaction/list', | ||
type: 'get', | ||
response: _ => { | ||
return { | ||
code: 20000, | ||
data: { | ||
total: 20, | ||
'items|20': [{ | ||
order_no: '@guid()', | ||
timestamp: +Mock.Random.date('T'), | ||
username: '@name()', | ||
price: '@float(1000, 15000, 0, 2)', | ||
'status|1': ['success', 'pending'] | ||
}] | ||
} | ||
} | ||
} | ||
} | ||
] |
Oops, something went wrong.