Skip to content

Commit a82a1ae

Browse files
author
scinorandex
committed
Add and improve documentation
1 parent f5c2abe commit a82a1ae

File tree

9 files changed

+40
-6
lines changed

9 files changed

+40
-6
lines changed

src/api/errorHandler.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { ErrorRequestHandler } from "express";
22

3+
/**
4+
*
5+
* @param error Error object, received from errors thrown in the code
6+
* @param res Response object from Express
7+
*/
38
export const errorHandler: ErrorRequestHandler = (error, _, res, _next) => {
49
const statusCode = res.statusCode === 200 ? 500 : res.statusCode;
510
res.status(statusCode);

src/api/handleAsync.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { Request, Response, NextFunction } from "express";
22
type Handler<R> = (req: Request, res: Response, next: NextFunction) => R;
3+
4+
/**
5+
* @example router.use("/path/", handleAsync((req, res, next)=>{res.send("Hello World!")}));
6+
* @param asyncFn An asyncronous function that takes in req, res, and next
7+
* @returns An asyncronous function where errors will be catched and sent to the error handler
8+
*/
39
const handleAsync: (asyncFn: Handler<Promise<void>>) => Handler<void> = (
410
asyncFn
511
) => {

src/api/plainRouter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import {
88
} from "../utils/plainHandlers";
99
import { isQuiet } from "./router";
1010

11+
/**
12+
* The plainRouter handles all the plain routes such as /basic, /cmd, and /plain
13+
* It also handles the quiet version of these routes
14+
*/
1115
export const plainRouter = Router({ mergeParams: true });
1216

1317
plainRouter.get(

src/api/userAgent.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Request, Response, NextFunction } from "express";
2+
const { version } = require("../../package.json");
23

34
// Type of middleware and handler
45
export type Handler = (
@@ -7,17 +8,28 @@ export type Handler = (
78
next: NextFunction
89
) => Promise<void> | void;
910

11+
/**
12+
*
13+
* @param userAgent The user agent of the requester
14+
* @returns A boolean that is true of the user agent provided is from curl / wget / httpie
15+
*/
1016
const isTerminal: (userAgent: string | undefined) => boolean = (userAgent) => {
1117
if (userAgent === undefined) return false;
1218
if (/curl|wget|httpie/i.test(userAgent)) return true;
1319
return false;
1420
};
1521

1622
export const userAgentMiddleware: Handler = (req, res, next) => {
23+
/**
24+
* Get the user agent from the request
25+
* Determine if the user agent is from curl / wget / httpie
26+
* If true then proceed using the next function
27+
* Else return with message
28+
*/
1729
const userAgent = req.headers["user-agent"];
1830
if (!isTerminal(userAgent)) {
1931
res.send(
20-
`Welcome to COVID-19 Tracker CLI v3.9.3 by Waren Gonzaga.\n\nPlease visit: https://warengonza.ga/covid19-tracker-cli`
32+
`Welcome to COVID-19 Tracker CLI v${version} by Waren Gonzaga with Wareneutron Developers\nPlease visit: https://warengonza.ga/covid19-tracker-cli\n`
2133
);
2234
return;
2335
}

src/utils/generatePlainOutput.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import { getSaying } from "./getSaying";
33
import { getTimestamp } from "./getTimestamp";
44
const { version } = require("../../package.json");
55

6+
/**
7+
* @param info The plain data that will be shown at the top in two columns
8+
* @param chartType The type of chart that will be shown. Ex: "Global Update", "Philippine Historical Chart"
9+
* @param quiet Boolean, set to true if the user requsted quiet mode
10+
* @param extraRows Any extra rows that will be presented under the main info. Used for Asciichart
11+
* @returns A string showing the provided data and configuration
12+
*/
613
export const generatePlainOutput: (
714
info: PlainData,
815
chartType: string,
@@ -45,9 +52,7 @@ export const generatePlainOutput: (
4552
}
4653

4754
// responseArray is the array of the raw data **before** adding the separator lines
48-
4955
let responseArray: string[] = [timestamp, table];
50-
5156
if (!quiet) responseArray.unshift(header);
5257

5358
// Add extraRows to responseArray

src/utils/generateTable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ const separatorHandler: (
176176
);
177177
}
178178

179-
return "If you have reached this then something has gone wrong";
179+
throw new Error("Separator handler conditions failed");
180180
};
181181

182182
/**

src/utils/getInformation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const getAllInfo: (
6262
* @param country the country code or string that the user provides from req.params or CLI
6363
* @param isPlain Set to true to recieve an object containing the responses instead of the rows
6464
* @returns an object containing the data and metainfo **if isPlain is set to true**
65-
* @returns an array in the format of [timestamp, API countryname, formal countryname, rows[]] **if isPlain is false
65+
* @returns an array in the format of [timestamp, API countryname, formal countryname, rows[]] **if isPlain is false**
6666
*/
6767
export const getCountryInfo: (
6868
country: string,

src/utils/getSaying.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ const sayings = [
3333
"Wine is the most healthful and most hygienic of beverages - Louis Pasteur",
3434
];
3535

36+
/**
37+
* @returns A random saying from the array of sayings
38+
*/
3639
export const getSaying: () => string = () => {
3740
const index = Math.floor(Math.random() * sayings.length);
3841
return sayings[index];

src/utils/getTimestamp.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/**
2-
*
32
* @param timestamp Timestamp in Epoch Time
43
* @returns String in form of As of MM/DD/YYYY, HH:mm:SS AM/PM [Date: MM/DD/YYYY]
54
*/

0 commit comments

Comments
 (0)