mock.js
1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* global require, module, window */
var Handler = require('./mock/handler')
var Util = require('./mock/util')
var Random = require('./mock/random')
var RE = require('./mock/regexp')
var toJSONSchema = require('./mock/schema')
var valid = require('./mock/valid')
var XHR
if (typeof window !== 'undefined') XHR = require('./mock/xhr')
/*!
Mock - 模拟请求 & 模拟数据
https://github.com/nuysoft/Mock
墨智 mozhi.gyy@taobao.com nuysoft@gmail.com
*/
var Mock = {
Handler: Handler,
Random: Random,
Util: Util,
XHR: XHR,
RE: RE,
toJSONSchema: toJSONSchema,
valid: valid,
heredoc: Util.heredoc,
setup: function(settings) {
return XHR.setup(settings)
},
_mocked: {}
}
Mock.version = '1.0.1-beta3'
// 避免循环依赖
if (XHR) XHR.Mock = Mock
/*
* Mock.mock( template )
* Mock.mock( function() )
* Mock.mock( rurl, template )
* Mock.mock( rurl, function(options) )
* Mock.mock( rurl, rtype, template )
* Mock.mock( rurl, rtype, function(options) )
根据数据模板生成模拟数据。
*/
Mock.mock = function(rurl, rtype, template) {
// Mock.mock(template)
if (arguments.length === 1) {
return Handler.gen(rurl)
}
// Mock.mock(rurl, template)
if (arguments.length === 2) {
template = rtype
rtype = undefined
}
// 拦截 XHR
if (XHR) window.XMLHttpRequest = XHR
Mock._mocked[rurl + (rtype || '')] = {
rurl: rurl,
rtype: rtype,
template: template
}
return Mock
}
module.exports = Mock