Skip to content

Commit 6a1a482

Browse files
authored
Update routes to be more testable (#161)
* Update routes * Refactor routes imports to use default
1 parent 131575f commit 6a1a482

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+2386
-3170
lines changed

.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ EMAIL_TEMPLATE_PASSWORDRESET_RESETLINK=http://localhost:3000/auth/password-reset
2424
EMAIL_TEMPLATE_VERIFYUSER_VERIFYLINK=http://localhost:3000/auth/verify?token={token}
2525

2626
DB_HOST=
27+
DB_ROOT_HOST=
2728
DB_PORT=
2829
DB_NAME=
2930
DB_AUTH_USER=

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ yarn-error.log
88
.DS_Store
99
secret.txt
1010

11+
.docker
1112
.terraform/
1213
*.tfvars
1314
*.tfstate

src/app.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ import connectFlash from "connect-flash";
1414
import { PugMiddleware } from "./middleware/pugMiddleware";
1515
import AppDataSource from "./database/dataSources/appDataSource";
1616

17-
import { AuthRouter } from "./routes/auth";
18-
import { DashboardRouter } from "./routes/dashboard";
19-
import { IndexRouter } from "./routes";
20-
import SettingsRouter from "./routes/settingsRouter";
21-
import ItemsRouter from "./routes/itemsRouter";
22-
import ItemPurchasesRouter from "./routes/itemPurchasesRouter";
23-
import SuppliesRouters from "./routes/suppliesRouter";
24-
import SupplyPurchasesRouter from "./routes/SupplyPurchasesRouter";
25-
import ListingsRouter from "./routes/listingsRouter";
26-
import OrdersRouter from "./routes/ordersRouter";
27-
import StorageRouter from "./routes/storageRouter";
28-
import ReturnsRouter from "./routes/ReturnsRouter";
29-
import PostagePolicyRouter from "./routes/postagePolicyRouter";
17+
import AuthRouter from "./routes/auth";
18+
import DashboardRouter from "./routes/dashboard";
19+
import IndexRouter from "./routes/index";
20+
import SettingsRouter from "./routes/settings";
21+
import ItemsRouter from "./routes/items";
22+
import ItemPurchasesRouter from "./routes/itemPurchases";
23+
import SuppliesRouters from "./routes/supplies";
24+
import SupplyPurchasesRouter from "./routes/supplyPurchases";
25+
import ListingsRouter from "./routes/listings";
26+
import OrdersRouter from "./routes/orders";
27+
import StorageRouter from "./routes/storage";
28+
import ReturnsRouter from "./routes/returns";
29+
import PostagePolicyRouter from "./routes/postagePolicy";
3030

3131
export class App {
3232
private _app: Express;

src/contracts/Page.ts

+6-25
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
1-
import { Router } from "express";
1+
import { NextFunction, Request, Response } from "express";
22

3-
interface IPage {
4-
Route(): any;
5-
OnGet(): any;
6-
OnPost(): any;
7-
}
3+
export default interface Page {
4+
OnGet?(req: Request, res: Response, next: NextFunction): void;
5+
OnPost?(req: Request, res: Response, next: NextFunction): void;
86

9-
export class Page implements IPage {
10-
private _router: Router;
11-
12-
constructor(router: Router) {
13-
this._router = router;
14-
}
15-
16-
get router() {
17-
return this._router;
18-
}
19-
20-
Route() {
21-
this.OnGet();
22-
this.OnPost();
23-
}
24-
25-
OnGet() {}
26-
27-
OnPost() {}
7+
OnGetAsync?(req: Request, res: Response, next: NextFunction): Promise<void>;
8+
OnPostAsync?(req: Request, res: Response, next: NextFunction): Promise<void>;
289
}

src/contracts/Route.ts

+58-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,71 @@
1-
import { Router } from "express"
1+
import { NextFunction, Router, Request, Response, response } from "express"
2+
import Page from "./Page";
3+
import { UserMiddleware } from "../middleware/userMiddleware";
24

3-
interface IRoute {
4-
Route(): Router;
5-
}
6-
7-
export class Route implements IRoute {
5+
export class Route {
86
private _router: Router;
7+
private _pages: { path: string, page: Page, authorise: boolean, adminAuthorise: boolean }[];
8+
private _subRoutes: { path: string, route: Route }[];
9+
10+
private _authorise: boolean;
11+
private _adminAuthorise: boolean;
912

10-
constructor() {
13+
constructor(authorise: boolean = false, adminAuthorise: boolean = false) {
1114
this._router = Router();
15+
this._pages = [];
16+
this._subRoutes = [];
17+
18+
this._authorise = authorise;
19+
this._adminAuthorise = adminAuthorise;
1220
}
1321

1422
get router() {
1523
return this._router;
1624
}
1725

26+
public AddPage(path: string, page: Page, authorise: boolean = false, adminAuthorise: boolean = false) {
27+
this._pages.push({ path, page, authorise, adminAuthorise});
28+
}
29+
30+
public AddSubRoute(path: string, route: Route) {
31+
this._subRoutes.push({ path, route });
32+
}
33+
1834
Route(): Router {
35+
for (let page of this._pages) {
36+
if (page.page.OnGet) {
37+
this._router.get(page.path, (req, res, next) => this.DoAuthorise(this._authorise || page.authorise, req, res, next), (req, res, next) => this.DoAdminAuthorise(this._adminAuthorise || page.adminAuthorise, req, res, next), page.page.OnGet);
38+
} else if (page.page.OnGetAsync) {
39+
this._router.get(page.path, (req, res, next) => this.DoAuthorise(this._authorise || page.authorise, req, res, next), (req, res, next) => this.DoAdminAuthorise(this._adminAuthorise || page.adminAuthorise, req, res, next), page.page.OnGetAsync);
40+
}
41+
42+
if (page.page.OnPost) {
43+
this._router.post(page.path, (req, res, next) => this.DoAuthorise(this._authorise || page.authorise, req, res, next), (req, res, next) => this.DoAdminAuthorise(this._adminAuthorise || page.adminAuthorise, req, res, next), page.page.OnPost);
44+
} else if (page.page.OnPostAsync) {
45+
this._router.post(page.path, (req, res, next) => this.DoAuthorise(this._authorise || page.authorise, req, res, next), (req, res, next) => this.DoAdminAuthorise(this._adminAuthorise || page.adminAuthorise, req, res, next), page.page.OnPostAsync);
46+
}
47+
}
48+
49+
for (let subRoute of this._subRoutes) {
50+
this._router.use(subRoute.path, subRoute.route.Route());
51+
}
52+
1953
return this._router;
2054
}
21-
}
55+
56+
private DoAuthorise(authorise: boolean, req: Request, res: Response, next: NextFunction) {
57+
if (authorise) {
58+
UserMiddleware.Authorise(req, res, next);
59+
} else {
60+
next();
61+
}
62+
}
63+
64+
private DoAdminAuthorise(adminAuthorise: boolean, req: Request, res: Response, next: NextFunction) {
65+
if (adminAuthorise) {
66+
UserMiddleware.AdminAuthorise(req, res, next);
67+
} else {
68+
next();
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)