forked from OSSPhilippines/covid19-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouterWrapper.ts
More file actions
38 lines (32 loc) · 1.17 KB
/
RouterWrapper.ts
File metadata and controls
38 lines (32 loc) · 1.17 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
import { Router } from "express";
import handleAsync, { AsyncFn } from "./handleAsync";
export interface RouterWrapperOptions {
addQuiet: boolean; // whether to add the quiet versions or not
onlyPrefixPrepended: boolean; // whether to add the standard strings themselves
}
/**
* Wraps a router to make registering routes easier
*/
export class RouterWrapper {
constructor(
public router: Router,
public prefixes: string[],
public options: RouterWrapperOptions
) {}
/**
* register routes
* @param path path to that route
* @param asyncFn callback
*/
register = (path: string, asyncFn: AsyncFn): void => {
let routes: string[] = [];
// add the base string to routes
if (this.options.onlyPrefixPrepended === false) routes.push(path);
this.prefixes.forEach((prefix) => routes.unshift(prefix + path));
// if addQuiet is true then add quiet versions to all the routes
if (this.options.addQuiet === true)
routes = routes.map((route) => `/quiet${route}`).concat(routes);
// console.log(routes);
this.router.get(routes, handleAsync(asyncFn));
};
}