加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
index.js 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
孟帅 提交于 2021-01-09 11:51 . Sat Jan 9 11:51:16 CST 2021
const Mock = require('mockjs')
const { param2Obj } = require('./utils')
const user = require('./user')
const country = require('./country')
const customer = require('./customer')
const event = require('./event')
const node = require('./node')
const photo = require('./photo')
const products = require('./products')
const mocks = [
...user,
...products,
...country,
...customer,
...event,
...node,
...photo
]
// 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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化