forked from OSSPhilippines/covid19-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboardRouter.ts
More file actions
54 lines (46 loc) · 1.72 KB
/
dashboardRouter.ts
File metadata and controls
54 lines (46 loc) · 1.72 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
import { Request, Router } from "express";
import {
globalDashboard,
DashboardSize,
countryDashboard,
} from "../utils/routes/dashboard/dashboardHandlers";
import handleAsync from "./handleAsync";
import { isTerminal } from "./userAgent";
export const dashboardRouter = Router({ mergeParams: true });
/**
*
* @param req Express request
* @returns True if the request is from a not from wget, curl or httpie
*/
const isWeb: (req: Request) => boolean = (req) => {
// Check if the link is asking for web version of dashboard
const link = req.baseUrl.startsWith("/history/web/charts");
// Check if the user agent is NOT coming from terminal based application
const isNotTerminal = !isTerminal(req.headers["user-agent"]);
return link && isNotTerminal;
};
dashboardRouter.get(
"/:size?",
handleAsync(async (req, res, next) => {
// Get parameters from request
let size = req.params.size as DashboardSize;
// Set default size and check then check if size var matches
if (size === undefined) size = "sm";
if (!["sm", "md", "lg"].includes(size)) return next();
let response = await globalDashboard(size, isWeb(req));
res.send(response);
})
);
dashboardRouter.get(
"/:country/:size?",
handleAsync(async (req, res, next) => {
// Get parameters from request
let country = req.params.country;
let size = req.params.size as DashboardSize;
// Set default size and check then check if size var matches
if (size === undefined) size = "sm";
if (!["sm", "md", "lg"].includes(size)) return next();
let response = await countryDashboard(country, size, isWeb(req));
res.send(response);
})
);