This repository has been archived by the owner on Mar 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.js
161 lines (157 loc) · 4.87 KB
/
api.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var xhook = require('xhook');
var users =
[
{
id: 1,
name: 'Mr. Test',
password: 'test',
email: 'test@test.com',
rememberMe: false
}
];
var posts =
[
{
id: 1,
title: 'What is the facepalm?',
body: 'A facepalm is the physical gesture of placing one’s hand across one’s face or lowering one’s face into one’s hand or hands, covering or closing one’s eyes.',
comments:
[{
id: 1,
postId: 1,
email: 'facepalm@test.com',
body: 'Thanks for this information. I would just like to mention here the double facepalm, which is similar to the facepalm but performed with two hands. Keep up the good work!'
},
{
id: 2,
postId: 1,
email: 'info@spam.org',
body: 'Buy potatoes online. Delivery next day.'
}]
},
{
id: 2,
title: 'Online use of the facepalm',
body: 'Use of the gesture is not limited to visual representations. Often just the word, facepalm, is used to show someone’s disapproval or embarrassment.',
comments: []
}
];
var userId = 1;
var postId = 2;
var commentId = 0;
var delay = 400;
xhook.before(function(request, callback) {
if (request.url.endsWith('auth/register') && 'POST' === request.method) {
setTimeout(function() {
var params = JSON.parse(request.body);
params.id = ++userId;
users.push(params);
var user = {
id: params.id,
name: params.name,
username: params.username,
email: params.email,
phoneNumber: params.phoneNumber,
rememberMe: false
};
callback({
status: 200,
data: JSON.stringify({ status: 'success', user: user }),
headers: { 'Content-Type': 'application/json' }
});
}, delay);
}
else if (request.url.endsWith('auth/login') && 'POST' === request.method) {
setTimeout(function() {
var params = JSON.parse(request.body),
filtered = users.filter(function(user) {
return user.email === params.email && user.password === params.password;
});
if (filtered.length > 0) {
var user = filtered[0];
user.rememberMe = params.rememberMe;
var response = { session: { user: user } };
callback({
status: 200,
data: JSON.stringify(response),
headers: { 'Content-Type': 'application/json' }
});
} else {
console.log('401 Unauthorized');
callback({
status: 401,
data: JSON.stringify({ error: 'Unauthorized' }),
headers: { 'Content-Type': 'application/json' }
});
}
}, delay);
} else if (request.url.endsWith('posts')) {
if ('GET' === request.method) {
setTimeout(function() {
callback({
status: 200,
data: JSON.stringify({ posts: posts.slice().reverse() }),
headers: { 'Content-Type': 'application/json' }
});
}, delay);
} else if ('POST' === request.method) {
setTimeout(function() {
var post = JSON.parse(request.body);
post.id = ++postId;
post.comments = [];
posts.push(post);
callback({
status: 200,
data: JSON.stringify({ post: post }),
headers: { 'Content-Type': 'application/json' }
});
}, delay);
}
} else if (/posts\/\d+$/.test(request.url) && 'GET' === request.method) {
setTimeout(function() {
var id = request.url.match(/posts\/(\d+)$/)[1],
filtered = posts.filter(function(post) { return post.id == id; });
if (filtered.length > 0) {
var response = filtered[0];
callback({
status: 200,
data: JSON.stringify({ post: response }),
headers: { 'Content-Type': 'application/json' }
});
} else {
console.log('Not Found');
callback({
status: 404,
data: JSON.stringify({ error: 'Not Found' }),
headers: { 'Content-Type': 'application/json' }
});
}
}, delay);
} else if (/posts\/\d+\/comments$/.test(request.url) && 'POST' === request.method) {
setTimeout(function() {
var comment = JSON.parse(request.body),
postId = request.url.match(/posts\/(\d+)\/comments$/)[1],
filtered = posts.filter(function(post) { return post.id == postId; });
if (filtered.length > 0) {
var post = filtered[0];
post.comments = post.comments || [];
comment.id = ++commentId;
post.comments.unshift(comment);
callback({
status: 200,
data: JSON.stringify({ post: post, comment: comment }),
headers: { 'Content-Type': 'application/json' }
});
} else {
console.log('Not Found');
callback({
status: 404,
data: JSON.stringify({ error: 'Not Found' }),
headers: { 'Content-Type': 'application/json' }
});
}
}, delay);
} else {
callback();
}
});