forked from OSSPhilippines/covid19-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.ts
More file actions
69 lines (59 loc) · 2.04 KB
/
router.ts
File metadata and controls
69 lines (59 loc) · 2.04 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
62
63
64
65
66
67
68
69
import { Request, Router } from "express";
import {
globalHistory,
globalInformation,
historyPerCountry,
informationPerCountry,
} from "../utils/handlers";
import handleAsync from "./handleAsync";
/**
*
* @param req Express request
* @returns Boolean if the request starts with /quiet
*/
export const isQuiet: (req: Request) => boolean = (req) =>
req.baseUrl.startsWith("/quiet");
/**
* The rootRouter handles all the processing of the requests *after* passing through
* all middlewares except not found and error handling middleware
*/
export const router = Router({ mergeParams: true });
// rootRouter.get("/history/:country/:type", historyPerCountryAndType);
router.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 globalHistory(mode, isQuiet(req)));
})
);
router.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 historyPerCountry(country, mode, isQuiet(req)));
})
);
router.get(
"/:country",
handleAsync(async (req, res, _next) => {
const country = req.params.country;
res.send(await informationPerCountry(country, isQuiet(req)));
})
);
router.get(
"/",
handleAsync(async (req, res, _next) => {
res.send(await globalInformation(isQuiet(req)));
})
);