requestUrl: 要攔截的URL,字符串或正則表達式
equestType: 要攔截的請求類型,get/post/put/delete/options...
template: 數據模板
function(options):生成響應數據的函數,options --> { url, type, body }javascript
數據模板中每一個屬性由3部分組成: 屬性名|生成規則:屬性值java
屬性值中能夠包含@佔位符
屬性值還指定了最終值的初始值和類型git
經過重複 string 生成一個字符串,重複次數大於等於 min,小於等於 maxweb
經過重複 string 生成一個字符串,重複次數等於 count正則表達式
屬性值自動加 1,初始值爲 numberjson
生成一個大於等於 min、小於等於 max 的整數,屬性值 number 只是用來肯定類型api
生成一個浮點數,整數部分大於等於 min、小於等於 max,小數部分保留 dmin 到 dmax 位數組
隨機生成一個布爾值,值爲 true 的機率是 1/2,值爲 false 的機率一樣是 1/2app
隨機生成一個布爾值,值爲 value 的機率是 min / (min + max),值爲 !value 的機率是 max / (min + max)dom
從屬性值 object 中隨機選取 count 個屬性
從屬性值 object 中隨機選取 min 到 max 個屬性
從屬性值 array 中隨機選取 1 個元素,做爲最終值
從屬性值 array 中順序選取 1 個元素,做爲最終值
經過重複屬性值 array 生成一個新數組,重複次數大於等於 min,小於等於 max
經過重複屬性值 array 生成一個新數組,重複次數爲 count
執行函數 function,取其返回值做爲最終的屬性值,函數的上下文爲屬性 'name' 所在的對象
根據正則表達式 regexp 反向生成能夠匹配它的字符串。用於生成自定義格式的字符串
配置Ajax請求的行爲,暫時支持的配置項有timeout
Mock.setup({ timeout: 500 }) Mock.setup({ timeout: '100-600' })
const Random = Mock.Random Random.email() // => sd.sdf@oksd.com Mock.mock('@email') // => sd.sdf@oksd.com Mock.mock({ email: 'sd.sdf@oksd.com' }) // => { email: "sd.sdf@oksd.com" }
Type | Method |
---|---|
Basic | boolean, natural, integer, float, character, string, range, date, time, datetime, now |
Image | image, dataImage |
Color | color |
Text | paragraph, sentence, word, title, cparagraph, csentence, cword, ctitle |
Name | first, last, name, cfirst, clast, cname |
Web | url, domain, email, ip, tld |
Address | area(region, province, city(bool), county(bool), zip), region |
Helper | capitalize(首字母大寫), upper(大寫), lower(小寫), pick(從數組任取一個), shuffle(打亂數組元素順序) |
Miscellaneous | guid, id |
Format | Description | Example |
---|---|---|
yyyy | A full numeric representation of a year, 4 digits | 1999 or 2003 |
yy | A two digit representation of a year | 99 or 03 |
y | A two digit representation of a year | 99 or 03 |
MM | Numeric representation of a month, with leading zeros | 01 to 12 |
M | Numeric representation of a month, without leading zeros | 1 to 12 |
dd | Day of the month, 2 digits with leading zeros | 01 to 31 |
d | Day of the month without leading zeros | 1 to 31 |
HH | 24-hour format of an hour with leading zeros | 00 to 23 |
H | 24-hour format of an hour without leading zeros | 0 to 23 |
hh | 12-hour format of an hour without leading zeros | 01 to 12 |
h | 12-hour format of an hour with leading zeros | 1 to 12 |
mm | Minutes, with leading zeros | 00 to 59 |
m | Minutes, without leading zeros | 0 to 59 |
ss | Seconds, with leading zeros | 00 to 59 |
s | Seconds, without leading zeros | 0 to 59 |
SS | Milliseconds, with leading zeros | 000 to 999 |
S | Milliseconds, without leading zeros | 0 to 999 |
A | Uppercase Ante meridiem and Post meridiem | AM or PM |
a | Lowercase Ante meridiem and Post meridiem | am or pm |
T | Milliseconds, since 1970-1-1 00:00:00 UTC | 759883437303 |
Random.extend({ fruit () { const fruit = ['apple', 'peach', 'lemon'] return this.pick(fruit) } }) Random.fruit() // => 'peach' Mock.mock('@fruit') // => 'lemon' Mock.mock({ fruit: '@fruit' // => 'peach' })