Skip to content

Commit de091c0

Browse files
committed
refactor: Add switch case for subroutes
1 parent 334fe5a commit de091c0

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/handler.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@ if (db.hasFeature('subscribe')) {
1313
sseHandler(sse);
1414
}
1515

16-
module.exports = async function(req, res) {
17-
const { pathname, query } = url.parse(req.url, /* parseQueryString */ true);
18-
19-
if (pathname === '/_realtime') {
20-
if (sse) {
21-
sse.addClient(req, res);
22-
} else {
23-
send(res, 400, {
24-
error: 'The current database adapter does not support live updates.',
25-
});
26-
}
16+
function realtimeHandler(req, res) {
17+
if (sse) {
18+
sse.addClient(req, res);
19+
} else {
20+
send(res, 400, {
21+
error: 'The current database adapter does not support live updates.',
22+
});
2723
}
24+
}
2825

26+
async function analyticsHandler(req, res) {
27+
const { pathname, query } = url.parse(req.url, /* parseQueryString */ true);
2928
res.setHeader('Access-Control-Allow-Origin', '*');
3029
// Send all views down if "?all" is true
3130
if (String(query.all) === 'true') {
@@ -72,4 +71,16 @@ module.exports = async function(req, res) {
7271
console.log(err);
7372
throw createError(500, 'Internal server error.');
7473
}
74+
}
75+
76+
module.exports = async function(req, res) {
77+
const { pathname, query } = url.parse(req.url, /* parseQueryString */ true);
78+
79+
switch (pathname) {
80+
case '/_realtime':
81+
return realtimeHandler(req, res);
82+
83+
default:
84+
return analyticsHandler(req, res);
85+
}
7586
};

0 commit comments

Comments
 (0)