Skip to content

Commit f5c2abe

Browse files
author
scinorandex
committed
Add documentation and quiet mode to plain routes
1 parent 11708da commit f5c2abe

File tree

5 files changed

+107
-49
lines changed

5 files changed

+107
-49
lines changed

src/api.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ const app = express();
1212
app.use(morgan("common"));
1313
app.use(userAgentMiddleware);
1414

15+
/**
16+
* Plain CMD/Basic routes have both quiet and full modes
17+
* Same with regular / routes with ansi color codes
18+
*/
19+
app.use(["/quiet/basic", "/quiet/cmd", "/quiet/plain"], plainRouter);
1520
app.use(["/basic", "/cmd", "/plain"], plainRouter);
21+
1622
app.use(["/quiet", "/"], router);
1723
app.use("/", errorHandler);
1824

25+
// Not found handler
1926
app.use("*", (_req, res) =>
2027
res.send(
2128
`Welcome to COVID-19 Tracker CLI v${version} by Waren Gonzaga with Wareneutron Developers\n

src/api/plainRouter.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
historyPerCountryPlain,
77
globalHistoryPlain,
88
} from "../utils/plainHandlers";
9+
import { isQuiet } from "./router";
10+
911
export const plainRouter = Router({ mergeParams: true });
1012

1113
plainRouter.get(
@@ -19,7 +21,7 @@ plainRouter.get(
1921

2022
// if the mode is not in the api then return to next handler
2123
if (!["cases", "deaths", "recovered"].includes(mode)) return next();
22-
res.send(await globalHistoryPlain(mode));
24+
res.send(await globalHistoryPlain(mode, isQuiet(req)));
2325
})
2426
);
2527

@@ -35,21 +37,21 @@ plainRouter.get(
3537

3638
// if the mode is not in the api then return to next handler
3739
if (!["cases", "deaths", "recovered"].includes(mode)) return next();
38-
res.send(await historyPerCountryPlain(country, mode));
40+
res.send(await historyPerCountryPlain(country, mode, isQuiet(req)));
3941
})
4042
);
4143

4244
plainRouter.get(
4345
"/:country",
4446
handleAsync(async (req, res, _next) => {
4547
const country = req.params.country;
46-
res.send(await informationPerCountryPlain(country));
48+
res.send(await informationPerCountryPlain(country, isQuiet(req)));
4749
})
4850
);
4951

5052
plainRouter.get(
5153
"/",
52-
handleAsync(async (_req, res, _next) => {
53-
res.send(await globalInformationPlain());
54+
handleAsync(async (req, res, _next) => {
55+
res.send(await globalInformationPlain(isQuiet(req)));
5456
})
5557
);

src/api/router.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Router } from "express";
1+
import { Request, Router } from "express";
22
import {
33
globalHistory,
44
globalInformation,
@@ -7,6 +7,14 @@ import {
77
} from "../utils/handlers";
88
import handleAsync from "./handleAsync";
99

10+
/**
11+
*
12+
* @param req Express request
13+
* @returns Boolean if the request starts with /quiet
14+
*/
15+
export const isQuiet: (req: Request) => boolean = (req) =>
16+
req.baseUrl.startsWith("/quiet");
17+
1018
/**
1119
* The rootRouter handles all the processing of the requests *after* passing through
1220
* all middlewares except not found and error handling middleware
@@ -25,7 +33,7 @@ router.get(
2533

2634
// if the mode is not in the api then return to next handler
2735
if (!["cases", "deaths", "recovered"].includes(mode)) return next();
28-
res.send(await globalHistory(mode, req.baseUrl.startsWith("/quiet")));
36+
res.send(await globalHistory(mode, isQuiet(req)));
2937
})
3038
);
3139

@@ -41,32 +49,21 @@ router.get(
4149

4250
// if the mode is not in the api then return to next handler
4351
if (!["cases", "deaths", "recovered"].includes(mode)) return next();
44-
res.send(
45-
await historyPerCountry(
46-
country,
47-
mode,
48-
req.baseUrl.startsWith("/quiet")
49-
)
50-
);
52+
res.send(await historyPerCountry(country, mode, isQuiet(req)));
5153
})
5254
);
5355

5456
router.get(
5557
"/:country",
5658
handleAsync(async (req, res, _next) => {
5759
const country = req.params.country;
58-
res.send(
59-
await informationPerCountry(
60-
country,
61-
req.baseUrl.startsWith("/quiet")
62-
)
63-
);
60+
res.send(await informationPerCountry(country, isQuiet(req)));
6461
})
6562
);
6663

6764
router.get(
6865
"/",
6966
handleAsync(async (req, res, _next) => {
70-
res.send(await globalInformation(req.baseUrl.startsWith("/quiet")));
67+
res.send(await globalInformation(isQuiet(req)));
7168
})
7269
);

src/utils/generatePlainOutput.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ const { version } = require("../../package.json");
66
export const generatePlainOutput: (
77
info: PlainData,
88
chartType: string,
9+
quiet: boolean,
910
extraRows?: string[]
10-
) => string = ({ data, metainfo }, chartType, extraRows) => {
11+
) => string = ({ data, metainfo }, chartType, quiet, extraRows) => {
1112
// Set line depending if it contains a chart or not
1213
let line = extraRows === undefined ? "-".repeat(60) : "-".repeat(68);
1314
line += "\n";
@@ -43,10 +44,11 @@ export const generatePlainOutput: (
4344
if (normalizedArray.length !== 0) table += `\n`; // do not add whitespace at the end of the table
4445
}
4546

46-
/**
47-
* responseArray is the array of the raw data **before** adding the separator lines
48-
*/
49-
let responseArray: string[] = [header, timestamp, table];
47+
// responseArray is the array of the raw data **before** adding the separator lines
48+
49+
let responseArray: string[] = [timestamp, table];
50+
51+
if (!quiet) responseArray.unshift(header);
5052

5153
// Add extraRows to responseArray
5254
if (extraRows !== undefined) {
@@ -56,14 +58,22 @@ export const generatePlainOutput: (
5658
}
5759

5860
// Add the help msg and other messages
59-
responseArray = responseArray.concat([
60-
"Help: Try to append the URL with /help to learn more...",
61-
"Source: https://www.worldometers.info/coronavirus/",
62-
"Code: https://github.com/warengonzaga/covid19-tracker-cli",
63-
`\n${saying}\n`,
64-
`Love this project? Help us to help others by means of coffee!\n${GCashMessage}(Buy Me A Coffee) warengonza.ga/coffee4dev`,
65-
"Follow me on twitter for more updates!\n@warengonzaga #covid19trackercli",
66-
]);
61+
if (!quiet)
62+
responseArray = responseArray.concat([
63+
"Help: Try to append the URL with /help to learn more...",
64+
"Source: https://www.worldometers.info/coronavirus/",
65+
"Code: https://github.com/warengonzaga/covid19-tracker-cli",
66+
`\n${saying}\n`,
67+
]);
68+
69+
responseArray.push(
70+
`Love this project? Help us to help others by means of coffee!\n${GCashMessage}(Buy Me A Coffee) warengonza.ga/coffee4dev`
71+
);
72+
73+
if (!quiet)
74+
responseArray.push(
75+
`Follow me on twitter for more updates!\n@warengonzaga #covid19trackercli`
76+
);
6777

6878
// Construct the final output
6979
let response: string = "\n";

src/utils/plainHandlers.ts

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ import { generateAsciichart } from "./generateAsciichart";
33
import { generatePlainOutput } from "./generatePlainOutput";
44
import { getAllInfo, getCountryInfo, PlainData } from "./getInformation";
55

6-
export const globalHistoryPlain: (mode: string) => Promise<string> = async (
7-
mode
8-
) => {
6+
/**
7+
* globalHistory shows a tablechart of the cases of all the countries
8+
* Shows Cases, Deaths, Recovered, Active, Cases/Million
9+
* and a graph of a country's cases
10+
* @param mode Mode that the user wants to query, must be: "cases" | "deaths" | "recoveries"
11+
* @param quiet tells the response to be in quiet mode or not
12+
*/
13+
export const globalHistoryPlain: (
14+
mode: string,
15+
quiet: boolean
16+
) => Promise<string> = async (mode, quiet) => {
917
// Get summary info
1018
const info = (await getAllInfo(true)) as PlainData;
1119

@@ -24,13 +32,26 @@ export const globalHistoryPlain: (mode: string) => Promise<string> = async (
2432
// Generate historical graph
2533
const chart = generateAsciichart(Object.values(data), true, 7);
2634

27-
return generatePlainOutput(info, `Global Historical Chart`, [date, chart]);
35+
return generatePlainOutput(info, `Global Historical Chart`, quiet, [
36+
date,
37+
chart,
38+
]);
2839
};
2940

41+
/**
42+
* historyPerCountry shows a tablechart of the <mode> of a country
43+
* Shows Cases, Deaths, Recovered, Active, Cases/Million
44+
* Today Cases, Today Deaths, Critical, Mortality %, Recovery in a chart
45+
* @param country country code or country name that the user wants to query
46+
* @param mode Mode that the user wants to query, must be: "cases" | "deaths" | "recoveries"
47+
* @param quiet tells the response to be in quiet mode or not
48+
*/
49+
3050
export const historyPerCountryPlain: (
3151
country: string,
32-
mode: string
33-
) => Promise<string> = async (country, mode) => {
52+
mode: string,
53+
quiet: boolean
54+
) => Promise<string> = async (country, mode, quiet) => {
3455
// Get summary info about a country
3556
const info = (await getCountryInfo(country, true)) as PlainData;
3657

@@ -49,20 +70,41 @@ export const historyPerCountryPlain: (
4970
// Generate historical graph
5071
const chart = generateAsciichart(Object.values(data), true, 7);
5172

52-
return generatePlainOutput(info, `${info.metainfo.countryName} Chart`, [
53-
date,
54-
chart,
55-
]);
73+
return generatePlainOutput(
74+
info,
75+
`${info.metainfo.countryName} Chart`,
76+
quiet,
77+
[date, chart]
78+
);
5679
};
5780

81+
/**
82+
* informationPerCountry tracks the info of a country
83+
* Shows Cases, Deaths, Recovered, Active, Cases/Million
84+
* Today Cases, Today Deaths, Critical, Mortality %, Recovery in a chart
85+
* @param country country code or country name that the user wants to query
86+
* @param quiet tells the response to be in quiet mode or not
87+
*/
5888
export const informationPerCountryPlain: (
59-
country: string
60-
) => Promise<string> = async (country) => {
89+
country: string,
90+
quiet: boolean
91+
) => Promise<string> = async (country, quiet) => {
6192
const info = (await getCountryInfo(country, true)) as PlainData;
62-
return generatePlainOutput(info, `${info.metainfo.countryName} Update`);
93+
return generatePlainOutput(
94+
info,
95+
`${info.metainfo.countryName} Update`,
96+
quiet
97+
);
6398
};
6499

65-
export const globalInformationPlain: () => Promise<string> = async () => {
100+
/**
101+
* globalInformation tracks the info of all countries
102+
* Shows Cases, Deaths, Recovered, Mortality %, Recovered% in a chart
103+
* @param quiet tells the response to be in quiet mode or not
104+
*/
105+
export const globalInformationPlain: (
106+
quiet: boolean
107+
) => Promise<string> = async (quiet) => {
66108
const info = (await getAllInfo(true)) as PlainData;
67-
return generatePlainOutput(info, "Global Update");
109+
return generatePlainOutput(info, "Global Update", quiet);
68110
};

0 commit comments

Comments
 (0)