-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathrouter.ts
More file actions
62 lines (54 loc) · 1.85 KB
/
router.ts
File metadata and controls
62 lines (54 loc) · 1.85 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
import { Router } from "express";
import {
globalHistory,
globalInformation,
historyPerCountry,
informationPerCountry,
} from "../utils/handlers";
import handleAsync from "./handleAsync";
/**
* 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;
console.log(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));
})
);
router.get(
"/history/:country/:mode?",
handleAsync(async (req, res, next) => {
const country = req.params.country;
console.log("eere");
// 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));
})
);
router.get(
"/:country",
handleAsync(async (req, res, _next) => {
const country = req.params.country;
res.send(await informationPerCountry(country));
})
);
router.get(
"/",
handleAsync(async (_req, res, _next) => {
res.send(await globalInformation());
})
);