-
-
Notifications
You must be signed in to change notification settings - Fork 30.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(mock): mak mock api compatible with both web-view and webpac…
…k server 1. 把 Mockjs 功能移到 server 端中间件,同时也兼容前端直接劫持 XHR 2. dev 环境下默认作为 express 中间件通过 webpack server 提供 mock api 3. prod 构建时,默认在前端用 Mockjs 劫持 XHR benefits: - dev 开发调试时能直接看到 XHR 请求,方便调试网络,能和后端对接联调 - 避开在开发时因为 Mockjs 引起的网络 bug - prod 构建时劫持 XHR,保证本项目的 Github Pages preview 能正常显示 (逻辑和 error-log 一样) - 前后台使用的 mock 是同一份代码,不会增加维护负担 ref: [#562](#562 (comment))
- Loading branch information
Showing
14 changed files
with
136 additions
and
92 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 |
---|---|---|
@@ -1 +1 @@ | ||
VUE_APP_BASE_API = 'https://api-dev' | ||
VUE_APP_BASE_API = '/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 |
---|---|---|
@@ -1 +1 @@ | ||
VUE_APP_BASE_API = 'https://api-prod' | ||
VUE_APP_BASE_API = '/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
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,50 @@ | ||
import Mock from 'mockjs' | ||
import mocks from './mocks' | ||
import { param2Obj } from '../src/utils' | ||
|
||
const MOCK_API_BASE = '/mock' | ||
|
||
export function mockXHR() { | ||
// 修复在使用 MockJS 情况下,设置 withCredentials = true,且未被拦截的跨域请求丢失 Cookies 的问题 | ||
// 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 | ||
} | ||
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 [route, respond] of Object.entries(mocks)) { | ||
Mock.mock(new RegExp(`${route}`), XHR2ExpressReqWrap(respond)) | ||
} | ||
} | ||
|
||
const responseFake = (route, respond) => ( | ||
{ | ||
route: new RegExp(`${MOCK_API_BASE}${route}`), | ||
response(req, res) { | ||
res.json(Mock.mock(respond instanceof Function ? respond(req, res) : respond)) | ||
} | ||
} | ||
) | ||
|
||
export default Object.keys(mocks).map(route => responseFake(route, mocks[route])) |
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
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,12 @@ | ||
import login from './login' | ||
import article from './article' | ||
import search from './remoteSearch' | ||
import transaction from './transaction' | ||
|
||
export default { | ||
...login, | ||
...article, | ||
...search, | ||
...transaction | ||
} | ||
|
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
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,16 @@ | ||
import Mock from 'mockjs' | ||
|
||
const count = 20 | ||
|
||
export default { | ||
'/transaction/list': { | ||
total: count, | ||
[`items|${count}`]: [{ | ||
order_no: '@guid()', | ||
timestamp: +Mock.Random.date('T'), | ||
username: '@name()', | ||
price: '@float(1000, 15000, 0, 2)', | ||
'status|1': ['success', 'pending'] | ||
}] | ||
} | ||
} |
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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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