小说后台管理系统
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

61 righe
1.5 KiB

  1. const Mock = require('mockjs')
  2. const { param2Obj } = require('./utils')
  3. const user = require('./user')
  4. const role = require('./role')
  5. const article = require('./article')
  6. const search = require('./remote-search')
  7. const mocks = [
  8. ...user,
  9. ...role,
  10. ...article,
  11. ...search
  12. ]
  13. // for front mock
  14. // please use it cautiously, it will redefine XMLHttpRequest,
  15. // which will cause many of your third-party libraries to be invalidated(like progress event).
  16. function mockXHR() {
  17. // mock patch
  18. // https://github.com/nuysoft/Mock/issues/300
  19. Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send
  20. Mock.XHR.prototype.send = function() {
  21. if (this.custom.xhr) {
  22. this.custom.xhr.withCredentials = this.withCredentials || false
  23. if (this.responseType) {
  24. this.custom.xhr.responseType = this.responseType
  25. }
  26. }
  27. this.proxy_send(...arguments)
  28. }
  29. function XHR2ExpressReqWrap(respond) {
  30. return function(options) {
  31. let result = null
  32. if (respond instanceof Function) {
  33. const { body, type, url } = options
  34. // https://expressjs.com/en/4x/api.html#req
  35. result = respond({
  36. method: type,
  37. body: JSON.parse(body),
  38. query: param2Obj(url)
  39. })
  40. } else {
  41. result = respond
  42. }
  43. return Mock.mock(result)
  44. }
  45. }
  46. for (const i of mocks) {
  47. Mock.mock(new RegExp(i.url), i.type || 'get', XHR2ExpressReqWrap(i.response))
  48. }
  49. }
  50. module.exports = {
  51. mocks,
  52. mockXHR
  53. }