forked from OSSPhilippines/covid19-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplainRouter.ts
More file actions
61 lines (52 loc) · 1.86 KB
/
plainRouter.ts
File metadata and controls
61 lines (52 loc) · 1.86 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
import { Router } from "express";
import handleAsync from "./handleAsync";
import {
globalInformationPlain,
informationPerCountryPlain,
historyPerCountryPlain,
globalHistoryPlain,
} from "../utils/plainHandlers";
import { isQuiet } from "./router";
/**
* The plainRouter handles all the plain routes such as /basic, /cmd, and /plain
* It also handles the quiet version of these routes
*/
export const plainRouter = Router({ mergeParams: true });
plainRouter.get(
"/history/:mode?",
handleAsync(async (req, res, next) => {
// get mode from params
let mode = req.params.mode as "cases" | "deaths" | "recovered";
//default to cases if mode is undefined
mode = mode === undefined ? "cases" : mode;
// if the mode is not in the api then return to next handler
if (!["cases", "deaths", "recovered"].includes(mode)) return next();
res.send(await globalHistoryPlain(mode, isQuiet(req)));
})
);
plainRouter.get(
"/history/:country/:mode?",
handleAsync(async (req, res, next) => {
const country = req.params.country;
// get mode from params
let mode = req.params.mode as "cases" | "deaths" | "recovered";
//default to cases if mode is undefined
mode = mode === undefined ? "cases" : mode;
// if the mode is not in the api then return to next handler
if (!["cases", "deaths", "recovered"].includes(mode)) return next();
res.send(await historyPerCountryPlain(country, mode, isQuiet(req)));
})
);
plainRouter.get(
"/:country",
handleAsync(async (req, res, _next) => {
const country = req.params.country;
res.send(await informationPerCountryPlain(country, isQuiet(req)));
})
);
plainRouter.get(
"/",
handleAsync(async (req, res, _next) => {
res.send(await globalInformationPlain(isQuiet(req)));
})
);