UNPKG

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