forked from OSSPhilippines/covid19-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainRouter.ts
More file actions
51 lines (43 loc) · 1.98 KB
/
mainRouter.ts
File metadata and controls
51 lines (43 loc) · 1.98 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
import { Router } from "express";
import { quietBasicMiddleware } from "../middlewares/quietBasicMiddleware";
import { Mode } from "../types/schema";
import {
countryData,
countryHistorical,
globalData,
globalHistorical,
} from "../utils/main";
import { RouterWrapper, RouterWrapperOptions } from "../utils/RouterWrapper";
export const modes: Mode[] = ["cases", "deaths", "recovered"];
export const modeNotFoundError = `Mode not found. Try <${modes.join(" | ")}>`;
export const plainPrefixes = ["/plain", "/basic", "/cmd"];
// main router that the project uses
export const mainRouter = Router();
const wrapperOptions: RouterWrapperOptions = {
addQuiet: true,
onlyPrefixPrepended: false,
};
let { register } = new RouterWrapper(mainRouter, plainPrefixes, wrapperOptions);
mainRouter.use(quietBasicMiddleware); // determines if the request is querying for basic or quiet mode
// please do not change the order that the routes are registered in as it matters
register("/history/:id/:mode?", async (req, res) => {
let { id, mode } = req.params;
if (modes.includes(id as Mode)) return; // the id is a mode so it must go through the global handler
if (mode === undefined) mode = "cases"; // set to cases by default
if (modes.includes(mode) === false) throw new Error(modeNotFoundError);
// prettier-ignore
res.locals.data = await countryHistorical(res.locals.callback, { id, mode });
});
register("/history/:mode?", async (req, res) => {
let { id, mode } = req.params;
if (mode === undefined) mode = "cases"; // set to cases by default
if (modes.includes(mode) === false) throw new Error(modeNotFoundError);
// prettier-ignore
res.locals.data = await globalHistorical(res.locals.callback, { id, mode });
});
register("/:id?", async (req, res) => {
let { id, mode } = req.params;
let mainFunction = id !== undefined ? countryData : globalData;
// prettier-ignore
res.locals.data = await mainFunction(res.locals.callback, {id, mode})
});