/
server.js
190 lines (165 loc) · 5.96 KB
/
server.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* eslint-disable @typescript-eslint/no-var-requires */
// eslint-disable-next-line node/no-unpublished-require
const jsonServer = require('json-server');
const fs = require('fs');
const path = require('path');
const https = require('https');
const server = jsonServer.create();
// Setup a static file server using the root of the project
const middlewares = jsonServer.defaults({ static: __dirname });
server.use(middlewares);
// Manifest and websdk
// =============================================================================
server.get('/fixtures/v1/apps/:id/websdk', (req, res) => {
const manifest = fs.readFileSync(path.join(__dirname, 'server', 'mocks', req.params.id, 'manifest-en.json'), {
encoding: 'utf-8',
});
const sdkFile = fs.readFileSync(path.join(__dirname, 'server', 'sdk.js'), { encoding: 'utf-8' });
const sdkFileModified = sdkFile
.replace('const manifest = {};', `const manifest = ${manifest};`)
.replace("id: 'REPLACEME',", `id: '${req.params.id}',`);
res.header('Content-Type', 'application/javascript');
res.send(sdkFileModified);
});
server.get('/api/v1/apps/:id/manifest', (req, res) => {
const language = req.query.locale ?? 'en';
const filePathPrefix = path.join(__dirname, 'server', 'mocks', req.params.id);
res.header('Content-Type', 'application/json');
if (fs.existsSync(path.join(filePathPrefix, `manifest-${language}.json`))) {
res.sendFile(path.join(filePathPrefix, `manifest-${language}.json`));
} else {
res.sendFile(path.join(filePathPrefix, `manifest-en.json`));
}
});
// To handle POST, PUT and PATCH you need to use a body-parser
// You can use the one used by JSON Server
server.use(jsonServer.bodyParser);
// People
// =============================================================================
server.put('/api/people/identify', (req, res, _) => {
const { person } = req.body;
const uniqueId = Date.now() * 3;
res.header('Content-Type', 'application/json');
res.send({
id: uniqueId,
name: person.name ?? null,
email: person.email ?? null,
phone_number: person.phone_number ?? null,
address: person.address ?? null,
birthday: person.birthday ?? null,
custom_data: person.custom_data ?? null,
});
});
server.put('/api/people', (req, res, _) => {
const { person } = req.body;
const uniqueId = Date.now() * 3;
res.header('Content-Type', 'application/json');
res.send({
id: uniqueId,
name: person.name ?? null,
email: person.email ?? null,
phone_number: person.phone_number ?? null,
address: person.address ?? null,
birthday: person.birthday ?? null,
custom_data: person.custom_data ?? null,
});
});
// Conversation
// =============================================================================
server.post('/api/conversation', (req, res, _) => {
const timestamp = Date.now();
const { app_release: appRelease, sdk } = req.body;
const authToken = req.headers?.authorization?.split('OAuth ')[1] ?? 'MOCK_TOKEN';
res.header('Content-Type', 'application/json');
res.status(201);
res.send({
state: 'new',
id: timestamp,
device_id: timestamp + 1,
person_id: timestamp + 2,
sdk,
app_release: appRelease,
token: authToken,
});
});
server.put('/api/conversation', (req, res, _) => {
const timestamp = Date.now();
const { app_release: appRelease, sdk } = req.body;
const authToken = req.headers?.authorization?.split('OAuth ')[1] ?? 'MOCK_TOKEN';
res.header('Content-Type', 'application/json');
res.status(201);
res.send({
state: 'new',
id: timestamp,
device_id: timestamp + 1,
person_id: timestamp + 2,
sdk,
app_release: appRelease,
token: authToken,
});
});
// Devices
// =============================================================================
server.put('/api/devices', (req, res, _) => {
const { device } = req.body;
res.header('Content-Type', 'application/javascript');
res.status(201);
res.send({
...device,
});
});
// Events
// =============================================================================
server.post('/api/events', (req, res, _) => {
res.header('Content-Type', 'application/javascript');
res.status(201);
res.send({});
});
// Messages
// =============================================================================
server.post('/api/messages', (req, res, _) => {
res.header('Content-Type', 'application/javascript');
res.status(201);
res.send();
});
// Uncomment to test error scenario on submitting in message center
// server.post('/api/messages', (req, res, _) => {
// res.header('Content-Type', 'application/json');
// res.status(500).send({ error: 'There was a problem sending the message!' });
// });
// Responses
// =============================================================================
server.post('/api/conversations/:conversationId/surveys/:interactionId/responses', (req, res, _) => {
res.header('Content-Type', 'application/javascript');
res.status(201);
res.send();
});
// Uncomment to test error scenario on submitting a survey
// server.post('/api/conversations/:convId/surveys/:surveyId/responses', (req, res, _) => {
// res.header('Content-Type', 'application/json');
// res.status(500).send('There was a problem submiting the survey!');
// });
// Server Setup
// =============================================================================
server.listen(3001, () => {
// eslint-disable-next-line no-console
console.log('JSON Server is running!');
// eslint-disable-next-line no-console
console.log(' - HTTP: http://127.0.0.1:3001/');
});
const keyFile = path.join(__dirname, 'server', 'certs', 'localhost+2-key.pem');
const certFile = path.join(__dirname, 'server', 'certs', 'localhost+2.pem');
if (fs.existsSync(keyFile) && fs.existsSync(certFile)) {
https
.createServer(
{
key: fs.readFileSync(keyFile),
cert: fs.readFileSync(certFile),
},
server
)
.listen(3002, () => {
// eslint-disable-next-line no-console
console.log(' - HTTPS: https://127.0.0.1:3002/');
});
}