_mock.js
1.9 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const PAGE_SIZE = 3;
let data = [
{
id: 1,
name: 'Leanne Graham',
email: 'Sincere@april.biz',
website: 'hildegard.org',
},
{
id: 2,
name: 'Ervin Howell',
email: 'Shanna@melissa.tv',
website: 'anastasia.net',
},
{
id: 3,
name: 'Clementine Bauch',
email: 'Nathan@yesenia.net',
website: 'ramiro.info',
},
{
id: 4,
name: 'Patricia Lebsack',
email: 'Julianne.OConner@kory.org',
website: 'kale.biz',
},
{
id: 5,
name: 'Chelsey Dietrich',
email: 'Lucio_Hettinger@annie.ca',
website: 'demarco.info',
},
{
id: 6,
name: 'Mrs. Dennis Schulist',
email: 'Karley_Dach@jasper.info',
website: 'ola.org',
},
{
id: 7,
name: 'Kurtis Weissnat',
email: 'Telly.Hoeger@billy.biz',
website: 'elvis.io',
},
{
id: 8,
name: 'Nicholas Runolfsdottir V',
email: 'Sherwood@rosamond.me',
website: 'jacynthe.com',
},
{
id: 9,
name: 'Glenna Reichert',
email: 'Chaim_McDermott@dana.io',
website: 'conrad.com',
},
];
function uid(len) {
len = len || 7;
return Math.random()
.toString(35)
.substr(2, len);
}
function getData(page) {
const start = (page - 1) * PAGE_SIZE;
return {
status: 'success',
total: data.length,
page,
data: data.slice(start, start + 3),
};
}
export default {
'GET /api/BLOCK_NAME': (req, res) => {
res.json(getData(parseInt(req.query.page, 10) || 1));
},
'DELETE /api/BLOCK_NAME/:id': (req, res) => {
data = data.filter(item => `${item.id}` !== `${req.params.id}`);
res.end('ok');
},
'PATCH /api/BLOCK_NAME/:id': (req, res) => {
data.forEach(item => {
if (`${item.id}` === `${req.params.id}`) {
Object.assign(item, req.body);
}
});
res.end('ok');
},
'POST /api/BLOCK_NAME': (req, res) => {
data.push({
...req.body,
id: uid(),
});
res.end('ok');
},
};