forked from OSSPhilippines/covid19-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquietBasicMiddleware.ts
More file actions
22 lines (19 loc) · 910 Bytes
/
quietBasicMiddleware.ts
File metadata and controls
22 lines (19 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { plainHandler } from "../handlers/plainHandler";
import { regularHandler } from "../handlers/regularHandler";
import { plainPrefixes } from "../routers/mainRouter";
import { Middleware } from "./notFoundHandler";
export const quietBasicMiddleware: Middleware = (req, { locals }, next) => {
let path = req.path;
let quiet: boolean = path.startsWith("/quiet");
if (quiet) path = path.replace("/quiet", "");
let plain: boolean = false; // assume that it is a regular handler
plainPrefixes.forEach((prefix) => {
if (plain) return; // might save some cpu time not having to recheck.
if (path.startsWith(prefix)) {
plain = true;
}
});
let curryFunc = plain ? plainHandler : regularHandler; // get the curry function based on the plain status
locals.callback = curryFunc(quiet); // get the callback function from curryfunc
next();
};