Skip to content
This repository was archived by the owner on Apr 19, 2023. It is now read-only.

Commit 335e7ee

Browse files
✨ Add better routing
1 parent c6651f1 commit 335e7ee

File tree

5 files changed

+64
-28
lines changed

5 files changed

+64
-28
lines changed

src/helpers/middleware.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import { Request, Response, NextFunction } from "express";
2+
import { HTTPError } from "../interfaces/general";
23

3-
export const errorHandler = (error: string) => {
4+
export const errorHandler = (
5+
error: any,
6+
req: Request,
7+
res: Response,
8+
next: NextFunction
9+
) => {
10+
const response: HTTPError = safeError(error.toString());
11+
res.status(response.status);
12+
res.json({ error: response.code, message: response.message });
13+
};
14+
15+
export const safeError = (error: string) => {
416
const errorString = error.toString();
517
if (errorString.startsWith("JsonWebTokenError"))
618
return { status: 401, code: "invalid-token" };

src/index.ts

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,13 @@
1-
import express, { Request, Response, NextFunction } from "express";
1+
import express from "express";
22
import { PORT } from "./config";
3-
import { register, verifyEmail } from "./rest/auth";
4-
import asyncHandler from "express-async-handler";
5-
import { HTTPError } from "./interfaces/general";
63
import { errorHandler, trackingHandler } from "./helpers/middleware";
4+
import { router } from "./routes";
75

86
const app = express();
97

108
app.use(trackingHandler);
9+
router(app);
1110

12-
app.get("/", (req, res) => res.json({ hello: "world" }));
13-
14-
app.put("/user", async (req, res) => {
15-
const user = req.body;
16-
const email = user.email;
17-
delete user.email;
18-
const users = await register(user, email);
19-
res.json({ success: true, users });
20-
});
21-
22-
app.get(
23-
"/verify-email/:token",
24-
asyncHandler(async (req, res) => {
25-
await verifyEmail(req.params.token, res.locals);
26-
res.json({ success: true });
27-
})
28-
);
29-
30-
app.use((error: any, req: Request, res: Response, next: NextFunction) => {
31-
const response: HTTPError = errorHandler(error.toString());
32-
res.status(response.status);
33-
res.json({ error: response.code, message: response.message });
34-
});
11+
app.use(errorHandler);
3512

3613
app.listen(PORT, () => console.log("App running"));

src/routes/emails.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Request, Response } from "express";
2+
import { verifyEmail } from "../rest/auth";
3+
4+
export const routeEmailVerify = async (req: Request, res: Response) => {
5+
await verifyEmail(req.body.token || req.params.token, res.locals);
6+
res.json({ success: true });
7+
};

src/routes/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Application } from "express";
2+
import asyncHandler from "express-async-handler";
3+
import { routeUserMe, routeUserPut } from "./users";
4+
import { routeEmailVerify } from "./emails";
5+
6+
export const router = (app: Application) => {
7+
app.get("/", (req, res) => res.json({ hello: "world" }));
8+
9+
routesUser(app);
10+
routesEmail(app);
11+
12+
return app;
13+
};
14+
15+
const routesUser = (app: Application) => {
16+
app.get("/users/me", asyncHandler(routeUserMe));
17+
app.put("/users", asyncHandler(routeUserPut));
18+
};
19+
20+
const routesEmail = (app: Application) => {
21+
app.post("/emails/verify", asyncHandler(routeEmailVerify));
22+
};

src/routes/users.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Request, Response } from "express";
2+
import { register } from "../rest/auth";
3+
4+
export const routeUserMe = async (req: Request, res: Response) => {
5+
const user = req.body;
6+
const email = user.email;
7+
delete user.email;
8+
const users = await register(user, email);
9+
res.json({ success: true, users });
10+
};
11+
12+
export const routeUserPut = async (req: Request, res: Response) => {
13+
const user = req.body;
14+
const email = user.email;
15+
delete user.email;
16+
const users = await register(user, email);
17+
res.json({ success: true, users });
18+
};

0 commit comments

Comments
 (0)